The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
qingxi | 0ca328f | 2017-05-19 15:20:03 -0700 | [diff] [blame] | 19 | import android.app.PendingIntent; |
Rubin Xu | e8490f1 | 2015-06-25 12:17:48 +0100 | [diff] [blame] | 20 | import android.app.ProgressDialog; |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 21 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
Rubin Xu | e8490f1 | 2015-06-25 12:17:48 +0100 | [diff] [blame] | 24 | import android.os.AsyncTask; |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 25 | import android.os.RecoverySystem; |
Rubin Xu | e8490f1 | 2015-06-25 12:17:48 +0100 | [diff] [blame] | 26 | import android.os.storage.StorageManager; |
qingxi | 0ca328f | 2017-05-19 15:20:03 -0700 | [diff] [blame] | 27 | import android.provider.Settings; |
qingxi | 240c2bb | 2017-04-12 17:31:18 -0700 | [diff] [blame] | 28 | import android.telephony.euicc.EuiccManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.util.Log; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 30 | import android.util.Slog; |
Rubin Xu | e8490f1 | 2015-06-25 12:17:48 +0100 | [diff] [blame] | 31 | import android.view.WindowManager; |
| 32 | |
| 33 | import com.android.internal.R; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 35 | import java.io.IOException; |
qingxi | 0ca328f | 2017-05-19 15:20:03 -0700 | [diff] [blame] | 36 | import java.util.concurrent.CountDownLatch; |
| 37 | import java.util.concurrent.TimeUnit; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 39 | public class MasterClearReceiver extends BroadcastReceiver { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | private static final String TAG = "MasterClear"; |
qingxi | 240c2bb | 2017-04-12 17:31:18 -0700 | [diff] [blame] | 41 | private boolean mWipeExternalStorage; |
qingxi | 0ca328f | 2017-05-19 15:20:03 -0700 | [diff] [blame] | 42 | private boolean mWipeEsims; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | |
| 44 | @Override |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 45 | public void onReceive(final Context context, final Intent intent) { |
Wei Huang | 97ecc9c | 2009-05-11 17:44:20 -0700 | [diff] [blame] | 46 | if (intent.getAction().equals(Intent.ACTION_REMOTE_INTENT)) { |
Costin Manolache | 63cfebf | 2010-02-04 16:52:34 -0800 | [diff] [blame] | 47 | if (!"google.com".equals(intent.getStringExtra("from"))) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 48 | Slog.w(TAG, "Ignoring master clear request -- not from trusted server."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | return; |
| 50 | } |
| 51 | } |
Lenka Trochtova | 73aeea2 | 2016-11-18 17:34:34 +0100 | [diff] [blame] | 52 | if (Intent.ACTION_MASTER_CLEAR.equals(intent.getAction())) { |
| 53 | Slog.w(TAG, "The request uses the deprecated Intent#ACTION_MASTER_CLEAR, " |
| 54 | + "Intent#ACTION_FACTORY_RESET should be used instead."); |
| 55 | } |
| 56 | if (intent.hasExtra(Intent.EXTRA_FORCE_MASTER_CLEAR)) { |
| 57 | Slog.w(TAG, "The request uses the deprecated Intent#EXTRA_FORCE_MASTER_CLEAR, " |
| 58 | + "Intent#EXTRA_FORCE_FACTORY_RESET should be used instead."); |
| 59 | } |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 60 | |
Doug Zongker | cdf0088 | 2014-03-18 12:52:04 -0700 | [diff] [blame] | 61 | final boolean shutdown = intent.getBooleanExtra("shutdown", false); |
Jeff Sharkey | 004a4b2 | 2014-09-24 11:45:24 -0700 | [diff] [blame] | 62 | final String reason = intent.getStringExtra(Intent.EXTRA_REASON); |
qingxi | 240c2bb | 2017-04-12 17:31:18 -0700 | [diff] [blame] | 63 | mWipeExternalStorage = intent.getBooleanExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, false); |
qingxi | 0ca328f | 2017-05-19 15:20:03 -0700 | [diff] [blame] | 64 | mWipeEsims = intent.getBooleanExtra(Intent.EXTRA_WIPE_ESIMS, false); |
Lenka Trochtova | 73aeea2 | 2016-11-18 17:34:34 +0100 | [diff] [blame] | 65 | final boolean forceWipe = intent.getBooleanExtra(Intent.EXTRA_FORCE_MASTER_CLEAR, false) |
| 66 | || intent.getBooleanExtra(Intent.EXTRA_FORCE_FACTORY_RESET, false); |
Doug Zongker | cdf0088 | 2014-03-18 12:52:04 -0700 | [diff] [blame] | 67 | |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 68 | Slog.w(TAG, "!!! FACTORY RESET !!!"); |
| 69 | // The reboot call is blocking, so we need to do it on another thread. |
| 70 | Thread thr = new Thread("Reboot") { |
| 71 | @Override |
| 72 | public void run() { |
| 73 | try { |
qingxi | 2f23151 | 2017-06-23 15:32:53 -0700 | [diff] [blame] | 74 | RecoverySystem |
| 75 | .rebootWipeUserData(context, shutdown, reason, forceWipe, mWipeEsims); |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 76 | Log.wtf(TAG, "Still running after master clear?!"); |
| 77 | } catch (IOException e) { |
| 78 | Slog.e(TAG, "Can't perform master clear/factory reset", e); |
Julia Reynolds | fe05380 | 2014-06-30 11:41:32 -0400 | [diff] [blame] | 79 | } catch (SecurityException e) { |
| 80 | Slog.e(TAG, "Can't perform master clear/factory reset", e); |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 81 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | } |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 83 | }; |
Rubin Xu | e8490f1 | 2015-06-25 12:17:48 +0100 | [diff] [blame] | 84 | |
qingxi | 0ca328f | 2017-05-19 15:20:03 -0700 | [diff] [blame] | 85 | if (mWipeExternalStorage || mWipeEsims) { |
Rubin Xu | e8490f1 | 2015-06-25 12:17:48 +0100 | [diff] [blame] | 86 | // thr will be started at the end of this task. |
qingxi | 240c2bb | 2017-04-12 17:31:18 -0700 | [diff] [blame] | 87 | new WipeDataTask(context, thr).execute(); |
Rubin Xu | e8490f1 | 2015-06-25 12:17:48 +0100 | [diff] [blame] | 88 | } else { |
| 89 | thr.start(); |
| 90 | } |
| 91 | } |
| 92 | |
qingxi | 240c2bb | 2017-04-12 17:31:18 -0700 | [diff] [blame] | 93 | private class WipeDataTask extends AsyncTask<Void, Void, Void> { |
Rubin Xu | e8490f1 | 2015-06-25 12:17:48 +0100 | [diff] [blame] | 94 | private final Thread mChainedTask; |
| 95 | private final Context mContext; |
| 96 | private final ProgressDialog mProgressDialog; |
| 97 | |
qingxi | 240c2bb | 2017-04-12 17:31:18 -0700 | [diff] [blame] | 98 | public WipeDataTask(Context context, Thread chainedTask) { |
Rubin Xu | e8490f1 | 2015-06-25 12:17:48 +0100 | [diff] [blame] | 99 | mContext = context; |
| 100 | mChainedTask = chainedTask; |
| 101 | mProgressDialog = new ProgressDialog(context); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | protected void onPreExecute() { |
| 106 | mProgressDialog.setIndeterminate(true); |
| 107 | mProgressDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); |
| 108 | mProgressDialog.setMessage(mContext.getText(R.string.progress_erasing)); |
| 109 | mProgressDialog.show(); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | protected Void doInBackground(Void... params) { |
| 114 | Slog.w(TAG, "Wiping adoptable disks"); |
qingxi | 240c2bb | 2017-04-12 17:31:18 -0700 | [diff] [blame] | 115 | if (mWipeExternalStorage) { |
| 116 | StorageManager sm = (StorageManager) mContext.getSystemService( |
| 117 | Context.STORAGE_SERVICE); |
| 118 | sm.wipeAdoptableDisks(); |
| 119 | } |
Rubin Xu | e8490f1 | 2015-06-25 12:17:48 +0100 | [diff] [blame] | 120 | return null; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | protected void onPostExecute(Void result) { |
| 125 | mProgressDialog.dismiss(); |
| 126 | mChainedTask.start(); |
| 127 | } |
| 128 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | } |
| 130 | } |