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_APP_ENABLED;
19 
20 import android.app.admin.DevicePolicyManager;
21 import android.content.Context;
22 import android.hardware.face.FaceManager;
23 import android.hardware.fingerprint.FingerprintManager;
24 import android.provider.Settings;
25 
26 import com.android.settings.Utils;
27 import com.android.settings.biometrics.activeunlock.ActiveUnlockStatusUtils;
28 import com.android.settings.core.TogglePreferenceController;
29 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
30 import com.android.settingslib.RestrictedLockUtilsInternal;
31 
32 /**
33  * Preference controller that controls whether the biometrics authentication to be used in apps.
34  */
35 public class BiometricSettingsAppPreferenceController extends TogglePreferenceController {
36     private static final int ON = 1;
37     private static final int OFF = 0;
38     private static final int DEFAULT = ON;
39 
40     private int mUserId;
41     private FaceManager mFaceManager;
42     private FingerprintManager mFingerprintManager;
43 
BiometricSettingsAppPreferenceController(Context context, String key)44     public BiometricSettingsAppPreferenceController(Context context, String key) {
45         super(context, key);
46         mFaceManager = Utils.getFaceManagerOrNull(context);
47         mFingerprintManager = Utils.getFingerprintManagerOrNull(context);
48     }
49 
getRestrictingAdmin()50     protected EnforcedAdmin getRestrictingAdmin() {
51         return RestrictedLockUtilsInternal.checkIfKeyguardFeaturesDisabled(mContext,
52                 DevicePolicyManager.KEYGUARD_DISABLE_BIOMETRICS, mUserId);
53     }
54 
setUserId(int userId)55     public void setUserId(int userId) {
56         mUserId = userId;
57     }
58 
59     @Override
isChecked()60     public boolean isChecked() {
61         return Settings.Secure.getIntForUser(mContext.getContentResolver(), BIOMETRIC_APP_ENABLED,
62                 DEFAULT, mUserId) == ON;
63     }
64 
65     @Override
setChecked(boolean isChecked)66     public boolean setChecked(boolean isChecked) {
67         return Settings.Secure.putIntForUser(mContext.getContentResolver(), BIOMETRIC_APP_ENABLED,
68                 isChecked ? ON : OFF, mUserId);
69     }
70 
71     @Override
getAvailabilityStatus()72     public int getAvailabilityStatus() {
73         final ActiveUnlockStatusUtils activeUnlockStatusUtils =
74                 new ActiveUnlockStatusUtils(mContext);
75         if (!Utils.isMultipleBiometricsSupported(mContext)
76                 && !activeUnlockStatusUtils.isAvailable()) {
77             return UNSUPPORTED_ON_DEVICE;
78         }
79         if (mFaceManager == null || mFingerprintManager == null) {
80             return AVAILABLE_UNSEARCHABLE;
81         }
82         // This preference will be available only if the user has registered either face auth
83         // or fingerprint.
84         final boolean hasFaceEnrolledUser = mFaceManager.hasEnrolledTemplates(mUserId);
85         final boolean hasFingerprintEnrolledUser =
86                 mFingerprintManager.hasEnrolledTemplates(mUserId);
87         if (hasFaceEnrolledUser || hasFingerprintEnrolledUser) {
88             return AVAILABLE;
89         } else {
90             return AVAILABLE_UNSEARCHABLE;
91         }
92     }
93 
94     @Override
isSliceable()95     public final boolean isSliceable() {
96         return false;
97     }
98 
99     @Override
getSliceHighlightMenuRes()100     public int getSliceHighlightMenuRes() {
101         // not needed since it's not sliceable
102         return NO_RES;
103     }
104 }
105