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 | 0ca328f | 2017-05-19 15:20:03 -0700 | [diff] [blame] | 41 | private static final String ACTION_WIPE_EUICC_DATA = |
| 42 | "com.android.internal.action.wipe_euicc_data"; |
| 43 | private static final long DEFAULT_EUICC_WIPING_TIMEOUT_MILLIS = 30000L; // 30 s |
qingxi | 240c2bb | 2017-04-12 17:31:18 -0700 | [diff] [blame] | 44 | private boolean mWipeExternalStorage; |
qingxi | 0ca328f | 2017-05-19 15:20:03 -0700 | [diff] [blame] | 45 | private boolean mWipeEsims; |
| 46 | private static CountDownLatch mEuiccFactoryResetLatch; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | |
| 48 | @Override |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 49 | public void onReceive(final Context context, final Intent intent) { |
qingxi | 0ca328f | 2017-05-19 15:20:03 -0700 | [diff] [blame] | 50 | if (ACTION_WIPE_EUICC_DATA.equals(intent.getAction())) { |
| 51 | if (getResultCode() != EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK) { |
| 52 | int detailedCode = intent.getIntExtra( |
| 53 | EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE, 0); |
| 54 | Slog.e(TAG, "Error wiping euicc data, Detailed code = " + detailedCode); |
| 55 | } |
| 56 | mEuiccFactoryResetLatch.countDown(); |
| 57 | return; |
| 58 | } |
Wei Huang | 97ecc9c | 2009-05-11 17:44:20 -0700 | [diff] [blame] | 59 | if (intent.getAction().equals(Intent.ACTION_REMOTE_INTENT)) { |
Costin Manolache | 63cfebf | 2010-02-04 16:52:34 -0800 | [diff] [blame] | 60 | if (!"google.com".equals(intent.getStringExtra("from"))) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 61 | 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] | 62 | return; |
| 63 | } |
| 64 | } |
Lenka Trochtova | 73aeea2 | 2016-11-18 17:34:34 +0100 | [diff] [blame] | 65 | if (Intent.ACTION_MASTER_CLEAR.equals(intent.getAction())) { |
| 66 | Slog.w(TAG, "The request uses the deprecated Intent#ACTION_MASTER_CLEAR, " |
| 67 | + "Intent#ACTION_FACTORY_RESET should be used instead."); |
| 68 | } |
| 69 | if (intent.hasExtra(Intent.EXTRA_FORCE_MASTER_CLEAR)) { |
| 70 | Slog.w(TAG, "The request uses the deprecated Intent#EXTRA_FORCE_MASTER_CLEAR, " |
| 71 | + "Intent#EXTRA_FORCE_FACTORY_RESET should be used instead."); |
| 72 | } |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 73 | |
Doug Zongker | cdf0088 | 2014-03-18 12:52:04 -0700 | [diff] [blame] | 74 | final boolean shutdown = intent.getBooleanExtra("shutdown", false); |
Jeff Sharkey | 004a4b2 | 2014-09-24 11:45:24 -0700 | [diff] [blame] | 75 | final String reason = intent.getStringExtra(Intent.EXTRA_REASON); |
qingxi | 240c2bb | 2017-04-12 17:31:18 -0700 | [diff] [blame] | 76 | mWipeExternalStorage = intent.getBooleanExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, false); |
qingxi | 0ca328f | 2017-05-19 15:20:03 -0700 | [diff] [blame] | 77 | mWipeEsims = intent.getBooleanExtra(Intent.EXTRA_WIPE_ESIMS, false); |
Lenka Trochtova | 73aeea2 | 2016-11-18 17:34:34 +0100 | [diff] [blame] | 78 | final boolean forceWipe = intent.getBooleanExtra(Intent.EXTRA_FORCE_MASTER_CLEAR, false) |
| 79 | || intent.getBooleanExtra(Intent.EXTRA_FORCE_FACTORY_RESET, false); |
Doug Zongker | cdf0088 | 2014-03-18 12:52:04 -0700 | [diff] [blame] | 80 | |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 81 | Slog.w(TAG, "!!! FACTORY RESET !!!"); |
| 82 | // The reboot call is blocking, so we need to do it on another thread. |
| 83 | Thread thr = new Thread("Reboot") { |
| 84 | @Override |
| 85 | public void run() { |
| 86 | try { |
Benjamin Franz | f9d5e6a | 2016-05-26 14:24:29 +0100 | [diff] [blame] | 87 | RecoverySystem.rebootWipeUserData(context, shutdown, reason, forceWipe); |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 88 | Log.wtf(TAG, "Still running after master clear?!"); |
| 89 | } catch (IOException e) { |
| 90 | Slog.e(TAG, "Can't perform master clear/factory reset", e); |
Julia Reynolds | fe05380 | 2014-06-30 11:41:32 -0400 | [diff] [blame] | 91 | } catch (SecurityException e) { |
| 92 | Slog.e(TAG, "Can't perform master clear/factory reset", e); |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 93 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | } |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 95 | }; |
Rubin Xu | e8490f1 | 2015-06-25 12:17:48 +0100 | [diff] [blame] | 96 | |
qingxi | 0ca328f | 2017-05-19 15:20:03 -0700 | [diff] [blame] | 97 | if (mWipeExternalStorage || mWipeEsims) { |
Rubin Xu | e8490f1 | 2015-06-25 12:17:48 +0100 | [diff] [blame] | 98 | // thr will be started at the end of this task. |
qingxi | 240c2bb | 2017-04-12 17:31:18 -0700 | [diff] [blame] | 99 | new WipeDataTask(context, thr).execute(); |
Rubin Xu | e8490f1 | 2015-06-25 12:17:48 +0100 | [diff] [blame] | 100 | } else { |
| 101 | thr.start(); |
| 102 | } |
| 103 | } |
| 104 | |
qingxi | 240c2bb | 2017-04-12 17:31:18 -0700 | [diff] [blame] | 105 | private class WipeDataTask extends AsyncTask<Void, Void, Void> { |
Rubin Xu | e8490f1 | 2015-06-25 12:17:48 +0100 | [diff] [blame] | 106 | private final Thread mChainedTask; |
| 107 | private final Context mContext; |
| 108 | private final ProgressDialog mProgressDialog; |
| 109 | |
qingxi | 240c2bb | 2017-04-12 17:31:18 -0700 | [diff] [blame] | 110 | public WipeDataTask(Context context, Thread chainedTask) { |
Rubin Xu | e8490f1 | 2015-06-25 12:17:48 +0100 | [diff] [blame] | 111 | mContext = context; |
| 112 | mChainedTask = chainedTask; |
| 113 | mProgressDialog = new ProgressDialog(context); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | protected void onPreExecute() { |
| 118 | mProgressDialog.setIndeterminate(true); |
| 119 | mProgressDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); |
| 120 | mProgressDialog.setMessage(mContext.getText(R.string.progress_erasing)); |
| 121 | mProgressDialog.show(); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | protected Void doInBackground(Void... params) { |
| 126 | Slog.w(TAG, "Wiping adoptable disks"); |
qingxi | 240c2bb | 2017-04-12 17:31:18 -0700 | [diff] [blame] | 127 | if (mWipeExternalStorage) { |
| 128 | StorageManager sm = (StorageManager) mContext.getSystemService( |
| 129 | Context.STORAGE_SERVICE); |
| 130 | sm.wipeAdoptableDisks(); |
| 131 | } |
qingxi | 0ca328f | 2017-05-19 15:20:03 -0700 | [diff] [blame] | 132 | if (mWipeEsims) { |
qingxi | 240c2bb | 2017-04-12 17:31:18 -0700 | [diff] [blame] | 133 | EuiccManager euiccManager = (EuiccManager) mContext.getSystemService( |
| 134 | Context.EUICC_SERVICE); |
qingxi | 0ca328f | 2017-05-19 15:20:03 -0700 | [diff] [blame] | 135 | Intent intent = new Intent(mContext, MasterClearReceiver.class); |
| 136 | intent.setAction(ACTION_WIPE_EUICC_DATA); |
| 137 | PendingIntent callbackIntent = PendingIntent.getBroadcast( |
| 138 | mContext, |
| 139 | 0 /* requestCode */, |
| 140 | intent, |
| 141 | PendingIntent.FLAG_UPDATE_CURRENT); |
| 142 | mEuiccFactoryResetLatch = new CountDownLatch(1); |
| 143 | euiccManager.eraseSubscriptions(callbackIntent); |
| 144 | try { |
| 145 | long waitingTime = Settings.Global.getLong( |
| 146 | mContext.getContentResolver(), |
| 147 | Settings.Global.EUICC_WIPING_TIMEOUT_MILLIS, |
| 148 | DEFAULT_EUICC_WIPING_TIMEOUT_MILLIS); |
| 149 | |
| 150 | if (!mEuiccFactoryResetLatch.await(waitingTime, TimeUnit.MILLISECONDS)) { |
| 151 | Slog.e(TAG, "Timeout wiping eUICC data."); |
| 152 | } |
| 153 | } catch (InterruptedException e) { |
| 154 | Thread.currentThread().interrupt(); |
| 155 | Slog.e(TAG, "Wiping eUICC data interrupted", e); |
| 156 | } |
qingxi | 240c2bb | 2017-04-12 17:31:18 -0700 | [diff] [blame] | 157 | } |
Rubin Xu | e8490f1 | 2015-06-25 12:17:48 +0100 | [diff] [blame] | 158 | return null; |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | protected void onPostExecute(Void result) { |
| 163 | mProgressDialog.dismiss(); |
| 164 | mChainedTask.start(); |
| 165 | } |
| 166 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | } |
| 168 | } |