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