Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
182 changes: 182 additions & 0 deletions app/src/main/kotlin/com/beatwave/music/playback/AndroidAutoLyrics.kt
Original file line number Diff line number Diff line change
@@ -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<LyricsEntry> = 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<String?>) {
// 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<LyricsEntry> {
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"
}
}
Original file line number Diff line number Diff line change
@@ -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<Player.Listener>()
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
}
}
Loading