Android local upgrade principle and process (I. Upper Layer), android process

Source: Internet
Author: User

Android local upgrade principle and process (I. Upper Layer), android process
1. First, let's take a look at the steps to go to system update; settings --> about mobile phones --> system updates① On the settings page, go to the settings page for system updates. The DeviceInfoSettings. java class under the settings module. The preference is Device_info_settings.xml as follows:
<Com. android. settings. XXUpdatePreference android: key = "system_update_settings"
Android: title = "@ string/system_update_settings_list_item_title"
Android: summary = "@ string/system_update_settings_list_item_summary">
<Intent android: action = "android. settings. SYSTEM_UPDATE_SETTINGS"/>
</Com. android. settings. XXUpdatePreference>
② We listen to the preference in DeviceInfoSettings. java. When a user triggers a click event, the XXX application receives the broadcast and starts XXX for further processing. As follows:
Else if (preference. getKey (). equals (KEY_DMSW_UPDATE )){
/// M: for DMSW to broadcast @{
Intent I = new Intent ();
I. setAction ("com. mediatek. DMSWUPDATE ");
GetActivity (). sendBroadcast (I );
///@}
}
2. generally, due to the normal OS update experience of Android, the system update applications on our mobile phones are all customized by the manufacturer. That is to say, when we click system updates, the system update applications customized by the manufacturer are generally entered, we will not go into details here. In the subsequent blog, we will analyze the system update application in the native.① Here, we will skip this step. We will only select a local upgrade from the vendor-tailored application, and then enter the SystemUpgradeChooserActivity setting warning interface. java (do not remove the battery, SIM card, and memory card ......), Click OK to go to the upgrade reminder page (the upgrade will be completed in two to five minutes ......), Some code is as follows:
Case R. id. okButton: // whether the SDK card is mounted
If (! HasSDCard ()){
ShowToast (R. string. no_sd_card );
Return;
} // Whether the update package exists
If (file = null |! File. exists ()){
ShowToast (R. string. no_update_file );
Return;
} // Enter the upgrade reminder page
Intent intent = new Intent (SystemUpgradeChooserActivity. this, UpdateToStart. class );
StartActivity (intent );
Break;
② At this time, we enter the update reminder interface UpdateToStart. java through intent. Here we click OK to start installing the update package. The detailed code is as follows:
ButtonOkUp. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Try {// whether the SDK card is mounted
If (! HasSDCard ()){
ShowToast (R. string. no_sd_card );
Return;
} // Update whether the File exists file File = new File (Environment. getExternalStorageDirectory () + "/dload/update.zip ");
If (file = null |! File. exists ()){
ShowToast (R. string. no_update_file );
Return;
} // Install the update package, which is the key to the new upper-layer system
RecoverySystem. installPackage (getApplicationContext (), file );
}
Catch (Exception e ){
//
}
}
});
3. As we have learned above, install the update package with all your efforts by calling RecoverySystem. installPackage. The specific method is as follows:/**
* In order to install the update package, the device will be restarted. To call this method, you must add the REBOOT permission to the configuration file.
* Context
* The packageFile parameter encapsulates the file object for updating the package path and other information.
*/
Public static void installPackage (Context context, File packageFile)
Throws IOException {
String filename = packageFile. getCanonicalPath ();
Log. w (TAG ,"!!! Rebooting to install "+ filename + "!!! ");// Concatenate parameters. This parameter will eventually be written to BCB. That is to say, after the device is restarted and enters Recovery mode, Recovery. cpp will parse the parameter and perform operations based on the parameter. It will be passed into the bootcommand (context, arg) function.String arg = "-- update_package =" + filename +
"\ N -- locale =" + Locale. getDefault (). toString ();

BootCommand (context, arg );
}

4. Analyze the bootCommand.
Private static void bootCommand (Context context, String arg) throws IOException {
RECOVERY_DIR. Mkdirs (); // create/cache/recovery directory
COMMAND_FILE. Delete (); // delete the command file and initialize it.
LOG_FILE. Delete (); // delete the log file and initialize it.

Log. d (TAG, "Preapre to write command:" + arg + "\ n ");
WriteByFdSync(COMMAND_FILE, arg); // write parameters to the command file.
Log. d (TAG, "Success to write command:" + arg + "\ n ");

Log. d (TAG, "Current build type is:" + Build. TYPE + "\ n ");
ReadInAndPrint(COMMAND_FILE); // read the command file and print the log

// Having written the command file, go ahead and reboot
PowerManager pm = (PowerManager) context. getSystemService (Context. POWER_SERVICE); // obtain the PowerManager instance from the System Service
Pm.Reboot("Recovery"); // restart
Log. d (TAG ,"!!! Pm. reboot failed !!! \ N ");
Throw new IOException ("Reboot failed (no permissions ?) ");
}

The following method call process is PowerManager: reboot ("recovery") --> PowerManagerService: reboot (reason)
--> PowerManagerService: shtudownOrRebootInternal (false, confirm, reason, wait)
--> PowerManagerService: shutdownOrRebootInternal (...)
--> ShutdownThread: reboot (mContext, reason, confirm );
--> ShutdownThread: run (): running ()......


@ Override // Binder call
Public voidReboot(Boolean confirm, String reason, boolean wait ){
MContext. enforceCallingOrSelfPermission (android. Manifest. permission. REBOOT, null );

Slog. I (TAG, "reboot call pid:" + Binder. getCallingPid () + "uid:" + Binder. getCallingUid ());
Final long ident = Binder. clearCallingIdentity ();
Try {
ShutdownOrRebootInternal (false, confirm, reason, wait );
} Finally {
Binder. restoreCallingIdentity (ident );
}
}


Private voidShutdownOrRebootInternal(Final boolean shutdown, final boolean confirm,
Final String reason, boolean wait ){
If (mHandler = null |! MSystemReady ){
Throw new IllegalStateException ("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 );
}
}
}
};

// ShutdownThread must run on a loopcapable of displaying the UI.
Message msg = Message. obtain (mHandler, runnable );
Msg. setAsynchronous (true );
MHandler. sendMessage (msg );

// PowerManager. reboot () is already ented not to return so just wait for the inevitable.
If (wait ){
Synchronized (runnable ){
While (true ){
Try {
Runnable. wait ();
} Catch (InterruptedException e ){
}
}
}
}
}

Public static voidReboot(Final Context context, String reason, boolean confirm ){
MReboot = true;
MRebootSafeMode = false;
MRebootReason = reason;
Log. d (TAG, "reboot ");

If (mSpew ){
StackTraceElement [] stack = new Throwable (). getStackTrace ();
For (StackTraceElement element: stack)
{
Log. d (TAG, "| ----" + element. toString ());
}
}

ShutdownInner (context, confirm );
}

In the run method, the running method writes the cause of restart to the system attribute file.
......
{
String reason = (mReboot? "1": "0") + (mRebootReason! = Null? MRebootReason :"");
SystemProperties. set (SHUTDOWN_ACTION_PROPERTY, reason );
}
......
Finally, restart the device.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.