blob: 6f8edecb1777f280b7cee9ba664ed8aef6e0d57e [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080027import android.util.Slog;
Rubin Xue8490f12015-06-25 12:17:48 +010028import android.view.WindowManager;
29
30import com.android.internal.R;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
Dan Egnor18e93962010-02-10 19:27:58 -080032import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
Dan Egnor18e93962010-02-10 19:27:58 -080034public class MasterClearReceiver extends BroadcastReceiver {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035 private static final String TAG = "MasterClear";
36
37 @Override
Dianne Hackborn42499172010-10-15 18:45:07 -070038 public void onReceive(final Context context, final Intent intent) {
Wei Huang97ecc9c2009-05-11 17:44:20 -070039 if (intent.getAction().equals(Intent.ACTION_REMOTE_INTENT)) {
Costin Manolache63cfebf2010-02-04 16:52:34 -080040 if (!"google.com".equals(intent.getStringExtra("from"))) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080041 Slog.w(TAG, "Ignoring master clear request -- not from trusted server.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 return;
43 }
44 }
Dan Egnor18e93962010-02-10 19:27:58 -080045
Doug Zongkercdf00882014-03-18 12:52:04 -070046 final boolean shutdown = intent.getBooleanExtra("shutdown", false);
Jeff Sharkey004a4b22014-09-24 11:45:24 -070047 final String reason = intent.getStringExtra(Intent.EXTRA_REASON);
Rubin Xue8490f12015-06-25 12:17:48 +010048 final boolean wipeExternalStorage = intent.getBooleanExtra(
49 Intent.EXTRA_WIPE_EXTERNAL_STORAGE, false);
Benjamin Franzf9d5e6a2016-05-26 14:24:29 +010050 final boolean forceWipe = intent.getBooleanExtra(Intent.EXTRA_FORCE_MASTER_CLEAR, false);
Doug Zongkercdf00882014-03-18 12:52:04 -070051
Dianne Hackborn42499172010-10-15 18:45:07 -070052 Slog.w(TAG, "!!! FACTORY RESET !!!");
53 // The reboot call is blocking, so we need to do it on another thread.
54 Thread thr = new Thread("Reboot") {
55 @Override
56 public void run() {
57 try {
Benjamin Franzf9d5e6a2016-05-26 14:24:29 +010058 RecoverySystem.rebootWipeUserData(context, shutdown, reason, forceWipe);
Dianne Hackborn42499172010-10-15 18:45:07 -070059 Log.wtf(TAG, "Still running after master clear?!");
60 } catch (IOException e) {
61 Slog.e(TAG, "Can't perform master clear/factory reset", e);
Julia Reynoldsfe053802014-06-30 11:41:32 -040062 } catch (SecurityException e) {
63 Slog.e(TAG, "Can't perform master clear/factory reset", e);
Dianne Hackborn42499172010-10-15 18:45:07 -070064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 }
Dianne Hackborn42499172010-10-15 18:45:07 -070066 };
Rubin Xue8490f12015-06-25 12:17:48 +010067
68 if (wipeExternalStorage) {
69 // thr will be started at the end of this task.
70 new WipeAdoptableDisksTask(context, thr).execute();
71 } else {
72 thr.start();
73 }
74 }
75
76 private class WipeAdoptableDisksTask extends AsyncTask<Void, Void, Void> {
77 private final Thread mChainedTask;
78 private final Context mContext;
79 private final ProgressDialog mProgressDialog;
80
81 public WipeAdoptableDisksTask(Context context, Thread chainedTask) {
82 mContext = context;
83 mChainedTask = chainedTask;
84 mProgressDialog = new ProgressDialog(context);
85 }
86
87 @Override
88 protected void onPreExecute() {
89 mProgressDialog.setIndeterminate(true);
90 mProgressDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
91 mProgressDialog.setMessage(mContext.getText(R.string.progress_erasing));
92 mProgressDialog.show();
93 }
94
95 @Override
96 protected Void doInBackground(Void... params) {
97 Slog.w(TAG, "Wiping adoptable disks");
98 StorageManager sm = (StorageManager) mContext.getSystemService(
99 Context.STORAGE_SERVICE);
100 sm.wipeAdoptableDisks();
101 return null;
102 }
103
104 @Override
105 protected void onPostExecute(Void result) {
106 mProgressDialog.dismiss();
107 mChainedTask.start();
108 }
109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 }
111}