2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.server;
19 import android.app.ProgressDialog;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.os.AsyncTask;
24 import android.os.RecoverySystem;
25 import android.os.storage.StorageManager;
26 import android.util.Log;
27 import android.util.Slog;
28 import android.view.WindowManager;
30 import com.android.internal.R;
32 import java.io.IOException;
34 public class MasterClearReceiver extends BroadcastReceiver {
35 private static final String TAG = "MasterClear";
38 public void onReceive(final Context context, final Intent intent) {
39 if (intent.getAction().equals(Intent.ACTION_REMOTE_INTENT)) {
40 if (!"google.com".equals(intent.getStringExtra("from"))) {
41 Slog.w(TAG, "Ignoring master clear request -- not from trusted server.");
46 final boolean shutdown = intent.getBooleanExtra("shutdown", false);
47 final String reason = intent.getStringExtra(Intent.EXTRA_REASON);
48 final boolean wipeExternalStorage = intent.getBooleanExtra(
49 Intent.EXTRA_WIPE_EXTERNAL_STORAGE, false);
50 final boolean forceWipe = intent.getBooleanExtra(Intent.EXTRA_FORCE_MASTER_CLEAR, false);
52 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") {
58 RecoverySystem.rebootWipeUserData(context, shutdown, reason, forceWipe);
59 Log.wtf(TAG, "Still running after master clear?!");
60 } catch (IOException e) {
61 Slog.e(TAG, "Can't perform master clear/factory reset", e);
62 } catch (SecurityException e) {
63 Slog.e(TAG, "Can't perform master clear/factory reset", e);
68 if (wipeExternalStorage) {
69 // thr will be started at the end of this task.
70 new WipeAdoptableDisksTask(context, thr).execute();
76 private class WipeAdoptableDisksTask extends AsyncTask<Void, Void, Void> {
77 private final Thread mChainedTask;
78 private final Context mContext;
79 private final ProgressDialog mProgressDialog;
81 public WipeAdoptableDisksTask(Context context, Thread chainedTask) {
83 mChainedTask = chainedTask;
84 mProgressDialog = new ProgressDialog(context);
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();
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();
105 protected void onPostExecute(Void result) {
106 mProgressDialog.dismiss();
107 mChainedTask.start();