Modify optimize code style

This commit is contained in:
2022-08-17 23:06:23 +08:00
parent 9028915274
commit 9abb3433d4

View File

@@ -124,7 +124,7 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
private var isUsingKeyValueCache = YukiHookAPI.Configs.isEnableModulePrefsCache private var isUsingKeyValueCache = YukiHookAPI.Configs.isEnableModulePrefsCache
/** 是否使用新版存储方式 EdXposed/LSPosed */ /** 是否使用新版存储方式 EdXposed/LSPosed */
private var isUsingNewXSharePrefs = false private var isUsingNewXSharedPreferences = false
/** 检查 API 装载状态 */ /** 检查 API 装载状态 */
private fun checkApi() { private fun checkApi() {
@@ -137,7 +137,7 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
* 获得 [XSharedPreferences] 对象 * 获得 [XSharedPreferences] 对象
* @return [XSharedPreferences] * @return [XSharedPreferences]
*/ */
private val xPref private val xPrefs
get() = runCatching { get() = runCatching {
XSharedPreferences(YukiHookBridge.modulePackageName, prefsName).apply { XSharedPreferences(YukiHookBridge.modulePackageName, prefsName).apply {
checkApi() checkApi()
@@ -151,20 +151,20 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
* 获得 [SharedPreferences] 对象 * 获得 [SharedPreferences] 对象
* @return [SharedPreferences] * @return [SharedPreferences]
*/ */
private val sPref private val sPrefs
get() = try { get() = try {
checkApi() checkApi()
context?.getSharedPreferences(prefsName, Context.MODE_WORLD_READABLE).also { isUsingNewXSharePrefs = true } context?.getSharedPreferences(prefsName, Context.MODE_WORLD_READABLE).also { isUsingNewXSharedPreferences = true }
?: error("If you want to use module prefs, you must set the context instance first") ?: error("If you want to use module prefs, you must set the context instance first")
} catch (_: Throwable) { } catch (_: Throwable) {
checkApi() checkApi()
context?.getSharedPreferences(prefsName, Context.MODE_PRIVATE).also { isUsingNewXSharePrefs = false } context?.getSharedPreferences(prefsName, Context.MODE_PRIVATE).also { isUsingNewXSharedPreferences = false }
?: error("If you want to use module prefs, you must set the context instance first") ?: error("If you want to use module prefs, you must set the context instance first")
} }
/** 设置全局可读可写 */ /** 设置全局可读可写 */
private fun makeWorldReadable() = runCatching { private fun makeWorldReadable() = runCatching {
if (isUsingNewXSharePrefs.not()) makeWorldReadable(context, prefsFileName = "${prefsName}.xml") if (isUsingNewXSharedPreferences.not()) makeWorldReadable(context, prefsFileName = "${prefsName}.xml")
} }
/** /**
@@ -174,7 +174,7 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
* @return [Boolean] 是否可读 * @return [Boolean] 是否可读
*/ */
val isXSharePrefsReadable val isXSharePrefsReadable
get() = if (isXposedEnvironment) (runCatching { xPref.let { it.file.exists() && it.file.canRead() } }.getOrNull() ?: false) else false get() = if (isXposedEnvironment) (runCatching { xPrefs.let { it.file.exists() && it.file.canRead() } }.getOrNull() ?: false) else false
/** /**
* 获取 [YukiHookModulePrefs] 是否正处于 EdXposed/LSPosed 的最高权限运行 * 获取 [YukiHookModulePrefs] 是否正处于 EdXposed/LSPosed 的最高权限运行
@@ -188,8 +188,8 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
get() = if (isXposedEnvironment.not()) get() = if (isXposedEnvironment.not())
runCatching { runCatching {
/** 执行一次装载 */ /** 执行一次装载 */
sPref.edit() sPrefs.edit()
isUsingNewXSharePrefs isUsingNewXSharedPreferences
}.getOrNull() ?: false }.getOrNull() ?: false
else false else false
@@ -231,13 +231,13 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
(if (isXposedEnvironment) (if (isXposedEnvironment)
if (isUsingKeyValueCache) if (isUsingKeyValueCache)
xPrefCacheKeyValueStrings[key].let { xPrefCacheKeyValueStrings[key].let {
(it ?: xPref.getString(key, value) ?: value).let { value -> (it ?: xPrefs.getString(key, value) ?: value).let { value ->
xPrefCacheKeyValueStrings[key] = value xPrefCacheKeyValueStrings[key] = value
value value
} }
} }
else resetCacheSet { xPref.getString(key, value) ?: value } else resetCacheSet { xPrefs.getString(key, value) ?: value }
else sPref.getString(key, value) ?: value).let { else sPrefs.getString(key, value) ?: value).let {
makeWorldReadable() makeWorldReadable()
it it
} }
@@ -256,13 +256,13 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
(if (isXposedEnvironment) (if (isXposedEnvironment)
if (isUsingKeyValueCache) if (isUsingKeyValueCache)
xPrefCacheKeyValueStrings[key].let { xPrefCacheKeyValueStrings[key].let {
(it ?: xPref.getStringSet(key, value) ?: value).let { value -> (it ?: xPrefs.getStringSet(key, value) ?: value).let { value ->
xPrefCacheKeyValueStringSets[key] = value as Set<String> xPrefCacheKeyValueStringSets[key] = value as Set<String>
value value
} }
} }
else resetCacheSet { xPref.getStringSet(key, value) ?: value } else resetCacheSet { xPrefs.getStringSet(key, value) ?: value }
else sPref.getStringSet(key, value) ?: value).let { else sPrefs.getStringSet(key, value) ?: value).let {
makeWorldReadable() makeWorldReadable()
it it
} }
@@ -281,13 +281,13 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
(if (isXposedEnvironment) (if (isXposedEnvironment)
if (isUsingKeyValueCache) if (isUsingKeyValueCache)
xPrefCacheKeyValueBooleans[key].let { xPrefCacheKeyValueBooleans[key].let {
it ?: xPref.getBoolean(key, value).let { value -> it ?: xPrefs.getBoolean(key, value).let { value ->
xPrefCacheKeyValueBooleans[key] = value xPrefCacheKeyValueBooleans[key] = value
value value
} }
} }
else resetCacheSet { xPref.getBoolean(key, value) } else resetCacheSet { xPrefs.getBoolean(key, value) }
else sPref.getBoolean(key, value)).let { else sPrefs.getBoolean(key, value)).let {
makeWorldReadable() makeWorldReadable()
it it
} }
@@ -306,13 +306,13 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
(if (isXposedEnvironment) (if (isXposedEnvironment)
if (isUsingKeyValueCache) if (isUsingKeyValueCache)
xPrefCacheKeyValueInts[key].let { xPrefCacheKeyValueInts[key].let {
it ?: xPref.getInt(key, value).let { value -> it ?: xPrefs.getInt(key, value).let { value ->
xPrefCacheKeyValueInts[key] = value xPrefCacheKeyValueInts[key] = value
value value
} }
} }
else resetCacheSet { xPref.getInt(key, value) } else resetCacheSet { xPrefs.getInt(key, value) }
else sPref.getInt(key, value)).let { else sPrefs.getInt(key, value)).let {
makeWorldReadable() makeWorldReadable()
it it
} }
@@ -331,13 +331,13 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
(if (isXposedEnvironment) (if (isXposedEnvironment)
if (isUsingKeyValueCache) if (isUsingKeyValueCache)
xPrefCacheKeyValueFloats[key].let { xPrefCacheKeyValueFloats[key].let {
it ?: xPref.getFloat(key, value).let { value -> it ?: xPrefs.getFloat(key, value).let { value ->
xPrefCacheKeyValueFloats[key] = value xPrefCacheKeyValueFloats[key] = value
value value
} }
} }
else resetCacheSet { xPref.getFloat(key, value) } else resetCacheSet { xPrefs.getFloat(key, value) }
else sPref.getFloat(key, value)).let { else sPrefs.getFloat(key, value)).let {
makeWorldReadable() makeWorldReadable()
it it
} }
@@ -356,13 +356,13 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
(if (isXposedEnvironment) (if (isXposedEnvironment)
if (isUsingKeyValueCache) if (isUsingKeyValueCache)
xPrefCacheKeyValueLongs[key].let { xPrefCacheKeyValueLongs[key].let {
it ?: xPref.getLong(key, value).let { value -> it ?: xPrefs.getLong(key, value).let { value ->
xPrefCacheKeyValueLongs[key] = value xPrefCacheKeyValueLongs[key] = value
value value
} }
} }
else resetCacheSet { xPref.getLong(key, value) } else resetCacheSet { xPrefs.getLong(key, value) }
else sPref.getLong(key, value)).let { else sPrefs.getLong(key, value)).let {
makeWorldReadable() makeWorldReadable()
it it
} }
@@ -377,8 +377,8 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
*/ */
fun all() = hashMapOf<String, Any?>().apply { fun all() = hashMapOf<String, Any?>().apply {
if (isXposedEnvironment) if (isXposedEnvironment)
xPref.all.forEach { (k, v) -> this[k] = v } xPrefs.all.forEach { (k, v) -> this[k] = v }
else sPref.all.forEach { (k, v) -> this[k] = v } else sPrefs.all.forEach { (k, v) -> this[k] = v }
} }
/** /**
@@ -390,7 +390,7 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
* @param key 键值名称 * @param key 键值名称
*/ */
fun remove(key: String) = moduleEnvironment { fun remove(key: String) = moduleEnvironment {
sPref.edit().remove(key).apply() sPrefs.edit().remove(key).apply()
makeWorldReadable() makeWorldReadable()
} }
@@ -412,7 +412,7 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
* - ❗在 (Xposed) 宿主环境下只读 - 无法使用 * - ❗在 (Xposed) 宿主环境下只读 - 无法使用
*/ */
fun clear() = moduleEnvironment { fun clear() = moduleEnvironment {
sPref.edit().clear().apply() sPrefs.edit().clear().apply()
makeWorldReadable() makeWorldReadable()
} }
@@ -428,7 +428,7 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
* @param value 键值数据 * @param value 键值数据
*/ */
fun putString(key: String, value: String) = moduleEnvironment { fun putString(key: String, value: String) = moduleEnvironment {
sPref.edit().putString(key, value).apply() sPrefs.edit().putString(key, value).apply()
makeWorldReadable() makeWorldReadable()
} }
@@ -444,7 +444,7 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
* @param value 键值数据 * @param value 键值数据
*/ */
fun putStringSet(key: String, value: Set<String>) = moduleEnvironment { fun putStringSet(key: String, value: Set<String>) = moduleEnvironment {
sPref.edit().putStringSet(key, value).apply() sPrefs.edit().putStringSet(key, value).apply()
makeWorldReadable() makeWorldReadable()
} }
@@ -460,7 +460,7 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
* @param value 键值数据 * @param value 键值数据
*/ */
fun putBoolean(key: String, value: Boolean) = moduleEnvironment { fun putBoolean(key: String, value: Boolean) = moduleEnvironment {
sPref.edit().putBoolean(key, value).apply() sPrefs.edit().putBoolean(key, value).apply()
makeWorldReadable() makeWorldReadable()
} }
@@ -476,7 +476,7 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
* @param value 键值数据 * @param value 键值数据
*/ */
fun putInt(key: String, value: Int) = moduleEnvironment { fun putInt(key: String, value: Int) = moduleEnvironment {
sPref.edit().putInt(key, value).apply() sPrefs.edit().putInt(key, value).apply()
makeWorldReadable() makeWorldReadable()
} }
@@ -492,7 +492,7 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
* @param value 键值数据 * @param value 键值数据
*/ */
fun putFloat(key: String, value: Float) = moduleEnvironment { fun putFloat(key: String, value: Float) = moduleEnvironment {
sPref.edit().putFloat(key, value).apply() sPrefs.edit().putFloat(key, value).apply()
makeWorldReadable() makeWorldReadable()
} }
@@ -508,7 +508,7 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
* @param value 键值数据 * @param value 键值数据
*/ */
fun putLong(key: String, value: Long) = moduleEnvironment { fun putLong(key: String, value: Long) = moduleEnvironment {
sPref.edit().putLong(key, value).apply() sPrefs.edit().putLong(key, value).apply()
makeWorldReadable() makeWorldReadable()
} }