blob: cfd20ebf03d9a8925e848380307ad7b96d8959bc [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
Fan Zhangc7162cd2018-06-18 15:21:41 -070019import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
20
Andres Moralesab61b0d2014-08-26 13:54:12 -070021import android.app.ProgressDialog;
Andres Morales7ab89292014-07-10 16:10:43 -070022import android.content.Context;
Jason Monk39b46742015-09-10 15:52:51 -040023import android.content.Intent;
Andres Moralesab61b0d2014-08-26 13:54:12 -070024import android.content.pm.ActivityInfo;
25import android.os.AsyncTask;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070026import android.os.Bundle;
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000027import android.os.UserHandle;
Julia Reynolds2c539332014-06-11 12:56:02 -040028import android.os.UserManager;
Andrew Scullc23357e2017-04-18 09:40:11 +010029import android.service.oemlock.OemLockManager;
Jason Monk39b46742015-09-10 15:52:51 -040030import android.service.persistentdata.PersistentDataBlockManager;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070031import android.view.LayoutInflater;
32import android.view.View;
33import android.view.ViewGroup;
34import android.widget.Button;
Julia Reynoldsce25af42015-07-08 16:56:31 -040035import android.widget.TextView;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070036
Tamas Berghammer265d3c22016-06-22 15:34:45 +010037import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Doris Ling72489722017-11-16 11:03:40 -080038import com.android.settings.core.InstrumentedFragment;
arangelov93081542017-10-27 13:51:28 +010039import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper;
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000040import com.android.settingslib.RestrictedLockUtils;
Philip P. Moltmanne3f72112018-08-28 15:01:43 -070041import com.android.settingslib.RestrictedLockUtilsInternal;
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000042
Amith Yamasanib14e1e02010-11-02 09:52:29 -070043/**
44 * Confirm and execute a reset of the device to a clean "just out of the box"
45 * state. Multiple confirmations are required: first, a general "are you sure
46 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
47 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
48 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
49 * locked, et cetera, then the confirmation sequence is abandoned.
50 *
51 * This is the confirmation screen.
52 */
Doris Ling72489722017-11-16 11:03:40 -080053public class MasterClearConfirm extends InstrumentedFragment {
Amith Yamasanib14e1e02010-11-02 09:52:29 -070054
55 private View mContentView;
56 private boolean mEraseSdCard;
qingxi072f2862017-04-11 18:28:40 -070057 private boolean mEraseEsims;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070058
59 /**
60 * The user has gone through the multiple confirmation, so now we go ahead
61 * and invoke the Checkin Service to reset the device to its factory-default
62 * state (rebooting in the process).
63 */
64 private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
65
66 public void onClick(View v) {
67 if (Utils.isMonkeyRunning()) {
68 return;
69 }
70
Andres Moralesab61b0d2014-08-26 13:54:12 -070071 final PersistentDataBlockManager pdbManager = (PersistentDataBlockManager)
Andres Morales7ab89292014-07-10 16:10:43 -070072 getActivity().getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
Andrew Scullc23357e2017-04-18 09:40:11 +010073 final OemLockManager oemLockManager = (OemLockManager)
74 getActivity().getSystemService(Context.OEM_LOCK_SERVICE);
Andres Morales7ab89292014-07-10 16:10:43 -070075
Andrew Scullc23357e2017-04-18 09:40:11 +010076 if (pdbManager != null && !oemLockManager.isOemUnlockAllowed() &&
Udam Saini679f7ad2016-03-25 10:47:00 -070077 Utils.isDeviceProvisioned(getActivity())) {
Andrew Scullc23357e2017-04-18 09:40:11 +010078 // if OEM unlock is allowed, the persistent data block will be wiped during FR
79 // process. If disabled, it will be wiped here, unless the device is still being
80 // provisioned, in which case the persistent data block will be preserved.
Andres Moralese6bf2a52014-09-08 13:14:00 -070081 new AsyncTask<Void, Void, Void>() {
Andres Moralesc17ec1b2015-06-01 16:23:41 -070082 int mOldOrientation;
83 ProgressDialog mProgressDialog;
84
Andres Moralese6bf2a52014-09-08 13:14:00 -070085 @Override
86 protected Void doInBackground(Void... params) {
87 pdbManager.wipe();
88 return null;
89 }
Andres Moralesab61b0d2014-08-26 13:54:12 -070090
Andres Moralese6bf2a52014-09-08 13:14:00 -070091 @Override
92 protected void onPostExecute(Void aVoid) {
Andres Moralesc17ec1b2015-06-01 16:23:41 -070093 mProgressDialog.hide();
Julia Reynolds8a388012015-08-19 16:19:55 -040094 if (getActivity() != null) {
95 getActivity().setRequestedOrientation(mOldOrientation);
96 doMasterClear();
97 }
Andres Moralese6bf2a52014-09-08 13:14:00 -070098 }
Andres Moralesc17ec1b2015-06-01 16:23:41 -070099
100 @Override
101 protected void onPreExecute() {
102 mProgressDialog = getProgressDialog();
103 mProgressDialog.show();
104
105 // need to prevent orientation changes as we're about to go into
106 // a long IO request, so we won't be able to access inflate resources on flash
107 mOldOrientation = getActivity().getRequestedOrientation();
108 getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
109 }
Andres Moralese6bf2a52014-09-08 13:14:00 -0700110 }.execute();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700111 } else {
Andres Moralesab61b0d2014-08-26 13:54:12 -0700112 doMasterClear();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700113 }
114 }
Andres Moralesab61b0d2014-08-26 13:54:12 -0700115
116 private ProgressDialog getProgressDialog() {
117 final ProgressDialog progressDialog = new ProgressDialog(getActivity());
118 progressDialog.setIndeterminate(true);
119 progressDialog.setCancelable(false);
120 progressDialog.setTitle(
121 getActivity().getString(R.string.master_clear_progress_title));
122 progressDialog.setMessage(
123 getActivity().getString(R.string.master_clear_progress_text));
124 return progressDialog;
125 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700126 };
127
Andres Moralesab61b0d2014-08-26 13:54:12 -0700128 private void doMasterClear() {
Lenka Trochtova3393dac2017-02-13 15:05:27 +0100129 Intent intent = new Intent(Intent.ACTION_FACTORY_RESET);
Christopher Tate1e99f0c2017-01-25 14:46:48 -0800130 intent.setPackage("android");
Rubin Xuccbdc572015-06-26 15:27:23 +0100131 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
132 intent.putExtra(Intent.EXTRA_REASON, "MasterClearConfirm");
133 intent.putExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, mEraseSdCard);
qingxi072f2862017-04-11 18:28:40 -0700134 intent.putExtra(Intent.EXTRA_WIPE_ESIMS, mEraseEsims);
Rubin Xuccbdc572015-06-26 15:27:23 +0100135 getActivity().sendBroadcast(intent);
136 // Intent handling is asynchronous -- assume it will happen soon.
Andres Moralesab61b0d2014-08-26 13:54:12 -0700137 }
138
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700139 /**
140 * Configure the UI for the final confirmation interaction
141 */
142 private void establishFinalConfirmationState() {
Andres Moralesab61b0d2014-08-26 13:54:12 -0700143 mContentView.findViewById(R.id.execute_master_clear)
144 .setOnClickListener(mFinalClickListener);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700145 }
146
147 @Override
148 public View onCreateView(LayoutInflater inflater, ViewGroup container,
149 Bundle savedInstanceState) {
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700150 final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000151 getActivity(), UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700152 if (RestrictedLockUtilsInternal.hasBaseUserRestriction(getActivity(),
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000153 UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId())) {
Julia Reynolds2c539332014-06-11 12:56:02 -0400154 return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000155 } else if (admin != null) {
arangelov93081542017-10-27 13:51:28 +0100156 new ActionDisabledByAdminDialogHelper(getActivity())
157 .prepareDialogBuilder(UserManager.DISALLOW_FACTORY_RESET, admin)
158 .setOnDismissListener(__ -> getActivity().finish())
159 .show();
160 return new View(getActivity());
Julia Reynolds2c539332014-06-11 12:56:02 -0400161 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700162 mContentView = inflater.inflate(R.layout.master_clear_confirm, null);
163 establishFinalConfirmationState();
Julia Reynoldsce25af42015-07-08 16:56:31 -0400164 setAccessibilityTitle();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700165 return mContentView;
166 }
167
Julia Reynoldsce25af42015-07-08 16:56:31 -0400168 private void setAccessibilityTitle() {
169 CharSequence currentTitle = getActivity().getTitle();
170 TextView confirmationMessage =
171 (TextView) mContentView.findViewById(R.id.master_clear_confirm);
172 if (confirmationMessage != null) {
arangelov93081542017-10-27 13:51:28 +0100173 String accessibleText = new StringBuilder(currentTitle).append(",").append(
Julia Reynoldsce25af42015-07-08 16:56:31 -0400174 confirmationMessage.getText()).toString();
arangelov93081542017-10-27 13:51:28 +0100175 getActivity().setTitle(Utils.createAccessibleSequence(currentTitle, accessibleText));
Julia Reynoldsce25af42015-07-08 16:56:31 -0400176 }
177 }
178
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700179 @Override
180 public void onCreate(Bundle savedInstanceState) {
181 super.onCreate(savedInstanceState);
182
183 Bundle args = getArguments();
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700184 mEraseSdCard = args != null
185 && args.getBoolean(MasterClear.ERASE_EXTERNAL_EXTRA);
qingxi072f2862017-04-11 18:28:40 -0700186 mEraseEsims = args != null
187 && args.getBoolean(MasterClear.ERASE_ESIMS_EXTRA);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700188 }
Chris Wren8a963ba2015-03-20 10:29:14 -0400189
190 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700191 public int getMetricsCategory() {
Chris Wren9d1bfd12016-01-26 18:04:01 -0500192 return MetricsEvent.MASTER_CLEAR_CONFIRM;
Chris Wren8a963ba2015-03-20 10:29:14 -0400193 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700194}