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.fragment.app.Fragment; 24 import androidx.preference.Preference; 25 import androidx.preference.TwoStatePreference; 26 27 import com.android.internal.widget.LockPatternUtils; 28 import com.android.settings.R; 29 import com.android.settings.core.PreferenceControllerMixin; 30 import com.android.settings.password.ChooseLockSettingsHelper; 31 import com.android.settingslib.core.AbstractPreferenceController; 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 Fragment mParentFragment; 44 AutoPinConfirmPreferenceController(Context context, int userId, LockPatternUtils lockPatternUtils, Fragment parentFragment)45 public AutoPinConfirmPreferenceController(Context context, int userId, 46 LockPatternUtils lockPatternUtils, Fragment parentFragment) { 47 super(context); 48 mUserId = userId; 49 mLockPatternUtils = lockPatternUtils; 50 mParentFragment = parentFragment; 51 } 52 53 @Override onPreferenceChange(Preference preference, Object newValue)54 public boolean onPreferenceChange(Preference preference, Object newValue) { 55 launchPinConfirmActivity((boolean) newValue); 56 return true; 57 } 58 59 @Override updateState(Preference preference)60 public void updateState(Preference preference) { 61 ((TwoStatePreference) preference).setChecked(getPinAutoConfirmSettingState()); 62 } 63 64 @Override isAvailable()65 public boolean isAvailable() { 66 return LockPatternUtils.isAutoPinConfirmFeatureAvailable() && isPinLock() 67 && isPinLengthEligibleForAutoConfirmation(); 68 } 69 70 @Override getPreferenceKey()71 public String getPreferenceKey() { 72 return PREF_KEY_PIN_AUTO_CONFIRM; 73 } 74 isPinLock()75 private boolean isPinLock() { 76 return mLockPatternUtils.getCredentialTypeForUser(mUserId) 77 == LockPatternUtils.CREDENTIAL_TYPE_PIN; 78 } 79 isPinLengthEligibleForAutoConfirmation()80 private boolean isPinLengthEligibleForAutoConfirmation() { 81 return mLockPatternUtils.getPinLength(mUserId) >= MIN_AUTO_PIN_REQUIREMENT_LENGTH; 82 } 83 getPinAutoConfirmSettingState()84 private boolean getPinAutoConfirmSettingState() { 85 return mLockPatternUtils.isAutoPinConfirmEnabled(mUserId); 86 } 87 setPinAutoConfirmSettingState(boolean state)88 private void setPinAutoConfirmSettingState(boolean state) { 89 mLockPatternUtils.setAutoPinConfirm(state, mUserId); 90 } 91 launchPinConfirmActivity(boolean newState)92 private void launchPinConfirmActivity(boolean newState) { 93 new ChooseLockSettingsHelper.Builder(mParentFragment.getActivity(), mParentFragment) 94 .setUserId(mUserId) 95 .setRequestCode(newState 96 ? ScreenLockSettings.AUTO_PIN_SETTING_ENABLING_REQUEST_CODE 97 : ScreenLockSettings.AUTO_PIN_SETTING_DISABLING_REQUEST_CODE) 98 .setDescription(newState 99 ? mContext.getString(R.string.auto_confirm_on_pin_verify_description) 100 : mContext.getString(R.string.auto_confirm_off_pin_verify_description)) 101 .setReturnCredentials(true) 102 .show(); 103 } 104 } 105