From 287a20baa9946a48de555ad14e7a0f22705280b6 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Wed, 5 Feb 2020 11:00:28 +0100 Subject: [PATCH] Display notification when sync worker is running --- .../java/com/readrops/app/ReadropsApp.java | 5 +++++ .../fragments/settings/SettingsFragment.java | 1 + .../java/com/readrops/app/utils/SyncWorker.kt | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/app/src/main/java/com/readrops/app/ReadropsApp.java b/app/src/main/java/com/readrops/app/ReadropsApp.java index e2b818a8..b61613d9 100644 --- a/app/src/main/java/com/readrops/app/ReadropsApp.java +++ b/app/src/main/java/com/readrops/app/ReadropsApp.java @@ -16,6 +16,7 @@ public class ReadropsApp extends Application { public static final String FEEDS_COLORS_CHANNEL_ID = "feedsColorsChannel"; public static final String OPML_EXPORT_CHANNEL_ID = "opmlExportChannel"; + public static final String SYNC_CHANNEL_ID = "syncChannel"; @Override public void onCreate() { @@ -44,10 +45,14 @@ public class ReadropsApp extends Application { getString(R.string.opml_export), NotificationManager.IMPORTANCE_DEFAULT); opmlExportChannel.setDescription(getString(R.string.opml_export_description)); + NotificationChannel syncChannel = new NotificationChannel(SYNC_CHANNEL_ID, + getString(R.string.auto_synchro), NotificationManager.IMPORTANCE_DEFAULT); + NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(feedsColorsChannel); manager.createNotificationChannel(opmlExportChannel); + manager.createNotificationChannel(syncChannel); } } } diff --git a/app/src/main/java/com/readrops/app/fragments/settings/SettingsFragment.java b/app/src/main/java/com/readrops/app/fragments/settings/SettingsFragment.java index f0f11fee..1215bd59 100644 --- a/app/src/main/java/com/readrops/app/fragments/settings/SettingsFragment.java +++ b/app/src/main/java/com/readrops/app/fragments/settings/SettingsFragment.java @@ -70,6 +70,7 @@ public class SettingsFragment extends PreferenceFragmentCompat { if (interval != null) { PeriodicWorkRequest request = new PeriodicWorkRequest.Builder(SyncWorker.class, interval.first, interval.second) .addTag(SyncWorker.Companion.getTAG()) + .setInitialDelay(15, TimeUnit.MINUTES) .build(); workManager.enqueueUniquePeriodicWork(SyncWorker.Companion.getTAG(), ExistingPeriodicWorkPolicy.REPLACE, request); diff --git a/app/src/main/java/com/readrops/app/utils/SyncWorker.kt b/app/src/main/java/com/readrops/app/utils/SyncWorker.kt index d99fb628..156cbc5a 100644 --- a/app/src/main/java/com/readrops/app/utils/SyncWorker.kt +++ b/app/src/main/java/com/readrops/app/utils/SyncWorker.kt @@ -1,8 +1,12 @@ package com.readrops.app.utils import android.content.Context +import androidx.core.app.NotificationCompat +import androidx.core.app.NotificationManagerCompat import androidx.work.Worker import androidx.work.WorkerParameters +import com.readrops.app.R +import com.readrops.app.ReadropsApp import com.readrops.app.repositories.ARepository import com.readrops.readropsdb.Database import io.reactivex.disposables.Disposable @@ -16,7 +20,18 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex val accounts = database.accountDao().selectAll() var result = Result.success() + val notificationBuilder = NotificationCompat.Builder(applicationContext, ReadropsApp.SYNC_CHANNEL_ID) + .setContentTitle(applicationContext.getString(R.string.auto_synchro)) + .setProgress(0, 0, true) + .setSmallIcon(R.drawable.ic_notif) + .setOnlyAlertOnce(true) + + val notificationManager = NotificationManagerCompat.from(applicationContext) + accounts.forEach { + notificationBuilder.setContentText(it.accountName) + notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build()) + val repository = ARepository.repositoryFactory(it, applicationContext) disposable = repository.sync(null) @@ -24,6 +39,8 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex .subscribe() } + notificationManager.cancel(NOTIFICATION_ID) + return result } @@ -34,5 +51,6 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex companion object { val TAG = SyncWorker::class.java.simpleName + private const val NOTIFICATION_ID = 2 } } \ No newline at end of file