From 75b1b3fe898d046365325715993e876e2905c231 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 6 Jan 2020 20:50:48 +0800 Subject: [PATCH] up --- .../io/legado/app/help/storage/Restore.kt | 5 ++ .../io/legado/app/help/storage/WebDavHelp.kt | 24 ++++----- .../app/ui/config/WebDavConfigFragment.kt | 51 ++++++++++++++++--- .../io/legado/app/ui/main/MainActivity.kt | 28 ++++++---- 4 files changed, 78 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/storage/Restore.kt b/app/src/main/java/io/legado/app/help/storage/Restore.kt index 116419a63..ea1bf5392 100644 --- a/app/src/main/java/io/legado/app/help/storage/Restore.kt +++ b/app/src/main/java/io/legado/app/help/storage/Restore.kt @@ -1,6 +1,7 @@ package io.legado.app.help.storage import android.content.Context +import android.net.Uri import android.util.Log import com.jayway.jsonpath.Configuration import com.jayway.jsonpath.JsonPath @@ -33,6 +34,10 @@ object Restore { ) } + fun restore(context: Context, uri: Uri) { + + } + fun restore(path: String = Backup.defaultPath) { doAsync { try { diff --git a/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt b/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt index a4ac7fbcc..6be8c205d 100644 --- a/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt +++ b/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt @@ -8,9 +8,10 @@ import io.legado.app.lib.webdav.WebDav import io.legado.app.lib.webdav.http.HttpAuth import io.legado.app.utils.ZipUtils import io.legado.app.utils.getPrefString +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.withContext import org.jetbrains.anko.doAsync import org.jetbrains.anko.selector -import org.jetbrains.anko.uiThread import java.io.File import java.text.SimpleDateFormat import java.util.* @@ -54,20 +55,17 @@ object WebDavHelp { return names } - fun showRestoreDialog(context: Context) { - doAsync { - val names = getWebDavFileNames() - if (names.isNotEmpty()) { - uiThread { - context.selector(title = "选择恢复文件", items = names) { _, index -> - if (index in 0 until names.size) { - restoreWebDav(names[index]) - } - } + suspend fun showRestoreDialog(context: Context): Boolean { + val names = withContext(IO) { getWebDavFileNames() } + return if (names.isNotEmpty()) { + context.selector(title = "选择恢复文件", items = names) { _, index -> + if (index in 0 until names.size) { + restoreWebDav(names[index]) } - } else { - Restore.restore() } + true + } else { + false } } diff --git a/app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt index 4dea5e0e7..763491d54 100644 --- a/app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt @@ -116,13 +116,7 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), override fun onPreferenceTreeClick(preference: Preference?): Boolean { when (preference?.key) { "web_dav_backup" -> backup() - "web_dav_restore" -> PermissionsCompat.Builder(this) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { - WebDavHelp.showRestoreDialog(requireContext()) - } - .request() + "web_dav_restore" -> restore() "import_old" -> importOldData() } return super.onPreferenceTreeClick(preference) @@ -157,6 +151,39 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), } } + fun restore() { + launch { + if (!WebDavHelp.showRestoreDialog(requireContext())) { + val backupPath = getPrefString(PreferKey.backupPath) + if (backupPath?.isEmpty() == true) { + selectRestoreFolder() + } else { + val uri = Uri.parse(backupPath) + val doc = DocumentFile.fromTreeUri(requireContext(), uri) + if (doc?.canWrite() == true) { + Restore.restore(requireContext(), uri) + } else { + selectBackupFolder() + } + } + } + } + } + + private fun selectRestoreFolder() { + try { + val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + startActivityForResult(intent, restoreSelectRequestCode) + } catch (e: java.lang.Exception) { + PermissionsCompat.Builder(this) + .addPermissions(*Permissions.Group.STORAGE) + .rationale(R.string.tip_perm_request_storage) + .onGranted { Restore.restore(Backup.legadoPath) } + .request() + } + } + private fun importOldData() { try { val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) @@ -270,6 +297,16 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), Backup.backup(requireContext(), uri) } } + restoreSelectRequestCode -> if (resultCode == RESULT_OK) { + data?.data?.let { uri -> + requireContext().contentResolver.takePersistableUriPermission( + uri, + Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION + ) + putPrefString(PreferKey.backupPath, uri.toString()) + Restore.restore(requireContext(), uri) + } + } } } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index 0a6ca93c1..6c1287adc 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -172,15 +172,22 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), } fun restore() { - PermissionsCompat.Builder(this) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { WebDavHelp.showRestoreDialog(this) } - .request() - } - - fun restore(uri: Uri) { - + launch { + if (!WebDavHelp.showRestoreDialog(this@MainActivity)) { + val backupPath = getPrefString(PreferKey.backupPath) + if (backupPath?.isEmpty() == true) { + selectRestoreFolder() + } else { + val uri = Uri.parse(backupPath) + val doc = DocumentFile.fromTreeUri(this@MainActivity, uri) + if (doc?.canWrite() == true) { + Restore.restore(this@MainActivity, uri) + } else { + selectBackupFolder() + } + } + } + } } private fun selectBackupFolder() { @@ -206,7 +213,7 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), PermissionsCompat.Builder(this) .addPermissions(*Permissions.Group.STORAGE) .rationale(R.string.tip_perm_request_storage) - .onGranted { Restore.restore() } + .onGranted { Restore.restore(Backup.legadoPath) } .request() } } @@ -231,6 +238,7 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION ) putPrefString(PreferKey.backupPath, uri.toString()) + Restore.restore(this, uri) } } }