Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Keep a resume card for Absorb in the Android media controls after the app is closed

Barnabasd34abd33e4f6
diff --git a/lib/services/android_auto_service.dart b/lib/services/android_auto_service.dart
index 50499865..e301f9ec 100644
--- a/lib/services/android_auto_service.dart
+++ b/lib/services/android_auto_service.dart
@@ -4,6 +4,7 @@ import 'dart:io' show File, Platform;
import 'package:audio_service/audio_service.dart';
import 'package:flutter/foundation.dart';
+import 'package:home_widget/home_widget.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'api_service.dart';
import 'audio_player_service.dart';
@@ -992,6 +993,10 @@ class AndroidAutoService {
/// Main entry point for browse tree. May make API calls for drilldowns.
Future<List<MediaItem>> getChildrenOf(String parentMediaId) {
+ // Android asks for this after the app is gone, to keep a resume card in
+ // the quick settings media carousel. Never cached - it has to follow
+ // whatever was played last.
+ if (parentMediaId == AudioService.recentRootId) return _recentChildren();
final cached = _childrenCache[parentMediaId];
if (cached != null) return Future.value(cached);
@@ -2000,6 +2005,62 @@ class AndroidAutoService {
}
}
+ /// The last book or episode played, as one playable item, from local data
+ /// only so it works with no connection. Same record the cold-start restore
+ /// resumes from, so tapping play on the card plays what the card shows.
+ Future<List<MediaItem>> _recentChildren() async {
+ try {
+ final prefs = await SharedPreferences.getInstance();
+ final itemId = prefs.getString('widget_item_id');
+ if (itemId == null || itemId.isEmpty) return const [];
+ final episodeId = prefs.getString('widget_episode_id');
+ final key = episodeId != null ? '$itemId-$episodeId' : itemId;
+
+ var title = '';
+ var author = '';
+ final known = findEntry(key);
+ final dl = DownloadService();
+ if (known != null) {
+ title = known.title;
+ author = known.author;
+ } else if (dl.isDownloaded(key)) {
+ final info = dl.getInfo(key);
+ title = info.title ?? '';
+ author = info.author ?? '';
+ }
+ if (title.isEmpty) {
+ // The widget keeps "Author · Book" (or "Show · Episode") under the
+ // chapter title, which is the best a streamed item leaves behind.
+ final subtitle =
+ await HomeWidget.getWidgetData<String>('widget_author') ?? '';
+ final cut = subtitle.indexOf(' · ');
+ if (cut > 0) {
+ author = subtitle.substring(0, cut);
+ title = subtitle.substring(cut + 3);
+ } else {
+ title = subtitle;
+ }
+ }
+ if (title.isEmpty) return const [];
+ final cover = Uri.tryParse(localCoverUri(itemId));
+ debugPrint('[AutoBrowse] Recent for media resumption: $key "$title"');
+ return [
+ MediaItem(
+ id: AutoMediaIds.itemId(key),
+ title: title,
+ artist: author,
+ album: title,
+ artUri: cover,
+ playable: true,
+ extras: cover != null ? {'artUri': cover.toString()} : null,
+ ),
+ ];
+ } catch (e) {
+ debugPrint('[AutoBrowse] Recent for media resumption failed: $e');
+ return const [];
+ }
+ }
+
// ─── Lookup helpers ────────────────────────────────────────────────
AutoBookEntry? findEntry(String absItemId) {
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index 1f11729f..939e5a28 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -6914,12 +6914,19 @@ class AudioPlayerService extends ChangeNotifier {
debugPrint(
'[Service] play() on cold-started service - routing to cold-start restore',
);
- final restore = AudioPlayerService.onColdStartPlayRequested;
+ // Android can start the process from the media resume card and send
+ // play while the app is still starting up, before main() has wired the
+ // restore handler. Wait for it rather than dropping the press.
+ var restore = AudioPlayerService.onColdStartPlayRequested;
+ for (var i = 0; restore == null && i < 50; i++) {
+ await Future.delayed(const Duration(milliseconds: 200));
+ restore = AudioPlayerService.onColdStartPlayRequested;
+ }
if (restore != null) {
unawaited(restore());
} else {
debugPrint(
- '[Service] No cold-start restore handler registered - ignoring play',
+ '[Service] No cold-start restore handler registered after 10s - ignoring play',
);
}
return;