From 30daf267a0a4b90093da0f264ce16ac7201b08b5 Mon Sep 17 00:00:00 2001 From: Yuriy Panov Date: Wed, 4 Nov 2026 15:08:45 +0600 Subject: [PATCH] Add Shuffle to collection and download lists in Android Auto Co-Authored-By: Claude Opus 5.5 --- .../java/it/panov/musyx/mobile/Library.kt | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/mobile/src/main/java/it/panov/musyx/mobile/Library.kt b/mobile/src/main/java/it/panov/musyx/mobile/Library.kt index 9377d25..0c4d54d 100644 --- a/mobile/src/main/java/it/panov/musyx/mobile/Library.kt +++ b/mobile/src/main/java/it/panov/musyx/mobile/Library.kt @@ -60,7 +60,7 @@ private const val RECENT_SHOWN = 30 private const val RESTORE_MS = 20_000L // Media ids. Folders are plain names or `kind:id`; a playable track is `t||`, -// so playing it can queue the rest of its folder. +// so playing it can queue the rest of its folder, and `s|` plays the folder shuffled. private const val ROOT = "root" private const val WAVE = "wave" private const val HOME = "home" @@ -72,6 +72,7 @@ private const val PLAYLISTS = "playlists" private const val ALBUMS = "albums" private const val STATION = "station|" private const val TRACK = "t|" +private const val SHUFFLE = "s|" private const val SEARCH = "search:" private const val DOWNLOADS = "downloads" /** 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)) /** - * Plays what the car picked: a station, a track with the rest of its folder, or, for a - * spoken request, the best tracks for it (My Wave when nothing was named). + * Plays what the car picked: a station, a track with the rest of its folder, a folder + * shuffled, or, for a spoken request, the best tracks for it (My Wave when nothing was named). */ override fun onSetMediaItems( 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(TRACK) -> { 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) } + id.startsWith(SHUFFLE) -> folderTracks(id.removePrefix(SHUFFLE))?.takeIf { it.isNotEmpty() }?.let { state.playTracks(it, 0, shuffle = true) } id.isEmpty() -> playFromSearch(state, query) } 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) } } + /** A folder's tracks; the collection's and the downloads' start with Shuffle, as in the app. */ private fun trackItems(folder: String, tracks: List): List { 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? = shown[folder] ?: offlineTracks(folder) ?: client()?.let { tracks(it, folder) } + /** Every downloaded track, then the albums and playlists with any of theirs on the phone. */ private fun downloads(): List { val d = state.downloads @@ -402,6 +413,18 @@ class Library(private val context: Context, private val state: AppState, private ) .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() .setMediaId("$TRACK$folder|$index") .setMediaMetadata(