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