From 61dcaaf17e4d1f9e941b961559a46823e6e25c99 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Thu, 20 Sep 2012 16:39:40 +0900 Subject: [PATCH] Use device independent parameter for gesture Change-Id: Iea95992e4482108a498f14ec595f3eacc7d7fc4b --- .../keyboard/internal/GestureStroke.java | 79 ++++++++++--------- 1 file changed, 40 insertions(+), 39 deletions(-) diff --git a/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java b/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java index 5ba57f565..f0be0ee94 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java +++ b/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java @@ -31,15 +31,18 @@ public class GestureStroke { private int mLastPointX; private int mLastPointY; - private int mMinGestureLength; - private int mMinGestureSampleLength; + private int mMinGestureLength; // pixel + private int mMinGestureSampleLength; // pixel + private int mGestureRecognitionThreshold; // pixel / sec // TODO: Move some of these to resource. private static final float MIN_GESTURE_LENGTH_RATIO_TO_KEY_WIDTH = 0.75f; private static final int MIN_GESTURE_START_DURATION = 100; // msec private static final int MIN_GESTURE_RECOGNITION_TIME = 100; // msec private static final float MIN_GESTURE_SAMPLING_RATIO_TO_KEY_WIDTH = 1.0f / 6.0f; - private static final float GESTURE_RECOG_SPEED_THRESHOLD = 0.4f; // dip/msec + private static final float GESTURE_RECOGNITION_SPEED_THRESHOLD_RATIO_TO_KEY_WIDTH = + 5.5f; // keyWidth / sec + private static final int MSEC_PER_SEC = 1000; public static final boolean hasRecognitionTimePast( final long currentTime, final long lastRecognitionTime) { @@ -54,6 +57,8 @@ public class GestureStroke { // TODO: Find an appropriate base metric for these length. Maybe diagonal length of the key? mMinGestureLength = (int)(keyWidth * MIN_GESTURE_LENGTH_RATIO_TO_KEY_WIDTH); mMinGestureSampleLength = (int)(keyWidth * MIN_GESTURE_SAMPLING_RATIO_TO_KEY_WIDTH); + mGestureRecognitionThreshold = + (int)(keyWidth * GESTURE_RECOGNITION_SPEED_THRESHOLD_RATIO_TO_KEY_WIDTH); } public boolean isStartOfAGesture() { @@ -72,47 +77,43 @@ public class GestureStroke { mYCoordinates.setLength(0); } - private void updateLastPoint(final int x, final int y, final int time) { + public void addPoint(final int x, final int y, final int time, final boolean isHistorical) { + final boolean needsSampling; + final int size = mEventTimes.getLength(); + if (size == 0) { + needsSampling = true; + } else { + final int lastIndex = size - 1; + final int lastX = mXCoordinates.get(lastIndex); + final int lastY = mYCoordinates.get(lastIndex); + final float dist = getDistance(lastX, lastY, x, y); + needsSampling = dist > mMinGestureSampleLength; + mLength += dist; + } + if (needsSampling) { + mEventTimes.add(time); + mXCoordinates.add(x); + mYCoordinates.add(y); + } + if (!isHistorical) { + updateIncrementalRecognitionSize(x, y, time); + } + } + + private void updateIncrementalRecognitionSize(final int x, final int y, final int time) { + final int msecs = (int)(time - mLastPointTime); + if (msecs > 0) { + final int pixels = (int)getDistance(mLastPointX, mLastPointY, x, y); + // Equivalent to (pixels / msecs < mGestureRecognitionThreshold / MSEC_PER_SEC) + if (pixels * MSEC_PER_SEC < mGestureRecognitionThreshold * msecs) { + mIncrementalRecognitionSize = mEventTimes.getLength(); + } + } mLastPointTime = time; mLastPointX = x; mLastPointY = y; } - public void addPoint(final int x, final int y, final int time, final boolean isHistorical) { - final int size = mEventTimes.getLength(); - if (size == 0) { - mEventTimes.add(time); - mXCoordinates.add(x); - mYCoordinates.add(y); - if (!isHistorical) { - updateLastPoint(x, y, time); - } - return; - } - - final int lastX = mXCoordinates.get(size - 1); - final int lastY = mYCoordinates.get(size - 1); - final float dist = getDistance(lastX, lastY, x, y); - if (dist > mMinGestureSampleLength) { - mEventTimes.add(time); - mXCoordinates.add(x); - mYCoordinates.add(y); - mLength += dist; - } - - if (!isHistorical) { - final int duration = (int)(time - mLastPointTime); - if (mLastPointTime != 0 && duration > 0) { - final float distance = getDistance(mLastPointX, mLastPointY, x, y); - final float speed = distance / duration; - if (speed < GESTURE_RECOG_SPEED_THRESHOLD) { - mIncrementalRecognitionSize = size; - } - } - updateLastPoint(x, y, time); - } - } - public void appendAllBatchPoints(final InputPointers out) { appendBatchPoints(out, mEventTimes.getLength()); }