blob: 3ace43621b6d0dd3fe465a269c88e29fcf287b2d [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;
Alex Johnston74260a72020-03-19 17:34:46 +000025import android.app.admin.DevicePolicyManager;
26import android.app.admin.FactoryResetProtectionPolicy;
Fan Zhang31b21002019-01-16 13:49:47 -080027import android.app.settings.SettingsEnums;
Andres Morales7ab89292014-07-10 16:10:43 -070028import android.content.Context;
Jason Monk39b46742015-09-10 15:52:51 -040029import android.content.Intent;
Andres Moralesab61b0d2014-08-26 13:54:12 -070030import android.content.pm.ActivityInfo;
felkachanga6cec472018-03-29 12:08:20 +080031import android.graphics.Color;
Andres Moralesab61b0d2014-08-26 13:54:12 -070032import android.os.AsyncTask;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070033import android.os.Bundle;
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000034import android.os.UserHandle;
Julia Reynolds2c539332014-06-11 12:56:02 -040035import android.os.UserManager;
Andrew Scullc23357e2017-04-18 09:40:11 +010036import android.service.oemlock.OemLockManager;
Jason Monk39b46742015-09-10 15:52:51 -040037import android.service.persistentdata.PersistentDataBlockManager;
felkachanga6cec472018-03-29 12:08:20 +080038import android.util.Log;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070039import android.view.LayoutInflater;
40import android.view.View;
41import android.view.ViewGroup;
42import android.widget.Button;
Julia Reynoldsce25af42015-07-08 16:56:31 -040043import android.widget.TextView;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070044
Andrew Sapperstein88930e32019-03-07 14:18:15 -080045import androidx.annotation.VisibleForTesting;
46
Doris Ling72489722017-11-16 11:03:40 -080047import com.android.settings.core.InstrumentedFragment;
arangelov93081542017-10-27 13:51:28 +010048import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper;
Philip P. Moltmanne3f72112018-08-28 15:01:43 -070049import com.android.settingslib.RestrictedLockUtilsInternal;
Pasty Changc1f86002018-12-11 02:22:55 +000050
pastychang9bdb59a2019-01-21 09:49:15 +080051import com.google.android.setupcompat.template.FooterBarMixin;
52import com.google.android.setupcompat.template.FooterButton;
53import com.google.android.setupcompat.template.FooterButton.ButtonType;
Raff Tsaic8987752019-10-18 09:57:49 +080054import com.google.android.setupcompat.util.WizardManagerHelper;
Maurice Lam59f1c1a2019-02-14 22:04:30 +000055import com.google.android.setupdesign.GlifLayout;
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000056
Amith Yamasanib14e1e02010-11-02 09:52:29 -070057/**
58 * Confirm and execute a reset of the device 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 ERASE EVERYTHING
62 * ON THE PHONE" 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 */
Doris Ling72489722017-11-16 11:03:40 -080067public class MasterClearConfirm extends InstrumentedFragment {
felkachanga6cec472018-03-29 12:08:20 +080068 private final static String TAG = "MasterClearConfirm";
Amith Yamasanib14e1e02010-11-02 09:52:29 -070069
Andrew Sapperstein88930e32019-03-07 14:18:15 -080070 @VisibleForTesting View mContentView;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070071 private boolean mEraseSdCard;
Andrew Sapperstein88930e32019-03-07 14:18:15 -080072 @VisibleForTesting boolean mEraseEsims;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070073
74 /**
75 * The user has gone through the multiple confirmation, so now we go ahead
76 * and invoke the Checkin Service to reset the device to its factory-default
77 * state (rebooting in the process).
78 */
79 private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
80
81 public void onClick(View v) {
82 if (Utils.isMonkeyRunning()) {
83 return;
84 }
85
Andres Moralesab61b0d2014-08-26 13:54:12 -070086 final PersistentDataBlockManager pdbManager = (PersistentDataBlockManager)
Andres Morales7ab89292014-07-10 16:10:43 -070087 getActivity().getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
88
Alex Johnston74260a72020-03-19 17:34:46 +000089 if (shouldWipePersistentDataBlock(pdbManager)) {
90
Andres Moralese6bf2a52014-09-08 13:14:00 -070091 new AsyncTask<Void, Void, Void>() {
Andres Moralesc17ec1b2015-06-01 16:23:41 -070092 int mOldOrientation;
93 ProgressDialog mProgressDialog;
94
Andres Moralese6bf2a52014-09-08 13:14:00 -070095 @Override
96 protected Void doInBackground(Void... params) {
97 pdbManager.wipe();
98 return null;
99 }
Andres Moralesab61b0d2014-08-26 13:54:12 -0700100
Andres Moralese6bf2a52014-09-08 13:14:00 -0700101 @Override
102 protected void onPostExecute(Void aVoid) {
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700103 mProgressDialog.hide();
Julia Reynolds8a388012015-08-19 16:19:55 -0400104 if (getActivity() != null) {
105 getActivity().setRequestedOrientation(mOldOrientation);
106 doMasterClear();
107 }
Andres Moralese6bf2a52014-09-08 13:14:00 -0700108 }
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700109
110 @Override
111 protected void onPreExecute() {
112 mProgressDialog = getProgressDialog();
113 mProgressDialog.show();
114
115 // need to prevent orientation changes as we're about to go into
felkachanga6cec472018-03-29 12:08:20 +0800116 // a long IO request, so we won't be able to access inflate resources on
117 // flash
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700118 mOldOrientation = getActivity().getRequestedOrientation();
felkachanga6cec472018-03-29 12:08:20 +0800119 getActivity().setRequestedOrientation(
120 ActivityInfo.SCREEN_ORIENTATION_LOCKED);
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700121 }
Andres Moralese6bf2a52014-09-08 13:14:00 -0700122 }.execute();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700123 } else {
Andres Moralesab61b0d2014-08-26 13:54:12 -0700124 doMasterClear();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700125 }
126 }
Andres Moralesab61b0d2014-08-26 13:54:12 -0700127
128 private ProgressDialog getProgressDialog() {
129 final ProgressDialog progressDialog = new ProgressDialog(getActivity());
130 progressDialog.setIndeterminate(true);
131 progressDialog.setCancelable(false);
132 progressDialog.setTitle(
133 getActivity().getString(R.string.master_clear_progress_title));
134 progressDialog.setMessage(
135 getActivity().getString(R.string.master_clear_progress_text));
136 return progressDialog;
137 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700138 };
139
Alex Johnston74260a72020-03-19 17:34:46 +0000140 @VisibleForTesting
141 boolean shouldWipePersistentDataBlock(PersistentDataBlockManager pdbManager) {
142 if (pdbManager == null) {
143 return false;
144 }
145 // The persistent data block will persist if the device is still being provisioned.
146 if (isDeviceStillBeingProvisioned()) {
147 return false;
148 }
149 // If OEM unlock is allowed, the persistent data block will be wiped during FR
150 // process. If disabled, it will be wiped here instead.
151 if (isOemUnlockedAllowed()) {
152 return false;
153 }
Alex Johnston05c0ab02020-04-28 13:47:14 +0100154 final DevicePolicyManager dpm = (DevicePolicyManager) getActivity()
155 .getSystemService(Context.DEVICE_POLICY_SERVICE);
156 // Do not erase the factory reset protection data (from Settings) if factory reset
157 // protection policy is not supported on the device.
158 if (!dpm.isFactoryResetProtectionPolicySupported()) {
159 return false;
160 }
Alex Johnston74260a72020-03-19 17:34:46 +0000161 // Do not erase the factory reset protection data (from Settings) if the
162 // device is an organization-owned managed profile device and a factory
163 // reset protection policy has been set.
Alex Johnston74260a72020-03-19 17:34:46 +0000164 FactoryResetProtectionPolicy frpPolicy = dpm.getFactoryResetProtectionPolicy(null);
165 if (dpm.isOrganizationOwnedDeviceWithManagedProfile() && frpPolicy != null
166 && frpPolicy.isNotEmpty()) {
167 return false;
168 }
169 return true;
170 }
171
172 @VisibleForTesting
173 boolean isOemUnlockedAllowed() {
174 return ((OemLockManager) getActivity().getSystemService(
175 Context.OEM_LOCK_SERVICE)).isOemUnlockAllowed();
176 }
177
178 @VisibleForTesting
179 boolean isDeviceStillBeingProvisioned() {
180 return !WizardManagerHelper.isDeviceProvisioned(getActivity());
181 }
182
Andres Moralesab61b0d2014-08-26 13:54:12 -0700183 private void doMasterClear() {
Lenka Trochtova3393dac2017-02-13 15:05:27 +0100184 Intent intent = new Intent(Intent.ACTION_FACTORY_RESET);
Christopher Tate1e99f0c2017-01-25 14:46:48 -0800185 intent.setPackage("android");
Rubin Xuccbdc572015-06-26 15:27:23 +0100186 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
187 intent.putExtra(Intent.EXTRA_REASON, "MasterClearConfirm");
188 intent.putExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, mEraseSdCard);
qingxi072f2862017-04-11 18:28:40 -0700189 intent.putExtra(Intent.EXTRA_WIPE_ESIMS, mEraseEsims);
Rubin Xuccbdc572015-06-26 15:27:23 +0100190 getActivity().sendBroadcast(intent);
191 // Intent handling is asynchronous -- assume it will happen soon.
Andres Moralesab61b0d2014-08-26 13:54:12 -0700192 }
193
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700194 /**
195 * Configure the UI for the final confirmation interaction
196 */
197 private void establishFinalConfirmationState() {
Maurice Lam59f1c1a2019-02-14 22:04:30 +0000198 final GlifLayout layout = mContentView.findViewById(R.id.setup_wizard_layout);
felkachanga6cec472018-03-29 12:08:20 +0800199
pastychang9bdb59a2019-01-21 09:49:15 +0800200 final FooterBarMixin mixin = layout.getMixin(FooterBarMixin.class);
201 mixin.setPrimaryButton(
pastychang908501e2018-12-26 10:46:40 +0800202 new FooterButton.Builder(getActivity())
203 .setText(R.string.master_clear_button_text)
204 .setListener(mFinalClickListener)
205 .setButtonType(ButtonType.OTHER)
pastychang79614822019-01-03 10:12:54 +0800206 .setTheme(R.style.SudGlifButton_Primary)
pastychang908501e2018-12-26 10:46:40 +0800207 .build()
pastychang103409b2018-12-13 23:00:17 +0800208 );
felkachanga6cec472018-03-29 12:08:20 +0800209 }
210
211 private void setUpActionBarAndTitle() {
212 final Activity activity = getActivity();
213 if (activity == null) {
214 Log.e(TAG, "No activity attached, skipping setUpActionBarAndTitle");
215 return;
216 }
217 final ActionBar actionBar = activity.getActionBar();
218 if (actionBar == null) {
219 Log.e(TAG, "No actionbar, skipping setUpActionBarAndTitle");
220 return;
221 }
222 actionBar.hide();
223 activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700224 }
225
226 @Override
227 public View onCreateView(LayoutInflater inflater, ViewGroup container,
228 Bundle savedInstanceState) {
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700229 final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000230 getActivity(), UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700231 if (RestrictedLockUtilsInternal.hasBaseUserRestriction(getActivity(),
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000232 UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId())) {
Julia Reynolds2c539332014-06-11 12:56:02 -0400233 return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000234 } else if (admin != null) {
arangelov93081542017-10-27 13:51:28 +0100235 new ActionDisabledByAdminDialogHelper(getActivity())
236 .prepareDialogBuilder(UserManager.DISALLOW_FACTORY_RESET, admin)
237 .setOnDismissListener(__ -> getActivity().finish())
238 .show();
239 return new View(getActivity());
Julia Reynolds2c539332014-06-11 12:56:02 -0400240 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700241 mContentView = inflater.inflate(R.layout.master_clear_confirm, null);
felkachanga6cec472018-03-29 12:08:20 +0800242 setUpActionBarAndTitle();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700243 establishFinalConfirmationState();
Julia Reynoldsce25af42015-07-08 16:56:31 -0400244 setAccessibilityTitle();
Andrew Sapperstein88930e32019-03-07 14:18:15 -0800245 setSubtitle();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700246 return mContentView;
247 }
248
Julia Reynoldsce25af42015-07-08 16:56:31 -0400249 private void setAccessibilityTitle() {
250 CharSequence currentTitle = getActivity().getTitle();
pastychangfa68ec42019-04-12 16:22:17 +0800251 TextView confirmationMessage = mContentView.findViewById(R.id.sud_layout_description);
Julia Reynoldsce25af42015-07-08 16:56:31 -0400252 if (confirmationMessage != null) {
arangelov93081542017-10-27 13:51:28 +0100253 String accessibleText = new StringBuilder(currentTitle).append(",").append(
Julia Reynoldsce25af42015-07-08 16:56:31 -0400254 confirmationMessage.getText()).toString();
arangelov93081542017-10-27 13:51:28 +0100255 getActivity().setTitle(Utils.createAccessibleSequence(currentTitle, accessibleText));
Julia Reynoldsce25af42015-07-08 16:56:31 -0400256 }
257 }
258
Andrew Sapperstein88930e32019-03-07 14:18:15 -0800259 @VisibleForTesting
260 void setSubtitle() {
261 if (mEraseEsims) {
pastychangfa68ec42019-04-12 16:22:17 +0800262 ((TextView) mContentView.findViewById(R.id.sud_layout_description))
Andrew Sapperstein88930e32019-03-07 14:18:15 -0800263 .setText(R.string.master_clear_final_desc_esim);
264 }
265 }
266
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700267 @Override
268 public void onCreate(Bundle savedInstanceState) {
269 super.onCreate(savedInstanceState);
270
271 Bundle args = getArguments();
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700272 mEraseSdCard = args != null
273 && args.getBoolean(MasterClear.ERASE_EXTERNAL_EXTRA);
qingxi072f2862017-04-11 18:28:40 -0700274 mEraseEsims = args != null
275 && args.getBoolean(MasterClear.ERASE_ESIMS_EXTRA);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700276 }
Chris Wren8a963ba2015-03-20 10:29:14 -0400277
278 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700279 public int getMetricsCategory() {
Fan Zhang31b21002019-01-16 13:49:47 -0800280 return SettingsEnums.MASTER_CLEAR_CONFIRM;
Chris Wren8a963ba2015-03-20 10:29:14 -0400281 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700282}