blob: 8ebb71054b358b93c7c41794a1643b15d37c3918 [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;
Andres Morales7ab89292014-07-10 16:10:43 -070025import android.content.Context;
Jason Monk39b46742015-09-10 15:52:51 -040026import android.content.Intent;
Andres Moralesab61b0d2014-08-26 13:54:12 -070027import android.content.pm.ActivityInfo;
felkachanga6cec472018-03-29 12:08:20 +080028import android.graphics.Color;
Andres Moralesab61b0d2014-08-26 13:54:12 -070029import android.os.AsyncTask;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070030import android.os.Bundle;
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000031import android.os.UserHandle;
Julia Reynolds2c539332014-06-11 12:56:02 -040032import android.os.UserManager;
Andrew Scullc23357e2017-04-18 09:40:11 +010033import android.service.oemlock.OemLockManager;
Jason Monk39b46742015-09-10 15:52:51 -040034import android.service.persistentdata.PersistentDataBlockManager;
felkachanga6cec472018-03-29 12:08:20 +080035import android.util.Log;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070036import android.view.LayoutInflater;
37import android.view.View;
38import android.view.ViewGroup;
39import android.widget.Button;
Julia Reynoldsce25af42015-07-08 16:56:31 -040040import android.widget.TextView;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070041
Tamas Berghammer265d3c22016-06-22 15:34:45 +010042import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Doris Ling72489722017-11-16 11:03:40 -080043import com.android.settings.core.InstrumentedFragment;
arangelov93081542017-10-27 13:51:28 +010044import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper;
Philip P. Moltmanne3f72112018-08-28 15:01:43 -070045import com.android.settingslib.RestrictedLockUtilsInternal;
felkachanga6cec472018-03-29 12:08:20 +080046import com.android.setupwizardlib.TemplateLayout;
47import com.android.setupwizardlib.template.ButtonFooterMixin;
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000048
Amith Yamasanib14e1e02010-11-02 09:52:29 -070049/**
50 * Confirm and execute a reset of the device to a clean "just out of the box"
51 * state. Multiple confirmations are required: first, a general "are you sure
52 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
53 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
54 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
55 * locked, et cetera, then the confirmation sequence is abandoned.
56 *
57 * This is the confirmation screen.
58 */
Doris Ling72489722017-11-16 11:03:40 -080059public class MasterClearConfirm extends InstrumentedFragment {
felkachanga6cec472018-03-29 12:08:20 +080060 private final static String TAG = "MasterClearConfirm";
Amith Yamasanib14e1e02010-11-02 09:52:29 -070061
62 private View mContentView;
63 private boolean mEraseSdCard;
qingxi072f2862017-04-11 18:28:40 -070064 private boolean mEraseEsims;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070065
66 /**
67 * The user has gone through the multiple confirmation, so now we go ahead
68 * and invoke the Checkin Service to reset the device to its factory-default
69 * state (rebooting in the process).
70 */
71 private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
72
73 public void onClick(View v) {
74 if (Utils.isMonkeyRunning()) {
75 return;
76 }
77
Andres Moralesab61b0d2014-08-26 13:54:12 -070078 final PersistentDataBlockManager pdbManager = (PersistentDataBlockManager)
Andres Morales7ab89292014-07-10 16:10:43 -070079 getActivity().getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
Andrew Scullc23357e2017-04-18 09:40:11 +010080 final OemLockManager oemLockManager = (OemLockManager)
81 getActivity().getSystemService(Context.OEM_LOCK_SERVICE);
Andres Morales7ab89292014-07-10 16:10:43 -070082
Andrew Scullc23357e2017-04-18 09:40:11 +010083 if (pdbManager != null && !oemLockManager.isOemUnlockAllowed() &&
Udam Saini679f7ad2016-03-25 10:47:00 -070084 Utils.isDeviceProvisioned(getActivity())) {
Andrew Scullc23357e2017-04-18 09:40:11 +010085 // if OEM unlock is allowed, the persistent data block will be wiped during FR
86 // process. If disabled, it will be wiped here, unless the device is still being
87 // provisioned, in which case the persistent data block will be preserved.
Andres Moralese6bf2a52014-09-08 13:14:00 -070088 new AsyncTask<Void, Void, Void>() {
Andres Moralesc17ec1b2015-06-01 16:23:41 -070089 int mOldOrientation;
90 ProgressDialog mProgressDialog;
91
Andres Moralese6bf2a52014-09-08 13:14:00 -070092 @Override
93 protected Void doInBackground(Void... params) {
94 pdbManager.wipe();
95 return null;
96 }
Andres Moralesab61b0d2014-08-26 13:54:12 -070097
Andres Moralese6bf2a52014-09-08 13:14:00 -070098 @Override
99 protected void onPostExecute(Void aVoid) {
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700100 mProgressDialog.hide();
Julia Reynolds8a388012015-08-19 16:19:55 -0400101 if (getActivity() != null) {
102 getActivity().setRequestedOrientation(mOldOrientation);
103 doMasterClear();
104 }
Andres Moralese6bf2a52014-09-08 13:14:00 -0700105 }
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700106
107 @Override
108 protected void onPreExecute() {
109 mProgressDialog = getProgressDialog();
110 mProgressDialog.show();
111
112 // need to prevent orientation changes as we're about to go into
felkachanga6cec472018-03-29 12:08:20 +0800113 // a long IO request, so we won't be able to access inflate resources on
114 // flash
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700115 mOldOrientation = getActivity().getRequestedOrientation();
felkachanga6cec472018-03-29 12:08:20 +0800116 getActivity().setRequestedOrientation(
117 ActivityInfo.SCREEN_ORIENTATION_LOCKED);
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700118 }
Andres Moralese6bf2a52014-09-08 13:14:00 -0700119 }.execute();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700120 } else {
Andres Moralesab61b0d2014-08-26 13:54:12 -0700121 doMasterClear();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700122 }
123 }
Andres Moralesab61b0d2014-08-26 13:54:12 -0700124
125 private ProgressDialog getProgressDialog() {
126 final ProgressDialog progressDialog = new ProgressDialog(getActivity());
127 progressDialog.setIndeterminate(true);
128 progressDialog.setCancelable(false);
129 progressDialog.setTitle(
130 getActivity().getString(R.string.master_clear_progress_title));
131 progressDialog.setMessage(
132 getActivity().getString(R.string.master_clear_progress_text));
133 return progressDialog;
134 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700135 };
136
Andres Moralesab61b0d2014-08-26 13:54:12 -0700137 private void doMasterClear() {
Lenka Trochtova3393dac2017-02-13 15:05:27 +0100138 Intent intent = new Intent(Intent.ACTION_FACTORY_RESET);
Christopher Tate1e99f0c2017-01-25 14:46:48 -0800139 intent.setPackage("android");
Rubin Xuccbdc572015-06-26 15:27:23 +0100140 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
141 intent.putExtra(Intent.EXTRA_REASON, "MasterClearConfirm");
142 intent.putExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, mEraseSdCard);
qingxi072f2862017-04-11 18:28:40 -0700143 intent.putExtra(Intent.EXTRA_WIPE_ESIMS, mEraseEsims);
Rubin Xuccbdc572015-06-26 15:27:23 +0100144 getActivity().sendBroadcast(intent);
145 // Intent handling is asynchronous -- assume it will happen soon.
Andres Moralesab61b0d2014-08-26 13:54:12 -0700146 }
147
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700148 /**
149 * Configure the UI for the final confirmation interaction
150 */
151 private void establishFinalConfirmationState() {
felkachanga6cec472018-03-29 12:08:20 +0800152 final TemplateLayout layout = mContentView.findViewById(R.id.setup_wizard_layout);
153
154 final ButtonFooterMixin buttonFooterMixin = layout.getMixin(ButtonFooterMixin.class);
155 buttonFooterMixin.removeAllViews();
156 buttonFooterMixin.addSpace();
157 buttonFooterMixin.addSpace();
158 buttonFooterMixin.addButton(R.string.master_clear_button_text,
159 R.style.SuwGlifButton_Primary).setOnClickListener(mFinalClickListener);
160 }
161
162 private void setUpActionBarAndTitle() {
163 final Activity activity = getActivity();
164 if (activity == null) {
165 Log.e(TAG, "No activity attached, skipping setUpActionBarAndTitle");
166 return;
167 }
168 final ActionBar actionBar = activity.getActionBar();
169 if (actionBar == null) {
170 Log.e(TAG, "No actionbar, skipping setUpActionBarAndTitle");
171 return;
172 }
173 actionBar.hide();
174 activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700175 }
176
177 @Override
178 public View onCreateView(LayoutInflater inflater, ViewGroup container,
179 Bundle savedInstanceState) {
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700180 final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000181 getActivity(), UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700182 if (RestrictedLockUtilsInternal.hasBaseUserRestriction(getActivity(),
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000183 UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId())) {
Julia Reynolds2c539332014-06-11 12:56:02 -0400184 return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000185 } else if (admin != null) {
arangelov93081542017-10-27 13:51:28 +0100186 new ActionDisabledByAdminDialogHelper(getActivity())
187 .prepareDialogBuilder(UserManager.DISALLOW_FACTORY_RESET, admin)
188 .setOnDismissListener(__ -> getActivity().finish())
189 .show();
190 return new View(getActivity());
Julia Reynolds2c539332014-06-11 12:56:02 -0400191 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700192 mContentView = inflater.inflate(R.layout.master_clear_confirm, null);
felkachanga6cec472018-03-29 12:08:20 +0800193 setUpActionBarAndTitle();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700194 establishFinalConfirmationState();
Julia Reynoldsce25af42015-07-08 16:56:31 -0400195 setAccessibilityTitle();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700196 return mContentView;
197 }
198
Julia Reynoldsce25af42015-07-08 16:56:31 -0400199 private void setAccessibilityTitle() {
200 CharSequence currentTitle = getActivity().getTitle();
felkachanga6cec472018-03-29 12:08:20 +0800201 TextView confirmationMessage = mContentView.findViewById(R.id.master_clear_confirm);
Julia Reynoldsce25af42015-07-08 16:56:31 -0400202 if (confirmationMessage != null) {
arangelov93081542017-10-27 13:51:28 +0100203 String accessibleText = new StringBuilder(currentTitle).append(",").append(
Julia Reynoldsce25af42015-07-08 16:56:31 -0400204 confirmationMessage.getText()).toString();
arangelov93081542017-10-27 13:51:28 +0100205 getActivity().setTitle(Utils.createAccessibleSequence(currentTitle, accessibleText));
Julia Reynoldsce25af42015-07-08 16:56:31 -0400206 }
207 }
208
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700209 @Override
210 public void onCreate(Bundle savedInstanceState) {
211 super.onCreate(savedInstanceState);
212
213 Bundle args = getArguments();
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700214 mEraseSdCard = args != null
215 && args.getBoolean(MasterClear.ERASE_EXTERNAL_EXTRA);
qingxi072f2862017-04-11 18:28:40 -0700216 mEraseEsims = args != null
217 && args.getBoolean(MasterClear.ERASE_ESIMS_EXTRA);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700218 }
Chris Wren8a963ba2015-03-20 10:29:14 -0400219
220 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700221 public int getMetricsCategory() {
Chris Wren9d1bfd12016-01-26 18:04:01 -0500222 return MetricsEvent.MASTER_CLEAR_CONFIRM;
Chris Wren8a963ba2015-03-20 10:29:14 -0400223 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700224}