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