From 0f5cb10a2a41c66b3955b3f2370044fec2a3378c Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 2 Apr 2026 19:12:48 +0000 Subject: [PATCH] fix: clamp PiP aspect ratio to Android's allowed range Videos with extreme aspect ratios (e.g. very tall or very wide) cause an IllegalArgumentException when entering PiP mode because Android requires the ratio to be between ~0.4184 and 2.39. Clamp the ratio before passing it to setAspectRatio. https://claude.ai/code/session_018uqcACcXpLQxwzVFhePisJ --- .../service/playback/pip/PictureInPicture.kt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/playback/pip/PictureInPicture.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/playback/pip/PictureInPicture.kt index ba836e13c..334132d8f 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/playback/pip/PictureInPicture.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/playback/pip/PictureInPicture.kt @@ -26,6 +26,14 @@ import android.graphics.Rect import android.os.Build import android.util.Rational +private const val MIN_PIP_RATIO = 0.42f +private const val MAX_PIP_RATIO = 2.39f + +private fun Float.toPipRational(): Rational { + val clamped = coerceIn(MIN_PIP_RATIO, MAX_PIP_RATIO) + return Rational((clamped * 1000).toInt(), 1000) +} + fun makePipParams( ratio: Float?, bounds: Rect?, @@ -37,7 +45,7 @@ fun makePipParams( setAutoEnterEnabled(true) } bounds?.let { setSourceRectHint(bounds) } - ratio?.let { setAspectRatio(Rational((it * 1000).toInt(), 1000)) } + ratio?.let { setAspectRatio(it.toPipRational()) } }.build() fun Activity.makePipParams( @@ -60,5 +68,5 @@ fun Activity.makePipParams( ), ) bounds?.let { setSourceRectHint(bounds) } - ratio?.let { setAspectRatio(Rational((it * 1000).toInt(), 1000)) } + ratio?.let { setAspectRatio(it.toPipRational()) } }.build()