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.UserIdInt;
19 import android.app.admin.DevicePolicyManager;
20 import android.content.Context;
21 
22 import com.android.internal.util.ArrayUtils;
23 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
24 import com.android.settingslib.RestrictedLockUtilsInternal;
25 
26 import org.robolectric.annotation.Implementation;
27 import org.robolectric.annotation.Implements;
28 import org.robolectric.annotation.Resetter;
29 
30 @Implements(RestrictedLockUtilsInternal.class)
31 public class ShadowRestrictedLockUtilsInternal {
32 
33     private static boolean sIsRestricted;
34     private static boolean sHasSystemFeature;
35     private static boolean sMaximumTimeToLockIsSet;
36     private static String[] sRestrictedPkgs;
37     private static DevicePolicyManager sDevicePolicyManager;
38     private static String[] sDisabledTypes;
39     private static int sKeyguardDisabledFeatures;
40 
41     @Resetter
reset()42     public static void reset() {
43         sIsRestricted = false;
44         sRestrictedPkgs = null;
45         sKeyguardDisabledFeatures = 0;
46         sDisabledTypes = new String[0];
47         sMaximumTimeToLockIsSet = false;
48     }
49 
50     @Implementation
checkIfMeteredDataRestricted(Context context, String packageName, int userId)51     protected static EnforcedAdmin checkIfMeteredDataRestricted(Context context,
52             String packageName, int userId) {
53         if (sIsRestricted) {
54             return new EnforcedAdmin();
55         }
56         if (ArrayUtils.contains(sRestrictedPkgs, packageName)) {
57             return new EnforcedAdmin();
58         }
59         return null;
60     }
61 
62     @Implementation
checkIfAccountManagementDisabled(Context context, String accountType, int userId)63     protected static EnforcedAdmin checkIfAccountManagementDisabled(Context context,
64             String accountType, int userId) {
65         if (accountType == null) {
66             return null;
67         }
68         if (!sHasSystemFeature || sDevicePolicyManager == null) {
69             return null;
70         }
71         boolean isAccountTypeDisabled = false;
72         if (ArrayUtils.contains(sDisabledTypes, accountType)) {
73             isAccountTypeDisabled = true;
74         }
75         if (!isAccountTypeDisabled) {
76             return null;
77         }
78         return new EnforcedAdmin();
79     }
80 
81     @Implementation
checkIfKeyguardFeaturesDisabled(Context context, int features, final @UserIdInt int userId)82     protected static EnforcedAdmin checkIfKeyguardFeaturesDisabled(Context context,
83             int features, final @UserIdInt int userId) {
84         return (sKeyguardDisabledFeatures & features) == 0 ? null : new EnforcedAdmin();
85     }
86 
87     @Implementation
hasBaseUserRestriction(Context context, String userRestriction, int userId)88     protected static boolean hasBaseUserRestriction(Context context,
89             String userRestriction, int userId) {
90         return sIsRestricted;
91     }
92 
93     @Implementation
checkIfRestrictionEnforced(Context context, String userRestriction, int userId)94     protected static EnforcedAdmin checkIfRestrictionEnforced(Context context,
95             String userRestriction, int userId) {
96         return sIsRestricted ? new EnforcedAdmin() : null;
97     }
98 
99     @Implementation
checkIfMaximumTimeToLockIsSet(Context context)100     protected static EnforcedAdmin checkIfMaximumTimeToLockIsSet(Context context) {
101         return sMaximumTimeToLockIsSet ? new EnforcedAdmin() : null;
102     }
103 
setRestricted(boolean restricted)104     public static void setRestricted(boolean restricted) {
105         sIsRestricted = restricted;
106     }
107 
setRestrictedPkgs(String... pkgs)108     public static void setRestrictedPkgs(String... pkgs) {
109         sRestrictedPkgs = pkgs;
110     }
111 
setHasSystemFeature(boolean hasSystemFeature)112     public static void setHasSystemFeature(boolean hasSystemFeature) {
113         sHasSystemFeature = hasSystemFeature;
114     }
115 
setDevicePolicyManager(DevicePolicyManager dpm)116     public static void setDevicePolicyManager(DevicePolicyManager dpm) {
117         sDevicePolicyManager = dpm;
118     }
119 
setDisabledTypes(String[] disabledTypes)120     public static void setDisabledTypes(String[] disabledTypes) {
121         sDisabledTypes = disabledTypes;
122     }
123 
clearDisabledTypes()124     public static void clearDisabledTypes() {
125         sDisabledTypes = new String[0];
126     }
127 
setKeyguardDisabledFeatures(int features)128     public static void setKeyguardDisabledFeatures(int features) {
129         sKeyguardDisabledFeatures = features;
130     }
131 
setMaximumTimeToLockIsSet(boolean isSet)132     public static void setMaximumTimeToLockIsSet(boolean isSet) {
133         sMaximumTimeToLockIsSet = isSet;
134     }
135 }
136