Add a phone app with Android Auto support

:mobile (it.panov.musyx) is a Material 3 phone app over the shared AppState: Home,
My Wave, search, collection, detail pages, Now Playing with queue and lyrics,
and sign-in through Yandex's page in the browser.

Its playback service is a MediaLibraryService, so Android Auto and Automotive
can browse My Wave and stations, Home picks and the collection, search, and
play by voice. Covers reach the car through a content provider limited to
Yandex hosts. Signed out, the car shows a Sign in button, and it reloads when
the session changes.

The session's playlist is now the current track and PlayerCore's next 50, so a
car's queue screen lists them and picking one jumps there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Yuriy PanovandClaude Opus 5.5 committed 2026-10-23 12:19:27 +06:00
1 parent f004859395
commit 729b9b8aef
89 files changed
+5773 -15

No files matched your search

@@ -10,7 +10,6 @@ import android.os.Looper
import androidx.media3.common.AudioAttributes
import androidx.media3.common.C
import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
import androidx.media3.common.PlaybackException
import androidx.media3.common.Player
import androidx.annotation.OptIn
@@ -91,15 +90,7 @@ class ExoEngine(context: Context, private val token: () -> String?, private val
keys.clear()
val key = stream.key?.let { hexKey(it) }
if (key != null) stream.urls.forEach { keys[it] = key }
val metadata = MediaMetadata.Builder()
.setTitle(track.fullTitle)
.setArtist(track.artistLine)
.setAlbumTitle(track.album?.title)
.setArtworkUri(track.coverUrl(400)?.let(Uri::parse))
.setIsPlayable(true)
.setIsBrowsable(false)
.build()
item = MediaItem.Builder().setMediaId(track.id).setMediaMetadata(metadata)
item = MediaItem.Builder().setMediaId(track.id).setMediaMetadata(track.mediaMetadata())
start(play)
}
@@ -122,6 +122,7 @@ class PlaybackController(
waveStation = core.waveStation,
)
positionMs = core.positionMs
sessionPlayer.queueChanged()
}
// ---- Engine events ----
@@ -1,22 +1,49 @@
// The player that each app's media session wraps.
package it.panov.musyx.player
import android.net.Uri
import androidx.annotation.OptIn
import androidx.media3.common.C
import androidx.media3.common.ForwardingSimpleBasePlayer
import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import com.google.common.util.concurrent.Futures
import com.google.common.util.concurrent.ListenableFuture
import it.panov.musyx.api.Track
/** Upcoming tracks shown to controllers; a long liked list would make every update heavy. */
private const val QUEUE_SHOWN = 50
/** How a track describes itself to the media session, for what's playing and what's next. */
internal fun Track.mediaMetadata(): MediaMetadata = MediaMetadata.Builder()
.setTitle(fullTitle)
.setArtist(artistLine)
.setAlbumTitle(album?.title)
.setArtworkUri(coverUrl(400)?.let(Uri::parse))
.setIsPlayable(true)
.setIsBrowsable(false)
.build()
/**
* The ExoPlayer as the media session sees it. ExoPlayer only ever holds the current
* track, so next and previous are added here and routed to the queue, as are play,
* pause and seeks, so PlayerCore stays the one source of truth.
* pause and seeks, so PlayerCore stays the one source of truth. The playlist controllers
* see is the current track followed by PlayerCore's upcoming ones, so a car's queue
* screen lists them and picking one jumps there.
*/
@OptIn(UnstableApi::class)
class SessionPlayer(player: Player, private val controller: PlaybackController) :
class SessionPlayer(private val player: Player, private val controller: PlaybackController) :
ForwardingSimpleBasePlayer(player) {
/** The upcoming tracks behind the playlist last shown, and their items, so a pick finds its track. */
private var shown: List<Pair<Int, Track>> = emptyList()
private var shownItems: List<MediaItemData> = emptyList()
/** PlayerCore's queue changed: controllers re-read the playlist. */
fun queueChanged() = invalidateState()
override fun getState(): State {
val state = super.getState()
val commands = state.availableCommands.buildUpon()
@@ -26,9 +53,40 @@ class SessionPlayer(player: Player, private val controller: PlaybackController)
Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM,
Player.COMMAND_SEEK_TO_PREVIOUS,
Player.COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM,
Player.COMMAND_SEEK_TO_MEDIA_ITEM,
)
.build()
return state.buildUpon().setAvailableCommands(commands).build()
val builder = state.buildUpon().setAvailableCommands(commands)
val current = player.currentMediaItem
val upcoming = controller.state.upcoming.take(QUEUE_SHOWN)
if (current != null && upcoming.isNotEmpty()) {
builder.setPlaylist(listOf(nowPlaying(current)) + queued(upcoming)).setCurrentMediaItemIndex(0)
} else {
shown = emptyList()
}
return builder.build()
}
/** The track ExoPlayer holds, as ExoPlayer describes it. */
private fun nowPlaying(item: MediaItem): MediaItemData = MediaItemData.Builder("now:${item.mediaId}")
.setMediaItem(item)
.setTracks(player.currentTracks)
.setIsSeekable(player.isCurrentMediaItemSeekable)
.setDurationUs(player.duration.takeIf { it != C.TIME_UNSET }?.times(1000) ?: C.TIME_UNSET)
.build()
private fun queued(upcoming: List<Pair<Int, Track>>): List<MediaItemData> {
if (upcoming != shown) {
shown = upcoming
shownItems = upcoming.map { (pos, track) ->
val item = MediaItem.Builder().setMediaId(track.id).setMediaMetadata(track.mediaMetadata()).build()
MediaItemData.Builder("next:$pos:${track.id}")
.setMediaItem(item)
.setDurationUs(track.durationMs * 1000)
.build()
}
}
return shownItems
}
override fun handleSetPlayWhenReady(playWhenReady: Boolean): ListenableFuture<*> {
@@ -45,10 +103,22 @@ class SessionPlayer(player: Player, private val controller: PlaybackController)
/** The queue prepares tracks itself; play after a stop goes through [handleSetPlayWhenReady]. */
override fun handlePrepare(): ListenableFuture<*> = Futures.immediateVoidFuture()
/**
* Items a controller (Android Auto, say) asks to play never reach ExoPlayer: the library
* session has already started them through PlayerCore by the time they arrive here.
*/
override fun handleSetMediaItems(mediaItems: List<MediaItem>, startIndex: Int, startPositionMs: Long): ListenableFuture<*> =
Futures.immediateVoidFuture()
override fun handleAddMediaItems(index: Int, mediaItems: List<MediaItem>): ListenableFuture<*> = Futures.immediateVoidFuture()
override fun handleSeek(mediaItemIndex: Int, positionMs: Long, seekCommand: Int): ListenableFuture<*> {
when (seekCommand) {
Player.COMMAND_SEEK_TO_NEXT, Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM -> controller.next()
Player.COMMAND_SEEK_TO_PREVIOUS, Player.COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM -> controller.previous()
// A pick from the queue: index 0 is the current track, the rest are `shown` in order.
Player.COMMAND_SEEK_TO_MEDIA_ITEM -> shown.getOrNull(mediaItemIndex - 1)?.let { (pos, _) -> controller.jumpTo(pos) }
?: controller.seek(positionMs.coerceAtLeast(0))
else -> controller.seek(positionMs.coerceAtLeast(0))
}
return Futures.immediateVoidFuture()