Encapsulate vertical and horizontal correction values into KeyDetector.
Bug: 2959169 Change-Id: Id2b0b974fffdf6f09ee1828e957b973d2ce1c315
This commit is contained in:
parent
6b4d521fb4
commit
400046d62e
4 changed files with 65 additions and 61 deletions
|
@ -25,12 +25,16 @@ abstract class KeyDetector {
|
|||
protected Keyboard mKeyboard;
|
||||
protected Key[] mKeys;
|
||||
|
||||
protected int mCorrectionX;
|
||||
protected int mCorrectionY;
|
||||
protected boolean mProximityCorrectOn;
|
||||
protected int mProximityThresholdSquare;
|
||||
|
||||
public Key[] setKeyboard(Keyboard keyboard) {
|
||||
public Key[] setKeyboard(Keyboard keyboard, float correctionX, float correctionY) {
|
||||
if (keyboard == null)
|
||||
throw new NullPointerException();
|
||||
mCorrectionX = (int)correctionX;
|
||||
mCorrectionY = (int)correctionY;
|
||||
mKeyboard = keyboard;
|
||||
List<Key> keys = mKeyboard.getKeys();
|
||||
Key[] array = keys.toArray(new Key[keys.size()]);
|
||||
|
@ -38,6 +42,14 @@ abstract class KeyDetector {
|
|||
return array;
|
||||
}
|
||||
|
||||
protected int getTouchX(int x) {
|
||||
return x + mCorrectionX;
|
||||
}
|
||||
|
||||
protected int getTouchY(int y) {
|
||||
return y + mCorrectionY;
|
||||
}
|
||||
|
||||
public void setProximityCorrectionEnabled(boolean enabled) {
|
||||
mProximityCorrectOn = enabled;
|
||||
}
|
||||
|
|
|
@ -165,7 +165,7 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener,
|
|||
private float mShadowRadius;
|
||||
private Drawable mKeyBackground;
|
||||
private float mBackgroundDimAmount;
|
||||
private int mVerticalCorrection;
|
||||
private float mVerticalCorrection;
|
||||
private int mPreviewOffset;
|
||||
private int mPreviewHeight;
|
||||
private int mPopupLayout;
|
||||
|
@ -541,7 +541,8 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener,
|
|||
mHandler.cancelPopupPreview();
|
||||
mKeyboard = keyboard;
|
||||
LatinImeLogger.onSetKeyboard(keyboard);
|
||||
mKeys = mKeyDetector.setKeyboard(keyboard);
|
||||
mKeys = mKeyDetector.setKeyboard(keyboard, -getPaddingLeft(),
|
||||
-getPaddingTop() + mVerticalCorrection);
|
||||
for (PointerTracker tracker : mPointerTrackers) {
|
||||
tracker.setKeyboard(mKeys, mDebounceHysteresis);
|
||||
}
|
||||
|
@ -613,9 +614,6 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener,
|
|||
return mSymbolColorScheme;
|
||||
}
|
||||
|
||||
public void setVerticalCorrection(int verticalOffset) {
|
||||
}
|
||||
|
||||
public void setPopupParent(View v) {
|
||||
mPopupParent = v;
|
||||
}
|
||||
|
@ -1070,14 +1068,6 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener,
|
|||
return mMiniKeyboardOnScreen;
|
||||
}
|
||||
|
||||
private int getTouchX(float x) {
|
||||
return (int)x - getPaddingLeft();
|
||||
}
|
||||
|
||||
private int getTouchY(float y) {
|
||||
return (int)y + mVerticalCorrection - getPaddingTop();
|
||||
}
|
||||
|
||||
private PointerTracker getPointerTracker(final int id) {
|
||||
final ArrayList<PointerTracker> pointers = mPointerTrackers;
|
||||
final Key[] keys = mKeys;
|
||||
|
@ -1132,29 +1122,29 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener,
|
|||
|
||||
if (action == MotionEvent.ACTION_MOVE) {
|
||||
for (int index = 0; index < pointerCount; index++) {
|
||||
int touchX = getTouchX(me.getX(index));
|
||||
int touchY = getTouchY(me.getY(index));
|
||||
int x = (int)me.getX(index);
|
||||
int y = (int)me.getY(index);
|
||||
int id = me.getPointerId(index);
|
||||
PointerTracker tracker = getPointerTracker(id);
|
||||
tracker.onMoveEvent(touchX, touchY, eventTime);
|
||||
tracker.onMoveEvent(x, y, eventTime);
|
||||
}
|
||||
} else {
|
||||
int index = me.getActionIndex();
|
||||
int touchX = getTouchX(me.getX(index));
|
||||
int touchY = getTouchY(me.getY(index));
|
||||
int x = (int)me.getX(index);
|
||||
int y = (int)me.getY(index);
|
||||
int id = me.getPointerId(index);
|
||||
PointerTracker tracker = getPointerTracker(id);
|
||||
switch (action) {
|
||||
case MotionEvent.ACTION_DOWN:
|
||||
case MotionEvent.ACTION_POINTER_DOWN:
|
||||
onDownEvent(tracker, touchX, touchY, eventTime);
|
||||
onDownEvent(tracker, x, y, eventTime);
|
||||
break;
|
||||
case MotionEvent.ACTION_UP:
|
||||
case MotionEvent.ACTION_POINTER_UP:
|
||||
onUpEvent(tracker, touchX, touchY, eventTime);
|
||||
onUpEvent(tracker, x, y, eventTime);
|
||||
break;
|
||||
case MotionEvent.ACTION_CANCEL:
|
||||
onCancelEvent(tracker, touchX, touchY, eventTime);
|
||||
onCancelEvent(tracker, x, y, eventTime);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -1162,12 +1152,12 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener,
|
|||
return true;
|
||||
}
|
||||
|
||||
private void onDownEvent(PointerTracker tracker, int touchX, int touchY, long eventTime) {
|
||||
tracker.onDownEvent(touchX, touchY, eventTime);
|
||||
private void onDownEvent(PointerTracker tracker, int x, int y, long eventTime) {
|
||||
tracker.onDownEvent(x, y, eventTime);
|
||||
mPointerQueue.add(tracker);
|
||||
}
|
||||
|
||||
private void onUpEvent(PointerTracker tracker, int touchX, int touchY, long eventTime) {
|
||||
private void onUpEvent(PointerTracker tracker, int x, int y, long eventTime) {
|
||||
int index = mPointerQueue.lastIndexOf(tracker);
|
||||
if (index >= 0) {
|
||||
mPointerQueue.releasePointersOlderThan(tracker, eventTime);
|
||||
|
@ -1175,12 +1165,12 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener,
|
|||
Log.w(TAG, "onUpEvent: corresponding down event not found for pointer "
|
||||
+ tracker.mPointerId);
|
||||
}
|
||||
tracker.onUpEvent(touchX, touchY, eventTime);
|
||||
tracker.onUpEvent(x, y, eventTime);
|
||||
mPointerQueue.remove(tracker);
|
||||
}
|
||||
|
||||
private void onCancelEvent(PointerTracker tracker, int touchX, int touchY, long eventTime) {
|
||||
tracker.onCancelEvent(touchX, touchY, eventTime);
|
||||
private void onCancelEvent(PointerTracker tracker, int x, int y, long eventTime) {
|
||||
tracker.onCancelEvent(x, y, eventTime);
|
||||
mPointerQueue.remove(tracker);
|
||||
}
|
||||
|
||||
|
|
|
@ -139,12 +139,12 @@ public class PointerTracker {
|
|||
}
|
||||
}
|
||||
|
||||
public void onDownEvent(int touchX, int touchY, long eventTime) {
|
||||
int keyIndex = mKeyDetector.getKeyIndexAndNearbyCodes(touchX, touchY, null);
|
||||
public void onDownEvent(int x, int y, long eventTime) {
|
||||
int keyIndex = mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null);
|
||||
mCurrentKey = keyIndex;
|
||||
mStartX = touchX;
|
||||
mStartY = touchY;
|
||||
startMoveDebouncing(touchX, touchY);
|
||||
mStartX = x;
|
||||
mStartY = y;
|
||||
startMoveDebouncing(x, y);
|
||||
startTimeDebouncing(eventTime);
|
||||
checkMultiTap(eventTime, keyIndex);
|
||||
if (mListener != null) {
|
||||
|
@ -159,19 +159,19 @@ public class PointerTracker {
|
|||
mHandler.startLongPressTimer(LONGPRESS_TIMEOUT, keyIndex, this);
|
||||
}
|
||||
showKeyPreviewAndUpdateKey(keyIndex);
|
||||
updateMoveDebouncing(touchX, touchY);
|
||||
updateMoveDebouncing(x, y);
|
||||
if (DEBUG)
|
||||
debugLog("onDownEvent:", touchX, touchY);
|
||||
debugLog("onDownEvent:", x, y);
|
||||
}
|
||||
|
||||
public void onMoveEvent(int touchX, int touchY, long eventTime) {
|
||||
int keyIndex = mKeyDetector.getKeyIndexAndNearbyCodes(touchX, touchY, null);
|
||||
public void onMoveEvent(int x, int y, long eventTime) {
|
||||
int keyIndex = mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null);
|
||||
if (isValidKeyIndex(keyIndex)) {
|
||||
if (mCurrentKey == NOT_A_KEY) {
|
||||
updateTimeDebouncing(eventTime);
|
||||
mCurrentKey = keyIndex;
|
||||
mHandler.startLongPressTimer(LONGPRESS_TIMEOUT, keyIndex, this);
|
||||
} else if (isMinorMoveBounce(touchX, touchY, keyIndex, mCurrentKey)) {
|
||||
} else if (isMinorMoveBounce(x, y, keyIndex, mCurrentKey)) {
|
||||
updateTimeDebouncing(eventTime);
|
||||
} else {
|
||||
resetMultiTap();
|
||||
|
@ -190,19 +190,19 @@ public class PointerTracker {
|
|||
* should not be showed as popup preview.
|
||||
*/
|
||||
showKeyPreviewAndUpdateKey(isMinorTimeBounce() ? mLastKey : mCurrentKey);
|
||||
updateMoveDebouncing(touchX, touchY);
|
||||
updateMoveDebouncing(x, y);
|
||||
if (DEBUG_MOVE)
|
||||
debugLog("onMoveEvent:", touchX, touchY);
|
||||
debugLog("onMoveEvent:", x, y);
|
||||
}
|
||||
|
||||
public void onUpEvent(int touchX, int touchY, long eventTime) {
|
||||
public void onUpEvent(int x, int y, long eventTime) {
|
||||
if (DEBUG)
|
||||
debugLog("onUpEvent :", touchX, touchY);
|
||||
int keyIndex = mKeyDetector.getKeyIndexAndNearbyCodes(touchX, touchY, null);
|
||||
debugLog("onUpEvent :", x, y);
|
||||
int keyIndex = mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null);
|
||||
boolean wasInKeyRepeat = mHandler.isInKeyRepeat();
|
||||
mHandler.cancelKeyTimers();
|
||||
mHandler.cancelPopupPreview();
|
||||
if (isMinorMoveBounce(touchX, touchY, keyIndex, mCurrentKey)) {
|
||||
if (isMinorMoveBounce(x, y, keyIndex, mCurrentKey)) {
|
||||
updateTimeDebouncing(eventTime);
|
||||
} else {
|
||||
resetMultiTap();
|
||||
|
@ -211,21 +211,21 @@ public class PointerTracker {
|
|||
}
|
||||
if (isMinorTimeBounce()) {
|
||||
mCurrentKey = mLastKey;
|
||||
touchX = mLastCodeX;
|
||||
touchY = mLastCodeY;
|
||||
x = mLastCodeX;
|
||||
y = mLastCodeY;
|
||||
}
|
||||
showKeyPreviewAndUpdateKey(NOT_A_KEY);
|
||||
// If we're not on a repeating key (which sends on a DOWN event)
|
||||
if (!wasInKeyRepeat && !mProxy.isMiniKeyboardOnScreen()) {
|
||||
detectAndSendKey(mCurrentKey, touchX, touchY, eventTime);
|
||||
detectAndSendKey(mCurrentKey, (int)x, (int)y, eventTime);
|
||||
}
|
||||
if (isValidKeyIndex(keyIndex))
|
||||
mProxy.invalidateKey(mKeys[keyIndex]);
|
||||
}
|
||||
|
||||
public void onCancelEvent(int touchX, int touchY, long eventTime) {
|
||||
public void onCancelEvent(int x, int y, long eventTime) {
|
||||
if (DEBUG)
|
||||
debugLog("onCancelEvt:", touchX, touchY);
|
||||
debugLog("onCancelEvt:", x, y);
|
||||
mHandler.cancelKeyTimers();
|
||||
mHandler.cancelPopupPreview();
|
||||
mProxy.dismissPopupKeyboard();
|
||||
|
@ -244,6 +244,14 @@ public class PointerTracker {
|
|||
}
|
||||
}
|
||||
|
||||
public int getLastX() {
|
||||
return mLastX;
|
||||
}
|
||||
|
||||
public int getLastY() {
|
||||
return mLastY;
|
||||
}
|
||||
|
||||
// These package scope methods are only for debugging purpose.
|
||||
/* package */ int getStartX() {
|
||||
return mStartX;
|
||||
|
@ -253,14 +261,6 @@ public class PointerTracker {
|
|||
return mStartY;
|
||||
}
|
||||
|
||||
/* package */ int getLastX() {
|
||||
return mLastX;
|
||||
}
|
||||
|
||||
/* package */ int getLastY() {
|
||||
return mLastY;
|
||||
}
|
||||
|
||||
private void startMoveDebouncing(int x, int y) {
|
||||
mLastCodeX = x;
|
||||
mLastCodeY = y;
|
||||
|
@ -426,7 +426,7 @@ public class PointerTracker {
|
|||
code = String.format((primaryCode < 0) ? "%4d" : "0x%02x", primaryCode);
|
||||
}
|
||||
Log.d(TAG,
|
||||
String.format("%s [%d] %d,%d %s %s", title, mPointerId, x, y, code,
|
||||
String.format("%s [%d] %3d,%3d %s %s", title, mPointerId, x, y, code,
|
||||
isModifier() ? "modifier" : ""));
|
||||
}
|
||||
}
|
|
@ -35,6 +35,8 @@ class ProximityKeyDetector extends KeyDetector {
|
|||
|
||||
@Override
|
||||
public int getKeyIndexAndNearbyCodes(int x, int y, int[] allKeys) {
|
||||
int touchX = getTouchX(x);
|
||||
int touchY = getTouchY(y);
|
||||
final Key[] keys = mKeys;
|
||||
if (keys == null)
|
||||
throw new IllegalStateException("keyboard isn't set");
|
||||
|
@ -44,18 +46,18 @@ class ProximityKeyDetector extends KeyDetector {
|
|||
int closestKeyDist = mProximityThresholdSquare + 1;
|
||||
int[] distances = mDistances;
|
||||
Arrays.fill(distances, Integer.MAX_VALUE);
|
||||
int [] nearestKeyIndices = mKeyboard.getNearestKeys(x, y);
|
||||
int [] nearestKeyIndices = mKeyboard.getNearestKeys(touchX, touchY);
|
||||
final int keyCount = nearestKeyIndices.length;
|
||||
for (int i = 0; i < keyCount; i++) {
|
||||
final Key key = keys[nearestKeyIndices[i]];
|
||||
int dist = 0;
|
||||
boolean isInside = key.isInside(x,y);
|
||||
boolean isInside = key.isInside(touchX, touchY);
|
||||
if (isInside) {
|
||||
primaryIndex = nearestKeyIndices[i];
|
||||
}
|
||||
|
||||
if (((mProximityCorrectOn
|
||||
&& (dist = key.squaredDistanceFrom(x, y)) < mProximityThresholdSquare)
|
||||
&& (dist = key.squaredDistanceFrom(touchX, touchY)) < mProximityThresholdSquare)
|
||||
|| isInside)
|
||||
&& key.codes[0] > 32) {
|
||||
// Find insertion point
|
||||
|
|
Loading…
Reference in a new issue