From 8e8c30c1eb16f4740c9d3760caced0099ea014db Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 2 Aug 2020 12:16:51 -0400 Subject: [PATCH] Move download warnings/errors to separate notification channel --- .../data/download/DownloadNotifier.kt | 42 ++++++++++++------- .../data/download/DownloadService.kt | 2 +- .../data/notification/Notifications.kt | 12 +++++- app/src/main/res/values/strings.xml | 1 + 4 files changed, 40 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt index f03f08bff2..b7babaa75b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt @@ -13,7 +13,6 @@ import eu.kanade.tachiyomi.util.lang.chop import eu.kanade.tachiyomi.util.system.notificationBuilder import eu.kanade.tachiyomi.util.system.notificationManager import java.util.regex.Pattern -import uy.kohesive.injekt.api.get import uy.kohesive.injekt.injectLazy /** @@ -25,12 +24,22 @@ internal class DownloadNotifier(private val context: Context) { private val preferences: PreferencesHelper by injectLazy() - private val progressNotificationBuilder = context.notificationBuilder(Notifications.CHANNEL_DOWNLOADER_PROGRESS) { - setLargeIcon(BitmapFactory.decodeResource(context.resources, R.mipmap.ic_launcher)) + private val progressNotificationBuilder by lazy { + context.notificationBuilder(Notifications.CHANNEL_DOWNLOADER_PROGRESS) { + setLargeIcon(BitmapFactory.decodeResource(context.resources, R.mipmap.ic_launcher)) + } } - private val completeNotificationBuilder = context.notificationBuilder(Notifications.CHANNEL_DOWNLOADER_COMPLETE) { - setAutoCancel(false) + private val completeNotificationBuilder by lazy { + context.notificationBuilder(Notifications.CHANNEL_DOWNLOADER_COMPLETE) { + setAutoCancel(false) + } + } + + private val errorNotificationBuilder by lazy { + context.notificationBuilder(Notifications.CHANNEL_DOWNLOADER_ERROR) { + setAutoCancel(false) + } } /** @@ -53,7 +62,7 @@ internal class DownloadNotifier(private val context: Context) { * * @param id the id of the notification. */ - private fun NotificationCompat.Builder.show(id: Int = Notifications.ID_DOWNLOAD_CHAPTER) { + private fun NotificationCompat.Builder.show(id: Int) { context.notificationManager.notify(id, build()) } @@ -71,7 +80,7 @@ internal class DownloadNotifier(private val context: Context) { * those can only be dismissed by the user. */ fun dismiss() { - context.notificationManager.cancel(Notifications.ID_DOWNLOAD_CHAPTER) + context.notificationManager.cancel(Notifications.ID_DOWNLOAD_CHAPTER_PROGRESS) } /** @@ -112,8 +121,9 @@ internal class DownloadNotifier(private val context: Context) { } setProgress(download.pages!!.size, download.downloadedImages, false) + + show(Notifications.ID_DOWNLOAD_CHAPTER_PROGRESS) } - progressNotificationBuilder.show() } /** @@ -141,8 +151,9 @@ internal class DownloadNotifier(private val context: Context) { context.getString(R.string.action_cancel_all), NotificationReceiver.clearDownloadsPendingBroadcast(context) ) + + show(Notifications.ID_DOWNLOAD_CHAPTER_PROGRESS) } - progressNotificationBuilder.show() // Reset initial values isDownloading = false @@ -162,8 +173,9 @@ internal class DownloadNotifier(private val context: Context) { setAutoCancel(true) setContentIntent(NotificationHandler.openDownloadManagerPendingActivity(context)) setProgress(0, 0, false) + + show(Notifications.ID_DOWNLOAD_CHAPTER_COMPLETE) } - completeNotificationBuilder.show(Notifications.ID_DOWNLOAD_CHAPTER_COMPLETE) } // Reset states to default @@ -177,7 +189,7 @@ internal class DownloadNotifier(private val context: Context) { * @param reason the text to show. */ fun onWarning(reason: String) { - with(completeNotificationBuilder) { + with(errorNotificationBuilder) { setContentTitle(context.getString(R.string.download_notifier_downloader_title)) setContentText(reason) setSmallIcon(android.R.drawable.stat_sys_warning) @@ -185,8 +197,9 @@ internal class DownloadNotifier(private val context: Context) { clearActions() setContentIntent(NotificationHandler.openDownloadManagerPendingActivity(context)) setProgress(0, 0, false) + + show(Notifications.ID_DOWNLOAD_CHAPTER_ERROR) } - completeNotificationBuilder.show() // Reset download information isDownloading = false @@ -201,7 +214,7 @@ internal class DownloadNotifier(private val context: Context) { */ fun onError(error: String? = null, chapter: String? = null) { // Create notification - with(completeNotificationBuilder) { + with(errorNotificationBuilder) { setContentTitle( chapter ?: context.getString(R.string.download_notifier_downloader_title) @@ -212,8 +225,9 @@ internal class DownloadNotifier(private val context: Context) { setAutoCancel(false) setContentIntent(NotificationHandler.openDownloadManagerPendingActivity(context)) setProgress(0, 0, false) + + show(Notifications.ID_DOWNLOAD_CHAPTER_ERROR) } - completeNotificationBuilder.show(Notifications.ID_DOWNLOAD_CHAPTER_ERROR) // Reset download information errorThrown = true diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt index 07043e56a6..403ccafc15 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt @@ -83,7 +83,7 @@ class DownloadService : Service() { */ override fun onCreate() { super.onCreate() - startForeground(Notifications.ID_DOWNLOAD_CHAPTER, getPlaceholderNotification()) + startForeground(Notifications.ID_DOWNLOAD_CHAPTER_PROGRESS, getPlaceholderNotification()) wakeLock = acquireWakeLock(javaClass.name) runningRelay.call(true) subscriptions = CompositeSubscription() diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/notification/Notifications.kt b/app/src/main/java/eu/kanade/tachiyomi/data/notification/Notifications.kt index 4edf0a9d14..0bd84af86e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/notification/Notifications.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/notification/Notifications.kt @@ -32,10 +32,11 @@ object Notifications { */ private const val GROUP_DOWNLOADER = "group_downloader" const val CHANNEL_DOWNLOADER_PROGRESS = "downloader_progress_channel" - const val ID_DOWNLOAD_CHAPTER = -201 + const val ID_DOWNLOAD_CHAPTER_PROGRESS = -201 const val CHANNEL_DOWNLOADER_COMPLETE = "downloader_complete_channel" - const val ID_DOWNLOAD_CHAPTER_ERROR = -202 const val ID_DOWNLOAD_CHAPTER_COMPLETE = -203 + const val CHANNEL_DOWNLOADER_ERROR = "downloader_error_channel" + const val ID_DOWNLOAD_CHAPTER_ERROR = -202 /** * Notification channel and ids used by the library updater. @@ -104,6 +105,13 @@ object Notifications { group = GROUP_DOWNLOADER setShowBadge(false) }, + NotificationChannel( + CHANNEL_DOWNLOADER_ERROR, context.getString(R.string.channel_errors), + NotificationManager.IMPORTANCE_LOW + ).apply { + group = GROUP_DOWNLOADER + setShowBadge(false) + }, NotificationChannel( CHANNEL_NEW_CHAPTERS, context.getString(R.string.channel_new_chapters), NotificationManager.IMPORTANCE_DEFAULT diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e07c3a8a43..d1fd1f279f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -678,6 +678,7 @@ Common Progress Complete + Errors Library Downloads Backup and restore