Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 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 | |
Fan Zhang | c0c3eaa | 2018-03-30 11:33:08 -0700 | [diff] [blame] | 19 | import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; |
| 20 | |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 21 | import android.app.Activity; |
| 22 | import android.app.ProgressDialog; |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 23 | import android.app.settings.SettingsEnums; |
Prathmesh Prabhu | 8d763c0 | 2016-03-11 13:27:17 -0800 | [diff] [blame] | 24 | import android.bluetooth.BluetoothAdapter; |
Ajay Panicker | 568d933 | 2015-07-29 11:39:16 -0700 | [diff] [blame] | 25 | import android.bluetooth.BluetoothManager; |
Malcolm Chen | e9d96b7 | 2017-04-19 16:12:45 -0700 | [diff] [blame] | 26 | import android.content.ContentResolver; |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 27 | import android.content.Context; |
Stuart Scott | 73281fb | 2015-03-30 13:16:29 -0700 | [diff] [blame] | 28 | import android.net.ConnectivityManager; |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 29 | import android.net.NetworkPolicyManager; |
Malcolm Chen | e9d96b7 | 2017-04-19 16:12:45 -0700 | [diff] [blame] | 30 | import android.net.Uri; |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 31 | import android.net.wifi.WifiManager; |
Jimmy Chen | d62453d | 2018-10-11 18:03:51 +0800 | [diff] [blame] | 32 | import android.net.wifi.p2p.WifiP2pManager; |
Qingxi Li | 0caad2f | 2018-01-05 10:52:42 -0800 | [diff] [blame] | 33 | import android.os.AsyncTask; |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 34 | import android.os.Bundle; |
Fan Zhang | c0c3eaa | 2018-03-30 11:33:08 -0700 | [diff] [blame] | 35 | import android.os.RecoverySystem; |
Sudheer Shanka | 7dbbe13 | 2016-02-16 14:19:32 +0000 | [diff] [blame] | 36 | import android.os.UserHandle; |
Stuart Scott | 47ed6ca | 2015-04-20 11:05:43 -0700 | [diff] [blame] | 37 | import android.os.UserManager; |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 38 | import android.telephony.SubscriptionManager; |
| 39 | import android.telephony.TelephonyManager; |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 40 | import android.view.LayoutInflater; |
| 41 | import android.view.View; |
| 42 | import android.view.ViewGroup; |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 43 | import android.widget.Button; |
Andrew Sapperstein | 88930e3 | 2019-03-07 14:18:15 -0800 | [diff] [blame] | 44 | import android.widget.TextView; |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 45 | import android.widget.Toast; |
| 46 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 47 | import androidx.annotation.VisibleForTesting; |
| 48 | import androidx.appcompat.app.AlertDialog; |
| 49 | |
Doris Ling | 7248972 | 2017-11-16 11:03:40 -0800 | [diff] [blame] | 50 | import com.android.settings.core.InstrumentedFragment; |
Fan Zhang | c0c3eaa | 2018-03-30 11:33:08 -0700 | [diff] [blame] | 51 | import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper; |
Pengquan Meng | 7a1c93d | 2018-04-02 16:41:54 -0700 | [diff] [blame] | 52 | import com.android.settings.network.ApnSettings; |
Philip P. Moltmann | e3f7211 | 2018-08-28 15:01:43 -0700 | [diff] [blame] | 53 | import com.android.settingslib.RestrictedLockUtilsInternal; |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 54 | |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 55 | /** |
| 56 | * Confirm and execute a reset of the network settings to a clean "just out of the box" |
| 57 | * state. Multiple confirmations are required: first, a general "are you sure |
| 58 | * you want to do this?" prompt, followed by a keyguard pattern trace if the user |
| 59 | * has defined one, followed by a final strongly-worded "THIS WILL RESET EVERYTHING" |
| 60 | * prompt. If at any time the phone is allowed to go to sleep, is |
| 61 | * locked, et cetera, then the confirmation sequence is abandoned. |
| 62 | * |
| 63 | * This is the confirmation screen. |
| 64 | */ |
Doris Ling | 7248972 | 2017-11-16 11:03:40 -0800 | [diff] [blame] | 65 | public class ResetNetworkConfirm extends InstrumentedFragment { |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 66 | |
Andrew Sapperstein | 88930e3 | 2019-03-07 14:18:15 -0800 | [diff] [blame] | 67 | @VisibleForTesting View mContentView; |
Qingxi Li | 0caad2f | 2018-01-05 10:52:42 -0800 | [diff] [blame] | 68 | @VisibleForTesting boolean mEraseEsim; |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 69 | @VisibleForTesting ResetNetworkTask mResetNetworkTask; |
| 70 | @VisibleForTesting Activity mActivity; |
| 71 | private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; |
| 72 | private ProgressDialog mProgressDialog; |
Raff Tsai | a5b25df | 2019-04-26 10:55:48 +0800 | [diff] [blame] | 73 | private AlertDialog mAlertDialog; |
Qingxi Li | 0caad2f | 2018-01-05 10:52:42 -0800 | [diff] [blame] | 74 | |
| 75 | /** |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 76 | * Async task used to do all reset task. If error happens during |
Qingxi Li | 0caad2f | 2018-01-05 10:52:42 -0800 | [diff] [blame] | 77 | * erasing eSIM profiles or timeout, an error msg is shown. |
| 78 | */ |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 79 | private class ResetNetworkTask extends AsyncTask<Void, Void, Boolean> { |
Qingxi Li | 0caad2f | 2018-01-05 10:52:42 -0800 | [diff] [blame] | 80 | private final Context mContext; |
| 81 | private final String mPackageName; |
| 82 | |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 83 | ResetNetworkTask(Context context) { |
Qingxi Li | 0caad2f | 2018-01-05 10:52:42 -0800 | [diff] [blame] | 84 | mContext = context; |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 85 | mPackageName = context.getPackageName(); |
Qingxi Li | 0caad2f | 2018-01-05 10:52:42 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
| 89 | protected Boolean doInBackground(Void... params) { |
Jiashen Wang | c1ed4d5 | 2020-12-14 22:44:41 -0800 | [diff] [blame] | 90 | boolean isResetSucceed = true; |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 91 | ConnectivityManager connectivityManager = (ConnectivityManager) |
| 92 | mContext.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 93 | if (connectivityManager != null) { |
| 94 | connectivityManager.factoryReset(); |
| 95 | } |
| 96 | |
| 97 | WifiManager wifiManager = (WifiManager) |
| 98 | mContext.getSystemService(Context.WIFI_SERVICE); |
| 99 | if (wifiManager != null) { |
| 100 | wifiManager.factoryReset(); |
| 101 | } |
| 102 | |
| 103 | p2pFactoryReset(mContext); |
| 104 | |
Jiashen Wang | c1ed4d5 | 2020-12-14 22:44:41 -0800 | [diff] [blame] | 105 | if (mEraseEsim) { |
| 106 | isResetSucceed = RecoverySystem.wipeEuiccData(mContext, mPackageName); |
| 107 | } |
| 108 | |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 109 | TelephonyManager telephonyManager = (TelephonyManager) |
zoey chen | 8ea5428 | 2019-12-17 16:36:52 +0800 | [diff] [blame] | 110 | mContext.getSystemService(TelephonyManager.class) |
| 111 | .createForSubscriptionId(mSubId); |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 112 | if (telephonyManager != null) { |
zoey chen | 8ea5428 | 2019-12-17 16:36:52 +0800 | [diff] [blame] | 113 | telephonyManager.resetSettings(); |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | NetworkPolicyManager policyManager = (NetworkPolicyManager) |
| 117 | mContext.getSystemService(Context.NETWORK_POLICY_SERVICE); |
| 118 | if (policyManager != null) { |
zoey chen | 353926a | 2019-12-24 19:37:11 +0800 | [diff] [blame] | 119 | String subscriberId = telephonyManager.getSubscriberId(); |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 120 | policyManager.factoryReset(subscriberId); |
| 121 | } |
| 122 | |
| 123 | BluetoothManager btManager = (BluetoothManager) |
| 124 | mContext.getSystemService(Context.BLUETOOTH_SERVICE); |
| 125 | if (btManager != null) { |
| 126 | BluetoothAdapter btAdapter = btManager.getAdapter(); |
| 127 | if (btAdapter != null) { |
| 128 | btAdapter.factoryReset(); |
| 129 | } |
| 130 | } |
| 131 | |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 132 | restoreDefaultApn(mContext); |
Jiashen Wang | c1ed4d5 | 2020-12-14 22:44:41 -0800 | [diff] [blame] | 133 | return isResetSucceed; |
Qingxi Li | 0caad2f | 2018-01-05 10:52:42 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override |
| 137 | protected void onPostExecute(Boolean succeeded) { |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 138 | mProgressDialog.dismiss(); |
Qingxi Li | 0caad2f | 2018-01-05 10:52:42 -0800 | [diff] [blame] | 139 | if (succeeded) { |
| 140 | Toast.makeText(mContext, R.string.reset_network_complete_toast, Toast.LENGTH_SHORT) |
| 141 | .show(); |
| 142 | } else { |
Raff Tsai | a5b25df | 2019-04-26 10:55:48 +0800 | [diff] [blame] | 143 | mAlertDialog = new AlertDialog.Builder(mContext) |
Qingxi Li | 0caad2f | 2018-01-05 10:52:42 -0800 | [diff] [blame] | 144 | .setTitle(R.string.reset_esim_error_title) |
| 145 | .setMessage(R.string.reset_esim_error_msg) |
| 146 | .setPositiveButton(android.R.string.ok, null /* listener */) |
| 147 | .show(); |
| 148 | } |
| 149 | } |
| 150 | } |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * The user has gone through the multiple confirmation, so now we go ahead |
| 154 | * and reset the network settings to its factory-default state. |
| 155 | */ |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 156 | @VisibleForTesting |
| 157 | Button.OnClickListener mFinalClickListener = new Button.OnClickListener() { |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 158 | |
| 159 | @Override |
| 160 | public void onClick(View v) { |
| 161 | if (Utils.isMonkeyRunning()) { |
| 162 | return; |
| 163 | } |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 164 | |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 165 | mProgressDialog = getProgressDialog(mActivity); |
| 166 | mProgressDialog.show(); |
Stuart Scott | 73281fb | 2015-03-30 13:16:29 -0700 | [diff] [blame] | 167 | |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 168 | mResetNetworkTask = new ResetNetworkTask(mActivity); |
| 169 | mResetNetworkTask.execute(); |
Qingxi Li | 0caad2f | 2018-01-05 10:52:42 -0800 | [diff] [blame] | 170 | } |
| 171 | }; |
Hyejin Kim | 623852f | 2015-11-05 12:35:39 +0900 | [diff] [blame] | 172 | |
Qingxi Li | 0caad2f | 2018-01-05 10:52:42 -0800 | [diff] [blame] | 173 | @VisibleForTesting |
Jimmy Chen | d62453d | 2018-10-11 18:03:51 +0800 | [diff] [blame] | 174 | void p2pFactoryReset(Context context) { |
| 175 | WifiP2pManager wifiP2pManager = (WifiP2pManager) |
| 176 | context.getSystemService(Context.WIFI_P2P_SERVICE); |
| 177 | if (wifiP2pManager != null) { |
| 178 | WifiP2pManager.Channel channel = wifiP2pManager.initialize( |
| 179 | context.getApplicationContext(), context.getMainLooper(), |
| 180 | null /* listener */); |
| 181 | if (channel != null) { |
| 182 | wifiP2pManager.factoryReset(channel, null /* listener */); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 187 | private ProgressDialog getProgressDialog(Context context) { |
| 188 | final ProgressDialog progressDialog = new ProgressDialog(context); |
| 189 | progressDialog.setIndeterminate(true); |
| 190 | progressDialog.setCancelable(false); |
| 191 | progressDialog.setMessage( |
| 192 | context.getString(R.string.master_clear_progress_text)); |
| 193 | return progressDialog; |
| 194 | } |
| 195 | |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 196 | /** |
Malcolm Chen | e9d96b7 | 2017-04-19 16:12:45 -0700 | [diff] [blame] | 197 | * Restore APN settings to default. |
| 198 | */ |
| 199 | private void restoreDefaultApn(Context context) { |
| 200 | Uri uri = Uri.parse(ApnSettings.RESTORE_CARRIERS_URI); |
| 201 | |
changbetty | ebe36cf | 2020-01-08 22:06:26 +0800 | [diff] [blame] | 202 | if (SubscriptionManager.isUsableSubscriptionId(mSubId)) { |
Malcolm Chen | e9d96b7 | 2017-04-19 16:12:45 -0700 | [diff] [blame] | 203 | uri = Uri.withAppendedPath(uri, "subId/" + String.valueOf(mSubId)); |
| 204 | } |
| 205 | |
| 206 | ContentResolver resolver = context.getContentResolver(); |
| 207 | resolver.delete(uri, null, null); |
| 208 | } |
| 209 | |
| 210 | /** |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 211 | * Configure the UI for the final confirmation interaction |
| 212 | */ |
| 213 | private void establishFinalConfirmationState() { |
| 214 | mContentView.findViewById(R.id.execute_reset_network) |
| 215 | .setOnClickListener(mFinalClickListener); |
| 216 | } |
| 217 | |
Andrew Sapperstein | 88930e3 | 2019-03-07 14:18:15 -0800 | [diff] [blame] | 218 | @VisibleForTesting |
| 219 | void setSubtitle() { |
| 220 | if (mEraseEsim) { |
| 221 | ((TextView) mContentView.findViewById(R.id.reset_network_confirm)) |
| 222 | .setText(R.string.reset_network_final_desc_esim); |
| 223 | } |
| 224 | } |
| 225 | |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 226 | @Override |
| 227 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 228 | Bundle savedInstanceState) { |
Philip P. Moltmann | e3f7211 | 2018-08-28 15:01:43 -0700 | [diff] [blame] | 229 | final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced( |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 230 | mActivity, UserManager.DISALLOW_NETWORK_RESET, UserHandle.myUserId()); |
| 231 | if (RestrictedLockUtilsInternal.hasBaseUserRestriction(mActivity, |
Sudheer Shanka | 7dbbe13 | 2016-02-16 14:19:32 +0000 | [diff] [blame] | 232 | UserManager.DISALLOW_NETWORK_RESET, UserHandle.myUserId())) { |
Stuart Scott | 47ed6ca | 2015-04-20 11:05:43 -0700 | [diff] [blame] | 233 | return inflater.inflate(R.layout.network_reset_disallowed_screen, null); |
Sudheer Shanka | 7dbbe13 | 2016-02-16 14:19:32 +0000 | [diff] [blame] | 234 | } else if (admin != null) { |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 235 | new ActionDisabledByAdminDialogHelper(mActivity) |
arangelov | e35badd | 2017-10-27 13:51:28 +0100 | [diff] [blame] | 236 | .prepareDialogBuilder(UserManager.DISALLOW_NETWORK_RESET, admin) |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 237 | .setOnDismissListener(__ -> mActivity.finish()) |
arangelov | e35badd | 2017-10-27 13:51:28 +0100 | [diff] [blame] | 238 | .show(); |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 239 | return new View(mActivity); |
Stuart Scott | 47ed6ca | 2015-04-20 11:05:43 -0700 | [diff] [blame] | 240 | } |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 241 | mContentView = inflater.inflate(R.layout.reset_network_confirm, null); |
| 242 | establishFinalConfirmationState(); |
Andrew Sapperstein | 88930e3 | 2019-03-07 14:18:15 -0800 | [diff] [blame] | 243 | setSubtitle(); |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 244 | return mContentView; |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public void onCreate(Bundle savedInstanceState) { |
| 249 | super.onCreate(savedInstanceState); |
| 250 | |
| 251 | Bundle args = getArguments(); |
| 252 | if (args != null) { |
tom hsu | a381196 | 2020-01-14 14:46:09 +0800 | [diff] [blame] | 253 | mSubId = args.getInt(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 254 | SubscriptionManager.INVALID_SUBSCRIPTION_ID); |
Qingxi Li | 0caad2f | 2018-01-05 10:52:42 -0800 | [diff] [blame] | 255 | mEraseEsim = args.getBoolean(MasterClear.ERASE_ESIMS_EXTRA); |
| 256 | } |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 257 | |
| 258 | mActivity = getActivity(); |
Qingxi Li | 0caad2f | 2018-01-05 10:52:42 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public void onDestroy() { |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 263 | if (mResetNetworkTask != null) { |
| 264 | mResetNetworkTask.cancel(true /* mayInterruptIfRunning */); |
| 265 | mResetNetworkTask = null; |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 266 | } |
Raff Tsai | a5b25df | 2019-04-26 10:55:48 +0800 | [diff] [blame] | 267 | if (mProgressDialog != null) { |
| 268 | mProgressDialog.dismiss(); |
| 269 | } |
| 270 | if (mAlertDialog != null) { |
| 271 | mAlertDialog.dismiss(); |
| 272 | } |
Antony Sargent | 1a0d442 | 2018-02-06 15:49:12 -0800 | [diff] [blame] | 273 | super.onDestroy(); |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 274 | } |
Chris Wren | 8a963ba | 2015-03-20 10:29:14 -0400 | [diff] [blame] | 275 | |
| 276 | @Override |
Fan Zhang | 6507613 | 2016-08-08 10:25:13 -0700 | [diff] [blame] | 277 | public int getMetricsCategory() { |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 278 | return SettingsEnums.RESET_NETWORK_CONFIRM; |
Chris Wren | 8a963ba | 2015-03-20 10:29:14 -0400 | [diff] [blame] | 279 | } |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 280 | } |