Fix a bug where album art and track info could get out of sync if you advance twice quickly

This commit is contained in:
James Babcock committed 2026-10-13 16:17:07 -07:00
1 parent eb91d46657
commit 13da595de0
6 files changed
+38 -13

No files matched your search

@@ -46,6 +46,7 @@ class FaceclawMediaController(context: Context) {
override fun onMetadataChanged(metadata: MediaMetadata?) { override fun onMetadataChanged(metadata: MediaMetadata?) {
synchronized(lock) { synchronized(lock) {
artVersion++
emitStateLocked() emitStateLocked()
} }
} }
@@ -92,6 +93,15 @@ class FaceclawMediaController(context: Context) {
private var sessionsListenerRegistered = false private var sessionsListenerRegistered = false
private var ignoredPackages: Set<String> = HashSet() private var ignoredPackages: Set<String> = HashSet()
/**
* Bumped whenever the art getAlbumArtGray would return may have changed:
* every metadata update and every switch of active session. Players
* re-publish the same title/album with a different bitmap (art attached
* after download, or a stale bitmap from the previous track replaced
* after a quick double skip), so the strings can't key an art cache.
*/
private var artVersion = 0
init { init {
this.listenerComponent = ComponentName(appContext, FaceclawMediaNotificationListenerService::class.java) this.listenerComponent = ComponentName(appContext, FaceclawMediaNotificationListenerService::class.java)
this.sessionManager = appContext.getSystemService(Context.MEDIA_SESSION_SERVICE) as MediaSessionManager? this.sessionManager = appContext.getSystemService(Context.MEDIA_SESSION_SERVICE) as MediaSessionManager?
@@ -404,6 +414,7 @@ class FaceclawMediaController(context: Context) {
previous.unregisterCallback(controllerCallback) previous.unregisterCallback(controllerCallback)
} }
activeController = controller activeController = controller
artVersion++
if (controller != null) { if (controller != null) {
controller.registerCallback(controllerCallback, mainHandler) controller.registerCallback(controllerCallback, mainHandler)
} }
@@ -503,6 +514,7 @@ class FaceclawMediaController(context: Context) {
val finalCanSkipPrevious = canSkipPrevious val finalCanSkipPrevious = canSkipPrevious
val finalAccessEnabled = accessEnabled val finalAccessEnabled = accessEnabled
val finalStatus = status val finalStatus = status
val finalArtVersion = artVersion
mainHandler.post { mainHandler.post {
currentListener.onStateChange( currentListener.onStateChange(
@@ -519,7 +531,8 @@ class FaceclawMediaController(context: Context) {
finalCanSkipNext, finalCanSkipNext,
finalCanSkipPrevious, finalCanSkipPrevious,
finalAccessEnabled, finalAccessEnabled,
finalStatus finalStatus,
finalArtVersion
) )
} }
} }
+1
View File
@@ -11,6 +11,7 @@
* Phone preview is centered on the phone screen when apps are not maximized * Phone preview is centered on the phone screen when apps are not maximized
* iOS: Add missing EvenHub icons on the phone home screen * iOS: Add missing EvenHub icons on the phone home screen
* Fix a bug where album art and track display could get out of sync if you advance twice quickly
## 0.8.3 - 2026-10-10 ## 0.8.3 - 2026-10-10
+4 -5
View File
@@ -49,7 +49,8 @@ export class MusicWidget implements GlanceWidget {
private unsubscribe: (() => void) | null = null; private unsubscribe: (() => void) | null = null;
private progressTimer: ReturnType<typeof setInterval> | null = null; private progressTimer: ReturnType<typeof setInterval> | null = null;
private art: GrayImage | null = null; private art: GrayImage | null = null;
private artKey = ""; /** The artVersion `art` was fetched for; -1 = not fetched yet. */
private artVersion = -1;
start(requestRender: () => void): void { start(requestRender: () => void): void {
this.unsubscribe = mediaControllerBridge.onStateChange((state) => { this.unsubscribe = mediaControllerBridge.onStateChange((state) => {
@@ -129,10 +130,8 @@ export class MusicWidget implements GlanceWidget {
} }
private drawArt(image: GrayImage, media: MediaControllerState, artY: number): void { private drawArt(image: GrayImage, media: MediaControllerState, artY: number): void {
const key = `${media.packageName}|${media.title}|${media.album}`; if (media.artVersion !== this.artVersion) {
// Retry while null: players often publish metadata before the bitmap. this.artVersion = media.artVersion;
if (key !== this.artKey || this.art === null) {
this.artKey = key;
this.art = mediaControllerBridge.getAlbumArt(ART_SIZE); this.art = mediaControllerBridge.getAlbumArt(ART_SIZE);
} }
if (this.art) { if (this.art) {
+8 -7
View File
@@ -83,7 +83,8 @@ class MusicAppLayer implements Layer {
*/ */
private libraryAppsFresh = false; private libraryAppsFresh = false;
private art: GrayImage | null = null; private art: GrayImage | null = null;
private artKey = ""; /** The artVersion `art` was fetched for; -1 = not fetched yet. */
private artVersion = -1;
isPlaylistFocused(): boolean { isPlaylistFocused(): boolean {
return this.focusColumn === "playlist"; return this.focusColumn === "playlist";
@@ -371,12 +372,12 @@ class MusicAppLayer implements Layer {
} }
private drawArt(image: GrayImage, media: MediaControllerState): void { private drawArt(image: GrayImage, media: MediaControllerState): void {
const key = `${media.packageName}|${media.title}|${media.album}`; // Keyed on artVersion, not the strings: players (e.g. Spotify) publish
// Retry while null: players (e.g. Spotify) often publish metadata before // metadata before the art bitmap is downloaded, or with the previous
// the art bitmap is downloaded, then re-emit the same strings with the // track's bitmap after a quick double skip, then re-emit the same
// bitmap attached. The no-art fetch path is a cheap near-no-op. // strings with the right bitmap attached.
if (key !== this.artKey || this.art === null) { if (media.artVersion !== this.artVersion) {
this.artKey = key; this.artVersion = media.artVersion;
this.art = mediaControllerBridge.getAlbumArt(ART_SIZE); this.art = mediaControllerBridge.getAlbumArt(ART_SIZE);
} }
if (this.art) { if (this.art) {
+9
View File
@@ -37,6 +37,12 @@ export type MediaControllerState = {
canPlayPause: boolean; canPlayPause: boolean;
canSkipNext: boolean; canSkipNext: boolean;
canSkipPrevious: boolean; canSkipPrevious: boolean;
/**
* Changes whenever getAlbumArt's result may have changed (any metadata
* update or session switch). Key cached art on this, not the title:
* players re-publish the same strings with a different bitmap.
*/
artVersion: number;
}; };
const DEFAULT_MEDIA_STATE: MediaControllerState = { const DEFAULT_MEDIA_STATE: MediaControllerState = {
@@ -55,6 +61,7 @@ const DEFAULT_MEDIA_STATE: MediaControllerState = {
canPlayPause: false, canPlayPause: false,
canSkipNext: false, canSkipNext: false,
canSkipPrevious: false, canSkipPrevious: false,
artVersion: 0,
}; };
export class FaceclawMediaControllerBridge { export class FaceclawMediaControllerBridge {
@@ -206,6 +213,7 @@ export class FaceclawMediaControllerBridge {
canSkipPrevious: boolean, canSkipPrevious: boolean,
accessEnabled: boolean, accessEnabled: boolean,
status: string, status: string,
artVersion: number,
) => { ) => {
this.state = { this.state = {
available: Boolean(title || artist || album || packageName) || String(playbackState) !== "idle", available: Boolean(title || artist || album || packageName) || String(playbackState) !== "idle",
@@ -223,6 +231,7 @@ export class FaceclawMediaControllerBridge {
canPlayPause: Boolean(canPlayPause), canPlayPause: Boolean(canPlayPause),
canSkipNext: Boolean(canSkipNext), canSkipNext: Boolean(canSkipNext),
canSkipPrevious: Boolean(canSkipPrevious), canSkipPrevious: Boolean(canSkipPrevious),
artVersion: Number(artVersion),
}; };
this.stateUpdatedAtMs = Date.now(); this.stateUpdatedAtMs = Date.now();
for (const listener of this.stateListeners) { for (const listener of this.stateListeners) {
@@ -4,6 +4,7 @@ interface FaceclawMediaControllerListener {
/** All observed session owners, before filtering or choosing the active player. */ /** All observed session owners, before filtering or choosing the active player. */
fun onSessionAppsChanged(appsJson: String?): Unit fun onSessionAppsChanged(appsJson: String?): Unit
/** artVersion changes whenever the current item's album art may have changed. */
fun onStateChange( fun onStateChange(
playbackState: String?, playbackState: String?,
packageName: String?, packageName: String?,
@@ -19,5 +20,6 @@ interface FaceclawMediaControllerListener {
canSkipPrevious: Boolean, canSkipPrevious: Boolean,
accessEnabled: Boolean, accessEnabled: Boolean,
status: String?, status: String?,
artVersion: Int,
): Unit ): Unit
} }