Split out backup/restore notifications into separate channels for progress/completion

This commit is contained in:
arkon 2020-04-29 22:30:33 -04:00
parent 02502c4ea5
commit 2661700d9a
3 changed files with 39 additions and 37 deletions

View File

@ -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)
}
}
}

View File

@ -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)
}
)

View File

@ -630,5 +630,7 @@
<string name="channel_new_chapters">Chapter updates</string>
<string name="channel_ext_updates">Extension updates</string>
<string name="channel_backup_restore">Backup and restore</string>
<string name="channel_backup_restore_progress">Progress</string>
<string name="channel_backup_restore_complete">Complete</string>
</resources>