Skip to content

Native Android client for Spotify, built with Jetpack Compose

The media session and the notification follow real changes only

kota-zo6100869083de
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3e3cdbc8..9b220adf 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,7 @@ Each release on GitHub shows its section below. The app shows it too when it off
## Unreleased
+- **Notification and lock screen no longer churn**: only a real change (song, play/pause, buffering, heart, shuffle) reaches the media session and the notification; the position is extrapolated by the system in between.
- **Skips a little faster and less work while music plays**: a fade is one call to the page instead of three (the ramp runs in the page while the command is on its way), the volume check comes with the media read instead of polling every 6 s, and the watcher looks every 4 s instead of 2 s when nothing plays.
- **Hearts (liked states) are asked once**: a page and its prefetch share the same request, and what Spotify confirmed less than a minute ago is not asked again (opening a playlist asked the whole list twice, then again for each page of tracks).
- **Far fewer calls to the player page**: "ready" is announced once per page load (twice meant every listener of it ran twice: state, token, quality, volume), the web token is fetched once and shared, and the Connect device list is read at most every 6 s in the page instead of every 2 s.
diff --git a/base/host-android/src/main/kotlin/app/myspoti/host/android/PlaybackService.kt b/base/host-android/src/main/kotlin/app/myspoti/host/android/PlaybackService.kt
index f3e99759..34c0ad6b 100644
--- a/base/host-android/src/main/kotlin/app/myspoti/host/android/PlaybackService.kt
+++ b/base/host-android/src/main/kotlin/app/myspoti/host/android/PlaybackService.kt
@@ -45,7 +45,12 @@ data class NowPlayingInfo(
val buffering: Boolean = false,
val liked: Boolean = false,
val shuffle: Boolean = false,
-)
+) {
+ /** Everything but the position: a moving position alone is not a new state. */
+ fun sameAs(other: NowPlayingInfo) = id == other.id && title == other.title && artist == other.artist &&
+ album == other.album && coverUrl == other.coverUrl && durationMs == other.durationMs &&
+ playing == other.playing && buffering == other.buffering && liked == other.liked && shuffle == other.shuffle
+}
/** Commands from the system (notification buttons, headset, car, lock screen). */
interface PlaybackControls {
@@ -344,6 +349,8 @@ class PlaybackService : MediaBrowserServiceCompat() {
@Volatile private var current: NowPlayingInfo? = null
private const val PAUSE_GRACE_MS = 60_000L
+ /** A state that did not change is republished at most that often (see [update]). */
+ private const val POSITION_REPUSH_MS = 10_000L
/** Packages allowed to browse the library (see onGetRoot). */
private val ALLOWED_BROWSERS = setOf(
@@ -363,6 +370,7 @@ class PlaybackService : MediaBrowserServiceCompat() {
/** Pushes the now-playing state; starts the service when playback starts. */
fun update(context: Context, info: NowPlayingInfo) {
+ val before = current
current = info
val svc = instance
if (svc == null) {
@@ -371,6 +379,12 @@ class PlaybackService : MediaBrowserServiceCompat() {
}
return
}
+ // The player reports several times per second (position, buffering): only a real
+ // change reaches the session and the notification, the system extrapolates the
+ // position in between (the known state is republished every POSITION_REPUSH_MS).
+ if (before != null && before.sameAs(info) &&
+ kotlin.math.abs(info.positionMs - before.positionMs) < POSITION_REPUSH_MS
+ ) return
svc.apply(info)
svc.show(info)
}