From d5015d37e19608c634b3a350a31548feb8b23026 Mon Sep 17 00:00:00 2001 From: arkon Date: Wed, 26 Jan 2022 22:31:28 -0500 Subject: [PATCH] Show error toast if empty URI is passed when trying to create/restore a backup --- .../data/backup/AbstractBackupManager.kt | 2 +- .../data/backup/full/FullBackupManager.kt | 4 ++-- .../ui/setting/SettingsBackupController.kt | 24 +++++++------------ app/src/main/res/values/strings.xml | 1 + 4 files changed, 13 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupManager.kt index 723fb259b..de637c6e5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupManager.kt @@ -21,7 +21,7 @@ abstract class AbstractBackupManager(protected val context: Context) { internal val trackManager: TrackManager by injectLazy() protected val preferences: PreferencesHelper by injectLazy() - abstract fun createBackup(uri: Uri, flags: Int, isJob: Boolean): String? + abstract fun createBackup(uri: Uri, flags: Int, isJob: Boolean): String /** * Returns manga diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupManager.kt index c334bcd21..171b34044 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupManager.kt @@ -44,7 +44,7 @@ class FullBackupManager(context: Context) : AbstractBackupManager(context) { * @param uri path of Uri * @param isJob backup called from job */ - override fun createBackup(uri: Uri, flags: Int, isJob: Boolean): String? { + override fun createBackup(uri: Uri, flags: Int, isJob: Boolean): String { // Create root object var backup: Backup? = null @@ -87,7 +87,7 @@ class FullBackupManager(context: Context) : AbstractBackupManager(context) { file.openOutputStream().sink().gzip().buffer().use { it.write(byteArray) } val fileUri = file.uri - // Validate it to make sure it works + // Make sure it's a valid backup file FullBackupRestoreValidator().validate(context, fileUri) return fileUri.toString() 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 4a0062d7d..744cc595a 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 @@ -162,39 +162,33 @@ class SettingsBackupController : SettingsController() { val activity = activity ?: return val uri = data.data + if (uri == null) { + activity.toast(R.string.backup_restore_invalid_uri) + return + } + when (requestCode) { CODE_BACKUP_DIR -> { // Get UriPermission so it's possible to write files val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION - if (uri != null) { - activity.contentResolver.takePersistableUriPermission(uri, flags) - } - - // Set backup Uri + activity.contentResolver.takePersistableUriPermission(uri, flags) preferences.backupsDirectory().set(uri.toString()) } CODE_BACKUP_CREATE -> { val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION - if (uri != null) { - activity.contentResolver.takePersistableUriPermission(uri, flags) - } - - val file = UniFile.fromUri(activity, uri) - - activity.toast(R.string.creating_backup) - + activity.contentResolver.takePersistableUriPermission(uri, flags) BackupCreateService.start( activity, - file.uri, + uri, backupFlags, ) } CODE_BACKUP_RESTORE -> { - uri?.let { RestoreBackupDialog(it).showDialog(router) } + RestoreBackupDialog(uri).showDialog(router) } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f65c4f7a7..0d8b69a02 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -425,6 +425,7 @@ Maximum backups Source not found: %1$s Not logged in: %1$s + Error: empty URI Backup created Invalid backup file File is missing data.