Add battery indicator for Android Wear watch

This commit is contained in:
James Babcock committed 2026-09-12 19:35:38 -07:00
1 parent 5bbb04abce
commit 937c73d81b
15 files changed
+279 -18

No files matched your search

+71 -2
View File
@@ -14,6 +14,12 @@
* stream to the watch as events so a query made from the wrist can be read
* from the wrist.
*
* 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.
*
* Transport is app/native/wear-bridge.ts (Java FaceclawWearBridge); the
* message shapes are documented in wear/PROTOCOL.md.
*/
@@ -88,6 +94,14 @@ 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;
// Each poll wakes the watch app's process for a moment; the level changes
// slowly, and a charger being plugged in is pushed at once whenever the
// watch app is open, so a leisurely cadence is enough.
const WATCH_BATTERY_POLL_INTERVAL_MS = 5 * 60_000;
// Floor between polls that state changes (a watch coming into range, the
// glasses connecting) would otherwise trigger back to back.
const WATCH_BATTERY_POLL_MIN_GAP_MS = 20_000;
const WATCH_BATTERY_URGENT_POLL_LIMIT = 3;
/** What the dashboard controller lends the watch remote. */
export type WearRemoteHost = {
@@ -120,6 +134,11 @@ export class WearRemote {
private assistantStreamTimer: ReturnType<typeof setTimeout> | null = null;
private assistantPendingText: string | null = null;
private lastAssistantSentAtMs = 0;
private lastWatchBatteryPollAtMs = 0;
private watchBatteryKnown = false;
// 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;
constructor(private readonly host: WearRemoteHost) {
this.active = wearBridge.isAvailable();
@@ -141,8 +160,16 @@ export class WearRemote {
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);
// A watch that just came into range gets the current state at once,
// and is asked for its battery; one that left takes its indicator
// with it (no placeholder).
if (changed) this.watchBatteryPollsUnanswered = 0;
if (connection.reachable) {
this.publishNow(true);
this.pollWatchBattery(true);
} else if (changed) {
this.setWatchBattery(null, null);
}
});
shell.onAssistantActivity((event) => this.forwardAssistantActivity(event));
shell.onAlertShown((text) => {
@@ -155,9 +182,38 @@ 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);
this.schedulePublish();
}
/**
* 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
* just appeared); a level already on screen waits for the regular cadence.
*/
private pollWatchBattery(urgent: boolean): void {
if (!this.active || !this.watchReachable) return;
const now = Date.now();
const hurry = urgent && !this.watchBatteryKnown && this.watchBatteryPollsUnanswered < WATCH_BATTERY_URGENT_POLL_LIMIT;
const gap = hurry ? WATCH_BATTERY_POLL_MIN_GAP_MS : WATCH_BATTERY_POLL_INTERVAL_MS;
if (now - this.lastWatchBatteryPollAtMs < gap) return;
this.lastWatchBatteryPollAtMs = now;
this.watchBatteryPollsUnanswered++;
wearBridge.sendToWatch(WEAR_PATHS.batteryRequest, {});
}
/** Record a watch battery report (or its absence) and repaint the indicators. */
private setWatchBattery(battery: number | null, charging: boolean | null): void {
const current = shell.getBatteryLevels();
if (current.watch === battery && current.watchCharging === charging) return;
this.watchBatteryKnown = battery !== null;
shell.setBatteryLevels({ watch: battery, watchCharging: charging });
// Repaint the top bar, as the controller does for G2/R1 reports.
const phase = this.host.getState().phase;
if (phase === "connected" || phase === "charging") this.host.requestShellRender();
this.host.appendLog(battery === null ? "watch: battery unknown" : `watch: battery ${battery}%${charging ? " (charging)" : ""}`);
}
/** Whether a watch running the Faceclaw watch app is reachable right now. */
isWatchReachable(): boolean {
return this.watchReachable;
@@ -183,6 +239,10 @@ export class WearRemote {
} catch (error) {
this.host.appendLog(`watch: state publish failed: ${formatError(error)}`);
}
// The indicator is only ever drawn while the glasses are connected, so
// a still-unknown level is worth asking for as soon as they are.
const phase = this.host.getState().phase;
if (phase === "connected" || phase === "charging") this.pollWatchBattery(true);
}
private buildState(): Record<string, unknown> {
@@ -240,6 +300,15 @@ export class WearRemote {
ack(true);
return;
}
if (message.path === WEAR_PATHS.battery) {
// Status, not control: not gated by the remote setting and never
// 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;
this.setWatchBattery(valid ? battery : null, valid ? message.payload.charging === true : null);
return;
}
if (!watchRemoteEnabledSetting.get()) {
ack(false, "Watch control is turned off in Faceclaw's Settings > Watch.");
return;