1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.car.settings.testutils;
18 
19 import android.app.admin.DevicePolicyManager;
20 
21 import com.android.internal.widget.LockPatternUtils;
22 import com.android.internal.widget.LockPatternView;
23 
24 import org.robolectric.annotation.Implementation;
25 import org.robolectric.annotation.Implements;
26 import org.robolectric.annotation.Resetter;
27 
28 import java.util.ArrayList;
29 import java.util.List;
30 
31 /**
32  * Shadow for LockPatternUtils.
33  */
34 @Implements(LockPatternUtils.class)
35 public class ShadowLockPatternUtils {
36 
37     public static final int NO_USER = -1;
38 
39     private static int sPasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
40     private static byte[] sSavedPassword;
41     private static List<LockPatternView.Cell> sSavedPattern;
42     private static byte[] sClearLockCredential;
43     private static int sClearLockUser = NO_USER;
44 
45 
46     @Resetter
reset()47     public static void reset() {
48         sPasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
49         sSavedPassword = null;
50         sSavedPattern = null;
51         sClearLockCredential = null;
52         sClearLockUser = NO_USER;
53     }
54 
55     /**
56      * Sets the current password quality that is returned by
57      * {@link LockPatternUtils#getKeyguardStoredPasswordQuality}.
58      */
setPasswordQuality(int passwordQuality)59     public static void setPasswordQuality(int passwordQuality) {
60         sPasswordQuality = passwordQuality;
61     }
62 
63     /**
64      * Returns the password saved by a call to {@link LockPatternUtils#saveLockPassword}.
65      */
getSavedPassword()66     public static byte[] getSavedPassword() {
67         return sSavedPassword;
68     }
69 
70     /**
71      * Returns the saved credential passed in to clear the lock, null if it has not been cleared.
72      */
getClearLockCredential()73     public static byte[] getClearLockCredential() {
74         return sClearLockCredential;
75     }
76 
77     /**
78      * Returns the user passed in to clear the lock, {@link #NO_USER} if it has not been cleared.
79      */
getClearLockUser()80     public static int getClearLockUser() {
81         return sClearLockUser;
82     }
83 
84 
85     /**
86      * Returns the pattern saved by a call to {@link LockPatternUtils#saveLockPattern}.
87      */
getSavedPattern()88     public static List<LockPatternView.Cell> getSavedPattern() {
89         return sSavedPattern;
90     }
91 
92     @Implementation
clearLock(byte[] savedCredential, int userHandle)93     protected void clearLock(byte[] savedCredential, int userHandle) {
94         sClearLockCredential = savedCredential;
95         sClearLockUser = userHandle;
96     }
97 
98     @Implementation
getKeyguardStoredPasswordQuality(int userHandle)99     protected int getKeyguardStoredPasswordQuality(int userHandle) {
100         return sPasswordQuality;
101     }
102 
103     @Implementation
saveLockPassword(byte[] password, byte[] savedPassword, int requestedQuality, int userHandler)104     public void saveLockPassword(byte[] password, byte[] savedPassword, int requestedQuality,
105             int userHandler) {
106         sSavedPassword = password;
107     }
108 
109     @Implementation
saveLockPattern(List<LockPatternView.Cell> pattern, byte[] savedPassword, int userId, boolean allowUntrustedChanges)110     public void saveLockPattern(List<LockPatternView.Cell> pattern, byte[] savedPassword,
111             int userId, boolean allowUntrustedChanges) {
112         sSavedPattern = new ArrayList<>(pattern);
113     }
114 }
115