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 package com.android.settings.biometrics.combination;
17 
18 import static android.provider.Settings.Secure.BIOMETRIC_KEYGUARD_ENABLED;
19 
20 import android.app.admin.DevicePolicyManager;
21 import android.content.Context;
22 import android.provider.Settings;
23 
24 import com.android.settings.Utils;
25 import com.android.settings.biometrics.activeunlock.ActiveUnlockStatusUtils;
26 import com.android.settings.core.TogglePreferenceController;
27 import com.android.settingslib.RestrictedLockUtils;
28 import com.android.settingslib.RestrictedLockUtilsInternal;
29 
30 /**
31  * Preference controller that controls whether the biometrics authentication to unlock the device.
32  */
33 public class BiometricSettingsKeyguardPreferenceController extends TogglePreferenceController {
34     private static final int ON = 1;
35     private static final int OFF = 0;
36     private static final int DEFAULT = ON;
37 
38     private int mUserId;
39 
BiometricSettingsKeyguardPreferenceController(Context context, String key)40     public BiometricSettingsKeyguardPreferenceController(Context context, String key) {
41         super(context, key);
42     }
43 
getRestrictingAdmin()44     protected RestrictedLockUtils.EnforcedAdmin getRestrictingAdmin() {
45         return RestrictedLockUtilsInternal.checkIfKeyguardFeaturesDisabled(mContext,
46                 DevicePolicyManager.KEYGUARD_DISABLE_BIOMETRICS, mUserId);
47     }
48 
setUserId(int userId)49     public void setUserId(int userId) {
50         mUserId = userId;
51     }
52 
53     @Override
isChecked()54     public boolean isChecked() {
55         return Settings.Secure.getIntForUser(mContext.getContentResolver(),
56                 BIOMETRIC_KEYGUARD_ENABLED, DEFAULT, mUserId) == ON;
57     }
58 
59     @Override
setChecked(boolean isChecked)60     public boolean setChecked(boolean isChecked) {
61         return Settings.Secure.putIntForUser(mContext.getContentResolver(),
62                 BIOMETRIC_KEYGUARD_ENABLED, isChecked ? ON : OFF, mUserId);
63     }
64 
65     @Override
getAvailabilityStatus()66     public int getAvailabilityStatus() {
67         final ActiveUnlockStatusUtils activeUnlockStatusUtils =
68                 new ActiveUnlockStatusUtils(mContext);
69         if (activeUnlockStatusUtils.isAvailable()) {
70             return getAvailabilityFromRestrictingAdmin();
71         }
72         if (!Utils.isMultipleBiometricsSupported(mContext)) {
73             return UNSUPPORTED_ON_DEVICE;
74         }
75         return getAvailabilityFromRestrictingAdmin();
76     }
77 
getAvailabilityFromRestrictingAdmin()78     private int getAvailabilityFromRestrictingAdmin() {
79         return getRestrictingAdmin() != null ? DISABLED_FOR_USER : AVAILABLE;
80     }
81 
82     @Override
isSliceable()83     public final boolean isSliceable() {
84         return false;
85     }
86 
87     @Override
getSliceHighlightMenuRes()88     public int getSliceHighlightMenuRes() {
89         // not needed since it's not sliceable
90         return NO_RES;
91     }
92 }
93