Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 02b0bd77 authored by LuK1337's avatar LuK1337 Committed by Bruno Martins
Browse files

Reimplement device hardware wake keys support

Author: LuK1337 <priv.luk@gmail.com>
Date:   Mon Jun 4 10:05:37 2018 +0200

    PhoneWindowManager: Improve home button wake haptic feedback handling

    * This fixes an issue where haptic feedback is used
      when screen is off and home button wake is disabled.

    Change-Id: I7ac4c00598cedf7f174dc99629f55dc7b74b0d2a

Author: Gabriele M <moto.falcon.git@gmail.com>
Date:   Mon Sep 26 00:43:08 2016 +0200

    Fix volume keys wakeup status handling

    The same status flag is used for the three different volume keys,
    however nothing prevents users from pressing multiple keys at the
    same time. This allows to set the status flag with one volume key
    and clear it with the other volume key.

    Use one flag per key so that we never end up in an inconsistent
    state. This fixes the seldom power button issues that happen when
    the "volume wake" feature is enabled.

    Change-Id: I08f5f9ff696bef3dd840cff97d570e44ebe03e4e

Author: Martin Brabham <optedoblivion@cyngn.com>
Date:   Wed Dec 3 11:48:28 2014 -0800

    Android Policy: handle volume key event as wake key when preference is set

    Change-Id: If9a61cd65553bf00f0efda1a75b1ab75b9129090

Author: willl03 <wgangers@gmail.com>
Date:   Mon Dec 8 11:13:28 2014 -0500

    Only go HOME if screen is fully awake

    Avoid going home when hardware home button is used to wake the device on an insecure keyguard

    Change-Id: I5d5d8c4fff76967c29e70251f7b165205005ba11

Author: Matt Garnes <matt@cyngn.com>
Date:   Tue Mar 31 14:39:38 2015 -0700

    If a wake key is disabled by the user, do not wake from doze.

    Currently, any wake key will wake the device from a doze, even if that
    key has not been enabled as a wake key in Settings.

    If the device is 'dreaming' in the Doze state, check if the user has
    explicitly disabled the wake key (or never enabled the setting in the
    first place) before waking the device.

    Change-Id: I7397087c143161e8e1ddb84d0e23f6027fea0aac

Author: Michael Bestas <mikeioannina@gmail.com>
Date:   Thu Dec 18 04:26:38 2014 +0200

    Cleanup button wake settings (2/2)

    Change-Id: Ie37136cbd57c4c334321abbfa4543727e940bc43

    Keep quiet when volume keys are used to wake up device

    - Userspace will make a 'beep' with it receives a key up, so
      consume that event as well.
    - Removed wake key check in music control code as it will already
      be disabled here.

    Change-Id: I93839acd39aec8a2ee40291f833a31f6b048c9f8

    Wake Keys: enforce the wake keys overlay

    * Keys disabled as wake keys by the overlay were
      still being allowed to wake the device. This change
      enforces the hardwareWakeKeys settings.

    Change-Id: Ifde0491b2de64a7f61a101cf22f5589cb5e841e2

    Allow disabling Search/Recents button wake (2/2)

    Change-Id: I6a2ac064efc4fe85413bf0b935c28aa7bde5d672

Author: Danny Baumann <dannybaumann@web.de>
Date:   Sun Nov 30 23:04:37 2014 -0600

    fw/base: allow home button to wake device [1/2]

    Change-Id: I2b79561dcfa7e569b2e24bbabfffb11517d4d313

Change-Id: Ic294515c7200c1260ac514db23ef3778d374d727
parent f09a552f
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -1981,6 +1981,9 @@ public class KeyEvent extends InputEvent implements Parcelable {
            case KeyEvent.KEYCODE_STEM_2:
            case KeyEvent.KEYCODE_STEM_3:
            case KeyEvent.KEYCODE_WAKEUP:
            case KeyEvent.KEYCODE_VOLUME_UP:
            case KeyEvent.KEYCODE_VOLUME_DOWN:
            case KeyEvent.KEYCODE_VOLUME_MUTE:
                return true;
        }
        return false;
