forked from Blendman974/faceclaw
<summary>Prompts Used</summary> Act as a code reviewer for https://github.com/jimrandomh/faceclaw/pull/14 (checked out in this working directory). > * The review finished — 42 raw candidates were verified down to 10 confirmed findings. Reporting them now. > > * Code review(review · 10 findings) > ⎿ app/phone-ui/main-view-model.ts > ● 766 [correctness] mirrorFraction subtracts view.getLocationOnScreen() from tap coordinates that NativeScript already delivers > view-local, so every phone-mirror tap is shifted up/left and misroutes > ● 84 [resource-leak] MainViewModel's constructor registers onAnySettingChanged and discards the unsubscriber; a new view model is > built on every page navigation, leaking listeners and retained preview bitmaps > wear/app/src/main/kotlin/com/faceclaw/wear/MainActivity.kt > ● 52 [correctness] onStop abandons an in-progress stem-button hold without sending LONG_PRESS_RELEASE, so the glasses shell > escape menu opens by itself > wear/app/src/main/kotlin/com/faceclaw/wear/ui/TouchpadGestures.kt > ● 62 [correctness] Breaking out of the touchpad gesture loop on a consumed pointer event (or coroutine cancellation mid-hold) > skips onLongPressEnd, leaving a stuck long-press > wear/app/src/main/kotlin/com/faceclaw/wear/PhoneLink.kt > ● 270 [correctness] loadStateItem queries the Data Layer with a hostless Uri (`wear:/faceclaw/state`), which matches no stored > items, so the cached phone state is never read > app/ui/shell/modal-layer.ts > ● 14 [correctness] MODAL_INTERIOR is a module-level constant frozen at import time from the (now runtime-dynamic) display mode, > while modalRect() is computed live, so modals render corrupted after a display-mode switch > app/g2/dashboard-controller.ts > ● 1404 [correctness] handleMirrorTouch wakes the glasses display unconditionally, bypassing the watchWakesDisplaySetting gate > that the parallel injectSyntheticRingInput path honors > ● 1440 [correctness] A mirror tap while any second layer is stacked (e.g. an in-window long-press menu) degrades to a blind > synthetic click that activates whatever is highlighted, not what was tapped > App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearBridge.java > ● 147 [correctness] Watch gestures/commands queued while the JS listener is down are replayed up to 30 seconds later on listener > registration, executing stale non-idempotent input the watch already reported as failed > wear/app/src/main/kotlin/com/faceclaw/wear/ui/RemoteScreen.kt > ● 232 [efficiency] WristTwistDetector (gyroscope, default-on) samples at 100 Hz via DisposableEffect and is not stopped when > the activity stops, draining the watch battery while the process is cached > > * Review of PR #14 (Wear OS companion app + remote controls, 85 files, +5813/−102) is done — 10 confirmed findings reported above, ranked by > severity. The headline items are the phone-mirror tap coordinate double-correction (which misroutes every mirror tap in the default > configuration) and two independent paths on the watch that can drop LONG_PRESS_RELEASE, each of which makes the shell escape menu open on the > glasses by itself. > > A few things beyond the reported list, for context: > > Refuted during verification (looked like bugs, aren't): the watch ack-timeout-during-render theory (input injection never blocks on BLE > transmit), mirror swipes stealing page scroll (no enclosing ScrollView), and a suspected raw-settings-key notification gap (the store-level > broadcast covers raw keys). > > Confirmed but below the 10-finding cap — worth folding in if you're touching those areas anyway: > - shell.closeAssistant() no-ops via popIfTop when another layer sits above the assistant, yet still acks ok to the watch. > - Landscape preview sizing still assumes the old 260-wide panel after the XML moved to 360 (overlap under ~568 dip). > - The shell escape menu and tool-debug dialogs hardcode SIDEBAR_WIDTH + 8, wrong in the new full-panel mode. > - A CapabilityClient IPC fires every 250 ms during streamed assistant replies even on watchless phones; backgrounded preview updates bypass > their 150 ms floor. > - Cleanups: the sidebarWidth() > 0 || focus === "sidebar" predicate appears three times, sidebar slot math is duplicated in chrome-layer.ts, > dead protocol-version plumbing, dead padPointers field, and display-mode is missing from wear/PROTOCOL.md. > > If you'd like, I can apply fixes for any subset of these, or post the findings as inline PR comments (/code-review 14 --comment). <hr> Thanks, let's go ahead and write fixes for all of the issues you described. > Everything builds and tests pass. All 16 issues from the review are fixed — the 10 reported findings plus the six below-cap items. Here's what changed, by fix: > > **Watch app (Kotlin)** > - **Stuck long-press via onStop** — `MainActivity.onStop` now sends `LONG_PRESS_RELEASE` when a stem hold is in progress before dropping the link, so the phone's escape-menu countdown can't run to completion on its own. > - **Stuck long-press via the gesture loop** — `detectTouchpadGestures` wraps the loop in try/finally that pairs every `onLongPressStart` with an `onLongPressEnd`, covering both the consumed-event `break` and coroutine cancellation mid-hold. > - **Cached state never loading** — `PhoneLink.loadStateItem` now queries `wear://*/faceclaw/state` (wildcard host) instead of a hostless Uri that matched nothing, so the watch shows the cached dashboard state when the phone is unreachable. > - **Sensors draining battery while stopped** — the gyroscope/accelerometer/off-body detectors are now driven by a `WhileStartedEffect` helper tied to the activity lifecycle: they stop on `ON_STOP` (palmed screen, back to watch face) and restart on `ON_START`, instead of running at 100 Hz for as long as the process is cached. > - **Dead protocol plumbing** — removed the unused `Protocol.PROTOCOL_VERSION` const and the never-read `PhoneState.protocol` field; the wire field stays and PROTOCOL.md now documents it as reserved. > > **Phone Java** > - **Stale input replay** — `FaceclawWearBridge` now queues only idempotent `/faceclaw/state/request` messages for replay after the JS listener registers; gestures and commands that arrived while the dashboard was down are acked as failed and dropped, never executed up to 30 seconds later. > > **Phone TypeScript** > - **Mirror taps misrouting (the top finding)** — `mirrorFraction` no longer subtracts `getLocationOnScreen()`; NativeScript's `getX`/`getY` are view-local DIPs (I verified this in the installed `@nativescript/core` gesture source), so every mirror tap was being shifted up-left by the status bar + action bar. Taps now land where you touch. > - **Mirror touch ignoring the wake setting** — `handleMirrorTouch` now honours `watchWakesDisplaySetting`: with it off, only a double-tap reaches a dark display, matching the rest of the watch scheme. > - **Blind click through open menus** — `LayerStack.hitTest` now consumes a mirror tap when any layer is stacked (menu, dialog) instead of reporting a miss, so a tap on visible app content can no longer activate whatever menu row happened to be highlighted (e.g. "Close window"). > - **Modal corruption after display-mode switch** — `MODAL_INTERIOR` is now a live `modalInterior()` function, and `ShellModalLayer.paint` re-syncs the inner stack's size each paint, so notification modals render correctly after switching Band ↔ Full panel. > - **View-model leak** — `MainViewModel` keeps its controller/settings unsubscribers and exposes `attach()`/`dispose()`; `main-page.ts` disposes on unload and re-attaches on load (re-attach is needed because suspend/resume fires unloaded/loaded without a navigation, and `subscribe` delivers a fresh snapshot so nothing is missed). > - **`closeAssistant` no-op** — new `LayerStack.popThrough` closes the assistant overlay plus anything stacked above it (follow-up voice dialog, alert); the watch ack now says "The assistant was not open." when there was nothing to close. > - **Watchless-phone IPC churn** — assistant stream events and alerts are skipped when no watch is reachable, so a streaming reply no longer triggers a CapabilityClient lookup every 250 ms. > - **Backgrounded preview floor bypass** — the 150 ms floor check (and timestamp) in `updateCompositePreview` now runs before the foreground-activity check, so backgrounded per-frame refreshes actually throttle. > - **Full-panel hardcodes** — the shell escape menu and tool-debug dialogs position at `sidebarWidth() + 8` (0 in full-panel mode) instead of the fixed 64 px strip. > - **Cleanups** — the strip-visible predicate is now one `sidebarStripVisible()` helper in geometry.ts (used in three places); sidebar slot math is a single `slotPosition()` shared by drawing and hit-testing; dead `padPointers` removed; landscape preview sizing updated to the XML's 360 dp panel; `wear/PROTOCOL.md` documents the `display-mode` command and `displayMode` state field. > > Verification: `./build.sh` (webpack + Gradle) succeeds, `npx tsc --noEmit` is clean, the wear `:app:assembleDebug` builds, and all 59 tests pass. The behavioral fixes — especially mirror-tap coordinates, the two long-press-release paths, and the watch state cache — still need a hardware pass to confirm on device. </details>
496 lines
17 KiB
TypeScript
496 lines
17 KiB
TypeScript
/**
|
|
* The Wear OS watch as an input device and status mirror for the glasses.
|
|
*
|
|
* Inbound, the watch sends ring-style gestures (tap, double-tap, hold,
|
|
* swipe/crown scrolls, "Hey Even") which feed the exact synthetic-ring path
|
|
* the phone UI's test buttons use, plus higher-level commands the ring has no
|
|
* way to express: launch an app, switch or close a window, wake/sleep/lock
|
|
* the display, send a spoken or typed query straight to the assistant, or
|
|
* type text into the foreground app.
|
|
*
|
|
* Outbound, the dashboard state (connection, battery, screen, lock, open
|
|
* windows, launchable apps) is mirrored into a Data Layer item so the watch
|
|
* UI can show it and offer the right actions; assistant replies and alerts
|
|
* stream to the watch as events so a query made from the wrist can be read
|
|
* from the wrist.
|
|
*
|
|
* Transport is app/native/wear-bridge.ts (Java FaceclawWearBridge); the
|
|
* message shapes are documented in wear/PROTOCOL.md.
|
|
*/
|
|
import { type AppDefinition } from "../apps/app-definition";
|
|
import { getInstalledEvenHubApps, installedEvenHubAppId } from "../apps/evenhub/installed-apps";
|
|
import { WEAR_PATHS, wearBridge, type WearMessage } from "../native/wear-bridge";
|
|
import {
|
|
DISPLAY_MODE_VALUES,
|
|
displayModeSetting,
|
|
onAnySettingChanged,
|
|
watchCanUnlockSetting,
|
|
watchMirrorAssistantSetting,
|
|
watchRemoteEnabledSetting,
|
|
} from "../ui/dashboard-settings";
|
|
import { shell, type AssistantActivityEvent } from "../ui/shell/shell";
|
|
import { voiceActivity } from "../ui/shell/voice-activity";
|
|
import { FACECLAW_VERSION } from "../version";
|
|
|
|
/** Protocol revision carried in the state item; bump on incompatible changes. */
|
|
export const WEAR_PROTOCOL_VERSION = 1;
|
|
|
|
export type WearRemoteInputKind =
|
|
| "click"
|
|
| "double-click"
|
|
| "scroll-up"
|
|
| "scroll-down"
|
|
| "long-press"
|
|
| "long-press-start"
|
|
| "long-press-release"
|
|
| "wakeword"
|
|
| "swipe-left"
|
|
| "swipe-right"
|
|
| "swipe-up"
|
|
| "swipe-down";
|
|
|
|
const INPUT_KINDS: readonly WearRemoteInputKind[] = [
|
|
"click",
|
|
"double-click",
|
|
"scroll-up",
|
|
"scroll-down",
|
|
"long-press",
|
|
"long-press-start",
|
|
"long-press-release",
|
|
"wakeword",
|
|
"swipe-left",
|
|
"swipe-right",
|
|
"swipe-up",
|
|
"swipe-down",
|
|
];
|
|
|
|
const COMMANDS = [
|
|
"launch-app",
|
|
"focus-window",
|
|
"close-window",
|
|
"sidebar",
|
|
"wake",
|
|
"sleep",
|
|
"lock",
|
|
"unlock",
|
|
"connect",
|
|
"disconnect",
|
|
"close-assistant",
|
|
"display-mode",
|
|
] as const;
|
|
type WearCommand = (typeof COMMANDS)[number];
|
|
|
|
const MAX_SCROLL_STEPS = 12;
|
|
const MAX_TEXT_LENGTH = 2000;
|
|
const STATE_PUBLISH_DEBOUNCE_MS = 150;
|
|
const STATE_REFRESH_INTERVAL_MS = 30_000;
|
|
const ASSISTANT_STREAM_MIN_INTERVAL_MS = 250;
|
|
|
|
/** What the dashboard controller lends the watch remote. */
|
|
export type WearRemoteHost = {
|
|
/** The launcher-grid apps; installed EvenHub packages are added dynamically. */
|
|
apps: readonly AppDefinition[];
|
|
injectInput: (kind: WearRemoteInputKind) => Promise<void>;
|
|
launchApp: (appId: string) => Promise<void>;
|
|
connect: () => Promise<void>;
|
|
disconnect: () => Promise<void>;
|
|
setGlassesLocked: (locked: boolean, reason: string) => void;
|
|
requestShellRender: () => void;
|
|
getState: () => {
|
|
phase: string;
|
|
status: string;
|
|
glassesLocked: boolean;
|
|
glassesWorn: boolean | null;
|
|
};
|
|
appendLog: (line: string) => void;
|
|
};
|
|
|
|
export class WearRemote {
|
|
private readonly active: boolean;
|
|
private publishTimer: ReturnType<typeof setTimeout> | null = null;
|
|
private watchReachable = false;
|
|
private watchName = "";
|
|
// Assistant text streams as deltas many times a second; the watch gets a
|
|
// snapshot every ASSISTANT_STREAM_MIN_INTERVAL_MS plus every phase change.
|
|
private assistantStreamTimer: ReturnType<typeof setTimeout> | null = null;
|
|
private assistantPendingText: string | null = null;
|
|
private lastAssistantSentAtMs = 0;
|
|
|
|
constructor(private readonly host: WearRemoteHost) {
|
|
this.active = wearBridge.isAvailable();
|
|
if (!this.active) {
|
|
host.appendLog("watch: Google Play services unavailable; watch remote disabled");
|
|
return;
|
|
}
|
|
wearBridge.onMessage((message) => {
|
|
void this.handleMessage(message).catch((error) => {
|
|
host.appendLog(`watch: ${message.path} failed: ${formatError(error)}`);
|
|
});
|
|
});
|
|
wearBridge.onWatchConnection((connection) => {
|
|
const changed = connection.reachable !== this.watchReachable || connection.watchName !== this.watchName;
|
|
this.watchReachable = connection.reachable;
|
|
this.watchName = connection.watchName;
|
|
if (changed) {
|
|
host.appendLog(
|
|
connection.reachable ? `watch: ${connection.watchName || "watch"} reachable` : "watch: no watch reachable",
|
|
);
|
|
}
|
|
// A watch that just came into range gets the current state at once.
|
|
if (connection.reachable) this.publishNow(true);
|
|
});
|
|
shell.onAssistantActivity((event) => this.forwardAssistantActivity(event));
|
|
shell.onAlertShown((text) => {
|
|
if (this.watchReachable && watchMirrorAssistantSetting.get()) {
|
|
wearBridge.sendEvent({ type: "alert", text });
|
|
}
|
|
});
|
|
voiceActivity.subscribe(() => this.schedulePublish());
|
|
onAnySettingChanged(() => this.schedulePublish());
|
|
// Catch-all refresh for state the controller does not announce (cheap:
|
|
// identical states are dropped on the Java side).
|
|
setInterval(() => this.schedulePublish(), STATE_REFRESH_INTERVAL_MS);
|
|
this.schedulePublish();
|
|
}
|
|
|
|
/** Whether a watch running the Faceclaw watch app is reachable right now. */
|
|
isWatchReachable(): boolean {
|
|
return this.watchReachable;
|
|
}
|
|
|
|
getWatchName(): string {
|
|
return this.watchName;
|
|
}
|
|
|
|
/** Mirror the dashboard state to the watch soon; coalesces bursts. */
|
|
schedulePublish(): void {
|
|
if (!this.active || this.publishTimer) return;
|
|
this.publishTimer = setTimeout(() => {
|
|
this.publishTimer = null;
|
|
this.publishNow(false);
|
|
}, STATE_PUBLISH_DEBOUNCE_MS);
|
|
}
|
|
|
|
private publishNow(force: boolean): void {
|
|
if (!this.active) return;
|
|
try {
|
|
wearBridge.publishState(this.buildState(), force);
|
|
} catch (error) {
|
|
this.host.appendLog(`watch: state publish failed: ${formatError(error)}`);
|
|
}
|
|
}
|
|
|
|
private buildState(): Record<string, unknown> {
|
|
const state = this.host.getState();
|
|
const battery = shell.getBatteryLevels();
|
|
const foregroundId = shell.foregroundWindow()?.windowId ?? null;
|
|
const foreground = shell.getForegroundApp();
|
|
return {
|
|
protocol: WEAR_PROTOCOL_VERSION,
|
|
version: FACECLAW_VERSION,
|
|
phase: state.phase,
|
|
status: state.status,
|
|
connected: state.phase === "connected" || state.phase === "charging",
|
|
screenOn: shell.isScreenOn(),
|
|
locked: state.glassesLocked,
|
|
worn: state.glassesWorn,
|
|
listening: voiceActivity.isActive(),
|
|
battery: battery.headset,
|
|
charging: battery.headsetCharging === true,
|
|
foreground: foreground ? { appId: foreground.appId, title: foreground.title } : null,
|
|
windows: shell.getWindows().map((window) => ({
|
|
windowId: window.windowId,
|
|
title: window.title,
|
|
appId: window.appId,
|
|
focused: window.windowId === foregroundId,
|
|
closeable: window.closeable,
|
|
acceptsText: typeof window.receiveTextInput === "function",
|
|
})),
|
|
apps: this.listApps(),
|
|
displayMode: displayModeSetting.get(),
|
|
remoteEnabled: watchRemoteEnabledSetting.get(),
|
|
canUnlock: watchCanUnlockSetting.get(),
|
|
mirrorAssistant: watchMirrorAssistantSetting.get(),
|
|
assistantAvailable: shell.isAssistantAvailable(),
|
|
};
|
|
}
|
|
|
|
/** Launchable apps, matching the launcher grid and the apps.launch tool. */
|
|
private listApps(): Array<{ appId: string; title: string }> {
|
|
const apps = this.host.apps.map((app) => ({ appId: app.appId, title: app.title }));
|
|
for (const installed of getInstalledEvenHubApps()) {
|
|
apps.push({ appId: installedEvenHubAppId(installed.packageId), title: installed.name });
|
|
}
|
|
return apps;
|
|
}
|
|
|
|
private async handleMessage(message: WearMessage): Promise<void> {
|
|
const seq = readNumber(message.payload.seq, 0);
|
|
const ack = (ok: boolean, text = "") => wearBridge.sendAck(message.nodeId, seq, ok, text);
|
|
|
|
if (message.path === WEAR_PATHS.stateRequest) {
|
|
this.publishNow(true);
|
|
ack(true);
|
|
return;
|
|
}
|
|
if (!watchRemoteEnabledSetting.get()) {
|
|
ack(false, "Watch control is turned off in Faceclaw's Settings > Watch.");
|
|
return;
|
|
}
|
|
|
|
switch (message.path) {
|
|
case WEAR_PATHS.input:
|
|
await this.handleInput(message.payload, ack);
|
|
return;
|
|
case WEAR_PATHS.command:
|
|
await this.handleCommand(message.payload, ack);
|
|
return;
|
|
case WEAR_PATHS.assistant: {
|
|
const text = readText(message.payload.text);
|
|
if (!text) {
|
|
ack(false, "Nothing to send.");
|
|
return;
|
|
}
|
|
if (!shell.isAssistantAvailable()) {
|
|
ack(false, "Set up the assistant in Faceclaw's Settings first.");
|
|
return;
|
|
}
|
|
this.host.appendLog(`watch: assistant query (${text.length} chars)`);
|
|
shell.sendToAssistant(text);
|
|
ack(true);
|
|
return;
|
|
}
|
|
case WEAR_PATHS.text: {
|
|
const text = readText(message.payload.text);
|
|
if (!text) {
|
|
ack(false, "Nothing to type.");
|
|
return;
|
|
}
|
|
const window = shell.foregroundWindow();
|
|
if (!window?.receiveTextInput) {
|
|
ack(false, `${window ? window.title : "The current app"} does not accept typed text.`);
|
|
return;
|
|
}
|
|
if (!shell.isScreenOn()) shell.wake("window");
|
|
this.host.appendLog(`watch: typed ${text.length} chars into ${window.title}`);
|
|
shell.sendTextToForegroundWindow(text);
|
|
ack(true);
|
|
return;
|
|
}
|
|
default:
|
|
ack(false, `Unknown message: ${message.path}`);
|
|
}
|
|
}
|
|
|
|
private async handleInput(payload: Record<string, unknown>, ack: (ok: boolean, text?: string) => void): Promise<void> {
|
|
const gesture = String(payload.gesture ?? "");
|
|
if (!(INPUT_KINDS as readonly string[]).includes(gesture)) {
|
|
ack(false, `Unknown gesture: ${gesture}`);
|
|
return;
|
|
}
|
|
const kind = gesture as WearRemoteInputKind;
|
|
const state = this.host.getState();
|
|
if (state.phase !== "connected" && state.phase !== "charging") {
|
|
ack(false, "The glasses are not connected.");
|
|
return;
|
|
}
|
|
// The lock only yields to a double-click, exactly as the ring's does
|
|
// (handled inside the controller); a dark display wakes on any watch-
|
|
// scheme gesture (injectSyntheticRingInput, shared with the phone's own
|
|
// pad and d-pad).
|
|
const repeatable = kind === "scroll-up" || kind === "scroll-down" || kind === "swipe-up" || kind === "swipe-down";
|
|
const steps = repeatable ? Math.min(MAX_SCROLL_STEPS, Math.max(1, Math.round(readNumber(payload.steps, 1)))) : 1;
|
|
for (let i = 0; i < steps; i++) {
|
|
await this.host.injectInput(kind);
|
|
}
|
|
ack(true);
|
|
}
|
|
|
|
private async handleCommand(payload: Record<string, unknown>, ack: (ok: boolean, text?: string) => void): Promise<void> {
|
|
const command = String(payload.command ?? "");
|
|
if (!(COMMANDS as readonly string[]).includes(command)) {
|
|
ack(false, `Unknown command: ${command}`);
|
|
return;
|
|
}
|
|
const state = this.host.getState();
|
|
const connected = state.phase === "connected" || state.phase === "charging";
|
|
this.host.appendLog(`watch: ${command}`);
|
|
switch (command as WearCommand) {
|
|
case "connect":
|
|
if (connected) {
|
|
ack(true, "Already connected.");
|
|
return;
|
|
}
|
|
ack(true, "Connecting...");
|
|
await this.host.connect();
|
|
return;
|
|
case "disconnect":
|
|
if (!connected && state.phase === "disconnected") {
|
|
ack(true, "Already disconnected.");
|
|
return;
|
|
}
|
|
ack(true, "Disconnecting...");
|
|
await this.host.disconnect();
|
|
return;
|
|
default:
|
|
break;
|
|
}
|
|
if (!connected) {
|
|
ack(false, "The glasses are not connected.");
|
|
return;
|
|
}
|
|
if (state.glassesLocked && command !== "unlock" && command !== "lock") {
|
|
ack(false, "The glasses are locked.");
|
|
return;
|
|
}
|
|
switch (command as WearCommand) {
|
|
case "launch-app": {
|
|
const appId = String(payload.appId ?? "").trim();
|
|
const app = this.listApps().find((entry) => entry.appId === appId);
|
|
if (!app) {
|
|
ack(false, `Unknown app: ${appId}`);
|
|
return;
|
|
}
|
|
shell.wake("window");
|
|
await this.host.launchApp(appId);
|
|
this.schedulePublish();
|
|
ack(true, `Opened ${app.title}.`);
|
|
return;
|
|
}
|
|
case "focus-window": {
|
|
const windowId = String(payload.windowId ?? "").trim();
|
|
const window = shell.getWindows().find((entry) => entry.windowId === windowId);
|
|
if (!window) {
|
|
ack(false, "That window is no longer open.");
|
|
return;
|
|
}
|
|
shell.wake("window");
|
|
shell.focusWindow(windowId);
|
|
this.host.requestShellRender();
|
|
this.schedulePublish();
|
|
ack(true);
|
|
return;
|
|
}
|
|
case "close-window": {
|
|
const windowId = String(payload.windowId ?? "").trim();
|
|
const window = shell.getWindows().find((entry) => entry.windowId === windowId);
|
|
if (!window) {
|
|
ack(false, "That window is no longer open.");
|
|
return;
|
|
}
|
|
if (!window.closeable) {
|
|
ack(false, `${window.title} cannot be closed.`);
|
|
return;
|
|
}
|
|
shell.closeWindow(windowId);
|
|
this.host.requestShellRender();
|
|
this.schedulePublish();
|
|
ack(true, `Closed ${window.title}.`);
|
|
return;
|
|
}
|
|
case "sidebar":
|
|
shell.wake("sidebar");
|
|
shell.yieldFocusToSidebar();
|
|
this.host.requestShellRender();
|
|
ack(true);
|
|
return;
|
|
case "wake":
|
|
if (!shell.wake("sidebar")) {
|
|
ack(true, "The display is already on.");
|
|
return;
|
|
}
|
|
this.host.requestShellRender();
|
|
ack(true);
|
|
return;
|
|
case "sleep":
|
|
if (!shell.isScreenOn()) {
|
|
ack(true, "The display is already off.");
|
|
return;
|
|
}
|
|
shell.sleep();
|
|
ack(true);
|
|
return;
|
|
case "lock":
|
|
this.host.setGlassesLocked(true, "locked from the watch");
|
|
this.schedulePublish();
|
|
ack(true);
|
|
return;
|
|
case "unlock":
|
|
if (!watchCanUnlockSetting.get()) {
|
|
ack(false, "Unlocking from the watch is turned off in Settings > Watch.");
|
|
return;
|
|
}
|
|
this.host.setGlassesLocked(false, "unlocked from the watch");
|
|
this.schedulePublish();
|
|
ack(true);
|
|
return;
|
|
case "close-assistant":
|
|
// Idempotent: closing an assistant that isn't open still succeeds,
|
|
// but tell the watch which of the two happened.
|
|
ack(true, shell.closeAssistant() ? "" : "The assistant was not open.");
|
|
return;
|
|
case "display-mode": {
|
|
const value = String(payload.value ?? "");
|
|
if (!(DISPLAY_MODE_VALUES as readonly string[]).includes(value)) {
|
|
ack(false, `Unknown display mode: ${value}`);
|
|
return;
|
|
}
|
|
displayModeSetting.set(value as (typeof DISPLAY_MODE_VALUES)[number]);
|
|
this.schedulePublish();
|
|
ack(true, `Display mode: ${value}`);
|
|
return;
|
|
}
|
|
default:
|
|
ack(false, `Unhandled command: ${command}`);
|
|
}
|
|
}
|
|
|
|
private forwardAssistantActivity(event: AssistantActivityEvent): void {
|
|
if (!watchMirrorAssistantSetting.get()) return;
|
|
// No watch to mirror to: sending anyway is not free — with no known
|
|
// watch nodes the Java side runs a capability lookup per event, which a
|
|
// streaming reply would repeat several times a second.
|
|
if (!this.watchReachable) return;
|
|
if (event.phase === "streaming") {
|
|
// Coalesce: the newest text wins, sent no more often than the interval.
|
|
this.assistantPendingText = event.text;
|
|
if (this.assistantStreamTimer) return;
|
|
const wait = Math.max(0, ASSISTANT_STREAM_MIN_INTERVAL_MS - (Date.now() - this.lastAssistantSentAtMs));
|
|
this.assistantStreamTimer = setTimeout(() => {
|
|
this.assistantStreamTimer = null;
|
|
if (this.assistantPendingText === null) return;
|
|
this.sendAssistantEvent("streaming", this.assistantPendingText);
|
|
this.assistantPendingText = null;
|
|
}, wait);
|
|
return;
|
|
}
|
|
// A phase change supersedes any queued stream snapshot.
|
|
if (this.assistantStreamTimer) {
|
|
clearTimeout(this.assistantStreamTimer);
|
|
this.assistantStreamTimer = null;
|
|
}
|
|
this.assistantPendingText = null;
|
|
this.sendAssistantEvent(event.phase, event.text);
|
|
this.schedulePublish();
|
|
}
|
|
|
|
private sendAssistantEvent(phase: AssistantActivityEvent["phase"], text: string): void {
|
|
this.lastAssistantSentAtMs = Date.now();
|
|
wearBridge.sendEvent({ type: "assistant", phase, text });
|
|
}
|
|
}
|
|
|
|
function readNumber(value: unknown, fallback: number): number {
|
|
const numeric = typeof value === "number" ? value : Number(value);
|
|
return Number.isFinite(numeric) ? numeric : fallback;
|
|
}
|
|
|
|
function readText(value: unknown): string {
|
|
const text = typeof value === "string" ? value : "";
|
|
return text.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f]/g, "").trim().slice(0, MAX_TEXT_LENGTH);
|
|
}
|
|
|
|
function formatError(error: unknown): string {
|
|
return (error as Error)?.message ?? String(error);
|
|
}
|