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     private static boolean sIsSuspended;
41 
42     @Resetter
reset()43     public static void reset() {
44         sIsRestricted = false;
45         sRestrictedPkgs = null;
46         sKeyguardDisabledFeatures = 0;
47         sDisabledTypes = new String[0];
48         sMaximumTimeToLockIsSet = false;
49         sIsSuspended = false;
50     }
51 
52     @Implementation
checkIfMeteredDataRestricted(Context context, String packageName, int userId)53     protected static EnforcedAdmin checkIfMeteredDataRestricted(Context context,
54             String packageName, int userId) {
55         if (sIsRestricted) {
56             return new EnforcedAdmin();
57         }
58         if (ArrayUtils.contains(sRestrictedPkgs, packageName)) {
59             return new EnforcedAdmin();
60         }
61         return null;
62     }
63 
64     @Implementation
checkIfAccountManagementDisabled(Context context, String accountType, int userId)65     protected static EnforcedAdmin checkIfAccountManagementDisabled(Context context,
66             String accountType, int userId) {
67         if (accountType == null) {
68             return null;
69         }
70         if (!sHasSystemFeature || sDevicePolicyManager == null) {
71             return null;
72         }
73         boolean isAccountTypeDisabled = false;
74         if (ArrayUtils.contains(sDisabledTypes, accountType)) {
75             isAccountTypeDisabled = true;
76         }
77         if (!isAccountTypeDisabled) {
78             return null;
79         }
80         return new EnforcedAdmin();
81     }
82 
83     @Implementation
checkIfKeyguardFeaturesDisabled(Context context, int features, final @UserIdInt int userId)84     protected static EnforcedAdmin checkIfKeyguardFeaturesDisabled(Context context,
85             int features, final @UserIdInt int userId) {
86         return (sKeyguardDisabledFeatures & features) == 0 ? null : new EnforcedAdmin();
87     }
88 
89     @Implementation
hasBaseUserRestriction(Context context, String userRestriction, int userId)90     protected static boolean hasBaseUserRestriction(Context context,
91             String userRestriction, int userId) {
92         return sIsRestricted;
93     }
94 
95     @Implementation
checkIfRestrictionEnforced(Context context, String userRestriction, int userId)96     protected static EnforcedAdmin checkIfRestrictionEnforced(Context context,
97             String userRestriction, int userId) {
98         return sIsRestricted ? new EnforcedAdmin() : null;
99     }
100 
101     @Implementation
checkIfMaximumTimeToLockIsSet(Context context)102     protected static EnforcedAdmin checkIfMaximumTimeToLockIsSet(Context context) {
103         return sMaximumTimeToLockIsSet ? new EnforcedAdmin() : null;
104     }
105 
106     @Implementation
checkIfApplicationIsSuspended(Context context, String packageName, int userId)107     protected static EnforcedAdmin checkIfApplicationIsSuspended(Context context,
108             String packageName, int userId) {
109         return sIsSuspended ? new EnforcedAdmin() : null;
110     }
111 
setRestricted(boolean restricted)112     public static void setRestricted(boolean restricted) {
113         sIsRestricted = restricted;
114     }
115 
setRestrictedPkgs(String... pkgs)116     public static void setRestrictedPkgs(String... pkgs) {
117         sRestrictedPkgs = pkgs;
118     }
119 
setHasSystemFeature(boolean hasSystemFeature)120     public static void setHasSystemFeature(boolean hasSystemFeature) {
121         sHasSystemFeature = hasSystemFeature;
122     }
123 
setDevicePolicyManager(DevicePolicyManager dpm)124     public static void setDevicePolicyManager(DevicePolicyManager dpm) {
125         sDevicePolicyManager = dpm;
126     }
127 
setDisabledTypes(String[] disabledTypes)128     public static void setDisabledTypes(String[] disabledTypes) {
129         sDisabledTypes = disabledTypes;
130     }
131 
clearDisabledTypes()132     public static void clearDisabledTypes() {
133         sDisabledTypes = new String[0];
134     }
135 
setKeyguardDisabledFeatures(int features)136     public static void setKeyguardDisabledFeatures(int features) {
137         sKeyguardDisabledFeatures = features;
138     }
139 
setMaximumTimeToLockIsSet(boolean isSet)140     public static void setMaximumTimeToLockIsSet(boolean isSet) {
141         sMaximumTimeToLockIsSet = isSet;
142     }
143 
setSuspended(boolean suspended)144     public static void setSuspended(boolean suspended) {
145         sIsRestricted = suspended;
146     }
147 }
148