Refactor PointerTracker class

Change-Id: Id6f69e82119a9a3f618d95443a3ecc766abab08a
main
Tadashi G. Takaoka 2010-12-18 20:04:44 +09:00
parent 9e91472285
commit dbc44989a5
2 changed files with 259 additions and 198 deletions

View File

@ -23,10 +23,13 @@ import android.content.res.Resources;
import android.util.Log; import android.util.Log;
import android.view.MotionEvent; import android.view.MotionEvent;
import java.util.Arrays;
public class PointerTracker { public class PointerTracker {
private static final String TAG = "PointerTracker"; private static final String TAG = PointerTracker.class.getSimpleName();
private static final boolean DEBUG = false; private static final boolean DEBUG_EVENT = false;
private static final boolean DEBUG_MOVE = false; private static final boolean DEBUG_MOVE_EVENT = false;
private static final boolean DEBUG_LISTENER = false;
public interface UIProxy { public interface UIProxy {
public void invalidateKey(Key key); public void invalidateKey(Key key);
@ -49,7 +52,7 @@ public class PointerTracker {
private final UIProxy mProxy; private final UIProxy mProxy;
private final UIHandler mHandler; private final UIHandler mHandler;
private final KeyDetector mKeyDetector; private final KeyDetector mKeyDetector;
private KeyboardActionListener mListener; private KeyboardActionListener mListener = EMPTY_LISTENER;
private final KeyboardSwitcher mKeyboardSwitcher; private final KeyboardSwitcher mKeyboardSwitcher;
private final boolean mHasDistinctMultitouch; private final boolean mHasDistinctMultitouch;
private final boolean mConfigSlidingKeyInputEnabled; private final boolean mConfigSlidingKeyInputEnabled;
@ -58,7 +61,7 @@ public class PointerTracker {
private Key[] mKeys; private Key[] mKeys;
private int mKeyHysteresisDistanceSquared = -1; private int mKeyHysteresisDistanceSquared = -1;
private final KeyState mKeyState; private final PointerTrackerKeyState mKeyState;
// true if event is already translated to a key action (long press or mini-keyboard) // true if event is already translated to a key action (long press or mini-keyboard)
private boolean mKeyAlreadyProcessed; private boolean mKeyAlreadyProcessed;
@ -79,94 +82,27 @@ public class PointerTracker {
// pressed key // pressed key
private int mPreviousKey = NOT_A_KEY; private int mPreviousKey = NOT_A_KEY;
// This class keeps track of a key index and a position where this pointer is. // Empty {@link KeyboardActionListener}
private static class KeyState { private static final KeyboardActionListener EMPTY_LISTENER = new KeyboardActionListener() {
private final KeyDetector mKeyDetector; @Override
public void onPress(int primaryCode) {}
// The position and time at which first down event occurred. @Override
private int mStartX; public void onRelease(int primaryCode) {}
private int mStartY; @Override
private long mDownTime; public void onKey(int primaryCode, int[] keyCodes, int x, int y) {}
@Override
// The current key index where this pointer is. public void onText(CharSequence text) {}
private int mKeyIndex = NOT_A_KEY; @Override
// The position where mKeyIndex was recognized for the first time. public void onCancel() {}
private int mKeyX; @Override
private int mKeyY; public void swipeLeft() {}
@Override
// Last pointer position. public void swipeRight() {}
private int mLastX; @Override
private int mLastY; public void swipeDown() {}
@Override
public KeyState(KeyDetector keyDetecor) { public void swipeUp() {}
mKeyDetector = keyDetecor; };
}
public int getKeyIndex() {
return mKeyIndex;
}
public int getKeyX() {
return mKeyX;
}
public int getKeyY() {
return mKeyY;
}
public int getStartX() {
return mStartX;
}
public int getStartY() {
return mStartY;
}
public long getDownTime() {
return mDownTime;
}
public int getLastX() {
return mLastX;
}
public int getLastY() {
return mLastY;
}
public int onDownKey(int x, int y, long eventTime) {
mStartX = x;
mStartY = y;
mDownTime = eventTime;
return onMoveToNewKey(onMoveKeyInternal(x, y), x, y);
}
private int onMoveKeyInternal(int x, int y) {
mLastX = x;
mLastY = y;
return mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null);
}
public int onMoveKey(int x, int y) {
return onMoveKeyInternal(x, y);
}
public int onMoveToNewKey(int keyIndex, int x, int y) {
mKeyIndex = keyIndex;
mKeyX = x;
mKeyY = y;
return keyIndex;
}
public int onUpKey(int x, int y) {
return onMoveKeyInternal(x, y);
}
public void onSetKeyboard() {
mKeyIndex = mKeyDetector.getKeyIndexAndNearbyCodes(mKeyX, mKeyY, null);
}
}
public PointerTracker(int id, UIHandler handler, KeyDetector keyDetector, UIProxy proxy, public PointerTracker(int id, UIHandler handler, KeyDetector keyDetector, UIProxy proxy,
Resources res) { Resources res) {
@ -177,7 +113,7 @@ public class PointerTracker {
mHandler = handler; mHandler = handler;
mKeyDetector = keyDetector; mKeyDetector = keyDetector;
mKeyboardSwitcher = KeyboardSwitcher.getInstance(); mKeyboardSwitcher = KeyboardSwitcher.getInstance();
mKeyState = new KeyState(keyDetector); mKeyState = new PointerTrackerKeyState(keyDetector);
mHasDistinctMultitouch = proxy.hasDistinctMultitouch(); mHasDistinctMultitouch = proxy.hasDistinctMultitouch();
mConfigSlidingKeyInputEnabled = res.getBoolean(R.bool.config_sliding_key_input_enabled); mConfigSlidingKeyInputEnabled = res.getBoolean(R.bool.config_sliding_key_input_enabled);
mDelayBeforeKeyRepeatStart = res.getInteger(R.integer.config_delay_before_key_repeat_start); mDelayBeforeKeyRepeatStart = res.getInteger(R.integer.config_delay_before_key_repeat_start);
@ -191,6 +127,37 @@ public class PointerTracker {
mListener = listener; mListener = listener;
} }
private void callListenerOnPress(int primaryCode) {
if (DEBUG_LISTENER)
Log.d(TAG, "onPress : " + keyCodePrintable(primaryCode));
mListener.onPress(primaryCode);
}
private void callListenerOnKey(int primaryCode, int[] keyCodes, int x, int y) {
if (DEBUG_LISTENER)
Log.d(TAG, "onKey : " + keyCodePrintable(primaryCode)
+ " codes="+ Arrays.toString(keyCodes) + " x=" + x + " y=" + y);
mListener.onKey(primaryCode, keyCodes, x, y);
}
private void callListenerOnText(CharSequence text) {
if (DEBUG_LISTENER)
Log.d(TAG, "onText : text=" + text);
mListener.onText(text);
}
private void callListenerOnRelease(int primaryCode) {
if (DEBUG_LISTENER)
Log.d(TAG, "onRelease : " + keyCodePrintable(primaryCode));
mListener.onRelease(primaryCode);
}
private void callListenerOnCancel() {
if (DEBUG_LISTENER)
Log.d(TAG, "onCancel");
mListener.onCancel();
}
public void setKeyboard(Keyboard keyboard, Key[] keys, float keyHysteresisDistance) { public void setKeyboard(Keyboard keyboard, Key[] keys, float keyHysteresisDistance) {
if (keyboard == null || keys == null || keyHysteresisDistance < 0) if (keyboard == null || keys == null || keyHysteresisDistance < 0)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
@ -209,15 +176,16 @@ public class PointerTracker {
return isValidKeyIndex(keyIndex) ? mKeys[keyIndex] : null; return isValidKeyIndex(keyIndex) ? mKeys[keyIndex] : null;
} }
private boolean isModifierInternal(int keyIndex) { private static boolean isModifierCode(int primaryCode) {
Key key = getKey(keyIndex);
if (key == null)
return false;
int primaryCode = key.mCodes[0];
return primaryCode == Keyboard.CODE_SHIFT return primaryCode == Keyboard.CODE_SHIFT
|| primaryCode == Keyboard.CODE_SWITCH_ALPHA_SYMBOL; || primaryCode == Keyboard.CODE_SWITCH_ALPHA_SYMBOL;
} }
private boolean isModifierInternal(int keyIndex) {
final Key key = getKey(keyIndex);
return key == null ? false : isModifierCode(key.mCodes[0]);
}
public boolean isModifier() { public boolean isModifier() {
return isModifierInternal(mKeyState.getKeyIndex()); return isModifierInternal(mKeyState.getKeyIndex());
} }
@ -281,24 +249,22 @@ public class PointerTracker {
} }
public void onDownEvent(int x, int y, long eventTime) { public void onDownEvent(int x, int y, long eventTime) {
if (DEBUG) if (DEBUG_EVENT)
debugLog("onDownEvent:", x, y); printTouchEvent("onDownEvent:", x, y, eventTime);
int keyIndex = mKeyState.onDownKey(x, y, eventTime); int keyIndex = mKeyState.onDownKey(x, y, eventTime);
// Sliding key is allowed when 1) enabled by configuration, 2) this pointer starts sliding // Sliding key is allowed when 1) enabled by configuration, 2) this pointer starts sliding
// form modifier key, or 3) this pointer is on mini-keyboard. // from modifier key, or 3) this pointer is on mini-keyboard.
mIsAllowedSlidingKeyInput = mConfigSlidingKeyInputEnabled || isModifierInternal(keyIndex) mIsAllowedSlidingKeyInput = mConfigSlidingKeyInputEnabled || isModifierInternal(keyIndex)
|| mKeyDetector instanceof MiniKeyboardKeyDetector; || mKeyDetector instanceof MiniKeyboardKeyDetector;
mKeyAlreadyProcessed = false; mKeyAlreadyProcessed = false;
mIsRepeatableKey = false; mIsRepeatableKey = false;
checkMultiTap(eventTime, keyIndex); checkMultiTap(eventTime, keyIndex);
if (mListener != null) {
if (isValidKeyIndex(keyIndex)) { if (isValidKeyIndex(keyIndex)) {
mListener.onPress(mKeys[keyIndex].mCodes[0]); callListenerOnPress(mKeys[keyIndex].mCodes[0]);
// This onPress call may have changed keyboard layout and have updated mKeyIndex. // This onPress call may have changed keyboard layout and have updated mKeyIndex.
// If that's the case, mKeyIndex has been updated in setKeyboard(). // If that's the case, mKeyIndex has been updated in setKeyboard().
keyIndex = mKeyState.getKeyIndex(); keyIndex = mKeyState.getKeyIndex();
} }
}
if (isValidKeyIndex(keyIndex)) { if (isValidKeyIndex(keyIndex)) {
if (mKeys[keyIndex].mRepeatable) { if (mKeys[keyIndex].mRepeatable) {
repeatKey(keyIndex); repeatKey(keyIndex);
@ -310,32 +276,29 @@ public class PointerTracker {
showKeyPreviewAndUpdateKeyGraphics(keyIndex); showKeyPreviewAndUpdateKeyGraphics(keyIndex);
} }
public void onMoveEvent(int x, int y, @SuppressWarnings("unused") long eventTime) { public void onMoveEvent(int x, int y, long eventTime) {
if (DEBUG_MOVE) if (DEBUG_MOVE_EVENT)
debugLog("onMoveEvent:", x, y); printTouchEvent("onMoveEvent:", x, y, eventTime);
if (mKeyAlreadyProcessed) if (mKeyAlreadyProcessed)
return; return;
final KeyState keyState = mKeyState; final PointerTrackerKeyState keyState = mKeyState;
final int keyIndex = keyState.onMoveKey(x, y); final int keyIndex = keyState.onMoveKey(x, y);
final Key oldKey = getKey(keyState.getKeyIndex()); final Key oldKey = getKey(keyState.getKeyIndex());
if (isValidKeyIndex(keyIndex)) { if (isValidKeyIndex(keyIndex)) {
if (oldKey == null) { if (oldKey == null) {
// The pointer has been slid in to the new key, but the finger was not on any keys. // The pointer has been slid in to the new key, but the finger was not on any keys.
// In this case, we must call onPress() to notify that the new key is being pressed. // In this case, we must call onPress() to notify that the new key is being pressed.
if (mListener != null) callListenerOnPress(getKey(keyIndex).mCodes[0]);
mListener.onPress(getKey(keyIndex).mCodes[0]);
keyState.onMoveToNewKey(keyIndex, x, y); keyState.onMoveToNewKey(keyIndex, x, y);
startLongPressTimer(keyIndex); startLongPressTimer(keyIndex);
} else if (!isMinorMoveBounce(x, y, keyIndex)) { } else if (!isMinorMoveBounce(x, y, keyIndex)) {
// The pointer has been slid in to the new key from the previous key, we must call // The pointer has been slid in to the new key from the previous key, we must call
// onRelease() first to notify that the previous key has been released, then call // onRelease() first to notify that the previous key has been released, then call
// onPress() to notify that the new key is being pressed. // onPress() to notify that the new key is being pressed.
if (mListener != null) callListenerOnRelease(oldKey.mCodes[0]);
mListener.onRelease(oldKey.mCodes[0]);
if (mIsAllowedSlidingKeyInput) { if (mIsAllowedSlidingKeyInput) {
resetMultiTap(); resetMultiTap();
if (mListener != null) callListenerOnPress(getKey(keyIndex).mCodes[0]);
mListener.onPress(getKey(keyIndex).mCodes[0]);
keyState.onMoveToNewKey(keyIndex, x, y); keyState.onMoveToNewKey(keyIndex, x, y);
startLongPressTimer(keyIndex); startLongPressTimer(keyIndex);
} else { } else {
@ -345,12 +308,14 @@ public class PointerTracker {
} }
} }
} else { } else {
// TODO: we should check isMinorMoveDebounce() first. if (!isMinorMoveBounce(x, y, keyIndex)) {
if (oldKey != null) { resetMultiTap();
keyState.onMoveToNewKey(keyIndex, x ,y);
mHandler.cancelLongPressTimers();
} else if (oldKey != null) {
// The pointer has been slid out from the previous key, we must call onRelease() to // The pointer has been slid out from the previous key, we must call onRelease() to
// notify that the previous key has been released. // notify that the previous key has been released.
if (mListener != null) callListenerOnRelease(oldKey.mCodes[0]);
mListener.onRelease(oldKey.mCodes[0]);
if (mIsAllowedSlidingKeyInput) { if (mIsAllowedSlidingKeyInput) {
keyState.onMoveToNewKey(keyIndex, x ,y); keyState.onMoveToNewKey(keyIndex, x ,y);
mHandler.cancelLongPressTimers(); mHandler.cancelLongPressTimers();
@ -359,10 +324,6 @@ public class PointerTracker {
showKeyPreviewAndUpdateKeyGraphics(NOT_A_KEY); showKeyPreviewAndUpdateKeyGraphics(NOT_A_KEY);
return; return;
} }
} else if (!isMinorMoveBounce(x, y, keyIndex)) {
resetMultiTap();
keyState.onMoveToNewKey(keyIndex, x ,y);
mHandler.cancelLongPressTimers();
} }
} }
showKeyPreviewAndUpdateKeyGraphics(mKeyState.getKeyIndex()); showKeyPreviewAndUpdateKeyGraphics(mKeyState.getKeyIndex());
@ -371,19 +332,20 @@ public class PointerTracker {
public void onUpEvent(int pointX, int pointY, long eventTime) { public void onUpEvent(int pointX, int pointY, long eventTime) {
int x = pointX; int x = pointX;
int y = pointY; int y = pointY;
if (DEBUG) if (DEBUG_EVENT)
debugLog("onUpEvent :", x, y); printTouchEvent("onUpEvent :", x, y, eventTime);
showKeyPreviewAndUpdateKeyGraphics(NOT_A_KEY); showKeyPreviewAndUpdateKeyGraphics(NOT_A_KEY);
if (mKeyAlreadyProcessed) if (mKeyAlreadyProcessed)
return; return;
mHandler.cancelKeyTimers(); mHandler.cancelKeyTimers();
mHandler.cancelPopupPreview(); mHandler.cancelPopupPreview();
int keyIndex = mKeyState.onUpKey(x, y); final PointerTrackerKeyState keyState = mKeyState;
int keyIndex = keyState.onUpKey(x, y);
if (isMinorMoveBounce(x, y, keyIndex)) { if (isMinorMoveBounce(x, y, keyIndex)) {
// Use previous fixed key index and coordinates. // Use previous fixed key index and coordinates.
keyIndex = mKeyState.getKeyIndex(); keyIndex = keyState.getKeyIndex();
x = mKeyState.getKeyX(); x = keyState.getKeyX();
y = mKeyState.getKeyY(); y = keyState.getKeyY();
} }
if (!mIsRepeatableKey) { if (!mIsRepeatableKey) {
detectAndSendKey(keyIndex, x, y, eventTime); detectAndSendKey(keyIndex, x, y, eventTime);
@ -393,9 +355,9 @@ public class PointerTracker {
mProxy.invalidateKey(mKeys[keyIndex]); mProxy.invalidateKey(mKeys[keyIndex]);
} }
public void onCancelEvent(int x, int y, @SuppressWarnings("unused") long eventTime) { public void onCancelEvent(int x, int y, long eventTime) {
if (DEBUG) if (DEBUG_EVENT)
debugLog("onCancelEvt:", x, y); printTouchEvent("onCancelEvt:", x, y, eventTime);
mHandler.cancelKeyTimers(); mHandler.cancelKeyTimers();
mHandler.cancelPopupPreview(); mHandler.cancelPopupPreview();
showKeyPreviewAndUpdateKeyGraphics(NOT_A_KEY); showKeyPreviewAndUpdateKeyGraphics(NOT_A_KEY);
@ -472,27 +434,22 @@ 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) {
final KeyboardActionListener listener = mListener;
final Key key = getKey(index); final Key key = getKey(index);
if (key == null) { if (key == null) {
if (listener != null) callListenerOnCancel();
listener.onCancel(); return;
} else {
if (key.mOutputText != null) {
if (listener != null) {
listener.onText(key.mOutputText);
listener.onRelease(NOT_A_KEY);
} }
if (key.mOutputText != null) {
callListenerOnText(key.mOutputText);
callListenerOnRelease(NOT_A_KEY);
} else { } else {
int code = key.mCodes[0]; int code = key.mCodes[0];
//TextEntryState.keyPressedAt(key, x, y); final int[] codes = mKeyDetector.newCodeArray();
int[] codes = mKeyDetector.newCodeArray();
mKeyDetector.getKeyIndexAndNearbyCodes(x, y, codes); mKeyDetector.getKeyIndexAndNearbyCodes(x, y, codes);
// Multi-tap // Multi-tap
if (mInMultiTap) { if (mInMultiTap) {
if (mTapCount != -1) { if (mTapCount != -1) {
mListener.onKey(Keyboard.CODE_DELETE, KEY_DELETE, x, y); callListenerOnKey(Keyboard.CODE_DELETE, KEY_DELETE, x, y);
} else { } else {
mTapCount = 0; mTapCount = 0;
} }
@ -501,30 +458,24 @@ public class PointerTracker {
// If keyboard is in manual temporary upper case state and key has manual temporary // If keyboard is in manual temporary upper case state and key has manual temporary
// shift code, alternate character code should be sent. // shift code, alternate character code should be sent.
if (mKeyboard.isManualTemporaryUpperCase() if (mKeyboard.isManualTemporaryUpperCase() && key.mManualTemporaryUpperCaseCode != 0) {
&& key.mManualTemporaryUpperCaseCode != 0) {
code = key.mManualTemporaryUpperCaseCode; code = key.mManualTemporaryUpperCaseCode;
codes[0] = code; codes[0] = code;
} }
/* // Swap the first and second values in the codes array if the primary code is not the
* Swap the first and second values in the codes array if the primary code is not // first value but the second value in the array. This happens when key debouncing is
* the first value but the second value in the array. This happens when key // in effect.
* debouncing is in effect.
*/
if (codes.length >= 2 && codes[0] != code && codes[1] == code) { if (codes.length >= 2 && codes[0] != code && codes[1] == code) {
codes[1] = codes[0]; codes[1] = codes[0];
codes[0] = code; codes[0] = code;
} }
if (listener != null) { callListenerOnKey(code, codes, x, y);
listener.onKey(code, codes, x, y); callListenerOnRelease(code);
listener.onRelease(code);
}
} }
mLastSentIndex = index; mLastSentIndex = index;
mLastTapTime = eventTime; mLastTapTime = eventTime;
} }
}
/** /**
* Handle multi-tap keys by producing the key label for the current multi-tap state. * Handle multi-tap keys by producing the key label for the current multi-tap state.
@ -569,18 +520,20 @@ public class PointerTracker {
} }
} }
private void debugLog(String title, int x, int y) { private long mPreviousEventTime;
int keyIndex = mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null);
Key key = getKey(keyIndex); private void printTouchEvent(String title, int x, int y, long eventTime) {
final String code; final int keyIndex = mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null);
if (key == null) { final Key key = getKey(keyIndex);
code = "----"; final String code = (key == null) ? "----" : keyCodePrintable(key.mCodes[0]);
} else { final long delta = eventTime - mPreviousEventTime;
int primaryCode = key.mCodes[0]; Log.d(TAG, String.format("%s%s[%d] %4d %4d %5d %3d(%s)", title,
code = String.format((primaryCode < 0) ? "%4d" : "0x%02x", primaryCode); (mKeyAlreadyProcessed ? "-" : " "), mPointerId, x, y, delta, keyIndex, code));
mPreviousEventTime = eventTime;
} }
Log.d(TAG, String.format("%s%s[%d] %3d,%3d %3d(%s) %s", title,
(mKeyAlreadyProcessed ? "-" : " "), mPointerId, x, y, keyIndex, code, private static String keyCodePrintable(int primaryCode) {
(isModifier() ? "modifier" : ""))); final String modifier = isModifierCode(primaryCode) ? " modifier" : "";
return String.format((primaryCode < 0) ? "%4d" : "0x%02x", primaryCode) + modifier;
} }
} }

View File

@ -0,0 +1,108 @@
/*
* Copyright (C) 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.android.inputmethod.keyboard;
/**
* This class keeps track of a key index and a position where {@link PointerTracker} is.
*/
/* package */ class PointerTrackerKeyState {
private final KeyDetector mKeyDetector;
// The position and time at which first down event occurred.
private int mStartX;
private int mStartY;
private long mDownTime;
// The current key index where this pointer is.
private int mKeyIndex = KeyDetector.NOT_A_KEY;
// The position where mKeyIndex was recognized for the first time.
private int mKeyX;
private int mKeyY;
// Last pointer position.
private int mLastX;
private int mLastY;
public PointerTrackerKeyState(KeyDetector keyDetecor) {
mKeyDetector = keyDetecor;
}
public int getKeyIndex() {
return mKeyIndex;
}
public int getKeyX() {
return mKeyX;
}
public int getKeyY() {
return mKeyY;
}
public int getStartX() {
return mStartX;
}
public int getStartY() {
return mStartY;
}
public long getDownTime() {
return mDownTime;
}
public int getLastX() {
return mLastX;
}
public int getLastY() {
return mLastY;
}
public int onDownKey(int x, int y, long eventTime) {
mStartX = x;
mStartY = y;
mDownTime = eventTime;
return onMoveToNewKey(onMoveKeyInternal(x, y), x, y);
}
private int onMoveKeyInternal(int x, int y) {
mLastX = x;
mLastY = y;
return mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null);
}
public int onMoveKey(int x, int y) {
return onMoveKeyInternal(x, y);
}
public int onMoveToNewKey(int keyIndex, int x, int y) {
mKeyIndex = keyIndex;
mKeyX = x;
mKeyY = y;
return keyIndex;
}
public int onUpKey(int x, int y) {
return onMoveKeyInternal(x, y);
}
public void onSetKeyboard() {
mKeyIndex = mKeyDetector.getKeyIndexAndNearbyCodes(mKeyX, mKeyY, null);
}
}