Skip to content

StreamVault is a TV-first free IPTV player for Android TV built with Kotlin, Jetpack Compose, Room, Hilt, and Media3.

Fix PiP parameter safety

David Nashashb45aca028b7c
diff --git a/app/src/main/java/com/streamvault/app/MainActivity.kt b/app/src/main/java/com/streamvault/app/MainActivity.kt
index 22a975f6..1ffce74b 100644
--- a/app/src/main/java/com/streamvault/app/MainActivity.kt
+++ b/app/src/main/java/com/streamvault/app/MainActivity.kt
@@ -3,6 +3,7 @@ package com.streamvault.app
import android.app.SearchManager
import android.content.Intent
import android.app.PictureInPictureParams
+import android.content.pm.PackageManager
import android.os.Bundle
import android.os.Build
import android.os.StrictMode
@@ -54,6 +55,8 @@ class MainActivity : ComponentActivity() {
companion object {
const val EXTRA_PLAYER_REQUEST = "com.streamvault.app.extra.PLAYER_REQUEST"
const val EXTRA_EXTERNAL_ROUTE = "com.streamvault.app.extra.EXTERNAL_ROUTE"
+ private const val MAX_PIP_ASPECT_RATIO = 2.39f
+ private const val MIN_PIP_ASPECT_RATIO = 1f / MAX_PIP_ASPECT_RATIO
}
private data class PlayerPictureInPictureState(
@@ -184,6 +187,7 @@ class MainActivity : ComponentActivity() {
videoHeight: Int,
pixelWidthHeightRatio: Float = 1f
) {
+ if (!supportsPictureInPicture()) return
playerPictureInPictureState = PlayerPictureInPictureState(
enabled = enabled,
isPlaying = isPlaying,
@@ -193,6 +197,7 @@ class MainActivity : ComponentActivity() {
}
fun clearPlayerPictureInPictureState() {
+ if (!supportsPictureInPicture()) return
playerPictureInPictureState = PlayerPictureInPictureState()
applyPlayerPictureInPictureParams()
}
@@ -214,7 +219,7 @@ class MainActivity : ComponentActivity() {
}
private fun enterPlayerPictureInPictureModeIfEligible(requirePlaying: Boolean = true): Boolean {
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || isInPictureInPictureMode) {
+ if (!supportsPictureInPicture() || isInPictureInPictureMode) {
return false
}
val state = playerPictureInPictureState
@@ -222,13 +227,17 @@ class MainActivity : ComponentActivity() {
return false
}
val params = buildPlayerPictureInPictureParams(state)
- setPictureInPictureParams(params)
- return enterPictureInPictureMode(params)
+ return runCatching {
+ setPictureInPictureParams(params)
+ enterPictureInPictureMode(params)
+ }.getOrDefault(false)
}
private fun applyPlayerPictureInPictureParams() {
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
- setPictureInPictureParams(buildPlayerPictureInPictureParams(playerPictureInPictureState))
+ if (!supportsPictureInPicture()) return
+ runCatching {
+ setPictureInPictureParams(buildPlayerPictureInPictureParams(playerPictureInPictureState))
+ }
}
private fun buildPlayerPictureInPictureParams(
@@ -242,10 +251,23 @@ class MainActivity : ComponentActivity() {
return builder.build()
}
- private fun videoAspectRatioOrNull(videoWidth: Int, videoHeight: Int, pixelWidthHeightRatio: Float = 1f): Rational? {
+ private fun videoAspectRatioOrNull(
+ videoWidth: Int,
+ videoHeight: Int,
+ pixelWidthHeightRatio: Float = 1f
+ ): Rational? {
if (videoWidth <= 0 || videoHeight <= 0) return null
- val displayWidth = (videoWidth * pixelWidthHeightRatio).toInt().coerceAtLeast(1)
- return Rational(displayWidth, videoHeight)
+ val safePixelRatio = pixelWidthHeightRatio.takeIf { it.isFinite() && it > 0f } ?: 1f
+ val rawAspectRatio = (videoWidth * safePixelRatio) / videoHeight.toFloat()
+ val clampedAspectRatio = rawAspectRatio
+ .coerceIn(MIN_PIP_ASPECT_RATIO, MAX_PIP_ASPECT_RATIO)
+ val numerator = (clampedAspectRatio * 10_000).toInt().coerceAtLeast(1)
+ return Rational(numerator, 10_000)
+ }
+
+ private fun supportsPictureInPicture(): Boolean {
+ return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
+ packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)
}
private fun handleExternalIntent(intent: Intent?) {
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index d79383f0..f77a6cb1 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -1,19 +1,12 @@
# Changelog
All notable product changes are recorded in this document.
-## [1.0.7] - 2026-04-19
+## [1.0.7] - 2026-04-20
### Fixed
-- Fixed crash on launch affecting some Android 9 (API 28) devices. On certain OEM builds
- (MediaTek-based chipsets, some Samsung and Huawei devices), the Android Keystore HAL
- throws a `GeneralSecurityException` or `KeyStoreException` when `EncryptedSharedPreferences`
- is first initialised. This preference store holds the parental PIN lockout state and is
- accessed during `ParentalControlManager` initialisation, which runs synchronously on the
- first ViewModelScope creation at app startup — causing an immediate cold-start crash. The
- fix wraps the encrypted store creation in a `runCatching` block and falls back to a
- standard (unencrypted) `SharedPreferences` file if the Keystore is unavailable. The PIN
- hash and salt themselves are stored in DataStore and are unaffected.
+- Fixed startup crashes on some Android 9 devices when secure preference storage fails to initialize.
+- Fixed Picture-in-Picture setup on devices that report invalid or unsupported video aspect ratios.
---