diff --git a/docs/api/public/YukiHookFactory.md b/docs/api/public/YukiHookFactory.md index 0f52eb0d..e799e2d3 100644 --- a/docs/api/public/YukiHookFactory.md +++ b/docs/api/public/YukiHookFactory.md @@ -335,6 +335,73 @@ val context: Context = ... // 假设这就是你的 Context context.startActivity(context, HostTestActivity::class.java) ``` +### applyTheme [method] + +```kotlin +fun Context.applyTheme(theme: Int): ModuleContextThemeWrapper +``` + +**变更记录** + +`v1.0.93` `新增` + +**功能描述** + +> 生成一个 `ContextThemeWrapper` 代理以应用主题资源。 + +在 Hook APP (宿主) 中使用此方法会自动调用 `injectModuleAppResources` 注入当前 Xposed 模块的资源。 + +为防止资源 ID 互相冲突,你需要在当前 Xposed 模块项目的 `build.gradle` 中修改资源 ID。 + +- Kotlin Gradle DSL + +```kotlin +androidResources.additionalParameters("--allow-reserved-package-id", "--package-id", "0x64") +``` + +- Groovy + +```groovy +aaptOptions.additionalParameters '--allow-reserved-package-id', '--package-id', '0x64' +``` + +!> 提供的示例资源 ID 值仅供参考,为了防止当前宿主存在多个 Xposed 模块,建议自定义你自己的资源 ID。 + +**功能示例** + +有时候,我们需要使用 `MaterialAlertDialogBuilder` 来美化自己在宿主中的对话框,但是拿不到 AppCompat 主题就无法创建。 + +- 会得到如下异常 + +``` +The style on this component requires your app theme to be Theme.AppCompat (or a descendant). +``` + +这时,我们想在宿主被 Hook 的当前 `Activity` 中使用 `MaterialAlertDialogBuilder` 来创建对话框,就可以有如下方法。 + +> 示例如下 + +```kotlin +injectMember { + method { + name = "onCreate" + param(BundleClass) + } + afterHook { + // 使用 applyTheme 创建一个当前模块中的主题资源 + val appCompatContext = instance().applyTheme(R.style.Theme_AppCompat) + // 直接使用这个包装了模块主题后的 Context 创建对话框 + MaterialAlertDialogBuilder(appCompatContext) + .setTitle("AppCompat 主题对话框") + .setMessage("我是一个在宿主中显示的 AppCompat 主题对话框。") + .setPositiveButton("确定", null) + .show() + } +} +``` + +这样,我们就可以在宿主中非常简单地使用 `MaterialAlertDialogBuilder` 创建对话框了。 + ### ~~isSupportResourcesHook [field]~~ **变更记录** 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 0578a098..416a27f0 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 @@ -36,7 +36,9 @@ import android.content.res.Resources import android.net.Uri import android.os.Bundle import android.os.Process +import android.view.ContextThemeWrapper import android.widget.ImageView +import androidx.annotation.StyleRes import com.highcapable.yukihookapi.YukiHookAPI import com.highcapable.yukihookapi.hook.entity.YukiBaseHooker import com.highcapable.yukihookapi.hook.param.PackageParam @@ -44,6 +46,7 @@ import com.highcapable.yukihookapi.hook.xposed.channel.YukiHookDataChannel import com.highcapable.yukihookapi.hook.xposed.parasitic.AppParasitics import com.highcapable.yukihookapi.hook.xposed.parasitic.activity.base.ModuleAppActivity import com.highcapable.yukihookapi.hook.xposed.parasitic.activity.base.ModuleAppCompatActivity +import com.highcapable.yukihookapi.hook.xposed.parasitic.context.wrapper.ModuleContextThemeWrapper import com.highcapable.yukihookapi.hook.xposed.prefs.YukiHookModulePrefs import com.highcapable.yukihookapi.hook.xposed.proxy.IYukiHookXposedInit import java.io.BufferedReader @@ -143,6 +146,17 @@ fun Resources.injectModuleAppResources() = AppParasitics.injectModuleAppResource */ fun Context.registerModuleAppActivities(proxy: Any? = null) = AppParasitics.registerModuleAppActivities(context = this, proxy) +/** + * 生成一个 [ContextThemeWrapper] 代理以应用主题资源 + * + * 在 Hook APP (宿主) 中使用此方法会自动调用 [injectModuleAppResources] 注入当前 Xposed 模块的资源 + * + * 详情请参考 [applyTheme](https://fankes.github.io/YukiHookAPI/#/api/document?id=applytheme-method) + * @param theme 主题资源 ID + * @return [ModuleContextThemeWrapper] + */ +fun Context.applyTheme(@StyleRes theme: Int) = ModuleContextThemeWrapper.wrapper(baseContext = this, theme) + /** * 仅判断模块是否在太极、无极中激活 * 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 new file mode 100644 index 00000000..a7a57444 --- /dev/null +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/xposed/parasitic/context/wrapper/ModuleContextThemeWrapper.kt @@ -0,0 +1,74 @@ +/* + * YukiHookAPI - An efficient Kotlin version of the Xposed Hook API. + * Copyright (C) 2019-2022 HighCapable + * https://github.com/fankes/YukiHookAPI + * + * MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * This file is Created by fankes on 2022/8/15. + * Thanks for providing https://github.com/cinit/QAuxiliary/blob/main/app/src/main/java/io/github/qauxv/ui/CommonContextWrapper.java + */ +package com.highcapable.yukihookapi.hook.xposed.parasitic.context.wrapper + +import android.content.Context +import android.content.res.Resources +import android.view.ContextThemeWrapper +import com.highcapable.yukihookapi.hook.factory.injectModuleAppResources +import com.highcapable.yukihookapi.hook.xposed.bridge.YukiHookBridge +import com.highcapable.yukihookapi.hook.xposed.parasitic.reference.ModuleClassLoader + +/** + * 代理 [ContextThemeWrapper] + * + * 通过包装 - 你可以轻松在 (Xposed) 宿主环境使用来自模块的主题资源 + * @param baseContext 原始 [Context] + * @param theme 使用的主题 + */ +class ModuleContextThemeWrapper private constructor(baseContext: Context, theme: Int) : ContextThemeWrapper(baseContext, theme) { + + internal companion object { + + /** + * 从 [Context] 创建 [ModuleContextThemeWrapper] + * @param baseContext 对接的 [Context] + * @param theme 需要使用的主题 + * @return [ModuleContextThemeWrapper] + * @throws IllegalStateException 如果重复装载 + */ + internal fun wrapper(baseContext: Context, theme: Int) = + if (baseContext !is ModuleContextThemeWrapper) + ModuleContextThemeWrapper(baseContext, theme) + else error("ModuleContextThemeWrapper already loaded") + } + + /** 创建用于替换的 [Resources] */ + private var baseResources: Resources? = null + + init { + if (baseContext.resources?.configuration != null) + baseResources = baseContext.createConfigurationContext(baseContext.resources.configuration).resources + if (YukiHookBridge.hasXposedBridge) resources?.injectModuleAppResources() + } + + override fun getClassLoader(): ClassLoader = ModuleClassLoader.instance() + + override fun getResources(): Resources? = baseResources ?: super.getResources() +} \ No newline at end of file