1/*
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
19import android.app.ProgressDialog;
20import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.os.AsyncTask;
24import android.os.RecoverySystem;
25import android.os.storage.StorageManager;
26import android.util.Log;
27import android.util.Slog;
28import android.view.WindowManager;
29
30import com.android.internal.R;
31
32import java.io.IOException;
33
34public class MasterClearReceiver extends BroadcastReceiver {
35    private static final String TAG = "MasterClear";
36
37    @Override
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.");
42                return;
43            }
44        }
45
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
51        Slog.w(TAG, "!!! FACTORY RESET !!!");
52        // The reboot call is blocking, so we need to do it on another thread.
53        Thread thr = new Thread("Reboot") {
54            @Override
55            public void run() {
56                try {
57                    RecoverySystem.rebootWipeUserData(context, shutdown, reason);
58                    Log.wtf(TAG, "Still running after master clear?!");
59                } catch (IOException e) {
60                    Slog.e(TAG, "Can't perform master clear/factory reset", e);
61                } catch (SecurityException e) {
62                    Slog.e(TAG, "Can't perform master clear/factory reset", e);
63                }
64            }
65        };
66
67        if (wipeExternalStorage) {
68            // thr will be started at the end of this task.
69            new WipeAdoptableDisksTask(context, thr).execute();
70        } else {
71            thr.start();
72        }
73    }
74
75    private class WipeAdoptableDisksTask extends AsyncTask<Void, Void, Void> {
76        private final Thread mChainedTask;
77        private final Context mContext;
78        private final ProgressDialog mProgressDialog;
79
80        public WipeAdoptableDisksTask(Context context, Thread chainedTask) {
81            mContext = context;
82            mChainedTask = chainedTask;
83            mProgressDialog = new ProgressDialog(context);
84        }
85
86        @Override
87        protected void onPreExecute() {
88            mProgressDialog.setIndeterminate(true);
89            mProgressDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
90            mProgressDialog.setMessage(mContext.getText(R.string.progress_erasing));
91            mProgressDialog.show();
92        }
93
94        @Override
95        protected Void doInBackground(Void... params) {
96            Slog.w(TAG, "Wiping adoptable disks");
97            StorageManager sm = (StorageManager) mContext.getSystemService(
98                    Context.STORAGE_SERVICE);
99            sm.wipeAdoptableDisks();
100            return null;
101        }
102
103        @Override
104        protected void onPostExecute(Void result) {
105            mProgressDialog.dismiss();
106            mChainedTask.start();
107        }
108
109    }
110}
111