blob: 06c46b908b7a8d3777f45923ef0059804709958e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 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.server;
18
qingxi0ca328f2017-05-19 15:20:03 -070019import android.app.PendingIntent;
Rubin Xue8490f12015-06-25 12:17:48 +010020import android.app.ProgressDialog;
Dan Egnor18e93962010-02-10 19:27:58 -080021import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.Context;
23import android.content.Intent;
Rubin Xue8490f12015-06-25 12:17:48 +010024import android.os.AsyncTask;
Dan Egnor18e93962010-02-10 19:27:58 -080025import android.os.RecoverySystem;
Rubin Xue8490f12015-06-25 12:17:48 +010026import android.os.storage.StorageManager;
qingxi0ca328f2017-05-19 15:20:03 -070027import android.provider.Settings;
qingxi240c2bb2017-04-12 17:31:18 -070028import android.telephony.euicc.EuiccManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080030import android.util.Slog;
Rubin Xue8490f12015-06-25 12:17:48 +010031import android.view.WindowManager;
32
33import com.android.internal.R;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
Dan Egnor18e93962010-02-10 19:27:58 -080035import java.io.IOException;
qingxi0ca328f2017-05-19 15:20:03 -070036import java.util.concurrent.CountDownLatch;
37import java.util.concurrent.TimeUnit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
Dan Egnor18e93962010-02-10 19:27:58 -080039public class MasterClearReceiver extends BroadcastReceiver {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 private static final String TAG = "MasterClear";
qingxi240c2bb2017-04-12 17:31:18 -070041 private boolean mWipeExternalStorage;
qingxi0ca328f2017-05-19 15:20:03 -070042 private boolean mWipeEsims;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
44 @Override
Dianne Hackborn42499172010-10-15 18:45:07 -070045 public void onReceive(final Context context, final Intent intent) {
Wei Huang97ecc9c2009-05-11 17:44:20 -070046 if (intent.getAction().equals(Intent.ACTION_REMOTE_INTENT)) {
Costin Manolache63cfebf2010-02-04 16:52:34 -080047 if (!"google.com".equals(intent.getStringExtra("from"))) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080048 Slog.w(TAG, "Ignoring master clear request -- not from trusted server.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 return;
50 }
51 }
Lenka Trochtova73aeea22016-11-18 17:34:34 +010052 if (Intent.ACTION_MASTER_CLEAR.equals(intent.getAction())) {
53 Slog.w(TAG, "The request uses the deprecated Intent#ACTION_MASTER_CLEAR, "
54 + "Intent#ACTION_FACTORY_RESET should be used instead.");
55 }
56 if (intent.hasExtra(Intent.EXTRA_FORCE_MASTER_CLEAR)) {
57 Slog.w(TAG, "The request uses the deprecated Intent#EXTRA_FORCE_MASTER_CLEAR, "
58 + "Intent#EXTRA_FORCE_FACTORY_RESET should be used instead.");
59 }
Dan Egnor18e93962010-02-10 19:27:58 -080060
Doug Zongkercdf00882014-03-18 12:52:04 -070061 final boolean shutdown = intent.getBooleanExtra("shutdown", false);
Jeff Sharkey004a4b22014-09-24 11:45:24 -070062 final String reason = intent.getStringExtra(Intent.EXTRA_REASON);
qingxi240c2bb2017-04-12 17:31:18 -070063 mWipeExternalStorage = intent.getBooleanExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, false);
qingxi0ca328f2017-05-19 15:20:03 -070064 mWipeEsims = intent.getBooleanExtra(Intent.EXTRA_WIPE_ESIMS, false);
Lenka Trochtova73aeea22016-11-18 17:34:34 +010065 final boolean forceWipe = intent.getBooleanExtra(Intent.EXTRA_FORCE_MASTER_CLEAR, false)
66 || intent.getBooleanExtra(Intent.EXTRA_FORCE_FACTORY_RESET, false);
Doug Zongkercdf00882014-03-18 12:52:04 -070067
Dianne Hackborn42499172010-10-15 18:45:07 -070068 Slog.w(TAG, "!!! FACTORY RESET !!!");
69 // The reboot call is blocking, so we need to do it on another thread.
70 Thread thr = new Thread("Reboot") {
71 @Override
72 public void run() {
73 try {
qingxi2f231512017-06-23 15:32:53 -070074 RecoverySystem
75 .rebootWipeUserData(context, shutdown, reason, forceWipe, mWipeEsims);
Dianne Hackborn42499172010-10-15 18:45:07 -070076 Log.wtf(TAG, "Still running after master clear?!");
77 } catch (IOException e) {
78 Slog.e(TAG, "Can't perform master clear/factory reset", e);
Julia Reynoldsfe053802014-06-30 11:41:32 -040079 } catch (SecurityException e) {
80 Slog.e(TAG, "Can't perform master clear/factory reset", e);
Dianne Hackborn42499172010-10-15 18:45:07 -070081 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 }
Dianne Hackborn42499172010-10-15 18:45:07 -070083 };
Rubin Xue8490f12015-06-25 12:17:48 +010084
qingxi0ca328f2017-05-19 15:20:03 -070085 if (mWipeExternalStorage || mWipeEsims) {
Rubin Xue8490f12015-06-25 12:17:48 +010086 // thr will be started at the end of this task.
qingxi240c2bb2017-04-12 17:31:18 -070087 new WipeDataTask(context, thr).execute();
Rubin Xue8490f12015-06-25 12:17:48 +010088 } else {
89 thr.start();
90 }
91 }
92
qingxi240c2bb2017-04-12 17:31:18 -070093 private class WipeDataTask extends AsyncTask<Void, Void, Void> {
Rubin Xue8490f12015-06-25 12:17:48 +010094 private final Thread mChainedTask;
95 private final Context mContext;
96 private final ProgressDialog mProgressDialog;
97
qingxi240c2bb2017-04-12 17:31:18 -070098 public WipeDataTask(Context context, Thread chainedTask) {
Rubin Xue8490f12015-06-25 12:17:48 +010099 mContext = context;
100 mChainedTask = chainedTask;
101 mProgressDialog = new ProgressDialog(context);
102 }
103
104 @Override
105 protected void onPreExecute() {
106 mProgressDialog.setIndeterminate(true);
107 mProgressDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
108 mProgressDialog.setMessage(mContext.getText(R.string.progress_erasing));
109 mProgressDialog.show();
110 }
111
112 @Override
113 protected Void doInBackground(Void... params) {
114 Slog.w(TAG, "Wiping adoptable disks");
qingxi240c2bb2017-04-12 17:31:18 -0700115 if (mWipeExternalStorage) {
116 StorageManager sm = (StorageManager) mContext.getSystemService(
117 Context.STORAGE_SERVICE);
118 sm.wipeAdoptableDisks();
119 }
Rubin Xue8490f12015-06-25 12:17:48 +0100120 return null;
121 }
122
123 @Override
124 protected void onPostExecute(Void result) {
125 mProgressDialog.dismiss();
126 mChainedTask.start();
127 }
128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 }
130}