Remove touch dead zone

KeyDetector should use the distance from the hit box to detect the
key.

Bug: 6174250
Change-Id: Id1745d90222d1d1a10467f194b45307c12449944
This commit is contained in:
Tadashi G. Takaoka 2012-03-15 19:44:43 +09:00
parent 2be7a37acf
commit 723aaa2eeb
4 changed files with 55 additions and 22 deletions

View file

@ -97,21 +97,21 @@ public class KeyDetector {
/** /**
* Computes maximum size of the array that can contain all nearby key codes returned by * 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 * @return Returns maximum size of the array that can contain all nearby key codes returned
* by {@link #getKeyAndNearbyCodes}. * by {@link #getNearbyCodes}.
*/ */
protected int getMaxNearbyKeys() { protected int getMaxNearbyKeys() {
return MAX_NEARBY_KEYS; 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}. * 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 * @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. * initialized by {@link #NOT_A_CODE} value.
*/ */
public int[] newCodeArray() { public int[] newCodeArray() {
@ -222,9 +222,9 @@ public class KeyDetector {
* @param x The x-coordinate of a touch point * @param x The x-coordinate of a touch point
* @param y The y-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 * @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 touchX = getTouchX(x);
final int touchY = getTouchY(y); final int touchY = getTouchY(y);
@ -241,7 +241,6 @@ public class KeyDetector {
} }
} }
if (allCodes != null && allCodes.length > 0) {
getNearbyKeyCodes(primaryKey != null ? primaryKey.mCode : NOT_A_CODE, allCodes); getNearbyKeyCodes(primaryKey != null ? primaryKey.mCode : NOT_A_CODE, allCodes);
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "x=" + x + " y=" + y Log.d(TAG, "x=" + x + " y=" + y
@ -250,7 +249,23 @@ public class KeyDetector {
} }
} }
return primaryKey; /**
* 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 null;
} }
public static String printableCode(Key key) { public static String printableCode(Key key) {

View file

@ -39,7 +39,7 @@ public class MoreKeysDetector extends KeyDetector {
} }
@Override @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 touchX = getTouchX(x);
final int touchY = getTouchY(y); 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; 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; return nearestKey;
} }

View file

@ -318,7 +318,7 @@ public class PointerTracker {
} }
public Key getKeyOn(int x, int y) { public Key getKeyOn(int x, int y) {
return mKeyDetector.getKeyAndNearbyCodes(x, y, null); return mKeyDetector.detectHitKey(x, y);
} }
private void setReleasedKeyGraphics(Key key) { private void setReleasedKeyGraphics(Key key) {
@ -421,7 +421,7 @@ public class PointerTracker {
private Key onMoveKeyInternal(int x, int y) { private Key onMoveKeyInternal(int x, int y) {
mLastX = x; mLastX = x;
mLastY = y; mLastY = y;
return mKeyDetector.getKeyAndNearbyCodes(x, y, null); return mKeyDetector.detectHitKey(x, y);
} }
private Key onMoveKey(int x, int y) { private Key onMoveKey(int x, int y) {
@ -748,7 +748,7 @@ public class PointerTracker {
private long mPreviousEventTime; private long mPreviousEventTime;
private void printTouchEvent(String title, int x, int y, long eventTime) { 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 String code = KeyDetector.printableCode(key);
final long delta = eventTime - mPreviousEventTime; final long delta = eventTime - mPreviousEventTime;
Log.d(TAG, String.format("%s%s[%d] %4d %4d %5d %s", title, Log.d(TAG, String.format("%s%s[%d] %4d %4d %5d %s", title,

View file

@ -141,7 +141,7 @@ public class WordComposer {
keyY = y; keyY = y;
} else { } else {
codes = keyDetector.newCodeArray(); codes = keyDetector.newCodeArray();
keyDetector.getKeyAndNearbyCodes(x, y, codes); keyDetector.getNearbyCodes(x, y, codes);
keyX = keyDetector.getTouchX(x); keyX = keyDetector.getTouchX(x);
keyY = keyDetector.getTouchY(y); keyY = keyDetector.getTouchY(y);
} }
@ -204,7 +204,7 @@ public class WordComposer {
final int x = key.mX + key.mWidth / 2; final int x = key.mX + key.mWidth / 2;
final int y = key.mY + key.mHeight / 2; final int y = key.mY + key.mHeight / 2;
final int[] codes = keyDetector.newCodeArray(); final int[] codes = keyDetector.newCodeArray();
keyDetector.getKeyAndNearbyCodes(x, y, codes); keyDetector.getNearbyCodes(x, y, codes);
add(codePoint, codes, x, y); add(codePoint, codes, x, y);
return; return;
} }