Adding API for apps to specify their aspect ratio when entering PIP.

Test: android.server.cts.ActivityManagerPinnedStackTests
Test: #testEnterPipAspectRatio
Test: #testEnterPipExtremeAspectRatios

Change-Id: I9efba942b9a6451dec07428fe1e428ef4a896867
diff --git a/services/core/java/com/android/server/wm/PinnedStackController.java b/services/core/java/com/android/server/wm/PinnedStackController.java
index 1ccf722..52c18f1 100644
--- a/services/core/java/com/android/server/wm/PinnedStackController.java
+++ b/services/core/java/com/android/server/wm/PinnedStackController.java
@@ -26,6 +26,7 @@
 import android.animation.ValueAnimator;
 import android.content.res.Resources;
 import android.graphics.Point;
+import android.graphics.PointF;
 import android.graphics.Rect;
 import android.os.Handler;
 import android.os.IBinder;
@@ -167,6 +168,25 @@
     }
 
     /**
+     * Returns the current bounds (or the default bounds if there are no current bounds) with the
+     * specified aspect ratio.
+     */
+    Rect getAspectRatioBounds(Rect stackBounds, float aspectRatio) {
+        // Save the snap fraction, calculate the aspect ratio based on the current bounds
+        final float snapFraction = mSnapAlgorithm.getSnapFraction(stackBounds,
+                getMovementBounds(stackBounds));
+        final float radius = PointF.length(stackBounds.width(), stackBounds.height());
+        final int height = (int) Math.round(Math.sqrt((radius * radius) /
+                (aspectRatio * aspectRatio + 1)));
+        final int width = Math.round(height * aspectRatio);
+        final int left = (int) (stackBounds.centerX() - width / 2f);
+        final int top = (int) (stackBounds.centerY() - height / 2f);
+        stackBounds.set(left, top, left + width, top + height);
+        mSnapAlgorithm.applySnapFraction(stackBounds, getMovementBounds(stackBounds), snapFraction);
+        return stackBounds;
+    }
+
+    /**
      * @return the default bounds to show the PIP when there is no active PIP.
      */
     Rect getDefaultBounds() {