Tune the gesture detection logic a bit

Change-Id: Ia8e8c15fdbbd49768d57cafd50325e7e45af6251
main
Tadashi G. Takaoka 2012-07-19 14:47:55 +09:00
parent 918e420d1b
commit 1e6f39a9f9
2 changed files with 45 additions and 25 deletions

View File

@ -42,7 +42,7 @@ public class PointerTracker {
// TODO: There should be an option to turn on/off the gesture input.
private static boolean sIsGestureEnabled = true;
private static final int MIN_RECOGNITION_TIME = 100; // msec
private static final int MIN_GESTURE_RECOGNITION_TIME = 100; // msec
public interface KeyEventHandler {
/**
@ -122,6 +122,10 @@ public class PointerTracker {
private static final ArrayList<PointerTracker> sTrackers = new ArrayList<PointerTracker>();
private static PointerTrackerQueue sPointerTrackerQueue;
// HACK: Change gesture detection criteria depending on this variable.
// TODO: Find more comprehensive ways to detect a gesture start.
// True when the previous user input was a gesture input, not a typing input.
private static boolean sWasInGesture;
public final int mPointerId;
@ -138,6 +142,7 @@ public class PointerTracker {
private boolean mIsPossibleGesture = false;
private boolean mInGesture = false;
// TODO: Remove these variables
private int mLastRecognitionPointSize = 0;
private long mLastRecognitionTime = 0;
@ -273,7 +278,7 @@ public class PointerTracker {
// TODO: To handle multi-touch gestures we may want to move this method to
// {@link PointerTrackerQueue}.
public static void clearBatchInputPoints() {
public static void clearBatchInputPointsOfAllPointerTrackers() {
for (final PointerTracker tracker : sTrackers) {
tracker.mGestureStroke.reset();
}
@ -550,18 +555,26 @@ public class PointerTracker {
Log.d(TAG, "onEndBatchInput: batchPoints=" + batchPoints.getPointerSize());
}
mListener.onEndBatchInput(batchPoints);
mInGesture = false;
clearBatchInputPoints();
clearBatchInputRecognitionStateOfThisPointerTracker();
clearBatchInputPointsOfAllPointerTrackers();
sWasInGesture = true;
}
private void abortBatchInput() {
clearBatchInputRecognitionStateOfThisPointerTracker();
clearBatchInputPointsOfAllPointerTrackers();
}
private void clearBatchInputRecognitionStateOfThisPointerTracker() {
mIsPossibleGesture = false;
mInGesture = false;
mLastRecognitionPointSize = 0;
mLastRecognitionTime = 0;
}
private boolean updateBatchInputRecognitionState(long eventTime, int size) {
if (size > mLastRecognitionPointSize
&& eventTime > mLastRecognitionTime + MIN_RECOGNITION_TIME) {
&& eventTime > mLastRecognitionTime + MIN_GESTURE_RECOGNITION_TIME) {
mLastRecognitionPointSize = size;
mLastRecognitionTime = eventTime;
return true;
@ -675,7 +688,7 @@ public class PointerTracker {
if (sIsGestureEnabled && mIsPossibleGesture) {
final GestureStroke stroke = mGestureStroke;
stroke.addPoint(x, y, gestureTime, isHistorical);
if (!mInGesture && stroke.isStartOfAGesture(gestureTime)) {
if (!mInGesture && stroke.isStartOfAGesture(gestureTime, sWasInGesture)) {
startBatchInput();
}
}
@ -865,10 +878,10 @@ public class PointerTracker {
}
public void onShowMoreKeysPanel(int x, int y, KeyEventHandler handler) {
abortBatchInput();
onLongPressed();
onDownEvent(x, y, SystemClock.uptimeMillis(), handler);
mIsShowingMoreKeysPanel = true;
abortBatchInput();
}
public void onLongPressed() {
@ -947,6 +960,7 @@ public class PointerTracker {
int code = key.mCode;
callListenerOnCodeInput(key, code, x, y);
callListenerOnRelease(key, code, false);
sWasInGesture = false;
}
private void printTouchEvent(String title, int x, int y, long eventTime) {

View File

@ -26,19 +26,21 @@ public class GestureStroke {
private final InputPointers mInputPointers = new InputPointers(DEFAULT_CAPACITY);
private float mLength;
private float mAngle;
private int mIncrementalRecognitionPoint;
private boolean mHasSharpCorner;
private int mIncrementalRecognitionSize;
private long mLastPointTime;
private int mLastPointX;
private int mLastPointY;
private int mMinGestureLength;
private int mMinGestureLengthWhileInGesture;
private int mMinGestureSampleLength;
// TODO: Tune these parameters.
private static final float MIN_GESTURE_DETECTION_RATIO_TO_KEY_WIDTH = 1.0f / 4.0f;
// TODO: Move some of these to resource.
private static final float MIN_GESTURE_LENGTH_RATIO_TO_KEY_WIDTH = 1.0f;
private static final float MIN_GESTURE_LENGTH_RATIO_TO_KEY_WIDTH_WHILE_IN_GESTURE = 0.5f;
private static final int MIN_GESTURE_DURATION = 150; // msec
private static final int MIN_GESTURE_DURATION_WHILE_IN_GESTURE = 75; // msec
private static final float MIN_GESTURE_SAMPLING_RATIO_TO_KEY_HEIGHT = 1.0f / 6.0f;
private static final int MIN_GESTURE_DURATION = 100; // msec
private static final float GESTURE_RECOG_SPEED_THRESHOLD = 0.4f; // dip/msec
private static final float GESTURE_RECOG_CURVATURE_THRESHOLD = (float)(Math.PI / 4.0f);
@ -50,19 +52,27 @@ public class GestureStroke {
}
public void setGestureSampleLength(final int keyWidth, final int keyHeight) {
mMinGestureLength = (int)(keyWidth * MIN_GESTURE_DETECTION_RATIO_TO_KEY_WIDTH);
// 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);
mMinGestureLengthWhileInGesture = (int)(
keyWidth * MIN_GESTURE_LENGTH_RATIO_TO_KEY_WIDTH_WHILE_IN_GESTURE);
mMinGestureSampleLength = (int)(keyHeight * MIN_GESTURE_SAMPLING_RATIO_TO_KEY_HEIGHT);
}
public boolean isStartOfAGesture(int downDuration) {
public boolean isStartOfAGesture(final int downDuration, final boolean wasInGesture) {
// The tolerance of the time duration and the stroke length to detect the start of a
// gesture stroke should be eased when the previous input was a gesture input.
if (wasInGesture) {
return downDuration > MIN_GESTURE_DURATION_WHILE_IN_GESTURE
&& mLength > mMinGestureLengthWhileInGesture;
}
return downDuration > MIN_GESTURE_DURATION && mLength > mMinGestureLength;
}
public void reset() {
mLength = 0;
mAngle = 0;
mIncrementalRecognitionPoint = 0;
mHasSharpCorner = false;
mIncrementalRecognitionSize = 0;
mLastPointTime = 0;
mInputPointers.reset();
}
@ -93,15 +103,11 @@ public class GestureStroke {
mLength += dist;
final float angle = getAngle(lastX, lastY, x, y);
if (size > 1) {
float curvature = getAngleDiff(angle, mAngle);
final float curvature = getAngleDiff(angle, mAngle);
if (curvature > GESTURE_RECOG_CURVATURE_THRESHOLD) {
if (size > mIncrementalRecognitionPoint) {
mIncrementalRecognitionPoint = size;
if (size > mIncrementalRecognitionSize) {
mIncrementalRecognitionSize = size;
}
mHasSharpCorner = true;
}
if (!mHasSharpCorner) {
mIncrementalRecognitionPoint = size;
}
}
mAngle = angle;
@ -112,7 +118,7 @@ public class GestureStroke {
if (mLastPointTime != 0 && duration > 0) {
final float speed = getDistance(mLastPointX, mLastPointY, x, y) / duration;
if (speed < GESTURE_RECOG_SPEED_THRESHOLD) {
mIncrementalRecognitionPoint = size;
mIncrementalRecognitionSize = size;
}
}
updateLastPoint(x, y, time);
@ -124,7 +130,7 @@ public class GestureStroke {
}
public void appendIncrementalBatchPoints(final InputPointers out) {
out.append(mInputPointers, 0, mIncrementalRecognitionPoint);
out.append(mInputPointers, 0, mIncrementalRecognitionSize);
}
private static float getDistance(final int p1x, final int p1y,