From 819a359745975bc2334fb488ea87f4e653711409 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 15 Sep 2019 22:41:07 +0800 Subject: [PATCH] up --- .../java/io/legado/app/help/storage/Backup.kt | 48 +++++------ .../io/legado/app/help/storage/Restore.kt | 82 ++++++++----------- .../io/legado/app/ui/main/my/MyFragment.kt | 14 ++++ app/src/main/res/drawable/ic_backup.xml | 30 ++----- app/src/main/res/drawable/ic_restore.xml | 27 ++---- app/src/main/res/menu/my_config.xml | 12 +++ 6 files changed, 94 insertions(+), 119 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 ac381ec52..6fd1f079b 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 @@ -4,42 +4,38 @@ import androidx.appcompat.app.AppCompatActivity import io.legado.app.App import io.legado.app.R import io.legado.app.help.FileHelp -import io.legado.app.help.permission.Permissions -import io.legado.app.help.permission.PermissionsCompat import io.legado.app.utils.FileUtils import io.legado.app.utils.GSON +import org.jetbrains.anko.doAsync +import org.jetbrains.anko.toast +import org.jetbrains.anko.uiThread import java.io.File object Backup { - fun backup(activity: AppCompatActivity) { - PermissionsCompat.Builder(activity) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { - val path = - FileUtils.getSdCardPath() + File.separator + "YueDu" + File.separator + "legadoBackUp" - backupBookshelf(path) - backupBookSource(path) - backupRssSource(path) - backupReplaceRule(path) + fun backup() { + doAsync { + val path = + FileUtils.getSdCardPath() + File.separator + "YueDu" + File.separator + "legadoBackUp" + backupBookshelf(path) + backupBookSource(path) + backupRssSource(path) + backupReplaceRule(path) + uiThread { + App.INSTANCE.toast(R.string.backup_success) } - .request() + } } fun autoBackup(activity: AppCompatActivity) { - PermissionsCompat.Builder(activity) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { - val path = - FileUtils.getSdCardPath() + File.separator + "YueDu" + File.separator + "legadoBackUp" - backupBookshelf(path) - backupBookSource(path) - backupRssSource(path) - backupReplaceRule(path) - } - .request() + doAsync { + val path = + FileUtils.getSdCardPath() + File.separator + "YueDu" + File.separator + "legadoBackUp" + backupBookshelf(path) + backupBookSource(path) + backupRssSource(path) + backupReplaceRule(path) + } } private fun backupBookshelf(path: String) { 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 2caae0a40..10bc36bf7 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 @@ -2,20 +2,16 @@ package io.legado.app.help.storage import android.content.Context import android.util.Log -import androidx.appcompat.app.AppCompatActivity import com.jayway.jsonpath.Configuration import com.jayway.jsonpath.JsonPath import com.jayway.jsonpath.Option import io.legado.app.App -import io.legado.app.R import io.legado.app.constant.AppConst import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.ReplaceRule import io.legado.app.data.entities.RssSource import io.legado.app.help.FileHelp -import io.legado.app.help.permission.Permissions -import io.legado.app.help.permission.PermissionsCompat import io.legado.app.utils.* import org.jetbrains.anko.doAsync import org.jetbrains.anko.toast @@ -29,54 +25,48 @@ object Restore { .build() ) - fun restore(activity: AppCompatActivity) { - PermissionsCompat.Builder(activity) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { - val path = - FileUtils.getSdCardPath() + File.separator + "YueDu" + File.separator + "legadoBackUp" - restore(path) - } - .request() + private val defaultPath by lazy { + FileUtils.getSdCardPath() + File.separator + "YueDu" + File.separator + "legadoBackUp" } - fun restore(path: String) { - try { - val file = FileHelp.getFile(path + File.separator + "bookshelf.json") - val json = file.readText() - GSON.fromJsonArray(json)?.let { - App.db.bookDao().insert(*it.toTypedArray()) + fun restore(path: String = defaultPath) { + doAsync { + try { + val file = FileHelp.getFile(path + File.separator + "bookshelf.json") + val json = file.readText() + GSON.fromJsonArray(json)?.let { + App.db.bookDao().insert(*it.toTypedArray()) + } + } catch (e: Exception) { + e.printStackTrace() } - } catch (e: Exception) { - e.printStackTrace() - } - try { - val file = FileHelp.getFile(path + File.separator + "bookSource.json") - val json = file.readText() - GSON.fromJsonArray(json)?.let { - App.db.bookSourceDao().insert(*it.toTypedArray()) + try { + val file = FileHelp.getFile(path + File.separator + "bookSource.json") + val json = file.readText() + GSON.fromJsonArray(json)?.let { + App.db.bookSourceDao().insert(*it.toTypedArray()) + } + } catch (e: Exception) { + e.printStackTrace() } - } catch (e: Exception) { - e.printStackTrace() - } - try { - val file = FileHelp.getFile(path + File.separator + "rssSource.json") - val json = file.readText() - GSON.fromJsonArray(json)?.let { - App.db.rssSourceDao().insert(*it.toTypedArray()) + try { + val file = FileHelp.getFile(path + File.separator + "rssSource.json") + val json = file.readText() + GSON.fromJsonArray(json)?.let { + App.db.rssSourceDao().insert(*it.toTypedArray()) + } + } catch (e: Exception) { + e.printStackTrace() } - } catch (e: Exception) { - e.printStackTrace() - } - try { - val file = FileHelp.getFile(path + File.separator + "replaceRule.json") - val json = file.readText() - GSON.fromJsonArray(json)?.let { - App.db.replaceRuleDao().insert(*it.toTypedArray()) + try { + val file = FileHelp.getFile(path + File.separator + "replaceRule.json") + val json = file.readText() + GSON.fromJsonArray(json)?.let { + App.db.replaceRuleDao().insert(*it.toTypedArray()) + } + } catch (e: Exception) { + e.printStackTrace() } - } catch (e: Exception) { - e.printStackTrace() } } 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 333fd9689..b98a655b0 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 @@ -10,6 +10,10 @@ import androidx.preference.PreferenceFragmentCompat import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseFragment +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.lib.theme.ATH import io.legado.app.ui.about.AboutActivity import io.legado.app.ui.about.DonateActivity @@ -38,6 +42,16 @@ 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 -> PermissionsCompat.Builder(this) + .addPermissions(*Permissions.Group.STORAGE) + .rationale(R.string.tip_perm_request_storage) + .onGranted { Backup.backup() } + .request() + R.id.menu_restore -> PermissionsCompat.Builder(this) + .addPermissions(*Permissions.Group.STORAGE) + .rationale(R.string.tip_perm_request_storage) + .onGranted { Restore.restore() } + .request() } } diff --git a/app/src/main/res/drawable/ic_backup.xml b/app/src/main/res/drawable/ic_backup.xml index fe8737deb..200bb7081 100644 --- a/app/src/main/res/drawable/ic_backup.xml +++ b/app/src/main/res/drawable/ic_backup.xml @@ -1,29 +1,9 @@ - - + android:viewportWidth="24.0" + android:viewportHeight="24.0"> - - - - - - - \ No newline at end of file + android:fillColor="#FF000000" + android:pathData="M19.35,10.04C18.67,6.59 15.64,4 12,4 9.11,4 6.6,5.64 5.35,8.04 2.34,8.36 0,10.91 0,14c0,3.31 2.69,6 6,6h13c2.76,0 5,-2.24 5,-5 0,-2.64 -2.05,-4.78 -4.65,-4.96zM14,13v4h-4v-4H7l5,-5 5,5h-3z" /> + diff --git a/app/src/main/res/drawable/ic_restore.xml b/app/src/main/res/drawable/ic_restore.xml index 75e5490f6..d9f75ea6d 100644 --- a/app/src/main/res/drawable/ic_restore.xml +++ b/app/src/main/res/drawable/ic_restore.xml @@ -1,26 +1,9 @@ - - + android:viewportWidth="24.0" + android:viewportHeight="24.0"> - - - - - - \ No newline at end of file + android:fillColor="#FF000000" + android:pathData="M13,3c-4.97,0 -9,4.03 -9,9L1,12l3.89,3.89 0.07,0.14L9,12L6,12c0,-3.87 3.13,-7 7,-7s7,3.13 7,7 -3.13,7 -7,7c-1.93,0 -3.68,-0.79 -4.94,-2.06l-1.42,1.42C8.27,19.99 10.51,21 13,21c4.97,0 9,-4.03 9,-9s-4.03,-9 -9,-9zM12,8v5l4.28,2.54 0.72,-1.21 -3.5,-2.08L13.5,8L12,8z" /> + diff --git a/app/src/main/res/menu/my_config.xml b/app/src/main/res/menu/my_config.xml index e54b68733..e2cceece0 100644 --- a/app/src/main/res/menu/my_config.xml +++ b/app/src/main/res/menu/my_config.xml @@ -8,4 +8,16 @@ android:icon="@drawable/ic_live_help_black_24dp" app:showAsAction="always" /> + + + +