Skip to content

Commit 311b116

Browse files
committed
feat(assists): web添加onAccessibilityEvent监听支持
1 parent 8069e20 commit 311b116

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

assists-web/src/main/java/com/ven/assists/web/ASWebView.kt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,23 @@ package com.ven.assists.web
33
import android.annotation.SuppressLint
44
import android.content.Context
55
import android.util.AttributeSet
6+
import android.view.accessibility.AccessibilityEvent
67
import android.webkit.WebView
78
import android.webkit.WebViewClient
89
import android.webkit.WebChromeClient
10+
import com.blankj.utilcode.util.GsonUtils
11+
import com.blankj.utilcode.util.LogUtils
12+
import com.google.gson.JsonArray
13+
import com.google.gson.JsonObject
14+
import com.ven.assists.AssistsCore
15+
import com.ven.assists.service.AssistsService
16+
import com.ven.assists.service.AssistsServiceListener
17+
import com.ven.assists.utils.CoroutineWrapper
18+
import com.ven.assists.utils.runMain
919
import kotlinx.coroutines.CoroutineScope
1020
import kotlinx.coroutines.Dispatchers
21+
import kotlinx.coroutines.Job
22+
import kotlinx.coroutines.launch
1123

1224
@SuppressLint("SetJavaScriptEnabled")
1325
class ASWebView @JvmOverloads constructor(
@@ -27,6 +39,30 @@ class ASWebView @JvmOverloads constructor(
2739
javascriptInterface.callIntercept = value
2840
}
2941

42+
val assistsServiceListener = object : AssistsServiceListener {
43+
override fun onAccessibilityEvent(event: AccessibilityEvent) {
44+
runCatching {
45+
val node = event.source?.toNode()
46+
val jsonObject = JsonObject().apply {
47+
addProperty("packageName", event.packageName?.toString() ?: "")
48+
addProperty("className", event.className?.toString() ?: "")
49+
addProperty("eventType", event.eventType)
50+
addProperty("action", event.action)
51+
add("texts", JsonArray().apply {
52+
event.text.forEach { text -> this.add(text.toString()) }
53+
})
54+
node?.let {
55+
val element = GsonUtils.getGson().toJsonTree(node)
56+
add("node", element.asJsonObject)
57+
}
58+
}
59+
onAccessibilityEvent(CallResponse(code = 0, data = jsonObject))
60+
}.onFailure {
61+
LogUtils.e(it)
62+
}
63+
}
64+
}
65+
3066

3167
init {
3268
// 初始化WebView设置
@@ -70,5 +106,20 @@ class ASWebView @JvmOverloads constructor(
70106
isFocusableInTouchMode = true
71107
isFocusable = true
72108
addJavascriptInterface(javascriptInterface, "assistsx")
109+
AssistsService.listeners.add(assistsServiceListener)
110+
}
111+
112+
fun <T> onAccessibilityEvent(result: CallResponse<T>) {
113+
runCatching {
114+
val json = GsonUtils.toJson(result)
115+
evaluateJavascript("javascript:onAccessibilityEvent('${json}')", null)
116+
}.onFailure {
117+
LogUtils.e(it)
118+
}
119+
}
120+
121+
override fun onDetachedFromWindow() {
122+
super.onDetachedFromWindow()
123+
AssistsService.listeners.remove(assistsServiceListener)
73124
}
74125
}

assists-web/src/main/java/com/ven/assists/web/Node.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import java.util.UUID
99

1010
class Node(
1111
val nodeId: String,
12+
val packageName: String,
1213
val text: String,
1314
val des: String,
1415
val viewId: String,
@@ -48,6 +49,7 @@ fun AccessibilityNodeInfo.toNode(): Node {
4849
getBoundsInScreen(boundsInScreenRect)
4950

5051
val node = Node(
52+
packageName = packageName?.toString() ?: "",
5153
nodeId = NodeCacheManager.add(this),
5254
text = this.text?.toString() ?: "",
5355
des = this.contentDescription?.toString() ?: "",

assists/src/main/java/com/ven/assists/service/AssistsService.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import android.content.Intent
55
import android.view.accessibility.AccessibilityEvent
66
import com.blankj.utilcode.util.LogUtils
77
import com.ven.assists.AssistsCore
8+
import com.ven.assists.utils.CoroutineWrapper
89
import com.ven.assists.window.AssistsWindowManager
10+
import kotlinx.coroutines.channels.BufferOverflow
11+
import kotlinx.coroutines.flow.MutableSharedFlow
12+
import kotlinx.coroutines.flow.SharedFlow
913
import java.util.Collections
1014

1115
/**
@@ -29,6 +33,11 @@ open class AssistsService : AccessibilityService() {
2933
* 用于分发服务生命周期和无障碍事件
3034
*/
3135
val listeners: MutableList<AssistsServiceListener> = Collections.synchronizedList(arrayListOf<AssistsServiceListener>())
36+
37+
// val onAccessibilityEventFlow =
38+
// MutableSharedFlow<AccessibilityEvent>(replay = 0, extraBufferCapacity = 64, onBufferOverflow = BufferOverflow.DROP_OLDEST)
39+
// val onServiceConnectedFlow =
40+
// MutableSharedFlow<AssistsService>(replay = 0, extraBufferCapacity = 64, onBufferOverflow = BufferOverflow.DROP_OLDEST)
3241
}
3342

3443
/**

0 commit comments

Comments
 (0)