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
This commit is contained in:
Arthur Hung
2022-08-12 09:22:55 +00:00
parent ffb129bc0e
commit 0f47e693b9
4 changed files with 98 additions and 2 deletions

View File

@@ -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;
}
@Override
public int hashCode() {
int result = mKeyCode1;
result = 31 * result + mKeyCode2;
return result;
}
}
public KeyCombinationManager(Handler handler) {
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.

View File

@@ -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.

View File

@@ -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));
}
}

View File

@@ -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));
}
}