Fix strange pip size in horizontal expanded mode

Fix: 228188320
Test: atest TvPipKeepClearAlgorithmTest#test_PipInsets
Change-Id: I217663863acbf15e8bdd6f7a7942f6de56942b95
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipKeepClearAlgorithm.kt b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipKeepClearAlgorithm.kt
index 9ede443..5086bed 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipKeepClearAlgorithm.kt
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipKeepClearAlgorithm.kt
@@ -133,7 +133,8 @@
         val pipAnchorBoundsWithAllDecors =
                 getNormalPipAnchorBounds(pipSizeWithAllDecors, transformedMovementBounds)
 
-        val pipAnchorBoundsWithPermanentDecors = removeTemporaryDecors(pipAnchorBoundsWithAllDecors)
+        val pipAnchorBoundsWithPermanentDecors =
+                removeTemporaryDecorsTransformed(pipAnchorBoundsWithAllDecors)
         val result = calculatePipPositionTransformed(
             pipAnchorBoundsWithPermanentDecors,
             transformedRestrictedAreas,
@@ -465,12 +466,10 @@
     }
 
     fun setPipPermanentDecorInsets(insets: Insets) {
-        if (pipPermanentDecorInsets == insets) return
         pipPermanentDecorInsets = insets
     }
 
     fun setPipTemporaryDecorInsets(insets: Insets) {
-        if (pipTemporaryDecorInsets == insets) return
         pipTemporaryDecorInsets = insets
     }
 
@@ -775,6 +774,7 @@
 
     /**
      * Removes the space that was reserved for permanent decorations around the pip
+     * @param bounds the bounds (in screen space) to remove the insets from
      */
     private fun removePermanentDecors(bounds: Rect): Rect {
         val pipDecorReverseInsets = Insets.subtract(Insets.NONE, pipPermanentDecorInsets)
@@ -784,11 +784,15 @@
 
     /**
      * Removes the space that was reserved for temporary decorations around the pip
+     * @param bounds the bounds (in base case) to remove the insets from
      */
-    private fun removeTemporaryDecors(bounds: Rect): Rect {
-        val pipDecorReverseInsets = Insets.subtract(Insets.NONE, pipTemporaryDecorInsets)
-        bounds.inset(pipDecorReverseInsets)
-        return bounds
+    private fun removeTemporaryDecorsTransformed(bounds: Rect): Rect {
+        if (pipTemporaryDecorInsets == Insets.NONE) return bounds
+
+        var reverseInsets = Insets.subtract(Insets.NONE, pipTemporaryDecorInsets)
+        var boundsInScreenSpace = fromTransformedSpace(bounds)
+        boundsInScreenSpace.inset(reverseInsets)
+        return toTransformedSpace(boundsInScreenSpace)
     }
 
     private fun Rect.offsetCopy(dx: Int, dy: Int) = Rect(this).apply { offset(dx, dy) }
diff --git a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/pip/tv/TvPipKeepClearAlgorithmTest.kt b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/pip/tv/TvPipKeepClearAlgorithmTest.kt
index e6ba70e..9919214 100644
--- a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/pip/tv/TvPipKeepClearAlgorithmTest.kt
+++ b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/pip/tv/TvPipKeepClearAlgorithmTest.kt
@@ -16,6 +16,7 @@
 
 package com.android.wm.shell.pip.tv
 
+import android.graphics.Insets
 import android.graphics.Rect
 import android.testing.AndroidTestingRunner
 import android.util.Size
@@ -432,6 +433,64 @@
         assertEquals(currentTime + algorithm.stashDuration, placement.unstashTime)
     }
 
+    @Test
+    fun test_PipInsets() {
+        val permInsets = Insets.of(-1, -2, -3, -4)
+        algorithm.setPipPermanentDecorInsets(permInsets)
+        testInsetsForAllPositions(permInsets)
+
+        val tempInsets = Insets.of(-4, -3, -2, -1)
+        algorithm.setPipPermanentDecorInsets(Insets.NONE)
+        algorithm.setPipTemporaryDecorInsets(tempInsets)
+        testInsetsForAllPositions(tempInsets)
+
+        algorithm.setPipPermanentDecorInsets(permInsets)
+        algorithm.setPipTemporaryDecorInsets(tempInsets)
+        testInsetsForAllPositions(Insets.add(permInsets, tempInsets))
+    }
+
+    private fun testInsetsForAllPositions(insets: Insets) {
+        gravity = Gravity.BOTTOM or Gravity.RIGHT
+        testAnchorPositionWithInsets(insets)
+
+        gravity = Gravity.BOTTOM or Gravity.LEFT
+        testAnchorPositionWithInsets(insets)
+
+        gravity = Gravity.TOP or Gravity.LEFT
+        testAnchorPositionWithInsets(insets)
+
+        gravity = Gravity.TOP or Gravity.RIGHT
+        testAnchorPositionWithInsets(insets)
+
+        pipSize = EXPANDED_WIDE_PIP_SIZE
+
+        gravity = Gravity.BOTTOM
+        testAnchorPositionWithInsets(insets)
+
+        gravity = Gravity.TOP
+        testAnchorPositionWithInsets(insets)
+
+        pipSize = Size(pipSize.height, pipSize.width)
+
+        gravity = Gravity.LEFT
+        testAnchorPositionWithInsets(insets)
+
+        gravity = Gravity.RIGHT
+        testAnchorPositionWithInsets(insets)
+    }
+
+    private fun testAnchorPositionWithInsets(insets: Insets) {
+        var pipRect = Rect(0, 0, pipSize.width, pipSize.height)
+        pipRect.inset(insets)
+        var expectedBounds = Rect()
+        Gravity.apply(gravity, pipRect.width(), pipRect.height(), movementBounds, expectedBounds)
+        val reverseInsets = Insets.subtract(Insets.NONE, insets)
+        expectedBounds.inset(reverseInsets)
+
+        var placement = getActualPlacement()
+        assertEquals(expectedBounds, placement.bounds)
+    }
+
     private fun makeSideBar(width: Int, @Gravity.GravityFlags side: Int): Rect {
         val sidebar = Rect(0, 0, width, SCREEN_SIZE.height)
         if (side == Gravity.RIGHT) {