From f69f78db34994708ff28f33072c8d56bc0f4e71f Mon Sep 17 00:00:00 2001 From: arkon Date: Tue, 21 Apr 2020 21:53:15 -0400 Subject: [PATCH] Move restore progress dialog to notification --- .../tachiyomi/data/backup/BackupManager.kt | 17 +------ .../data/backup/BackupRestoreService.kt | 8 ++- .../data/notification/NotificationReceiver.kt | 36 ++++++++++++- .../ui/setting/SettingsBackupController.kt | 50 ++----------------- .../ui/setting/backup/BackupNotifier.kt | 24 +++++++-- app/src/main/res/values/strings.xml | 2 +- 6 files changed, 68 insertions(+), 69 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupManager.kt index 41a8abe7fb..9b86bb6742 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupManager.kt @@ -58,20 +58,10 @@ import uy.kohesive.injekt.injectLazy class BackupManager(val context: Context, version: Int = CURRENT_VERSION) { - /** - * Database. - */ internal val databaseHelper: DatabaseHelper by injectLazy() - - /** - * Source manager. - */ internal val sourceManager: SourceManager by injectLazy() - - /** - * Tracking manager - */ internal val trackManager: TrackManager by injectLazy() + private val preferences: PreferencesHelper by injectLazy() /** * Version of parser @@ -84,11 +74,6 @@ class BackupManager(val context: Context, version: Int = CURRENT_VERSION) { */ var parser: Gson = initParser() - /** - * Preferences - */ - private val preferences: PreferencesHelper by injectLazy() - /** * Set version of parser * diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt index 234c10ac60..3bd2998bbc 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt @@ -81,6 +81,12 @@ class BackupRestoreService : Service() { */ fun stop(context: Context) { context.stopService(Intent(context, BackupRestoreService::class.java)) + + val errorIntent = Intent(BackupConst.INTENT_FILTER).apply { + putExtra(BackupConst.ACTION, BackupConst.ACTION_RESTORE_ERROR) + putExtra(BackupConst.EXTRA_ERROR_MESSAGE, context.getString(R.string.restoring_backup_canceled)) + } + context.sendLocalBroadcast(errorIntent) } } @@ -456,7 +462,7 @@ class BackupRestoreService : Service() { amount: Int, title: String, errors: Int, - content: String = getString(R.string.dialog_restoring_backup, title.chop(15)) + content: String = title.chop(30) ) { val intent = Intent(BackupConst.INTENT_FILTER).apply { putExtra(BackupConst.EXTRA_PROGRESS, progress) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt b/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt index 9a8d4ae406..0fbbdf4a26 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt @@ -9,6 +9,7 @@ import android.os.Build import android.os.Handler import eu.kanade.tachiyomi.BuildConfig.APPLICATION_ID as ID import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.backup.BackupRestoreService import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Manga @@ -61,6 +62,8 @@ class NotificationReceiver : BroadcastReceiver() { // Share backup file ACTION_SHARE_BACKUP -> shareBackup(context, intent.getParcelableExtra(EXTRA_URI), intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1)) + ACTION_CANCEL_RESTORE -> cancelRestore(context, + intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1)) // Cancel library update and dismiss notification ACTION_CANCEL_LIBRARY_UPDATE -> cancelLibraryUpdate(context, Notifications.ID_LIBRARY_PROGRESS) // Open reader activity @@ -170,6 +173,17 @@ class NotificationReceiver : BroadcastReceiver() { DiskUtil.scanMedia(context, file) } + /** + * Method called when user wants to stop a backup restore job. + * + * @param context context of application + * @param notificationId id of notification + */ + private fun cancelRestore(context: Context, notificationId: Int) { + BackupRestoreService.stop(context) + Handler().post { dismissNotification(context, notificationId) } + } + /** * Method called when user wants to stop a library update * @@ -222,6 +236,9 @@ class NotificationReceiver : BroadcastReceiver() { // Called to launch send intent. private const val ACTION_SHARE_BACKUP = "$ID.$NAME.SEND_BACKUP" + // Called to cancel backup restore job. + private const val ACTION_CANCEL_RESTORE = "$ID.$NAME.CANCEL_RESTORE" + // Called to cancel library update. private const val ACTION_CANCEL_LIBRARY_UPDATE = "$ID.$NAME.CANCEL_LIBRARY_UPDATE" @@ -475,7 +492,7 @@ class NotificationReceiver : BroadcastReceiver() { * @param notificationId id of notification * @return [PendingIntent] */ - internal fun shareBackup(context: Context, uri: Uri, notificationId: Int): PendingIntent { + internal fun shareBackupPendingBroadcast(context: Context, uri: Uri, notificationId: Int): PendingIntent { val intent = Intent(context, NotificationReceiver::class.java).apply { action = ACTION_SHARE_BACKUP putExtra(EXTRA_URI, uri) @@ -491,7 +508,7 @@ class NotificationReceiver : BroadcastReceiver() { * @param uri uri of error log file * @return [PendingIntent] */ - internal fun openErrorLog(context: Context, uri: Uri): PendingIntent { + internal fun openErrorLogPendingActivity(context: Context, uri: Uri): PendingIntent { val intent = Intent().apply { action = Intent.ACTION_VIEW setDataAndType(uri, "text/plain") @@ -499,5 +516,20 @@ class NotificationReceiver : BroadcastReceiver() { } return PendingIntent.getActivity(context, 0, intent, 0) } + + /** + * Returns [PendingIntent] that cancels a backup restore job. + * + * @param context context of application + * @param notificationId id of notification + * @return [PendingIntent] + */ + internal fun cancelRestorePendingBroadcast(context: Context, notificationId: Int): PendingIntent { + val intent = Intent(context, NotificationReceiver::class.java).apply { + action = ACTION_CANCEL_RESTORE + putExtra(EXTRA_NOTIFICATION_ID, notificationId) + } + return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) + } } } 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 de820dac84..6b25a19c87 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 @@ -23,7 +23,6 @@ import eu.kanade.tachiyomi.data.backup.models.Backup import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys 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 @@ -197,7 +196,9 @@ class SettingsBackupController : SettingsController() { CODE_BACKUP_RESTORE -> if (data != null && resultCode == Activity.RESULT_OK) { val uri = data.data if (uri != null) { - RestoreBackupDialog(uri).showDialog(router) + val ctrl = RestoreBackupDialog(uri) + ctrl.targetController = this@SettingsBackupController + ctrl.showDialog(router) } } } @@ -268,9 +269,8 @@ class SettingsBackupController : SettingsController() { .onPositive { _, _ -> val context = applicationContext if (context != null) { - RestoringBackupDialog().showDialog(router, TAG_RESTORING_BACKUP_DIALOG) + (targetController as SettingsBackupController).notifier.showRestoreProgress() BackupRestoreService.start(context, args.getParcelable(KEY_URI)!!) - isRestoreStarted = true } } @@ -282,41 +282,6 @@ class SettingsBackupController : SettingsController() { } } - class RestoringBackupDialog : DialogController() { - private var materialDialog: MaterialDialog? = null - - override fun onCreateDialog(savedViewState: Bundle?): Dialog { - return MaterialDialog.Builder(activity!!) - .title(R.string.backup) - .content(R.string.restoring_backup) - .progress(false, 100, true) - .cancelable(false) - .negativeText(R.string.action_stop) - .onNegative { _, _ -> - applicationContext?.let { BackupRestoreService.stop(it) } - } - .build() - .also { materialDialog = it } - } - - override fun onDestroyView(view: View) { - super.onDestroyView(view) - materialDialog = null - } - - override fun onRestoreInstanceState(savedInstanceState: Bundle) { - super.onRestoreInstanceState(savedInstanceState) - router.popController(this) - } - - fun updateProgress(content: String?, progress: Int, amount: Int) { - val dialog = materialDialog ?: return - dialog.setContent(content) - dialog.setProgress(progress) - dialog.maxProgress = amount - } - } - inner class BackupBroadcastReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { when (intent.getStringExtra(BackupConst.ACTION)) { @@ -336,13 +301,11 @@ class SettingsBackupController : SettingsController() { val progress = intent.getIntExtra(BackupConst.EXTRA_PROGRESS, 0) val amount = intent.getIntExtra(BackupConst.EXTRA_AMOUNT, 0) val content = intent.getStringExtra(BackupConst.EXTRA_CONTENT) - (router.getControllerWithTag(TAG_RESTORING_BACKUP_DIALOG) - as? RestoringBackupDialog)?.updateProgress(content, progress, amount) + notifier.showRestoreProgress(content, progress, amount) } BackupConst.ACTION_RESTORE_COMPLETED -> { isRestoreStarted = false - router.popControllerWithTag(TAG_RESTORING_BACKUP_DIALOG) val time = intent.getLongExtra(BackupConst.EXTRA_TIME, 0) val errorCount = intent.getIntExtra(BackupConst.EXTRA_ERRORS, 0) val path = intent.getStringExtra(BackupConst.EXTRA_ERROR_FILE_PATH) @@ -352,7 +315,6 @@ class SettingsBackupController : SettingsController() { BackupConst.ACTION_RESTORE_ERROR -> { isRestoreStarted = false - router.popControllerWithTag(TAG_RESTORING_BACKUP_DIALOG) notifier.showRestoreError(intent.getStringExtra(BackupConst.EXTRA_ERROR_MESSAGE)) } } @@ -364,8 +326,6 @@ class SettingsBackupController : SettingsController() { const val CODE_BACKUP_RESTORE = 502 const val CODE_BACKUP_DIR = 503 - const val TAG_RESTORING_BACKUP_DIALOG = "RestoringBackupDialog" - var isBackupStarted = false var isRestoreStarted = false } 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 index bf57becfdd..acb4daef9f 100644 --- 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 @@ -66,14 +66,30 @@ internal class BackupNotifier(private val context: Context) { addAction(R.drawable.ic_share_24dp, context.getString(R.string.action_share), - NotificationReceiver.shareBackup(context, unifile.uri, Notifications.ID_BACKUP)) + NotificationReceiver.shareBackupPendingBroadcast(context, unifile.uri, Notifications.ID_BACKUP)) } notificationBuilder.show(Notifications.ID_BACKUP) } - fun showRestoreProgress() { - // TODO + fun showRestoreProgress(content: String = "", progress: Int = 0, maxAmount: Int = 100) { + with(notificationBuilder) { + setContentTitle(context.getString(R.string.restoring_backup)) + setContentText(content) + + setProgress(maxAmount, progress, false) + + // Clear old actions if they exist + if (mActions.isNotEmpty()) { + mActions.clear() + } + + addAction(R.drawable.ic_close_24dp, + context.getString(R.string.action_stop), + NotificationReceiver.cancelRestorePendingBroadcast(context, Notifications.ID_RESTORE)) + } + + notificationBuilder.show(Notifications.ID_RESTORE) } fun showRestoreError(error: String?) { @@ -116,7 +132,7 @@ internal class BackupNotifier(private val context: Context) { addAction(R.drawable.nnf_ic_file_folder, context.getString(R.string.action_open_log), - NotificationReceiver.openErrorLog(context, uri)) + NotificationReceiver.openErrorLogPendingActivity(context, uri)) } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b50074408f..477c2ead5c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -315,7 +315,6 @@ Service Backup frequency Max automatic backups - Restoring backup\n%1$s added to library Source not found Restoring backup\n%1$s source not found Backup created @@ -330,6 +329,7 @@ Restore is already in progress Restoring backup Restoring backup failed + Canceled restore Clear chapter cache