blob: 516f8f67a7901db5eb1529fd574680fadf842a4c [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";
qingxi0ca328f2017-05-19 15:20:03 -070041 private static final String ACTION_WIPE_EUICC_DATA =
42 "com.android.internal.action.wipe_euicc_data";
43 private static final long DEFAULT_EUICC_WIPING_TIMEOUT_MILLIS = 30000L; // 30 s
qingxi240c2bb2017-04-12 17:31:18 -070044 private boolean mWipeExternalStorage;
qingxi0ca328f2017-05-19 15:20:03 -070045 private boolean mWipeEsims;
46 private static CountDownLatch mEuiccFactoryResetLatch;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
48 @Override
Dianne Hackborn42499172010-10-15 18:45:07 -070049 public void onReceive(final Context context, final Intent intent) {
qingxi0ca328f2017-05-19 15:20:03 -070050 if (ACTION_WIPE_EUICC_DATA.equals(intent.getAction())) {
51 if (getResultCode() != EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK) {
52 int detailedCode = intent.getIntExtra(
53 EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE, 0);
54 Slog.e(TAG, "Error wiping euicc data, Detailed code = " + detailedCode);
55 }
56 mEuiccFactoryResetLatch.countDown();
57 return;
58 }
Wei Huang97ecc9c2009-05-11 17:44:20 -070059 if (intent.getAction().equals(Intent.ACTION_REMOTE_INTENT)) {
Costin Manolache63cfebf2010-02-04 16:52:34 -080060 if (!"google.com".equals(intent.getStringExtra("from"))) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080061 Slog.w(TAG, "Ignoring master clear request -- not from trusted server.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 return;
63 }
64 }
Lenka Trochtova73aeea22016-11-18 17:34:34 +010065 if (Intent.ACTION_MASTER_CLEAR.equals(intent.getAction())) {
66 Slog.w(TAG, "The request uses the deprecated Intent#ACTION_MASTER_CLEAR, "
67 + "Intent#ACTION_FACTORY_RESET should be used instead.");
68 }
69 if (intent.hasExtra(Intent.EXTRA_FORCE_MASTER_CLEAR)) {
70 Slog.w(TAG, "The request uses the deprecated Intent#EXTRA_FORCE_MASTER_CLEAR, "
71 + "Intent#EXTRA_FORCE_FACTORY_RESET should be used instead.");
72 }
Dan Egnor18e93962010-02-10 19:27:58 -080073
Doug Zongkercdf00882014-03-18 12:52:04 -070074 final boolean shutdown = intent.getBooleanExtra("shutdown", false);
Jeff Sharkey004a4b22014-09-24 11:45:24 -070075 final String reason = intent.getStringExtra(Intent.EXTRA_REASON);
qingxi240c2bb2017-04-12 17:31:18 -070076 mWipeExternalStorage = intent.getBooleanExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, false);
qingxi0ca328f2017-05-19 15:20:03 -070077 mWipeEsims = intent.getBooleanExtra(Intent.EXTRA_WIPE_ESIMS, false);
Lenka Trochtova73aeea22016-11-18 17:34:34 +010078 final boolean forceWipe = intent.getBooleanExtra(Intent.EXTRA_FORCE_MASTER_CLEAR, false)
79 || intent.getBooleanExtra(Intent.EXTRA_FORCE_FACTORY_RESET, false);
Doug Zongkercdf00882014-03-18 12:52:04 -070080
Dianne Hackborn42499172010-10-15 18:45:07 -070081 Slog.w(TAG, "!!! FACTORY RESET !!!");
82 // The reboot call is blocking, so we need to do it on another thread.
83 Thread thr = new Thread("Reboot") {
84 @Override
85 public void run() {
86 try {
Benjamin Franzf9d5e6a2016-05-26 14:24:29 +010087 RecoverySystem.rebootWipeUserData(context, shutdown, reason, forceWipe);
Dianne Hackborn42499172010-10-15 18:45:07 -070088 Log.wtf(TAG, "Still running after master clear?!");
89 } catch (IOException e) {
90 Slog.e(TAG, "Can't perform master clear/factory reset", e);
Julia Reynoldsfe053802014-06-30 11:41:32 -040091 } catch (SecurityException e) {
92 Slog.e(TAG, "Can't perform master clear/factory reset", e);
Dianne Hackborn42499172010-10-15 18:45:07 -070093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 }
Dianne Hackborn42499172010-10-15 18:45:07 -070095 };
Rubin Xue8490f12015-06-25 12:17:48 +010096
qingxi0ca328f2017-05-19 15:20:03 -070097 if (mWipeExternalStorage || mWipeEsims) {
Rubin Xue8490f12015-06-25 12:17:48 +010098 // thr will be started at the end of this task.
qingxi240c2bb2017-04-12 17:31:18 -070099 new WipeDataTask(context, thr).execute();
Rubin Xue8490f12015-06-25 12:17:48 +0100100 } else {
101 thr.start();
102 }
103 }
104
qingxi240c2bb2017-04-12 17:31:18 -0700105 private class WipeDataTask extends AsyncTask<Void, Void, Void> {
Rubin Xue8490f12015-06-25 12:17:48 +0100106 private final Thread mChainedTask;
107 private final Context mContext;
108 private final ProgressDialog mProgressDialog;
109
qingxi240c2bb2017-04-12 17:31:18 -0700110 public WipeDataTask(Context context, Thread chainedTask) {
Rubin Xue8490f12015-06-25 12:17:48 +0100111 mContext = context;
112 mChainedTask = chainedTask;
113 mProgressDialog = new ProgressDialog(context);
114 }
115
116 @Override
117 protected void onPreExecute() {
118 mProgressDialog.setIndeterminate(true);
119 mProgressDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
120 mProgressDialog.setMessage(mContext.getText(R.string.progress_erasing));
121 mProgressDialog.show();
122 }
123
124 @Override
125 protected Void doInBackground(Void... params) {
126 Slog.w(TAG, "Wiping adoptable disks");
qingxi240c2bb2017-04-12 17:31:18 -0700127 if (mWipeExternalStorage) {
128 StorageManager sm = (StorageManager) mContext.getSystemService(
129 Context.STORAGE_SERVICE);
130 sm.wipeAdoptableDisks();
131 }
qingxi0ca328f2017-05-19 15:20:03 -0700132 if (mWipeEsims) {
qingxi240c2bb2017-04-12 17:31:18 -0700133 EuiccManager euiccManager = (EuiccManager) mContext.getSystemService(
134 Context.EUICC_SERVICE);
qingxi0ca328f2017-05-19 15:20:03 -0700135 Intent intent = new Intent(mContext, MasterClearReceiver.class);
136 intent.setAction(ACTION_WIPE_EUICC_DATA);
137 PendingIntent callbackIntent = PendingIntent.getBroadcast(
138 mContext,
139 0 /* requestCode */,
140 intent,
141 PendingIntent.FLAG_UPDATE_CURRENT);
142 mEuiccFactoryResetLatch = new CountDownLatch(1);
143 euiccManager.eraseSubscriptions(callbackIntent);
144 try {
145 long waitingTime = Settings.Global.getLong(
146 mContext.getContentResolver(),
147 Settings.Global.EUICC_WIPING_TIMEOUT_MILLIS,
148 DEFAULT_EUICC_WIPING_TIMEOUT_MILLIS);
149
150 if (!mEuiccFactoryResetLatch.await(waitingTime, TimeUnit.MILLISECONDS)) {
151 Slog.e(TAG, "Timeout wiping eUICC data.");
152 }
153 } catch (InterruptedException e) {
154 Thread.currentThread().interrupt();
155 Slog.e(TAG, "Wiping eUICC data interrupted", e);
156 }
qingxi240c2bb2017-04-12 17:31:18 -0700157 }
Rubin Xue8490f12015-06-25 12:17:48 +0100158 return null;
159 }
160
161 @Override
162 protected void onPostExecute(Void result) {
163 mProgressDialog.dismiss();
164 mChainedTask.start();
165 }
166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 }
168}