diff --git a/app/src/main/kotlin/com/beatwave/music/constants/MediaSessionConstants.kt b/app/src/main/kotlin/com/beatwave/music/constants/MediaSessionConstants.kt index 1ff54e50c..c3a0b8f97 100644 --- a/app/src/main/kotlin/com/beatwave/music/constants/MediaSessionConstants.kt +++ b/app/src/main/kotlin/com/beatwave/music/constants/MediaSessionConstants.kt @@ -14,9 +14,11 @@ object MediaSessionConstants { const val ACTION_TOGGLE_LIKE = "TOGGLE_LIKE" const val ACTION_TOGGLE_SHUFFLE = "TOGGLE_SHUFFLE" const val ACTION_TOGGLE_REPEAT_MODE = "TOGGLE_REPEAT_MODE" + const val ACTION_TOGGLE_CAR_LYRICS = "TOGGLE_CAR_LYRICS" val CommandToggleLibrary = SessionCommand(ACTION_TOGGLE_LIBRARY, Bundle.EMPTY) val CommandToggleLike = SessionCommand(ACTION_TOGGLE_LIKE, Bundle.EMPTY) val CommandToggleStartRadio = SessionCommand(ACTION_TOGGLE_START_RADIO, Bundle.EMPTY) val CommandToggleShuffle = SessionCommand(ACTION_TOGGLE_SHUFFLE, Bundle.EMPTY) val CommandToggleRepeatMode = SessionCommand(ACTION_TOGGLE_REPEAT_MODE, Bundle.EMPTY) + val CommandToggleCarLyrics = SessionCommand(ACTION_TOGGLE_CAR_LYRICS, Bundle.EMPTY) } diff --git a/app/src/main/kotlin/com/beatwave/music/constants/PreferenceKeys.kt b/app/src/main/kotlin/com/beatwave/music/constants/PreferenceKeys.kt index 2f2143ada..1d65a899d 100644 --- a/app/src/main/kotlin/com/beatwave/music/constants/PreferenceKeys.kt +++ b/app/src/main/kotlin/com/beatwave/music/constants/PreferenceKeys.kt @@ -602,6 +602,7 @@ val SwipeLyricsKey = booleanPreferencesKey("swipeLyrics") val EnableLyricsThumbnailPlayPauseKey = booleanPreferencesKey("enableLyricsThumbnailPlayPause") val LyricsTextPositionKey = stringPreferencesKey("lyricsTextPosition") val LyricsClickKey = booleanPreferencesKey("lyricsClick") +val AndroidAutoLyricsKey = booleanPreferencesKey("androidAutoLyrics") val LyricsScrollKey = booleanPreferencesKey("lyricsScrollKey") val LyricsRomanizeJapaneseKey = booleanPreferencesKey("lyricsRomanizeJapanese") val LyricsRomanizeKoreanKey = booleanPreferencesKey("lyricsRomanizeKorean") diff --git a/app/src/main/kotlin/com/beatwave/music/playback/AndroidAutoLyrics.kt b/app/src/main/kotlin/com/beatwave/music/playback/AndroidAutoLyrics.kt new file mode 100644 index 000000000..be076281b --- /dev/null +++ b/app/src/main/kotlin/com/beatwave/music/playback/AndroidAutoLyrics.kt @@ -0,0 +1,182 @@ +/** + * BeatWave Project (C) 2026 + * Licensed under GPL-3.0 | See git history for contributors + */ + +package com.beatwave.music.playback + +import android.app.UiModeManager +import android.content.Context +import android.content.pm.PackageManager +import android.content.res.Configuration +import com.beatwave.music.constants.AndroidAutoLyricsKey +import com.beatwave.music.db.MusicDatabase +import com.beatwave.music.db.entities.LyricsEntity.Companion.LYRICS_NOT_FOUND +import com.beatwave.music.lyrics.LyricsEntry +import com.beatwave.music.lyrics.LyricsUtils +import com.beatwave.music.utils.dataStore +import com.beatwave.music.utils.get +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.flatMapLatest +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch +import java.util.concurrent.atomic.AtomicInteger + +/** + * Synced lyrics for Android Auto. + * + * A media app can't draw its own screen in the car, Android Auto only renders + * what the media session publishes. So lyrics reach the car two ways: + * the current line is swapped into the now playing metadata (see + * [LyricsMetadataPlayer]), and the "Lyrics" browse tab shows it with the next + * line below, where tapping it seeks to the start of the line. + */ +class AndroidAutoLyrics( + private val context: Context, + private val scope: CoroutineScope, + private val database: MusicDatabase, + private val positionMs: () -> Long, + private val isPlaying: () -> Boolean, +) { + /** Fired on the main thread when the current line, the song or the setting changes. */ + var onChanged: () -> Unit = {} + + @Volatile + var enabled = false + private set + @Volatile + var songId: String? = null + private set + @Volatile + var lines: List = emptyList() + private set + @Volatile + var currentIndex = -1 + private set + + private val uiModeManager = context.getSystemService(Context.UI_MODE_SERVICE) as UiModeManager + private val isAutomotive = context.packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE) + + /** + * Only in the car does the lyric line replace the song info, so the phone's own + * notification stays normal. Newer Android Auto versions don't put the phone in + * car mode, so a connected Android Auto controller counts too. + */ + @Volatile + var isInCar = false + private set + + private val carControllers = AtomicInteger(0) + + /** Called by the session callback when Android Auto connects to or leaves the session. */ + fun onCarControllerConnected(connected: Boolean) { + if (connected) { + carControllers.incrementAndGet() + } else { + carControllers.updateAndGet { (it - 1).coerceAtLeast(0) } + } + scope.launch { updateInCar() } + } + + private fun checkInCar() = + isAutomotive || + carControllers.get() > 0 || + uiModeManager.currentModeType == Configuration.UI_MODE_TYPE_CAR + + private fun updateInCar() { + val inCar = checkInCar() + if (inCar != isInCar) { + isInCar = inCar + onChanged() + } + } + + /** The line to show in the now playing metadata for [mediaId], or null to leave it alone. */ + fun metadataLine(mediaId: String?): String? { + if (!enabled || mediaId == null || mediaId != songId || !isInCar) return null + return lines.getOrNull(currentIndex)?.text?.trim()?.takeIf { it.isNotEmpty() } + } + + @OptIn(ExperimentalCoroutinesApi::class) + fun start(songIds: Flow) { + // Read once up front: Android Auto can ask for the tabs before the flow below + // first emits, and would get a tab list without Lyrics. + enabled = context.dataStore.get(AndroidAutoLyricsKey, true) + + scope.launch { + context.dataStore.data + .map { it[AndroidAutoLyricsKey] ?: true } + .distinctUntilChanged() + .collect { + enabled = it + onChanged() + } + } + + scope.launch { + songIds + .distinctUntilChanged() + .flatMapLatest { id -> + if (id == null) { + flowOf(null to emptyList()) + } else { + database.lyrics(id).map { id to parseSynced(it?.lyrics) } + } + }.collect { (id, parsed) -> + songId = id + lines = parsed + currentIndex = -1 + tick(force = true) + } + } + + // Car mode is a system call, so it's checked about once a second, not per metadata read. + scope.launch { + var sinceCarCheck = IDLE_MS + while (isActive) { + val ticking = enabled && lines.isNotEmpty() && isPlaying() + if (sinceCarCheck >= IDLE_MS) { + sinceCarCheck = 0 + updateInCar() + } + if (ticking) tick(force = false) + val wait = if (ticking) TICK_MS else IDLE_MS + sinceCarCheck += wait + delay(wait) + } + } + } + + private fun tick(force: Boolean) { + val index = + if (lines.isEmpty()) -1 else LyricsUtils.findCurrentLineIndex(lines, positionMs()) + if (force || index != currentIndex) { + currentIndex = index + onChanged() + } + } + + /** Unsynced lyrics have no timestamps, so there's no current line to follow. */ + private fun parseSynced(raw: String?): List { + if (raw.isNullOrBlank() || raw == LYRICS_NOT_FOUND || !raw.trimStart().startsWith("[")) { + return emptyList() + } + return runCatching { LyricsUtils.parseLyrics(raw) } + .getOrDefault(emptyList()) + .filterNot { it.isBackground } + } + + companion object { + private const val TICK_MS = 200L + private const val IDLE_MS = 1000L + + /** Package of the Android Auto app, which connects to the session while projecting. */ + const val ANDROID_AUTO_PACKAGE = "com.google.android.projection.gearhead" + } +} diff --git a/app/src/main/kotlin/com/beatwave/music/playback/LyricsMetadataPlayer.kt b/app/src/main/kotlin/com/beatwave/music/playback/LyricsMetadataPlayer.kt new file mode 100644 index 000000000..b3d0f16b4 --- /dev/null +++ b/app/src/main/kotlin/com/beatwave/music/playback/LyricsMetadataPlayer.kt @@ -0,0 +1,100 @@ +/** + * BeatWave Project (C) 2026 + * Licensed under GPL-3.0 | See git history for contributors + */ + +package com.beatwave.music.playback + +import androidx.media3.common.ForwardingPlayer +import androidx.media3.common.MediaMetadata +import androidx.media3.common.Player +import androidx.media3.common.util.UnstableApi +import java.util.concurrent.CopyOnWriteArraySet + +/** + * Wraps the player handed to the media session so the now playing metadata can + * carry the current lyric line, which is what Android Auto shows. + * Only the session sees this; the player itself and the in-app UI keep the real + * metadata. + * + * A short line takes the title row, with the song below it. A long one would get cut off, so it is + * split across the title and artist rows instead. + */ +@UnstableApi +class LyricsMetadataPlayer( + player: Player, + private val lyricLine: (mediaId: String?) -> String?, +) : ForwardingPlayer(player) { + private val listeners = CopyOnWriteArraySet() + private var publishedLine: String? = null + + override fun addListener(listener: Player.Listener) { + super.addListener(listener) + listeners += listener + } + + override fun removeListener(listener: Player.Listener) { + super.removeListener(listener) + listeners -= listener + } + + override fun getMediaMetadata(): MediaMetadata { + val metadata = super.getMediaMetadata() + val line = lyricLine(currentMediaItem?.mediaId) ?: return metadata + return metadata.withLyricLine(line) + } + + /** Pushes the metadata to the session again if the line changed since the last push. */ + fun invalidateLyricLine() { + val line = lyricLine(currentMediaItem?.mediaId) + if (line == publishedLine) return + publishedLine = line + val metadata = mediaMetadata + listeners.forEach { it.onMediaMetadataChanged(metadata) } + } + + private fun MediaMetadata.withLyricLine(line: String): MediaMetadata { + val builder = buildUpon() + if (line.length <= SINGLE_ROW_MAX_CHARS) { + // The title row is the big, bright one in the car, so the line goes + // there and the song moves down to the smaller row. + val song = listOfNotNull(displayTitle ?: title, artist) + .filter { it.isNotBlank() } + .joinToString(" · ") + builder + .setTitle(line) + .setDisplayTitle(line) + .setArtist(song) + .setSubtitle(song) + } else { + val split = splitPoint(line) + val top = line.substring(0, split).trim() + val bottom = line.substring(split).trim() + builder + .setTitle(top) + .setDisplayTitle(top) + .setArtist(bottom) + .setSubtitle(bottom) + } + return builder.build() + } + + /** The space closest to the middle, so both rows come out about the same length. */ + private fun splitPoint(line: String): Int { + val middle = line.length / 2 + val before = line.lastIndexOf(' ', middle) + val after = line.indexOf(' ', middle) + return when { + before < 0 && after < 0 -> middle + before < 0 -> after + after < 0 -> before + middle - before <= after - middle -> before + else -> after + } + } + + companion object { + /** About what fits on the artist row of a typical Android Auto screen. */ + private const val SINGLE_ROW_MAX_CHARS = 32 + } +} diff --git a/app/src/main/kotlin/com/beatwave/music/playback/MediaLibrarySessionCallback.kt b/app/src/main/kotlin/com/beatwave/music/playback/MediaLibrarySessionCallback.kt index 85812e764..2bcf441e5 100644 --- a/app/src/main/kotlin/com/beatwave/music/playback/MediaLibrarySessionCallback.kt +++ b/app/src/main/kotlin/com/beatwave/music/playback/MediaLibrarySessionCallback.kt @@ -11,9 +11,11 @@ import android.net.Uri import android.os.Bundle import androidx.annotation.DrawableRes import androidx.core.net.toUri +import androidx.datastore.preferences.core.edit import androidx.media3.common.C import androidx.media3.common.MediaItem import androidx.media3.common.MediaMetadata +import androidx.media3.common.Player import androidx.media3.common.util.UnstableApi import androidx.media3.exoplayer.offline.Download import androidx.media3.session.LibraryResult @@ -35,6 +37,7 @@ import com.music.innertube.models.SongItem import com.music.innertube.models.filterExplicit import com.music.innertube.models.filterVideoSongs import com.beatwave.music.R +import com.beatwave.music.constants.AndroidAutoLyricsKey import com.beatwave.music.constants.HideExplicitKey import com.beatwave.music.constants.HideVideoSongsKey import com.beatwave.music.constants.DataSaverEnabledKey @@ -57,6 +60,7 @@ import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.map import kotlinx.coroutines.guava.future +import kotlinx.coroutines.launch import kotlinx.coroutines.plus import javax.inject.Inject @@ -73,12 +77,19 @@ constructor( var toggleLike: () -> Unit = {} var toggleStartRadio: () -> Unit = {} var toggleLibrary: () -> Unit = {} + var androidAutoLyrics: AndroidAutoLyrics? = null + + // Android Auto shows only this many root items as tabs. + private var rootChildrenLimit = Int.MAX_VALUE override fun onConnect( session: MediaSession, controller: MediaSession.ControllerInfo, ): MediaSession.ConnectionResult { val connectionResult = super.onConnect(session, controller) + if (controller.packageName == AndroidAutoLyrics.ANDROID_AUTO_PACKAGE) { + androidAutoLyrics?.onCarControllerConnected(true) + } return MediaSession.ConnectionResult.accept( connectionResult.availableSessionCommands .buildUpon() @@ -87,11 +98,21 @@ constructor( .add(MediaSessionConstants.CommandToggleLibrary) .add(MediaSessionConstants.CommandToggleShuffle) .add(MediaSessionConstants.CommandToggleRepeatMode) + .add(MediaSessionConstants.CommandToggleCarLyrics) .build(), connectionResult.availablePlayerCommands, ) } + override fun onDisconnected( + session: MediaSession, + controller: MediaSession.ControllerInfo, + ) { + if (controller.packageName == AndroidAutoLyrics.ANDROID_AUTO_PACKAGE) { + androidAutoLyrics?.onCarControllerConnected(false) + } + } + override fun onCustomCommand( session: MediaSession, controller: MediaSession.ControllerInfo, @@ -106,6 +127,7 @@ constructor( !session.player.shuffleModeEnabled MediaSessionConstants.ACTION_TOGGLE_REPEAT_MODE -> session.player.toggleRepeatMode() + MediaSessionConstants.ACTION_TOGGLE_CAR_LYRICS -> toggleCarLyrics() } return Futures.immediateFuture(SessionResult(SessionResult.RESULT_SUCCESS)) } @@ -122,13 +144,17 @@ constructor( session: MediaLibrarySession, browser: MediaSession.ControllerInfo, params: MediaLibraryService.LibraryParams?, - ): ListenableFuture> = - Futures.immediateFuture( + ): ListenableFuture> { + params?.extras?.getInt(MediaConstants.EXTRAS_KEY_ROOT_CHILDREN_LIMIT, 0) + ?.takeIf { it > 0 } + ?.let { rootChildrenLimit = it } + return Futures.immediateFuture( LibraryResult.ofItem( rootMediaItem(), params.withContentStyleHints(), ), ) + } override fun onGetChildren( session: MediaLibrarySession, @@ -141,7 +167,9 @@ constructor( scope.future(Dispatchers.IO) { val children = when (parentId) { - MusicService.ROOT -> rootChildren() + MusicService.ROOT -> rootTabs() + + MusicService.LYRICS -> lyricsChildren() MusicService.SONG -> database.songsByCreateDateAsc().first() .map { it.toMediaItem(parentId) } @@ -388,8 +416,16 @@ constructor( mediaItems: MutableList, startIndex: Int, startPositionMs: Long, - ): ListenableFuture = - scope.future { + ): ListenableFuture { + val lyricPath = mediaItems.firstOrNull()?.mediaId?.split("/") + if (lyricPath?.firstOrNull() == MusicService.LYRICS) { + seekToLyricLine(mediaSession.player, lyricPath) + // Failing keeps the queue as it is; the tap was only a seek. + return Futures.immediateFailedFuture( + UnsupportedOperationException("Lyric lines seek, they don't replace the queue"), + ) + } + return scope.future { val defaultResult = MediaItemsWithStartPosition(emptyList(), startIndex, startPositionMs) val path = mediaItems.firstOrNull()?.mediaId?.split("/") ?: return@future defaultResult @@ -579,6 +615,7 @@ constructor( else -> defaultResult } } + } private fun drawableUri( @DrawableRes id: Int, @@ -638,6 +675,88 @@ constructor( ), ) + /** Root tabs, plus Lyrics when it's on. Artists gives way if the car can't show them all. */ + private fun rootTabs(): List { + if (androidAutoLyrics?.enabled != true) return rootChildren() + val tabs = (rootChildren() + lyricsTabMediaItem()).toMutableList() + if (tabs.size > rootChildrenLimit) tabs.removeAll { it.mediaId == MusicService.ARTIST } + return tabs.take(rootChildrenLimit) + } + + private fun lyricsTabMediaItem() = browsableMediaItem( + MusicService.LYRICS, + context.getString(R.string.lyrics), + null, + drawableUri(R.drawable.lyrics), + MediaMetadata.MEDIA_TYPE_FOLDER_MIXED, + ) + + /** + * One row: the current line as the title and the next one as the subtitle, which + * Android Auto draws smaller and dimmer. It's rebuilt on every line change. + */ + private fun lyricsChildren(): List { + val lyrics = androidAutoLyrics ?: return emptyList() + val songId = lyrics.songId + ?: return listOf(lyricsMessageItem(context.getString(R.string.android_auto_lyrics_no_song))) + if (lyrics.lines.isEmpty()) { + return listOf(lyricsMessageItem(context.getString(R.string.android_auto_lyrics_not_synced))) + } + + val index = lyrics.currentIndex.coerceAtLeast(0) + val line = lyrics.lines[index] + return listOf( + lyricLineMediaItem( + id = "${MusicService.LYRICS}/$songId/${line.time}", + title = line.text.trim().ifEmpty { "♪" }, + iconUri = null, + subtitle = lyrics.lines.getOrNull(index + 1)?.text?.trim(), + ), + ) + } + + // Not playable: Android Auto offers playable items as "For you" suggestions on the + // home screen, and a status message has no business there. + private fun lyricsMessageItem(message: String) = + lyricLineMediaItem(MusicService.LYRICS + "/", message, drawableUri(R.drawable.lyrics), playable = false) + + private fun lyricLineMediaItem( + id: String, + title: String, + iconUri: Uri?, + subtitle: String? = null, + playable: Boolean = true, + ) = MediaItem + .Builder() + .setMediaId(id) + .setMediaMetadata( + MediaMetadata + .Builder() + .setTitle(title) + .setSubtitle(subtitle) + .setArtworkUri(iconUri) + .setIsPlayable(playable) + .setIsBrowsable(false) + .setMediaType(MediaMetadata.MEDIA_TYPE_MUSIC) + .setExtras(playableMediaItemExtras()) + .build(), + ).build() + + private fun toggleCarLyrics() { + scope.launch { + context.dataStore.edit { it[AndroidAutoLyricsKey] = !(it[AndroidAutoLyricsKey] ?: true) } + } + } + + /** mediaId is lyrics//; only seeks if that song is still playing. */ + private fun seekToLyricLine(player: Player, path: List) { + val songId = path.getOrNull(1) ?: return + val timeMs = path.getOrNull(2)?.toLongOrNull() ?: return + if (player.currentMediaItem?.mediaId != songId) return + player.seekTo(timeMs) + if (!player.isPlaying) player.play() + } + private suspend fun getMediaItem(mediaId: String): MediaItem? { val path = mediaId.split("/") val type = path.firstOrNull() ?: return null @@ -645,6 +764,8 @@ constructor( return when (type) { MusicService.ROOT -> rootMediaItem() + MusicService.LYRICS -> lyricsTabMediaItem() + MusicService.SONG -> { if (path.size == 1) { rootChildren().first { it.mediaId == MusicService.SONG } diff --git a/app/src/main/kotlin/com/beatwave/music/playback/MusicService.kt b/app/src/main/kotlin/com/beatwave/music/playback/MusicService.kt index 925a3f986..1233107d5 100644 --- a/app/src/main/kotlin/com/beatwave/music/playback/MusicService.kt +++ b/app/src/main/kotlin/com/beatwave/music/playback/MusicService.kt @@ -129,6 +129,7 @@ import com.beatwave.music.constants.ListenBrainzEnabledKey import com.beatwave.music.constants.ListenBrainzTokenKey import com.beatwave.music.constants.HistoryDuration import com.beatwave.music.constants.LastFMUseNowPlaying +import com.beatwave.music.constants.MediaSessionConstants.CommandToggleCarLyrics import com.beatwave.music.constants.MediaSessionConstants.CommandToggleLike import com.beatwave.music.constants.MediaSessionConstants.CommandToggleRepeatMode import com.beatwave.music.constants.MediaSessionConstants.CommandToggleShuffle @@ -426,6 +427,11 @@ class MusicService : private var duckVolumeRestoreJob: Job? = null private lateinit var mediaSession: MediaLibrarySession + private lateinit var androidAutoLyrics: AndroidAutoLyrics + private lateinit var sessionPlayer: LyricsMetadataPlayer + private var lastCarButtonState: Pair? = null + private var lyricsTabRefreshJob: Job? = null + private var lastLyricsTabRefresh = 0L // Tracks if player has been properly initilized private val playerInitialized = MutableStateFlow(false) @@ -654,9 +660,18 @@ class MusicService : toggleStartRadio = ::toggleStartRadio toggleLibrary = ::toggleLibrary } + androidAutoLyrics = AndroidAutoLyrics( + context = this, + scope = scope, + database = database, + positionMs = { player.currentPosition }, + isPlaying = { player.isPlaying }, + ) + mediaLibrarySessionCallback.androidAutoLyrics = androidAutoLyrics + sessionPlayer = lyricsSessionPlayer(player) mediaSession = MediaLibrarySession - .Builder(this, player, mediaLibrarySessionCallback) + .Builder(this, sessionPlayer, mediaLibrarySessionCallback) .setSessionActivity( PendingIntent.getActivity( this, @@ -666,6 +681,21 @@ class MusicService : ), ).setBitmapLoader(CoilBitmapLoader(this, scope)) .build() + androidAutoLyrics.onChanged = { + sessionPlayer.invalidateLyricLine() + refreshLyricsTab() + // The last car button depends on these, so redraw the buttons when they flip. + val carButtonState = androidAutoLyrics.isInCar to androidAutoLyrics.enabled + if (carButtonState != lastCarButtonState) { + // The Lyrics tab comes and goes with the setting, so the car needs a fresh tab list. + if (carButtonState.second != lastCarButtonState?.second) { + mediaSession.notifyChildrenChanged(ROOT, Int.MAX_VALUE, null) + } + lastCarButtonState = carButtonState + updateNotification() + } + } + androidAutoLyrics.start(currentMediaMetadata.map { it?.id }) player.repeatMode = dataStore.get(RepeatModeKey, REPEAT_MODE_OFF) // Restore shuffle mode if remember option is enabled @@ -1505,6 +1535,42 @@ class MusicService : updateWidgetUI(false) } + private fun lyricsSessionPlayer(player: Player) = + LyricsMetadataPlayer(player) { mediaId -> androidAutoLyrics.metadataLine(mediaId) } + + // Android Auto throttles browse refreshes, so fast lines are coalesced into one + // refresh at most every LYRICS_TAB_REFRESH_MS; the list is built when it's fetched. + private fun refreshLyricsTab() { + if (lyricsTabRefreshJob?.isActive == true) return + val wait = LYRICS_TAB_REFRESH_MS - (SystemClock.elapsedRealtime() - lastLyricsTabRefresh) + lyricsTabRefreshJob = scope.launch { + if (wait > 0) delay(wait) + lastLyricsTabRefresh = SystemClock.elapsedRealtime() + mediaSession.notifyChildrenChanged(LYRICS, Int.MAX_VALUE, null) + } + } + + /** + * In the car the radio button gives way to a lyrics on/off switch, since the car + * has no other way to reach that setting. The phone keeps the radio button. + */ + private fun lastButton(): CommandButton { + if (::androidAutoLyrics.isInitialized && androidAutoLyrics.isInCar) { + val on = androidAutoLyrics.enabled + return CommandButton.Builder() + .setDisplayName(getString(if (on) R.string.android_auto_lyrics_hide else R.string.android_auto_lyrics_show)) + .setIconResId(if (on) R.drawable.lyrics_active else R.drawable.lyrics) + .setSessionCommand(CommandToggleCarLyrics) + .build() + } + return CommandButton.Builder() + .setDisplayName(getString(R.string.start_radio)) + .setIconResId(R.drawable.radio) + .setSessionCommand(CommandToggleStartRadio) + .setEnabled(currentSong.value != null) + .build() + } + private fun updateNotification() { mediaSession.setCustomLayout( listOf( @@ -1551,12 +1617,7 @@ class MusicService : .setIconResId(if (player.shuffleModeEnabled) R.drawable.shuffle_on else R.drawable.shuffle) .setSessionCommand(CommandToggleShuffle) .build(), - CommandButton.Builder() - .setDisplayName(getString(R.string.start_radio)) - .setIconResId(R.drawable.radio) - .setSessionCommand(CommandToggleStartRadio) - .setEnabled(currentSong.value != null) - .build(), + lastButton(), ), ) } @@ -4454,7 +4515,8 @@ class MusicService : sleepTimer.player = player try { - (mediaSession as MediaSession).player = player + sessionPlayer = lyricsSessionPlayer(player) + (mediaSession as MediaSession).player = sessionPlayer } catch (e: Exception) { timber.log.Timber.e(e, "Failed to swap player in MediaSession") } @@ -4610,6 +4672,8 @@ class MusicService : const val PLAYLIST = "playlist" const val YOUTUBE_PLAYLIST = "youtube_playlist" const val SEARCH = "search" + const val LYRICS = "lyrics" + private const val LYRICS_TAB_REFRESH_MS = 500L const val SHUFFLE_ACTION = "__shuffle__" const val CHANNEL_ID = "music_channel_01" diff --git a/app/src/main/kotlin/com/beatwave/music/ui/screens/settings/ContentSettings.kt b/app/src/main/kotlin/com/beatwave/music/ui/screens/settings/ContentSettings.kt index 629f7693d..502b7272a 100644 --- a/app/src/main/kotlin/com/beatwave/music/ui/screens/settings/ContentSettings.kt +++ b/app/src/main/kotlin/com/beatwave/music/ui/screens/settings/ContentSettings.kt @@ -118,6 +118,7 @@ import com.beatwave.music.constants.ContentCountryKey import com.beatwave.music.constants.ContentLanguageKey import com.beatwave.music.constants.CountryCodeToName import com.beatwave.music.constants.DataSaverEnabledKey +import com.beatwave.music.constants.AndroidAutoLyricsKey import com.beatwave.music.constants.EnableBetterLyricsKey import com.beatwave.music.constants.EnableKugouKey import com.beatwave.music.constants.EnableLrcLibKey @@ -237,6 +238,7 @@ fun ContentSettings( val (enableKugou, onEnableKugouChange) = rememberPreference(key = EnableKugouKey, defaultValue = true) val (enableLrclib, onEnableLrclibChange) = rememberPreference(key = EnableLrcLibKey, defaultValue = true) val (enableBetterLyrics, onEnableBetterLyricsChange) = rememberPreference(key = EnableBetterLyricsKey, defaultValue = true) + val (androidAutoLyrics, onAndroidAutoLyricsChange) = rememberPreference(key = AndroidAutoLyricsKey, defaultValue = true) val (enableSimpMusic, onEnableSimpMusicChange) = rememberPreference(key = EnableSimpMusicKey, defaultValue = true) val (enableYouLyPlus, onEnableYouLyPlusChange) = rememberPreference(key = EnableYouLyPlusKey, defaultValue = true) val (enablePaxsenix, onEnablePaxsenixChange) = rememberPreference(key = EnablePaxsenixKey, defaultValue = true) @@ -1189,6 +1191,27 @@ fun ContentSettings( Material3SettingsGroup( title = stringResource(R.string.lyrics), items = listOf( + Material3SettingsItem( + icon = painterResource(R.drawable.lyrics), + title = { Text(stringResource(R.string.android_auto_lyrics)) }, + description = { Text(stringResource(R.string.android_auto_lyrics_desc)) }, + trailingContent = { + Switch( + checked = androidAutoLyrics, + onCheckedChange = onAndroidAutoLyricsChange, + thumbContent = { + Icon( + painter = painterResource( + id = if (androidAutoLyrics) R.drawable.check else R.drawable.close + ), + contentDescription = null, + modifier = Modifier.size(SwitchDefaults.IconSize) + ) + } + ) + }, + onClick = { onAndroidAutoLyricsChange(!androidAutoLyrics) } + ), Material3SettingsItem( icon = painterResource(R.drawable.lyrics), title = { Text(stringResource(R.string.enable_lrclib)) }, diff --git a/app/src/main/kotlin/com/beatwave/music/utils/YTPlayerUtils.kt b/app/src/main/kotlin/com/beatwave/music/utils/YTPlayerUtils.kt index 9b3474c96..4020b0a7d 100644 --- a/app/src/main/kotlin/com/beatwave/music/utils/YTPlayerUtils.kt +++ b/app/src/main/kotlin/com/beatwave/music/utils/YTPlayerUtils.kt @@ -67,6 +67,7 @@ import java.net.URI import java.io.IOException import kotlinx.coroutines.async import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.runInterruptible /** Higher first. Ranks Tidal search-result candidates so the matcher below tries * the best-quality candidate first instead of Tidal's own (unordered w.r.t. @@ -963,7 +964,10 @@ object YTPlayerUtils { val metaSessionId = YouTube.dataSyncId if (METADATA_CLIENT.useWebPoTokens && metaSessionId != null) { try { - metaPoToken = poTokenGenerator.getWebClientPoToken(videoId, metaSessionId) + // Interruptible, so cancelling this job below really stops it. + metaPoToken = runInterruptible { + poTokenGenerator.getWebClientPoToken(videoId, metaSessionId) + } } catch (e: Exception) { Timber.tag(logTag).e(e, "Metadata PoToken generation failed") } @@ -982,7 +986,13 @@ object YTPlayerUtils { null } } - mainDeferred.await() to kotlinx.coroutines.withTimeoutOrNull(400L) { metaDeferred.await() } + val main = mainDeferred.await() + // coroutineScope waits for every child, so a metadata fetch that is still + // running would hold up playback. Past the timeout its result is dropped + // anyway, so cancel it. + val meta = kotlinx.coroutines.withTimeoutOrNull(400L) { metaDeferred.await() } + if (meta == null) metaDeferred.cancel() + main to meta } // Debug uploaded track response diff --git a/app/src/main/kotlin/com/beatwave/music/utils/potoken/PoTokenGenerator.kt b/app/src/main/kotlin/com/beatwave/music/utils/potoken/PoTokenGenerator.kt index 2c6de8c59..f625cde47 100644 --- a/app/src/main/kotlin/com/beatwave/music/utils/potoken/PoTokenGenerator.kt +++ b/app/src/main/kotlin/com/beatwave/music/utils/potoken/PoTokenGenerator.kt @@ -3,10 +3,12 @@ package com.beatwave.music.utils.potoken import android.webkit.CookieManager import com.beatwave.music.utils.cipher.CipherDeobfuscator import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.TimeoutCancellationException import kotlinx.coroutines.runBlocking import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.withContext +import kotlinx.coroutines.withTimeout import timber.log.Timber class PoTokenGenerator { @@ -20,6 +22,11 @@ class PoTokenGenerator { private var webPoTokenStreamingPot: String? = null private var webPoTokenGenerator: PoTokenWebView? = null + // Set after a timeout, so the next call starts from a fresh WebView instead of + // waiting on the one that stopped answering. + @Volatile + private var recreateNext = false + fun getWebClientPoToken(videoId: String, sessionId: String): PoTokenResult? { Timber.tag(TAG).d("getWebClientPoToken called: videoId=$videoId, sessionId=$sessionId") Timber.tag(TAG).d("WebView state: supported=$webViewSupported, badImpl=$webViewBadImpl") @@ -30,10 +37,22 @@ class PoTokenGenerator { return try { Timber.tag(TAG).d("Calling runBlocking to generate poToken...") - runBlocking { getWebClientPoToken(videoId, sessionId, forceRecreate = false) } + // The WebView can stop answering (for example when the service was started by + // Android Auto with no activity around). Without a limit this call blocked + // forever while holding webPoTokenGenLock, so the song never started and + // every later song got stuck behind it until the app was restarted. + runBlocking { + withTimeout(PO_TOKEN_TIMEOUT_MS) { + getWebClientPoToken(videoId, sessionId, forceRecreate = recreateNext) + } + }.also { recreateNext = false } } catch (e: Exception) { Timber.tag(TAG).e(e, "poToken generation exception: ${e.javaClass.simpleName}: ${e.message}") when (e) { + is TimeoutCancellationException -> { + recreateNext = true + null + } is BadWebViewException -> { Timber.tag(TAG).e(e, "Could not obtain poToken because WebView is broken") webViewBadImpl = true @@ -97,4 +116,8 @@ class PoTokenGenerator { return PoTokenResult(playerPot, streamingPot) } + + private companion object { + const val PO_TOKEN_TIMEOUT_MS = 10_000L + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b387dd3fc..329d36854 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -435,6 +435,12 @@ When this option is on, screenshots and the app\'s view in Recents are disabled. Enable LrcLib lyrics provider Enable KuGou lyrics provider + Lyrics on Android Auto + Show the current line on the car screen and add a Lyrics tab. Tap a line to jump to it + Nothing is playing + No synced lyrics for this song + Show lyrics + Hide lyrics Hide explicit content Backup and restore