Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 61dcaaf1 authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka
Browse files

Use device independent parameter for gesture

Change-Id: Iea95992e4482108a498f14ec595f3eacc7d7fc4b
parent 84c1bbd7
Loading
Loading
Loading
Loading
+32 −31
Original line number Diff line number Diff line
@@ -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,45 +77,41 @@ public class GestureStroke {
        mYCoordinates.setLength(0);
    }

    private void updateLastPoint(final int x, final int y, final int time) {
        mLastPointTime = time;
        mLastPointX = x;
        mLastPointY = y;
    }

    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) {
            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);
            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);
        if (dist > mMinGestureSampleLength) {
            needsSampling = dist > mMinGestureSampleLength;
            mLength += dist;
        }
        if (needsSampling) {
            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;
            updateIncrementalRecognitionSize(x, y, time);
        }
    }
            updateLastPoint(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 appendAllBatchPoints(final InputPointers out) {