1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.settings.testutils.shadow;
17 
18 import android.annotation.NonNull;
19 import android.annotation.UserIdInt;
20 import android.app.admin.DevicePolicyManager;
21 import android.content.Context;
22 import android.content.Intent;
23 
24 import com.android.internal.util.ArrayUtils;
25 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
26 import com.android.settingslib.RestrictedLockUtilsInternal;
27 
28 import org.robolectric.annotation.Implementation;
29 import org.robolectric.annotation.Implements;
30 import org.robolectric.annotation.Resetter;
31 
32 @Implements(RestrictedLockUtilsInternal.class)
33 public class ShadowRestrictedLockUtilsInternal {
34 
35     private static boolean sIsRestricted;
36     private static boolean sHasSystemFeature;
37     private static boolean sMaximumTimeToLockIsSet;
38     private static boolean sMteOverridden;
39     private static String[] sRestrictedPkgs;
40     private static DevicePolicyManager sDevicePolicyManager;
41     private static String[] sDisabledTypes;
42     private static int sKeyguardDisabledFeatures;
43     private static String[] sEcmRestrictedPkgs;
44     private static boolean sAccessibilityServiceRestricted;
45 
46     @Resetter
reset()47     public static void reset() {
48         sIsRestricted = false;
49         sRestrictedPkgs = null;
50         sKeyguardDisabledFeatures = 0;
51         sDisabledTypes = new String[0];
52         sMaximumTimeToLockIsSet = false;
53         sMteOverridden = false;
54         sEcmRestrictedPkgs = new String[0];
55         sAccessibilityServiceRestricted = false;
56     }
57 
58     @Implementation
checkIfMeteredDataUsageUserControlDisabled(Context context, String packageName, int userId)59     protected static EnforcedAdmin checkIfMeteredDataUsageUserControlDisabled(Context context,
60             String packageName, int userId) {
61         if (sIsRestricted) {
62             return new EnforcedAdmin();
63         }
64         if (ArrayUtils.contains(sRestrictedPkgs, packageName)) {
65             return new EnforcedAdmin();
66         }
67         return null;
68     }
69 
70     @Implementation
checkIfAccountManagementDisabled(Context context, String accountType, int userId)71     protected static EnforcedAdmin checkIfAccountManagementDisabled(Context context,
72             String accountType, int userId) {
73         if (accountType == null) {
74             return null;
75         }
76         if (!sHasSystemFeature || sDevicePolicyManager == null) {
77             return null;
78         }
79         boolean isAccountTypeDisabled = false;
80         if (ArrayUtils.contains(sDisabledTypes, accountType)) {
81             isAccountTypeDisabled = true;
82         }
83         if (!isAccountTypeDisabled) {
84             return null;
85         }
86         return new EnforcedAdmin();
87     }
88 
89     @Implementation
checkIfKeyguardFeaturesDisabled(Context context, int features, final @UserIdInt int userId)90     protected static EnforcedAdmin checkIfKeyguardFeaturesDisabled(Context context,
91             int features, final @UserIdInt int userId) {
92         return (sKeyguardDisabledFeatures & features) == 0 ? null : new EnforcedAdmin();
93     }
94 
95     @Implementation
hasBaseUserRestriction(Context context, String userRestriction, int userId)96     protected static boolean hasBaseUserRestriction(Context context,
97             String userRestriction, int userId) {
98         return sIsRestricted;
99     }
100 
101     @Implementation
checkIfRestrictionEnforced(Context context, String userRestriction, int userId)102     protected static EnforcedAdmin checkIfRestrictionEnforced(Context context,
103             String userRestriction, int userId) {
104         return sIsRestricted ? new EnforcedAdmin() : null;
105     }
106 
107     @Implementation
checkIfMaximumTimeToLockIsSet(Context context)108     protected static EnforcedAdmin checkIfMaximumTimeToLockIsSet(Context context) {
109         return sMaximumTimeToLockIsSet ? new EnforcedAdmin() : null;
110     }
111 
112     @Implementation
checkIfMteIsDisabled(Context context)113     public static EnforcedAdmin checkIfMteIsDisabled(Context context) {
114         return sMteOverridden ? new EnforcedAdmin() : null;
115     }
116 
checkIfAccessibilityServiceDisallowed(Context context, String packageName, int userId)117     public static EnforcedAdmin checkIfAccessibilityServiceDisallowed(Context context,
118             String packageName, int userId) {
119         return sAccessibilityServiceRestricted ? new EnforcedAdmin() : null;
120     }
121 
122     @Implementation
checkIfRequiresEnhancedConfirmation(@onNull Context context, @NonNull String settingIdentifier, @NonNull String packageName)123     public static Intent checkIfRequiresEnhancedConfirmation(@NonNull Context context,
124             @NonNull String settingIdentifier, @NonNull String packageName) {
125         if (ArrayUtils.contains(sEcmRestrictedPkgs, packageName)) {
126             return new Intent();
127         }
128 
129         return null;
130     }
131 
132     @Implementation
isEnhancedConfirmationRestricted(@onNull Context context, @NonNull String settingIdentifier, @NonNull String packageName)133     public static boolean isEnhancedConfirmationRestricted(@NonNull Context context,
134             @NonNull String settingIdentifier, @NonNull String packageName) {
135         return false;
136     }
137 
setRestricted(boolean restricted)138     public static void setRestricted(boolean restricted) {
139         sIsRestricted = restricted;
140     }
141 
setEcmRestrictedPkgs(String... pkgs)142     public static void setEcmRestrictedPkgs(String... pkgs) {
143         sEcmRestrictedPkgs = pkgs;
144     }
145 
setRestrictedPkgs(String... pkgs)146     public static void setRestrictedPkgs(String... pkgs) {
147         sRestrictedPkgs = pkgs;
148     }
149 
setHasSystemFeature(boolean hasSystemFeature)150     public static void setHasSystemFeature(boolean hasSystemFeature) {
151         sHasSystemFeature = hasSystemFeature;
152     }
153 
setDevicePolicyManager(DevicePolicyManager dpm)154     public static void setDevicePolicyManager(DevicePolicyManager dpm) {
155         sDevicePolicyManager = dpm;
156     }
157 
setDisabledTypes(String[] disabledTypes)158     public static void setDisabledTypes(String[] disabledTypes) {
159         sDisabledTypes = disabledTypes;
160     }
161 
clearDisabledTypes()162     public static void clearDisabledTypes() {
163         sDisabledTypes = new String[0];
164     }
165 
setKeyguardDisabledFeatures(int features)166     public static void setKeyguardDisabledFeatures(int features) {
167         sKeyguardDisabledFeatures = features;
168     }
169 
setMaximumTimeToLockIsSet(boolean isSet)170     public static void setMaximumTimeToLockIsSet(boolean isSet) {
171         sMaximumTimeToLockIsSet = isSet;
172     }
173 
setMteIsDisabled(boolean isSet)174     public static void setMteIsDisabled(boolean isSet) {
175         sMteOverridden = isSet;
176     }
177 }
178