From 310d89d23a7d6cca63f4ac360ffdbfb7276fc8c3 Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 26 Feb 2020 19:30:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/ui/config/BackupRestoreUi.kt | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt b/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt index 16fd3941b..ddc41afce 100644 --- a/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt +++ b/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt @@ -26,7 +26,7 @@ import kotlinx.coroutines.Dispatchers.Main import org.jetbrains.anko.toast object BackupRestoreUi { - + private const val selectFolderRequestCode = 21 private const val backupSelectRequestCode = 22 private const val restoreSelectRequestCode = 33 private const val oldDataRequestCode = 11 @@ -34,7 +34,7 @@ object BackupRestoreUi { fun backup(fragment: Fragment) { val backupPath = AppConfig.backupPath if (backupPath.isNullOrEmpty()) { - selectBackupFolder(fragment) + selectBackupFolder(fragment, backupSelectRequestCode) } else { if (backupPath.isContentPath()) { val uri = Uri.parse(backupPath) @@ -46,7 +46,7 @@ object BackupRestoreUi { fragment.toast(R.string.backup_success) } } else { - selectBackupFolder(fragment) + selectBackupFolder(fragment, backupSelectRequestCode) } } else { backupUsePermission(fragment) @@ -69,7 +69,7 @@ object BackupRestoreUi { .request() } - fun selectBackupFolder(fragment: Fragment) { + fun selectBackupFolder(fragment: Fragment, requestCode: Int = selectFolderRequestCode) { fragment.alert { titleResource = R.string.select_folder items(fragment.resources.getStringArray(R.array.select_folder).toList()) { _, index -> @@ -79,7 +79,7 @@ object BackupRestoreUi { try { val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - fragment.startActivityForResult(intent, backupSelectRequestCode) + fragment.startActivityForResult(intent, requestCode) } catch (e: java.lang.Exception) { e.printStackTrace() fragment.toast(e.localizedMessage ?: "ERROR") @@ -88,7 +88,7 @@ object BackupRestoreUi { 2 -> { FileChooserDialog.show( fragment.childFragmentManager, - backupSelectRequestCode, + requestCode, mode = FileChooserDialog.DIRECTORY ) } @@ -264,6 +264,15 @@ object BackupRestoreUi { } } } + selectFolderRequestCode -> if (resultCode == RESULT_OK) { + data?.data?.let { uri -> + App.INSTANCE.contentResolver.takePersistableUriPermission( + uri, + Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION + ) + AppConfig.backupPath = uri.toString() + } + } oldDataRequestCode -> if (resultCode == RESULT_OK) data?.data?.let { uri -> ImportOldData.importUri(uri)