Added ModuleContextThemeWrapper function

This commit is contained in:
2022-08-16 02:19:36 +08:00
parent 986eda2cd2
commit 50995c1e3a
3 changed files with 155 additions and 0 deletions

View File

@@ -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)
/**
* 仅判断模块是否在太极、无极中激活
*

View File

@@ -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()
}