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

Commit 8e35c9eb authored by Omar Miatello's avatar Omar Miatello Committed by Android (Google) Code Review
Browse files

Merge "BackMotionEvent: add velocity (in pixels/second)" into udc-dev

parents ab8218db 4ad57489
Loading
Loading
Loading
Loading
+46 −7
Original line number Diff line number Diff line
@@ -34,6 +34,8 @@ public final class BackMotionEvent implements Parcelable {
    private final float mTouchX;
    private final float mTouchY;
    private final float mProgress;
    private final float mVelocityX;
    private final float mVelocityY;

    @BackEvent.SwipeEdge
    private final int mSwipeEdge;
@@ -43,19 +45,32 @@ public final class BackMotionEvent implements Parcelable {
    /**
     * Creates a new {@link BackMotionEvent} instance.
     *
     * <p>Note: Velocity is only computed for last event, for performance reasons.</p>
     *
     * @param touchX Absolute X location of the touch point of this event.
     * @param touchY Absolute Y location of the touch point of this event.
     * @param progress Value between 0 and 1 on how far along the back gesture is.
     * @param velocityX X velocity computed from the touch point of this event.
     *                  Value in pixels/second. {@link Float#NaN} if was not computed.
     * @param velocityY Y velocity computed from the touch point of this event.
     *                  Value in pixels/second. {@link Float#NaN} if was not computed.
     * @param swipeEdge Indicates which edge the swipe starts from.
     * @param departingAnimationTarget The remote animation target of the departing
     *                                 application window.
     */
    public BackMotionEvent(float touchX, float touchY, float progress,
    public BackMotionEvent(
            float touchX,
            float touchY,
            float progress,
            float velocityX,
            float velocityY,
            @BackEvent.SwipeEdge int swipeEdge,
            @Nullable RemoteAnimationTarget departingAnimationTarget) {
        mTouchX = touchX;
        mTouchY = touchY;
        mProgress = progress;
        mVelocityX = velocityX;
        mVelocityY = velocityY;
        mSwipeEdge = swipeEdge;
        mDepartingAnimationTarget = departingAnimationTarget;
    }
@@ -64,6 +79,8 @@ public final class BackMotionEvent implements Parcelable {
        mTouchX = in.readFloat();
        mTouchY = in.readFloat();
        mProgress = in.readFloat();
        mVelocityX = in.readFloat();
        mVelocityY = in.readFloat();
        mSwipeEdge = in.readInt();
        mDepartingAnimationTarget = in.readTypedObject(RemoteAnimationTarget.CREATOR);
    }
@@ -91,10 +108,26 @@ public final class BackMotionEvent implements Parcelable {
        dest.writeFloat(mTouchX);
        dest.writeFloat(mTouchY);
        dest.writeFloat(mProgress);
        dest.writeFloat(mVelocityX);
        dest.writeFloat(mVelocityY);
        dest.writeInt(mSwipeEdge);
        dest.writeTypedObject(mDepartingAnimationTarget, flags);
    }

    /**
     * Returns the absolute X location of the touch point.
     */
    public float getTouchX() {
        return mTouchX;
    }

    /**
     * Returns the absolute Y location of the touch point.
     */
    public float getTouchY() {
        return mTouchY;
    }

    /**
     * Returns the progress of a {@link BackEvent}.
     *
@@ -106,17 +139,21 @@ public final class BackMotionEvent implements Parcelable {
    }

    /**
     * Returns the absolute X location of the touch point.
     * Returns the X velocity computed from the touch point.
     *
     * @return value in pixels/second or {@link Float#NaN} if was not computed.
     */
    public float getTouchX() {
        return mTouchX;
    public float getVelocityX() {
        return mVelocityX;
    }

    /**
     * Returns the absolute Y location of the touch point.
     * Returns the Y velocity computed from the touch point.
     *
     * @return value in pixels/second or {@link Float#NaN} if was not computed.
     */
    public float getTouchY() {
        return mTouchY;
    public float getVelocityY() {
        return mVelocityY;
    }

    /**
@@ -143,6 +180,8 @@ public final class BackMotionEvent implements Parcelable {
                + "mTouchX=" + mTouchX
                + ", mTouchY=" + mTouchY
                + ", mProgress=" + mProgress
                + ", mVelocityX=" + mVelocityX
                + ", mVelocityY=" + mVelocityY
                + ", mSwipeEdge" + mSwipeEdge
                + ", mDepartingAnimationTarget" + mDepartingAnimationTarget
                + "}";
+7 −1
Original line number Diff line number Diff line
@@ -70,7 +70,13 @@ public class WindowOnBackInvokedDispatcherTest {
    private ApplicationInfo mApplicationInfo;

    private final BackMotionEvent mBackEvent = new BackMotionEvent(
            0, 0, 0, BackEvent.EDGE_LEFT, null);
            /* touchX = */ 0,
            /* touchY = */ 0,
            /* progress = */ 0,
            /* velocityX = */ 0,
            /* velocityY = */ 0,
            /* swipeEdge = */ BackEvent.EDGE_LEFT,
            /* departingAnimationTarget = */ null);

    @Before
    public void setUp() throws Exception {
+8 −1
Original line number Diff line number Diff line
@@ -33,12 +33,19 @@ public interface BackAnimation {
     *
     * @param touchX the X touch position of the {@link MotionEvent}.
     * @param touchY the Y touch position of the {@link MotionEvent}.
     * @param velocityX the X velocity computed from the {@link MotionEvent}.
     * @param velocityY the Y velocity computed from the {@link MotionEvent}.
     * @param keyAction the original {@link KeyEvent#getAction()} when the event was dispatched to
     *               the process. This is forwarded separately because the input pipeline may mutate
     *               the {#event} action state later.
     * @param swipeEdge the edge from which the swipe begins.
     */
    void onBackMotion(float touchX, float touchY, int keyAction,
    void onBackMotion(
            float touchX,
            float touchY,
            float velocityX,
            float velocityY,
            int keyAction,
            @BackEvent.SwipeEdge int swipeEdge);

    /**
+21 −4
Original line number Diff line number Diff line
@@ -256,8 +256,20 @@ public class BackAnimationController implements RemoteCallable<BackAnimationCont
    private class BackAnimationImpl implements BackAnimation {
        @Override
        public void onBackMotion(
                float touchX, float touchY, int keyAction, @BackEvent.SwipeEdge int swipeEdge) {
            mShellExecutor.execute(() -> onMotionEvent(touchX, touchY, keyAction, swipeEdge));
                float touchX,
                float touchY,
                float velocityX,
                float velocityY,
                int keyAction,
                @BackEvent.SwipeEdge int swipeEdge
        ) {
            mShellExecutor.execute(() -> onMotionEvent(
                    /* touchX = */ touchX,
                    /* touchY = */ touchY,
                    /* velocityX = */ velocityX,
                    /* velocityY = */ velocityY,
                    /* keyAction = */ keyAction,
                    /* swipeEdge = */ swipeEdge));
        }

        @Override
@@ -332,13 +344,18 @@ public class BackAnimationController implements RemoteCallable<BackAnimationCont
     * Called when a new motion event needs to be transferred to this
     * {@link BackAnimationController}
     */
    public void onMotionEvent(float touchX, float touchY, int keyAction,
    public void onMotionEvent(
            float touchX,
            float touchY,
            float velocityX,
            float velocityY,
            int keyAction,
            @BackEvent.SwipeEdge int swipeEdge) {
        if (mPostCommitAnimationInProgress) {
            return;
        }

        mTouchTracker.update(touchX, touchY);
        mTouchTracker.update(touchX, touchY, velocityX, velocityY);
        if (keyAction == MotionEvent.ACTION_DOWN) {
            if (!mBackGestureStarted) {
                mShouldStartOnNextMoveEvent = true;
+21 −3
Original line number Diff line number Diff line
@@ -42,11 +42,13 @@ class TouchTracker {
     */
    private float mInitTouchX;
    private float mInitTouchY;
    private float mLatestVelocityX;
    private float mLatestVelocityY;
    private float mStartThresholdX;
    private int mSwipeEdge;
    private boolean mCancelled;

    void update(float touchX, float touchY) {
    void update(float touchX, float touchY, float velocityX, float velocityY) {
        /**
         * If back was previously cancelled but the user has started swiping in the forward
         * direction again, restart back.
@@ -58,6 +60,8 @@ class TouchTracker {
        }
        mLatestTouchX = touchX;
        mLatestTouchY = touchY;
        mLatestVelocityX = velocityX;
        mLatestVelocityY = velocityY;
    }

    void setTriggerBack(boolean triggerBack) {
@@ -84,7 +88,14 @@ class TouchTracker {
    }

    BackMotionEvent createStartEvent(RemoteAnimationTarget target) {
        return new BackMotionEvent(mInitTouchX, mInitTouchY, 0, mSwipeEdge, target);
        return new BackMotionEvent(
                /* touchX = */ mInitTouchX,
                /* touchY = */ mInitTouchY,
                /* progress = */ 0,
                /* velocityX = */ 0,
                /* velocityY = */ 0,
                /* swipeEdge = */ mSwipeEdge,
                /* departingAnimationTarget = */ target);
    }

    BackMotionEvent createProgressEvent() {
@@ -111,7 +122,14 @@ class TouchTracker {
    }

    BackMotionEvent createProgressEvent(float progress) {
        return new BackMotionEvent(mLatestTouchX, mLatestTouchY, progress, mSwipeEdge, null);
        return new BackMotionEvent(
                /* touchX = */ mLatestTouchX,
                /* touchY = */ mLatestTouchY,
                /* progress = */ progress,
                /* velocityX = */ mLatestVelocityX,
                /* velocityY = */ mLatestVelocityY,
                /* swipeEdge = */ mSwipeEdge,
                /* departingAnimationTarget = */ null);
    }

    public void setProgressThreshold(float progressThreshold) {
Loading