From 111dc4963dab4de87ae08587b1dd44b6b986ab74 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sat, 4 Sep 2021 14:53:11 +0300 Subject: [PATCH] Ignore feed update threshold when run from NotificationWorker --- .../feed/notifications/NotificationWorker.kt | 22 +++++++++++++++++-- .../local/feed/service/FeedLoadManager.kt | 19 +++++++++++----- .../local/feed/service/FeedLoadService.kt | 2 +- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationWorker.kt b/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationWorker.kt index 896735983..ee16a403d 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationWorker.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationWorker.kt @@ -1,10 +1,12 @@ package org.schabi.newpipe.local.feed.notifications import android.content.Context +import androidx.core.app.NotificationCompat import androidx.preference.PreferenceManager import androidx.work.BackoffPolicy import androidx.work.Constraints import androidx.work.ExistingPeriodicWorkPolicy +import androidx.work.ForegroundInfo import androidx.work.NetworkType import androidx.work.OneTimeWorkRequestBuilder import androidx.work.PeriodicWorkRequest @@ -16,11 +18,12 @@ import io.reactivex.rxjava3.core.Single import org.schabi.newpipe.R import org.schabi.newpipe.database.subscription.NotificationMode import org.schabi.newpipe.local.feed.service.FeedLoadManager +import org.schabi.newpipe.local.feed.service.FeedLoadService import java.util.concurrent.TimeUnit class NotificationWorker( appContext: Context, - workerParams: WorkerParameters + workerParams: WorkerParameters, ) : RxWorker(appContext, workerParams) { private val notificationHelper by lazy { @@ -29,7 +32,7 @@ class NotificationWorker( private val feedLoadManager = FeedLoadManager(appContext) override fun createWork(): Single = if (isEnabled(applicationContext)) { - feedLoadManager.startLoading() + feedLoadManager.startLoading(ignoreOutdatedThreshold = true) .map { feed -> feed.mapNotNull { x -> x.value?.takeIf { @@ -38,12 +41,27 @@ class NotificationWorker( } } } + .doOnSubscribe { setForegroundAsync(createForegroundInfo()) } .flatMapObservable { Observable.fromIterable(it) } .flatMapCompletable { x -> notificationHelper.notify(x) } .toSingleDefault(Result.success()) .onErrorReturnItem(Result.failure()) } else Single.just(Result.success()) + private fun createForegroundInfo(): ForegroundInfo { + val notification = NotificationCompat.Builder( + applicationContext, + applicationContext.getString(R.string.notification_channel_id) + ).setOngoing(true) + .setProgress(-1, -1, true) + .setSmallIcon(R.drawable.ic_newpipe_triangle_white) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + .setPriority(NotificationCompat.PRIORITY_LOW) + .setContentTitle(applicationContext.getString(R.string.feed_notification_loading)) + .build() + return ForegroundInfo(FeedLoadService.NOTIFICATION_ID, notification) + } + companion object { private const val TAG = "streams_notifications" diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadManager.kt b/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadManager.kt index 79c4b747b..bea699999 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadManager.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadManager.kt @@ -38,19 +38,26 @@ class FeedLoadManager(private val context: Context) { } fun startLoading( - groupId: Long = FeedGroupEntity.GROUP_ALL_ID + groupId: Long = FeedGroupEntity.GROUP_ALL_ID, + ignoreOutdatedThreshold: Boolean = false, ): Single>> { val defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) val useFeedExtractor = defaultSharedPreferences.getBoolean( context.getString(R.string.feed_use_dedicated_fetch_method_key), false ) - val thresholdOutdatedSeconds = defaultSharedPreferences.getString( - context.getString(R.string.feed_update_threshold_key), - context.getString(R.string.feed_update_threshold_default_value) - )!!.toInt() - val outdatedThreshold = OffsetDateTime.now(ZoneOffset.UTC).minusSeconds(thresholdOutdatedSeconds.toLong()) + val outdatedThreshold = if (ignoreOutdatedThreshold) { + OffsetDateTime.now(ZoneOffset.UTC) + } else { + val thresholdOutdatedSeconds = ( + defaultSharedPreferences.getString( + context.getString(R.string.feed_update_threshold_key), + context.getString(R.string.feed_update_threshold_default_value) + ) ?: context.getString(R.string.feed_update_threshold_default_value) + ).toInt() + OffsetDateTime.now(ZoneOffset.UTC).minusSeconds(thresholdOutdatedSeconds.toLong()) + } val subscriptions = when (groupId) { FeedGroupEntity.GROUP_ALL_ID -> feedDatabaseManager.outdatedSubscriptions(outdatedThreshold) diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadService.kt b/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadService.kt index ea181d3d9..0d5d904e8 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadService.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadService.kt @@ -48,7 +48,7 @@ import java.util.concurrent.TimeUnit class FeedLoadService : Service() { companion object { private val TAG = FeedLoadService::class.java.simpleName - private const val NOTIFICATION_ID = 7293450 + const val NOTIFICATION_ID = 7293450 private const val ACTION_CANCEL = App.PACKAGE_NAME + ".local.feed.service.FeedLoadService.CANCEL" /**