From 4b73cbff352f45dfc8fc8d97615bb043ebc94e7a Mon Sep 17 00:00:00 2001 From: James Babcock Date: Sun, 5 Jul 2026 00:35:03 -0700 Subject: [PATCH] Add charging mode --- .../faceclaw/app/FaceclawBleCommunicator.java | 143 +++++++++++++----- .../app/g2protocol/ConnectionOptions.java | 3 + app/g2/dashboard-controller.ts | 29 +++- app/native/faceclaw-communicator.ts | 1 + app/phone-ui/main-view-model.ts | 9 +- 5 files changed, 138 insertions(+), 47 deletions(-) diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java index cb24816d..f21e9904 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java @@ -90,6 +90,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { private int headsetCharging = -1; private boolean audioCaptureActive; 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 PowerManager.WakeLock g2ScreenWakeLock; @@ -537,6 +540,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { fixedLayoutCreated = false; warmedUp = false; startupProbePending = false; + chargingMode = false; audioCaptureActive = false; audioPacketListener = null; clearAllMessagesLocked("connection lost"); @@ -782,44 +786,61 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { } } - 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 (chargingMode) { + // Glasses are in the case: no display traffic, only battery + // polls (which also detect the end of charging). + finishDesiredFrameLocked("discarded: glasses charging"); + if (sessionReady && inFlightMessages.isEmpty() && !pendingMessages.isEmpty()) { + messageToWrite = pendingMessages.removeFirst(); + Log.i(TAG, "sending pending message (charging): " + messageToWrite.label); + } else if (sessionReady && pendingMessages.isEmpty() && inFlightMessages.isEmpty() + && now - lastBatteryRefreshAtMs >= ConnectionOptions.CHARGING_BATTERY_POLL_MS) { + Log.i(TAG, "Writing charging-mode battery poll"); + messageToWrite = createBatteryQueryMessageLocked(); + lastBatteryRefreshAtMs = now; + } else { + 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()) { - return ConnectionOptions.IDLE_SLEEP_MS; - } + if (messageToPrewrite == null && handleHeartbeat()) { + return ConnectionOptions.IDLE_SLEEP_MS; + } - if (messageToPrewrite == null && sessionReady && inFlightMessages.isEmpty() && !pendingMessages.isEmpty()) { - OutboundMessage pending = pendingMessages.peekFirst(); - messageToWrite = pendingMessages.removeFirst(); - Log.i(TAG, "sending pending message: " + messageToWrite.label); - } else if (messageToPrewrite == null && !shutdownRequested && fixedLayoutCreated && warmedUp && pendingMessages.isEmpty() && inFlightMessages.isEmpty() - && now >= imageRetryAfterMs - && !getDesiredFingerprint().equals(displayedFingerprint)) { - Log.i(TAG, "Enqueued image update"); - enqueueDesiredImageLocked(); - return 0; - } else if (messageToPrewrite == null && shouldPollBatteryLocked(now)) { - Log.i(TAG, "Writing battery query"); - messageToWrite = createBatteryQueryMessageLocked(); - lastBatteryRefreshAtMs = now; - } else if (messageToPrewrite == null && (!pendingMessages.isEmpty() || !inFlightMessages.isEmpty())) { - return ConnectionOptions.IDLE_SLEEP_MS; - } else if (messageToPrewrite == null) { - return 250; + if (messageToPrewrite == null && sessionReady && inFlightMessages.isEmpty() && !pendingMessages.isEmpty()) { + OutboundMessage pending = pendingMessages.peekFirst(); + messageToWrite = pendingMessages.removeFirst(); + Log.i(TAG, "sending pending message: " + messageToWrite.label); + } else if (messageToPrewrite == null && !shutdownRequested && fixedLayoutCreated && warmedUp && pendingMessages.isEmpty() && inFlightMessages.isEmpty() + && now >= imageRetryAfterMs + && !getDesiredFingerprint().equals(displayedFingerprint)) { + Log.i(TAG, "Enqueued image update"); + enqueueDesiredImageLocked(); + return 0; + } else if (messageToPrewrite == null && shouldPollBatteryLocked(now)) { + Log.i(TAG, "Writing battery query"); + messageToWrite = createBatteryQueryMessageLocked(); + lastBatteryRefreshAtMs = now; + } else if (messageToPrewrite == null && (!pendingMessages.isEmpty() || !inFlightMessages.isEmpty())) { + return ConnectionOptions.IDLE_SLEEP_MS; + } else if (messageToPrewrite == null) { + return 250; + } } } @@ -1360,6 +1381,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { headsetBattery = snapshot.battery; headsetCharging = snapshot.charging; emitBatteryState(headsetBattery, headsetCharging); + updateChargingModeLocked(snapshot.charging > 0, snapshot.battery); } BleProtocol.FirmwareInfo firmwareInfo = BleProtocol.parseSettingsFirmwareInfo(message.ackPayload); if (firmwareInfo != null) { @@ -1372,6 +1394,53 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { 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) { for (OutboundMessage queued : pendingMessages) { if (kind.equals(queued.kind)) { @@ -1513,6 +1582,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { warmedUp = false; startupProbePending = false; shutdownRequested = false; + chargingMode = false; imageRetryAfterMs = 0; displayedFingerprint = ""; displayedBmp = new byte[0]; @@ -1531,6 +1601,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { sessionReady = false; fixedLayoutCreated = false; warmedUp = false; + chargingMode = false; rightConnected = false; leftConnected = false; ringConnected = false; diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/ConnectionOptions.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/ConnectionOptions.java index ca28493a..71bbefe2 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/ConnectionOptions.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/ConnectionOptions.java @@ -18,6 +18,9 @@ public class ConnectionOptions { 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_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_RETRY_DELAY_MS = 2_000; public static final int MAX_CONSECUTIVE_ACK_TIMEOUTS = 8; diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index c5866670..d3349254 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -39,7 +39,7 @@ import { import { saveRawScreenshot } from "../native/raw-screenshot"; import { isIgnoringBatteryOptimizations, requestIgnoreBatteryOptimizations } from "../native/battery-optimization"; -type ConnectionPhase = "disconnected" | "connecting" | "connected" | "disconnecting"; +type ConnectionPhase = "disconnected" | "connecting" | "connected" | "charging" | "disconnecting"; export type DashboardSnapshot = { phase: ConnectionPhase; @@ -326,11 +326,18 @@ class DashboardController { const mappedPhase = state.phase === "connected" ? "connected" - : state.phase === "disconnecting" - ? "disconnecting" - : state.phase === "disconnected" - ? "disconnected" - : "connecting"; + : state.phase === "charging" + ? "charging" + : state.phase === "disconnecting" + ? "disconnecting" + : 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.setStatus(state.status); }); @@ -345,7 +352,7 @@ class DashboardController { headset: state.battery, 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) => { const message = this.formatError(error); this.appendLog(`battery update failed: ${message}`); @@ -700,6 +707,14 @@ class DashboardController { if (!updatePreviewAfterTransmit) { 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 (dashboardState.screenOn) { await this.communicator.setG2ScreenOn(true); diff --git a/app/native/faceclaw-communicator.ts b/app/native/faceclaw-communicator.ts index 70ff3ee6..3b888dc2 100644 --- a/app/native/faceclaw-communicator.ts +++ b/app/native/faceclaw-communicator.ts @@ -7,6 +7,7 @@ export type CommunicatorPhase = | "disconnected" | "connecting" | "connected" + | "charging" | "retrying" | "disconnecting"; diff --git a/app/phone-ui/main-view-model.ts b/app/phone-ui/main-view-model.ts index 49182815..8c44d3db 100644 --- a/app/phone-ui/main-view-model.ts +++ b/app/phone-ui/main-view-model.ts @@ -18,7 +18,7 @@ export class MainViewModel extends Observable { private _rawScreenshotsEnabled = false; private _batteryOptimizationWarningVisible = false; private _showLog = false; - private _phase: "disconnected" | "connecting" | "connected" | "disconnecting" = "disconnected"; + private _phase: "disconnected" | "connecting" | "connected" | "charging" | "disconnecting" = "disconnected"; constructor() { super(); @@ -278,11 +278,11 @@ export class MainViewModel extends Observable { dashboardController.requestBatteryOptimizationExemption(); } - get phase(): "disconnected" | "connecting" | "connected" | "disconnecting" { + get phase(): "disconnected" | "connecting" | "connected" | "charging" | "disconnecting" { return this._phase; } - set phase(value: "disconnected" | "connecting" | "connected" | "disconnecting") { + set phase(value: "disconnected" | "connecting" | "connected" | "charging" | "disconnecting") { if (this._phase !== value) { this._phase = value; this.notifyPropertyChange("phase", value); @@ -296,6 +296,7 @@ export class MainViewModel extends Observable { case "connecting": return "Connecting..."; case "connected": + case "charging": return "Disconnect"; case "disconnecting": return "Disconnecting..."; @@ -312,7 +313,7 @@ export class MainViewModel extends Observable { if (!this.canRun) return; try { - if (this.phase === "connected") { + if (this.phase === "connected" || this.phase === "charging") { await dashboardController.disconnect(); } else { await dashboardController.connect();