Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Fix Android Auto coming up empty when the app isn't running

Barnabasd016769707a2
diff --git a/android/app/src/main/kotlin/com/barnabas/absorb/MainActivity.kt b/android/app/src/main/kotlin/com/barnabas/absorb/MainActivity.kt
index 2e7d7fb0..9b7951ca 100644
--- a/android/app/src/main/kotlin/com/barnabas/absorb/MainActivity.kt
+++ b/android/app/src/main/kotlin/com/barnabas/absorb/MainActivity.kt
@@ -19,9 +19,12 @@ import android.os.StatFs
import android.util.Log
import android.view.KeyEvent
+import com.ryanheise.audioservice.AudioService
import com.ryanheise.audioservice.AudioServiceActivity
+import com.ryanheise.audioservice.AudioServicePlugin
import com.ryanheise.just_audio.MonoController
import io.flutter.embedding.engine.FlutterEngine
+import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
@@ -47,6 +50,27 @@ class MainActivity : AudioServiceActivity() {
// init proves the engine is unavailable, skip attaching native effects.
private var effectsAvailable: Boolean = true
+ override fun provideFlutterEngine(context: Context): FlutterEngine? {
+ // Headless service starts (Android Auto binds, media buttons on a
+ // dead process) boot the engine with no activity so the browse tree
+ // works. Never attach the UI to such an engine unless it's actually
+ // mid-playback - it has never rendered a frame and the launch sticks
+ // on the splash screen. Tear it down and boot fresh instead.
+ val cache = FlutterEngineCache.getInstance()
+ val cached = cache.get(AudioServicePlugin.getFlutterEngineId())
+ if (cached != null && AudioServicePlugin.wasEngineBornHeadless() &&
+ !AudioService.isInstancePlaying()) {
+ try {
+ cache.remove(AudioServicePlugin.getFlutterEngineId())
+ cached.destroy()
+ Log.d(TAG, "Replaced idle headless-born engine with a fresh launch")
+ } catch (e: Exception) {
+ Log.e(TAG, "Headless engine teardown failed", e)
+ }
+ }
+ return super.provideFlutterEngine(context)
+ }
+
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
diff --git a/lib/main.dart b/lib/main.dart
index 220e4f62..0320ced8 100644
--- a/lib/main.dart
+++ b/lib/main.dart
@@ -557,9 +557,12 @@ class _AuthGateState extends State<AuthGate> {
classicWordingNotifier.value = await PlayerSettings.getClassicWording();
PlayerSettings.showExplicitBadge = await PlayerSettings.getShowExplicitBadge();
// Rotation lock: main() applied it before scope was active, so that pass
- // read the never-written unscoped key and always came up unlocked.
+ // read the never-written unscoped key and always came up unlocked. The
+ // timeout matters: on a headless boot (Android Auto bind) there is no
+ // activity, the orientation channel never answers, and a bare await here
+ // wedges the entire init - the browse tree never builds.
try {
- await applyOrientationLock();
+ await applyOrientationLock().timeout(const Duration(seconds: 2));
} catch (_) {}
// Background new-episode notifications: re-register the periodic job
// (self-healing after OEM kills) and wire up notification taps.
diff --git a/lib/screens/app_shell.dart b/lib/screens/app_shell.dart
index 39a31dd3..74d7669a 100644
--- a/lib/screens/app_shell.dart
+++ b/lib/screens/app_shell.dart
@@ -589,6 +589,12 @@ class _AppShellState extends State<AppShell>
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
+ // Before the backgrounded guard: when the activity attaches to an
+ // engine that has never drawn (born from a headless Android Auto /
+ // media-button start, kept alive because audio is playing), nothing
+ // schedules a frame for the new surface and the launch sits on the
+ // splash screen - and that engine never saw a backgrounded state.
+ WidgetsBinding.instance.scheduleForcedFrame();
_handleAppForegrounded();
break;
case AppLifecycleState.hidden:
diff --git a/packages/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java b/packages/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java
index 5bfb55fd..16090fda 100644
--- a/packages/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java
+++ b/packages/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java
@@ -290,6 +290,12 @@ public class AudioService extends MediaBrowserServiceCompat {
return processingState;
}
+ // Absorb patch: playback state for code outside this package (MainActivity
+ // decides whether a headless-born engine is safe to tear down on attach).
+ public static boolean isInstancePlaying() {
+ return instance != null && instance.isPlaying();
+ }
+
public boolean isPlaying() {
return playing;
}
@@ -350,15 +356,15 @@ public class AudioService extends MediaBrowserServiceCompat {
}
};
- // Absorb patch: only adopt an engine that already exists - never
- // create one here. A headless service start (widget media-button
- // press or the system's media resumption bind after the process died,
- // e.g. an app update installed mid-listen) would run the app's full
- // main() with no activity, and that half-initialized engine gets
- // cached: the next app launch attaches to it and freezes on the
- // splash screen until the user force-stops the app. Real launches
- // create the engine through the activity before connecting.
- flutterEngine = FlutterEngineCache.getInstance().get(AudioServicePlugin.getFlutterEngineId());
+ // Absorb patch v2: create the engine when it's missing so headless
+ // starts (Android Auto browse binds, media buttons, media resumption)
+ // boot the app and can serve the browse tree - the cache-only version
+ // of this patch left Android Auto with an empty library whenever the
+ // process was dead. The old poisoned-cache problem (a headless-born
+ // engine froze the next real launch on the splash screen) is handled
+ // on the other side now: MainActivity tears down an idle headless-born
+ // engine and boots fresh instead of attaching to it.
+ flutterEngine = AudioServicePlugin.getFlutterEngine(this);
}
@Override
diff --git a/packages/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioServicePlugin.java b/packages/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioServicePlugin.java
index 16c1d5cf..9066c805 100644
--- a/packages/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioServicePlugin.java
+++ b/packages/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioServicePlugin.java
@@ -67,9 +67,19 @@ public class AudioServicePlugin implements FlutterPlugin, ActivityAware {
public static String getFlutterEngineId() {
return flutterEngineId;
}
+ // Absorb patch: true when the cached engine was created by a headless
+ // service start (Android Auto bind, media button on a dead process).
+ // MainActivity uses this to replace such an engine with a fresh one
+ // instead of attaching to it - a never-rendered engine sticks the launch
+ // on the splash screen.
+ private static boolean engineBornHeadless = false;
+ public static boolean wasEngineBornHeadless() {
+ return engineBornHeadless;
+ }
public static synchronized FlutterEngine getFlutterEngine(Context context) {
FlutterEngine flutterEngine = FlutterEngineCache.getInstance().get(flutterEngineId);
if (flutterEngine == null) {
+ engineBornHeadless = !(context instanceof Activity);
// XXX: The constructor triggers onAttachedToEngine so this variable doesn't help us.
// Maybe need a boolean flag to tell us we're currently loading the main flutter engine.
flutterEngine = new FlutterEngine(context.getApplicationContext());