From 5304e612d5e02b51605e98eccf7916a9da635e4a Mon Sep 17 00:00:00 2001 From: Allegra Date: Wed, 22 May 2024 13:38:38 -0400 Subject: [PATCH 1/3] Override WebClient url loading for external links --- .../mobile/webapp/JellyfinWebViewClient.kt | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/app/src/main/java/org/jellyfin/mobile/webapp/JellyfinWebViewClient.kt b/app/src/main/java/org/jellyfin/mobile/webapp/JellyfinWebViewClient.kt index 1cea01d46..5cd82b140 100644 --- a/app/src/main/java/org/jellyfin/mobile/webapp/JellyfinWebViewClient.kt +++ b/app/src/main/java/org/jellyfin/mobile/webapp/JellyfinWebViewClient.kt @@ -1,11 +1,13 @@ package org.jellyfin.mobile.webapp +import android.content.Intent import android.net.Uri import android.net.http.SslError import android.webkit.SslErrorHandler import android.webkit.WebResourceRequest import android.webkit.WebResourceResponse import android.webkit.WebView +import androidx.core.content.ContextCompat.startActivity import androidx.webkit.WebResourceErrorCompat import androidx.webkit.WebViewAssetLoader.AssetsPathHandler import androidx.webkit.WebViewClientCompat @@ -37,6 +39,25 @@ abstract class JellyfinWebViewClient( abstract fun onErrorReceived() + override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean { + val url = request.url + val path = url.path?.lowercase(Locale.ROOT) ?: "" + return when { + path.matches(Constants.MAIN_BUNDLE_PATH_REGEX) && "deferred" !in url.query.orEmpty() -> false + path.contains("/native/") -> false + path.endsWith(Constants.CAST_SDK_PATH) -> false + path.endsWith(Constants.SESSION_CAPABILITIES_PATH) -> false + url.toString().contains(server.hostname) -> false + else -> { + val intent = Intent(Intent.ACTION_VIEW, url) + if (intent.resolveActivity(view.context.packageManager) != null) { + startActivity(view.context, intent, null) + } + true + } + } + } + override fun shouldInterceptRequest(webView: WebView, request: WebResourceRequest): WebResourceResponse? { val url = request.url val path = url.path?.lowercase(Locale.ROOT) ?: return null From f707d324bc213bcb53f13ec90408e9171abb056b Mon Sep 17 00:00:00 2001 From: Allegra Date: Thu, 23 May 2024 23:16:37 -0400 Subject: [PATCH 2/3] Simplify url load overriding to use the system for any external link --- .../org/jellyfin/mobile/webapp/JellyfinWebViewClient.kt | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/jellyfin/mobile/webapp/JellyfinWebViewClient.kt b/app/src/main/java/org/jellyfin/mobile/webapp/JellyfinWebViewClient.kt index 5cd82b140..74bfd9f2b 100644 --- a/app/src/main/java/org/jellyfin/mobile/webapp/JellyfinWebViewClient.kt +++ b/app/src/main/java/org/jellyfin/mobile/webapp/JellyfinWebViewClient.kt @@ -40,15 +40,12 @@ abstract class JellyfinWebViewClient( abstract fun onErrorReceived() override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean { + Timber.d("shouldOverrideUrlLoading: %s", request.url.toString()) val url = request.url - val path = url.path?.lowercase(Locale.ROOT) ?: "" return when { - path.matches(Constants.MAIN_BUNDLE_PATH_REGEX) && "deferred" !in url.query.orEmpty() -> false - path.contains("/native/") -> false - path.endsWith(Constants.CAST_SDK_PATH) -> false - path.endsWith(Constants.SESSION_CAPABILITIES_PATH) -> false - url.toString().contains(server.hostname) -> false + url.toString().startsWith(server.hostname) -> false else -> { + Timber.d("shouldOverrideUrlLoading: external link, handle with system") val intent = Intent(Intent.ACTION_VIEW, url) if (intent.resolveActivity(view.context.packageManager) != null) { startActivity(view.context, intent, null) From 9b945c34c878f2b56e1e1fba3e3d02b354f5dae5 Mon Sep 17 00:00:00 2001 From: Allegra Date: Fri, 24 May 2024 00:19:47 -0400 Subject: [PATCH 3/3] Add check for gesture before handling url externally --- .../org/jellyfin/mobile/webapp/JellyfinWebViewClient.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/jellyfin/mobile/webapp/JellyfinWebViewClient.kt b/app/src/main/java/org/jellyfin/mobile/webapp/JellyfinWebViewClient.kt index 74bfd9f2b..93292192e 100644 --- a/app/src/main/java/org/jellyfin/mobile/webapp/JellyfinWebViewClient.kt +++ b/app/src/main/java/org/jellyfin/mobile/webapp/JellyfinWebViewClient.kt @@ -41,12 +41,12 @@ abstract class JellyfinWebViewClient( override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean { Timber.d("shouldOverrideUrlLoading: %s", request.url.toString()) - val url = request.url return when { - url.toString().startsWith(server.hostname) -> false + !request.hasGesture() -> false + request.url.toString().startsWith(server.hostname) -> false else -> { - Timber.d("shouldOverrideUrlLoading: external link, handle with system") - val intent = Intent(Intent.ACTION_VIEW, url) + Timber.d("shouldOverrideUrlLoading: external link with gesture, handle with system") + val intent = Intent(Intent.ACTION_VIEW, request.url) if (intent.resolveActivity(view.context.packageManager) != null) { startActivity(view.context, intent, null) }