pull/68/head
kunfei 5 years ago
parent 78a9aace2f
commit cde3e5688f
  1. 141
      app/src/main/java/io/legado/app/help/storage/Backup.kt
  2. 15
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt

@ -4,21 +4,19 @@ import android.content.Context
import android.net.Uri import android.net.Uri
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import io.legado.app.App import io.legado.app.App
import io.legado.app.R
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.utils.DocumentUtils import io.legado.app.utils.DocumentUtils
import io.legado.app.utils.FileUtils import io.legado.app.utils.FileUtils
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import org.jetbrains.anko.defaultSharedPreferences import org.jetbrains.anko.defaultSharedPreferences
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.toast
import org.jetbrains.anko.uiThread
import java.io.File import java.io.File
object Backup { object Backup {
val backupPath = App.INSTANCE.filesDir.absolutePath + File.separator + "backup"
val defaultPath by lazy { val defaultPath by lazy {
FileUtils.getSdCardPath() + File.separator + "YueDu" FileUtils.getSdCardPath() + File.separator + "YueDu"
} }
@ -31,134 +29,42 @@ object Backup {
legadoPath + File.separator + "Export" legadoPath + File.separator + "Export"
} }
fun backup(context: Context, uri: Uri) { 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) {
App.db.bookDao().allBooks.let { App.db.bookDao().allBooks.let {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
val json = GSON.toJson(it) val json = GSON.toJson(it)
val file = FileHelp.getFile(path + File.separator + "bookshelf.json") FileHelp.getFile(backupPath + File.separator + "bookshelf.json").writeText(json)
file.writeText(json)
} }
} }
}
private fun backupBookGroup(path: String) {
App.db.bookGroupDao().all().let { App.db.bookGroupDao().all().let {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
val json = GSON.toJson(it) val json = GSON.toJson(it)
val file = FileHelp.getFile(path + File.separator + "bookGroup.json") FileHelp.getFile(backupPath + File.separator + "bookGroup.json").writeText(json)
file.writeText(json)
} }
} }
}
private fun backupBookSource(path: String) {
App.db.bookSourceDao().all.let { App.db.bookSourceDao().all.let {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
val json = GSON.toJson(it) val json = GSON.toJson(it)
val file = FileHelp.getFile(path + File.separator + "bookSource.json") FileHelp.getFile(backupPath + File.separator + "bookSource.json").writeText(json)
file.writeText(json)
} }
} }
}
private fun backupRssSource(path: String) {
App.db.rssSourceDao().all.let { App.db.rssSourceDao().all.let {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
val json = GSON.toJson(it) val json = GSON.toJson(it)
val file = FileHelp.getFile(path + File.separator + "rssSource.json") FileHelp.getFile(backupPath + File.separator + "rssSource.json").writeText(json)
file.writeText(json)
} }
} }
}
private fun backupReplaceRule(path: String) {
App.db.replaceRuleDao().all.let { App.db.replaceRuleDao().all.let {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
val json = GSON.toJson(it) val json = GSON.toJson(it)
val file = FileHelp.getFile(path + File.separator + "replaceRule.json") FileHelp.getFile(backupPath + File.separator + "replaceRule.json").writeText(json)
file.writeText(json)
} }
} }
}
private fun backupReadConfig(path: String) {
GSON.toJson(ReadBookConfig.configList)?.let { GSON.toJson(ReadBookConfig.configList)?.let {
FileHelp.getFile(path + File.separator + ReadBookConfig.readConfigFileName) FileHelp.getFile(backupPath + File.separator + ReadBookConfig.readConfigFileName)
.writeText(it) .writeText(it)
} }
} Preferences.getSharedPreferences(App.INSTANCE, backupPath, "config")?.let { sp ->
private fun backupPreference(path: String) {
Preferences.getSharedPreferences(App.INSTANCE, path, "config")?.let { sp ->
val edit = sp.edit() val edit = sp.edit()
App.INSTANCE.defaultSharedPreferences.all.map { App.INSTANCE.defaultSharedPreferences.all.map {
when (val value = it.value) { when (val value = it.value) {
@ -172,6 +78,33 @@ object Backup {
} }
edit.commit() 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
)
}
}
}
} }
} }

@ -33,6 +33,9 @@ import io.legado.app.ui.main.my.MyFragment
import io.legado.app.ui.main.rss.RssFragment import io.legado.app.ui.main.rss.RssFragment
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main), class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main),
BottomNavigationView.OnNavigationItemSelectedListener, BottomNavigationView.OnNavigationItemSelectedListener,
@ -123,9 +126,15 @@ class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main),
override fun finish() { override fun finish() {
if (!BuildConfig.DEBUG) { if (!BuildConfig.DEBUG) {
Backup.autoBackup() launch {
withContext(IO) {
backup()
}
super.finish()
}
} else {
super.finish()
} }
super.finish()
} }
override fun onDestroy() { override fun onDestroy() {
@ -183,7 +192,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 { Backup.backup() } .onGranted { Backup.backup(this, null) }
.request() .request()
} }
} }

Loading…
Cancel
Save