瀏覽代碼

Replace backup progress dialog with notification

arkon 5 年之前
父節點
當前提交
42b536e40b

+ 6 - 0
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.
      *

+ 6 - 20
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"
     }
 }

+ 38 - 0
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()
+    }
+}