diff --git a/app/src/main/java/io/legado/app/help/storage/Backup.kt b/app/src/main/java/io/legado/app/help/storage/Backup.kt index 2f0b040a4..7062d1951 100644 --- a/app/src/main/java/io/legado/app/help/storage/Backup.kt +++ b/app/src/main/java/io/legado/app/help/storage/Backup.kt @@ -20,23 +20,26 @@ object Backup { } val legadoPath by lazy { - defaultPath + File.separator + "legadoBackUp" + FileUtils.getSdCardPath() + File.separator + "YueDu3.0" } val exportPath by lazy { 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() { doAsync { - val path = legadoPath - backupBookshelf(path) - backupBookSource(path) - backupRssSource(path) - backupReplaceRule(path) - backupReadConfig(path) - backupPreference(path) - WebDavHelp.backUpWebDav(path) + pbackup() uiThread { App.INSTANCE.toast(R.string.backup_success) } @@ -45,14 +48,7 @@ object Backup { fun autoBackup() { doAsync { - val path = legadoPath - backupBookshelf(path) - backupBookSource(path) - backupRssSource(path) - backupReplaceRule(path) - backupReadConfig(path) - backupPreference(path) - WebDavHelp.backUpWebDav(path) + pbackup() } } 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 7d4c8191d..53b040f28 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 @@ -7,6 +7,7 @@ import com.jayway.jsonpath.JsonPath import com.jayway.jsonpath.Option import com.jayway.jsonpath.ParseContext import io.legado.app.App +import io.legado.app.R import io.legado.app.constant.AppConst import io.legado.app.data.entities.Book 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.help.FileHelp 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 kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main @@ -37,7 +36,7 @@ object Restore { ) } - fun restore(path: String = legadoPath) { + fun restore(path: String = Backup.legadoPath) { doAsync { try { val file = FileHelp.getFile(path + File.separator + "bookshelf.json") @@ -99,14 +98,14 @@ object Restore { } edit.commit() } - uiThread { App.INSTANCE.toast("恢复完成") } + uiThread { App.INSTANCE.toast(R.string.restore_success) } } } fun importYueDuData(context: Context) { GlobalScope.launch(IO) { try {// 导入书架 - val shelfFile = FileHelp.getFile(defaultPath + File.separator + "myBookShelf.json") + val shelfFile = FileHelp.getFile(Backup.defaultPath + File.separator + "myBookShelf.json") val books = mutableListOf() val items: List> = jsonPath.parse(shelfFile.readText()).read("$") val existingBooks = App.db.bookDao().allBookUrls.toSet() @@ -156,7 +155,7 @@ object Restore { try {// Book source val sourceFile = - FileHelp.getFile(defaultPath + File.separator + "myBookSource.json") + FileHelp.getFile(Backup.defaultPath + File.separator + "myBookSource.json") val bookSources = mutableListOf() val items: List> = jsonPath.parse(sourceFile.readText()).read("$") for (item in items) { @@ -177,7 +176,7 @@ object Restore { try {// Replace rules val ruleFile = - FileHelp.getFile(defaultPath + File.separator + "myBookReplaceRule.json") + FileHelp.getFile(Backup.defaultPath + File.separator + "myBookReplaceRule.json") val replaceRules = mutableListOf() val items: List> = jsonPath.parse(ruleFile.readText()).read("$") val existingRules = App.db.replaceRuleDao().all.map { it.pattern }.toSet() 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 7d2022b5b..a4ac7fbcc 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 @@ -18,6 +18,9 @@ import kotlin.math.min object WebDavHelp { private val zipFilePath = FileHelp.getCachePath() + "/backup" + ".zip" + private val unzipFilesPath by lazy { + FileHelp.getCachePath() + } private fun getWebDavUrl(): String? { var url = App.INSTANCE.getPrefString("web_dav_url") @@ -73,8 +76,8 @@ object WebDavHelp { getWebDavUrl()?.let { val file = WebDav(it + "legado/" + name) file.downloadTo(zipFilePath, true) - ZipUtils.unzipFile(zipFilePath, Backup.legadoPath) - Restore.restore() + ZipUtils.unzipFile(zipFilePath, unzipFilesPath) + Restore.restore(unzipFilesPath) } } } diff --git a/app/src/main/java/io/legado/app/ui/config/ConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/ConfigFragment.kt index a4a7bd62d..927804039 100644 --- a/app/src/main/java/io/legado/app/ui/config/ConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/ConfigFragment.kt @@ -61,8 +61,10 @@ class ConfigFragment : PreferenceFragmentCompat(), initPath = getPreferenceString(PreferKey.downloadPath) ) PreferKey.cleanCache -> { - LogUtils.d("xxx","cleancaches") - FileHelp.deleteFile(getPreferenceString(PreferKey.downloadPath)) + getPreferenceString(PreferKey.downloadPath).let { + FileHelp.deleteFile(it) + FileHelp.getFolder(it) + } toast("成功清理缓存") } } 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 1e4d34a73..9eb761712 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 @@ -1,6 +1,5 @@ package io.legado.app.ui.config -import android.content.Context import android.os.Build import android.os.Bundle import android.text.InputType @@ -106,7 +105,22 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), Preference.OnPreference WebDavHelp.showRestoreDialog(requireContext()) } .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) } @@ -133,21 +147,4 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), Preference.OnPreference 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() - } } \ No newline at end of file diff --git a/app/src/main/res/xml/pref_config_web_dav.xml b/app/src/main/res/xml/pref_config_web_dav.xml index 51234ec1a..42733f458 100644 --- a/app/src/main/res/xml/pref_config_web_dav.xml +++ b/app/src/main/res/xml/pref_config_web_dav.xml @@ -20,19 +20,23 @@ android:summary="@string/web_dav_pw_s" app:iconSpaceReserved="false" /> - + - + - + + + + + \ No newline at end of file