pull/34/head
kunfei 5 years ago
parent 4b3d4dc84b
commit 819a359745
  1. 48
      app/src/main/java/io/legado/app/help/storage/Backup.kt
  2. 82
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  3. 14
      app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt
  4. 28
      app/src/main/res/drawable/ic_backup.xml
  5. 25
      app/src/main/res/drawable/ic_restore.xml
  6. 12
      app/src/main/res/menu/my_config.xml

@ -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) {

@ -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<Book>(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<Book>(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<BookSource>(json)?.let {
App.db.bookSourceDao().insert(*it.toTypedArray())
try {
val file = FileHelp.getFile(path + File.separator + "bookSource.json")
val json = file.readText()
GSON.fromJsonArray<BookSource>(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<RssSource>(json)?.let {
App.db.rssSourceDao().insert(*it.toTypedArray())
try {
val file = FileHelp.getFile(path + File.separator + "rssSource.json")
val json = file.readText()
GSON.fromJsonArray<RssSource>(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<ReplaceRule>(json)?.let {
App.db.replaceRuleDao().insert(*it.toTypedArray())
try {
val file = FileHelp.getFile(path + File.separator + "replaceRule.json")
val json = file.readText()
GSON.fromJsonArray<ReplaceRule>(json)?.let {
App.db.replaceRuleDao().insert(*it.toTypedArray())
}
} catch (e: Exception) {
e.printStackTrace()
}
} catch (e: Exception) {
e.printStackTrace()
}
}

@ -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<AboutActivity>()
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()
}
}

@ -1,29 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#595757"
android:pathData="M15.923,11.848 C13.675,11.848,11.846,13.678,11.846,15.924 C11.846,18.172,13.675,20,15.923,20 S20,18.172,20,15.924 C20,13.678,18.171,11.848,15.923,11.848 Z M15.923,18.713 C14.385,18.713,13.133,17.461,13.133,15.924 C13.133,14.385,14.385,13.135,15.923,13.135 C17.461,13.135,18.713,14.385,18.713,15.924 C18.713,17.461,17.461,18.713,15.923,18.713 Z" />
<path
android:fillColor="#595757"
android:pathData="M16.567,13.961 L15.279,13.961 L15.279,16.568 L17.886,16.568 L17.886,15.279 L16.567,15.279 Z" />
<path
android:fillColor="#595757"
android:pathData="M5.287,5.623 C5.287,5.439,5.439,5.289,5.625,5.289 L16.412,5.289 C16.599,5.289,16.751,5.439,16.751,5.623 L16.751,11.019 L18.038,11.019 L18.038,5.623 C18.038,4.729,17.309,4,16.412,4 L5.625,4 C4.73,4,4,4.729,4,5.623 L4,18.375 C4,19.273,4.73,20,5.625,20 L11.018,20 L11.018,18.713 L5.625,18.713 C5.439,18.713,5.287,18.561,5.287,18.375 L5.287,18.039 L11.018,18.039 L11.018,16.752 L5.287,16.752 L5.287,5.623 Z" />
<path
android:fillColor="#595757"
android:pathData="M5.96,6.115 L7.247,6.115 L7.247,7.095 L5.96,7.095 L5.96,6.115 Z" />
<path
android:fillColor="#595757"
android:pathData="M14.788,6.115 L16.075,6.115 L16.075,7.095 L14.788,7.095 L14.788,6.115 Z" />
<path
android:fillColor="#595757"
android:pathData="M5.96,14.943 L7.247,14.943 L7.247,15.923 L5.96,15.923 L5.96,14.943 Z" />
<path
android:fillColor="#595757"
android:pathData="M11.474,11.475 L13.387,9.561 C13.65,9.987,13.809,10.483,13.809,11.02 L15.096,11.02 C15.096,10.127,14.799,9.308,14.311,8.636 L14.908,8.039 L13.997,7.131 L13.4,7.727 C12.728,7.24,11.909,6.944,11.018,6.944 C8.77,6.944,6.941,8.772,6.941,11.02 S8.77,15.096,11.018,15.096 L11.018,13.807 C9.48,13.807,8.228,12.557,8.228,11.02 C8.228,9.479,9.479,8.229,11.018,8.229 C11.554,8.229,12.05,8.388,12.476,8.651 L10.563,10.563 L11.474,11.475 Z" />
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" />
</vector>

@ -1,26 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#595757"
android:pathData="M18.807,13.043 C18.035,12.273,17.011,11.848,15.924,11.848 C14.876,11.848,13.893,12.25,13.134,12.966 L13.134,12.281 L11.847,12.281 L11.847,15.095 L12.49,15.095 L13.135,15.095 L14.908,15.095 L14.908,13.806 L14.128,13.806 C14.632,13.378,15.256,13.132,15.924,13.132 C16.668,13.132,17.369,13.425,17.896,13.952 C18.423,14.477,18.713,15.179,18.713,15.925 C18.713,16.667,18.423,17.366,17.896,17.896 C17.37,18.421,16.668,18.71,15.924,18.71 C15.178,18.71,14.479,18.421,13.949,17.896 C13.425,17.367,13.135,16.669,13.135,15.925 L11.848,15.925 C11.848,17.013,12.27,18.038,13.041,18.804 C13.81,19.576,14.834,20,15.924,20 C17.011,20,18.035,19.576,18.807,18.805 C19.576,18.037,20,17.012,20,15.926 C20,14.836,19.576,13.813,18.807,13.043 Z" />
<path
android:fillColor="#595757"
android:pathData="M5.287,5.625 C5.287,5.437,5.439,5.287,5.625,5.287 L16.412,5.287 C16.596,5.287,16.751,5.441,16.751,5.625 L16.751,11.02 L18.038,11.02 L18.038,5.625 C18.038,4.727,17.308,3.998,16.412,3.998 L5.625,3.998 C4.73,3.998,4,4.727,4,5.625 L4,18.377 C4,19.273,4.73,20.002,5.625,20.002 L11.018,20.002 L11.018,18.713 L5.625,18.713 C5.439,18.713,5.287,18.563,5.287,18.377 L5.287,18.039 L11.018,18.039 L11.018,16.754 L5.287,16.754 L5.287,5.625 Z" />
<path
android:fillColor="#595757"
android:pathData="M5.96,6.117 L7.247,6.117 L7.247,7.097 L5.96,7.097 L5.96,6.117 Z" />
<path
android:fillColor="#595757"
android:pathData="M14.789,6.117 L16.076,6.117 L16.076,7.097 L14.789,7.097 L14.789,6.117 Z" />
<path
android:fillColor="#595757"
android:pathData="M5.96,14.945 L7.247,14.945 L7.247,15.925 L5.96,15.925 L5.96,14.945 Z" />
<path
android:fillColor="#595757"
android:pathData="M11.473,11.475 L13.387,9.562 C13.65,9.988,13.809,10.484,13.809,11.02 L15.097,11.02 C15.097,10.128,14.801,9.309,14.312,8.637 L14.908,8.041 L13.996,7.133 L13.401,7.728 C12.729,7.24,11.91,6.944,11.018,6.944 C8.769,6.944,6.941,8.774,6.941,11.02 C6.941,13.268,8.77,15.096,11.018,15.096 L11.018,13.807 C9.48,13.807,8.228,12.557,8.228,11.02 C8.228,9.481,9.48,8.229,11.018,8.229 C11.554,8.229,12.051,8.388,12.476,8.652 L10.562,10.565 L11.473,11.475 Z" />
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" />
</vector>

@ -8,4 +8,16 @@
android:icon="@drawable/ic_live_help_black_24dp"
app:showAsAction="always" />
<item
android:id="@+id/menu_backup"
android:title="@string/help"
android:icon="@drawable/ic_backup"
app:showAsAction="always" />
<item
android:id="@+id/menu_restore"
android:title="@string/help"
android:icon="@drawable/ic_restore"
app:showAsAction="always" />
</menu>

Loading…
Cancel
Save