From 2661700d9a1e41f0a516b7cb7c375c112456f011 Mon Sep 17 00:00:00 2001 From: arkon Date: Wed, 29 Apr 2020 22:30:33 -0400 Subject: [PATCH] Split out backup/restore notifications into separate channels for progress/completion --- .../tachiyomi/data/backup/BackupNotifier.kt | 54 +++++++------------ .../data/notification/Notifications.kt | 20 +++++-- app/src/main/res/values/strings.xml | 2 + 3 files changed, 39 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupNotifier.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupNotifier.kt index 91654181ee..a731f51267 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupNotifier.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupNotifier.kt @@ -19,7 +19,14 @@ internal class BackupNotifier(private val context: Context) { private val preferences: PreferencesHelper by injectLazy() - private val notificationBuilder = context.notificationBuilder(Notifications.CHANNEL_BACKUP_RESTORE) { + private val progressNotificationBuilder = context.notificationBuilder(Notifications.CHANNEL_BACKUP_RESTORE_PROGRESS) { + setLargeIcon(BitmapFactory.decodeResource(context.resources, R.mipmap.ic_launcher)) + setSmallIcon(R.drawable.ic_tachi) + setAutoCancel(false) + setOngoing(true) + } + + private val completeNotificationBuilder = context.notificationBuilder(Notifications.CHANNEL_BACKUP_RESTORE_COMPLETE) { setLargeIcon(BitmapFactory.decodeResource(context.resources, R.mipmap.ic_launcher)) setSmallIcon(R.drawable.ic_tachi) setAutoCancel(false) @@ -30,11 +37,10 @@ internal class BackupNotifier(private val context: Context) { } fun showBackupProgress(): NotificationCompat.Builder { - val builder = with(notificationBuilder) { + val builder = with(progressNotificationBuilder) { setContentTitle(context.getString(R.string.creating_backup)) setProgress(0, 0, true) - setOngoing(true) } builder.show(Notifications.ID_BACKUP_PROGRESS) @@ -45,32 +51,24 @@ internal class BackupNotifier(private val context: Context) { fun showBackupError(error: String?) { context.notificationManager.cancel(Notifications.ID_BACKUP_PROGRESS) - with(notificationBuilder) { + with(completeNotificationBuilder) { setContentTitle(context.getString(R.string.creating_backup_error)) setContentText(error) - // Remove progress bar - setProgress(0, 0, false) - setOngoing(false) + show(Notifications.ID_BACKUP_COMPLETE) } - - notificationBuilder.show(Notifications.ID_BACKUP_COMPLETE) } fun showBackupComplete(unifile: UniFile) { context.notificationManager.cancel(Notifications.ID_BACKUP_PROGRESS) - with(notificationBuilder) { + with(completeNotificationBuilder) { setContentTitle(context.getString(R.string.backup_created)) if (unifile.filePath != null) { setContentText(unifile.filePath) } - // Remove progress bar - setProgress(0, 0, false) - setOngoing(false) - // Clear old actions if they exist if (mActions.isNotEmpty()) { mActions.clear() @@ -81,13 +79,13 @@ internal class BackupNotifier(private val context: Context) { context.getString(R.string.action_share), NotificationReceiver.shareBackupPendingBroadcast(context, unifile.uri, Notifications.ID_BACKUP_COMPLETE) ) - } - notificationBuilder.show(Notifications.ID_BACKUP_COMPLETE) + show(Notifications.ID_BACKUP_COMPLETE) + } } fun showRestoreProgress(content: String = "", progress: Int = 0, maxAmount: Int = 100): NotificationCompat.Builder { - val builder = with(notificationBuilder) { + val builder = with(progressNotificationBuilder) { setContentTitle(context.getString(R.string.restoring_backup)) if (!preferences.hideNotificationContent()) { @@ -95,7 +93,6 @@ internal class BackupNotifier(private val context: Context) { } setProgress(maxAmount, progress, false) - setOngoing(true) // Clear old actions if they exist if (mActions.isNotEmpty()) { @@ -117,16 +114,12 @@ internal class BackupNotifier(private val context: Context) { fun showRestoreError(error: String?) { context.notificationManager.cancel(Notifications.ID_RESTORE_PROGRESS) - with(notificationBuilder) { + with(completeNotificationBuilder) { setContentTitle(context.getString(R.string.restoring_backup_error)) setContentText(error) - // Remove progress bar - setProgress(0, 0, false) - setOngoing(false) + show(Notifications.ID_RESTORE_COMPLETE) } - - notificationBuilder.show(Notifications.ID_RESTORE_COMPLETE) } fun showRestoreComplete(time: Long, errorCount: Int, path: String?, file: String?) { @@ -140,17 +133,10 @@ internal class BackupNotifier(private val context: Context) { ) ) - with(notificationBuilder) { - setSmallIcon(R.drawable.ic_tachi) - setAutoCancel(false) - + with(completeNotificationBuilder) { setContentTitle(context.getString(R.string.restore_completed)) setContentText(context.getString(R.string.restore_completed_content, timeString, errorCount)) - // Remove progress bar - setProgress(0, 0, false) - setOngoing(false) - // Clear old actions if they exist if (mActions.isNotEmpty()) { mActions.clear() @@ -166,8 +152,8 @@ internal class BackupNotifier(private val context: Context) { NotificationReceiver.openErrorLogPendingActivity(context, uri) ) } - } - notificationBuilder.show(Notifications.ID_RESTORE_COMPLETE) + show(Notifications.ID_RESTORE_COMPLETE) + } } } 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 3192590e1f..184ef92baa 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 @@ -1,6 +1,7 @@ package eu.kanade.tachiyomi.data.notification import android.app.NotificationChannel +import android.app.NotificationChannelGroup import android.app.NotificationManager import android.content.Context import android.os.Build @@ -48,10 +49,12 @@ object Notifications { /** * Notification channel and ids used by the backup/restore system. */ - const val CHANNEL_BACKUP_RESTORE = "backup_restore_channel" + private const val GROUP_BACK_RESTORE = "group_backup_restore" + const val CHANNEL_BACKUP_RESTORE_PROGRESS = "backup_restore_progress_channel" const val ID_BACKUP_PROGRESS = -501 - const val ID_BACKUP_COMPLETE = -502 const val ID_RESTORE_PROGRESS = -503 + const val CHANNEL_BACKUP_RESTORE_COMPLETE = "backup_restore_complete_channel" + const val ID_BACKUP_COMPLETE = -502 const val ID_RESTORE_COMPLETE = -504 /** @@ -62,6 +65,9 @@ object Notifications { fun createChannels(context: Context) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return + val backupRestoreGroup = NotificationChannelGroup(GROUP_BACK_RESTORE, context.getString(R.string.channel_backup_restore)) + context.notificationManager.createNotificationChannelGroup(backupRestoreGroup) + val channels = listOf( NotificationChannel( CHANNEL_COMMON, context.getString(R.string.channel_common), @@ -88,9 +94,17 @@ object Notifications { NotificationManager.IMPORTANCE_DEFAULT ), NotificationChannel( - CHANNEL_BACKUP_RESTORE, context.getString(R.string.channel_backup_restore), + CHANNEL_BACKUP_RESTORE_PROGRESS, context.getString(R.string.channel_backup_restore_progress), + NotificationManager.IMPORTANCE_DEFAULT + ).apply { + group = GROUP_BACK_RESTORE + setShowBadge(false) + }, + NotificationChannel( + CHANNEL_BACKUP_RESTORE_COMPLETE, context.getString(R.string.channel_backup_restore_complete), NotificationManager.IMPORTANCE_HIGH ).apply { + group = GROUP_BACK_RESTORE setShowBadge(false) } ) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6ad4fa47eb..e89e431c4a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -630,5 +630,7 @@ Chapter updates Extension updates Backup and restore + Progress + Complete