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 17 package com.android.server; 18 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; 29 30 import com.android.internal.R; 31 32 import java.io.IOException; 33 34 public class MasterClearReceiver extends BroadcastReceiver { 35 private static final String TAG = "MasterClear"; 36 37 @Override onReceive(final Context context, final Intent intent)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 final boolean forceWipe = intent.getBooleanExtra(Intent.EXTRA_FORCE_MASTER_CLEAR, false); 51 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") { 55 @Override 56 public void run() { 57 try { 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); 64 } 65 } 66 }; 67 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 WipeAdoptableDisksTask(Context context, Thread chainedTask)81 public WipeAdoptableDisksTask(Context context, Thread chainedTask) { 82 mContext = context; 83 mChainedTask = chainedTask; 84 mProgressDialog = new ProgressDialog(context); 85 } 86 87 @Override onPreExecute()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 doInBackground(Void... params)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 onPostExecute(Void result)105 protected void onPostExecute(Void result) { 106 mProgressDialog.dismiss(); 107 mChainedTask.start(); 108 } 109 110 } 111 } 112