今天研究学习了Android的关机流程。
在interceptKeyBeforeQueueing方法中主要使用interceptPowerKeyDown方法来触发长按事件。
在interceptPowerKeyDown方法中,通过postDelayed使得mPowerLongPress延时一段时间执行,而在mPowerLongPress中,则通过showGlobalActionsDialog方法来弹出全局对话框。
其中mGlobalActions是GlobalActions类型的对象(frameworks/base/policy/src/com/android/internal/policy/impl/GlobalActions.java),其showDialog实现如下所示
此方法将mDialog显示出来,而mDialog是使用createDialog来创建的。
方法createDialog用于创建全局对话框中的各个选项,其中包括关机选项,当选择关机选项时会使用ShutdownThread类中的shutdown方法来执行关机(frameworks/base/core/java/com/android/internal/app/ShutdownThread.java)。
方法shutdown主要是用来弹出一个确认对话框,如果点击确认了会再使用beginShutdownSequence来执行关机。
在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。
在android_os_Power_shutdown会调用android_reboot,而android_reboot的实现是在/system/core/libcutils/android_reboot.c中,在android_reboot中还会调用reboot方法,此方法实现于/bionic/libc/unistd/reboot.c中。
而__reboot方法使用汇编语言传递这些参数至kernel,在kernel/kernel/sys.c中有实现
在此方法中会根据参数cmd来执行,如果cmd为LINUX_REBOOT_CMD_POWER_OFF,则会执行kernel_power_off方法,然后使用machine_power_off来关闭系统,machine_power_off定义在/kernel/arch/arm/kernel/process.c中。
以上就是Android系统的关机流程。
大家都知道,当长按power按键时,手机就会弹出一个对话框,从对话框中我们可以选择“关机”选项。此功能是在PhoneWindowManager.java中实现完成(frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java),在此文件中有interceptKeyBeforeQueueing方法用于接收按键事件,然后对各个不同的事件做出相应的操作,其中power按键的操作如下:
public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags, boolean isScreenOn) {
......
switch (keyCode) {
......
case KeyEvent.KEYCODE_POWER: {
if ((mTopFullscreenOpaqueWindowState.getAttrs().flags & WindowManager.LayoutParams.PREVENT_POWER_KEY) != 0){
return result;
}
result &= ~ACTION_PASS_TO_USER;
if (down) { // 如果是按下键
if (isScreenOn && !mPowerKeyTriggered
&& (event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0) {
mPowerKeyTriggered = true; // 按键触发为true
mPowerKeyTime = event.getDownTime(); // 记录事件时间
cancelPendingRingerChordAction(); // 结束音量长按事件
interceptScreenshotChord(); // 判断是否是截屏处理
}
// 当电话铃响起时,则会静音,而正在通话时,则会挂掉电话
ITelephony telephonyService = getTelephonyService();
boolean hungUp = false;
if (telephonyService != null) {
try {
if (telephonyService.isRinging()) {
// Pressing Power while there's a ringing incoming
// call should silence the ringer.
telephonyService.silenceRinger();
} else if ((mIncallPowerBehavior
& Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_HANGUP) != 0
&& telephonyService.isOffhook()) {
// Otherwise, if "Power button ends call" is enabled,
// the Power button will hang up any current active call.
hungUp = telephonyService.endCall();
}
} catch (RemoteException ex) {
Log.w(TAG, "ITelephony threw RemoteException", ex);
}
}
// 延迟固定时间触发长按事件,长按事件主要是弹出全局对话框
interceptPowerKeyDown(!isScreenOn || hungUp
|| mVolumeDownKeyTriggered || mVolumeUpKeyTriggered);
} else {
mPowerKeyTriggered = false;
cancelPendingScreenshotChordAction();
if (interceptPowerKeyUp(canceled || mPendingPowerKeyUpCanceled)) {
result = (result & ~ACTION_POKE_USER_ACTIVITY) | ACTION_GO_TO_SLEEP;
}
mPendingPowerKeyUpCanceled = false;
}
break;
}
......
}
}在interceptKeyBeforeQueueing方法中主要使用interceptPowerKeyDown方法来触发长按事件。
private void interceptPowerKeyDown(boolean handled) {
mPowerKeyHandled = handled;
if (!handled) {
mHandler.postDelayed(mPowerLongPress, ViewConfiguration.getGlobalActionKeyTimeout());
}
}
private final Runnable mPowerLongPress = new Runnable() {
public void run() {
// The context isn't read
if (mLongPressOnPowerBehavior < 0) {
mLongPressOnPowerBehavior = mContext.getResources().getInteger(
com.android.internal.R.integer.config_longPressOnPowerBehavior);
}
switch (mLongPressOnPowerBehavior) {
case LONG_PRESS_POWER_NOTHING:
break;
case LONG_PRESS_POWER_GLOBAL_ACTIONS:
mPowerKeyHandled = true;
performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
showGlobalActionsDialog();
break;
case LONG_PRESS_POWER_SHUT_OFF:
mPowerKeyHandled = true;
performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
ShutdownThread.shutdown(mContext, true);
break;
}
}
};在interceptPowerKeyDown方法中,通过postDelayed使得mPowerLongPress延时一段时间执行,而在mPowerLongPress中,则通过showGlobalActionsDialog方法来弹出全局对话框。
void showGlobalActionsDialog() {
if (mGlobalActions == null) {
mGlobalActions = new GlobalActions(mContext);
}
final boolean keyguardShowing = mKeyguardMediator.isShowingAndNotHidden();
mGlobalActions.showDialog(keyguardShowing, isDeviceProvisioned());
if (keyguardShowing) {
// since it took two seconds of long press to bring this up,
// poke the wake lock so they have some time to see the dialog.
mKeyguardMediator.pokeWakelock();
}
}其中mGlobalActions是GlobalActions类型的对象(frameworks/base/policy/src/com/android/internal/policy/impl/GlobalActions.java),其showDialog实现如下所示
public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned) {
mKeyguardShowing = keyguardShowing;
mDeviceProvisioned = isDeviceProvisioned;
if (mDialog != null && mUiContext == null) {
mDialog.dismiss();
mDialog = null;
}
if (mDialog == null) {
mDialog = createDialog();
}
prepareDialog();
mDialog.show();
mDialog.getWindow().getDecorView().setSystemUiVisibility(View.STATUS_BAR_DISABLE_EXPAND);
}此方法将mDialog显示出来,而mDialog是使用createDialog来创建的。
private AlertDialog createDialog() {
......
mItems = new ArrayList<Action>();
// first: power off
mItems.add(
new SinglePressAction(
com.android.internal.R.drawable.ic_lock_power_off,
R.string.global_action_power_off) {
public void onPress() {
// shutdown by making sure radio and power are handled accordingly.
ShutdownThread.shutdown(getUiContext(), true);
}
public boolean showDuringKeyguard() {
return true;
}
public boolean showBeforeProvisioning() {
return true;
}
});
......
return dialog;
}方法createDialog用于创建全局对话框中的各个选项,其中包括关机选项,当选择关机选项时会使用ShutdownThread类中的shutdown方法来执行关机(frameworks/base/core/java/com/android/internal/app/ShutdownThread.java)。
public static void shutdown(final Context context, boolean confirm) {
......
if (confirm) {
final AlertDialog dialog;
// Set different dialog message based on whether or not we're rebooting
if (mReboot) {
......
} else {
dialog = new AlertDialog.Builder(context)
.setIconAttribute(android.R.attr.alertDialogIcon)
.setTitle(com.android.internal.R.string.power_off)
.setMessage(com.android.internal.R.string.shutdown_confirm)
.setPositiveButton(com.android.internal.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
beginShutdownSequence(context);
}
})
.setNegativeButton(com.android.internal.R.string.no, null)
.create();
}
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
dialog.show();
} else {
beginShutdownSequence(context);
}
}方法shutdown主要是用来弹出一个确认对话框,如果点击确认了会再使用beginShutdownSequence来执行关机。
private static void beginShutdownSequence(Context context) {
......
// start the thread that initiates shutdown
sInstance.mHandler = new Handler() {
};
sInstance.start();
}
public void run() {
......
rebootOrShutdown(mReboot, mRebootReason);
}
public static void rebootOrShutdown(boolean reboot, String reason) {
......
// Shutdown power
Log.i(TAG, "Performing low-level shutdown...");
Power.shutdown();
}在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。
static void android_os_Power_shutdown(JNIEnv *env, jobject clazz)
{
android_reboot(ANDROID_RB_POWEROFF, 0, 0);
}在android_os_Power_shutdown会调用android_reboot,而android_reboot的实现是在/system/core/libcutils/android_reboot.c中,在android_reboot中还会调用reboot方法,此方法实现于/bionic/libc/unistd/reboot.c中。
int android_reboot(int cmd, int flags, char *arg)
{
......
switch (cmd) {
case ANDROID_RB_RESTART:
reason = RB_AUTOBOOT;
break;
case ANDROID_RB_POWEROFF:
ret = reboot(RB_POWER_OFF);
return ret;
case ANDROID_RB_RESTART2:
// REBOOT_MAGIC
break;
default:
return -1;
}
......
return ret;
}
int reboot (int mode)
{
return __reboot( LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, mode, NULL );
}而__reboot方法使用汇编语言传递这些参数至kernel,在kernel/kernel/sys.c中有实现
SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
void __user *, arg)
{
char buffer[256];
int ret = 0;
......
/* For safety, we require "magic" arguments. */
if (magic1 != LINUX_REBOOT_MAGIC1 ||
(magic2 != LINUX_REBOOT_MAGIC2 &&
magic2 != LINUX_REBOOT_MAGIC2A &&
magic2 != LINUX_REBOOT_MAGIC2B &&
magic2 != LINUX_REBOOT_MAGIC2C))
return -EINVAL;
/* Instead of trying to make the power_off code look like
* halt when pm_power_off is not set do it the easy way.
*/
if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
cmd = LINUX_REBOOT_CMD_HALT;
mutex_lock(&reboot_mutex);
switch (cmd) {
case LINUX_REBOOT_CMD_RESTART:
kernel_restart(NULL);
break;
case LINUX_REBOOT_CMD_CAD_ON:
C_A_D = 1;
break;
case LINUX_REBOOT_CMD_CAD_OFF:
C_A_D = 0;
break;
case LINUX_REBOOT_CMD_HALT:
kernel_halt();
do_exit(0);
panic("cannot halt");
case LINUX_REBOOT_CMD_POWER_OFF:
kernel_power_off();
do_exit(0);
break;
case LINUX_REBOOT_CMD_RESTART2:
if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
ret = -EFAULT;
break;
}
buffer[sizeof(buffer) - 1] = '\0';
kernel_restart(buffer);
break;
......
default:
ret = -EINVAL;
break;
}
mutex_unlock(&reboot_mutex);
return ret;
}在此方法中会根据参数cmd来执行,如果cmd为LINUX_REBOOT_CMD_POWER_OFF,则会执行kernel_power_off方法,然后使用machine_power_off来关闭系统,machine_power_off定义在/kernel/arch/arm/kernel/process.c中。
void kernel_power_off(void)
{
kernel_shutdown_prepare(SYSTEM_POWER_OFF);
if (pm_power_off_prepare)
pm_power_off_prepare();
disable_nonboot_cpus();
syscore_shutdown();
printk(KERN_EMERG "Power down.\n");
kmsg_dump(KMSG_DUMP_POWEROFF);
machine_power_off();
}
EXPORT_SYMBOL_GPL(kernel_power_off);
void machine_power_off(void)
{
machine_shutdown();
if (pm_power_off)
pm_power_off(); // 执行pm_power_off关闭power
}以上就是Android系统的关机流程。
crcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcr
Hufikyu的学习空间,欢迎大家提出问题,共同进步。
crcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcr

9316

被折叠的 条评论
为什么被折叠?



