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.
41 lines
2.0 KiB
Dart
41 lines
2.0 KiB
Dart
import 'package:flutter/widgets.dart';
|
|
|
|
import '../utils/platform_detector.dart';
|
|
|
|
/// Android Automotive OS driver-distraction gating for Plezy's `video` app
|
|
/// category (car app quality `DD-2` / `DD-3`).
|
|
///
|
|
/// While a vehicle's user-experience restrictions are active the system hides
|
|
/// the app's activity. That delivers `onPause` — Flutter
|
|
/// [AppLifecycleState.inactive] — at minimum; only devices carrying the
|
|
/// Automotive compatibility mode go on to deliver `onStop`
|
|
/// ([AppLifecycleState.hidden] then [AppLifecycleState.paused]). Reacting to
|
|
/// lifecycle callbacks is the mechanism the platform documents as sufficient,
|
|
/// so playback authority is derived from lifecycle state alone and no
|
|
/// `android.car` dependency is required.
|
|
///
|
|
/// Two obligations follow from `DD-2`, and this single predicate serves both:
|
|
/// audio must stop when driving starts, and it must not be resumable while
|
|
/// driving. The second obligation covers every path that can start audio, not
|
|
/// just OS media-session commands — a gapless track transition or queue
|
|
/// auto-advance landing just after the lifecycle pause must fail closed too.
|
|
///
|
|
/// The gate itself fails closed: an unknown (null) lifecycle state denies
|
|
/// playback so a command arriving before the first lifecycle message cannot
|
|
/// slip through; nothing is playing that early, so the strictness costs nothing.
|
|
bool automotivePlaybackAllowed({required bool isAutomotive, required AppLifecycleState? state}) {
|
|
if (!isAutomotive) return true;
|
|
return state == AppLifecycleState.resumed;
|
|
}
|
|
|
|
/// [automotivePlaybackAllowed] against the ambient form factor and lifecycle,
|
|
/// for owners that hold no injected lifecycle state of their own.
|
|
///
|
|
/// Short-circuits before reading [WidgetsBinding.instance] so this stays usable
|
|
/// from plain `test()` suites, where the binding is not initialized and the
|
|
/// `instance` getter throws.
|
|
bool automotivePlaybackAllowedNow() {
|
|
if (!PlatformDetector.isAutomotive()) return true;
|
|
return automotivePlaybackAllowed(isAutomotive: true, state: WidgetsBinding.instance.lifecycleState);
|
|
}
|