Add silent-mode and low-battery indicators to Android preview form

This commit is contained in:
James Babcock committed 2026-07-20 12:02:20 -07:00
1 parent bbd3bbcf13
commit a5c6f18795
8 files changed
+216 -3

No files matched your search

@@ -99,6 +99,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private long lastShutdownExitAtMs = 0;
private int headsetBattery = -1;
private int headsetCharging = -1;
// Silent mode: 1 = on, 0 = off, -1 = not yet known. See updateSilentModeLocked.
private int silentMode = -1;
private boolean audioCaptureActive;
private boolean firmwareInfoQueried;
// Glasses are in the charging case: nobody is wearing them, so display
@@ -190,6 +192,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
workerThread = null;
resetSessionStateLocked();
clearAllMessagesLocked("disconnect");
// Unknown until the next connection's first push or settings poll.
silentMode = -1;
}
bleManager.disconnect(rightAddress);
bleManager.disconnect(leftAddress);
@@ -624,6 +628,16 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
G2Event event = null;
synchronized (lock) {
lastIncomingAtMs = SystemClock.elapsedRealtime();
if (frame.ok && frame.sid == BleProtocol.SID_UI_SETTING) {
// Device-initiated settings push. It carries a magic the glasses
// chose, so it would otherwise fall through to resolveAckLocked,
// match nothing, and be logged as an unexpected ack.
int pushedSilentMode = BleProtocol.parseSilentModePush(frame.pb);
if (pushedSilentMode >= 0) {
updateSilentModeLocked(pushedSilentMode > 0);
return;
}
}
if (frame.ok && frame.msgSeq >= 0 && frame.flag != BleProtocol.FLAG_NOTIFY && frame.flag != BleProtocol.FLAG_NOTIFY_ALT) {
lastAckAtMs = lastIncomingAtMs;
resolveAckLocked(frame.sid, frame.msgSeq, frame.pb);
@@ -638,6 +652,12 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
if (!pureImuSample) {
lastConnectionOrInputAtMs = lastIncomingAtMs;
}
if ("list-click".equals(event.kind) || "text-click".equals(event.kind)) {
// Container-routed touchpad input reached us, so the
// firmware is dispatching input: silent mode is off,
// whether or not its end-of-silent push arrived.
updateSilentModeLocked(false);
}
if ("sys-event".equals(event.kind)) {
if (event.eventType == BleProtocol.EVENT_FOREGROUND_EXIT || event.eventType == BleProtocol.EVENT_ABNORMAL_EXIT || event.eventType == BleProtocol.EVENT_SYSTEM_EXIT) {
if (shutdownRequested) {
@@ -1664,6 +1684,12 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
headsetBattery = snapshot.battery;
headsetCharging = snapshot.charging;
emitBatteryState(headsetBattery, headsetCharging);
if (snapshot.silentMode >= 0) {
// Backstop for the push in onNotification: the firmware is
// confirmed to push silent-mode-on, but the off transition is
// not, so re-read the authoritative value on every poll.
updateSilentModeLocked(snapshot.silentMode > 0);
}
updateChargingModeLocked(snapshot.charging > 0, snapshot.battery);
}
BleProtocol.FirmwareInfo firmwareInfo = BleProtocol.parseSettingsFirmwareInfo(message.ackPayload);
@@ -1677,6 +1703,22 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
return message;
}
/**
* Track silent mode, which the wearer toggles by long-pressing both
* touchpads at once. While it is on the firmware refuses input events and
* app launches and powers the display down, so the glasses look dead even
* though the BLE session is healthy; the phone UI says so explicitly.
*/
private void updateSilentModeLocked(boolean silent) {
int next = silent ? 1 : 0;
if (silentMode == next) {
return;
}
silentMode = next;
logLine(silent ? "glasses entered silent mode" : "glasses left silent mode");
emitSilentMode(silent);
}
/**
* Track whether the glasses are in the charging case. Charging means nobody
* is wearing them: display communication pauses (no heartbeats, so the
@@ -1921,6 +1963,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
audioPacketListener = null;
displayedFingerprint = "";
displayedBmp = new byte[0];
// Deliberately not clearing silentMode: it is a property of the glasses,
// not of our session, and silent mode blocks app launches, so it can be
// the very cause of the session teardown that got us here.
}
private boolean hasPendingOrInflightTileLocked(int tileIndex) {
@@ -1989,6 +2034,20 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
});
}
private void emitSilentMode(boolean silent) {
final FaceclawBleCommunicatorListener current = listener;
if (current == null) {
return;
}
mainHandler.post(() -> {
try {
current.onSilentMode(silent);
} catch (Throwable t) {
Log.w(TAG, "listener onSilentMode failed", t);
}
});
}
private void emitBatteryState(int headsetBattery, int headsetCharging) {
final FaceclawBleCommunicatorListener current = listener;
if (current == null) {
@@ -5,6 +5,7 @@ public interface FaceclawBleCommunicatorListener {
void onStateChange(String phase, String status);
void onRingEvent(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode, int frameId);
void onBatteryState(int headsetBattery, int headsetCharging);
void onSilentMode(boolean silent);
void onEvenAppConflict(String message);
void onFrameMetrics(int paintMs, int transmitMs, int tileCount);
void onFrameFinished(int frameId, String outcome);
@@ -31,6 +31,8 @@ public class BleProtocol {
public static final int SID_APP_LAUNCH = 0x01;
public static final int SID_EVENHUB = 0xe0;
public static final int SID_UI_SETTING = 0x09;
/** G2SettingPackage.commandId for device-initiated pushes (vs. 2 = app read). */
public static final int SETTINGS_CMD_DEVICE_SEND_TO_APP = 3;
// UI_FOREGROUND_EVEN_AI_ID: the stock "Even AI" assistant app. It is a
// FOREGROUND app, whereas EvenHub (and therefore faceclaw) is the
// BACKGROUND app -- which is why an Even AI launch draws over us.
@@ -286,10 +288,37 @@ public class BleProtocol {
}
int battery = readVarintFieldValue(request, 12, -1);
int charging = readVarintFieldValue(request, 13, -1);
int silentMode = readVarintFieldValue(request, 14, -1);
if (battery < 0) {
return null;
}
return new BatterySnapshot(battery, charging);
return new BatterySnapshot(battery, charging, silentMode);
}
/**
* Silent mode from an unsolicited settings push. The wearer toggles silent
* mode by long-pressing both touchpads at once; the firmware then sends
* G2SettingPackage{commandId=3 (DeviceSendToAPP), deviceSendInfoToApp(5){
* silentModeSwitch(2)}} on sid 0x09, with a magic it picked itself, so this
* arrives outside the request/ack flow.
*
* Returns -1 when the frame is not such a push. Ordinary settings-read acks
* (the battery poll) carry commandId=2 and deviceReceiveRequestFromApp in
* field 4, so they never match and stay on the normal ack path.
*/
public static int parseSilentModePush(byte[] pb) {
if (pb == null) {
return -1;
}
byte[] root = stripTrailingCrc(pb);
if (readVarintFieldValue(root, 1, -1) != SETTINGS_CMD_DEVICE_SEND_TO_APP) {
return -1;
}
byte[] info = readFieldBytes(root, 5);
if (info == null) {
return -1;
}
return readVarintFieldValue(info, 2, -1);
}
/**
@@ -699,10 +728,13 @@ public class BleProtocol {
public static final class BatterySnapshot {
final int battery;
final int charging;
/** 1 = silent mode on, 0 = off, -1 = the ack didn't carry the field. */
final int silentMode;
BatterySnapshot(int battery, int charging) {
BatterySnapshot(int battery, int charging, int silentMode) {
this.battery = battery;
this.charging = charging;
this.silentMode = silentMode;
}
}
+17
View File
@@ -29,6 +29,23 @@ Button.-primary {
font-size: 18;
}
/*
* Stands in for the lens preview when showing it would be misleading (silent
* mode, or a disconnect explained by a flat battery). Sized by the view model
* to the preview's own box so the form doesn't reflow, and kept dark so it
* reads as "where the preview goes" rather than as a page-level banner.
*/
.display-preview-message {
background-color: #222222;
color: #ffffff;
padding: 16;
}
.display-preview-message Label {
font-size: 18;
text-align: center;
}
.system-name-modal {
margin: 16;
padding: 16;
+51
View File
@@ -90,6 +90,12 @@ export type DashboardSnapshot = {
status: string;
log: string;
displayPreview: ImageSource | null;
/**
* When non-empty, the phone UI shows this instead of the display preview:
* the preview would be a black rectangle indistinguishable from dead
* glasses, and this says which harmless thing is actually going on.
*/
displayPreviewMessage: string;
activeTextSettingId: string | null;
activeTextSettingTitle: string;
activeTextSettingValue: string;
@@ -114,6 +120,8 @@ const SCREEN_TIMEOUT_CHECK_MS = 1_000;
const FOREGROUND_NOTIFICATION_MIN_UPDATE_MS = 30_000;
const FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS = 6_000;
const CONNECTED_PREVIEW_MIN_UPDATE_MS = 1_000;
// Below this, a disconnect is more likely a flat battery than a BLE problem.
const LOW_BATTERY_PERCENT = 5;
const EVEN_APP_DETECTED_MESSAGE =
"The Even Realities app appears to be running. If Faceclaw has trouble connecting, open its app settings and force stop it.";
@@ -155,6 +163,10 @@ class DashboardController {
private firmwareWarningMessage = "";
private batteryOptimizationWarningVisible = false;
private displayPreview: ImageSource | null = createInitialDisplayPreview();
private silentMode = false;
// Last battery level the glasses reported, kept across disconnects so a
// drop-off right after a low reading can be explained as a flat battery.
private lastHeadsetBattery: number | null = null;
private readonly listeners = new Set<DashboardListener>();
// Set at connect time from the persisted flag; the one-time post-onboarding
// welcome sound plays on the first rendered frame (proof the session is warm).
@@ -168,6 +180,7 @@ class DashboardController {
private offLog: (() => void) | null = null;
private offRing: (() => void) | null = null;
private offBattery: (() => void) | null = null;
private offSilentMode: (() => void) | null = null;
private offEvenAppConflict: (() => void) | null = null;
private offFrameMetrics: (() => void) | null = null;
private offFirmwareInfo: (() => void) | null = null;
@@ -299,6 +312,7 @@ class DashboardController {
status: this.status,
log: this.log,
displayPreview: this.displayPreview,
displayPreviewMessage: this.displayPreviewMessage(),
activeTextSettingId: this.activeTextSetting?.id ?? null,
activeTextSettingTitle: this.activeTextSetting?.editorTitle ?? "",
activeTextSettingValue: this.activeTextSetting?.get() ?? "",
@@ -311,6 +325,28 @@ class DashboardController {
};
}
/**
* Message to show in place of the display preview, or "" to show the preview.
*
* Both cases look identical to a dead pair of glasses from the phone side:
* silent mode blanks the display and swallows input while the BLE session
* stays up, and a battery that just ran out simply stops answering.
*/
private displayPreviewMessage(): string {
if (this.silentMode && (this.phase === "connected" || this.phase === "charging")) {
return "Connected (Silent mode enabled)";
}
const connectionFailing = this.phase === "disconnected" || this.phase === "connecting";
if (
connectionFailing &&
this.lastHeadsetBattery !== null &&
this.lastHeadsetBattery < LOW_BATTERY_PERCENT
) {
return "Disconnected (low battery)";
}
return "";
}
/**
* Re-check the Doze exemption (cheap system call, but cached so snapshot()
* stays trivial). Called on connect, page load, and after the user answers
@@ -444,7 +480,13 @@ class DashboardController {
this.appendLog(`input handler failed: ${message}`);
});
});
this.offSilentMode = communicator.onSilentMode((silent) => {
if (this.silentMode === silent) return;
this.silentMode = silent;
this.emit();
});
this.offBattery = communicator.onBatteryState((state) => {
this.lastHeadsetBattery = state.battery >= 0 ? state.battery : null;
shell.setBatteryLevels({
headset: state.battery,
headsetCharging: state.chargingStatus > 0,
@@ -545,6 +587,8 @@ class DashboardController {
this.offRing = null;
this.offBattery?.();
this.offBattery = null;
this.offSilentMode?.();
this.offSilentMode = null;
this.offEvenAppConflict?.();
this.offEvenAppConflict = null;
this.offFrameMetrics?.();
@@ -585,6 +629,8 @@ class DashboardController {
this.offRing = null;
this.offBattery?.();
this.offBattery = null;
this.offSilentMode?.();
this.offSilentMode = null;
this.offEvenAppConflict?.();
this.offEvenAppConflict = null;
this.offFrameMetrics?.();
@@ -1146,6 +1192,11 @@ class DashboardController {
private setPhase(phase: ConnectionPhase): void {
if (this.phase === phase) return;
this.phase = phase;
if (phase === "disconnected") {
// Kept across "connecting": silent mode blocks app launches, so it can
// itself cause the reconnect churn, and Java re-reports it either way.
this.silentMode = false;
}
this.emit();
}
+13
View File
@@ -108,6 +108,7 @@ export class FaceclawCommunicatorBridge {
private readonly stateListeners = new Set<(state: CommunicatorState) => void>();
private readonly ringListeners = new Set<(event: RawInputEvent) => void>();
private readonly batteryListeners = new Set<(state: HeadsetBatteryState) => void>();
private readonly silentModeListeners = new Set<(silent: boolean) => void>();
private readonly evenAppConflictListeners = new Set<(message: string) => void>();
private readonly frameMetricsListeners = new Set<(metrics: FrameMetrics) => void>();
private readonly firmwareInfoListeners = new Set<(info: FirmwareInfo) => void>();
@@ -159,6 +160,9 @@ export class FaceclawCommunicatorBridge {
};
this.emitAsync(this.batteryListeners, state);
},
onSilentMode: (silent: boolean) => {
this.emitAsync(this.silentModeListeners, Boolean(silent));
},
onEvenAppConflict: (message: string) => {
this.emitAsync(this.evenAppConflictListeners, String(message));
},
@@ -239,6 +243,15 @@ export class FaceclawCommunicatorBridge {
return () => this.batteryListeners.delete(listener);
}
/**
* Silent mode, toggled on the glasses by long-pressing both touchpads. While
* it is on the firmware ignores all input and blanks the display.
*/
onSilentMode(listener: (silent: boolean) => void): () => void {
this.silentModeListeners.add(listener);
return () => this.silentModeListeners.delete(listener);
}
onEvenAppConflict(listener: (message: string) => void): () => void {
this.evenAppConflictListeners.add(listener);
return () => this.evenAppConflictListeners.delete(listener);
+17
View File
@@ -11,6 +11,13 @@
<StackLayout row="0" visibility="{{ displayPreviewVisibility }}">
<Image src="{{ displayPreview }}" stretch="fill" height="{{ displayPreviewHeight }}" />
</StackLayout>
<GridLayout
row="0"
class="display-preview-message"
height="{{ displayPreviewHeight }}"
visibility="{{ displayPreviewMessageVisibility }}">
<Label text="{{ displayPreviewMessage }}" textWrap="true" horizontalAlignment="center" verticalAlignment="center" />
</GridLayout>
<GridLayout row="1" rows="auto,auto,auto,*" class="p-20">
<StackLayout row="0" class="m-b-8">
<StackLayout visibility="{{ evenAppConflictWarningVisibility }}">
@@ -62,6 +69,16 @@
width="{{ landscapeDisplayPreviewWidth }}"
height="{{ landscapeDisplayPreviewHeight }}" />
</StackLayout>
<GridLayout
row="0"
col="0"
class="display-preview-message"
horizontalAlignment="left"
width="{{ landscapeDisplayPreviewWidth }}"
height="{{ landscapeDisplayPreviewHeight }}"
visibility="{{ displayPreviewMessageVisibility }}">
<Label text="{{ displayPreviewMessage }}" textWrap="true" horizontalAlignment="center" verticalAlignment="center" />
</GridLayout>
<StackLayout row="0" col="1" class="m-l-16">
<GridLayout rows="auto,auto,auto" columns="*,*,*">
<Button row="0" col="1" text="Swipe up" tap="{{ onSyntheticUpTap }}" />
+24 -1
View File
@@ -8,6 +8,7 @@ export class MainViewModel extends Observable {
private _status = "Disconnected.";
private _log = "";
private _displayPreview: ImageSource | null = null;
private _displayPreviewMessage = "";
private _layoutOrientation: LayoutOrientation = this.readLayoutOrientation();
private _activeTextSettingId: string | null = null;
private _activeTextSettingTitle = "";
@@ -27,6 +28,7 @@ export class MainViewModel extends Observable {
this.status = snapshot.status;
this.log = snapshot.log;
this.displayPreview = snapshot.displayPreview;
this.displayPreviewMessage = snapshot.displayPreviewMessage;
this.phase = snapshot.phase;
this.activeTextSettingId = snapshot.activeTextSettingId;
this.activeTextSettingTitle = snapshot.activeTextSettingTitle;
@@ -75,12 +77,33 @@ export class MainViewModel extends Observable {
}
}
get displayPreviewMessage(): string {
return this._displayPreviewMessage;
}
set displayPreviewMessage(value: string) {
if (this._displayPreviewMessage !== value) {
this._displayPreviewMessage = value;
this.notifyPropertyChange("displayPreviewMessage", value);
this.notifyPropertyChange("displayPreviewMessageVisibility", this.displayPreviewMessageVisibility);
this.notifyPropertyChange("displayPreviewVisibility", this.displayPreviewVisibility);
}
}
get hasDisplayPreview(): boolean {
return this._displayPreview !== null;
}
/**
* The preview and its stand-in message are mutually exclusive and occupy the
* same box, so the form doesn't reflow when one replaces the other.
*/
get displayPreviewVisibility(): "visible" | "collapse" {
return this.hasDisplayPreview ? "visible" : "collapse";
return this.hasDisplayPreview && !this._displayPreviewMessage ? "visible" : "collapse";
}
get displayPreviewMessageVisibility(): "visible" | "collapse" {
return this._displayPreviewMessage ? "visible" : "collapse";
}
get displayPreviewHeight(): number {