Add like and don't-recommend buttons to the car's Now Playing
The library session offers two custom commands, and its media button preferences show a heart that follows the track's like and a "don't recommend" that dislikes and skips, as from the app's menu. Previous and next keep their slots; Android Auto puts the two at the right edge. Recently played and Chart get their first track's cover, so Auto's grid no longer shows broken images for them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
729b9b8aef
commit
2fac4f94fb
2 files changed
+78
-5
No files matched your search
@@ -15,13 +15,16 @@ import androidx.media3.common.C
|
||||
import androidx.media3.common.MediaItem
|
||||
import androidx.media3.common.MediaMetadata
|
||||
import androidx.media3.common.util.UnstableApi
|
||||
import androidx.media3.session.CommandButton
|
||||
import androidx.media3.session.LibraryResult
|
||||
import androidx.media3.session.MediaConstants
|
||||
import androidx.media3.session.MediaLibraryService.LibraryParams
|
||||
import androidx.media3.session.MediaLibraryService.MediaLibrarySession
|
||||
import androidx.media3.session.MediaSession
|
||||
import androidx.media3.session.MediaSession.MediaItemsWithStartPosition
|
||||
import androidx.media3.session.SessionCommand
|
||||
import androidx.media3.session.SessionError
|
||||
import androidx.media3.session.SessionResult
|
||||
import com.google.common.collect.ImmutableList
|
||||
import com.google.common.util.concurrent.Futures
|
||||
import com.google.common.util.concurrent.ListenableFuture
|
||||
@@ -68,6 +71,10 @@ private const val STATION = "station|"
|
||||
private const val TRACK = "t|"
|
||||
private const val SEARCH = "search:"
|
||||
|
||||
// The buttons beside the car's transport: the heart, and "don't recommend".
|
||||
private val LIKE = SessionCommand("it.panov.musyx.LIKE", Bundle.EMPTY)
|
||||
private val DISLIKE = SessionCommand("it.panov.musyx.DISLIKE", Bundle.EMPTY)
|
||||
|
||||
@OptIn(UnstableApi::class)
|
||||
class Library(private val context: Context, private val state: AppState, private val scope: CoroutineScope) :
|
||||
MediaLibrarySession.Callback {
|
||||
@@ -79,9 +86,10 @@ class Library(private val context: Context, private val state: AppState, private
|
||||
|
||||
/**
|
||||
* Has the car reload whenever someone signs in or out, so it never keeps showing "Sign in"
|
||||
* (or someone else's music). Returns what stops watching.
|
||||
* (or someone else's music), and keeps the heart in step with the track. Returns what
|
||||
* stops watching.
|
||||
*/
|
||||
fun watchSession(session: MediaLibrarySession): () -> Unit {
|
||||
fun watch(session: MediaLibrarySession): () -> Unit {
|
||||
// snapshotFlow hears about changes through Compose's apply notifications, which only the
|
||||
// app's UI sends; with just the car connected, nothing would. Send them here as well.
|
||||
var queued = false
|
||||
@@ -104,12 +112,75 @@ class Library(private val context: Context, private val state: AppState, private
|
||||
session.notifyChildrenChanged(ROOT, Int.MAX_VALUE, null)
|
||||
}
|
||||
}
|
||||
val buttons = scope.launch {
|
||||
snapshotFlow {
|
||||
val track = state.playback.state.track
|
||||
Triple(track?.id, track?.let { state.liked.contains(it.id) }, track?.let { it.id in state.likes.dislikedTracks })
|
||||
}
|
||||
.distinctUntilChanged()
|
||||
.collect { session.setMediaButtonPreferences(buttons()) }
|
||||
}
|
||||
return {
|
||||
job.cancel()
|
||||
buttons.cancel()
|
||||
writes.dispose()
|
||||
}
|
||||
}
|
||||
|
||||
/** Every controller may like and dislike, and starts with the buttons for what's playing. */
|
||||
override fun onConnect(session: MediaSession, controller: MediaSession.ControllerInfo): MediaSession.ConnectionResult {
|
||||
// The library commands must stay: without "get root" a car's browser connection is refused.
|
||||
val library = if (controller.isTrusted) {
|
||||
MediaSession.ConnectionResult.DEFAULT_SESSION_AND_LIBRARY_COMMANDS
|
||||
} else {
|
||||
MediaSession.ConnectionResult.DEFAULT_UNTRUSTED_SESSION_AND_LIBRARY_COMMANDS
|
||||
}
|
||||
return MediaSession.ConnectionResult.AcceptedResultBuilder(session)
|
||||
.setAvailableSessionCommands(library.buildUpon().add(LIKE).add(DISLIKE).build())
|
||||
.setMediaButtonPreferences(buttons())
|
||||
.build()
|
||||
}
|
||||
|
||||
override fun onCustomCommand(
|
||||
session: MediaSession,
|
||||
controller: MediaSession.ControllerInfo,
|
||||
customCommand: SessionCommand,
|
||||
args: Bundle,
|
||||
): ListenableFuture<SessionResult> {
|
||||
val track = state.playback.state.track
|
||||
?: return Futures.immediateFuture(SessionResult(SessionError.ERROR_INVALID_STATE))
|
||||
when (customCommand.customAction) {
|
||||
LIKE.customAction -> state.likeTrack(track, !state.liked.contains(track.id))
|
||||
// Disliking the playing track also skips it, as from the app's menu.
|
||||
DISLIKE.customAction -> state.dislikeTrack(track, track.id !in state.likes.dislikedTracks)
|
||||
else -> return Futures.immediateFuture(SessionResult(SessionError.ERROR_NOT_SUPPORTED))
|
||||
}
|
||||
return Futures.immediateFuture(SessionResult(SessionResult.RESULT_SUCCESS))
|
||||
}
|
||||
|
||||
/**
|
||||
* The heart and "don't recommend" for what's playing, asked for right after next; previous
|
||||
* and next keep their slots. Android Auto ignores the slot and puts them at the right edge.
|
||||
* Speech has nothing to recommend.
|
||||
*/
|
||||
private fun buttons(): List<CommandButton> {
|
||||
val track = state.playback.state.track ?: return emptyList()
|
||||
val liked = state.liked.contains(track.id)
|
||||
val heart = CommandButton.Builder(if (liked) CommandButton.ICON_HEART_FILLED else CommandButton.ICON_HEART_UNFILLED)
|
||||
.setDisplayName(context.getString(if (liked) R.string.menu_unlike else R.string.menu_like))
|
||||
.setSessionCommand(LIKE)
|
||||
.setSlots(CommandButton.SLOT_FORWARD_SECONDARY, CommandButton.SLOT_OVERFLOW)
|
||||
.build()
|
||||
if (track.spoken) return listOf(heart)
|
||||
val disliked = track.id in state.likes.dislikedTracks
|
||||
val block = CommandButton.Builder(CommandButton.ICON_BLOCK)
|
||||
.setDisplayName(context.getString(if (disliked) R.string.menu_undislike else R.string.menu_dislike))
|
||||
.setSessionCommand(DISLIKE)
|
||||
.setSlots(CommandButton.SLOT_FORWARD_SECONDARY, CommandButton.SLOT_OVERFLOW)
|
||||
.build()
|
||||
return listOf(heart, block)
|
||||
}
|
||||
|
||||
override fun onGetLibraryRoot(
|
||||
session: MediaLibrarySession,
|
||||
browser: MediaSession.ControllerInfo,
|
||||
@@ -211,9 +282,11 @@ class Library(private val context: Context, private val state: AppState, private
|
||||
}
|
||||
HOME -> {
|
||||
val home = client.home()
|
||||
// Grid tiles without art show a broken image in Auto: these borrow their first track's cover.
|
||||
val latest = state.recent.get()?.firstOrNull() ?: runCatching { client.recentlyPlayed(1).firstOrNull() }.getOrNull()
|
||||
listOf(
|
||||
folder(RECENT, context.getString(R.string.section_recent)),
|
||||
folder(CHART, context.getString(R.string.section_chart)),
|
||||
folder(RECENT, context.getString(R.string.section_recent), latest?.coverUrl(ART_SIZE)),
|
||||
folder(CHART, context.getString(R.string.section_chart), home.chart.firstOrNull()?.track?.coverUrl(ART_SIZE)),
|
||||
) + home.personal.map { playlist(it.playlist) }
|
||||
}
|
||||
COLLECTION -> listOf(
|
||||
|
||||
@@ -33,7 +33,7 @@ class PlaybackService : MediaLibraryService() {
|
||||
session = MediaLibrarySession.Builder(this, app.state.playback.sessionPlayer, library)
|
||||
.setSessionActivity(open)
|
||||
.build()
|
||||
.also { stopWatching = library.watchSession(it) }
|
||||
.also { stopWatching = library.watch(it) }
|
||||
}
|
||||
|
||||
override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaLibrarySession? = session
|
||||
|
||||
Reference in new issue
Block a user