Add charging mode

This commit is contained in:
James Babcock committed 2026-07-05 00:35:03 -07:00
1 parent 875956e140
commit 4b73cbff35
5 files changed
+138 -47

No files matched your search

@@ -90,6 +90,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private int headsetCharging = -1; private int headsetCharging = -1;
private boolean audioCaptureActive; private boolean audioCaptureActive;
private boolean firmwareInfoQueried; private boolean firmwareInfoQueried;
// Glasses are in the charging case: nobody is wearing them, so display
// communication pauses and only battery polls flow (see driveSession).
private boolean chargingMode;
private volatile FaceclawAudioPacketListener audioPacketListener; private volatile FaceclawAudioPacketListener audioPacketListener;
private PowerManager.WakeLock g2ScreenWakeLock; private PowerManager.WakeLock g2ScreenWakeLock;
@@ -537,6 +540,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
fixedLayoutCreated = false; fixedLayoutCreated = false;
warmedUp = false; warmedUp = false;
startupProbePending = false; startupProbePending = false;
chargingMode = false;
audioCaptureActive = false; audioCaptureActive = false;
audioPacketListener = null; audioPacketListener = null;
clearAllMessagesLocked("connection lost"); clearAllMessagesLocked("connection lost");
@@ -782,44 +786,61 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
} }
} }
if (messageToPrewrite == null if (chargingMode) {
&& !shutdownRequested // Glasses are in the case: no display traffic, only battery
&& !fixedLayoutCreated // polls (which also detect the end of charging).
&& pendingMessages.isEmpty() finishDesiredFrameLocked("discarded: glasses charging");
&& inFlightMessages.isEmpty()) { if (sessionReady && inFlightMessages.isEmpty() && !pendingMessages.isEmpty()) {
Log.i(TAG, "enqueueing create layout"); messageToWrite = pendingMessages.removeFirst();
enqueueCreateLayoutLocked(); Log.i(TAG, "sending pending message (charging): " + messageToWrite.label);
} else if (messageToPrewrite != null) { } else if (sessionReady && pendingMessages.isEmpty() && inFlightMessages.isEmpty()
// Prewrite outside the lock; the logical message remains pending until && now - lastBatteryRefreshAtMs >= ConnectionOptions.CHARGING_BATTERY_POLL_MS) {
// its final BLE frame is sent after the current protocol ACK. Log.i(TAG, "Writing charging-mode battery poll");
} messageToWrite = createBatteryQueryMessageLocked();
if (messageToPrewrite == null && !shutdownRequested && fixedLayoutCreated && !warmedUp && pendingMessages.isEmpty() && inFlightMessages.isEmpty()) { lastBatteryRefreshAtMs = now;
Log.i(TAG, "enqueueing warmup"); } else {
enqueueWarmupLocked(); return 1_000;
} }
} else {
if (messageToPrewrite == null
&& !shutdownRequested
&& !fixedLayoutCreated
&& pendingMessages.isEmpty()
&& inFlightMessages.isEmpty()) {
Log.i(TAG, "enqueueing create layout");
enqueueCreateLayoutLocked();
} else if (messageToPrewrite != null) {
// Prewrite outside the lock; the logical message remains pending until
// its final BLE frame is sent after the current protocol ACK.
}
if (messageToPrewrite == null && !shutdownRequested && fixedLayoutCreated && !warmedUp && pendingMessages.isEmpty() && inFlightMessages.isEmpty()) {
Log.i(TAG, "enqueueing warmup");
enqueueWarmupLocked();
}
if (messageToPrewrite == null && handleHeartbeat()) { if (messageToPrewrite == null && handleHeartbeat()) {
return ConnectionOptions.IDLE_SLEEP_MS; return ConnectionOptions.IDLE_SLEEP_MS;
} }
if (messageToPrewrite == null && sessionReady && inFlightMessages.isEmpty() && !pendingMessages.isEmpty()) { if (messageToPrewrite == null && sessionReady && inFlightMessages.isEmpty() && !pendingMessages.isEmpty()) {
OutboundMessage pending = pendingMessages.peekFirst(); OutboundMessage pending = pendingMessages.peekFirst();
messageToWrite = pendingMessages.removeFirst(); messageToWrite = pendingMessages.removeFirst();
Log.i(TAG, "sending pending message: " + messageToWrite.label); Log.i(TAG, "sending pending message: " + messageToWrite.label);
} else if (messageToPrewrite == null && !shutdownRequested && fixedLayoutCreated && warmedUp && pendingMessages.isEmpty() && inFlightMessages.isEmpty() } else if (messageToPrewrite == null && !shutdownRequested && fixedLayoutCreated && warmedUp && pendingMessages.isEmpty() && inFlightMessages.isEmpty()
&& now >= imageRetryAfterMs && now >= imageRetryAfterMs
&& !getDesiredFingerprint().equals(displayedFingerprint)) { && !getDesiredFingerprint().equals(displayedFingerprint)) {
Log.i(TAG, "Enqueued image update"); Log.i(TAG, "Enqueued image update");
enqueueDesiredImageLocked(); enqueueDesiredImageLocked();
return 0; return 0;
} else if (messageToPrewrite == null && shouldPollBatteryLocked(now)) { } else if (messageToPrewrite == null && shouldPollBatteryLocked(now)) {
Log.i(TAG, "Writing battery query"); Log.i(TAG, "Writing battery query");
messageToWrite = createBatteryQueryMessageLocked(); messageToWrite = createBatteryQueryMessageLocked();
lastBatteryRefreshAtMs = now; lastBatteryRefreshAtMs = now;
} else if (messageToPrewrite == null && (!pendingMessages.isEmpty() || !inFlightMessages.isEmpty())) { } else if (messageToPrewrite == null && (!pendingMessages.isEmpty() || !inFlightMessages.isEmpty())) {
return ConnectionOptions.IDLE_SLEEP_MS; return ConnectionOptions.IDLE_SLEEP_MS;
} else if (messageToPrewrite == null) { } else if (messageToPrewrite == null) {
return 250; return 250;
}
} }
} }
@@ -1360,6 +1381,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
headsetBattery = snapshot.battery; headsetBattery = snapshot.battery;
headsetCharging = snapshot.charging; headsetCharging = snapshot.charging;
emitBatteryState(headsetBattery, headsetCharging); emitBatteryState(headsetBattery, headsetCharging);
updateChargingModeLocked(snapshot.charging > 0, snapshot.battery);
} }
BleProtocol.FirmwareInfo firmwareInfo = BleProtocol.parseSettingsFirmwareInfo(message.ackPayload); BleProtocol.FirmwareInfo firmwareInfo = BleProtocol.parseSettingsFirmwareInfo(message.ackPayload);
if (firmwareInfo != null) { if (firmwareInfo != null) {
@@ -1372,6 +1394,53 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
return message; return message;
} }
/**
* Track whether the glasses are in the charging case. Charging means nobody
* is wearing them: display communication pauses (no heartbeats, so the
* firmware tears down its EvenHub context on its own) and only battery polls
* continue. When charging stops, tear the transport down and let the normal
* reconnect loop rebuild the session, layout, and first frame.
*/
private void updateChargingModeLocked(boolean charging, int battery) {
if (charging == chargingMode) {
if (chargingMode) {
setStateDisplay("charging", chargingStatusText(battery));
}
return;
}
if (charging) {
chargingMode = true;
clearAllMessagesLocked("glasses charging");
fixedLayoutCreated = false;
warmedUp = false;
startupProbePending = false;
displayedFingerprint = "";
displayedBmp = new byte[0];
finishDesiredFrameLocked("discarded: glasses charging");
logLine("glasses are charging; pausing display communication");
setStateDisplay("charging", chargingStatusText(battery));
} else {
chargingMode = false;
logLine("glasses removed from charger; reconnecting");
handleTransportFailure("charging ended");
}
}
private static String chargingStatusText(int battery) {
return battery >= 0
? "Glasses charging. Battery " + battery + "%."
: "Glasses charging.";
}
private void finishDesiredFrameLocked(String outcome) {
int frameIdToFinish;
synchronized (desiredTilesLock) {
frameIdToFinish = desiredFrameId;
desiredFrameId = 0;
}
finishFrame(frameIdToFinish, outcome);
}
private boolean hasPendingOrInflightKindLocked(String kind) { private boolean hasPendingOrInflightKindLocked(String kind) {
for (OutboundMessage queued : pendingMessages) { for (OutboundMessage queued : pendingMessages) {
if (kind.equals(queued.kind)) { if (kind.equals(queued.kind)) {
@@ -1513,6 +1582,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
warmedUp = false; warmedUp = false;
startupProbePending = false; startupProbePending = false;
shutdownRequested = false; shutdownRequested = false;
chargingMode = false;
imageRetryAfterMs = 0; imageRetryAfterMs = 0;
displayedFingerprint = ""; displayedFingerprint = "";
displayedBmp = new byte[0]; displayedBmp = new byte[0];
@@ -1531,6 +1601,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
sessionReady = false; sessionReady = false;
fixedLayoutCreated = false; fixedLayoutCreated = false;
warmedUp = false; warmedUp = false;
chargingMode = false;
rightConnected = false; rightConnected = false;
leftConnected = false; leftConnected = false;
ringConnected = false; ringConnected = false;
@@ -18,6 +18,9 @@ public class ConnectionOptions {
public static final int HEARTBEAT_URGENT_MS = 6_000; public static final int HEARTBEAT_URGENT_MS = 6_000;
public static final int BATTERY_REFRESH_INTERVAL_MS = 5 * 60_000; public static final int BATTERY_REFRESH_INTERVAL_MS = 5 * 60_000;
public static final int BATTERY_INPUT_QUIET_MS = 5_000; public static final int BATTERY_INPUT_QUIET_MS = 5_000;
// Poll cadence while in charging mode; also bounds how quickly we notice
// the glasses coming back out of the case.
public static final int CHARGING_BATTERY_POLL_MS = 30_000;
public static final int IMAGE_FRAGMENT_SIZE = 3800; public static final int IMAGE_FRAGMENT_SIZE = 3800;
public static final int IMAGE_RETRY_DELAY_MS = 2_000; public static final int IMAGE_RETRY_DELAY_MS = 2_000;
public static final int MAX_CONSECUTIVE_ACK_TIMEOUTS = 8; public static final int MAX_CONSECUTIVE_ACK_TIMEOUTS = 8;
+22 -7
View File
@@ -39,7 +39,7 @@ import {
import { saveRawScreenshot } from "../native/raw-screenshot"; import { saveRawScreenshot } from "../native/raw-screenshot";
import { isIgnoringBatteryOptimizations, requestIgnoreBatteryOptimizations } from "../native/battery-optimization"; import { isIgnoringBatteryOptimizations, requestIgnoreBatteryOptimizations } from "../native/battery-optimization";
type ConnectionPhase = "disconnected" | "connecting" | "connected" | "disconnecting"; type ConnectionPhase = "disconnected" | "connecting" | "connected" | "charging" | "disconnecting";
export type DashboardSnapshot = { export type DashboardSnapshot = {
phase: ConnectionPhase; phase: ConnectionPhase;
@@ -326,11 +326,18 @@ class DashboardController {
const mappedPhase = const mappedPhase =
state.phase === "connected" state.phase === "connected"
? "connected" ? "connected"
: state.phase === "disconnecting" : state.phase === "charging"
? "disconnecting" ? "charging"
: state.phase === "disconnected" : state.phase === "disconnecting"
? "disconnected" ? "disconnecting"
: "connecting"; : state.phase === "disconnected"
? "disconnected"
: "connecting";
if (mappedPhase === "charging" && this.phase !== "charging") {
// Nobody is wearing the glasses; drop the G2-screen wakelock so the
// phone can sleep normally while they charge.
void this.communicator?.setG2ScreenOn(false).catch(() => {});
}
this.setPhase(mappedPhase); this.setPhase(mappedPhase);
this.setStatus(state.status); this.setStatus(state.status);
}); });
@@ -345,7 +352,7 @@ class DashboardController {
headset: state.battery, headset: state.battery,
headsetCharging: state.chargingStatus > 0, headsetCharging: state.chargingStatus > 0,
}); });
if (this.phase === "connected" && this.communicator) { if ((this.phase === "connected" || this.phase === "charging") && this.communicator) {
void this.requestRender("interval").catch((error) => { void this.requestRender("interval").catch((error) => {
const message = this.formatError(error); const message = this.formatError(error);
this.appendLog(`battery update failed: ${message}`); this.appendLog(`battery update failed: ${message}`);
@@ -700,6 +707,14 @@ class DashboardController {
if (!updatePreviewAfterTransmit) { if (!updatePreviewAfterTransmit) {
this.updateDisplayPreviewFromImage(image); this.updateDisplayPreviewFromImage(image);
} }
if (this.communicator && this.phase === "charging") {
// Glasses are in the charging case; keep the phone preview fresh but
// send nothing.
frameTimings.finishFrame(frameId, "discarded: glasses charging");
this.updateConnectedForegroundNotification();
console.log("renderDashbaord finished (charging)");
return;
}
if (this.communicator) { if (this.communicator) {
if (dashboardState.screenOn) { if (dashboardState.screenOn) {
await this.communicator.setG2ScreenOn(true); await this.communicator.setG2ScreenOn(true);
+1
View File
@@ -7,6 +7,7 @@ export type CommunicatorPhase =
| "disconnected" | "disconnected"
| "connecting" | "connecting"
| "connected" | "connected"
| "charging"
| "retrying" | "retrying"
| "disconnecting"; | "disconnecting";
+5 -4
View File
@@ -18,7 +18,7 @@ export class MainViewModel extends Observable {
private _rawScreenshotsEnabled = false; private _rawScreenshotsEnabled = false;
private _batteryOptimizationWarningVisible = false; private _batteryOptimizationWarningVisible = false;
private _showLog = false; private _showLog = false;
private _phase: "disconnected" | "connecting" | "connected" | "disconnecting" = "disconnected"; private _phase: "disconnected" | "connecting" | "connected" | "charging" | "disconnecting" = "disconnected";
constructor() { constructor() {
super(); super();
@@ -278,11 +278,11 @@ export class MainViewModel extends Observable {
dashboardController.requestBatteryOptimizationExemption(); dashboardController.requestBatteryOptimizationExemption();
} }
get phase(): "disconnected" | "connecting" | "connected" | "disconnecting" { get phase(): "disconnected" | "connecting" | "connected" | "charging" | "disconnecting" {
return this._phase; return this._phase;
} }
set phase(value: "disconnected" | "connecting" | "connected" | "disconnecting") { set phase(value: "disconnected" | "connecting" | "connected" | "charging" | "disconnecting") {
if (this._phase !== value) { if (this._phase !== value) {
this._phase = value; this._phase = value;
this.notifyPropertyChange("phase", value); this.notifyPropertyChange("phase", value);
@@ -296,6 +296,7 @@ export class MainViewModel extends Observable {
case "connecting": case "connecting":
return "Connecting..."; return "Connecting...";
case "connected": case "connected":
case "charging":
return "Disconnect"; return "Disconnect";
case "disconnecting": case "disconnecting":
return "Disconnecting..."; return "Disconnecting...";
@@ -312,7 +313,7 @@ export class MainViewModel extends Observable {
if (!this.canRun) return; if (!this.canRun) return;
try { try {
if (this.phase === "connected") { if (this.phase === "connected" || this.phase === "charging") {
await dashboardController.disconnect(); await dashboardController.disconnect();
} else { } else {
await dashboardController.connect(); await dashboardController.connect();