feat: webapp multi-tab JS bridge and multi-WebView management
This commit is contained in:
@@ -2,17 +2,44 @@ package top.yeij.hearth.webview
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.graphics.Color
|
||||
import android.view.ViewGroup
|
||||
import android.webkit.WebView
|
||||
import android.webkit.WebSettings
|
||||
import android.webkit.WebViewClient
|
||||
|
||||
// WebView 工厂:统一内核配置,桌面层挂到指定父容器,内容层按需创建
|
||||
// WebView factory: unified kernel config; the desktop layer attaches to a given
|
||||
// parent container, content layers are created on demand
|
||||
class WebViewManager(private val activity: Activity) {
|
||||
// 创建桌面 WebView(底层 H5)挂到 parent,加载内置 index.html
|
||||
// Create the desktop WebView (bottom H5 layer) under parent, load bundled index.html
|
||||
@SuppressLint("SetJavaScriptEnabled")
|
||||
fun attach(bridge: JsBridge): WebView {
|
||||
fun attach(bridge: JsBridge, parent: ViewGroup): WebView {
|
||||
val webView = WebView(activity)
|
||||
// 透明背景,露出原生壁纸
|
||||
// Transparent background to reveal native wallpaper
|
||||
webView.setBackgroundColor(android.graphics.Color.TRANSPARENT)
|
||||
webView.setBackgroundColor(Color.TRANSPARENT)
|
||||
configure(webView)
|
||||
webView.addJavascriptInterface(bridge, "HearthBridge")
|
||||
webView.webViewClient = WebViewClient()
|
||||
webView.loadUrl("file:///android_asset/h5/index.html")
|
||||
parent.addView(webView, ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
|
||||
return webView
|
||||
}
|
||||
|
||||
// 创建内容 WebView(webAPP 标签页),仅配置不加载、不挂载(由宿主管理)
|
||||
// Create a content WebView (webapp tab), configured but not loaded/attached
|
||||
// (lifecycle is managed by the host)
|
||||
@SuppressLint("SetJavaScriptEnabled")
|
||||
fun createContentWebView(): WebView {
|
||||
val webView = WebView(activity)
|
||||
webView.setBackgroundColor(Color.WHITE)
|
||||
configure(webView)
|
||||
return webView
|
||||
}
|
||||
|
||||
// 统一内核配置:JS、宽视口、DOM 存储,禁用缩放
|
||||
// Common kernel config: JS, wide viewport, DOM storage, zoom disabled
|
||||
@SuppressLint("SetJavaScriptEnabled")
|
||||
private fun configure(webView: WebView) {
|
||||
webView.settings.apply {
|
||||
javaScriptEnabled = true
|
||||
useWideViewPort = true
|
||||
@@ -20,10 +47,5 @@ class WebViewManager(private val activity: Activity) {
|
||||
setSupportZoom(false)
|
||||
domStorageEnabled = true
|
||||
}
|
||||
webView.addJavascriptInterface(bridge, "HearthBridge")
|
||||
webView.webViewClient = WebViewClient()
|
||||
webView.loadUrl("file:///android_asset/h5/index.html")
|
||||
activity.setContentView(webView)
|
||||
return webView
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user