Files
corescope-rx/src/wakelock.js
T
efiten f280dec686 android: keep the WebView running with the screen off
Three v1.24.3 field logs of 2026-11-11 (three phones) show the same thing:
about 60 s after "app hidden" every upload, RF sample, region ask and uplink
ping stops until the screen is back on, for 7 to 25 minutes, while receptions
and discover go on. The catch-up of 149e39b keeps monitorTick alive but not
the jobs that wait on a timer, on IndexedDB or on the broker.

What changes:
- AlwaysVisibleWebView reports its window as visible at all times, so
  Chromium never treats the page as hidden. An app copy of
  capacitor_bridge_layout_main.xml puts it in place of CapacitorWebView.
- MainActivity sends appHidden / appVisible on onStop / onStart, because
  visibilitychange no longer fires. app.js logs both, wakelock.js re-acquires
  the screen lock on appVisible and leaves one that is still held alone.
- Service worker cache v64.

Verified: npm test, 485 of 485 pass. assembleDebug builds, and aapt2 shows
eu.on8ar.coredriverx.AlwaysVisibleWebView in the APK's layout.

Not verified: that this stops the freeze. No phone has run this build yet.
Battery use with the screen off is not measured. No version bump and no
release notes: those follow once a screen-off drive confirms it.
The web app (Chrome, Bluefy) is not changed by this and still freezes.
2026-11-11 20:09:21 +01:00

95 lines
3.2 KiB
JavaScript

// Keep the screen awake while capturing. Generic across browsers: use the native
// Screen Wake Lock API where available (Chrome Android/desktop, Safari 16.4+ tab),
// and fall back to a hidden muted looping <video> (the "NoSleep" trick) where it
// isn't (e.g. Bluefy / WKWebView on iOS). The native sentinel is auto-released when
// the page is hidden, so it is re-acquired on visibilitychange. In the Android app the
// page is never hidden (android AlwaysVisibleWebView), so MainActivity's appVisible
// event does the same there.
//
// navigator/document are injectable so the native-vs-fallback selection is unit
// testable without a real browser.
import { webm, mp4 } from './wakelock-media.js';
export function createWakeLock(deps = {}) {
const nav = deps.navigator ?? (typeof navigator !== 'undefined' ? navigator : undefined);
const doc = deps.document ?? (typeof document !== 'undefined' ? document : undefined);
const native = !!(nav && nav.wakeLock);
let active = false; // the caller wants the screen kept awake
let sentinel = null; // native WakeLockSentinel
let video = null; // fallback <video>
async function acquireNative() {
try {
sentinel = await nav.wakeLock.request('screen');
} catch (e) {
// request can reject (e.g. document not visible) — use the video instead.
startVideo();
}
}
function ensureVideo() {
if (video || !doc) return;
video = doc.createElement('video');
video.setAttribute('muted', '');
video.muted = true;
video.setAttribute('playsinline', '');
video.setAttribute('loop', '');
video.style.position = 'fixed';
video.style.width = '1px';
video.style.height = '1px';
video.style.opacity = '0';
const addSrc = (type, data) => {
const s = doc.createElement('source');
s.type = type;
s.src = data;
video.appendChild(s);
};
addSrc('video/webm', webm);
addSrc('video/mp4', mp4);
doc.body.appendChild(video);
}
function startVideo() {
ensureVideo();
if (video && video.play) {
const p = video.play();
if (p && p.catch) p.catch(() => {});
}
}
function onVisible() {
// The OS releases the native sentinel when the page hides; re-acquire on return.
// One that is still held is left alone: a second request would orphan it.
if (sentinel && sentinel.released === false) return;
if (active && native && doc && doc.visibilityState === 'visible') acquireNative();
}
async function enable() {
if (active) return; // idempotent — don't stack locks
active = true;
if (native) {
if (doc && doc.addEventListener) {
doc.addEventListener('visibilitychange', onVisible);
doc.addEventListener('appVisible', onVisible);
}
await acquireNative();
} else {
startVideo();
}
}
async function disable() {
if (!active) return; // no-op when not enabled
active = false;
if (doc && doc.removeEventListener) {
doc.removeEventListener('visibilitychange', onVisible);
doc.removeEventListener('appVisible', onVisible);
}
if (sentinel) { try { await sentinel.release(); } catch (e) {} sentinel = null; }
if (video && video.pause) { try { video.pause(); } catch (e) {} }
}
return { enable, disable };
}