android FactoryReset

private void doMasterClear() {
    Intent intent = new Intent(Intent.ACTION_FACTORY_RESET);
    intent.setPackage("android");
    intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
    intent.putExtra(Intent.EXTRA_REASON, "MasterClearConfirm");
    intent.putExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, true);
    intent.putExtra(Intent.EXTRA_WIPE_ESIMS, true);
    getActivity().sendBroadcast(intent);
    // Intent handling is asynchronous -- assume it will happen soon.
}

private ProgressDialog getProgressDialog() {
    final ProgressDialog progressDialog = new ProgressDialog(getActivity());
    progressDialog.setIndeterminate(true);
    progressDialog.setCancelable(false);
    progressDialog.setTitle(
            getActivity().getString(R.string.master_clear_progress_title));
    progressDialog.setMessage(
            getActivity().getString(R.string.master_clear_progress_text));
    return progressDialog;
}

private void test(){
    if (Utils.isMonkeyRunning()) {
        return;
    }

    final PersistentDataBlockManager pdbManager = (PersistentDataBlockManager)
            getActivity().getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
    final OemLockManager oemLockManager = (OemLockManager)
            getActivity().getSystemService(Context.OEM_LOCK_SERVICE);

    if (pdbManager != null && !oemLockManager.isOemUnlockAllowed() &&
            Utils.isDeviceProvisioned(getActivity())) {
        // if OEM unlock is allowed, the persistent data block will be wiped during FR
        // process. If disabled, it will be wiped here, unless the device is still being
        // provisioned, in which case the persistent data block will be preserved.
        new AsyncTask<Void, Void, Void>() {
            int mOldOrientation;
            ProgressDialog mProgressDialog;

            @Override
            protected Void doInBackground(Void... params) {
                pdbManager.wipe();
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                mProgressDialog.hide();
                if (getActivity() != null) {
                    getActivity().setRequestedOrientation(mOldOrientation);
                    doMasterClear();
                }
            }

            @Override
            protected void onPreExecute() {
                mProgressDialog = getProgressDialog();
                mProgressDialog.show();

                // need to prevent orientation changes as we're about to go into
                // a long IO request, so we won't be able to access inflate resources on flash
                mOldOrientation = getActivity().getRequestedOrientation();
                getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
            }
        }.execute();
    } else {
        doMasterClear();
    }
}

相关推荐