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. 16
      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. 26
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt

@ -1,6 +1,7 @@
package io.legado.app.help.storage package io.legado.app.help.storage
import android.content.Context import android.content.Context
import android.net.Uri
import android.util.Log import android.util.Log
import com.jayway.jsonpath.Configuration import com.jayway.jsonpath.Configuration
import com.jayway.jsonpath.JsonPath import com.jayway.jsonpath.JsonPath
@ -33,6 +34,10 @@ object Restore {
) )
} }
fun restore(context: Context, uri: Uri) {
}
fun restore(path: String = Backup.defaultPath) { fun restore(path: String = Backup.defaultPath) {
doAsync { doAsync {
try { try {

@ -8,9 +8,10 @@ import io.legado.app.lib.webdav.WebDav
import io.legado.app.lib.webdav.http.HttpAuth import io.legado.app.lib.webdav.http.HttpAuth
import io.legado.app.utils.ZipUtils import io.legado.app.utils.ZipUtils
import io.legado.app.utils.getPrefString import io.legado.app.utils.getPrefString
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext
import org.jetbrains.anko.doAsync import org.jetbrains.anko.doAsync
import org.jetbrains.anko.selector import org.jetbrains.anko.selector
import org.jetbrains.anko.uiThread
import java.io.File import java.io.File
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
@ -54,20 +55,17 @@ object WebDavHelp {
return names return names
} }
fun showRestoreDialog(context: Context) { suspend fun showRestoreDialog(context: Context): Boolean {
doAsync { val names = withContext(IO) { getWebDavFileNames() }
val names = getWebDavFileNames() return if (names.isNotEmpty()) {
if (names.isNotEmpty()) {
uiThread {
context.selector(title = "选择恢复文件", items = names) { _, index -> context.selector(title = "选择恢复文件", items = names) { _, index ->
if (index in 0 until names.size) { if (index in 0 until names.size) {
restoreWebDav(names[index]) restoreWebDav(names[index])
} }
} }
} true
} else { } else {
Restore.restore() false
}
} }
} }

@ -116,13 +116,7 @@ class WebDavConfigFragment : PreferenceFragmentCompat(),
override fun onPreferenceTreeClick(preference: Preference?): Boolean { override fun onPreferenceTreeClick(preference: Preference?): Boolean {
when (preference?.key) { when (preference?.key) {
"web_dav_backup" -> backup() "web_dav_backup" -> backup()
"web_dav_restore" -> PermissionsCompat.Builder(this) "web_dav_restore" -> restore()
.addPermissions(*Permissions.Group.STORAGE)
.rationale(R.string.tip_perm_request_storage)
.onGranted {
WebDavHelp.showRestoreDialog(requireContext())
}
.request()
"import_old" -> importOldData() "import_old" -> importOldData()
} }
return super.onPreferenceTreeClick(preference) 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() { private fun importOldData() {
try { try {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
@ -270,6 +297,16 @@ class WebDavConfigFragment : PreferenceFragmentCompat(),
Backup.backup(requireContext(), uri) 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() { fun restore() {
PermissionsCompat.Builder(this) launch {
.addPermissions(*Permissions.Group.STORAGE) if (!WebDavHelp.showRestoreDialog(this@MainActivity)) {
.rationale(R.string.tip_perm_request_storage) val backupPath = getPrefString(PreferKey.backupPath)
.onGranted { WebDavHelp.showRestoreDialog(this) } if (backupPath?.isEmpty() == true) {
.request() 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()
}
}
}
} }
fun restore(uri: Uri) {
} }
private fun selectBackupFolder() { private fun selectBackupFolder() {
@ -206,7 +213,7 @@ class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main),
PermissionsCompat.Builder(this) PermissionsCompat.Builder(this)
.addPermissions(*Permissions.Group.STORAGE) .addPermissions(*Permissions.Group.STORAGE)
.rationale(R.string.tip_perm_request_storage) .rationale(R.string.tip_perm_request_storage)
.onGranted { Restore.restore() } .onGranted { Restore.restore(Backup.legadoPath) }
.request() .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 Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
) )
putPrefString(PreferKey.backupPath, uri.toString()) putPrefString(PreferKey.backupPath, uri.toString())
Restore.restore(this, uri)
} }
} }
} }

Loading…
Cancel
Save