From 855331aafac2461c182fbcb4c0755c7e31beec13 Mon Sep 17 00:00:00 2001 From: Alexander Capehart Date: Fri, 9 Sep 2022 20:45:14 -0600 Subject: [PATCH] system: update indexing notif every few seconds Instead of updating the notif every 50 songs, update it every 1.5s. This is in preparation for automatic reloading, which will once again make Indexed song updates EXTREMELY fast and would thus rate-limit the notification if the previous system was used. --- .../music/system/IndexerNotifications.kt | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/org/oxycblt/auxio/music/system/IndexerNotifications.kt b/app/src/main/java/org/oxycblt/auxio/music/system/IndexerNotifications.kt index 10c875a73..71a1530eb 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/system/IndexerNotifications.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/system/IndexerNotifications.kt @@ -18,6 +18,7 @@ package org.oxycblt.auxio.music.system import android.content.Context +import android.os.SystemClock import androidx.core.app.NotificationCompat import org.oxycblt.auxio.BuildConfig import org.oxycblt.auxio.IntegerTable @@ -29,6 +30,8 @@ import org.oxycblt.auxio.util.newMainPendingIntent /** The notification responsible for showing the indexer state. */ class IndexingNotification(private val context: Context) : ServiceNotification(context, INDEXER_CHANNEL) { + private var lastUpdateTime: Int = -1 + init { setSmallIcon(R.drawable.ic_indexer_24) setCategory(NotificationCompat.CATEGORY_PROGRESS) @@ -53,20 +56,20 @@ class IndexingNotification(private val context: Context) : return true } is Indexer.Indexing.Songs -> { - // Only update the notification every 50 songs to prevent excessive updates. - // TODO: Use a timeout instead to handle rapid-fire updates w/o rate limiting - if (indexing.current % 50 == 0) { - logD("Updating state to $indexing") - setContentText( - context.getString(R.string.fmt_indexing, indexing.current, indexing.total) - ) - setProgress(indexing.total, indexing.current, false) - return true + val now = SystemClock.elapsedRealtime() + if (lastUpdateTime != -1 && (now - lastUpdateTime) < 1500) { + return false } + + // Only update the notification every two seconds to prevent rate-limiting. + logD("Updating state to $indexing") + setContentText( + context.getString(R.string.fmt_indexing, indexing.current, indexing.total) + ) + setProgress(indexing.total, indexing.current, false) + return true } } - - return false } }