1 /* 2 * Copyright (C) 2017 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.settings.testutils.shadow; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.app.admin.DevicePolicyManager; 22 import android.app.admin.PasswordMetrics; 23 import android.content.ComponentName; 24 import android.content.Context; 25 import android.content.pm.UserInfo; 26 import android.os.UserHandle; 27 28 import com.android.internal.widget.LockPatternUtils; 29 import com.android.internal.widget.LockscreenCredential; 30 31 import org.robolectric.annotation.Implementation; 32 import org.robolectric.annotation.Implements; 33 import org.robolectric.annotation.Resetter; 34 35 import java.util.HashMap; 36 import java.util.List; 37 import java.util.Map; 38 39 @Implements(LockPatternUtils.class) 40 public class ShadowLockPatternUtils { 41 42 private static boolean sDeviceEncryptionEnabled; 43 private static Map<Integer, Integer> sUserToActivePasswordQualityMap = new HashMap<>(); 44 private static Map<Integer, Integer> sUserToComplexityMap = new HashMap<>(); 45 private static Map<Integer, Integer> sUserToProfileComplexityMap = new HashMap<>(); 46 private static Map<Integer, PasswordMetrics> sUserToMetricsMap = new HashMap<>(); 47 private static Map<Integer, PasswordMetrics> sUserToProfileMetricsMap = new HashMap<>(); 48 private static Map<Integer, Boolean> sUserToIsSecureMap = new HashMap<>(); 49 private static Map<Integer, Boolean> sUserToPatternEverChosenMap = new HashMap<>(); 50 private static Map<Integer, Boolean> sUserToVisiblePatternEnabledMap = new HashMap<>(); 51 private static Map<Integer, Boolean> sUserToBiometricAllowedMap = new HashMap<>(); 52 private static Map<Integer, Boolean> sUserToLockPatternEnabledMap = new HashMap<>(); 53 54 private static boolean sIsUserOwnsFrpCredential; 55 56 @Resetter reset()57 public static void reset() { 58 sUserToActivePasswordQualityMap.clear(); 59 sUserToComplexityMap.clear(); 60 sUserToProfileComplexityMap.clear(); 61 sUserToMetricsMap.clear(); 62 sUserToProfileMetricsMap.clear(); 63 sUserToIsSecureMap.clear(); 64 sUserToPatternEverChosenMap.clear(); 65 sUserToVisiblePatternEnabledMap.clear(); 66 sUserToBiometricAllowedMap.clear(); 67 sUserToLockPatternEnabledMap.clear(); 68 sDeviceEncryptionEnabled = false; 69 sIsUserOwnsFrpCredential = false; 70 } 71 72 @Implementation hasSecureLockScreen()73 protected boolean hasSecureLockScreen() { 74 return true; 75 } 76 77 @Implementation isSecure(int userId)78 protected boolean isSecure(int userId) { 79 Boolean isSecure = sUserToIsSecureMap.get(userId); 80 if (isSecure == null) { 81 return true; 82 } 83 return isSecure; 84 } 85 setIsSecure(int userId, boolean isSecure)86 public static void setIsSecure(int userId, boolean isSecure) { 87 sUserToIsSecureMap.put(userId, isSecure); 88 } 89 90 @Implementation getActivePasswordQuality(int userId)91 protected int getActivePasswordQuality(int userId) { 92 final Integer activePasswordQuality = sUserToActivePasswordQualityMap.get(userId); 93 if (activePasswordQuality == null) { 94 return DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; 95 } 96 return activePasswordQuality; 97 } 98 99 @Implementation getKeyguardStoredPasswordQuality(int userHandle)100 protected int getKeyguardStoredPasswordQuality(int userHandle) { 101 return 1; 102 } 103 104 @Implementation isDeviceEncryptionEnabled()105 protected static boolean isDeviceEncryptionEnabled() { 106 return sDeviceEncryptionEnabled; 107 } 108 109 @Implementation getEnabledTrustAgents(int userId)110 protected List<ComponentName> getEnabledTrustAgents(int userId) { 111 return null; 112 } 113 setDeviceEncryptionEnabled(boolean deviceEncryptionEnabled)114 public static void setDeviceEncryptionEnabled(boolean deviceEncryptionEnabled) { 115 sDeviceEncryptionEnabled = deviceEncryptionEnabled; 116 } 117 118 @Implementation getPasswordHistoryHashFactor(LockscreenCredential currentPassword, int userId)119 protected byte[] getPasswordHistoryHashFactor(LockscreenCredential currentPassword, 120 int userId) { 121 return null; 122 } 123 124 @Implementation checkPasswordHistory(byte[] passwordToCheck, byte[] hashFactor, int userId)125 protected boolean checkPasswordHistory(byte[] passwordToCheck, byte[] hashFactor, int userId) { 126 return false; 127 } 128 129 @Implementation getRequestedPasswordComplexity(int userId)130 public @DevicePolicyManager.PasswordComplexity int getRequestedPasswordComplexity(int userId) { 131 return getRequestedPasswordComplexity(userId, false); 132 } 133 134 @Implementation getRequestedPasswordComplexity(int userId, boolean deviceWideOnly)135 public @DevicePolicyManager.PasswordComplexity int getRequestedPasswordComplexity(int userId, 136 boolean deviceWideOnly) { 137 int complexity = sUserToComplexityMap.getOrDefault(userId, 138 DevicePolicyManager.PASSWORD_COMPLEXITY_NONE); 139 if (!deviceWideOnly) { 140 complexity = Math.max(complexity, sUserToProfileComplexityMap.getOrDefault(userId, 141 DevicePolicyManager.PASSWORD_COMPLEXITY_NONE)); 142 } 143 return complexity; 144 } 145 146 @Implementation userOwnsFrpCredential(Context context, UserInfo info)147 public static boolean userOwnsFrpCredential(Context context, UserInfo info) { 148 return sIsUserOwnsFrpCredential; 149 } 150 setUserOwnsFrpCredential(boolean isUserOwnsFrpCredential)151 public static void setUserOwnsFrpCredential(boolean isUserOwnsFrpCredential) { 152 sIsUserOwnsFrpCredential = isUserOwnsFrpCredential; 153 } 154 155 @Implementation isVisiblePatternEnabled(int userId)156 public boolean isVisiblePatternEnabled(int userId) { 157 return sUserToVisiblePatternEnabledMap.getOrDefault(userId, false); 158 } 159 setIsVisiblePatternEnabled(int userId, boolean isVisiblePatternEnabled)160 public static void setIsVisiblePatternEnabled(int userId, boolean isVisiblePatternEnabled) { 161 sUserToVisiblePatternEnabledMap.put(userId, isVisiblePatternEnabled); 162 } 163 164 @Implementation isPatternEverChosen(int userId)165 public boolean isPatternEverChosen(int userId) { 166 return sUserToPatternEverChosenMap.getOrDefault(userId, true); 167 } 168 setIsPatternEverChosen(int userId, boolean isPatternEverChosen)169 public static void setIsPatternEverChosen(int userId, boolean isPatternEverChosen) { 170 sUserToPatternEverChosenMap.put(userId, isPatternEverChosen); 171 } 172 173 @Implementation isBiometricAllowedForUser(int userId)174 public boolean isBiometricAllowedForUser(int userId) { 175 return sUserToBiometricAllowedMap.getOrDefault(userId, false); 176 } 177 setIsBiometricAllowedForUser(int userId, boolean isBiometricAllowed)178 public static void setIsBiometricAllowedForUser(int userId, boolean isBiometricAllowed) { 179 sUserToBiometricAllowedMap.put(userId, isBiometricAllowed); 180 } 181 182 @Implementation isLockPatternEnabled(int userId)183 public boolean isLockPatternEnabled(int userId) { 184 return sUserToBiometricAllowedMap.getOrDefault(userId, false); 185 } 186 setIsLockPatternEnabled(int userId, boolean isLockPatternEnabled)187 public static void setIsLockPatternEnabled(int userId, boolean isLockPatternEnabled) { 188 sUserToLockPatternEnabledMap.put(userId, isLockPatternEnabled); 189 } 190 191 @Implementation setLockCredential(@onNull LockscreenCredential newCredential, @NonNull LockscreenCredential savedCredential, int userHandle)192 public boolean setLockCredential(@NonNull LockscreenCredential newCredential, 193 @NonNull LockscreenCredential savedCredential, int userHandle) { 194 setIsSecure(userHandle, true); 195 return true; 196 } 197 198 @Implementation checkCredential(@onNull LockscreenCredential credential, int userId, @Nullable LockPatternUtils.CheckCredentialProgressCallback progressCallback)199 public boolean checkCredential(@NonNull LockscreenCredential credential, int userId, 200 @Nullable LockPatternUtils.CheckCredentialProgressCallback progressCallback) 201 throws LockPatternUtils.RequestThrottledException { 202 return true; 203 } 204 setRequiredPasswordComplexity(int userHandle, int complexity)205 public static void setRequiredPasswordComplexity(int userHandle, int complexity) { 206 sUserToComplexityMap.put(userHandle, complexity); 207 } 208 setRequiredPasswordComplexity(int complexity)209 public static void setRequiredPasswordComplexity(int complexity) { 210 sUserToComplexityMap.put(UserHandle.myUserId(), complexity); 211 } 212 setRequiredProfilePasswordComplexity(int complexity)213 public static void setRequiredProfilePasswordComplexity(int complexity) { 214 sUserToProfileComplexityMap.put(UserHandle.myUserId(), complexity); 215 } 216 217 @Implementation getRequestedPasswordMetrics(int userId, boolean deviceWideOnly)218 public PasswordMetrics getRequestedPasswordMetrics(int userId, boolean deviceWideOnly) { 219 PasswordMetrics metrics = sUserToMetricsMap.getOrDefault(userId, 220 new PasswordMetrics(LockPatternUtils.CREDENTIAL_TYPE_NONE)); 221 if (!deviceWideOnly) { 222 metrics.maxWith(sUserToProfileMetricsMap.getOrDefault(userId, 223 new PasswordMetrics(LockPatternUtils.CREDENTIAL_TYPE_NONE))); 224 } 225 return metrics; 226 } 227 setRequestedPasswordMetrics(PasswordMetrics metrics)228 public static void setRequestedPasswordMetrics(PasswordMetrics metrics) { 229 sUserToMetricsMap.put(UserHandle.myUserId(), metrics); 230 } 231 setRequestedProfilePasswordMetrics(PasswordMetrics metrics)232 public static void setRequestedProfilePasswordMetrics(PasswordMetrics metrics) { 233 sUserToProfileMetricsMap.put(UserHandle.myUserId(), metrics); 234 } 235 setActivePasswordQuality(int quality)236 public static void setActivePasswordQuality(int quality) { 237 sUserToActivePasswordQualityMap.put(UserHandle.myUserId(), quality); 238 } 239 240 @Implementation isLockScreenDisabled(int userId)241 public boolean isLockScreenDisabled(int userId) { 242 return false; 243 } 244 245 @Implementation isSeparateProfileChallengeEnabled(int userHandle)246 public boolean isSeparateProfileChallengeEnabled(int userHandle) { 247 return false; 248 } 249 } 250