• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.settings.biometrics.face;
18 
19 import static android.provider.Settings.Secure.FACE_UNLOCK_KEYGUARD_ENABLED;
20 
21 import android.content.Context;
22 import android.hardware.face.FaceManager;
23 import android.provider.Settings;
24 
25 import androidx.preference.Preference;
26 
27 import com.android.settings.Utils;
28 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
29 import com.android.settingslib.RestrictedSwitchPreference;
30 
31 /**
32  * Preference controller for Face settings page controlling the ability to unlock the phone
33  * with face.
34  */
35 public class FaceSettingsKeyguardPreferenceController extends FaceSettingsPreferenceController {
36 
37     static final String KEY = "security_settings_face_keyguard";
38 
39     private static final int ON = 1;
40     private static final int OFF = 0;
41     private static final int DEFAULT = ON;  // face unlock is enabled on keyguard by default
42 
43     private FaceManager mFaceManager;
44 
FaceSettingsKeyguardPreferenceController(Context context, String preferenceKey)45     public FaceSettingsKeyguardPreferenceController(Context context, String preferenceKey) {
46         super(context, preferenceKey);
47         mFaceManager = Utils.getFaceManagerOrNull(context);
48     }
49 
FaceSettingsKeyguardPreferenceController(Context context)50     public FaceSettingsKeyguardPreferenceController(Context context) {
51         this(context, KEY);
52     }
53 
54     @Override
isChecked()55     public boolean isChecked() {
56         if (!FaceSettings.isFaceHardwareDetected(mContext)) {
57             return false;
58         } else if (getRestrictingAdmin() != null) {
59             return false;
60         }
61         return Settings.Secure.getIntForUser(mContext.getContentResolver(),
62                 FACE_UNLOCK_KEYGUARD_ENABLED, DEFAULT, getUserId()) == ON;
63     }
64 
65     @Override
setChecked(boolean isChecked)66     public boolean setChecked(boolean isChecked) {
67         return Settings.Secure.putIntForUser(mContext.getContentResolver(),
68                 FACE_UNLOCK_KEYGUARD_ENABLED, isChecked ? ON : OFF, getUserId());
69     }
70 
71     @Override
getAvailabilityStatus()72     public int getAvailabilityStatus() {
73         // When the device supports multiple biometrics auth, this preference will be unavailable.
74         return Utils.isMultipleBiometricsSupported(mContext) ? UNSUPPORTED_ON_DEVICE : AVAILABLE;
75     }
76 
77     @Override
updateState(Preference preference)78     public void updateState(Preference preference) {
79         EnforcedAdmin admin;
80         super.updateState(preference);
81         if (!FaceSettings.isFaceHardwareDetected(mContext)) {
82             preference.setEnabled(false);
83         } else if ((admin = getRestrictingAdmin()) != null) {
84             ((RestrictedSwitchPreference) preference).setDisabledByAdmin(admin);
85         } else if (!mFaceManager.hasEnrolledTemplates(getUserId())) {
86             preference.setEnabled(false);
87         } else {
88             preference.setEnabled(true);
89         }
90     }
91 }
92