From b982d1fd0341c881e989f28991ff911c04659719 Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Sat, 20 Aug 2022 02:48:30 +0800 Subject: [PATCH] Modify add isUseNewConfig in YukiHookFactory and ModuleContextThemeWrapper --- docs/api/public/YukiHookFactory.md | 4 +++- .../yukihookapi/hook/factory/YukiHookFactory.kt | 7 ++++++- .../context/wrapper/ModuleContextThemeWrapper.kt | 14 +++++++++----- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/docs/api/public/YukiHookFactory.md b/docs/api/public/YukiHookFactory.md index 45fe317d..b8d09ce6 100644 --- a/docs/api/public/YukiHookFactory.md +++ b/docs/api/public/YukiHookFactory.md @@ -340,7 +340,7 @@ context.startActivity(context, HostTestActivity::class.java) ### Context.applyTheme *- ext-method* ```kotlin -fun Context.applyTheme(theme: Int): ModuleContextThemeWrapper +fun Context.applyTheme(theme: Int, isUseNewConfig: Boolean): ModuleContextThemeWrapper ``` **变更记录** @@ -353,6 +353,8 @@ fun Context.applyTheme(theme: Int): ModuleContextThemeWrapper 在 Hook APP (宿主) 中使用此方法会自动调用 `injectModuleAppResources` 注入当前 Xposed 模块的资源。 +如果在 Hook APP (宿主) 中使用此方法发生 `ClassCastException`,请设置 `isUseNewConfig` 为 `true`。 + 为防止资源 ID 互相冲突,你需要在当前 Xposed 模块项目的 `build.gradle` 中修改资源 ID。 - Kotlin Gradle DSL diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/factory/YukiHookFactory.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/factory/YukiHookFactory.kt index 94485be6..49789e05 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/factory/YukiHookFactory.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/factory/YukiHookFactory.kt @@ -32,6 +32,7 @@ package com.highcapable.yukihookapi.hook.factory import android.app.Activity import android.content.Context import android.content.Intent +import android.content.res.Configuration import android.content.res.Resources import android.net.Uri import android.os.Bundle @@ -153,11 +154,15 @@ fun Context.registerModuleAppActivities(proxy: Any? = null) = AppParasitics.regi * * 在 Hook APP (宿主) 中使用此方法会自动调用 [injectModuleAppResources] 注入当前 Xposed 模块的资源 * + * - 如果在 Hook APP (宿主) 中使用此方法发生 [ClassCastException] - 请设置 [isUseNewConfig] 为 true + * * 详情请参考 [API 文档 - Context.applyTheme](https://fankes.github.io/YukiHookAPI/#/api/document?id=contextapplytheme-ext-method) * @param theme 主题资源 ID + * @param isUseNewConfig 是否使用新的 [Configuration] - 默认否 * @return [ModuleContextThemeWrapper] */ -fun Context.applyTheme(@StyleRes theme: Int) = ModuleContextThemeWrapper.wrapper(baseContext = this, theme) +fun Context.applyTheme(@StyleRes theme: Int, isUseNewConfig: Boolean = false) = + ModuleContextThemeWrapper.wrapper(baseContext = this, theme, isUseNewConfig) /** * 仅判断模块是否在太极、无极中激活 diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/xposed/parasitic/context/wrapper/ModuleContextThemeWrapper.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/xposed/parasitic/context/wrapper/ModuleContextThemeWrapper.kt index a7a57444..906c2d6b 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/xposed/parasitic/context/wrapper/ModuleContextThemeWrapper.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/xposed/parasitic/context/wrapper/ModuleContextThemeWrapper.kt @@ -29,6 +29,7 @@ package com.highcapable.yukihookapi.hook.xposed.parasitic.context.wrapper import android.content.Context +import android.content.res.Configuration import android.content.res.Resources import android.view.ContextThemeWrapper import com.highcapable.yukihookapi.hook.factory.injectModuleAppResources @@ -41,8 +42,10 @@ import com.highcapable.yukihookapi.hook.xposed.parasitic.reference.ModuleClassLo * 通过包装 - 你可以轻松在 (Xposed) 宿主环境使用来自模块的主题资源 * @param baseContext 原始 [Context] * @param theme 使用的主题 + * @param isUseNewConfig 是否使用新的 [Configuration] */ -class ModuleContextThemeWrapper private constructor(baseContext: Context, theme: Int) : ContextThemeWrapper(baseContext, theme) { +class ModuleContextThemeWrapper private constructor(baseContext: Context, theme: Int, isUseNewConfig: Boolean) : + ContextThemeWrapper(baseContext, theme) { internal companion object { @@ -50,12 +53,13 @@ class ModuleContextThemeWrapper private constructor(baseContext: Context, theme: * 从 [Context] 创建 [ModuleContextThemeWrapper] * @param baseContext 对接的 [Context] * @param theme 需要使用的主题 + * @param isUseNewConfig 是否使用新的 [Configuration] * @return [ModuleContextThemeWrapper] * @throws IllegalStateException 如果重复装载 */ - internal fun wrapper(baseContext: Context, theme: Int) = + internal fun wrapper(baseContext: Context, theme: Int, isUseNewConfig: Boolean) = if (baseContext !is ModuleContextThemeWrapper) - ModuleContextThemeWrapper(baseContext, theme) + ModuleContextThemeWrapper(baseContext, theme, isUseNewConfig) else error("ModuleContextThemeWrapper already loaded") } @@ -63,8 +67,8 @@ class ModuleContextThemeWrapper private constructor(baseContext: Context, theme: private var baseResources: Resources? = null init { - if (baseContext.resources?.configuration != null) - baseResources = baseContext.createConfigurationContext(baseContext.resources.configuration).resources + if (isUseNewConfig && baseContext.resources?.configuration != null) + baseResources = baseContext.createConfigurationContext(baseContext.resources.configuration)?.resources if (YukiHookBridge.hasXposedBridge) resources?.injectModuleAppResources() }