Match the live tile cut out to the correct task view position in landscape mode

Bug: 171838067
Test: test live tile in landscape mode

Change-Id: I12c375305e19917a53e30d9e40dc709f828ad4a6
diff --git a/quickstep/src/com/android/quickstep/AbsSwipeUpHandler.java b/quickstep/src/com/android/quickstep/AbsSwipeUpHandler.java
index 4dbc0f3..48d4679 100644
--- a/quickstep/src/com/android/quickstep/AbsSwipeUpHandler.java
+++ b/quickstep/src/com/android/quickstep/AbsSwipeUpHandler.java
@@ -1733,6 +1733,8 @@
             LiveTileOverlay.INSTANCE.update(
                     mTaskViewSimulator.getCurrentRect(),
                     mTaskViewSimulator.getCurrentCornerRadius());
+            LiveTileOverlay.INSTANCE.setRotation(
+                    mRecentsView.getPagedViewOrientedState().getDisplayRotation());
         }
         ProtoTracer.INSTANCE.get(mContext).scheduleFrameUpdate();
     }
diff --git a/quickstep/src/com/android/quickstep/util/RecentsOrientedState.java b/quickstep/src/com/android/quickstep/util/RecentsOrientedState.java
index e273aeb..facc99a 100644
--- a/quickstep/src/com/android/quickstep/util/RecentsOrientedState.java
+++ b/quickstep/src/com/android/quickstep/util/RecentsOrientedState.java
@@ -519,6 +519,29 @@
         }
     }
 
+    /**
+     * Contrary to {@link #postDisplayRotation}.
+     */
+    public static void preDisplayRotation(@SurfaceRotation int displayRotation,
+            float screenWidth, float screenHeight, Matrix out) {
+        switch (displayRotation) {
+            case ROTATION_0:
+                return;
+            case ROTATION_90:
+                out.postRotate(90);
+                out.postTranslate(screenWidth, 0);
+                break;
+            case ROTATION_180:
+                out.postRotate(180);
+                out.postTranslate(screenHeight, screenWidth);
+                break;
+            case ROTATION_270:
+                out.postRotate(270);
+                out.postTranslate(0, screenHeight);
+                break;
+        }
+    }
+
     @NonNull
     @Override
     public String toString() {
diff --git a/quickstep/src/com/android/quickstep/util/TaskViewSimulator.java b/quickstep/src/com/android/quickstep/util/TaskViewSimulator.java
index 2f4bb8e..5a7f541 100644
--- a/quickstep/src/com/android/quickstep/util/TaskViewSimulator.java
+++ b/quickstep/src/com/android/quickstep/util/TaskViewSimulator.java
@@ -19,6 +19,7 @@
 import static com.android.launcher3.states.RotationHelper.deltaRotation;
 import static com.android.launcher3.touch.PagedOrientationHandler.MATRIX_POST_TRANSLATE;
 import static com.android.quickstep.util.RecentsOrientedState.postDisplayRotation;
+import static com.android.quickstep.util.RecentsOrientedState.preDisplayRotation;
 import static com.android.systemui.shared.system.WindowManagerWrapper.WINDOWING_MODE_FULLSCREEN;
 
 import android.animation.TimeInterpolator;
@@ -80,6 +81,7 @@
     private DeviceProfile mDp;
 
     private final Matrix mMatrix = new Matrix();
+    private final Matrix mMatrixTmp = new Matrix();
     private final Point mRunningTargetWindowPosition = new Point();
 
     // Thumbnail view properties
@@ -211,7 +213,10 @@
      */
     public RectF getCurrentRect() {
         RectF result = getCurrentCropRect();
-        mMatrix.mapRect(result);
+        mMatrixTmp.set(mMatrix);
+        preDisplayRotation(mOrientationState.getDisplayRotation(), mDp.widthPx, mDp.heightPx,
+                mMatrixTmp);
+        mMatrixTmp.mapRect(result);
         return result;
     }
 
diff --git a/quickstep/src/com/android/quickstep/views/LiveTileOverlay.java b/quickstep/src/com/android/quickstep/views/LiveTileOverlay.java
index f6eb0e2..747c3f2 100644
--- a/quickstep/src/com/android/quickstep/views/LiveTileOverlay.java
+++ b/quickstep/src/com/android/quickstep/views/LiveTileOverlay.java
@@ -1,5 +1,10 @@
 package com.android.quickstep.views;
 
+import static android.view.Surface.ROTATION_0;
+import static android.view.Surface.ROTATION_180;
+import static android.view.Surface.ROTATION_270;
+import static android.view.Surface.ROTATION_90;
+
 import static com.android.launcher3.anim.Interpolators.FAST_OUT_SLOW_IN;
 import static com.android.launcher3.anim.Interpolators.LINEAR;
 
@@ -19,6 +24,7 @@
 import android.view.ViewOverlay;
 
 import com.android.launcher3.anim.Interpolators;
+import com.android.quickstep.util.RecentsOrientedState.SurfaceRotation;
 
 public class LiveTileOverlay extends Drawable {
 
@@ -43,6 +49,8 @@
     private final RectF mCurrentRect = new RectF();
     private final Rect mBoundsRect = new Rect();
 
+    private @SurfaceRotation int mRotation = ROTATION_0;
+
     private float mCornerRadius;
     private Drawable mIcon;
     private Animator mIconAnimator;
@@ -69,6 +77,10 @@
         mCurrentRect.set(left, top, right, bottom);
     }
 
+    public void setRotation(@SurfaceRotation int rotation) {
+        mRotation = rotation;
+    }
+
     public void setIcon(Drawable icon) {
         mIcon = icon;
     }
@@ -103,8 +115,35 @@
             canvas.save();
             float scale = Interpolators.clampToProgress(FAST_OUT_SLOW_IN, 0f,
                     1f).getInterpolation(mIconAnimationProgress);
-            canvas.translate(mCurrentRect.centerX() - mIcon.getBounds().width() / 2 * scale,
-                    mCurrentRect.top - mIcon.getBounds().height() / 2 * scale);
+
+            int iconRadius = mIcon.getBounds().width() / 2;
+            float dx = 0;
+            float dy = 0;
+
+            switch (mRotation) {
+                case ROTATION_0:
+                    dx = mCurrentRect.centerX() - iconRadius * scale;
+                    dy = mCurrentRect.top - iconRadius * scale;
+                    break;
+                case ROTATION_90:
+                    dx = mCurrentRect.right - iconRadius * scale;
+                    dy = mCurrentRect.centerY() - iconRadius * scale;
+                    break;
+                case ROTATION_270:
+                    dx = mCurrentRect.left - iconRadius * scale;
+                    dy = mCurrentRect.centerY() - iconRadius * scale;
+                    break;
+                case ROTATION_180:
+                    dx = mCurrentRect.centerX() - iconRadius * scale;
+                    dy = mCurrentRect.bottom - iconRadius * scale;
+                    break;
+            }
+
+            int rotationDegrees = mRotation * 90;
+            if (mRotation == ROTATION_90 || mRotation == ROTATION_270) {
+                canvas.rotate(rotationDegrees, dx + iconRadius, dy + iconRadius);
+            }
+            canvas.translate(dx, dy);
             canvas.scale(scale, scale);
             mIcon.draw(canvas);
             canvas.restore();