Modify optimize code in YukiHookModulePrefs

This commit is contained in:
2022-08-19 22:47:28 +08:00
parent 809d2d20da
commit 9aafb95f42

View File

@@ -129,7 +129,7 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
/** 检查 API 装载状态 */ /** 检查 API 装载状态 */
private fun checkApi() { private fun checkApi() {
if (YukiHookAPI.isLoadedFromBaseContext) error("YukiHookModulePrefs not allowed in Custom Hook API") if (YukiHookAPI.isLoadedFromBaseContext) error("YukiHookModulePrefs not allowed in Custom Hook API")
if (YukiHookBridge.hasXposedBridge && YukiHookBridge.modulePackageName.isBlank()) if (isXposedEnvironment && YukiHookBridge.modulePackageName.isBlank())
error("Xposed modulePackageName load failed, please reset and rebuild it") error("Xposed modulePackageName load failed, please reset and rebuild it")
} }
@@ -155,11 +155,11 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
get() = try { get() = try {
checkApi() checkApi()
context?.getSharedPreferences(prefsName, Context.MODE_WORLD_READABLE).also { isUsingNewXSharedPreferences = 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("YukiHookModulePrefs missing Context instance")
} catch (_: Throwable) { } catch (_: Throwable) {
checkApi() checkApi()
context?.getSharedPreferences(prefsName, Context.MODE_PRIVATE).also { isUsingNewXSharedPreferences = 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("YukiHookModulePrefs missing Context instance")
} }
/** 设置全局可读可写 */ /** 设置全局可读可写 */
@@ -610,6 +610,6 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
*/ */
private inline fun moduleEnvironment(callback: () -> Unit) { private inline fun moduleEnvironment(callback: () -> Unit) {
if (isXposedEnvironment.not()) callback() if (isXposedEnvironment.not()) callback()
else yLoggerW(msg = "You cannot use write prefs function in Xposed Environment") else yLoggerW(msg = "YukiHookModulePrefs write operation not allowed in Xposed Environment")
} }
} }