forked from Blendman974/faceclaw
Virtualize timers in app webviews
This commit is contained in:
1 parent
1d00849eb2
commit
5cd61da362
2 files changed
+119
-6
No files matched your search
+43
-3
@@ -1,11 +1,16 @@
|
|||||||
package com.faceclaw.app;
|
package com.faceclaw.app;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Looper;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.webkit.WebView;
|
import android.webkit.WebView;
|
||||||
import android.widget.FrameLayout;
|
import android.widget.FrameLayout;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Keeps EvenHub app WebViews alive and rendering while the phone shows the
|
* Keeps EvenHub app WebViews alive and rendering while the phone shows the
|
||||||
* Faceclaw dashboard instead of the app.
|
* Faceclaw dashboard instead of the app.
|
||||||
@@ -27,16 +32,41 @@ import android.widget.FrameLayout;
|
|||||||
* the chosen WebView to the top of the overlay); hiding sends it back behind.
|
* the chosen WebView to the top of the overlay); hiding sends it back behind.
|
||||||
* All view work happens on the main thread (callers are on the NS/JS thread).
|
* All view work happens on the main thread (callers are on the NS/JS thread).
|
||||||
*
|
*
|
||||||
* Backgrounding note: when the whole activity is not resumed (phone asleep /
|
* Timer keep-alive: when the phone screen turns off, Chromium heavily throttles
|
||||||
* Faceclaw not foregrounded) the platform stops producing frames for these
|
* the page's own setTimeout/setInterval (intensive background throttling clamps
|
||||||
* WebViews regardless — true screen-off rendering is a separate, harder problem.
|
* them to ~1/sec), so timer-driven apps (e.g. snake's game loop) slow to a
|
||||||
|
* crawl even though the renderer is still alive. Host-initiated
|
||||||
|
* evaluateJavascript is NOT subject to that throttle, so we drive the app's
|
||||||
|
* timers ourselves: a document-start shim replaces setTimeout/setInterval (and
|
||||||
|
* requestAnimationFrame) with JS queues fired by window.__fcTimerTick() /
|
||||||
|
* __fcRafTick(), and this host ticks those on the main thread at a fixed rate
|
||||||
|
* 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.)
|
||||||
*/
|
*/
|
||||||
public class FaceclawEvenHubWebViewHost {
|
public class FaceclawEvenHubWebViewHost {
|
||||||
private static FaceclawEvenHubWebViewHost instance;
|
private static FaceclawEvenHubWebViewHost instance;
|
||||||
|
|
||||||
|
/** How often the host fires the JS timer queue. 60Hz keeps games smooth. */
|
||||||
|
private static final long TICK_MS = 16;
|
||||||
|
|
||||||
private FrameLayout overlay;
|
private FrameLayout overlay;
|
||||||
private boolean shown = false;
|
private boolean shown = false;
|
||||||
|
|
||||||
|
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||||
|
private final List<WebView> webViews = new ArrayList<>();
|
||||||
|
private boolean ticking = false;
|
||||||
|
private final Runnable ticker = new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
for (int i = 0; i < webViews.size(); i++) {
|
||||||
|
webViews.get(i).evaluateJavascript(
|
||||||
|
"window.__fcTimerTick&&__fcTimerTick();window.__fcRafTick&&__fcRafTick()", null);
|
||||||
|
}
|
||||||
|
if (ticking) mainHandler.postDelayed(this, TICK_MS);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
public static synchronized FaceclawEvenHubWebViewHost getInstance() {
|
public static synchronized FaceclawEvenHubWebViewHost getInstance() {
|
||||||
if (instance == null) instance = new FaceclawEvenHubWebViewHost();
|
if (instance == null) instance = new FaceclawEvenHubWebViewHost();
|
||||||
return instance;
|
return instance;
|
||||||
@@ -62,6 +92,11 @@ public class FaceclawEvenHubWebViewHost {
|
|||||||
}
|
}
|
||||||
// resumeTimers() is process-global; make sure nothing left timers paused.
|
// resumeTimers() is process-global; make sure nothing left timers paused.
|
||||||
web.resumeTimers();
|
web.resumeTimers();
|
||||||
|
if (!webViews.contains(web)) webViews.add(web);
|
||||||
|
if (!ticking) {
|
||||||
|
ticking = true;
|
||||||
|
mainHandler.postDelayed(ticker, TICK_MS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Bring an app's WebView to the front so it is visible on the phone. */
|
/** Bring an app's WebView to the front so it is visible on the phone. */
|
||||||
@@ -89,6 +124,11 @@ public class FaceclawEvenHubWebViewHost {
|
|||||||
|
|
||||||
/** Remove and destroy a WebView (its app is closing). */
|
/** Remove and destroy a WebView (its app is closing). */
|
||||||
public void detach(WebView web) {
|
public void detach(WebView web) {
|
||||||
|
webViews.remove(web);
|
||||||
|
if (webViews.isEmpty() && ticking) {
|
||||||
|
ticking = false;
|
||||||
|
mainHandler.removeCallbacks(ticker);
|
||||||
|
}
|
||||||
if (overlay != null) overlay.removeView(web);
|
if (overlay != null) overlay.removeView(web);
|
||||||
web.destroy();
|
web.destroy();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,9 +48,11 @@ const SYSTEM_EXIT_EVENT = 7;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Injected into every served HTML document at document start (before any app
|
* Injected into every served HTML document at document start (before any app
|
||||||
* JS): the promise-returning flutter_inappwebview.callHandler the EvenHub
|
* JS): (1) the promise-returning flutter_inappwebview.callHandler the EvenHub
|
||||||
* SDK expects, backed by the FaceclawEvenHubJsBridge Java object. Kept in
|
* SDK expects, backed by the FaceclawEvenHubJsBridge Java object; and (2) a
|
||||||
* ES5 so it runs on any webview.
|
* host-driven setTimeout/setInterval/requestAnimationFrame replacement that
|
||||||
|
* survives the screen-off throttle (fired by the native ticker in
|
||||||
|
* FaceclawEvenHubWebViewHost). Kept in ES5 so it runs on any webview.
|
||||||
*/
|
*/
|
||||||
export const EVENHUB_BRIDGE_INJECT_SCRIPT = `
|
export const EVENHUB_BRIDGE_INJECT_SCRIPT = `
|
||||||
(function () {
|
(function () {
|
||||||
@@ -74,6 +76,77 @@ export const EVENHUB_BRIDGE_INJECT_SCRIPT = `
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
(function () {
|
||||||
|
// Host-driven timers. Chromium heavily throttles a page's own
|
||||||
|
// setTimeout/setInterval when the phone screen is off, which slows
|
||||||
|
// timer-driven apps to a crawl. Replace them with a queue fired by
|
||||||
|
// window.__fcTimerTick(), which the native host calls at ~60Hz regardless of
|
||||||
|
// screen state (host-initiated evaluateJavascript escapes the throttle).
|
||||||
|
if (window.__fcTimerTick) return;
|
||||||
|
var timers = new Map();
|
||||||
|
var nextId = 1;
|
||||||
|
window.setTimeout = function (cb, delay) {
|
||||||
|
var args = Array.prototype.slice.call(arguments, 2);
|
||||||
|
var id = nextId++;
|
||||||
|
timers.set(id, { cb: cb, args: args, due: Date.now() + (+delay || 0), interval: 0 });
|
||||||
|
return id;
|
||||||
|
};
|
||||||
|
window.setInterval = function (cb, delay) {
|
||||||
|
var args = Array.prototype.slice.call(arguments, 2);
|
||||||
|
var id = nextId++;
|
||||||
|
var d = +delay || 0;
|
||||||
|
timers.set(id, { cb: cb, args: args, due: Date.now() + d, interval: d });
|
||||||
|
return id;
|
||||||
|
};
|
||||||
|
window.clearTimeout = function (id) { timers.delete(id); };
|
||||||
|
window.clearInterval = function (id) { timers.delete(id); };
|
||||||
|
window.__fcTimerTick = function () {
|
||||||
|
var now = Date.now();
|
||||||
|
var dueIds = [];
|
||||||
|
timers.forEach(function (entry, id) { if (entry.due <= now) dueIds.push(id); });
|
||||||
|
dueIds.sort(function (a, b) { return timers.get(a).due - timers.get(b).due; });
|
||||||
|
for (var i = 0; i < dueIds.length; i++) {
|
||||||
|
var entry = timers.get(dueIds[i]);
|
||||||
|
if (!entry) continue; // cleared by an earlier callback this tick
|
||||||
|
if (entry.interval > 0) {
|
||||||
|
// Advance one period (keeps cadence); if we've fallen behind, skip the
|
||||||
|
// missed periods rather than fast-forwarding a burst of catch-up fires.
|
||||||
|
entry.due += entry.interval;
|
||||||
|
if (entry.due <= now) entry.due = now + entry.interval;
|
||||||
|
} else {
|
||||||
|
timers.delete(dueIds[i]);
|
||||||
|
}
|
||||||
|
try { entry.cb.apply(null, entry.args); } catch (e) { if (window.console) console.error(e); }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
(function () {
|
||||||
|
// Host-driven requestAnimationFrame, same idea as the timers above: a hidden
|
||||||
|
// page's rAF is paused/throttled with the screen off, stalling canvas apps
|
||||||
|
// that render on a rAF loop. Drive it from window.__fcRafTick(), called by the
|
||||||
|
// native ticker each frame. (The glasses render via BLE, not the phone's
|
||||||
|
// display, so vsync alignment doesn't matter — a steady frame rate does.)
|
||||||
|
if (window.__fcRafTick) return;
|
||||||
|
var rafs = new Map();
|
||||||
|
var nextRafId = 1;
|
||||||
|
window.requestAnimationFrame = function (cb) {
|
||||||
|
var id = nextRafId++;
|
||||||
|
rafs.set(id, cb);
|
||||||
|
return id;
|
||||||
|
};
|
||||||
|
window.cancelAnimationFrame = function (id) { rafs.delete(id); };
|
||||||
|
window.__fcRafTick = function () {
|
||||||
|
if (rafs.size === 0) return;
|
||||||
|
var ts = window.performance && performance.now ? performance.now() : Date.now();
|
||||||
|
// Snapshot and clear: callbacks that re-request during this frame run on
|
||||||
|
// the NEXT frame, matching rAF semantics (so a loop doesn't spin forever).
|
||||||
|
var current = rafs;
|
||||||
|
rafs = new Map();
|
||||||
|
current.forEach(function (cb) {
|
||||||
|
try { cb(ts); } catch (e) { if (window.console) console.error(e); }
|
||||||
|
});
|
||||||
|
};
|
||||||
|
})();
|
||||||
`;
|
`;
|
||||||
|
|
||||||
/** Stock blocks a repeated createStartUpPageContainer ~2s before failing it. */
|
/** Stock blocks a repeated createStartUpPageContainer ~2s before failing it. */
|
||||||
|
|||||||
Reference in new issue
Block a user