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