From 1899955c1c273d87388830f312c341e966c31e03 Mon Sep 17 00:00:00 2001 From: Vishnu Nair Date: Thu, 13 Dec 2018 09:28:11 -0800 Subject: Pip: Use raw input coordiates when calculating pip movement offsets * Recently the pip_input_consumer changed to move with the pinned stack bounds. * Change the touch logic to use raw input coordinates so that we do not have to account for the pip_input_consumer position. * Add workaround for velocity tracker to support moving input frame. * Use getActionMasked instead of getAction for MotionEvents. This fixes b/120942892. Test: Test moving pip window in YouTube and Chrome Bug: 120663157, 120942892 Change-Id: I0a8b2eea7ee5930a6651ad037eaa0f898fe8635d --- api/current.txt | 2 ++ core/java/android/view/MotionEvent.java | 32 ++++++++++++++++++++++ .../android/systemui/pip/phone/PipTouchState.java | 29 ++++++++++++++------ 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/api/current.txt b/api/current.txt index 2ef4a392ef23..3def5dc7e4cd 100644 --- a/api/current.txt +++ b/api/current.txt @@ -48737,7 +48737,9 @@ package android.view { method public float getPressure(); method public float getPressure(int); method public float getRawX(); + method public float getRawX(int); method public float getRawY(); + method public float getRawY(int); method public float getSize(); method public float getSize(int); method public int getSource(); diff --git a/core/java/android/view/MotionEvent.java b/core/java/android/view/MotionEvent.java index b59d8c720b9d..a86abe517b6a 100644 --- a/core/java/android/view/MotionEvent.java +++ b/core/java/android/view/MotionEvent.java @@ -2587,6 +2587,38 @@ public final class MotionEvent extends InputEvent implements Parcelable { return nativeGetRawAxisValue(mNativePtr, AXIS_Y, 0, HISTORY_CURRENT); } + /** + * Returns the original raw X coordinate of this event. For touch + * events on the screen, this is the original location of the event + * on the screen, before it had been adjusted for the containing window + * and views. + * + * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 + * (the first pointer that is down) to {@link #getPointerCount()}-1. + * + * @see #getX(int) + * @see #AXIS_X + */ + public float getRawX(int pointerIndex) { + return nativeGetRawAxisValue(mNativePtr, AXIS_X, pointerIndex, HISTORY_CURRENT); + } + + /** + * Returns the original raw Y coordinate of this event. For touch + * events on the screen, this is the original location of the event + * on the screen, before it had been adjusted for the containing window + * and views. + * + * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 + * (the first pointer that is down) to {@link #getPointerCount()}-1. + * + * @see #getY(int) + * @see #AXIS_Y + */ + public float getRawY(int pointerIndex) { + return nativeGetRawAxisValue(mNativePtr, AXIS_Y, pointerIndex, HISTORY_CURRENT); + } + /** * Return the precision of the X coordinates being reported. You can * multiply this number with {@link #getX} to find the actual hardware diff --git a/packages/SystemUI/src/com/android/systemui/pip/phone/PipTouchState.java b/packages/SystemUI/src/com/android/systemui/pip/phone/PipTouchState.java index 9aa21f8270b3..69efbc8575e0 100644 --- a/packages/SystemUI/src/com/android/systemui/pip/phone/PipTouchState.java +++ b/packages/SystemUI/src/com/android/systemui/pip/phone/PipTouchState.java @@ -84,7 +84,7 @@ public class PipTouchState { * Processes a given touch event and updates the state. */ public void onTouchEvent(MotionEvent ev) { - switch (ev.getAction()) { + switch (ev.getActionMasked()) { case MotionEvent.ACTION_DOWN: { if (!mAllowTouches) { return; @@ -92,12 +92,13 @@ public class PipTouchState { // Initialize the velocity tracker initOrResetVelocityTracker(); + addMovement(ev); mActivePointerId = ev.getPointerId(0); if (DEBUG) { Log.e(TAG, "Setting active pointer id on DOWN: " + mActivePointerId); } - mLastTouch.set(ev.getX(), ev.getY()); + mLastTouch.set(ev.getRawX(), ev.getRawY()); mDownTouch.set(mLastTouch); mAllowDraggingOffscreen = true; mIsUserInteracting = true; @@ -118,15 +119,15 @@ public class PipTouchState { } // Update the velocity tracker - mVelocityTracker.addMovement(ev); + addMovement(ev); int pointerIndex = ev.findPointerIndex(mActivePointerId); if (pointerIndex == -1) { Log.e(TAG, "Invalid active pointer id on MOVE: " + mActivePointerId); break; } - float x = ev.getX(pointerIndex); - float y = ev.getY(pointerIndex); + float x = ev.getRawX(pointerIndex); + float y = ev.getRawY(pointerIndex); mLastDelta.set(x - mLastTouch.x, y - mLastTouch.y); mDownDelta.set(x - mDownTouch.x, y - mDownTouch.y); @@ -149,7 +150,7 @@ public class PipTouchState { } // Update the velocity tracker - mVelocityTracker.addMovement(ev); + addMovement(ev); int pointerIndex = ev.getActionIndex(); int pointerId = ev.getPointerId(pointerIndex); @@ -161,7 +162,7 @@ public class PipTouchState { Log.e(TAG, "Relinquish active pointer id on POINTER_UP: " + mActivePointerId); } - mLastTouch.set(ev.getX(newPointerIndex), ev.getY(newPointerIndex)); + mLastTouch.set(ev.getRawX(newPointerIndex), ev.getRawY(newPointerIndex)); } break; } @@ -172,7 +173,7 @@ public class PipTouchState { } // Update the velocity tracker - mVelocityTracker.addMovement(ev); + addMovement(ev); mVelocityTracker.computeCurrentVelocity(1000, mViewConfig.getScaledMaximumFlingVelocity()); mVelocity.set(mVelocityTracker.getXVelocity(), mVelocityTracker.getYVelocity()); @@ -184,7 +185,7 @@ public class PipTouchState { } mUpTouchTime = ev.getEventTime(); - mLastTouch.set(ev.getX(pointerIndex), ev.getY(pointerIndex)); + mLastTouch.set(ev.getRawX(pointerIndex), ev.getRawY(pointerIndex)); mPreviouslyDragging = mIsDragging; mIsWaitingForDoubleTap = !mIsDoubleTap && !mIsDragging && (mUpTouchTime - mDownTouchTime) < DOUBLE_TAP_TIMEOUT; @@ -331,6 +332,16 @@ public class PipTouchState { } } + private void addMovement(MotionEvent event) { + // Add movement to velocity tracker using raw screen X and Y coordinates instead + // of window coordinates because the window frame may be moving at the same time. + float deltaX = event.getRawX() - event.getX(); + float deltaY = event.getRawY() - event.getY(); + event.offsetLocation(deltaX, deltaY); + mVelocityTracker.addMovement(event); + event.offsetLocation(-deltaX, -deltaY); + } + public void dump(PrintWriter pw, String prefix) { final String innerPrefix = prefix + " "; pw.println(prefix + TAG); -- cgit v1.2.3-59-g8ed1b