继续分析 twrp ui.xml中的相关内容,以<page name="reboot">为讲解内容
<object type="button"> <highlight color="%highlight_color%" /> <condition var1="tw_reboot_system" var2="1" /> <placement x="%col1_x%" y="%row1_y%" /> <font resource="font" color="%button_text_color%" /> <text>System</text> <image resource="main_button" /> <actions> <action function="set">tw_back=reboot</action> <action function="set">tw_action=reboot</action> <action function="set">tw_action_param=system</action> <action function="set">tw_has_action2=0</action> <action function="set">tw_text1=No OS Installed! Are you</action> <action function="set">tw_text2=sure you wish to reboot?</action> <action function="set">tw_action_text1=Rebooting...</action> <action function="set">tw_complete_text1=Rebooting...</action> <action function="set">tw_slider_text=Swipe to Reboot</action> <action function="page">rebootcheck</action> </actions> </object> <object type="button"> <highlight color="%highlight_color%" /> <condition var1="tw_reboot_poweroff" var2="1" /> <placement x="%col2_x%" y="%row1_y%" /> <font resource="font" color="%button_text_color%" /> <text>Power Off</text> <image resource="main_button" /> <actions> <action function="set">tw_back=reboot</action> <action function="set">tw_action=reboot</action> <action function="set">tw_action_param=poweroff</action> <action function="set">tw_has_action2=0</action> <action function="set">tw_text1=No OS Installed! Are you</action> <action function="set">tw_text2=sure you wish to power off?</action> <action function="set">tw_action_text1=Turning Off...</action> <action function="set">tw_complete_text1=Turning Off...</action> <action function="set">tw_slider_text=Swipe to Power Off</action> <action function="page">rebootcheck</action> </actions> </object> <object type="button"> <highlight color="%highlight_color%" /> <condition var1="tw_reboot_recovery" var2="1" /> <placement x="%col1_x%" y="%row2_y%" /> <font resource="font" color="%button_text_color%" /> <text>Recovery</text> <image resource="main_button" /> <actions> <action function="set">tw_back=reboot</action> <action function="set">tw_action=reboot</action> <action function="set">tw_action_param=recovery</action> <action function="set">tw_has_action2=0</action> <action function="set">tw_text1=No OS Installed! Are you</action> <action function="set">tw_text2=sure you wish to reboot?</action> <action function="set">tw_action_text1=Rebooting...</action> <action function="set">tw_complete_text1=Rebooting...</action> <action function="set">tw_slider_text=Swipe to Reboot</action> <action function="page">rebootcheck</action> </actions> </object> <object type="button"> <highlight color="%highlight_color%" /> <condition var1="tw_reboot_bootloader" var2="1" /> <placement x="%col2_x%" y="%row2_y%" /> <font resource="font" color="%button_text_color%" /> <text>Bootloader</text> <image resource="main_button" /> <actions> <action function="set">tw_back=reboot</action> <action function="set">tw_action=reboot</action> <action function="set">tw_action_param=bootloader</action> <action function="set">tw_has_action2=0</action> <action function="set">tw_text1=No OS Installed! Are you</action> <action function="set">tw_text2=sure you wish to reboot?</action> <action function="set">tw_action_text1=Rebooting...</action> <action function="set">tw_complete_text1=Rebooting...</action> <action function="set">tw_slider_text=Swipe to Reboot</action> <action function="page">rebootcheck</action> </actions> </object>
tw_actoin=reboot传入的是命令,tw_action_param=system 传入的是参数
在gui/actions.cpp 中有如下的函数定义
1 int GUIAction::doAction(Action action, int isThreaded /* = 0 */) 2 { 3 static string zip_queue[10]; 4 static int zip_queue_index; 5 static pthread_t terminal_command; 6 int simulate; 7 8 std::string arg = gui_parse_text(action.mArg); //这里获取tw_action=的参数即tw_action_param传入的参数 9 10 std::string function = gui_parse_text(action.mFunction); //获取tw_action传入的函数名字 11 12 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate); 13 14 if (function == "reboot") 15 { 16 //curtainClose(); this sometimes causes a crash 17 18 sync(); 19 DataManager::SetValue("tw_gui_done", 1); 20 DataManager::SetValue("tw_reboot_arg", arg); //把参数传入到,其中tw_reboot_arg的类型是 map<string, string> 21 22 return 0; 23 }
...
好了,再看别外一个文件:
1 // reboot: Reboot the system. Return -1 on error, no return on success 2 int TWFunc::tw_reboot(RebootCommand command) //RebootCommand 中一个enum类型,定义在twrp-functions.hpp中 3 { 4 // Always force a sync before we reboot 5 sync(); 6 7 switch (command) { 8 case rb_current: 9 case rb_system: 10 Update_Log_File(); 11 Update_Intent_File("s"); 12 sync(); 13 check_and_run_script("/sbin/rebootsystem.sh", "reboot system"); 14 return reboot(RB_AUTOBOOT); 15 case rb_recovery: 16 check_and_run_script("/sbin/rebootrecovery.sh", "reboot recovery"); 17 return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "recovery"); 18 case rb_bootloader: 19 check_and_run_script("/sbin/rebootbootloader.sh", "reboot bootloader"); 20 return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "bootloader"); 21 case rb_poweroff: 22 check_and_run_script("/sbin/poweroff.sh", "power off"); 23 #ifdef ANDROID_RB_POWEROFF 24 android_reboot(ANDROID_RB_POWEROFF, 0, 0); 25 #endif 26 return reboot(RB_POWER_OFF); 27 case rb_download: 28 check_and_run_script("/sbin/rebootdownload.sh", "reboot download"); 29 return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "download"); 30 default: 31 return -1; 32 } 33 return -1; 34 }
RebootCommand类型定义如下:
typedef enum { rb_recovery, rb_poweroff, rb_bootloader, rb_download, } RebootCommand;
触发重启进入什么类型的调用在twrp.cpp::main()函数中
定义如下:
... // Reboot TWFunc::Update_Intent_File(Reboot_Value); TWFunc::Update_Log_File(); gui_print("Rebooting... "); string Reboot_Arg; DataManager::GetValue("tw_reboot_arg", Reboot_Arg); //这里获取tw_reboot_arg中的变量 if (Reboot_Arg == "recovery") TWFunc::tw_reboot(rb_recovery); //传入enum变量,触发重启进入recovery动作 else if (Reboot_Arg == "poweroff") TWFunc::tw_reboot(rb_poweroff); else if (Reboot_Arg == "bootloader") TWFunc::tw_reboot(rb_bootloader); else if (Reboot_Arg == "download") TWFunc::tw_reboot(rb_download); else TWFunc::tw_reboot(rb_system); ...