forked from Blendman974/faceclaw
Add battery indicator for Android Wear watch
This commit is contained in:
1 parent
5bbb04abce
commit
937c73d81b
15 files changed
+279
-18
No files matched your search
@@ -11,6 +11,7 @@ import {
|
||||
onAnySettingChanged,
|
||||
phoneBatteryVisibilitySetting,
|
||||
ringBatteryVisibilitySetting,
|
||||
watchBatteryVisibilitySetting,
|
||||
type BatteryIndicatorVisibility,
|
||||
} from "../../../ui/dashboard-settings";
|
||||
import { shell } from "../../../ui/shell/shell";
|
||||
@@ -30,9 +31,10 @@ const BATTERY_LABEL_VALUE = 150;
|
||||
type BatteryItem = { label: string; percent: number; charging: boolean };
|
||||
|
||||
/**
|
||||
* Date and time, the phone's notification icons, the phone/G2/R1 battery
|
||||
* indicators, and the Timers app's countdowns: the top bar's contents plus
|
||||
* timers, laid out for a card. Battery style and per-device visibility
|
||||
* Date and time, the phone's notification icons, the phone/watch/G2/R1
|
||||
* battery indicators (the Wear OS watch's only while one is reachable), and
|
||||
* the Timers app's countdowns: the top bar's contents plus timers, laid out
|
||||
* for a card. Battery style and per-device visibility
|
||||
* follow Settings > Display > Battery indicators, so the card agrees with
|
||||
* the bar. Countdowns tick once a second only while one is running.
|
||||
*/
|
||||
@@ -137,6 +139,7 @@ function collectBatteryItems(): BatteryItem[] {
|
||||
const phone = readPhoneBatteryState();
|
||||
push(phoneBatteryVisibilitySetting.get(), "Phone", phone.battery, phone.charging);
|
||||
const levels = shell.getBatteryLevels();
|
||||
push(watchBatteryVisibilitySetting.get(), "Watch", levels.watch, levels.watchCharging);
|
||||
push(glassesBatteryVisibilitySetting.get(), "G2", levels.headset, levels.headsetCharging);
|
||||
if (levels.ring !== null && Number.isInteger(levels.ring) && levels.ring >= 0 && levels.ring <= 100) {
|
||||
push(ringBatteryVisibilitySetting.get(), "R1", levels.ring, levels.ringCharging);
|
||||
|
||||
+71
-2
@@ -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;
|
||||
|
||||
@@ -24,6 +24,10 @@ export const WEAR_PATHS = {
|
||||
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. */
|
||||
@@ -157,7 +161,12 @@ class WearBridge {
|
||||
|
||||
/** Send an event to every reachable watch. */
|
||||
sendEvent(payload: Record<string, unknown>): void {
|
||||
this.getJava()?.sendToWatch(WEAR_PATHS.event, JSON.stringify(payload));
|
||||
this.sendToWatch(WEAR_PATHS.event, payload);
|
||||
}
|
||||
|
||||
/** Send a message on any phone -> watch path to every reachable watch. */
|
||||
sendToWatch(path: string, payload: Record<string, unknown>): void {
|
||||
this.getJava()?.sendToWatch(path, JSON.stringify(payload));
|
||||
}
|
||||
|
||||
/** Acknowledge a watch message; `seq` echoes the watch's sequence number. */
|
||||
|
||||
@@ -244,6 +244,10 @@ 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. */
|
||||
export const watchBatteryVisibilitySetting = batteryVisibilitySetting(
|
||||
"watchBatteryVisibility", "Watch", "display.battery.watchVisibility",
|
||||
);
|
||||
|
||||
/** Whether an indicator with this visibility setting shows at the given charge. */
|
||||
export function batteryIndicatorVisible(visibility: BatteryIndicatorVisibility, percent: number): boolean {
|
||||
@@ -262,6 +266,7 @@ export function batteryIndicatorSettingsKey(): string {
|
||||
phoneBatteryVisibilitySetting.get(),
|
||||
glassesBatteryVisibilitySetting.get(),
|
||||
ringBatteryVisibilitySetting.get(),
|
||||
watchBatteryVisibilitySetting.get(),
|
||||
].join("|");
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,7 @@ import {
|
||||
glassesBatteryVisibilitySetting,
|
||||
phoneBatteryVisibilitySetting,
|
||||
ringBatteryVisibilitySetting,
|
||||
watchBatteryVisibilitySetting,
|
||||
displayModeSetting,
|
||||
navigateDisplayModeSetting,
|
||||
navigateVerticalPositionSetting,
|
||||
@@ -248,11 +249,12 @@ function batteryIndicatorsMenuItem(): MenuItem {
|
||||
return {
|
||||
label: "Battery indicators",
|
||||
description:
|
||||
"Top-bar battery indicators for the phone, glasses, and ring: their style, and whether each shows always, only when low, or never.",
|
||||
"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.",
|
||||
onSelect: (ctx) => {
|
||||
openSettingsSubMenu(ctx, "Battery indicators", [
|
||||
enumSettingMenuItem(batteryDisplayModeSetting),
|
||||
enumSettingMenuItem(phoneBatteryVisibilitySetting),
|
||||
enumSettingMenuItem(watchBatteryVisibilitySetting),
|
||||
enumSettingMenuItem(glassesBatteryVisibilitySetting),
|
||||
enumSettingMenuItem(ringBatteryVisibilitySetting),
|
||||
]);
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
glassesBatteryVisibilitySetting,
|
||||
phoneBatteryVisibilitySetting,
|
||||
ringBatteryVisibilitySetting,
|
||||
watchBatteryVisibilitySetting,
|
||||
type BatteryIndicatorVisibility,
|
||||
} from "../dashboard-settings";
|
||||
import { formatClockDate, formatClockTime } from "../clock-format";
|
||||
@@ -108,7 +109,15 @@ export type ShellChromeState = {
|
||||
/** Height mode of the foreground window; decides where its top bar sits. */
|
||||
foregroundHeightMode: WindowHeightMode;
|
||||
foregroundAppId?: string;
|
||||
battery: { headset: number | null; headsetCharging: boolean | null; ring: number | null; ringCharging: boolean | null };
|
||||
battery: {
|
||||
headset: number | null;
|
||||
headsetCharging: boolean | null;
|
||||
ring: number | null;
|
||||
ringCharging: boolean | null;
|
||||
/** The Wear OS watch (app/g2/wear-remote.ts); null when no watch is reachable. */
|
||||
watch: number | null;
|
||||
watchCharging: boolean | null;
|
||||
};
|
||||
/** App-provided tray images, drawn between notification icons and batteries. */
|
||||
trayIcons: GrayImage[];
|
||||
};
|
||||
@@ -342,9 +351,10 @@ export class ShellChromeLayer implements Layer {
|
||||
}
|
||||
|
||||
/**
|
||||
* Labelled battery indicators for the phone, G2, and R1, right-aligned in
|
||||
* the top bar. The Settings > Display > Battery indicators submenu picks
|
||||
* the style (label beside a gauge icon or percentage, or stacked above
|
||||
* Labelled battery indicators for the phone, Wear OS watch, G2, and R1,
|
||||
* right-aligned in the top bar. The watch one exists only while a watch
|
||||
* running the Faceclaw watch app is reachable (no placeholder otherwise).
|
||||
* The Settings > Display > Battery indicators submenu picks the style (label beside a gauge icon or percentage, or stacked above
|
||||
* either) and, per device, whether the indicator shows always, only below
|
||||
* 50%, or never. Returns the left edge of the battery block.
|
||||
*/
|
||||
@@ -355,6 +365,10 @@ export class ShellChromeLayer implements Layer {
|
||||
if (phone.battery !== null && Number.isFinite(phone.battery)) {
|
||||
pushBatteryItem(items, phoneBatteryVisibilitySetting.get(), "Phone", phone.battery, Boolean(phone.charging));
|
||||
}
|
||||
if (state.battery.watch !== null && Number.isFinite(state.battery.watch)) {
|
||||
pushBatteryItem(items, watchBatteryVisibilitySetting.get(), "Watch", state.battery.watch,
|
||||
Boolean(state.battery.watchCharging));
|
||||
}
|
||||
if (state.battery.headset !== null && Number.isFinite(state.battery.headset)) {
|
||||
pushBatteryItem(items, glassesBatteryVisibilitySetting.get(), "G2", state.battery.headset,
|
||||
Boolean(state.battery.headsetCharging));
|
||||
|
||||
@@ -304,7 +304,9 @@ class Shell {
|
||||
private lastInputAtMs = Date.now();
|
||||
/** The most recent input event received, for windows gaining focus (see ShellWindow.onFocus). */
|
||||
private lastInput: InputEvent | null = null;
|
||||
private battery: ShellChromeState["battery"] = { headset: null, headsetCharging: null, ring: null, ringCharging: null };
|
||||
private battery: ShellChromeState["battery"] = {
|
||||
headset: null, headsetCharging: null, ring: null, ringCharging: null, watch: null, watchCharging: null,
|
||||
};
|
||||
private attention = new Map<string, boolean>();
|
||||
// App-provided top-bar tray icons, keyed by owner id; drawn between the
|
||||
// notification icons and the battery indicators.
|
||||
@@ -495,7 +497,7 @@ class Shell {
|
||||
return this.screenOn;
|
||||
}
|
||||
|
||||
/** Current headset battery levels (for the assistant's get_state tool). */
|
||||
/** Current G2, R1, and Wear OS watch battery levels (top bar, Glanceboard, assistant). */
|
||||
getBatteryLevels(): ShellChromeState["battery"] {
|
||||
return this.battery;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user