1 package com.android.settings.testutils.shadow; 2 3 import android.annotation.NonNull; 4 import android.annotation.Nullable; 5 import android.annotation.UserIdInt; 6 import android.app.admin.DevicePolicyManager; 7 import android.content.ComponentName; 8 9 import org.robolectric.RuntimeEnvironment; 10 import org.robolectric.annotation.Implementation; 11 import org.robolectric.annotation.Implements; 12 import org.robolectric.shadow.api.Shadow; 13 14 import java.util.HashMap; 15 import java.util.Map; 16 import java.util.Objects; 17 18 @Implements(DevicePolicyManager.class) 19 public class ShadowDevicePolicyManager extends org.robolectric.shadows.ShadowDevicePolicyManager { 20 21 private final Map<Integer, Long> mProfileTimeouts = new HashMap<>(); 22 private Map<Integer, CharSequence> mSupportMessagesMap = new HashMap<>(); 23 private boolean mIsAdminActiveAsUser = false; 24 ComponentName mDeviceOwnerComponentName; 25 private int mDeviceOwnerUserId = -1; 26 27 public void setShortSupportMessageForUser(ComponentName admin, int userHandle, String message) { 28 mSupportMessagesMap.put(Objects.hash(admin, userHandle), message); 29 } 30 31 @Implementation 32 public @Nullable 33 CharSequence getShortSupportMessageForUser(@NonNull ComponentName admin, 34 int userHandle) { 35 return mSupportMessagesMap.get(Objects.hash(admin, userHandle)); 36 } 37 38 @Implementation 39 public boolean isAdminActiveAsUser(@NonNull ComponentName admin, int userId) { 40 return mIsAdminActiveAsUser; 41 } 42 43 @Implementation 44 public int getDeviceOwnerUserId() { 45 return mDeviceOwnerUserId; 46 } 47 48 @Implementation 49 public long getMaximumTimeToLock(ComponentName admin, @UserIdInt int userHandle) { 50 return mProfileTimeouts.getOrDefault(userHandle, 0L); 51 } 52 53 @Implementation 54 public ComponentName getDeviceOwnerComponentOnAnyUser() { 55 return mDeviceOwnerComponentName; 56 } 57 58 public void setIsAdminActiveAsUser(boolean active) { 59 mIsAdminActiveAsUser = active; 60 } 61 62 public void setDeviceOwnerUserId(int id) { 63 mDeviceOwnerUserId = id; 64 } 65 66 public void setMaximumTimeToLock(@UserIdInt int userHandle, Long timeout) { 67 mProfileTimeouts.put(userHandle, timeout); 68 } 69 70 public void setDeviceOwnerComponentOnAnyUser(ComponentName admin) { 71 mDeviceOwnerComponentName = admin; 72 } 73 74 public static ShadowDevicePolicyManager getShadow() { 75 return (ShadowDevicePolicyManager) Shadow.extract( 76 RuntimeEnvironment.application.getSystemService(DevicePolicyManager.class)); 77 } 78 } 79