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> sUserToComplexityMap = new HashMap<>(); 40 private static Map<Integer, Integer> sUserToProfileComplexityMap = new HashMap<>(); 41 private static Map<Integer, PasswordMetrics> sUserToMetricsMap = new HashMap<>(); 42 private static Map<Integer, PasswordMetrics> sUserToProfileMetricsMap = new HashMap<>(); 43 44 45 @Resetter reset()46 public static void reset() { 47 sUserToComplexityMap.clear(); 48 sUserToProfileComplexityMap.clear(); 49 sUserToMetricsMap.clear(); 50 sUserToProfileMetricsMap.clear(); 51 sDeviceEncryptionEnabled = false; 52 } 53 54 @Implementation hasSecureLockScreen()55 protected boolean hasSecureLockScreen() { 56 return true; 57 } 58 59 @Implementation isSecure(int id)60 protected boolean isSecure(int id) { 61 return true; 62 } 63 64 @Implementation getActivePasswordQuality(int userId)65 protected int getActivePasswordQuality(int userId) { 66 return DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; 67 } 68 69 @Implementation getKeyguardStoredPasswordQuality(int userHandle)70 protected int getKeyguardStoredPasswordQuality(int userHandle) { 71 return 1; 72 } 73 74 @Implementation isDeviceEncryptionEnabled()75 protected static boolean isDeviceEncryptionEnabled() { 76 return sDeviceEncryptionEnabled; 77 } 78 79 @Implementation getEnabledTrustAgents(int userId)80 protected List<ComponentName> getEnabledTrustAgents(int userId) { 81 return null; 82 } 83 setDeviceEncryptionEnabled(boolean deviceEncryptionEnabled)84 public static void setDeviceEncryptionEnabled(boolean deviceEncryptionEnabled) { 85 sDeviceEncryptionEnabled = deviceEncryptionEnabled; 86 } 87 88 @Implementation getPasswordHistoryHashFactor(LockscreenCredential currentPassword, int userId)89 protected byte[] getPasswordHistoryHashFactor(LockscreenCredential currentPassword, 90 int userId) { 91 return null; 92 } 93 94 @Implementation checkPasswordHistory(byte[] passwordToCheck, byte[] hashFactor, int userId)95 protected boolean checkPasswordHistory(byte[] passwordToCheck, byte[] hashFactor, int userId) { 96 return false; 97 } 98 99 @Implementation getRequestedPasswordComplexity(int userId)100 public @DevicePolicyManager.PasswordComplexity int getRequestedPasswordComplexity(int userId) { 101 return getRequestedPasswordComplexity(userId, false); 102 } 103 104 @Implementation getRequestedPasswordComplexity(int userId, boolean deviceWideOnly)105 public @DevicePolicyManager.PasswordComplexity int getRequestedPasswordComplexity(int userId, 106 boolean deviceWideOnly) { 107 int complexity = sUserToComplexityMap.getOrDefault(userId, 108 DevicePolicyManager.PASSWORD_COMPLEXITY_NONE); 109 if (!deviceWideOnly) { 110 complexity = Math.max(complexity, sUserToProfileComplexityMap.getOrDefault(userId, 111 DevicePolicyManager.PASSWORD_COMPLEXITY_NONE)); 112 } 113 return complexity; 114 } 115 setRequiredPasswordComplexity(int userHandle, int complexity)116 public static void setRequiredPasswordComplexity(int userHandle, int complexity) { 117 sUserToComplexityMap.put(userHandle, complexity); 118 } 119 setRequiredPasswordComplexity(int complexity)120 public static void setRequiredPasswordComplexity(int complexity) { 121 sUserToComplexityMap.put(UserHandle.myUserId(), complexity); 122 } 123 setRequiredProfilePasswordComplexity(int complexity)124 public static void setRequiredProfilePasswordComplexity(int complexity) { 125 sUserToProfileComplexityMap.put(UserHandle.myUserId(), complexity); 126 } 127 128 @Implementation getRequestedPasswordMetrics(int userId, boolean deviceWideOnly)129 public PasswordMetrics getRequestedPasswordMetrics(int userId, boolean deviceWideOnly) { 130 PasswordMetrics metrics = sUserToMetricsMap.getOrDefault(userId, 131 new PasswordMetrics(LockPatternUtils.CREDENTIAL_TYPE_NONE)); 132 if (!deviceWideOnly) { 133 metrics.maxWith(sUserToProfileMetricsMap.getOrDefault(userId, 134 new PasswordMetrics(LockPatternUtils.CREDENTIAL_TYPE_NONE))); 135 } 136 return metrics; 137 } 138 setRequestedPasswordMetrics(PasswordMetrics metrics)139 public static void setRequestedPasswordMetrics(PasswordMetrics metrics) { 140 sUserToMetricsMap.put(UserHandle.myUserId(), metrics); 141 } 142 setRequestedProfilePasswordMetrics(PasswordMetrics metrics)143 public static void setRequestedProfilePasswordMetrics(PasswordMetrics metrics) { 144 sUserToProfileMetricsMap.put(UserHandle.myUserId(), metrics); 145 } 146 147 } 148