Modern Apps git

← Modern-Apps.git

web: HTML5 fullscreen video in browser and PWAs

Commit
4c7730989a5193e0097d230bd458cf759f18ce95
Author
vayun-mathur <[email protected]>
When
2026-09-29 23:25 UTC (2 months ago)
Parents
4c8568f

Diff

web/src/main/java/com/vayunmathur/web/platform/PwaActivity.kt
diff --git a/web/src/main/java/com/vayunmathur/web/platform/PwaActivity.kt b/web/src/main/java/com/vayunmathur/web/platform/PwaActivity.ktindex eaf94f841d..1669c44222 100644--- a/web/src/main/java/com/vayunmathur/web/platform/PwaActivity.kt+++ b/web/src/main/java/com/vayunmathur/web/platform/PwaActivity.kt@@ -50,6 +50,7 @@ import com.vayunmathur.web.platform.shields.ShieldsEngine import com.vayunmathur.web.platform.shields.ShieldsServiceWorkerClient import com.vayunmathur.web.platform.shields.ShieldsWebViewClient import com.vayunmathur.web.ui.LinkContextMenu+import com.vayunmathur.web.ui.WebFullscreenHost import com.vayunmathur.web.ui.applySystemDarkMode import com.vayunmathur.web.ui.linkUrlFromHitTest import com.vayunmathur.web.domain.EffectiveShields@@ -155,6 +156,7 @@ private fun PwaBrowser(     var currentTitle by remember { mutableStateOf(title ?: "") }     var webViewRef by remember { mutableStateOf<WebView?>(null) }     var linkMenuUrl by remember { mutableStateOf<String?>(null) }+    val fullscreenHost = remember(context) { WebFullscreenHost(context) }      // For permission handling     var pendingSysPermissionRequest by remember { mutableStateOf<PermissionRequest?>(null) }@@ -231,10 +233,10 @@ private fun PwaBrowser(      BackHandler {         val wv = webViewRef-        if (wv != null && wv.canGoBack()) {-            wv.goBack()-        } else {-            activity?.finish()+        when {+            fullscreenHost.isFullscreen -> fullscreenHost.onHideCustomView()+            wv != null && wv.canGoBack() -> wv.goBack()+            else -> activity?.finish()         }     } @@ -350,6 +352,14 @@ private fun PwaBrowser(                             if (!t.isNullOrBlank()) currentTitle = t                         } +                        override fun onShowCustomView(view: android.view.View, callback: CustomViewCallback) {+                            fullscreenHost.onShowCustomView(view, callback)+                        }++                        override fun onHideCustomView() {+                            if (!fullscreenHost.onHideCustomView()) super.onHideCustomView()+                        }+                         override fun onGeolocationPermissionsShowPrompt(                             origin: String,                             callback: GeolocationPermissions.Callback
web/src/main/java/com/vayunmathur/web/ui/BrowserOverlays.kt
diff --git a/web/src/main/java/com/vayunmathur/web/ui/BrowserOverlays.kt b/web/src/main/java/com/vayunmathur/web/ui/BrowserOverlays.ktindex 45e2d0eafe..3b8e14f77e 100644--- a/web/src/main/java/com/vayunmathur/web/ui/BrowserOverlays.kt+++ b/web/src/main/java/com/vayunmathur/web/ui/BrowserOverlays.kt@@ -194,6 +194,7 @@ internal fun BrowserContent(     activeTab: BrowserTab?,     isNewTabActive: Boolean,     webViewPool: MutableMap<String, WebView>,+    fullscreenHost: WebFullscreenHost,     onOpenUrl: (BrowserTab, String) -> Unit,     onRequestNewTab: (BrowserTab, String) -> Unit,     onLinkLongPress: (String) -> Unit,@@ -227,6 +228,7 @@ internal fun BrowserContent(                         initialUrl = activeTab.url,                         viewModel = viewModel,                         webViewPool = webViewPool,+                        fullscreenHost = fullscreenHost,                         onRequestNewTab = { url -> onRequestNewTab(activeTab, url) },                         onLinkLongPress = onLinkLongPress,                         modifier = Modifier.fillMaxSize()
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 be262ea8d6..ed5dd75d95 100644--- a/web/src/main/java/com/vayunmathur/web/ui/BrowserPage.kt+++ b/web/src/main/java/com/vayunmathur/web/ui/BrowserPage.kt@@ -86,13 +86,16 @@ fun BrowserPage(     var showInstallDialog by remember { mutableStateOf(false) }     var linkContextMenuUrl by remember { mutableStateOf<String?>(null) }     val searchFocusRequester = remember { FocusRequester() }+    // Activity-wide: fullscreen video survives tab switches until dismissed.+    val fullscreenHost = remember(context) { WebFullscreenHost(context) } -    BackHandler(enabled = viewModel.showTabSwitcher) { viewModel.showTabSwitcher = false }-    BackHandler(enabled = !viewModel.showTabSwitcher && viewModel.omniboxFocused) {+    BackHandler(enabled = fullscreenHost.isFullscreen) { fullscreenHost.onHideCustomView() }+    BackHandler(enabled = !fullscreenHost.isFullscreen && viewModel.showTabSwitcher) { viewModel.showTabSwitcher = false }+    BackHandler(enabled = !fullscreenHost.isFullscreen && !viewModel.showTabSwitcher && viewModel.omniboxFocused) {         viewModel.omniboxFocused = false         focusManager.clearFocus()     }-    BackHandler(enabled = !viewModel.showTabSwitcher && !viewModel.omniboxFocused && canGoBack) {+    BackHandler(enabled = !fullscreenHost.isFullscreen && !viewModel.showTabSwitcher && !viewModel.omniboxFocused && canGoBack) {         activeTab?.let { tab -> webViewPool[tab.id]?.goBack() }     } @@ -172,6 +175,7 @@ fun BrowserPage(                     activeTab = activeTab,                     isNewTabActive = isNewTabActive,                     webViewPool = webViewPool,+                    fullscreenHost = fullscreenHost,                     onOpenUrl = { tab, url ->                         viewModel.markFreshNavigation(tab.id)                         viewModel.onTabUrlChange(tab.id, url)
web/src/main/java/com/vayunmathur/web/ui/WebFullscreenHost.kt
diff --git a/web/src/main/java/com/vayunmathur/web/ui/WebFullscreenHost.kt b/web/src/main/java/com/vayunmathur/web/ui/WebFullscreenHost.ktnew file mode 100644index 0000000000..fbc041f0d0--- /dev/null+++ b/web/src/main/java/com/vayunmathur/web/ui/WebFullscreenHost.kt@@ -0,0 +1,68 @@+package com.vayunmathur.web.ui++import android.app.Activity+import android.content.pm.ActivityInfo+import android.graphics.Color+import android.view.View+import android.view.ViewGroup+import android.webkit.WebChromeClient+import android.widget.FrameLayout+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.setValue+import com.vayunmathur.library.ui.findActivity++/**+ * Hosts HTML5 fullscreen video (e.g. YouTube fullscreen) for a WebView.+ *+ * Without [WebChromeClient.onShowCustomView] the page's fullscreen request dies+ * silently. The host attaches the video view to the activity decor with a black+ * background, and detaches it when the page exits fullscreen. Compose-observable+ * [isFullscreen] lets back-press handlers exit fullscreen first.+ */+class WebFullscreenHost(private val activityFrom: () -> Activity?) {+    var isFullscreen by mutableStateOf(false)+        private set++    private var customView: View? = null+    private var customViewCallback: WebChromeClient.CustomViewCallback? = null+    private var previousOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED++    fun onShowCustomView(view: View, callback: WebChromeClient.CustomViewCallback) {+        val activity = activityFrom() ?: return+        if (isFullscreen) {+            callback.onCustomViewHidden()+            return+        }+        previousOrientation = activity.requestedOrientation+        customView = view+        customViewCallback = callback+        view.setBackgroundColor(Color.BLACK)+        (activity.window.decorView as? ViewGroup)?.addView(+            view,+            FrameLayout.LayoutParams(+                ViewGroup.LayoutParams.MATCH_PARENT,+                ViewGroup.LayoutParams.MATCH_PARENT,+            ),+        )+        isFullscreen = true+    }++    fun onHideCustomView(): Boolean {+        val view = customView ?: return false+        val activity = activityFrom()+        (activity?.window?.decorView as? ViewGroup)?.removeView(view)+        customView = null+        try {+            customViewCallback?.onCustomViewHidden()+        } catch (_: Exception) {}+        customViewCallback = null+        activity?.let { it.requestedOrientation = previousOrientation }+        isFullscreen = false+        return true+    }+}++/** Creates a host bound to the activity behind [android.content.Context]. */+fun WebFullscreenHost(context: android.content.Context): WebFullscreenHost =+    WebFullscreenHost { context.findActivity() }
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 2f9fa32b42..4d19f23eab 100644--- a/web/src/main/java/com/vayunmathur/web/ui/WebViewBrowser.kt+++ b/web/src/main/java/com/vayunmathur/web/ui/WebViewBrowser.kt@@ -53,6 +53,7 @@ fun WebViewBrowser(     modifier: Modifier = Modifier,     onRequestNewTab: (String) -> Unit = {},     webViewPool: MutableMap<String, WebView>,+    fullscreenHost: WebFullscreenHost? = null,     onLinkLongPress: (String) -> Unit = {}, ) {     val context = LocalContext.current@@ -208,7 +209,7 @@ fun WebViewBrowser(                 })                  webViewClient = createBrowserWebViewClient(ctx, tabId, viewModel, isPrivateTab)-                webChromeClient = createBrowserWebChromeClient(tabId, viewModel, isPrivateTab, clientDeps, onRequestNewTab)+                webChromeClient = createBrowserWebChromeClient(tabId, viewModel, isPrivateTab, clientDeps, onRequestNewTab, fullscreenHost)                  val toLoad = if (initialUrl.isBlank()) "about:blank" else initialUrl                 // Before the first load: document-start scripts do not apply retroactively.
web/src/main/java/com/vayunmathur/web/ui/WebViewClients.kt
diff --git a/web/src/main/java/com/vayunmathur/web/ui/WebViewClients.kt b/web/src/main/java/com/vayunmathur/web/ui/WebViewClients.ktindex 93f661b441..3317ef906f 100644--- a/web/src/main/java/com/vayunmathur/web/ui/WebViewClients.kt+++ b/web/src/main/java/com/vayunmathur/web/ui/WebViewClients.kt@@ -6,6 +6,7 @@ import android.content.pm.PackageManager import android.graphics.Bitmap import android.net.Uri import android.util.Log+import android.view.View import android.webkit.CookieManager import android.webkit.GeolocationPermissions import android.webkit.PermissionRequest@@ -131,6 +132,7 @@ internal fun createBrowserWebChromeClient(     isPrivateTab: Boolean,     deps: WebViewClientDeps,     onRequestNewTab: (String) -> Unit,+    fullscreenHost: WebFullscreenHost? = null, ): WebChromeClient = object : WebChromeClient() {     override fun onProgressChanged(view: WebView, newProgress: Int) {         viewModel.onTabProgress(tabId, newProgress / 100f)@@ -236,6 +238,14 @@ internal fun createBrowserWebChromeClient(         return true     } +    override fun onShowCustomView(view: View, callback: CustomViewCallback) {+        fullscreenHost?.onShowCustomView(view, callback) ?: super.onShowCustomView(view, callback)+    }++    override fun onHideCustomView() {+        if (fullscreenHost?.onHideCustomView() != true) super.onHideCustomView()+    }+     override fun onCreateWindow(view: WebView, isDialog: Boolean, isUserGesture: Boolean, resultMsg: android.os.Message?): Boolean {         val transport = resultMsg?.obj as? WebView.WebViewTransport ?: return false         val newWebView = WebView(view.context).apply {