commit backup restore

pull/55/head
ca1e 5 years ago
parent 302a561ed5
commit 4a12578339
  1. 30
      app/src/main/java/io/legado/app/help/storage/Backup.kt
  2. 13
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  3. 7
      app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt
  4. 6
      app/src/main/java/io/legado/app/ui/config/ConfigFragment.kt
  5. 35
      app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt
  6. 2
      app/src/main/res/values/arrays.xml
  7. 28
      app/src/main/res/xml/pref_config_web_dav.xml

@ -20,23 +20,26 @@ object Backup {
} }
val legadoPath by lazy { val legadoPath by lazy {
defaultPath + File.separator + "legadoBackUp" FileUtils.getSdCardPath() + File.separator + "YueDu3.0"
} }
val exportPath by lazy { val exportPath by lazy {
legadoPath + File.separator + "Export" legadoPath + File.separator + "Export"
} }
private fun pbackup(path :String = legadoPath){
backupBookshelf(path)
backupBookSource(path)
backupRssSource(path)
backupReplaceRule(path)
backupReadConfig(path)
backupPreference(path)
WebDavHelp.backUpWebDav(path)
}
fun backup() { fun backup() {
doAsync { doAsync {
val path = legadoPath pbackup()
backupBookshelf(path)
backupBookSource(path)
backupRssSource(path)
backupReplaceRule(path)
backupReadConfig(path)
backupPreference(path)
WebDavHelp.backUpWebDav(path)
uiThread { uiThread {
App.INSTANCE.toast(R.string.backup_success) App.INSTANCE.toast(R.string.backup_success)
} }
@ -45,14 +48,7 @@ object Backup {
fun autoBackup() { fun autoBackup() {
doAsync { doAsync {
val path = legadoPath pbackup()
backupBookshelf(path)
backupBookSource(path)
backupRssSource(path)
backupReplaceRule(path)
backupReadConfig(path)
backupPreference(path)
WebDavHelp.backUpWebDav(path)
} }
} }

@ -7,6 +7,7 @@ import com.jayway.jsonpath.JsonPath
import com.jayway.jsonpath.Option import com.jayway.jsonpath.Option
import com.jayway.jsonpath.ParseContext import com.jayway.jsonpath.ParseContext
import io.legado.app.App import io.legado.app.App
import io.legado.app.R
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
@ -14,8 +15,6 @@ import io.legado.app.data.entities.ReplaceRule
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
import io.legado.app.help.FileHelp import io.legado.app.help.FileHelp
import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadBookConfig
import io.legado.app.help.storage.Backup.defaultPath
import io.legado.app.help.storage.Backup.legadoPath
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
@ -37,7 +36,7 @@ object Restore {
) )
} }
fun restore(path: String = legadoPath) { fun restore(path: String = Backup.legadoPath) {
doAsync { doAsync {
try { try {
val file = FileHelp.getFile(path + File.separator + "bookshelf.json") val file = FileHelp.getFile(path + File.separator + "bookshelf.json")
@ -99,14 +98,14 @@ object Restore {
} }
edit.commit() edit.commit()
} }
uiThread { App.INSTANCE.toast("恢复完成") } uiThread { App.INSTANCE.toast(R.string.restore_success) }
} }
} }
fun importYueDuData(context: Context) { fun importYueDuData(context: Context) {
GlobalScope.launch(IO) { GlobalScope.launch(IO) {
try {// 导入书架 try {// 导入书架
val shelfFile = FileHelp.getFile(defaultPath + File.separator + "myBookShelf.json") val shelfFile = FileHelp.getFile(Backup.defaultPath + File.separator + "myBookShelf.json")
val books = mutableListOf<Book>() val books = mutableListOf<Book>()
val items: List<Map<String, Any>> = jsonPath.parse(shelfFile.readText()).read("$") val items: List<Map<String, Any>> = jsonPath.parse(shelfFile.readText()).read("$")
val existingBooks = App.db.bookDao().allBookUrls.toSet() val existingBooks = App.db.bookDao().allBookUrls.toSet()
@ -156,7 +155,7 @@ object Restore {
try {// Book source try {// Book source
val sourceFile = val sourceFile =
FileHelp.getFile(defaultPath + File.separator + "myBookSource.json") FileHelp.getFile(Backup.defaultPath + File.separator + "myBookSource.json")
val bookSources = mutableListOf<BookSource>() val bookSources = mutableListOf<BookSource>()
val items: List<Map<String, Any>> = jsonPath.parse(sourceFile.readText()).read("$") val items: List<Map<String, Any>> = jsonPath.parse(sourceFile.readText()).read("$")
for (item in items) { for (item in items) {
@ -177,7 +176,7 @@ object Restore {
try {// Replace rules try {// Replace rules
val ruleFile = val ruleFile =
FileHelp.getFile(defaultPath + File.separator + "myBookReplaceRule.json") FileHelp.getFile(Backup.defaultPath + File.separator + "myBookReplaceRule.json")
val replaceRules = mutableListOf<ReplaceRule>() val replaceRules = mutableListOf<ReplaceRule>()
val items: List<Map<String, Any>> = jsonPath.parse(ruleFile.readText()).read("$") val items: List<Map<String, Any>> = jsonPath.parse(ruleFile.readText()).read("$")
val existingRules = App.db.replaceRuleDao().all.map { it.pattern }.toSet() val existingRules = App.db.replaceRuleDao().all.map { it.pattern }.toSet()

@ -18,6 +18,9 @@ import kotlin.math.min
object WebDavHelp { object WebDavHelp {
private val zipFilePath = FileHelp.getCachePath() + "/backup" + ".zip" private val zipFilePath = FileHelp.getCachePath() + "/backup" + ".zip"
private val unzipFilesPath by lazy {
FileHelp.getCachePath()
}
private fun getWebDavUrl(): String? { private fun getWebDavUrl(): String? {
var url = App.INSTANCE.getPrefString("web_dav_url") var url = App.INSTANCE.getPrefString("web_dav_url")
@ -73,8 +76,8 @@ object WebDavHelp {
getWebDavUrl()?.let { getWebDavUrl()?.let {
val file = WebDav(it + "legado/" + name) val file = WebDav(it + "legado/" + name)
file.downloadTo(zipFilePath, true) file.downloadTo(zipFilePath, true)
ZipUtils.unzipFile(zipFilePath, Backup.legadoPath) ZipUtils.unzipFile(zipFilePath, unzipFilesPath)
Restore.restore() Restore.restore(unzipFilesPath)
} }
} }
} }

@ -61,8 +61,10 @@ class ConfigFragment : PreferenceFragmentCompat(),
initPath = getPreferenceString(PreferKey.downloadPath) initPath = getPreferenceString(PreferKey.downloadPath)
) )
PreferKey.cleanCache -> { PreferKey.cleanCache -> {
LogUtils.d("xxx","cleancaches") getPreferenceString(PreferKey.downloadPath).let {
FileHelp.deleteFile(getPreferenceString(PreferKey.downloadPath)) FileHelp.deleteFile(it)
FileHelp.getFolder(it)
}
toast("成功清理缓存") toast("成功清理缓存")
} }
} }

@ -1,6 +1,5 @@
package io.legado.app.ui.config package io.legado.app.ui.config
import android.content.Context
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.text.InputType import android.text.InputType
@ -106,7 +105,22 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), Preference.OnPreference
WebDavHelp.showRestoreDialog(requireContext()) WebDavHelp.showRestoreDialog(requireContext())
} }
.request() .request()
"import_old" -> needInstallApps { importOld() } "import_old" -> needInstallApps {
alert(title = "导入") {
message = "是否导入旧版本数据"
yesButton {
PermissionsCompat.Builder(this@WebDavConfigFragment)
.addPermissions(*Permissions.Group.STORAGE)
.rationale(R.string.tip_perm_request_storage)
.onGranted {
Restore.importYueDuData(requireContext())
}
.request()
}
noButton {
}
}.show().applyTint()
}
} }
return super.onPreferenceTreeClick(preference) return super.onPreferenceTreeClick(preference)
} }
@ -133,21 +147,4 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), Preference.OnPreference
callback() callback()
} }
} }
private fun importOld() {
alert(title = "导入") {
message = "是否导入旧版本数据"
yesButton {
PermissionsCompat.Builder(this@WebDavConfigFragment)
.addPermissions(*Permissions.Group.STORAGE)
.rationale(R.string.tip_perm_request_storage)
.onGranted {
Restore.importYueDuData(requireContext())
}
.request()
}
noButton {
}
}.show().applyTint()
}
} }

