Add Shuffle to collection and download lists in Android Auto
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5597c4c235
commit
30daf267a0
1 file changed
+28
-5
@@ -60,7 +60,7 @@ private const val RECENT_SHOWN = 30
|
|||||||
private const val RESTORE_MS = 20_000L
|
private const val RESTORE_MS = 20_000L
|
||||||
|
|
||||||
// Media ids. Folders are plain names or `kind:id`; a playable track is `t|<folder>|<index>`,
|
// Media ids. Folders are plain names or `kind:id`; a playable track is `t|<folder>|<index>`,
|
||||||
// so playing it can queue the rest of its folder.
|
// so playing it can queue the rest of its folder, and `s|<folder>` plays the folder shuffled.
|
||||||
private const val ROOT = "root"
|
private const val ROOT = "root"
|
||||||
private const val WAVE = "wave"
|
private const val WAVE = "wave"
|
||||||
private const val HOME = "home"
|
private const val HOME = "home"
|
||||||
@@ -72,6 +72,7 @@ private const val PLAYLISTS = "playlists"
|
|||||||
private const val ALBUMS = "albums"
|
private const val ALBUMS = "albums"
|
||||||
private const val STATION = "station|"
|
private const val STATION = "station|"
|
||||||
private const val TRACK = "t|"
|
private const val TRACK = "t|"
|
||||||
|
private const val SHUFFLE = "s|"
|
||||||
private const val SEARCH = "search:"
|
private const val SEARCH = "search:"
|
||||||
private const val DOWNLOADS = "downloads"
|
private const val DOWNLOADS = "downloads"
|
||||||
/** Every downloaded track. */
|
/** Every downloaded track. */
|
||||||
@@ -264,8 +265,8 @@ class Library(private val context: Context, private val state: AppState, private
|
|||||||
Futures.immediateFuture(LibraryResult.ofItemList(searches[query].orEmpty().page(page, pageSize), params))
|
Futures.immediateFuture(LibraryResult.ofItemList(searches[query].orEmpty().page(page, pageSize), params))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plays what the car picked: a station, a track with the rest of its folder, or, for a
|
* Plays what the car picked: a station, a track with the rest of its folder, a folder
|
||||||
* spoken request, the best tracks for it (My Wave when nothing was named).
|
* shuffled, or, for a spoken request, the best tracks for it (My Wave when nothing was named).
|
||||||
*/
|
*/
|
||||||
override fun onSetMediaItems(
|
override fun onSetMediaItems(
|
||||||
mediaSession: MediaSession,
|
mediaSession: MediaSession,
|
||||||
@@ -281,9 +282,10 @@ class Library(private val context: Context, private val state: AppState, private
|
|||||||
id.startsWith(STATION) -> state.playWave(id.removePrefix(STATION))
|
id.startsWith(STATION) -> state.playWave(id.removePrefix(STATION))
|
||||||
id.startsWith(TRACK) -> {
|
id.startsWith(TRACK) -> {
|
||||||
val (folder, index) = id.removePrefix(TRACK).let { it.substringBeforeLast('|') to it.substringAfterLast('|').toIntOrNull() }
|
val (folder, index) = id.removePrefix(TRACK).let { it.substringBeforeLast('|') to it.substringAfterLast('|').toIntOrNull() }
|
||||||
val tracks = shown[folder] ?: offlineTracks(folder) ?: client()?.let { tracks(it, folder) }
|
val tracks = folderTracks(folder)
|
||||||
if (tracks != null && index != null && index in tracks.indices) state.playTracks(tracks, index)
|
if (tracks != null && index != null && index in tracks.indices) state.playTracks(tracks, index)
|
||||||
}
|
}
|
||||||
|
id.startsWith(SHUFFLE) -> folderTracks(id.removePrefix(SHUFFLE))?.takeIf { it.isNotEmpty() }?.let { state.playTracks(it, 0, shuffle = true) }
|
||||||
id.isEmpty() -> playFromSearch(state, query)
|
id.isEmpty() -> playFromSearch(state, query)
|
||||||
}
|
}
|
||||||
MediaItemsWithStartPosition(mediaItems, startIndex, C.TIME_UNSET)
|
MediaItemsWithStartPosition(mediaItems, startIndex, C.TIME_UNSET)
|
||||||
@@ -324,11 +326,20 @@ class Library(private val context: Context, private val state: AppState, private
|
|||||||
else -> tracks(client, parent)?.let { tracks -> trackItems(parent, tracks) }
|
else -> tracks(client, parent)?.let { tracks -> trackItems(parent, tracks) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** A folder's tracks; the collection's and the downloads' start with Shuffle, as in the app. */
|
||||||
private fun trackItems(folder: String, tracks: List<Track>): List<MediaItem> {
|
private fun trackItems(folder: String, tracks: List<Track>): List<MediaItem> {
|
||||||
shown[folder] = tracks
|
shown[folder] = tracks
|
||||||
return tracks.mapIndexed { i, t -> track(folder, i, t) }
|
val rows = tracks.mapIndexed { i, t -> track(folder, i, t) }
|
||||||
|
return if (shuffles(folder) && tracks.size > 1) listOf(shuffle(folder)) + rows else rows
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Liked tracks, albums, playlists and downloads; not the recent, the chart or search results. */
|
||||||
|
private fun shuffles(folder: String): Boolean =
|
||||||
|
folder == LIKED || folder == OFFLINE_ALL || folder.startsWith(OFFLINE) || folder.startsWith("album:") || folder.startsWith("playlist:")
|
||||||
|
|
||||||
|
/** A folder's tracks as last shown, or fetched again when the car plays it cold. */
|
||||||
|
private suspend fun folderTracks(folder: String): List<Track>? = shown[folder] ?: offlineTracks(folder) ?: client()?.let { tracks(it, folder) }
|
||||||
|
|
||||||
/** Every downloaded track, then the albums and playlists with any of theirs on the phone. */
|
/** Every downloaded track, then the albums and playlists with any of theirs on the phone. */
|
||||||
private fun downloads(): List<MediaItem> {
|
private fun downloads(): List<MediaItem> {
|
||||||
val d = state.downloads
|
val d = state.downloads
|
||||||
@@ -402,6 +413,18 @@ class Library(private val context: Context, private val state: AppState, private
|
|||||||
)
|
)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
|
private fun shuffle(folder: String) = MediaItem.Builder()
|
||||||
|
.setMediaId(SHUFFLE + folder)
|
||||||
|
.setMediaMetadata(
|
||||||
|
MediaMetadata.Builder()
|
||||||
|
.setTitle(context.getString(R.string.action_shuffle))
|
||||||
|
.setIsBrowsable(false)
|
||||||
|
.setIsPlayable(true)
|
||||||
|
.setMediaType(MediaMetadata.MEDIA_TYPE_PLAYLIST)
|
||||||
|
.build(),
|
||||||
|
)
|
||||||
|
.build()
|
||||||
|
|
||||||
private fun track(folder: String, index: Int, t: Track) = MediaItem.Builder()
|
private fun track(folder: String, index: Int, t: Track) = MediaItem.Builder()
|
||||||
.setMediaId("$TRACK$folder|$index")
|
.setMediaId("$TRACK$folder|$index")
|
||||||
.setMediaMetadata(
|
.setMediaMetadata(
|
||||||
|
|||||||
Reference in new issue
Block a user