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