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