music: mark songs recently played only when they play
- Commit
fd2ccf2b92a17f31f9787dfc8513a0149ca1cab8- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 20:14 UTC (28 days ago)
- Parents
1ead65d
Symptom: opening an album or playlist in Android Auto put every song in it at the front of Recently played, in reverse queue order, before any of them had played; a long queue flooded the list's 50 slots. Root cause: PlaybackService.onAddMediaItems called markPlayed with the whole resolved queue. That callback resolves items for playback; it is not evidence any of them became the current track. Fix: stop marking on resolution and instead mark the single item in the session player's onMediaItemTransition (the listener added for notification state in the previous change, renamed accordingly). Skipping within a queue now records each track as it actually plays, for car and phone playback alike. Verification: compile plus module unit tests after this series; MusicLibraryTree.markPlayed itself is unchanged.
Diff
music/src/main/java/com/vayunmathur/music/service/PlaybackService.kt
diff --git a/music/src/main/java/com/vayunmathur/music/service/PlaybackService.kt b/music/src/main/java/com/vayunmathur/music/service/PlaybackService.ktindex 9ebd5078fe..136576d8ff 100644--- a/music/src/main/java/com/vayunmathur/music/service/PlaybackService.kt+++ b/music/src/main/java/com/vayunmathur/music/service/PlaybackService.kt@@ -50,19 +50,27 @@ class PlaybackService : MediaLibraryService() { private val mainHandler = Handler(Looper.getMainLooper()) /**- * Keeps the notification's shuffle/repeat icons honest when the mode is- * changed from somewhere other than the notification itself (the app's- * now-playing screen, a headset, Android Auto). Without this the buttons- * were only redrawn on service start and on their own clicks, so they- * showed the mode as it was before the app last changed it - and a tap- * then toggled from the player's real state, appearing to do nothing- * or the opposite of the icon shown.+ * Reacts to player changes no single controller owns:+ * - keeps the notification's shuffle/repeat icons honest when the mode+ * is changed from somewhere other than the notification itself (the+ * app's now-playing screen, a headset, Android Auto). Without this+ * the buttons were only redrawn on service start and on their own+ * clicks, so they showed the mode as it was before the app last+ * changed it - and a tap then toggled from the player's real state,+ * appearing to do nothing or the opposite of the icon shown.+ * - records the track that actually became current in the browse+ * tree's recently-played list. Queueing a list used to mark every+ * queued song as recently played before any of them had played. */- private val buttonStateListener = object : Player.Listener {+ private val playerListener = object : Player.Listener { override fun onShuffleModeEnabledChanged(shuffleModeEnabled: Boolean) = updateNotificationButtons() override fun onRepeatModeChanged(repeatMode: Int) = updateNotificationButtons()++ override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {+ mediaItem?.let { libraryTree.markPlayed(listOf(it.mediaId)) }+ } } // Custom Command Constants@@ -106,7 +114,7 @@ class PlaybackService : MediaLibraryService() { mainHandler.post { notifyBrowseChildrenChanged() } } - player.addListener(buttonStateListener)+ player.addListener(playerListener) // Initialize the notification buttons for the first time updateNotificationButtons()@@ -251,7 +259,9 @@ class PlaybackService : MediaLibraryService() { else -> libraryTree.resolveForPlayback(item.mediaId)?.let { resolved.add(it) } } }- libraryTree.markPlayed(resolved.map { it.mediaId })+ // Not marked as played here: resolving a queue is not playing+ // it. The player listener records each track as it becomes+ // current, which is also what phone playback does. return Futures.immediateFuture(resolved) } }@@ -261,7 +271,7 @@ class PlaybackService : MediaLibraryService() { castQueue?.release() castQueue = null mediaSession?.run {- player.removeListener(buttonStateListener)+ player.removeListener(playerListener) // Releasing the wrapper releases what it wraps, so the local player goes with it. player.release() release()