Skip to content

Commit 6610b68

Browse files
committed
feat(assists-web): 添加浮窗功能并优化 JavaScript 接口
- 在 ASJavascriptInterface 中添加 callIntercept 属性,用于拦截和处理特定的 JavaScript 调用 - 在 AssistsWindowManager 中实现浮窗的添加、显示和隐藏等功能 - 在 ASWebView 中集成 JavaScript 接口,并支持浮窗功能 - 新增 CallInterceptResult 数据类,用于定义拦截结果 - 更新 CallMethod枚举,添加新的浮窗相关方法 - 添加 WebFloatingWindow布局文件,用于浮窗视图
1 parent 55aac08 commit 6610b68

File tree

7 files changed

+145
-63
lines changed

7 files changed

+145
-63
lines changed

assists-web/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ android {
2828
sourceCompatibility JavaVersion.VERSION_17
2929
targetCompatibility JavaVersion.VERSION_17
3030
}
31+
32+
viewBinding {
33+
enabled = true
34+
}
3135
}
3236

3337
dependencies {

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

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.graphics.Bitmap
44
import android.graphics.Rect
55
import android.os.Build
66
import android.util.Base64
7+
import android.view.LayoutInflater
78
import android.webkit.JavascriptInterface
89
import android.webkit.WebView
910
import com.blankj.utilcode.util.GsonUtils
@@ -36,17 +37,21 @@ import com.ven.assists.AssistsCore.setNodeText
3637
import com.ven.assists.AssistsCore.takeScreenshot
3738
import com.ven.assists.mp.MPManager
3839
import com.ven.assists.mp.MPManager.getBitmap
40+
import com.ven.assists.service.AssistsService
3941
import com.ven.assists.utils.CoroutineWrapper
42+
import com.ven.assists.utils.runIO
4043
import com.ven.assists.window.AssistsWindowManager
4144
import com.ven.assists.window.AssistsWindowManager.overlayToast
45+
import com.ven.assists.window.AssistsWindowWrapper
46+
import com.ven.assists_web.databinding.WebFloatingWindowBinding
4247
import kotlinx.coroutines.CoroutineScope
4348
import kotlinx.coroutines.Dispatchers
4449
import kotlinx.coroutines.delay
4550
import kotlinx.coroutines.launch
4651
import java.io.ByteArrayOutputStream
4752

48-
4953
class ASJavascriptInterface(val webView: WebView) {
54+
var callIntercept: ((json: String) -> CallInterceptResult)? = null
5055
private val coroutineScope = CoroutineScope(Dispatchers.Main)
5156

5257
fun <T> callback(result: CallResponse<T>) {
@@ -61,11 +66,54 @@ class ASJavascriptInterface(val webView: WebView) {
6166
}
6267

6368
@JavascriptInterface
64-
fun call(json: String): String {
69+
fun call(originJson: String): String {
70+
var requestJson = originJson
71+
72+
runCatching {
73+
callIntercept?.invoke(originJson)?.let {
74+
if (it.intercept) {
75+
return it.result
76+
} else {
77+
requestJson = it.result
78+
}
79+
}
80+
}.onFailure { LogUtils.e(it) }
81+
6582
var result = GsonUtils.toJson(CallResponse<Any>(code = -1))
6683
runCatching {
67-
val request = GsonUtils.fromJson<CallRequest<JsonObject>>(json, object : TypeToken<CallRequest<JsonObject>>() {}.type)
84+
val request = GsonUtils.fromJson<CallRequest<JsonObject>>(requestJson, object : TypeToken<CallRequest<JsonObject>>() {}.type)
6885
when (request.method) {
86+
CallMethod.addWebFloatingWindow -> {
87+
CoroutineWrapper.launch(isMain = true) {
88+
val url = request.arguments?.get("url")?.asString ?: ""
89+
val initialWidth = request.arguments?.get("initialWidth")?.asInt ?: (ScreenUtils.getScreenWidth() * 0.8).toInt()
90+
val initialHeight = request.arguments?.get("initialHeight")?.asInt ?: (ScreenUtils.getScreenHeight() * 0.5).toInt()
91+
val minWidth = request.arguments?.get("minWidth")?.asInt ?: (ScreenUtils.getScreenHeight() * 0.5).toInt()
92+
val minHeight = request.arguments?.get("minHeight")?.asInt ?: (ScreenUtils.getScreenHeight() * 0.5).toInt()
93+
val initialCenter = request.arguments?.get("initialCenter")?.asBoolean ?: true
94+
95+
AssistsWindowManager.add(
96+
windowWrapper = AssistsWindowWrapper(
97+
wmLayoutParams = AssistsWindowManager.createLayoutParams().apply {
98+
width = initialWidth
99+
height = initialHeight
100+
},
101+
view = WebFloatingWindowBinding.inflate(LayoutInflater.from(AssistsService.instance)).apply {
102+
webView.loadUrl(url)
103+
}.root
104+
).apply {
105+
this.minWidth = minWidth
106+
this.minHeight = minHeight
107+
this.initialCenter = initialCenter
108+
}
109+
)
110+
}
111+
112+
result = GsonUtils.toJson(CallResponse<JsonObject>(code = 0, data = JsonObject().apply {
113+
addProperty("resultType", "callback")
114+
}))
115+
}
116+
69117
CallMethod.scanQR -> {
70118
CoroutineWrapper.launch {
71119
val scanIntentResult = CustomFileProvider.requestLaunchersScan(ScanOptions())

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ class ASWebView @JvmOverloads constructor(
1919
private val coroutineScope = CoroutineScope(Dispatchers.Main)
2020

2121
var onReceivedTitle: ((title: String) -> Unit)? = null
22+
val javascriptInterface = ASJavascriptInterface(webView = this)
23+
24+
var callIntercept: ((json: String) -> CallInterceptResult)? = null
25+
set(value) {
26+
field = value
27+
javascriptInterface.callIntercept = value
28+
}
29+
2230

2331
init {
2432
// 初始化WebView设置
@@ -36,8 +44,8 @@ class ASWebView @JvmOverloads constructor(
3644
loadWithOverviewMode = true
3745
allowUniversalAccessFromFileURLs = true
3846
allowFileAccessFromFileURLs = true
39-
domStorageEnabled=true
40-
databaseEnabled=true
47+
domStorageEnabled = true
48+
databaseEnabled = true
4149
setWebContentsDebuggingEnabled(true)
4250
}
4351

@@ -61,7 +69,6 @@ class ASWebView @JvmOverloads constructor(
6169
requestFocus()
6270
isFocusableInTouchMode = true
6371
isFocusable = true
64-
65-
addJavascriptInterface(ASJavascriptInterface(this), "assistsx")
72+
addJavascriptInterface(javascriptInterface, "assistsx")
6673
}
6774
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.ven.assists.web
2+
3+
data class CallInterceptResult(val intercept: Boolean, val result: String)
Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
11
package com.ven.assists.web
22

33
object CallMethod {
4-
const val setNodeText="setNodeText"
5-
const val findByTags="findByTags"
6-
const val findById="findById"
7-
const val findByText="findByText"
8-
const val findByTextAllMatch="findByTextAllMatch"
9-
const val containsText="containsText"
10-
const val getAllText="getAllText"
11-
const val findFirstParentByTags="findFirstParentByTags"
12-
const val getAllNodes="getAllNodes"
13-
const val getNodes="getNodes"
14-
const val findFirstParentClickable="findFirstParentClickable"
15-
const val getChildren="getChildren"
16-
const val dispatchGesture="dispatchGesture"
17-
const val getBoundsInScreen="getBoundsInScreen"
18-
const val getBoundsInParent="getBoundsInParent"
19-
const val isVisible="isVisible"
20-
const val click="click"
21-
const val longClick="longClick"
22-
const val gestureClick="gestureClick"
23-
const val back="back"
24-
const val home="home"
25-
const val notifications="notifications"
26-
const val recentApps="recentApps"
27-
const val paste="paste"
28-
const val selectionText="selectionText"
29-
const val scrollForward="scrollForward"
30-
const val scrollBackward="scrollBackward"
31-
const val launchApp="launchApp"
32-
const val getPackageName="getPackageName"
33-
const val overlayToast="overlayToast"
34-
const val getScreenSize="getScreenSize"
35-
const val getAppScreenSize="getAppScreenSize"
36-
const val nodeGestureClick="nodeGestureClick"
37-
const val nodeGestureClickByDouble="nodeGestureClickByDouble"
38-
const val takeScreenshot="takeScreenshot"
39-
const val setOverlayFlags="setOverlayFlags"
40-
const val scanQR="scanQR"
4+
const val setNodeText = "setNodeText"
5+
const val findByTags = "findByTags"
6+
const val findById = "findById"
7+
const val findByText = "findByText"
8+
const val findByTextAllMatch = "findByTextAllMatch"
9+
const val containsText = "containsText"
10+
const val getAllText = "getAllText"
11+
const val findFirstParentByTags = "findFirstParentByTags"
12+
const val getAllNodes = "getAllNodes"
13+
const val getNodes = "getNodes"
14+
const val findFirstParentClickable = "findFirstParentClickable"
15+
const val getChildren = "getChildren"
16+
const val dispatchGesture = "dispatchGesture"
17+
const val getBoundsInScreen = "getBoundsInScreen"
18+
const val getBoundsInParent = "getBoundsInParent"
19+
const val isVisible = "isVisible"
20+
const val click = "click"
21+
const val longClick = "longClick"
22+
const val gestureClick = "gestureClick"
23+
const val back = "back"
24+
const val home = "home"
25+
const val notifications = "notifications"
26+
const val recentApps = "recentApps"
27+
const val paste = "paste"
28+
const val selectionText = "selectionText"
29+
const val scrollForward = "scrollForward"
30+
const val scrollBackward = "scrollBackward"
31+
const val launchApp = "launchApp"
32+
const val getPackageName = "getPackageName"
33+
const val overlayToast = "overlayToast"
34+
const val getScreenSize = "getScreenSize"
35+
const val getAppScreenSize = "getAppScreenSize"
36+
const val nodeGestureClick = "nodeGestureClick"
37+
const val nodeGestureClickByDouble = "nodeGestureClickByDouble"
38+
const val takeScreenshot = "takeScreenshot"
39+
const val setOverlayFlags = "setOverlayFlags"
40+
const val scanQR = "scanQR"
41+
const val addWebFloatingWindow = "addWebFloatingWindow"
42+
const val getLocalDomain = "getLocalDomain"
4143
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:orientation="vertical"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent">
6+
7+
<com.ven.assists.web.ASWebView
8+
android:id="@+id/web_view"
9+
android:layout_width="match_parent"
10+
android:layout_height="match_parent"/>
11+
12+
</LinearLayout>

0 commit comments

Comments
 (0)