Android 开关机POWER键 长按短按代码分析

代码定位

路径:frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
	.......
	.......
	.......
	case KeyEvent.KEYCODE_POWER: {
                EventLogTags.writeInterceptPower(
                        KeyEvent.actionToString(event.getAction()),
                        mPowerKeyHandled ? 1 : 0, mPowerKeyPressCounter);
                // Any activity on the power button stops the accessibility shortcut
                cancelPendingAccessibilityShortcutAction();
                result &= ~ACTION_PASS_TO_USER;
                isWakeKey = false; // wake-up will be handled separately
                if (down) {
                    interceptPowerKeyDown(event, interactive);//按下动作  interactive->是否亮屏
                } else {
                    interceptPowerKeyUp(event, interactive, canceled);//释放动作
                }
                break;
            }
    case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_DOWN:
             // fall through
    case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP:
             // fall through
    case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_LEFT:
             // fall through
    case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_RIGHT: {
             if(!isBox){
             result &= ~ACTION_PASS_TO_USER;
             interceptSystemNavigationKey(event);
     }
                break;
    }
	.......
	.......
	.......

}

根据上述代码追踪到这个interceptPowerKeyUp()函数接口

private void interceptPowerKeyUp(KeyEvent event, boolean interactive, boolean canceled) {
        final boolean handled = canceled || mPowerKeyHandled;
        mScreenshotChordPowerKeyTriggered = false;
        cancelPendingScreenshotChordAction();
        cancelPendingPowerKeyAction();

        if (!handled) {
            if ((event.getFlags() & KeyEvent.FLAG_LONG_PRESS) == 0) {
                // Abort possibly stuck animations only when power key up without long press case.
                mHandler.post(mWindowManagerFuncs::triggerAnimationFailsafe);
            }

            // Figure out how to handle the key now that it has been released.
            mPowerKeyPressCounter += 1;

            final int maxCount = getMaxMultiPressPowerCount();
            final long eventTime = event.getDownTime();
            if (mPowerKeyPressCounter < maxCount) {
                // This could be a multi-press.  Wait a little bit longer to confirm.
                // Continue holding the wake lock.
                Message msg = mHandler.obtainMessage(MSG_POWER_DELAYED_PRESS,
                        interactive ? 1 : 0, mPowerKeyPressCounter, eventTime);
                msg.setAsynchronous(true);
                mHandler.sendMessageDelayed(msg, ViewConfiguration.getMultiPressTimeout());
                return;
            }

            // No other actions.  Handle it immediately.
            powerPress(eventTime, interactive, mPowerKeyPressCounter);
        }

        // Done.  Reset our state.
        finishPowerKeyPress();
    }

然后注意到上述一个函数接口 powerPress(eventTime, interactive, mPowerKeyPressCounter);

 private void powerPress(long eventTime, boolean interactive, int count) {
        if (mDefaultDisplayPolicy.isScreenOnEarly() && !mDefaultDisplayPolicy.isScreenOnFully()) {
            Slog.i(TAG, "Suppressed redundant power key press while "
                    + "already in the process of turning the screen on.");
            return;
        }
        Slog.d(TAG, "powerPress: eventTime=" + eventTime + " interactive=" + interactive
                + " count=" + count + " beganFromNonInteractive=" + mBeganFromNonInteractive +
                " mShortPressOnPowerBehavior=" + mShortPressOnPowerBehavior);

        if (count == 2) {
            powerMultiPressAction(eventTime, interactive, mDoublePressOnPowerBehavior);
        } else if (count == 3) {
            powerMultiPressAction(eventTime, interactive, mTriplePressOnPowerBehavior);
        } else if (interactive && !mBeganFromNonInteractive) {
            switch (mShortPressOnPowerBehavior) {
                case SHORT_PRESS_POWER_NOTHING:
                    break;
                case SHORT_PRESS_POWER_GO_TO_SLEEP:
                    String product = SystemProperties.get("ro.target.product","");
                    if (!TextUtils.isEmpty(product) && (product.equals("box") || product.equals("atv"))) {
                        int powerKey = SystemProperties.getInt("persist.sys.power_key",0);
                        Slog.i("ROCKCHIP", "powerKey = " + powerKey);
                        if (powerKey == 1) {
                            shutDown();
                        } else if (powerKey == 2) {
                            reboot();
                        } else {
                            goToSleepFromPowerButton(eventTime, 0);
                        }
                    } else {
                        goToSleepFromPowerButton(eventTime, 0);
                    }
                    break;
                case SHORT_PRESS_POWER_REALLY_GO_TO_SLEEP:
                    goToSleepFromPowerButton(eventTime, PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE);
                    break;
                case SHORT_PRESS_POWER_REALLY_GO_TO_SLEEP_AND_GO_HOME:
                    if (goToSleepFromPowerButton(eventTime,
                            PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE)) {
                        launchHomeFromHotKey(DEFAULT_DISPLAY);
                    }
                    break;
                case SHORT_PRESS_POWER_GO_HOME:
                    shortPressPowerGoHome();
                    break;
                case SHORT_PRESS_POWER_CLOSE_IME_OR_GO_HOME: {
                    if (mDismissImeOnBackKeyPressed) {
                        if (mInputMethodManagerInternal == null) {
                            mInputMethodManagerInternal =
                                    LocalServices.getService(InputMethodManagerInternal.class);
                        }
                        if (mInputMethodManagerInternal != null) {
                            mInputMethodManagerInternal.hideCurrentInputMethod(
                                    SoftInputShowHideReason.HIDE_POWER_BUTTON_GO_HOME);
                        }
                    } else {
                        shortPressPowerGoHome();
                    }
                    break;
                }
            }
        }
    }

如果想取消短按熄屏如何做呢?接下来是我的做法如下

case SHORT_PRESS_POWER_GO_TO_SLEEP:
                    String product = SystemProperties.get("ro.target.product","");
                    if (!TextUtils.isEmpty(product) && (product.equals("box") || product.equals("atv"))) {
                        int powerKey = SystemProperties.getInt("persist.sys.power_key",0);
                        Slog.i("ROCKCHIP", "powerKey = " + powerKey);
                        if (powerKey == 1) {
                            shutDown();
                        } else if (powerKey == 2) {
                            reboot();
                        } else {
                            //goToSleepFromPowerButton(eventTime, 0);
                        }
                    } else {
                        //goToSleepFromPowerButton(eventTime, 0);
                    }
                    break;

你可能感兴趣的:(瑞芯微RK驱动调试,android)