Fix issue with EvenHub apps freezing when not foregrounded on the phone

This commit is contained in:
James Babcock committed 2026-08-16 21:01:20 -07:00
1 parent b6785e5a5a
commit dcf35dd606
3 files changed
+68 -4

No files matched your search

@@ -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);
}
}
@@ -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;