1 /*
2  * Copyright (C) 2022 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.fingerprint;
18 
19 import android.content.Context;
20 import android.hardware.fingerprint.FingerprintManager;
21 import android.os.UserHandle;
22 import android.provider.Settings;
23 
24 import androidx.preference.Preference;
25 
26 import com.android.internal.annotations.VisibleForTesting;
27 import com.android.settings.Utils;
28 import com.android.settings.search.BaseSearchIndexProvider;
29 import com.android.settingslib.search.SearchIndexable;
30 
31 /**
32  * Preference controller that controls whether a SFPS device is required to be interactive for
33  * fingerprint authentication to unlock the device.
34  */
35 @SearchIndexable
36 public class FingerprintSettingsRequireScreenOnToAuthPreferenceController
37         extends FingerprintSettingsPreferenceController {
38     private static final String TAG =
39             "FingerprintSettingsRequireScreenOnToAuthPreferenceController";
40 
41     @VisibleForTesting
42     protected FingerprintManager mFingerprintManager;
43 
FingerprintSettingsRequireScreenOnToAuthPreferenceController( Context context, String prefKey)44     public FingerprintSettingsRequireScreenOnToAuthPreferenceController(
45             Context context, String prefKey) {
46         super(context, prefKey);
47         mFingerprintManager = Utils.getFingerprintManagerOrNull(context);
48     }
49 
50     @Override
isChecked()51     public boolean isChecked() {
52         if (!FingerprintSettings.isFingerprintHardwareDetected(mContext)) {
53             return false;
54         } else if (getRestrictingAdmin() != null) {
55             return false;
56         }
57         int toReturn = Settings.Secure.getIntForUser(
58                 mContext.getContentResolver(),
59                 Settings.Secure.SFPS_PERFORMANT_AUTH_ENABLED,
60                 -1,
61                 getUserHandle());
62         if (toReturn == -1) {
63             toReturn = mContext.getResources().getBoolean(
64                     com.android.internal.R.bool.config_performantAuthDefault) ? 1 : 0;
65             Settings.Secure.putIntForUser(mContext.getContentResolver(),
66                     Settings.Secure.SFPS_PERFORMANT_AUTH_ENABLED, toReturn, getUserHandle());
67         }
68 
69         return toReturn == 1;
70     }
71 
72     @Override
setChecked(boolean isChecked)73     public boolean setChecked(boolean isChecked) {
74         Settings.Secure.putIntForUser(
75                 mContext.getContentResolver(),
76                 Settings.Secure.SFPS_PERFORMANT_AUTH_ENABLED,
77                 isChecked ? 1 : 0,
78                 getUserHandle());
79         return true;
80     }
81 
82     @Override
updateState(Preference preference)83     public void updateState(Preference preference) {
84         super.updateState(preference);
85         if (!FingerprintSettings.isFingerprintHardwareDetected(mContext)) {
86             preference.setEnabled(false);
87         } else if (!mFingerprintManager.hasEnrolledTemplates(getUserId())) {
88             preference.setEnabled(false);
89         } else if (getRestrictingAdmin() != null) {
90             preference.setEnabled(false);
91         } else {
92             preference.setEnabled(true);
93         }
94     }
95 
96     @Override
getAvailabilityStatus()97     public int getAvailabilityStatus() {
98         if (mFingerprintManager != null
99                 && mFingerprintManager.isHardwareDetected()
100                 && mFingerprintManager.isPowerbuttonFps()) {
101             return mFingerprintManager.hasEnrolledTemplates(getUserId())
102                     ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
103         } else {
104             return UNSUPPORTED_ON_DEVICE;
105         }
106     }
107 
getUserHandle()108     private int getUserHandle() {
109         return UserHandle.of(getUserId()).getIdentifier();
110     }
111 
112     /**
113      * This feature is not directly searchable.
114      */
115     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
116             new BaseSearchIndexProvider() {};
117 
118 }
119