Android RecoverSystem 类
来源:互联网 发布:linux cp 排除文件夹 编辑:程序博客网 时间:2024/04/30 09:44
昨天开会讨论IP网络升级时,需要一个后台服务做升级检测及下载数据,最后在定义下载的数据存放位置及如何做开机标志时,突然发现一个很好用的上层java类对象。
frameworks\base\core\java\android\os\RecoverySystem.java
那么这个类做了什么事情呢?又是如何做到的。
static void installPackage(Context context, File packageFile) //重启设备,安装一个更新包
下面具体看一下代码中是如何实现:
1、安装更新包:
2、出厂恢复
3、验证签名
public static void verifyPackage(File packageFile,
ProgressListener listener,
File deviceCertsZipFile) 请自行阅读源码
reboot 调用 android_reboot(ANDROID_RB_RESTART2, 0, (char *) chars);
最终都是调用到下面函数:
int reboot (int mode)
{
return __reboot( LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, mode, NULL );
}
=====linux kernel 内核流程=====
/bionic/libc/arch-arm/syscalls/__reboot.S
这个文件由gensyscalls.py自动产生,调用系统调用 __NR_reboot
bionic/libc/include/sys/linux-syscalls.h
#define __NR_reboot (__NR_SYSCALL_BASE+88)
/arch/arm/kernel/calls.S
__NR_reboot?就是系统函数sys_reboot
sys_reboot定义在./include/linux/syscalls.h,实现在./kernel/sys.c?sys_reboot会调用kernel_restart
最终kernel_restart调用每一个架构特定的machine_restart,即machine_restart执行每个架构特定的函数
frameworks\base\core\java\android\os\RecoverySystem.java
那么这个类做了什么事情呢?又是如何做到的。
RecoverySystem类,可以帮助我们调用系统还原等操作:
static void installPackage(Context context, File packageFile) //重启设备,安装一个更新包
static void rebootWipeUserData(Context context) //重启设备,清除用户数据分区类似恢复出厂设置
static String handleAftermath() 提供清除recover中相关文件,在开机广播中被调用
static void verifyPackage(File packageFile, RecoverySystem.ProgressListener listener, File deviceCertsZipFile) //验证加密签名的系统更新包在安装前,其中第二个数接口的具体定义为 android.os.RecoverySystem.ProgressListener 其中只有一个回调方法 abstract void onProgress(int progress) 来显示效验的进度。下面具体看一下代码中是如何实现:
1、安装更新包:
/** * Reboots the device in order to install the given update * package. * Requires the {@link android.Manifest.permission#REBOOT} permission. * * @param context the Context to use * @param packageFile the update package to install. Must be on * a partition mountable by recovery. (The set of partitions * known to recovery may vary from device to device. Generally, * /cache and /data are safe.) * * @throws IOException if writing the recovery command file * fails, or if the reboot itself fails. */ public static void installPackage(Context context, File packageFile) throws IOException { String filename = packageFile.getCanonicalPath(); Log.w(TAG, "!!! REBOOTING TO INSTALL " + filename + " !!!"); String arg = "--update_package=" + filename; bootCommand(context, arg); // 都是调用了这个函数 } /** * Reboot into the recovery system with the supplied argument. * @param arg to pass to the recovery utility. * @throws IOException if something goes wrong. */ private static void bootCommand(Context context, String arg) 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); // 写命令写入到recover中 try { 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("recovery"); throw new IOException("Reboot failed (no permissions?)"); }
2、出厂恢复
/** * Reboots the device and wipes the user data partition. 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 * * @throws IOException if writing the recovery command file * fails, or if the reboot itself fails. */ public static void rebootWipeUserData(Context context) throws IOException { final ConditionVariable condition = new ConditionVariable(); Intent intent = new Intent("android.intent.action.MASTER_CLEAR_NOTIFICATION"); context.sendOrderedBroadcast(intent, 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(); bootCommand(context, "--wipe_data"); } /** * Reboot into the recovery system to wipe the /cache partition. * @throws IOException if something goes wrong. */ public static void rebootWipeCache(Context context) throws IOException { bootCommand(context, "--wipe_cache"); }
3、验证签名
public static void verifyPackage(File packageFile,
ProgressListener listener,
File deviceCertsZipFile) 请自行阅读源码
后面说一下重启 reboot 命令如何执行的:
无论是 factory reset 工厂恢复还是 安装更新包都会调用到reboot函数,调用 pm.reboot("recovery");
通到jni就是: android_os_Power.cpp
reboot 调用 android_reboot(ANDROID_RB_RESTART2, 0, (char *) chars);
最终都是调用到下面函数:
int reboot (int mode)
{
return __reboot( LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, mode, NULL );
}
=====linux kernel 内核流程=====
/bionic/libc/arch-arm/syscalls/__reboot.S
这个文件由gensyscalls.py自动产生,调用系统调用 __NR_reboot
bionic/libc/include/sys/linux-syscalls.h
#define __NR_reboot (__NR_SYSCALL_BASE+88)
/arch/arm/kernel/calls.S
__NR_reboot?就是系统函数sys_reboot
sys_reboot定义在./include/linux/syscalls.h,实现在./kernel/sys.c?sys_reboot会调用kernel_restart
最终kernel_restart调用每一个架构特定的machine_restart,即machine_restart执行每个架构特定的函数
- Android RecoverSystem 类
- Android RecoverSystem 类
- android类
- [Android工具类]Android File工具类
- Android中的android.app.Application类
- [android] android源码中的 PduParser 类详解。
- [android] android framework中的 PhoneNumberUtils 类详解
- Android Log架构/Android Log工具类
- (Android design)新闻资讯类android客户端开源拉
- (Android design)新闻资讯类android客户端开源拉
- [Android UI开发] Android常用工具类
- [Android教程] android的 Http工具类
- 【Android】 Android之API类库整理
- Android类库介绍
- Android Activity类详解
- Android类库介绍
- Android activity类
- Android activity类 【转】
- CentOS安装thrift
- 在Eclipse中配置tomcat
- hadoop全套免费视频
- arm架构mmu
- 如何向App Store提交应用
- Android RecoverSystem 类
- linux gdb 调试多线程 多进程 显示宏
- 51单片机模拟电梯汇编程序
- 视频监控分辨率CIF、DCIF、D1格式的简介
- word中公式编辑器编辑的公式显示不全,如何解决?(除增大行间距的方法)
- 【JAVA】java使用
- Linux iptables 增删改查
- 33. 面向对象的LotusScript(六)之为自定义对象模拟事件
- CAS配置(一)-集成RESTFul