Account for early display rotation for gestures with shell transitions

- Ensure the events are in a consistent coordinate frame from the start
  of the gesture
- Now that the display rotation happens at the start of the gesture, we
  need to use the rotation at the start of the gesture in some cases
- Use the pre-rotation screenspace bounds when calculating the thumbnail
  matrix

Bug: 197687032
Test: Enable shell transitions and swipe up from a landscape app into
      force-portrait Launcher

Change-Id: I61ebe9947a55937c59a47336aa92561e7e3e8b66
diff --git a/quickstep/src/com/android/quickstep/OrientationRectF.java b/quickstep/src/com/android/quickstep/OrientationRectF.java
index 59a202c..aa01b05 100644
--- a/quickstep/src/com/android/quickstep/OrientationRectF.java
+++ b/quickstep/src/com/android/quickstep/OrientationRectF.java
@@ -66,7 +66,7 @@
         return applyTransform(event, deltaRotation(mRotation, currentRotation), forceTransform);
     }
 
-    private boolean applyTransform(MotionEvent event, int deltaRotation, boolean forceTransform) {
+    public boolean applyTransform(MotionEvent event, int deltaRotation, boolean forceTransform) {
         mTmpMatrix.reset();
         postDisplayRotation(deltaRotation, mHeight, mWidth, mTmpMatrix);
         if (forceTransform) {
diff --git a/quickstep/src/com/android/quickstep/OrientationTouchTransformer.java b/quickstep/src/com/android/quickstep/OrientationTouchTransformer.java
index ecff4f1..d2d3ba3 100644
--- a/quickstep/src/com/android/quickstep/OrientationTouchTransformer.java
+++ b/quickstep/src/com/android/quickstep/OrientationTouchTransformer.java
@@ -22,6 +22,7 @@
 import static android.view.MotionEvent.ACTION_POINTER_DOWN;
 import static android.view.MotionEvent.ACTION_UP;
 
+import static com.android.launcher3.states.RotationHelper.deltaRotation;
 import android.content.res.Resources;
 import android.graphics.Point;
 import android.graphics.RectF;
@@ -358,7 +359,18 @@
                 if (mLastRectTouched == null) {
                     return;
                 }
-                mLastRectTouched.applyTransformFromRotation(event, mCurrentDisplay.rotation, true);
+                if (TaskAnimationManager.ENABLE_SHELL_TRANSITIONS) {
+                    if (event.getSurfaceRotation() != mActiveTouchRotation) {
+                        // With Shell transitions, we should rotated to the orientation at the start
+                        // of the gesture not the current display rotation which will happen early
+                        mLastRectTouched.applyTransform(event,
+                                deltaRotation(event.getSurfaceRotation(), mActiveTouchRotation),
+                                true);
+                    }
+                } else {
+                    mLastRectTouched.applyTransformFromRotation(event, mCurrentDisplay.rotation,
+                            true);
+                }
                 break;
             }
             case ACTION_CANCEL:
@@ -366,7 +378,18 @@
                 if (mLastRectTouched == null) {
                     return;
                 }
-                mLastRectTouched.applyTransformFromRotation(event, mCurrentDisplay.rotation, true);
+                if (TaskAnimationManager.ENABLE_SHELL_TRANSITIONS) {
+                    if (event.getSurfaceRotation() != mActiveTouchRotation) {
+                        // With Shell transitions, we should rotated to the orientation at the start
+                        // of the gesture not the current display rotation which will happen early
+                        mLastRectTouched.applyTransform(event,
+                                deltaRotation(event.getSurfaceRotation(), mActiveTouchRotation),
+                                true);
+                    }
+                } else {
+                    mLastRectTouched.applyTransformFromRotation(event, mCurrentDisplay.rotation,
+                            true);
+                }
                 mLastRectTouched = null;
                 break;
             }
diff --git a/quickstep/src/com/android/quickstep/RemoteTargetGluer.java b/quickstep/src/com/android/quickstep/RemoteTargetGluer.java
index b031c47..a12a670 100644
--- a/quickstep/src/com/android/quickstep/RemoteTargetGluer.java
+++ b/quickstep/src/com/android/quickstep/RemoteTargetGluer.java
@@ -117,8 +117,8 @@
             secondaryTaskTarget = targets.findTask(splitIds[1]);
 
             mStagedSplitBounds = new StagedSplitBounds(
-                    primaryTaskTarget.screenSpaceBounds,
-                    secondaryTaskTarget.screenSpaceBounds, splitIds[0], splitIds[1]);
+                    primaryTaskTarget.startScreenSpaceBounds,
+                    secondaryTaskTarget.startScreenSpaceBounds, splitIds[0], splitIds[1]);
             mRemoteTargetHandles[0].mTransformParams.setTargetSet(
                     createRemoteAnimationTargetsForTarget(primaryTaskTarget, targets));
             mRemoteTargetHandles[0].mTaskViewSimulator.setPreview(primaryTaskTarget,
diff --git a/quickstep/src/com/android/quickstep/util/RecentsOrientedState.java b/quickstep/src/com/android/quickstep/util/RecentsOrientedState.java
index 8ccab71..ae8e45a 100644
--- a/quickstep/src/com/android/quickstep/util/RecentsOrientedState.java
+++ b/quickstep/src/com/android/quickstep/util/RecentsOrientedState.java
@@ -52,6 +52,7 @@
 import com.android.launcher3.util.SettingsCache;
 import com.android.quickstep.BaseActivityInterface;
 import com.android.quickstep.SystemUiProxy;
+import com.android.quickstep.TaskAnimationManager;
 import com.android.quickstep.views.TaskView;
 
 import java.lang.annotation.Retention;
@@ -340,6 +341,11 @@
 
     @SurfaceRotation
     public int getDisplayRotation() {
+        if (TaskAnimationManager.ENABLE_SHELL_TRANSITIONS) {
+            // When shell transitions are enabled, both the display and activity rotations should
+            // be the same once the gesture starts
+            return mRecentsActivityRotation;
+        }
         return mDisplayRotation;
     }
 
diff --git a/quickstep/src/com/android/quickstep/util/TaskViewSimulator.java b/quickstep/src/com/android/quickstep/util/TaskViewSimulator.java
index f676091..f66a6de 100644
--- a/quickstep/src/com/android/quickstep/util/TaskViewSimulator.java
+++ b/quickstep/src/com/android/quickstep/util/TaskViewSimulator.java
@@ -44,6 +44,7 @@
 import com.android.launcher3.util.TraceHelper;
 import com.android.quickstep.AnimatedFloat;
 import com.android.quickstep.BaseActivityInterface;
+import com.android.quickstep.TaskAnimationManager;
 import com.android.quickstep.views.TaskThumbnailView.PreviewPositionHelper;
 import com.android.quickstep.views.TaskView.FullscreenDrawParams;
 import com.android.systemui.shared.recents.model.ThumbnailData;
@@ -170,7 +171,7 @@
      * Sets the targets which the simulator will control
      */
     public void setPreview(RemoteAnimationTargetCompat runningTarget) {
-        setPreviewBounds(runningTarget.screenSpaceBounds, runningTarget.contentInsets);
+        setPreviewBounds(runningTarget.startScreenSpaceBounds, runningTarget.contentInsets);
     }
 
     /**
@@ -304,7 +305,13 @@
             mOrientationStateId = mOrientationState.getStateId();
 
             getFullScreenScale();
-            mThumbnailData.rotation = mOrientationState.getDisplayRotation();
+            if (TaskAnimationManager.ENABLE_SHELL_TRANSITIONS) {
+                // With shell transitions, the display is rotated early so we need to actually use
+                // the rotation when the gesture starts
+                mThumbnailData.rotation = mOrientationState.getTouchRotation();
+            } else {
+                mThumbnailData.rotation = mOrientationState.getDisplayRotation();
+            }
 
             // mIsRecentsRtl is the inverse of TaskView RTL.
             boolean isRtlEnabled = !mIsRecentsRtl;