@ -40,7 +40,7 @@
<item>@string/layout_grid3</item> <item>@string/layout_grid3</item>
<item>@string/layout_grid4</item> <item>@string/layout_grid4</item>
<item>@string/layout_grid5</item> <item>@string/layout_grid5</item>
<item>@string/layout_grid6</item> <!-- <item>@string/layout_grid6</item>-->
</string-array> </string-array>
<string-array name="indent"> <string-array name="indent">

@ -20,19 +20,23 @@
android:summary="@string/web_dav_pw_s" android:summary="@string/web_dav_pw_s"
app:iconSpaceReserved="false" /> app:iconSpaceReserved="false" />
<androidx.preference.Preference <io.legado.app.lib.theme.prefs.ATEPreferenceCategory app:iconSpaceReserved="false">
android:key="web_dav_backup"
android:title="备份"
app:iconSpaceReserved="false" />
<androidx.preference.Preference <androidx.preference.Preference
android:key="web_dav_restore" android:key="web_dav_backup"
android:title="恢复" android:title="备份"
app:iconSpaceReserved="false" /> app:iconSpaceReserved="false" />
<androidx.preference.Preference <androidx.preference.Preference
android:key="import_old" android:key="web_dav_restore"
android:title="导入旧版本数据" android:title="恢复"
app:iconSpaceReserved="false" /> app:iconSpaceReserved="false" />
<androidx.preference.Preference
android:key="import_old"
android:title="导入旧版本数据"
app:iconSpaceReserved="false" />
</io.legado.app.lib.theme.prefs.ATEPreferenceCategory>
</androidx.preference.PreferenceScreen> </androidx.preference.PreferenceScreen>
Loading…
Cancel
Save