Include horizontal and vertical gap in the count of Key.isOnKey
This change also reverts I8f6102d6 and considers that the point is on the key while sorting in proximity key detection. Bug: 4348994 Change-Id: I3ee913675e28da7e7b164805a7a683f0814b38b9
This commit is contained in:
parent
8d165bb5d1
commit
44fe4a0598
3 changed files with 25 additions and 25 deletions
|
@ -200,8 +200,8 @@ public class Key {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Horizontal gap is divided equally to both sides of the key.
|
// Horizontal gap is divided equally to both sides of the key.
|
||||||
this.mX = x + mGap / 2;
|
mX = x + mGap / 2;
|
||||||
this.mY = y;
|
mY = y;
|
||||||
|
|
||||||
final TypedArray keyAttr = res.obtainAttributes(Xml.asAttributeSet(parser),
|
final TypedArray keyAttr = res.obtainAttributes(Xml.asAttributeSet(parser),
|
||||||
R.styleable.Keyboard_Key);
|
R.styleable.Keyboard_Key);
|
||||||
|
@ -351,12 +351,13 @@ public class Key {
|
||||||
final boolean rightEdge = (flags & Keyboard.EDGE_RIGHT) != 0;
|
final boolean rightEdge = (flags & Keyboard.EDGE_RIGHT) != 0;
|
||||||
final boolean topEdge = (flags & Keyboard.EDGE_TOP) != 0;
|
final boolean topEdge = (flags & Keyboard.EDGE_TOP) != 0;
|
||||||
final boolean bottomEdge = (flags & Keyboard.EDGE_BOTTOM) != 0;
|
final boolean bottomEdge = (flags & Keyboard.EDGE_BOTTOM) != 0;
|
||||||
final int left = this.mX;
|
final int left = mX - mGap / 2;
|
||||||
final int right = left + this.mWidth;
|
final int right = left + mWidth + mGap;
|
||||||
final int top = this.mY;
|
final int top = mY;
|
||||||
final int bottom = top + this.mHeight;
|
final int bottom = top + mHeight + mKeyboard.getVerticalGap();
|
||||||
return (x >= left || leftEdge) && (x < right || rightEdge)
|
// In order to mitigate rounding errors, we use (left <= x <= right) here.
|
||||||
&& (y >= top || topEdge) && (y < bottom || bottomEdge);
|
return (x >= left || leftEdge) && (x <= right || rightEdge)
|
||||||
|
&& (y >= top || topEdge) && (y <= bottom || bottomEdge);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -366,10 +367,10 @@ public class Key {
|
||||||
* @return the square of the distance of the point from the nearest edge of the key
|
* @return the square of the distance of the point from the nearest edge of the key
|
||||||
*/
|
*/
|
||||||
public int squaredDistanceToEdge(int x, int y) {
|
public int squaredDistanceToEdge(int x, int y) {
|
||||||
final int left = this.mX;
|
final int left = mX;
|
||||||
final int right = left + this.mWidth;
|
final int right = left + mWidth;
|
||||||
final int top = this.mY;
|
final int top = mY;
|
||||||
final int bottom = top + this.mHeight;
|
final int bottom = top + mHeight;
|
||||||
final int edgeX = x < left ? left : (x > right ? right : x);
|
final int edgeX = x < left ? left : (x > right ? right : x);
|
||||||
final int edgeY = y < top ? top : (y > bottom ? bottom : y);
|
final int edgeY = y < top ? top : (y > bottom ? bottom : y);
|
||||||
final int dx = x - edgeX;
|
final int dx = x - edgeX;
|
||||||
|
|
|
@ -91,11 +91,11 @@ public class KeyDetector {
|
||||||
*
|
*
|
||||||
* @return Allocates and returns an array that can hold all key indices returned by
|
* @return Allocates and returns an array that can hold all key indices returned by
|
||||||
* {@link #getKeyIndexAndNearbyCodes} method. All elements in the returned array are
|
* {@link #getKeyIndexAndNearbyCodes} method. All elements in the returned array are
|
||||||
* initialized by {@link #NOT_A_KEY} value.
|
* initialized by {@link #NOT_A_CODE} value.
|
||||||
*/
|
*/
|
||||||
public int[] newCodeArray() {
|
public int[] newCodeArray() {
|
||||||
int[] codes = new int[getMaxNearbyKeys()];
|
int[] codes = new int[getMaxNearbyKeys()];
|
||||||
Arrays.fill(codes, NOT_A_KEY);
|
Arrays.fill(codes, NOT_A_CODE);
|
||||||
return codes;
|
return codes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,16 +106,20 @@ public class KeyDetector {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Insert the key into nearby keys buffer and sort nearby keys by ascending order of distance.
|
* Insert the key into nearby keys buffer and sort nearby keys by ascending order of distance.
|
||||||
|
* If the distance of two keys are the same, the key which the point is on should be considered
|
||||||
|
* as a closer one.
|
||||||
*
|
*
|
||||||
* @param keyIndex index of the key.
|
* @param keyIndex index of the key.
|
||||||
* @param distance distance between the key's edge and user touched point.
|
* @param distance distance between the key's edge and user touched point.
|
||||||
|
* @param isOnKey true if the point is on the key.
|
||||||
* @return order of the key in the nearby buffer, 0 if it is the nearest key.
|
* @return order of the key in the nearby buffer, 0 if it is the nearest key.
|
||||||
*/
|
*/
|
||||||
private int sortNearbyKeys(int keyIndex, int distance) {
|
private int sortNearbyKeys(int keyIndex, int distance, boolean isOnKey) {
|
||||||
final int[] distances = mDistances;
|
final int[] distances = mDistances;
|
||||||
final int[] indices = mIndices;
|
final int[] indices = mIndices;
|
||||||
for (int insertPos = 0; insertPos < distances.length; insertPos++) {
|
for (int insertPos = 0; insertPos < distances.length; insertPos++) {
|
||||||
if (distance < distances[insertPos]) {
|
final int comparingDistance = distances[insertPos];
|
||||||
|
if (distance < comparingDistance || (distance == comparingDistance && isOnKey)) {
|
||||||
final int nextPos = insertPos + 1;
|
final int nextPos = insertPos + 1;
|
||||||
if (nextPos < distances.length) {
|
if (nextPos < distances.length) {
|
||||||
System.arraycopy(distances, insertPos, distances, nextPos,
|
System.arraycopy(distances, insertPos, distances, nextPos,
|
||||||
|
@ -174,12 +178,11 @@ public class KeyDetector {
|
||||||
int primaryIndex = NOT_A_KEY;
|
int primaryIndex = NOT_A_KEY;
|
||||||
for (final int index : mKeyboard.getNearestKeys(touchX, touchY)) {
|
for (final int index : mKeyboard.getNearestKeys(touchX, touchY)) {
|
||||||
final Key key = keys.get(index);
|
final Key key = keys.get(index);
|
||||||
// TODO: should be okay to skip calling isInside()
|
final boolean isOnKey = key.isOnKey(touchX, touchY);
|
||||||
final boolean isInside = mKeyboard.isInside(key, touchX, touchY);
|
|
||||||
final int distance = key.squaredDistanceToEdge(touchX, touchY);
|
final int distance = key.squaredDistanceToEdge(touchX, touchY);
|
||||||
if (isInside || (mProximityCorrectOn && distance < mProximityThresholdSquare)) {
|
if (isOnKey || (mProximityCorrectOn && distance < mProximityThresholdSquare)) {
|
||||||
final int insertedPosition = sortNearbyKeys(index, distance);
|
final int insertedPosition = sortNearbyKeys(index, distance, isOnKey);
|
||||||
if (insertedPosition == 0)
|
if (insertedPosition == 0 && isOnKey)
|
||||||
primaryIndex = index;
|
primaryIndex = index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -380,10 +380,6 @@ public class Keyboard {
|
||||||
mProximityInfo.setProximityInfo(mGridNeighbors, getMinWidth(), getHeight(), mKeys);
|
mProximityInfo.setProximityInfo(mGridNeighbors, getMinWidth(), getHeight(), mKeys);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isInside(Key key, int x, int y) {
|
|
||||||
return key.isOnKey(x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the indices of the keys that are closest to the given point.
|
* Returns the indices of the keys that are closest to the given point.
|
||||||
* @param x the x-coordinate of the point
|
* @param x the x-coordinate of the point
|
||||||
|
|
Loading…
Reference in a new issue