Fix restore in Android 11

This commit is contained in:
arkon 2020-11-27 21:36:42 -05:00
parent 2be9871d05
commit c0e4863229
2 changed files with 48 additions and 41 deletions

View File

@ -11,6 +11,7 @@ import android.os.Bundle
import android.view.View import android.view.View
import androidx.core.net.toUri import androidx.core.net.toUri
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.documentfile.provider.DocumentFile
import androidx.preference.PreferenceScreen import androidx.preference.PreferenceScreen
import com.afollestad.materialdialogs.MaterialDialog import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.list.listItemsMultiChoice import com.afollestad.materialdialogs.list.listItemsMultiChoice
@ -81,7 +82,18 @@ class SettingsBackupController : SettingsController() {
titleRes = R.string.pref_restore_backup titleRes = R.string.pref_restore_backup
summaryRes = R.string.pref_restore_backup_summ summaryRes = R.string.pref_restore_backup_summ
onClick { restore(context) } onClick {
if (!BackupRestoreService.isRunning(context)) {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "application/*"
val title = resources?.getString(R.string.file_select_backup)
val chooser = Intent.createChooser(intent, title)
startActivityForResult(chooser, CODE_BACKUP_RESTORE)
} else {
context.toast(R.string.restore_in_progress)
}
}
} }
} }
preferenceCategory { preferenceCategory {
@ -193,33 +205,40 @@ class SettingsBackupController : SettingsController() {
) )
} }
CODE_BACKUP_RESTORE -> { CODE_BACKUP_RESTORE -> {
if (uri?.path != null) { uri?.path?.let { path ->
if (uri.path!!.endsWith(".proto.gz")) { val fileName = DocumentFile.fromSingleUri(activity, uri)!!.name!!
val options = arrayOf( when {
R.string.full_restore_offline, fileName.endsWith(".proto.gz") -> {
R.string.full_restore_online val options = arrayOf(
) R.string.full_restore_offline,
.map { activity.getString(it) } R.string.full_restore_online
MaterialDialog(activity) )
.title(R.string.full_restore_mode) .map { activity.getString(it) }
.listItemsSingleChoice( MaterialDialog(activity)
items = options, .title(R.string.full_restore_mode)
initialSelection = 0 .listItemsSingleChoice(
) { _, index, _ -> items = options,
RestoreBackupDialog( initialSelection = 0
uri, ) { _, index, _ ->
BackupConst.BACKUP_TYPE_FULL, RestoreBackupDialog(
isOnline = index != 0 uri,
).showDialog(router) BackupConst.BACKUP_TYPE_FULL,
} isOnline = index != 0
.positiveButton(R.string.action_restore) ).showDialog(router)
.show() }
} else if (uri.path!!.endsWith(".json")) { .positiveButton(R.string.action_restore)
RestoreBackupDialog( .show()
uri, }
BackupConst.BACKUP_TYPE_LEGACY, fileName.endsWith(".json") -> {
isOnline = true RestoreBackupDialog(
).showDialog(router) uri,
BackupConst.BACKUP_TYPE_LEGACY,
isOnline = true
).showDialog(router)
}
else -> {
activity.toast(activity.getString(R.string.invalid_backup_file_type, fileName))
}
} }
} }
} }
@ -237,19 +256,6 @@ class SettingsBackupController : SettingsController() {
} }
} }
private fun restore(context: Context) {
if (!BackupRestoreService.isRunning(context)) {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "application/*"
val title = resources?.getString(R.string.file_select_backup)
val chooser = Intent.createChooser(intent, title)
startActivityForResult(chooser, CODE_BACKUP_RESTORE)
} else {
context.toast(R.string.restore_in_progress)
}
}
fun createBackup(flags: Int, type: Int) { fun createBackup(flags: Int, type: Int) {
backupFlags = flags backupFlags = flags
val currentDir = preferences.backupsDirectory().get() val currentDir = preferences.backupsDirectory().get()

View File

@ -365,6 +365,7 @@
<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_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_type">Invalid backup file: %1$s</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>
<string name="invalid_backup_file_missing_manga">Backup does not contain any manga.</string> <string name="invalid_backup_file_missing_manga">Backup does not contain any manga.</string>
<string name="backup_restore_missing_sources">Missing sources:</string> <string name="backup_restore_missing_sources">Missing sources:</string>