1 package com.android.settings.testutils.shadow; 2 3 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; 4 5 import android.annotation.NonNull; 6 import android.annotation.Nullable; 7 import android.annotation.UserIdInt; 8 import android.app.admin.DevicePolicyManager; 9 import android.app.admin.PasswordMetrics; 10 import android.app.admin.PasswordPolicy; 11 import android.content.ComponentName; 12 13 import org.robolectric.RuntimeEnvironment; 14 import org.robolectric.annotation.Implementation; 15 import org.robolectric.annotation.Implements; 16 import org.robolectric.shadow.api.Shadow; 17 18 import java.util.HashMap; 19 import java.util.Map; 20 import java.util.Objects; 21 22 @Implements(DevicePolicyManager.class) 23 public class ShadowDevicePolicyManager extends org.robolectric.shadows.ShadowDevicePolicyManager { 24 25 private final Map<Integer, Long> mProfileTimeouts = new HashMap<>(); 26 private Map<Integer, CharSequence> mSupportMessagesMap = new HashMap<>(); 27 private boolean mIsAdminActiveAsUser = false; 28 private ComponentName mDeviceOwnerComponentName; 29 private int mDeviceOwnerUserId = -1; 30 private int mPasswordMinQuality = PASSWORD_QUALITY_UNSPECIFIED; 31 private int mPasswordMinLength = 0; 32 private int mPasswordMinSymbols = 0; 33 setShortSupportMessageForUser(ComponentName admin, int userHandle, String message)34 public void setShortSupportMessageForUser(ComponentName admin, int userHandle, String message) { 35 mSupportMessagesMap.put(Objects.hash(admin, userHandle), message); 36 } 37 38 @Implementation getShortSupportMessageForUser(@onNull ComponentName admin, int userHandle)39 protected @Nullable CharSequence getShortSupportMessageForUser(@NonNull ComponentName admin, 40 int userHandle) { 41 return mSupportMessagesMap.get(Objects.hash(admin, userHandle)); 42 } 43 44 @Implementation isAdminActiveAsUser(@onNull ComponentName admin, int userId)45 protected boolean isAdminActiveAsUser(@NonNull ComponentName admin, int userId) { 46 return mIsAdminActiveAsUser; 47 } 48 49 @Implementation getDeviceOwnerUserId()50 protected int getDeviceOwnerUserId() { 51 return mDeviceOwnerUserId; 52 } 53 54 @Implementation getMaximumTimeToLock(ComponentName admin, @UserIdInt int userHandle)55 protected long getMaximumTimeToLock(ComponentName admin, @UserIdInt int userHandle) { 56 return mProfileTimeouts.getOrDefault(userHandle, 0L); 57 } 58 59 @Implementation getDeviceOwnerComponentOnAnyUser()60 protected ComponentName getDeviceOwnerComponentOnAnyUser() { 61 return mDeviceOwnerComponentName; 62 } 63 setIsAdminActiveAsUser(boolean active)64 public void setIsAdminActiveAsUser(boolean active) { 65 mIsAdminActiveAsUser = active; 66 } 67 setDeviceOwnerUserId(int id)68 public void setDeviceOwnerUserId(int id) { 69 mDeviceOwnerUserId = id; 70 } 71 setMaximumTimeToLock(@serIdInt int userHandle, Long timeout)72 public void setMaximumTimeToLock(@UserIdInt int userHandle, Long timeout) { 73 mProfileTimeouts.put(userHandle, timeout); 74 } 75 setDeviceOwnerComponentOnAnyUser(ComponentName admin)76 public void setDeviceOwnerComponentOnAnyUser(ComponentName admin) { 77 mDeviceOwnerComponentName = admin; 78 } 79 80 @Implementation getPasswordMinimumMetrics(int userHandle)81 public PasswordMetrics getPasswordMinimumMetrics(int userHandle) { 82 PasswordPolicy policy = new PasswordPolicy(); 83 policy.quality = mPasswordMinQuality; 84 policy.length = mPasswordMinLength; 85 policy.symbols = mPasswordMinSymbols; 86 return policy.getMinMetrics(); 87 } 88 setPasswordQuality(int quality)89 public void setPasswordQuality(int quality) { 90 mPasswordMinQuality = quality; 91 } 92 setPasswordMinimumLength(int length)93 public void setPasswordMinimumLength(int length) { 94 mPasswordMinLength = length; 95 } 96 setPasswordMinimumSymbols(int numOfSymbols)97 public void setPasswordMinimumSymbols(int numOfSymbols) { 98 mPasswordMinSymbols = numOfSymbols; 99 } 100 getShadow()101 public static ShadowDevicePolicyManager getShadow() { 102 return (ShadowDevicePolicyManager) Shadow.extract( 103 RuntimeEnvironment.application.getSystemService(DevicePolicyManager.class)); 104 } 105 } 106