pull/68/head^2
kunfei 5 years ago
parent 75b1b3fe89
commit a433637e3c
  1. 5
      app/src/main/java/io/legado/app/data/AppDatabase.kt
  2. 4
      app/src/main/java/io/legado/app/help/storage/Backup.kt
  3. 25
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  4. 5
      app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt
  5. 11
      app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt
  6. 14
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt

@ -7,7 +7,10 @@ import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase import androidx.sqlite.db.SupportSQLiteDatabase
import io.legado.app.data.dao.* import io.legado.app.data.dao.*
import io.legado.app.data.entities.* import io.legado.app.data.entities.*
import io.legado.app.help.storage.Backup
import io.legado.app.help.storage.Restore import io.legado.app.help.storage.Restore
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
@Database( @Database(
@ -28,7 +31,7 @@ abstract class AppDatabase : RoomDatabase() {
.fallbackToDestructiveMigration() .fallbackToDestructiveMigration()
.addCallback(object : Callback() { .addCallback(object : Callback() {
override fun onDestructiveMigration(db: SupportSQLiteDatabase) { override fun onDestructiveMigration(db: SupportSQLiteDatabase) {
Restore.restore() GlobalScope.launch { Restore.restore(Backup.defaultPath) }
} }
}) })
.build() .build()

@ -15,7 +15,7 @@ import java.io.File
object Backup { object Backup {
private val backupPath = App.INSTANCE.filesDir.absolutePath + File.separator + "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"
@ -29,7 +29,7 @@ object Backup {
legadoPath + File.separator + "Export" legadoPath + File.separator + "Export"
} }
private val backupFileNames by lazy { val backupFileNames by lazy {
arrayOf( arrayOf(
"bookshelf.json", "bookshelf.json",
"bookGroup.json", "bookGroup.json",

@ -3,12 +3,12 @@ package io.legado.app.help.storage
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
import android.util.Log import android.util.Log
import androidx.documentfile.provider.DocumentFile
import com.jayway.jsonpath.Configuration import com.jayway.jsonpath.Configuration
import com.jayway.jsonpath.JsonPath import com.jayway.jsonpath.JsonPath
import com.jayway.jsonpath.Option import com.jayway.jsonpath.Option
import com.jayway.jsonpath.ParseContext import com.jayway.jsonpath.ParseContext
import io.legado.app.App import io.legado.app.App
import io.legado.app.R
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.data.entities.* import io.legado.app.data.entities.*
import io.legado.app.help.FileHelp import io.legado.app.help.FileHelp
@ -20,9 +20,7 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.jetbrains.anko.defaultSharedPreferences import org.jetbrains.anko.defaultSharedPreferences
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.toast import org.jetbrains.anko.toast
import org.jetbrains.anko.uiThread
import java.io.File import java.io.File
object Restore { object Restore {
@ -34,12 +32,24 @@ object Restore {
) )
} }
fun restore(context: Context, uri: Uri) { suspend fun restore(context: Context, uri: Uri) {
withContext(IO) {
DocumentFile.fromTreeUri(context, uri)?.listFiles()?.forEach { doc ->
for (fileName in Backup.backupFileNames) {
if (doc.name == fileName) {
DocumentUtils.readText(context, doc.uri)?.let {
FileHelp.getFile(Backup.backupPath + File.separator + fileName)
.writeText(it)
}
}
}
}
}
restore(Backup.defaultPath)
} }
fun restore(path: String = Backup.defaultPath) { suspend fun restore(path: String) {
doAsync { withContext(IO) {
try { try {
val file = FileHelp.getFile(path + File.separator + "bookshelf.json") val file = FileHelp.getFile(path + File.separator + "bookshelf.json")
val json = file.readText() val json = file.readText()
@ -109,7 +119,6 @@ object Restore {
} }
edit.commit() edit.commit()
} }
uiThread { App.INSTANCE.toast(R.string.restore_success) }
} }
} }

@ -4,13 +4,13 @@ import android.content.Context
import io.legado.app.App import io.legado.app.App
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.help.coroutine.Coroutine
import io.legado.app.lib.webdav.WebDav import io.legado.app.lib.webdav.WebDav
import io.legado.app.lib.webdav.http.HttpAuth import io.legado.app.lib.webdav.http.HttpAuth
import io.legado.app.utils.ZipUtils import io.legado.app.utils.ZipUtils
import io.legado.app.utils.getPrefString import io.legado.app.utils.getPrefString
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.selector import org.jetbrains.anko.selector
import java.io.File import java.io.File
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
@ -70,10 +70,11 @@ object WebDavHelp {
} }
private fun restoreWebDav(name: String) { private fun restoreWebDav(name: String) {
doAsync { Coroutine.async {
getWebDavUrl()?.let { getWebDavUrl()?.let {
val file = WebDav(it + "legado/" + name) val file = WebDav(it + "legado/" + name)
file.downloadTo(zipFilePath, true) file.downloadTo(zipFilePath, true)
@Suppress("BlockingMethodInNonBlockingContext")
ZipUtils.unzipFile(zipFilePath, unzipFilesPath) ZipUtils.unzipFile(zipFilePath, unzipFilesPath)
Restore.restore(unzipFilesPath) Restore.restore(unzipFilesPath)
} }

@ -162,6 +162,7 @@ class WebDavConfigFragment : PreferenceFragmentCompat(),
val doc = DocumentFile.fromTreeUri(requireContext(), uri) val doc = DocumentFile.fromTreeUri(requireContext(), uri)
if (doc?.canWrite() == true) { if (doc?.canWrite() == true) {
Restore.restore(requireContext(), uri) Restore.restore(requireContext(), uri)
toast(R.string.restore_success)
} else { } else {
selectBackupFolder() selectBackupFolder()
} }
@ -179,7 +180,12 @@ class WebDavConfigFragment : PreferenceFragmentCompat(),
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 { Restore.restore(Backup.legadoPath) } .onGranted {
launch {
Restore.restore(Backup.legadoPath)
toast(R.string.restore_success)
}
}
.request() .request()
} }
} }
@ -304,7 +310,10 @@ class WebDavConfigFragment : PreferenceFragmentCompat(),
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
) )
putPrefString(PreferKey.backupPath, uri.toString()) putPrefString(PreferKey.backupPath, uri.toString())
launch {
Restore.restore(requireContext(), uri) Restore.restore(requireContext(), uri)
toast(R.string.restore_success)
}
} }
} }
} }

