fix(automotive): stop playback while a vehicle restricts the app

Plezy declares appCategory="video", so on Android Automotive OS it is a
parked app bound by car app quality DD-2/DD-3: audio must stop when the
vehicle starts driving and must not be resumable while driving. Two paths
kept audio alive. Music playback ran under a mediaPlayback foreground
service whose lifecycle observer was registered for Apple TV only, so it
never paused when Android backgrounded the app. Video pausing hung off
AppLifecycleState.hidden, which Flutter only synthesizes once Android
delivers onStop; a car without the Automotive compatibility mode delivers
onPause alone, which maps to AppLifecycleState.inactive and the player
ignored.

Gate every path that can start audio on a new lifecycle predicate,
automotivePlaybackAllowed, which permits playback on a car only while the
app is resumed and fails closed on an unknown lifecycle state. That covers
explicit play, gapless arming and track transitions, live retry and
channel switch, frame-rate-match resume, VOD/live startup, and the queue
navigation commands of the OS media session, plus a last-resort pause for
when the platform player resumes itself on native audio-focus regain.
Playback authority on the media-session router is deliberately left alone:
the router consumes a denied event, so gating it would swallow PauseEvent
and leave the OS unable to stop audio. Reacting to lifecycle callbacks is
the mechanism the platform documents as sufficient, so no android.car
dependency is added.

The music queue no longer requests POST_NOTIFICATIONS on a car, where the
foreground service and its notification never start: there is nothing to
authorize, and the prompt would take focus and make the gate discard the
first play intent.

Detect the form factor too: FEATURE_AUTOMOTIVE now vetoes the Android TV
verdict, so a rotary-only head unit no longer inherits the leanback
experience. Picture-in-picture is gated on FEATURE_PICTURE_IN_PICTURE,
which cars lack, so the app's UI cannot stay on screen while driving, and
nothing forces a preferred orientation on a fixed-orientation display.
This commit is contained in:
edde746
2026-07-28 23:28:25 +02:00
parent 8aa836d106
commit 41ffaa7f2b
19 changed files with 768 additions and 50 deletions
@@ -96,6 +96,8 @@ class MainActivity : FlutterActivity() {
private fun isAndroidTvDevice(): Boolean = getAndroidTvDetection()["isTv"] as Boolean
private fun isPipSupportedDevice(): Boolean = !isAndroidTvDevice() && packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)
private fun isImeVisible(): Boolean {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) return false
return window.decorView.rootWindowInsets?.isVisible(WindowInsets.Type.ime()) == true
@@ -168,6 +170,7 @@ class MainActivity : FlutterActivity() {
val hasFireTvFeature = pm.hasSystemFeature("amazon.hardware.fire_tv")
val hasTouchscreen = pm.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN)
val hasFakeTouch = pm.hasSystemFeature(PackageManager.FEATURE_FAKETOUCH)
val isAutomotive = pm.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)
val reasons = mutableListOf<String>()
if (isTelevisionUiMode) reasons.add("ui_mode_television")
@@ -177,7 +180,11 @@ class MainActivity : FlutterActivity() {
if (!hasTouchscreen) reasons.add("no_touchscreen")
return mapOf(
"isTv" to reasons.isNotEmpty(),
// A car is never a TV: rotary-only head units report no touchscreen, and
// an OEM image can carry a stray leanback flag. Keep the raw reasons for
// diagnostics, but never let them promote a vehicle to the TV experience.
"isTv" to (!isAutomotive && reasons.isNotEmpty()),
"isAutomotive" to isAutomotive,
"reasons" to reasons,
"isTelevisionUiMode" to isTelevisionUiMode,
"hasTelevisionFeature" to hasTelevisionFeature,
@@ -662,7 +669,7 @@ class MainActivity : FlutterActivity() {
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, PIP_CHANNEL).setMethodCallHandler { call, result ->
when (call.method) {
"isSupported" -> {
result.success(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !isAndroidTvDevice())
result.success(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && isPipSupportedDevice())
}
"enter" -> {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
@@ -670,7 +677,7 @@ class MainActivity : FlutterActivity() {
return@setMethodCallHandler
}
if (isAndroidTvDevice()) {
if (!isPipSupportedDevice()) {
result.success(mapOf("success" to false, "errorCode" to "not_supported"))
return@setMethodCallHandler
}
@@ -697,7 +704,7 @@ class MainActivity : FlutterActivity() {
}
}
"setAutoPipReady" -> {
if (isAndroidTvDevice()) {
if (!isPipSupportedDevice()) {
autoPipReady = false
result.success(true)
return@setMethodCallHandler
@@ -821,7 +828,7 @@ class MainActivity : FlutterActivity() {
override fun onUserLeaveHint() {
super.onUserLeaveHint()
// Auto PiP for API 26-30 (API 31+ uses setAutoEnterEnabled)
if (!isAndroidTvDevice() &&
if (isPipSupportedDevice() &&
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
Build.VERSION.SDK_INT < Build.VERSION_CODES.S &&
autoPipReady &&
@@ -5,15 +5,19 @@ import android.content.pm.PackageManager
import android.content.res.Configuration
/**
* Native mirror of MainActivity.getAndroidTvDetection(): any TV signal counts.
* Kept in sync with the Dart-facing detection so native gating matches
* PlatformDetector.isTV().
* Native mirror of MainActivity.getAndroidTvDetection(): any TV signal counts,
* except that FEATURE_AUTOMOTIVE vetoes the verdict outright. Kept in sync with
* the Dart-facing detection so native gating matches PlatformDetector.isTV().
*/
object TvDetection {
fun isTv(context: Context): Boolean {
val pm = context.packageManager
val uiModeType = context.resources.configuration.uiMode and Configuration.UI_MODE_TYPE_MASK
// A car is never a TV: rotary-only head units report no touchscreen, and an
// OEM image can carry a stray leanback flag.
if (pm.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) return false
@Suppress("DEPRECATION")
return uiModeType == Configuration.UI_MODE_TYPE_TELEVISION ||
pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION) ||