1 /* 2 * Copyright (C) 2015 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; 18 19 import android.app.Activity; 20 import android.app.settings.SettingsEnums; 21 import android.content.ContentResolver; 22 import android.content.Context; 23 import android.content.res.Resources; 24 import android.os.Bundle; 25 import android.provider.Settings; 26 import android.provider.Settings.Global; 27 import android.telephony.SubscriptionInfo; 28 import android.telephony.SubscriptionManager; 29 import android.telephony.euicc.EuiccManager; 30 import android.text.TextUtils; 31 import android.util.Log; 32 import android.view.LayoutInflater; 33 import android.view.View; 34 import android.view.View.OnClickListener; 35 import android.view.ViewGroup; 36 import android.widget.ArrayAdapter; 37 import android.widget.Button; 38 import android.widget.CheckBox; 39 import android.widget.Spinner; 40 41 import androidx.activity.result.ActivityResult; 42 import androidx.activity.result.ActivityResultLauncher; 43 import androidx.activity.result.contract.ActivityResultContracts; 44 import androidx.annotation.Nullable; 45 import androidx.annotation.VisibleForTesting; 46 47 import com.android.internal.telephony.flags.Flags; 48 import com.android.settings.core.InstrumentedFragment; 49 import com.android.settings.core.SubSettingLauncher; 50 import com.android.settings.network.ResetNetworkRestrictionViewBuilder; 51 import com.android.settings.network.SubscriptionUtil; 52 import com.android.settings.network.telephony.EuiccRacConnectivityDialogActivity; 53 import com.android.settings.password.ChooseLockSettingsHelper; 54 import com.android.settings.password.ConfirmLockPattern; 55 import com.android.settingslib.development.DevelopmentSettingsEnabler; 56 57 import java.util.ArrayList; 58 import java.util.Collections; 59 import java.util.List; 60 import java.util.Optional; 61 62 /** 63 * Confirm and execute a reset of the device's network settings to a clean "just out of the box" 64 * state. Multiple confirmations are required: first, a general "are you sure you want to do this?" 65 * prompt, followed by a keyguard pattern trace if the user has defined one, followed by a final 66 * strongly-worded "THIS WILL RESET EVERYTHING" prompt. If at any time the phone is allowed to go 67 * to sleep, is locked, et cetera, then the confirmation sequence is abandoned. 68 * 69 * This is the initial screen. 70 */ 71 public class ResetNetwork extends InstrumentedFragment { 72 private static final String TAG = "ResetNetwork"; 73 74 // Arbitrary to avoid conficts 75 private static final int KEYGUARD_REQUEST = 55; 76 77 private ActivityResultLauncher mActivityResultLauncher; 78 private List<SubscriptionInfo> mSubscriptions; 79 80 private View mContentView; 81 private Spinner mSubscriptionSpinner; 82 private Button mInitiateButton; 83 @VisibleForTesting View mEsimContainer; 84 @VisibleForTesting CheckBox mEsimCheckbox; 85 86 @Override onCreate(@ullable Bundle savedInstanceState)87 public void onCreate(@Nullable Bundle savedInstanceState) { 88 super.onCreate(savedInstanceState); 89 getActivity().setTitle(R.string.reset_mobile_network_settings_title); 90 91 mActivityResultLauncher = registerForActivityResult( 92 new ActivityResultContracts.StartActivityForResult(), 93 result -> onActivityLauncherResult(result)); 94 } 95 96 /** 97 * Keyguard validation is run using the standard {@link ConfirmLockPattern} 98 * component as a subactivity 99 * @param request the request code to be returned once confirmation finishes 100 * @return true if confirmation launched 101 */ runKeyguardConfirmation(int request)102 private boolean runKeyguardConfirmation(int request) { 103 Resources res = getActivity().getResources(); 104 final ChooseLockSettingsHelper.Builder builder = 105 new ChooseLockSettingsHelper.Builder(getActivity(), this); 106 return builder.setRequestCode(request) 107 .setTitle(res.getText(R.string.reset_mobile_network_settings_title)) 108 .setActivityResultLauncher(mActivityResultLauncher) 109 .show(); 110 } 111 onActivityLauncherResult(ActivityResult result)112 public void onActivityLauncherResult(ActivityResult result) { 113 // If the user entered a valid keyguard trace, present the final 114 // confirmation prompt; otherwise, go back to the initial state. 115 if (result.getResultCode() == Activity.RESULT_OK) { 116 showFinalConfirmation(); 117 } else if (mContentView != null) { 118 establishInitialState(getActiveSubscriptionInfoList()); 119 } 120 } 121 122 @VisibleForTesting showFinalConfirmation()123 void showFinalConfirmation() { 124 Bundle args = new Bundle(); 125 Context context = getContext(); 126 boolean resetSims = false; 127 128 // TODO(b/317276437) Simplify the logic once flag is released 129 int resetOptions = ResetNetworkRequest.RESET_CONNECTIVITY_MANAGER 130 | ResetNetworkRequest.RESET_VPN_MANAGER; 131 if (Flags.resetMobileNetworkSettings()) { 132 resetOptions |= ResetNetworkRequest.RESET_IMS_STACK; 133 resetOptions |= ResetNetworkRequest.RESET_PHONE_PROCESS; 134 resetOptions |= ResetNetworkRequest.RESET_RILD; 135 } 136 ResetNetworkRequest request = new ResetNetworkRequest(resetOptions); 137 if (mSubscriptions != null && mSubscriptions.size() > 0) { 138 int selectedIndex = mSubscriptionSpinner.getSelectedItemPosition(); 139 SubscriptionInfo subscription = mSubscriptions.get(selectedIndex); 140 int subId = subscription.getSubscriptionId(); 141 request.setResetTelephonyAndNetworkPolicyManager(subId) 142 .setResetApn(subId); 143 if (Flags.resetMobileNetworkSettings()) { 144 request.setResetImsSubId(subId); 145 } 146 } 147 if (mEsimContainer.getVisibility() == View.VISIBLE && mEsimCheckbox.isChecked()) { 148 resetSims = true; 149 request.setResetEsim(context.getPackageName()).writeIntoBundle(args); 150 } else { 151 request.writeIntoBundle(args); 152 } 153 154 SubSettingLauncher launcher = 155 new SubSettingLauncher(context) 156 .setDestination(ResetNetworkConfirm.class.getName()) 157 .setArguments(args) 158 .setTitleRes(R.string.reset_mobile_network_settings_confirm_title) 159 .setSourceMetricsCategory(getMetricsCategory()); 160 161 if (resetSims && SubscriptionUtil.shouldShowRacDialogWhenErasingAllEsims(context)) { 162 context.startActivity( 163 EuiccRacConnectivityDialogActivity.getIntent(context, launcher.toIntent())); 164 } else { 165 launcher.launch(); 166 } 167 } 168 169 /** 170 * If the user clicks to begin the reset sequence, we next require a 171 * keyguard confirmation if the user has currently enabled one. If there 172 * is no keyguard available, we simply go to the final confirmation prompt. 173 */ 174 private final Button.OnClickListener mInitiateListener = new Button.OnClickListener() { 175 176 @Override 177 public void onClick(View v) { 178 if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) { 179 showFinalConfirmation(); 180 } 181 } 182 }; 183 184 /** 185 * In its initial state, the activity presents a button for the user to 186 * click in order to initiate a confirmation sequence. This method is 187 * called from various other points in the code to reset the activity to 188 * this base state. 189 * 190 * <p>Reinflating views from resources is expensive and prevents us from 191 * caching widget pointers, so we use a single-inflate pattern: we lazy- 192 * inflate each view, caching all of the widget pointers we'll need at the 193 * time, then simply reuse the inflated views directly whenever we need 194 * to change contents. 195 * 196 * @param subscriptionsList is a list of SubscriptionInfo(s) which allow user to select from 197 */ establishInitialState(List<SubscriptionInfo> subscriptionsList)198 private void establishInitialState(List<SubscriptionInfo> subscriptionsList) { 199 mSubscriptionSpinner = (Spinner) mContentView.findViewById(R.id.reset_network_subscription); 200 mEsimContainer = mContentView.findViewById(R.id.erase_esim_container); 201 mEsimCheckbox = mContentView.findViewById(R.id.erase_esim); 202 203 mSubscriptions = subscriptionsList; 204 if (mSubscriptions != null && mSubscriptions.size() > 0) { 205 // Get the default subscription in the order of data, voice, sms, first up. 206 int defaultSubscription = SubscriptionManager.getDefaultDataSubscriptionId(); 207 if (!SubscriptionManager.isUsableSubscriptionId(defaultSubscription)) { 208 defaultSubscription = SubscriptionManager.getDefaultVoiceSubscriptionId(); 209 } 210 if (!SubscriptionManager.isUsableSubscriptionId(defaultSubscription)) { 211 defaultSubscription = SubscriptionManager.getDefaultSmsSubscriptionId(); 212 } 213 if (!SubscriptionManager.isUsableSubscriptionId(defaultSubscription)) { 214 defaultSubscription = SubscriptionManager.getDefaultSubscriptionId(); 215 } 216 217 int selectedIndex = 0; 218 int size = mSubscriptions.size(); 219 List<String> subscriptionNames = new ArrayList<>(); 220 for (SubscriptionInfo record : mSubscriptions) { 221 if (record.getSubscriptionId() == defaultSubscription) { 222 // Set the first selected value to the default 223 selectedIndex = subscriptionNames.size(); 224 } 225 String name = SubscriptionUtil.getUniqueSubscriptionDisplayName( 226 record, getContext()).toString(); 227 if (TextUtils.isEmpty(name)) { 228 name = record.getNumber(); 229 } 230 if (TextUtils.isEmpty(name)) { 231 CharSequence carrierName = record.getCarrierName(); 232 name = TextUtils.isEmpty(carrierName) ? "" : carrierName.toString(); 233 } 234 if (TextUtils.isEmpty(name)) { 235 name = String.format("MCC:%s MNC:%s Slot:%s Id:%s", record.getMcc(), 236 record.getMnc(), record.getSimSlotIndex(), record.getSubscriptionId()); 237 } 238 subscriptionNames.add(name); 239 } 240 ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), 241 android.R.layout.simple_spinner_item, subscriptionNames); 242 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 243 mSubscriptionSpinner.setAdapter(adapter); 244 mSubscriptionSpinner.setSelection(selectedIndex); 245 if (mSubscriptions.size() > 1) { 246 mSubscriptionSpinner.setVisibility(View.VISIBLE); 247 } else { 248 mSubscriptionSpinner.setVisibility(View.INVISIBLE); 249 } 250 } else { 251 mSubscriptionSpinner.setVisibility(View.INVISIBLE); 252 } 253 mInitiateButton = (Button) mContentView.findViewById(R.id.initiate_reset_network); 254 mInitiateButton.setOnClickListener(mInitiateListener); 255 if (showEuiccSettings(getContext())) { 256 mEsimContainer.setVisibility(View.VISIBLE); 257 mEsimContainer.setOnClickListener(new OnClickListener() { 258 @Override 259 public void onClick(View v) { 260 mEsimCheckbox.toggle(); 261 } 262 }); 263 } else { 264 mEsimCheckbox.setChecked(false /* checked */); 265 } 266 } 267 getActiveSubscriptionInfoList()268 private List<SubscriptionInfo> getActiveSubscriptionInfoList() { 269 if (!SubscriptionUtil.isSimHardwareVisible(getActivity())) { 270 return Collections.emptyList(); 271 } 272 SubscriptionManager mgr = getActivity().getSystemService(SubscriptionManager.class); 273 if (mgr == null) { 274 Log.w(TAG, "No SubscriptionManager"); 275 return Collections.emptyList(); 276 } 277 return Optional.ofNullable(mgr.getActiveSubscriptionInfoList()) 278 .orElse(Collections.emptyList()); 279 } 280 281 @Override onResume()282 public void onResume() { 283 super.onResume(); 284 285 if (mContentView == null) { 286 return; 287 } 288 289 // update options if subcription has been changed 290 List<SubscriptionInfo> updatedSubscriptions = getActiveSubscriptionInfoList(); 291 if ((mSubscriptions != null) 292 && (mSubscriptions.size() == updatedSubscriptions.size()) 293 && mSubscriptions.containsAll(updatedSubscriptions)) { 294 return; 295 } 296 Log.d(TAG, "subcription list changed"); 297 establishInitialState(updatedSubscriptions); 298 } 299 showEuiccSettings(Context context)300 private boolean showEuiccSettings(Context context) { 301 if (!SubscriptionUtil.isSimHardwareVisible(context)) { 302 return false; 303 } 304 EuiccManager euiccManager = 305 (EuiccManager) context.getSystemService(Context.EUICC_SERVICE); 306 if (euiccManager == null || !euiccManager.isEnabled()) { 307 return false; 308 } 309 ContentResolver resolver = context.getContentResolver(); 310 return Settings.Global.getInt(resolver, Global.EUICC_PROVISIONED, 0) != 0 311 || DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(context); 312 } 313 314 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)315 public View onCreateView(LayoutInflater inflater, ViewGroup container, 316 Bundle savedInstanceState) { 317 View view = (new ResetNetworkRestrictionViewBuilder(getActivity())).build(); 318 if (view != null) { 319 Log.w(TAG, "Access deny."); 320 return view; 321 } 322 323 mContentView = inflater.inflate(R.layout.reset_mobile_network_settings, null); 324 325 establishInitialState(getActiveSubscriptionInfoList()); 326 return mContentView; 327 } 328 329 @Override getMetricsCategory()330 public int getMetricsCategory() { 331 return SettingsEnums.RESET_NETWORK; 332 } 333 } 334