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