Prevent multiple concurrent backups/restores

This commit is contained in:
arkon 2020-04-21 21:34:22 -04:00
parent f2a7f8efda
commit fc2c465bac
5 changed files with 49 additions and 24 deletions

View file

@ -5,11 +5,11 @@ import eu.kanade.tachiyomi.BuildConfig.APPLICATION_ID as ID
object BackupConst { object BackupConst {
const val INTENT_FILTER = "SettingsBackupFragment" const val INTENT_FILTER = "SettingsBackupFragment"
const val ACTION_BACKUP_COMPLETED_DIALOG = "$ID.$INTENT_FILTER.ACTION_BACKUP_COMPLETED_DIALOG" const val ACTION_BACKUP_COMPLETED = "$ID.$INTENT_FILTER.ACTION_BACKUP_COMPLETED"
const val ACTION_SET_PROGRESS_DIALOG = "$ID.$INTENT_FILTER.ACTION_SET_PROGRESS_DIALOG" const val ACTION_BACKUP_ERROR = "$ID.$INTENT_FILTER.ACTION_BACKUP_ERROR"
const val ACTION_ERROR_BACKUP_DIALOG = "$ID.$INTENT_FILTER.ACTION_ERROR_BACKUP_DIALOG" const val ACTION_RESTORE_PROGRESS = "$ID.$INTENT_FILTER.ACTION_RESTORE_PROGRESS"
const val ACTION_ERROR_RESTORE_DIALOG = "$ID.$INTENT_FILTER.ACTION_ERROR_RESTORE_DIALOG" const val ACTION_RESTORE_COMPLETED = "$ID.$INTENT_FILTER.ACTION_RESTORE_COMPLETED"
const val ACTION_RESTORE_COMPLETED_DIALOG = "$ID.$INTENT_FILTER.ACTION_RESTORE_COMPLETED_DIALOG" const val ACTION_RESTORE_ERROR = "$ID.$INTENT_FILTER.ACTION_RESTORE_ERROR"
const val ACTION = "$ID.$INTENT_FILTER.ACTION" const val ACTION = "$ID.$INTENT_FILTER.ACTION"
const val EXTRA_PROGRESS = "$ID.$INTENT_FILTER.EXTRA_PROGRESS" const val EXTRA_PROGRESS = "$ID.$INTENT_FILTER.EXTRA_PROGRESS"
const val EXTRA_AMOUNT = "$ID.$INTENT_FILTER.EXTRA_AMOUNT" const val EXTRA_AMOUNT = "$ID.$INTENT_FILTER.EXTRA_AMOUNT"

View file

@ -179,7 +179,7 @@ class BackupManager(val context: Context, version: Int = CURRENT_VERSION) {
// Show completed dialog // Show completed dialog
val intent = Intent(BackupConst.INTENT_FILTER).apply { val intent = Intent(BackupConst.INTENT_FILTER).apply {
putExtra(BackupConst.ACTION, BackupConst.ACTION_BACKUP_COMPLETED_DIALOG) putExtra(BackupConst.ACTION, BackupConst.ACTION_BACKUP_COMPLETED)
putExtra(BackupConst.EXTRA_URI, file.uri.toString()) putExtra(BackupConst.EXTRA_URI, file.uri.toString())
} }
context.sendLocalBroadcast(intent) context.sendLocalBroadcast(intent)
@ -189,7 +189,7 @@ class BackupManager(val context: Context, version: Int = CURRENT_VERSION) {
if (!isJob) { if (!isJob) {
// Show error dialog // Show error dialog
val intent = Intent(BackupConst.INTENT_FILTER).apply { val intent = Intent(BackupConst.INTENT_FILTER).apply {
putExtra(BackupConst.ACTION, BackupConst.ACTION_ERROR_BACKUP_DIALOG) putExtra(BackupConst.ACTION, BackupConst.ACTION_BACKUP_ERROR)
putExtra(BackupConst.EXTRA_ERROR_MESSAGE, e.message) putExtra(BackupConst.EXTRA_ERROR_MESSAGE, e.message)
} }
context.sendLocalBroadcast(intent) context.sendLocalBroadcast(intent)

View file

@ -251,7 +251,7 @@ class BackupRestoreService : Service() {
putExtra(BackupConst.EXTRA_ERRORS, errors.size) putExtra(BackupConst.EXTRA_ERRORS, errors.size)
putExtra(BackupConst.EXTRA_ERROR_FILE_PATH, logFile.parent) putExtra(BackupConst.EXTRA_ERROR_FILE_PATH, logFile.parent)
putExtra(BackupConst.EXTRA_ERROR_FILE, logFile.name) putExtra(BackupConst.EXTRA_ERROR_FILE, logFile.name)
putExtra(BackupConst.ACTION, BackupConst.ACTION_RESTORE_COMPLETED_DIALOG) putExtra(BackupConst.ACTION, BackupConst.ACTION_RESTORE_COMPLETED)
} }
sendLocalBroadcast(completeIntent) sendLocalBroadcast(completeIntent)
} }
@ -259,7 +259,7 @@ class BackupRestoreService : Service() {
Timber.e(error) Timber.e(error)
writeErrorLog() writeErrorLog()
val errorIntent = Intent(BackupConst.INTENT_FILTER).apply { val errorIntent = Intent(BackupConst.INTENT_FILTER).apply {
putExtra(BackupConst.ACTION, BackupConst.ACTION_ERROR_RESTORE_DIALOG) putExtra(BackupConst.ACTION, BackupConst.ACTION_RESTORE_ERROR)
putExtra(BackupConst.EXTRA_ERROR_MESSAGE, error.message) putExtra(BackupConst.EXTRA_ERROR_MESSAGE, error.message)
} }
sendLocalBroadcast(errorIntent) sendLocalBroadcast(errorIntent)
@ -463,7 +463,7 @@ class BackupRestoreService : Service() {
putExtra(BackupConst.EXTRA_AMOUNT, amount) putExtra(BackupConst.EXTRA_AMOUNT, amount)
putExtra(BackupConst.EXTRA_CONTENT, content) putExtra(BackupConst.EXTRA_CONTENT, content)
putExtra(BackupConst.EXTRA_ERRORS, errors) putExtra(BackupConst.EXTRA_ERRORS, errors)
putExtra(BackupConst.ACTION, BackupConst.ACTION_SET_PROGRESS_DIALOG) putExtra(BackupConst.ACTION, BackupConst.ACTION_RESTORE_PROGRESS)
} }
sendLocalBroadcast(intent) sendLocalBroadcast(intent)
} }

View file

@ -73,9 +73,13 @@ class SettingsBackupController : SettingsController() {
summaryRes = R.string.pref_create_backup_summ summaryRes = R.string.pref_create_backup_summ
onClick { onClick {
if (!isBackupStarted) {
val ctrl = CreateBackupDialog() val ctrl = CreateBackupDialog()
ctrl.targetController = this@SettingsBackupController ctrl.targetController = this@SettingsBackupController
ctrl.showDialog(router) ctrl.showDialog(router)
} else {
context.toast(R.string.backup_in_progress)
}
} }
} }
preference { preference {
@ -83,12 +87,16 @@ class SettingsBackupController : SettingsController() {
summaryRes = R.string.pref_restore_backup_summ summaryRes = R.string.pref_restore_backup_summ
onClick { onClick {
if (!isRestoreStarted) {
val intent = Intent(Intent.ACTION_GET_CONTENT) val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.addCategory(Intent.CATEGORY_OPENABLE) intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "application/*" intent.type = "application/*"
val title = resources?.getString(R.string.file_select_backup) val title = resources?.getString(R.string.file_select_backup)
val chooser = Intent.createChooser(intent, title) val chooser = Intent.createChooser(intent, title)
startActivityForResult(chooser, CODE_BACKUP_RESTORE) startActivityForResult(chooser, CODE_BACKUP_RESTORE)
} else {
context.toast(R.string.restore_in_progress)
}
} }
} }
preferenceCategory { preferenceCategory {
@ -183,6 +191,8 @@ class SettingsBackupController : SettingsController() {
notifier.showBackupProgress() notifier.showBackupProgress()
BackupCreateService.makeBackup(activity, file.uri, backupFlags) BackupCreateService.makeBackup(activity, file.uri, backupFlags)
isBackupStarted = true
} }
CODE_BACKUP_RESTORE -> if (data != null && resultCode == Activity.RESULT_OK) { CODE_BACKUP_RESTORE -> if (data != null && resultCode == Activity.RESULT_OK) {
val uri = data.data val uri = data.data
@ -260,6 +270,8 @@ class SettingsBackupController : SettingsController() {
if (context != null) { if (context != null) {
RestoringBackupDialog().showDialog(router, TAG_RESTORING_BACKUP_DIALOG) RestoringBackupDialog().showDialog(router, TAG_RESTORING_BACKUP_DIALOG)
BackupRestoreService.start(context, args.getParcelable(KEY_URI)!!) BackupRestoreService.start(context, args.getParcelable(KEY_URI)!!)
isRestoreStarted = true
} }
} }
.build() .build()
@ -308,22 +320,28 @@ class SettingsBackupController : SettingsController() {
inner class BackupBroadcastReceiver : BroadcastReceiver() { inner class BackupBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
when (intent.getStringExtra(BackupConst.ACTION)) { when (intent.getStringExtra(BackupConst.ACTION)) {
BackupConst.ACTION_BACKUP_COMPLETED_DIALOG -> { BackupConst.ACTION_BACKUP_COMPLETED -> {
isBackupStarted = false
val uri = Uri.parse(intent.getStringExtra(BackupConst.EXTRA_URI)) val uri = Uri.parse(intent.getStringExtra(BackupConst.EXTRA_URI))
val unifile = UniFile.fromUri(activity, uri) val unifile = UniFile.fromUri(activity, uri)
notifier.showBackupComplete(unifile) notifier.showBackupComplete(unifile)
} }
BackupConst.ACTION_ERROR_BACKUP_DIALOG -> { BackupConst.ACTION_BACKUP_ERROR -> {
isBackupStarted = false
notifier.showBackupError(intent.getStringExtra(BackupConst.EXTRA_ERROR_MESSAGE)) notifier.showBackupError(intent.getStringExtra(BackupConst.EXTRA_ERROR_MESSAGE))
} }
BackupConst.ACTION_SET_PROGRESS_DIALOG -> { BackupConst.ACTION_RESTORE_PROGRESS -> {
val progress = intent.getIntExtra(BackupConst.EXTRA_PROGRESS, 0) val progress = intent.getIntExtra(BackupConst.EXTRA_PROGRESS, 0)
val amount = intent.getIntExtra(BackupConst.EXTRA_AMOUNT, 0) val amount = intent.getIntExtra(BackupConst.EXTRA_AMOUNT, 0)
val content = intent.getStringExtra(BackupConst.EXTRA_CONTENT) val content = intent.getStringExtra(BackupConst.EXTRA_CONTENT)
(router.getControllerWithTag(TAG_RESTORING_BACKUP_DIALOG) (router.getControllerWithTag(TAG_RESTORING_BACKUP_DIALOG)
as? RestoringBackupDialog)?.updateProgress(content, progress, amount) as? RestoringBackupDialog)?.updateProgress(content, progress, amount)
} }
BackupConst.ACTION_RESTORE_COMPLETED_DIALOG -> { BackupConst.ACTION_RESTORE_COMPLETED -> {
isRestoreStarted = false
router.popControllerWithTag(TAG_RESTORING_BACKUP_DIALOG) router.popControllerWithTag(TAG_RESTORING_BACKUP_DIALOG)
val time = intent.getLongExtra(BackupConst.EXTRA_TIME, 0) val time = intent.getLongExtra(BackupConst.EXTRA_TIME, 0)
val errorCount = intent.getIntExtra(BackupConst.EXTRA_ERRORS, 0) val errorCount = intent.getIntExtra(BackupConst.EXTRA_ERRORS, 0)
@ -331,7 +349,9 @@ class SettingsBackupController : SettingsController() {
val file = intent.getStringExtra(BackupConst.EXTRA_ERROR_FILE) val file = intent.getStringExtra(BackupConst.EXTRA_ERROR_FILE)
notifier.showRestoreComplete(time, errorCount, path, file) notifier.showRestoreComplete(time, errorCount, path, file)
} }
BackupConst.ACTION_ERROR_RESTORE_DIALOG -> { BackupConst.ACTION_RESTORE_ERROR -> {
isRestoreStarted = false
router.popControllerWithTag(TAG_RESTORING_BACKUP_DIALOG) router.popControllerWithTag(TAG_RESTORING_BACKUP_DIALOG)
notifier.showRestoreError(intent.getStringExtra(BackupConst.EXTRA_ERROR_MESSAGE)) notifier.showRestoreError(intent.getStringExtra(BackupConst.EXTRA_ERROR_MESSAGE))
} }
@ -345,5 +365,8 @@ class SettingsBackupController : SettingsController() {
const val CODE_BACKUP_DIR = 503 const val CODE_BACKUP_DIR = 503
const val TAG_RESTORING_BACKUP_DIALOG = "RestoringBackupDialog" const val TAG_RESTORING_BACKUP_DIALOG = "RestoringBackupDialog"
var isBackupStarted = false
var isRestoreStarted = false
} }
} }

View file

@ -323,9 +323,11 @@
<string name="restore_completed_content">Restore took %1$s with %2$s errors</string> <string name="restore_completed_content">Restore took %1$s with %2$s errors</string>
<string name="backup_restore_content">Restore uses sources to fetch data, carrier costs may apply.\n\nMake sure you have installed all necessary extensions and are logged in to sources and tracking services before restoring.</string> <string name="backup_restore_content">Restore uses sources to fetch data, carrier costs may apply.\n\nMake sure you have installed all necessary extensions and are logged in to sources and tracking services before restoring.</string>
<string name="file_saved">File saved at %1$s</string> <string name="file_saved">File saved at %1$s</string>
<string name="backup_in_progress">Backup is already in progress</string>
<string name="backup_choice">What do you want to backup?</string> <string name="backup_choice">What do you want to backup?</string>
<string name="creating_backup">Creating backup</string> <string name="creating_backup">Creating backup</string>
<string name="creating_backup_error">Backup failed</string> <string name="creating_backup_error">Backup failed</string>
<string name="restore_in_progress">Restore is already in progress</string>
<string name="restoring_backup">Restoring backup</string> <string name="restoring_backup">Restoring backup</string>
<string name="restoring_backup_error">Restoring backup failed</string> <string name="restoring_backup_error">Restoring backup failed</string>