From 0a44cd300bc67e7b751e06a72ae04f302e913dba Mon Sep 17 00:00:00 2001 From: Vincent TE Date: Wed, 2 Oct 2024 15:31:40 +0200 Subject: [PATCH] Add the possibility to specify an url to quit the webview automatically and let the caller activity know we reach that specific url --- .../infomaniak/lib/core/ui/WebViewActivity.kt | 43 ++++++++++++++++--- .../res/navigation/webview_navigation.xml | 7 ++- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/infomaniak/lib/core/ui/WebViewActivity.kt b/src/main/java/com/infomaniak/lib/core/ui/WebViewActivity.kt index 740b1b0f..b8c7298a 100644 --- a/src/main/java/com/infomaniak/lib/core/ui/WebViewActivity.kt +++ b/src/main/java/com/infomaniak/lib/core/ui/WebViewActivity.kt @@ -21,7 +21,10 @@ import android.annotation.SuppressLint import android.content.Context import android.content.Intent import android.os.Bundle +import android.webkit.WebResourceRequest +import android.webkit.WebView import android.webkit.WebViewClient +import androidx.activity.result.ActivityResultLauncher import androidx.appcompat.app.AppCompatActivity import androidx.navigation.navArgs import com.infomaniak.lib.core.databinding.ActivityWebviewBinding @@ -38,19 +41,49 @@ class WebViewActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(binding.root) binding.webview.apply { - webViewClient = WebViewClient() + webViewClient = CustomWebViewClient(navArgs.urlToQuit, onUrlToQuitReached = { + setResult(RESULT_OK) + finish() + }) settings.javaScriptEnabled = true val headers = navArgs.headers?.let { Json.decodeFromString>(it) } ?: mapOf() loadUrl(navArgs.url, headers) } } + private class CustomWebViewClient( + private val urlToQuit: String?, + private val onUrlToQuitReached: () -> Unit, + ) : WebViewClient() { + + override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest?): Boolean { + return if (urlToQuit != null && request?.url?.toString()?.contains(urlToQuit) == true) { + onUrlToQuitReached() + true + } else { + super.shouldOverrideUrlLoading(view, request) + } + } + } + companion object { - fun startActivity(context: Context, url: String, headers: Map? = mapOf()) { - Intent(context, WebViewActivity::class.java).apply { - putExtras(WebViewActivityArgs(url, Json.encodeToString(headers)).toBundle()) - }.also(context::startActivity) + fun startActivity( + context: Context, + url: String, + headers: Map? = mapOf(), + urlToQuit: String? = null, + activityResultLauncher: ActivityResultLauncher? = null, + ) { + val intent = Intent(context, WebViewActivity::class.java).apply { + val webViewActivityArgs = WebViewActivityArgs(url, Json.encodeToString(headers), urlToQuit) + putExtras(webViewActivityArgs.toBundle()) + } + activityResultLauncher?.let { + activityResultLauncher.launch(intent) + } ?: run { + context.startActivity(intent) + } } } } diff --git a/src/main/res/navigation/webview_navigation.xml b/src/main/res/navigation/webview_navigation.xml index 8694d1d0..e877ffa1 100644 --- a/src/main/res/navigation/webview_navigation.xml +++ b/src/main/res/navigation/webview_navigation.xml @@ -29,13 +29,18 @@ + app:argType="string"/> + +