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