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
@@ -468,8 +468,11 @@ extension _VideoPlayerPlaybackServiceMethods on VideoPlayerScreenState {
_mediaControlsManager = mediaControlsManager;
final mediaControlRouter = MediaControlRouter(
// Authority stays Watch Together's. The automotive gate lives in the
// playback-intent wrappers below, so `onPause` can never be denied: a
// gated `canControlPlayback` would make the router swallow `PauseEvent`.
canControlPlayback: _canControlPlayback,
canNavigateMediaItems: _canNavigateMediaItems,
canNavigateMediaItems: () => _canNavigateMediaItems() && automotivePlaybackAllowedNow(),
onPlay: () {
final currentPlayer = player;
if (currentPlayer == null) return;
@@ -592,6 +595,24 @@ extension _VideoPlayerPlaybackServiceMethods on VideoPlayerScreenState {
_lastPlaybackPauseAt = DateTime.now();
}
if (isPlaying && !automotivePlaybackAllowedNow()) {
// Native audio-focus regain resumes the platform player directly
// (ExoPlayer's AudioFocusManager, mpv's resumeAfterAudioFocusGain), so it
// never passes through the Dart playback-intent wrappers. Last line of
// defence for `DD-2`: audio must not resume while the vehicle restricts
// the app. Also catches any async open that raced the lifecycle pause.
appLogger.w('Playback started while Android Automotive UX restrictions are active; pausing');
Sentry.addBreadcrumb(
Breadcrumb(message: 'Blocked automotive restricted playback start', category: 'player.driver_distraction'),
);
final currentPlayer = player;
if (currentPlayer != null) {
unawaited(_pauseWithPlaybackIntent(currentPlayer));
}
unawaited(_wakelockController.setEnabled(false));
return;
}
if (isPlaying && _mediaControlsSuspendedForTvBackground) {
appLogger.w('Playback started while Android TV background media controls are suspended; pausing');
Sentry.addBreadcrumb(