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

Commit 97e87bb2 authored by Arthur Hung's avatar Arthur Hung Committed by Android (Google) Code Review
Browse files

Merge "Shortcut key improvements"

parents 266bef46 0f47e693
Loading
Loading
Loading
Loading
+28 −1
Original line number Original line Diff line number Diff line
@@ -106,16 +106,43 @@ public class KeyCombinationManager {
            return KeyEvent.keyCodeToString(mKeyCode1) + " + "
            return KeyEvent.keyCodeToString(mKeyCode1) + " + "
                    + KeyEvent.keyCodeToString(mKeyCode2);
                    + 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;
        mHandler = handler;
    }
    }


    void addRule(TwoKeysCombinationRule rule) {
    void addRule(TwoKeysCombinationRule rule) {
        if (mRules.contains(rule)) {
            throw new IllegalArgumentException("Rule : " + rule + " already exists.");
        }
        mRules.add(rule);
        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
     * Check if the key event could be intercepted by combination key rule before it is dispatched
     * to a window.
     * to a window.
+24 −0
Original line number Original line Diff line number Diff line
@@ -151,6 +151,23 @@ public final class SingleKeyGestureDetector {
                    + ", VeryLongPress=" + supportVeryLongPress()
                    + ", VeryLongPress=" + supportVeryLongPress()
                    + ", MaxMultiPressCount=" + getMaxMultiPressCount();
                    + ", 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) {
    static SingleKeyGestureDetector get(Context context) {
@@ -167,9 +184,16 @@ public final class SingleKeyGestureDetector {
    }
    }


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


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

    void interceptKey(KeyEvent event, boolean interactive) {
    void interceptKey(KeyEvent event, boolean interactive) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            // Store the non interactive state when first down.
            // Store the non interactive state when first down.
+27 −1
Original line number Original line Diff line number Diff line
@@ -228,4 +228,30 @@ public class KeyCombinationManagerTests {
        mKeyCombinationManager.interceptKey(firstKeyDown, true);
        mKeyCombinationManager.interceptKey(firstKeyDown, true);
        assertTrue(mKeyCombinationManager.getKeyInterceptTimeout(KEYCODE_VOLUME_UP) > eventTime);
        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 Original line Diff line number Diff line
@@ -297,4 +297,23 @@ public class SingleKeyGestureTests {
        pressKey(KEYCODE_BACK, mLongPressTime);
        pressKey(KEYCODE_BACK, mLongPressTime);
        assertTrue(mLongPressed.await(mWaitTimeout, TimeUnit.MILLISECONDS));
        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));
    }
}
}