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_APP_ENABLED;
20 
21 import android.content.Context;
22 import android.hardware.face.FaceManager;
23 import android.provider.Settings;
24 
25 import com.android.settings.Utils;
26 
27 import androidx.preference.Preference;
28 
29 /**
30  * Preference controller for Face settings page controlling the ability to use
31  * Face authentication in apps (through BiometricPrompt).
32  */
33 public class FaceSettingsAppPreferenceController extends FaceSettingsPreferenceController {
34 
35     static final String KEY = "security_settings_face_app";
36 
37     private static final int ON = 1;
38     private static final int OFF = 0;
39     private static final int DEFAULT = ON;  // face unlock is enabled for BiometricPrompt by default
40 
41     private FaceManager mFaceManager;
42 
FaceSettingsAppPreferenceController(Context context, String preferenceKey)43     public FaceSettingsAppPreferenceController(Context context, String preferenceKey) {
44         super(context, preferenceKey);
45         mFaceManager = Utils.getFaceManagerOrNull(context);
46     }
47 
FaceSettingsAppPreferenceController(Context context)48     public FaceSettingsAppPreferenceController(Context context) {
49         this(context, KEY);
50     }
51 
52     @Override
isChecked()53     public boolean isChecked() {
54         if (!FaceSettings.isAvailable(mContext)) {
55             return false;
56         }
57         return Settings.Secure.getIntForUser(
58                 mContext.getContentResolver(), FACE_UNLOCK_APP_ENABLED, DEFAULT, getUserId()) == ON;
59     }
60 
61     @Override
setChecked(boolean isChecked)62     public boolean setChecked(boolean isChecked) {
63         return Settings.Secure.putIntForUser(mContext.getContentResolver(), FACE_UNLOCK_APP_ENABLED,
64                 isChecked ? ON : OFF, getUserId());
65     }
66 
67     @Override
updateState(Preference preference)68     public void updateState(Preference preference) {
69         super.updateState(preference);
70         if (!FaceSettings.isAvailable(mContext)) {
71             preference.setEnabled(false);
72         } else if (!mFaceManager.hasEnrolledTemplates(getUserId())) {
73             preference.setEnabled(false);
74         } else {
75             preference.setEnabled(true);
76         }
77     }
78 
79     @Override
getAvailabilityStatus()80     public int getAvailabilityStatus() {
81         if(mFaceManager == null){
82             return AVAILABLE_UNSEARCHABLE;
83         }
84 
85         // By only allowing this preference controller to be searchable when the feature is turned
86         // off, it will give preference to the face setup controller.
87         final boolean hasEnrolledUser = mFaceManager.hasEnrolledTemplates(getUserId());
88         final boolean appUnlockEnabled = Settings.Secure.getIntForUser(
89                 mContext.getContentResolver(), FACE_UNLOCK_APP_ENABLED, OFF, getUserId()) == ON;
90         if (hasEnrolledUser && !appUnlockEnabled) {
91             return AVAILABLE;
92         } else {
93             return AVAILABLE_UNSEARCHABLE;
94         }
95     }
96 }
97