From 16da0c909f81d89b4757b7e258f1be2ca37271ab Mon Sep 17 00:00:00 2001 From: advplyr Date: Wed, 26 Jan 2022 18:03:12 -0600 Subject: [PATCH] Add:Sync local progress to server natively #74 --- .../app/AudiobookProgressSyncer.kt | 30 +++++++++++++++++++ .../app/PlayerNotificationService.kt | 9 ++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/android/app/src/main/java/com/audiobookshelf/app/AudiobookProgressSyncer.kt b/android/app/src/main/java/com/audiobookshelf/app/AudiobookProgressSyncer.kt index f628f4c7..ff6b19d1 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/AudiobookProgressSyncer.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/AudiobookProgressSyncer.kt @@ -114,9 +114,36 @@ class AudiobookProgressSyncer constructor(playerNotificationService:PlayerNotifi } else if (listeningStreamId == "download") { // TODO: Save downloaded audiobook progress & send to server if connected Log.d(tag, "ListeningTimer: Is listening download") + + // Send sync data only for local books + var syncData: JSObject = JSObject() + var duration = playerNotificationService.getAudiobookDuration() / 1000 + var currentTime = playerNotificationService.getCurrentTime() / 1000 + syncData.put("totalDuration", duration) + syncData.put("currentTime", currentTime) + syncData.put("progress", if (duration > 0) (currentTime / duration) else 0) + syncData.put("isRead", false) + syncData.put("lastUpdate", System.currentTimeMillis()) + syncData.put("audiobookId", listeningBookId) + sendLocalSyncData(syncData) { + Log.d(tag, "Local sync done") + } } } + fun sendLocalSyncData(payload:JSObject, cb: (() -> Unit)) { + var serverUrl = playerNotificationService.getServerUrl() + var token = playerNotificationService.getUserToken() + + if (serverUrl == "" || token == "") { + return + } + + Log.d(tag, "Sync Local $serverUrl | $token") + var url = "$serverUrl/api/syncLocal" + sendServerRequest(url, token, payload, cb) + } + fun sendStreamSyncData(payload:JSObject, cb: (() -> Unit)) { var serverUrl = playerNotificationService.getServerUrl() var token = playerNotificationService.getUserToken() @@ -127,7 +154,10 @@ class AudiobookProgressSyncer constructor(playerNotificationService:PlayerNotifi Log.d(tag, "Sync Stream $serverUrl | $token") var url = "$serverUrl/api/syncStream" + sendServerRequest(url, token, payload, cb) + } + fun sendServerRequest(url:String, token:String, payload:JSObject, cb: () -> Unit) { val mediaType = "application/json; charset=utf-8".toMediaType() val requestBody = payload.toString().toRequestBody(mediaType) val request = Request.Builder().post(requestBody) diff --git a/android/app/src/main/java/com/audiobookshelf/app/PlayerNotificationService.kt b/android/app/src/main/java/com/audiobookshelf/app/PlayerNotificationService.kt index c4b524fe..23007805 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/PlayerNotificationService.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/PlayerNotificationService.kt @@ -648,8 +648,7 @@ class PlayerNotificationService : MediaBrowserServiceCompat() { Log.d(tag, "Playing ${getCurrentBookTitle()} | ${currentPlayer.mediaMetadata.title} | ${currentPlayer.mediaMetadata.displayTitle}") if (player.isPlaying) { audiobookProgressSyncer.start() - } - if (!player.isPlaying && audiobookProgressSyncer.listeningTimerRunning) { + } else { audiobookProgressSyncer.stop() } @@ -784,6 +783,12 @@ class PlayerNotificationService : MediaBrowserServiceCompat() { return currentAudiobookStreamData?.id } + // The duration stored on the audiobook + fun getAudiobookDuration() : Long { + if (currentAudiobookStreamData == null) return 0L + return currentAudiobookStreamData!!.duration + } + fun getServerUrl(): String { return audiobookManager.serverUrl }