Modern Apps git

← Modern-Apps.git

web: only redirect into an app on fresh navigation (#672)

Commit
6b2d91cd4488e3055af11b86e00d71be40fbc4b8
Author
vayun-mathur <[email protected]>
When
2026-09-11 17:07 UTC (2 months ago)
Parents
c50f9b0

Diff

web/src/main/java/com/vayunmathur/web/platform/WebViewModel.kt
diff --git a/web/src/main/java/com/vayunmathur/web/platform/WebViewModel.kt b/web/src/main/java/com/vayunmathur/web/platform/WebViewModel.ktindex 80f3ac6678..d514de4147 100644--- a/web/src/main/java/com/vayunmathur/web/platform/WebViewModel.kt+++ b/web/src/main/java/com/vayunmathur/web/platform/WebViewModel.kt@@ -185,6 +185,14 @@ class WebViewModel(     private val tabCanGoForward = mutableMapOf<String, Boolean>()     private val tabCurrentUrl = mutableMapOf<String, String>() +    /**+     * Tabs whose content came back from the saved session and that nobody has navigated since.+     *+     * Only read and written from the main thread (the restore continuation and the WebView+     * client callbacks), so a plain set is enough.+     */+    private val restoredTabIds = mutableSetOf<String>()+     // PWA / installed site detection per tab     val pwaInfos = mutableStateMapOf<String, PwaInfo>() @@ -293,6 +301,8 @@ class WebViewModel(                                 activeTabId = activeId ?: tabs.firstOrNull()?.id                             }                         }+                        decodedSaved?.forEach { restoredTabIds.add(it.id) }+                         activeTab?.let {                             omniboxText = if (it.url.isBlank() || it.url == "about:blank") "" else it.url                             searchDraft = omniboxText@@ -533,10 +543,36 @@ class WebViewModel(         val active = activeTab ?: return         val dest = BrowserUtils.toNavigationUrl(input, searchEngine)         noteNavigation(dest)+        markFreshNavigation(active.id)         onTabUrlChange(active.id, dest)         omniboxFocused = false     } +    // ---- External app redirects ----++    /**+     * Records that the user just pointed [tabId] somewhere themselves — typed an address,+     * picked a bookmark, or reloaded — so the page it lands on is a fresh navigation again.+     */+    fun markFreshNavigation(tabId: String) {+        restoredTabIds.remove(tabId)+    }++    /**+     * Whether a navigation out of [tabId] into another app should be honoured.+     *+     * A restored tab is one the user never asked for on this launch, so a page that bounces+     * into another app would drag them straight back out of the browser they just opened. The+     * redirect is held back until they act in that tab; [userGesture] (a tap on the page) counts,+     * as does any fresh navigation.+     */+    fun allowExternalRedirect(tabId: String, userGesture: Boolean): Boolean {+        if (tabId !in restoredTabIds) return true+        if (!userGesture) return false+        restoredTabIds.remove(tabId)+        return true+    }+     // ---- Local network permission ----      /**
web/src/main/java/com/vayunmathur/web/ui/BrowserPage.kt
diff --git a/web/src/main/java/com/vayunmathur/web/ui/BrowserPage.kt b/web/src/main/java/com/vayunmathur/web/ui/BrowserPage.ktindex b0cae65c3c..6366e7346a 100644--- a/web/src/main/java/com/vayunmathur/web/ui/BrowserPage.kt+++ b/web/src/main/java/com/vayunmathur/web/ui/BrowserPage.kt@@ -391,7 +391,10 @@ fun BrowserPage(                                 text = { Text(stringResource(R.string.reload)) },                                 onClick = {                                     showMenu = false-                                    activeTab?.let { webViewPool[it.id]?.reload() }+                                    activeTab?.let {+                                        viewModel.markFreshNavigation(it.id)+                                        webViewPool[it.id]?.reload()+                                    }                                 }                             )                         }@@ -449,7 +452,12 @@ fun BrowserPage(                         QuickAccess(                             bookmarks = bookmarks.take(12),                             history = history.take(8),-                            onOpenUrl = { url -> activeTab?.let { viewModel.onTabUrlChange(it.id, url) } },+                            onOpenUrl = { url ->+                                activeTab?.let {+                                    viewModel.markFreshNavigation(it.id)+                                    viewModel.onTabUrlChange(it.id, url)+                                }+                            },                             faviconFor = viewModel::faviconFor,                             modifier = Modifier.fillMaxSize()                         )@@ -514,6 +522,7 @@ fun BrowserPage(                             // only apply to documents that start loading after the call.                             (webView.webViewClient as? ShieldsWebViewClient)                                 ?.installFarbling(webView, viewModel.farblingConfig())+                            viewModel.markFreshNavigation(tab.id)                             webView.reload()                         }                     }
web/src/main/java/com/vayunmathur/web/ui/WebViewBrowser.kt
diff --git a/web/src/main/java/com/vayunmathur/web/ui/WebViewBrowser.kt b/web/src/main/java/com/vayunmathur/web/ui/WebViewBrowser.ktindex 7f0e53eb26..c01c6c98a3 100644--- a/web/src/main/java/com/vayunmathur/web/ui/WebViewBrowser.kt+++ b/web/src/main/java/com/vayunmathur/web/ui/WebViewBrowser.kt@@ -224,6 +224,10 @@ fun WebViewBrowser(                     override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {                         val scheme = request.url.scheme ?: return false                         if (scheme !in setOf("http", "https", "about", "data", "blob", "javascript")) {+                            // Swallowed rather than passed on when the tab is only being+                            // restored: the page still renders, it just doesn't throw the+                            // user back into the app they last left.+                            if (!viewModel.allowExternalRedirect(tabId, request.hasGesture())) return true                             return com.vayunmathur.web.platform.openExternalUri(                                 ctx,                                 request.url.toString(),