mirror of
https://github.com/HighCapable/YukiHookAPI.git
synced 2025-09-04 09:45:19 +08:00
Make YukiHookModulePrefs singleton
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
## YukiHookModulePrefs [class]
|
||||
|
||||
```kotlin
|
||||
class YukiHookModulePrefs(private val context: Context?)
|
||||
class YukiHookModulePrefs(private var context: Context?)
|
||||
```
|
||||
|
||||
**变更记录**
|
||||
|
@@ -539,7 +539,7 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
// 可以使用但是不推荐
|
||||
// ❗为防止存在多个实例 - 不要这样使用
|
||||
YukiHookModulePrefs(this).getBoolean("test_data")
|
||||
// ✅ 推荐的使用方法
|
||||
modulePrefs.getBoolean("test_data")
|
||||
|
@@ -77,14 +77,14 @@ fun YukiHookXposedInitProxy.encase(vararg hooker: YukiBaseHooker) = YukiHookAPI.
|
||||
* 获取模块的存取对象
|
||||
* @return [YukiHookModulePrefs]
|
||||
*/
|
||||
val Context.modulePrefs get() = YukiHookModulePrefs(context = this)
|
||||
val Context.modulePrefs get() = YukiHookModulePrefs.instance(context = this)
|
||||
|
||||
/**
|
||||
* 获取模块的存取对象
|
||||
* @param name 自定义 Sp 存储名称
|
||||
* @return [YukiHookModulePrefs]
|
||||
*/
|
||||
fun Context.modulePrefs(name: String) = YukiHookModulePrefs(context = this).name(name)
|
||||
fun Context.modulePrefs(name: String) = YukiHookModulePrefs.instance(context = this).name(name)
|
||||
|
||||
/**
|
||||
* 获取当前进程名称
|
||||
|
@@ -150,7 +150,7 @@ open class PackageParam(@PublishedApi internal var wrapper: PackageParamWrapper?
|
||||
* - ❗作为 Hook API 装载时无法使用 - 会抛出异常
|
||||
* @return [YukiHookModulePrefs]
|
||||
*/
|
||||
val prefs by lazy { YukiHookModulePrefs() }
|
||||
val prefs get() = YukiHookModulePrefs.instance()
|
||||
|
||||
/**
|
||||
* 获得当前使用的存取数据对象缓存实例
|
||||
|
@@ -27,7 +27,7 @@
|
||||
*/
|
||||
@file:Suppress(
|
||||
"SetWorldReadable", "CommitPrefEdits", "DEPRECATION", "WorldReadableFiles",
|
||||
"unused", "UNCHECKED_CAST", "MemberVisibilityCanBePrivate"
|
||||
"unused", "UNCHECKED_CAST", "MemberVisibilityCanBePrivate", "StaticFieldLeak"
|
||||
)
|
||||
|
||||
package com.highcapable.yukihookapi.hook.xposed.prefs
|
||||
@@ -64,19 +64,34 @@ import java.io.File
|
||||
* - 详情请参考 [API 文档 - YukiHookModulePrefs](https://fankes.github.io/YukiHookAPI/#/api/document?id=yukihookmoduleprefs-class)
|
||||
* @param context 上下文实例 - 默认空
|
||||
*/
|
||||
class YukiHookModulePrefs(private val context: Context? = null) {
|
||||
class YukiHookModulePrefs(private var context: Context? = null) {
|
||||
|
||||
internal companion object {
|
||||
|
||||
/** 当前 [YukiHookModulePrefs] 单例 */
|
||||
private var instance: YukiHookModulePrefs? = null
|
||||
|
||||
/**
|
||||
* 获取 [YukiHookModulePrefs] 单例
|
||||
* @param context 实例 - Xposed 环境为空
|
||||
* @return [YukiHookModulePrefs]
|
||||
*/
|
||||
internal fun instance(context: Context? = null) =
|
||||
instance?.apply { this.context = context } ?: YukiHookModulePrefs(context).apply { instance = this }
|
||||
|
||||
/**
|
||||
* 设置全局可读可写
|
||||
* @param context 实例
|
||||
* @param prefsFileName Sp 文件名
|
||||
*/
|
||||
internal fun makeWorldReadable(context: Context?, prefsFileName: String) = runCatching {
|
||||
File(File(context!!.applicationInfo.dataDir, "shared_prefs"), prefsFileName).apply {
|
||||
setReadable(true, false)
|
||||
setExecutable(true, false)
|
||||
internal fun makeWorldReadable(context: Context?, prefsFileName: String) {
|
||||
runCatching {
|
||||
context?.also {
|
||||
File(File(it.applicationInfo.dataDir, "shared_prefs"), prefsFileName).apply {
|
||||
setReadable(true, false)
|
||||
setExecutable(true, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user