pull/34/head
kunfei 5 years ago
parent 446e72e856
commit 390e64ec72
  1. 27
      app/src/main/java/io/legado/app/help/storage/Backup.kt
  2. 48
      app/src/main/java/io/legado/app/help/storage/Preferences.kt
  3. 16
      app/src/main/java/io/legado/app/help/storage/Restore.kt

@ -6,11 +6,13 @@ import io.legado.app.R
import io.legado.app.help.FileHelp import io.legado.app.help.FileHelp
import io.legado.app.utils.FileUtils import io.legado.app.utils.FileUtils
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import org.jetbrains.anko.defaultSharedPreferences
import org.jetbrains.anko.doAsync import org.jetbrains.anko.doAsync
import org.jetbrains.anko.toast import org.jetbrains.anko.toast
import org.jetbrains.anko.uiThread import org.jetbrains.anko.uiThread
import java.io.File import java.io.File
object Backup { object Backup {
val defaultPath by lazy { val defaultPath by lazy {
@ -24,6 +26,7 @@ object Backup {
backupBookSource(path) backupBookSource(path)
backupRssSource(path) backupRssSource(path)
backupReplaceRule(path) backupReplaceRule(path)
backupPreference(path)
WebDavHelp.backUpWebDav() WebDavHelp.backUpWebDav()
uiThread { uiThread {
App.INSTANCE.toast(R.string.backup_success) App.INSTANCE.toast(R.string.backup_success)
@ -42,12 +45,6 @@ object Backup {
} }
} }
private fun backupPreference(path: String) {
val file = FileHelp.getFile(path + File.separator + "config.xml")
}
private fun backupBookshelf(path: String) { private fun backupBookshelf(path: String) {
val json = GSON.toJson(App.db.bookDao().allBooks) val json = GSON.toJson(App.db.bookDao().allBooks)
val file = FileHelp.getFile(path + File.separator + "bookshelf.json") val file = FileHelp.getFile(path + File.separator + "bookshelf.json")
@ -71,4 +68,22 @@ object Backup {
val file = FileHelp.getFile(path + File.separator + "replaceRule.json") val file = FileHelp.getFile(path + File.separator + "replaceRule.json")
file.writeText(json) file.writeText(json)
} }
private fun backupPreference(path: String) {
Preferences.getSharedPreferences(App.INSTANCE, path, "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()
}
}
} }

@ -0,0 +1,48 @@
package io.legado.app.help.storage
import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.content.SharedPreferences
import java.io.File
object Preferences {
/**
* 用反射生成 SharedPreferences
* @param context
* @param dir
* @param fileName 文件名,不需要 '.xml' 后缀
* @return
*/
fun getSharedPreferences(
context: Context,
dir: String,
fileName: String
): SharedPreferences? {
try {
// 获取 ContextWrapper对象中的mBase变量。该变量保存了 ContextImpl 对象
val fieldMBase = ContextWrapper::class.java.getDeclaredField("mBase")
fieldMBase.isAccessible = true
// 获取 mBase变量
val objMBase = fieldMBase.get(context)
// 获取 ContextImpl。mPreferencesDir变量,该变量保存了数据文件的保存路径
val fieldMPreferencesDir = objMBase.javaClass.getDeclaredField("mPreferencesDir")
fieldMPreferencesDir.isAccessible = true
// 创建自定义路径
// String FILE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()+"/Android";
val file = File(dir)
// 修改mPreferencesDir变量的值
fieldMPreferencesDir.set(objMBase, file)
// 返回修改路径以后的 SharedPreferences :%FILE_PATH%/%fileName%.xml
return context.getSharedPreferences(fileName, Activity.MODE_PRIVATE)
} catch (e: NoSuchFieldException) {
e.printStackTrace()
} catch (e: IllegalArgumentException) {
e.printStackTrace()
} catch (e: IllegalAccessException) {
e.printStackTrace()
}
return null
}
}

@ -14,6 +14,7 @@ import io.legado.app.data.entities.RssSource
import io.legado.app.help.FileHelp import io.legado.app.help.FileHelp
import io.legado.app.help.storage.Backup.defaultPath import io.legado.app.help.storage.Backup.defaultPath
import io.legado.app.utils.* import io.legado.app.utils.*
import org.jetbrains.anko.defaultSharedPreferences
import org.jetbrains.anko.doAsync import org.jetbrains.anko.doAsync
import org.jetbrains.anko.toast import org.jetbrains.anko.toast
import org.jetbrains.anko.uiThread import org.jetbrains.anko.uiThread
@ -64,6 +65,18 @@ object Restore {
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
} }
Preferences.getSharedPreferences(App.INSTANCE, path, "config")?.all?.map {
val edit = App.INSTANCE.defaultSharedPreferences.edit()
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()
}
uiThread { App.INSTANCE.toast("恢复完成") } uiThread { App.INSTANCE.toast("恢复完成") }
} }
} }
@ -91,7 +104,8 @@ object Restore {
book.origin = jsonItem.readString("$.tag") ?: "" book.origin = jsonItem.readString("$.tag") ?: ""
book.originName = jsonItem.readString("$.bookInfoBean.origin") ?: "" book.originName = jsonItem.readString("$.bookInfoBean.origin") ?: ""
book.author = jsonItem.readString("$.bookInfoBean.author") ?: "" book.author = jsonItem.readString("$.bookInfoBean.author") ?: ""
book.type = if (jsonItem.readString("$.bookInfoBean.bookSourceType") == "AUDIO") 1 else 0 book.type =
if (jsonItem.readString("$.bookInfoBean.bookSourceType") == "AUDIO") 1 else 0
book.tocUrl = jsonItem.readString("$.bookInfoBean.chapterUrl") ?: book.bookUrl book.tocUrl = jsonItem.readString("$.bookInfoBean.chapterUrl") ?: book.bookUrl
book.coverUrl = jsonItem.readString("$.bookInfoBean.coverUrl") book.coverUrl = jsonItem.readString("$.bookInfoBean.coverUrl")
book.customCoverUrl = jsonItem.readString("$.customCoverPath") book.customCoverUrl = jsonItem.readString("$.customCoverPath")

Loading…
Cancel
Save