Android关机流程

今天研究学习了Android的关机流程。

大家都知道,当长按power按键时,手机就会弹出一个对话框,从对话框中我们可以选择“关机”选项。此功能是在PhoneWindowManager.java中实现完成(frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java),在此文件中有interceptKeyBeforeQueueing方法用于接收按键事件,然后对各个不同的事件做出相应的操作,其中power按键的操作如下:

[java]  view plain copy
  1. public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags, boolean isScreenOn) {  
  2.     ......  
  3.     switch (keyCode) {  
  4.     ......  
  5.     case KeyEvent.KEYCODE_POWER: {  
  6.         if ((mTopFullscreenOpaqueWindowState.getAttrs().flags & WindowManager.LayoutParams.PREVENT_POWER_KEY) != 0){  
  7.             return result;  
  8.         }  
  9.         result &= ~ACTION_PASS_TO_USER;  
  10.         if (down) { // 如果是按下键  
  11.         if (isScreenOn && !mPowerKeyTriggered  
  12.             && (event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0) {  
  13.             mPowerKeyTriggered = true// 按键触发为true  
  14.             mPowerKeyTime = event.getDownTime(); // 记录事件时间  
  15.             cancelPendingRingerChordAction(); // 结束音量长按事件  
  16.             interceptScreenshotChord(); // 判断是否是截屏处理  
  17.         }  
  18.         // 当电话铃响起时,则会静音,而正在通话时,则会挂掉电话  
  19.         ITelephony telephonyService = getTelephonyService();  
  20.         boolean hungUp = false;  
  21.         if (telephonyService != null) {  
  22.             try {  
  23.                 if (telephonyService.isRinging()) {  
  24.                 // Pressing Power while there's a ringing incoming  
  25.                 // call should silence the ringer.  
  26.                 telephonyService.silenceRinger();  
  27.                 } else if ((mIncallPowerBehavior  
  28.                 & Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_HANGUP) != 0  
  29.                 && telephonyService.isOffhook()) {  
  30.                 // Otherwise, if "Power button ends call" is enabled,  
  31.                 // the Power button will hang up any current active call.  
  32.                 hungUp = telephonyService.endCall();  
  33.                 }  
  34.             } catch (RemoteException ex) {  
  35.                 Log.w(TAG, "ITelephony threw RemoteException", ex);  
  36.             }  
  37.             }  
  38.         // 延迟固定时间触发长按事件,长按事件主要是弹出全局对话框  
  39.         interceptPowerKeyDown(!isScreenOn || hungUp  
  40.                             || mVolumeDownKeyTriggered || mVolumeUpKeyTriggered);  
  41.         } else {  
  42.         mPowerKeyTriggered = false;  
  43.         cancelPendingScreenshotChordAction();  
  44.         if (interceptPowerKeyUp(canceled || mPendingPowerKeyUpCanceled)) {  
  45.             result = (result & ~ACTION_POKE_USER_ACTIVITY) | ACTION_GO_TO_SLEEP;  
  46.         }  
  47.         mPendingPowerKeyUpCanceled = false;  
  48.         }  
  49.         break;  
  50.     }  
  51.     ......  
  52.     }  
  53. }  

在interceptKeyBeforeQueueing方法中主要使用interceptPowerKeyDown方法来触发长按事件。

[java]  view plain copy
  1. private void interceptPowerKeyDown(boolean handled) {  
  2.     mPowerKeyHandled = handled;  
  3.     if (!handled) {  
  4.     mHandler.postDelayed(mPowerLongPress, ViewConfiguration.getGlobalActionKeyTimeout());  
  5.     }  
  6. }  
  7.   
  8. private final Runnable mPowerLongPress = new Runnable() {  
  9.     public void run() {  
  10.     // The context isn't read  
  11.     if (mLongPressOnPowerBehavior < 0) {  
  12.         mLongPressOnPowerBehavior = mContext.getResources().getInteger(  
  13.             com.android.internal.R.integer.config_longPressOnPowerBehavior);  
  14.     }  
  15.     switch (mLongPressOnPowerBehavior) {  
  16.             case LONG_PRESS_POWER_NOTHING:  
  17.                 break;  
  18.             case LONG_PRESS_POWER_GLOBAL_ACTIONS:  
  19.                 mPowerKeyHandled = true;  
  20.                 performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);  
  21.                 sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);  
  22.                 showGlobalActionsDialog();  
  23.                 break;  
  24.             case LONG_PRESS_POWER_SHUT_OFF:  
  25.                 mPowerKeyHandled = true;  
  26.                 performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);  
  27.                 sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);  
  28.                 ShutdownThread.shutdown(mContext, true);  
  29.                 break;  
  30.         }  
  31.     }  
  32. };  

在interceptPowerKeyDown方法中,通过postDelayed使得mPowerLongPress延时一段时间执行,而在mPowerLongPress中,则通过showGlobalActionsDialog方法来弹出全局对话框。

