pull/68/head^2
kunfei 5 years ago
parent a433637e3c
commit fd649bc73b
  1. 100
      app/src/main/java/io/legado/app/help/storage/Backup.kt
  2. 20
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt

@ -9,6 +9,8 @@ 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 kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext
import org.jetbrains.anko.defaultSharedPreferences
import java.io.File
@ -41,60 +43,64 @@ object Backup {
)
}
fun backup(context: Context, uri: Uri?) {
App.db.bookDao().allBooks.let {
if (it.isNotEmpty()) {
val json = GSON.toJson(it)
FileHelp.getFile(backupPath + File.separator + "bookshelf.json").writeText(json)
suspend fun backup(context: Context, uri: Uri?) {
withContext(IO) {
App.db.bookDao().allBooks.let {
if (it.isNotEmpty()) {
val json = GSON.toJson(it)
FileHelp.getFile(backupPath + File.separator + "bookshelf.json").writeText(json)
}
}
}
App.db.bookGroupDao().all().let {
if (it.isNotEmpty()) {
val json = GSON.toJson(it)
FileHelp.getFile(backupPath + File.separator + "bookGroup.json").writeText(json)
App.db.bookGroupDao().all().let {
if (it.isNotEmpty()) {
val json = GSON.toJson(it)
FileHelp.getFile(backupPath + File.separator + "bookGroup.json").writeText(json)
}
}
}
App.db.bookSourceDao().all.let {
if (it.isNotEmpty()) {
val json = GSON.toJson(it)
FileHelp.getFile(backupPath + File.separator + "bookSource.json").writeText(json)
App.db.bookSourceDao().all.let {
if (it.isNotEmpty()) {
val json = GSON.toJson(it)
FileHelp.getFile(backupPath + File.separator + "bookSource.json")
.writeText(json)
}
}
}
App.db.rssSourceDao().all.let {
if (it.isNotEmpty()) {
val json = GSON.toJson(it)
FileHelp.getFile(backupPath + File.separator + "rssSource.json").writeText(json)
App.db.rssSourceDao().all.let {
if (it.isNotEmpty()) {
val json = GSON.toJson(it)
FileHelp.getFile(backupPath + File.separator + "rssSource.json").writeText(json)
}
}
}
App.db.replaceRuleDao().all.let {
if (it.isNotEmpty()) {
val json = GSON.toJson(it)
FileHelp.getFile(backupPath + File.separator + "replaceRule.json").writeText(json)
App.db.replaceRuleDao().all.let {
if (it.isNotEmpty()) {
val json = GSON.toJson(it)
FileHelp.getFile(backupPath + File.separator + "replaceRule.json")
.writeText(json)
}
}
}
GSON.toJson(ReadBookConfig.configList)?.let {
FileHelp.getFile(backupPath + File.separator + ReadBookConfig.readConfigFileName)
.writeText(it)
}
Preferences.getSharedPreferences(App.INSTANCE, backupPath, "config")?.let { sp ->
val edit = sp.edit()
App.INSTANCE.defaultSharedPreferences.all.map {
when (val value = it.value) {
is Int -> edit.putInt(it.key, value)
is Boolean -> edit.putBoolean(it.key, value)
is Long -> edit.putLong(it.key, value)
is Float -> edit.putFloat(it.key, value)
is String -> edit.putString(it.key, value)
else -> Unit
GSON.toJson(ReadBookConfig.configList)?.let {
FileHelp.getFile(backupPath + File.separator + ReadBookConfig.readConfigFileName)
.writeText(it)
}
Preferences.getSharedPreferences(App.INSTANCE, backupPath, "config")?.let { sp ->
val edit = sp.edit()
App.INSTANCE.defaultSharedPreferences.all.map {
when (val value = it.value) {
is Int -> edit.putInt(it.key, value)
is Boolean -> edit.putBoolean(it.key, value)
is Long -> edit.putLong(it.key, value)
is Float -> edit.putFloat(it.key, value)
is String -> edit.putString(it.key, value)
else -> Unit
}
}
edit.commit()
}
WebDavHelp.backUpWebDav(backupPath)
if (uri != null) {
copyBackup(context, uri)
} else {
copyBackup()
}
edit.commit()
}
WebDavHelp.backUpWebDav(backupPath)
if (uri != null) {
copyBackup(context, uri)
} else {
copyBackup()
}
}

@ -33,9 +33,7 @@ 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
import org.jetbrains.anko.toast
class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main),
@ -128,9 +126,7 @@ class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main),
override fun finish() {
if (!BuildConfig.DEBUG) {
launch {
withContext(IO) {
backup()
}
backup()
super.finish()
}
} else {
@ -165,7 +161,9 @@ class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main),
val uri = Uri.parse(backupPath)
val doc = DocumentFile.fromTreeUri(this, uri)
if (doc?.canWrite() == true) {
Backup.backup(this, uri)
launch {
Backup.backup(this@MainActivity, uri)
}
} else {
selectBackupFolder()
}
@ -201,7 +199,11 @@ class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main),
PermissionsCompat.Builder(this)
.addPermissions(*Permissions.Group.STORAGE)
.rationale(R.string.tip_perm_request_storage)
.onGranted { Backup.backup(this, null) }
.onGranted {
launch {
Backup.backup(this@MainActivity, null)
}
}
.request()
}
}
@ -235,7 +237,9 @@ class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main),
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
putPrefString(PreferKey.backupPath, uri.toString())
Backup.backup(this, uri)
launch {
Backup.backup(this@MainActivity, uri)
}
}
}
restoreSelectRequestCode -> if (resultCode == Activity.RESULT_OK) {

Loading…
Cancel
Save