Fix calls on Huawei devices: skip addWebMessageListener on Chromium < 119 (#6640)
* Fix calls on Huawei: skip addWebMessageListener on Chromium < 119 * Fix lint issues, log webview version --------- Co-authored-by: manfrommedan <manfrommedan@users.noreply.github.com> Co-authored-by: Jorge Martín <jorgem@element.io>
This commit is contained in:
parent
5733c3149a
commit
27869c0e04
1 changed files with 21 additions and 14 deletions
|
|
@ -140,26 +140,33 @@ class WebViewWidgetMessageInterceptor(
|
|||
}
|
||||
}
|
||||
|
||||
// Create a WebMessageListener, which will receive messages from the WebView and reply to them
|
||||
val webMessageListener = WebViewCompat.WebMessageListener { _, message, _, _, _ ->
|
||||
onMessageReceived(message.data)
|
||||
}
|
||||
// Always register JavascriptInterface as the baseline message channel.
|
||||
// This works on all WebView implementations including Huawei.
|
||||
webView.addJavascriptInterface(object {
|
||||
@JavascriptInterface
|
||||
fun postMessage(json: String?) {
|
||||
onMessageReceived(json)
|
||||
}
|
||||
}, LISTENER_NAME)
|
||||
|
||||
// Use WebMessageListener if supported, otherwise use JavascriptInterface
|
||||
if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)) {
|
||||
// Additionally register WebMessageListener on WebViews that reliably support it.
|
||||
// Huawei WebView (Chromium < 119) reports WEB_MESSAGE_LISTENER as supported
|
||||
// but silently drops messages, so we only trust it on Chromium 119+.
|
||||
// See: https://github.com/element-hq/element-x-android/issues/6632
|
||||
val webViewVersionName = WebViewCompat.getCurrentWebViewPackage(webView.context)?.versionName.orEmpty()
|
||||
Timber.d("Using WebView version: $webViewVersionName")
|
||||
val webViewVersionCode = webViewVersionName.split(".").firstOrNull()?.toIntOrNull() ?: 0
|
||||
|
||||
if (webViewVersionCode >= 119 &&
|
||||
WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)) {
|
||||
WebViewCompat.addWebMessageListener(
|
||||
webView,
|
||||
LISTENER_NAME,
|
||||
setOf("*"),
|
||||
webMessageListener
|
||||
)
|
||||
} else {
|
||||
webView.addJavascriptInterface(object {
|
||||
@JavascriptInterface
|
||||
fun postMessage(json: String?) {
|
||||
onMessageReceived(json)
|
||||
WebViewCompat.WebMessageListener { _, message, _, _, _ ->
|
||||
onMessageReceived(message.data)
|
||||
}
|
||||
}, LISTENER_NAME)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue