Android 5.1 修改POWER键功能之两次按下返回上一级,三次按下返回主页面

时间:2024-02-19 16:22:02

修改以下两个地方

frameworks\base\core\res\res\values\config.xml

将config_triplePressOnPowerBehavior和config_doublePressOnPowerBehavior的值改为1

frameworks\base\policy\src\com\android\internal\policy\impl\PhoneWindowManager.java

增加处理值

    static final int MULTI_PRESS_POWER_TWO = 3;
    static final int MULTI_PRESS_POWER_THREE = 4;

修改void powerPress(long eventTime, boolean interactive, int count)和void powerMultiPressAction(long eventTime, boolean interactive, int behavior)函数中次数的处理

    private void powerPress(long eventTime, boolean interactive, int count) {
        if (mScreenOnEarly && !mScreenOnFully) {
            Slog.i(TAG, "Suppressed redundant power key press while "
                    + "already in the process of turning the screen on.");
            return;
        }

        if (count == 2) {
            Log.d(TAG,"count == 2");
            powerMultiPressAction(eventTime, interactive, MULTI_PRESS_POWER_TWO); //mDoublePressOnPowerBehavior
        } else if (count == 3) {
            Log.d(TAG,"count == 3");
            powerMultiPressAction(eventTime, interactive, MULTI_PRESS_POWER_THREE); //mTriplePressOnPowerBehavior
        } /*else if (interactive && !mBeganFromNonInteractive) { //屏蔽短按休眠功能
            switch (mShortPressOnPowerBehavior) {
                case SHORT_PRESS_POWER_NOTHING:
                    break;
                case SHORT_PRESS_POWER_GO_TO_SLEEP:
                    mPowerManager.goToSleep(eventTime,
                            PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON, 0);
                    break;
                case SHORT_PRESS_POWER_REALLY_GO_TO_SLEEP:
                    mPowerManager.goToSleep(eventTime,
                            PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON,
                            PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE);
                    break;
                case SHORT_PRESS_POWER_REALLY_GO_TO_SLEEP_AND_GO_HOME:
                    mPowerManager.goToSleep(eventTime,
                            PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON,
                            PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE);
                    launchHomeFromHotKey();
                    break;
            }
        }*/
    }
    private void powerMultiPressAction(long eventTime, boolean interactive, int behavior) {
        switch (behavior) {
            case MULTI_PRESS_POWER_NOTHING:
                break;
            case MULTI_PRESS_POWER_THEATER_MODE:
                if (isTheaterModeEnabled()) {
                    Slog.i(TAG, "Toggling theater mode off.");
                    Settings.Global.putInt(mContext.getContentResolver(),
                            Settings.Global.THEATER_MODE_ON, 0);
                    if (!interactive) {
                        wakeUpFromPowerKey(eventTime);
                    }
                } else {
                    Slog.i(TAG, "Toggling theater mode on.");
                    Settings.Global.putInt(mContext.getContentResolver(),
                            Settings.Global.THEATER_MODE_ON, 1);

                    if (mGoToSleepOnButtonPressTheaterMode && interactive) {
                        mPowerManager.goToSleep(eventTime,
                                PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON, 0);
                    }
                }
                break;
            case MULTI_PRESS_POWER_BRIGHTNESS_BOOST:
                Slog.i(TAG, "Starting brightness boost.");
                if (!interactive) {
                    wakeUpFromPowerKey(eventTime);
                }
                mPowerManager.boostScreenBrightness(eventTime);
                break;
            case MULTI_PRESS_POWER_TWO: //增加两次处理
                Slog.i(TAG, "two presses of the power key");
                powerDefinePress(MULTI_PRESS_POWER_TWO);
                break;
            case MULTI_PRESS_POWER_THREE: //增加三次处理
                Slog.i(TAG, "three presses of the power key");
                powerDefinePress(MULTI_PRESS_POWER_THREE);
                break;
        }
    }

添加处理函数

    private void powerDefinePress(int pressType) {
        if (pressType == MULTI_PRESS_POWER_TWO) {
            new Thread() {
                public void run() {
                    try {
                        Instrumentation inst = new Instrumentation();
                        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                    } catch (Exception e) {
                        Log.e("Exception when sendKeyDownUpSync", e.toString());
                    }
                }
            }.start();
        } else {
            new Thread() {
                public void run() {
                    try {
                        Instrumentation inst = new Instrumentation();
                        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_HOME);            
                    } catch (Exception e) {
                        Log.e("Exception when sendKeyDownUpSync", e.toString());
                    }
                }
            }.start();
        }    
    }