From bff441b07e8dbed02c93eda6cad7cbde602e2fc1 Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Wed, 27 Jul 2022 23:48:50 +0800 Subject: [PATCH] Added systemContext function in PackageParam --- docs/api/public/PackageParam.md | 14 ++++++++ docs/config/api-exception.md | 19 ++++++++++ .../yukihookapi/hook/param/PackageParam.kt | 35 +++++++++++-------- .../hook/xposed/bridge/YukiHookBridge.kt | 16 ++++++--- 4 files changed, 66 insertions(+), 18 deletions(-) diff --git a/docs/api/public/PackageParam.md b/docs/api/public/PackageParam.md index 99377efb..544b132a 100644 --- a/docs/api/public/PackageParam.md +++ b/docs/api/public/PackageParam.md @@ -72,6 +72,20 @@ val appResources:Resources !> 你只能在 `HookResources.hook` 方法体内或 `appContext` 装载完毕时进行调用。 +### systemContext [field] + +```kotlin +val systemContext: Context +``` + +**变更记录** + +`v1.0.93` `新增` + +**功能描述** + +> 获取当前系统框架的 `Context`。 + ### processName [field] ```kotlin diff --git a/docs/config/api-exception.md b/docs/config/api-exception.md index 9fa07c75..09da9dc4 100644 --- a/docs/config/api-exception.md +++ b/docs/config/api-exception.md @@ -456,6 +456,25 @@ Resources 的 Hook 并非类似方法的 Hook,其必须拥有完整的名称 > 这些异常会直接导致 APP 停止运行(FC),同时会在控制台打印 `E` 级别的日志,还会造成 Hook 进程“死掉”。 +!> `IllegalStateException` Failed to got SystemContext + +**异常原因** + +在被 Hook 的宿主内调用了 `systemContext` 但并未成功获取到实例对象。 + +> 示例如下 + +```kotlin +encase { + // 调用了此变量 + systemContext... +} +``` + +**解决方案** + +这种情况不应该存在,由于 `systemContext` 通过反射从 `ActivityThread` 中得到,除非系统进程发生异常,否则获取到的对象不会为空。 + !> `IllegalStateException` App is dead, You cannot call to appContext **异常原因** diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/param/PackageParam.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/param/PackageParam.kt index f4f087df..0bdb41a5 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/param/PackageParam.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/param/PackageParam.kt @@ -85,20 +85,6 @@ open class PackageParam internal constructor(@PublishedApi internal var wrapper: */ val appInfo get() = wrapper?.appInfo ?: YukiHookAppHelper.currentApplicationInfo() ?: ApplicationInfo() - /** - * 获取当前 Hook APP 的进程名称 - * - * 默认的进程名称是 [packageName] - * @return [String] - */ - val processName get() = wrapper?.processName ?: YukiHookAppHelper.currentProcessName() ?: packageName - - /** - * 获取当前 Hook APP 的包名 - * @return [String] - */ - val packageName get() = wrapper?.packageName ?: YukiHookAppHelper.currentPackageName() ?: "" - /** * 获取当前 Hook APP 的 [Application] 实例 * @@ -117,6 +103,27 @@ open class PackageParam internal constructor(@PublishedApi internal var wrapper: */ val appResources get() = wrapper?.appResources ?: appContext.resources ?: error("You cannot call to appResources in this time") + /** + * 获取当前系统框架的 [Context] + * @return [Context] ContextImpl 实例对象 + * @throws IllegalStateException 如果获取不到系统框架的 [Context] + */ + val systemContext get() = YukiHookBridge.systemContext + + /** + * 获取当前 Hook APP 的进程名称 + * + * 默认的进程名称是 [packageName] + * @return [String] + */ + val processName get() = wrapper?.processName ?: YukiHookAppHelper.currentProcessName() ?: packageName + + /** + * 获取当前 Hook APP 的包名 + * @return [String] + */ + val packageName get() = wrapper?.packageName ?: YukiHookAppHelper.currentPackageName() ?: "" + /** * 获取当前 Hook APP 是否为第一个 [Application] * @return [Boolean] diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/xposed/bridge/YukiHookBridge.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/xposed/bridge/YukiHookBridge.kt index 0f5404ba..5a2c088f 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/xposed/bridge/YukiHookBridge.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/xposed/bridge/YukiHookBridge.kt @@ -46,10 +46,7 @@ import com.highcapable.yukihookapi.hook.param.PackageParam import com.highcapable.yukihookapi.hook.param.type.HookEntryType import com.highcapable.yukihookapi.hook.param.wrapper.HookParamWrapper import com.highcapable.yukihookapi.hook.param.wrapper.PackageParamWrapper -import com.highcapable.yukihookapi.hook.type.android.ApplicationClass -import com.highcapable.yukihookapi.hook.type.android.ConfigurationClass -import com.highcapable.yukihookapi.hook.type.android.ContextClass -import com.highcapable.yukihookapi.hook.type.android.InstrumentationClass +import com.highcapable.yukihookapi.hook.type.android.* import com.highcapable.yukihookapi.hook.type.java.IntType import com.highcapable.yukihookapi.hook.xposed.bridge.dummy.YukiModuleResources import com.highcapable.yukihookapi.hook.xposed.bridge.dummy.YukiResources @@ -122,6 +119,17 @@ object YukiHookBridge { */ internal val moduleGeneratedVersion get() = YukiHookBridge_Injector.getModuleGeneratedVersion() + /** + * 获取当前系统框架的 [Context] + * @return [Context] ContextImpl 实例对象 + * @throws IllegalStateException 如果获取不到系统框架的 [Context] + */ + internal val systemContext + get() = runCatching { + Hooker.findMethod(ActivityThreadClass, name = "getSystemContext") + .invoke(Hooker.findMethod(ActivityThreadClass, name = "currentActivityThread").invoke(null)) as? Context? + }.getOrNull() ?: error("Failed to got SystemContext") + /** * 模块是否装载了 Xposed 回调方法 *