blob: 9b324c3fddfece50daec6c1de41227a063afb2b1 [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
Andres Moralesab61b0d2014-08-26 13:54:12 -070019import android.app.ProgressDialog;
Andres Morales7ab89292014-07-10 16:10:43 -070020import android.content.Context;
Jason Monk39b46742015-09-10 15:52:51 -040021import android.content.Intent;
Andres Moralesab61b0d2014-08-26 13:54:12 -070022import android.content.pm.ActivityInfo;
23import android.os.AsyncTask;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070024import android.os.Bundle;
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000025import android.os.UserHandle;
Julia Reynolds2c539332014-06-11 12:56:02 -040026import android.os.UserManager;
Andrew Scullc23357e2017-04-18 09:40:11 +010027import android.service.oemlock.OemLockManager;
Jason Monk39b46742015-09-10 15:52:51 -040028import android.service.persistentdata.PersistentDataBlockManager;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070029import android.view.LayoutInflater;
30import android.view.View;
31import android.view.ViewGroup;
32import android.widget.Button;
Julia Reynoldsce25af42015-07-08 16:56:31 -040033import android.widget.TextView;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070034
Tamas Berghammer265d3c22016-06-22 15:34:45 +010035import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Doris Linged4685f2017-10-25 14:08:57 -070036import com.android.settings.core.InstrumentedPreferenceFragment;
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000037import com.android.settingslib.RestrictedLockUtils;
38
39import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
Jason Monk39b46742015-09-10 15:52:51 -040040
Amith Yamasanib14e1e02010-11-02 09:52:29 -070041/**
42 * Confirm and execute a reset of the device to a clean "just out of the box"
43 * state. Multiple confirmations are required: first, a general "are you sure
44 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
45 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
46 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
47 * locked, et cetera, then the confirmation sequence is abandoned.
48 *
49 * This is the confirmation screen.
50 */
Doris Linged4685f2017-10-25 14:08:57 -070051public class MasterClearConfirm extends InstrumentedPreferenceFragment {
Amith Yamasanib14e1e02010-11-02 09:52:29 -070052
53 private View mContentView;
54 private boolean mEraseSdCard;
qingxi072f2862017-04-11 18:28:40 -070055 private boolean mEraseEsims;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070056
57 /**
58 * The user has gone through the multiple confirmation, so now we go ahead
59 * and invoke the Checkin Service to reset the device to its factory-default
60 * state (rebooting in the process).
61 */
62 private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
63
64 public void onClick(View v) {
65 if (Utils.isMonkeyRunning()) {
66 return;
67 }
68
Andres Moralesab61b0d2014-08-26 13:54:12 -070069 final PersistentDataBlockManager pdbManager = (PersistentDataBlockManager)
Andres Morales7ab89292014-07-10 16:10:43 -070070 getActivity().getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
Andrew Scullc23357e2017-04-18 09:40:11 +010071 final OemLockManager oemLockManager = (OemLockManager)
72 getActivity().getSystemService(Context.OEM_LOCK_SERVICE);
Andres Morales7ab89292014-07-10 16:10:43 -070073
Andrew Scullc23357e2017-04-18 09:40:11 +010074 if (pdbManager != null && !oemLockManager.isOemUnlockAllowed() &&
Udam Saini679f7ad2016-03-25 10:47:00 -070075 Utils.isDeviceProvisioned(getActivity())) {
Andrew Scullc23357e2017-04-18 09:40:11 +010076 // if OEM unlock is allowed, the persistent data block will be wiped during FR
77 // process. If disabled, it will be wiped here, unless the device is still being
78 // provisioned, in which case the persistent data block will be preserved.
Andres Moralese6bf2a52014-09-08 13:14:00 -070079 new AsyncTask<Void, Void, Void>() {
Andres Moralesc17ec1b2015-06-01 16:23:41 -070080 int mOldOrientation;
81 ProgressDialog mProgressDialog;
82
Andres Moralese6bf2a52014-09-08 13:14:00 -070083 @Override
84 protected Void doInBackground(Void... params) {
85 pdbManager.wipe();
86 return null;
87 }
Andres Moralesab61b0d2014-08-26 13:54:12 -070088
Andres Moralese6bf2a52014-09-08 13:14:00 -070089 @Override
90 protected void onPostExecute(Void aVoid) {
Andres Moralesc17ec1b2015-06-01 16:23:41 -070091 mProgressDialog.hide();
Julia Reynolds8a388012015-08-19 16:19:55 -040092 if (getActivity() != null) {
93 getActivity().setRequestedOrientation(mOldOrientation);
94 doMasterClear();
95 }
Andres Moralese6bf2a52014-09-08 13:14:00 -070096 }
Andres Moralesc17ec1b2015-06-01 16:23:41 -070097
98 @Override
99 protected void onPreExecute() {
100 mProgressDialog = getProgressDialog();
101 mProgressDialog.show();
102
103 // need to prevent orientation changes as we're about to go into
104 // a long IO request, so we won't be able to access inflate resources on flash
105 mOldOrientation = getActivity().getRequestedOrientation();
106 getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
107 }
Andres Moralese6bf2a52014-09-08 13:14:00 -0700108 }.execute();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700109 } else {
Andres Moralesab61b0d2014-08-26 13:54:12 -0700110 doMasterClear();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700111 }
112 }
Andres Moralesab61b0d2014-08-26 13:54:12 -0700113
114 private ProgressDialog getProgressDialog() {
115 final ProgressDialog progressDialog = new ProgressDialog(getActivity());
116 progressDialog.setIndeterminate(true);
117 progressDialog.setCancelable(false);
118 progressDialog.setTitle(
119 getActivity().getString(R.string.master_clear_progress_title));
120 progressDialog.setMessage(
121 getActivity().getString(R.string.master_clear_progress_text));
122 return progressDialog;
123 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700124 };
125
Andres Moralesab61b0d2014-08-26 13:54:12 -0700126 private void doMasterClear() {
Lenka Trochtova3393dac2017-02-13 15:05:27 +0100127 Intent intent = new Intent(Intent.ACTION_FACTORY_RESET);
Christopher Tate1e99f0c2017-01-25 14:46:48 -0800128 intent.setPackage("android");
Rubin Xuccbdc572015-06-26 15:27:23 +0100129 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
130 intent.putExtra(Intent.EXTRA_REASON, "MasterClearConfirm");
131 intent.putExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, mEraseSdCard);
qingxi072f2862017-04-11 18:28:40 -0700132 intent.putExtra(Intent.EXTRA_WIPE_ESIMS, mEraseEsims);
Rubin Xuccbdc572015-06-26 15:27:23 +0100133 getActivity().sendBroadcast(intent);
134 // Intent handling is asynchronous -- assume it will happen soon.
Andres Moralesab61b0d2014-08-26 13:54:12 -0700135 }
136
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700137 /**
138 * Configure the UI for the final confirmation interaction
139 */
140 private void establishFinalConfirmationState() {
Andres Moralesab61b0d2014-08-26 13:54:12 -0700141 mContentView.findViewById(R.id.execute_master_clear)
142 .setOnClickListener(mFinalClickListener);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700143 }
144
145 @Override
146 public View onCreateView(LayoutInflater inflater, ViewGroup container,
147 Bundle savedInstanceState) {
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000148 final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(
149 getActivity(), UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
150 if (RestrictedLockUtils.hasBaseUserRestriction(getActivity(),
151 UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId())) {
Julia Reynolds2c539332014-06-11 12:56:02 -0400152 return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000153 } else if (admin != null) {
154 View view = inflater.inflate(R.layout.admin_support_details_empty_view, null);
155 ShowAdminSupportDetailsDialog.setAdminSupportDetails(getActivity(), view, admin, false);
156 view.setVisibility(View.VISIBLE);
157 return view;
Julia Reynolds2c539332014-06-11 12:56:02 -0400158 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700159 mContentView = inflater.inflate(R.layout.master_clear_confirm, null);
160 establishFinalConfirmationState();
Julia Reynoldsce25af42015-07-08 16:56:31 -0400161 setAccessibilityTitle();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700162 return mContentView;
163 }
164
Julia Reynoldsce25af42015-07-08 16:56:31 -0400165 private void setAccessibilityTitle() {
166 CharSequence currentTitle = getActivity().getTitle();
167 TextView confirmationMessage =
168 (TextView) mContentView.findViewById(R.id.master_clear_confirm);
169 if (confirmationMessage != null) {
170 String accessibileText = new StringBuilder(currentTitle).append(",").append(
171 confirmationMessage.getText()).toString();
172 getActivity().setTitle(Utils.createAccessibleSequence(currentTitle, accessibileText));
173 }
174 }
175
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700176 @Override
177 public void onCreate(Bundle savedInstanceState) {
178 super.onCreate(savedInstanceState);
179
180 Bundle args = getArguments();
Andres Moralesc17ec1b2015-06-01 16:23:41 -0700181 mEraseSdCard = args != null
182 && args.getBoolean(MasterClear.ERASE_EXTERNAL_EXTRA);
qingxi072f2862017-04-11 18:28:40 -0700183 mEraseEsims = args != null
184 && args.getBoolean(MasterClear.ERASE_ESIMS_EXTRA);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700185 }
Chris Wren8a963ba2015-03-20 10:29:14 -0400186
187 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700188 public int getMetricsCategory() {
Chris Wren9d1bfd12016-01-26 18:04:01 -0500189 return MetricsEvent.MASTER_CLEAR_CONFIRM;
Chris Wren8a963ba2015-03-20 10:29:14 -0400190 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700191}