关于恢复出厂设置的说明

原创 2016年06月01日 15:52:23

恢复出厂设置分为两种 ,要么清除sdcard么不清除。无论哪种都是通过发送广播的形式来执行这个操作。

private void doMasterClear() {
    if (mEraseSdCard) {
        Intent intent = new Intent(ExternalStorageFormatter.FORMAT_AND_FACTORY_RESET);
        intent.putExtra(Intent.EXTRA_REASON, "MasterClearConfirm");
        intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME);
        getActivity().startService(intent);
    } else {
        Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR);
        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        intent.putExtra(Intent.EXTRA_REASON, "MasterClearConfirm");
        getActivity().sendBroadcast(intent);
        // Intent handling is asynchronous -- assume it will happen soon.
    }

}

其中接收广播并执行操作的是com.android.server.MasterClearReceiver这个类。

public class MasterClearReceiver extends BroadcastReceiver {
private static final String TAG = “MasterClear”;

@Override
public void onReceive(final Context context, final Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_REMOTE_INTENT)) {
        if (!"google.com".equals(intent.getStringExtra("from"))) {
            Slog.w(TAG, "Ignoring master clear request -- not from trusted server.");
            return;
        }
    }

    final boolean shutdown = intent.getBooleanExtra("shutdown", false);
    final String reason = intent.getStringExtra(Intent.EXTRA_REASON);

    Slog.w(TAG, "!!! FACTORY RESET !!!");
    // The reboot call is blocking, so we need to do it on another thread.
    Thread thr = new Thread("Reboot") {
        @Override
        public void run() {
            try {
                RecoverySystem.rebootWipeUserData(context, shutdown, reason);
                Log.wtf(TAG, "Still running after master clear?!");
            } catch (IOException e) {
                Slog.e(TAG, "Can't perform master clear/factory reset", e);
            } catch (SecurityException e) {
                Slog.e(TAG, "Can't perform master clear/factory reset", e);
            }
        }
    };
    thr.start();
}

}

其中 RecoverySystem.rebootWipeUserData(context, shutdown, reason);做了实际清除userdata等等操作
中rebootWipeUserData 代码如下。

/**
 * Reboots the device and wipes the user data and cache
 * partitions.  This is sometimes called a "factory reset", which
 * is something of a misnomer because the system partition is not
 * restored to its factory state.  Requires the
 * {@link android.Manifest.permission#REBOOT} permission.
 *
 * @param context   the Context to use
 * @param shutdown  if true, the device will be powered down after
 *                  the wipe completes, rather than being rebooted
 *                  back to the regular system.
 *
 * @throws IOException  if writing the recovery command file
 * fails, or if the reboot itself fails.
 * @throws SecurityException if the current user is not allowed to wipe data.
 *
 * @hide
 */
public static void rebootWipeUserData(Context context, boolean shutdown, String reason)
        throws IOException {
    UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
    if (um.hasUserRestriction(UserManager.DISALLOW_FACTORY_RESET)) {
        throw new SecurityException("Wiping data is not allowed for this user.");
    }
    final ConditionVariable condition = new ConditionVariable();

    Intent intent = new Intent("android.intent.action.MASTER_CLEAR_NOTIFICATION");
    intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
    context.sendOrderedBroadcastAsUser(intent, UserHandle.OWNER,
            android.Manifest.permission.MASTER_CLEAR,
            new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    condition.open();
                }
            }, null, 0, null, null);

    // Block until the ordered broadcast has completed.
    condition.block();

    String shutdownArg = null;
    if (shutdown) {
        shutdownArg = "--shutdown_after";
    }

    String reasonArg = null;
    if (!TextUtils.isEmpty(reason)) {
        reasonArg = "--reason=" + sanitizeArg(reason);
    }

    final String localeArg = "--locale=" + Locale.getDefault().toString();
    bootCommand(context, shutdownArg, "--wipe_data", reasonArg, localeArg);

}

其中:
/**
* Reboot into the recovery system with the supplied argument.
* @param args to pass to the recovery utility.
* @throws IOException if something goes wrong.
*/
private static void bootCommand(Context context, String… args) throws IOException {
RECOVERY_DIR.mkdirs(); // In case we need it
COMMAND_FILE.delete(); // In case it’s not writable
LOG_FILE.delete();

    FileWriter command = new FileWriter(COMMAND_FILE);
    try {
        for (String arg : args) {
            if (!TextUtils.isEmpty(arg)) {
                command.write(arg);
                command.write("\n");
            }
        }
    } finally {
        command.close();
    }

    // Having written the command file, go ahead and reboot
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    pm.reboot(PowerManager.REBOOT_RECOVERY);

    throw new IOException("Reboot failed (no permissions?)");
}

关机,然后开机,底层判断节点后进入恢复出厂模式,recevory.img释放完全后,进入开机的流程。。。至于具体想知道系统做了什么可以查看源代码找一下后续逻辑和行为。
我这仅仅是发送广播。

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

android N 恢复出厂设置流程简析

前言: 恢复出厂设置的本质上是先写入command到BCB中,然后让手机进入recovery模式,此时会根据BCB中的命令来执行对应的操作,本文主要分析java层的流程。力有不逮,如有错漏,请多指...

系统重置

这个设置在android源码下的代码如下: if (mEraseSdCard) { Intent intent = new Intent(ExternalStorageFormatter.F...

Setting中格式化SD卡

今天分析setting中直接格式化的流程,直接上代码: Setting中的MediaFormat.java中的mFinalClickListener 这个回调,就是调用Format的地方:其是其了一...

Android 恢复出厂设置(recovery)

Android 恢复出厂设置基本流程 (1)遥控器/按键板后门键触发,或者应用里面从系统设置里面恢复出厂选项也可触发; // 后面以系统设置的应用触发为例 (2)选择恢复出厂设置之后,就会发送广播“a...

android 恢复出厂设置流程

在MasterClearConfirm.java中显示恢复出厂提示和对应button,点击button后调用button的 click方法  1. 如果选中erase sd card, 则star...

GM8804C2恢复出厂设置

  • 2015-09-05 13:09
  • 176KB
  • 下载

Android 恢复出厂设置流程分析

最近看恢复出厂的一个问题,以前也查过这方面的流程,所以这里整理一些AP+framework层的流程;       在setting-->备份与重置--->恢复出厂设置--->重置手机--->清除全部...

w101恢复出厂设置文件

  • 2012-10-28 17:47
  • 49.58MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)