Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.
Kotlin alternativeandroidandroid-applicationclientglassmorphisminnertubelibrespotliquid-glassspotifyspotify-connectspotifyapiyoutube-musicyoutube-playerytdlnis
Give the car and the shade their own buttons
Leloniod640c303879c
diff --git a/app/src/main/java/dev/lelonio/square/playback/MediaBrowseTree.kt b/app/src/main/java/dev/lelonio/square/playback/MediaBrowseTree.ktindex 3c24f09..766d9a0 100644--- a/app/src/main/java/dev/lelonio/square/playback/MediaBrowseTree.kt+++ b/app/src/main/java/dev/lelonio/square/playback/MediaBrowseTree.kt@@ -9,7 +9,10 @@ import androidx.media3.common.util.UnstableApiimport androidx.media3.session.LibraryResultimport androidx.media3.session.MediaLibraryService.LibraryParamsimport androidx.media3.session.MediaLibraryService.MediaLibrarySession+import androidx.media3.session.CommandButtonimport androidx.media3.session.MediaSession+import androidx.media3.session.SessionCommand+import androidx.media3.session.SessionResultimport com.google.common.collect.ImmutableListimport com.google.common.util.concurrent.Futuresimport com.google.common.util.concurrent.ListenableFuture@@ -24,6 +27,7 @@ import dev.lelonio.square.ui.EXTRA_CONTEXT_ORDEREDimport dev.lelonio.square.ui.EXTRA_CONTEXT_URIimport kotlinx.coroutines.CoroutineScopeimport kotlinx.coroutines.delay+import kotlinx.coroutines.launchimport kotlinx.coroutines.guava.futureimport kotlinx.coroutines.sync.Muteximport kotlinx.coroutines.sync.withLock@@ -72,6 +76,140 @@ class MediaBrowseTree(/** One catalogue read at a time: the car fires children requests in parallel. */private val lock = Mutex()+ /**+ * The buttons the car draws beside play and skip.+ *+ * Android Auto shows the transport it knows about and nothing else: shuffle+ * and repeat are the player's own modes, and a session that does not offer+ * them as commands simply has no way to say so. They are declared per+ * controller rather than for everyone, so the phone's notification keeps+ * its own two buttons instead of growing four.+ *+ * The icons say what pressing will show, not what pressing does: this is+ * how every other player behaves, and the alternative is a control whose+ * picture disagrees with the thing it is describing.+ */+ @JvmOverloads+ fun layoutFor(+ player: androidx.media3.common.Player,+ /**+ * The shade has room for two buttons beside the transport and is looked+ * at rather than operated: repeat is a setting people change once,+ * while radio is a thing to do with the song on screen. The car keeps+ * repeat, because there it is the mode a driver actually reaches for.+ */+ radioInsteadOfRepeat: Boolean = false,+ ): ImmutableList<CommandButton> =+ ImmutableList.of(+ CommandButton.Builder(+ if (player.shuffleModeEnabled) {+ CommandButton.ICON_SHUFFLE_ON+ } else {+ CommandButton.ICON_SHUFFLE_OFF+ },+ )+ .setSessionCommand(SessionCommand(CMD_SHUFFLE, Bundle.EMPTY))+ .setDisplayName(strings.getString(R.string.shuffle))+ .build(),+ if (radioInsteadOfRepeat) {+ CommandButton.Builder(CommandButton.ICON_RADIO)+ .setSessionCommand(SessionCommand(CMD_RADIO, Bundle.EMPTY))+ .setDisplayName(strings.getString(R.string.radio))+ .build()+ } else {+ CommandButton.Builder(+ when (player.repeatMode) {+ androidx.media3.common.Player.REPEAT_MODE_ONE ->+ CommandButton.ICON_REPEAT_ONE+ androidx.media3.common.Player.REPEAT_MODE_ALL ->+ CommandButton.ICON_REPEAT_ALL+ else -> CommandButton.ICON_REPEAT_OFF+ },+ )+ .setSessionCommand(SessionCommand(CMD_REPEAT, Bundle.EMPTY))+ .setDisplayName(strings.getString(R.string.repeat))+ .build()+ },+ )++ override fun onConnect(+ session: MediaSession,+ controller: MediaSession.ControllerInfo,+ ): MediaSession.ConnectionResult {+ val commands = MediaSession.ConnectionResult.DEFAULT_SESSION_AND_LIBRARY_COMMANDS+ .buildUpon()+ .add(SessionCommand(CMD_SHUFFLE, Bundle.EMPTY))+ .add(SessionCommand(CMD_REPEAT, Bundle.EMPTY))+ .add(SessionCommand(CMD_RADIO, Bundle.EMPTY))+ .build()++ return MediaSession.ConnectionResult.AcceptedResultBuilder(session)+ .setAvailableSessionCommands(commands)+ .setCustomLayout(+ layoutFor(+ session.player,+ radioInsteadOfRepeat = session.isMediaNotificationController(controller),+ ),+ )+ .build()+ }++ override fun onCustomCommand(+ session: MediaSession,+ controller: MediaSession.ControllerInfo,+ customCommand: SessionCommand,+ args: Bundle,+ ): ListenableFuture<SessionResult> {+ val player = session.player+ when (customCommand.customAction) {+ CMD_SHUFFLE -> player.shuffleModeEnabled = !player.shuffleModeEnabled+ CMD_REPEAT -> player.repeatMode = when (player.repeatMode) {+ androidx.media3.common.Player.REPEAT_MODE_OFF ->+ androidx.media3.common.Player.REPEAT_MODE_ALL+ androidx.media3.common.Player.REPEAT_MODE_ALL ->+ androidx.media3.common.Player.REPEAT_MODE_ONE+ else -> androidx.media3.common.Player.REPEAT_MODE_OFF+ }+ CMD_RADIO -> {+ startRadio(session)+ return Futures.immediateFuture(SessionResult(SessionResult.RESULT_SUCCESS))+ }++ else -> return Futures.immediateFuture(+ SessionResult(SessionResult.RESULT_ERROR_NOT_SUPPORTED),+ )+ }+ // Redrawn at once. The button carries the state, so a press that left+ // the old icon up read as a press that had not landed.+ session.setCustomLayout(layoutFor(player))+ return Futures.immediateFuture(SessionResult(SessionResult.RESULT_SUCCESS))+ }++ /**+ * A station from what is playing, without a screen to ask anything of.+ *+ * The same list the player's own radio button builds: Spotify assembles it+ * and this asks for it. If it will not, the queue is left alone rather than+ * replaced with something nobody chose.+ */+ private fun startRadio(session: MediaSession) {+ val player = session.player+ val uri = player.currentMediaItem?.mediaId ?: return+ if (!uri.startsWith("spotify:track:")) return++ scope.launch {+ val station = "spotify:station:track:${uri.substringAfterLast(':')}"+ val tracks = runCatching { Catalog.tracks(Catalog.contextTrackUris(station)) }+ .onFailure { android.util.Log.w(TAG, "no station for $uri: $it") }+ .getOrDefault(emptyList())+ if (tracks.isEmpty()) return@launch++ player.setMediaItems(tracks.map { queueItem(it, strings.getString(R.string.radio)) })+ player.prepare()+ player.play()+ }+ }+override fun onGetLibraryRoot(session: MediaLibrarySession,browser: MediaSession.ControllerInfo,@@ -388,6 +526,11 @@ class MediaBrowseTree(private companion object {const val TAG = "MediaBrowseTree"+ /** The two modes the car cannot reach through the standard transport. */+ const val CMD_SHUFFLE = "dev.lelonio.square.SHUFFLE"+ const val CMD_REPEAT = "dev.lelonio.square.REPEAT"+ const val CMD_RADIO = "dev.lelonio.square.RADIO"+const val ID_ROOT = "sq/root"const val ID_PLAYLISTS = "sq/playlists"const val ID_RECENT = "sq/recent"diff --git a/app/src/main/java/dev/lelonio/square/playback/PlaybackService.kt b/app/src/main/java/dev/lelonio/square/playback/PlaybackService.ktindex 6d1950f..2015e65 100644--- a/app/src/main/java/dev/lelonio/square/playback/PlaybackService.kt+++ b/app/src/main/java/dev/lelonio/square/playback/PlaybackService.kt@@ -80,7 +80,8 @@ class PlaybackService : MediaLibraryService() {crossfade = container.crossfadeplayer = buildPlayer(container.preferences.backend.value)- session = MediaLibrarySession.Builder(this, player, MediaBrowseTree(this, scope))+ val browseTree = MediaBrowseTree(this, scope)+ session = MediaLibrarySession.Builder(this, player, browseTree)// Without this the notification is inert to a tap: Media3 has no way// to know which activity owns the session. `SINGLE_TOP` so an app// already running comes forward rather than starting a second copy@@ -113,6 +114,33 @@ class PlaybackService : MediaLibraryService() {.apply { setSmallIcon(dev.lelonio.square.R.drawable.ic_notification) },)+ // The car's shuffle and repeat buttons carry their own state, so they+ // are redrawn whenever the mode changes here — from the app, from a+ // headset, or from another Connect device.+ player.addListener(object : androidx.media3.common.Player.Listener {+ override fun onShuffleModeEnabledChanged(shuffleModeEnabled: Boolean) = redrawButtons()++ override fun onRepeatModeChanged(repeatMode: Int) = redrawButtons()++ /**+ * Per controller, because the two layouts differ: the shade shows+ * radio where the car shows repeat, and one blanket update would+ * hand every controller the same pair.+ */+ private fun redrawButtons() {+ val live = session ?: return+ live.connectedControllers.forEach { controller ->+ live.setCustomLayout(+ controller,+ browseTree.layoutFor(+ player,+ radioInsteadOfRepeat = live.isMediaNotificationController(controller),+ ),+ )+ }+ }+ })+// Already read by the application object, and harmless twice: `load`// returns at once once the file is open.AudioEffects.load(this)