blob: 784ffa7719c4015a52c6e62df7970680a08a9efd [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) {
Jiashen Wangc1ed4d52020-12-14 22:44:41 -080090 boolean isResetSucceed = true;
Raff Tsai3aeb9152019-04-22 14:52:28 +080091 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 Wangc1ed4d52020-12-14 22:44:41 -0800105 if (mEraseEsim) {
106 isResetSucceed = RecoverySystem.wipeEuiccData(mContext, mPackageName);
107 }
108
Raff Tsai3aeb9152019-04-22 14:52:28 +0800109 TelephonyManager telephonyManager = (TelephonyManager)
zoey chen8ea54282019-12-17 16:36:52 +0800110 mContext.getSystemService(TelephonyManager.class)
111 .createForSubscriptionId(mSubId);
Raff Tsai3aeb9152019-04-22 14:52:28 +0800112 if (telephonyManager != null) {
zoey chen8ea54282019-12-17 16:36:52 +0800113 telephonyManager.resetSettings();
Raff Tsai3aeb9152019-04-22 14:52:28 +0800114 }
115
116 NetworkPolicyManager policyManager = (NetworkPolicyManager)
117 mContext.getSystemService(Context.NETWORK_POLICY_SERVICE);
118 if (policyManager != null) {
zoey chen353926a2019-12-24 19:37:11 +0800119 String subscriberId = telephonyManager.getSubscriberId();
Raff Tsai3aeb9152019-04-22 14:52:28 +0800120 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 Tsai3aeb9152019-04-22 14:52:28 +0800132 restoreDefaultApn(mContext);
Jiashen Wangc1ed4d52020-12-14 22:44:41 -0800133 return isResetSucceed;
Qingxi Li0caad2f2018-01-05 10:52:42 -0800134 }
135
136 @Override
137 protected void onPostExecute(Boolean succeeded) {
Raff Tsai3aeb9152019-04-22 14:52:28 +0800138 mProgressDialog.dismiss();
Qingxi Li0caad2f2018-01-05 10:52:42 -0800139 if (succeeded) {
140 Toast.makeText(mContext, R.string.reset_network_complete_toast, Toast.LENGTH_SHORT)
141 .show();
142 } else {
Raff Tsaia5b25df2019-04-26 10:55:48 +0800143 mAlertDialog = new AlertDialog.Builder(mContext)
Qingxi Li0caad2f2018-01-05 10:52:42 -0800144 .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 Scottbe903412014-07-24 19:22:06 -0700151
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 Tsai3aeb9152019-04-22 14:52:28 +0800156 @VisibleForTesting
157 Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
Stuart Scottbe903412014-07-24 19:22:06 -0700158
159 @Override
160 public void onClick(View v) {
161 if (Utils.isMonkeyRunning()) {
162 return;
163 }
Stuart Scottbe903412014-07-24 19:22:06 -0700164
Raff Tsai3aeb9152019-04-22 14:52:28 +0800165 mProgressDialog = getProgressDialog(mActivity);
166 mProgressDialog.show();
Stuart Scott73281fb2015-03-30 13:16:29 -0700167
Raff Tsai3aeb9152019-04-22 14:52:28 +0800168 mResetNetworkTask = new ResetNetworkTask(mActivity);
169 mResetNetworkTask.execute();
Qingxi Li0caad2f2018-01-05 10:52:42 -0800170 }
171 };
Hyejin Kim623852f2015-11-05 12:35:39 +0900172
Qingxi Li0caad2f2018-01-05 10:52:42 -0800173 @VisibleForTesting
Jimmy Chend62453d2018-10-11 18:03:51 +0800174 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 Tsai3aeb9152019-04-22 14:52:28 +0800187 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 Scottbe903412014-07-24 19:22:06 -0700196 /**
Malcolm Chene9d96b72017-04-19 16:12:45 -0700197 * Restore APN settings to default.
198 */
199 private void restoreDefaultApn(Context context) {
200 Uri uri = Uri.parse(ApnSettings.RESTORE_CARRIERS_URI);
201
changbettyebe36cf2020-01-08 22:06:26 +0800202 if (SubscriptionManager.isUsableSubscriptionId(mSubId)) {
Malcolm Chene9d96b72017-04-19 16:12:45 -0700203 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 Scottbe903412014-07-24 19:22:06 -0700211 * 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 Sapperstein88930e32019-03-07 14:18:15 -0800218 @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 Scottbe903412014-07-24 19:22:06 -0700226 @Override
227 public View onCreateView(LayoutInflater inflater, ViewGroup container,
228 Bundle savedInstanceState) {
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700229 final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
Raff Tsai3aeb9152019-04-22 14:52:28 +0800230 mActivity, UserManager.DISALLOW_NETWORK_RESET, UserHandle.myUserId());
231 if (RestrictedLockUtilsInternal.hasBaseUserRestriction(mActivity,
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000232 UserManager.DISALLOW_NETWORK_RESET, UserHandle.myUserId())) {
Stuart Scott47ed6ca2015-04-20 11:05:43 -0700233 return inflater.inflate(R.layout.network_reset_disallowed_screen, null);
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000234 } else if (admin != null) {
Raff Tsai3aeb9152019-04-22 14:52:28 +0800235 new ActionDisabledByAdminDialogHelper(mActivity)
arangelove35badd2017-10-27 13:51:28 +0100236 .prepareDialogBuilder(UserManager.DISALLOW_NETWORK_RESET, admin)
Raff Tsai3aeb9152019-04-22 14:52:28 +0800237 .setOnDismissListener(__ -> mActivity.finish())
arangelove35badd2017-10-27 13:51:28 +0100238 .show();
Raff Tsai3aeb9152019-04-22 14:52:28 +0800239 return new View(mActivity);
Stuart Scott47ed6ca2015-04-20 11:05:43 -0700240 }
Stuart Scottbe903412014-07-24 19:22:06 -0700241 mContentView = inflater.inflate(R.layout.reset_network_confirm, null);
242 establishFinalConfirmationState();
Andrew Sapperstein88930e32019-03-07 14:18:15 -0800243 setSubtitle();
Stuart Scottbe903412014-07-24 19:22:06 -0700244 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 hsua3811962020-01-14 14:46:09 +0800253 mSubId = args.getInt(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX,
Stuart Scottbe903412014-07-24 19:22:06 -0700254 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Qingxi Li0caad2f2018-01-05 10:52:42 -0800255 mEraseEsim = args.getBoolean(MasterClear.ERASE_ESIMS_EXTRA);
256 }
Raff Tsai3aeb9152019-04-22 14:52:28 +0800257
258 mActivity = getActivity();
Qingxi Li0caad2f2018-01-05 10:52:42 -0800259 }
260
261 @Override
262 public void onDestroy() {
Raff Tsai3aeb9152019-04-22 14:52:28 +0800263 if (mResetNetworkTask != null) {
264 mResetNetworkTask.cancel(true /* mayInterruptIfRunning */);
265 mResetNetworkTask = null;
Stuart Scottbe903412014-07-24 19:22:06 -0700266 }
Raff Tsaia5b25df2019-04-26 10:55:48 +0800267 if (mProgressDialog != null) {
268 mProgressDialog.dismiss();
269 }
270 if (mAlertDialog != null) {
271 mAlertDialog.dismiss();
272 }
Antony Sargent1a0d4422018-02-06 15:49:12 -0800273 super.onDestroy();
Stuart Scottbe903412014-07-24 19:22:06 -0700274 }
Chris Wren8a963ba2015-03-20 10:29:14 -0400275
276 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700277 public int getMetricsCategory() {
Fan Zhang31b21002019-01-16 13:49:47 -0800278 return SettingsEnums.RESET_NETWORK_CONFIRM;
Chris Wren8a963ba2015-03-20 10:29:14 -0400279 }
Stuart Scottbe903412014-07-24 19:22:06 -0700280}