现在的位置: 首页 > 综合 > 正文

android4.2 recovery&reset_factory

2013年01月31日 ⁄ 综合 ⁄ 共 16566字 ⁄ 字号 评论关闭

settings中,恢复出场设置

 

1:frameworks/base/core/java/android/os/RecoverySystem.java

 

public static voidrebootWipeUserData(Context context) throws IOException {

 

       final ConditionVariable condition = new ConditionVariable();

 

 

 

       Intent intent = newIntent("android.intent.action.MASTER_CLEAR_NOTIFICATION");

 

       context.sendOrderedBroadcastAsUser(intent, UserHandle.OWNER,

 

                android.Manifest.permission.MASTER_CLEAR,

 

                new BroadcastReceiver() {

 

                    @Override

 

                    public voidonReceive(Context context, Intent intent) {

 

                        condition.open();

 

                    }

 

                }, null, 0, null, null);

 

 

 

       // Block until the ordered broadcast has completed.

 

       condition.block();

 

 

 

       Log.d(TAG,"++++sam debug settints backup&reset 111111  data--------- ");

 

       bootCommand(context,"--wipe_data\n--locale=" + Locale.getDefault().toString());

 

   }

 

 

 

 

frameworks/base/services/java/com/android/server/MasterClearReceiver.java

public class MasterClearReceiverextends 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, "Ignoringmaster clear request -- not from trusted server.");

 

                return;

 

            }

 

       }

 

 

 

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

 

                    Log.wtf(TAG, "Stillrunning after master clear?!");

 

                } catch (IOException e) {

 

                    Slog.e(TAG, "Can'tperform master clear/factory reset", e);

 

                }

 

            }

 

       };

 

       thr.start();

 

   }

 

}

 

~                              

 

 

 

 

 

////

 

 

 

2: private static void bootCommand(Context context, String arg) throwsIOException {

 

       RECOVERY_DIR.mkdirs();  // In casewe need it

 

       COMMAND_FILE.delete();  // In caseit's not writable

 

       LOG_FILE.delete();

 

 

 

       FileWriter command = new FileWriter(COMMAND_FILE);

 

       try {

 

            command.write(arg);

 

            command.write("\n");

 

       } finally {

 

            command.close();

 

       }

 

 

 

       Log.d(TAG,"++++sam debug settints backup&reset erasedata--------------------- ");

 

 

 

 

 

       // Having written the command file, go ahead and reboot

 

       PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);

 

       pm.reboot("recovery");

 

 

 

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

 

   }

 

 

 

////  /frameworks/base/core/java/android/os/PowerManager.java

 

 

 

 

 

 /**

 

    * Reboot the device.  Will notreturn if the reboot is successful.

 

    * <p>

 

    * Requires the {@link android.Manifest.permission#REBOOT} permission.

 

    * </p>

 

    *

 

    * @param reason code to pass to the kernel (e.g., "recovery")to

 

    *               request specialboot modes, or null.

 

    */

 

   public void reboot(String reason) {

 

       try {

 

            mService.reboot(false,reason, true);

 

       } catch (RemoteException e) {

 

       }

 

   }

 

 

 

////

 

