diff --git a/android/app/src/main/java/com/audiobookshelf/app/device/DeviceManager.kt b/android/app/src/main/java/com/audiobookshelf/app/device/DeviceManager.kt index 43d378dc..d233ffc4 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/device/DeviceManager.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/device/DeviceManager.kt @@ -26,4 +26,9 @@ object DeviceManager { fun getBase64Id(id:String):String { return android.util.Base64.encodeToString(id.toByteArray(), android.util.Base64.URL_SAFE or android.util.Base64.NO_WRAP) } + + fun getServerConnectionConfig(id:String?):ServerConnectionConfig? { + if (id == null) return null + return deviceData.serverConnectionConfigs.find { it.id == id } + } } diff --git a/android/app/src/main/java/com/audiobookshelf/app/player/PlayerListener.kt b/android/app/src/main/java/com/audiobookshelf/app/player/PlayerListener.kt index 59a73f21..a3d15c0b 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/player/PlayerListener.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/player/PlayerListener.kt @@ -5,7 +5,7 @@ import com.audiobookshelf.app.data.PlayerState import com.google.android.exoplayer2.PlaybackException import com.google.android.exoplayer2.Player -const val PAUSE_LEN_BEFORE_RECHECK = 60000 // 1 minute +const val PAUSE_LEN_BEFORE_RECHECK = 30000 // 30 seconds class PlayerListener(var playerNotificationService:PlayerNotificationService) : Player.Listener { var tag = "PlayerListener" @@ -85,7 +85,8 @@ class PlayerListener(var playerNotificationService:PlayerNotificationService) : } // Check if playback session still exists or sync media progress if updated - if (lastPauseTime > PAUSE_LEN_BEFORE_RECHECK) { + val pauseLength: Long = System.currentTimeMillis() - lastPauseTime + if (pauseLength > PAUSE_LEN_BEFORE_RECHECK) { val shouldCarryOn = playerNotificationService.checkCurrentSessionProgress() if (!shouldCarryOn) return } diff --git a/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationService.kt b/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationService.kt index 6bfb5e6f..ce80c1bd 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationService.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationService.kt @@ -509,10 +509,19 @@ class PlayerNotificationService : MediaBrowserServiceCompat() { } if (playbackSession.isLocal) { + + // Make sure this connection config exists + val serverConnectionConfig = DeviceManager.getServerConnectionConfig(playbackSession.serverConnectionConfigId) + if (serverConnectionConfig == null) { + Log.d(tag, "checkCurrentSessionProgress: Local library item server connection config is not saved ${playbackSession.serverConnectionConfigId}") + return true // carry on + } + // Local playback session check if server has updated media progress Log.d(tag, "checkCurrentSessionProgress: Checking if local media progress was updated on server") - apiHandler.getMediaProgress(playbackSession.libraryItemId!!, playbackSession.episodeId) { mediaProgress -> - if (mediaProgress.lastUpdate > playbackSession.updatedAt && mediaProgress.currentTime != playbackSession.currentTime) { + apiHandler.getMediaProgress(playbackSession.libraryItemId!!, playbackSession.episodeId, serverConnectionConfig) { mediaProgress -> + + if (mediaProgress != null && mediaProgress.lastUpdate > playbackSession.updatedAt && mediaProgress.currentTime != playbackSession.currentTime) { Log.d(tag, "checkCurrentSessionProgress: Media progress was updated since last play time updating from ${playbackSession.currentTime} to ${mediaProgress.currentTime}") mediaProgressSyncer.syncFromServerProgress(mediaProgress) diff --git a/android/app/src/main/java/com/audiobookshelf/app/server/ApiHandler.kt b/android/app/src/main/java/com/audiobookshelf/app/server/ApiHandler.kt index 1bf04c4c..874507a1 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/server/ApiHandler.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/server/ApiHandler.kt @@ -87,13 +87,15 @@ class ApiHandler(var ctx:Context) { override fun onFailure(call: Call, e: IOException) { Log.d(tag, "FAILURE TO CONNECT") e.printStackTrace() - cb(JSObject()) + + val jsobj = JSObject() + jsobj.put("error", "Failed to connect") + cb(jsobj) } override fun onResponse(call: Call, response: Response) { response.use { if (!it.isSuccessful) { -// throw IOException("Unexpected code $response") val jsobj = JSObject() jsobj.put("error", "Unexpected code $response") cb(jsobj) @@ -269,11 +271,18 @@ class ApiHandler(var ctx:Context) { } } - fun getMediaProgress(libraryItemId:String, episodeId:String?, cb: (MediaProgress) -> Unit) { + fun getMediaProgress(libraryItemId:String, episodeId:String?, serverConnectionConfig:ServerConnectionConfig?, cb: (MediaProgress?) -> Unit) { val endpoint = if(episodeId.isNullOrEmpty()) "/api/me/progress/$libraryItemId" else "/api/me/progress/$libraryItemId/$episodeId" - getRequest(endpoint, null, null) { - val progress = jacksonMapper.readValue(it.toString()) - cb(progress) + + // TODO: Using ping client here allows for shorter timeout (3 seconds), maybe rename or make diff client for requests requiring quicker response + getRequest(endpoint, pingClient, serverConnectionConfig) { + if (it.has("error")) { + Log.e(tag, "getMediaProgress: Failed to get progress") + cb(null) + } else { + val progress = jacksonMapper.readValue(it.toString()) + cb(progress) + } } }