@ -36,6 +36,7 @@ import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.jetbrains.anko.toast
class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main), class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main),
BottomNavigationView.OnNavigationItemSelectedListener, BottomNavigationView.OnNavigationItemSelectedListener,
@ -182,6 +183,7 @@ class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main),
val doc = DocumentFile.fromTreeUri(this@MainActivity, uri) val doc = DocumentFile.fromTreeUri(this@MainActivity, uri)
if (doc?.canWrite() == true) { if (doc?.canWrite() == true) {
Restore.restore(this@MainActivity, uri) Restore.restore(this@MainActivity, uri)
toast(R.string.restore_success)
} else { } else {
selectBackupFolder() selectBackupFolder()
} }
@ -213,7 +215,12 @@ 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 { Restore.restore(Backup.legadoPath) } .onGranted {
launch {
Restore.restore(Backup.legadoPath)
toast(R.string.restore_success)
}
}
.request() .request()
} }
} }
@ -238,7 +245,10 @@ class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main),
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
) )
putPrefString(PreferKey.backupPath, uri.toString()) putPrefString(PreferKey.backupPath, uri.toString())
Restore.restore(this, uri) launch {
Restore.restore(this@MainActivity, uri)
toast(R.string.restore_success)
}
} }
} }
} }

Loading…
Cancel
Save