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) { |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 90 | ConnectivityManager connectivityManager = (ConnectivityManager) |
| 91 | mContext.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 92 | if (connectivityManager != null) { |
| 93 | connectivityManager.factoryReset(); |
| 94 | } |
| 95 | |
| 96 | WifiManager wifiManager = (WifiManager) |
| 97 | mContext.getSystemService(Context.WIFI_SERVICE); |
| 98 | if (wifiManager != null) { |
| 99 | wifiManager.factoryReset(); |
| 100 | } |
| 101 | |
| 102 | p2pFactoryReset(mContext); |
| 103 | |
| 104 | TelephonyManager telephonyManager = (TelephonyManager) |
zoey chen | 8ea5428 | 2019-12-17 16:36:52 +0800 | [diff] [blame] | 105 | mContext.getSystemService(TelephonyManager.class) |
| 106 | .createForSubscriptionId(mSubId); |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 107 | if (telephonyManager != null) { |
zoey chen | 8ea5428 | 2019-12-17 16:36:52 +0800 | [diff] [blame] | 108 | telephonyManager.resetSettings(); |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | NetworkPolicyManager policyManager = (NetworkPolicyManager) |
| 112 | mContext.getSystemService(Context.NETWORK_POLICY_SERVICE); |
| 113 | if (policyManager != null) { |
zoey chen | 353926a | 2019-12-24 19:37:11 +0800 | [diff] [blame] | 114 | String subscriberId = telephonyManager.getSubscriberId(); |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 115 | policyManager.factoryReset(subscriberId); |
| 116 | } |
| 117 | |
| 118 | BluetoothManager btManager = (BluetoothManager) |
| 119 | mContext.getSystemService(Context.BLUETOOTH_SERVICE); |
| 120 | if (btManager != null) { |
| 121 | BluetoothAdapter btAdapter = btManager.getAdapter(); |
| 122 | if (btAdapter != null) { |
| 123 | btAdapter.factoryReset(); |
| 124 | } |
| 125 | } |
| 126 | |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 127 | restoreDefaultApn(mContext); |
| 128 | if (mEraseEsim) { |
| 129 | return RecoverySystem.wipeEuiccData(mContext, mPackageName); |
| 130 | } else { |
| 131 | return true; |
| 132 | } |
Qingxi Li | 0caad2f | 2018-01-05 10:52:42 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | protected void onPostExecute(Boolean succeeded) { |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 137 | mProgressDialog.dismiss(); |
Qingxi Li | 0caad2f | 2018-01-05 10:52:42 -0800 | [diff] [blame] | 138 | if (succeeded) { |
| 139 | Toast.makeText(mContext, R.string.reset_network_complete_toast, Toast.LENGTH_SHORT) |
| 140 | .show(); |
| 141 | } else { |
Raff Tsai | a5b25df | 2019-04-26 10:55:48 +0800 | [diff] [blame] | 142 | mAlertDialog = new AlertDialog.Builder(mContext) |
Qingxi Li | 0caad2f | 2018-01-05 10:52:42 -0800 | [diff] [blame] | 143 | .setTitle(R.string.reset_esim_error_title) |
| 144 | .setMessage(R.string.reset_esim_error_msg) |
| 145 | .setPositiveButton(android.R.string.ok, null /* listener */) |
| 146 | .show(); |
| 147 | } |
| 148 | } |
| 149 | } |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 150 | |
| 151 | /** |
| 152 | * The user has gone through the multiple confirmation, so now we go ahead |
| 153 | * and reset the network settings to its factory-default state. |
| 154 | */ |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 155 | @VisibleForTesting |
| 156 | Button.OnClickListener mFinalClickListener = new Button.OnClickListener() { |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 157 | |
| 158 | @Override |
| 159 | public void onClick(View v) { |
| 160 | if (Utils.isMonkeyRunning()) { |
| 161 | return; |
| 162 | } |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 163 | |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 164 | mProgressDialog = getProgressDialog(mActivity); |
| 165 | mProgressDialog.show(); |
Stuart Scott | 73281fb | 2015-03-30 13:16:29 -0700 | [diff] [blame] | 166 | |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 167 | mResetNetworkTask = new ResetNetworkTask(mActivity); |
| 168 | mResetNetworkTask.execute(); |
Qingxi Li | 0caad2f | 2018-01-05 10:52:42 -0800 | [diff] [blame] | 169 | } |
| 170 | }; |
Hyejin Kim | 623852f | 2015-11-05 12:35:39 +0900 | [diff] [blame] | 171 | |
Qingxi Li | 0caad2f | 2018-01-05 10:52:42 -0800 | [diff] [blame] | 172 | @VisibleForTesting |
Jimmy Chen | d62453d | 2018-10-11 18:03:51 +0800 | [diff] [blame] | 173 | void p2pFactoryReset(Context context) { |
| 174 | WifiP2pManager wifiP2pManager = (WifiP2pManager) |
| 175 | context.getSystemService(Context.WIFI_P2P_SERVICE); |
| 176 | if (wifiP2pManager != null) { |
| 177 | WifiP2pManager.Channel channel = wifiP2pManager.initialize( |
| 178 | context.getApplicationContext(), context.getMainLooper(), |
| 179 | null /* listener */); |
| 180 | if (channel != null) { |
| 181 | wifiP2pManager.factoryReset(channel, null /* listener */); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 186 | private ProgressDialog getProgressDialog(Context context) { |
| 187 | final ProgressDialog progressDialog = new ProgressDialog(context); |
| 188 | progressDialog.setIndeterminate(true); |
| 189 | progressDialog.setCancelable(false); |
| 190 | progressDialog.setMessage( |
| 191 | context.getString(R.string.master_clear_progress_text)); |
| 192 | return progressDialog; |
| 193 | } |
| 194 | |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 195 | /** |
Malcolm Chen | e9d96b7 | 2017-04-19 16:12:45 -0700 | [diff] [blame] | 196 | * Restore APN settings to default. |
| 197 | */ |
| 198 | private void restoreDefaultApn(Context context) { |
| 199 | Uri uri = Uri.parse(ApnSettings.RESTORE_CARRIERS_URI); |
| 200 | |
changbetty | ebe36cf | 2020-01-08 22:06:26 +0800 | [diff] [blame] | 201 | if (SubscriptionManager.isUsableSubscriptionId(mSubId)) { |
Malcolm Chen | e9d96b7 | 2017-04-19 16:12:45 -0700 | [diff] [blame] | 202 | uri = Uri.withAppendedPath(uri, "subId/" + String.valueOf(mSubId)); |
| 203 | } |
| 204 | |
| 205 | ContentResolver resolver = context.getContentResolver(); |
| 206 | resolver.delete(uri, null, null); |
| 207 | } |
| 208 | |
| 209 | /** |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 210 | * Configure the UI for the final confirmation interaction |
| 211 | */ |
| 212 | private void establishFinalConfirmationState() { |
| 213 | mContentView.findViewById(R.id.execute_reset_network) |
| 214 | .setOnClickListener(mFinalClickListener); |
| 215 | } |
| 216 | |
Andrew Sapperstein | 88930e3 | 2019-03-07 14:18:15 -0800 | [diff] [blame] | 217 | @VisibleForTesting |
| 218 | void setSubtitle() { |
| 219 | if (mEraseEsim) { |
| 220 | ((TextView) mContentView.findViewById(R.id.reset_network_confirm)) |
| 221 | .setText(R.string.reset_network_final_desc_esim); |
| 222 | } |
| 223 | } |
| 224 | |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 225 | @Override |
| 226 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 227 | Bundle savedInstanceState) { |
Philip P. Moltmann | e3f7211 | 2018-08-28 15:01:43 -0700 | [diff] [blame] | 228 | final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced( |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 229 | mActivity, UserManager.DISALLOW_NETWORK_RESET, UserHandle.myUserId()); |
| 230 | if (RestrictedLockUtilsInternal.hasBaseUserRestriction(mActivity, |
Sudheer Shanka | 7dbbe13 | 2016-02-16 14:19:32 +0000 | [diff] [blame] | 231 | UserManager.DISALLOW_NETWORK_RESET, UserHandle.myUserId())) { |
Stuart Scott | 47ed6ca | 2015-04-20 11:05:43 -0700 | [diff] [blame] | 232 | return inflater.inflate(R.layout.network_reset_disallowed_screen, null); |
Sudheer Shanka | 7dbbe13 | 2016-02-16 14:19:32 +0000 | [diff] [blame] | 233 | } else if (admin != null) { |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 234 | new ActionDisabledByAdminDialogHelper(mActivity) |
arangelov | e35badd | 2017-10-27 13:51:28 +0100 | [diff] [blame] | 235 | .prepareDialogBuilder(UserManager.DISALLOW_NETWORK_RESET, admin) |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 236 | .setOnDismissListener(__ -> mActivity.finish()) |
arangelov | e35badd | 2017-10-27 13:51:28 +0100 | [diff] [blame] | 237 | .show(); |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 238 | return new View(mActivity); |
Stuart Scott | 47ed6ca | 2015-04-20 11:05:43 -0700 | [diff] [blame] | 239 | } |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 240 | mContentView = inflater.inflate(R.layout.reset_network_confirm, null); |
| 241 | establishFinalConfirmationState(); |
Andrew Sapperstein | 88930e3 | 2019-03-07 14:18:15 -0800 | [diff] [blame] | 242 | setSubtitle(); |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 243 | return mContentView; |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public void onCreate(Bundle savedInstanceState) { |
| 248 | super.onCreate(savedInstanceState); |
| 249 | |
| 250 | Bundle args = getArguments(); |
| 251 | if (args != null) { |
tom hsu | a381196 | 2020-01-14 14:46:09 +0800 | [diff] [blame] | 252 | mSubId = args.getInt(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 253 | SubscriptionManager.INVALID_SUBSCRIPTION_ID); |
Qingxi Li | 0caad2f | 2018-01-05 10:52:42 -0800 | [diff] [blame] | 254 | mEraseEsim = args.getBoolean(MasterClear.ERASE_ESIMS_EXTRA); |
| 255 | } |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 256 | |
| 257 | mActivity = getActivity(); |
Qingxi Li | 0caad2f | 2018-01-05 10:52:42 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public void onDestroy() { |
Raff Tsai | 3aeb915 | 2019-04-22 14:52:28 +0800 | [diff] [blame] | 262 | if (mResetNetworkTask != null) { |
| 263 | mResetNetworkTask.cancel(true /* mayInterruptIfRunning */); |
| 264 | mResetNetworkTask = null; |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 265 | } |
Raff Tsai | a5b25df | 2019-04-26 10:55:48 +0800 | [diff] [blame] | 266 | if (mProgressDialog != null) { |
| 267 | mProgressDialog.dismiss(); |
| 268 | } |
| 269 | if (mAlertDialog != null) { |
| 270 | mAlertDialog.dismiss(); |
| 271 | } |
Antony Sargent | 1a0d442 | 2018-02-06 15:49:12 -0800 | [diff] [blame] | 272 | super.onDestroy(); |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 273 | } |
Chris Wren | 8a963ba | 2015-03-20 10:29:14 -0400 | [diff] [blame] | 274 | |
| 275 | @Override |
Fan Zhang | 6507613 | 2016-08-08 10:25:13 -0700 | [diff] [blame] | 276 | public int getMetricsCategory() { |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 277 | return SettingsEnums.RESET_NETWORK_CONFIRM; |
Chris Wren | 8a963ba | 2015-03-20 10:29:14 -0400 | [diff] [blame] | 278 | } |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 279 | } |