1. PhoneWindowManager.java中监听KeyEvent.KEYCODE_POWER事件(代码:mKeyguardMediator.onWakeKeyWhenKeyguardShowingTq(KeyEvent.KEYCODE_POWER)
2.在interceptKeyBeforeQueueing中会有关KeyEvent.KEYCODE_POWER事件的处理
case KeyEvent.KEYCODE_POWER: { result &= ~ACTION_PASS_TO_USER; if (down) { 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); // 在测试长按时,一般来说isScreenOn为true,hungUp即电话挂断状态一般为false } else { if (interceptPowerKeyUp(canceled)) { result = (result & ~ACTION_POKE_USER_ACTIVITY) | ACTION_GO_TO_SLEEP; } } break; }3.interceptPowerKeyDown函数代码如下:
private void interceptPowerKeyDown(boolean handled) { mPowerKeyHandled = handled; if (!handled) { mHandler.postDelayed(mPowerLongPress, ViewConfiguration.getGlobalActionKeyTimeout()); } }
private final Runnable mPowerLongPress = new Runnable() { public void run() { if (!mPowerKeyHandled) { mPowerKeyHandled = true; performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false); sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS); showGlobalActionsDialog(); } } };
private boolean interceptPowerKeyUp(boolean canceled) { if (!mPowerKeyHandled) { // 如果不是长按的话,就会remove掉长按处理,因为只响应短按了 mHandler.removeCallbacks(mPowerLongPress); return !canceled; } else { mPowerKeyHandled = true; return false; } }