diff options
Diffstat (limited to 'java/src')
-rw-r--r-- | java/src/com/android/inputmethod/accessibility/KeyboardAccessibilityDelegate.java | 168 | ||||
-rw-r--r-- | java/src/com/android/inputmethod/latin/SuggestedWords.java | 6 |
2 files changed, 118 insertions, 56 deletions
diff --git a/java/src/com/android/inputmethod/accessibility/KeyboardAccessibilityDelegate.java b/java/src/com/android/inputmethod/accessibility/KeyboardAccessibilityDelegate.java index 0ebad9774..7cae9861c 100644 --- a/java/src/com/android/inputmethod/accessibility/KeyboardAccessibilityDelegate.java +++ b/java/src/com/android/inputmethod/accessibility/KeyboardAccessibilityDelegate.java @@ -17,11 +17,11 @@ package com.android.inputmethod.accessibility; import android.content.Context; -import android.os.SystemClock; import android.support.v4.view.AccessibilityDelegateCompat; import android.support.v4.view.ViewCompat; import android.support.v4.view.accessibility.AccessibilityEventCompat; import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat; +import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.ViewParent; @@ -31,6 +31,7 @@ import com.android.inputmethod.keyboard.Key; import com.android.inputmethod.keyboard.KeyDetector; import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.KeyboardView; +import com.android.inputmethod.keyboard.PointerTracker; public class KeyboardAccessibilityDelegate<KV extends KeyboardView> extends AccessibilityDelegateCompat { @@ -66,10 +67,18 @@ public class KeyboardAccessibilityDelegate<KV extends KeyboardView> mKeyboard = keyboard; } - protected Keyboard getKeyboard() { + protected final Keyboard getKeyboard() { return mKeyboard; } + protected final void setCurrentHoverKey(final Key key) { + mCurrentHoverKey = key; + } + + protected final Key getCurrentHoverKey() { + return mCurrentHoverKey; + } + /** * Sends a window state change event with the specified string resource id. * @@ -128,83 +137,126 @@ public class KeyboardAccessibilityDelegate<KV extends KeyboardView> } /** + * Get a key that a hover event is on. + * + * @param event The hover event. + * @return key The key that the <code>event</code> is on. + */ + protected final Key getHoverKey(final MotionEvent event) { + final int actionIndex = event.getActionIndex(); + final int x = (int)event.getX(actionIndex); + final int y = (int)event.getY(actionIndex); + return mKeyDetector.detectHitKey(x, y); + } + + /** * Receives hover events when touch exploration is turned on in SDK versions ICS and higher. * * @param event The hover event. * @return {@code true} if the event is handled. */ public boolean onHoverEvent(final MotionEvent event) { - final int x = (int) event.getX(); - final int y = (int) event.getY(); - final Key previousKey = mCurrentHoverKey; - final Key key = mKeyDetector.detectHitKey(x, y); - mCurrentHoverKey = key; - - switch (event.getAction()) { - case MotionEvent.ACTION_HOVER_EXIT: - // Make sure we're not getting an EXIT event because the user slid - // off the keyboard area, then force a key press. - if (key != null) { - final long downTime = simulateKeyPress(key); - simulateKeyRelease(key, downTime); - onHoverExitKey(key); - } - mCurrentHoverKey = null; - break; + switch (event.getActionMasked()) { case MotionEvent.ACTION_HOVER_ENTER: - if (key != null) { - onHoverEnterKey(key); - } + onHoverEnter(event); break; case MotionEvent.ACTION_HOVER_MOVE: - if (key != previousKey) { - if (previousKey != null) { - onHoverExitKey(previousKey); - } - if (key != null) { - onHoverEnterKey(key); - } + onHoverMove(event); + break; + case MotionEvent.ACTION_HOVER_EXIT: + onHoverExit(event); + break; + default: + Log.w(getClass().getSimpleName(), "Unknown hover event: " + event); + break; + } + return true; + } + + /** + * Process {@link MotionEvent#ACTION_HOVER_ENTER} event. + * + * @param event A hover enter event. + */ + protected void onHoverEnter(final MotionEvent event) { + final Key key = getHoverKey(event); + if (key != null) { + onHoverEnterKey(key); + } + setCurrentHoverKey(key); + } + + /** + * Process {@link MotionEvent#ACTION_HOVER_MOVE} event. + * + * @param event A hover move event. + */ + protected void onHoverMove(final MotionEvent event) { + final Key previousKey = getCurrentHoverKey(); + final Key key = getHoverKey(event); + if (key != previousKey) { + if (previousKey != null) { + onHoverExitKey(previousKey); } if (key != null) { - onHoverMoveKey(key); + onHoverEnterKey(key); } - break; } - return true; + if (key != null) { + onHoverMoveKey(key); + } + setCurrentHoverKey(key); + } + + /** + * Process {@link MotionEvent#ACTION_HOVER_EXIT} event. + * + * @param event A hover exit event. + */ + protected void onHoverExit(final MotionEvent event) { + final Key key = getHoverKey(event); + // Make sure we're not getting an EXIT event because the user slid + // off the keyboard area, then force a key press. + if (key != null) { + simulateTouchEvent(MotionEvent.ACTION_DOWN, event); + simulateTouchEvent(MotionEvent.ACTION_UP, event); + onHoverExitKey(key); + } + setCurrentHoverKey(null); } /** - * Simulates a key press by injecting touch an event into the keyboard view. - * This avoids the complexity of trackers and listeners within the keyboard. + * Simulating a touch event by injecting a synthesized touch event into {@link PointerTracker}. * - * @param key The key to press. - * @return the event time of the simulated key press. + * @param touchAction The action of the synthesizing touch event. + * @param hoverEvent The base hover event from that the touch event is synthesized. */ - private long simulateKeyPress(final Key key) { - final int x = key.getHitBox().centerX(); - final int y = key.getHitBox().centerY(); - final long downTime = SystemClock.uptimeMillis(); - final MotionEvent downEvent = MotionEvent.obtain( - downTime, downTime, MotionEvent.ACTION_DOWN, x, y, 0); - mKeyboardView.onTouchEvent(downEvent); - downEvent.recycle(); - return downTime; + protected void simulateTouchEvent(final int touchAction, final MotionEvent hoverEvent) { + final MotionEvent touchEvent = synthesizeTouchEvent(touchAction, hoverEvent); + final int actionIndex = touchEvent.getActionIndex(); + final int pointerId = touchEvent.getPointerId(actionIndex); + final PointerTracker tracker = PointerTracker.getPointerTracker(pointerId); + tracker.processMotionEvent(touchEvent, mKeyDetector); + touchEvent.recycle(); } /** - * Simulates a key release by injecting touch an event into the keyboard view. - * This avoids the complexity of trackers and listeners within the keyboard. + * Synthesize a touch event from a hover event. * - * @param key The key to release. - * @param downTime the event time of the key press. + * @param touchAction The action of the synthesizing touch event. + * @param event The base hover event from that the touch event is synthesized. + * @return The synthesized touch event of <code>touchAction</code> that has pointer information + * of <code>event</code>. */ - private void simulateKeyRelease(final Key key, final long downTime) { - final int x = key.getHitBox().centerX(); - final int y = key.getHitBox().centerY(); - final MotionEvent upEvent = MotionEvent.obtain( - downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, x, y, 0); - mKeyboardView.onTouchEvent(upEvent); - upEvent.recycle(); + protected static MotionEvent synthesizeTouchEvent(final int touchAction, + final MotionEvent event) { + final long downTime = event.getDownTime(); + final long eventTime = event.getEventTime(); + final int actionIndex = event.getActionIndex(); + final float x = event.getX(actionIndex); + final float y = event.getY(actionIndex); + final int pointerId = event.getPointerId(actionIndex); + return MotionEvent.obtain(downTime, eventTime, touchAction, x, y, pointerId); } /** @@ -213,6 +265,8 @@ public class KeyboardAccessibilityDelegate<KV extends KeyboardView> * @param key The currently hovered key. */ protected void onHoverEnterKey(final Key key) { + key.onPressed(); + mKeyboardView.invalidateKey(key); final KeyboardAccessibilityNodeProvider provider = getAccessibilityNodeProvider(); provider.sendAccessibilityEventForKey(key, AccessibilityEventCompat.TYPE_VIEW_HOVER_ENTER); provider.performActionForKey(key, AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS); @@ -231,6 +285,8 @@ public class KeyboardAccessibilityDelegate<KV extends KeyboardView> * @param key The currently hovered key. */ protected void onHoverExitKey(final Key key) { + key.onReleased(); + mKeyboardView.invalidateKey(key); final KeyboardAccessibilityNodeProvider provider = getAccessibilityNodeProvider(); provider.sendAccessibilityEventForKey(key, AccessibilityEventCompat.TYPE_VIEW_HOVER_EXIT); } diff --git a/java/src/com/android/inputmethod/latin/SuggestedWords.java b/java/src/com/android/inputmethod/latin/SuggestedWords.java index 23a6086ef..62da0f992 100644 --- a/java/src/com/android/inputmethod/latin/SuggestedWords.java +++ b/java/src/com/android/inputmethod/latin/SuggestedWords.java @@ -207,6 +207,8 @@ public class SuggestedWords { public static final int NOT_AN_INDEX = -1; public static final int NOT_A_CONFIDENCE = -1; public static final int MAX_SCORE = Integer.MAX_VALUE; + + // TODO: Make KIND_MASK_KIND private. public static final int KIND_MASK_KIND = 0xFF; // Mask to get only the kind public static final int KIND_TYPED = 0; // What user typed public static final int KIND_CORRECTION = 1; // Simple correction/suggestion @@ -222,6 +224,7 @@ public class SuggestedWords { public static final int KIND_RESUMED = 9; public static final int KIND_OOV_CORRECTION = 10; // Most probable string correction + // TODO: Make KIND_MASK_FLAGS private. public static final int KIND_MASK_FLAGS = 0xFFFFFF00; // Mask to get the flags public static final int KIND_FLAG_POSSIBLY_OFFENSIVE = 0x80000000; public static final int KIND_FLAG_EXACT_MATCH = 0x40000000; @@ -232,6 +235,7 @@ public class SuggestedWords { // the application (including keyboard-computed ones, so this is almost always null) public final CompletionInfo mApplicationSpecifiedCompletionInfo; public final int mScore; + // TODO: Rename to mKindAndFlags and make this private. public final int mKind; // kind and kind flags public final int mCodePointCount; public final Dictionary mSourceDict; @@ -290,6 +294,8 @@ public class SuggestedWords { return (mKind & KIND_MASK_KIND) == kind; } + // TODO: Add predicate methods for each flag. + public void setDebugString(final String str) { if (null == str) throw new NullPointerException("Debug info is null"); mDebugString = str; |