blob: 9208c4fbf6a602884983f5338a2a7692b0fe9d96 [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;
Edgar Wangf8739ed2022-04-11 21:50:01 +080034import android.os.SystemProperties;
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000035import android.os.UserHandle;
Julia Reynolds2c539332014-06-11 12:56:02 -040036import android.os.UserManager;
Andrew Scullc23357e2017-04-18 09:40:11 +010037import android.service.oemlock.OemLockManager;
Jason Monk39b46742015-09-10 15:52:51 -040038import android.service.persistentdata.PersistentDataBlockManager;
felkachanga6cec472018-03-29 12:08:20 +080039import android.util.Log;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070040import android.view.LayoutInflater;
41import android.view.View;
42import android.view.ViewGroup;
43import android.widget.Button;
Julia Reynoldsce25af42015-07-08 16:56:31 -040044import android.widget.TextView;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070045
Andrew Sapperstein88930e32019-03-07 14:18:15 -080046import androidx.annotation.VisibleForTesting;
47
Doris Ling72489722017-11-16 11:03:40 -080048import com.android.settings.core.InstrumentedFragment;
arangelov93081542017-10-27 13:51:28 +010049import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper;
Philip P. Moltmanne3f72112018-08-28 15:01:43 -070050import com.android.settingslib.RestrictedLockUtilsInternal;
Pasty Changc1f86002018-12-11 02:22:55 +000051
pastychang9bdb59a2019-01-21 09:49:15 +080052import com.google.android.setupcompat.template.FooterBarMixin;
53import com.google.android.setupcompat.template.FooterButton;
54import com.google.android.setupcompat.template.FooterButton.ButtonType;
Raff Tsaic8987752019-10-18 09:57:49 +080055import com.google.android.setupcompat.util.WizardManagerHelper;
Maurice Lam59f1c1a2019-02-14 22:04:30 +000056import com.google.android.setupdesign.GlifLayout;
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000057
Amith Yamasanib14e1e02010-11-02 09:52:29 -070058/**
59 * Confirm and execute a reset of the device to a clean "just out of the box"
60 * state. Multiple confirmations are required: first, a general "are you sure
61 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
62 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
63 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
64 * locked, et cetera, then the confirmation sequence is abandoned.
65 *
66 * This is the confirmation screen.
67 */
Edgar Wang21f8cb32020-08-04 23:13:05 +080068public class MainClearConfirm extends InstrumentedFragment {
69 private static final String TAG = "MainClearConfirm";
Amith Yamasanib14e1e02010-11-02 09:52:29 -070070
Edgar Wangf8739ed2022-04-11 21:50:01 +080071 private static final String PERSISTENT_DATA_BLOCK_PROP = "ro.frp.pst";
72
Andrew Sapperstein88930e32019-03-07 14:18:15 -080073 @VisibleForTesting View mContentView;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070074 private boolean mEraseSdCard;
Andrew Sapperstein88930e32019-03-07 14:18:15 -080075 @VisibleForTesting boolean mEraseEsims;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070076
77 /**
78 * The user has gone through the multiple confirmation, so now we go ahead
79 * and invoke the Checkin Service to reset the device to its factory-default
80 * state (rebooting in the process).
81 */
82 private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
83
84 public void onClick(View v) {
85 if (Utils.isMonkeyRunning()) {
86 return;
87 }
88
Edgar Wangf8739ed2022-04-11 21:50:01 +080089 // pre-flight check hardware support PersistentDataBlockManager
90 if (SystemProperties.get(PERSISTENT_DATA_BLOCK_PROP).equals("")) {
91 return;
92 }
93
Andres Moralesab61b0d2014-08-26 13:54:12 -070094 final PersistentDataBlockManager pdbManager = (PersistentDataBlockManager)
Andres Morales7ab89292014-07-10 16:10:43 -070095 getActivity().getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
96
Alex Johnston74260a72020-03-19 17:34:46 +000097 if (shouldWipePersistentDataBlock(pdbManager)) {
98
Andres Moralese6bf2a52014-09-08 13:14:00 -070099 new AsyncTask<Void, Void, Void>() {
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700100 int mOldOrientation;
101 ProgressDialog mProgressDialog;
102
Andres Moralese6bf2a52014-09-08 13:14:00 -0700103 @Override
104 protected Void doInBackground(Void... params) {
105 pdbManager.wipe();
106 return null;
107 }
Andres Moralesab61b0d2014-08-26 13:54:12 -0700108
Andres Moralese6bf2a52014-09-08 13:14:00 -0700109 @Override
110 protected void onPostExecute(Void aVoid) {
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700111 mProgressDialog.hide();
Julia Reynolds8a388012015-08-19 16:19:55 -0400112 if (getActivity() != null) {
113 getActivity().setRequestedOrientation(mOldOrientation);
Edgar Wang21f8cb32020-08-04 23:13:05 +0800114 doMainClear();
Julia Reynolds8a388012015-08-19 16:19:55 -0400115 }
Andres Moralese6bf2a52014-09-08 13:14:00 -0700116 }
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700117
118 @Override
119 protected void onPreExecute() {
120 mProgressDialog = getProgressDialog();
121 mProgressDialog.show();
122
123 // need to prevent orientation changes as we're about to go into
felkachanga6cec472018-03-29 12:08:20 +0800124 // a long IO request, so we won't be able to access inflate resources on
125 // flash
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700126 mOldOrientation = getActivity().getRequestedOrientation();
felkachanga6cec472018-03-29 12:08:20 +0800127 getActivity().setRequestedOrientation(
128 ActivityInfo.SCREEN_ORIENTATION_LOCKED);
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700129 }
Andres Moralese6bf2a52014-09-08 13:14:00 -0700130 }.execute();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700131 } else {
Edgar Wang21f8cb32020-08-04 23:13:05 +0800132 doMainClear();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700133 }
134 }
Andres Moralesab61b0d2014-08-26 13:54:12 -0700135
136 private ProgressDialog getProgressDialog() {
137 final ProgressDialog progressDialog = new ProgressDialog(getActivity());
138 progressDialog.setIndeterminate(true);
139 progressDialog.setCancelable(false);
140 progressDialog.setTitle(
Edgar Wang21f8cb32020-08-04 23:13:05 +0800141 getActivity().getString(R.string.main_clear_progress_title));
Andres Moralesab61b0d2014-08-26 13:54:12 -0700142 progressDialog.setMessage(
Edgar Wang21f8cb32020-08-04 23:13:05 +0800143 getActivity().getString(R.string.main_clear_progress_text));
Andres Moralesab61b0d2014-08-26 13:54:12 -0700144 return progressDialog;
145 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700146 };
147
Alex Johnston74260a72020-03-19 17:34:46 +0000148 @VisibleForTesting
149 boolean shouldWipePersistentDataBlock(PersistentDataBlockManager pdbManager) {
150 if (pdbManager == null) {
151 return false;
152 }
153 // The persistent data block will persist if the device is still being provisioned.
154 if (isDeviceStillBeingProvisioned()) {
155 return false;
156 }
157 // If OEM unlock is allowed, the persistent data block will be wiped during FR
158 // process. If disabled, it will be wiped here instead.
159 if (isOemUnlockedAllowed()) {
160 return false;
161 }
Alex Johnston05c0ab02020-04-28 13:47:14 +0100162 final DevicePolicyManager dpm = (DevicePolicyManager) getActivity()
163 .getSystemService(Context.DEVICE_POLICY_SERVICE);
164 // Do not erase the factory reset protection data (from Settings) if factory reset
165 // protection policy is not supported on the device.
166 if (!dpm.isFactoryResetProtectionPolicySupported()) {
167 return false;
168 }
Alex Johnston74260a72020-03-19 17:34:46 +0000169 // Do not erase the factory reset protection data (from Settings) if the
170 // device is an organization-owned managed profile device and a factory
171 // reset protection policy has been set.
Alex Johnston74260a72020-03-19 17:34:46 +0000172 FactoryResetProtectionPolicy frpPolicy = dpm.getFactoryResetProtectionPolicy(null);
173 if (dpm.isOrganizationOwnedDeviceWithManagedProfile() && frpPolicy != null
174 && frpPolicy.isNotEmpty()) {
175 return false;
176 }
177 return true;
178 }
179
180 @VisibleForTesting
181 boolean isOemUnlockedAllowed() {
182 return ((OemLockManager) getActivity().getSystemService(
183 Context.OEM_LOCK_SERVICE)).isOemUnlockAllowed();
184 }
185
186 @VisibleForTesting
187 boolean isDeviceStillBeingProvisioned() {
188 return !WizardManagerHelper.isDeviceProvisioned(getActivity());
189 }
190
Edgar Wang21f8cb32020-08-04 23:13:05 +0800191 private void doMainClear() {
Lenka Trochtova3393dac2017-02-13 15:05:27 +0100192 Intent intent = new Intent(Intent.ACTION_FACTORY_RESET);
Christopher Tate1e99f0c2017-01-25 14:46:48 -0800193 intent.setPackage("android");
Rubin Xuccbdc572015-06-26 15:27:23 +0100194 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Edgar Wang21f8cb32020-08-04 23:13:05 +0800195 intent.putExtra(Intent.EXTRA_REASON, "MainClearConfirm");
Rubin Xuccbdc572015-06-26 15:27:23 +0100196 intent.putExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, mEraseSdCard);
qingxi072f2862017-04-11 18:28:40 -0700197 intent.putExtra(Intent.EXTRA_WIPE_ESIMS, mEraseEsims);
Rubin Xuccbdc572015-06-26 15:27:23 +0100198 getActivity().sendBroadcast(intent);
199 // Intent handling is asynchronous -- assume it will happen soon.
Andres Moralesab61b0d2014-08-26 13:54:12 -0700200 }
201
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700202 /**
203 * Configure the UI for the final confirmation interaction
204 */
205 private void establishFinalConfirmationState() {
Maurice Lam59f1c1a2019-02-14 22:04:30 +0000206 final GlifLayout layout = mContentView.findViewById(R.id.setup_wizard_layout);
felkachanga6cec472018-03-29 12:08:20 +0800207
pastychang9bdb59a2019-01-21 09:49:15 +0800208 final FooterBarMixin mixin = layout.getMixin(FooterBarMixin.class);
209 mixin.setPrimaryButton(
pastychang908501e2018-12-26 10:46:40 +0800210 new FooterButton.Builder(getActivity())
Edgar Wang21f8cb32020-08-04 23:13:05 +0800211 .setText(R.string.main_clear_button_text)
pastychang908501e2018-12-26 10:46:40 +0800212 .setListener(mFinalClickListener)
213 .setButtonType(ButtonType.OTHER)
pastychang79614822019-01-03 10:12:54 +0800214 .setTheme(R.style.SudGlifButton_Primary)
pastychang908501e2018-12-26 10:46:40 +0800215 .build()
pastychang103409b2018-12-13 23:00:17 +0800216 );
felkachanga6cec472018-03-29 12:08:20 +0800217 }
218
219 private void setUpActionBarAndTitle() {
220 final Activity activity = getActivity();
221 if (activity == null) {
222 Log.e(TAG, "No activity attached, skipping setUpActionBarAndTitle");
223 return;
224 }
225 final ActionBar actionBar = activity.getActionBar();
226 if (actionBar == null) {
227 Log.e(TAG, "No actionbar, skipping setUpActionBarAndTitle");
228 return;
229 }
230 actionBar.hide();
231 activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700232 }
233
234 @Override
235 public View onCreateView(LayoutInflater inflater, ViewGroup container,
236 Bundle savedInstanceState) {
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700237 final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000238 getActivity(), UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700239 if (RestrictedLockUtilsInternal.hasBaseUserRestriction(getActivity(),
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000240 UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId())) {
Edgar Wang21f8cb32020-08-04 23:13:05 +0800241 return inflater.inflate(R.layout.main_clear_disallowed_screen, null);
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000242 } else if (admin != null) {
arangelov93081542017-10-27 13:51:28 +0100243 new ActionDisabledByAdminDialogHelper(getActivity())
244 .prepareDialogBuilder(UserManager.DISALLOW_FACTORY_RESET, admin)
245 .setOnDismissListener(__ -> getActivity().finish())
246 .show();
247 return new View(getActivity());
Julia Reynolds2c539332014-06-11 12:56:02 -0400248 }
Edgar Wang21f8cb32020-08-04 23:13:05 +0800249 mContentView = inflater.inflate(R.layout.main_clear_confirm, null);
felkachanga6cec472018-03-29 12:08:20 +0800250 setUpActionBarAndTitle();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700251 establishFinalConfirmationState();
Julia Reynoldsce25af42015-07-08 16:56:31 -0400252 setAccessibilityTitle();
Andrew Sapperstein88930e32019-03-07 14:18:15 -0800253 setSubtitle();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700254 return mContentView;
255 }
256
Julia Reynoldsce25af42015-07-08 16:56:31 -0400257 private void setAccessibilityTitle() {
258 CharSequence currentTitle = getActivity().getTitle();
pastychangfa68ec42019-04-12 16:22:17 +0800259 TextView confirmationMessage = mContentView.findViewById(R.id.sud_layout_description);
Julia Reynoldsce25af42015-07-08 16:56:31 -0400260 if (confirmationMessage != null) {
arangelov93081542017-10-27 13:51:28 +0100261 String accessibleText = new StringBuilder(currentTitle).append(",").append(
Julia Reynoldsce25af42015-07-08 16:56:31 -0400262 confirmationMessage.getText()).toString();
arangelov93081542017-10-27 13:51:28 +0100263 getActivity().setTitle(Utils.createAccessibleSequence(currentTitle, accessibleText));
Julia Reynoldsce25af42015-07-08 16:56:31 -0400264 }
265 }
266
Andrew Sapperstein88930e32019-03-07 14:18:15 -0800267 @VisibleForTesting
268 void setSubtitle() {
269 if (mEraseEsims) {
pastychangfa68ec42019-04-12 16:22:17 +0800270 ((TextView) mContentView.findViewById(R.id.sud_layout_description))
Edgar Wang21f8cb32020-08-04 23:13:05 +0800271 .setText(R.string.main_clear_final_desc_esim);
Andrew Sapperstein88930e32019-03-07 14:18:15 -0800272 }
273 }
274
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700275 @Override
276 public void onCreate(Bundle savedInstanceState) {
277 super.onCreate(savedInstanceState);
278
279 Bundle args = getArguments();
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700280 mEraseSdCard = args != null
Edgar Wang21f8cb32020-08-04 23:13:05 +0800281 && args.getBoolean(MainClear.ERASE_EXTERNAL_EXTRA);
qingxi072f2862017-04-11 18:28:40 -0700282 mEraseEsims = args != null
Edgar Wang21f8cb32020-08-04 23:13:05 +0800283 && args.getBoolean(MainClear.ERASE_ESIMS_EXTRA);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700284 }
Chris Wren8a963ba2015-03-20 10:29:14 -0400285
286 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700287 public int getMetricsCategory() {
Fan Zhang31b21002019-01-16 13:49:47 -0800288 return SettingsEnums.MASTER_CLEAR_CONFIRM;
Chris Wren8a963ba2015-03-20 10:29:14 -0400289 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700290}