Skip to content

Commit

Permalink
[Playback resumption] Provide cached artwork boot. Tidy up.
Browse files Browse the repository at this point in the history
  • Loading branch information
dturner committed Aug 18, 2020
1 parent 01d6a4c commit ce5d42c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,7 @@ open class MusicService : MediaBrowserServiceCompat() {
* If the caller requests the recent root, return the most recently played song.
*/
if (parentMediaId == UAMP_RECENT_ROOT) {
val recentSong = storage.loadRecentSong()
val recentSongList = if (recentSong != null) listOf(recentSong) else null
result.sendResult(recentSongList)
result.sendResult(storage.loadRecentSong()?.let { song -> listOf(song) })
} else {
// If the media source is ready, the results will be set synchronously here.
val resultsSent = mediaSource.whenReady { successfullyInitialized ->
Expand Down Expand Up @@ -578,8 +576,9 @@ open class MusicService : MediaBrowserServiceCompat() {
// If playing, save the current media item in persistent
// storage so that playback can be resumed between device reboots.
// Search for "media resumption" for more information.
storage.saveRecentSong(currentPlaylistItems[currentPlayer.currentWindowIndex].description)
//storage.saveRecentMediaItemId(currentPlaylistItems[currentPlayer.currentWindowIndex].description.mediaId)
serviceScope.launch{
storage.saveRecentSong(currentPlaylistItems[currentPlayer.currentWindowIndex].description)
}
} else {
// If playback is paused we remove the foreground state which allows the
// notification to be dismissed. An alternative would be to provide a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ import android.net.Uri
import android.support.v4.media.MediaBrowserCompat
import android.support.v4.media.MediaBrowserCompat.MediaItem.FLAG_PLAYABLE
import android.support.v4.media.MediaDescriptionCompat
import com.bumptech.glide.Glide
import com.example.android.uamp.media.extensions.asAlbumArtContentUri
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class PersistentStorage private constructor(val context: Context) {

/**
* Store any data which must persist between restarts, such as the most recently played song
* , in shared preferences.
* Store any data which must persist between restarts, such as the most recently played song.
*/
private var preferences: SharedPreferences = context.getSharedPreferences(
PREFERENCES_NAME,
Expand All @@ -45,13 +48,26 @@ class PersistentStorage private constructor(val context: Context) {
}
}

fun saveRecentSong(description: MediaDescriptionCompat) {
preferences.edit()
.putString(RECENT_SONG_MEDIA_ID_KEY, description.mediaId)
.putString(RECENT_SONG_TITLE_KEY, description.title.toString())
.putString(RECENT_SONG_SUBTITLE_KEY, description.subtitle.toString())
.putString(RECENT_SONG_ICON_URI_KEY, description.iconUri.toString())
.apply()
suspend fun saveRecentSong(description: MediaDescriptionCompat) {
withContext(Dispatchers.IO) {

/**
* After booting, Android will attempt to build static media controls for the most
* recently played song. Artwork for these media controls should not be loaded
* from the network as it may be too slow or unavailable immediately after boot. Instead
* we convert the iconUri to point to the Glide on-disk cache.
*/
val localIconUri = Glide.with(context).asFile().load(description.iconUri)
.submit(NOTIFICATION_LARGE_ICON_SIZE, NOTIFICATION_LARGE_ICON_SIZE).get()
.asAlbumArtContentUri()

preferences.edit()
.putString(RECENT_SONG_MEDIA_ID_KEY, description.mediaId)
.putString(RECENT_SONG_TITLE_KEY, description.title.toString())
.putString(RECENT_SONG_SUBTITLE_KEY, description.subtitle.toString())
.putString(RECENT_SONG_ICON_URI_KEY, localIconUri.toString())
.apply()
}
}

fun loadRecentSong(): MediaBrowserCompat.MediaItem? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ class UampNotificationManager(
}
}

private const val NOTIFICATION_LARGE_ICON_SIZE = 144 // px
const val NOTIFICATION_LARGE_ICON_SIZE = 144 // px

private val glideOptions = RequestOptions()
.fallback(R.drawable.default_art)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.diskCacheStrategy(DiskCacheStrategy.DATA)

private const val MODE_READ_ONLY = "r"

0 comments on commit ce5d42c

Please sign in to comment.