+137 −8
Original line number Diff line number Diff line
@@ -512,6 +512,19 @@ public class PhoneWindowManager implements WindowManagerPolicy {
    private int mDeviceHardwareKeys;
    private boolean mHandleVolumeKeysInWM;

    // Button wake control flags
    boolean mWakeOnHomeKeyPress;
    boolean mWakeOnMenuKeyPress;
    boolean mWakeOnAppSwitchKeyPress;
    boolean mWakeOnVolumeKeyPress;

    // During wakeup by volume keys, we still need to capture subsequent events
    // until the key is released. This is required since the beep sound is produced
    // post keypressed.
    boolean mVolumeDownWakeTriggered;
    boolean mVolumeUpWakeTriggered;
    boolean mVolumeMuteWakeTriggered;

    private boolean mPendingKeyguardOccluded;
    private boolean mKeyguardOccludedChanged;

@@ -844,6 +857,24 @@ public class PhoneWindowManager implements WindowManagerPolicy {
            resolver.registerContentObserver(LineageSettings.System.getUriFor(
                    LineageSettings.System.KEY_APP_SWITCH_LONG_PRESS_ACTION), false, this,
                    UserHandle.USER_ALL);
            resolver.registerContentObserver(LineageSettings.System.getUriFor(
                    LineageSettings.System.HOME_WAKE_SCREEN), false, this,
                    UserHandle.USER_ALL);
            resolver.registerContentObserver(LineageSettings.System.getUriFor(
                    LineageSettings.System.BACK_WAKE_SCREEN), false, this,
                    UserHandle.USER_ALL);
            resolver.registerContentObserver(LineageSettings.System.getUriFor(
                    LineageSettings.System.MENU_WAKE_SCREEN), false, this,
                    UserHandle.USER_ALL);
            resolver.registerContentObserver(LineageSettings.System.getUriFor(
                    LineageSettings.System.ASSIST_WAKE_SCREEN), false, this,
                    UserHandle.USER_ALL);
            resolver.registerContentObserver(LineageSettings.System.getUriFor(
                    LineageSettings.System.APP_SWITCH_WAKE_SCREEN), false, this,
                    UserHandle.USER_ALL);
            resolver.registerContentObserver(LineageSettings.System.getUriFor(
                    LineageSettings.System.VOLUME_WAKE_SCREEN), false, this,
                    UserHandle.USER_ALL);

            updateSettings();
        }
@@ -1884,10 +1915,6 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        Resources res = mContext.getResources();
        mWakeOnDpadKeyPress =
                res.getBoolean(com.android.internal.R.bool.config_wakeOnDpadKeyPress);
        mWakeOnAssistKeyPress =
                res.getBoolean(com.android.internal.R.bool.config_wakeOnAssistKeyPress);
        mWakeOnBackKeyPress =
                res.getBoolean(com.android.internal.R.bool.config_wakeOnBackKeyPress);

        // Init display burn-in protection
        boolean burnInProtectionEnabled = context.getResources().getBoolean(
@@ -2228,6 +2255,8 @@ public class PhoneWindowManager implements WindowManagerPolicy {
    public void updateSettings() {
        ContentResolver resolver = mContext.getContentResolver();
        boolean updateRotation = false;
        int mDeviceHardwareWakeKeys = mContext.getResources().getInteger(
                org.lineageos.platform.internal.R.integer.config_deviceHardwareWakeKeys);
        synchronized (mLock) {
            mEndcallBehavior = Settings.System.getIntForUser(resolver,
                    Settings.System.END_BUTTON_BEHAVIOR,
@@ -2261,6 +2290,24 @@ public class PhoneWindowManager implements WindowManagerPolicy {
            mTorchTimeout = LineageSettings.System.getIntForUser(
                    resolver, LineageSettings.System.TORCH_LONG_PRESS_POWER_TIMEOUT, 0,
                    UserHandle.USER_CURRENT);
            mWakeOnHomeKeyPress = (LineageSettings.System.getIntForUser(resolver,
                    LineageSettings.System.HOME_WAKE_SCREEN, 1, UserHandle.USER_CURRENT) == 1)
                    && ((mDeviceHardwareWakeKeys & KEY_MASK_HOME) != 0);
            mWakeOnBackKeyPress = (LineageSettings.System.getIntForUser(resolver,
                    LineageSettings.System.BACK_WAKE_SCREEN, 0, UserHandle.USER_CURRENT) == 1)
                    && ((mDeviceHardwareWakeKeys & KEY_MASK_BACK) != 0);
            mWakeOnMenuKeyPress = (LineageSettings.System.getIntForUser(resolver,
                    LineageSettings.System.MENU_WAKE_SCREEN, 0, UserHandle.USER_CURRENT) == 1)
                    && ((mDeviceHardwareWakeKeys & KEY_MASK_MENU) != 0);
            mWakeOnAssistKeyPress = (LineageSettings.System.getIntForUser(resolver,
                    LineageSettings.System.ASSIST_WAKE_SCREEN, 0, UserHandle.USER_CURRENT) == 1)
                    && ((mDeviceHardwareWakeKeys & KEY_MASK_ASSIST) != 0);
            mWakeOnAppSwitchKeyPress = (LineageSettings.System.getIntForUser(resolver,
                    LineageSettings.System.APP_SWITCH_WAKE_SCREEN, 0, UserHandle.USER_CURRENT) == 1)
                    && ((mDeviceHardwareWakeKeys & KEY_MASK_APP_SWITCH) != 0);
            mWakeOnVolumeKeyPress = (LineageSettings.System.getIntForUser(resolver,
                    LineageSettings.System.VOLUME_WAKE_SCREEN, 0, UserHandle.USER_CURRENT) == 1)
                    && ((mDeviceHardwareWakeKeys & KEY_MASK_VOLUME) != 0);

            // Configure wake gesture.
            boolean wakeGestureEnabledSetting = Settings.Secure.getIntForUser(resolver,
@@ -3740,7 +3787,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                awakenDreams();
            }
            hideRecentApps(false, true);
        } else {
        } else if (mDefaultDisplayPolicy.isScreenOnFully()) {
            // Otherwise, just launch Home
            startDockOrHome(displayId, true /*fromHomeKey*/, awakenFromDreams);
        }
@@ -3815,6 +3862,36 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        }
    }

    private void setVolumeWakeTriggered(final int keyCode, boolean triggered) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                mVolumeDownWakeTriggered = triggered;
                break;
            case KeyEvent.KEYCODE_VOLUME_UP:
                mVolumeUpWakeTriggered = triggered;
                break;
            case KeyEvent.KEYCODE_VOLUME_MUTE:
                mVolumeMuteWakeTriggered = triggered;
                break;
            default:
                Log.w(TAG, "setVolumeWakeTriggered: unexpected keyCode=" + keyCode);
        }
    }

    private boolean getVolumeWakeTriggered(final int keyCode) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                return mVolumeDownWakeTriggered;
            case KeyEvent.KEYCODE_VOLUME_UP:
                return mVolumeUpWakeTriggered;
            case KeyEvent.KEYCODE_VOLUME_MUTE:
                return mVolumeMuteWakeTriggered;
            default:
                Log.w(TAG, "getVolumeWakeTriggered: unexpected keyCode=" + keyCode);
                return false;
        }
    }

    /** {@inheritDoc} */
    @Override
    public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
