Android Framework層Power鍵關機流程(一,Power長按鍵動作處理)
一:Android處理Power按鍵長按操作
在Framework層中,Android4.x對Power鍵(KeyEvent.KEYCODE_POWER)的操作,我們從PhoneWindowManager開始分析,在分析前我這裡列印了該方法的堆棧調用資訊。大家可以參考一下。
public long interceptKeyBeforeDispatching(WindowState win, KeyEvent event, int policyFlags) {
......
android.util.Log.d("BILL",android.util.Log.getStackTraceString(new Throwable()));
......
}
1-13 19:35:32.458 D/BILL ( 718): java.lang.Throwable
01-13 19:35:32.458 D/BILL ( 718): at com.android.internal.policy.impl.PhoneWindowManager.interceptKeyBeforeDispatching(PhoneWindowManager.java:2224)
01-13 19:35:32.458 D/BILL ( 718): at com.android.server.wm.InputMonitor.interceptKeyBeforeDispatching(InputMonitor.java:501)
01-13 19:35:32.458 D/BILL ( 718): at com.android.server.input.InputManagerService.interceptKeyBeforeDispatching(InputManagerService.java:1383)
01-13 19:35:32.458 D/BILL ( 718): at dalvik.system.NativeStart.run(Native Method)
調用流程如下(只貼出關鍵代碼):
interceptKeyBeforeDispatching()-->interceptPowerKeyDown()-->mPowerLongPress.run()
1>
public long interceptKeyBeforeDispatching(WindowState win, KeyEvent event, int policyFlags) {
......
case KeyEvent.KEYCODE_POWER: {
result &= ~ACTION_PASS_TO_USER;
if (down) {
mImmersiveModeConfirmation.onPowerKeyDown(isScreenOn, event.getDownTime(),
isImmersiveMode(mLastSystemUiFlags));
if (isScreenOn && !mPowerKeyTriggered
&& (event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0) {
mPowerKeyTriggered = true;
mPowerKeyTime = event.getDownTime();
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();
/// M: [ALPS00093981] @{
} else if ((isScreenOn
|| mScreenOffReason == OFF_BECAUSE_OF_PROX_SENSOR)
/// @}
&& (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_WAKE_UP) | ACTION_GO_TO_SLEEP;
}
mPendingPowerKeyUpCanceled = false;
}
break;
}
......
}
注!紅色為判斷長按(down),藍色為判斷短按(up)。
2>
private void interceptPowerKeyDown(boolean handled) {
mPowerKeyHandled = handled;
if (!handled) {
mHandler.postDelayed(mPowerLongPress, ViewConfiguration.getGlobalActionKeyTimeout());
}
}
3>
private final Runnable mPowerLongPress = new Runnable() {
......
case LONG_PRESS_POWER_GLOBAL_ACTIONS:
mPowerKeyHandled = true;
if (!performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false)) {
performAuditoryFeedbackForAccessibilityIfNeed();
}
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
showGlobalActionsDialog();
break;
case LONG_PRESS_POWER_SHUT_OFF:
case LONG_PRESS_POWER_SHUT_OFF_NO_CONFIRM:
mPowerKeyHandled = true;
performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
mWindowManagerFuncs.shutdown(resolvedBehavior == LONG_PRESS_POWER_SHUT_OFF);
break;
......
};
註:上述代碼中底線即彈出(關機、重啟、飛航模式等選項)的對話方塊。