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

Commit 0c17a76e authored by ryanlwlin's avatar ryanlwlin
Browse files

Add GesturesObserver to detect series of gestures

Adding GestureObserver for the gesture detection mechanism.

Bug: 149269335
Test: GesturesObserverTest, SimpleSwipeTest, TwoFingersDownTest
Change-Id: I052ede9ec0698c9811a6cf2b2a13333a313b2943
parent 1e303ff4
Loading
Loading
Loading
Loading
+21 −13
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.server.accessibility.gestures;
import static com.android.server.accessibility.gestures.TouchExplorer.DEBUG;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.os.Handler;
import android.util.Slog;
import android.view.MotionEvent;
@@ -31,21 +32,22 @@ import android.view.ViewConfiguration;
 * onUp, etc methods as necessary. If you don't override a method your matcher will do nothing in
 * response to that type of event. Finally, be sure to give your gesture a name by overriding
 * getGestureName().
 * @hide
 */
abstract class GestureMatcher {
public abstract class GestureMatcher {
    // Potential states for this individual gesture matcher.
    // In STATE_CLEAR, this matcher is accepting new motion events but has not formally signaled
    // that there is enough data to judge that a gesture has started.
    static final int STATE_CLEAR = 0;
    public static final int STATE_CLEAR = 0;
    // In STATE_GESTURE_STARTED, this matcher continues to accept motion events and it has signaled
    // to the gesture manifold that what looks like the specified gesture has started.
    static final int STATE_GESTURE_STARTED = 1;
    public static final int STATE_GESTURE_STARTED = 1;
    // In STATE_GESTURE_COMPLETED, this matcher has successfully matched the specified gesture. and
    // will not accept motion events until it is cleared.
    static final int STATE_GESTURE_COMPLETED = 2;
    public static final int STATE_GESTURE_COMPLETED = 2;
    // In STATE_GESTURE_CANCELED, this matcher will not accept new motion events because it is
    // impossible that this set of motion events will match the specified gesture.
    static final int STATE_GESTURE_CANCELED = 3;
    public static final int STATE_GESTURE_CANCELED = 3;

    @IntDef({STATE_CLEAR, STATE_GESTURE_STARTED, STATE_GESTURE_COMPLETED, STATE_GESTURE_CANCELED})
    public @interface State {}
@@ -56,14 +58,14 @@ abstract class GestureMatcher {
    // handler for asynchronous operations like timeouts
    private final Handler mHandler;

    private final StateChangeListener mListener;
    private StateChangeListener mListener;

    // Use this to transition to new states after a delay.
    // e.g. cancel or complete after some timeout.
    // Convenience functions for tapTimeout and doubleTapTimeout are already defined here.
    protected final DelayedTransition mDelayedTransition;

    GestureMatcher(int gestureId, Handler handler, StateChangeListener listener) {
    protected GestureMatcher(int gestureId, Handler handler, StateChangeListener listener) {
        mGestureId = gestureId;
        mHandler = handler;
        mDelayedTransition = new DelayedTransition();
@@ -75,12 +77,12 @@ abstract class GestureMatcher {
     * information should override this method to reset their own state information and call
     * super.clear().
     */
    protected void clear() {
    public void clear() {
        mState = STATE_CLEAR;
        cancelPendingTransitions();
    }

    public int getState() {
    public final int getState() {
        return mState;
    }

@@ -92,8 +94,10 @@ abstract class GestureMatcher {
            @State int state, MotionEvent event, MotionEvent rawEvent, int policyFlags) {
        mState = state;
        cancelPendingTransitions();
        if (mListener != null) {
            mListener.onStateChanged(mGestureId, mState, event, rawEvent, policyFlags);
        }
    }

    /** Indicates that there is evidence to suggest that this gesture has started. */
    protected final void startGesture(MotionEvent event, MotionEvent rawEvent, int policyFlags) {
@@ -110,6 +114,10 @@ abstract class GestureMatcher {
        setState(STATE_GESTURE_COMPLETED, event, rawEvent, policyFlags);
    }

    public final void setListener(@NonNull StateChangeListener listener) {
        mListener = listener;
    }

    public int getGestureId() {
        return mGestureId;
    }
@@ -272,7 +280,7 @@ abstract class GestureMatcher {
        completeAfter(ViewConfiguration.getDoubleTapTimeout(), event, rawEvent, policyFlags);
    }

    public static String getStateSymbolicName(@State int state) {
    static String getStateSymbolicName(@State int state) {
        switch (state) {
            case STATE_CLEAR:
                return "STATE_CLEAR";
@@ -291,7 +299,7 @@ abstract class GestureMatcher {
     * Returns a readable name for this matcher that can be displayed to the user and in system
     * logs.
     */
    abstract String getGestureName();
    protected abstract String getGestureName();

    /**
     * Returns a String representation of this matcher. Each matcher can override this method to add
@@ -363,7 +371,7 @@ abstract class GestureMatcher {
    }

    /** Interface to allow a class to listen for state changes in a specific gesture matcher */
    interface StateChangeListener {
    public interface StateChangeListener {

        void onStateChanged(
                int gestureId, int state, MotionEvent event, MotionEvent rawEvent, int policyFlags);
+1 −1
Original line number Diff line number Diff line
@@ -79,7 +79,7 @@ class MultiFingerMultiTap extends GestureMatcher {
    }

    @Override
    protected void clear() {
    public void clear() {
        mCompletedTapCount = 0;
        mIsTargetFingerCountReached = false;
        for (int i = 0; i < mBases.length; i++) {
+2 −2
Original line number Diff line number Diff line
@@ -106,7 +106,7 @@ class MultiFingerSwipe extends GestureMatcher {
    }

    @Override
    protected void clear() {
    public void clear() {
        mTargetFingerCountReached = false;
        mCurrentFingerCount = 0;
        for (int i = 0; i < mTargetFingerCount; ++i) {
@@ -489,7 +489,7 @@ class MultiFingerSwipe extends GestureMatcher {
    }

    @Override
    String getGestureName() {
    protected String getGestureName() {
        StringBuilder builder = new StringBuilder();
        builder.append(mTargetFingerCount).append("-finger ");
        builder.append("Swipe ").append(directionToString(mDirection));
+1 −1
Original line number Diff line number Diff line
@@ -51,7 +51,7 @@ class MultiTap extends GestureMatcher {
    }

    @Override
    protected void clear() {
    public void clear() {
        mCurrentTaps = 0;
        mBaseX = Float.NaN;
        mBaseY = Float.NaN;
+1 −1
Original line number Diff line number Diff line
@@ -53,7 +53,7 @@ class SecondFingerMultiTap extends GestureMatcher {
    }

    @Override
    protected void clear() {
    public void clear() {
        mCurrentTaps = 0;
        mBaseX = Float.NaN;
        mBaseY = Float.NaN;
Loading