Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Let volume buttons turn pages in the ebook reader

Barnabasf6d6d2f4f1b2
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 8c845045..2e7d7fb0 100644
--- a/android/app/src/main/kotlin/com/barnabas/absorb/MainActivity.kt
+++ b/android/app/src/main/kotlin/com/barnabas/absorb/MainActivity.kt
@@ -18,6 +18,7 @@ import android.os.Environment
import android.os.StatFs
import android.util.Log
+import android.view.KeyEvent
import com.ryanheise.audioservice.AudioServiceActivity
import com.ryanheise.just_audio.MonoController
import io.flutter.embedding.engine.FlutterEngine
@@ -28,6 +29,11 @@ class MainActivity : AudioServiceActivity() {
private val TAG = "AbsorbEQ"
private val CHANNEL = "com.absorb.equalizer"
+ // Ebook reader: volume keys turn pages while watching is on. The keys are
+ // consumed here so system volume doesn't change.
+ private var volumeKeysChannel: MethodChannel? = null
+ private var watchVolumeKeys = false
+
private var equalizer: Equalizer? = null
private var bassBoost: BassBoost? = null
private var virtualizer: Virtualizer? = null
@@ -118,11 +124,46 @@ class MainActivity : AudioServiceActivity() {
}
}
+ volumeKeysChannel = MethodChannel(
+ flutterEngine.dartExecutor.binaryMessenger, "com.absorb.volume_keys")
+ volumeKeysChannel?.setMethodCallHandler { call, result ->
+ when (call.method) {
+ "watch" -> { watchVolumeKeys = true; result.success(true) }
+ "clearWatch" -> { watchVolumeKeys = false; result.success(true) }
+ else -> result.notImplemented()
+ }
+ }
+
// GMS-backed channels (cast foreground service, wear bridges).
// Resolves to the real impl in github/playstore, no-op in fdroid.
PlatformIntegration.registerChannels(this, flutterEngine)
}
+ override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
+ if (watchVolumeKeys) {
+ when (keyCode) {
+ KeyEvent.KEYCODE_VOLUME_UP -> {
+ volumeKeysChannel?.invokeMethod("volumePressed", "up")
+ return true
+ }
+ KeyEvent.KEYCODE_VOLUME_DOWN -> {
+ volumeKeysChannel?.invokeMethod("volumePressed", "down")
+ return true
+ }
+ }
+ }
+ return super.onKeyDown(keyCode, event)
+ }
+
+ // Consume the matching key-up too so the system doesn't act on it.
+ override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
+ if (watchVolumeKeys &&
+ (keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)) {
+ return true
+ }
+ return super.onKeyUp(keyCode, event)
+ }
+
// Move downloaded temp files into the user's SAF folder, creating the nested
// [subfolder] (e.g. "Author/Title") under the granted tree via the DocumentFile
// chain so files nest correctly and stay readable through the original grant.
diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift
index ae3a76af..c4ddd78b 100644
--- a/ios/Runner/AppDelegate.swift
+++ b/ios/Runner/AppDelegate.swift
@@ -12,6 +12,8 @@ let flutterEngine = FlutterEngine(name: "SharedEngine", project: nil, allowHeadl
@main
@objc class AppDelegate: FlutterAppDelegate {
private var widgetChannel: FlutterMethodChannel?
+ private var volumeKeysChannel: FlutterMethodChannel?
+ private let volumeKeyWatcher = VolumeKeyWatcher()
/// True once this process has become the foreground audio owner. Used to
/// ignore our own takeover broadcast so we don't stop our own playback (#285).
@@ -349,6 +351,27 @@ let flutterEngine = FlutterEngine(name: "SharedEngine", project: nil, allowHeadl
}
}
+ // Ebook reader: volume keys turn pages. Watching intercepts presses via
+ // an outputVolume observer and resets the level so volume never moves.
+ let volKeys = FlutterMethodChannel(name: "com.absorb.volume_keys",
+ binaryMessenger: messenger)
+ volumeKeysChannel = volKeys
+ volumeKeyWatcher.onPressed = { [weak self] direction in
+ self?.volumeKeysChannel?.invokeMethod("volumePressed", arguments: direction)
+ }
+ volKeys.setMethodCallHandler { [weak self] (call, result) in
+ switch call.method {
+ case "watch":
+ self?.volumeKeyWatcher.start()
+ result(true)
+ case "clearWatch":
+ self?.volumeKeyWatcher.stop()
+ result(true)
+ default:
+ result(FlutterMethodNotImplemented)
+ }
+ }
+
let storageChannel = FlutterMethodChannel(name: "com.absorb.storage",
binaryMessenger: messenger)
storageChannel.setMethodCallHandler { (call, result) in
@@ -622,3 +645,73 @@ enum AudioClipExporter {
}
}
}
+
+/// Ebook reader page turns via the physical volume keys. Observes the audio
+/// session's outputVolume; a press away from the baseline fires a direction,
+/// then the level is snapped back through a hidden MPVolumeView slider (whose
+/// presence in the window also suppresses the system volume HUD). The user's
+/// real volume is saved on start and restored on stop.
+private final class VolumeKeyWatcher {
+ private var observation: NSKeyValueObservation?
+ private var volumeView: MPVolumeView?
+ private var baseline: Float = 0.5
+ private var savedVolume: Float?
+
+ var onPressed: ((String) -> Void)?
+
+ func start() {
+ guard observation == nil else { return }
+ let session = AVAudioSession.sharedInstance()
+ try? session.setActive(true)
+
+ let vv = MPVolumeView(frame: CGRect(x: -3000, y: -3000, width: 1, height: 1))
+ vv.clipsToBounds = true
+ let scenes = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }
+ let host = scenes.flatMap { $0.windows }.first { $0.isKeyWindow }
+ ?? scenes.flatMap { $0.windows }.first
+ host?.addSubview(vv)
+ volumeView = vv
+
+ savedVolume = session.outputVolume
+ // Snap the baseline to iOS's 1/16 volume steps, away from the extremes so
+ // both directions stay detectable at min/max volume.
+ baseline = (min(max(session.outputVolume, 0.25), 0.75) * 16).rounded() / 16
+
+ // Give the volume view a beat to land in the hierarchy before its slider
+ // will accept a value, then normalize to the baseline.
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
+ guard let self, self.observation != nil else { return }
+ self.setSystemVolume(self.baseline)
+ }
+
+ observation = session.observe(\.outputVolume, options: [.new]) { [weak self] _, change in
+ guard let self, let new = change.newValue else { return }
+ // Our own baseline resets land exactly on the baseline - ignore them.
+ if abs(new - self.baseline) < 0.01 { return }
+ let direction = new > self.baseline ? "up" : "down"
+ DispatchQueue.main.async {
+ self.onPressed?(direction)
+ self.setSystemVolume(self.baseline)
+ }
+ }
+ }
+
+ func stop() {
+ guard observation != nil else { return }
+ observation?.invalidate()
+ observation = nil
+ if let saved = savedVolume { setSystemVolume(saved) }
+ savedVolume = nil
+ let vv = volumeView
+ volumeView = nil
+ // Keep the view alive briefly so the restore above goes through.
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
+ vv?.removeFromSuperview()
+ }
+ }
+
+ private func setSystemVolume(_ value: Float) {
+ guard let slider = volumeView?.subviews.compactMap({ $0 as? UISlider }).first else { return }
+ slider.value = value
+ }
+}
diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb
index 49372ae0..7d190ded 100644
--- a/lib/l10n/app_en.arb
+++ b/lib/l10n/app_en.arb
@@ -3877,6 +3877,11 @@
"readerLayoutTwoPage": "Two-page",
"readerTheme": "Theme",
"readerFont": "Font",
+ "readerVolumeNav": "Volume keys turn pages",
+ "readerVolumeNavOff": "Off",
+ "readerVolumeNavNormal": "Normal",
+ "readerVolumeNavMirrored": "Mirrored",
+ "readerVolumeNavWhilePlaying": "Even while audio is playing",
"readerMoreFonts": "Download more fonts",
"readerFontRemove": "Remove download",
"readerFontDownloadFailed": "Couldn't download {font}",
diff --git a/lib/l10n/app_localizations.dart b/lib/l10n/app_localizations.dart
index f8ec4f1e..4f05b8f2 100644
--- a/lib/l10n/app_localizations.dart
+++ b/lib/l10n/app_localizations.dart
@@ -11685,6 +11685,36 @@ abstract class AppLocalizations {
/// **'Font'**
String get readerFont;
+ /// No description provided for @readerVolumeNav.
+ ///
+ /// In en, this message translates to:
+ /// **'Volume keys turn pages'**
+ String get readerVolumeNav;
+
+ /// No description provided for @readerVolumeNavOff.
+ ///
+ /// In en, this message translates to:
+ /// **'Off'**
+ String get readerVolumeNavOff;
+
+ /// No description provided for @readerVolumeNavNormal.
+ ///
+ /// In en, this message translates to:
+ /// **'Normal'**
+ String get readerVolumeNavNormal;
+
+ /// No description provided for @readerVolumeNavMirrored.
+ ///
+ /// In en, this message translates to:
+ /// **'Mirrored'**
+ String get readerVolumeNavMirrored;
+
+ /// No description provided for @readerVolumeNavWhilePlaying.
+ ///
+ /// In en, this message translates to:
+ /// **'Even while audio is playing'**
+ String get readerVolumeNavWhilePlaying;
+
/// No description provided for @readerMoreFonts.
///
/// In en, this message translates to:
diff --git a/lib/l10n/app_localizations_de.dart b/lib/l10n/app_localizations_de.dart
index 7c762885..ad182bd3 100644
--- a/lib/l10n/app_localizations_de.dart
+++ b/lib/l10n/app_localizations_de.dart
@@ -6731,6 +6731,21 @@ class AppLocalizationsDe extends AppLocalizations {
@override
String get readerFont => 'Font';
+ @override
+ String get readerVolumeNav => 'Volume keys turn pages';
+
+ @override
+ String get readerVolumeNavOff => 'Off';
+
+ @override
+ String get readerVolumeNavNormal => 'Normal';
+
+ @override
+ String get readerVolumeNavMirrored => 'Mirrored';
+
+ @override
+ String get readerVolumeNavWhilePlaying => 'Even while audio is playing';
+
@override
String get readerMoreFonts => 'Download more fonts';
diff --git a/lib/l10n/app_localizations_el.dart b/lib/l10n/app_localizations_el.dart
index 24141616..f8e358a6 100644
--- a/lib/l10n/app_localizations_el.dart
+++ b/lib/l10n/app_localizations_el.dart
@@ -6664,6 +6664,21 @@ class AppLocalizationsEl extends AppLocalizations {
@override
String get readerFont => 'Font';
+ @override
+ String get readerVolumeNav => 'Volume keys turn pages';
+
+ @override
+ String get readerVolumeNavOff => 'Off';
+
+ @override
+ String get readerVolumeNavNormal => 'Normal';
+
+ @override
+ String get readerVolumeNavMirrored => 'Mirrored';
+
+ @override
+ String get readerVolumeNavWhilePlaying => 'Even while audio is playing';
+
@override
String get readerMoreFonts => 'Download more fonts';
diff --git a/lib/l10n/app_localizations_en.dart b/lib/l10n/app_localizations_en.dart
index 0f7ef04d..d5a5e469 100644
--- a/lib/l10n/app_localizations_en.dart
+++ b/lib/l10n/app_localizations_en.dart
@@ -6664,6 +6664,21 @@ class AppLocalizationsEn extends AppLocalizations {
@override
String get readerFont => 'Font';
+ @override
+ String get readerVolumeNav => 'Volume keys turn pages';
+
+ @override
+ String get readerVolumeNavOff => 'Off';
+
+ @override
+ String get readerVolumeNavNormal => 'Normal';
+
+ @override
+ String get readerVolumeNavMirrored => 'Mirrored';
+
+ @override
+ String get readerVolumeNavWhilePlaying => 'Even while audio is playing';
+
@override
String get readerMoreFonts => 'Download more fonts';
diff --git a/lib/l10n/app_localizations_es.dart b/lib/l10n/app_localizations_es.dart
index 4b9ad0e8..8569907c 100644
--- a/lib/l10n/app_localizations_es.dart
+++ b/lib/l10n/app_localizations_es.dart
@@ -6664,6 +6664,21 @@ class AppLocalizationsEs extends AppLocalizations {
@override
String get readerFont => 'Font';
+ @override
+ String get readerVolumeNav => 'Volume keys turn pages';
+
+ @override
+ String get readerVolumeNavOff => 'Off';
+
+ @override
+ String get readerVolumeNavNormal => 'Normal';
+
+ @override
+ String get readerVolumeNavMirrored => 'Mirrored';
+
+ @override
+ String get readerVolumeNavWhilePlaying => 'Even while audio is playing';
+
@override
String get readerMoreFonts => 'Download more fonts';
diff --git a/lib/l10n/app_localizations_fr.dart b/lib/l10n/app_localizations_fr.dart
index 7cb6aa29..6a639839 100644
--- a/lib/l10n/app_localizations_fr.dart
+++ b/lib/l10n/app_localizations_fr.dart
@@ -6666,6 +6666,21 @@ class AppLocalizationsFr extends AppLocalizations {
@override
String get readerFont => 'Font';
+ @override
+ String get readerVolumeNav => 'Volume keys turn pages';
+
+ @override
+ String get readerVolumeNavOff => 'Off';
+
+ @override
+ String get readerVolumeNavNormal => 'Normal';
+
+ @override
+ String get readerVolumeNavMirrored => 'Mirrored';
+
+ @override
+ String get readerVolumeNavWhilePlaying => 'Even while audio is playing';
+
@override
String get readerMoreFonts => 'Download more fonts';
diff --git a/lib/l10n/app_localizations_it.dart b/lib/l10n/app_localizations_it.dart
index f0a93be2..5ccf125e 100644
--- a/lib/l10n/app_localizations_it.dart
+++ b/lib/l10n/app_localizations_it.dart
@@ -6664,6 +6664,21 @@ class AppLocalizationsIt extends AppLocalizations {
@override
String get readerFont => 'Font';
+ @override
+ String get readerVolumeNav => 'Volume keys turn pages';
+
+ @override
+ String get readerVolumeNavOff => 'Off';
+
+ @override
+ String get readerVolumeNavNormal => 'Normal';
+
+ @override
+ String get readerVolumeNavMirrored => 'Mirrored';
+
+ @override
+ String get readerVolumeNavWhilePlaying => 'Even while audio is playing';
+
@override
String get readerMoreFonts => 'Download more fonts';
diff --git a/lib/l10n/app_localizations_ja.dart b/lib/l10n/app_localizations_ja.dart
index 4ddbbfc5..e6820324 100644
--- a/lib/l10n/app_localizations_ja.dart
+++ b/lib/l10n/app_localizations_ja.dart
@@ -6664,6 +6664,21 @@ class AppLocalizationsJa extends AppLocalizations {
@override
String get readerFont => 'Font';
+ @override
+ String get readerVolumeNav => 'Volume keys turn pages';
+
+ @override
+ String get readerVolumeNavOff => 'Off';
+
+ @override
+ String get readerVolumeNavNormal => 'Normal';
+
+ @override
+ String get readerVolumeNavMirrored => 'Mirrored';
+
+ @override
+ String get readerVolumeNavWhilePlaying => 'Even while audio is playing';
+
@override
String get readerMoreFonts => 'Download more fonts';
diff --git a/lib/l10n/app_localizations_no.dart b/lib/l10n/app_localizations_no.dart
index 10ab6cdf..b8125927 100644
--- a/lib/l10n/app_localizations_no.dart
+++ b/lib/l10n/app_localizations_no.dart
@@ -6664,6 +6664,21 @@ class AppLocalizationsNo extends AppLocalizations {
@override
String get readerFont => 'Font';
+ @override
+ String get readerVolumeNav => 'Volume keys turn pages';
+
+ @override
+ String get readerVolumeNavOff => 'Off';
+
+ @override
+ String get readerVolumeNavNormal => 'Normal';
+
+ @override
+ String get readerVolumeNavMirrored => 'Mirrored';
+
+ @override
+ String get readerVolumeNavWhilePlaying => 'Even while audio is playing';
+
@override
String get readerMoreFonts => 'Download more fonts';
diff --git a/lib/l10n/app_localizations_pt.dart b/lib/l10n/app_localizations_pt.dart
index 3878e4ef..15fc7317 100644
--- a/lib/l10n/app_localizations_pt.dart
+++ b/lib/l10n/app_localizations_pt.dart
@@ -6664,6 +6664,21 @@ class AppLocalizationsPt extends AppLocalizations {
@override
String get readerFont => 'Font';
+ @override
+ String get readerVolumeNav => 'Volume keys turn pages';
+
+ @override
+ String get readerVolumeNavOff => 'Off';
+
+ @override
+ String get readerVolumeNavNormal => 'Normal';
+
+ @override
+ String get readerVolumeNavMirrored => 'Mirrored';
+
+ @override
+ String get readerVolumeNavWhilePlaying => 'Even while audio is playing';
+
@override
String get readerMoreFonts => 'Download more fonts';
diff --git a/lib/l10n/app_localizations_ro.dart b/lib/l10n/app_localizations_ro.dart
index f32ee746..cf326d03 100644
--- a/lib/l10n/app_localizations_ro.dart
+++ b/lib/l10n/app_localizations_ro.dart
@@ -6664,6 +6664,21 @@ class AppLocalizationsRo extends AppLocalizations {
@override
String get readerFont => 'Font';
+ @override
+ String get readerVolumeNav => 'Volume keys turn pages';
+
+ @override
+ String get readerVolumeNavOff => 'Off';
+
+ @override
+ String get readerVolumeNavNormal => 'Normal';
+
+ @override
+ String get readerVolumeNavMirrored => 'Mirrored';
+
+ @override
+ String get readerVolumeNavWhilePlaying => 'Even while audio is playing';
+
@override
String get readerMoreFonts => 'Download more fonts';
diff --git a/lib/l10n/app_localizations_ru.dart b/lib/l10n/app_localizations_ru.dart
index be7f766a..0decaa37 100644
--- a/lib/l10n/app_localizations_ru.dart
+++ b/lib/l10n/app_localizations_ru.dart
@@ -6667,6 +6667,21 @@ class AppLocalizationsRu extends AppLocalizations {
@override
String get readerFont => 'Font';
+ @override
+ String get readerVolumeNav => 'Volume keys turn pages';
+
+ @override
+ String get readerVolumeNavOff => 'Off';
+
+ @override
+ String get readerVolumeNavNormal => 'Normal';
+
+ @override
+ String get readerVolumeNavMirrored => 'Mirrored';
+
+ @override
+ String get readerVolumeNavWhilePlaying => 'Even while audio is playing';
+
@override
String get readerMoreFonts => 'Download more fonts';
diff --git a/lib/l10n/app_localizations_zh.dart b/lib/l10n/app_localizations_zh.dart
index 49ff8286..35d5292b 100644
--- a/lib/l10n/app_localizations_zh.dart
+++ b/lib/l10n/app_localizations_zh.dart
@@ -6587,6 +6587,21 @@ class AppLocalizationsZh extends AppLocalizations {
@override
String get readerFont => 'Font';
+ @override
+ String get readerVolumeNav => 'Volume keys turn pages';
+
+ @override
+ String get readerVolumeNavOff => 'Off';
+
+ @override
+ String get readerVolumeNavNormal => 'Normal';
+
+ @override
+ String get readerVolumeNavMirrored => 'Mirrored';
+
+ @override
+ String get readerVolumeNavWhilePlaying => 'Even while audio is playing';
+
@override
String get readerMoreFonts => 'Download more fonts';
diff --git a/lib/services/player_settings.dart b/lib/services/player_settings.dart
index 570a7066..328d4367 100644
--- a/lib/services/player_settings.dart
+++ b/lib/services/player_settings.dart
@@ -589,6 +589,15 @@ class PlayerSettings {
static Future<bool> getFullScreenPlayer() => _get('fullScreenPlayer', false);
static Future<void> setFullScreenPlayer(bool value) => _set('fullScreenPlayer', value);
+ // Ebook reader: turn pages with the volume keys. 'off' | 'normal'
+ // (up = previous page, down = next, matching the ABS app) | 'mirrored'.
+ static Future<String> getEreaderVolumeNav() => _get('ereaderVolumeNav', 'off');
+ static Future<void> setEreaderVolumeNav(String value) => _set('ereaderVolumeNav', value, notify: true);
+ // Keep turning pages with the volume keys even while audio is playing
+ // (off = playing audio gives the keys back to system volume).
+ static Future<bool> getEreaderVolumeNavWhilePlaying() => _get('ereaderVolumeNavWhilePlaying', false);
+ static Future<void> setEreaderVolumeNavWhilePlaying(bool value) => _set('ereaderVolumeNavWhilePlaying', value, notify: true);
+
/// When on, the screen is locked to portrait (rotation disabled). Default off
/// keeps the current behaviour where all orientations are allowed.
static Future<bool> getLockPortrait() => _get('lockPortrait', false);
diff --git a/lib/services/volume_key_service.dart b/lib/services/volume_key_service.dart
new file mode 100644
index 00000000..32fd9d1b
--- /dev/null
+++ b/lib/services/volume_key_service.dart
@@ -0,0 +1,86 @@
+import 'package:flutter/material.dart';
+import 'package:flutter/services.dart';
+import 'audio_player_service.dart';
+
+/// Turns the physical volume keys into ebook page turns while a reader is
+/// open. The native side (MainActivity / AppDelegate) consumes the presses so
+/// system volume never moves; this wrapper decides WHEN to watch - reader
+/// setting on, app in the foreground, and audio not playing (unless the
+/// while-playing option is on) - and maps presses to page turns.
+class EreaderVolumeNav with WidgetsBindingObserver {
+ EreaderVolumeNav({required this.onPrev, required this.onNext});
+
+ final VoidCallback onPrev;
+ final VoidCallback onNext;
+
+ static const _channel = MethodChannel('com.absorb.volume_keys');
+ // Only one reader is ever open; the channel handler routes to it.
+ static EreaderVolumeNav? _active;
+
+ String _mode = 'off';
+ bool _whilePlaying = false;
+ bool _watching = false;
+ bool _attached = false;
+
+ Future<void> attach() async {
+ if (_attached) return;
+ _attached = true;
+ _active = this;
+ _channel.setMethodCallHandler(_onCall);
+ WidgetsBinding.instance.addObserver(this);
+ AudioPlayerService().addListener(_reevaluate);
+ PlayerSettings.settingsChanged.addListener(_reloadSettings);
+ await _reloadSettings();
+ }
+
+ Future<void> detach() async {
+ if (!_attached) return;
+ _attached = false;
+ if (identical(_active, this)) _active = null;
+ WidgetsBinding.instance.removeObserver(this);
+ AudioPlayerService().removeListener(_reevaluate);
+ PlayerSettings.settingsChanged.removeListener(_reloadSettings);
+ await _setWatching(false);
+ }
+
+ Future<void> _reloadSettings() async {
+ _mode = await PlayerSettings.getEreaderVolumeNav();
+ _whilePlaying = await PlayerSettings.getEreaderVolumeNavWhilePlaying();
+ _reevaluate();
+ }
+
+ void _reevaluate() {
+ final lifecycle = WidgetsBinding.instance.lifecycleState;
+ final foreground = lifecycle == null || lifecycle == AppLifecycleState.resumed;
+ final playing = AudioPlayerService().isPlaying;
+ final want =
+ _attached && _mode != 'off' && foreground && (_whilePlaying || !playing);
+ _setWatching(want);
+ }
+
+ @override
+ void didChangeAppLifecycleState(AppLifecycleState state) => _reevaluate();
+
+ Future<void> _setWatching(bool want) async {
+ if (want == _watching) return;
+ _watching = want;
+ try {
+ await _channel.invokeMethod(want ? 'watch' : 'clearWatch');
+ } catch (_) {}
+ }
+
+ static Future<dynamic> _onCall(MethodCall call) async {
+ if (call.method != 'volumePressed') return null;
+ final nav = _active;
+ if (nav == null || !nav._watching) return null;
+ final up = (call.arguments as String? ?? '') == 'up';
+ // 'normal' matches the ABS app: volume up = previous page, down = next.
+ final prev = nav._mode == 'mirrored' ? !up : up;
+ if (prev) {
+ nav.onPrev();
+ } else {
+ nav.onNext();
+ }
+ return null;
+ }
+}
diff --git a/lib/widgets/ebook_reader_view.dart b/lib/widgets/ebook_reader_view.dart
index b25f26b5..d22356f6 100644
--- a/lib/widgets/ebook_reader_view.dart
+++ b/lib/widgets/ebook_reader_view.dart
@@ -14,6 +14,7 @@ import '../services/ebook_annotation_service.dart';
import '../services/ebook_cache.dart';
import '../services/reader_font_service.dart';
import '../services/scoped_prefs.dart';
+import '../services/volume_key_service.dart';
import 'overlay_toast.dart';
import 'card_buttons.dart' show CardSpeedSheet;
@@ -115,6 +116,8 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
int _fontSize = 16;
double _lineHeight = 1.4;
int _marginH = 16; // left + right
+ String _volumeNavMode = 'off';
+ bool _volumeNavWhilePlaying = false;
int _marginV = 16; // top + bottom
// Page layout: auto shows two pages on wide screens (tablets), single forces
// one page, two forces a spread. Stored as index 0=auto/1=single/2=two.
@@ -164,8 +167,14 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
PlayerSettings.settingsChanged.addListener(_loadSkipSettings);
_loadSkipSettings();
_loadAudioMeta();
+ _volumeNav.attach();
}
+ late final EreaderVolumeNav _volumeNav = EreaderVolumeNav(
+ onPrev: () => _epubController?.prev(),
+ onNext: () => _epubController?.next(),
+ );
+
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
@@ -276,6 +285,8 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
_spread = _spreadModes[si];
_themeId = await ScopedPrefs.getString(_kTheme) ?? '';
_fontId = await ScopedPrefs.getString(_kFont) ?? 'original';
+ _volumeNavMode = await PlayerSettings.getEreaderVolumeNav();
+ _volumeNavWhilePlaying = await PlayerSettings.getEreaderVolumeNavWhilePlaying();
if (mounted) setState(() {});
}
@@ -437,6 +448,7 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
+ _volumeNav.detach();
_routeAnim?.removeStatusListener(_onRouteAnim);
PlayerSettings.settingsChanged.removeListener(_loadSkipSettings);
// Restore system UI when leaving
@@ -1091,6 +1103,45 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
]),
),
),
+ const SizedBox(height: 16),
+
+ // Volume keys turn pages (normal: up = previous, down = next)
+ Row(children: [
+ Icon(Icons.swap_vert_rounded, size: 20, color: cs.onSurfaceVariant),
+ const SizedBox(width: 12),
+ Expanded(child: Text(l.readerVolumeNav, style: tt.bodyMedium)),
+ ]),
+ const SizedBox(height: 8),
+ SizedBox(
+ width: double.infinity,
+ child: SegmentedButton<String>(
+ showSelectedIcon: false,
+ segments: [
+ ButtonSegment(value: 'off', label: Text(l.readerVolumeNavOff)),
+ ButtonSegment(value: 'normal', label: Text(l.readerVolumeNavNormal)),
+ ButtonSegment(value: 'mirrored', label: Text(l.readerVolumeNavMirrored)),
+ ],
+ selected: {_volumeNavMode},
+ onSelectionChanged: (sel) {
+ final v = sel.first;
+ setSheetState(() {});
+ setState(() => _volumeNavMode = v);
+ PlayerSettings.setEreaderVolumeNav(v);
+ },
+ style: const ButtonStyle(visualDensity: VisualDensity.compact),
+ ),
+ ),
+ if (_volumeNavMode != 'off')
+ SwitchListTile(
+ contentPadding: EdgeInsets.zero,
+ title: Text(l.readerVolumeNavWhilePlaying, style: tt.bodyMedium),
+ value: _volumeNavWhilePlaying,
+ onChanged: (v) {
+ setSheetState(() {});
+ setState(() => _volumeNavWhilePlaying = v);
+ PlayerSettings.setEreaderVolumeNavWhilePlaying(v);
+ },
+ ),
],
),
),
diff --git a/lib/widgets/foliate_reader_view.dart b/lib/widgets/foliate_reader_view.dart
index 99fd7bcd..f8a4427c 100644
--- a/lib/widgets/foliate_reader_view.dart
+++ b/lib/widgets/foliate_reader_view.dart
@@ -10,6 +10,7 @@ import '../services/api_service.dart';
import '../services/audio_player_service.dart';
import '../services/ebook_cache.dart';
import '../services/foliate_book_server.dart';
+import '../services/volume_key_service.dart';
/// A table-of-contents entry from foliate-js.
class _TocItem {
@@ -88,10 +89,17 @@ class _FoliateReaderViewState extends State<FoliateReaderView> {
_lib = context.read<LibraryProvider>();
_loadInitialCfi();
_prepare();
+ _volumeNav.attach();
}
+ late final EreaderVolumeNav _volumeNav = EreaderVolumeNav(
+ onPrev: () => _controller?.evaluateJavascript(source: 'window.AbsorbReader.prev();'),
+ onNext: () => _controller?.evaluateJavascript(source: 'window.AbsorbReader.next();'),
+ );
+
@override
void dispose() {
+ _volumeNav.detach();
_setFullscreen(false);
_flushProgress();
_server.stop();
diff --git a/lib/widgets/pdf_reader_view.dart b/lib/widgets/pdf_reader_view.dart
index 3852e365..f9ff922c 100644
--- a/lib/widgets/pdf_reader_view.dart
+++ b/lib/widgets/pdf_reader_view.dart
@@ -8,6 +8,7 @@ import '../providers/auth_provider.dart';
import '../providers/library_provider.dart';
import '../services/api_service.dart';
import '../services/ebook_cache.dart';
+import '../services/volume_key_service.dart';
/// Full-screen PDF reader. PDFs are fixed-layout, so this is deliberately
/// simpler than the EPUB reader (no font/margin/theme reflow): page scrolling,
@@ -56,10 +57,23 @@ class _PdfReaderViewState extends State<PdfReaderView> {
_lib = context.read<LibraryProvider>();
_loadInitialPage();
_open();
+ _volumeNav.attach();
}
+ late final EreaderVolumeNav _volumeNav = EreaderVolumeNav(
+ onPrev: () {
+ if (_page > 1) _controller.goToPage(pageNumber: _page - 1);
+ },
+ onNext: () {
+ if (_pageCount <= 0 || _page < _pageCount) {
+ _controller.goToPage(pageNumber: _page + 1);
+ }
+ },
+ );
+
@override
void dispose() {
+ _volumeNav.detach();
_setFullscreen(false);
_flushProgress();
super.dispose();