From 75b1b3fe898d046365325715993e876e2905c231 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 6 Jan 2020 20:50:48 +0800 Subject: [PATCH 01/17] up --- .../io/legado/app/help/storage/Restore.kt | 5 ++ .../io/legado/app/help/storage/WebDavHelp.kt | 24 ++++----- .../app/ui/config/WebDavConfigFragment.kt | 51 ++++++++++++++++--- .../io/legado/app/ui/main/MainActivity.kt | 28 ++++++---- 4 files changed, 78 insertions(+), 30 deletions(-) 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 116419a63..ea1bf5392 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 @@ -1,6 +1,7 @@ package io.legado.app.help.storage import android.content.Context +import android.net.Uri import android.util.Log import com.jayway.jsonpath.Configuration import com.jayway.jsonpath.JsonPath @@ -33,6 +34,10 @@ object Restore { ) } + fun restore(context: Context, uri: Uri) { + + } + fun restore(path: String = Backup.defaultPath) { doAsync { try { 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 a4ac7fbcc..6be8c205d 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 @@ -8,9 +8,10 @@ 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 org.jetbrains.anko.uiThread import java.io.File import java.text.SimpleDateFormat import java.util.* @@ -54,20 +55,17 @@ object WebDavHelp { return names } - fun showRestoreDialog(context: Context) { - doAsync { - val names = getWebDavFileNames() - if (names.isNotEmpty()) { - uiThread { - context.selector(title = "选择恢复文件", items = names) { _, index -> - if (index in 0 until names.size) { - restoreWebDav(names[index]) - } - } + suspend fun showRestoreDialog(context: Context): Boolean { + val names = withContext(IO) { getWebDavFileNames() } + return if (names.isNotEmpty()) { + context.selector(title = "选择恢复文件", items = names) { _, index -> + if (index in 0 until names.size) { + restoreWebDav(names[index]) } - } else { - Restore.restore() } + true + } else { + false } } 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 4dea5e0e7..763491d54 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 @@ -116,13 +116,7 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), override fun onPreferenceTreeClick(preference: Preference?): Boolean { when (preference?.key) { "web_dav_backup" -> backup() - "web_dav_restore" -> PermissionsCompat.Builder(this) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { - WebDavHelp.showRestoreDialog(requireContext()) - } - .request() + "web_dav_restore" -> restore() "import_old" -> importOldData() } return super.onPreferenceTreeClick(preference) @@ -157,6 +151,39 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), } } + fun restore() { + launch { + if (!WebDavHelp.showRestoreDialog(requireContext())) { + val backupPath = getPrefString(PreferKey.backupPath) + if (backupPath?.isEmpty() == true) { + selectRestoreFolder() + } else { + val uri = Uri.parse(backupPath) + val doc = DocumentFile.fromTreeUri(requireContext(), uri) + if (doc?.canWrite() == true) { + Restore.restore(requireContext(), uri) + } else { + selectBackupFolder() + } + } + } + } + } + + private fun selectRestoreFolder() { + try { + val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + startActivityForResult(intent, restoreSelectRequestCode) + } catch (e: java.lang.Exception) { + PermissionsCompat.Builder(this) + .addPermissions(*Permissions.Group.STORAGE) + .rationale(R.string.tip_perm_request_storage) + .onGranted { Restore.restore(Backup.legadoPath) } + .request() + } + } + private fun importOldData() { try { val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) @@ -270,6 +297,16 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), Backup.backup(requireContext(), uri) } } + restoreSelectRequestCode -> if (resultCode == RESULT_OK) { + data?.data?.let { uri -> + requireContext().contentResolver.takePersistableUriPermission( + uri, + Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION + ) + putPrefString(PreferKey.backupPath, uri.toString()) + Restore.restore(requireContext(), uri) + } + } } } } \ No newline at end of file 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 0a6ca93c1..6c1287adc 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 @@ -172,15 +172,22 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), } fun restore() { - PermissionsCompat.Builder(this) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { WebDavHelp.showRestoreDialog(this) } - .request() - } - - fun restore(uri: Uri) { - + launch { + if (!WebDavHelp.showRestoreDialog(this@MainActivity)) { + val backupPath = getPrefString(PreferKey.backupPath) + if (backupPath?.isEmpty() == true) { + selectRestoreFolder() + } else { + val uri = Uri.parse(backupPath) + val doc = DocumentFile.fromTreeUri(this@MainActivity, uri) + if (doc?.canWrite() == true) { + Restore.restore(this@MainActivity, uri) + } else { + selectBackupFolder() + } + } + } + } } private fun selectBackupFolder() { @@ -206,7 +213,7 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), PermissionsCompat.Builder(this) .addPermissions(*Permissions.Group.STORAGE) .rationale(R.string.tip_perm_request_storage) - .onGranted { Restore.restore() } + .onGranted { Restore.restore(Backup.legadoPath) } .request() } } @@ -231,6 +238,7 @@ 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) } } } From a433637e3cd4e8a0e3f242d329a364d760697295 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 6 Jan 2020 21:17:50 +0800 Subject: [PATCH 02/17] 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) + } } } } From fd649bc73bf69e022ba3be0bbb9ad31468e2d058 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 6 Jan 2020 21:41:07 +0800 Subject: [PATCH 03/17] up --- .../java/io/legado/app/help/storage/Backup.kt | 100 ++++++++++-------- .../io/legado/app/ui/main/MainActivity.kt | 20 ++-- 2 files changed, 65 insertions(+), 55 deletions(-) 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 1eee3d582..e0efa4b9e 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 @@ -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() } } 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 fd1188e77..c37e93149 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 @@ -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(R.layout.activity_main), @@ -128,9 +126,7 @@ class MainActivity : VMBaseActivity(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(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(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(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) { From 4e0aa155510e10212bbbea3be6a3a87627d0d1e6 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 6 Jan 2020 21:48:12 +0800 Subject: [PATCH 04/17] up --- .../legado/app/ui/config/WebDavConfigFragment.kt | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) 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 490b82e67..0f70be7ef 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 @@ -130,7 +130,9 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), val uri = Uri.parse(backupPath) val doc = DocumentFile.fromTreeUri(requireContext(), uri) if (doc?.canWrite() == true) { - Backup.backup(requireContext(), uri) + launch { + Backup.backup(requireContext(), uri) + } } else { selectBackupFolder() } @@ -146,7 +148,11 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), PermissionsCompat.Builder(this) .addPermissions(*Permissions.Group.STORAGE) .rationale(R.string.tip_perm_request_storage) - .onGranted { Backup.backup(requireContext(), null) } + .onGranted { + launch { + Backup.backup(requireContext(), null) + } + } .request() } } @@ -300,7 +306,9 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION ) putPrefString(PreferKey.backupPath, uri.toString()) - Backup.backup(requireContext(), uri) + launch { + Backup.backup(requireContext(), uri) + } } } restoreSelectRequestCode -> if (resultCode == RESULT_OK) { From 61345f3b676624402668e18ff41529cdb249f2b0 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 6 Jan 2020 21:57:45 +0800 Subject: [PATCH 05/17] up --- .../io/legado/app/ui/config/WebDavConfigFragment.kt | 12 ++++++------ .../main/java/io/legado/app/ui/main/MainActivity.kt | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) 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 0f70be7ef..b269f7a95 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 @@ -124,9 +124,7 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), private fun backup() { val backupPath = getPrefString(PreferKey.backupPath) - if (backupPath?.isEmpty() == true) { - selectBackupFolder() - } else { + if (backupPath?.isNotEmpty() == true) { val uri = Uri.parse(backupPath) val doc = DocumentFile.fromTreeUri(requireContext(), uri) if (doc?.canWrite() == true) { @@ -136,6 +134,8 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), } else { selectBackupFolder() } + } else { + selectBackupFolder() } } @@ -161,9 +161,7 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), launch { if (!WebDavHelp.showRestoreDialog(requireContext())) { val backupPath = getPrefString(PreferKey.backupPath) - if (backupPath?.isEmpty() == true) { - selectRestoreFolder() - } else { + if (backupPath?.isNotEmpty() == true) { val uri = Uri.parse(backupPath) val doc = DocumentFile.fromTreeUri(requireContext(), uri) if (doc?.canWrite() == true) { @@ -172,6 +170,8 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), } else { selectBackupFolder() } + } else { + selectRestoreFolder() } } } 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 c37e93149..80db372ca 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 @@ -155,9 +155,7 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), fun backup() { val backupPath = getPrefString(PreferKey.backupPath) - if (backupPath?.isEmpty() == true) { - selectBackupFolder() - } else { + if (backupPath?.isNotEmpty() == true) { val uri = Uri.parse(backupPath) val doc = DocumentFile.fromTreeUri(this, uri) if (doc?.canWrite() == true) { @@ -167,6 +165,8 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), } else { selectBackupFolder() } + } else { + selectBackupFolder() } } @@ -174,9 +174,7 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), launch { if (!WebDavHelp.showRestoreDialog(this@MainActivity)) { val backupPath = getPrefString(PreferKey.backupPath) - if (backupPath?.isEmpty() == true) { - selectRestoreFolder() - } else { + if (backupPath?.isNotEmpty() == true) { val uri = Uri.parse(backupPath) val doc = DocumentFile.fromTreeUri(this@MainActivity, uri) if (doc?.canWrite() == true) { @@ -185,6 +183,8 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), } else { selectBackupFolder() } + } else { + selectRestoreFolder() } } } From df3cfe08996226492d7dd9d95f27e667466b1fdd Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 6 Jan 2020 22:00:39 +0800 Subject: [PATCH 06/17] up --- app/src/main/java/io/legado/app/data/AppDatabase.kt | 2 +- app/src/main/java/io/legado/app/help/storage/Restore.kt | 2 +- 2 files changed, 2 insertions(+), 2 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 0b43afd10..d65b43ada 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -31,7 +31,7 @@ abstract class AppDatabase : RoomDatabase() { .fallbackToDestructiveMigration() .addCallback(object : Callback() { override fun onDestructiveMigration(db: SupportSQLiteDatabase) { - GlobalScope.launch { Restore.restore(Backup.defaultPath) } + GlobalScope.launch { Restore.restore(Backup.backupPath) } } }) .build() 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 801d27f01..a95626228 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 @@ -45,7 +45,7 @@ object Restore { } } } - restore(Backup.defaultPath) + restore(Backup.backupPath) } suspend fun restore(path: String) { From 3178d9b893d0a6878bc923ec88752e602972e0d5 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 6 Jan 2020 22:05:47 +0800 Subject: [PATCH 07/17] up --- app/src/main/java/io/legado/app/help/storage/Backup.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 e0efa4b9e..995ff8fdd 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 @@ -121,7 +121,7 @@ object Backup { private fun copyBackup() { for (fileName in backupFileNames) { FileHelp.getFile(backupPath + File.separator + "bookshelf.json") - .copyTo(FileHelp.getFile(legadoPath + File.separator + "bookshelf.json")) + .copyTo(FileHelp.getFile(legadoPath + File.separator + "bookshelf.json"), true) } } } \ No newline at end of file From ecc11a584788625376e8ea01d2d7b8148b6b8e1d Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 6 Jan 2020 22:08:14 +0800 Subject: [PATCH 08/17] up --- .../java/io/legado/app/ui/config/WebDavConfigFragment.kt | 8 ++++---- app/src/main/java/io/legado/app/ui/main/MainActivity.kt | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) 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 b269f7a95..446c93f68 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 @@ -34,10 +34,10 @@ import kotlin.coroutines.CoroutineContext class WebDavConfigFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeListener, CoroutineScope { - lateinit var job: Job - private val oldDataRequestCode = 23156 - private val backupSelectRequestCode = 4567489 - private val restoreSelectRequestCode = 654872 + private lateinit var job: Job + private val oldDataRequestCode = 11 + private val backupSelectRequestCode = 22 + private val restoreSelectRequestCode = 33 override val coroutineContext: CoroutineContext get() = job + Dispatchers.Main 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 80db372ca..30e72a0dd 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 @@ -39,8 +39,8 @@ import org.jetbrains.anko.toast class MainActivity : VMBaseActivity(R.layout.activity_main), BottomNavigationView.OnNavigationItemSelectedListener, ViewPager.OnPageChangeListener by ViewPager.SimpleOnPageChangeListener() { - private val backupSelectRequestCode = 4567489 - private val restoreSelectRequestCode = 654872 + private val backupSelectRequestCode = 11 + private val restoreSelectRequestCode = 22 override val viewModel: MainViewModel get() = getViewModel(MainViewModel::class.java) @@ -195,7 +195,7 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) startActivityForResult(intent, backupSelectRequestCode) - } catch (e: java.lang.Exception) { + } catch (e: Exception) { PermissionsCompat.Builder(this) .addPermissions(*Permissions.Group.STORAGE) .rationale(R.string.tip_perm_request_storage) From 92f0ce90df971e18e220dc67d3dc44b8f95f322b Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 6 Jan 2020 22:12:39 +0800 Subject: [PATCH 09/17] up --- app/src/main/assets/updateLog.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index 2d01b7c58..9312c54e0 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -4,6 +4,10 @@ * 先在旧版阅读(2.x)中进行备份,然后在新版阅读(3.x)【我的】->【备份与恢复】,选择【导入旧版本数据】,提示存储权限,选择允许即可导入成功。 * 注意:由于安卓10更改了权限策略,还需要给「允许安装其他应用」的权限才能导入源。MIUI11也需要此权限。 +**2020/01/06** +* 适配Android 10 权限 +* 备份恢复不再需要存储权限 + **2020/01/03** * 适配Android 10 权限 * 导入旧版本配置不在需要存储权限 From 70a3f8fc1fb5fe41330a2ccb55d9029fa4b67045 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 6 Jan 2020 22:16:26 +0800 Subject: [PATCH 10/17] up --- app/src/main/assets/updateLog.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index 9312c54e0..b9780bb44 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -1,8 +1,7 @@ ## 更新日志 * 旧版数据导入教程: -* 先在旧版阅读(2.x)中进行备份,然后在新版阅读(3.x)【我的】->【备份与恢复】,选择【导入旧版本数据】,提示存储权限,选择允许即可导入成功。 -* 注意:由于安卓10更改了权限策略,还需要给「允许安装其他应用」的权限才能导入源。MIUI11也需要此权限。 +* 先在旧版阅读(2.x)中进行备份,然后在新版阅读(3.x)【我的】->【备份与恢复】,选择【导入旧版本数据】。 **2020/01/06** * 适配Android 10 权限 From a6e3489e256eb76a81e4d27523dcd58519849a06 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 7 Jan 2020 07:58:28 +0800 Subject: [PATCH 11/17] up --- app/src/main/java/io/legado/app/help/storage/Backup.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 995ff8fdd..b6960c41e 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 @@ -107,7 +107,8 @@ object Backup { private fun copyBackup(context: Context, uri: Uri) { DocumentFile.fromTreeUri(context, uri)?.let { treeDoc -> for (fileName in backupFileNames) { - treeDoc.createFile("text/plain", fileName)?.let { doc -> + val doc = treeDoc.findFile(fileName) ?: treeDoc.createFile("text/plain", fileName) + doc?.let { DocumentUtils.writeText( context, FileHelp.getFile(backupPath + File.separator + fileName).readText(), From 74dc25b99f1697a7c0a713c00a7b08d61b3a30b1 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 7 Jan 2020 08:32:52 +0800 Subject: [PATCH 12/17] up --- app/src/main/java/io/legado/app/help/storage/Backup.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 b6960c41e..7f5b726ae 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 @@ -107,7 +107,7 @@ object Backup { private fun copyBackup(context: Context, uri: Uri) { DocumentFile.fromTreeUri(context, uri)?.let { treeDoc -> for (fileName in backupFileNames) { - val doc = treeDoc.findFile(fileName) ?: treeDoc.createFile("text/plain", fileName) + val doc = treeDoc.findFile(fileName) ?: treeDoc.createFile("", fileName) doc?.let { DocumentUtils.writeText( context, From 0e67bbb8a1bb04e5111cc1761d0d7c1b4743f39b Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 7 Jan 2020 08:36:39 +0800 Subject: [PATCH 13/17] up --- app/src/main/assets/updateLog.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index b9780bb44..e38e525d7 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -3,6 +3,9 @@ * 旧版数据导入教程: * 先在旧版阅读(2.x)中进行备份,然后在新版阅读(3.x)【我的】->【备份与恢复】,选择【导入旧版本数据】。 +**2020/01/07** +* 修复备份问题 + **2020/01/06** * 适配Android 10 权限 * 备份恢复不再需要存储权限 From a6275b5af91b073cd7ccaaef52734c33c591f95c Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 7 Jan 2020 08:53:39 +0800 Subject: [PATCH 14/17] up --- .../io/legado/app/ui/main/MainActivity.kt | 117 ---------------- .../io/legado/app/ui/main/my/MyFragment.kt | 126 +++++++++++++++++- 2 files changed, 119 insertions(+), 124 deletions(-) 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 30e72a0dd..39a5ad05f 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 @@ -1,12 +1,8 @@ package io.legado.app.ui.main -import android.app.Activity -import android.content.Intent -import android.net.Uri import android.os.Bundle import android.view.KeyEvent import android.view.MenuItem -import androidx.documentfile.provider.DocumentFile import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentStatePagerAdapter @@ -17,12 +13,6 @@ import io.legado.app.BuildConfig import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.Bus -import io.legado.app.constant.PreferKey -import io.legado.app.help.permission.Permissions -import io.legado.app.help.permission.PermissionsCompat -import io.legado.app.help.storage.Backup -import io.legado.app.help.storage.Restore -import io.legado.app.help.storage.WebDavHelp import io.legado.app.lib.theme.ATH import io.legado.app.service.BaseReadAloudService import io.legado.app.service.help.ReadAloud @@ -34,7 +24,6 @@ import io.legado.app.ui.main.rss.RssFragment import io.legado.app.utils.* import kotlinx.android.synthetic.main.activity_main.* import kotlinx.coroutines.launch -import org.jetbrains.anko.toast class MainActivity : VMBaseActivity(R.layout.activity_main), BottomNavigationView.OnNavigationItemSelectedListener, @@ -126,7 +115,6 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), override fun finish() { if (!BuildConfig.DEBUG) { launch { - backup() super.finish() } } else { @@ -153,111 +141,6 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), } } - fun backup() { - val backupPath = getPrefString(PreferKey.backupPath) - if (backupPath?.isNotEmpty() == true) { - val uri = Uri.parse(backupPath) - val doc = DocumentFile.fromTreeUri(this, uri) - if (doc?.canWrite() == true) { - launch { - Backup.backup(this@MainActivity, uri) - } - } else { - selectBackupFolder() - } - } else { - selectBackupFolder() - } - } - - fun restore() { - launch { - if (!WebDavHelp.showRestoreDialog(this@MainActivity)) { - val backupPath = getPrefString(PreferKey.backupPath) - if (backupPath?.isNotEmpty() == true) { - val uri = Uri.parse(backupPath) - val doc = DocumentFile.fromTreeUri(this@MainActivity, uri) - if (doc?.canWrite() == true) { - Restore.restore(this@MainActivity, uri) - toast(R.string.restore_success) - } else { - selectBackupFolder() - } - } else { - selectRestoreFolder() - } - } - } - } - - private fun selectBackupFolder() { - try { - val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - startActivityForResult(intent, backupSelectRequestCode) - } catch (e: Exception) { - PermissionsCompat.Builder(this) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { - launch { - Backup.backup(this@MainActivity, null) - } - } - .request() - } - } - - private fun selectRestoreFolder() { - try { - val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - startActivityForResult(intent, restoreSelectRequestCode) - } catch (e: java.lang.Exception) { - PermissionsCompat.Builder(this) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { - launch { - Restore.restore(Backup.legadoPath) - toast(R.string.restore_success) - } - } - .request() - } - } - - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - super.onActivityResult(requestCode, resultCode, data) - when (requestCode) { - backupSelectRequestCode -> if (resultCode == Activity.RESULT_OK) { - data?.data?.let { uri -> - contentResolver.takePersistableUriPermission( - uri, - Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION - ) - putPrefString(PreferKey.backupPath, uri.toString()) - launch { - Backup.backup(this@MainActivity, uri) - } - } - } - restoreSelectRequestCode -> if (resultCode == Activity.RESULT_OK) { - data?.data?.let { uri -> - contentResolver.takePersistableUriPermission( - uri, - Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION - ) - putPrefString(PreferKey.backupPath, uri.toString()) - launch { - Restore.restore(this@MainActivity, uri) - toast(R.string.restore_success) - } - } - } - } - } - private inner class TabFragmentPageAdapter internal constructor(fm: FragmentManager) : FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) { diff --git a/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt b/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt index 5b2a24114..6d79f55d4 100644 --- a/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt @@ -1,10 +1,14 @@ package io.legado.app.ui.main.my +import android.app.Activity +import android.content.Intent import android.content.SharedPreferences +import android.net.Uri import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.view.View +import androidx.documentfile.provider.DocumentFile import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import androidx.preference.SwitchPreference @@ -12,7 +16,13 @@ import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseFragment import io.legado.app.constant.Bus +import io.legado.app.constant.PreferKey import io.legado.app.help.BookHelp +import io.legado.app.help.permission.Permissions +import io.legado.app.help.permission.PermissionsCompat +import io.legado.app.help.storage.Backup +import io.legado.app.help.storage.Restore +import io.legado.app.help.storage.WebDavHelp import io.legado.app.lib.theme.ATH import io.legado.app.service.WebService import io.legado.app.ui.about.AboutActivity @@ -20,13 +30,15 @@ import io.legado.app.ui.about.DonateActivity import io.legado.app.ui.book.source.manage.BookSourceActivity import io.legado.app.ui.config.ConfigActivity import io.legado.app.ui.config.ConfigViewModel -import io.legado.app.ui.main.MainActivity import io.legado.app.ui.replacerule.ReplaceRuleActivity import io.legado.app.utils.* import kotlinx.android.synthetic.main.view_title_bar.* +import kotlinx.coroutines.launch import org.jetbrains.anko.startActivity class MyFragment : BaseFragment(R.layout.fragment_my_config) { + private val backupSelectRequestCode = 22 + private val restoreSelectRequestCode = 33 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { setSupportToolbar(toolbar) @@ -43,13 +55,113 @@ class MyFragment : BaseFragment(R.layout.fragment_my_config) { override fun onCompatOptionsItemSelected(item: MenuItem) { when (item.itemId) { R.id.menu_help -> startActivity() - R.id.menu_backup -> { - val activity = activity as? MainActivity - activity?.backup() + R.id.menu_backup -> backup() + R.id.menu_restore -> restore() + } + } + + + private fun backup() { + val backupPath = getPrefString(PreferKey.backupPath) + if (backupPath?.isNotEmpty() == true) { + val uri = Uri.parse(backupPath) + val doc = DocumentFile.fromTreeUri(requireContext(), uri) + if (doc?.canWrite() == true) { + launch { + Backup.backup(requireContext(), uri) + } + } else { + selectBackupFolder() + } + } else { + selectBackupFolder() + } + } + + private fun selectBackupFolder() { + try { + val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + startActivityForResult(intent, backupSelectRequestCode) + } catch (e: java.lang.Exception) { + PermissionsCompat.Builder(this) + .addPermissions(*Permissions.Group.STORAGE) + .rationale(R.string.tip_perm_request_storage) + .onGranted { + launch { + Backup.backup(requireContext(), null) + } + } + .request() + } + } + + fun restore() { + launch { + if (!WebDavHelp.showRestoreDialog(requireContext())) { + val backupPath = getPrefString(PreferKey.backupPath) + if (backupPath?.isNotEmpty() == true) { + val uri = Uri.parse(backupPath) + val doc = DocumentFile.fromTreeUri(requireContext(), uri) + if (doc?.canWrite() == true) { + Restore.restore(requireContext(), uri) + toast(R.string.restore_success) + } else { + selectBackupFolder() + } + } else { + selectRestoreFolder() + } } - R.id.menu_restore -> { - val activity = activity as? MainActivity - activity?.restore() + } + } + + private fun selectRestoreFolder() { + try { + val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + startActivityForResult(intent, restoreSelectRequestCode) + } catch (e: java.lang.Exception) { + PermissionsCompat.Builder(this) + .addPermissions(*Permissions.Group.STORAGE) + .rationale(R.string.tip_perm_request_storage) + .onGranted { + launch { + Restore.restore(Backup.legadoPath) + toast(R.string.restore_success) + } + } + .request() + } + } + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + when (requestCode) { + backupSelectRequestCode -> if (resultCode == Activity.RESULT_OK) { + data?.data?.let { uri -> + requireContext().contentResolver.takePersistableUriPermission( + uri, + Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION + ) + putPrefString(PreferKey.backupPath, uri.toString()) + launch { + Backup.backup(requireContext(), uri) + } + } + } + restoreSelectRequestCode -> if (resultCode == Activity.RESULT_OK) { + data?.data?.let { uri -> + requireContext().contentResolver.takePersistableUriPermission( + uri, + Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION + ) + putPrefString(PreferKey.backupPath, uri.toString()) + launch { + Restore.restore(requireContext(), uri) + toast(R.string.restore_success) + } + } } } } From 65bdc5541cd2686445ed5d4d0ec9fab6d1ff3325 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 7 Jan 2020 08:57:41 +0800 Subject: [PATCH 15/17] up --- .../java/io/legado/app/help/storage/Backup.kt | 10 +++++++--- .../io/legado/app/ui/main/MainActivity.kt | 20 +++++++++++++++++-- 2 files changed, 25 insertions(+), 5 deletions(-) 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 7f5b726ae..90bf3b9e2 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 @@ -120,9 +120,13 @@ object Backup { } private fun copyBackup() { - for (fileName in backupFileNames) { - FileHelp.getFile(backupPath + File.separator + "bookshelf.json") - .copyTo(FileHelp.getFile(legadoPath + File.separator + "bookshelf.json"), true) + try { + for (fileName in backupFileNames) { + FileHelp.getFile(backupPath + File.separator + "bookshelf.json") + .copyTo(FileHelp.getFile(legadoPath + File.separator + "bookshelf.json"), true) + } + } catch (e: Exception) { + e.printStackTrace() } } } \ No newline at end of file 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 39a5ad05f..571b32139 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 @@ -1,8 +1,10 @@ package io.legado.app.ui.main +import android.net.Uri import android.os.Bundle import android.view.KeyEvent import android.view.MenuItem +import androidx.documentfile.provider.DocumentFile import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentStatePagerAdapter @@ -13,6 +15,8 @@ import io.legado.app.BuildConfig import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.Bus +import io.legado.app.constant.PreferKey +import io.legado.app.help.storage.Backup import io.legado.app.lib.theme.ATH import io.legado.app.service.BaseReadAloudService import io.legado.app.service.help.ReadAloud @@ -28,8 +32,6 @@ import kotlinx.coroutines.launch class MainActivity : VMBaseActivity(R.layout.activity_main), BottomNavigationView.OnNavigationItemSelectedListener, ViewPager.OnPageChangeListener by ViewPager.SimpleOnPageChangeListener() { - private val backupSelectRequestCode = 11 - private val restoreSelectRequestCode = 22 override val viewModel: MainViewModel get() = getViewModel(MainViewModel::class.java) @@ -115,6 +117,7 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), override fun finish() { if (!BuildConfig.DEBUG) { launch { + backup() super.finish() } } else { @@ -122,6 +125,19 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), } } + private suspend fun backup() { + val backupPath = getPrefString(PreferKey.backupPath) + if (backupPath?.isNotEmpty() == true) { + val uri = Uri.parse(backupPath) + val doc = DocumentFile.fromTreeUri(this, uri) + if (doc?.canWrite() == true) { + Backup.backup(this@MainActivity, uri) + } + } else { + Backup.backup(this@MainActivity, null) + } + } + override fun onDestroy() { super.onDestroy() ReadAloud.stop(this) From fc1d36d1c8655c6ad5812420c8cdb00ec45ae635 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 7 Jan 2020 09:20:30 +0800 Subject: [PATCH 16/17] up --- .../io/legado/app/ui/book/read/page/ContentView.kt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt index 903cc3abc..f5d329356 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt @@ -17,6 +17,7 @@ import io.legado.app.utils.* import kotlinx.android.synthetic.main.view_book_page.view.* import org.jetbrains.anko.matchParent import org.jetbrains.anko.sdk27.listeners.onScrollChange +import java.io.File import java.util.* @@ -83,10 +84,13 @@ class ContentView : FrameLayout { } context.getPrefString(PreferKey.readBookFont)?.let { if (it.isNotEmpty()) { - content_text_view.typeface = Typeface.createFromFile(it) - } else { - content_text_view.typeface = Typeface.DEFAULT + val file = File(it) + if (file.exists()) { + content_text_view.typeface = Typeface.createFromFile(it) + return@let + } } + content_text_view.typeface = Typeface.DEFAULT } } From 3a9d9e33e28e0202d5fa6ecf98a84476ebad7f3b Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 7 Jan 2020 09:24:17 +0800 Subject: [PATCH 17/17] up --- .../main/java/io/legado/app/ui/book/read/page/ContentView.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt index f5d329356..cc9e9e666 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt @@ -88,6 +88,8 @@ class ContentView : FrameLayout { if (file.exists()) { content_text_view.typeface = Typeface.createFromFile(it) return@let + } else { + context.putPrefString(PreferKey.readBookFont, "") } } content_text_view.typeface = Typeface.DEFAULT