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:
@@ -0,0 +1,67 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:os_media_controls/os_media_controls.dart';
|
||||
import 'package:plezy/screens/video_player_screen.dart';
|
||||
import 'package:plezy/services/driver_distraction.dart';
|
||||
import 'package:plezy/services/media_control_router.dart';
|
||||
import 'package:plezy/utils/platform_detector.dart';
|
||||
|
||||
void main() {
|
||||
setUp(TvDetectionService.debugReset);
|
||||
tearDown(TvDetectionService.debugReset);
|
||||
|
||||
test('automotive background policy pauses independently of handheld and TV detection', () {
|
||||
expect(shouldPauseVideoForBackground(isHandheld: false, isTv: false, isAutomotive: true), isTrue);
|
||||
expect(shouldPauseVideoForBackground(isHandheld: true, isTv: false, isAutomotive: false), isTrue);
|
||||
expect(shouldPauseVideoForBackground(isHandheld: false, isTv: true, isAutomotive: false), isTrue);
|
||||
expect(shouldPauseVideoForBackground(isHandheld: false, isTv: false, isAutomotive: false), isFalse);
|
||||
});
|
||||
|
||||
test('automotive playback is allowed only while resumed', () {
|
||||
expect(automotivePlaybackAllowed(isAutomotive: true, state: AppLifecycleState.resumed), isTrue);
|
||||
expect(automotivePlaybackAllowed(isAutomotive: true, state: AppLifecycleState.inactive), isFalse);
|
||||
expect(automotivePlaybackAllowed(isAutomotive: true, state: null), isFalse);
|
||||
expect(automotivePlaybackAllowed(isAutomotive: false, state: AppLifecycleState.inactive), isTrue);
|
||||
});
|
||||
|
||||
testWidgets('automotive media controls block navigation but never swallow pause', (tester) async {
|
||||
addTearDown(() => tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed));
|
||||
TvDetectionService.debugSetAutomotiveOverride(true);
|
||||
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
|
||||
|
||||
final calls = <String>[];
|
||||
// Mirrors the production wiring in playback_services.dart: playback
|
||||
// authority is Watch Together's alone, and only the navigation gate carries
|
||||
// the automotive requirement. Starting playback is refused downstream by
|
||||
// the playback-intent wrappers, not here — `route` consumes a denied event,
|
||||
// so gating `canControlPlayback` would silently drop `PauseEvent`.
|
||||
final router = MediaControlRouter(
|
||||
canControlPlayback: () => true,
|
||||
canNavigateMediaItems: automotivePlaybackAllowedNow,
|
||||
onPlay: () => calls.add('play'),
|
||||
onPause: () => calls.add('pause'),
|
||||
onTogglePlayPause: () => calls.add('toggle'),
|
||||
onSeek: (_) => calls.add('seek'),
|
||||
onNext: () => calls.add('next'),
|
||||
onPrevious: () => calls.add('previous'),
|
||||
onStop: () => calls.add('stop'),
|
||||
onSkipForward: (_) => calls.add('forward'),
|
||||
onSkipBackward: (_) => calls.add('backward'),
|
||||
onSetSpeed: (_) => calls.add('speed'),
|
||||
);
|
||||
|
||||
// Stopping audio must stay reachable while the vehicle restricts the app.
|
||||
expect(router.route(const PauseEvent()), isTrue);
|
||||
expect(router.route(const StopEvent()), isTrue);
|
||||
expect(calls, ['pause', 'stop']);
|
||||
|
||||
// Queue navigation starts audio, so it is refused while restricted.
|
||||
expect(router.route(const NextTrackEvent()), isTrue);
|
||||
expect(router.route(const PreviousTrackEvent()), isTrue);
|
||||
expect(calls, ['pause', 'stop']);
|
||||
|
||||
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
|
||||
expect(router.route(const NextTrackEvent()), isTrue);
|
||||
expect(calls, ['pause', 'stop', 'next']);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user