forked from Blendman974/faceclaw
Merge branch 'main' into rayneo-io-support
This commit is contained in:
commit
739e81c8a6
47 files changed
+3967
-90
No files matched your search
@@ -29,7 +29,8 @@ import { OsEventTypeList } from './events'
|
||||
import { loadDeviceAddresses } from './device-addresses'
|
||||
import { updateGlassesPresence } from './glasses-presence'
|
||||
import { firmwareIncompatibilityMessage, hasCompatibleFirmware } from './firmware-compat'
|
||||
import { isHaltedSessionPhase, suppressAutoReconnect } from './reconnect-policy'
|
||||
import { isHaltedSessionPhase, resumeAutoReconnect, suppressAutoReconnect } from './reconnect-policy'
|
||||
import { WearRemote, type WearRemoteInputKind } from './wear-remote'
|
||||
import { deviceAddressError } from './ios-peripheral-identity'
|
||||
import { createLauncherWindow, LAUNCHER_SURFACE_ID } from '../apps/launcher/launcher-app'
|
||||
import { createNotificationsAppWindow, NOTIFICATIONS_SURFACE_ID, NOTIFICATIONS_WINDOW_ID } from '../apps/notifications/notifications-app'
|
||||
@@ -167,6 +168,8 @@ export class IosPreviewController {
|
||||
private continuousPending = false
|
||||
private continuousCaptureToken = 0
|
||||
private sessionConnected = false
|
||||
/** The Apple Watch remote; constructed last so it sees a fully wired shell. */
|
||||
private wearRemote: WearRemote | null = null
|
||||
private readonly actions: LayerActions = {
|
||||
...noopLayerActions,
|
||||
playBuzzerSequence: payload => this.communicator?.playBuzzerSequence(payload),
|
||||
@@ -210,7 +213,7 @@ export class IosPreviewController {
|
||||
prepareVoiceCapture: () => this.prepareVoiceCapture(),
|
||||
getScreenTimeoutMs: () => null,
|
||||
requestShellRender: () => this.requestShellRender(),
|
||||
onWindowsChanged: () => { this.requestShellRender(); this.syncEvenHubPhoneUi() },
|
||||
onWindowsChanged: () => { this.requestShellRender(); this.syncEvenHubPhoneUi(); this.wearRemote?.schedulePublish() },
|
||||
openNotificationsWindow: () =>
|
||||
this.openInProcessAppInBackground(NOTIFICATIONS_WINDOW_ID, NOTIFICATIONS_SURFACE_ID, createNotificationsAppWindow),
|
||||
onSwitcherBarHeightChanged: () => {
|
||||
@@ -227,6 +230,7 @@ export class IosPreviewController {
|
||||
this.unblankAfterShellRender = on
|
||||
if (!on) void this.display?.setScreenBlanked(true).catch(error => this.fail(error))
|
||||
this.requestShellRender()
|
||||
this.wearRemote?.schedulePublish()
|
||||
},
|
||||
})
|
||||
const launcher = createLauncherWindow({
|
||||
@@ -275,6 +279,20 @@ export class IosPreviewController {
|
||||
// Every capture end (drained, failed, replaced) may leave continuous capture to restart.
|
||||
iosVoiceInput.onSpeechEnd(() => this.syncContinuousCapture())
|
||||
for (const app of ALL_APPS) if (app.appId !== 'launcher' && !iosAppUnavailableReason(app.appId)) app.boot?.(this.buildAppContext(app))
|
||||
this.wearRemote = new WearRemote({
|
||||
apps: ALL_APPS.filter(app => app.showInLauncher !== false && !iosAppUnavailableReason(app.appId)),
|
||||
injectInput: kind => this.injectWatchInput(kind),
|
||||
launchApp: id => this.launchApp(id),
|
||||
// The watch's Connect/Disconnect are explicit user choices, like the phone's own buttons.
|
||||
connect: () => { resumeAutoReconnect(); return this.connect(true) },
|
||||
disconnect: () => { suppressAutoReconnect(); return this.disconnect() },
|
||||
setGlassesLocked: locked => this.setGlassesLocked(locked),
|
||||
requestShellRender: () => this.requestShellRender(),
|
||||
previewInputActive: () => !this.communicator && this.runtimeRunning,
|
||||
getState: () => ({ phase: this.state.phase, status: this.state.status, glassesLocked: this.glassesLocked,
|
||||
glassesWorn: this.glassesWorn, silentMode: false, lastHeadsetBattery: this.state.battery }),
|
||||
appendLog: line => this.logBluetooth(line),
|
||||
})
|
||||
}
|
||||
|
||||
/** The compositor frames go to: the live session, else the headless preview. */
|
||||
@@ -443,6 +461,7 @@ export class IosPreviewController {
|
||||
// a session's first snapshot) wakes the screen, to the lock notice if locked.
|
||||
const putOn = wearing && this.glassesWorn === false
|
||||
this.glassesWorn = wearing
|
||||
this.wearRemote?.schedulePublish()
|
||||
this.syncPresence()
|
||||
this.logBluetooth(`Glasses wear state: ${wearing ? 'ON_HEAD' : 'OFF_HEAD'}`)
|
||||
if (!wearing && this.phoneLocked && this.lockEnabled) this.setGlassesLocked(true)
|
||||
@@ -451,6 +470,7 @@ export class IosPreviewController {
|
||||
private setGlassesLocked(locked: boolean): void {
|
||||
if (locked === this.glassesLocked) return
|
||||
this.glassesLocked = locked
|
||||
this.wearRemote?.schedulePublish()
|
||||
if (locked) {
|
||||
this.glance.dismiss()
|
||||
if (iosVoiceInput.continuousSource === 'glasses') this.continuousPending = true
|
||||
@@ -599,7 +619,25 @@ export class IosPreviewController {
|
||||
console.log(`[ios-preview] ${origin} ${gesture}: ${shell.describeInputTarget()}`)
|
||||
}).catch(error => this.fail(error))
|
||||
}
|
||||
/** Input from the Apple Watch: shares the controller queue with physical input. */
|
||||
private injectWatchInput(kind: WearRemoteInputKind): Promise<void> {
|
||||
const task = this.inputQueue.then(async () => {
|
||||
this.onActivity()
|
||||
// As on Android, and because the watch may be acting on a stale state snapshot: a dark
|
||||
// display takes a double-click (wake) or a tap/hold (Glanceboard), and scrolls mean nothing.
|
||||
if (!shell.isScreenOn() && (kind === 'scroll-up' || kind === 'scroll-down' || kind === 'short-then-long-press')) return
|
||||
// A real hold releases when the finger lifts; a discrete long-press releases at once.
|
||||
const type = kind === 'long-press-start' ? 'long-press' : kind
|
||||
await this.receiveInput(makeInputEvent({ type, source: 'watch' } as InputEventPayload))
|
||||
if (kind === 'long-press') await this.receiveInput(makeInputEvent({ type: 'long-press-release', source: 'watch' }))
|
||||
console.log(`[ios-preview] apple-watch ${kind}: ${shell.describeInputTarget()}`)
|
||||
})
|
||||
this.inputQueue = task.catch(error => this.fail(error))
|
||||
return task
|
||||
}
|
||||
private async receiveInput(event: InputEvent, headTilt = false): Promise<void> {
|
||||
// Input can change the foreground app or the display state, and the shell doesn't announce those.
|
||||
this.wearRemote?.schedulePublish()
|
||||
if (!acceptInput(event)) return
|
||||
if (this.glassesLocked) {
|
||||
// Preserve the locked display's sleep/wake controls without dispatching
|
||||
@@ -807,6 +845,7 @@ export class IosPreviewController {
|
||||
this.syncRuntime()
|
||||
if (this.active) this.onConnectionState({ ...this.state })
|
||||
this.requestShellRender()
|
||||
this.wearRemote?.schedulePublish()
|
||||
}
|
||||
/** The ring level arrives through the glasses, so like Android it is shown only while connected. */
|
||||
private pushBatteryLevels(): void {
|
||||
@@ -814,11 +853,16 @@ export class IosPreviewController {
|
||||
shell.setBatteryLevels({ headset: this.state.battery, headsetCharging: this.state.charging,
|
||||
ring: connected ? this.state.ringBattery : null, ringCharging: connected ? this.state.ringCharging : null })
|
||||
}
|
||||
async connect(): Promise<void> {
|
||||
if (!this.active || this.connecting || this.stopping) return
|
||||
/** `fromBackground`: the watch asked, and the phone may be in a pocket with the app suspended. */
|
||||
async connect(fromBackground = false): Promise<void> {
|
||||
if ((!this.active && !fromBackground) || this.connecting || this.stopping) return
|
||||
if (this.communicator && ['connecting', 'connected', 'retrying', 'disconnecting'].includes(this.state.phase)) return
|
||||
const addresses = loadDeviceAddresses(), error = deviceAddressError(addresses)
|
||||
if (error) { this.onError(error); return }
|
||||
if (error) {
|
||||
if (this.active) this.onError(error)
|
||||
else this.logBluetooth(error)
|
||||
return
|
||||
}
|
||||
this.connecting = true
|
||||
this.sessionCreated = true
|
||||
try {
|
||||
@@ -882,6 +926,7 @@ export class IosPreviewController {
|
||||
this.pushBatteryLevels()
|
||||
if (this.active) this.onConnectionState({ ...this.state })
|
||||
this.requestShellRender()
|
||||
this.wearRemote?.schedulePublish()
|
||||
}),
|
||||
communicator.onCaseState(state => {
|
||||
if (this.communicator !== communicator) return
|
||||
@@ -889,6 +934,7 @@ export class IosPreviewController {
|
||||
// the case's own charging is only known while the glasses are docked.
|
||||
shell.setBatteryLevels({ ...(state.battery !== null ? { case: state.battery } : {}),
|
||||
caseCharging: state.docked ? state.charging : null })
|
||||
this.wearRemote?.schedulePublish()
|
||||
}),
|
||||
communicator.onFirmwareInfo(info => {
|
||||
if (this.communicator !== communicator) return
|
||||
|
||||
+51
-13
@@ -1,5 +1,6 @@
|
||||
/**
|
||||
* The Wear OS watch as an input device and status mirror for the glasses.
|
||||
* The watch (Wear OS or Apple 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
|
||||
@@ -17,11 +18,14 @@
|
||||
* The watch's own battery flows the other way for the glasses' top-bar and
|
||||
* Glanceboard indicators: the phone polls it (the watch answers from a
|
||||
* listener service even while its app is closed) and the open watch app
|
||||
* pushes changes unprompted. The level lives in shell.getBatteryLevels()
|
||||
* beside the G2 and R1 ones and is cleared when no watch is reachable.
|
||||
* pushes changes unprompted. An Apple Watch app can't be woken to answer, so
|
||||
* it reports when it can instead (see wear/PROTOCOL.md). The level lives in
|
||||
* shell.getBatteryLevels() beside the G2 and R1 ones and is cleared when no
|
||||
* watch is reachable or the last report has gone stale.
|
||||
*
|
||||
* Transport is app/native/wear-bridge.ts (Java FaceclawWearBridge); the
|
||||
* message shapes are documented in wear/PROTOCOL.md.
|
||||
* Transport is app/native/wear-bridge.ts (Java FaceclawWearBridge) or
|
||||
* wear-bridge.ios.ts (FaceclawWatchBridge); the message shapes are documented
|
||||
* in wear/PROTOCOL.md.
|
||||
*/
|
||||
import { flagAnalyticsEvent } from "../analytics/analytics";
|
||||
import { type AppDefinition } from "../apps/app-definition";
|
||||
@@ -33,6 +37,8 @@ import {
|
||||
watchCrownClockwiseNextSetting,
|
||||
watchMirrorAssistantSetting,
|
||||
watchRemoteEnabledSetting,
|
||||
watchSoundEffectsSetting,
|
||||
watchVibrateOnInputSetting,
|
||||
} from "../ui/dashboard-settings";
|
||||
import { shell, type AssistantActivityEvent } from "../ui/shell/shell";
|
||||
import { voiceActivity } from "../ui/shell/voice-activity";
|
||||
@@ -102,6 +108,10 @@ const WATCH_BATTERY_POLL_INTERVAL_MS = 5 * 60_000;
|
||||
// glasses connecting) would otherwise trigger back to back.
|
||||
const WATCH_BATTERY_POLL_MIN_GAP_MS = 20_000;
|
||||
const WATCH_BATTERY_URGENT_POLL_LIMIT = 3;
|
||||
// Wear OS answers every poll. A closed Apple Watch app only reports when the
|
||||
// system gives it background time, which can be hours apart, so a level this
|
||||
// old says more about the watch being gone than about its charge.
|
||||
const WATCH_BATTERY_MAX_AGE_MS = 4 * 60 * 60_000;
|
||||
|
||||
/** What the dashboard controller lends the watch remote. */
|
||||
export type WearRemoteHost = {
|
||||
@@ -113,6 +123,11 @@ export type WearRemoteHost = {
|
||||
disconnect: () => Promise<void>;
|
||||
setGlassesLocked: (locked: boolean, reason: string) => void;
|
||||
requestShellRender: () => void;
|
||||
/**
|
||||
* The shell is running on the phone's preview with no glasses involved, so
|
||||
* gestures have somewhere to go even though nothing is connected.
|
||||
*/
|
||||
previewInputActive?: () => boolean;
|
||||
getState: () => {
|
||||
phase: string;
|
||||
status: string;
|
||||
@@ -136,6 +151,7 @@ export class WearRemote {
|
||||
private lastAssistantSentAtMs = 0;
|
||||
private lastWatchBatteryPollAtMs = 0;
|
||||
private watchBatteryKnown = false;
|
||||
private watchBatteryReportedAtMs = 0;
|
||||
// Urgent polls that got no answer; a watch app too old to answer them
|
||||
// must not be poked every publish, so after a few the cadence drops back.
|
||||
private watchBatteryPollsUnanswered = 0;
|
||||
@@ -143,7 +159,7 @@ export class WearRemote {
|
||||
constructor(private readonly host: WearRemoteHost) {
|
||||
this.active = wearBridge.isAvailable();
|
||||
if (!this.active) {
|
||||
host.appendLog("watch: Google Play services unavailable; watch remote disabled");
|
||||
host.appendLog(`watch: ${global.isIOS ? "WatchConnectivity" : "Google Play services"} unavailable; watch remote disabled`);
|
||||
return;
|
||||
}
|
||||
wearBridge.onMessage((message) => {
|
||||
@@ -183,10 +199,25 @@ export class WearRemote {
|
||||
// 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);
|
||||
setInterval(() => this.pollWatchBattery(false), WATCH_BATTERY_POLL_INTERVAL_MS);
|
||||
setInterval(() => {
|
||||
this.expireWatchBattery();
|
||||
this.pollWatchBattery(false);
|
||||
}, WATCH_BATTERY_POLL_INTERVAL_MS);
|
||||
this.schedulePublish();
|
||||
}
|
||||
|
||||
/** Gestures have a display to act on: the glasses, or the phone's preview without them. */
|
||||
private takesInput(): boolean {
|
||||
const phase = this.host.getState().phase;
|
||||
return phase === "connected" || phase === "charging" || this.host.previewInputActive?.() === true;
|
||||
}
|
||||
|
||||
private expireWatchBattery(): void {
|
||||
if (this.watchBatteryKnown && Date.now() - this.watchBatteryReportedAtMs > WATCH_BATTERY_MAX_AGE_MS) {
|
||||
this.setWatchBattery(null, null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ask the reachable watch for its battery. `urgent` skips the poll gap for
|
||||
* a level that is not known yet (the glasses just connected, the watch
|
||||
@@ -257,6 +288,7 @@ export class WearRemote {
|
||||
phase: state.phase,
|
||||
status: state.status,
|
||||
connected: state.phase === "connected" || state.phase === "charging",
|
||||
preview: this.host.previewInputActive?.() === true,
|
||||
screenOn: shell.isScreenOn(),
|
||||
locked: state.glassesLocked,
|
||||
worn: state.glassesWorn,
|
||||
@@ -278,6 +310,8 @@ export class WearRemote {
|
||||
apps: this.listApps(),
|
||||
remoteEnabled: watchRemoteEnabledSetting.get(),
|
||||
crownClockwiseNext: watchCrownClockwiseNextSetting.get(),
|
||||
vibrateOnInput: watchVibrateOnInputSetting.get(),
|
||||
soundEffects: watchSoundEffectsSetting.get(),
|
||||
canUnlock: watchCanUnlockSetting.get(),
|
||||
mirrorAssistant: watchMirrorAssistantSetting.get(),
|
||||
assistantAvailable: shell.isAssistantAvailable(),
|
||||
@@ -307,7 +341,11 @@ export class WearRemote {
|
||||
// acked (the watch sends it fire-and-forget, without a seq).
|
||||
this.watchBatteryPollsUnanswered = 0;
|
||||
const battery = readNumber(message.payload.battery, -1);
|
||||
const valid = Number.isInteger(battery) && battery >= 0 && battery <= 100;
|
||||
// A report the watch left behind earlier says when it was taken; a live one is current.
|
||||
const reportedAtMs = readNumber(message.payload.at, Date.now());
|
||||
const valid = Number.isInteger(battery) && battery >= 0 && battery <= 100 &&
|
||||
Date.now() - reportedAtMs <= WATCH_BATTERY_MAX_AGE_MS;
|
||||
this.watchBatteryReportedAtMs = reportedAtMs;
|
||||
this.setWatchBattery(valid ? battery : null, valid ? message.payload.charging === true : null);
|
||||
return;
|
||||
}
|
||||
@@ -315,8 +353,7 @@ export class WearRemote {
|
||||
// Touch-down ahead of the gesture that follows it. Fire-and-forget like
|
||||
// the battery (no seq, never acked): wherever that gesture would be
|
||||
// refused, the press is dropped silently and the gesture's ack says why.
|
||||
const phase = this.host.getState().phase;
|
||||
if (watchRemoteEnabledSetting.get() && (phase === "connected" || phase === "charging")) {
|
||||
if (watchRemoteEnabledSetting.get() && this.takesInput()) {
|
||||
await this.host.injectInput("ring-press");
|
||||
}
|
||||
return;
|
||||
@@ -378,8 +415,7 @@ export class WearRemote {
|
||||
return;
|
||||
}
|
||||
const kind = gesture as WearRemoteInputKind;
|
||||
const state = this.host.getState();
|
||||
if (state.phase !== "connected" && state.phase !== "charging") {
|
||||
if (!this.takesInput()) {
|
||||
ack(false, "The glasses are not connected.");
|
||||
return;
|
||||
}
|
||||
@@ -422,7 +458,7 @@ export class WearRemote {
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (!connected) {
|
||||
if (!this.takesInput()) {
|
||||
ack(false, "The glasses are not connected.");
|
||||
return;
|
||||
}
|
||||
@@ -561,6 +597,8 @@ export class WearRemote {
|
||||
}
|
||||
|
||||
function readNumber(value: unknown, fallback: number): number {
|
||||
// Number(null) is 0: a watch reporting an unknown battery would read as empty.
|
||||
if (value === null) return fallback;
|
||||
const numeric = typeof value === "number" ? value : Number(value);
|
||||
return Number.isFinite(numeric) ? numeric : fallback;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* The watch transport's wire names and shapes, shared by the Wear OS bridge
|
||||
* (wear-bridge.ts) and the Apple Watch one (wear-bridge.ios.ts). See
|
||||
* wear/PROTOCOL.md for the messages themselves.
|
||||
*/
|
||||
|
||||
/** Message paths shared with the watch apps (wear/app/.../Protocol.kt, apple-watch/Faceclaw/Protocol.swift). */
|
||||
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. */
|
||||
assistant: "/faceclaw/assistant",
|
||||
/** Watch -> phone: text to type into the foreground app. */
|
||||
text: "/faceclaw/text",
|
||||
/** Watch -> phone: please re-publish the state item. */
|
||||
stateRequest: "/faceclaw/state/request",
|
||||
/** Watch -> phone: the watch's own battery ({battery, charging}); never acked. */
|
||||
battery: "/faceclaw/battery",
|
||||
/** Phone -> watch: please report the watch battery (answered even while the watch app is closed). */
|
||||
batteryRequest: "/faceclaw/battery/request",
|
||||
/** Phone -> watch: reply to any of the above. */
|
||||
ack: "/faceclaw/ack",
|
||||
/** Phone -> watch: assistant activity, alerts, voice-dialog transcripts. */
|
||||
event: "/faceclaw/event",
|
||||
/** Phone -> watch Data Layer item: the mirrored dashboard state. */
|
||||
state: "/faceclaw/state",
|
||||
} as const;
|
||||
|
||||
export type WearMessage = {
|
||||
path: string;
|
||||
/** Parsed JSON payload ({} when the watch sent none or it was malformed). */
|
||||
payload: Record<string, unknown>;
|
||||
nodeId: string;
|
||||
};
|
||||
|
||||
export type WearWatchConnection = {
|
||||
reachable: boolean;
|
||||
watchName: string;
|
||||
};
|
||||
|
||||
export function parseWearPayload(json: string): Record<string, unknown> {
|
||||
try {
|
||||
const parsed = JSON.parse(json);
|
||||
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
* iOS twin of wear-bridge.ts: the phone side of the Apple Watch remote, over
|
||||
* WatchConnectivity (App_Resources/iOS/src/FaceclawWatchBridge). It has the
|
||||
* same surface and carries the same (path, JSON) messages, so
|
||||
* app/g2/wear-remote.ts gives them their meaning on both platforms.
|
||||
*
|
||||
* "Reachable" here means an Apple Watch with the Faceclaw app is paired, not
|
||||
* that the app is running: iOS can't wake a closed watch app, and it is
|
||||
* suspended whenever the wrist drops, so WCSession's live reachability flips
|
||||
* constantly. The native side sends live while it can and otherwise leaves
|
||||
* the latest state in the application context for the app's next wake.
|
||||
*/
|
||||
import { WEAR_PATHS, parseWearPayload, type WearMessage, type WearWatchConnection } from './wear-bridge-types'
|
||||
|
||||
export { WEAR_PATHS, type WearMessage, type WearWatchConnection }
|
||||
|
||||
declare const FaceclawWatchBridge: any
|
||||
|
||||
/** WatchConnectivity doesn't expose the watch's own name. */
|
||||
const WATCH_NAME = 'Apple Watch'
|
||||
/** Replies go to the session's one counterpart; this only fills WearMessage.nodeId. */
|
||||
const WATCH_NODE_ID = 'apple-watch'
|
||||
|
||||
type MessageListener = (message: WearMessage) => void
|
||||
type ConnectionListener = (connection: WearWatchConnection) => void
|
||||
|
||||
class WearBridge {
|
||||
private nativeBridge: any = undefined
|
||||
private listening = false
|
||||
private readonly messageListeners = new Set<MessageListener>()
|
||||
private readonly connectionListeners = new Set<ConnectionListener>()
|
||||
|
||||
private native(): any {
|
||||
if (this.nativeBridge !== undefined) return this.nativeBridge
|
||||
try {
|
||||
const bridge = FaceclawWatchBridge.shared()
|
||||
this.nativeBridge = bridge.isAvailable() ? bridge : null
|
||||
} catch (error) {
|
||||
console.warn('watch bridge unavailable', error)
|
||||
this.nativeBridge = null
|
||||
}
|
||||
return this.nativeBridge
|
||||
}
|
||||
|
||||
/** Whether WatchConnectivity works on this phone (it does on every iPhone). */
|
||||
isAvailable(): boolean {
|
||||
return this.native() !== null
|
||||
}
|
||||
|
||||
private ensureListener(): void {
|
||||
if (this.listening) return
|
||||
const native = this.native()
|
||||
if (!native) return
|
||||
this.listening = true
|
||||
native.setMessageHandlerConnectionHandler((path: string, json: string) => {
|
||||
const message: WearMessage = { path: String(path), payload: parseWearPayload(String(json)), nodeId: WATCH_NODE_ID }
|
||||
for (const listener of Array.from(this.messageListeners)) {
|
||||
try { listener(message) }
|
||||
catch (error) { console.warn('watch message listener failed', error) }
|
||||
}
|
||||
}, () => {
|
||||
const connection = this.getWatchConnection()
|
||||
for (const listener of Array.from(this.connectionListeners)) {
|
||||
try { listener(connection) }
|
||||
catch (error) { console.warn('watch connection listener failed', error) }
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/** Subscribe to watch messages. Registers the native listener on first use. */
|
||||
onMessage(listener: MessageListener): () => void {
|
||||
this.messageListeners.add(listener)
|
||||
this.ensureListener()
|
||||
return () => { this.messageListeners.delete(listener) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe to the watch's availability; the current value is delivered
|
||||
* immediately. Also fires when the watch app comes to the front with the
|
||||
* value unchanged, which is what prompts WearRemote to push it fresh state.
|
||||
*/
|
||||
onWatchConnection(listener: ConnectionListener): () => void {
|
||||
this.connectionListeners.add(listener)
|
||||
this.ensureListener()
|
||||
listener(this.getWatchConnection())
|
||||
return () => { this.connectionListeners.delete(listener) }
|
||||
}
|
||||
|
||||
getWatchConnection(): WearWatchConnection {
|
||||
const reachable = Boolean(this.native()?.isWatchAppAvailable())
|
||||
return { reachable, watchName: reachable ? WATCH_NAME : '' }
|
||||
}
|
||||
|
||||
/** WatchConnectivity pushes changes; there is nothing to re-query. */
|
||||
refreshWatchNodes(): void {}
|
||||
|
||||
/** Mirror the dashboard state; unchanged states are dropped natively unless `force`. */
|
||||
publishState(state: Record<string, unknown>, force = false): void {
|
||||
this.native()?.publishStateForce(JSON.stringify(state), force)
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an event to the watch. A suspended app misses live messages, so all
|
||||
* but streaming snapshots also stay behind for its next wake: that is how a
|
||||
* reply that finished with the wrist down still reaches the Assistant screen.
|
||||
*/
|
||||
sendEvent(payload: Record<string, unknown>): void {
|
||||
const sticky = !(payload.type === 'assistant' && payload.phase === 'streaming')
|
||||
this.native()?.sendEventSticky(JSON.stringify(payload), sticky)
|
||||
}
|
||||
|
||||
/** Send a live message on any phone -> watch path; dropped if the app isn't in front. */
|
||||
sendToWatch(path: string, payload: Record<string, unknown>): void {
|
||||
this.native()?.sendToWatchJson(path, JSON.stringify(payload))
|
||||
}
|
||||
|
||||
/** Acknowledge a watch message; `seq` echoes the watch's sequence number. */
|
||||
sendAck(_nodeId: string, seq: number, ok: boolean, message = ''): void {
|
||||
this.native()?.sendAckOkJsReadyMessage(seq, ok, true, message)
|
||||
}
|
||||
}
|
||||
|
||||
export const wearBridge = new WearBridge()
|
||||
@@ -9,47 +9,12 @@
|
||||
* Data Layer is part of it), so the rest of the app never has to care.
|
||||
*/
|
||||
import { Utils } from "@nativescript/core";
|
||||
import { WEAR_PATHS, parseWearPayload, type WearMessage, type WearWatchConnection } from "./wear-bridge-types";
|
||||
|
||||
export { WEAR_PATHS, type WearMessage, type WearWatchConnection };
|
||||
|
||||
declare const com: any;
|
||||
|
||||
/** Message paths shared with the watch app (wear/app/.../Protocol.kt). */
|
||||
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. */
|
||||
assistant: "/faceclaw/assistant",
|
||||
/** Watch -> phone: text to type into the foreground app. */
|
||||
text: "/faceclaw/text",
|
||||
/** Watch -> phone: please re-publish the state item. */
|
||||
stateRequest: "/faceclaw/state/request",
|
||||
/** Watch -> phone: the watch's own battery ({battery, charging}); never acked. */
|
||||
battery: "/faceclaw/battery",
|
||||
/** Phone -> watch: please report the watch battery (answered even while the watch app is closed). */
|
||||
batteryRequest: "/faceclaw/battery/request",
|
||||
/** Phone -> watch: reply to any of the above. */
|
||||
ack: "/faceclaw/ack",
|
||||
/** Phone -> watch: assistant activity, alerts, voice-dialog transcripts. */
|
||||
event: "/faceclaw/event",
|
||||
/** Phone -> watch Data Layer item: the mirrored dashboard state. */
|
||||
state: "/faceclaw/state",
|
||||
} as const;
|
||||
|
||||
export type WearMessage = {
|
||||
path: string;
|
||||
/** Parsed JSON payload ({} when the watch sent none or it was malformed). */
|
||||
payload: Record<string, unknown>;
|
||||
nodeId: string;
|
||||
};
|
||||
|
||||
export type WearWatchConnection = {
|
||||
reachable: boolean;
|
||||
watchName: string;
|
||||
};
|
||||
|
||||
type MessageListener = (message: WearMessage) => void;
|
||||
type ConnectionListener = (connection: WearWatchConnection) => void;
|
||||
|
||||
@@ -91,7 +56,7 @@ class WearBridge {
|
||||
onMessage: (path: string, json: string, nodeId: string) => {
|
||||
const message: WearMessage = {
|
||||
path: String(path),
|
||||
payload: parsePayload(String(json)),
|
||||
payload: parseWearPayload(String(json)),
|
||||
nodeId: String(nodeId),
|
||||
};
|
||||
for (const listener of Array.from(this.messageListeners)) {
|
||||
@@ -177,13 +142,4 @@ class WearBridge {
|
||||
}
|
||||
}
|
||||
|
||||
function parsePayload(json: string): Record<string, unknown> {
|
||||
try {
|
||||
const parsed = JSON.parse(json);
|
||||
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
export const wearBridge = new WearBridge();
|
||||
@@ -307,7 +307,7 @@ export const glassesBatteryVisibilitySetting = batteryVisibilitySetting(
|
||||
export const ringBatteryVisibilitySetting = batteryVisibilitySetting(
|
||||
"ringBatteryVisibility", "R1", "display.battery.ringVisibility",
|
||||
);
|
||||
/** The Wear OS watch; the indicator only exists while a watch is reachable. */
|
||||
/** The watch; the indicator only exists while a watch is reachable. */
|
||||
export const watchBatteryVisibilitySetting = batteryVisibilitySetting(
|
||||
"watchBatteryVisibility", "Watch", "display.battery.watchVisibility",
|
||||
);
|
||||
@@ -702,7 +702,7 @@ export const mirrorTouchSetting = new ConfigSettingBoolean({
|
||||
"Let touches on the phone's mirror act on the glasses UI: tap selects what it lands on, double-tap goes back, a hold opens the menu, swipes navigate.",
|
||||
});
|
||||
|
||||
// Wear OS watch remote (app/g2/wear-remote.ts, wear/). All three are read on
|
||||
// Watch remote (app/g2/wear-remote.ts, wear/, apple-watch/). All three are read on
|
||||
// every watch message, so a change applies immediately.
|
||||
export const watchRemoteEnabledSetting = new ConfigSettingBoolean({
|
||||
id: "watch-remote-enabled",
|
||||
@@ -710,7 +710,7 @@ export const watchRemoteEnabledSetting = new ConfigSettingBoolean({
|
||||
storageKey: "watch.remoteEnabled",
|
||||
defaultValue: true,
|
||||
description:
|
||||
"Accept input from the Faceclaw Wear OS app: spatial swipes, taps, holds, crown, voice queries and app commands. The ring's scheme is unaffected. Turn off to ignore the watch.",
|
||||
"Accept input from the Faceclaw watch app: spatial swipes, taps, holds, crown, voice queries and app commands. The ring's scheme is unaffected. Turn off to ignore the watch.",
|
||||
});
|
||||
|
||||
export const watchCanUnlockSetting = new ConfigSettingBoolean({
|
||||
@@ -722,6 +722,7 @@ export const watchCanUnlockSetting = new ConfigSettingBoolean({
|
||||
"Let the watch unlock the glasses' lock screen (which otherwise waits for the phone to be unlocked). Your watch is on your wrist; turn this off if you would rather it stay a phone-only unlock.",
|
||||
});
|
||||
|
||||
// Wear OS only: the Apple Watch's crown doesn't reach the app, so iOS doesn't offer this.
|
||||
export const watchCrownClockwiseNextSetting = new ConfigSettingBoolean({
|
||||
id: "watch-crown-clockwise-next",
|
||||
label: "Clockwise crown = next",
|
||||
@@ -731,6 +732,26 @@ export const watchCrownClockwiseNextSetting = new ConfigSettingBoolean({
|
||||
"Choose which crown direction moves to the next item. Off: clockwise moves to the previous item. On: clockwise moves to the next item.",
|
||||
});
|
||||
|
||||
// Apple Watch only: Wear OS has no sounds, and keeps its vibration setting on the watch.
|
||||
// The watch reads these from the mirrored state (apple-watch/Faceclaw/WatchPrefs.swift).
|
||||
export const watchVibrateOnInputSetting = new ConfigSettingBoolean({
|
||||
id: "watch-vibrate-on-input",
|
||||
label: "Vibrate on input",
|
||||
storageKey: "watch.vibrateOnInput",
|
||||
defaultValue: true,
|
||||
description:
|
||||
"Adds a slight vibration with each watch input",
|
||||
});
|
||||
|
||||
export const watchSoundEffectsSetting = new ConfigSettingBoolean({
|
||||
id: "watch-sound-effects",
|
||||
label: "Sound effects",
|
||||
storageKey: "watch.soundEffects",
|
||||
defaultValue: false,
|
||||
description:
|
||||
"On: each input gets its own watchOS feedback, a sound with a vibration (watchOS can't play one alone, so this vibrates even with Vibrate on input off). Off: a light tap for every input. Only Silent Mode on the watch guarantees no sound.",
|
||||
});
|
||||
|
||||
export const watchMirrorAssistantSetting = new ConfigSettingBoolean({
|
||||
id: "watch-mirror-assistant",
|
||||
label: "Mirror assistant to watch",
|
||||
|
||||
@@ -95,6 +95,8 @@ import {
|
||||
watchCrownClockwiseNextSetting,
|
||||
watchMirrorAssistantSetting,
|
||||
watchRemoteEnabledSetting,
|
||||
watchSoundEffectsSetting,
|
||||
watchVibrateOnInputSetting,
|
||||
} from "../dashboard-settings";
|
||||
import { wearBridge } from "../../native/wear-bridge";
|
||||
import { openSettingsSubMenu, SettingsPanelLayer, type SettingsSection } from "./settings-panel";
|
||||
@@ -210,11 +212,16 @@ function settingsSections(): SettingsSection[] {
|
||||
},
|
||||
{
|
||||
label: "Watch",
|
||||
// Wear OS remote (wear/); the status line above the items says whether
|
||||
// a watch running the companion app is currently reachable.
|
||||
// Watch remote (wear/, apple-watch/); the status line above the items says
|
||||
// whether a watch running the companion app is currently reachable.
|
||||
items: [
|
||||
toggleSettingMenuItem(watchRemoteEnabledSetting),
|
||||
toggleSettingMenuItem(watchCrownClockwiseNextSetting),
|
||||
...(global.isIOS ? [
|
||||
toggleSettingMenuItem(watchVibrateOnInputSetting),
|
||||
toggleSettingMenuItem(watchSoundEffectsSetting),
|
||||
] : [
|
||||
toggleSettingMenuItem(watchCrownClockwiseNextSetting),
|
||||
]),
|
||||
toggleSettingMenuItem(watchCanUnlockSetting),
|
||||
toggleSettingMenuItem(watchMirrorAssistantSetting),
|
||||
],
|
||||
@@ -273,17 +280,12 @@ function settingsSections(): SettingsSection[] {
|
||||
},
|
||||
];
|
||||
if (!global.isIOS) return sections;
|
||||
const deferred = new Set(["Watch"]);
|
||||
return sections.map(section => {
|
||||
if (section.label === "Developer") return { ...section, items: [enumSettingMenuItem(ringConnectionModeSetting),
|
||||
toggleSettingMenuItem(showBleBandwidthSetting)] };
|
||||
// No downloadable on-device models: iOS's on-device option is Apple's recognizer.
|
||||
if (section.label === "Voice") return { label: "Voice", items: [enumSettingMenuItem(wakeWordActionSetting),
|
||||
enumSettingMenuItem(voiceProviderSetting), toggleSettingMenuItem(saveTranscribeAudioSetting), ...whisperServerMenuItems()] };
|
||||
if (deferred.has(section.label)) return { label: section.label, items: [{
|
||||
label: "Not available on iOS yet", disabled: true, onSelect: () => {},
|
||||
description: `${section.label} integration has not been ported to iOS.`,
|
||||
}] };
|
||||
if (section.label === "Display") return { ...section, items: section.items.filter(item =>
|
||||
item.label !== screenTimeoutSetting.label) };
|
||||
return section;
|
||||
@@ -361,7 +363,7 @@ function batteryIndicatorsMenuItem(): MenuItem {
|
||||
return {
|
||||
label: "Battery indicators",
|
||||
description:
|
||||
"Top-bar battery indicators for the phone, Wear OS watch, glasses, and ring: their style, and whether each shows always, only when low, or never.",
|
||||
"Top-bar battery indicators for the phone, watch, glasses, and ring: their style, and whether each shows always, only when low, or never.",
|
||||
onSelect: (ctx) => {
|
||||
openSettingsSubMenu(ctx, "Battery indicators", [
|
||||
enumSettingMenuItem(batteryDisplayModeSetting),
|
||||
@@ -622,12 +624,21 @@ function renderWatchStatus(args: { image: GrayImage; x: number; y: number; width
|
||||
const font = getDefaultSmallFont();
|
||||
let status: string;
|
||||
if (!wearBridge.isAvailable()) {
|
||||
status = "Google Play services is unavailable on this phone, so no watch can connect.";
|
||||
status = global.isIOS
|
||||
? "This device can't pair with an Apple Watch."
|
||||
: "Google Play services is unavailable on this phone, so no watch can connect.";
|
||||
} else {
|
||||
const connection = wearBridge.getWatchConnection();
|
||||
status = connection.reachable
|
||||
? `Connected to ${connection.watchName || "a watch"}.`
|
||||
: "No watch connected. Install the Faceclaw watch app (wear/ in the source tree) on a Wear OS watch paired with this phone.";
|
||||
if (global.isIOS) {
|
||||
// iOS only says whether the app is installed; it may not be running.
|
||||
status = connection.reachable
|
||||
? "The Faceclaw app is installed on your Apple Watch."
|
||||
: "No watch connected. Install the Faceclaw watch app (apple-watch/ in the source tree) on an Apple Watch paired with this iPhone.";
|
||||
} else {
|
||||
status = connection.reachable
|
||||
? `Connected to ${connection.watchName || "a watch"}.`
|
||||
: "No watch connected. Install the Faceclaw watch app (wear/ in the source tree) on a Wear OS watch paired with this phone.";
|
||||
}
|
||||
}
|
||||
const lines = wrapText(font, status, width);
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
|
||||
+4
-4
@@ -2,9 +2,9 @@ import type { RingInput } from "../g2/ring-input";
|
||||
|
||||
/**
|
||||
* Where an input came from. The ring and the glasses' arms are the stock
|
||||
* sources; "watch" is the Wear OS remote, which components may treat with a
|
||||
* richer scheme (see isWatchInput) — the ring's semantics are never changed
|
||||
* by it.
|
||||
* sources; "watch" is the watch remote (Wear OS or Apple Watch), which
|
||||
* components may treat with a richer scheme (see isWatchInput) — the ring's
|
||||
* semantics are never changed by it.
|
||||
*/
|
||||
export type InputSource = "ring" | "left-arm" | "right-arm" | "watch";
|
||||
|
||||
@@ -80,7 +80,7 @@ export function makeInputEvent(payload: InputEventPayload): InputEvent {
|
||||
return { ...payload, timestampMs: Date.now() };
|
||||
}
|
||||
|
||||
/** True for input from the Wear OS remote (any type that carries a source). */
|
||||
/** True for input from the watch remote (any type that carries a source). */
|
||||
export function isWatchInput(event: InputEvent): boolean {
|
||||
return "source" in event && event.source === "watch";
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user