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 android.content.Context;
20 import android.hardware.face.FaceManager;
21 import android.hardware.face.FaceManager.GetFeatureCallback;
22 import android.hardware.face.FaceManager.SetFeatureCallback;
23 import android.provider.Settings;
24 
25 import androidx.preference.PreferenceScreen;
26 import androidx.preference.SwitchPreference;
27 
28 import com.android.settings.Utils;
29 
30 /**
31  * Preference controller that manages the ability to use face authentication with/without
32  * user attention. See {@link FaceManager#setRequireAttention(boolean, byte[])}.
33  */
34 public class FaceSettingsAttentionPreferenceController extends FaceSettingsPreferenceController {
35 
36     public static final String KEY = "security_settings_face_require_attention";
37 
38     private byte[] mToken;
39     private FaceManager mFaceManager;
40     private SwitchPreference mPreference;
41 
42     private final SetFeatureCallback mSetFeatureCallback = new SetFeatureCallback() {
43         @Override
44         public void onCompleted(boolean success, int feature) {
45             if (feature == FaceManager.FEATURE_REQUIRE_ATTENTION) {
46                 mPreference.setEnabled(true);
47                 if (!success) {
48                     mPreference.setChecked(!mPreference.isChecked());
49                 } else {
50                     Settings.Secure.putIntForUser(mContext.getContentResolver(),
51                             Settings.Secure.FACE_UNLOCK_ATTENTION_REQUIRED,
52                             mPreference.isChecked() ? 1 : 0, getUserId());
53                 }
54             }
55         }
56     };
57 
58     private final GetFeatureCallback mGetFeatureCallback = new GetFeatureCallback() {
59         @Override
60         public void onCompleted(boolean success, int feature, boolean value) {
61             if (feature == FaceManager.FEATURE_REQUIRE_ATTENTION && success) {
62                 if (!mFaceManager.hasEnrolledTemplates(getUserId())) {
63                     mPreference.setEnabled(false);
64                 } else {
65                     mPreference.setEnabled(true);
66                     mPreference.setChecked(value);
67                 }
68             }
69         }
70     };
71 
FaceSettingsAttentionPreferenceController(Context context, String preferenceKey)72     public FaceSettingsAttentionPreferenceController(Context context, String preferenceKey) {
73         super(context, preferenceKey);
74         mFaceManager = Utils.getFaceManagerOrNull(context);
75     }
76 
FaceSettingsAttentionPreferenceController(Context context)77     public FaceSettingsAttentionPreferenceController(Context context) {
78         this(context, KEY);
79     }
80 
setToken(byte[] token)81     public void setToken(byte[] token) {
82         mToken = token;
83     }
84 
85     /**
86      * Displays preference in this controller.
87      */
88     @Override
displayPreference(PreferenceScreen screen)89     public void displayPreference(PreferenceScreen screen) {
90         super.displayPreference(screen);
91         mPreference = screen.findPreference(KEY);
92     }
93 
94     @Override
isChecked()95     public boolean isChecked() {
96         if (!FaceSettings.isFaceHardwareDetected(mContext)) {
97             return true;
98         }
99         // Set to disabled until we know the true value.
100         mPreference.setEnabled(false);
101         mFaceManager.getFeature(getUserId(), FaceManager.FEATURE_REQUIRE_ATTENTION,
102                 mGetFeatureCallback);
103 
104         // Ideally returns a cached value.
105         return true;
106     }
107 
108     @Override
setChecked(boolean isChecked)109     public boolean setChecked(boolean isChecked) {
110         // Optimistically update state and set to disabled until we know it succeeded.
111         mPreference.setEnabled(false);
112         mPreference.setChecked(isChecked);
113 
114         mFaceManager.setFeature(getUserId(), FaceManager.FEATURE_REQUIRE_ATTENTION, isChecked,
115                 mToken, mSetFeatureCallback);
116         return true;
117     }
118 
119     @Override
isSliceable()120     public boolean isSliceable() {
121         return false;
122     }
123 
124     @Override
getAvailabilityStatus()125     public int getAvailabilityStatus() {
126         return AVAILABLE;
127     }
128 }
129