Ignore feed update threshold when run from NotificationWorker

This commit is contained in:
Koitharu 2021-09-04 14:53:11 +03:00
parent 5a6d0455ec
commit 111dc4963d
No known key found for this signature in database
GPG key ID: 8E861F8CE6E7CE27
3 changed files with 34 additions and 9 deletions

View file

@ -1,10 +1,12 @@
package org.schabi.newpipe.local.feed.notifications package org.schabi.newpipe.local.feed.notifications
import android.content.Context import android.content.Context
import androidx.core.app.NotificationCompat
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import androidx.work.BackoffPolicy import androidx.work.BackoffPolicy
import androidx.work.Constraints import androidx.work.Constraints
import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.ForegroundInfo
import androidx.work.NetworkType import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequestBuilder import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.PeriodicWorkRequest import androidx.work.PeriodicWorkRequest
@ -16,11 +18,12 @@ import io.reactivex.rxjava3.core.Single
import org.schabi.newpipe.R import org.schabi.newpipe.R
import org.schabi.newpipe.database.subscription.NotificationMode import org.schabi.newpipe.database.subscription.NotificationMode
import org.schabi.newpipe.local.feed.service.FeedLoadManager import org.schabi.newpipe.local.feed.service.FeedLoadManager
import org.schabi.newpipe.local.feed.service.FeedLoadService
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
class NotificationWorker( class NotificationWorker(
appContext: Context, appContext: Context,
workerParams: WorkerParameters workerParams: WorkerParameters,
) : RxWorker(appContext, workerParams) { ) : RxWorker(appContext, workerParams) {
private val notificationHelper by lazy { private val notificationHelper by lazy {
@ -29,7 +32,7 @@ class NotificationWorker(
private val feedLoadManager = FeedLoadManager(appContext) private val feedLoadManager = FeedLoadManager(appContext)
override fun createWork(): Single<Result> = if (isEnabled(applicationContext)) { override fun createWork(): Single<Result> = if (isEnabled(applicationContext)) {
feedLoadManager.startLoading() feedLoadManager.startLoading(ignoreOutdatedThreshold = true)
.map { feed -> .map { feed ->
feed.mapNotNull { x -> feed.mapNotNull { x ->
x.value?.takeIf { x.value?.takeIf {
@ -38,12 +41,27 @@ class NotificationWorker(
} }
} }
} }
.doOnSubscribe { setForegroundAsync(createForegroundInfo()) }
.flatMapObservable { Observable.fromIterable(it) } .flatMapObservable { Observable.fromIterable(it) }
.flatMapCompletable { x -> notificationHelper.notify(x) } .flatMapCompletable { x -> notificationHelper.notify(x) }
.toSingleDefault(Result.success()) .toSingleDefault(Result.success())
.onErrorReturnItem(Result.failure()) .onErrorReturnItem(Result.failure())
} else Single.just(Result.success()) } 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 { companion object {
private const val TAG = "streams_notifications" private const val TAG = "streams_notifications"

View file

@ -38,19 +38,26 @@ class FeedLoadManager(private val context: Context) {
} }
fun startLoading( fun startLoading(
groupId: Long = FeedGroupEntity.GROUP_ALL_ID groupId: Long = FeedGroupEntity.GROUP_ALL_ID,
ignoreOutdatedThreshold: Boolean = false,
): Single<List<Notification<FeedUpdateInfo>>> { ): Single<List<Notification<FeedUpdateInfo>>> {
val defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) val defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
val useFeedExtractor = defaultSharedPreferences.getBoolean( val useFeedExtractor = defaultSharedPreferences.getBoolean(
context.getString(R.string.feed_use_dedicated_fetch_method_key), context.getString(R.string.feed_use_dedicated_fetch_method_key),
false 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) { val subscriptions = when (groupId) {
FeedGroupEntity.GROUP_ALL_ID -> feedDatabaseManager.outdatedSubscriptions(outdatedThreshold) FeedGroupEntity.GROUP_ALL_ID -> feedDatabaseManager.outdatedSubscriptions(outdatedThreshold)

View file

@ -48,7 +48,7 @@ import java.util.concurrent.TimeUnit
class FeedLoadService : Service() { class FeedLoadService : Service() {
companion object { companion object {
private val TAG = FeedLoadService::class.java.simpleName 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" private const val ACTION_CANCEL = App.PACKAGE_NAME + ".local.feed.service.FeedLoadService.CANCEL"
/** /**