From cde3e5688fa58a81514dc9131b8aecd0971b396b Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 5 Jan 2020 21:46:07 +0800 Subject: [PATCH 1/3] up --- .../java/io/legado/app/help/storage/Backup.kt | 141 +++++------------- .../io/legado/app/ui/main/MainActivity.kt | 15 +- 2 files changed, 49 insertions(+), 107 deletions(-) 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 53d9a4279..a7fce4b21 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 @@ -4,21 +4,19 @@ import android.content.Context import android.net.Uri import androidx.documentfile.provider.DocumentFile import io.legado.app.App -import io.legado.app.R import io.legado.app.help.FileHelp import io.legado.app.help.ReadBookConfig import io.legado.app.utils.DocumentUtils import io.legado.app.utils.FileUtils import io.legado.app.utils.GSON import org.jetbrains.anko.defaultSharedPreferences -import org.jetbrains.anko.doAsync -import org.jetbrains.anko.toast -import org.jetbrains.anko.uiThread import java.io.File object Backup { + val backupPath = App.INSTANCE.filesDir.absolutePath + File.separator + "backup" + val defaultPath by lazy { FileUtils.getSdCardPath() + File.separator + "YueDu" } @@ -31,134 +29,42 @@ object Backup { legadoPath + File.separator + "Export" } - fun backup(context: Context, uri: Uri) { - DocumentFile.fromTreeUri(context, uri)?.listFiles()?.forEach { doc -> - when (doc.name) { - "bookshelf.json" -> App.db.bookDao().allBooks.let { - if (it.isNotEmpty()) { - val json = GSON.toJson(it) - DocumentUtils.writeText(context, json, doc.uri) - } - } - "bookGroup.json" -> App.db.bookGroupDao().all().let { - if (it.isNotEmpty()) { - val json = GSON.toJson(it) - DocumentUtils.writeText(context, json, doc.uri) - } - } - "bookSource.json" -> App.db.bookSourceDao().all.let { - if (it.isNotEmpty()) { - val json = GSON.toJson(it) - DocumentUtils.writeText(context, json, doc.uri) - } - } - "rssSource.json" -> App.db.rssSourceDao().all.let { - if (it.isNotEmpty()) { - val json = GSON.toJson(it) - DocumentUtils.writeText(context, json, doc.uri) - } - } - "replaceRule.json" -> App.db.replaceRuleDao().all.let { - if (it.isNotEmpty()) { - val json = GSON.toJson(it) - DocumentUtils.writeText(context, json, doc.uri) - } - } - ReadBookConfig.readConfigFileName -> GSON.toJson(ReadBookConfig.configList)?.let { - DocumentUtils.writeText(context, it, doc.uri) - } - "config.xml" -> { - - } - } - } - } - - private fun pBackup(path: String = legadoPath) { - backupBookshelf(path) - backupBookGroup(path) - backupBookSource(path) - backupRssSource(path) - backupReplaceRule(path) - backupReadConfig(path) - backupPreference(path) - WebDavHelp.backUpWebDav(path) - } - - fun backup() { - doAsync { - pBackup() - uiThread { - App.INSTANCE.toast(R.string.backup_success) - } - } - } - - fun autoBackup() { - doAsync { - pBackup() - } - } - - private fun backupBookshelf(path: String) { + fun backup(context: Context, uri: Uri?) { App.db.bookDao().allBooks.let { if (it.isNotEmpty()) { val json = GSON.toJson(it) - val file = FileHelp.getFile(path + File.separator + "bookshelf.json") - file.writeText(json) + FileHelp.getFile(backupPath + File.separator + "bookshelf.json").writeText(json) } } - } - - private fun backupBookGroup(path: String) { App.db.bookGroupDao().all().let { if (it.isNotEmpty()) { val json = GSON.toJson(it) - val file = FileHelp.getFile(path + File.separator + "bookGroup.json") - file.writeText(json) + FileHelp.getFile(backupPath + File.separator + "bookGroup.json").writeText(json) } } - } - - private fun backupBookSource(path: String) { App.db.bookSourceDao().all.let { if (it.isNotEmpty()) { val json = GSON.toJson(it) - val file = FileHelp.getFile(path + File.separator + "bookSource.json") - file.writeText(json) + FileHelp.getFile(backupPath + File.separator + "bookSource.json").writeText(json) } } - } - - private fun backupRssSource(path: String) { App.db.rssSourceDao().all.let { if (it.isNotEmpty()) { val json = GSON.toJson(it) - val file = FileHelp.getFile(path + File.separator + "rssSource.json") - file.writeText(json) + FileHelp.getFile(backupPath + File.separator + "rssSource.json").writeText(json) } } - } - - private fun backupReplaceRule(path: String) { App.db.replaceRuleDao().all.let { if (it.isNotEmpty()) { val json = GSON.toJson(it) - val file = FileHelp.getFile(path + File.separator + "replaceRule.json") - file.writeText(json) + FileHelp.getFile(backupPath + File.separator + "replaceRule.json").writeText(json) } } - } - - private fun backupReadConfig(path: String) { GSON.toJson(ReadBookConfig.configList)?.let { - FileHelp.getFile(path + File.separator + ReadBookConfig.readConfigFileName) + FileHelp.getFile(backupPath + File.separator + ReadBookConfig.readConfigFileName) .writeText(it) } - } - - private fun backupPreference(path: String) { - Preferences.getSharedPreferences(App.INSTANCE, path, "config")?.let { sp -> + Preferences.getSharedPreferences(App.INSTANCE, backupPath, "config")?.let { sp -> val edit = sp.edit() App.INSTANCE.defaultSharedPreferences.all.map { when (val value = it.value) { @@ -172,6 +78,33 @@ object Backup { } edit.commit() } + WebDavHelp.backUpWebDav(backupPath) + if (uri != null) { + copyBackup(context, uri) + } + } + + private fun copyBackup(context: Context, uri: Uri) { + DocumentFile.fromTreeUri(context, uri)?.let { treeDoc -> + val fileNames = arrayOf( + "bookshelf.json", + "bookGroup.json", + "bookSource.json", + "rssSource.json", + "replaceRule.json", + ReadBookConfig.readConfigFileName, + "config.xml" + ) + for (fileName in fileNames) { + treeDoc.createFile("text/plain", fileName)?.let { doc -> + DocumentUtils.writeText( + context, + FileHelp.getFile(backupPath + File.separator + fileName).readText(), + doc.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 76874e612..0a6ca93c1 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 @@ -33,6 +33,9 @@ import io.legado.app.ui.main.my.MyFragment import io.legado.app.ui.main.rss.RssFragment import io.legado.app.utils.* import kotlinx.android.synthetic.main.activity_main.* +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext class MainActivity : VMBaseActivity(R.layout.activity_main), BottomNavigationView.OnNavigationItemSelectedListener, @@ -123,9 +126,15 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), override fun finish() { if (!BuildConfig.DEBUG) { - Backup.autoBackup() + launch { + withContext(IO) { + backup() + } + super.finish() + } + } else { + super.finish() } - super.finish() } override fun onDestroy() { @@ -183,7 +192,7 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), PermissionsCompat.Builder(this) .addPermissions(*Permissions.Group.STORAGE) .rationale(R.string.tip_perm_request_storage) - .onGranted { Backup.backup() } + .onGranted { Backup.backup(this, null) } .request() } } From 2645bfeaea0a0e7d11e12ee6b7802799c0aecce0 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 5 Jan 2020 21:54:09 +0800 Subject: [PATCH 2/3] up --- .../java/io/legado/app/help/storage/Backup.kt | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) 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 a7fce4b21..462bc445a 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 @@ -15,7 +15,7 @@ import java.io.File object Backup { - val backupPath = App.INSTANCE.filesDir.absolutePath + File.separator + "backup" + private val backupPath = App.INSTANCE.filesDir.absolutePath + File.separator + "backup" val defaultPath by lazy { FileUtils.getSdCardPath() + File.separator + "YueDu" @@ -29,6 +29,18 @@ object Backup { legadoPath + File.separator + "Export" } + val backupFileNames by lazy { + arrayOf( + "bookshelf.json", + "bookGroup.json", + "bookSource.json", + "rssSource.json", + "replaceRule.json", + ReadBookConfig.readConfigFileName, + "config.xml" + ) + } + fun backup(context: Context, uri: Uri?) { App.db.bookDao().allBooks.let { if (it.isNotEmpty()) { @@ -81,21 +93,14 @@ object Backup { WebDavHelp.backUpWebDav(backupPath) if (uri != null) { copyBackup(context, uri) + } else { + copyBackup() } } private fun copyBackup(context: Context, uri: Uri) { DocumentFile.fromTreeUri(context, uri)?.let { treeDoc -> - val fileNames = arrayOf( - "bookshelf.json", - "bookGroup.json", - "bookSource.json", - "rssSource.json", - "replaceRule.json", - ReadBookConfig.readConfigFileName, - "config.xml" - ) - for (fileName in fileNames) { + for (fileName in backupFileNames) { treeDoc.createFile("text/plain", fileName)?.let { doc -> DocumentUtils.writeText( context, @@ -107,4 +112,10 @@ object Backup { } } + private fun copyBackup() { + for (fileName in backupFileNames) { + FileHelp.getFile(backupPath + File.separator + "bookshelf.json") + .copyTo(FileHelp.getFile(legadoPath + File.separator + "bookshelf.json")) + } + } } \ No newline at end of file From 447397d47a1072506f781237b270cc633e52e558 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 5 Jan 2020 21:54:25 +0800 Subject: [PATCH 3/3] up --- app/src/main/java/io/legado/app/help/storage/Backup.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 462bc445a..3f8e65dd7 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 @@ -29,7 +29,7 @@ object Backup { legadoPath + File.separator + "Export" } - val backupFileNames by lazy { + private val backupFileNames by lazy { arrayOf( "bookshelf.json", "bookGroup.json",