From b5fd71549f6e0306d25ad55ec2128b2ca9c38a73 Mon Sep 17 00:00:00 2001 From: Ikram Gabiyev Date: Fri, 2 Jun 2023 13:44:24 -0700 Subject: [PATCH] Make pip size spec calculations more precise Improve the procision of the new pip size spec calculations by making sure we are as close to the requested aspect ratio as possible. Also make sure to use rounding instead of casting to integer. Update the PipSizeSpecHandlerTest to reflect the changes as well. Bug: 283500710 Test: atest WMShellUnitTests:PipSizeSpecHandlerTest Test: atest PinnedStackTests Change-Id: Iec0f9df11720584de46c839f5b30559bd8d22222 --- .../shell/pip/phone/PipSizeSpecHandler.java | 23 +++++++++---------- .../pip/phone/PipSizeSpecHandlerTest.java | 12 +++++----- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipSizeSpecHandler.java b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipSizeSpecHandler.java index 82fe38ccc7b3c..7971c049ff3bf 100644 --- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipSizeSpecHandler.java +++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipSizeSpecHandler.java @@ -135,14 +135,14 @@ public class PipSizeSpecHandler { maxWidth = (int) (mOptimizedAspectRatio * shorterLength + shorterLength * (aspectRatio - mOptimizedAspectRatio) / (1 + aspectRatio)); - maxHeight = (int) (maxWidth / aspectRatio); + maxHeight = Math.round(maxWidth / aspectRatio); } else { if (aspectRatio > 1f) { maxWidth = shorterLength; - maxHeight = (int) (maxWidth / aspectRatio); + maxHeight = Math.round(maxWidth / aspectRatio); } else { maxHeight = shorterLength; - maxWidth = (int) (maxHeight * aspectRatio); + maxWidth = Math.round(maxHeight * aspectRatio); } } @@ -165,10 +165,9 @@ public class PipSizeSpecHandler { Size maxSize = this.getMaxSize(aspectRatio); - int defaultWidth = Math.max((int) (maxSize.getWidth() * mDefaultSizePercent), + int defaultWidth = Math.max(Math.round(maxSize.getWidth() * mDefaultSizePercent), minSize.getWidth()); - int defaultHeight = Math.max((int) (maxSize.getHeight() * mDefaultSizePercent), - minSize.getHeight()); + int defaultHeight = Math.round(defaultWidth / aspectRatio); return new Size(defaultWidth, defaultHeight); } @@ -188,16 +187,16 @@ public class PipSizeSpecHandler { Size maxSize = this.getMaxSize(aspectRatio); - int minWidth = (int) (maxSize.getWidth() * mMinimumSizePercent); - int minHeight = (int) (maxSize.getHeight() * mMinimumSizePercent); + int minWidth = Math.round(maxSize.getWidth() * mMinimumSizePercent); + int minHeight = Math.round(maxSize.getHeight() * mMinimumSizePercent); // make sure the calculated min size is not smaller than the allowed default min size if (aspectRatio > 1f) { - minHeight = (int) Math.max(minHeight, mDefaultMinSize); - minWidth = (int) (minHeight * aspectRatio); + minHeight = Math.max(minHeight, mDefaultMinSize); + minWidth = Math.round(minHeight * aspectRatio); } else { - minWidth = (int) Math.max(minWidth, mDefaultMinSize); - minHeight = (int) (minWidth / aspectRatio); + minWidth = Math.max(minWidth, mDefaultMinSize); + minHeight = Math.round(minWidth / aspectRatio); } return new Size(minWidth, minHeight); } diff --git a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/pip/phone/PipSizeSpecHandlerTest.java b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/pip/phone/PipSizeSpecHandlerTest.java index 425bbf056b859..1379aedc22840 100644 --- a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/pip/phone/PipSizeSpecHandlerTest.java +++ b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/pip/phone/PipSizeSpecHandlerTest.java @@ -106,21 +106,21 @@ public class PipSizeSpecHandlerTest extends ShellTestCase { sExpectedDefaultSizes = new HashMap<>(); sExpectedMinSizes = new HashMap<>(); - sExpectedMaxSizes.put(16f / 9, new Size(1000, 562)); - sExpectedDefaultSizes.put(16f / 9, new Size(600, 337)); - sExpectedMinSizes.put(16f / 9, new Size(499, 281)); + sExpectedMaxSizes.put(16f / 9, new Size(1000, 563)); + sExpectedDefaultSizes.put(16f / 9, new Size(600, 338)); + sExpectedMinSizes.put(16f / 9, new Size(501, 282)); sExpectedMaxSizes.put(4f / 3, new Size(892, 669)); sExpectedDefaultSizes.put(4f / 3, new Size(535, 401)); - sExpectedMinSizes.put(4f / 3, new Size(445, 334)); + sExpectedMinSizes.put(4f / 3, new Size(447, 335)); sExpectedMaxSizes.put(3f / 4, new Size(669, 892)); sExpectedDefaultSizes.put(3f / 4, new Size(401, 535)); - sExpectedMinSizes.put(3f / 4, new Size(334, 445)); + sExpectedMinSizes.put(3f / 4, new Size(335, 447)); sExpectedMaxSizes.put(9f / 16, new Size(562, 999)); sExpectedDefaultSizes.put(9f / 16, new Size(337, 599)); - sExpectedMinSizes.put(9f / 16, new Size(281, 499)); + sExpectedMinSizes.put(9f / 16, new Size(281, 500)); } private void forEveryTestCaseCheck(Map expectedSizes,