From 9409b53490b71350d1fef7997b45a521fccec8b4 Mon Sep 17 00:00:00 2001 From: Mudit200408 Date: Sat, 23 May 2026 15:42:34 +0530 Subject: [PATCH] perf(android): optimize seekbar sync and filter out trivial media progress updates - Implement `hasSignificantMediaChange()` to perform deep metadata comparisons (playback state, title, artist, album art, buffering state, like status, and track duration). - Ignore minor progress elapsed time ticks during comparison to eliminate the CPU-intensive, every-second WebSocket synchronization loop. - Significantly reduce battery consumption, CPU overhead, and WebSocket network payloads on the Android client. --- .../service/MediaNotificationListener.kt | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/sameerasw/airsync/service/MediaNotificationListener.kt b/app/src/main/java/com/sameerasw/airsync/service/MediaNotificationListener.kt index ed8c31c5..51c1e765 100644 --- a/app/src/main/java/com/sameerasw/airsync/service/MediaNotificationListener.kt +++ b/app/src/main/java/com/sameerasw/airsync/service/MediaNotificationListener.kt @@ -61,6 +61,19 @@ class MediaNotificationListener : NotificationListenerService() { SyncManager.checkAndSyncDeviceStatus(context, forceSync = true) } + fun hasSignificantMediaChange(old: MediaInfo?, new: MediaInfo?): Boolean { + if (old == null && new == null) return false + if (old == null || new == null) return true + return old.isPlaying != new.isPlaying || + old.title != new.title || + old.artist != new.artist || + old.albumArt != new.albumArt || + old.albumArtLite != new.albumArtLite || + old.durationMs != new.durationMs || + old.isBuffering != new.isBuffering || + old.likeStatus != new.likeStatus + } + // In-memory cache of like status per track key private val likeStatusCache = LinkedHashMap(32, 0.75f, true) @@ -472,7 +485,7 @@ class MediaNotificationListener : NotificationListenerService() { updateMediaInfo() // If media info changed, trigger sync - if (previousMediaInfo != currentMediaInfo) { + if (hasSignificantMediaChange(previousMediaInfo, currentMediaInfo)) { Log.d(TAG, "Media info changed, triggering sync") SyncManager.onMediaStateChanged(this) } @@ -527,7 +540,7 @@ class MediaNotificationListener : NotificationListenerService() { updateMediaInfo() // If media info changed, trigger sync - if (previousMediaInfo != currentMediaInfo) { + if (hasSignificantMediaChange(previousMediaInfo, currentMediaInfo)) { Log.d(TAG, "Media info changed after notification removal, triggering sync") SyncManager.onMediaStateChanged(this) }