diff options
Diffstat (limited to 'java/src/com/android/inputmethod/keyboard/PointerTracker.java')
-rw-r--r-- | java/src/com/android/inputmethod/keyboard/PointerTracker.java | 672 |
1 files changed, 420 insertions, 252 deletions
diff --git a/java/src/com/android/inputmethod/keyboard/PointerTracker.java b/java/src/com/android/inputmethod/keyboard/PointerTracker.java index 4b3fe8b8b..aa2f3af48 100644 --- a/java/src/com/android/inputmethod/keyboard/PointerTracker.java +++ b/java/src/com/android/inputmethod/keyboard/PointerTracker.java @@ -16,61 +16,104 @@ package com.android.inputmethod.keyboard; -import com.android.inputmethod.keyboard.KeyboardView.UIHandler; -import com.android.inputmethod.latin.LatinImeLogger; -import com.android.inputmethod.latin.R; - +import android.content.Context; import android.content.res.Resources; import android.util.Log; -import android.view.MotionEvent; +import com.android.inputmethod.keyboard.internal.PointerTrackerQueue; +import com.android.inputmethod.latin.LatinImeLogger; +import com.android.inputmethod.latin.R; +import com.android.inputmethod.latin.SubtypeSwitcher; + +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; public class PointerTracker { private static final String TAG = PointerTracker.class.getSimpleName(); - private static final boolean ENABLE_ASSERTION = false; private static final boolean DEBUG_EVENT = false; private static final boolean DEBUG_MOVE_EVENT = false; private static final boolean DEBUG_LISTENER = false; private static boolean DEBUG_MODE = LatinImeLogger.sDBG; - public interface UIProxy { + public interface KeyEventHandler { + /** + * Get KeyDetector object that is used for this PointerTracker. + * @return the KeyDetector object that is used for this PointerTracker + */ + public KeyDetector getKeyDetector(); + + /** + * Get KeyboardActionListener object that is used to register key code and so on. + * @return the KeyboardActionListner for this PointerTracker + */ + public KeyboardActionListener getKeyboardActionListener(); + + /** + * Get DrawingProxy object that is used for this PointerTracker. + * @return the DrawingProxy object that is used for this PointerTracker + */ + public DrawingProxy getDrawingProxy(); + + /** + * Get TimerProxy object that handles key repeat and long press timer event for this + * PointerTracker. + * @return the TimerProxy object that handles key repeat and long press timer event. + */ + public TimerProxy getTimerProxy(); + } + + public interface DrawingProxy { public void invalidateKey(Key key); - public void showPreview(int keyIndex, PointerTracker tracker); - public boolean hasDistinctMultitouch(); - public boolean isAccessibilityEnabled(); + public void showKeyPreview(int keyIndex, PointerTracker tracker); + public void cancelShowKeyPreview(PointerTracker tracker); + public void dismissKeyPreview(PointerTracker tracker); } - public final int mPointerId; + public interface TimerProxy { + public void startKeyRepeatTimer(long delay, int keyIndex, PointerTracker tracker); + public void startLongPressTimer(long delay, int keyIndex, PointerTracker tracker); + public void startLongPressShiftTimer(long delay, int keyIndex, PointerTracker tracker); + public void cancelLongPressTimers(); + public void cancelKeyTimers(); + } + private static KeyboardSwitcher sKeyboardSwitcher; + private static boolean sConfigSlidingKeyInputEnabled; // Timing constants - private final int mDelayBeforeKeyRepeatStart; - private final int mLongPressKeyTimeout; - private final int mLongPressShiftKeyTimeout; + private static int sDelayBeforeKeyRepeatStart; + private static int sLongPressKeyTimeout; + private static int sLongPressShiftKeyTimeout; + private static int sTouchNoiseThresholdMillis; + private static int sTouchNoiseThresholdDistanceSquared; - // Miscellaneous constants - private static final int NOT_A_KEY = KeyDetector.NOT_A_KEY; + private static final List<PointerTracker> sTrackers = new ArrayList<PointerTracker>(); + private static PointerTrackerQueue sPointerTrackerQueue; - private final UIProxy mProxy; - private final UIHandler mHandler; - private final KeyDetector mKeyDetector; - private KeyboardActionListener mListener = EMPTY_LISTENER; - private final KeyboardSwitcher mKeyboardSwitcher; - private final boolean mHasDistinctMultitouch; - private final boolean mConfigSlidingKeyInputEnabled; + public final int mPointerId; - private final int mTouchNoiseThresholdMillis; - private final int mTouchNoiseThresholdDistanceSquared; + private DrawingProxy mDrawingProxy; + private TimerProxy mTimerProxy; + private KeyDetector mKeyDetector; + private KeyboardActionListener mListener = EMPTY_LISTENER; private Keyboard mKeyboard; - private Key[] mKeys; - private int mKeyHysteresisDistanceSquared = -1; + private List<Key> mKeys; private int mKeyQuarterWidthSquared; - private final PointerTrackerKeyState mKeyState; + // The position and time at which first down event occurred. + private long mDownTime; + private long mUpTime; + + // 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; - // true if accessibility is enabled in the parent keyboard - private boolean mIsAccessibilityEnabled; + // Last pointer position. + private int mLastX; + private int mLastY; // true if keyboard layout has been changed. private boolean mKeyboardLayoutHasBeenChanged; @@ -82,13 +125,19 @@ public class PointerTracker { private boolean mIsRepeatableKey; // true if this pointer is in sliding key input - private boolean mIsInSlidingKeyInput; + boolean mIsInSlidingKeyInput; // true if sliding key is allowed. private boolean mIsAllowedSlidingKeyInput; - // pressed key - private int mPreviousKey = NOT_A_KEY; + // ignore modifier key if true + private boolean mIgnoreModifierKey; + + // TODO: Remove these hacking variables + // true if this pointer is in sliding language switch + private boolean mIsInSlidingLanguageSwitch; + private int mSpaceKeyIndex; + private static SubtypeSwitcher sSubtypeSwitcher; // Empty {@link KeyboardActionListener} private static final KeyboardActionListener EMPTY_LISTENER = new KeyboardActionListener() { @@ -102,46 +151,85 @@ public class PointerTracker { public void onTextInput(CharSequence text) {} @Override public void onCancelInput() {} - @Override - public void onSwipeDown() {} }; - public PointerTracker(int id, UIHandler handler, KeyDetector keyDetector, UIProxy proxy, - Resources res) { - if (proxy == null || handler == null || keyDetector == null) - throw new NullPointerException(); - mPointerId = id; - mProxy = proxy; - mHandler = handler; - mKeyDetector = keyDetector; - mKeyboardSwitcher = KeyboardSwitcher.getInstance(); - mKeyState = new PointerTrackerKeyState(keyDetector); - mIsAccessibilityEnabled = proxy.isAccessibilityEnabled(); - mHasDistinctMultitouch = proxy.hasDistinctMultitouch(); - mConfigSlidingKeyInputEnabled = res.getBoolean(R.bool.config_sliding_key_input_enabled); - mDelayBeforeKeyRepeatStart = res.getInteger(R.integer.config_delay_before_key_repeat_start); - mLongPressKeyTimeout = res.getInteger(R.integer.config_long_press_key_timeout); - mLongPressShiftKeyTimeout = res.getInteger(R.integer.config_long_press_shift_key_timeout); - mTouchNoiseThresholdMillis = res.getInteger(R.integer.config_touch_noise_threshold_millis); + public static void init(boolean hasDistinctMultitouch, Context context) { + if (hasDistinctMultitouch) { + sPointerTrackerQueue = new PointerTrackerQueue(); + } else { + sPointerTrackerQueue = null; + } + + final Resources res = context.getResources(); + sConfigSlidingKeyInputEnabled = res.getBoolean(R.bool.config_sliding_key_input_enabled); + sDelayBeforeKeyRepeatStart = res.getInteger(R.integer.config_delay_before_key_repeat_start); + sLongPressKeyTimeout = res.getInteger(R.integer.config_long_press_key_timeout); + sLongPressShiftKeyTimeout = res.getInteger(R.integer.config_long_press_shift_key_timeout); + sTouchNoiseThresholdMillis = res.getInteger(R.integer.config_touch_noise_threshold_millis); final float touchNoiseThresholdDistance = res.getDimension( R.dimen.config_touch_noise_threshold_distance); - mTouchNoiseThresholdDistanceSquared = (int)( + sTouchNoiseThresholdDistanceSquared = (int)( touchNoiseThresholdDistance * touchNoiseThresholdDistance); + sKeyboardSwitcher = KeyboardSwitcher.getInstance(); + sSubtypeSwitcher = SubtypeSwitcher.getInstance(); + } + + public static PointerTracker getPointerTracker(final int id, KeyEventHandler handler) { + final List<PointerTracker> trackers = sTrackers; + + // Create pointer trackers until we can get 'id+1'-th tracker, if needed. + for (int i = trackers.size(); i <= id; i++) { + final PointerTracker tracker = new PointerTracker(i, handler); + trackers.add(tracker); + } + + return trackers.get(id); + } + + public static boolean isAnyInSlidingKeyInput() { + return sPointerTrackerQueue != null ? sPointerTrackerQueue.isAnyInSlidingKeyInput() : false; + } + + public static void setKeyboardActionListener(KeyboardActionListener listener) { + for (final PointerTracker tracker : sTrackers) { + tracker.mListener = listener; + } + } + + public static void setKeyDetector(KeyDetector keyDetector) { + for (final PointerTracker tracker : sTrackers) { + tracker.setKeyDetectorInner(keyDetector); + // Mark that keyboard layout has been changed. + tracker.mKeyboardLayoutHasBeenChanged = true; + } } - public void setOnKeyboardActionListener(KeyboardActionListener listener) { - mListener = listener; + public static void dismissAllKeyPreviews() { + for (final PointerTracker tracker : sTrackers) { + tracker.setReleasedKeyGraphics(); + tracker.dismissKeyPreview(); + } } - public void setAccessibilityEnabled(boolean accessibilityEnabled) { - mIsAccessibilityEnabled = accessibilityEnabled; + public PointerTracker(int id, KeyEventHandler handler) { + if (handler == null) + throw new NullPointerException(); + mPointerId = id; + setKeyDetectorInner(handler.getKeyDetector()); + mListener = handler.getKeyboardActionListener(); + mDrawingProxy = handler.getDrawingProxy(); + mTimerProxy = handler.getTimerProxy(); } // Returns true if keyboard has been changed by this callback. private boolean callListenerOnPressAndCheckKeyboardLayoutChange(Key key, boolean withSliding) { + final boolean ignoreModifierKey = mIgnoreModifierKey && isModifierCode(key.mCode); if (DEBUG_LISTENER) - Log.d(TAG, "onPress : " + keyCodePrintable(key.mCode) + " sliding=" + withSliding); - if (key.mEnabled) { + Log.d(TAG, "onPress : " + keyCodePrintable(key.mCode) + " sliding=" + withSliding + + " ignoreModifier=" + ignoreModifierKey); + if (ignoreModifierKey) + return false; + if (key.isEnabled()) { mListener.onPress(key.mCode, withSliding); final boolean keyboardLayoutHasBeenChanged = mKeyboardLayoutHasBeenChanged; mKeyboardLayoutHasBeenChanged = false; @@ -153,26 +241,34 @@ public class PointerTracker { // Note that we need primaryCode argument because the keyboard may in shifted state and the // primaryCode is different from {@link Key#mCode}. private void callListenerOnCodeInput(Key key, int primaryCode, int[] keyCodes, int x, int y) { + final boolean ignoreModifierKey = mIgnoreModifierKey && isModifierCode(key.mCode); if (DEBUG_LISTENER) Log.d(TAG, "onCodeInput: " + keyCodePrintable(primaryCode) - + " codes="+ Arrays.toString(keyCodes) + " x=" + x + " y=" + y); - if (key.mEnabled) + + " codes="+ Arrays.toString(keyCodes) + " x=" + x + " y=" + y + + " ignoreModifier=" + ignoreModifierKey); + if (ignoreModifierKey) + return; + if (key.isEnabled()) mListener.onCodeInput(primaryCode, keyCodes, x, y); } private void callListenerOnTextInput(Key key) { if (DEBUG_LISTENER) Log.d(TAG, "onTextInput: text=" + key.mOutputText); - if (key.mEnabled) + if (key.isEnabled()) mListener.onTextInput(key.mOutputText); } // Note that we need primaryCode argument because the keyboard may in shifted state and the // primaryCode is different from {@link Key#mCode}. private void callListenerOnRelease(Key key, int primaryCode, boolean withSliding) { + final boolean ignoreModifierKey = mIgnoreModifierKey && isModifierCode(key.mCode); if (DEBUG_LISTENER) - Log.d(TAG, "onRelease : " + keyCodePrintable(primaryCode) + " sliding=" + withSliding); - if (key.mEnabled) + Log.d(TAG, "onRelease : " + keyCodePrintable(primaryCode) + " sliding=" + + withSliding + " ignoreModifier=" + ignoreModifierKey); + if (ignoreModifierKey) + return; + if (key.isEnabled()) mListener.onRelease(primaryCode, withSliding); } @@ -182,16 +278,12 @@ public class PointerTracker { mListener.onCancelInput(); } - public void setKeyboard(Keyboard keyboard, Key[] keys, float keyHysteresisDistance) { - if (keyboard == null || keys == null || keyHysteresisDistance < 0) - throw new IllegalArgumentException(); - mKeyboard = keyboard; - mKeys = keys; - mKeyHysteresisDistanceSquared = (int)(keyHysteresisDistance * keyHysteresisDistance); - final int keyQuarterWidth = keyboard.getKeyWidth() / 4; + public void setKeyDetectorInner(KeyDetector keyDetector) { + mKeyDetector = keyDetector; + mKeyboard = keyDetector.getKeyboard(); + mKeys = mKeyboard.getKeys(); + final int keyQuarterWidth = mKeyboard.getKeyWidth() / 4; mKeyQuarterWidthSquared = keyQuarterWidth * keyQuarterWidth; - // Mark that keyboard layout has been changed. - mKeyboardLayoutHasBeenChanged = true; } public boolean isInSlidingKeyInput() { @@ -199,11 +291,11 @@ public class PointerTracker { } private boolean isValidKeyIndex(int keyIndex) { - return keyIndex >= 0 && keyIndex < mKeys.length; + return keyIndex >= 0 && keyIndex < mKeys.size(); } public Key getKey(int keyIndex) { - return isValidKeyIndex(keyIndex) ? mKeys[keyIndex] : null; + return isValidKeyIndex(keyIndex) ? mKeys.get(keyIndex) : null; } private static boolean isModifierCode(int primaryCode) { @@ -217,7 +309,7 @@ public class PointerTracker { } public boolean isModifier() { - return isModifierInternal(mKeyState.getKeyIndex()); + return isModifierInternal(mKeyIndex); } private boolean isOnModifierKey(int x, int y) { @@ -229,84 +321,99 @@ public class PointerTracker { return key != null && key.mCode == Keyboard.CODE_SHIFT; } + public int getKeyIndexOn(int x, int y) { + return mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null); + } + public boolean isSpaceKey(int keyIndex) { Key key = getKey(keyIndex); return key != null && key.mCode == Keyboard.CODE_SPACE; } - public void releaseKey() { - updateKeyGraphics(NOT_A_KEY); + public void setReleasedKeyGraphics() { + setReleasedKeyGraphics(mKeyIndex); } - private void updateKeyGraphics(int keyIndex) { - int oldKeyIndex = mPreviousKey; - mPreviousKey = keyIndex; - if (keyIndex != oldKeyIndex) { - if (isValidKeyIndex(oldKeyIndex)) { - // if new key index is not a key, old key was just released inside of the key. - final boolean inside = (keyIndex == NOT_A_KEY); - mKeys[oldKeyIndex].onReleased(inside); - mProxy.invalidateKey(mKeys[oldKeyIndex]); - } - if (isValidKeyIndex(keyIndex)) { - mKeys[keyIndex].onPressed(); - mProxy.invalidateKey(mKeys[keyIndex]); - } + private void setReleasedKeyGraphics(int keyIndex) { + final Key key = getKey(keyIndex); + if (key != null) { + key.onReleased(); + mDrawingProxy.invalidateKey(key); } } - public void setAlreadyProcessed() { - mKeyAlreadyProcessed = true; + private void setPressedKeyGraphics(int keyIndex) { + final Key key = getKey(keyIndex); + if (key != null && key.isEnabled()) { + key.onPressed(); + mDrawingProxy.invalidateKey(key); + } } - private void checkAssertion(PointerTrackerQueue queue) { - if (mHasDistinctMultitouch && queue == null) - throw new RuntimeException( - "PointerTrackerQueue must be passed on distinct multi touch device"); - if (!mHasDistinctMultitouch && queue != null) - throw new RuntimeException( - "PointerTrackerQueue must be null on non-distinct multi touch device"); - } - - public void onTouchEvent(int action, int x, int y, long eventTime, PointerTrackerQueue queue) { - switch (action) { - case MotionEvent.ACTION_MOVE: - onMoveEvent(x, y, eventTime, queue); - break; - case MotionEvent.ACTION_DOWN: - case MotionEvent.ACTION_POINTER_DOWN: - onDownEvent(x, y, eventTime, queue); - break; - case MotionEvent.ACTION_UP: - case MotionEvent.ACTION_POINTER_UP: - onUpEvent(x, y, eventTime, queue); - break; - case MotionEvent.ACTION_CANCEL: - onCancelEvent(x, y, eventTime, queue); - break; - } + public int getLastX() { + return mLastX; } - public void onDownEvent(int x, int y, long eventTime, PointerTrackerQueue queue) { - if (ENABLE_ASSERTION) checkAssertion(queue); + public int getLastY() { + return mLastY; + } + + public long getDownTime() { + return mDownTime; + } + + private int onDownKey(int x, int y, long eventTime) { + 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); + } + + private int onMoveKey(int x, int y) { + return onMoveKeyInternal(x, y); + } + + private int onMoveToNewKey(int keyIndex, int x, int y) { + mKeyIndex = keyIndex; + mKeyX = x; + mKeyY = y; + return keyIndex; + } + + private int onUpKey(int x, int y, long eventTime) { + mUpTime = eventTime; + mKeyIndex = KeyDetector.NOT_A_KEY; + return onMoveKeyInternal(x, y); + } + + public void onDownEvent(int x, int y, long eventTime, KeyEventHandler handler) { if (DEBUG_EVENT) printTouchEvent("onDownEvent:", x, y, eventTime); + mDrawingProxy = handler.getDrawingProxy(); + mTimerProxy = handler.getTimerProxy(); + setKeyboardActionListener(handler.getKeyboardActionListener()); + setKeyDetectorInner(handler.getKeyDetector()); // Naive up-to-down noise filter. - final long deltaT = eventTime - mKeyState.getUpTime(); - if (deltaT < mTouchNoiseThresholdMillis) { - final int dx = x - mKeyState.getLastX(); - final int dy = y - mKeyState.getLastY(); + final long deltaT = eventTime - mUpTime; + if (deltaT < sTouchNoiseThresholdMillis) { + final int dx = x - mLastX; + final int dy = y - mLastY; final int distanceSquared = (dx * dx + dy * dy); - if (distanceSquared < mTouchNoiseThresholdDistanceSquared) { + if (distanceSquared < sTouchNoiseThresholdDistanceSquared) { if (DEBUG_MODE) Log.w(TAG, "onDownEvent: ignore potential noise: time=" + deltaT + " distance=" + distanceSquared); - setAlreadyProcessed(); + mKeyAlreadyProcessed = true; return; } } + final PointerTrackerQueue queue = sPointerTrackerQueue; if (queue != null) { if (isOnModifierKey(x, y)) { // Before processing a down event of modifier key, all pointers already being @@ -319,48 +426,54 @@ public class PointerTracker { } private void onDownEventInternal(int x, int y, long eventTime) { - int keyIndex = mKeyState.onDownKey(x, y, eventTime); + int keyIndex = onDownKey(x, y, eventTime); // Sliding key is allowed when 1) enabled by configuration, 2) this pointer starts sliding - // from modifier key, 3) this pointer is on mini-keyboard, or 4) accessibility is enabled. - mIsAllowedSlidingKeyInput = mConfigSlidingKeyInputEnabled || isModifierInternal(keyIndex) - || mKeyDetector instanceof MiniKeyboardKeyDetector - || mIsAccessibilityEnabled; + // from modifier key, or 3) this pointer is on mini-keyboard. + mIsAllowedSlidingKeyInput = sConfigSlidingKeyInputEnabled || isModifierInternal(keyIndex) + || mKeyDetector instanceof MiniKeyboardKeyDetector; mKeyboardLayoutHasBeenChanged = false; mKeyAlreadyProcessed = false; mIsRepeatableKey = false; mIsInSlidingKeyInput = false; + mIsInSlidingLanguageSwitch = false; + mIgnoreModifierKey = false; if (isValidKeyIndex(keyIndex)) { // This onPress call may have changed keyboard layout. Those cases are detected at // {@link #setKeyboard}. In those cases, we should update keyIndex according to the new // keyboard layout. - if (callListenerOnPressAndCheckKeyboardLayoutChange(mKeys[keyIndex], false)) - keyIndex = mKeyState.onDownKey(x, y, eventTime); - } - if (isValidKeyIndex(keyIndex)) { - // Accessibility disables key repeat because users may need to pause on a key to hear - // its spoken description. - if (mKeys[keyIndex].mRepeatable && !mIsAccessibilityEnabled) { - repeatKey(keyIndex); - mHandler.startKeyRepeatTimer(mDelayBeforeKeyRepeatStart, keyIndex, this); - mIsRepeatableKey = true; - } + if (callListenerOnPressAndCheckKeyboardLayoutChange(getKey(keyIndex), false)) + keyIndex = onDownKey(x, y, eventTime); + + startRepeatKey(keyIndex); startLongPressTimer(keyIndex); + showKeyPreview(keyIndex); + setPressedKeyGraphics(keyIndex); } - showKeyPreviewAndUpdateKeyGraphics(keyIndex); } - public void onMoveEvent(int x, int y, long eventTime, PointerTrackerQueue queue) { - if (ENABLE_ASSERTION) checkAssertion(queue); + private void startSlidingKeyInput(Key key) { + if (!mIsInSlidingKeyInput) + mIgnoreModifierKey = isModifierCode(key.mCode); + mIsInSlidingKeyInput = true; + } + + public void onMoveEvent(int x, int y, long eventTime) { if (DEBUG_MOVE_EVENT) printTouchEvent("onMoveEvent:", x, y, eventTime); if (mKeyAlreadyProcessed) return; - final PointerTrackerKeyState keyState = mKeyState; - final int lastX = keyState.getLastX(); - final int lastY = keyState.getLastY(); - int keyIndex = keyState.onMoveKey(x, y); - final Key oldKey = getKey(keyState.getKeyIndex()); + // TODO: Remove this hacking code + if (mIsInSlidingLanguageSwitch) { + ((LatinKeyboard)mKeyboard).updateSpacebarPreviewIcon(x - mKeyX); + showKeyPreview(mSpaceKeyIndex); + return; + } + final int lastX = mLastX; + final int lastY = mLastY; + final int oldKeyIndex = mKeyIndex; + final Key oldKey = getKey(oldKeyIndex); + int keyIndex = onMoveKey(x, y); if (isValidKeyIndex(keyIndex)) { if (oldKey == null) { // The pointer has been slid in to the new key, but the finger was not on any keys. @@ -369,24 +482,30 @@ public class PointerTracker { // {@link #setKeyboard}. In those cases, we should update keyIndex according to the // new keyboard layout. if (callListenerOnPressAndCheckKeyboardLayoutChange(getKey(keyIndex), true)) - keyIndex = keyState.onMoveKey(x, y); - keyState.onMoveToNewKey(keyIndex, x, y); + keyIndex = onMoveKey(x, y); + onMoveToNewKey(keyIndex, x, y); startLongPressTimer(keyIndex); - } else if (!isMinorMoveBounce(x, y, keyIndex)) { + showKeyPreview(keyIndex); + setPressedKeyGraphics(keyIndex); + } else if (isMajorEnoughMoveToBeOnNewKey(x, y, keyIndex)) { // 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 // onPress() to notify that the new key is being pressed. - mIsInSlidingKeyInput = true; + setReleasedKeyGraphics(oldKeyIndex); callListenerOnRelease(oldKey, oldKey.mCode, true); - mHandler.cancelLongPressTimers(); + startSlidingKeyInput(oldKey); + mTimerProxy.cancelKeyTimers(); + startRepeatKey(keyIndex); if (mIsAllowedSlidingKeyInput) { // This onPress call may have changed keyboard layout. Those cases are detected // at {@link #setKeyboard}. In those cases, we should update keyIndex according // to the new keyboard layout. if (callListenerOnPressAndCheckKeyboardLayoutChange(getKey(keyIndex), true)) - keyIndex = keyState.onMoveKey(x, y); - keyState.onMoveToNewKey(keyIndex, x, y); + keyIndex = onMoveKey(x, y); + onMoveToNewKey(keyIndex, x, y); startLongPressTimer(keyIndex); + setPressedKeyGraphics(keyIndex); + showKeyPreview(keyIndex); } else { // HACK: On some devices, quick successive touches may be translated to sudden // move by touch panel firmware. This hack detects the case and translates the @@ -398,165 +517,219 @@ public class PointerTracker { if (DEBUG_MODE) Log.w(TAG, String.format("onMoveEvent: sudden move is translated to " + "up[%d,%d]/down[%d,%d] events", lastX, lastY, x, y)); - onUpEventInternal(lastX, lastY, eventTime); + onUpEventInternal(lastX, lastY, eventTime, true); onDownEventInternal(x, y, eventTime); } else { - setAlreadyProcessed(); - showKeyPreviewAndUpdateKeyGraphics(NOT_A_KEY); + mKeyAlreadyProcessed = true; + dismissKeyPreview(); + setReleasedKeyGraphics(oldKeyIndex); + } + } + } + // TODO: Remove this hack code + else if (isSpaceKey(keyIndex) && !mIsInSlidingLanguageSwitch + && mKeyboard instanceof LatinKeyboard) { + final LatinKeyboard keyboard = ((LatinKeyboard)mKeyboard); + if (sSubtypeSwitcher.useSpacebarLanguageSwitcher() + && sSubtypeSwitcher.getEnabledKeyboardLocaleCount() > 1) { + final int diff = x - mKeyX; + if (keyboard.shouldTriggerSpacebarSlidingLanguageSwitch(diff)) { + // Detect start sliding language switch. + mIsInSlidingLanguageSwitch = true; + mSpaceKeyIndex = keyIndex; + keyboard.updateSpacebarPreviewIcon(diff); + // Display spacebar slide language switcher. + showKeyPreview(keyIndex); + final PointerTrackerQueue queue = sPointerTrackerQueue; + if (queue != null) + queue.releaseAllPointersExcept(this, eventTime, true); } - return; } } } else { - if (oldKey != null && !isMinorMoveBounce(x, y, keyIndex)) { + if (oldKey != null && isMajorEnoughMoveToBeOnNewKey(x, y, keyIndex)) { // The pointer has been slid out from the previous key, we must call onRelease() to // notify that the previous key has been released. - mIsInSlidingKeyInput = true; + setReleasedKeyGraphics(oldKeyIndex); callListenerOnRelease(oldKey, oldKey.mCode, true); - mHandler.cancelLongPressTimers(); + startSlidingKeyInput(oldKey); + mTimerProxy.cancelLongPressTimers(); if (mIsAllowedSlidingKeyInput) { - keyState.onMoveToNewKey(keyIndex, x ,y); + onMoveToNewKey(keyIndex, x, y); } else { - setAlreadyProcessed(); - showKeyPreviewAndUpdateKeyGraphics(NOT_A_KEY); - return; + mKeyAlreadyProcessed = true; + dismissKeyPreview(); } } } - showKeyPreviewAndUpdateKeyGraphics(mKeyState.getKeyIndex()); } - public void onUpEvent(int x, int y, long eventTime, PointerTrackerQueue queue) { - if (ENABLE_ASSERTION) checkAssertion(queue); + public void onUpEvent(int x, int y, long eventTime) { if (DEBUG_EVENT) printTouchEvent("onUpEvent :", x, y, eventTime); + final PointerTrackerQueue queue = sPointerTrackerQueue; if (queue != null) { if (isModifier()) { // Before processing an up event of modifier key, all pointers already being // tracked should be released. - queue.releaseAllPointersExcept(this, eventTime); + queue.releaseAllPointersExcept(this, eventTime, true); } else { queue.releaseAllPointersOlderThan(this, eventTime); } queue.remove(this); } - onUpEventInternal(x, y, eventTime); + onUpEventInternal(x, y, eventTime, true); } - public void onUpEventForRelease(int x, int y, long eventTime) { - onUpEventInternal(x, y, eventTime); + // Let this pointer tracker know that one of newer-than-this pointer trackers got an up event. + // This pointer tracker needs to keep the key top graphics "pressed", but needs to get a + // "virtual" up event. + public void onPhantomUpEvent(int x, int y, long eventTime, boolean updateReleasedKeyGraphics) { + if (DEBUG_EVENT) + printTouchEvent("onPhntEvent:", x, y, eventTime); + onUpEventInternal(x, y, eventTime, updateReleasedKeyGraphics); + mKeyAlreadyProcessed = true; } - private void onUpEventInternal(int pointX, int pointY, long eventTime) { - int x = pointX; - int y = pointY; - mHandler.cancelKeyTimers(); - mHandler.cancelPopupPreview(); - showKeyPreviewAndUpdateKeyGraphics(NOT_A_KEY); + private void onUpEventInternal(int x, int y, long eventTime, + boolean updateReleasedKeyGraphics) { + mTimerProxy.cancelKeyTimers(); + mDrawingProxy.cancelShowKeyPreview(this); mIsInSlidingKeyInput = false; + final int keyX, keyY; + if (isMajorEnoughMoveToBeOnNewKey(x, y, onMoveKey(x, y))) { + keyX = x; + keyY = y; + } else { + // Use previous fixed key coordinates. + keyX = mKeyX; + keyY = mKeyY; + } + final int keyIndex = onUpKey(keyX, keyY, eventTime); + dismissKeyPreview(); + if (updateReleasedKeyGraphics) + setReleasedKeyGraphics(keyIndex); if (mKeyAlreadyProcessed) return; - final PointerTrackerKeyState keyState = mKeyState; - int keyIndex = keyState.onUpKey(x, y, eventTime); - if (isMinorMoveBounce(x, y, keyIndex)) { - // Use previous fixed key index and coordinates. - keyIndex = keyState.getKeyIndex(); - x = keyState.getKeyX(); - y = keyState.getKeyY(); + // TODO: Remove this hacking code + if (mIsInSlidingLanguageSwitch) { + setReleasedKeyGraphics(mSpaceKeyIndex); + final int languageDir = ((LatinKeyboard)mKeyboard).getLanguageChangeDirection(); + if (languageDir != 0) { + final int code = (languageDir == 1) + ? LatinKeyboard.CODE_NEXT_LANGUAGE : LatinKeyboard.CODE_PREV_LANGUAGE; + // This will change keyboard layout. + mListener.onCodeInput(code, new int[] {code}, keyX, keyY); + } + mIsInSlidingLanguageSwitch = false; + ((LatinKeyboard)mKeyboard).setSpacebarSlidingLanguageSwitchDiff(0); + return; } if (!mIsRepeatableKey) { - detectAndSendKey(keyIndex, x, y); + detectAndSendKey(keyIndex, keyX, keyY); } + } - if (isValidKeyIndex(keyIndex)) - mProxy.invalidateKey(mKeys[keyIndex]); + public void onLongPressed() { + mKeyAlreadyProcessed = true; + setReleasedKeyGraphics(); + dismissKeyPreview(); + final PointerTrackerQueue queue = sPointerTrackerQueue; + if (queue != null) { + queue.remove(this); + } } - public void onCancelEvent(int x, int y, long eventTime, PointerTrackerQueue queue) { - if (ENABLE_ASSERTION) checkAssertion(queue); + public void onCancelEvent(int x, int y, long eventTime) { if (DEBUG_EVENT) printTouchEvent("onCancelEvt:", x, y, eventTime); - if (queue != null) + final PointerTrackerQueue queue = sPointerTrackerQueue; + if (queue != null) { + queue.releaseAllPointersExcept(this, eventTime, true); queue.remove(this); + } onCancelEventInternal(); } private void onCancelEventInternal() { - mHandler.cancelKeyTimers(); - mHandler.cancelPopupPreview(); - showKeyPreviewAndUpdateKeyGraphics(NOT_A_KEY); + mTimerProxy.cancelKeyTimers(); + mDrawingProxy.cancelShowKeyPreview(this); + dismissKeyPreview(); + setReleasedKeyGraphics(mKeyIndex); mIsInSlidingKeyInput = false; - int keyIndex = mKeyState.getKeyIndex(); - if (isValidKeyIndex(keyIndex)) - mProxy.invalidateKey(mKeys[keyIndex]); } - public void repeatKey(int keyIndex) { + private void startRepeatKey(int keyIndex) { + final Key key = getKey(keyIndex); + if (key != null && key.mRepeatable) { + dismissKeyPreview(); + onRepeatKey(keyIndex); + mTimerProxy.startKeyRepeatTimer(sDelayBeforeKeyRepeatStart, keyIndex, this); + mIsRepeatableKey = true; + } else { + mIsRepeatableKey = false; + } + } + + public void onRepeatKey(int keyIndex) { Key key = getKey(keyIndex); if (key != null) { detectAndSendKey(keyIndex, key.mX, key.mY); } } - public int getLastX() { - return mKeyState.getLastX(); - } - - public int getLastY() { - return mKeyState.getLastY(); - } - - public long getDownTime() { - return mKeyState.getDownTime(); - } - - private boolean isMinorMoveBounce(int x, int y, int newKey) { - if (mKeys == null || mKeyHysteresisDistanceSquared < 0) - throw new IllegalStateException("keyboard and/or hysteresis not set"); - int curKey = mKeyState.getKeyIndex(); + private boolean isMajorEnoughMoveToBeOnNewKey(int x, int y, int newKey) { + if (mKeys == null || mKeyDetector == null) + throw new NullPointerException("keyboard and/or key detector not set"); + int curKey = mKeyIndex; if (newKey == curKey) { - return true; + return false; } else if (isValidKeyIndex(curKey)) { - return mKeys[curKey].squaredDistanceToEdge(x, y) < mKeyHysteresisDistanceSquared; + return mKeys.get(curKey).squaredDistanceToEdge(x, y) + >= mKeyDetector.getKeyHysteresisDistanceSquared(); } else { - return false; + return true; } } - private void showKeyPreviewAndUpdateKeyGraphics(int keyIndex) { - updateKeyGraphics(keyIndex); - // The modifier key, such as shift key, should not be shown as preview when multi-touch is - // supported. On the other hand, if multi-touch is not supported, the modifier key should - // be shown as preview. If accessibility is turned on, the modifier key should be shown as - // preview. - if (mHasDistinctMultitouch && isModifier() && !mIsAccessibilityEnabled) { - mProxy.showPreview(NOT_A_KEY, this); - } else { - mProxy.showPreview(keyIndex, this); - } + // The modifier key, such as shift key, should not show its key preview. + private boolean isKeyPreviewNotRequired(int keyIndex) { + final Key key = getKey(keyIndex); + if (key == null || !key.isEnabled()) + return true; + // Such as spacebar sliding language switch. + if (mKeyboard.needSpacebarPreview(keyIndex)) + return false; + final int code = key.mCode; + return isModifierCode(code) || code == Keyboard.CODE_DELETE + || code == Keyboard.CODE_ENTER || code == Keyboard.CODE_SPACE; } - private void startLongPressTimer(int keyIndex) { - // Accessibility disables long press because users are likely to need to pause on a key - // for an unspecified duration in order to hear the key's spoken description. - if (mIsAccessibilityEnabled) { + private void showKeyPreview(int keyIndex) { + if (isKeyPreviewNotRequired(keyIndex)) return; - } + mDrawingProxy.showKeyPreview(keyIndex, this); + } + + private void dismissKeyPreview() { + mDrawingProxy.dismissKeyPreview(this); + } + + private void startLongPressTimer(int keyIndex) { Key key = getKey(keyIndex); if (key.mCode == Keyboard.CODE_SHIFT) { - mHandler.startLongPressShiftTimer(mLongPressShiftKeyTimeout, keyIndex, this); - } else if (key.mManualTemporaryUpperCaseCode != Keyboard.CODE_DUMMY - && mKeyboard.isManualTemporaryUpperCase()) { + mTimerProxy.startLongPressShiftTimer(sLongPressShiftKeyTimeout, keyIndex, this); + } else if (key.hasUppercaseLetter() && mKeyboard.isManualTemporaryUpperCase()) { // We need not start long press timer on the key which has manual temporary upper case // code defined and the keyboard is in manual temporary upper case mode. return; - } else if (mKeyboardSwitcher.isInMomentaryAutoModeSwitchState()) { + } else if (sKeyboardSwitcher.isInMomentarySwitchState()) { // We use longer timeout for sliding finger input started from the symbols mode key. - mHandler.startLongPressTimer(mLongPressKeyTimeout * 3, keyIndex, this); + mTimerProxy.startLongPressTimer(sLongPressKeyTimeout * 3, keyIndex, this); } else { - mHandler.startLongPressTimer(mLongPressKeyTimeout, keyIndex, this); + mTimerProxy.startLongPressTimer(sLongPressKeyTimeout, keyIndex, this); } } @@ -575,10 +748,9 @@ public class PointerTracker { mKeyDetector.getKeyIndexAndNearbyCodes(x, y, codes); // If keyboard is in manual temporary upper case state and key has manual temporary - // shift code, alternate character code should be sent. - if (mKeyboard.isManualTemporaryUpperCase() - && key.mManualTemporaryUpperCaseCode != Keyboard.CODE_DUMMY) { - code = key.mManualTemporaryUpperCaseCode; + // uppercase letter as key hint letter, alternate character code should be sent. + if (mKeyboard.isManualTemporaryUpperCase() && key.hasUppercaseLetter()) { + code = key.mHintLabel.charAt(0); codes[0] = code; } @@ -594,10 +766,6 @@ public class PointerTracker { } } - public CharSequence getPreviewText(Key key) { - return key.mLabel; - } - private long mPreviousEventTime; private void printTouchEvent(String title, int x, int y, long eventTime) { |