2020-03-20 22:22:39 -04:00
|
|
|
package eu.kanade.tachiyomi.extension
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import androidx.core.app.NotificationCompat
|
|
|
|
import androidx.core.app.NotificationManagerCompat
|
|
|
|
import androidx.work.Constraints
|
|
|
|
import androidx.work.CoroutineWorker
|
|
|
|
import androidx.work.ExistingPeriodicWorkPolicy
|
|
|
|
import androidx.work.NetworkType
|
|
|
|
import androidx.work.PeriodicWorkRequestBuilder
|
|
|
|
import androidx.work.WorkManager
|
|
|
|
import androidx.work.WorkerParameters
|
|
|
|
import eu.kanade.tachiyomi.R
|
|
|
|
import eu.kanade.tachiyomi.data.notification.NotificationReceiver
|
|
|
|
import eu.kanade.tachiyomi.data.notification.Notifications
|
|
|
|
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
|
|
|
import eu.kanade.tachiyomi.extension.api.ExtensionGithubApi
|
2022-01-26 22:21:01 -05:00
|
|
|
import eu.kanade.tachiyomi.util.system.logcat
|
2020-03-20 22:22:39 -04:00
|
|
|
import eu.kanade.tachiyomi.util.system.notification
|
|
|
|
import kotlinx.coroutines.coroutineScope
|
2022-01-26 22:21:01 -05:00
|
|
|
import logcat.LogPriority
|
2020-03-20 22:22:39 -04:00
|
|
|
import uy.kohesive.injekt.Injekt
|
|
|
|
import uy.kohesive.injekt.api.get
|
2020-09-13 18:48:20 -04:00
|
|
|
import java.util.concurrent.TimeUnit
|
2020-03-20 22:22:39 -04:00
|
|
|
|
|
|
|
class ExtensionUpdateJob(private val context: Context, workerParams: WorkerParameters) :
|
|
|
|
CoroutineWorker(context, workerParams) {
|
|
|
|
|
|
|
|
override suspend fun doWork(): Result = coroutineScope {
|
|
|
|
val pendingUpdates = try {
|
|
|
|
ExtensionGithubApi().checkForUpdates(context)
|
|
|
|
} catch (e: Exception) {
|
2022-01-26 22:21:01 -05:00
|
|
|
logcat(LogPriority.ERROR, e)
|
2020-03-20 22:22:39 -04:00
|
|
|
return@coroutineScope Result.failure()
|
|
|
|
}
|
2020-03-20 22:53:24 -04:00
|
|
|
|
2022-03-25 11:11:16 -04:00
|
|
|
if (!pendingUpdates.isNullOrEmpty()) {
|
2020-03-20 22:53:24 -04:00
|
|
|
createUpdateNotification(pendingUpdates.map { it.name })
|
2020-03-20 22:22:39 -04:00
|
|
|
}
|
2020-03-20 22:53:24 -04:00
|
|
|
|
2020-03-20 22:22:39 -04:00
|
|
|
Result.success()
|
|
|
|
}
|
|
|
|
|
2020-03-20 22:53:24 -04:00
|
|
|
private fun createUpdateNotification(names: List<String>) {
|
|
|
|
NotificationManagerCompat.from(context).apply {
|
2020-04-25 14:24:45 -04:00
|
|
|
notify(
|
|
|
|
Notifications.ID_UPDATES_TO_EXTS,
|
2021-10-30 17:42:06 -04:00
|
|
|
context.notification(Notifications.CHANNEL_EXTENSIONS_UPDATE) {
|
2020-03-20 22:53:24 -04:00
|
|
|
setContentTitle(
|
|
|
|
context.resources.getQuantityString(
|
|
|
|
R.plurals.update_check_notification_ext_updates,
|
|
|
|
names.size,
|
|
|
|
names.size
|
|
|
|
)
|
|
|
|
)
|
|
|
|
val extNames = names.joinToString(", ")
|
|
|
|
setContentText(extNames)
|
|
|
|
setStyle(NotificationCompat.BigTextStyle().bigText(extNames))
|
|
|
|
setSmallIcon(R.drawable.ic_extension_24dp)
|
|
|
|
setContentIntent(NotificationReceiver.openExtensionsPendingActivity(context))
|
|
|
|
setAutoCancel(true)
|
2020-04-25 14:24:45 -04:00
|
|
|
}
|
|
|
|
)
|
2020-03-20 22:53:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 22:22:39 -04:00
|
|
|
companion object {
|
2020-03-20 22:53:24 -04:00
|
|
|
private const val TAG = "ExtensionUpdate"
|
2020-03-20 22:22:39 -04:00
|
|
|
|
|
|
|
fun setupTask(context: Context, forceAutoUpdateJob: Boolean? = null) {
|
|
|
|
val preferences = Injekt.get<PreferencesHelper>()
|
2020-04-17 18:30:05 -04:00
|
|
|
val autoUpdateJob = forceAutoUpdateJob ?: preferences.automaticExtUpdates().get()
|
2020-03-20 22:22:39 -04:00
|
|
|
if (autoUpdateJob) {
|
|
|
|
val constraints = Constraints.Builder()
|
|
|
|
.setRequiredNetworkType(NetworkType.CONNECTED)
|
|
|
|
.build()
|
|
|
|
|
|
|
|
val request = PeriodicWorkRequestBuilder<ExtensionUpdateJob>(
|
2021-08-13 18:24:21 -04:00
|
|
|
2,
|
|
|
|
TimeUnit.DAYS,
|
|
|
|
3,
|
2020-09-13 18:48:20 -04:00
|
|
|
TimeUnit.HOURS
|
2020-04-25 14:24:45 -04:00
|
|
|
)
|
2020-03-20 22:22:39 -04:00
|
|
|
.addTag(TAG)
|
|
|
|
.setConstraints(constraints)
|
|
|
|
.build()
|
|
|
|
|
|
|
|
WorkManager.getInstance(context).enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request)
|
|
|
|
} else {
|
|
|
|
WorkManager.getInstance(context).cancelAllWorkByTag(TAG)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|