blob: 7080c41fea26acfdce61f6a3451131011be9c206 [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
Rubin Xue8490f12015-06-25 12:17:48 +010019import android.app.ProgressDialog;
Dan Egnor18e93962010-02-10 19:27:58 -080020import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.Context;
22import android.content.Intent;
Rubin Xue8490f12015-06-25 12:17:48 +010023import android.os.AsyncTask;
Dan Egnor18e93962010-02-10 19:27:58 -080024import android.os.RecoverySystem;
Rubin Xue8490f12015-06-25 12:17:48 +010025import android.os.storage.StorageManager;
qingxi240c2bb2017-04-12 17:31:18 -070026import android.telephony.euicc.EuiccManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080028import android.util.Slog;
Rubin Xue8490f12015-06-25 12:17:48 +010029import android.view.WindowManager;
30
31import com.android.internal.R;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
Dan Egnor18e93962010-02-10 19:27:58 -080033import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
Dan Egnor18e93962010-02-10 19:27:58 -080035public class MasterClearReceiver extends BroadcastReceiver {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036 private static final String TAG = "MasterClear";
qingxi240c2bb2017-04-12 17:31:18 -070037 private boolean mWipeExternalStorage;
38 private boolean mWipeEims;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40 @Override
Dianne Hackborn42499172010-10-15 18:45:07 -070041 public void onReceive(final Context context, final Intent intent) {
Wei Huang97ecc9c2009-05-11 17:44:20 -070042 if (intent.getAction().equals(Intent.ACTION_REMOTE_INTENT)) {
Costin Manolache63cfebf2010-02-04 16:52:34 -080043 if (!"google.com".equals(intent.getStringExtra("from"))) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080044 Slog.w(TAG, "Ignoring master clear request -- not from trusted server.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 return;
46 }
47 }
Lenka Trochtova73aeea22016-11-18 17:34:34 +010048 if (Intent.ACTION_MASTER_CLEAR.equals(intent.getAction())) {
49 Slog.w(TAG, "The request uses the deprecated Intent#ACTION_MASTER_CLEAR, "
50 + "Intent#ACTION_FACTORY_RESET should be used instead.");
51 }
52 if (intent.hasExtra(Intent.EXTRA_FORCE_MASTER_CLEAR)) {
53 Slog.w(TAG, "The request uses the deprecated Intent#EXTRA_FORCE_MASTER_CLEAR, "
54 + "Intent#EXTRA_FORCE_FACTORY_RESET should be used instead.");
55 }
Dan Egnor18e93962010-02-10 19:27:58 -080056
Doug Zongkercdf00882014-03-18 12:52:04 -070057 final boolean shutdown = intent.getBooleanExtra("shutdown", false);
Jeff Sharkey004a4b22014-09-24 11:45:24 -070058 final String reason = intent.getStringExtra(Intent.EXTRA_REASON);
qingxi240c2bb2017-04-12 17:31:18 -070059 mWipeExternalStorage = intent.getBooleanExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, false);
60 mWipeEims = intent.getBooleanExtra(Intent.EXTRA_WIPE_ESIMS, false);
Lenka Trochtova73aeea22016-11-18 17:34:34 +010061 final boolean forceWipe = intent.getBooleanExtra(Intent.EXTRA_FORCE_MASTER_CLEAR, false)
62 || intent.getBooleanExtra(Intent.EXTRA_FORCE_FACTORY_RESET, false);
Doug Zongkercdf00882014-03-18 12:52:04 -070063
Dianne Hackborn42499172010-10-15 18:45:07 -070064 Slog.w(TAG, "!!! FACTORY RESET !!!");
65 // The reboot call is blocking, so we need to do it on another thread.
66 Thread thr = new Thread("Reboot") {
67 @Override
68 public void run() {
69 try {
Benjamin Franzf9d5e6a2016-05-26 14:24:29 +010070 RecoverySystem.rebootWipeUserData(context, shutdown, reason, forceWipe);
Dianne Hackborn42499172010-10-15 18:45:07 -070071 Log.wtf(TAG, "Still running after master clear?!");
72 } catch (IOException e) {
73 Slog.e(TAG, "Can't perform master clear/factory reset", e);
Julia Reynoldsfe053802014-06-30 11:41:32 -040074 } catch (SecurityException e) {
75 Slog.e(TAG, "Can't perform master clear/factory reset", e);
Dianne Hackborn42499172010-10-15 18:45:07 -070076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 }
Dianne Hackborn42499172010-10-15 18:45:07 -070078 };
Rubin Xue8490f12015-06-25 12:17:48 +010079
qingxi240c2bb2017-04-12 17:31:18 -070080 if (mWipeExternalStorage || mWipeEims) {
Rubin Xue8490f12015-06-25 12:17:48 +010081 // thr will be started at the end of this task.
qingxi240c2bb2017-04-12 17:31:18 -070082 new WipeDataTask(context, thr).execute();
Rubin Xue8490f12015-06-25 12:17:48 +010083 } else {
84 thr.start();
85 }
86 }
87
qingxi240c2bb2017-04-12 17:31:18 -070088 private class WipeDataTask extends AsyncTask<Void, Void, Void> {
Rubin Xue8490f12015-06-25 12:17:48 +010089 private final Thread mChainedTask;
90 private final Context mContext;
91 private final ProgressDialog mProgressDialog;
92
qingxi240c2bb2017-04-12 17:31:18 -070093 public WipeDataTask(Context context, Thread chainedTask) {
Rubin Xue8490f12015-06-25 12:17:48 +010094 mContext = context;
95 mChainedTask = chainedTask;
96 mProgressDialog = new ProgressDialog(context);
97 }
98
99 @Override
100 protected void onPreExecute() {
101 mProgressDialog.setIndeterminate(true);
102 mProgressDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
103 mProgressDialog.setMessage(mContext.getText(R.string.progress_erasing));
104 mProgressDialog.show();
105 }
106
107 @Override
108 protected Void doInBackground(Void... params) {
109 Slog.w(TAG, "Wiping adoptable disks");
qingxi240c2bb2017-04-12 17:31:18 -0700110 if (mWipeExternalStorage) {
111 StorageManager sm = (StorageManager) mContext.getSystemService(
112 Context.STORAGE_SERVICE);
113 sm.wipeAdoptableDisks();
114 }
115 if (mWipeEims) {
116 EuiccManager euiccManager = (EuiccManager) mContext.getSystemService(
117 Context.EUICC_SERVICE);
118 // STOPSHIP: add EuiccManager API to factory reset eUICC
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}