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

Commit 0f47e693 authored by Arthur Hung's avatar Arthur Hung
Browse files

Shortcut key improvements

1. Override `equals` to compare two rules.
2. Add `removeRule` to remove existed rule.
3. Skip adding new rule if the rule already exists.

Bug: 241965800
Test: atest KeyCombinationManagerTests SingleKeyGestureTests
Change-Id: I2f922c0d3c122161bd6a7a66ab413c4627354bae
parent ffb129bc
Loading
Loading
Loading
Loading
+28 −1
Original line number Diff line number Diff line
@@ -106,16 +106,43 @@ public class KeyCombinationManager {
            return KeyEvent.keyCodeToString(mKeyCode1) + " + "
                    + KeyEvent.keyCodeToString(mKeyCode2);
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o instanceof TwoKeysCombinationRule) {
                TwoKeysCombinationRule that = (TwoKeysCombinationRule) o;
                return (mKeyCode1 == that.mKeyCode1 && mKeyCode2 == that.mKeyCode2) || (
                        mKeyCode1 == that.mKeyCode2 && mKeyCode2 == that.mKeyCode1);
            }
            return false;
        }

    public KeyCombinationManager(Handler handler) {
        @Override
        public int hashCode() {
            int result = mKeyCode1;
            result = 31 * result + mKeyCode2;
            return result;
        }
    }

    KeyCombinationManager(Handler handler) {
        mHandler = handler;
    }

    void addRule(TwoKeysCombinationRule rule) {
        if (mRules.contains(rule)) {
            throw new IllegalArgumentException("Rule : " + rule + " already exists.");
        }
        mRules.add(rule);
    }

    void removeRule(TwoKeysCombinationRule rule) {
        mRules.remove(rule);
    }

    /**
     * Check if the key event could be intercepted by combination key rule before it is dispatched
     * to a window.
+24 −0
Original line number Diff line number Diff line
@@ -151,6 +151,23 @@ public final class SingleKeyGestureDetector {
                    + ", VeryLongPress=" + supportVeryLongPress()
                    + ", MaxMultiPressCount=" + getMaxMultiPressCount();
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o instanceof SingleKeyRule) {
                SingleKeyRule that = (SingleKeyRule) o;
                return mKeyCode == that.mKeyCode;
            }
            return false;
        }

        @Override
        public int hashCode() {
            return mKeyCode;
        }
    }

    static SingleKeyGestureDetector get(Context context) {
@@ -167,9 +184,16 @@ public final class SingleKeyGestureDetector {
    }

    void addRule(SingleKeyRule rule) {
        if (mRules.contains(rule)) {
            throw new IllegalArgumentException("Rule : " + rule + " already exists.");
        }
        mRules.add(rule);
    }

    void removeRule(SingleKeyRule rule) {
        mRules.remove(rule);
    }

    void interceptKey(KeyEvent event, boolean interactive) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            // Store the non interactive state when first down.
+27 −1
Original line number Diff line number Diff line
@@ -228,4 +228,30 @@ public class KeyCombinationManagerTests {
        mKeyCombinationManager.interceptKey(firstKeyDown, true);
        assertTrue(mKeyCombinationManager.getKeyInterceptTimeout(KEYCODE_VOLUME_UP) > eventTime);
    }

    @Test
    public void testAddRemove() throws InterruptedException {
        final KeyCombinationManager.TwoKeysCombinationRule rule =
                new KeyCombinationManager.TwoKeysCombinationRule(KEYCODE_VOLUME_DOWN,
                        KEYCODE_POWER) {
                    @Override
                    void execute() {
                        mAction1Triggered.countDown();
                    }

                    @Override
                    void cancel() {
                    }
                };

        long eventTime = SystemClock.uptimeMillis();
        mKeyCombinationManager.removeRule(rule);
        pressKeys(eventTime, KEYCODE_POWER, eventTime, KEYCODE_VOLUME_DOWN);
        assertFalse(mAction1Triggered.await(SCHEDULE_TIME, TimeUnit.MILLISECONDS));

        mKeyCombinationManager.addRule(rule);
        eventTime = SystemClock.uptimeMillis();
        pressKeys(eventTime, KEYCODE_POWER, eventTime, KEYCODE_VOLUME_DOWN);
        assertTrue(mAction1Triggered.await(SCHEDULE_TIME, TimeUnit.MILLISECONDS));
    }
}
 No newline at end of file
+19 −0
Original line number Diff line number Diff line
@@ -297,4 +297,23 @@ public class SingleKeyGestureTests {
        pressKey(KEYCODE_BACK, mLongPressTime);
        assertTrue(mLongPressed.await(mWaitTimeout, TimeUnit.MILLISECONDS));
    }

    @Test
    public void testAddRemove() throws InterruptedException {
        final SingleKeyGestureDetector.SingleKeyRule rule =
                new SingleKeyGestureDetector.SingleKeyRule(KEYCODE_POWER) {
                    @Override
                    void onPress(long downTime) {
                        mShortPressed.countDown();
                    }
                };

        mDetector.removeRule(rule);
        pressKey(KEYCODE_POWER, 0 /* pressTime */);
        assertFalse(mShortPressed.await(mWaitTimeout, TimeUnit.MILLISECONDS));

        mDetector.addRule(rule);
        pressKey(KEYCODE_POWER, 0 /* pressTime */);
        assertTrue(mShortPressed.await(mWaitTimeout, TimeUnit.MILLISECONDS));
    }
}