From 05bfcafa8a6f2ac3130d365c91b12eaaaeb3a24d 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 --- .../main/java/com/ichi2/anki/worker/SyncMediaWorker.kt | 10 +++++++--- 1 file changed, 7 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 18cbceee2478..b454dcfbf695 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/worker/SyncMediaWorker.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/worker/SyncMediaWorker.kt @@ -57,7 +57,6 @@ class SyncMediaWorker( override suspend fun doWork(): Result { Timber.v("SyncMediaWorker::doWork") - trySetForeground(getForegroundInfo()) val auth = syncAuth { hkey = inputData.getString(HKEY_KEY)!! @@ -69,7 +68,12 @@ 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) { + trySetForeground(getForegroundInfo()) + monitorProgress(backend) + } } catch (cancellationException: CancellationException) { Timber.w(cancellationException) cancelMediaSync(CollectionManager.getBackend()) @@ -115,7 +119,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)