• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 android.content.Context;
20 import android.content.pm.PackageManager;
21 import android.hardware.face.FaceManager;
22 import android.os.UserHandle;
23 import android.os.UserManager;
24 import android.provider.Settings;
25 
26 import androidx.preference.Preference;
27 
28 import com.android.internal.annotations.VisibleForTesting;
29 import com.android.settings.Utils;
30 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
31 import com.android.settingslib.RestrictedSwitchPreference;
32 
33 public class FaceSettingsLockscreenBypassPreferenceController
34         extends FaceSettingsPreferenceController {
35 
36     @VisibleForTesting
37     protected FaceManager mFaceManager;
38     private UserManager mUserManager;
39 
FaceSettingsLockscreenBypassPreferenceController(Context context, String preferenceKey)40     public FaceSettingsLockscreenBypassPreferenceController(Context context, String preferenceKey) {
41         super(context, preferenceKey);
42         if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_FACE)) {
43             mFaceManager = context.getSystemService(FaceManager.class);
44         }
45 
46         mUserManager = context.getSystemService(UserManager.class);
47     }
48 
49     @Override
isChecked()50     public boolean isChecked() {
51         if (!FaceSettings.isFaceHardwareDetected(mContext)) {
52             return false;
53         } else if (getRestrictingAdmin() != null) {
54             return false;
55         }
56         int defaultValue = mContext.getResources().getBoolean(
57                 com.android.internal.R.bool.config_faceAuthDismissesKeyguard) ? 1 : 0;
58         return Settings.Secure.getIntForUser(mContext.getContentResolver(),
59                 Settings.Secure.FACE_UNLOCK_DISMISSES_KEYGUARD, defaultValue, getUserHandle()) != 0;
60     }
61 
62     @Override
setChecked(boolean isChecked)63     public boolean setChecked(boolean isChecked) {
64         Settings.Secure.putIntForUser(mContext.getContentResolver(),
65                 Settings.Secure.FACE_UNLOCK_DISMISSES_KEYGUARD, isChecked ? 1 : 0, getUserHandle());
66         return true;
67     }
68 
69     @Override
updateState(Preference preference)70     public void updateState(Preference preference) {
71         EnforcedAdmin admin;
72         super.updateState(preference);
73         if (!FaceSettings.isFaceHardwareDetected(mContext)) {
74             preference.setEnabled(false);
75         } else if ((admin = getRestrictingAdmin()) != null) {
76             ((RestrictedSwitchPreference) preference).setDisabledByAdmin(admin);
77         } else if (!mFaceManager.hasEnrolledTemplates(getUserId())) {
78             preference.setEnabled(false);
79         } else {
80             preference.setEnabled(true);
81         }
82     }
83 
84     @Override
getAvailabilityStatus()85     public int getAvailabilityStatus() {
86         // When the device supports multiple biometrics auth, this preference won't be shown
87         // in face unlock category.
88         if (Utils.isMultipleBiometricsSupported(mContext)) {
89             return UNSUPPORTED_ON_DEVICE;
90         }
91         if (mUserManager.isManagedProfile(getUserId())) {
92             return UNSUPPORTED_ON_DEVICE;
93         }
94 
95         if (mFaceManager != null && mFaceManager.isHardwareDetected()) {
96             return mFaceManager.hasEnrolledTemplates(getUserId())
97                     ? AVAILABLE : DISABLED_DEPENDENT_SETTING;
98         } else {
99             return UNSUPPORTED_ON_DEVICE;
100         }
101     }
102 
getUserHandle()103     private int getUserHandle() {
104         return UserHandle.of(getUserId()).getIdentifier();
105     }
106 }
107