3:mService.reboot-----/base/core/java/android/os/IPowerManager.aidl()////

 

                 -----base/services/java/com/android/server/power/PowerManagerService.java

 

 

 

 /**

 

    * Reboots the device.

 

    *

 

    * @param confirm If true, shows a reboot confirmation dialog.

 

    * @param reason The reason for the reboot, or null if none.

 

    * @param wait If true, this call waits for the reboot to complete anddoes not return.

 

    */

 

   @Override // Binder call

 

   public void reboot(boolean confirm, String reason, boolean wait) {

 

       mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT,null);

 

 

 

       final long ident = Binder.clearCallingIdentity();

 

       try {

 

            shutdownOrRebootInternal(false, confirm,reason, wait);

 

       } finally {

 

            Binder.restoreCallingIdentity(ident);

 

       }

 

   }

 

 

 

 private void shutdownOrRebootInternal(final boolean shutdown, finalboolean confirm,

 

            final String reason, boolean wait){

 

       if (mHandler == null || !mSystemReady) {

 

            throw newIllegalStateException("Too early to call shutdown() or reboot()");

 

       }

 

 

 

       Runnable runnable = new Runnable() {

 

           @Override

 

            public void run() {

 

                synchronized (this) {

 

                    if (shutdown) {

 

                        ShutdownThread.shutdown(mContext, confirm);

 

                    } else {

 

                        ShutdownThread.reboot(mContext,reason, confirm);

 

                    }

 

                }

 

            }

 

       };

 

                  

 

4:./base/services/java/com/android/server/power/ShutdownThread.java

 

   /**

 

    * Request a clean shutdown, waiting for subsystems to clean up their

 

    * state etc.  Must be called froma Looper thread in which its UI

 

    * is shown.

 

    *

 

    * @param context Context used to display the shutdown progress dialog.

 

    * @param reason code to pass to the kernel (e.g. "recovery"),or null.

 

    * @param confirm true if user confirmation is needed before shuttingdown.

 

    */

 

   public static void reboot(final Context context, String reason, booleanconfirm) {

 

       mReboot = true;

 

       mRebootSafeMode = false;

 

       mRebootReason = reason;

 

       shutdownInner(context,confirm);

 

   }

 

 

 

5: 由shutdownInner------

 

static void shutdownInner(finalContext context, boolean confirm) {

 

       // ensure that only one thread is trying to power down.

 

       // any additional calls arejust returned

 

       synchronized (sIsStartedGuard) {

 

            if (sIsStarted) {

 

                Log.d(TAG, "Request toshutdown already running, returning.");

 

                return;

 

            }

 

       }

 

 

 

       final int longPressBehavior =context.getResources().getInteger(

 

                        com.android.internal.R.integer.config_longPressOnPowerBehavior);

 

       final int resourceId = mRebootSafeMode

 

                ?com.android.internal.R.string.reboot_safemode_confirm

 

                : (longPressBehavior == 2

 

                        ?com.android.internal.R.string.shutdown_confirm_question

 

                        :com.android.internal.R.string.shutdown_confirm);

 

 

 

       Log.d(TAG, "Notifying thread to start shutdownlongPressBehavior=" + longPressBehavior);

 

 

 

       if (confirm) {

 

            final CloseDialogReceiver closer =new CloseDialogReceiver(context);

 

            if (sConfirmDialog != null) {

 

                sConfirmDialog.dismiss();

 

            }

 

            sConfirmDialog = newAlertDialog.Builder(context)

 

                    .setTitle(mRebootSafeMode

 

                            ?com.android.internal.R.string.reboot_safemode_title

 

                            : com.android.internal.R.string.power_off)

 

                    .setMessage(resourceId)

 

                    .setPositiveButton(com.android.internal.R.string.yes,new DialogInterface.OnClickListener() {

 

                        public voidonClick(DialogInterface dialog, int which) {

 

                               Log.d(TAG,"++++SAMDEBUG BBBBBBBBBBBBBBBBBBBB");

 

              beginShutdownSequence(context);

 

                        }

 

                    })

 

                    .setNegativeButton(com.android.internal.R.string.no,null)

 

                    .create();

 

            closer.dialog = sConfirmDialog;

 

            sConfirmDialog.setOnDismissListener(closer);

 

            sConfirmDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);

 

            sConfirmDialog.show();

 

       } else {

 

        Log.d(TAG,"++++SAM DEBUGccccccccccccccccccccccccccccccccccccccccc");

 

            beginShutdownSequence(context);

 

       }

 

   }

 

 

 

 

 

 

 

