1 /* 2 * Copyright (C) 2023 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.security.screenlock; 18 19 import static com.android.internal.widget.LockPatternUtils.MIN_AUTO_PIN_REQUIREMENT_LENGTH; 20 21 import android.content.Context; 22 23 import androidx.preference.Preference; 24 import androidx.preference.TwoStatePreference; 25 26 import com.android.internal.widget.LockPatternUtils; 27 import com.android.settings.R; 28 import com.android.settings.core.PreferenceControllerMixin; 29 import com.android.settings.password.ChooseLockSettingsHelper; 30 import com.android.settingslib.core.AbstractPreferenceController; 31 import com.android.settingslib.core.lifecycle.ObservablePreferenceFragment; 32 33 /** 34 * Preference controller for the pin_auto_confirm setting. 35 */ 36 public class AutoPinConfirmPreferenceController extends AbstractPreferenceController implements 37 PreferenceControllerMixin, Preference.OnPreferenceChangeListener { 38 39 private static final String PREF_KEY_PIN_AUTO_CONFIRM = "auto_pin_confirm"; 40 41 private final int mUserId; 42 private final LockPatternUtils mLockPatternUtils; 43 private final ObservablePreferenceFragment mParentFragment; 44 AutoPinConfirmPreferenceController(Context context, int userId, LockPatternUtils lockPatternUtils, ObservablePreferenceFragment parentFragment)45 public AutoPinConfirmPreferenceController(Context context, int userId, 46 LockPatternUtils lockPatternUtils, 47 ObservablePreferenceFragment parentFragment) { 48 super(context); 49 mUserId = userId; 50 mLockPatternUtils = lockPatternUtils; 51 mParentFragment = parentFragment; 52 } 53 54 @Override onPreferenceChange(Preference preference, Object newValue)55 public boolean onPreferenceChange(Preference preference, Object newValue) { 56 launchPinConfirmActivity((boolean) newValue); 57 return true; 58 } 59 60 @Override updateState(Preference preference)61 public void updateState(Preference preference) { 62 ((TwoStatePreference) preference).setChecked(getPinAutoConfirmSettingState()); 63 } 64 65 @Override isAvailable()66 public boolean isAvailable() { 67 return LockPatternUtils.isAutoPinConfirmFeatureAvailable() && isPinLock() 68 && isPinLengthEligibleForAutoConfirmation(); 69 } 70 71 @Override getPreferenceKey()72 public String getPreferenceKey() { 73 return PREF_KEY_PIN_AUTO_CONFIRM; 74 } 75 isPinLock()76 private boolean isPinLock() { 77 return mLockPatternUtils.getCredentialTypeForUser(mUserId) 78 == LockPatternUtils.CREDENTIAL_TYPE_PIN; 79 } 80 isPinLengthEligibleForAutoConfirmation()81 private boolean isPinLengthEligibleForAutoConfirmation() { 82 return mLockPatternUtils.getPinLength(mUserId) >= MIN_AUTO_PIN_REQUIREMENT_LENGTH; 83 } 84 getPinAutoConfirmSettingState()85 private boolean getPinAutoConfirmSettingState() { 86 return mLockPatternUtils.isAutoPinConfirmEnabled(mUserId); 87 } 88 setPinAutoConfirmSettingState(boolean state)89 private void setPinAutoConfirmSettingState(boolean state) { 90 mLockPatternUtils.setAutoPinConfirm(state, mUserId); 91 } 92 launchPinConfirmActivity(boolean newState)93 private void launchPinConfirmActivity(boolean newState) { 94 new ChooseLockSettingsHelper.Builder(mParentFragment.getActivity(), mParentFragment) 95 .setUserId(mUserId) 96 .setRequestCode(newState 97 ? ScreenLockSettings.AUTO_PIN_SETTING_ENABLING_REQUEST_CODE 98 : ScreenLockSettings.AUTO_PIN_SETTING_DISABLING_REQUEST_CODE) 99 .setTitle(mContext.getString(R.string.lock_screen_auto_pin_confirm_title)) 100 .setDescription(newState 101 ? mContext.getString(R.string.auto_confirm_on_pin_verify_description) 102 : mContext.getString(R.string.auto_confirm_off_pin_verify_description)) 103 .setReturnCredentials(true) 104 .show(); 105 } 106 } 107