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
+2
View File
@@ -11,6 +11,7 @@ class OrientationHelper {
/// This should be called when leaving full-screen experiences like
/// the video player to restore the app's default orientation behavior.
static void restoreDefaultOrientations(BuildContext context) {
if (PlatformDetector.isAutomotive()) return;
final isPhone = PlatformDetector.isPhone(context);
if (isPhone) {
@@ -30,6 +31,7 @@ class OrientationHelper {
/// Used by the video player to force landscape orientation during playback.
static void setLandscapeOrientation() {
SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky);
if (PlatformDetector.isAutomotive()) return;
SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]);
}
+45 -4
View File
@@ -12,12 +12,20 @@ const _androidFeatureTelevision = 'android.hardware.type.television';
const _androidFeatureLeanback = 'android.software.leanback';
const _androidFeatureFireTv = 'amazon.hardware.fire_tv';
const _androidFeatureTouchscreen = 'android.hardware.touchscreen';
const _androidFeatureAutomotive = 'android.hardware.type.automotive';
class AndroidTvFeatureDetection {
final bool isTv;
/// True on Android Automotive OS head units. Never true together with
/// [isTv]: `FEATURE_AUTOMOTIVE` is authoritative for the car form factor.
final bool isAutomotive;
/// Diagnostic TV signals, surfaced in the log export only while TV mode is
/// active. Non-empty with [isTv] false when automotive vetoed the verdict.
final List<String> reasons;
const AndroidTvFeatureDetection({required this.isTv, required this.reasons});
const AndroidTvFeatureDetection({required this.isTv, required this.isAutomotive, required this.reasons});
}
AndroidTvFeatureDetection detectAndroidTvFromSystemFeatures(Iterable<String> features) {
@@ -28,7 +36,16 @@ AndroidTvFeatureDetection detectAndroidTvFromSystemFeatures(Iterable<String> fea
if (featureSet.contains(_androidFeatureFireTv)) reasons.add('fire_tv');
if (featureSet.isNotEmpty && !featureSet.contains(_androidFeatureTouchscreen)) reasons.add('no_touchscreen');
return AndroidTvFeatureDetection(isTv: reasons.isNotEmpty, reasons: reasons);
// A car is never a TV. Rotary-only head units report no touchscreen, and OEM
// images derived from other AOSP variants can carry a stray leanback flag;
// either would otherwise route a vehicle through the leanback experience.
final isAutomotive = featureSet.contains(_androidFeatureAutomotive);
return AndroidTvFeatureDetection(
isTv: !isAutomotive && reasons.isNotEmpty,
isAutomotive: isAutomotive,
reasons: reasons,
);
}
/// Service for detecting if the app is running on Android TV or Apple TV.
@@ -37,10 +54,12 @@ class TvDetectionService {
@visibleForTesting
static set debugDetectionGate(Future<void>? value) => _singleton.debugGate = value;
static bool? _debugAppleTVOverride;
static bool? _debugAutomotiveOverride;
bool _detected = false;
bool _forceTv = false;
bool _isTV = false;
bool _isAppleTV = false;
bool _isAutomotive = false;
bool _initialized = false;
List<String> _detectionReasons = const [];
@@ -62,6 +81,7 @@ class TvDetectionService {
final detection =
nativeDetection ?? detectAndroidTvFromSystemFeatures((await deviceInfo.androidInfo).systemFeatures);
_detected = detection.isTv;
_isAutomotive = detection.isAutomotive;
_detectionReasons = detection.reasons;
} else if (Platform.isIOS) {
if (_tvosBuild) {
@@ -91,6 +111,10 @@ class TvDetectionService {
bool get isTV => _isTV;
/// True on Android Automotive OS. Independent of the force-TV override so
/// driver-distraction gating cannot be switched off from settings.
bool get isAutomotive => _isAutomotive;
List<String> get _effectiveDetectionReasons {
final reasons = <String>[..._detectionReasons];
if (_forceTv && !reasons.contains('force_tv')) reasons.add('force_tv');
@@ -104,8 +128,9 @@ class TvDetectionService {
final reasonsValue = result['reasons'];
final reasons = reasonsValue is Iterable ? reasonsValue.whereType<String>().toList() : <String>[];
final isTv = result['isTv'] == true;
final isAutomotive = result['isAutomotive'] == true;
if (isTv && reasons.isEmpty) reasons.add('native');
return AndroidTvFeatureDetection(isTv: isTv, reasons: reasons);
return AndroidTvFeatureDetection(isTv: isTv && !isAutomotive, isAutomotive: isAutomotive, reasons: reasons);
} on MissingPluginException {
return null;
} on PlatformException {
@@ -139,15 +164,24 @@ class TvDetectionService {
/// Synchronous Apple TV check (returns false if not initialized or not tvOS).
static bool isAppleTVSync() => _debugAppleTVOverride ?? (_tvosBuild || _singleton.instance?._isAppleTV == true);
/// Synchronous Android Automotive OS check (false before initialization).
static bool isAutomotiveSync() => _debugAutomotiveOverride ?? _singleton.instance?._isAutomotive ?? false;
@visibleForTesting
static void debugSetAppleTVOverride(bool? value) {
_debugAppleTVOverride = value;
}
@visibleForTesting
static void debugSetAutomotiveOverride(bool? value) {
_debugAutomotiveOverride = value;
}
@visibleForTesting
static void debugReset() {
_singleton.debugReset();
_debugAppleTVOverride = null;
_debugAutomotiveOverride = null;
}
static List<String> tvDetectionReasonsSync() => _singleton.instance?._effectiveDetectionReasons ?? const [];
@@ -165,6 +199,11 @@ class PlatformDetector {
return TvDetectionService.isAppleTVSync();
}
/// True on Android Automotive OS head units.
static bool isAutomotive() {
return TvDetectionService.isAutomotiveSync();
}
/// Detects if the app should use side navigation (Desktop or TV)
static bool shouldUseSideNavigation(BuildContext context) {
return isDesktop(context) || isTV();
@@ -229,7 +268,9 @@ class PlatformDetector {
}
static bool supportsPictureInPicture() {
return !isAppleTV() && !isTV() && (Platform.isAndroid || Platform.isIOS || Platform.isMacOS);
// Cars commonly lack FEATURE_PICTURE_IN_PICTURE, and a floating player
// would keep the app's UI on screen while driving, which `DD-2` forbids.
return !isAppleTV() && !isTV() && !isAutomotive() && (Platform.isAndroid || Platform.isIOS || Platform.isMacOS);
}
/// Detects if the device is likely a tablet based on screen size