From 023311a8747f4acd62747700593bd65717b7440a Mon Sep 17 00:00:00 2001 From: arkon Date: Mon, 12 Apr 2021 13:43:46 -0400 Subject: [PATCH] Start download when tapping update notification (closes #4825) --- .../tachiyomi/data/updater/UpdaterJob.kt | 45 ++++--------------- .../tachiyomi/data/updater/UpdaterNotifier.kt | 22 +++++++++ 2 files changed, 30 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterJob.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterJob.kt index fd8d965212..6be3120e17 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterJob.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterJob.kt @@ -1,9 +1,6 @@ package eu.kanade.tachiyomi.data.updater -import android.app.PendingIntent import android.content.Context -import android.content.Intent -import androidx.core.app.NotificationCompat import androidx.work.Constraints import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.NetworkType @@ -11,52 +8,26 @@ import androidx.work.PeriodicWorkRequestBuilder import androidx.work.WorkManager import androidx.work.Worker import androidx.work.WorkerParameters -import eu.kanade.tachiyomi.R -import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.updater.github.GithubUpdateChecker -import eu.kanade.tachiyomi.util.system.notificationManager import kotlinx.coroutines.runBlocking import java.util.concurrent.TimeUnit class UpdaterJob(private val context: Context, workerParams: WorkerParameters) : Worker(context, workerParams) { - override fun doWork(): Result { - return runBlocking { - try { - val result = GithubUpdateChecker().checkForUpdate() + override fun doWork() = runBlocking { + try { + val result = GithubUpdateChecker().checkForUpdate() - if (result is UpdateResult.NewUpdate<*>) { - val url = result.release.downloadLink - - val intent = Intent(context, UpdaterService::class.java).apply { - putExtra(UpdaterService.EXTRA_DOWNLOAD_URL, url) - } - - NotificationCompat.Builder(context, Notifications.CHANNEL_COMMON).update { - setContentTitle(context.getString(R.string.app_name)) - setContentText(context.getString(R.string.update_check_notification_update_available)) - setSmallIcon(android.R.drawable.stat_sys_download_done) - // Download action - addAction( - android.R.drawable.stat_sys_download_done, - context.getString(R.string.action_download), - PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) - ) - } - } - Result.success() - } catch (e: Exception) { - Result.failure() + if (result is UpdateResult.NewUpdate<*>) { + UpdaterNotifier(context).promptUpdate(result.release.downloadLink) } + Result.success() + } catch (e: Exception) { + Result.failure() } } - fun NotificationCompat.Builder.update(block: NotificationCompat.Builder.() -> Unit) { - block() - context.notificationManager.notify(Notifications.ID_UPDATER, build()) - } - companion object { private const val TAG = "UpdateChecker" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterNotifier.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterNotifier.kt index b054e6c869..2d8d278032 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterNotifier.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterNotifier.kt @@ -1,6 +1,8 @@ package eu.kanade.tachiyomi.data.updater +import android.app.PendingIntent import android.content.Context +import android.content.Intent import android.net.Uri import androidx.core.app.NotificationCompat import eu.kanade.tachiyomi.R @@ -28,6 +30,26 @@ internal class UpdaterNotifier(private val context: Context) { context.notificationManager.notify(id, build()) } + fun promptUpdate(url: String) { + val intent = Intent(context, UpdaterService::class.java).apply { + putExtra(UpdaterService.EXTRA_DOWNLOAD_URL, url) + } + val pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) + with(notificationBuilder) { + setContentTitle(context.getString(R.string.app_name)) + setContentText(context.getString(R.string.update_check_notification_update_available)) + setSmallIcon(android.R.drawable.stat_sys_download_done) + setContentIntent(pendingIntent) + // Download action + addAction( + android.R.drawable.stat_sys_download_done, + context.getString(R.string.action_download), + pendingIntent + ) + } + notificationBuilder.show() + } + /** * Call when apk download starts. *