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