diff --git a/mobile/src/main/java/it/panov/musyx/mobile/ArtworkProvider.kt b/mobile/src/main/java/it/panov/musyx/mobile/ArtworkProvider.kt index 8190a87..e23c790 100644 --- a/mobile/src/main/java/it/panov/musyx/mobile/ArtworkProvider.kt +++ b/mobile/src/main/java/it/panov/musyx/mobile/ArtworkProvider.kt @@ -1,11 +1,13 @@ // Covers for Android Auto. Auto loads browse artwork only from content:// URIs, so each https -// cover is fetched once into the cache and served from there. +// cover is fetched once into the cache and served from there, as are the app's own icons. package it.panov.musyx.mobile import android.content.ContentProvider import android.content.ContentValues import android.content.Context import android.database.Cursor +import android.graphics.Bitmap +import android.graphics.Canvas import android.net.Uri import android.os.ParcelFileDescriptor import it.panov.musyx.data.Downloads @@ -21,6 +23,7 @@ class ArtworkProvider : ContentProvider() { override fun onCreate(): Boolean = true override fun openFile(uri: Uri, mode: String): ParcelFileDescriptor { + if (uri.lastPathSegment == SHUFFLE) return ParcelFileDescriptor.open(icon(R.drawable.ic_shuffle, "shuffle"), ParcelFileDescriptor.MODE_READ_ONLY) val url = uri.lastPathSegment?.let(Uri::parse) ?: throw FileNotFoundException("$uri") // Exported for the car, so it fetches only Yandex covers, never an arbitrary URL. val host = url.host.orEmpty() @@ -50,7 +53,23 @@ class ArtworkProvider : ContentProvider() { } } - override fun getType(uri: Uri): String = "image/jpeg" + /** A white icon on clear ground, drawn once into the cache as a PNG; the car can't draw vectors. */ + private fun icon(id: Int, name: String): File { + val file = File(requireNotNull(context).cacheDir, "artwork/icon-$name.png") + if (file.exists()) return file + val drawable = requireNotNull(requireNotNull(context).getDrawable(id)) + val bitmap = Bitmap.createBitmap(ICON_PX, ICON_PX, Bitmap.Config.ARGB_8888) + val inset = ICON_PX / 5 + drawable.setBounds(inset, inset, ICON_PX - inset, ICON_PX - inset) + drawable.draw(Canvas(bitmap)) + file.parentFile?.mkdirs() + val part = File(file.path + ".part") + part.outputStream().use { bitmap.compress(Bitmap.CompressFormat.PNG, 100, it) } + if (!part.renameTo(file)) throw FileNotFoundException(file.path) + return file + } + + override fun getType(uri: Uri): String = if (uri.lastPathSegment == SHUFFLE) "image/png" else "image/jpeg" override fun query(uri: Uri, projection: Array?, selection: String?, selectionArgs: Array?, sortOrder: String?): Cursor? = null override fun insert(uri: Uri, values: ContentValues?): Uri? = null @@ -58,6 +77,10 @@ class ArtworkProvider : ContentProvider() { override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array?): Int = 0 companion object { + /** The path of the Shuffle row's icon, in place of a cover's URL. */ + const val SHUFFLE = "icon:shuffle" + private const val ICON_PX = 192 + /** The content URI the car loads `url` from. */ fun uri(context: Context, url: String): Uri = Uri.Builder().scheme("content").authority("${context.packageName}.artwork").appendPath(url).build() 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 0c4d54d..992e92f 100644 --- a/mobile/src/main/java/it/panov/musyx/mobile/Library.kt +++ b/mobile/src/main/java/it/panov/musyx/mobile/Library.kt @@ -80,9 +80,10 @@ private const val OFFLINE_ALL = "offline" /** A downloaded album, playlist or Liked tracks, by its [it.panov.musyx.data.OfflineSet] key. */ private const val OFFLINE = "offline:" -// The buttons beside the car's transport: the heart, and "don't recommend". +// The buttons beside the car's transport: the heart, "don't recommend", and shuffle. private val LIKE = SessionCommand("it.panov.musyx.LIKE", Bundle.EMPTY) private val DISLIKE = SessionCommand("it.panov.musyx.DISLIKE", Bundle.EMPTY) +private val SHUFFLE_MODE = SessionCommand("it.panov.musyx.SHUFFLE", Bundle.EMPTY) @OptIn(UnstableApi::class) class Library(private val context: Context, private val state: AppState, private val scope: CoroutineScope) : @@ -96,7 +97,7 @@ class Library(private val context: Context, private val state: AppState, private /** * Has the car reload whenever someone signs in or out, or the app goes offline or back, so * it never keeps showing "Sign in" (or someone else's music, or music it can't reach), and - * keeps the heart in step with the track. Returns what stops watching. + * keeps the heart and shuffle in step with the player. Returns what stops watching. */ fun watch(session: MediaLibrarySession): () -> Unit { // snapshotFlow hears about changes through Compose's apply notifications, which only the @@ -124,8 +125,9 @@ class Library(private val context: Context, private val state: AppState, private } 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 }) + val p = state.playback.state + val track = p.track + listOf(track?.id, track?.let { state.liked.contains(it.id) }, track?.let { it.id in state.likes.dislikedTracks }, p.shuffle, p.waveStation) } .distinctUntilChanged() .collect { session.setMediaButtonPreferences(buttons()) } @@ -137,7 +139,7 @@ class Library(private val context: Context, private val state: AppState, private } } - /** Every controller may like and dislike, and starts with the buttons for what's playing. */ + /** Every controller may like, dislike and shuffle, 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) { @@ -146,7 +148,7 @@ class Library(private val context: Context, private val state: AppState, private MediaSession.ConnectionResult.DEFAULT_UNTRUSTED_SESSION_AND_LIBRARY_COMMANDS } return MediaSession.ConnectionResult.AcceptedResultBuilder(session) - .setAvailableSessionCommands(library.buildUpon().add(LIKE).add(DISLIKE).build()) + .setAvailableSessionCommands(library.buildUpon().add(LIKE).add(DISLIKE).add(SHUFFLE_MODE).build()) .setMediaButtonPreferences(buttons()) .build() } @@ -163,32 +165,38 @@ class Library(private val context: Context, private val state: AppState, private 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) + SHUFFLE_MODE.customAction -> state.playback.toggleShuffle() 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. + * The heart, "don't recommend" and shuffle 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, and a station decides its own order. */ private fun buttons(): List { - val track = state.playback.state.track ?: return emptyList() + val p = state.playback.state + val track = p.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) + val block = if (track.spoken) null else 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) + val shuffle = if (p.waveStation != null) null else CommandButton.Builder(if (p.shuffle) CommandButton.ICON_SHUFFLE_ON else CommandButton.ICON_SHUFFLE_OFF) + .setDisplayName(context.getString(if (p.shuffle) R.string.action_shuffle_off else R.string.action_shuffle)) + .setSessionCommand(SHUFFLE_MODE) + .setSlots(CommandButton.SLOT_FORWARD_SECONDARY, CommandButton.SLOT_OVERFLOW) + .build() + return listOfNotNull(heart, block, shuffle) } override fun onGetLibraryRoot( @@ -418,6 +426,8 @@ class Library(private val context: Context, private val state: AppState, private .setMediaMetadata( MediaMetadata.Builder() .setTitle(context.getString(R.string.action_shuffle)) + // Without an icon the row reads as a heading. + .setArtworkUri(ArtworkProvider.uri(context, ArtworkProvider.SHUFFLE)) .setIsBrowsable(false) .setIsPlayable(true) .setMediaType(MediaMetadata.MEDIA_TYPE_PLAYLIST) diff --git a/mobile/src/main/res/values-ru/strings.xml b/mobile/src/main/res/values-ru/strings.xml index 3220c90..80a1a8e 100644 --- a/mobile/src/main/res/values-ru/strings.xml +++ b/mobile/src/main/res/values-ru/strings.xml @@ -25,6 +25,7 @@ Вставьте OAuth-токен Слушать Вперемешку + Выключить перемешивание Пауза Продолжить Повторить diff --git a/mobile/src/main/res/values/strings.xml b/mobile/src/main/res/values/strings.xml index 73bf744..5e62eb6 100644 --- a/mobile/src/main/res/values/strings.xml +++ b/mobile/src/main/res/values/strings.xml @@ -26,6 +26,7 @@ Paste OAuth token Play Shuffle + Turn off shuffle Pause Resume Try again