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.app.admin.DevicePolicyManager; 20 import android.app.admin.PasswordMetrics; 21 import android.content.ComponentName; 22 import android.os.UserHandle; 23 24 import com.android.internal.widget.LockPatternUtils; 25 import com.android.internal.widget.LockscreenCredential; 26 27 import org.robolectric.annotation.Implementation; 28 import org.robolectric.annotation.Implements; 29 import org.robolectric.annotation.Resetter; 30 31 import java.util.HashMap; 32 import java.util.List; 33 import java.util.Map; 34 35 @Implements(LockPatternUtils.class) 36 public class ShadowLockPatternUtils { 37 38 private static boolean sDeviceEncryptionEnabled; 39 private static Map<Integer, Integer> sUserToActivePasswordQualityMap = new HashMap<>(); 40 private static Map<Integer, Integer> sUserToComplexityMap = new HashMap<>(); 41 private static Map<Integer, Integer> sUserToProfileComplexityMap = new HashMap<>(); 42 private static Map<Integer, PasswordMetrics> sUserToMetricsMap = new HashMap<>(); 43 private static Map<Integer, PasswordMetrics> sUserToProfileMetricsMap = new HashMap<>(); 44 private static Map<Integer, Boolean> sUserToIsSecureMap = new HashMap<>(); 45 46 @Resetter reset()47 public static void reset() { 48 sUserToActivePasswordQualityMap.clear(); 49 sUserToComplexityMap.clear(); 50 sUserToProfileComplexityMap.clear(); 51 sUserToMetricsMap.clear(); 52 sUserToProfileMetricsMap.clear(); 53 sUserToIsSecureMap.clear(); 54 sDeviceEncryptionEnabled = false; 55 } 56 57 @Implementation hasSecureLockScreen()58 protected boolean hasSecureLockScreen() { 59 return true; 60 } 61 62 @Implementation isSecure(int userId)63 protected boolean isSecure(int userId) { 64 Boolean isSecure = sUserToIsSecureMap.get(userId); 65 if (isSecure == null) { 66 return true; 67 } 68 return isSecure; 69 } 70 setIsSecure(int userId, boolean isSecure)71 public static void setIsSecure(int userId, boolean isSecure) { 72 sUserToIsSecureMap.put(userId, isSecure); 73 } 74 75 @Implementation getActivePasswordQuality(int userId)76 protected int getActivePasswordQuality(int userId) { 77 final Integer activePasswordQuality = sUserToActivePasswordQualityMap.get(userId); 78 if (activePasswordQuality == null) { 79 return DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; 80 } 81 return activePasswordQuality; 82 } 83 84 @Implementation getKeyguardStoredPasswordQuality(int userHandle)85 protected int getKeyguardStoredPasswordQuality(int userHandle) { 86 return 1; 87 } 88 89 @Implementation isDeviceEncryptionEnabled()90 protected static boolean isDeviceEncryptionEnabled() { 91 return sDeviceEncryptionEnabled; 92 } 93 94 @Implementation getEnabledTrustAgents(int userId)95 protected List<ComponentName> getEnabledTrustAgents(int userId) { 96 return null; 97 } 98 setDeviceEncryptionEnabled(boolean deviceEncryptionEnabled)99 public static void setDeviceEncryptionEnabled(boolean deviceEncryptionEnabled) { 100 sDeviceEncryptionEnabled = deviceEncryptionEnabled; 101 } 102 103 @Implementation getPasswordHistoryHashFactor(LockscreenCredential currentPassword, int userId)104 protected byte[] getPasswordHistoryHashFactor(LockscreenCredential currentPassword, 105 int userId) { 106 return null; 107 } 108 109 @Implementation checkPasswordHistory(byte[] passwordToCheck, byte[] hashFactor, int userId)110 protected boolean checkPasswordHistory(byte[] passwordToCheck, byte[] hashFactor, int userId) { 111 return false; 112 } 113 114 @Implementation getRequestedPasswordComplexity(int userId)115 public @DevicePolicyManager.PasswordComplexity int getRequestedPasswordComplexity(int userId) { 116 return getRequestedPasswordComplexity(userId, false); 117 } 118 119 @Implementation getRequestedPasswordComplexity(int userId, boolean deviceWideOnly)120 public @DevicePolicyManager.PasswordComplexity int getRequestedPasswordComplexity(int userId, 121 boolean deviceWideOnly) { 122 int complexity = sUserToComplexityMap.getOrDefault(userId, 123 DevicePolicyManager.PASSWORD_COMPLEXITY_NONE); 124 if (!deviceWideOnly) { 125 complexity = Math.max(complexity, sUserToProfileComplexityMap.getOrDefault(userId, 126 DevicePolicyManager.PASSWORD_COMPLEXITY_NONE)); 127 } 128 return complexity; 129 } 130 setRequiredPasswordComplexity(int userHandle, int complexity)131 public static void setRequiredPasswordComplexity(int userHandle, int complexity) { 132 sUserToComplexityMap.put(userHandle, complexity); 133 } 134 setRequiredPasswordComplexity(int complexity)135 public static void setRequiredPasswordComplexity(int complexity) { 136 sUserToComplexityMap.put(UserHandle.myUserId(), complexity); 137 } 138 setRequiredProfilePasswordComplexity(int complexity)139 public static void setRequiredProfilePasswordComplexity(int complexity) { 140 sUserToProfileComplexityMap.put(UserHandle.myUserId(), complexity); 141 } 142 143 @Implementation getRequestedPasswordMetrics(int userId, boolean deviceWideOnly)144 public PasswordMetrics getRequestedPasswordMetrics(int userId, boolean deviceWideOnly) { 145 PasswordMetrics metrics = sUserToMetricsMap.getOrDefault(userId, 146 new PasswordMetrics(LockPatternUtils.CREDENTIAL_TYPE_NONE)); 147 if (!deviceWideOnly) { 148 metrics.maxWith(sUserToProfileMetricsMap.getOrDefault(userId, 149 new PasswordMetrics(LockPatternUtils.CREDENTIAL_TYPE_NONE))); 150 } 151 return metrics; 152 } 153 setRequestedPasswordMetrics(PasswordMetrics metrics)154 public static void setRequestedPasswordMetrics(PasswordMetrics metrics) { 155 sUserToMetricsMap.put(UserHandle.myUserId(), metrics); 156 } 157 setRequestedProfilePasswordMetrics(PasswordMetrics metrics)158 public static void setRequestedProfilePasswordMetrics(PasswordMetrics metrics) { 159 sUserToProfileMetricsMap.put(UserHandle.myUserId(), metrics); 160 } 161 setActivePasswordQuality(int quality)162 public static void setActivePasswordQuality(int quality) { 163 sUserToActivePasswordQualityMap.put(UserHandle.myUserId(), quality); 164 } 165 166 @Implementation isLockScreenDisabled(int userId)167 public boolean isLockScreenDisabled(int userId) { 168 return false; 169 } 170 171 @Implementation isSeparateProfileChallengeEnabled(int userHandle)172 public boolean isSeparateProfileChallengeEnabled(int userHandle) { 173 return false; 174 } 175 } 176