Compare commits

...
Author SHA1 Message Date
advplyr 16da0c909f Add:Sync local progress to server natively #74 2022-01-26 18:03:12 -06:00
2 changed files with 37 additions and 2 deletions
@@ -114,9 +114,36 @@ class AudiobookProgressSyncer constructor(playerNotificationService:PlayerNotifi
} else if (listeningStreamId == "download") { } else if (listeningStreamId == "download") {
// TODO: Save downloaded audiobook progress & send to server if connected // TODO: Save downloaded audiobook progress & send to server if connected
Log.d(tag, "ListeningTimer: Is listening download") 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)) { fun sendStreamSyncData(payload:JSObject, cb: (() -> Unit)) {
var serverUrl = playerNotificationService.getServerUrl() var serverUrl = playerNotificationService.getServerUrl()
var token = playerNotificationService.getUserToken() var token = playerNotificationService.getUserToken()
@@ -127,7 +154,10 @@ class AudiobookProgressSyncer constructor(playerNotificationService:PlayerNotifi
Log.d(tag, "Sync Stream $serverUrl | $token") Log.d(tag, "Sync Stream $serverUrl | $token")
var url = "$serverUrl/api/syncStream" 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 mediaType = "application/json; charset=utf-8".toMediaType()
val requestBody = payload.toString().toRequestBody(mediaType) val requestBody = payload.toString().toRequestBody(mediaType)
val request = Request.Builder().post(requestBody) val request = Request.Builder().post(requestBody)
@@ -648,8 +648,7 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
Log.d(tag, "Playing ${getCurrentBookTitle()} | ${currentPlayer.mediaMetadata.title} | ${currentPlayer.mediaMetadata.displayTitle}") Log.d(tag, "Playing ${getCurrentBookTitle()} | ${currentPlayer.mediaMetadata.title} | ${currentPlayer.mediaMetadata.displayTitle}")
if (player.isPlaying) { if (player.isPlaying) {
audiobookProgressSyncer.start() audiobookProgressSyncer.start()
} } else {
if (!player.isPlaying && audiobookProgressSyncer.listeningTimerRunning) {
audiobookProgressSyncer.stop() audiobookProgressSyncer.stop()
} }
@@ -784,6 +783,12 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
return currentAudiobookStreamData?.id return currentAudiobookStreamData?.id
} }
// The duration stored on the audiobook
fun getAudiobookDuration() : Long {
if (currentAudiobookStreamData == null) return 0L
return currentAudiobookStreamData!!.duration
}
fun getServerUrl(): String { fun getServerUrl(): String {
return audiobookManager.serverUrl return audiobookManager.serverUrl
} }