From 852a04c2dacb6407c51299c9cc1c6e7874a98614 Mon Sep 17 00:00:00 2001 From: Ben Lin Date: Tue, 2 Feb 2021 12:46:46 -0800 Subject: [PATCH] PiP: Better rotation angle calculation. Bug: 178881304 Test: Manual Change-Id: Ib63ef96a7b8771b2a920a25e44d8583dc584cf05 --- .../pip/phone/PipResizeGestureHandler.java | 51 ++++++++++++------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipResizeGestureHandler.java b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipResizeGestureHandler.java index b91ba34c44644..53571ff70c6f8 100644 --- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipResizeGestureHandler.java +++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipResizeGestureHandler.java @@ -107,6 +107,7 @@ public class PipResizeGestureHandler { private boolean mThresholdCrossed0; private boolean mThresholdCrossed1; private boolean mUsingPinchToZoom = false; + private float mAngle = 0; int mFirstIndex = -1; int mSecondIndex = -1; @@ -420,18 +421,25 @@ public class PipResizeGestureHandler { float down1X = mDownSecondaryPoint.x; float down1Y = mDownSecondaryPoint.y; - // Top right + Bottom left pinch to zoom. - if ((down0X > focusX && down0Y < focusY && down1X < focusX && down1Y > focusY) - || (down1X > focusX && down1Y < focusY - && down0X < focusX && down0Y > focusY)) { + if (down0X > focusX && down0Y < focusY && down1X < focusX && down1Y > focusY) { + // Top right + Bottom left pinch to zoom. mAngle = calculateRotationAngle(mLastResizeBounds.centerX(), mLastResizeBounds.centerY(), x0, y0, x1, y1, true); - } else if ((down0X < focusX && down0Y < focusY - && down1X > focusX && down1Y > focusY) - || (down1X < focusX && down1Y < focusY - && down0X > focusX && down0Y > focusY)) { + } else if (down1X > focusX && down1Y < focusY + && down0X < focusX && down0Y > focusY) { + // Top right + Bottom left pinch to zoom. + mAngle = calculateRotationAngle(mLastResizeBounds.centerX(), + mLastResizeBounds.centerY(), x1, y1, x0, y0, true); + } else if (down0X < focusX && down0Y < focusY + && down1X > focusX && down1Y > focusY) { + // Top left + bottom right pinch to zoom. mAngle = calculateRotationAngle(mLastResizeBounds.centerX(), mLastResizeBounds.centerY(), x0, y0, x1, y1, false); + } else if (down1X < focusX && down1Y < focusY + && down0X > focusX && down0Y > focusY) { + // Top left + bottom right pinch to zoom. + mAngle = calculateRotationAngle(mLastResizeBounds.centerX(), + mLastResizeBounds.centerY(), x1, y1, x0, y0, false); } mLastResizeBounds.set(PipPinchResizingAlgorithm.pinchResize(x0, y0, x1, y1, @@ -445,21 +453,26 @@ public class PipResizeGestureHandler { } } - private float mAngle = 0; - - private float calculateRotationAngle(int focusX, int focusY, float x0, float y0, - float x1, float y1, boolean positive) { + private float calculateRotationAngle(int pivotX, int pivotY, float topX, float topY, + float bottomX, float bottomY, boolean positive) { // The base angle is the angle formed by taking the angle between the center horizontal // and one of the corners. - double baseAngle = Math.toDegrees(Math.atan2(Math.abs(mLastResizeBounds.top - focusY), - Math.abs(mLastResizeBounds.right - focusX))); + double baseAngle = Math.toDegrees(Math.atan2(Math.abs(mLastResizeBounds.top - pivotY), + Math.abs(mLastResizeBounds.right - pivotX))); + double angle0 = mThresholdCrossed0 - ? Math.toDegrees(Math.atan2(Math.abs(y0 - focusY), Math.abs(x0 - focusX))) - : baseAngle; - double angle1 = mThresholdCrossed1 - ? Math.toDegrees(Math.atan2(Math.abs(y1 - focusY), Math.abs(x1 - focusX))) - : baseAngle; + ? Math.toDegrees(Math.atan2(pivotY - topY, topX - pivotX)) : baseAngle; + double angle1 = mThresholdCrossed0 + ? Math.toDegrees(Math.atan2(pivotY - bottomY, bottomX - pivotX)) : baseAngle; + + + if (positive) { + angle1 = angle1 < 0 ? 180 + angle1 : angle1 - 180; + } else { + angle0 = angle0 < 0 ? -angle0 - 180 : 180 - angle0; + angle1 = -angle1; + } // Calculate the percentage difference of [0, 90] compare to the base angle. double diff0 = (Math.max(0, Math.min(angle0, 90)) - baseAngle) / 90;