blob: 6856a5a140e1b01583d0af4ff34646d5cf676827 [file] [log] [blame]
Stuart Scottbe903412014-07-24 19:22:06 -07001/*
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
17package com.android.settings;
18
Fan Zhangc0c3eaa2018-03-30 11:33:08 -070019import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
20
Raff Tsai3aeb9152019-04-22 14:52:28 +080021import android.app.Activity;
22import android.app.ProgressDialog;
Fan Zhang31b21002019-01-16 13:49:47 -080023import android.app.settings.SettingsEnums;
Prathmesh Prabhu8d763c02016-03-11 13:27:17 -080024import android.bluetooth.BluetoothAdapter;
Ajay Panicker568d9332015-07-29 11:39:16 -070025import android.bluetooth.BluetoothManager;
Malcolm Chene9d96b72017-04-19 16:12:45 -070026import android.content.ContentResolver;
Jason Monk39b46742015-09-10 15:52:51 -040027import android.content.Context;
Stuart Scott73281fb2015-03-30 13:16:29 -070028import android.net.ConnectivityManager;
Stuart Scottbe903412014-07-24 19:22:06 -070029import android.net.NetworkPolicyManager;
Malcolm Chene9d96b72017-04-19 16:12:45 -070030import android.net.Uri;
Stuart Scottbe903412014-07-24 19:22:06 -070031import android.net.wifi.WifiManager;
Jimmy Chend62453d2018-10-11 18:03:51 +080032import android.net.wifi.p2p.WifiP2pManager;
Qingxi Li0caad2f2018-01-05 10:52:42 -080033import android.os.AsyncTask;
Stuart Scottbe903412014-07-24 19:22:06 -070034import android.os.Bundle;
Fan Zhangc0c3eaa2018-03-30 11:33:08 -070035import android.os.RecoverySystem;
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000036import android.os.UserHandle;
Stuart Scott47ed6ca2015-04-20 11:05:43 -070037import android.os.UserManager;
Stuart Scottbe903412014-07-24 19:22:06 -070038import android.telephony.SubscriptionManager;
39import android.telephony.TelephonyManager;
Stuart Scottbe903412014-07-24 19:22:06 -070040import android.view.LayoutInflater;
41import android.view.View;
42import android.view.ViewGroup;
Stuart Scottbe903412014-07-24 19:22:06 -070043import android.widget.Button;
Andrew Sapperstein88930e32019-03-07 14:18:15 -080044import android.widget.TextView;
Stuart Scottbe903412014-07-24 19:22:06 -070045import android.widget.Toast;
46
Fan Zhang23f8d592018-08-28 15:11:40 -070047import androidx.annotation.VisibleForTesting;
48import androidx.appcompat.app.AlertDialog;
49
Doris Ling72489722017-11-16 11:03:40 -080050import com.android.settings.core.InstrumentedFragment;
Fan Zhangc0c3eaa2018-03-30 11:33:08 -070051import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper;
Pengquan Meng7a1c93d2018-04-02 16:41:54 -070052import com.android.settings.network.ApnSettings;
Philip P. Moltmanne3f72112018-08-28 15:01:43 -070053import com.android.settingslib.RestrictedLockUtilsInternal;
Stuart Scottbe903412014-07-24 19:22:06 -070054
Stuart Scottbe903412014-07-24 19:22:06 -070055/**
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 Ling72489722017-11-16 11:03:40 -080065public class ResetNetworkConfirm extends InstrumentedFragment {
Stuart Scottbe903412014-07-24 19:22:06 -070066
Andrew Sapperstein88930e32019-03-07 14:18:15 -080067 @VisibleForTesting View mContentView;
Qingxi Li0caad2f2018-01-05 10:52:42 -080068 @VisibleForTesting boolean mEraseEsim;
Raff Tsai3aeb9152019-04-22 14:52:28 +080069 @VisibleForTesting ResetNetworkTask mResetNetworkTask;
70 @VisibleForTesting Activity mActivity;
71 private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
72 private ProgressDialog mProgressDialog;
Raff Tsaia5b25df2019-04-26 10:55:48 +080073 private AlertDialog mAlertDialog;
Qingxi Li0caad2f2018-01-05 10:52:42 -080074
75 /**
Raff Tsai3aeb9152019-04-22 14:52:28 +080076 * Async task used to do all reset task. If error happens during
Qingxi Li0caad2f2018-01-05 10:52:42 -080077 * erasing eSIM profiles or timeout, an error msg is shown.
78 */
Raff Tsai3aeb9152019-04-22 14:52:28 +080079 private class ResetNetworkTask extends AsyncTask<Void, Void, Boolean> {
Qingxi Li0caad2f2018-01-05 10:52:42 -080080 private final Context mContext;
81 private final String mPackageName;
82
Raff Tsai3aeb9152019-04-22 14:52:28 +080083 ResetNetworkTask(Context context) {
Qingxi Li0caad2f2018-01-05 10:52:42 -080084 mContext = context;
Raff Tsai3aeb9152019-04-22 14:52:28 +080085 mPackageName = context.getPackageName();
Qingxi Li0caad2f2018-01-05 10:52:42 -080086 }
87
88 @Override
89 protected Boolean doInBackground(Void... params) {
Raff Tsai3aeb9152019-04-22 14:52:28 +080090 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 chen8ea54282019-12-17 16:36:52 +0800105 mContext.getSystemService(TelephonyManager.class)
106 .createForSubscriptionId(mSubId);
Raff Tsai3aeb9152019-04-22 14:52:28 +0800107 if (telephonyManager != null) {
zoey chen8ea54282019-12-17 16:36:52 +0800108 telephonyManager.resetSettings();
Raff Tsai3aeb9152019-04-22 14:52:28 +0800109 }
110
111 NetworkPolicyManager policyManager = (NetworkPolicyManager)
112 mContext.getSystemService(Context.NETWORK_POLICY_SERVICE);
113 if (policyManager != null) {
zoey chen353926a2019-12-24 19:37:11 +0800114 String subscriberId = telephonyManager.getSubscriberId();
Raff Tsai3aeb9152019-04-22 14:52:28 +0800115 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 Tsai3aeb9152019-04-22 14:52:28 +0800127 restoreDefaultApn(mContext);
128 if (mEraseEsim) {
129 return RecoverySystem.wipeEuiccData(mContext, mPackageName);
130 } else {
131 return true;
132 }
Qingxi Li0caad2f2018-01-05 10:52:42 -0800133 }
134
135 @Override
136 protected void onPostExecute(Boolean succeeded) {
Raff Tsai3aeb9152019-04-22 14:52:28 +0800137 mProgressDialog.dismiss();
Qingxi Li0caad2f2018-01-05 10:52:42 -0800138 if (succeeded) {
139 Toast.makeText(mContext, R.string.reset_network_complete_toast, Toast.LENGTH_SHORT)
140 .show();
141 } else {
Raff Tsaia5b25df2019-04-26 10:55:48 +0800142 mAlertDialog = new AlertDialog.Builder(mContext)
Qingxi Li0caad2f2018-01-05 10:52:42 -0800143 .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 Scottbe903412014-07-24 19:22:06 -0700150
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 Tsai3aeb9152019-04-22 14:52:28 +0800155 @VisibleForTesting
156 Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
Stuart Scottbe903412014-07-24 19:22:06 -0700157
158 @Override
159 public void onClick(View v) {
160 if (Utils.isMonkeyRunning()) {
161 return;
162 }
Stuart Scottbe903412014-07-24 19:22:06 -0700163
Raff Tsai3aeb9152019-04-22 14:52:28 +0800164 mProgressDialog = getProgressDialog(mActivity);
165 mProgressDialog.show();
Stuart Scott73281fb2015-03-30 13:16:29 -0700166
Raff Tsai3aeb9152019-04-22 14:52:28 +0800167 mResetNetworkTask = new ResetNetworkTask(mActivity);
168 mResetNetworkTask.execute();
Qingxi Li0caad2f2018-01-05 10:52:42 -0800169 }
170 };
Hyejin Kim623852f2015-11-05 12:35:39 +0900171
Qingxi Li0caad2f2018-01-05 10:52:42 -0800172 @VisibleForTesting
Jimmy Chend62453d2018-10-11 18:03:51 +0800173 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 Tsai3aeb9152019-04-22 14:52:28 +0800186 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 Scottbe903412014-07-24 19:22:06 -0700195 /**
Malcolm Chene9d96b72017-04-19 16:12:45 -0700196 * Restore APN settings to default.
197 */
198 private void restoreDefaultApn(Context context) {
199 Uri uri = Uri.parse(ApnSettings.RESTORE_CARRIERS_URI);
200
changbettyebe36cf2020-01-08 22:06:26 +0800201 if (SubscriptionManager.isUsableSubscriptionId(mSubId)) {
Malcolm Chene9d96b72017-04-19 16:12:45 -0700202 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 Scottbe903412014-07-24 19:22:06 -0700210 * 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 Sapperstein88930e32019-03-07 14:18:15 -0800217 @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 Scottbe903412014-07-24 19:22:06 -0700225 @Override
226 public View onCreateView(LayoutInflater inflater, ViewGroup container,
227 Bundle savedInstanceState) {
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700228 final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
Raff Tsai3aeb9152019-04-22 14:52:28 +0800229 mActivity, UserManager.DISALLOW_NETWORK_RESET, UserHandle.myUserId());
230 if (RestrictedLockUtilsInternal.hasBaseUserRestriction(mActivity,
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000231 UserManager.DISALLOW_NETWORK_RESET, UserHandle.myUserId())) {
Stuart Scott47ed6ca2015-04-20 11:05:43 -0700232 return inflater.inflate(R.layout.network_reset_disallowed_screen, null);
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000233 } else if (admin != null) {
Raff Tsai3aeb9152019-04-22 14:52:28 +0800234 new ActionDisabledByAdminDialogHelper(mActivity)
arangelove35badd2017-10-27 13:51:28 +0100235 .prepareDialogBuilder(UserManager.DISALLOW_NETWORK_RESET, admin)
Raff Tsai3aeb9152019-04-22 14:52:28 +0800236 .setOnDismissListener(__ -> mActivity.finish())
arangelove35badd2017-10-27 13:51:28 +0100237 .show();
Raff Tsai3aeb9152019-04-22 14:52:28 +0800238 return new View(mActivity);
Stuart Scott47ed6ca2015-04-20 11:05:43 -0700239 }
Stuart Scottbe903412014-07-24 19:22:06 -0700240 mContentView = inflater.inflate(R.layout.reset_network_confirm, null);
241 establishFinalConfirmationState();
Andrew Sapperstein88930e32019-03-07 14:18:15 -0800242 setSubtitle();
Stuart Scottbe903412014-07-24 19:22:06 -0700243 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 hsua3811962020-01-14 14:46:09 +0800252 mSubId = args.getInt(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX,
Stuart Scottbe903412014-07-24 19:22:06 -0700253 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Qingxi Li0caad2f2018-01-05 10:52:42 -0800254 mEraseEsim = args.getBoolean(MasterClear.ERASE_ESIMS_EXTRA);
255 }
Raff Tsai3aeb9152019-04-22 14:52:28 +0800256
257 mActivity = getActivity();
Qingxi Li0caad2f2018-01-05 10:52:42 -0800258 }
259
260 @Override
261 public void onDestroy() {
Raff Tsai3aeb9152019-04-22 14:52:28 +0800262 if (mResetNetworkTask != null) {
263 mResetNetworkTask.cancel(true /* mayInterruptIfRunning */);
264 mResetNetworkTask = null;
Stuart Scottbe903412014-07-24 19:22:06 -0700265 }
Raff Tsaia5b25df2019-04-26 10:55:48 +0800266 if (mProgressDialog != null) {
267 mProgressDialog.dismiss();
268 }
269 if (mAlertDialog != null) {
270 mAlertDialog.dismiss();
271 }
Antony Sargent1a0d4422018-02-06 15:49:12 -0800272 super.onDestroy();
Stuart Scottbe903412014-07-24 19:22:06 -0700273 }
Chris Wren8a963ba2015-03-20 10:29:14 -0400274
275 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700276 public int getMetricsCategory() {
Fan Zhang31b21002019-01-16 13:49:47 -0800277 return SettingsEnums.RESET_NETWORK_CONFIRM;
Chris Wren8a963ba2015-03-20 10:29:14 -0400278 }
Stuart Scottbe903412014-07-24 19:22:06 -0700279}