[java]  view plain copy
  1. void showGlobalActionsDialog() {  
  2.     if (mGlobalActions == null) {  
  3.     mGlobalActions = new GlobalActions(mContext);  
  4.     }  
  5.     final boolean keyguardShowing = mKeyguardMediator.isShowingAndNotHidden();  
  6.     mGlobalActions.showDialog(keyguardShowing, isDeviceProvisioned());  
  7.     if (keyguardShowing) {  
  8.     // since it took two seconds of long press to bring this up,  
  9.     // poke the wake lock so they have some time to see the dialog.  
  10.     mKeyguardMediator.pokeWakelock();  
  11.     }  
  12. }  

其中mGlobalActions是GlobalActions类型的对象(frameworks/base/policy/src/com/android/internal/policy/impl/GlobalActions.java),其showDialog实现如下所示

[java]  view plain copy
  1. public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned) {  
  2.     mKeyguardShowing = keyguardShowing;  
  3.     mDeviceProvisioned = isDeviceProvisioned;  
  4.     if (mDialog != null && mUiContext == null) {  
  5.     mDialog.dismiss();  
  6.     mDialog = null;  
  7.     }  
  8.     if (mDialog == null) {  
  9.     mDialog = createDialog();  
  10.     }  
  11.     prepareDialog();  
  12.   
  13.     mDialog.show();  
  14.     mDialog.getWindow().getDecorView().setSystemUiVisibility(View.STATUS_BAR_DISABLE_EXPAND);  
  15. }  

此方法将mDialog显示出来,而mDialog是使用createDialog来创建的。

[java]  view plain copy
  1. private AlertDialog createDialog() {  
  2.     ......  
  3.     mItems = new ArrayList<Action>();  
  4.   
  5.     // first: power off  
  6.     mItems.add(  
  7.     new SinglePressAction(  
  8.         com.android.internal.R.drawable.ic_lock_power_off,  
  9.         R.string.global_action_power_off) {  
  10.   
  11.         public void onPress() {  
  12.         // shutdown by making sure radio and power are handled accordingly.  
  13.         ShutdownThread.shutdown(getUiContext(), true);  
  14.         }  
  15.   
  16.         public boolean showDuringKeyguard() {  
  17.         return true;  
  18.         }  
  19.   
  20.         public boolean showBeforeProvisioning() {  
  21.         return true;  
  22.         }  
  23.     });  
  24.     ......  
  25.     return dialog;  
  26. }  

方法createDialog用于创建全局对话框中的各个选项,其中包括关机选项,当选择关机选项时会使用ShutdownThread类中的shutdown方法来执行关机(frameworks/base/core/java/com/android/internal/app/ShutdownThread.java)。

[java]  view plain copy
  1. public static void shutdown(final Context context, boolean confirm) {  
  2.     ......  
  3.     if (confirm) {  
  4.     final AlertDialog dialog;  
  5.     // Set different dialog message based on whether or not we're rebooting  
  6.     if (mReboot) {  
  7.             ......      
  8.     } else {  
  9.         dialog = new AlertDialog.Builder(context)  
  10.             .setIconAttribute(android.R.attr.alertDialogIcon)  
  11.             .setTitle(com.android.internal.R.string.power_off)  
  12.             .setMessage(com.android.internal.R.string.shutdown_confirm)  
  13.             .setPositiveButton(com.android.internal.R.string.yes, new DialogInterface.OnClickListener() {  
  14.             public void onClick(DialogInterface dialog, int which) {  
  15.                 beginShutdownSequence(context);  
  16.             }  
  17.             })  
  18.             .setNegativeButton(com.android.internal.R.string.no, null)  
  19.             .create();  
  20.     }  
  21.     dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);  
  22.     dialog.show();  
  23.     } else {  
  24.     beginShutdownSequence(context);  
  25.     }  
  26. }  

方法shutdown主要是用来弹出一个确认对话框,如果点击确认了会再使用beginShutdownSequence来执行关机。

[java]  view plain copy
  1. private static void beginShutdownSequence(Context context) {  
  2.     ......  
  3.   
  4.     // start the thread that initiates shutdown  
  5.     sInstance.mHandler = new Handler() {  
  6.     };  
  7.     sInstance.start();  
  8. }  
  9.   
  10. public void run() {  
  11.     ......  
  12.     rebootOrShutdown(mReboot, mRebootReason);  
  13. }  
  14.   
  15. public static void rebootOrShutdown(boolean reboot, String reason) {  
  16.     ......  
  17.     // Shutdown power  
  18.     Log.i(TAG, "Performing low-level shutdown...");  
  19.     Power.shutdown();  
  20. }  

在beginShutdownSequence方法中会启动一个线程来执行关机,而sInstance就是ShutdownThread类型的对象,当其start时会调用run函数,而在run函数中会使用rebootOrShutdown,最终会调用Power类中的shutdown来执行关机操作(frameworks/base/core/java/os/Power.java)。而在Power类中会调用android_os_Power.cpp(frameworks/base/core/jni/android_os_Power.cpp)中的jni方法android_os_Power_shutdown。

