From df5409422100eba4e3021a5fa6b5917dc0cfc47c Mon Sep 17 00:00:00 2001 From: advplyr Date: Sun, 8 Jan 2023 17:30:57 -0600 Subject: [PATCH] Fix:Android auto play downloaded media if available #385 --- .../audiobookshelf/app/data/DataClasses.kt | 23 +++++++++++++++---- .../audiobookshelf/app/data/LibraryItem.kt | 13 +++++++++-- .../app/data/LocalLibraryItem.kt | 4 ++++ .../audiobookshelf/app/media/MediaManager.kt | 9 ++++++++ .../app/player/PlayerNotificationService.kt | 13 +++++++++++ 5 files changed, 55 insertions(+), 7 deletions(-) diff --git a/android/app/src/main/java/com/audiobookshelf/app/data/DataClasses.kt b/android/app/src/main/java/com/audiobookshelf/app/data/DataClasses.kt index e3d039ada0..7c2b00f619 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/data/DataClasses.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/data/DataClasses.kt @@ -52,7 +52,8 @@ class Podcast( // Add new episodes audioTracks.forEach { at -> if (episodes?.find{ it.audioTrack?.localFileId == at.localFileId } == null) { - val newEpisode = PodcastEpisode("local_ep_" + at.localFileId,(episodes?.size ?: 0) + 1,null,null,at.title,null,null,null, null, null, at,at.duration,0, null) + val localEpisodeId = "local_ep_" + at.localFileId + val newEpisode = PodcastEpisode(localEpisodeId,(episodes?.size ?: 0) + 1,null,null,at.title,null,null,null, null, null, at,at.duration,0, null, localEpisodeId) episodes?.add(newEpisode) } } @@ -65,7 +66,8 @@ class Podcast( } @JsonIgnore override fun addAudioTrack(audioTrack:AudioTrack) { - val newEpisode = PodcastEpisode("local_ep_" + audioTrack.localFileId,(episodes?.size ?: 0) + 1,null,null,audioTrack.title,null,null,null, null, null,audioTrack,audioTrack.duration,0, null) + val localEpisodeId = "local_ep_" + audioTrack.localFileId + val newEpisode = PodcastEpisode(localEpisodeId,(episodes?.size ?: 0) + 1,null,null,audioTrack.title,null,null,null, null, null,audioTrack,audioTrack.duration,0, null, localEpisodeId) episodes?.add(newEpisode) var index = 1 @@ -93,7 +95,8 @@ class Podcast( @JsonIgnore fun addEpisode(audioTrack:AudioTrack, episode:PodcastEpisode):PodcastEpisode { - val newEpisode = PodcastEpisode("local_ep_" + episode.id,(episodes?.size ?: 0) + 1,episode.episode,episode.episodeType,episode.title,episode.subtitle,episode.description,null,null,null,audioTrack,audioTrack.duration,0, episode.id) + val localEpisodeId = "local_ep_" + episode.id + val newEpisode = PodcastEpisode(localEpisodeId,(episodes?.size ?: 0) + 1,episode.episode,episode.episodeType,episode.title,episode.subtitle,episode.description,null,null,null,audioTrack,audioTrack.duration,0, episode.id, localEpisodeId) episodes?.add(newEpisode) var index = 1 @@ -249,7 +252,8 @@ data class PodcastEpisode( var audioTrack:AudioTrack?, var duration:Double?, var size:Long?, - var serverEpisodeId:String? // For local podcasts to match with server podcasts + var serverEpisodeId:String?, // For local podcasts to match with server podcasts + var localEpisodeId:String? // For Android Auto server episodes with local copy ) { @JsonIgnore fun getMediaDescription(libraryItem:LibraryItemWrapper, progress:MediaProgressWrapper?, ctx: Context?): MediaDescriptionCompat { @@ -260,6 +264,14 @@ data class PodcastEpisode( } val extras = Bundle() + + if (localEpisodeId != null) { + extras.putLong( + MediaDescriptionCompat.EXTRA_DOWNLOAD_STATUS, + MediaDescriptionCompat.STATUS_DOWNLOADED + ) + } + if (progress != null) { if (progress.isFinished) { extras.putInt( @@ -283,8 +295,9 @@ data class PodcastEpisode( } val libraryItemDescription = libraryItem.getMediaDescription(null, ctx) + val mediaId = localEpisodeId ?: id val mediaDescriptionBuilder = MediaDescriptionCompat.Builder() - .setMediaId(id) + .setMediaId(mediaId) .setTitle(title) .setIconUri(coverUri) .setSubtitle(libraryItemDescription.title) diff --git a/android/app/src/main/java/com/audiobookshelf/app/data/LibraryItem.kt b/android/app/src/main/java/com/audiobookshelf/app/data/LibraryItem.kt index 4291a0f08b..b0e9a5a008 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/data/LibraryItem.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/data/LibraryItem.kt @@ -30,7 +30,8 @@ class LibraryItem( var mediaType:String, var media:MediaType, var libraryFiles:MutableList?, - var userMediaProgress:MediaProgress? // Only included when requesting library item with progress (for downloads) + var userMediaProgress:MediaProgress?, // Only included when requesting library item with progress (for downloads) + var localLibraryItemId:String? // For Android Auto ) : LibraryItemWrapper(id) { @get:JsonIgnore val title get() = media.metadata.title @@ -59,6 +60,13 @@ class LibraryItem( override fun getMediaDescription(progress:MediaProgressWrapper?, ctx: Context?): MediaDescriptionCompat { val extras = Bundle() + if (localLibraryItemId != null) { + extras.putLong( + MediaDescriptionCompat.EXTRA_DOWNLOAD_STATUS, + MediaDescriptionCompat.STATUS_DOWNLOADED + ) + } + if (progress != null) { if (progress.isFinished) { extras.putInt( @@ -81,8 +89,9 @@ class LibraryItem( ) } + val mediaId = localLibraryItemId ?: id return MediaDescriptionCompat.Builder() - .setMediaId(id) + .setMediaId(mediaId) .setTitle(title) .setIconUri(getCoverUri()) .setSubtitle(authorName) diff --git a/android/app/src/main/java/com/audiobookshelf/app/data/LocalLibraryItem.kt b/android/app/src/main/java/com/audiobookshelf/app/data/LocalLibraryItem.kt index 8d884c40f1..28e1d21cd3 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/data/LocalLibraryItem.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/data/LocalLibraryItem.kt @@ -117,6 +117,10 @@ class LocalLibraryItem( } val extras = Bundle() + extras.putLong( + MediaDescriptionCompat.EXTRA_DOWNLOAD_STATUS, + MediaDescriptionCompat.STATUS_DOWNLOADED + ) if (progress != null) { if (progress.isFinished) { extras.putInt( diff --git a/android/app/src/main/java/com/audiobookshelf/app/media/MediaManager.kt b/android/app/src/main/java/com/audiobookshelf/app/media/MediaManager.kt index b83d82f369..ac2a0db602 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/media/MediaManager.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/media/MediaManager.kt @@ -139,6 +139,7 @@ class MediaManager(var apiHandler: ApiHandler, var ctx: Context) { val progress = DeviceManager.dbManager.getLocalMediaProgress("${libraryItemWrapper.id}-${podcastEpisode.id}") val description = podcastEpisode.getMediaDescription(libraryItemWrapper, progress, ctx) + MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_PLAYABLE) } children?.let { cb(children as MutableList) } ?: cb(mutableListOf()) @@ -157,6 +158,14 @@ class MediaManager(var apiHandler: ApiHandler, var ctx: Context) { val children = podcast.episodes?.map { podcastEpisode -> val progress = serverUserMediaProgress.find { it.libraryItemId == libraryItemWrapper.id && it.episodeId == podcastEpisode.id } + + // to show download icon + val localLibraryItem = DeviceManager.dbManager.getLocalLibraryItemByLId(libraryItemWrapper.id) + localLibraryItem?.let { lli -> + val localEpisode = (lli.media as Podcast).episodes?.find { it.serverEpisodeId == podcastEpisode.id } + podcastEpisode.localEpisodeId = localEpisode?.id + } + val description = podcastEpisode.getMediaDescription(libraryItemWrapper, progress, ctx) MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_PLAYABLE) } 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 443978ee62..4872e9821a 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 @@ -939,6 +939,8 @@ class PlayerNotificationService : MediaBrowserServiceCompat() { MediaBrowserCompat.MediaItem(mediaDescription, MediaBrowserCompat.MediaItem.FLAG_BROWSABLE) } else { val progress = mediaManager.serverUserMediaProgress.find { it.libraryItemId == libraryItem.id } + val localLibraryItem = DeviceManager.dbManager.getLocalLibraryItemByLId(libraryItem.id) + libraryItem.localLibraryItemId = localLibraryItem?.id val description = libraryItem.getMediaDescription(progress, ctx) MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_PLAYABLE) } @@ -975,6 +977,14 @@ class PlayerNotificationService : MediaBrowserServiceCompat() { progress = DeviceManager.dbManager.getLocalMediaProgress("${itemInProgress.libraryItemWrapper.id}-${itemInProgress.episode.id}") } else { progress = mediaManager.serverUserMediaProgress.find { it.libraryItemId == itemInProgress.libraryItemWrapper.id && it.episodeId == itemInProgress.episode.id } + + // to show download icon + val localLibraryItem = DeviceManager.dbManager.getLocalLibraryItemByLId(itemInProgress.libraryItemWrapper.id) + localLibraryItem?.let { lli -> + val localEpisode = (lli.media as Podcast).episodes?.find { it.serverEpisodeId == itemInProgress.episode.id } + itemInProgress.episode.localEpisodeId = localEpisode?.id + } + } mediaDescription = itemInProgress.episode.getMediaDescription(itemInProgress.libraryItemWrapper, progress, ctx) } else { @@ -982,6 +992,9 @@ class PlayerNotificationService : MediaBrowserServiceCompat() { progress = DeviceManager.dbManager.getLocalMediaProgress(itemInProgress.libraryItemWrapper.id) } else { progress = mediaManager.serverUserMediaProgress.find { it.libraryItemId == itemInProgress.libraryItemWrapper.id } + + val localLibraryItem = DeviceManager.dbManager.getLocalLibraryItemByLId(itemInProgress.libraryItemWrapper.id) + (itemInProgress.libraryItemWrapper as LibraryItem).localLibraryItemId = localLibraryItem?.id // To show downloaded icon } mediaDescription = itemInProgress.libraryItemWrapper.getMediaDescription(progress, ctx) }