From 723aaa2eebcfea0d285f11fc265941057332664d Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Thu, 15 Mar 2012 19:44:43 +0900 Subject: [PATCH] Remove touch dead zone KeyDetector should use the distance from the hit box to detect the key. Bug: 6174250 Change-Id: Id1745d90222d1d1a10467f194b45307c12449944 --- .../inputmethod/keyboard/KeyDetector.java | 45 ++++++++++++------- .../keyboard/MoreKeysDetector.java | 22 ++++++++- .../inputmethod/keyboard/PointerTracker.java | 6 +-- .../inputmethod/latin/WordComposer.java | 4 +- 4 files changed, 55 insertions(+), 22 deletions(-) diff --git a/java/src/com/android/inputmethod/keyboard/KeyDetector.java b/java/src/com/android/inputmethod/keyboard/KeyDetector.java index 3638eae8d..ea3f6236a 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyDetector.java +++ b/java/src/com/android/inputmethod/keyboard/KeyDetector.java @@ -97,21 +97,21 @@ public class KeyDetector { /** * Computes maximum size of the array that can contain all nearby key codes returned by - * {@link #getKeyAndNearbyCodes}. + * {@link #getNearbyCodes}. * * @return Returns maximum size of the array that can contain all nearby key codes returned - * by {@link #getKeyAndNearbyCodes}. + * by {@link #getNearbyCodes}. */ protected int getMaxNearbyKeys() { return MAX_NEARBY_KEYS; } /** - * Allocates array that can hold all key codes returned by {@link #getKeyAndNearbyCodes} + * Allocates array that can hold all key codes returned by {@link #getNearbyCodes} * method. The maximum size of the array should be computed by {@link #getMaxNearbyKeys}. * * @return Allocates and returns an array that can hold all key codes returned by - * {@link #getKeyAndNearbyCodes} method. All elements in the returned array are + * {@link #getNearbyCodes} method. All elements in the returned array are * initialized by {@link #NOT_A_CODE} value. */ public int[] newCodeArray() { @@ -222,15 +222,15 @@ public class KeyDetector { * @param x The x-coordinate of a touch point * @param y The y-coordinate of a touch point * @param allCodes All nearby key codes except functional key are returned in this array - * @return The nearest key */ - public Key getKeyAndNearbyCodes(int x, int y, final int[] allCodes) { + // TODO: Move this method to native code. + public void getNearbyCodes(int x, int y, final int[] allCodes) { final int touchX = getTouchX(x); final int touchY = getTouchY(y); initializeNearbyKeys(); Key primaryKey = null; - for (final Key key: mKeyboard.getNearestKeys(touchX, touchY)) { + for (final Key key : mKeyboard.getNearestKeys(touchX, touchY)) { final boolean isOnKey = key.isOnKey(touchX, touchY); final int distance = key.squaredDistanceToEdge(touchX, touchY); if (isOnKey || (mProximityCorrectOn && distance < mProximityThresholdSquare)) { @@ -241,16 +241,31 @@ public class KeyDetector { } } - if (allCodes != null && allCodes.length > 0) { - getNearbyKeyCodes(primaryKey != null ? primaryKey.mCode : NOT_A_CODE, allCodes); - if (DEBUG) { - Log.d(TAG, "x=" + x + " y=" + y - + " primary=" + printableCode(primaryKey) - + " codes=" + printableCodes(allCodes)); + getNearbyKeyCodes(primaryKey != null ? primaryKey.mCode : NOT_A_CODE, allCodes); + if (DEBUG) { + Log.d(TAG, "x=" + x + " y=" + y + + " primary=" + printableCode(primaryKey) + + " codes=" + printableCodes(allCodes)); + } + } + + /** + * Detect the key whose hitbox the touch point is in. + * + * @param x The x-coordinate of a touch point + * @param y The y-coordinate of a touch point + * @return the key that the touch point hits. + */ + public Key detectHitKey(int x, int y) { + final int touchX = getTouchX(x); + final int touchY = getTouchY(y); + + for (final Key key : mKeyboard.getNearestKeys(touchX, touchY)) { + if (key.isOnKey(touchX, touchY)) { + return key; } } - - return primaryKey; + return null; } public static String printableCode(Key key) { diff --git a/java/src/com/android/inputmethod/keyboard/MoreKeysDetector.java b/java/src/com/android/inputmethod/keyboard/MoreKeysDetector.java index 742ee98d7..6c8d02016 100644 --- a/java/src/com/android/inputmethod/keyboard/MoreKeysDetector.java +++ b/java/src/com/android/inputmethod/keyboard/MoreKeysDetector.java @@ -39,7 +39,7 @@ public class MoreKeysDetector extends KeyDetector { } @Override - public Key getKeyAndNearbyCodes(int x, int y, final int[] allCodes) { + public void getNearbyCodes(int x, int y, final int[] allCodes) { final int touchX = getTouchX(x); final int touchY = getTouchY(y); @@ -53,8 +53,26 @@ public class MoreKeysDetector extends KeyDetector { } } - if (allCodes != null && nearestKey != null) { + if (nearestKey != null) { allCodes[0] = nearestKey.mCode; + } else { + allCodes[0] = NOT_A_CODE; + } + } + + @Override + public Key detectHitKey(int x, int y) { + final int touchX = getTouchX(x); + final int touchY = getTouchY(y); + + Key nearestKey = null; + int nearestDist = (y < 0) ? mSlideAllowanceSquareTop : mSlideAllowanceSquare; + for (final Key key : getKeyboard().mKeys) { + final int dist = key.squaredDistanceToEdge(touchX, touchY); + if (dist < nearestDist) { + nearestKey = key; + nearestDist = dist; + } } return nearestKey; } diff --git a/java/src/com/android/inputmethod/keyboard/PointerTracker.java b/java/src/com/android/inputmethod/keyboard/PointerTracker.java index ed889712a..ec9081681 100644 --- a/java/src/com/android/inputmethod/keyboard/PointerTracker.java +++ b/java/src/com/android/inputmethod/keyboard/PointerTracker.java @@ -318,7 +318,7 @@ public class PointerTracker { } public Key getKeyOn(int x, int y) { - return mKeyDetector.getKeyAndNearbyCodes(x, y, null); + return mKeyDetector.detectHitKey(x, y); } private void setReleasedKeyGraphics(Key key) { @@ -421,7 +421,7 @@ public class PointerTracker { private Key onMoveKeyInternal(int x, int y) { mLastX = x; mLastY = y; - return mKeyDetector.getKeyAndNearbyCodes(x, y, null); + return mKeyDetector.detectHitKey(x, y); } private Key onMoveKey(int x, int y) { @@ -748,7 +748,7 @@ public class PointerTracker { private long mPreviousEventTime; private void printTouchEvent(String title, int x, int y, long eventTime) { - final Key key = mKeyDetector.getKeyAndNearbyCodes(x, y, null); + final Key key = mKeyDetector.detectHitKey(x, y); final String code = KeyDetector.printableCode(key); final long delta = eventTime - mPreviousEventTime; Log.d(TAG, String.format("%s%s[%d] %4d %4d %5d %s", title, diff --git a/java/src/com/android/inputmethod/latin/WordComposer.java b/java/src/com/android/inputmethod/latin/WordComposer.java index 5f6be6867..126ac47e7 100644 --- a/java/src/com/android/inputmethod/latin/WordComposer.java +++ b/java/src/com/android/inputmethod/latin/WordComposer.java @@ -141,7 +141,7 @@ public class WordComposer { keyY = y; } else { codes = keyDetector.newCodeArray(); - keyDetector.getKeyAndNearbyCodes(x, y, codes); + keyDetector.getNearbyCodes(x, y, codes); keyX = keyDetector.getTouchX(x); keyY = keyDetector.getTouchY(y); } @@ -204,7 +204,7 @@ public class WordComposer { final int x = key.mX + key.mWidth / 2; final int y = key.mY + key.mHeight / 2; final int[] codes = keyDetector.newCodeArray(); - keyDetector.getKeyAndNearbyCodes(x, y, codes); + keyDetector.getNearbyCodes(x, y, codes); add(codePoint, codes, x, y); return; }