Fix received data failed when same Host App scope decided and change the default action name in YukiHookDataChannel

This commit is contained in:
2023-04-10 00:52:35 +08:00
parent a8121d8735
commit 98d46a3d9e

View File

@@ -37,10 +37,7 @@ import android.content.Context
import android.content.Context.ACTIVITY_SERVICE import android.content.Context.ACTIVITY_SERVICE
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.os.Bundle import android.os.*
import android.os.Parcel
import android.os.Parcelable
import android.os.TransactionTooLargeException
import com.highcapable.yukihookapi.YukiHookAPI import com.highcapable.yukihookapi.YukiHookAPI
import com.highcapable.yukihookapi.annotation.CauseProblemsApi import com.highcapable.yukihookapi.annotation.CauseProblemsApi
import com.highcapable.yukihookapi.hook.log.* import com.highcapable.yukihookapi.hook.log.*
@@ -193,16 +190,15 @@ class YukiHookDataChannel private constructor() {
* @param packageName 包名 * @param packageName 包名
* @return [String] * @return [String]
*/ */
private fun hostActionName(packageName: String) = "yukihookapi.intent.action.HOST_DATA_CHANNEL_${packageName.trim().hashCode()}" private fun hostActionName(packageName: String) = "yuki_hook_host_data_channel_${packageName.trim().hashCode()}"
/** /**
* 获取模块广播 Action 名称 * 获取模块广播 Action 名称
* @param context 实例 - 默认空 * @param context 实例 - 默认空
* @return [String] * @return [String]
*/ */
private fun moduleActionName(context: Context? = null) = "yukihookapi.intent.action.MODULE_DATA_CHANNEL_${ private fun moduleActionName(context: Context? = null) =
YukiXposedModule.modulePackageName.ifBlank { context?.packageName ?: "" }.trim().hashCode() "yuki_hook_module_data_channel_${YukiXposedModule.modulePackageName.ifBlank { context?.packageName ?: "" }.trim().hashCode()}"
}"
/** /**
* 注册广播 * 注册广播
@@ -381,7 +377,7 @@ class YukiHookDataChannel private constructor() {
receiverCallbacks[key + keyShortName(CallbackKeyType.SINGLE)] = Pair(context) { action, intent -> receiverCallbacks[key + keyShortName(CallbackKeyType.SINGLE)] = Pair(context) { action, intent ->
if (priority == null || priority.result) if (priority == null || priority.result)
if (action == if (isXposedEnvironment) hostActionName(packageName) else moduleActionName(context)) if (action == if (isXposedEnvironment) hostActionName(packageName) else moduleActionName(context))
parseReceivedData(intent.extras?.get(key + keyNonRepeatName)?.toChannelWrapper(), result) parseReceivedData(intent.getDataWrapper(key), result)
} }
} }
@@ -395,7 +391,7 @@ class YukiHookDataChannel private constructor() {
receiverCallbacks[data.key + keyShortName(CallbackKeyType.CDATA)] = Pair(context) { action, intent -> receiverCallbacks[data.key + keyShortName(CallbackKeyType.CDATA)] = Pair(context) { action, intent ->
if (priority == null || priority.result) if (priority == null || priority.result)
if (action == if (isXposedEnvironment) hostActionName(packageName) else moduleActionName(context)) if (action == if (isXposedEnvironment) hostActionName(packageName) else moduleActionName(context))
parseReceivedData(intent.extras?.get(data.key + keyNonRepeatName)?.toChannelWrapper(), result) parseReceivedData(intent.getDataWrapper(data.key), result)
} }
} }
@@ -411,8 +407,7 @@ class YukiHookDataChannel private constructor() {
receiverCallbacks[key + keyShortName(CallbackKeyType.VMFL)] = Pair(context) { action, intent -> receiverCallbacks[key + keyShortName(CallbackKeyType.VMFL)] = Pair(context) { action, intent ->
if (priority == null || priority.result) if (priority == null || priority.result)
if (action == if (isXposedEnvironment) hostActionName(packageName) else moduleActionName(context)) if (action == if (isXposedEnvironment) hostActionName(packageName) else moduleActionName(context))
intent.extras?.get(key + keyNonRepeatName)?.toChannelWrapper<String>() intent.getDataWrapper<String>(key)?.let { if (it.instance.value == VALUE_WAIT_FOR_LISTENER) callback() }
?.let { if (it.instance.value == VALUE_WAIT_FOR_LISTENER) callback() }
} }
} }
@@ -445,12 +440,12 @@ class YukiHookDataChannel private constructor() {
} }
/** /**
* 接收到的任意类型数据转换为 [ChannelDataWrapper]<[T]> 实例 * 从 [Intent] 获取接收到的任意类型数据转换为 [ChannelDataWrapper]<[T]> 实例
* @return [ChannelDataWrapper]<[T]> * @param key 键值名称
* @throws IllegalStateException 如果数据类型不正确 * @return [ChannelDataWrapper]<[T]> or null
*/ */
private fun <T> Any.toChannelWrapper() = private fun <T> Intent.getDataWrapper(key: String) =
this as? ChannelDataWrapper<T> ?: error("Received data type ${javaClass.name} is not a vailed YukiHookDataChannel's data") runCatching { extras?.getSerializable(key + keyNonRepeatName) as? ChannelDataWrapper<T> }.getOrNull()
/** /**
* [ChannelData]<[T]> 转换为 [ChannelDataWrapper]<[T]> 实例 * [ChannelData]<[T]> 转换为 [ChannelDataWrapper]<[T]> 实例