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 boolean sMteOverridden; 37 private static String[] sRestrictedPkgs; 38 private static DevicePolicyManager sDevicePolicyManager; 39 private static String[] sDisabledTypes; 40 private static int sKeyguardDisabledFeatures; 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 sMteOverridden = 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 checkIfMteIsDisabled(Context context)107 public static EnforcedAdmin checkIfMteIsDisabled(Context context) { 108 return sMteOverridden ? new EnforcedAdmin() : null; 109 } 110 setRestricted(boolean restricted)111 public static void setRestricted(boolean restricted) { 112 sIsRestricted = restricted; 113 } 114 setRestrictedPkgs(String... pkgs)115 public static void setRestrictedPkgs(String... pkgs) { 116 sRestrictedPkgs = pkgs; 117 } 118 setHasSystemFeature(boolean hasSystemFeature)119 public static void setHasSystemFeature(boolean hasSystemFeature) { 120 sHasSystemFeature = hasSystemFeature; 121 } 122 setDevicePolicyManager(DevicePolicyManager dpm)123 public static void setDevicePolicyManager(DevicePolicyManager dpm) { 124 sDevicePolicyManager = dpm; 125 } 126 setDisabledTypes(String[] disabledTypes)127 public static void setDisabledTypes(String[] disabledTypes) { 128 sDisabledTypes = disabledTypes; 129 } 130 clearDisabledTypes()131 public static void clearDisabledTypes() { 132 sDisabledTypes = new String[0]; 133 } 134 setKeyguardDisabledFeatures(int features)135 public static void setKeyguardDisabledFeatures(int features) { 136 sKeyguardDisabledFeatures = features; 137 } 138 setMaximumTimeToLockIsSet(boolean isSet)139 public static void setMaximumTimeToLockIsSet(boolean isSet) { 140 sMaximumTimeToLockIsSet = isSet; 141 } 142 setMteIsDisabled(boolean isSet)143 public static void setMteIsDisabled(boolean isSet) { 144 sMteOverridden = isSet; 145 } 146 } 147