6: private static voidbeginShutdownSequence(Context context) {

 

       synchronized(sIsStartedGuard) {

 

            if (sIsStarted) {

 

                Log.d(TAG, "Shutdownsequence already running, returning.");

 

                return;

 

            }

 

            sIsStarted = true;

 

       }

 

 

 

       // throw up an indeterminate system dialog to indicate radio is

 

       // shutting down.

 

       ProgressDialog pd = new ProgressDialog(context);

 

       pd.setTitle(context.getText(com.android.internal.R.string.power_off));

 

       pd.setMessage(context.getText(com.android.internal.R.string.shutdown_progress));

 

       pd.setIndeterminate(true);

 

       pd.setCancelable(false);

 

       pd.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);

 

 

 

       pd.show();

 

 

 

       sInstance.mContext = context;

 

       sInstance.mPowerManager =(PowerManager)context.getSystemService(Context.POWER_SERVICE);

 

 

 

       // make sure we never fall asleep again

 

       sInstance.mCpuWakeLock = null;

 

       try {

 

            sInstance.mCpuWakeLock = sInstance.mPowerManager.newWakeLock(

 

                    PowerManager.PARTIAL_WAKE_LOCK,TAG + "-cpu");

 

            sInstance.mCpuWakeLock.setReferenceCounted(false);

 

            sInstance.mCpuWakeLock.acquire();

 

       } catch (SecurityException e) {

 

           Log.w(TAG, "Nopermission to acquire wake lock", e);

 

            sInstance.mCpuWakeLock = null;

 

       }

 

 

 

       // also make sure the screen stays on for better user experience

 

       sInstance.mScreenWakeLock = null;

 

       if (sInstance.mPowerManager.isScreenOn()) {

 

            try {

 

                sInstance.mScreenWakeLock =sInstance.mPowerManager.newWakeLock(

 

                        PowerManager.FULL_WAKE_LOCK,TAG + "-screen");

 

                sInstance.mScreenWakeLock.setReferenceCounted(false);

 

                sInstance.mScreenWakeLock.acquire();

 

            } catch (SecurityException e) {

 

                Log.w(TAG, "No permissionto acquire wake lock", e);

 

                sInstance.mScreenWakeLock =null;

 

            }

 

        }

 

 

 

       // start the thread that initiates shutdown

 

       sInstance.mHandler = new Handler() {

 

       };

 

    Log.d(TAG,"++++SAMDEBUG AAAAAAAAAAAAAAAAAAAAAAAAAAA");

 

       sInstance.start();

 

   }

 

 

 

   void actionDone() {

 

       synchronized (mActionDoneSync) {

 

            mActionDone = true;

 

            mActionDoneSync.notifyAll();

 

       }

 

   }

 

 

 

   /**

 

    * Makes sure we handle the shutdown gracefully.

 

    * Shuts off power regardless of radio and bluetooth state if the allotedtime has passed.

 

    */

 

   public void run() {

 

       BroadcastReceiver br = new BroadcastReceiver() {

 

            @Override public voidonReceive(Context context, Intent intent) {

 

                // We don't allow apps tocancel this, so ignore the result.

 

                actionDone();

 

            }

 

       };

 

 

 

    Log.d(TAG,"++++samdebug run 11111111111111111111111-----");

 

 

 

       /*

 

         * Write a system property in case thesystem_server reboots before we

 

         * get to the actual hardware restart.If that happens, we'll retry at

 

         * the beginning of the SystemServerstartup.

 

         */

 

       {

 

            String reason = (mReboot ?"1" : "0") + (mRebootReason != null ? mRebootReason :"");

 

            SystemProperties.set(SHUTDOWN_ACTION_PROPERTY,reason);

 

       }

 

 

 

       /*

 

         * If we are rebooting into safe mode,write a system property

 

         * indicating so.

 

         */

 

       if (mRebootSafeMode) {

 

            SystemProperties.set(REBOOT_SAFEMODE_PROPERTY,"1");

 

       }

 

 

 

       Log.i(TAG, "Sending shutdown broadcast...");

 

       

 

       // First send the high-level shut down broadcast.

 

       mActionDone = false;

 

       mContext.sendOrderedBroadcastAsUser(new Intent(Intent.ACTION_SHUTDOWN),

 

                UserHandle.ALL, null, br,mHandler, 0, null, null);

 

       

 

       final long endTime = SystemClock.elapsedRealtime() + MAX_BROADCAST_TIME;

 

       synchronized (mActionDoneSync) {

 

            while (!mActionDone) {

 

                long delay = endTime -SystemClock.elapsedRealtime();

 

                if (delay <= 0) {

 

                    Log.w(TAG, "Shutdownbroadcast timed out");

 

                    break;

 

                }

 

                try {

 

                    mActionDoneSync.wait(delay);

 

                } catch (InterruptedExceptione) {

 

                }

 

            }

 

       }

 

       

 

       Log.i(TAG, "Shutting down activity manager...");

 

       

 

       final IActivityManager am =

 

           ActivityManagerNative.asInterface(ServiceManager.checkService("activity"));

 

       if (am != null) {

 

            try {

 

                am.shutdown(MAX_BROADCAST_TIME);

 

            } catch (RemoteException e) {

 

            }

 

       }

 

 

 

       // Shutdown radios.

 

       shutdownRadios(MAX_RADIO_WAIT_TIME);

 

 

 

       // Shutdown MountService to ensure media is in a safe state

 

       IMountShutdownObserver observer = new IMountShutdownObserver.Stub() {

 

            public void onShutDownComplete(intstatusCode) throws RemoteException {

 

                Log.w(TAG, "Result code" + statusCode + " from MountService.shutdown");

 

                actionDone();

 

            }

 

       };

 

 

 

       Log.i(TAG, "Shutting down MountService");

 

 

 

       // Set initial variables andtime out time.

 

       mActionDone = false;

 

       final long endShutTime = SystemClock.elapsedRealtime() +MAX_SHUTDOWN_WAIT_TIME;

 

       synchronized (mActionDoneSync) {

 

            try {

 

                final IMountService mount =IMountService.Stub.asInterface(

 

                        ServiceManager.checkService("mount"));

 

                if (mount != null) {

 

                    mount.shutdown(observer);

 

                } else {

 

                    Log.w(TAG,"MountService unavailable for shutdown");

 

                }

 

            } catch (Exception e) {

 

                Log.e(TAG, "Exceptionduring MountService shutdown", e);

 

            }

 

            while (!mActionDone) {

 

                long delay = endShutTime -SystemClock.elapsedRealtime();

 

                if (delay <= 0) {

 

                    Log.w(TAG, "Shutdownwait timed out");

 

                    break;

 

                }

 

                try {

 

                    mActionDoneSync.wait(delay);

 

                } catch (InterruptedExceptione) {

 

                }

 

            }

 

       }

 

 

 

       rebootOrShutdown(mReboot,mRebootReason);

 

   }

 

 

 

 

 

 

 

 

 

