A cross platform Audiobookshelf client for Android and iOS
Dart
Add app-icon long-press shortcuts for Continue, Search, Downloads, Bookmarks
Barnabas1175394e51c4
diff --git a/android/app/src/main/res/drawable/ic_shortcut_bookmarks.xml b/android/app/src/main/res/drawable/ic_shortcut_bookmarks.xmlnew file mode 100644index 00000000..b354af5f--- /dev/null+++ b/android/app/src/main/res/drawable/ic_shortcut_bookmarks.xml@@ -0,0 +1,15 @@+<vector xmlns:android="http://schemas.android.com/apk/res/android"+ android:width="108dp"+ android:height="108dp"+ android:viewportWidth="108"+ android:viewportHeight="108">+ <group+ android:translateX="18"+ android:translateY="18"+ android:scaleX="3"+ android:scaleY="3">+ <path+ android:fillColor="#FF000000"+ android:pathData="M17,3L7,3c-1.1,0 -1.99,0.9 -1.99,2L5,21l7,-3 7,3L19,5c0,-1.1 -0.9,-2 -2,-2zM17,18l-5,-2.18L7,18L7,5h10v13z"/>+ </group>+</vector>diff --git a/android/app/src/main/res/drawable/ic_shortcut_continue.xml b/android/app/src/main/res/drawable/ic_shortcut_continue.xmlnew file mode 100644index 00000000..23f3db58--- /dev/null+++ b/android/app/src/main/res/drawable/ic_shortcut_continue.xml@@ -0,0 +1,9 @@+<vector xmlns:android="http://schemas.android.com/apk/res/android"+ android:width="108dp"+ android:height="108dp"+ android:viewportWidth="108"+ android:viewportHeight="108">+ <path+ android:fillColor="#FF000000"+ android:pathData="M36,24v60l48,-30z"/>+</vector>diff --git a/android/app/src/main/res/drawable/ic_shortcut_downloads.xml b/android/app/src/main/res/drawable/ic_shortcut_downloads.xmlnew file mode 100644index 00000000..5067b417--- /dev/null+++ b/android/app/src/main/res/drawable/ic_shortcut_downloads.xml@@ -0,0 +1,15 @@+<vector xmlns:android="http://schemas.android.com/apk/res/android"+ android:width="108dp"+ android:height="108dp"+ android:viewportWidth="108"+ android:viewportHeight="108">+ <group+ android:translateX="18"+ android:translateY="18"+ android:scaleX="3"+ android:scaleY="3">+ <path+ android:fillColor="#FF000000"+ android:pathData="M5,20h14v-2L5,18v2zM19,9h-4L15,3L9,3v6L5,9l7,7 7,-7z"/>+ </group>+</vector>diff --git a/android/app/src/main/res/drawable/ic_shortcut_search.xml b/android/app/src/main/res/drawable/ic_shortcut_search.xmlnew file mode 100644index 00000000..81d511b1--- /dev/null+++ b/android/app/src/main/res/drawable/ic_shortcut_search.xml@@ -0,0 +1,15 @@+<vector xmlns:android="http://schemas.android.com/apk/res/android"+ android:width="108dp"+ android:height="108dp"+ android:viewportWidth="108"+ android:viewportHeight="108">+ <group+ android:translateX="18"+ android:translateY="18"+ android:scaleX="3"+ android:scaleY="3">+ <path+ android:fillColor="#FF000000"+ android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/>+ </group>+</vector>diff --git a/android/app/src/main/res/raw/keep.xml b/android/app/src/main/res/raw/keep.xmlindex 9755eab5..00cb1174 100644--- a/android/app/src/main/res/raw/keep.xml+++ b/android/app/src/main/res/raw/keep.xml@@ -1,3 +1,3 @@<?xml version="1.0" encoding="utf-8"?><resources xmlns:tools="http://schemas.android.com/tools"- tools:keep="@drawable/ic_skip_*,@drawable/ic_widget_*,@drawable/ic_notification" />+ tools:keep="@drawable/ic_skip_*,@drawable/ic_widget_*,@drawable/ic_notification,@drawable/ic_shortcut_*" />diff --git a/lib/main.dart b/lib/main.dartindex ecfbff5d..5a70f181 100644--- a/lib/main.dart+++ b/lib/main.dart@@ -24,6 +24,7 @@ import 'services/android_auto_service.dart';import 'services/chromecast_service.dart';import 'services/home_widget_service.dart';import 'services/log_service.dart';+import 'services/quick_actions_service.dart';import 'screens/login_screen.dart';import 'screens/app_shell.dart';import 'widgets/absorb_wave_icon.dart';@@ -73,6 +74,11 @@ void applyTrustAllCerts(bool enabled) {final GlobalKey<ScaffoldMessengerState> scaffoldMessengerKey =GlobalKey<ScaffoldMessengerState>();+/// Global navigator key so non-widget code (e.g. app-icon shortcut handlers)+/// can push routes without needing a BuildContext.+final GlobalKey<NavigatorState> rootNavigatorKey =+ GlobalKey<NavigatorState>();+ThemeMode parseThemeMode(String value) {switch (value) {case 'light': return ThemeMode.light;@@ -220,6 +226,7 @@ class AbsorbApp extends StatelessWidget {return MaterialApp(scaffoldMessengerKey: scaffoldMessengerKey,+ navigatorKey: rootNavigatorKey,title: 'Absorb',debugShowCheckedModeBanner: false,localizationsDelegates: AppLocalizations.localizationsDelegates,@@ -487,6 +494,10 @@ class _AuthGateState extends State<AuthGate> {Future.microtask(() => AndroidAutoService().refresh());// Initialize homescreen widgetawait HomeWidgetService().init();+ // App-icon long-press shortcuts (Continue Listening / Downloads).+ // Depends on AudioPlayerService + HomeWidgetService so they're ready+ // when the shortcut handler fires.+ await QuickActionsService().init();}} catch (e) {debugPrint('[Init] Service init failed: $e');diff --git a/lib/screens/app_shell.dart b/lib/screens/app_shell.dartindex 259337e9..0a8ee35b 100644--- a/lib/screens/app_shell.dart+++ b/lib/screens/app_shell.dart@@ -11,7 +11,8 @@ import '../services/sleep_timer_service.dart';import 'dart:io';import 'package:cached_network_image/cached_network_image.dart';import 'package:palette_generator/palette_generator.dart';-import '../main.dart' show snappyTransitionsNotifier, coverSchemeNotifier;+import '../main.dart'+ show snappyTransitionsNotifier, coverSchemeNotifier, rootNavigatorKey;import '../l10n/app_localizations.dart';import '../services/android_auto_service.dart';import '../widgets/expanded_card.dart';@@ -43,6 +44,16 @@ class AppShell extends StatefulWidget {_AppShellState._instance?._expandedIsOpen = open;}+ /// Switch to the Library tab and focus the search bar. Used by the+ /// app-icon "Search" shortcut. Returns false when the shell isn't mounted+ /// yet so callers can retry during cold start.+ static bool openSearchGlobal() {+ final inst = _AppShellState._instance;+ if (inst == null) return false;+ inst._openSearch();+ return true;+ }+@overrideState<AppShell> createState() => _AppShellState();}@@ -76,6 +87,33 @@ class _AppShellState extends State<AppShell> with WidgetsBindingObserver, Ticker// work until the user actually visits those tabs.final List<Widget?> _pages = List<Widget?>.filled(5, null, growable: false);+ void _openSearch() {+ if (!mounted) return;+ // If the user triggered Search while a pushed route (Downloads, Bookmarks,+ // Settings pages, etc.) is on top of the shell, pop back so the shell's+ // Library tab actually becomes visible.+ final nav = rootNavigatorKey.currentState;+ if (nav != null && nav.canPop()) {+ nav.popUntil((r) => r.isFirst);+ }+ _navigateTo(1);+ // Library tab may need a frame to mount its state before we can focus+ // the search field. Retry up to a few frames to cover fade transitions.+ int attempts = 0;+ void tryFocus() {+ if (!mounted) return;+ final state = _libraryKey.currentState;+ if (state != null) {+ state.focusSearch();+ return;+ }+ if (++attempts < 10) {+ WidgetsBinding.instance.addPostFrameCallback((_) => tryFocus());+ }+ }+ WidgetsBinding.instance.addPostFrameCallback((_) => tryFocus());+ }+void _switchToAbsorbing() {if (mounted) {_navigateTo(2);diff --git a/lib/screens/library_screen.dart b/lib/screens/library_screen.dartindex dd5d9a95..b70f842c 100644--- a/lib/screens/library_screen.dart+++ b/lib/screens/library_screen.dart@@ -3,6 +3,7 @@ import 'dart:convert';import 'dart:math';import 'dart:ui' as ui;import 'package:flutter/material.dart';+import 'package:flutter/services.dart';import 'package:provider/provider.dart';import '../providers/auth_provider.dart';import '../providers/library_provider.dart';@@ -56,6 +57,24 @@ class LibraryScreenState extends State<LibraryScreen> with TickerProviderStateMi_focusNode.unfocus();_showBars();}++ /// Give focus to the search bar (used by the app-icon "Search" shortcut).+ void focusSearch() {+ if (!mounted) return;+ _showBars();+ FocusScope.of(context).requestFocus(_focusNode);+ // Delay the explicit keyboard-show so focus has time to attach to the+ // text field and any in-flight navigation (popping Downloads/Bookmarks,+ // fading into the Library tab) can settle. Without this delay the IME+ // connection isn't ready yet and TextInput.show is a no-op.+ Future.delayed(const Duration(milliseconds: 200), () {+ if (!mounted) return;+ if (!_focusNode.hasFocus) {+ FocusScope.of(context).requestFocus(_focusNode);+ }+ SystemChannels.textInput.invokeMethod<void>('TextInput.show');+ });+ }List<dynamic> _searchBookResults = [];List<dynamic> _searchSeriesResults = [];List<dynamic> _searchAuthorResults = [];diff --git a/lib/services/quick_actions_service.dart b/lib/services/quick_actions_service.dartnew file mode 100644index 00000000..504cccad--- /dev/null+++ b/lib/services/quick_actions_service.dart@@ -0,0 +1,168 @@+import 'package:flutter/foundation.dart';+import 'package:flutter/material.dart';+import 'package:quick_actions/quick_actions.dart';++import '../main.dart' show rootNavigatorKey;+import '../screens/app_shell.dart';+import '../screens/bookmarks_screen.dart';+import '../screens/downloads_screen.dart';+import 'audio_player_service.dart';+import 'home_widget_service.dart';++/// Wires up Android / iOS app-icon long-press shortcuts:+/// - "Continue Listening" resumes the last-played item (same cold-start path+/// used by the home widget and lock-screen play button) or resumes a+/// paused session directly when one is already loaded.+/// - "Downloads" pushes [DownloadsScreen] onto the navigator.+/// - "Search" switches to the Library tab and focuses the search bar.+/// - "Bookmarks" pushes [BookmarksScreen] onto the navigator.+///+/// The Continue Listening label is updated to include the active book's+/// title whenever the player's current item changes, so the long-press menu+/// reflects what will actually start.+class QuickActionsService {+ QuickActionsService._();+ static final QuickActionsService _instance = QuickActionsService._();+ factory QuickActionsService() => _instance;++ static const _typeContinue = 'continue_listening';+ static const _typeDownloads = 'open_downloads';+ static const _typeSearch = 'open_search';+ static const _typeBookmarks = 'open_bookmarks';++ final QuickActions _quickActions = const QuickActions();+ bool _initialised = false;+ String? _lastPushedTitle;++ Future<void> init() async {+ if (_initialised) return;+ _initialised = true;++ _quickActions.initialize((type) {+ debugPrint('[QuickActions] triggered: $type');+ switch (type) {+ case _typeContinue:+ _handleContinue();+ break;+ case _typeDownloads:+ _handleDownloads();+ break;+ case _typeSearch:+ _handleSearch();+ break;+ case _typeBookmarks:+ _handleBookmarks();+ break;+ }+ });++ await _pushShortcuts();+ AudioPlayerService().addListener(_onPlayerChanged);+ }++ void _onPlayerChanged() {+ final title = AudioPlayerService().currentTitle;+ if (title == _lastPushedTitle) return;+ _pushShortcuts();+ }++ Future<void> _pushShortcuts() async {+ final title = AudioPlayerService().currentTitle;+ _lastPushedTitle = title;+ // Keep the label one word so Android doesn't truncate it in the+ // long-press menu: "Resume" when a session is loaded, "Continue" otherwise.+ final continueLabel =+ (title != null && title.isNotEmpty) ? 'Resume' : 'Continue';++ try {+ await _quickActions.setShortcutItems(<ShortcutItem>[+ ShortcutItem(+ type: _typeContinue,+ localizedTitle: continueLabel,+ icon: 'ic_shortcut_continue',+ ),+ const ShortcutItem(+ type: _typeDownloads,+ localizedTitle: 'Downloads',+ icon: 'ic_shortcut_downloads',+ ),+ const ShortcutItem(+ type: _typeSearch,+ localizedTitle: 'Search',+ icon: 'ic_shortcut_search',+ ),+ const ShortcutItem(+ type: _typeBookmarks,+ localizedTitle: 'Bookmarks',+ icon: 'ic_shortcut_bookmarks',+ ),+ ]);+ } catch (e) {+ debugPrint('[QuickActions] setShortcutItems failed: $e');+ }+ }++ Future<void> _handleContinue() async {+ try {+ // Pop any pushed route (Downloads/Bookmarks/Settings etc.) and switch+ // to the Absorbing tab so the user actually sees the player after+ // triggering Resume/Continue. Best-effort: ignore if shell/nav aren't+ // mounted yet during cold start - the default tab is Absorbing anyway.+ final nav = rootNavigatorKey.currentState;+ if (nav != null && nav.canPop()) {+ nav.popUntil((r) => r.isFirst);+ }+ AppShell.goToAbsorbingGlobal();++ final player = AudioPlayerService();+ // If a session is already loaded (e.g. just paused), resume it directly.+ // HomeWidgetService.resumeLastPlayedIfAvailable() is the cold-start path+ // and early-returns when player.hasBook is true, so we can't reuse it.+ if (player.hasBook) {+ if (!player.isPlaying) {+ await player.play();+ }+ return;+ }+ await HomeWidgetService().resumeLastPlayedIfAvailable();+ } catch (e) {+ debugPrint('[QuickActions] resume failed: $e');+ }+ }++ Future<void> _handleDownloads() async {+ for (int i = 0; i < 20; i++) {+ final nav = rootNavigatorKey.currentState;+ if (nav != null && nav.mounted) {+ nav.push(+ MaterialPageRoute(builder: (_) => const DownloadsScreen()),+ );+ return;+ }+ await Future.delayed(const Duration(milliseconds: 250));+ }+ debugPrint('[QuickActions] downloads: navigator never became ready');+ }++ Future<void> _handleSearch() async {+ for (int i = 0; i < 20; i++) {+ if (AppShell.openSearchGlobal()) return;+ await Future.delayed(const Duration(milliseconds: 250));+ }+ debugPrint('[QuickActions] search: app shell never became ready');+ }++ Future<void> _handleBookmarks() async {+ for (int i = 0; i < 20; i++) {+ final nav = rootNavigatorKey.currentState;+ if (nav != null && nav.mounted) {+ nav.push(+ MaterialPageRoute(builder: (_) => const BookmarksScreen()),+ );+ return;+ }+ await Future.delayed(const Duration(milliseconds: 250));+ }+ debugPrint('[QuickActions] bookmarks: navigator never became ready');+ }+}diff --git a/pubspec.lock b/pubspec.lockindex baf357a3..23f5d998 100644--- a/pubspec.lock+++ b/pubspec.lock@@ -780,6 +780,38 @@ packages:url: "https://pub.dev"source: hostedversion: "6.1.5+1"+ quick_actions:+ dependency: "direct main"+ description:+ name: quick_actions+ sha256: "7e35dd6a21f5bbd21acf6899039eaf85001a5ac26d52cbd6a8a2814505b90798"+ url: "https://pub.dev"+ source: hosted+ version: "1.1.0"+ quick_actions_android:+ dependency: transitive+ description:+ name: quick_actions_android+ sha256: "8e491323342fa68420ca26a9bd5a2bbcdb526bda33db45b6427c6d139fb7316d"+ url: "https://pub.dev"+ source: hosted+ version: "1.0.29"+ quick_actions_ios:+ dependency: transitive+ description:+ name: quick_actions_ios+ sha256: be1496e7ca1debc86d9ea08e56325649fbc5abb2b6930690c97ba0dae59992b1+ url: "https://pub.dev"+ source: hosted+ version: "1.2.4"+ quick_actions_platform_interface:+ dependency: transitive+ description:+ name: quick_actions_platform_interface+ sha256: "1fec7068db5122cd019e9340d3d7be5d36eab099695ef3402c7059ee058329a4"+ url: "https://pub.dev"+ source: hosted+ version: "1.1.0"rename_app:dependency: "direct dev"description:@@ -1218,5 +1250,5 @@ packages:source: hostedversion: "3.1.3"sdks:- dart: ">=3.9.0-0 <4.0.0"- flutter: ">=3.32.0"+ dart: ">=3.10.0 <4.0.0"+ flutter: ">=3.38.0"diff --git a/pubspec.yaml b/pubspec.yamlindex 0dc0f42a..f1577f64 100644--- a/pubspec.yaml+++ b/pubspec.yaml@@ -80,6 +80,9 @@ dependencies:# Homescreen widgethome_widget: ^0.7.0+ # App icon long-press shortcuts (Continue Listening / Downloads)+ quick_actions: ^1.0.8+# Splash screen (preserve during init)flutter_native_splash: ^2.4.4vibration: ^3.1.8