From aa8462f219189cc80b711f878559ff6b5a0f033b Mon Sep 17 00:00:00 2001 From: Brayan Oliveira <69634269+brayandso@users.noreply.github.com> Date: Fri, 29 Mar 2024 10:25:34 -0300 Subject: [PATCH] feat: delay media sync notification to avoid showing it if sync is too fast, which can occur when there is nothing to sync --- .../java/com/ichi2/anki/worker/SyncMediaWorker.kt | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/worker/SyncMediaWorker.kt b/AnkiDroid/src/main/java/com/ichi2/anki/worker/SyncMediaWorker.kt index aed84dc14935..60b45672acc8 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/worker/SyncMediaWorker.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/worker/SyncMediaWorker.kt @@ -56,7 +56,6 @@ class SyncMediaWorker( override suspend fun doWork(): Result { Timber.v("SyncMediaWorker::doWork") - trySetForeground(getForegroundInfo()) val auth = syncAuth { hkey = inputData.getString(HKEY_KEY)!! @@ -68,7 +67,13 @@ class SyncMediaWorker( try { val backend = CollectionManager.getBackend() backend.syncMedia(auth) - monitorProgress(backend) + + delay(1000) // avoid notifications if sync occurs too quickly + if (backend.mediaSyncStatus().active) { + Timber.i("Showing SyncMediaWorker's notification") + trySetForeground(getForegroundInfo()) + monitorProgress(backend) + } } catch (cancellationException: CancellationException) { Timber.w(cancellationException) cancelMediaSync(CollectionManager.getBackend()) @@ -113,7 +118,7 @@ class SyncMediaWorker( setOngoing(true) setProgress(0, 0, true) addAction(R.drawable.close_icon, cancelTitle, cancelIntent) - foregroundServiceBehavior = NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE + foregroundServiceBehavior = NotificationCompat.FOREGROUND_SERVICE_DEFERRED } return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { ForegroundInfo(NotificationId.SYNC_MEDIA, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)