From 390e64ec72f204f0d4ac7a8149cd8fb53c6912aa Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 17 Sep 2019 20:34:57 +0800 Subject: [PATCH] up --- .../java/io/legado/app/help/storage/Backup.kt | 27 ++++++++--- .../io/legado/app/help/storage/Preferences.kt | 48 +++++++++++++++++++ .../io/legado/app/help/storage/Restore.kt | 16 ++++++- 3 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/io/legado/app/help/storage/Preferences.kt 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 56bae33eb..14faab468 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 @@ -6,11 +6,13 @@ import io.legado.app.R import io.legado.app.help.FileHelp import io.legado.app.utils.FileUtils import io.legado.app.utils.GSON +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 Backup { val defaultPath by lazy { @@ -24,6 +26,7 @@ object Backup { backupBookSource(path) backupRssSource(path) backupReplaceRule(path) + backupPreference(path) WebDavHelp.backUpWebDav() uiThread { 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) { val json = GSON.toJson(App.db.bookDao().allBooks) val file = FileHelp.getFile(path + File.separator + "bookshelf.json") @@ -71,4 +68,22 @@ object Backup { val file = FileHelp.getFile(path + File.separator + "replaceRule.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() + } + } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/storage/Preferences.kt b/app/src/main/java/io/legado/app/help/storage/Preferences.kt new file mode 100644 index 000000000..11907e83f --- /dev/null +++ b/app/src/main/java/io/legado/app/help/storage/Preferences.kt @@ -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 + } +} \ No newline at end of file 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 94b24cb00..52ead5de6 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 @@ -14,6 +14,7 @@ import io.legado.app.data.entities.RssSource import io.legado.app.help.FileHelp import io.legado.app.help.storage.Backup.defaultPath import io.legado.app.utils.* +import org.jetbrains.anko.defaultSharedPreferences import org.jetbrains.anko.doAsync import org.jetbrains.anko.toast import org.jetbrains.anko.uiThread @@ -64,6 +65,18 @@ object Restore { } catch (e: Exception) { 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("恢复完成") } } } @@ -91,7 +104,8 @@ object Restore { book.origin = jsonItem.readString("$.tag") ?: "" book.originName = jsonItem.readString("$.bookInfoBean.origin") ?: "" 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.coverUrl = jsonItem.readString("$.bookInfoBean.coverUrl") book.customCoverUrl = jsonItem.readString("$.customCoverPath")