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.LockscreenCredential;
23 
24 import org.robolectric.annotation.Implementation;
25 import org.robolectric.annotation.Implements;
26 import org.robolectric.annotation.Resetter;
27 
28 /**
29  * Shadow for LockPatternUtils.
30  */
31 @Implements(LockPatternUtils.class)
32 public class ShadowLockPatternUtils {
33 
34     public static final int NO_USER = -1;
35 
36     private static int sPasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
37     private static byte[] sSavedPassword;
38     private static byte[] sSavedPattern;
39     private static LockscreenCredential sClearLockCredential;
40     private static int sClearLockUser = NO_USER;
41 
42 
43     @Resetter
reset()44     public static void reset() {
45         sPasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
46         sSavedPassword = null;
47         sSavedPattern = null;
48         sClearLockCredential = null;
49         sClearLockUser = NO_USER;
50     }
51 
52     /**
53      * Sets the current password quality that is returned by
54      * {@link LockPatternUtils#getKeyguardStoredPasswordQuality}.
55      */
setPasswordQuality(int passwordQuality)56     public static void setPasswordQuality(int passwordQuality) {
57         sPasswordQuality = passwordQuality;
58     }
59 
60     /**
61      * Returns the password saved by a call to {@link LockPatternUtils#saveLockPassword}.
62      */
getSavedPassword()63     public static byte[] getSavedPassword() {
64         return sSavedPassword;
65     }
66 
67     /**
68      * Returns the saved credential passed in to clear the lock, null if it has not been cleared.
69      */
getClearLockCredential()70     public static LockscreenCredential getClearLockCredential() {
71         return sClearLockCredential;
72     }
73 
74     /**
75      * Returns the user passed in to clear the lock, {@link #NO_USER} if it has not been cleared.
76      */
getClearLockUser()77     public static int getClearLockUser() {
78         return sClearLockUser;
79     }
80 
81 
82     /**
83      * Returns the pattern saved by a call to {@link LockPatternUtils#saveLockPattern}.
84      */
getSavedPattern()85     public static byte[] getSavedPattern() {
86         return sSavedPattern;
87     }
88 
89     @Implementation
getKeyguardStoredPasswordQuality(int userHandle)90     protected int getKeyguardStoredPasswordQuality(int userHandle) {
91         return sPasswordQuality;
92     }
93 
94     @Implementation
setLockCredential(LockscreenCredential newCredential, LockscreenCredential savedCredential, int userId)95     public boolean setLockCredential(LockscreenCredential newCredential,
96             LockscreenCredential savedCredential, int userId) {
97         if (newCredential.isPassword() || newCredential.isPin()) {
98             sSavedPassword = newCredential.duplicate().getCredential();
99         } else if (newCredential.isPattern()) {
100             sSavedPattern = newCredential.duplicate().getCredential();
101         } else if (newCredential.isNone()) {
102             sClearLockCredential = savedCredential.duplicate();
103             sClearLockUser = userId;
104         }
105         return true;
106     }
107 }
108