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 android.content.Context 20 import android.provider.Settings 21 import androidx.preference.Preference 22 import androidx.preference.TwoStatePreference 23 import com.android.internal.widget.LockPatternUtils 24 import com.android.internal.widget.LockPatternUtils.* 25 import com.android.settings.core.PreferenceControllerMixin 26 import com.android.settingslib.core.AbstractPreferenceController 27 28 class PinPrivacyPreferenceController( 29 context: Context, 30 private val userId: Int, 31 private val lockPatternUtils: LockPatternUtils 32 ) : AbstractPreferenceController(context), PreferenceControllerMixin, 33 Preference.OnPreferenceChangeListener { 34 35 companion object { 36 private const val PREF_KEY = "enhancedPinPrivacy" 37 } 38 isAvailablenull39 override fun isAvailable(): Boolean { 40 val credentialType = lockPatternUtils.getCredentialTypeForUser(userId) 41 return credentialType == CREDENTIAL_TYPE_PIN 42 } 43 getPreferenceKeynull44 override fun getPreferenceKey(): String { 45 return PREF_KEY 46 } 47 onPreferenceChangenull48 override fun onPreferenceChange(preference: Preference, value: Any): Boolean { 49 lockPatternUtils.setPinEnhancedPrivacyEnabled((value as Boolean), userId) 50 return true 51 } 52 updateStatenull53 override fun updateState(preference: Preference) { 54 (preference as TwoStatePreference).isChecked = getCurrentPreferenceState() 55 } 56 getCurrentPreferenceStatenull57 private fun getCurrentPreferenceState(): Boolean { 58 return lockPatternUtils.isPinEnhancedPrivacyEnabled(userId) 59 } 60 }