Modern Apps git

← Modern-Apps.git

music: keep notification shuffle/repeat icons in sync

Commit
1ead65d0e5a47f66a0bfcbb9caeb28c9283084d3
Author
Vayun Mathur <[email protected]>
When
2026-11-07 20:13 UTC (28 days ago)
Parents
52b162e
Symptom: toggle shuffle or repeat in the app (or from a headset or
Android Auto) and the media notification kept showing the old icon.
Tapping the stale button then toggled the player's real state, so it
appeared to do nothing or the opposite of what the icon promised.

Root cause: updateNotificationButtons() ran only in onCreate and in
the notification's own custom-command handler. Nothing observed the
session player for mode changes arriving through the MediaController.

Fix: register a Player.Listener on the session player that redraws the
media-button preferences on shuffle/repeat changes, and remove it in
onDestroy before the player is released.

Verification: compile plus module unit tests after this series; the
listener is a thin forwarding to the existing redraw path.

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 a701edd375..9ebd5078fe 100644--- a/music/src/main/java/com/vayunmathur/music/service/PlaybackService.kt+++ b/music/src/main/java/com/vayunmathur/music/service/PlaybackService.kt@@ -49,6 +49,22 @@ class PlaybackService : MediaLibraryService() {     private var castQueue: CastQueue? = null     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.+     */+    private val buttonStateListener = object : Player.Listener {+        override fun onShuffleModeEnabledChanged(shuffleModeEnabled: Boolean) =+            updateNotificationButtons()++        override fun onRepeatModeChanged(repeatMode: Int) = updateNotificationButtons()+    }+     // Custom Command Constants     companion object {         const val ACTION_SHUFFLE = "ACTION_SHUFFLE"@@ -90,6 +106,8 @@ class PlaybackService : MediaLibraryService() {             mainHandler.post { notifyBrowseChildrenChanged() }         } +        player.addListener(buttonStateListener)+         // Initialize the notification buttons for the first time         updateNotificationButtons()     }@@ -243,6 +261,7 @@ class PlaybackService : MediaLibraryService() {         castQueue?.release()         castQueue = null         mediaSession?.run {+            player.removeListener(buttonStateListener)             // Releasing the wrapper releases what it wraps, so the local player goes with it.             player.release()             release()