diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebView.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebView.java new file mode 100644 index 00000000..5a590902 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebView.java @@ -0,0 +1,40 @@ +package com.faceclaw.app; + +import android.content.Context; +import android.view.View; +import android.webkit.WebView; + +/** + * A WebView for hosting an EvenHub app that keeps rendering and running JS even + * when the whole Faceclaw activity is backgrounded (phone on the home screen or + * another app). + * + * The problem: Chromium ties a WebView's page-visibility to its window's + * visibility. When the host activity stops, every view gets + * onWindowVisibilityChanged(GONE); Chromium then marks the page hidden and + * freezes/throttles the renderer. A frozen renderer doesn't execute + * evaluateJavascript, so the host timer/rAF ticks AND pushed input events queue + * up and only run once the app is foregrounded again — exactly the "Hub apps + * freeze in the background" symptom (also seen in early official-app versions). + * + * The occluded-overlay trick in FaceclawEvenHubWebViewHost keeps the VIEW + * visible while the activity is foreground, but can't help once the WINDOW goes + * away. So we lie about window visibility: this WebView always tells Chromium + * the window is VISIBLE, so the page never goes hidden and the renderer keeps + * running in the background. We drive the glasses over BLE from the foreground + * service regardless of the phone's screen, so "always rendering" is what we + * actually want; there is no on-phone cost while the app is unseen. + */ +public class FaceclawEvenHubWebView extends WebView { + + public FaceclawEvenHubWebView(Context context) { + super(context); + } + + @Override + protected void onWindowVisibilityChanged(int visibility) { + // Never propagate a hidden window: keep Chromium's page-visibility + // "visible" so the renderer isn't frozen when Faceclaw backgrounds. + super.onWindowVisibilityChanged(View.VISIBLE); + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.java index fd7e7e4f..d50d00ae 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.java @@ -1,6 +1,7 @@ package com.faceclaw.app; import android.app.Activity; +import android.os.Build; import android.os.Handler; import android.os.Looper; import android.view.View; @@ -43,6 +44,17 @@ import java.util.List; * regardless of screen state. The main Looper keeps running under the * foreground service, so the tick survives the screen turning off. (Matches the * official Even app's approach.) + * + * Background keep-alive: the occluded-overlay trick only works while the + * activity is foreground. When Faceclaw itself is backgrounded, the window goes + * invisible and Chromium freezes the renderer — the timer/rAF ticks and pushed + * input events then queue up and only run once foregrounded again. Two things + * prevent that: the WebViews are {@link FaceclawEvenHubWebView}, which lies to + * Chromium about window visibility so the page never goes hidden; and each is + * pinned to IMPORTANT renderer priority (not waived when not visible) with + * timers/onResume asserted on attach. The main Looper keeps ticking under the + * foreground service, so apps keep driving their glasses windows in the + * background. */ public class FaceclawEvenHubWebViewHost { private static FaceclawEvenHubWebViewHost instance; @@ -91,7 +103,17 @@ public class FaceclawEvenHubWebViewHost { ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); } // resumeTimers() is process-global; make sure nothing left timers paused. + // onResume() undoes any per-WebView pause. Together with + // FaceclawEvenHubWebView faking window visibility, this keeps the page + // running JS while Faceclaw is backgrounded. web.resumeTimers(); + web.onResume(); + // Keep the renderer process at IMPORTANT priority even when the WebView + // isn't visible (waivedWhenNotVisible=false), so Android doesn't + // deprioritize/kill it in the background. + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + web.setRendererPriorityPolicy(WebView.RENDERER_PRIORITY_IMPORTANT, false); + } if (!webViews.contains(web)) webViews.add(web); if (!ticking) { ticking = true; diff --git a/app/apps/evenhub/webview.ts b/app/apps/evenhub/webview.ts index 002cfc3c..597fd6cb 100644 --- a/app/apps/evenhub/webview.ts +++ b/app/apps/evenhub/webview.ts @@ -5,9 +5,11 @@ * glasses window closes — it renders the app to the glasses whether or not the * phone is currently showing it (see the host for the off-screen-render trick). * - * A raw android.webkit.WebView (not the NativeScript component) is used so we - * fully own the WebViewClient: offline asset serving from a fake per-app origin - * plus document-start injection of the flutter_inappwebview bridge shim. + * A raw WebView subclass (FaceclawEvenHubWebView, not the NativeScript + * component) is used so we fully own the WebViewClient (offline asset serving + * from a fake per-app origin plus document-start injection of the + * flutter_inappwebview bridge shim) and so the page keeps running JS while the + * host app is backgrounded (see FaceclawEvenHubWebView / the host). */ import { Application, Utils } from "@nativescript/core"; import { EVENHUB_BRIDGE_INJECT_SCRIPT, type EvenHubSession } from "./session"; @@ -37,7 +39,7 @@ function originHost(session: EvenHubSession): string { export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView { const activity = Application.android?.foregroundActivity ?? Application.android?.startActivity; const context = activity ?? Utils.android.getApplicationContext(); - const webView = new android.webkit.WebView(context); + const webView = new com.faceclaw.app.FaceclawEvenHubWebView(context); const settings = webView.getSettings(); settings.setJavaScriptEnabled(true); settings.setDomStorageEnabled(true);