mirror of
https://github.com/HighCapable/YukiHookAPI.git
synced 2025-09-07 19:14:08 +08:00
Added new registerReceiver function and fix Host App lifecycle events call multiple times in AppLifecycle in PackageParam
This commit is contained in:
@@ -1151,10 +1151,18 @@ fun onConfigurationChanged(result: (self: Application, config: Configuration) ->
|
|||||||
fun registerReceiver(vararg action: String, result: (context: Context, intent: Intent) -> Unit)
|
fun registerReceiver(vararg action: String, result: (context: Context, intent: Intent) -> Unit)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```kotlin:no-line-numbers
|
||||||
|
fun registerReceiver(filter: IntentFilter, result: (context: Context, intent: Intent) -> Unit)
|
||||||
|
```
|
||||||
|
|
||||||
**Change Records**
|
**Change Records**
|
||||||
|
|
||||||
`v1.0.88` `added`
|
`v1.0.88` `added`
|
||||||
|
|
||||||
|
`v1.1.7` `modified`
|
||||||
|
|
||||||
|
新增直接使用 `IntentFilter` 注册系统广播监听
|
||||||
|
|
||||||
**Function Illustrate**
|
**Function Illustrate**
|
||||||
|
|
||||||
> 注册系统广播监听。
|
> 注册系统广播监听。
|
@@ -1143,10 +1143,18 @@ fun onConfigurationChanged(result: (self: Application, config: Configuration) ->
|
|||||||
fun registerReceiver(vararg action: String, result: (context: Context, intent: Intent) -> Unit)
|
fun registerReceiver(vararg action: String, result: (context: Context, intent: Intent) -> Unit)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```kotlin:no-line-numbers
|
||||||
|
fun registerReceiver(filter: IntentFilter, result: (context: Context, intent: Intent) -> Unit)
|
||||||
|
```
|
||||||
|
|
||||||
**变更记录**
|
**变更记录**
|
||||||
|
|
||||||
`v1.0.88` `新增`
|
`v1.0.88` `新增`
|
||||||
|
|
||||||
|
`v1.1.7` `修改`
|
||||||
|
|
||||||
|
新增直接使用 `IntentFilter` 注册系统广播监听
|
||||||
|
|
||||||
**功能描述**
|
**功能描述**
|
||||||
|
|
||||||
> 注册系统广播监听。
|
> 注册系统广播监听。
|
@@ -32,6 +32,7 @@ package com.highcapable.yukihookapi.hook.param
|
|||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.content.IntentFilter
|
||||||
import android.content.pm.ApplicationInfo
|
import android.content.pm.ApplicationInfo
|
||||||
import android.content.res.Configuration
|
import android.content.res.Configuration
|
||||||
import android.content.res.Resources
|
import android.content.res.Resources
|
||||||
@@ -670,12 +671,20 @@ open class PackageParam internal constructor(@PublishedApi internal var wrapper:
|
|||||||
*/
|
*/
|
||||||
inner class AppLifecycle @PublishedApi internal constructor(private val isOnFailureThrowToApp: Boolean) {
|
inner class AppLifecycle @PublishedApi internal constructor(private val isOnFailureThrowToApp: Boolean) {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否为当前操作 [HookEntryType.PACKAGE] 的调用域
|
||||||
|
*
|
||||||
|
* 为避免多次设置回调事件 - 回调事件仅在 Hook 开始后生效
|
||||||
|
* @return [Boolean]
|
||||||
|
*/
|
||||||
|
private val isCurrentScope get() = wrapper?.type == HookEntryType.PACKAGE
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 监听当前 Hook APP 装载 [Application.attachBaseContext]
|
* 监听当前 Hook APP 装载 [Application.attachBaseContext]
|
||||||
* @param result 回调 - ([Context] baseContext,[Boolean] 是否已执行 super)
|
* @param result 回调 - ([Context] baseContext,[Boolean] 是否已执行 super)
|
||||||
*/
|
*/
|
||||||
fun attachBaseContext(result: (baseContext: Context, hasCalledSuper: Boolean) -> Unit) {
|
fun attachBaseContext(result: (baseContext: Context, hasCalledSuper: Boolean) -> Unit) {
|
||||||
AppParasitics.AppLifecycleCallback.attachBaseContextCallback = result
|
if (isCurrentScope) AppParasitics.AppLifecycleCallback.attachBaseContextCallback = result
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -683,7 +692,7 @@ open class PackageParam internal constructor(@PublishedApi internal var wrapper:
|
|||||||
* @param initiate 方法体
|
* @param initiate 方法体
|
||||||
*/
|
*/
|
||||||
fun onCreate(initiate: Application.() -> Unit) {
|
fun onCreate(initiate: Application.() -> Unit) {
|
||||||
AppParasitics.AppLifecycleCallback.onCreateCallback = initiate
|
if (isCurrentScope) AppParasitics.AppLifecycleCallback.onCreateCallback = initiate
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -691,7 +700,7 @@ open class PackageParam internal constructor(@PublishedApi internal var wrapper:
|
|||||||
* @param initiate 方法体
|
* @param initiate 方法体
|
||||||
*/
|
*/
|
||||||
fun onTerminate(initiate: Application.() -> Unit) {
|
fun onTerminate(initiate: Application.() -> Unit) {
|
||||||
AppParasitics.AppLifecycleCallback.onTerminateCallback = initiate
|
if (isCurrentScope) AppParasitics.AppLifecycleCallback.onTerminateCallback = initiate
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -699,7 +708,7 @@ open class PackageParam internal constructor(@PublishedApi internal var wrapper:
|
|||||||
* @param initiate 方法体
|
* @param initiate 方法体
|
||||||
*/
|
*/
|
||||||
fun onLowMemory(initiate: Application.() -> Unit) {
|
fun onLowMemory(initiate: Application.() -> Unit) {
|
||||||
AppParasitics.AppLifecycleCallback.onLowMemoryCallback = initiate
|
if (isCurrentScope) AppParasitics.AppLifecycleCallback.onLowMemoryCallback = initiate
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -707,7 +716,7 @@ open class PackageParam internal constructor(@PublishedApi internal var wrapper:
|
|||||||
* @param result 回调 - ([Application] 当前实例,[Int] 类型)
|
* @param result 回调 - ([Application] 当前实例,[Int] 类型)
|
||||||
*/
|
*/
|
||||||
fun onTrimMemory(result: (self: Application, level: Int) -> Unit) {
|
fun onTrimMemory(result: (self: Application, level: Int) -> Unit) {
|
||||||
AppParasitics.AppLifecycleCallback.onTrimMemoryCallback = result
|
if (isCurrentScope) AppParasitics.AppLifecycleCallback.onTrimMemoryCallback = result
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -715,7 +724,7 @@ open class PackageParam internal constructor(@PublishedApi internal var wrapper:
|
|||||||
* @param result 回调 - ([Application] 当前实例,[Configuration] 配置实例)
|
* @param result 回调 - ([Application] 当前实例,[Configuration] 配置实例)
|
||||||
*/
|
*/
|
||||||
fun onConfigurationChanged(result: (self: Application, config: Configuration) -> Unit) {
|
fun onConfigurationChanged(result: (self: Application, config: Configuration) -> Unit) {
|
||||||
AppParasitics.AppLifecycleCallback.onConfigurationChangedCallback = result
|
if (isCurrentScope) AppParasitics.AppLifecycleCallback.onConfigurationChangedCallback = result
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -724,7 +733,17 @@ open class PackageParam internal constructor(@PublishedApi internal var wrapper:
|
|||||||
* @param result 回调 - ([Context] 当前上下文,[Intent] 当前 Intent)
|
* @param result 回调 - ([Context] 当前上下文,[Intent] 当前 Intent)
|
||||||
*/
|
*/
|
||||||
fun registerReceiver(vararg action: String, result: (context: Context, intent: Intent) -> Unit) {
|
fun registerReceiver(vararg action: String, result: (context: Context, intent: Intent) -> Unit) {
|
||||||
if (action.isNotEmpty()) AppParasitics.AppLifecycleCallback.onReceiversCallback[action.value()] = Pair(action, result)
|
if (isCurrentScope && action.isNotEmpty())
|
||||||
|
AppParasitics.AppLifecycleCallback.onReceiverActionsCallbacks[action.value()] = Pair(action, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册系统广播监听
|
||||||
|
* @param filter 广播意图过滤器
|
||||||
|
* @param result 回调 - ([Context] 当前上下文,[Intent] 当前 Intent)
|
||||||
|
*/
|
||||||
|
fun registerReceiver(filter: IntentFilter, result: (context: Context, intent: Intent) -> Unit) {
|
||||||
|
if (isCurrentScope) AppParasitics.AppLifecycleCallback.onReceiverFiltersCallbacks[filter.toString()] = Pair(filter, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 设置创建生命周期监听回调 */
|
/** 设置创建生命周期监听回调 */
|
||||||
|
@@ -292,16 +292,27 @@ internal object AppParasitics {
|
|||||||
override fun afterHookedMember(param: Param) {
|
override fun afterHookedMember(param: Param) {
|
||||||
runCatching {
|
runCatching {
|
||||||
(param.args?.get(0) as? Application?)?.also {
|
(param.args?.get(0) as? Application?)?.also {
|
||||||
hostApplication = it
|
/**
|
||||||
AppLifecycleCallback.onCreateCallback?.invoke(it)
|
* 注册广播
|
||||||
AppLifecycleCallback.onReceiversCallback.takeIf { e -> e.isNotEmpty() }?.forEach { (_, e) ->
|
* @param result 回调 - ([Context] 当前实例, [Intent] 当前对象)
|
||||||
if (e.first.isNotEmpty()) it.registerReceiver(object : BroadcastReceiver() {
|
*/
|
||||||
|
fun IntentFilter.registerReceiver(result: (Context, Intent) -> Unit) {
|
||||||
|
it.registerReceiver(object : BroadcastReceiver() {
|
||||||
override fun onReceive(context: Context?, intent: Intent?) {
|
override fun onReceive(context: Context?, intent: Intent?) {
|
||||||
if (context == null || intent == null) return
|
if (context == null || intent == null) return
|
||||||
if (e.first.any { e -> e == intent.action }) e.second(context, intent)
|
result(context, intent)
|
||||||
}
|
}
|
||||||
}, IntentFilter().apply { e.first.forEach { e -> addAction(e) } })
|
}, this)
|
||||||
}
|
}
|
||||||
|
hostApplication = it
|
||||||
|
AppLifecycleCallback.onCreateCallback?.invoke(it)
|
||||||
|
AppLifecycleCallback.onReceiverActionsCallbacks.takeIf { e -> e.isNotEmpty() }?.forEach { (_, e) ->
|
||||||
|
if (e.first.isNotEmpty()) IntentFilter().apply {
|
||||||
|
e.first.forEach { action -> addAction(action) }
|
||||||
|
}.registerReceiver(e.second)
|
||||||
|
}
|
||||||
|
AppLifecycleCallback.onReceiverFiltersCallbacks.takeIf { e -> e.isNotEmpty() }
|
||||||
|
?.forEach { (_, e) -> e.first.registerReceiver(e.second) }
|
||||||
runCatching {
|
runCatching {
|
||||||
/** 过滤系统框架与一系列服务组件包名不唯一的情况 */
|
/** 过滤系统框架与一系列服务组件包名不唯一的情况 */
|
||||||
if (isDataChannelRegistered ||
|
if (isDataChannelRegistered ||
|
||||||
@@ -422,6 +433,9 @@ internal object AppParasitics {
|
|||||||
internal var onConfigurationChangedCallback: ((Application, Configuration) -> Unit)? = null
|
internal var onConfigurationChangedCallback: ((Application, Configuration) -> Unit)? = null
|
||||||
|
|
||||||
/** 系统广播监听回调 */
|
/** 系统广播监听回调 */
|
||||||
internal val onReceiversCallback = HashMap<String, Pair<Array<out String>, (Context, Intent) -> Unit>>()
|
internal val onReceiverActionsCallbacks = HashMap<String, Pair<Array<out String>, (Context, Intent) -> Unit>>()
|
||||||
|
|
||||||
|
/** 系统广播监听回调 */
|
||||||
|
internal val onReceiverFiltersCallbacks = HashMap<String, Pair<IntentFilter, (Context, Intent) -> Unit>>()
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user