5:mRebootReason----

 

rebootOrShutdown(mReboot,mRebootReason);---

 

 

 

   /**

 

    * Do not call this directly. Use {@link #reboot(Context, String,boolean)}

 

    * or {@link #shutdown(Context, boolean)} instead.

 

    *

 

    * @param reboot true to reboot or false to shutdown

 

    * @param reason reason for reboot

 

    */

 

   public static void rebootOrShutdown(boolean reboot, String reason) {

 

       if (reboot) {

 

            Log.i(TAG, "Rebooting, reason:" + reason);

 

            try {

 

                PowerManagerService.lowLevelReboot(reason);

 

            } catch (Exception e) {

 

                Log.e(TAG, "Reboot failed,will attempt shutdown instead", e);

 

            }

 

       } else if (SHUTDOWN_VIBRATE_MS > 0) {

 

            // vibrate before shutting down

 

            Vibrator vibrator = newSystemVibrator();

 

            try {

 

                vibrator.vibrate(SHUTDOWN_VIBRATE_MS);

 

            } catch (Exception e) {

 

                // Failure to vibrate shouldn'tinterrupt shutdown.  Just log it.

 

                Log.w(TAG, "Failed tovibrate during shutdown.", e);

 

           }

 

 

 

            // vibrator is asynchronous so weneed to wait to avoid shutting down too soon.

 

            try {

 

                Thread.sleep(SHUTDOWN_VIBRATE_MS);

 

            } catch (InterruptedExceptionunused) {

 

            }

 

       }

 

 

 

       // Shutdown power

 

       Log.i(TAG, "Performing low-level shutdown...");

 

       PowerManagerService.lowLevelShutdown();

 

   }

 

}

 

 

 

PowerManagerService.lowLevelReboot(reason);

 

 

 

 

 

 PowerManagerService.lowLevelShutdown();

 

 

 

 

 

6:  

 

   /**

 

    * Low-level function turn the device off immediately, without trying

 

    * to be clean.  Most people shoulduse {@link ShutdownThread} for a clean shutdown.

 

    */

 

   public static void lowLevelShutdown() {

 

       nativeShutdown();

 

   }

 

 

 

   /**

 

    * Low-level function to reboot the device.

 

    *

 

    * @param reason code to pass to the kernel (e.g. "recovery"),or null.

 

    * @throws IOException if reboot fails for some reason (eg, lack of

 

    *         permission)

 

    */

 

    publicstatic void lowLevelReboot(String reason) throws IOException {

 

       nativeReboot(reason);

 

   }

 

 

 

7:frameworks/base/services/jni/com_android_server_power_PowerManagerService.cpp

 

static void nativeShutdown(JNIEnv*env, jclass clazz) {

 

   android_reboot(ANDROID_RB_POWEROFF, 0, 0);

 

}

 

 

 

static void nativeReboot(JNIEnv *env,jclass clazz, jstring reason) {

 

   if (reason == NULL) {

 

       android_reboot(ANDROID_RB_RESTART, 0, 0);

 

   } else {

 

       const char *chars = env->GetStringUTFChars(reason, NULL);

 

       android_reboot(ANDROID_RB_RESTART2,0, (char *) chars);

 

       env->ReleaseStringUTFChars(reason, chars);  // In case it fails.

 

   }

 

   jniThrowIOException(env, errno);

 

}

 

 

 

8:system/core/libcutils/android_reboot.c

 

int android_reboot(int cmd, int flags,char *arg)

{

   int ret;

 

 

   if (!(flags & ANDROID_RB_FLAG_NO_SYNC))

       sync();

 

 

   if (!(flags & ANDROID_RB_FLAG_NO_REMOUNT_RO))

       remount_ro();

 

 

   switch (cmd) {

       caseANDROID_RB_RESTART:

            ret = reboot(RB_AUTOBOOT);

            break;

 

 

       case ANDROID_RB_POWEROFF:

            ret = reboot(RB_POWER_OFF);

            break;

 

 

       caseANDROID_RB_RESTART2:

            ret = __reboot(LINUX_REBOOT_MAGIC1,LINUX_REBOOT_MAGIC2,

                           LINUX_REBOOT_CMD_RESTART2,arg);

            break;

 

 

       default:

            ret = -1;

   }

 

 

   return ret;

}

 

 

 

 

 

 

 

 

 

 

 

抱歉!评论已关闭.