blob: 676789edc8d1eb7f50417116d6327edefde68724 [file] [log] [blame]
Amith Yamasanib14e1e02010-11-02 09:52:29 -07001/*
2 * Copyright (C) 2010 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
felkachanga6cec472018-03-29 12:08:20 +080019
Fan Zhangc7162cd2018-06-18 15:21:41 -070020import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
21
felkachanga6cec472018-03-29 12:08:20 +080022import android.app.ActionBar;
23import android.app.Activity;
Jiashen Wangbbcd54d2020-08-09 20:18:13 -070024import android.app.AlertDialog;
Andres Moralesab61b0d2014-08-26 13:54:12 -070025import android.app.ProgressDialog;
Alex Johnston74260a72020-03-19 17:34:46 +000026import android.app.admin.DevicePolicyManager;
27import android.app.admin.FactoryResetProtectionPolicy;
Fan Zhang31b21002019-01-16 13:49:47 -080028import android.app.settings.SettingsEnums;
Andres Morales7ab89292014-07-10 16:10:43 -070029import android.content.Context;
Jiashen Wangbbcd54d2020-08-09 20:18:13 -070030import android.content.DialogInterface;
Jason Monk39b46742015-09-10 15:52:51 -040031import android.content.Intent;
Andres Moralesab61b0d2014-08-26 13:54:12 -070032import android.content.pm.ActivityInfo;
felkachanga6cec472018-03-29 12:08:20 +080033import android.graphics.Color;
Andres Moralesab61b0d2014-08-26 13:54:12 -070034import android.os.AsyncTask;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070035import android.os.Bundle;
Jiashen Wangbbcd54d2020-08-09 20:18:13 -070036import android.os.PowerManager;
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000037import android.os.UserHandle;
Julia Reynolds2c539332014-06-11 12:56:02 -040038import android.os.UserManager;
Andrew Scullc23357e2017-04-18 09:40:11 +010039import android.service.oemlock.OemLockManager;
Jason Monk39b46742015-09-10 15:52:51 -040040import android.service.persistentdata.PersistentDataBlockManager;
Jiashen Wangbbcd54d2020-08-09 20:18:13 -070041import android.telephony.TelephonyManager;
42import android.telephony.UiccSlotInfo;
43import android.telephony.euicc.EuiccManager;
felkachanga6cec472018-03-29 12:08:20 +080044import android.util.Log;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070045import android.view.LayoutInflater;
46import android.view.View;
47import android.view.ViewGroup;
48import android.widget.Button;
Julia Reynoldsce25af42015-07-08 16:56:31 -040049import android.widget.TextView;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070050
Andrew Sapperstein88930e32019-03-07 14:18:15 -080051import androidx.annotation.VisibleForTesting;
52
Doris Ling72489722017-11-16 11:03:40 -080053import com.android.settings.core.InstrumentedFragment;
arangelov93081542017-10-27 13:51:28 +010054import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper;
Philip P. Moltmanne3f72112018-08-28 15:01:43 -070055import com.android.settingslib.RestrictedLockUtilsInternal;
Pasty Changc1f86002018-12-11 02:22:55 +000056
pastychang9bdb59a2019-01-21 09:49:15 +080057import com.google.android.setupcompat.template.FooterBarMixin;
58import com.google.android.setupcompat.template.FooterButton;
59import com.google.android.setupcompat.template.FooterButton.ButtonType;
Raff Tsaic8987752019-10-18 09:57:49 +080060import com.google.android.setupcompat.util.WizardManagerHelper;
Maurice Lam59f1c1a2019-02-14 22:04:30 +000061import com.google.android.setupdesign.GlifLayout;
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000062
Jiashen Wangbbcd54d2020-08-09 20:18:13 -070063import java.util.Arrays;
64
Amith Yamasanib14e1e02010-11-02 09:52:29 -070065/**
66 * Confirm and execute a reset of the device to a clean "just out of the box"
67 * state. Multiple confirmations are required: first, a general "are you sure
68 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
69 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
70 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
71 * locked, et cetera, then the confirmation sequence is abandoned.
72 *
73 * This is the confirmation screen.
74 */
Doris Ling72489722017-11-16 11:03:40 -080075public class MasterClearConfirm extends InstrumentedFragment {
felkachanga6cec472018-03-29 12:08:20 +080076 private final static String TAG = "MasterClearConfirm";
Amith Yamasanib14e1e02010-11-02 09:52:29 -070077
Andrew Sapperstein88930e32019-03-07 14:18:15 -080078 @VisibleForTesting View mContentView;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070079 private boolean mEraseSdCard;
Andrew Sapperstein88930e32019-03-07 14:18:15 -080080 @VisibleForTesting boolean mEraseEsims;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070081
82 /**
83 * The user has gone through the multiple confirmation, so now we go ahead
84 * and invoke the Checkin Service to reset the device to its factory-default
85 * state (rebooting in the process).
86 */
87 private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
88
89 public void onClick(View v) {
90 if (Utils.isMonkeyRunning()) {
91 return;
92 }
93
Jiashen Wangbbcd54d2020-08-09 20:18:13 -070094 // If the eSIM slot is in an error state, display a dialog to warn users that their eSIM
95 // profiles may not be fully deleted during FDR.
96 if (shouldShowEsimEraseFailureDialog()) {
97 Log.e(TAG, "eUICC card is in an error state. Display a dialog to warn the user.");
98 showEsimErrorDialog();
99 return;
100 }
101
102 performFactoryReset();
103 }
104
105 /**
106 * Returns true if the user choose to erase eSIM profile but the eUICC card is in an error
107 * state.
108 */
109 private boolean shouldShowEsimEraseFailureDialog() {
110 EuiccManager euiccManager = getActivity().getSystemService(EuiccManager.class);
111 TelephonyManager telephonyManager =
112 getActivity().getSystemService(TelephonyManager.class);
113
114 if (euiccManager == null || !euiccManager.isEnabled()) {
115 Log.i(
116 TAG,
117 "eSIM manager is disabled. No need to check eSIM slot before FDR.");
118 return false;
119 }
120 if (!mEraseEsims) {
121 Log.i(
122 TAG,
123 "eSIM does not need to be reset. No need to check eSIM slot before FDR.");
124 return false;
125 }
126 UiccSlotInfo[] slotInfos = telephonyManager.getUiccSlotsInfo();
127 if (slotInfos == null) {
128 Log.i(TAG, "Unable to get UICC slots.");
129 return false;
130 }
131 // If getIsEuicc() returns false for an eSIM slot, it means the eSIM is in the error
132 // state.
133 return Arrays.stream(slotInfos).anyMatch(
134 slot -> slot != null && !slot.isRemovable() && !slot.getIsEuicc());
135 }
136
137 private void showEsimErrorDialog() {
138 new AlertDialog.Builder(getActivity())
139 .setTitle(R.string.fdr_esim_failure_title)
140 .setMessage(R.string.fdr_esim_failure_text)
141 .setNeutralButton(R.string.dlg_cancel,
142 (DialogInterface.OnClickListener) (dialog, which) -> {
143 dialog.dismiss();
144 })
145 .setNegativeButton(R.string.fdr_esim_failure_reboot_btn,
146 (DialogInterface.OnClickListener) (dialog, which) -> {
147 dialog.dismiss();
148 PowerManager pm = (PowerManager) getActivity()
149 .getSystemService(Context.POWER_SERVICE);
150 pm.reboot(null);
151 })
152 .setPositiveButton(R.string.lockpassword_continue_label,
153 (DialogInterface.OnClickListener) (dialog, which) -> {
154 dialog.dismiss();
155 showContinueFdrDialog();
156 })
157 .show();
158 }
159
160 private void showContinueFdrDialog() {
161 new AlertDialog.Builder(getActivity())
162 .setTitle(R.string.fdr_continue_title)
163 .setMessage(R.string.fdr_continue_text)
164 .setNegativeButton(R.string.dlg_cancel,
165 (DialogInterface.OnClickListener) (dialog, which) -> {
166 dialog.dismiss();
167 })
168 .setPositiveButton(R.string.fdr_continue_btn,
169 (DialogInterface.OnClickListener) (dialog, which) -> {
170 dialog.dismiss();
171 performFactoryReset();
172 })
173 .show();
174 }
175
176 private void performFactoryReset() {
Andres Moralesab61b0d2014-08-26 13:54:12 -0700177 final PersistentDataBlockManager pdbManager = (PersistentDataBlockManager)
Andres Morales7ab89292014-07-10 16:10:43 -0700178 getActivity().getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
179
Alex Johnston74260a72020-03-19 17:34:46 +0000180 if (shouldWipePersistentDataBlock(pdbManager)) {
181
Andres Moralese6bf2a52014-09-08 13:14:00 -0700182 new AsyncTask<Void, Void, Void>() {
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700183 int mOldOrientation;
184 ProgressDialog mProgressDialog;
185
Andres Moralese6bf2a52014-09-08 13:14:00 -0700186 @Override
187 protected Void doInBackground(Void... params) {
188 pdbManager.wipe();
189 return null;
190 }
Andres Moralesab61b0d2014-08-26 13:54:12 -0700191
Andres Moralese6bf2a52014-09-08 13:14:00 -0700192 @Override
193 protected void onPostExecute(Void aVoid) {
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700194 mProgressDialog.hide();
Julia Reynolds8a388012015-08-19 16:19:55 -0400195 if (getActivity() != null) {
196 getActivity().setRequestedOrientation(mOldOrientation);
197 doMasterClear();
198 }
Andres Moralese6bf2a52014-09-08 13:14:00 -0700199 }
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700200
201 @Override
202 protected void onPreExecute() {
203 mProgressDialog = getProgressDialog();
204 mProgressDialog.show();
205
206 // need to prevent orientation changes as we're about to go into
felkachanga6cec472018-03-29 12:08:20 +0800207 // a long IO request, so we won't be able to access inflate resources on
208 // flash
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700209 mOldOrientation = getActivity().getRequestedOrientation();
felkachanga6cec472018-03-29 12:08:20 +0800210 getActivity().setRequestedOrientation(
211 ActivityInfo.SCREEN_ORIENTATION_LOCKED);
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700212 }
Andres Moralese6bf2a52014-09-08 13:14:00 -0700213 }.execute();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700214 } else {
Andres Moralesab61b0d2014-08-26 13:54:12 -0700215 doMasterClear();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700216 }
217 }
Andres Moralesab61b0d2014-08-26 13:54:12 -0700218
219 private ProgressDialog getProgressDialog() {
220 final ProgressDialog progressDialog = new ProgressDialog(getActivity());
221 progressDialog.setIndeterminate(true);
222 progressDialog.setCancelable(false);
223 progressDialog.setTitle(
224 getActivity().getString(R.string.master_clear_progress_title));
225 progressDialog.setMessage(
226 getActivity().getString(R.string.master_clear_progress_text));
227 return progressDialog;
228 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700229 };
230
Alex Johnston74260a72020-03-19 17:34:46 +0000231 @VisibleForTesting
232 boolean shouldWipePersistentDataBlock(PersistentDataBlockManager pdbManager) {
233 if (pdbManager == null) {
234 return false;
235 }
236 // The persistent data block will persist if the device is still being provisioned.
237 if (isDeviceStillBeingProvisioned()) {
238 return false;
239 }
240 // If OEM unlock is allowed, the persistent data block will be wiped during FR
241 // process. If disabled, it will be wiped here instead.
242 if (isOemUnlockedAllowed()) {
243 return false;
244 }
Alex Johnston05c0ab02020-04-28 13:47:14 +0100245 final DevicePolicyManager dpm = (DevicePolicyManager) getActivity()
246 .getSystemService(Context.DEVICE_POLICY_SERVICE);
247 // Do not erase the factory reset protection data (from Settings) if factory reset
248 // protection policy is not supported on the device.
249 if (!dpm.isFactoryResetProtectionPolicySupported()) {
250 return false;
251 }
Alex Johnston74260a72020-03-19 17:34:46 +0000252 // Do not erase the factory reset protection data (from Settings) if the
253 // device is an organization-owned managed profile device and a factory
254 // reset protection policy has been set.
Alex Johnston74260a72020-03-19 17:34:46 +0000255 FactoryResetProtectionPolicy frpPolicy = dpm.getFactoryResetProtectionPolicy(null);
256 if (dpm.isOrganizationOwnedDeviceWithManagedProfile() && frpPolicy != null
257 && frpPolicy.isNotEmpty()) {
258 return false;
259 }
260 return true;
261 }
262
263 @VisibleForTesting
264 boolean isOemUnlockedAllowed() {
265 return ((OemLockManager) getActivity().getSystemService(
266 Context.OEM_LOCK_SERVICE)).isOemUnlockAllowed();
267 }
268
269 @VisibleForTesting
270 boolean isDeviceStillBeingProvisioned() {
271 return !WizardManagerHelper.isDeviceProvisioned(getActivity());
272 }
273
Andres Moralesab61b0d2014-08-26 13:54:12 -0700274 private void doMasterClear() {
Lenka Trochtova3393dac2017-02-13 15:05:27 +0100275 Intent intent = new Intent(Intent.ACTION_FACTORY_RESET);
Christopher Tate1e99f0c2017-01-25 14:46:48 -0800276 intent.setPackage("android");
Rubin Xuccbdc572015-06-26 15:27:23 +0100277 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
278 intent.putExtra(Intent.EXTRA_REASON, "MasterClearConfirm");
279 intent.putExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, mEraseSdCard);
qingxi072f2862017-04-11 18:28:40 -0700280 intent.putExtra(Intent.EXTRA_WIPE_ESIMS, mEraseEsims);
Rubin Xuccbdc572015-06-26 15:27:23 +0100281 getActivity().sendBroadcast(intent);
282 // Intent handling is asynchronous -- assume it will happen soon.
Andres Moralesab61b0d2014-08-26 13:54:12 -0700283 }
284
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700285 /**
286 * Configure the UI for the final confirmation interaction
287 */
288 private void establishFinalConfirmationState() {
Maurice Lam59f1c1a2019-02-14 22:04:30 +0000289 final GlifLayout layout = mContentView.findViewById(R.id.setup_wizard_layout);
felkachanga6cec472018-03-29 12:08:20 +0800290
pastychang9bdb59a2019-01-21 09:49:15 +0800291 final FooterBarMixin mixin = layout.getMixin(FooterBarMixin.class);
292 mixin.setPrimaryButton(
pastychang908501e2018-12-26 10:46:40 +0800293 new FooterButton.Builder(getActivity())
294 .setText(R.string.master_clear_button_text)
295 .setListener(mFinalClickListener)
296 .setButtonType(ButtonType.OTHER)
pastychang79614822019-01-03 10:12:54 +0800297 .setTheme(R.style.SudGlifButton_Primary)
pastychang908501e2018-12-26 10:46:40 +0800298 .build()
pastychang103409b2018-12-13 23:00:17 +0800299 );
felkachanga6cec472018-03-29 12:08:20 +0800300 }
301
302 private void setUpActionBarAndTitle() {
303 final Activity activity = getActivity();
304 if (activity == null) {
305 Log.e(TAG, "No activity attached, skipping setUpActionBarAndTitle");
306 return;
307 }
308 final ActionBar actionBar = activity.getActionBar();
309 if (actionBar == null) {
310 Log.e(TAG, "No actionbar, skipping setUpActionBarAndTitle");
311 return;
312 }
313 actionBar.hide();
314 activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700315 }
316
317 @Override
318 public View onCreateView(LayoutInflater inflater, ViewGroup container,
319 Bundle savedInstanceState) {
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700320 final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000321 getActivity(), UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700322 if (RestrictedLockUtilsInternal.hasBaseUserRestriction(getActivity(),
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000323 UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId())) {
Julia Reynolds2c539332014-06-11 12:56:02 -0400324 return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000325 } else if (admin != null) {
arangelov93081542017-10-27 13:51:28 +0100326 new ActionDisabledByAdminDialogHelper(getActivity())
327 .prepareDialogBuilder(UserManager.DISALLOW_FACTORY_RESET, admin)
328 .setOnDismissListener(__ -> getActivity().finish())
329 .show();
330 return new View(getActivity());
Julia Reynolds2c539332014-06-11 12:56:02 -0400331 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700332 mContentView = inflater.inflate(R.layout.master_clear_confirm, null);
felkachanga6cec472018-03-29 12:08:20 +0800333 setUpActionBarAndTitle();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700334 establishFinalConfirmationState();
Julia Reynoldsce25af42015-07-08 16:56:31 -0400335 setAccessibilityTitle();
Andrew Sapperstein88930e32019-03-07 14:18:15 -0800336 setSubtitle();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700337 return mContentView;
338 }
339
Julia Reynoldsce25af42015-07-08 16:56:31 -0400340 private void setAccessibilityTitle() {
341 CharSequence currentTitle = getActivity().getTitle();
pastychangfa68ec42019-04-12 16:22:17 +0800342 TextView confirmationMessage = mContentView.findViewById(R.id.sud_layout_description);
Julia Reynoldsce25af42015-07-08 16:56:31 -0400343 if (confirmationMessage != null) {
arangelov93081542017-10-27 13:51:28 +0100344 String accessibleText = new StringBuilder(currentTitle).append(",").append(
Julia Reynoldsce25af42015-07-08 16:56:31 -0400345 confirmationMessage.getText()).toString();
arangelov93081542017-10-27 13:51:28 +0100346 getActivity().setTitle(Utils.createAccessibleSequence(currentTitle, accessibleText));
Julia Reynoldsce25af42015-07-08 16:56:31 -0400347 }
348 }
349
Andrew Sapperstein88930e32019-03-07 14:18:15 -0800350 @VisibleForTesting
351 void setSubtitle() {
352 if (mEraseEsims) {
pastychangfa68ec42019-04-12 16:22:17 +0800353 ((TextView) mContentView.findViewById(R.id.sud_layout_description))
Andrew Sapperstein88930e32019-03-07 14:18:15 -0800354 .setText(R.string.master_clear_final_desc_esim);
355 }
356 }
357
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700358 @Override
359 public void onCreate(Bundle savedInstanceState) {
360 super.onCreate(savedInstanceState);
361
362 Bundle args = getArguments();
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700363 mEraseSdCard = args != null
364 && args.getBoolean(MasterClear.ERASE_EXTERNAL_EXTRA);
qingxi072f2862017-04-11 18:28:40 -0700365 mEraseEsims = args != null
366 && args.getBoolean(MasterClear.ERASE_ESIMS_EXTRA);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700367 }
Chris Wren8a963ba2015-03-20 10:29:14 -0400368
369 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700370 public int getMetricsCategory() {
Fan Zhang31b21002019-01-16 13:49:47 -0800371 return SettingsEnums.MASTER_CLEAR_CONFIRM;
Chris Wren8a963ba2015-03-20 10:29:14 -0400372 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700373}