pull/68/head^2
kunfei 5 years ago
parent 96c839f580
commit 75b1b3fe89
  1. 5
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  2. 24
      app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt
  3. 51
      app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt
  4. 28
      app/src/main/java/io/legado/app/ui/main/MainActivity.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 {

@ -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
}
}

@ -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)
}
}
}
}
}

@ -172,15 +172,22 @@ class MainActivity : VMBaseActivity<MainViewModel>(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<MainViewModel>(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<MainViewModel>(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)
}
}
}

Loading…
Cancel
Save