Extract code that log a ConsoleMessage so that it can be reused.
This commit is contained in:
parent
82fd2866d7
commit
14c7a63f45
3 changed files with 77 additions and 31 deletions
|
|
@ -8,7 +8,6 @@
|
||||||
package io.element.android.features.call.impl.ui
|
package io.element.android.features.call.impl.ui
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.util.Log
|
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.webkit.ConsoleMessage
|
import android.webkit.ConsoleMessage
|
||||||
import android.webkit.JavascriptInterface
|
import android.webkit.JavascriptInterface
|
||||||
|
|
@ -60,6 +59,7 @@ interface CallScreenNavigator {
|
||||||
internal fun CallScreenView(
|
internal fun CallScreenView(
|
||||||
state: CallScreenState,
|
state: CallScreenState,
|
||||||
pipState: PictureInPictureState,
|
pipState: PictureInPictureState,
|
||||||
|
onConsoleMessage: (ConsoleMessage) -> Unit,
|
||||||
requestPermissions: (Array<String>, RequestPermissionCallback) -> Unit,
|
requestPermissions: (Array<String>, RequestPermissionCallback) -> Unit,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
) {
|
) {
|
||||||
|
|
@ -108,6 +108,7 @@ internal fun CallScreenView(
|
||||||
val callback: RequestPermissionCallback = { request.grant(it) }
|
val callback: RequestPermissionCallback = { request.grant(it) }
|
||||||
requestPermissions(androidPermissions.toTypedArray(), callback)
|
requestPermissions(androidPermissions.toTypedArray(), callback)
|
||||||
},
|
},
|
||||||
|
onConsoleMessage = onConsoleMessage,
|
||||||
onCreateWebView = { webView ->
|
onCreateWebView = { webView ->
|
||||||
webView.addBackHandler(onBackPressed = ::handleBack)
|
webView.addBackHandler(onBackPressed = ::handleBack)
|
||||||
val interceptor = WebViewWidgetMessageInterceptor(
|
val interceptor = WebViewWidgetMessageInterceptor(
|
||||||
|
|
@ -174,6 +175,7 @@ private fun CallWebView(
|
||||||
url: AsyncData<String>,
|
url: AsyncData<String>,
|
||||||
userAgent: String,
|
userAgent: String,
|
||||||
onPermissionsRequest: (PermissionRequest) -> Unit,
|
onPermissionsRequest: (PermissionRequest) -> Unit,
|
||||||
|
onConsoleMessage: (ConsoleMessage) -> Unit,
|
||||||
onCreateWebView: (WebView) -> Unit,
|
onCreateWebView: (WebView) -> Unit,
|
||||||
onDestroyWebView: (WebView) -> Unit,
|
onDestroyWebView: (WebView) -> Unit,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
|
|
@ -188,7 +190,11 @@ private fun CallWebView(
|
||||||
factory = { context ->
|
factory = { context ->
|
||||||
WebView(context).apply {
|
WebView(context).apply {
|
||||||
onCreateWebView(this)
|
onCreateWebView(this)
|
||||||
setup(userAgent, onPermissionsRequest)
|
setup(
|
||||||
|
userAgent = userAgent,
|
||||||
|
onPermissionsRequested = onPermissionsRequest,
|
||||||
|
onConsoleMessage = onConsoleMessage,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
update = { webView ->
|
update = { webView ->
|
||||||
|
|
@ -208,6 +214,7 @@ private fun CallWebView(
|
||||||
private fun WebView.setup(
|
private fun WebView.setup(
|
||||||
userAgent: String,
|
userAgent: String,
|
||||||
onPermissionsRequested: (PermissionRequest) -> Unit,
|
onPermissionsRequested: (PermissionRequest) -> Unit,
|
||||||
|
onConsoleMessage: (ConsoleMessage) -> Unit,
|
||||||
) {
|
) {
|
||||||
layoutParams = ViewGroup.LayoutParams(
|
layoutParams = ViewGroup.LayoutParams(
|
||||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||||
|
|
@ -232,35 +239,7 @@ private fun WebView.setup(
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onConsoleMessage(consoleMessage: ConsoleMessage): Boolean {
|
override fun onConsoleMessage(consoleMessage: ConsoleMessage): Boolean {
|
||||||
val priority = when (consoleMessage.messageLevel()) {
|
onConsoleMessage(consoleMessage)
|
||||||
ConsoleMessage.MessageLevel.ERROR -> Log.ERROR
|
|
||||||
ConsoleMessage.MessageLevel.WARNING -> Log.WARN
|
|
||||||
else -> Log.DEBUG
|
|
||||||
}
|
|
||||||
|
|
||||||
val message = buildString {
|
|
||||||
append(consoleMessage.sourceId())
|
|
||||||
append(":")
|
|
||||||
append(consoleMessage.lineNumber())
|
|
||||||
append(" ")
|
|
||||||
append(consoleMessage.message())
|
|
||||||
}
|
|
||||||
|
|
||||||
if (message.contains("password=")) {
|
|
||||||
// Avoid logging any messages that contain "password" to prevent leaking sensitive information
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
Timber.tag("WebView").log(
|
|
||||||
priority = priority,
|
|
||||||
message = buildString {
|
|
||||||
append(consoleMessage.sourceId())
|
|
||||||
append(":")
|
|
||||||
append(consoleMessage.lineNumber())
|
|
||||||
append(" ")
|
|
||||||
append(consoleMessage.message())
|
|
||||||
},
|
|
||||||
)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -286,6 +265,7 @@ internal fun CallScreenViewPreview(
|
||||||
state = state,
|
state = state,
|
||||||
pipState = aPictureInPictureState(),
|
pipState = aPictureInPictureState(),
|
||||||
requestPermissions = { _, _ -> },
|
requestPermissions = { _, _ -> },
|
||||||
|
onConsoleMessage = {},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,7 @@ import io.element.android.features.call.impl.pip.PipView
|
||||||
import io.element.android.features.call.impl.services.CallForegroundService
|
import io.element.android.features.call.impl.services.CallForegroundService
|
||||||
import io.element.android.features.call.impl.utils.CallIntentDataParser
|
import io.element.android.features.call.impl.utils.CallIntentDataParser
|
||||||
import io.element.android.features.enterprise.api.EnterpriseService
|
import io.element.android.features.enterprise.api.EnterpriseService
|
||||||
|
import io.element.android.libraries.androidutils.browser.ConsoleMessageLogger
|
||||||
import io.element.android.libraries.architecture.Presenter
|
import io.element.android.libraries.architecture.Presenter
|
||||||
import io.element.android.libraries.architecture.bindings
|
import io.element.android.libraries.architecture.bindings
|
||||||
import io.element.android.libraries.audio.api.AudioFocus
|
import io.element.android.libraries.audio.api.AudioFocus
|
||||||
|
|
@ -65,6 +66,7 @@ class ElementCallActivity :
|
||||||
@Inject lateinit var pictureInPicturePresenter: PictureInPicturePresenter
|
@Inject lateinit var pictureInPicturePresenter: PictureInPicturePresenter
|
||||||
@Inject lateinit var buildMeta: BuildMeta
|
@Inject lateinit var buildMeta: BuildMeta
|
||||||
@Inject lateinit var audioFocus: AudioFocus
|
@Inject lateinit var audioFocus: AudioFocus
|
||||||
|
@Inject lateinit var consoleMessageLogger: ConsoleMessageLogger
|
||||||
|
|
||||||
private lateinit var presenter: Presenter<CallScreenState>
|
private lateinit var presenter: Presenter<CallScreenState>
|
||||||
|
|
||||||
|
|
@ -119,6 +121,9 @@ class ElementCallActivity :
|
||||||
CallScreenView(
|
CallScreenView(
|
||||||
state = state,
|
state = state,
|
||||||
pipState = pipState,
|
pipState = pipState,
|
||||||
|
onConsoleMessage = {
|
||||||
|
consoleMessageLogger.log("ElementCall", it)
|
||||||
|
},
|
||||||
requestPermissions = { permissions, callback ->
|
requestPermissions = { permissions, callback ->
|
||||||
requestPermissionCallback = callback
|
requestPermissionCallback = callback
|
||||||
requestPermissionsLauncher.launch(permissions)
|
requestPermissionsLauncher.launch(permissions)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,61 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2025 New Vector Ltd.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||||
|
* Please see LICENSE files in the repository root for full details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.element.android.libraries.androidutils.browser
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
|
import android.webkit.ConsoleMessage
|
||||||
|
import dev.zacsweers.metro.AppScope
|
||||||
|
import dev.zacsweers.metro.ContributesBinding
|
||||||
|
import dev.zacsweers.metro.Inject
|
||||||
|
import timber.log.Timber
|
||||||
|
|
||||||
|
interface ConsoleMessageLogger {
|
||||||
|
fun log(
|
||||||
|
tag: String,
|
||||||
|
consoleMessage: ConsoleMessage,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@ContributesBinding(AppScope::class)
|
||||||
|
@Inject
|
||||||
|
class DefaultConsoleMessageLogger : ConsoleMessageLogger {
|
||||||
|
override fun log(
|
||||||
|
tag: String,
|
||||||
|
consoleMessage: ConsoleMessage,
|
||||||
|
) {
|
||||||
|
val priority = when (consoleMessage.messageLevel()) {
|
||||||
|
ConsoleMessage.MessageLevel.ERROR -> Log.ERROR
|
||||||
|
ConsoleMessage.MessageLevel.WARNING -> Log.WARN
|
||||||
|
else -> Log.DEBUG
|
||||||
|
}
|
||||||
|
|
||||||
|
val message = buildString {
|
||||||
|
append(consoleMessage.sourceId())
|
||||||
|
append(":")
|
||||||
|
append(consoleMessage.lineNumber())
|
||||||
|
append(" ")
|
||||||
|
append(consoleMessage.message())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Avoid logging any messages that contain "password" to prevent leaking sensitive information
|
||||||
|
if (message.contains("password=")) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
Timber.tag(tag).log(
|
||||||
|
priority = priority,
|
||||||
|
message = buildString {
|
||||||
|
append(consoleMessage.sourceId())
|
||||||
|
append(":")
|
||||||
|
append(consoleMessage.lineNumber())
|
||||||
|
append(" ")
|
||||||
|
append(consoleMessage.message())
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue