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

Unverified Commit f00966e7 authored by Pranav Vashi's avatar Pranav Vashi Committed by Michael Bestas
Browse files

PhoneWindowManager: Use SingleKeyRule for app switch long press



Change-Id: Iba313f1cdb150318d084f5b5fdca481351a5f0bc
Signed-off-by: default avatarPranav Vashi <neobuddy89@gmail.com>
parent 672e4c70
Loading
Loading
Loading
Loading
+77 −45
Original line number Diff line number Diff line
@@ -668,7 +668,6 @@ public class PhoneWindowManager implements WindowManagerPolicy {

    boolean mMenuPressed;
    boolean mAssistPressed;
    boolean mAppSwitchLongPressed;
    Intent mHomeIntent;
    Intent mCarDockIntent;
    Intent mDeskDockIntent;
@@ -1719,6 +1718,36 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        }
    }

    private void appSwitchPress() {
        if (!keyguardOn() && mAppSwitchPressAction != Action.NOTHING) {
            if (mAppSwitchPressAction != Action.APP_SWITCH) {
                cancelPreloadRecentApps();
            }
            long now = SystemClock.uptimeMillis();
            KeyEvent event = new KeyEvent(now, now, KeyEvent.ACTION_DOWN,
                    KeyEvent.KEYCODE_APP_SWITCH, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
                    KeyEvent.FLAG_FROM_SYSTEM, InputDevice.SOURCE_KEYBOARD);

            performKeyAction(mAppSwitchPressAction, event);
        }
    }

    private void appSwitchLongPress() {
        if (!keyguardOn() && mAppSwitchLongPressAction != Action.NOTHING) {
            if (mAppSwitchLongPressAction != Action.APP_SWITCH) {
                cancelPreloadRecentApps();
            }

            long now = SystemClock.uptimeMillis();
            KeyEvent event = new KeyEvent(now, now, KeyEvent.ACTION_DOWN,
                    KeyEvent.KEYCODE_APP_SWITCH, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
                    KeyEvent.FLAG_FROM_SYSTEM, InputDevice.SOURCE_KEYBOARD);

            performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, "Recents - Long Press");
            performKeyAction(mAppSwitchLongPressAction, event);
        }
    }

    private void sleepPress() {
        if (mShortPressOnSleepBehavior == SHORT_PRESS_SLEEP_GO_TO_SLEEP_AND_GO_HOME) {
            launchHomeFromHotKey(DEFAULT_DISPLAY, false /* awakenDreams */,
@@ -2901,6 +2930,40 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        }
    }

    /**
     * Rule for single app switch key gesture.
     */
    private final class AppSwitchKeyRule extends SingleKeyGestureDetector.SingleKeyRule {
        AppSwitchKeyRule() {
            super(KeyEvent.KEYCODE_APP_SWITCH);
        }

        @Override
        boolean supportLongPress() {
            return mAppSwitchLongPressAction != Action.NOTHING;
        }

        @Override
        int getMaxMultiPressCount() {
            return 1;
        }

        @Override
        void onKeyGesture(@NonNull SingleKeyGestureEvent event) {
            if (event.getAction() != ACTION_COMPLETE) {
                return;
            }
            switch (event.getType()) {
                case SINGLE_KEY_GESTURE_TYPE_PRESS:
                    appSwitchPress();
                    break;
                case SINGLE_KEY_GESTURE_TYPE_LONG_PRESS:
                    appSwitchLongPress();
                    break;
            }
        }
    }

    /**
     * Rule for single stem primary key gesture.
     */
@@ -3116,6 +3179,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        mSingleKeyGestureDetector.addRule(new PowerKeyRule());
        mSingleKeyGestureDetector.addRule(new StylusTailButtonRule());
        mSingleKeyGestureDetector.addRule(new BackKeyRule());
        mSingleKeyGestureDetector.addRule(new AppSwitchKeyRule());
    }

    private void updateKeyAssignments() {
@@ -3804,40 +3868,6 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                    }
                }

                return true;
            case KeyEvent.KEYCODE_APP_SWITCH:
                if (!keyguardOn) {
                    if (down) {
                        if (mAppSwitchPressAction == Action.APP_SWITCH
                                || mAppSwitchLongPressAction == Action.APP_SWITCH) {
                            preloadRecentApps();
                        }
                        if (repeatCount == 0) {
                            mAppSwitchLongPressed = false;
                        } else if (longPress) {
                            if (!keyguardOn && mAppSwitchLongPressAction != Action.NOTHING) {
                                if (mAppSwitchLongPressAction != Action.APP_SWITCH) {
                                    cancelPreloadRecentApps();
                                }
                                performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
                                        "Recents - Long Press");
                                performKeyAction(mAppSwitchLongPressAction, event);
                                mAppSwitchLongPressed = true;
                            }
                        }
                    } else {
                        if (mAppSwitchLongPressed) {
                            mAppSwitchLongPressed = false;
                        } else {
                            if (mAppSwitchPressAction != Action.APP_SWITCH) {
                                cancelPreloadRecentApps();
                            }
                            if (!canceled) {
                                performKeyAction(mAppSwitchPressAction, event);
                            }
                        }
                    }
                }
                return true;
            case KeyEvent.KEYCODE_VOLUME_UP:
            case KeyEvent.KEYCODE_VOLUME_DOWN:
@@ -3883,7 +3913,6 @@ public class PhoneWindowManager implements WindowManagerPolicy {
    @SuppressLint("MissingPermission")
    private void initKeyGestures() {
        List<Integer> supportedGestures = new ArrayList<>(List.of(
                KeyGestureEvent.KEY_GESTURE_TYPE_APP_SWITCH,
                KeyGestureEvent.KEY_GESTURE_TYPE_LAUNCH_ASSISTANT,
                KeyGestureEvent.KEY_GESTURE_TYPE_LAUNCH_VOICE_ASSISTANT,
                KeyGestureEvent.KEY_GESTURE_TYPE_HOME,
@@ -3942,15 +3971,6 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                    showRecentApps(false);
                }
                break;
            case KeyGestureEvent.KEY_GESTURE_TYPE_APP_SWITCH:
                if (!keyguardOn) {
                    if (start) {
                        preloadRecentApps();
                    } else if (complete) {
                        toggleRecentApps();
                    }
                }
                break;
            case KeyGestureEvent.KEY_GESTURE_TYPE_LAUNCH_ASSISTANT:
            case KeyGestureEvent.KEY_GESTURE_TYPE_LAUNCH_VOICE_ASSISTANT:
                boolean isPowerLongPress = event.isLongPress() && isPowerKeyPressed;
@@ -5059,6 +5079,18 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                break;
            }

            case KeyEvent.KEYCODE_APP_SWITCH: {
                if (!keyguardOn()) {
                    if (down) {
                        if (mAppSwitchPressAction == Action.APP_SWITCH
                                || mAppSwitchLongPressAction == Action.APP_SWITCH) {
                            preloadRecentApps();
                        }
                    }
                }
                break;
            }

            case KeyEvent.KEYCODE_VOLUME_DOWN:
            case KeyEvent.KEYCODE_VOLUME_UP:
            case KeyEvent.KEYCODE_VOLUME_MUTE: {