summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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 e0c3230f8c27..88b5275d37f8 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;