From cca6d5fa040a6c11a9b9be88db6199728ebc5c1a Mon Sep 17 00:00:00 2001 From: Koitharu Date: Wed, 6 Apr 2022 17:50:08 +0300 Subject: [PATCH] Migrate to expedited jobs --- .../suggestions/ui/SuggestionsWorker.kt | 44 ++++++++++++++++++- .../kotatsu/tracker/work/TrackWorker.kt | 5 ++- app/src/main/res/values/strings.xml | 1 + 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/koitharu/kotatsu/suggestions/ui/SuggestionsWorker.kt b/app/src/main/java/org/koitharu/kotatsu/suggestions/ui/SuggestionsWorker.kt index 4420d4481..fe6b73ffb 100644 --- a/app/src/main/java/org/koitharu/kotatsu/suggestions/ui/SuggestionsWorker.kt +++ b/app/src/main/java/org/koitharu/kotatsu/suggestions/ui/SuggestionsWorker.kt @@ -1,9 +1,17 @@ package org.koitharu.kotatsu.suggestions.ui +import android.app.NotificationChannel +import android.app.NotificationManager import android.content.Context +import android.os.Build +import androidx.core.app.NotificationCompat +import androidx.core.content.ContextCompat import androidx.work.* +import java.util.concurrent.TimeUnit +import kotlin.math.pow import org.koin.core.component.KoinComponent import org.koin.core.component.inject +import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.parser.MangaRepository import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.history.domain.HistoryRepository @@ -11,8 +19,6 @@ import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.SortOrder import org.koitharu.kotatsu.suggestions.domain.MangaSuggestion import org.koitharu.kotatsu.suggestions.domain.SuggestionRepository -import java.util.concurrent.TimeUnit -import kotlin.math.pow class SuggestionsWorker(appContext: Context, params: WorkerParameters) : CoroutineWorker(appContext, params), KoinComponent { @@ -28,6 +34,37 @@ class SuggestionsWorker(appContext: Context, params: WorkerParameters) : Result.failure() } + override suspend fun getForegroundInfo(): ForegroundInfo { + val manager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + val title = applicationContext.getString(R.string.suggestions_updating) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val channel = NotificationChannel( + WORKER_CHANNEL_ID, + title, + NotificationManager.IMPORTANCE_LOW + ) + channel.setShowBadge(false) + channel.enableVibration(false) + channel.setSound(null, null) + channel.enableLights(false) + manager.createNotificationChannel(channel) + } + + val notification = NotificationCompat.Builder(applicationContext, WORKER_CHANNEL_ID) + .setContentTitle(title) + .setPriority(NotificationCompat.PRIORITY_MIN) + .setDefaults(0) + .setColor(ContextCompat.getColor(applicationContext, R.color.blue_primary_dark)) + .setSilent(true) + .setProgress(0, 0, true) + .setSmallIcon(android.R.drawable.stat_notify_sync) + .setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_DEFERRED) + .setOngoing(true) + .build() + + return ForegroundInfo(WORKER_NOTIFICATION_ID, notification) + } + private suspend fun doWorkImpl(): Int { if (!appSettings.isSuggestionsEnabled) { suggestionRepository.clear() @@ -74,6 +111,8 @@ class SuggestionsWorker(appContext: Context, params: WorkerParameters) : private const val TAG_ONESHOT = "suggestions_oneshot" private const val LIMIT = 140 private const val DATA_COUNT = "count" + private const val WORKER_CHANNEL_ID = "suggestion_worker" + private const val WORKER_NOTIFICATION_ID = 36 fun setup(context: Context) { val constraints = Constraints.Builder() @@ -96,6 +135,7 @@ class SuggestionsWorker(appContext: Context, params: WorkerParameters) : val request = OneTimeWorkRequestBuilder() .setConstraints(constraints) .addTag(TAG_ONESHOT) + .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) .build() WorkManager.getInstance(context) .enqueue(request) diff --git a/app/src/main/java/org/koitharu/kotatsu/tracker/work/TrackWorker.kt b/app/src/main/java/org/koitharu/kotatsu/tracker/work/TrackWorker.kt index 04ad1695b..1144bcce0 100644 --- a/app/src/main/java/org/koitharu/kotatsu/tracker/work/TrackWorker.kt +++ b/app/src/main/java/org/koitharu/kotatsu/tracker/work/TrackWorker.kt @@ -53,7 +53,7 @@ class TrackWorker(context: Context, workerParams: WorkerParameters) : if (tracks.isEmpty()) { return Result.success() } - setForeground(createForegroundInfo()) + setForeground(getForegroundInfo()) var success = 0 val workData = Data.Builder() .putInt(DATA_TOTAL, tracks.size) @@ -201,7 +201,7 @@ class TrackWorker(context: Context, workerParams: WorkerParameters) : } } - private fun createForegroundInfo(): ForegroundInfo { + override suspend fun getForegroundInfo(): ForegroundInfo { val title = applicationContext.getString(R.string.check_for_new_chapters) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel( @@ -281,6 +281,7 @@ class TrackWorker(context: Context, workerParams: WorkerParameters) : val request = OneTimeWorkRequestBuilder() .setConstraints(constraints) .addTag(TAG_ONESHOT) + .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) .build() WorkManager.getInstance(context) .enqueue(request) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index aeace4628..6a880bd56 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -268,4 +268,5 @@ Content GitHub Discord + Suggestions updating \ No newline at end of file