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 static android.provider.Settings.Secure.FACE_UNLOCK_ALWAYS_REQUIRE_CONFIRMATION;
20 
21 import android.content.Context;
22 import android.hardware.biometrics.SensorProperties;
23 import android.hardware.face.FaceManager;
24 import android.hardware.face.FaceSensorProperties;
25 import android.provider.Settings;
26 
27 import androidx.preference.Preference;
28 
29 import com.android.settings.R;
30 import com.android.settings.Utils;
31 
32 import java.util.List;
33 
34 /**
35  * Preference controller giving the user an option to always require confirmation.
36  */
37 public class FaceSettingsConfirmPreferenceController extends FaceSettingsPreferenceController {
38 
39     static final String KEY = "security_settings_face_require_confirmation";
40 
41     private static final int ON = 1;
42     private static final int OFF = 0;
43     private static final int DEFAULT = OFF;
44 
45     private FaceManager mFaceManager;
46 
FaceSettingsConfirmPreferenceController(Context context)47     public FaceSettingsConfirmPreferenceController(Context context) {
48         this(context, KEY);
49     }
50 
FaceSettingsConfirmPreferenceController(Context context, String preferenceKey)51     public FaceSettingsConfirmPreferenceController(Context context,
52             String preferenceKey) {
53         super(context, preferenceKey);
54         mFaceManager = Utils.getFaceManagerOrNull(context);
55     }
56 
57     @Override
isChecked()58     public boolean isChecked() {
59         return Settings.Secure.getIntForUser(mContext.getContentResolver(),
60                 FACE_UNLOCK_ALWAYS_REQUIRE_CONFIRMATION, DEFAULT, getUserId()) == ON;
61     }
62 
63     @Override
setChecked(boolean isChecked)64     public boolean setChecked(boolean isChecked) {
65         return Settings.Secure.putIntForUser(mContext.getContentResolver(),
66                 FACE_UNLOCK_ALWAYS_REQUIRE_CONFIRMATION, isChecked ? ON : OFF, getUserId());
67     }
68 
69     @Override
updateState(Preference preference)70     public void updateState(Preference preference) {
71         super.updateState(preference);
72         if (!FaceSettings.isFaceHardwareDetected(mContext)) {
73             preference.setEnabled(false);
74         } else if (!mFaceManager.hasEnrolledTemplates(getUserId())) {
75             preference.setEnabled(false);
76         } else if (getRestrictingAdmin() != null) {
77             preference.setEnabled(false);
78         } else {
79             preference.setEnabled(true);
80             // Update summary for private space face settings toggle
81             if (Utils.isPrivateProfile(getUserId(), mContext)) {
82                 preference.setSummary(mContext.getString(
83                         R.string.private_space_face_settings_require_confirmation_details));
84             }
85         }
86     }
87 
88     @Override
getAvailabilityStatus()89     public int getAvailabilityStatus() {
90         List<FaceSensorProperties> properties = mFaceManager.getSensorProperties();
91         // If a sensor is convenience, it is possible that it becomes weak or strong with
92         // an update. For this reason, the sensor is conditionally unavailable.
93         if (!properties.isEmpty()
94                 && properties.get(0).getSensorStrength() == SensorProperties.STRENGTH_CONVENIENCE) {
95             return CONDITIONALLY_UNAVAILABLE;
96         } else {
97             return AVAILABLE;
98         }
99     }
100 }
101