summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Hongwei Wang <hwwang@google.com> 2024-07-10 13:22:26 -0700
committer Hongwei Wang <hwwang@google.com> 2024-07-18 10:40:37 -0700
commit8232c305f66364a26eba5d97bda2fb1c635f460a (patch)
treef537cbbd72afde7b6d23a57ef88c44b5d9f202ce
parent2cb43006a192bb7d13ff387e253f3a04b092f970 (diff)
Add PictureInPictureParams#isSameAspectRatio [1/N]
This is to replace the float number check when comparing the given Rect over aspect ratio. In this new static method, we allow for rounding errors on width / height. CTS tests have been updated to use and test this method. What's next: - Move the aspect ratio check from WMShell to Activity Flag: EXEMPT refactor Bug: 352118806 Test: atest CtsWindowManagerDeviceOther:PictureInPictureParamsTest Change-Id: I205abb2b6cf4414be9ad8700ed1adf7fcea50ad5
-rw-r--r--core/api/test-current.txt1
-rw-r--r--core/java/android/app/PictureInPictureParams.java27
2 files changed, 28 insertions, 0 deletions
diff --git a/core/api/test-current.txt b/core/api/test-current.txt
index 44c4ab4e1b57..1e51fea849c9 100644
--- a/core/api/test-current.txt
+++ b/core/api/test-current.txt
@@ -423,6 +423,7 @@ package android.app {
public final class PictureInPictureParams implements android.os.Parcelable {
method public float getAspectRatioFloat();
method public float getExpandedAspectRatioFloat();
+ method public static boolean isSameAspectRatio(@NonNull android.graphics.Rect, @NonNull android.util.Rational);
}
public final class PictureInPictureUiState implements android.os.Parcelable {
diff --git a/core/java/android/app/PictureInPictureParams.java b/core/java/android/app/PictureInPictureParams.java
index 96d874ee110b..afe915eece26 100644
--- a/core/java/android/app/PictureInPictureParams.java
+++ b/core/java/android/app/PictureInPictureParams.java
@@ -654,6 +654,33 @@ public final class PictureInPictureParams implements Parcelable {
&& !hasSetSubtitle() && mIsLaunchIntoPip == null;
}
+ /**
+ * Compare a given {@link Rect} against the aspect ratio, with rounding error tolerance.
+ * @param bounds The {@link Rect} represents the source rect hint, this check is not needed
+ * if app provides a null source rect hint.
+ * @param aspectRatio {@link Rational} representation of aspect ratio, this check is not needed
+ * if app provides a null aspect ratio.
+ * @return {@code true} if the given {@link Rect} matches the aspect ratio.
+ * @hide
+ */
+ @SuppressWarnings("UnflaggedApi")
+ @TestApi
+ public static boolean isSameAspectRatio(@NonNull Rect bounds, @NonNull Rational aspectRatio) {
+ // Validations
+ if (bounds.isEmpty() || aspectRatio.floatValue() <= 0) {
+ return false;
+ }
+ // Check against both the width and height.
+ final int exactWidth = (aspectRatio.getNumerator() * bounds.height())
+ / aspectRatio.getDenominator();
+ if (Math.abs(exactWidth - bounds.width()) <= 1) {
+ return true;
+ }
+ final int exactHeight = (aspectRatio.getDenominator() * bounds.width())
+ / aspectRatio.getNumerator();
+ return Math.abs(exactHeight - bounds.height()) <= 1;
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) return true;