[cpp]  view plain copy
  1. static void android_os_Power_shutdown(JNIEnv *env, jobject clazz)  
  2. {  
  3.     android_reboot(ANDROID_RB_POWEROFF, 0, 0);  
  4. }  

在android_os_Power_shutdown会调用android_reboot,而android_reboot的实现是在/system/core/libcutils/android_reboot.c中,在android_reboot中还会调用reboot方法,此方法实现于/bionic/libc/unistd/reboot.c中。

[cpp]  view plain copy
  1. int android_reboot(int cmd, int flags, char *arg)  
  2. {  
  3.     ......  
  4.     switch (cmd) {  
  5.         case ANDROID_RB_RESTART:  
  6.             reason = RB_AUTOBOOT;  
  7.             break;  
  8.   
  9.         case ANDROID_RB_POWEROFF:  
  10.             ret = reboot(RB_POWER_OFF);  
  11.             return ret;  
  12.   
  13.         case ANDROID_RB_RESTART2:  
  14.             // REBOOT_MAGIC  
  15.             break;  
  16.   
  17.         default:  
  18.             return -1;  
  19.     }  
  20.     ......  
  21.     return ret;  
  22. }  
  23.   
  24. int reboot (int  mode)   
  25. {  
  26.     return __reboot( LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, mode, NULL );  
  27. }  

而__reboot方法使用汇编语言传递这些参数至kernel,在kernel/kernel/sys.c中有实现

[cpp]  view plain copy
  1. SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,  
  2.         void __user *, arg)  
  3. {  
  4.     char buffer[256];  
  5.     int ret = 0;  
  6.   
  7.     ......  
  8.   
  9.     /* For safety, we require "magic" arguments. */  
  10.     if (magic1 != LINUX_REBOOT_MAGIC1 ||  
  11.         (magic2 != LINUX_REBOOT_MAGIC2 &&  
  12.         magic2 != LINUX_REBOOT_MAGIC2A &&  
  13.         magic2 != LINUX_REBOOT_MAGIC2B &&  
  14.         magic2 != LINUX_REBOOT_MAGIC2C))  
  15.     return -EINVAL;  
  16.   
  17.     /* Instead of trying to make the power_off code look like 
  18.     * halt when pm_power_off is not set do it the easy way. 
  19.     */  
  20.     if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)  
  21.     cmd = LINUX_REBOOT_CMD_HALT;  
  22.   
  23.     mutex_lock(&reboot_mutex);  
  24.     switch (cmd) {  
  25.     case LINUX_REBOOT_CMD_RESTART:  
  26.         kernel_restart(NULL);  
  27.         break;  
  28.   
  29.     case LINUX_REBOOT_CMD_CAD_ON:  
  30.         C_A_D = 1;  
  31.         break;  
  32.   
  33.     case LINUX_REBOOT_CMD_CAD_OFF:  
  34.         C_A_D = 0;  
  35.         break;  
  36.   
  37.     case LINUX_REBOOT_CMD_HALT:  
  38.         kernel_halt();  
  39.         do_exit(0);  
  40.         panic("cannot halt");  
  41.   
  42.     case LINUX_REBOOT_CMD_POWER_OFF:  
  43.         kernel_power_off();  
  44.         do_exit(0);  
  45.         break;  
  46.   
  47.     case LINUX_REBOOT_CMD_RESTART2:  
  48.         if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {  
  49.         ret = -EFAULT;  
  50.         break;  
  51.         }  
  52.         buffer[sizeof(buffer) - 1] = '\0';  
  53.         kernel_restart(buffer);  
  54.         break;  
  55.     ......  
  56.     default:  
  57.         ret = -EINVAL;  
  58.         break;  
  59.     }  
  60.     mutex_unlock(&reboot_mutex);  
  61.     return ret;  
  62. }  

在此方法中会根据参数cmd来执行,如果cmd为LINUX_REBOOT_CMD_POWER_OFF,则会执行kernel_power_off方法,然后使用machine_power_off来关闭系统,machine_power_off定义在/kernel/arch/arm/kernel/process.c中。

[cpp]  view plain copy
  1. void kernel_power_off(void)  
  2. {  
  3.     kernel_shutdown_prepare(SYSTEM_POWER_OFF);  
  4.     if (pm_power_off_prepare)  
  5.     pm_power_off_prepare();  
  6.     disable_nonboot_cpus();  
  7.     syscore_shutdown();  
  8.     printk(KERN_EMERG "Power down.\n");  
  9.     kmsg_dump(KMSG_DUMP_POWEROFF);  
  10.     machine_power_off();  
  11. }  
  12. EXPORT_SYMBOL_GPL(kernel_power_off);  
  13.   
  14. void machine_power_off(void)  
  15. {  
  16.     machine_shutdown();  
  17.     if (pm_power_off)  
  18.     pm_power_off(); // 执行pm_power_off关闭power  
  19. }  

以上就是Android系统的关机流程。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值