From 42b536e40be03982c30660aab95320c6c4c92e3e Mon Sep 17 00:00:00 2001 From: arkon Date: Mon, 20 Apr 2020 23:11:02 -0400 Subject: [PATCH] Replace backup progress dialog with notification --- .../data/notification/Notifications.kt | 6 +++ .../ui/setting/SettingsBackupController.kt | 26 +++---------- .../ui/setting/backup/BackupNotifier.kt | 38 +++++++++++++++++++ 3 files changed, 50 insertions(+), 20 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/setting/backup/BackupNotifier.kt 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 b9b2f98e1..1f4e75178 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 @@ -45,6 +45,12 @@ object Notifications { const val CHANNEL_UPDATES_TO_EXTS = "updates_ext_channel" const val ID_UPDATES_TO_EXTS = -401 + /** + * Notification channel and ids used by the backup/restore system. + */ + const val CHANNEL_BACKUP = "backup_channel" + const val ID_BACKUP = -501 + /** * Creates the notification channels introduced in Android Oreo. * diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt index a683895a6..387af45e1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt @@ -25,6 +25,7 @@ import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.ui.base.controller.popControllerWithTag import eu.kanade.tachiyomi.ui.base.controller.requestPermissionsSafe +import eu.kanade.tachiyomi.ui.setting.backup.BackupNotifier import eu.kanade.tachiyomi.util.preference.defaultValue import eu.kanade.tachiyomi.util.preference.entriesRes import eu.kanade.tachiyomi.util.preference.intListPreference @@ -49,6 +50,8 @@ class SettingsBackupController : SettingsController() { */ private var backupFlags = 0 + private val notifier by lazy { BackupNotifier(preferences.context) } + private val receiver = BackupBroadcastReceiver() init { @@ -179,7 +182,7 @@ class SettingsBackupController : SettingsController() { val file = UniFile.fromUri(activity, uri) - CreatingBackupDialog().showDialog(router, TAG_CREATING_BACKUP_DIALOG) + notifier.showBackupNotification() BackupCreateService.makeBackup(activity, file.uri, backupFlags) } CODE_BACKUP_RESTORE -> if (data != null && resultCode == Activity.RESULT_OK) { @@ -244,22 +247,6 @@ class SettingsBackupController : SettingsController() { } } - class CreatingBackupDialog : DialogController() { - override fun onCreateDialog(savedViewState: Bundle?): Dialog { - return MaterialDialog.Builder(activity!!) - .title(R.string.backup) - .content(R.string.creating_backup) - .progress(true, 0) - .cancelable(false) - .build() - } - - override fun onRestoreInstanceState(savedInstanceState: Bundle) { - super.onRestoreInstanceState(savedInstanceState) - router.popController(this) - } - } - class CreatedBackupDialog(bundle: Bundle? = null) : DialogController(bundle) { constructor(uri: Uri) : this(Bundle().apply { putParcelable(KEY_URI, uri) @@ -405,7 +392,7 @@ class SettingsBackupController : SettingsController() { override fun onReceive(context: Context, intent: Intent) { when (intent.getStringExtra(BackupConst.ACTION)) { BackupConst.ACTION_BACKUP_COMPLETED_DIALOG -> { - router.popControllerWithTag(TAG_CREATING_BACKUP_DIALOG) + notifier.dismiss() val uri = Uri.parse(intent.getStringExtra(BackupConst.EXTRA_URI)) CreatedBackupDialog(uri).showDialog(router) } @@ -427,7 +414,7 @@ class SettingsBackupController : SettingsController() { } } BackupConst.ACTION_ERROR_BACKUP_DIALOG -> { - router.popControllerWithTag(TAG_CREATING_BACKUP_DIALOG) + notifier.dismiss() context.toast(intent.getStringExtra(BackupConst.EXTRA_ERROR_MESSAGE)) } BackupConst.ACTION_ERROR_RESTORE_DIALOG -> { @@ -443,7 +430,6 @@ class SettingsBackupController : SettingsController() { const val CODE_BACKUP_RESTORE = 502 const val CODE_BACKUP_DIR = 503 - const val TAG_CREATING_BACKUP_DIALOG = "CreatingBackupDialog" const val TAG_RESTORING_BACKUP_DIALOG = "RestoringBackupDialog" } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/backup/BackupNotifier.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/backup/BackupNotifier.kt new file mode 100644 index 000000000..6cca68cbe --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/backup/BackupNotifier.kt @@ -0,0 +1,38 @@ +package eu.kanade.tachiyomi.ui.setting.backup + +import android.content.Context +import android.graphics.BitmapFactory +import androidx.core.app.NotificationCompat +import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.notification.Notifications +import eu.kanade.tachiyomi.util.system.notificationBuilder +import eu.kanade.tachiyomi.util.system.notificationManager + +internal class BackupNotifier(private val context: Context) { + + private val notificationBuilder = context.notificationBuilder(Notifications.CHANNEL_DOWNLOADER) { + setLargeIcon(BitmapFactory.decodeResource(context.resources, R.mipmap.ic_launcher)) + } + + private fun NotificationCompat.Builder.show(id: Int = Notifications.ID_BACKUP) { + context.notificationManager.notify(id, build()) + } + + fun dismiss() { + context.notificationManager.cancel(Notifications.ID_BACKUP) + } + + fun showBackupNotification() { + with(notificationBuilder) { + setSmallIcon(R.drawable.ic_tachi) + setAutoCancel(false) + + setContentTitle(context.getString(R.string.backup)) + setContentText(context.getString(R.string.creating_backup)) + + setProgress(0, 0, true) + } + + notificationBuilder.show() + } +}