1 /*
2  * Copyright (C) 2021 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 
17 package com.android.settingslib.enterprise;
18 
19 import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_FINANCED;
20 
21 import static com.android.settingslib.enterprise.ActionDisabledLearnMoreButtonLauncher.DEFAULT_RESOLVE_ACTIVITY_CHECKER;
22 import static com.android.settingslib.enterprise.ManagedDeviceActionDisabledByAdminController.DEFAULT_FOREGROUND_USER_CHECKER;
23 
24 import android.app.admin.DevicePolicyManager;
25 import android.content.ComponentName;
26 import android.content.Context;
27 import android.hardware.biometrics.BiometricAuthenticator;
28 import android.hardware.biometrics.ParentalControlsUtilsInternal;
29 import android.os.UserHandle;
30 import android.os.UserManager;
31 import android.provider.DeviceConfig;
32 import android.text.TextUtils;
33 
34 /**
35  * A factory that returns the relevant instance of {@link ActionDisabledByAdminController}.
36  */
37 public final class ActionDisabledByAdminControllerFactory {
38 
39     /**
40      * Returns the relevant instance of {@link ActionDisabledByAdminController}.
41      * @param userHandle user on which to launch the help page, if necessary
42      */
createInstance(Context context, String restriction, DeviceAdminStringProvider stringProvider, UserHandle userHandle)43     public static ActionDisabledByAdminController createInstance(Context context,
44             String restriction, DeviceAdminStringProvider stringProvider,
45             UserHandle userHandle) {
46         if (doesBiometricRequireParentalConsent(context, restriction)) {
47             return new BiometricActionDisabledByAdminController(stringProvider);
48         } else if (isFinancedDevice(context)) {
49             return new FinancedDeviceActionDisabledByAdminController(stringProvider);
50         } else if (isSupervisedDevice(context)) {
51             return new SupervisedDeviceActionDisabledByAdminController(stringProvider, restriction);
52         } else {
53             return new ManagedDeviceActionDisabledByAdminController(
54                     stringProvider,
55                     userHandle,
56                     DEFAULT_FOREGROUND_USER_CHECKER,
57                     DEFAULT_RESOLVE_ACTIVITY_CHECKER);
58         }
59     }
60 
isSupervisedDevice(Context context)61     private static boolean isSupervisedDevice(Context context) {
62         DevicePolicyManager devicePolicyManager =
63                 context.getSystemService(DevicePolicyManager.class);
64         ComponentName supervisionComponent =
65                 devicePolicyManager.getProfileOwnerOrDeviceOwnerSupervisionComponent(
66                         new UserHandle(UserHandle.myUserId()));
67         return supervisionComponent != null;
68     }
69 
70     /**
71      * @return true if the restriction == UserManager.DISALLOW_BIOMETRIC and parental consent
72      * is required.
73      */
doesBiometricRequireParentalConsent(Context context, String restriction)74     private static boolean doesBiometricRequireParentalConsent(Context context,
75             String restriction) {
76         if (!TextUtils.equals(UserManager.DISALLOW_BIOMETRIC, restriction)) {
77             return false;
78         }
79         DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
80         return ParentalControlsUtilsInternal.parentConsentRequired(context, dpm,
81                 BiometricAuthenticator.TYPE_ANY_BIOMETRIC, new UserHandle(UserHandle.myUserId()));
82     }
83 
isFinancedDevice(Context context)84     private static boolean isFinancedDevice(Context context) {
85         DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
86         // TODO(b/259908270): remove
87         if (DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_DEVICE_POLICY_MANAGER,
88                 DevicePolicyManager.ADD_ISFINANCED_DEVICE_FLAG,
89                 DevicePolicyManager.ADD_ISFINANCED_FEVICE_DEFAULT)) {
90             return dpm.isFinancedDevice();
91         }
92         return dpm.isDeviceManaged() && dpm.getDeviceOwnerType(
93                 dpm.getDeviceOwnerComponentOnAnyUser()) == DEVICE_OWNER_TYPE_FINANCED;
94     }
95 
ActionDisabledByAdminControllerFactory()96     private ActionDisabledByAdminControllerFactory() {
97         throw new UnsupportedOperationException("provides only static methods");
98     }
99 }
100