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;
}
}