@@ -3963,8 +4040,9 @@ public class PhoneWindowManager implements WindowManagerPolicy {
            // If we're currently dozing with the screen on and the keyguard showing, pass the key
            // to the application but preserve its wake key status to make sure we still move
            // from dozing to fully interactive if we would normally go from off to fully
            // interactive.
            // interactive, unless the user has explicitly disabled this wake key.
            result = ACTION_PASS_TO_USER;
            isWakeKey = isWakeKey && isWakeKeyEnabled(keyCode);
            // Since we're dispatching the input, reset the pending key
            mPendingWakeKey = PENDING_KEY_NULL;
        } else {
@@ -4023,6 +4101,17 @@ public class PhoneWindowManager implements WindowManagerPolicy {
            case KeyEvent.KEYCODE_VOLUME_DOWN:
            case KeyEvent.KEYCODE_VOLUME_UP:
            case KeyEvent.KEYCODE_VOLUME_MUTE: {
                // Eat all down & up keys when using volume wake.
                // This disables volume control, music control, and "beep" on key up.
                if (isWakeKey && mWakeOnVolumeKeyPress) {
                    setVolumeWakeTriggered(keyCode, true);
                    break;
                } else if (getVolumeWakeTriggered(keyCode) && !down) {
                    result &= ~ACTION_PASS_TO_USER;
                    setVolumeWakeTriggered(keyCode, false);
                    break;
                }

                if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
                    if (down) {
                        // Any activity on the vol down button stops the ringer toggle shortcut
@@ -4115,7 +4204,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                    // Defer special key handlings to
                    // {@link interceptKeyBeforeDispatching()}.
                    result |= ACTION_PASS_TO_USER;
                } else if ((result & ACTION_PASS_TO_USER) == 0) {
                } else if ((result & ACTION_PASS_TO_USER) == 0 && !mWakeOnVolumeKeyPress) {
                    if (mLineageButtons.handleVolumeKey(event, interactive)) {
                        break;
                    }
@@ -4129,6 +4218,15 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                break;
            }

            case KeyEvent.KEYCODE_HOME:
                if (down && !interactive) {
                    isWakeKey = mWakeOnHomeKeyPress;
                    if (!isWakeKey) {
                        useHapticFeedback = false;
                    }
                }
                break;

            case KeyEvent.KEYCODE_ENDCALL: {
                result &= ~ACTION_PASS_TO_USER;
                if (down) {
@@ -4438,6 +4536,30 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        }
    }

    /**
     * Check if the given keyCode represents a key that is considered a wake key
     * and is currently enabled by the user in Settings or for another reason.
     */
    private boolean isWakeKeyEnabled(int keyCode) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
            case KeyEvent.KEYCODE_VOLUME_DOWN:
            case KeyEvent.KEYCODE_VOLUME_MUTE:
                // Volume keys are still wake keys if the device is docked.
                return mWakeOnVolumeKeyPress ||
                        mDefaultDisplayPolicy.getDockMode() != Intent.EXTRA_DOCK_STATE_UNDOCKED;
            case KeyEvent.KEYCODE_BACK:
                return mWakeOnBackKeyPress;
            case KeyEvent.KEYCODE_MENU:
                return mWakeOnMenuKeyPress;
            case KeyEvent.KEYCODE_ASSIST:
                return mWakeOnAssistKeyPress;
            case KeyEvent.KEYCODE_APP_SWITCH:
                return mWakeOnAppSwitchKeyPress;
        }
        return true;
    }

    /**
     * When the screen is off we ignore some keys that might otherwise typically
     * be considered wake keys.  We filter them out here.
@@ -4450,7 +4572,8 @@ public class PhoneWindowManager implements WindowManagerPolicy {
            case KeyEvent.KEYCODE_VOLUME_UP:
            case KeyEvent.KEYCODE_VOLUME_DOWN:
            case KeyEvent.KEYCODE_VOLUME_MUTE:
                return mDefaultDisplayPolicy.getDockMode() != Intent.EXTRA_DOCK_STATE_UNDOCKED;
                return mWakeOnVolumeKeyPress ||
                        mDefaultDisplayPolicy.getDockMode() != Intent.EXTRA_DOCK_STATE_UNDOCKED;

            case KeyEvent.KEYCODE_MUTE:
            case KeyEvent.KEYCODE_HEADSETHOOK:
@@ -4478,6 +4601,12 @@ public class PhoneWindowManager implements WindowManagerPolicy {

            case KeyEvent.KEYCODE_BACK:
                return mWakeOnBackKeyPress;

            case KeyEvent.KEYCODE_MENU:
                return mWakeOnMenuKeyPress;

            case KeyEvent.KEYCODE_APP_SWITCH:
                return mWakeOnAppSwitchKeyPress;
        }

        return true;