优化
This commit is contained in:
@@ -21,7 +21,9 @@ import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
|
||||
/**
|
||||
* 备份
|
||||
*/
|
||||
object Backup {
|
||||
|
||||
val backupPath: String by lazy {
|
||||
@@ -102,7 +104,7 @@ object Backup {
|
||||
.writeText(GSON.toJson(it))
|
||||
}
|
||||
ensureActive()
|
||||
Preferences.getSharedPreferences(appCtx, backupPath, "config")?.let { sp ->
|
||||
appCtx.getSharedPreferences(backupPath, "config")?.let { sp ->
|
||||
val edit = sp.edit()
|
||||
appCtx.defaultSharedPreferences.all.forEach { (key, value) ->
|
||||
if (BackupConfig.keyIsNotIgnore(key)) {
|
||||
|
||||
@@ -7,6 +7,9 @@ import io.legado.app.utils.GSON
|
||||
import io.legado.app.utils.fromJsonObject
|
||||
import splitties.init.appCtx
|
||||
|
||||
/**
|
||||
* 备份配置
|
||||
*/
|
||||
object BackupConfig {
|
||||
|
||||
private val ignoreConfigPath = FileUtils.getPath(appCtx.filesDir, "restoreIgnore.json")
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
package io.legado.app.help.storage
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.ContextWrapper
|
||||
import android.content.SharedPreferences
|
||||
import io.legado.app.utils.printOnDebug
|
||||
|
||||
|
||||
import java.io.File
|
||||
|
||||
object Preferences {
|
||||
|
||||
/**
|
||||
* 用反射生成 SharedPreferences
|
||||
* @param context
|
||||
* @param dir
|
||||
* @param fileName 文件名,不需要 '.xml' 后缀
|
||||
* @return
|
||||
*/
|
||||
@SuppressLint("DiscouragedPrivateApi")
|
||||
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
|
||||
// 创建自定义路径
|
||||
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.printOnDebug()
|
||||
} catch (e: IllegalArgumentException) {
|
||||
e.printOnDebug()
|
||||
} catch (e: IllegalAccessException) {
|
||||
e.printOnDebug()
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,9 @@ import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
|
||||
|
||||
/**
|
||||
* 恢复
|
||||
*/
|
||||
object Restore {
|
||||
|
||||
suspend fun restore(context: Context, path: String) {
|
||||
@@ -165,7 +167,7 @@ object Restore {
|
||||
AppLog.put("恢复阅读界面出错\n${e.localizedMessage}", e)
|
||||
}
|
||||
}
|
||||
Preferences.getSharedPreferences(appCtx, path, "config")?.all?.let { map ->
|
||||
appCtx.getSharedPreferences(path, "config")?.all?.let { map ->
|
||||
val edit = appCtx.defaultSharedPreferences.edit()
|
||||
map.forEach { (key, value) ->
|
||||
if (BackupConfig.keyIsNotIgnore(key)) {
|
||||
|
||||
@@ -2,9 +2,49 @@
|
||||
|
||||
package io.legado.app.utils
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.ContextWrapper
|
||||
import android.content.SharedPreferences
|
||||
import androidx.core.content.edit
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* 获取自定义路径的SharedPreferences, 用反射生成 SharedPreferences
|
||||
* @param dir 目录路径
|
||||
* @param fileName 文件名,不需要 '.xml' 后缀
|
||||
* @return SharedPreferences
|
||||
*/
|
||||
@SuppressLint("DiscouragedPrivateApi")
|
||||
fun Context.getSharedPreferences(
|
||||
dir: String,
|
||||
fileName: String
|
||||
): SharedPreferences? {
|
||||
try {
|
||||
// 获取 ContextWrapper对象中的mBase变量。该变量保存了 ContextImpl 对象
|
||||
val fieldMBase = ContextWrapper::class.java.getDeclaredField("mBase")
|
||||
fieldMBase.isAccessible = true
|
||||
// 获取 mBase变量
|
||||
val objMBase = fieldMBase.get(this)
|
||||
// 获取 ContextImpl。mPreferencesDir变量,该变量保存了数据文件的保存路径
|
||||
val fieldMPreferencesDir = objMBase.javaClass.getDeclaredField("mPreferencesDir")
|
||||
fieldMPreferencesDir.isAccessible = true
|
||||
// 创建自定义路径
|
||||
val file = File(dir)
|
||||
// 修改mPreferencesDir变量的值
|
||||
fieldMPreferencesDir.set(objMBase, file)
|
||||
// 返回修改路径以后的 SharedPreferences :%FILE_PATH%/%fileName%.xml
|
||||
return getSharedPreferences(fileName, Activity.MODE_PRIVATE)
|
||||
} catch (e: NoSuchFieldException) {
|
||||
e.printOnDebug()
|
||||
} catch (e: IllegalArgumentException) {
|
||||
e.printOnDebug()
|
||||
} catch (e: IllegalAccessException) {
|
||||
e.printOnDebug()
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fun SharedPreferences.getString(key: String): String? {
|
||||
return getString(key, null)
|
||||
|
||||
Reference in New Issue
Block a user