From a433637e3cd4e8a0e3f242d329a364d760697295 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 6 Jan 2020 21:17:50 +0800 Subject: [PATCH] up --- .../java/io/legado/app/data/AppDatabase.kt | 5 +++- .../java/io/legado/app/help/storage/Backup.kt | 4 +-- .../io/legado/app/help/storage/Restore.kt | 25 +++++++++++++------ .../io/legado/app/help/storage/WebDavHelp.kt | 5 ++-- .../app/ui/config/WebDavConfigFragment.kt | 13 ++++++++-- .../io/legado/app/ui/main/MainActivity.kt | 14 +++++++++-- 6 files changed, 49 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/AppDatabase.kt b/app/src/main/java/io/legado/app/data/AppDatabase.kt index cab698fbd..0b43afd10 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -7,7 +7,10 @@ import androidx.room.RoomDatabase import androidx.sqlite.db.SupportSQLiteDatabase import io.legado.app.data.dao.* import io.legado.app.data.entities.* +import io.legado.app.help.storage.Backup import io.legado.app.help.storage.Restore +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch @Database( @@ -28,7 +31,7 @@ abstract class AppDatabase : RoomDatabase() { .fallbackToDestructiveMigration() .addCallback(object : Callback() { override fun onDestructiveMigration(db: SupportSQLiteDatabase) { - Restore.restore() + GlobalScope.launch { Restore.restore(Backup.defaultPath) } } }) .build() 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 3f8e65dd7..1eee3d582 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 { - private val backupPath = App.INSTANCE.filesDir.absolutePath + File.separator + "backup" + val backupPath = App.INSTANCE.filesDir.absolutePath + File.separator + "backup" val defaultPath by lazy { FileUtils.getSdCardPath() + File.separator + "YueDu" @@ -29,7 +29,7 @@ object Backup { legadoPath + File.separator + "Export" } - private val backupFileNames by lazy { + val backupFileNames by lazy { arrayOf( "bookshelf.json", "bookGroup.json", diff --git a/app/src/main/java/io/legado/app/help/storage/Restore.kt b/app/src/main/java/io/legado/app/help/storage/Restore.kt index ea1bf5392..801d27f01 100644 --- a/app/src/main/java/io/legado/app/help/storage/Restore.kt +++ b/app/src/main/java/io/legado/app/help/storage/Restore.kt @@ -3,12 +3,12 @@ package io.legado.app.help.storage import android.content.Context import android.net.Uri import android.util.Log +import androidx.documentfile.provider.DocumentFile import com.jayway.jsonpath.Configuration import com.jayway.jsonpath.JsonPath import com.jayway.jsonpath.Option import com.jayway.jsonpath.ParseContext import io.legado.app.App -import io.legado.app.R import io.legado.app.constant.AppConst import io.legado.app.data.entities.* import io.legado.app.help.FileHelp @@ -20,9 +20,7 @@ import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import kotlinx.coroutines.withContext 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 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) { - doAsync { + suspend fun restore(path: String) { + withContext(IO) { try { val file = FileHelp.getFile(path + File.separator + "bookshelf.json") val json = file.readText() @@ -109,7 +119,6 @@ object Restore { } edit.commit() } - uiThread { App.INSTANCE.toast(R.string.restore_success) } } } diff --git a/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt b/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt index 6be8c205d..4a0f3e152 100644 --- a/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt +++ b/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt @@ -4,13 +4,13 @@ import android.content.Context import io.legado.app.App import io.legado.app.help.FileHelp 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.http.HttpAuth import io.legado.app.utils.ZipUtils import io.legado.app.utils.getPrefString import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.withContext -import org.jetbrains.anko.doAsync import org.jetbrains.anko.selector import java.io.File import java.text.SimpleDateFormat @@ -70,10 +70,11 @@ object WebDavHelp { } private fun restoreWebDav(name: String) { - doAsync { + Coroutine.async { getWebDavUrl()?.let { val file = WebDav(it + "legado/" + name) file.downloadTo(zipFilePath, true) + @Suppress("BlockingMethodInNonBlockingContext") ZipUtils.unzipFile(zipFilePath, unzipFilesPath) Restore.restore(unzipFilesPath) } diff --git a/app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt index 763491d54..490b82e67 100644 --- a/app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt @@ -162,6 +162,7 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), val doc = DocumentFile.fromTreeUri(requireContext(), uri) if (doc?.canWrite() == true) { Restore.restore(requireContext(), uri) + toast(R.string.restore_success) } else { selectBackupFolder() } @@ -179,7 +180,12 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), PermissionsCompat.Builder(this) .addPermissions(*Permissions.Group.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() } } @@ -304,7 +310,10 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION ) putPrefString(PreferKey.backupPath, uri.toString()) - Restore.restore(requireContext(), uri) + launch { + Restore.restore(requireContext(), uri) + toast(R.string.restore_success) + } } } } 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 6c1287adc..fd1188e77 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 @@ -36,6 +36,7 @@ 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(R.layout.activity_main), BottomNavigationView.OnNavigationItemSelectedListener, @@ -182,6 +183,7 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), val doc = DocumentFile.fromTreeUri(this@MainActivity, uri) if (doc?.canWrite() == true) { Restore.restore(this@MainActivity, uri) + toast(R.string.restore_success) } else { selectBackupFolder() } @@ -213,7 +215,12 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), PermissionsCompat.Builder(this) .addPermissions(*Permissions.Group.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() } } @@ -238,7 +245,10 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION ) putPrefString(PreferKey.backupPath, uri.toString()) - Restore.restore(this, uri) + launch { + Restore.restore(this@MainActivity, uri) + toast(R.string.restore_success) + } } } }