Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d1858a7d authored by Rubin Xu's avatar Rubin Xu Committed by Android Git Automerger
Browse files

am 0a202eac: Merge "Use StorageManager.wipeAdoptableDisks to wipe external disks" into mnc-dev

* commit '0a202eac':
  Use StorageManager.wipeAdoptableDisks to wipe external disks
parents e02ee117 0a202eac
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -3784,6 +3784,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.
+4 −0
Original line number Diff line number Diff line
@@ -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";
+51 −1
Original line number Diff line number Diff line
@@ -16,12 +16,18 @@

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 {
                }
            }
        };

        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();
        }

    }
}
+10 −20
Original line number Diff line number Diff line
@@ -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,27 +3307,17 @@ 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 {
        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);
        }
    }
    }

    @Override
    public void wipeData(int flags, final int userHandle) {