Show error toast if empty URI is passed when trying to create/restore a backup

This commit is contained in:
arkon 2022-01-26 22:31:28 -05:00
parent 1b71e4cee7
commit d5015d37e1
4 changed files with 13 additions and 18 deletions

View File

@ -21,7 +21,7 @@ abstract class AbstractBackupManager(protected val context: Context) {
internal val trackManager: TrackManager by injectLazy() internal val trackManager: TrackManager by injectLazy()
protected val preferences: PreferencesHelper 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 * Returns manga

View File

@ -44,7 +44,7 @@ class FullBackupManager(context: Context) : AbstractBackupManager(context) {
* @param uri path of Uri * @param uri path of Uri
* @param isJob backup called from job * @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 // Create root object
var backup: Backup? = null var backup: Backup? = null
@ -87,7 +87,7 @@ class FullBackupManager(context: Context) : AbstractBackupManager(context) {
file.openOutputStream().sink().gzip().buffer().use { it.write(byteArray) } file.openOutputStream().sink().gzip().buffer().use { it.write(byteArray) }
val fileUri = file.uri val fileUri = file.uri
// Validate it to make sure it works // Make sure it's a valid backup file
FullBackupRestoreValidator().validate(context, fileUri) FullBackupRestoreValidator().validate(context, fileUri)
return fileUri.toString() return fileUri.toString()

View File

@ -162,39 +162,33 @@ class SettingsBackupController : SettingsController() {
val activity = activity ?: return val activity = activity ?: return
val uri = data.data val uri = data.data
if (uri == null) {
activity.toast(R.string.backup_restore_invalid_uri)
return
}
when (requestCode) { when (requestCode) {
CODE_BACKUP_DIR -> { CODE_BACKUP_DIR -> {
// Get UriPermission so it's possible to write files // Get UriPermission so it's possible to write files
val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION
if (uri != null) { activity.contentResolver.takePersistableUriPermission(uri, flags)
activity.contentResolver.takePersistableUriPermission(uri, flags)
}
// Set backup Uri
preferences.backupsDirectory().set(uri.toString()) preferences.backupsDirectory().set(uri.toString())
} }
CODE_BACKUP_CREATE -> { CODE_BACKUP_CREATE -> {
val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION
if (uri != null) { activity.contentResolver.takePersistableUriPermission(uri, flags)
activity.contentResolver.takePersistableUriPermission(uri, flags)
}
val file = UniFile.fromUri(activity, uri)
activity.toast(R.string.creating_backup)
BackupCreateService.start( BackupCreateService.start(
activity, activity,
file.uri, uri,
backupFlags, backupFlags,
) )
} }
CODE_BACKUP_RESTORE -> { CODE_BACKUP_RESTORE -> {
uri?.let { RestoreBackupDialog(it).showDialog(router) } RestoreBackupDialog(uri).showDialog(router)
} }
} }
} }

View File

@ -425,6 +425,7 @@
<string name="pref_backup_slots">Maximum backups</string> <string name="pref_backup_slots">Maximum backups</string>
<string name="source_not_found_name">Source not found: %1$s</string> <string name="source_not_found_name">Source not found: %1$s</string>
<string name="tracker_not_logged_in">Not logged in: %1$s</string> <string name="tracker_not_logged_in">Not logged in: %1$s</string>
<string name="backup_restore_invalid_uri">Error: empty URI</string>
<string name="backup_created">Backup created</string> <string name="backup_created">Backup created</string>
<string name="invalid_backup_file">Invalid backup file</string> <string name="invalid_backup_file">Invalid backup file</string>
<string name="invalid_backup_file_missing_data">File is missing data.</string> <string name="invalid_backup_file_missing_data">File is missing data.</string>