blob: 679f18f39c005119c5ddb2c4456275f05bce5544 [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;
Andres Moralesab61b0d2014-08-26 13:54:12 -070024import android.app.ProgressDialog;
Fan Zhang31b21002019-01-16 13:49:47 -080025import android.app.settings.SettingsEnums;
Andres Morales7ab89292014-07-10 16:10:43 -070026import android.content.Context;
Jason Monk39b46742015-09-10 15:52:51 -040027import android.content.Intent;
Andres Moralesab61b0d2014-08-26 13:54:12 -070028import android.content.pm.ActivityInfo;
felkachanga6cec472018-03-29 12:08:20 +080029import android.graphics.Color;
Andres Moralesab61b0d2014-08-26 13:54:12 -070030import android.os.AsyncTask;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070031import android.os.Bundle;
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000032import android.os.UserHandle;
Julia Reynolds2c539332014-06-11 12:56:02 -040033import android.os.UserManager;
Andrew Scullc23357e2017-04-18 09:40:11 +010034import android.service.oemlock.OemLockManager;
Jason Monk39b46742015-09-10 15:52:51 -040035import android.service.persistentdata.PersistentDataBlockManager;
felkachanga6cec472018-03-29 12:08:20 +080036import android.util.Log;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070037import android.view.LayoutInflater;
38import android.view.View;
39import android.view.ViewGroup;
40import android.widget.Button;
Julia Reynoldsce25af42015-07-08 16:56:31 -040041import android.widget.TextView;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070042
Andrew Sapperstein88930e32019-03-07 14:18:15 -080043import androidx.annotation.VisibleForTesting;
44
Doris Ling72489722017-11-16 11:03:40 -080045import com.android.settings.core.InstrumentedFragment;
arangelov93081542017-10-27 13:51:28 +010046import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper;
Philip P. Moltmanne3f72112018-08-28 15:01:43 -070047import com.android.settingslib.RestrictedLockUtilsInternal;
Pasty Changc1f86002018-12-11 02:22:55 +000048
pastychang9bdb59a2019-01-21 09:49:15 +080049import com.google.android.setupcompat.template.FooterBarMixin;
50import com.google.android.setupcompat.template.FooterButton;
51import com.google.android.setupcompat.template.FooterButton.ButtonType;
Raff Tsaic8987752019-10-18 09:57:49 +080052import com.google.android.setupcompat.util.WizardManagerHelper;
Maurice Lam59f1c1a2019-02-14 22:04:30 +000053import com.google.android.setupdesign.GlifLayout;
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000054
Amith Yamasanib14e1e02010-11-02 09:52:29 -070055/**
56 * Confirm and execute a reset of the device 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 ERASE EVERYTHING
60 * ON THE PHONE" 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 MasterClearConfirm extends InstrumentedFragment {
felkachanga6cec472018-03-29 12:08:20 +080066 private final static String TAG = "MasterClearConfirm";
Amith Yamasanib14e1e02010-11-02 09:52:29 -070067
Andrew Sapperstein88930e32019-03-07 14:18:15 -080068 @VisibleForTesting View mContentView;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070069 private boolean mEraseSdCard;
Andrew Sapperstein88930e32019-03-07 14:18:15 -080070 @VisibleForTesting boolean mEraseEsims;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070071
72 /**
73 * The user has gone through the multiple confirmation, so now we go ahead
74 * and invoke the Checkin Service to reset the device to its factory-default
75 * state (rebooting in the process).
76 */
77 private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
78
79 public void onClick(View v) {
80 if (Utils.isMonkeyRunning()) {
81 return;
82 }
83
Andres Moralesab61b0d2014-08-26 13:54:12 -070084 final PersistentDataBlockManager pdbManager = (PersistentDataBlockManager)
Andres Morales7ab89292014-07-10 16:10:43 -070085 getActivity().getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
Andrew Scullc23357e2017-04-18 09:40:11 +010086 final OemLockManager oemLockManager = (OemLockManager)
87 getActivity().getSystemService(Context.OEM_LOCK_SERVICE);
Andres Morales7ab89292014-07-10 16:10:43 -070088
Andrew Scullc23357e2017-04-18 09:40:11 +010089 if (pdbManager != null && !oemLockManager.isOemUnlockAllowed() &&
Raff Tsaic8987752019-10-18 09:57:49 +080090 WizardManagerHelper.isDeviceProvisioned(getActivity())) {
Andrew Scullc23357e2017-04-18 09:40:11 +010091 // if OEM unlock is allowed, the persistent data block will be wiped during FR
92 // process. If disabled, it will be wiped here, unless the device is still being
93 // provisioned, in which case the persistent data block will be preserved.
Andres Moralese6bf2a52014-09-08 13:14:00 -070094 new AsyncTask<Void, Void, Void>() {
Andres Moralesc17ec1b2015-06-01 16:23:41 -070095 int mOldOrientation;
96 ProgressDialog mProgressDialog;
97
Andres Moralese6bf2a52014-09-08 13:14:00 -070098 @Override
99 protected Void doInBackground(Void... params) {
100 pdbManager.wipe();
101 return null;
102 }
Andres Moralesab61b0d2014-08-26 13:54:12 -0700103
Andres Moralese6bf2a52014-09-08 13:14:00 -0700104 @Override
105 protected void onPostExecute(Void aVoid) {
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700106 mProgressDialog.hide();
Julia Reynolds8a388012015-08-19 16:19:55 -0400107 if (getActivity() != null) {
108 getActivity().setRequestedOrientation(mOldOrientation);
109 doMasterClear();
110 }
Andres Moralese6bf2a52014-09-08 13:14:00 -0700111 }
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700112
113 @Override
114 protected void onPreExecute() {
115 mProgressDialog = getProgressDialog();
116 mProgressDialog.show();
117
118 // need to prevent orientation changes as we're about to go into
felkachanga6cec472018-03-29 12:08:20 +0800119 // a long IO request, so we won't be able to access inflate resources on
120 // flash
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700121 mOldOrientation = getActivity().getRequestedOrientation();
felkachanga6cec472018-03-29 12:08:20 +0800122 getActivity().setRequestedOrientation(
123 ActivityInfo.SCREEN_ORIENTATION_LOCKED);
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700124 }
Andres Moralese6bf2a52014-09-08 13:14:00 -0700125 }.execute();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700126 } else {
Andres Moralesab61b0d2014-08-26 13:54:12 -0700127 doMasterClear();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700128 }
129 }
Andres Moralesab61b0d2014-08-26 13:54:12 -0700130
131 private ProgressDialog getProgressDialog() {
132 final ProgressDialog progressDialog = new ProgressDialog(getActivity());
133 progressDialog.setIndeterminate(true);
134 progressDialog.setCancelable(false);
135 progressDialog.setTitle(
136 getActivity().getString(R.string.master_clear_progress_title));
137 progressDialog.setMessage(
138 getActivity().getString(R.string.master_clear_progress_text));
139 return progressDialog;
140 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700141 };
142
Andres Moralesab61b0d2014-08-26 13:54:12 -0700143 private void doMasterClear() {
Lenka Trochtova3393dac2017-02-13 15:05:27 +0100144 Intent intent = new Intent(Intent.ACTION_FACTORY_RESET);
Christopher Tate1e99f0c2017-01-25 14:46:48 -0800145 intent.setPackage("android");
Rubin Xuccbdc572015-06-26 15:27:23 +0100146 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
147 intent.putExtra(Intent.EXTRA_REASON, "MasterClearConfirm");
148 intent.putExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, mEraseSdCard);
qingxi072f2862017-04-11 18:28:40 -0700149 intent.putExtra(Intent.EXTRA_WIPE_ESIMS, mEraseEsims);
Rubin Xuccbdc572015-06-26 15:27:23 +0100150 getActivity().sendBroadcast(intent);
151 // Intent handling is asynchronous -- assume it will happen soon.
Andres Moralesab61b0d2014-08-26 13:54:12 -0700152 }
153
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700154 /**
155 * Configure the UI for the final confirmation interaction
156 */
157 private void establishFinalConfirmationState() {
Maurice Lam59f1c1a2019-02-14 22:04:30 +0000158 final GlifLayout layout = mContentView.findViewById(R.id.setup_wizard_layout);
felkachanga6cec472018-03-29 12:08:20 +0800159
pastychang9bdb59a2019-01-21 09:49:15 +0800160 final FooterBarMixin mixin = layout.getMixin(FooterBarMixin.class);
161 mixin.setPrimaryButton(
pastychang908501e2018-12-26 10:46:40 +0800162 new FooterButton.Builder(getActivity())
163 .setText(R.string.master_clear_button_text)
164 .setListener(mFinalClickListener)
165 .setButtonType(ButtonType.OTHER)
pastychang79614822019-01-03 10:12:54 +0800166 .setTheme(R.style.SudGlifButton_Primary)
pastychang908501e2018-12-26 10:46:40 +0800167 .build()
pastychang103409b2018-12-13 23:00:17 +0800168 );
felkachanga6cec472018-03-29 12:08:20 +0800169 }
170
171 private void setUpActionBarAndTitle() {
172 final Activity activity = getActivity();
173 if (activity == null) {
174 Log.e(TAG, "No activity attached, skipping setUpActionBarAndTitle");
175 return;
176 }
177 final ActionBar actionBar = activity.getActionBar();
178 if (actionBar == null) {
179 Log.e(TAG, "No actionbar, skipping setUpActionBarAndTitle");
180 return;
181 }
182 actionBar.hide();
183 activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700184 }
185
186 @Override
187 public View onCreateView(LayoutInflater inflater, ViewGroup container,
188 Bundle savedInstanceState) {
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700189 final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000190 getActivity(), UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700191 if (RestrictedLockUtilsInternal.hasBaseUserRestriction(getActivity(),
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000192 UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId())) {
Julia Reynolds2c539332014-06-11 12:56:02 -0400193 return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000194 } else if (admin != null) {
arangelov93081542017-10-27 13:51:28 +0100195 new ActionDisabledByAdminDialogHelper(getActivity())
196 .prepareDialogBuilder(UserManager.DISALLOW_FACTORY_RESET, admin)
197 .setOnDismissListener(__ -> getActivity().finish())
198 .show();
199 return new View(getActivity());
Julia Reynolds2c539332014-06-11 12:56:02 -0400200 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700201 mContentView = inflater.inflate(R.layout.master_clear_confirm, null);
felkachanga6cec472018-03-29 12:08:20 +0800202 setUpActionBarAndTitle();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700203 establishFinalConfirmationState();
Julia Reynoldsce25af42015-07-08 16:56:31 -0400204 setAccessibilityTitle();
Andrew Sapperstein88930e32019-03-07 14:18:15 -0800205 setSubtitle();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700206 return mContentView;
207 }
208
Julia Reynoldsce25af42015-07-08 16:56:31 -0400209 private void setAccessibilityTitle() {
210 CharSequence currentTitle = getActivity().getTitle();
pastychangfa68ec42019-04-12 16:22:17 +0800211 TextView confirmationMessage = mContentView.findViewById(R.id.sud_layout_description);
Julia Reynoldsce25af42015-07-08 16:56:31 -0400212 if (confirmationMessage != null) {
arangelov93081542017-10-27 13:51:28 +0100213 String accessibleText = new StringBuilder(currentTitle).append(",").append(
Julia Reynoldsce25af42015-07-08 16:56:31 -0400214 confirmationMessage.getText()).toString();
arangelov93081542017-10-27 13:51:28 +0100215 getActivity().setTitle(Utils.createAccessibleSequence(currentTitle, accessibleText));
Julia Reynoldsce25af42015-07-08 16:56:31 -0400216 }
217 }
218
Andrew Sapperstein88930e32019-03-07 14:18:15 -0800219 @VisibleForTesting
220 void setSubtitle() {
221 if (mEraseEsims) {
pastychangfa68ec42019-04-12 16:22:17 +0800222 ((TextView) mContentView.findViewById(R.id.sud_layout_description))
Andrew Sapperstein88930e32019-03-07 14:18:15 -0800223 .setText(R.string.master_clear_final_desc_esim);
224 }
225 }
226
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700227 @Override
228 public void onCreate(Bundle savedInstanceState) {
229 super.onCreate(savedInstanceState);
230
231 Bundle args = getArguments();
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700232 mEraseSdCard = args != null
233 && args.getBoolean(MasterClear.ERASE_EXTERNAL_EXTRA);
qingxi072f2862017-04-11 18:28:40 -0700234 mEraseEsims = args != null
235 && args.getBoolean(MasterClear.ERASE_ESIMS_EXTRA);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700236 }
Chris Wren8a963ba2015-03-20 10:29:14 -0400237
238 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700239 public int getMetricsCategory() {
Fan Zhang31b21002019-01-16 13:49:47 -0800240 return SettingsEnums.MASTER_CLEAR_CONFIRM;
Chris Wren8a963ba2015-03-20 10:29:14 -0400241 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700242}