OSDN Git Service

Use StorageManager.wipeAdoptableDisks to wipe external disks
authorRubin Xu <rubinxu@google.com>
Thu, 25 Jun 2015 11:17:48 +0000 (12:17 +0100)
committerRubin Xu <rubinxu@google.com>
Fri, 26 Jun 2015 14:58:21 +0000 (15:58 +0100)
Retire FORMAT_AND_FACTORY_RESET which is more fragile.

Bug: 9433509
Change-Id: I158ee987274bb4db41d466de9f1e3c60ffc1d140

core/java/android/content/Intent.java
core/java/com/android/internal/os/storage/ExternalStorageFormatter.java
services/core/java/com/android/server/MasterClearReceiver.java
services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java

index 5190037..fd7ce7a 100644 (file)
@@ -3779,6 +3779,9 @@ public class Intent implements Parcelable, Cloneable {
     /** {@hide} */
     public static final String EXTRA_REASON = "android.intent.extra.REASON";
 
+    /** {@hide} */
+    public static final String EXTRA_WIPE_EXTERNAL_STORAGE = "android.intent.extra.WIPE_EXTERNAL_STORAGE";
+
     /**
      * Optional {@link android.app.PendingIntent} extra used to deliver the result of the SIM
      * activation request.
index 1d0511f..0a01ae9 100644 (file)
@@ -17,6 +17,10 @@ import com.android.internal.R;
 
 /**
  * Takes care of unmounting and formatting external storage.
+ *
+ * @deprecated Please use {@link Intent#ACTION_MASTER_CLEAR} broadcast with extra
+ * {@link Intent#EXTRA_WIPE_EXTERNAL_STORAGE} to wipe and factory reset, or call
+ * {@link StorageManager#wipeAdoptableDisks} directly to format external storages.
  */
 public class ExternalStorageFormatter extends Service {
     static final String TAG = "ExternalStorageFormatter";
index f1d5aa3..1653db9 100644 (file)
 
 package com.android.server;
 
+import android.app.ProgressDialog;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
+import android.os.AsyncTask;
 import android.os.RecoverySystem;
+import android.os.storage.StorageManager;
 import android.util.Log;
 import android.util.Slog;
+import android.view.WindowManager;
+
+import com.android.internal.R;
 
 import java.io.IOException;
 
@@ -39,6 +45,8 @@ public class MasterClearReceiver extends BroadcastReceiver {
 
         final boolean shutdown = intent.getBooleanExtra("shutdown", false);
         final String reason = intent.getStringExtra(Intent.EXTRA_REASON);
+        final boolean wipeExternalStorage = intent.getBooleanExtra(
+                Intent.EXTRA_WIPE_EXTERNAL_STORAGE, false);
 
         Slog.w(TAG, "!!! FACTORY RESET !!!");
         // The reboot call is blocking, so we need to do it on another thread.
@@ -55,6 +63,48 @@ public class MasterClearReceiver extends BroadcastReceiver {
                 }
             }
         };
-        thr.start();
+
+        if (wipeExternalStorage) {
+            // thr will be started at the end of this task.
+            new WipeAdoptableDisksTask(context, thr).execute();
+        } else {
+            thr.start();
+        }
+    }
+
+    private class WipeAdoptableDisksTask extends AsyncTask<Void, Void, Void> {
+        private final Thread mChainedTask;
+        private final Context mContext;
+        private final ProgressDialog mProgressDialog;
+
+        public WipeAdoptableDisksTask(Context context, Thread chainedTask) {
+            mContext = context;
+            mChainedTask = chainedTask;
+            mProgressDialog = new ProgressDialog(context);
+        }
+
+        @Override
+        protected void onPreExecute() {
+            mProgressDialog.setIndeterminate(true);
+            mProgressDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
+            mProgressDialog.setMessage(mContext.getText(R.string.progress_erasing));
+            mProgressDialog.show();
+        }
+
+        @Override
+        protected Void doInBackground(Void... params) {
+            Slog.w(TAG, "Wiping adoptable disks");
+            StorageManager sm = (StorageManager) mContext.getSystemService(
+                    Context.STORAGE_SERVICE);
+            sm.wipeAdoptableDisks();
+            return null;
+        }
+
+        @Override
+        protected void onPostExecute(Void result) {
+            mProgressDialog.dismiss();
+            mChainedTask.start();
+        }
+
     }
 }
index 491b412..5cfbb40 100644 (file)
@@ -85,6 +85,7 @@ import android.os.SystemClock;
 import android.os.SystemProperties;
 import android.os.UserHandle;
 import android.os.UserManager;
+import android.os.storage.StorageManager;
 import android.provider.ContactsContract.QuickContact;
 import android.provider.ContactsInternal;
 import android.provider.Settings;
@@ -108,7 +109,6 @@ import android.view.inputmethod.InputMethodInfo;
 import android.view.inputmethod.InputMethodManager;
 
 import com.android.internal.R;
-import com.android.internal.os.storage.ExternalStorageFormatter;
 import com.android.internal.statusbar.IStatusBarService;
 import com.android.internal.util.FastXmlSerializer;
 import com.android.internal.util.JournaledFile;
@@ -3307,25 +3307,15 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
     }
 
     private void wipeDataLocked(boolean wipeExtRequested, String reason) {
-        // TODO: wipe all public volumes on device
-
-        // If the SD card is encrypted and non-removable, we have to force a wipe.
-        boolean forceExtWipe = !Environment.isExternalStorageRemovable() && isExtStorageEncrypted();
-
-        // Note: we can only do the wipe via ExternalStorageFormatter if the volume is not emulated.
-        if ((forceExtWipe || wipeExtRequested) && !Environment.isExternalStorageEmulated()) {
-            Intent intent = new Intent(ExternalStorageFormatter.FORMAT_AND_FACTORY_RESET);
-            intent.putExtra(ExternalStorageFormatter.EXTRA_ALWAYS_RESET, true);
-            intent.putExtra(Intent.EXTRA_REASON, reason);
-            intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME);
-            mWakeLock.acquire(10000);
-            mContext.startService(intent);
-        } else {
-            try {
-                RecoverySystem.rebootWipeUserData(mContext, reason);
-            } catch (IOException | SecurityException e) {
-                Slog.w(LOG_TAG, "Failed requesting data wipe", e);
-            }
+        if (wipeExtRequested) {
+            StorageManager sm = (StorageManager) mContext.getSystemService(
+                    Context.STORAGE_SERVICE);
+            sm.wipeAdoptableDisks();
+        }
+        try {
+            RecoverySystem.rebootWipeUserData(mContext, reason);
+        } catch (IOException | SecurityException e) {
+            Slog.w(LOG_TAG, "Failed requesting data wipe", e);
         }
     }