Merge branch 'main' of https://github.com/jimrandomh/faceclaw into ring-health-fixes

This commit is contained in:
James Babcock committed 2026-10-06 13:32:49 -07:00
commit e12cfaaf38
309 files changed
+15906 -3352

No files matched your search

+1 -1
View File
@@ -193,7 +193,7 @@ function finalizeBlocks(blocks: Map<number, StreamingBlock>): AnthropicContentBl
}
/** Parse one SSE line; returns the JSON payload of a data: line, else null. */
function parseSseDataLine(line: string): any | null {
function parseSseDataLine(line: string): any {
if (!line.startsWith("data:")) return null;
const payload = line.slice(5).trim();
if (!payload || payload === "[DONE]") return null;
+77
View File
@@ -0,0 +1,77 @@
/**
* Passive view of the glasses' audio traffic, for diagnostics. Nothing here
* sends anything to the glasses: the listeners only observe packets and CFW
* mic_control status records that arrive because something else (a stock
* EvenHub mic enable, or a CFW mic_control CONFIGURE) turned the mics on.
*/
import { toUint8Array } from "../util/array-util";
declare const com: any;
declare const global: any;
export type AudioArm = "L" | "R" | "?";
function activeCommunicator(): any {
if (!global.isAndroid) return null;
try {
return com.faceclaw.app.FaceclawBleCommunicator.getActive();
} catch {
return null;
}
}
/** The communicator instance listeners attach to; changes across reconnects. */
export function audioMonitorCommunicator(): any {
return activeCommunicator();
}
/**
* True while Faceclaw itself is forwarding glasses audio to one of its apps
* (stock mic enable or CFW mic_control forwarding). Packets can also arrive
* with this false, e.g. when capture was armed and Faceclaw's side stopped.
*/
export function isFaceclawAudioCaptureActive(): boolean {
try {
return Boolean(activeCommunicator()?.isAudioCaptureActive());
} catch {
return false;
}
}
/**
* Subscribe `communicator` to every audio-characteristic packet and every
* field-104 mic status record body (undecoded). Delivered on the main thread.
* Returns an unsubscribe function.
*/
export function addAudioMonitorListeners(
communicator: any,
onPacket: (data: Uint8Array, arm: AudioArm, arrivalMs: number) => void,
onStatus: (body: Uint8Array, arm: AudioArm) => void,
): () => void {
if (!communicator) return () => {};
const packetProxy = new com.faceclaw.app.FaceclawAudioPacketListener({
onAudioPacket: (data: any, arm: string, arrivalMs: number) => {
onPacket(toUint8Array(data), toArm(arm), Number(arrivalMs));
},
});
const statusProxy = new com.faceclaw.app.FaceclawMicStatusListener({
onMicStatus: (body: any, arm: string) => {
onStatus(toUint8Array(body), toArm(arm));
},
});
communicator.addAudioMonitorListener(packetProxy);
communicator.addMicStatusListener(statusProxy);
return () => {
try {
communicator.removeAudioMonitorListener(packetProxy);
communicator.removeMicStatusListener(statusProxy);
} catch {
// The communicator may have been torn down during a disconnect.
}
};
}
function toArm(arm: string): AudioArm {
const value = String(arm);
return value === "L" || value === "R" ? value : "?";
}
+35 -13
View File
@@ -1,15 +1,37 @@
import { fetchWithUserAgent } from '../util/http';
declare const com: any;
declare const java: any;
export type BinaryResponse = { status: number; bytes: Uint8Array; contentLength: number };
export async function downloadBinary(url: string, timeoutMs: number): Promise<BinaryResponse> {
let timer: ReturnType<typeof setTimeout>;
const request = async () => {
const response = await fetchWithUserAgent(url);
return { status: response.status, bytes: new Uint8Array(await response.arrayBuffer()),
contentLength: Number(response.headers.get('Content-Length') ?? 0) };
};
try {
return await Promise.race([request(), new Promise<never>((_, reject) => {
timer = setTimeout(() => reject(new Error('Download timed out.')), timeoutMs);
})]);
} finally { clearTimeout(timer!); }
let nextDownloadSerial = 0;
/**
* GET a binary body. Not fetch(): NativeScript's polyfill copies the Java
* byte[] into JS one bridge call per byte (http-request's toArrayBuffer), which
* for a 43MB EvenHub package pinned the main thread for ~20s and then ran the
* process out of native memory. FaceclawFileDownload streams the body to a
* cache file instead, and readBinaryFile copies it back in bulk.
*/
export function downloadBinary(url: string, timeoutMs: number): Promise<BinaryResponse> {
// Required lazily so Node tests can load this module's importers.
const { appCacheDirPath, readBinaryFile } = require("./file-access") as typeof import("./file-access");
const path = `${appCacheDirPath()}/binary-download-${Date.now()}-${nextDownloadSerial++}`;
const remove = () => { try { new java.io.File(path).delete(); } catch { /* best effort */ } };
return new Promise((resolve, reject) => {
const listener = new com.faceclaw.app.FaceclawFileDownloadListener({
onDone: (status: number, contentLength: number) => {
try {
const ok = status >= 200 && status < 300;
const bytes = ok ? readBinaryFile(path) : new Uint8Array(0);
if (!bytes) reject(new Error("Could not read the downloaded file."));
else resolve({ status, bytes, contentLength: Math.max(0, Number(contentLength)) });
} finally { remove(); }
},
onFailure: (message: string) => {
remove();
reject(new Error(/timeout|timed out/i.test(String(message)) ? "Download timed out." : `Download failed: ${message}`));
},
});
new com.faceclaw.app.FaceclawFileDownload(url, path, timeoutMs, listener).start();
});
}
+1 -1
View File
@@ -24,7 +24,7 @@ async function requestPermission(): Promise<boolean> {
okButtonText: "Open Settings",
cancelButtonText: "Cancel",
});
if (open) await Utils.openUrl("app-settings:");
if (open) Utils.openUrl("app-settings:");
return false;
}
if (status === "restricted") {
+1 -1
View File
@@ -97,7 +97,7 @@ export async function readUpcomingEventsAsync(maxEvents = 50, windowMs = DEFAULT
try {
if (!hasCalendarPermission()) throw new Error("Calendar permission is required.");
resolve(parseEvents(json, error, maxEvents, endMs));
} catch (error) { reject(error); }
} catch (err) { reject(err); }
});
});
}
+31 -3
View File
@@ -1,6 +1,6 @@
import type { RingInput } from "../g2/ring-input";
import { File, knownFolders, path, type ImageSource } from "@nativescript/core";
import { type FirmwareInfo } from "../g2/firmware-compat";
import { REQUIRED_FACECLAW_FIRMWARE_VERSION, type FirmwareInfo } from "../g2/firmware-compat";
import { type CompassEvent } from "./compass-types";
import { fromData, toData } from "./kotlin-data";
import { previewPixels } from "./ios-graphics";
@@ -10,7 +10,6 @@ export type { FirmwareInfo };
// Kotlin/Native facades exported by FaceclawKit (see native/kotlin/shared/src/iosMain/.../ble).
declare const FaceclawKitIosGlassesSession: any;
declare const FaceclawKitIosProtocolPlatform: any;
declare const FaceclawKitFaceclawBleCommunicatorListener: any;
declare const FaceclawKitIosAncsListener: any;
declare const FaceclawKitIosAudioPacketListener: any;
@@ -24,6 +23,7 @@ export type CommunicatorPhase =
| "charging"
| "retrying"
| "unpaired"
| "incompatible-firmware"
| "disconnecting";
export type CommunicatorState = {
@@ -191,6 +191,7 @@ export class FaceclawCommunicatorBridge {
private latestPhoneLockState: boolean | null = null;
private readonly evenAppConflictListeners = new Set<(message: string) => void>();
private readonly frameMetricsListeners = new Set<(metrics: FrameMetrics) => void>();
private readonly previewAnimationListeners = new Set<() => void>();
private readonly firmwareInfoListeners = new Set<(info: FirmwareInfo) => void>();
private readonly ancsAuthorizationListeners = new Set<(authorized: boolean) => void>();
private readonly ancsRelayListeners = new Set<(frame: Uint8Array) => void>();
@@ -204,9 +205,15 @@ export class FaceclawCommunicatorBridge {
String(addresses.left ?? "").toUpperCase(),
String(addresses.ring ?? "").toUpperCase(),
);
// The shared session halts on any other firmware ("incompatible-firmware").
this.communicator.setRequiredFirmwareRevisionRevision(REQUIRED_FACECLAW_FIRMWARE_VERSION);
this.listenerProxy = SessionListener.new() as SessionListener;
this.listenerProxy.bridge = this;
this.communicator.setListenerListener(this.listenerProxy);
// Kotlin calls this on the main queue.
this.communicator.setPreviewAnimationListenerListener(() => {
for (const listener of Array.from(this.previewAnimationListeners)) listener();
});
}
// ----- Kotlin listener entry points (main queue) -----------------------------------
@@ -355,6 +362,15 @@ export class FaceclawCommunicatorBridge {
return () => this.frameMetricsListeners.delete(listener);
}
/**
* Each step of an animation the phone preview is replaying (a menu slide
* reaches the glasses as one frame, so frame metrics fire only at its start).
*/
onPreviewAnimationFrame(listener: () => void): () => void {
this.previewAnimationListeners.add(listener);
return () => this.previewAnimationListeners.delete(listener);
}
onFirmwareInfo(listener: (info: FirmwareInfo) => void): () => void {
this.firmwareInfoListeners.add(listener);
return () => this.firmwareInfoListeners.delete(listener);
@@ -536,6 +552,14 @@ export class FaceclawCommunicatorBridge {
await this.enqueueNativeCall(() => { this.communicator.setScreenBlankedBlanked(Boolean(blanked)); });
}
/**
* See the Android bridge. Not wired to the shared compositor's check on iOS
* yet, so every repaint is submitted as before.
*/
isSurfaceCurrent(_surfaceId: string, _fingerprint: string): boolean {
return false;
}
async submitSurfaceFrame(
surfaceId: string,
pixels8bpp: Uint8Array,
@@ -593,7 +617,11 @@ export class FaceclawCommunicatorBridge {
async close(): Promise<void> {
this.setAncsListeners(false);
await this.enqueueNativeCall(() => this.communicator.close());
this.previewAnimationListeners.clear();
await this.enqueueNativeCall(() => {
this.communicator.setPreviewAnimationListenerListener(null);
this.communicator.close();
});
}
// ----- iOS extras: compass, microphone, iPhone notifications (ANCS) ----------------------
+55 -4
View File
@@ -14,6 +14,7 @@ export type CommunicatorPhase =
| "charging"
| "retrying"
| "unpaired"
| "incompatible-firmware"
| "disconnecting";
export type CommunicatorState = {
@@ -34,7 +35,7 @@ export type FrameMetrics = {
tileCount: number;
};
import { type FirmwareInfo } from "../g2/firmware-compat";
import { REQUIRED_FACECLAW_FIRMWARE_VERSION, type FirmwareInfo } from "../g2/firmware-compat";
export type { FirmwareInfo };
@@ -133,6 +134,8 @@ export class FaceclawCommunicatorBridge {
private javaCallQueue: Promise<void> = Promise.resolve();
/** Calls waiting on javaCallQueue; 0 means enqueueJavaCall's fast path is safe. */
private queuedJavaCalls = 0;
/** Queued calls that may change what a surface holds; see isSurfaceCurrent. */
private queuedSurfaceChanges = 0;
// Reused Java-side buffers for byte payloads; passing a JS ArrayBuffer to
// Java leaks it (see java-direct-buffer.ts). Loaded inside the queued call,
// immediately before the synchronous Java call that consumes them.
@@ -156,6 +159,7 @@ export class FaceclawCommunicatorBridge {
private readonly evenAppConflictListeners = new Set<(message: string) => void>();
private readonly frameMetricsListeners = new Set<(metrics: FrameMetrics) => void>();
private readonly firmwareInfoListeners = new Set<(info: FirmwareInfo) => void>();
private readonly previewAnimationListeners = new Set<() => void>();
constructor(addresses: { right: string; left: string; ring?: string }) {
const context = Utils.android.getApplicationContext();
@@ -167,6 +171,8 @@ export class FaceclawCommunicatorBridge {
addresses.left,
addresses.ring ?? "",
);
// The shared session halts on any other firmware ("incompatible-firmware").
this.communicator.setRequiredFirmwareRevision(REQUIRED_FACECLAW_FIRMWARE_VERSION);
this.listenerProxy = new com.faceclaw.app.FaceclawBleCommunicatorListener({
onStateChange: (phase: string, status: string) => {
const state = {
@@ -246,6 +252,14 @@ export class FaceclawCommunicatorBridge {
},
});
this.communicator.setListener(this.listenerProxy);
// Already posted to the main looper by the Java side.
this.communicator.setPreviewAnimationListener(
new java.lang.Runnable({
run: () => {
for (const listener of Array.from(this.previewAnimationListeners)) listener();
},
}),
);
}
private emitAsync<T>(listeners: Set<(value: T) => void>, value: T): void {
@@ -267,8 +281,12 @@ export class FaceclawCommunicatorBridge {
* the frame timings), which is pure latency on a call the caller is already
* awaiting. Off by default: a call that might block for a while should keep
* yielding to the main looper first.
*
* preservesSurfaces marks a call that neither changes a surface's retained
* content nor leaves a change waiting for a later composite (it composites
* itself), so having it queued does not make isSurfaceCurrent answer false.
*/
private enqueueJavaCall<T>(operation: () => T, inlineWhenIdle = false): Promise<T> {
private enqueueJavaCall<T>(operation: () => T, inlineWhenIdle = false, preservesSurfaces = false): Promise<T> {
if (inlineWhenIdle && this.queuedJavaCalls === 0) {
try {
return Promise.resolve(operation());
@@ -277,10 +295,12 @@ export class FaceclawCommunicatorBridge {
}
}
this.queuedJavaCalls++;
if (!preservesSurfaces) this.queuedSurfaceChanges++;
const run = () =>
new Promise<T>((resolve, reject) => {
setTimeout(() => {
this.queuedJavaCalls--;
if (!preservesSurfaces) this.queuedSurfaceChanges--;
try {
resolve(operation());
} catch (error) {
@@ -353,6 +373,15 @@ export class FaceclawCommunicatorBridge {
return () => this.frameMetricsListeners.delete(listener);
}
/**
* Each step of an animation the phone preview is replaying (a menu slide
* reaches the glasses as one frame, so frame metrics fire only at its start).
*/
onPreviewAnimationFrame(listener: () => void): () => void {
this.previewAnimationListeners.add(listener);
return () => this.previewAnimationListeners.delete(listener);
}
onFirmwareInfo(listener: (info: FirmwareInfo) => void): () => void {
this.firmwareInfoListeners.add(listener);
return () => this.firmwareInfoListeners.delete(listener);
@@ -463,6 +492,11 @@ export class FaceclawCommunicatorBridge {
await this.enqueueJavaCall(() => this.communicator.enableWearDetectionAndRequestState());
}
/** Whether the transport prelude has finished (each arm reports "connected" before it). */
isSessionReady(): boolean {
return Boolean(this.communicator.isSessionReady());
}
/** Set the compositor's output frame size. Call before configuring surfaces. */
async configureCompositorScreen(width: number, height: number): Promise<void> {
await this.enqueueJavaCall(() => {
@@ -549,6 +583,7 @@ export class FaceclawCommunicatorBridge {
await this.enqueueJavaCall(
() => this.communicator.submitShellScene(this.shellSceneBuffer.load(snapshot), paintMs, frameId),
true,
true,
);
}
@@ -562,7 +597,19 @@ export class FaceclawCommunicatorBridge {
async setSurfaceVisible(id: string, visible: boolean): Promise<void> {
await this.enqueueJavaCall(() => {
this.communicator.setSurfaceVisible(id, Boolean(visible));
});
}, false, true);
}
/**
* Whether submitting a full-surface frame with this fingerprint would
* change nothing: Java already holds that content for the surface, and no
* queued or not-yet-composited change is waiting on a composite. Lets a
* repaint that came out identical (a window re-rendered when it comes to
* the foreground, say) skip flattening and submitting its frame.
*/
isSurfaceCurrent(surfaceId: string, fingerprint: string): boolean {
if (this.queuedSurfaceChanges > 0) return false;
return Boolean(this.communicator.isSurfaceCurrent(surfaceId, fingerprint));
}
/**
@@ -691,6 +738,10 @@ export class FaceclawCommunicatorBridge {
}
async close(): Promise<void> {
await this.enqueueJavaCall(() => this.communicator.close());
this.previewAnimationListeners.clear();
await this.enqueueJavaCall(() => {
this.communicator.setPreviewAnimationListener(null);
this.communicator.close();
});
}
}
+17 -11
View File
@@ -124,19 +124,19 @@ export function readBinaryFile(path: string): Uint8Array | null {
try {
const file = new java.io.File(path);
if (!file.isFile()) return null;
const bytes = java.nio.file.Files.readAllBytes(file.toPath());
// Read into a direct buffer, which the V8 runtime can view in place, then
// copy once so the result outlives it. Nothing lands on the Java heap, and
// no path copies per byte (EvenHub packages can be tens of MB).
const length = Number(file.length());
const buffer = java.nio.ByteBuffer.allocateDirect(Math.max(length, 1));
const channel = new java.io.FileInputStream(file).getChannel();
try {
// The V8 runtime can wrap a ByteBuffer's memory directly; copy out of it
// so the result outlives the Java array.
const arrayBuffer = (ArrayBuffer as any).from(java.nio.ByteBuffer.wrap(bytes));
return new Uint8Array(arrayBuffer).slice();
} catch {
const out = new Uint8Array(bytes.length);
for (let i = 0; i < bytes.length; i++) {
out[i] = bytes[i] & 0xff;
}
return out;
while (buffer.position() < length && channel.read(buffer) >= 0) { /* fill */ }
} finally {
channel.close();
}
const read = Number(buffer.position());
return new Uint8Array((ArrayBuffer as any).from(buffer), 0, read).slice();
} catch (error) {
console.warn(`readBinaryFile failed for ${path}: ${error}`);
return null;
@@ -189,6 +189,12 @@ export function appFilesDirPath(): string {
return String(context.getFilesDir().getAbsolutePath());
}
/** The app-private cache directory, for scratch files the OS may reclaim. */
export function appCacheDirPath(): string {
const context = Utils.android.getApplicationContext();
return String(context.getCacheDir().getAbsolutePath());
}
/** Read a UTF-8 text file (size-capped); null when unreadable or too large. */
export function readTextFile(path: string): string | null {
try {
-1
View File
@@ -1,7 +1,6 @@
import { openSocket } from "./socket";
import { toUint8Array } from "../util/array-util";
declare const com: any;
declare const android: any;
declare const java: any;
+18
View File
@@ -27,3 +27,21 @@ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null {
);
return new ImageSource(bitmap);
}
/**
* A glasses icon as a white-on-transparent phone image (gray becomes alpha),
* for the dark action bar.
*/
export function grayImageToWhiteIconSource(image: GrayImage): ImageSource | null {
if (!global.isAndroid) {
return null;
}
const baked = image.withDrawsBaked();
const bitmap = com.faceclaw.app.PreviewBitmapUtil.whiteIconFromGray(
copyToJavaByteBuffer(baked.pixels),
baked.width,
baked.height,
);
return new ImageSource(bitmap);
}
+2 -2
View File
@@ -38,7 +38,7 @@ export class IosBluetooth {
this.state = Number(state); this.authorization = Number(authorization)
this.emit({ kind: 'state', state: this.state, authorization: this.authorization })
},
onAdvertisementIdentifierNameManufacturerDataHexRssiConnectable: (identifier: string, name: string, manufacturerData: string, rssi: number, connectable: boolean) => {
onAdvertisementIdentifierNameManufacturerDataHexRssiConnectable: (identifier: string, name: string, manufacturerData: string, rssi: number, connectable: boolean | undefined) => {
// The Kotlin central already merges split advertisements and bounds its cache.
const raw: IosAdvertisement = { identifier: String(identifier), name: String(name ?? ''), manufacturerData: String(manufacturerData ?? ''), rssi: Number(rssi), connectable: connectable !== false }
this.emit({ kind: 'advertisement', ...raw })
@@ -65,7 +65,7 @@ export class IosBluetooth {
ensureReady(): Promise<void> {
if (this.state === BLE_STATE_POWERED_ON) return Promise.resolve()
return new Promise((resolve, reject) => {
const finish = (error?: Error) => { clearTimeout(timer); off(); error ? reject(error) : resolve() }
const finish = (error?: Error) => { clearTimeout(timer); off(); if (error) reject(error); else resolve() }
const off = this.onEvent(event => {
if (event.kind !== 'state') return
if (event.state === BLE_STATE_POWERED_ON) finish()
-38
View File
@@ -1,38 +0,0 @@
import { isAndroid } from "@nativescript/core";
import { createKotlinBridge } from "./kotlin-platform";
/** Exercise the real native metadata and both directions of return values. */
export function runKotlinBridgeSmokeTest(): void {
try {
const bridge = createKotlinBridge();
const platform = bridge.platform();
const thread = bridge.threadName();
const callerOnMain = bridge.isMainThread();
const greeting = bridge.greet("Faceclaw");
const message = "Faceclaw \u2194 Kotlin \ud83d\udc53";
let callbackCount = 0;
let callbackMessage = "";
let callbackOnMain = false;
// Validate after returning; never throw from a native callback.
const reply = bridge.roundTrip(message, (value: string): string => {
callbackCount++;
callbackMessage = value;
callbackOnMain = bridge.isMainThread();
return `TypeScript received: ${value}`;
});
if (greeting !== "Hello, Faceclaw, from Kotlin"
|| platform !== (isAndroid ? "Android" : "iOS")
|| !thread || !callerOnMain || !callbackOnMain
|| callbackCount !== 1
|| callbackMessage !== `Kotlin received: ${message}`
|| reply !== `Kotlin received callback: TypeScript received: Kotlin received: ${message}`) {
throw new Error(`Unexpected Kotlin bridge result: ${JSON.stringify({
platform, thread, callerOnMain, callbackOnMain, greeting, callbackCount, callbackMessage, reply,
})}`);
}
console.log("FACECLAW_KOTLIN_BRIDGE_PASS " + JSON.stringify({ platform, thread, reply }));
} catch (error) {
// Diagnostics must not prevent the rest of the app from launching.
console.error("FACECLAW_KOTLIN_BRIDGE_FAIL " + error);
}
}
-33
View File
@@ -1,33 +0,0 @@
import { runKotlinProtocolSmokeTest } from './kotlin-protocol-smoke';
import type { KotlinBridge } from './kotlin-platform';
// Handwritten declarations for the small native surface, checked against the
// compiled API. NativeScript runtime metadata is generated from the AAR.
declare namespace com.faceclaw.shared {
class KotlinBridge extends java.lang.Object {
constructor();
platform(): string;
threadName(): string;
isMainThread(): boolean;
greet(name: string): string;
roundTrip(message: string, listener: KotlinBridgeListener): string;
}
class KotlinBridgeListener extends java.lang.Object {
constructor(implementation: { transform(message: string): string });
}
}
export function createKotlinBridge(): KotlinBridge {
runKotlinProtocolSmokeTest();
const bridge = new com.faceclaw.shared.KotlinBridge();
return {
platform: () => bridge.platform(),
threadName: () => bridge.threadName(),
isMainThread: () => bridge.isMainThread(),
greet: name => bridge.greet(name),
roundTrip(message, transform) {
const listener = new com.faceclaw.shared.KotlinBridgeListener({ transform });
return bridge.roundTrip(message, listener);
},
};
}
-11
View File
@@ -1,11 +0,0 @@
/** Exported native API normalized across Java interfaces and Obj-C protocols. */
export interface KotlinBridge {
platform(): string;
threadName(): string;
isMainThread(): boolean;
greet(name: string): string;
/** Synchronous callback; must not throw across the native boundary. */
roundTrip(message: string, transform: (message: string) => string): string;
}
export function createKotlinBridge(): KotlinBridge;
-44
View File
@@ -1,44 +0,0 @@
import { runKotlinProtocolSmokeTest } from './kotlin-protocol-smoke.ios';
import type { KotlinBridge } from './kotlin-platform';
// Handwritten declarations matching FaceclawKit's Objective-C header.
declare class FaceclawKitKotlinBridge extends NSObject {
static alloc(): FaceclawKitKotlinBridge;
init(): this;
platform(): string;
threadName(): string;
isMainThread(): boolean;
greetName(name: string): string;
roundTripMessageListener(message: string, listener: FaceclawKitKotlinBridgeListener): string;
}
interface FaceclawKitKotlinBridgeListener {
transformMessage(message: string): string;
}
declare const FaceclawKitKotlinBridgeListener: {
prototype: FaceclawKitKotlinBridgeListener;
};
@NativeClass()
class KotlinCallback extends NSObject implements FaceclawKitKotlinBridgeListener {
static ObjCProtocols = [FaceclawKitKotlinBridgeListener];
transform!: (message: string) => string;
transformMessage(message: string): string { return this.transform(message); }
}
export function createKotlinBridge(): KotlinBridge {
runKotlinProtocolSmokeTest();
const bridge = FaceclawKitKotlinBridge.alloc().init();
return {
platform: () => bridge.platform(),
threadName: () => bridge.threadName(),
isMainThread: () => bridge.isMainThread(),
greet: name => bridge.greetName(name),
roundTrip(message, transform) {
const listener = KotlinCallback.new() as KotlinCallback;
listener.transform = transform;
// Kotlin uses this listener synchronously and never retains it.
return bridge.roundTripMessageListener(message, listener);
},
};
}
-46
View File
@@ -1,46 +0,0 @@
import { GrayImage } from "../graphics/image"
import { encodeShellScene } from "../graphics/shell-scene"
import { decodeImageBytes } from './image-bytes.ios'
import * as protocol from '../g2/ble-protocol.ios'
import { SurfaceCompositor } from '../graphics/surface-compositor.ios'
import { lvglMetrics } from './lvgl-font.ios'
/** Development-only checks of real NativeScript selectors and bulk data crossings. */
export function runKotlinProtocolSmokeTest(): void {
const hex = (data: Uint8Array) => Array.from(data, byte => byte.toString(16).padStart(2, '0')).join('')
const assert = (condition: boolean, what: string) => { if (!condition) throw new Error(`Kotlin protocol bridge: ${what}`) }
const auth = protocol.authentication(101)
assert(hex(auth) === '080410651a0408011004', 'authentication')
assert(protocol.crc16(new Uint8Array([1, 2, 3])) === 0xadad, 'CRC')
const receiver = new protocol.MessageReceiver()
const frame = protocol.frameMessage(auth, 128, 0, 64)[0]
const received = receiver.receive('test', frame)
assert(received.length === 1 && received[0].magic === 101 && hex(received[0].payload) === hex(auth), 'reassembly')
const payload = protocol.bytes(13, protocol.bytes(3, protocol.concat(protocol.integer(1, 12), protocol.integer(2, 2))))
const event = protocol.decodeGlassesInput({ sid: 224, flag: 1, payload, command: 0, magic: 0 })
assert(event?.kind === 'display-wake' && event.eventSource === 2, 'event')
const c = new SurfaceCompositor(3, 2)
c.configureSurface('test', { x: 0, y: 0, width: 3, height: 2, zOrder: 0, transparency: 'opaque' })
c.submitSurfaceFrame('test', new Uint8Array([0, 16, 255, 32, 48, 64]), { x: 0, y: 0, width: 3, height: 2 })
assert(hex(c.composite()) === '0010ff203040', 'composition')
assert(hex(protocol.packGray4(c.composite(), 3, 2)) === '01f02340', 'packing')
c.setShellScene(new Uint8Array([1,0,1,0,0,0,0,0,1,0,1,0,128,0,0,0,0,0,255]))
assert(hex(c.composite()) === 'f00070101020', 'shell scene bridge and 4bpp composition')
c.setScreenBlanked(true); assert(c.composite().every(p => p === 0), 'blanking')
const selectedSurface = new GrayImage(8, 4, 1)
selectedSurface.drawMenuSelection(new GrayImage(4, 2, 255), 1, 1, 16, 48, 0, 2)
const selectedPreview = new SurfaceCompositor(8, 4)
selectedPreview.setShellScene(encodeShellScene([{ image: selectedSurface, x: 0, y: 0, shellKey: 100 }]))
const selectedPixels = selectedPreview.composite()
assert(selectedPixels[10] === 240 && selectedPixels[9] === 0, 'menu selection depth and bridge')
assert(lvglMetrics('/nonexistent/faceclaw-font').length === 0, 'font bridge')
const compass = protocol.decodeCompassInput({ sid: 8, flag: 1, command: 15, magic: 0,
payload: new Uint8Array([8, 15, 16, 0, 82, 3, 8, 231, 2, 162, 6, 12, 67, 77, 1, 3, 2, 3, 140, 0, 152, 186, 220, 254]) })
if (compass?.headingDegrees !== 359 || compass.diagnostics?.sampleTimeMs !== 0xfedcba98)
throw new Error('Kotlin compass notification mismatch')
const png = NSData.alloc().initWithBase64EncodedStringOptions('iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAEklEQVR4nGP4z8DAAMIM/4EAAB/uBfsL2WiLAAAAAElFTkSuQmCC', 0 as NSDataBase64DecodingOptions)
const decoded = decodeImageBytes(interop.bufferFromData(png), 2, 2)
assert(!!decoded && Array.from(decoded.pixels).join(',') === '54,182,18,255', 'native map image orientation and grayscale')
assert(decodeImageBytes(new ArrayBuffer(0), 2, 2) === null, 'invalid map image')
console.log('FACECLAW_KOTLIN_PROTOCOL_PASS iOS')
}
-22
View File
@@ -1,22 +0,0 @@
import { copyToJavaByteBuffer } from './java-direct-buffer'
declare const com: any
/** Development-only checks of NativeScript metadata and bulk ByteBuffer crossings. */
export function runKotlinProtocolSmokeTest(): void {
const native = com.faceclaw.app
const hex = (data: any) => Array.from({ length: data.length }, (_, i) => (data[i] & 255).toString(16).padStart(2, '0')).join('')
const assert = (condition: boolean, what: string) => { if (!condition) throw new Error(`Kotlin protocol bridge: ${what}`) }
const auth = native.BleProtocol.buildAuthenticationRequest(101)
assert(hex(auth) === '080410651a0408011004', 'authentication')
const compositor = new native.SurfaceCompositor()
compositor.configureScreen(3, 2)
compositor.configureSurface('test', 0, 0, 3, 2, 0, 0)
const pixels = copyToJavaByteBuffer(new Uint8Array([0, 16, 255, 32, 48, 64]))
compositor.submitSurface('test', new native.AndroidByteReader(pixels), 0, 0, 3, 2, '1')
assert(hex(compositor.composite().gray) === '0010ff203040', 'composition/ByteBuffer')
assert(hex(native.BmpUtil.pack4bppFromGray8(compositor.composite().gray, 3, 2)) === '01f02340', 'packing')
const id = native.ImageAtlas.ensure('kotlin-smoke', 3, 2, new native.AndroidByteReader(pixels))
assert(id > 0 && native.ImageAtlas.get(id).width === 3, 'image atlas/reused buffer')
console.log('FACECLAW_KOTLIN_PROTOCOL_PASS Android')
}
+2 -2
View File
@@ -30,8 +30,8 @@ type PendingRequest<T> = {
* Holds at most one connection at a time.
*/
export class FaceclawMediaBrowserBridge {
private browser: any | null = null;
private listenerProxy: any | null = null;
private browser: any = null;
private listenerProxy: any = null;
private nextRequestId = 1;
private readonly pendingConnects = new Map<number, PendingRequest<string>>();
private readonly pendingBrowses = new Map<number, PendingRequest<MediaBrowseItem[]>>();
+2 -2
View File
@@ -59,8 +59,8 @@ const DEFAULT_MEDIA_STATE: MediaControllerState = {
export class FaceclawMediaControllerBridge {
private readonly stateListeners = new Set<(state: MediaControllerState) => void>();
private controller: any | null = null;
private listenerProxy: any | null = null;
private controller: any = null;
private listenerProxy: any = null;
private state: MediaControllerState = { ...DEFAULT_MEDIA_STATE };
private stateUpdatedAtMs = Date.now();
private started = false;
+3 -4
View File
@@ -1,5 +1,5 @@
import { nightscoutApiTokenSetting, nightscoutSiteUrlSetting } from "../ui/dashboard-settings";
import { fetchWithUserAgent } from "../util/http";
import { fetchTextWithUserAgent } from "../util/http";
export type NightscoutPoint = {
timestampMs: number;
@@ -106,7 +106,6 @@ type NightscoutTreatmentResponse = {
const NIGHTSCOUT_REFRESH_MS = 60_000;
const NIGHTSCOUT_HISTORY_COUNT = 30;
const NIGHTSCOUT_GRAPH_WINDOW_MS = 2 * 60 * 60 * 1000;
const NIGHTSCOUT_TREATMENT_LOOKBACK_MS = 3 * 60 * 60 * 1000;
// The v1 treatments API silently adds `created_at >= now - 4 days` to any
// query without an explicit date filter (lib/server/query.js enforceDateFilter),
@@ -305,11 +304,11 @@ export class NightscoutBridge {
}
async function fetchJson<T>(url: string): Promise<T> {
const response = await fetchWithUserAgent(url);
const response = await fetchTextWithUserAgent(url);
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
return (await response.json()) as T;
return response.json<T>();
}
function parseTreatments(treatments: NightscoutTreatmentResponse[], nowMs: number): {
+5 -2
View File
@@ -36,8 +36,11 @@ function bell(): GrayImage {
return image
}
export function readActiveNotificationIcons(maxIcons: number, _allowStale: boolean) {
const sources = new Set(readActiveNotifications(ALL_NOTIFICATIONS).map(n => n.packageName))
return {icons: Array.from(sources).slice(0,Math.max(0,maxIcons)).map(() => bell()),stale:false}
// One icon per source, standing for its first notification.
const sources = new Map<string, string>()
for (const n of readActiveNotifications(ALL_NOTIFICATIONS)) if (!sources.has(n.packageName)) sources.set(n.packageName, n.key)
const keys = Array.from(sources.values()).slice(0,Math.max(0,maxIcons))
return {icons: keys.map(() => bell()),keys,stale:false}
}
export function readNotificationIconByKey(key: string, _allowStale: boolean) {
return {icon: readActiveNotifications(ALL_NOTIFICATIONS).some(n => n.key === key) ? bell() : null,stale:false}
+19 -5
View File
@@ -4,6 +4,7 @@ import { toUint8Array } from "../util/array-util";
import { rememberNotificationSources } from "./notification-sources";
declare const com: any;
declare const java: any;
const ICON_SIZE = 24;
/** Ask the native listener for every active source, including those below the list's display limit. */
@@ -17,10 +18,11 @@ export const ALL_NOTIFICATIONS = 0x7fffffff;
const ICON_CACHE_MS = 60_000;
let cachedIcons: GrayImage[] = [];
let cachedKeys: string[] = [];
let cachedAtMs = 0;
const keyedIconCache = new Map<string, { icon: GrayImage | null; atMs: number }>();
const KEYED_ICON_CACHE_MAX = 128;
let notificationListenerProxy: any | null = null;
let notificationListenerProxy: any = null;
const notificationPostedListeners = new Set<(notificationKey: string) => void>();
function invalidateIconCaches(): void {
@@ -33,6 +35,11 @@ export type { AndroidNotification, AndroidNotificationAction } from "./notificat
export type NotificationIconsResult = {
icons: GrayImage[];
/**
* The notification each icon stands for, by key, in step with icons: the
* member of its group (which gets one icon) the icon was taken from.
*/
keys: string[];
/** True when the icons came from an expired (or empty) cache under allowStale. */
stale: boolean;
};
@@ -46,23 +53,25 @@ export type NotificationIconsResult = {
* on what is in the tray.
*/
export function readActiveNotificationIcons(maxIcons: number, allowStale: boolean): NotificationIconsResult {
if (!global.isAndroid || maxIcons <= 0) return { icons: [], stale: false };
if (!global.isAndroid || maxIcons <= 0) return { icons: [], keys: [], stale: false };
const now = Date.now();
if (cachedAtMs > 0 && now - cachedAtMs < ICON_CACHE_MS) {
logCurrent("notification icons served from cache");
return { icons: cachedIcons.map(icon => icon.clone()), stale: false };
return { icons: cachedIcons.map(icon => icon.clone()), keys: cachedKeys.slice(), stale: false };
}
if (allowStale) {
logCurrent("notification icons served stale");
return { icons: cachedIcons.map(icon => icon.clone()), stale: true };
return { icons: cachedIcons.map(icon => icon.clone()), keys: cachedKeys.slice(), stale: true };
}
const keyList = new java.util.ArrayList();
const bytes = spanCurrent("fetch-notification-icons", () =>
toUint8Array(
com.faceclaw.app.FaceclawMediaNotificationListenerService.getActiveNotificationIconGrays(
ICON_SIZE,
maxIcons,
keyList,
),
),
);
@@ -74,10 +83,15 @@ export function readActiveNotificationIcons(maxIcons: number, allowStale: boolea
icon.pixels.set(bytes.subarray(index * iconByteLength, (index + 1) * iconByteLength));
icons.push(icon);
}
const keys: string[] = [];
for (let index = 0; index < iconCount; index++) {
keys.push(index < keyList.size() ? String(keyList.get(index)) : "");
}
cachedIcons = icons;
cachedKeys = keys;
cachedAtMs = now;
return { icons: icons.map(icon => icon.clone()), stale: false };
return { icons: icons.map(icon => icon.clone()), keys: keys.slice(), stale: false };
}
export type NotificationIconResult = {
+1 -1
View File
@@ -288,7 +288,7 @@ function extractMessageText(item: any): string {
.join("");
}
function parseSseDataLine(line: string): any | null {
function parseSseDataLine(line: string): any {
if (!line.startsWith("data:")) return null;
const payload = line.slice(5).trim();
if (!payload || payload === "[DONE]") return null;
+18
View File
@@ -7,7 +7,25 @@ export type PhoneBatteryState = {
charging: boolean | null;
};
// The Android read is a sticky-broadcast binder round trip, and the top bar
// asks on every shell paint; the level moves far slower than that.
const ANDROID_CACHE_MS = 5000;
let cachedState: PhoneBatteryState | null = null;
let cachedAtMs = 0;
export function readPhoneBatteryState(): PhoneBatteryState {
if (global.isAndroid) {
const now = Date.now();
if (cachedState === null || now - cachedAtMs >= ANDROID_CACHE_MS || now < cachedAtMs) {
cachedState = readPhoneBatteryStateUncached();
cachedAtMs = now;
}
return { ...cachedState };
}
return readPhoneBatteryStateUncached();
}
function readPhoneBatteryStateUncached(): PhoneBatteryState {
if (global.isIOS) {
const device = UIDevice.currentDevice
const level = device.batteryLevel
+16 -2
View File
@@ -1,5 +1,5 @@
import { File, knownFolders, path, type ImageSource } from "@nativescript/core";
import { SurfaceCompositor } from "../graphics/surface-compositor";
import { SurfaceCompositor } from "../graphics/surface-compositor.ios";
import { G2_LENS_WIDTH, G2_LENS_HEIGHT } from "../graphics/image";
import { previewPixels } from "./ios-graphics";
import { toData } from "./kotlin-data";
@@ -18,6 +18,7 @@ export type DisplayTarget = Pick<
| "setUnderlayDim"
| "setScreenBlanked"
| "submitSurfaceFrame"
| "isSurfaceCurrent"
| "submitShellScene"
| "waitForFrameFinished"
| "getCompositePreview"
@@ -44,14 +45,20 @@ export class PreviewDisplayTarget implements DisplayTarget {
private onFrameComposited: (() => void) | null = null;
private released = false;
/** Receive a callback per applied frame, the stand-in for the connected path's frame-finished callback. */
/**
* Receive a callback per applied frame, the stand-in for the connected
* path's frame-finished callback, and per step of an animation the
* preview is replaying.
*/
activate(onFrameComposited: () => void): void {
this.onFrameComposited = onFrameComposited;
this.compositor.setPreviewAnimationListener(() => this.composited());
}
release(): void {
this.released = true;
this.onFrameComposited = null;
this.compositor.setPreviewAnimationListener(null);
}
private composited(): void {
@@ -60,7 +67,9 @@ export class PreviewDisplayTarget implements DisplayTarget {
async configureCompositorScreen(width: number, height: number): Promise<void> {
if (Math.round(width) !== this.compositor.width || Math.round(height) !== this.compositor.height) {
this.compositor.setPreviewAnimationListener(null);
this.compositor = new SurfaceCompositor(Math.round(width), Math.round(height));
if (this.onFrameComposited) this.compositor.setPreviewAnimationListener(() => this.composited());
}
}
@@ -109,6 +118,11 @@ export class PreviewDisplayTarget implements DisplayTarget {
this.composited();
}
/** Never skips: preview frames are cheap and nothing is transmitted. */
isSurfaceCurrent(_surfaceId: string, _fingerprint: string): boolean {
return false;
}
async submitShellScene(bytes: Uint8Array, _paintMs = 0, _frameId = 0): Promise<void> {
this.compositor.setShellScene(bytes);
this.composited();
+6
View File
@@ -24,6 +24,7 @@ export type DisplayTarget = Pick<
| "setUnderlayDim"
| "setScreenBlanked"
| "submitSurfaceFrame"
| "isSurfaceCurrent"
| "submitShellScene"
| "waitForFrameFinished"
| "getCompositePreview"
@@ -109,6 +110,11 @@ export class PreviewDisplayTarget implements DisplayTarget {
this.compositor.setSurfaceDepth(id, Math.round(depth));
}
/** Never skips: preview frames are cheap and nothing is transmitted. */
isSurfaceCurrent(_surfaceId: string, _fingerprint: string): boolean {
return false;
}
async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise<void> {
this.compositor.submitShellScene(this.shellSceneBuffer.load(bytes), paintMs, frameId);
}
+2 -1
View File
@@ -14,7 +14,8 @@ export function scanQrCode(): Promise<string | null> {
if (settled) return
settled = true
if (activeScanner === scanner) activeScanner = null
error ? reject(new Error(String(error))) : resolve(text == null ? null : String(text))
if (error) reject(new Error(String(error)))
else resolve(text == null ? null : String(text))
}
try { scanner.startWithCompletion(finish) }
catch (error) { finish(null, String(error)) }
+81 -16
View File
@@ -8,17 +8,35 @@
import { Utils } from "@nativescript/core";
declare const com: any;
declare const android: any;
let javaInstance: any = null;
// The Java-side listener proxy must stay referenced or it gets GC'd.
let retainedListenerProxy: any = null;
const changeListeners = new Set<(key: string) => void>();
// Values already read through JNI, which cost a bridge call each and are read
// many times per paint (display mode, fonts, clock format). Only used while
// this isolate's Java listener is registered on a thread with a Looper: every
// write, from any isolate, notifies that listener, which drops the key before
// any other change listener runs. A write from another isolate is visible here
// once its notification has been dispatched (one message-loop tick), the same
// point at which change listeners hear about it.
// Strings are cached without a default (null = unset). Booleans are cached per
// default, since the Java getter cannot report "unset", except after a write
// from this isolate (defaultValue null: stored, so any default reads it).
const stringCache = new Map<string, string | null>();
const booleanCache = new Map<string, { defaultValue: boolean | null; value: boolean }>();
let cachingEnabled: boolean | null = null;
/** Release this isolate's native observer before its worker is terminated. */
export function disposeSettingsStore(): void {
changeListeners.clear();
if (retainedListenerProxy !== null) getJava().unregisterListener(retainedListenerProxy);
retainedListenerProxy = null;
cachingEnabled = false;
stringCache.clear();
booleanCache.clear();
}
function getJava(): any {
@@ -31,20 +49,80 @@ function getJava(): any {
return javaInstance;
}
function canCache(): boolean {
if (cachingEnabled === null) {
cachingEnabled = false;
try {
// FaceclawSettings dispatches notifications through the registering
// thread's Looper; without one this isolate would never hear of writes.
if (android.os.Looper.myLooper() !== null) {
ensureJavaListener();
cachingEnabled = true;
}
} catch {
cachingEnabled = false;
}
}
return cachingEnabled;
}
export function getStringSetting(key: string, defaultValue: string): string {
return String(getJava().getString(key, defaultValue));
if (!canCache()) return String(getJava().getString(key, defaultValue));
let value = stringCache.get(key);
if (value === undefined) {
const raw = getJava().getString(key, null);
value = raw === null || raw === undefined ? null : String(raw);
stringCache.set(key, value);
}
return value ?? defaultValue;
}
export function setStringSetting(key: string, value: string): void {
getJava().setString(key, value);
if (cachingEnabled) stringCache.set(key, value === null || value === undefined ? null : String(value));
}
export function getBooleanSetting(key: string, defaultValue: boolean): boolean {
return Boolean(getJava().getBoolean(key, defaultValue));
if (!canCache()) return Boolean(getJava().getBoolean(key, defaultValue));
const cached = booleanCache.get(key);
if (cached !== undefined && (cached.defaultValue === null || cached.defaultValue === defaultValue)) {
return cached.value;
}
const value = Boolean(getJava().getBoolean(key, defaultValue));
booleanCache.set(key, { defaultValue, value });
return value;
}
export function setBooleanSetting(key: string, value: boolean): void {
getJava().setBoolean(key, value);
if (cachingEnabled) booleanCache.set(key, { defaultValue: null, value: Boolean(value) });
}
function forgetCachedSetting(key: string | null | undefined): void {
if (key === null || key === undefined) {
stringCache.clear();
booleanCache.clear();
return;
}
stringCache.delete(key);
booleanCache.delete(key);
}
function ensureJavaListener(): void {
if (retainedListenerProxy !== null) return;
retainedListenerProxy = new com.faceclaw.app.FaceclawSettingsListener({
onSettingChanged: (key: string) => {
forgetCachedSetting(key === null || key === undefined ? key : String(key));
for (const registered of Array.from(changeListeners)) {
try {
registered(String(key));
} catch (error) {
console.warn("settings change listener failed", error);
}
}
},
});
getJava().registerListener(retainedListenerProxy);
}
/**
@@ -53,20 +131,7 @@ export function setBooleanSetting(key: string, value: boolean): void {
* change.
*/
export function onSettingsStoreChanged(listener: (key: string) => void): () => void {
if (retainedListenerProxy === null) {
retainedListenerProxy = new com.faceclaw.app.FaceclawSettingsListener({
onSettingChanged: (key: string) => {
for (const registered of Array.from(changeListeners)) {
try {
registered(String(key));
} catch (error) {
console.warn("settings change listener failed", error);
}
}
},
});
getJava().registerListener(retainedListenerProxy);
}
ensureJavaListener();
changeListeners.add(listener);
return () => {
changeListeners.delete(listener);
-42
View File
@@ -1,42 +0,0 @@
import { Utils } from "@nativescript/core";
import { GrayImage } from "../graphics/image";
import { spanCurrent } from "./frame-timings";
import { toUint8Array } from "~/util/array-util";
declare const com: any;
const ICON_SIZE = 24;
const ICON_CACHE_MS = 5_000;
let cachedIcons: GrayImage[] = [];
let cachedAtMs = 0;
export function readSystemStatusIcons(): GrayImage[] {
if (!global.isAndroid) return [];
const now = Date.now();
if (now - cachedAtMs < ICON_CACHE_MS) {
return cachedIcons.map(cachedIcon => cachedIcon.clone());
}
const context = Utils.android.getApplicationContext();
if (!context) return [];
const bytes = spanCurrent("fetch-system-status-icons", () =>
toUint8Array(
com.faceclaw.app.FaceclawSystemStatusIconProvider.getSystemStatusIconGrays(context, ICON_SIZE),
),
);
const iconByteLength = ICON_SIZE * ICON_SIZE;
const iconCount = Math.floor(bytes.length / iconByteLength);
const icons: GrayImage[] = [];
for (let index = 0; index < iconCount; index++) {
const icon = new GrayImage(ICON_SIZE, ICON_SIZE, 0);
icon.pixels.set(bytes.subarray(index * iconByteLength, (index + 1) * iconByteLength));
icons.push(icon);
}
cachedIcons = icons;
cachedAtMs = now;
return icons.map(icon => icon.clone());
}
+148
View File
@@ -0,0 +1,148 @@
/**
* Bridge to the Android FaceclawVideoPlayer: decodes a video file and
* streams its frames, scaled to gray, straight into a rect of a window's
* compositor surface, with the audio on the phone's default media route.
* Android only; on iOS probeVideo reports an error and nothing plays.
*/
import { Utils } from "@nativescript/core";
import { GrayImage } from "../graphics/image";
declare const com: any;
declare const java: any;
declare const global: any;
/** Containers MediaExtractor opens (AVI is not one of them). */
const PLAYABLE_VIDEO = /\.(mp4|m4v|mkv|webm|3gp|mov)$/i;
export function isPlayableVideoFile(name: string): boolean {
return PLAYABLE_VIDEO.test(name);
}
export type VideoInfo = {
/** Display size, rotation already applied. */
width: number;
height: number;
durationMs: number;
hasAudio: boolean;
};
/** Read a video's size, duration and tracks; an error string when it can't be played. */
export function probeVideo(path: string): VideoInfo | { error: string } {
if (!global.isAndroid) return { error: "Video playback is not supported on this phone" };
try {
const raw = JSON.parse(String(com.faceclaw.app.FaceclawVideoPlayer.probe(path)));
if (raw.error) return { error: String(raw.error) };
return {
width: Number(raw.width) || 0,
height: Number(raw.height) || 0,
durationMs: Number(raw.durationMs) || 0,
hasAudio: Boolean(raw.hasAudio),
};
} catch (error) {
return { error: String(error) };
}
}
export type VideoPlaybackState = "playing" | "paused" | "ended";
export type VideoPlayerCallbacks = {
/** State change, or a paused seek that landed on a new frame (state "paused"). */
onState: (state: VideoPlaybackState, positionMs: number) => void;
onError: (message: string) => void;
};
export type VideoStartOptions = {
path: string;
surfaceId: string;
/** The video's rect in surface coordinates. */
rect: { x: number; y: number; width: number; height: number };
monochrome: boolean;
/** Tone curve for gray output (ignored for monochrome); see PHOTO_GAMMA. */
gamma: number;
audio: boolean;
startMs: number;
};
/** One player; frames go to whatever surface the latest start() named. Main thread only. */
export class VideoPlayer {
private readonly player: any;
// The Java listener proxy must stay referenced or it gets GC'd.
private readonly listenerProxy: any;
// Reused direct buffer (and its JS view) for copyLastFrame; see java-direct-buffer.ts
// for why frames cross as a Java-allocated buffer.
private frameBuffer: any = null;
private frameView: Uint8Array | null = null;
constructor(callbacks: VideoPlayerCallbacks) {
this.player = new com.faceclaw.app.FaceclawVideoPlayer(Utils.android.getApplicationContext());
this.listenerProxy = new com.faceclaw.app.FaceclawVideoPlayer.Listener({
onStateChanged: (state: string, positionMs: number) => {
callbacks.onState(String(state) as VideoPlaybackState, Number(positionMs));
},
onError: (message: string) => callbacks.onError(String(message ?? "Playback failed")),
});
this.player.setListener(this.listenerProxy);
}
start(options: VideoStartOptions): void {
const { rect } = options;
this.player.start(
options.path,
options.surfaceId,
Math.round(rect.x),
Math.round(rect.y),
Math.round(rect.width),
Math.round(rect.height),
options.monochrome,
options.gamma,
options.audio,
Math.max(0, Math.round(options.startMs)),
);
}
/** Once this returns, no more frames reach the surface until resume(). */
pause(): void {
this.player.pause();
}
resume(): void {
this.player.resume();
}
seekTo(positionMs: number): void {
this.player.seekTo(Math.max(0, Math.round(positionMs)));
}
positionMs(): number {
return Number(this.player.getPositionMs());
}
isPlaying(): boolean {
return Boolean(this.player.isPlaying());
}
/** The latest decoded frame at the given size, or null before one exists. */
lastFrame(width: number, height: number): GrayImage | null {
const bytes = width * height;
if (bytes <= 0) return null;
if (!this.frameView || this.frameView.length !== bytes) {
this.frameBuffer = java.nio.ByteBuffer.allocateDirect(bytes);
this.frameView = new Uint8Array((ArrayBuffer as any).from(this.frameBuffer));
}
if (!this.player.copyLastFrame(this.frameBuffer)) return null;
const image = new GrayImage(width, height, 0);
image.pixels.set(this.frameView);
return image;
}
/** End playback and free the decoder and audio (the last frame stays readable). */
stop(): void {
this.player.stop();
}
release(): void {
this.player.release();
this.frameBuffer = null;
this.frameView = null;
}
}
+3 -3
View File
@@ -64,8 +64,8 @@ export class IosVoiceControlBridge {
const generation = ++this.generation
this.log = log; this.source = 'glasses'; this.session = session; this.packets = 0; this.capturing = true
this.completion = new Promise(resolve => { this.resolveCompletion = resolve })
const error = String(this.ensureNative().startWithEndpointing(endpointing) ?? '')
if (error) { this.stop(); this.setStatus(error); this.emitEnd(); return }
const startError = String(this.ensureNative().startWithEndpointing(endpointing) ?? '')
if (startError) { this.stop(); this.setStatus(startError); this.emitEnd(); return }
try {
await session.setMicrophone(true, packet => {
if (generation !== this.generation || !this.capturing) return
@@ -136,7 +136,7 @@ export class IosVoiceControlBridge {
this.log(`Voice audio: ${event.seconds?.toFixed(1)}s, packets=${event.packets}, RMS=${event.rms?.toFixed(4)}, missing=${event.missing}, errors=${event.errors}`)
if (this.capturing) this.setStatus(`Listening on ${this.source === 'phone' ? 'phone' : 'glasses'}… ${event.seconds?.toFixed(0)}s`)
} else if (event.kind === 'finishing') {
this.stopPushToTalk(); this.emitEnd()
void this.stopPushToTalk(); this.emitEnd()
} else if (event.kind === 'ended') {
this.source = null
this.capturing = this.finalizing = false
+2 -2
View File
@@ -102,8 +102,8 @@ export class FaceclawVoiceControlBridge {
private readonly speechPauseListeners = new Set<() => void>();
private readonly speechPause = new SpeechPauseDetector();
private readonly speechEndListeners = new Set<() => void>();
private controller: any | null = null;
private listenerProxy: any | null = null;
private controller: any = null;
private listenerProxy: any = null;
private status = "Voice control stopped.";
private listening = false;
private detail = "";
+4 -2
View File
@@ -16,6 +16,8 @@ declare const com: any;
export const WEAR_PATHS = {
/** Watch -> phone: a ring-style gesture. */
input: "/faceclaw/input",
/** Watch -> phone: a finger landed on the pad (the ring-press ahead of a gesture); never acked. */
press: "/faceclaw/press",
/** Watch -> phone: a shell command (launch app, wake, lock, ...). */
command: "/faceclaw/command",
/** Watch -> phone: a text query for the assistant. */
@@ -52,14 +54,14 @@ type MessageListener = (message: WearMessage) => void;
type ConnectionListener = (connection: WearWatchConnection) => void;
class WearBridge {
private java: any | null | undefined = undefined;
private java: any = undefined;
// The Java-side listener proxy must stay referenced or it gets GC'd.
private retainedListenerProxy: any = null;
private readonly messageListeners = new Set<MessageListener>();
private readonly connectionListeners = new Set<ConnectionListener>();
private lastConnection: WearWatchConnection = { reachable: false, watchName: "" };
private getJava(): any | null {
private getJava(): any {
if (this.java !== undefined) return this.java;
if (!global.isAndroid) {
this.java = null;
+244 -14
View File
@@ -1,10 +1,25 @@
import { hasLocationPermission } from "./location-permissions";
import { getCurrentLocation, type CurrentLocation } from "./location";
import { getStringSetting, setStringSetting } from "./settings-store";
import { fetchWithUserAgent } from "../util/http";
import { USER_AGENT } from "../version";
export type WeatherPhase = "permission-required" | "locating" | "loading" | "ready" | "error";
/** Sky and precipitation state, as coarse as the icons that show it. */
export type WeatherCondition =
| "clear"
| "partly-cloudy"
| "cloudy"
| "fog"
| "wind"
| "drizzle"
| "showers"
| "rain"
| "thunderstorm"
| "snow"
| "sleet";
export type CurrentWeather = {
temperatureF: number | null;
description: string;
@@ -13,6 +28,8 @@ export type CurrentWeather = {
windDirection: string;
timestampMs: number | null;
observed: boolean;
condition: WeatherCondition | null;
isDaytime: boolean;
};
export type ForecastPeriod = {
@@ -25,6 +42,7 @@ export type ForecastPeriod = {
windSpeed: string;
windDirection: string;
isDaytime: boolean;
condition: WeatherCondition | null;
};
export type WeatherState = {
@@ -64,6 +82,7 @@ type NwsForecastPeriodResponse = {
windSpeed?: unknown;
windDirection?: unknown;
isDaytime?: unknown;
icon?: unknown;
};
type NwsStationsResponse = {
@@ -74,6 +93,7 @@ type NwsObservationResponse = {
properties?: {
timestamp?: unknown;
textDescription?: unknown;
icon?: unknown;
temperature?: NwsMeasure;
relativeHumidity?: NwsMeasure;
windSpeed?: NwsMeasure;
@@ -92,9 +112,17 @@ const NWS_HEADERS = {
// NWS asks callers to identify themselves with a contact address.
"User-Agent": `${USER_AGENT} (https://github.com/jimrandomh/faceclaw)`,
};
const WEATHER_REFRESH_MS = 30 * 60 * 1000;
/** Refresh period while the Weather app is open. */
const APP_REFRESH_MS = 30 * 60 * 1000;
/** Refresh period for the status-bar and system-card indicators. */
const BACKGROUND_REFRESH_MS = 60 * 60 * 1000;
/** Delay before retrying after a failed refresh (capped at the period). */
const RETRY_AFTER_FAILURE_MS = 10 * 60 * 1000;
const FETCH_TIMEOUT_MS = 20_000;
const MAX_FORECAST_PERIODS = 14;
/** Settings-store key holding the last successful refresh. */
const CACHE_KEY = "weather.cache";
const CACHE_VERSION = 1;
const DEFAULT_STATE: WeatherState = {
phase: "permission-required",
@@ -105,12 +133,31 @@ const DEFAULT_STATE: WeatherState = {
lastUpdatedMs: null,
};
/** Shared weather state and periodic refresh, active only while its app is open. */
type WeatherCache = {
version: number;
locationName: string;
current: CurrentWeather | null;
forecast: ForecastPeriod[];
lastUpdatedMs: number;
};
/**
* Shared weather state, refreshed on a schedule while anything shows it:
* every 30 minutes while the Weather app is open, and hourly while a weather
* indicator (status bar or Glanceboard system card) is enabled and the
* glasses session is up. The last successful refresh is persisted, so a
* restart shows it at once and the schedule counts from its age.
*/
export class WeatherBridge {
private readonly listeners = new Set<(state: WeatherState) => void>();
private state: WeatherState = cloneState(DEFAULT_STATE);
private refreshHandle: ReturnType<typeof setInterval> | null = null;
private loadedState: WeatherState | null = null;
private refreshTimer: ReturnType<typeof setTimeout> | null = null;
private refreshInFlight: Promise<void> | null = null;
/** Start of the latest refresh attempt, successful or not. */
private lastAttemptMs: number | null = null;
private appOpen = false;
private sessionActive = false;
private backgroundWanted = false;
onStateChange(listener: (state: WeatherState) => void): () => void {
this.listeners.add(listener);
@@ -122,25 +169,40 @@ export class WeatherBridge {
return cloneState(this.state);
}
/** The Weather app opened (or asked to retry): refresh now, then every 30 minutes. */
start(): void {
if (!this.refreshHandle) {
this.refreshHandle = setInterval(() => void this.refreshNow(), WEATHER_REFRESH_MS);
}
this.appOpen = true;
void this.refreshNow();
}
/** The Weather app closed; any indicator schedule continues. */
stop(): void {
if (this.refreshHandle) {
clearInterval(this.refreshHandle);
this.refreshHandle = null;
}
this.appOpen = false;
this.schedule();
}
/** Whether a glasses session is up; indicator refreshes only run during one. */
setSessionActive(active: boolean): void {
if (active === this.sessionActive) return;
this.sessionActive = active;
this.schedule();
}
/** Whether an indicator wants hourly refreshes (see app/apps/weather/weather-indicators.ts). */
setBackgroundRefresh(enabled: boolean): void {
if (enabled === this.backgroundWanted) return;
this.backgroundWanted = enabled;
this.schedule();
}
async refreshNow(): Promise<void> {
if (this.refreshInFlight) return this.refreshInFlight;
this.clearRefreshTimer();
this.lastAttemptMs = Date.now();
if (!hasLocationPermission()) {
this.state = cloneState(DEFAULT_STATE);
this.emit();
this.schedule();
return;
}
@@ -149,6 +211,48 @@ export class WeatherBridge {
await this.refreshInFlight;
} finally {
this.refreshInFlight = null;
this.schedule();
}
}
private get state(): WeatherState {
if (!this.loadedState) this.loadedState = loadCachedState();
return this.loadedState;
}
private set state(state: WeatherState) {
this.loadedState = state;
}
private refreshIntervalMs(): number | null {
if (this.appOpen) return APP_REFRESH_MS;
if (this.sessionActive && this.backgroundWanted) return BACKGROUND_REFRESH_MS;
return null;
}
/**
* Arm the timer for the next refresh: one period after the last success,
* but no sooner than the retry delay after a failed attempt.
*/
private schedule(): void {
this.clearRefreshTimer();
// A running refresh reschedules when it settles.
if (this.refreshInFlight) return;
const intervalMs = this.refreshIntervalMs();
if (intervalMs === null) return;
const lastSuccessMs = this.state.lastUpdatedMs ?? -Infinity;
const lastAttemptMs = this.lastAttemptMs ?? -Infinity;
const dueMs = Math.max(lastSuccessMs + intervalMs, lastAttemptMs + Math.min(intervalMs, RETRY_AFTER_FAILURE_MS));
this.refreshTimer = setTimeout(() => {
this.refreshTimer = null;
void this.refreshNow();
}, Math.max(0, dueMs - Date.now()));
}
private clearRefreshTimer(): void {
if (this.refreshTimer) {
clearTimeout(this.refreshTimer);
this.refreshTimer = null;
}
}
@@ -169,6 +273,7 @@ export class WeatherBridge {
forecast: weather.forecast,
lastUpdatedMs: Date.now(),
};
saveCachedState(this.state);
this.emit();
} catch (error) {
const message = friendlyWeatherError(error);
@@ -188,6 +293,51 @@ export class WeatherBridge {
}
}
/** The persisted last refresh, or the default state when there is none (or no permission to refresh it). */
function loadCachedState(): WeatherState {
try {
if (!hasLocationPermission()) return cloneState(DEFAULT_STATE);
const raw = getStringSetting(CACHE_KEY, "");
if (!raw) return cloneState(DEFAULT_STATE);
const cache = JSON.parse(raw) as Partial<WeatherCache> | null;
if (
!cache ||
cache.version !== CACHE_VERSION ||
typeof cache.lastUpdatedMs !== "number" ||
!Array.isArray(cache.forecast)
) {
return cloneState(DEFAULT_STATE);
}
return cloneState({
phase: "ready",
status: "Weather updated.",
locationName: typeof cache.locationName === "string" ? cache.locationName : "",
current: cache.current && typeof cache.current === "object" ? cache.current : null,
forecast: cache.forecast,
lastUpdatedMs: cache.lastUpdatedMs,
});
} catch (error) {
console.warn(`weather cache unreadable: ${error}`);
return cloneState(DEFAULT_STATE);
}
}
function saveCachedState(state: WeatherState): void {
if (state.lastUpdatedMs === null) return;
const cache: WeatherCache = {
version: CACHE_VERSION,
locationName: state.locationName,
current: state.current,
forecast: state.forecast,
lastUpdatedMs: state.lastUpdatedMs,
};
try {
setStringSetting(CACHE_KEY, JSON.stringify(cache));
} catch (error) {
console.warn(`weather cache write failed: ${error}`);
}
}
async function loadNwsWeather(location: CurrentLocation): Promise<{
locationName: string;
current: CurrentWeather;
@@ -260,14 +410,18 @@ function normalizeObservation(
const temperatureF = convertTemperatureToF(properties?.temperature);
const windSpeedMph = convertSpeedToMph(properties?.windSpeed);
const windDegrees = finiteNumber(properties?.windDirection?.value);
const textDescription = stringValue(properties?.textDescription);
const icon = parseNwsIcon(properties?.icon);
return {
temperatureF: temperatureF ?? hourly?.temperatureF ?? null,
description: stringValue(properties?.textDescription) || hourly?.shortForecast || "Current conditions",
description: textDescription || hourly?.shortForecast || "Current conditions",
humidityPercent: finiteNumber(properties?.relativeHumidity?.value),
windSpeedMph,
windDirection: windDegrees === null ? hourly?.windDirection ?? "" : degreesToCompass(windDegrees),
timestampMs: timestampValue(properties?.timestamp),
observed: true,
condition: icon.condition ?? conditionFromText(textDescription) ?? hourly?.condition ?? null,
isDaytime: icon.isDaytime ?? hourly?.isDaytime ?? isLocalDaytime(),
};
}
@@ -281,6 +435,8 @@ function currentFromHourly(hourly: ForecastPeriod | undefined, fallback: Forecas
windDirection: source.windDirection,
timestampMs: source.startTimeMs || null,
observed: false,
condition: source.condition,
isDaytime: source.isDaytime,
};
}
@@ -289,19 +445,93 @@ function normalizeForecastPeriod(value: NwsForecastPeriodResponse): ForecastPeri
const name = stringValue(value.name);
const temperature = finiteNumber(value.temperature);
const unit = stringValue(value.temperatureUnit).toUpperCase();
const shortForecast = stringValue(value.shortForecast);
const icon = parseNwsIcon(value.icon);
return {
name: name || "Forecast",
startTimeMs: timestampValue(value.startTime) ?? 0,
temperatureF: temperature === null ? null : unit === "C" ? temperature * 9 / 5 + 32 : temperature,
shortForecast: stringValue(value.shortForecast),
shortForecast,
detailedForecast: stringValue(value.detailedForecast),
precipitationPercent: finiteNumber(value.probabilityOfPrecipitation?.value),
windSpeed: stringValue(value.windSpeed),
windDirection: stringValue(value.windDirection),
isDaytime: Boolean(value.isDaytime),
isDaytime: icon.isDaytime ?? Boolean(value.isDaytime),
condition: icon.condition ?? conditionFromText(shortForecast),
};
}
// NWS icon codes (https://api.weather.gov/icons) by the condition they show.
const NWS_ICON_CONDITIONS: Record<string, WeatherCondition> = {
skc: "clear",
hot: "clear",
cold: "clear",
few: "partly-cloudy",
sct: "partly-cloudy",
bkn: "cloudy",
ovc: "cloudy",
wind_skc: "wind",
wind_few: "wind",
wind_sct: "wind",
wind_bkn: "wind",
wind_ovc: "wind",
fog: "fog",
haze: "fog",
smoke: "fog",
dust: "fog",
rain: "rain",
rain_showers: "rain",
rain_showers_hi: "showers",
tsra: "thunderstorm",
tsra_sct: "thunderstorm",
tsra_hi: "thunderstorm",
tornado: "thunderstorm",
hurricane: "thunderstorm",
tropical_storm: "thunderstorm",
snow: "snow",
blizzard: "snow",
sleet: "sleet",
fzra: "sleet",
rain_fzra: "sleet",
snow_fzra: "sleet",
rain_sleet: "sleet",
snow_sleet: "sleet",
rain_snow: "sleet",
};
/**
* Condition and day/night from an NWS icon URL such as
* ".../icons/land/day/tsra_sct,40/rain,20?size=medium". A period that
* changes partway lists two codes; the first is the one in effect now.
*/
function parseNwsIcon(value: unknown): { condition: WeatherCondition | null; isDaytime: boolean | null } {
const match = stringValue(value).match(/\/icons\/[^/]+\/(day|night)\/([a-z_]+)/);
if (!match) return { condition: null, isDaytime: null };
return { condition: NWS_ICON_CONDITIONS[match[2]!] ?? null, isDaytime: match[1] === "day" };
}
/** Condition from an NWS text description ("Chance Rain Showers"), for when there is no icon. */
function conditionFromText(text: string): WeatherCondition | null {
const lower = text.toLowerCase();
if (/thunder|t-storm|tstorm/.test(lower)) return "thunderstorm";
if (/freezing|sleet|ice pellets|wintry|hail|rain and snow|snow and rain/.test(lower)) return "sleet";
if (/snow|flurr|blizzard/.test(lower)) return "snow";
if (/drizzle/.test(lower)) return "drizzle";
if (/rain|showers/.test(lower)) return /chance|slight|scattered|isolated|patchy/.test(lower) ? "showers" : "rain";
if (/fog|haze|smoke|dust|mist/.test(lower)) return "fog";
if (/windy|breezy|blustery/.test(lower)) return "wind";
if (/partly|few clouds|mostly sunny|mostly clear/.test(lower)) return "partly-cloudy";
if (/cloud|overcast/.test(lower)) return "cloudy";
if (/sunny|clear|fair/.test(lower)) return "clear";
return null;
}
/** Rough day/night by the phone's clock, for when NWS gives no hint. */
function isLocalDaytime(): boolean {
const hour = new Date().getHours();
return hour >= 6 && hour < 18;
}
function convertTemperatureToF(measure: NwsMeasure | undefined): number | null {
const value = finiteNumber(measure?.value);
if (value === null) return null;