diff --git a/quickstep/src/com/android/quickstep/inputconsumers/MotionEventsHandler.java b/quickstep/src/com/android/quickstep/inputconsumers/MotionEventsHandler.java deleted file mode 100644 index 54edde682d..0000000000 --- a/quickstep/src/com/android/quickstep/inputconsumers/MotionEventsHandler.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright (C) 2022 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.android.quickstep.inputconsumers; - -import static android.view.MotionEvent.AXIS_GESTURE_X_OFFSET; -import static android.view.MotionEvent.AXIS_GESTURE_Y_OFFSET; -import static android.view.MotionEvent.INVALID_POINTER_ID; - -import static com.android.launcher3.Utilities.getTrackpadMotionEventScale; -import static com.android.launcher3.Utilities.isTrackpadMotionEvent; - -import android.content.Context; -import android.graphics.PointF; -import android.view.MotionEvent; - -import com.android.quickstep.util.NavBarPosition; - -/** - * A motion event handler that can tracks the states of a gesture, whether it's from on-screen - * touch or trackpad gesture. - */ -public class MotionEventsHandler { - - private final PointF mDownPos = new PointF(); - private final PointF mLastPos = new PointF(); - private final int mScale; - - private int mActivePointerId = INVALID_POINTER_ID; - - private float mCurrentTrackpadOffsetX = 0; - private float mCurrentTrackpadOffsetY = 0; - - public MotionEventsHandler(Context context) { - mScale = getTrackpadMotionEventScale(context); - } - - public int getActivePointerId() { - return mActivePointerId; - } - - public void onActionDown(MotionEvent ev) { - mActivePointerId = ev.getPointerId(0); - if (isTrackpadMotionEvent(ev)) { - mCurrentTrackpadOffsetX = 0; - mCurrentTrackpadOffsetY = 0; - } else { - mDownPos.set(ev.getX(), ev.getY()); - mLastPos.set(mDownPos); - } - } - - public void onActionMove(MotionEvent ev) { - int pointerIndex = ev.findPointerIndex(mActivePointerId); - if (isTrackpadMotionEvent(ev)) { - mCurrentTrackpadOffsetX += ev.getAxisValue(AXIS_GESTURE_X_OFFSET, pointerIndex) - * mScale; - mCurrentTrackpadOffsetY += ev.getAxisValue(AXIS_GESTURE_Y_OFFSET, pointerIndex) - * mScale; - } else { - mLastPos.set(ev.getX(pointerIndex), ev.getY(pointerIndex)); - } - } - - public void onActionPointerUp(MotionEvent ev) { - int ptrIdx = ev.getActionIndex(); - int ptrId = ev.getPointerId(ptrIdx); - if (ptrId == mActivePointerId && !isTrackpadMotionEvent(ev)) { - final int newPointerIdx = ptrIdx == 0 ? 1 : 0; - mDownPos.set( - ev.getX(newPointerIdx) - (mLastPos.x - mDownPos.x), - ev.getY(newPointerIdx) - (mLastPos.y - mDownPos.y)); - mLastPos.set(ev.getX(newPointerIdx), ev.getY(newPointerIdx)); - mActivePointerId = ev.getPointerId(newPointerIdx); - } - } - - public float getDisplacement(MotionEvent ev, NavBarPosition mNavBarPosition) { - if (mNavBarPosition.isRightEdge()) { - if (isTrackpadMotionEvent(ev)) { - return mCurrentTrackpadOffsetX; - } - return ev.getX() - mDownPos.x; - } else if (mNavBarPosition.isLeftEdge()) { - if (isTrackpadMotionEvent(ev)) { - return -mCurrentTrackpadOffsetX; - } - return mDownPos.x - ev.getX(); - } else { - if (isTrackpadMotionEvent(ev)) { - return mCurrentTrackpadOffsetY; - } - return ev.getY() - mDownPos.y; - } - } - - public float getDisplacementX(MotionEvent ev) { - return isTrackpadMotionEvent(ev) ? mCurrentTrackpadOffsetX : mLastPos.x - mDownPos.x; - } - - public float getDisplacementY(MotionEvent ev) { - return isTrackpadMotionEvent(ev) ? mCurrentTrackpadOffsetY : mLastPos.y - mDownPos.y; - } -} diff --git a/quickstep/src/com/android/quickstep/inputconsumers/OtherActivityInputConsumer.java b/quickstep/src/com/android/quickstep/inputconsumers/OtherActivityInputConsumer.java index cd0d463cbb..601533d7e6 100644 --- a/quickstep/src/com/android/quickstep/inputconsumers/OtherActivityInputConsumer.java +++ b/quickstep/src/com/android/quickstep/inputconsumers/OtherActivityInputConsumer.java @@ -26,8 +26,6 @@ import static android.view.MotionEvent.INVALID_POINTER_ID; import static com.android.launcher3.PagedView.ACTION_MOVE_ALLOW_EASY_FLING; import static com.android.launcher3.PagedView.DEBUG_FAILED_QUICKSWITCH; import static com.android.launcher3.Utilities.EDGE_NAV_BAR; -import static com.android.launcher3.Utilities.getXVelocity; -import static com.android.launcher3.Utilities.getYVelocity; import static com.android.launcher3.Utilities.squaredHypot; import static com.android.launcher3.util.Executors.MAIN_EXECUTOR; import static com.android.launcher3.util.TraceHelper.FLAG_CHECK_FOR_RACE_CONDITIONS; @@ -112,8 +110,9 @@ public class OtherActivityInputConsumer extends ContextWrapper implements InputC private final FinishImmediatelyHandler mCleanupHandler = new FinishImmediatelyHandler(); private final boolean mIsDeferredDownTarget; - - private final MotionEventsHandler mMotionEventsHandler; + private final PointF mDownPos = new PointF(); + private final PointF mLastPos = new PointF(); + private int mActivePointerId = INVALID_POINTER_ID; // Distance after which we start dragging the window. private final float mTouchSlop; @@ -154,7 +153,6 @@ public class OtherActivityInputConsumer extends ContextWrapper implements InputC mVelocityTracker = VelocityTracker.obtain(); mInputMonitorCompat = inputMonitorCompat; mInputEventReceiver = inputEventReceiver; - mMotionEventsHandler = new MotionEventsHandler(base); boolean continuingPreviousGesture = mTaskAnimationManager.isRecentsAnimationRunning(); mIsDeferredDownTarget = !continuingPreviousGesture && isDeferredDownTarget; @@ -224,7 +222,9 @@ public class OtherActivityInputConsumer extends ContextWrapper implements InputC Object traceToken = TraceHelper.INSTANCE.beginSection(DOWN_EVT, FLAG_CHECK_FOR_RACE_CONDITIONS); - mMotionEventsHandler.onActionDown(ev); + mActivePointerId = ev.getPointerId(0); + mDownPos.set(ev.getX(), ev.getY()); + mLastPos.set(mDownPos); // Start the window animation on down to give more time for launcher to draw if the // user didn't start the gesture over the back button @@ -247,20 +247,27 @@ public class OtherActivityInputConsumer extends ContextWrapper implements InputC break; } case ACTION_POINTER_UP: { - mMotionEventsHandler.onActionPointerUp(ev); + int ptrIdx = ev.getActionIndex(); + int ptrId = ev.getPointerId(ptrIdx); + if (ptrId == mActivePointerId) { + final int newPointerIdx = ptrIdx == 0 ? 1 : 0; + mDownPos.set( + ev.getX(newPointerIdx) - (mLastPos.x - mDownPos.x), + ev.getY(newPointerIdx) - (mLastPos.y - mDownPos.y)); + mLastPos.set(ev.getX(newPointerIdx), ev.getY(newPointerIdx)); + mActivePointerId = ev.getPointerId(newPointerIdx); + } break; } case ACTION_MOVE: { - int pointerIndex = ev.findPointerIndex(mMotionEventsHandler.getActivePointerId()); + int pointerIndex = ev.findPointerIndex(mActivePointerId); if (pointerIndex == INVALID_POINTER_ID) { break; } - - mMotionEventsHandler.onActionMove(ev); - final float displacement = mMotionEventsHandler.getDisplacement(ev, - mNavBarPosition); - final float displacementX = mMotionEventsHandler.getDisplacementX(ev); - final float displacementY= mMotionEventsHandler.getDisplacementY(ev); + mLastPos.set(ev.getX(pointerIndex), ev.getY(pointerIndex)); + float displacement = getDisplacement(ev); + float displacementX = mLastPos.x - mDownPos.x; + float displacementY = mLastPos.y - mDownPos.y; if (!mPassedWindowMoveSlop) { if (!mIsDeferredDownTarget) { @@ -342,8 +349,8 @@ public class OtherActivityInputConsumer extends ContextWrapper implements InputC case ACTION_CANCEL: case ACTION_UP: { if (DEBUG_FAILED_QUICKSWITCH && !mPassedWindowMoveSlop) { - final float displacementX = mMotionEventsHandler.getDisplacementX(ev); - final float displacementY = mMotionEventsHandler.getDisplacementY(ev); + float displacementX = mLastPos.x - mDownPos.x; + float displacementY = mLastPos.y - mDownPos.y; Log.d("Quickswitch", "mPassedWindowMoveSlop=false" + " disp=" + squaredHypot(displacementX, displacementY) + " slop=" + mSquaredTouchSlop); @@ -400,19 +407,16 @@ public class OtherActivityInputConsumer extends ContextWrapper implements InputC mInteractionHandler.onGestureCancelled(); } else { mVelocityTracker.computeCurrentVelocity(PX_PER_MS); - int activePointerId = mMotionEventsHandler.getActivePointerId(); - float velocityXPxPerMs = getXVelocity(mVelocityTracker, ev, activePointerId); - float velocityYPxPerMs = getYVelocity(mVelocityTracker, ev, activePointerId); + float velocityXPxPerMs = mVelocityTracker.getXVelocity(mActivePointerId); + float velocityYPxPerMs = mVelocityTracker.getYVelocity(mActivePointerId); float velocityPxPerMs = mNavBarPosition.isRightEdge() ? velocityXPxPerMs : mNavBarPosition.isLeftEdge() ? -velocityXPxPerMs : velocityYPxPerMs; - mInteractionHandler.updateDisplacement( - mMotionEventsHandler.getDisplacement(ev, mNavBarPosition) - - mStartDisplacement); - mInteractionHandler.onGestureEnded( - velocityPxPerMs, new PointF(velocityXPxPerMs, velocityYPxPerMs)); + mInteractionHandler.updateDisplacement(getDisplacement(ev) - mStartDisplacement); + mInteractionHandler.onGestureEnded(velocityPxPerMs, + new PointF(velocityXPxPerMs, velocityYPxPerMs)); } } else { // Since we start touch tracking on DOWN, we may reach this state without actually @@ -475,6 +479,16 @@ public class OtherActivityInputConsumer extends ContextWrapper implements InputC } } + private float getDisplacement(MotionEvent ev) { + if (mNavBarPosition.isRightEdge()) { + return ev.getX() - mDownPos.x; + } else if (mNavBarPosition.isLeftEdge()) { + return mDownPos.x - ev.getX(); + } else { + return ev.getY() - mDownPos.y; + } + } + @Override public boolean allowInterceptByParent() { return !mPassedPilferInputSlop; diff --git a/src/com/android/launcher3/Utilities.java b/src/com/android/launcher3/Utilities.java index b66ed81a81..1354d21069 100644 --- a/src/com/android/launcher3/Utilities.java +++ b/src/com/android/launcher3/Utilities.java @@ -62,7 +62,6 @@ import android.util.DisplayMetrics; import android.util.Log; import android.util.TypedValue; import android.view.MotionEvent; -import android.view.VelocityTracker; import android.view.View; import android.view.ViewConfiguration; import android.view.animation.Interpolator; @@ -156,8 +155,6 @@ public final class Utilities { public static boolean IS_RUNNING_IN_TEST_HARNESS = ActivityManager.isRunningInTestHarness(); - private static final int TRACKPAD_GESTURE_SCALE = 60; - public static void enableRunningInTestHarnessForTests() { IS_RUNNING_IN_TEST_HARNESS = true; } @@ -716,38 +713,6 @@ public final class Utilities { && (event.getSource() & SOURCE_TOUCHSCREEN) != SOURCE_TOUCHSCREEN; } - public static int getTrackpadMotionEventScale(Context context) { - return ViewConfiguration.get(context).getScaledTouchSlop() * TRACKPAD_GESTURE_SCALE; - } - - public static float getXVelocity(VelocityTracker velocityTracker, MotionEvent event, - int pointerId) { - // Will be enabled after ag/20353570 is submitted -// if (isTrackpadMotionEvent(event)) { -// return velocityTracker.getAxisVelocity(AXIS_GESTURE_X_OFFSET, pointerId); -// } else { - return velocityTracker.getXVelocity(pointerId); -// } - } - - public static float getXVelocity(VelocityTracker velocityTracker, MotionEvent event) { - return getXVelocity(velocityTracker, event, -1 /* ACTIVE_POINTER_ID */); - } - - public static float getYVelocity(VelocityTracker velocityTracker, MotionEvent event, - int pointerId) { - // Will be enabled after ag/20353570 is submitted -// if (isTrackpadMotionEvent(event)) { -// return velocityTracker.getAxisVelocity(AXIS_GESTURE_Y_OFFSET, pointerId); -// } else { - return velocityTracker.getYVelocity(pointerId); -// } - } - - public static float getYVelocity(VelocityTracker velocityTracker, MotionEvent event) { - return getYVelocity(velocityTracker, event, -1 /* ACTIVE_POINTER_ID */); - } - /** Logs the Scale and Translate properties of a matrix. Ignores skew and perspective. */ public static void logMatrix(String label, Matrix matrix) { float[] matrixValues = new float[9];