Merge "Refactor PointerTracker to add isValidKeyIndex() predicate." into gingerbread
commit
f7d71c338f
|
@ -100,21 +100,25 @@ public class PointerTracker {
|
||||||
mKeyDebounceThresholdSquared = (int)(hysteresisPixel * hysteresisPixel);
|
mKeyDebounceThresholdSquared = (int)(hysteresisPixel * hysteresisPixel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isValidKeyIndex(int keyIndex) {
|
||||||
|
return keyIndex >= 0 && keyIndex < mKeys.length;
|
||||||
|
}
|
||||||
|
|
||||||
public Key getKey(int keyIndex) {
|
public Key getKey(int keyIndex) {
|
||||||
return (keyIndex >= 0 && keyIndex < mKeys.length) ? mKeys[keyIndex] : null;
|
return isValidKeyIndex(keyIndex) ? mKeys[keyIndex] : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateKey(int keyIndex) {
|
public void updateKey(int keyIndex) {
|
||||||
int oldKeyIndex = mPreviousKey;
|
int oldKeyIndex = mPreviousKey;
|
||||||
mPreviousKey = keyIndex;
|
mPreviousKey = keyIndex;
|
||||||
if (keyIndex != oldKeyIndex) {
|
if (keyIndex != oldKeyIndex) {
|
||||||
if (oldKeyIndex != NOT_A_KEY && oldKeyIndex < mKeys.length) {
|
if (isValidKeyIndex(oldKeyIndex)) {
|
||||||
// if new key index is not a key, old key was just released inside of the key.
|
// if new key index is not a key, old key was just released inside of the key.
|
||||||
final boolean inside = (keyIndex == NOT_A_KEY);
|
final boolean inside = (keyIndex == NOT_A_KEY);
|
||||||
mKeys[oldKeyIndex].onReleased(inside);
|
mKeys[oldKeyIndex].onReleased(inside);
|
||||||
mProxy.invalidateKey(mKeys[oldKeyIndex]);
|
mProxy.invalidateKey(mKeys[oldKeyIndex]);
|
||||||
}
|
}
|
||||||
if (keyIndex != NOT_A_KEY && keyIndex < mKeys.length) {
|
if (isValidKeyIndex(keyIndex)) {
|
||||||
mKeys[keyIndex].onPressed();
|
mKeys[keyIndex].onPressed();
|
||||||
mProxy.invalidateKey(mKeys[keyIndex]);
|
mProxy.invalidateKey(mKeys[keyIndex]);
|
||||||
}
|
}
|
||||||
|
@ -130,14 +134,14 @@ public class PointerTracker {
|
||||||
startTimeDebouncing(eventTime);
|
startTimeDebouncing(eventTime);
|
||||||
checkMultiTap(eventTime, keyIndex);
|
checkMultiTap(eventTime, keyIndex);
|
||||||
if (mListener != null) {
|
if (mListener != null) {
|
||||||
int primaryCode = (keyIndex != NOT_A_KEY) ? mKeys[keyIndex].codes[0] : 0;
|
int primaryCode = isValidKeyIndex(keyIndex) ? mKeys[keyIndex].codes[0] : 0;
|
||||||
mListener.onPress(primaryCode);
|
mListener.onPress(primaryCode);
|
||||||
}
|
}
|
||||||
if (keyIndex >= 0 && mKeys[keyIndex].repeatable) {
|
if (isValidKeyIndex(keyIndex)) {
|
||||||
|
if (mKeys[keyIndex].repeatable) {
|
||||||
repeatKey(keyIndex);
|
repeatKey(keyIndex);
|
||||||
mHandler.startKeyRepeatTimer(REPEAT_START_DELAY, keyIndex, this);
|
mHandler.startKeyRepeatTimer(REPEAT_START_DELAY, keyIndex, this);
|
||||||
}
|
}
|
||||||
if (keyIndex != NOT_A_KEY) {
|
|
||||||
mHandler.startLongPressTimer(keyIndex, LONGPRESS_TIMEOUT);
|
mHandler.startLongPressTimer(keyIndex, LONGPRESS_TIMEOUT);
|
||||||
}
|
}
|
||||||
showKeyPreviewAndUpdateKey(keyIndex);
|
showKeyPreviewAndUpdateKey(keyIndex);
|
||||||
|
@ -146,7 +150,7 @@ public class PointerTracker {
|
||||||
|
|
||||||
public void onMoveEvent(int touchX, int touchY, long eventTime) {
|
public void onMoveEvent(int touchX, int touchY, long eventTime) {
|
||||||
int keyIndex = mKeyDetector.getKeyIndexAndNearbyCodes(touchX, touchY, null);
|
int keyIndex = mKeyDetector.getKeyIndexAndNearbyCodes(touchX, touchY, null);
|
||||||
if (keyIndex != NOT_A_KEY) {
|
if (isValidKeyIndex(keyIndex)) {
|
||||||
if (mCurrentKey == NOT_A_KEY) {
|
if (mCurrentKey == NOT_A_KEY) {
|
||||||
updateTimeDebouncing(eventTime);
|
updateTimeDebouncing(eventTime);
|
||||||
mCurrentKey = keyIndex;
|
mCurrentKey = keyIndex;
|
||||||
|
@ -195,7 +199,7 @@ public class PointerTracker {
|
||||||
if (!wasInKeyRepeat && !mProxy.isMiniKeyboardOnScreen()) {
|
if (!wasInKeyRepeat && !mProxy.isMiniKeyboardOnScreen()) {
|
||||||
detectAndSendKey(mCurrentKey, touchX, touchY, eventTime);
|
detectAndSendKey(mCurrentKey, touchX, touchY, eventTime);
|
||||||
}
|
}
|
||||||
if (keyIndex != NOT_A_KEY && keyIndex < mKeys.length)
|
if (isValidKeyIndex(keyIndex))
|
||||||
mProxy.invalidateKey(mKeys[keyIndex]);
|
mProxy.invalidateKey(mKeys[keyIndex]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,16 +209,18 @@ public class PointerTracker {
|
||||||
mProxy.dismissPopupKeyboard();
|
mProxy.dismissPopupKeyboard();
|
||||||
showKeyPreviewAndUpdateKey(NOT_A_KEY);
|
showKeyPreviewAndUpdateKey(NOT_A_KEY);
|
||||||
int keyIndex = mCurrentKey;
|
int keyIndex = mCurrentKey;
|
||||||
if (keyIndex != NOT_A_KEY && keyIndex < mKeys.length)
|
if (isValidKeyIndex(keyIndex))
|
||||||
mProxy.invalidateKey(mKeys[keyIndex]);
|
mProxy.invalidateKey(mKeys[keyIndex]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void repeatKey(int keyIndex) {
|
public void repeatKey(int keyIndex) {
|
||||||
Key key = mKeys[keyIndex];
|
Key key = getKey(keyIndex);
|
||||||
// While key is repeating, because there is no need to handle multi-tap key, we can pass
|
if (key != null) {
|
||||||
// -1 as eventTime argument.
|
// While key is repeating, because there is no need to handle multi-tap key, we can
|
||||||
|
// pass -1 as eventTime argument.
|
||||||
detectAndSendKey(keyIndex, key.x, key.y, -1);
|
detectAndSendKey(keyIndex, key.x, key.y, -1);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// These package scope methods are only for debugging purpose.
|
// These package scope methods are only for debugging purpose.
|
||||||
/* package */ int getStartX() {
|
/* package */ int getStartX() {
|
||||||
|
@ -253,7 +259,7 @@ public class PointerTracker {
|
||||||
throw new IllegalStateException("keyboard and/or hysteresis not set");
|
throw new IllegalStateException("keyboard and/or hysteresis not set");
|
||||||
if (newKey == curKey) {
|
if (newKey == curKey) {
|
||||||
return true;
|
return true;
|
||||||
} else if (curKey >= 0 && curKey < mKeys.length) {
|
} else if (isValidKeyIndex(curKey)) {
|
||||||
return getSquareDistanceToKeyEdge(x, y, mKeys[curKey])
|
return getSquareDistanceToKeyEdge(x, y, mKeys[curKey])
|
||||||
< mKeyDebounceThresholdSquared;
|
< mKeyDebounceThresholdSquared;
|
||||||
} else {
|
} else {
|
||||||
|
@ -303,7 +309,7 @@ public class PointerTracker {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void detectAndSendKey(int index, int x, int y, long eventTime) {
|
private void detectAndSendKey(int index, int x, int y, long eventTime) {
|
||||||
if (index != NOT_A_KEY && index < mKeys.length) {
|
if (isValidKeyIndex(index)) {
|
||||||
final Key key = mKeys[index];
|
final Key key = mKeys[index];
|
||||||
OnKeyboardActionListener listener = mListener;
|
OnKeyboardActionListener listener = mListener;
|
||||||
if (key.text != null) {
|
if (key.text != null) {
|
||||||
|
@ -366,11 +372,15 @@ public class PointerTracker {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkMultiTap(long eventTime, int keyIndex) {
|
private void checkMultiTap(long eventTime, int keyIndex) {
|
||||||
if (keyIndex == NOT_A_KEY) return;
|
Key key = getKey(keyIndex);
|
||||||
Key key = mKeys[keyIndex];
|
if (key == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
final boolean isMultiTap =
|
||||||
|
(eventTime < mLastTapTime + MULTITAP_INTERVAL && keyIndex == mLastSentIndex);
|
||||||
if (key.codes.length > 1) {
|
if (key.codes.length > 1) {
|
||||||
mInMultiTap = true;
|
mInMultiTap = true;
|
||||||
if (eventTime < mLastTapTime + MULTITAP_INTERVAL && keyIndex == mLastSentIndex) {
|
if (isMultiTap) {
|
||||||
mTapCount = (mTapCount + 1) % key.codes.length;
|
mTapCount = (mTapCount + 1) % key.codes.length;
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
|
@ -378,7 +388,7 @@ public class PointerTracker {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (eventTime > mLastTapTime + MULTITAP_INTERVAL || keyIndex != mLastSentIndex) {
|
if (!isMultiTap) {
|
||||||
resetMultiTap();
|
resetMultiTap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue