1 /* 2 * Copyright (C) 2020 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; 18 19 import android.app.KeyguardManager; 20 import android.app.settings.SettingsEnums; 21 import android.content.Context; 22 import android.provider.Settings; 23 24 import androidx.preference.Preference; 25 import androidx.preference.TwoStatePreference; 26 27 import com.android.settings.R; 28 import com.android.settings.core.BasePreferenceController; 29 import com.android.settings.network.telephony.MobileNetworkUtils; 30 import com.android.settings.overlay.FeatureFactory; 31 import com.android.settings.wifi.dpp.WifiDppUtils; 32 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 33 34 /** Enable/disable user confirmation before deleting an eSim */ 35 public class ConfirmSimDeletionPreferenceController extends BasePreferenceController implements 36 Preference.OnPreferenceChangeListener{ 37 public static final String KEY_CONFIRM_SIM_DELETION = "confirm_sim_deletion"; 38 private boolean mConfirmationDefaultOn; 39 private MetricsFeatureProvider mMetricsFeatureProvider; 40 ConfirmSimDeletionPreferenceController(Context context, String key)41 public ConfirmSimDeletionPreferenceController(Context context, String key) { 42 super(context, key); 43 mConfirmationDefaultOn = 44 context.getResources() 45 .getBoolean(R.bool.config_sim_deletion_confirmation_default_on); 46 mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider(); 47 } 48 49 @Override getAvailabilityStatus()50 public int getAvailabilityStatus() { 51 // hide if eSim is not supported on the device 52 return MobileNetworkUtils.showEuiccSettings(mContext) ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 53 } 54 getGlobalState()55 private boolean getGlobalState() { 56 return Settings.Global.getInt( 57 mContext.getContentResolver(), 58 KEY_CONFIRM_SIM_DELETION, 59 mConfirmationDefaultOn ? 1 : 0) 60 == 1; 61 } 62 isChecked()63 public boolean isChecked() { 64 return getGlobalState(); 65 } 66 setChecked(boolean isChecked)67 public boolean setChecked(boolean isChecked) { 68 Settings.Global.putInt( 69 mContext.getContentResolver(), KEY_CONFIRM_SIM_DELETION, isChecked ? 1 : 0); 70 return true; 71 } 72 73 // handle UI change 74 @Override onPreferenceChange(Preference preference, Object newValue)75 public boolean onPreferenceChange(Preference preference, Object newValue) { 76 if (!preference.getKey().equals(getPreferenceKey())) { 77 return false; 78 } 79 if (!isChecked()) { 80 mMetricsFeatureProvider.action(mContext, 81 SettingsEnums.ACTION_CONFIRM_SIM_DELETION_ON); 82 setChecked(true); 83 return true; 84 } else { 85 // prevent disabling the feature until authorized 86 WifiDppUtils.showLockScreen(mContext, () -> { 87 mMetricsFeatureProvider.action(mContext, 88 SettingsEnums.ACTION_CONFIRM_SIM_DELETION_OFF); 89 // set data 90 setChecked(false); 91 // set UI 92 ((TwoStatePreference) preference).setChecked(false); 93 }); 94 return false; 95 } 96 } 97 98 @Override updateState(Preference preference)99 public void updateState(Preference preference) { 100 final KeyguardManager keyguardManager = mContext.getSystemService(KeyguardManager.class); 101 if (!keyguardManager.isKeyguardSecure()) { 102 preference.setEnabled(false); 103 if (preference instanceof TwoStatePreference) { 104 ((TwoStatePreference) preference).setChecked(false); 105 } 106 preference.setSummary(R.string.disabled_because_no_backup_security); 107 } else { 108 preference.setEnabled(true); 109 if (preference instanceof TwoStatePreference) { 110 ((TwoStatePreference) preference).setChecked(getGlobalState()); 111 } 112 preference.setSummary(R.string.confirm_sim_deletion_description); 113 } 114 } 115 } 116