Migrate to expedited jobs
This commit is contained in:
@@ -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<SuggestionsWorker>()
|
||||
.setConstraints(constraints)
|
||||
.addTag(TAG_ONESHOT)
|
||||
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
|
||||
.build()
|
||||
WorkManager.getInstance(context)
|
||||
.enqueue(request)
|
||||
|
||||
@@ -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<TrackWorker>()
|
||||
.setConstraints(constraints)
|
||||
.addTag(TAG_ONESHOT)
|
||||
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
|
||||
.build()
|
||||
WorkManager.getInstance(context)
|
||||
.enqueue(request)
|
||||
|
||||
@@ -268,4 +268,5 @@
|
||||
<string name="content">Content</string>
|
||||
<string name="github" translatable="false">GitHub</string>
|
||||
<string name="discord" translatable="false">Discord</string>
|
||||
<string name="suggestions_updating">Suggestions updating</string>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user