From b5273715ae63186883a4466a934066fbe55ca6f9 Mon Sep 17 00:00:00 2001 From: James Babcock Date: Mon, 27 Apr 2026 03:05:53 -0700 Subject: [PATCH] UI changes, system status icons --- .../Android/src/main/AndroidManifest.xml | 1 + .../faceclaw/app/FaceclawBleCommunicator.java | 113 +++++- .../app/FaceclawBleCommunicatorListener.java | 1 + ...eclawMediaNotificationListenerService.java | 2 +- .../app/FaceclawSystemStatusIconProvider.java | 193 ++++++++++ README | 15 + app/g2/dashboard-controller.ts | 57 ++- app/graphics/battery.ts | 60 +++ app/native/faceclaw-communicator.ts | 31 +- app/native/gray-image-preview.ts | 25 +- app/native/system-status-icons.ts | 54 +++ app/ui/dashboard-settings.ts | 102 ++++- app/ui/dashboard.ts | 352 +++++++++--------- app/ui/menu.ts | 60 ++- 14 files changed, 830 insertions(+), 236 deletions(-) create mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSystemStatusIconProvider.java create mode 100644 app/graphics/battery.ts create mode 100644 app/native/system-status-icons.ts diff --git a/App_Resources/Android/src/main/AndroidManifest.xml b/App_Resources/Android/src/main/AndroidManifest.xml index a3ba9ac0..144a5326 100644 --- a/App_Resources/Android/src/main/AndroidManifest.xml +++ b/App_Resources/Android/src/main/AndroidManifest.xml @@ -12,6 +12,7 @@ + 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 b4904b77..c551013c 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 @@ -35,12 +35,12 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { private static final int DESCRIPTOR_TIMEOUT_MS = 5_000; private static final int WRITE_TIMEOUT_MS = 2_000; private static final int PRELUDE_TIMEOUT_MS = 2_000; - private static final int ACK_TIMEOUT_MS = 2_000; + private static final int ACK_TIMEOUT_MS = 3_500; private static final int HEARTBEAT_INTERVAL_MS = 5_000; private static final int BATTERY_REFRESH_INTERVAL_MS = 5 * 60_000; private static final int BATTERY_INPUT_QUIET_MS = 5_000; - private static final int WINDOW_SIZE = 1; - private static final int IMAGE_FRAGMENT_SIZE = 4000; + private static final int WINDOW_SIZE = 3; + private static final int IMAGE_FRAGMENT_SIZE = 1000; private static final int IMAGE_RETRY_DELAY_MS = 10_000; private static final boolean IMAGE_FRAGMENT_NO_ACK = false; private static final int WARMUP_FRAGMENT_TIMEOUT_MS = 3_000; @@ -103,10 +103,13 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { private String displayedFingerprint = ""; private byte[][] displayedTileBmps = emptyTileSet(); + private final Map imageUpdateStats = new HashMap<>(); private final Object desiredTilesLock = new Object(); private String desiredFingerprint = ""; private byte[][] desiredTileBmps = emptyTileSet(); + private boolean desiredForceTiledCommit; + private int desiredPaintMs; private final ArrayDeque pendingMessages = new ArrayDeque<>(); private final ArrayDeque inFlightMessages = new ArrayDeque<>(); @@ -178,6 +181,14 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { public void submitDashboardImage(List tileBmps, String fingerprint) { + submitDashboardImage(tileBmps, fingerprint, false, -1); + } + + public void submitDashboardImage(List tileBmps, String fingerprint, boolean forceTiledCommit) { + submitDashboardImage(tileBmps, fingerprint, forceTiledCommit, -1); + } + + public void submitDashboardImage(List tileBmps, String fingerprint, boolean forceTiledCommit, int paintMs) { if (tileBmps == null || tileBmps.size() != DASHBOARD_TILES.length) { throw new IllegalArgumentException("exactly 4 tile bitmaps are required"); } @@ -188,6 +199,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { desiredTileBmps[i] = bmp == null ? new byte[0] : Arrays.copyOf(bmp, bmp.length); } desiredFingerprint = fingerprint == null ? "" : fingerprint; + desiredForceTiledCommit = forceTiledCommit; + desiredPaintMs = paintMs; } interruptibleSleep.interrupt(); } @@ -198,6 +211,29 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { byte[] tile2Bmp, byte[] tile3Bmp, String fingerprint + ) { + submitDashboardImage4(tile0Bmp, tile1Bmp, tile2Bmp, tile3Bmp, fingerprint, false, -1); + } + + public void submitDashboardImage4( + byte[] tile0Bmp, + byte[] tile1Bmp, + byte[] tile2Bmp, + byte[] tile3Bmp, + String fingerprint, + boolean forceTiledCommit + ) { + submitDashboardImage4(tile0Bmp, tile1Bmp, tile2Bmp, tile3Bmp, fingerprint, forceTiledCommit, -1); + } + + public void submitDashboardImage4( + byte[] tile0Bmp, + byte[] tile1Bmp, + byte[] tile2Bmp, + byte[] tile3Bmp, + String fingerprint, + boolean forceTiledCommit, + int paintMs ) { synchronized (desiredTilesLock) { desiredTileBmps = emptyTileSet(); @@ -206,6 +242,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { desiredTileBmps[2] = tile2Bmp == null ? new byte[0] : Arrays.copyOf(tile2Bmp, tile2Bmp.length); desiredTileBmps[3] = tile3Bmp == null ? new byte[0] : Arrays.copyOf(tile3Bmp, tile3Bmp.length); desiredFingerprint = fingerprint == null ? "" : fingerprint; + desiredForceTiledCommit = forceTiledCommit; + desiredPaintMs = paintMs; } interruptibleSleep.interrupt(); } @@ -542,6 +580,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { } private boolean writeMessageLocked(OutboundMessage message) { + long writeStartedAtMs = SystemClock.elapsedRealtime(); if (!bleManager.writeFrames( rightAddress, BleProtocol.WRITE_CHAR_UUID, @@ -551,6 +590,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { )) { return false; } + message.writeStartedAtMs = writeStartedAtMs; message.sentAtMs = SystemClock.elapsedRealtime(); message.ackDeadlineAtMs = message.sentAtMs + message.ackTimeoutMs; if (message.magic != 0) { @@ -657,6 +697,10 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { return; } if (message.imageMessageNumber == 1) { + ImageUpdateStats stats = imageUpdateStats.get(message.imageUpdateId); + if (stats != null && stats.firstWriteStartedAtMs <= 0) { + stats.firstWriteStartedAtMs = message.writeStartedAtMs > 0 ? message.writeStartedAtMs : message.sentAtMs; + } logImageUpdateLandmarkLocked("first bluetooth message sent", message, message.sentAtMs); } if (message.imageMessageNumber == message.imageMessageCount) { @@ -668,7 +712,12 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { if (message.imageUpdateId <= 0 || message.imageMessageNumber != message.imageMessageCount) { return; } - logImageUpdateLandmarkLocked("last bluetooth message acked", message, SystemClock.elapsedRealtime()); + long ackedAtMs = SystemClock.elapsedRealtime(); + ImageUpdateStats stats = imageUpdateStats.remove(message.imageUpdateId); + if (stats != null && stats.firstWriteStartedAtMs > 0) { + emitFrameMetrics(stats.paintMs, (int) Math.max(0, ackedAtMs - stats.firstWriteStartedAtMs), stats.tileCount); + } + logImageUpdateLandmarkLocked("last bluetooth message acked", message, ackedAtMs); } private void logImageUpdateLandmarkLocked(String event, OutboundMessage message, long elapsedMs) { @@ -759,8 +808,13 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { String fingerprint = getDesiredFingerprint(); List changedTiles = new ArrayList<>(); byte[][] tileBmps; + boolean forceTiledCommit; + int paintMs; synchronized (desiredTilesLock) { tileBmps = desiredTileBmps; + forceTiledCommit = desiredForceTiledCommit; + desiredForceTiledCommit = false; + paintMs = desiredPaintMs; } for (int i = 0; i < DASHBOARD_TILES.length; i++) { BleProtocol.ImageTileOptions tile = DASHBOARD_TILES[i]; @@ -779,22 +833,27 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { return; } - boolean synchronizeCommits = changedTiles.size() > 1; + boolean synchronizedCommits = changedTiles.size() > 1; + if (forceTiledCommit) { + synchronizedCommits = false; + } boolean reserveLastByte = false; for (TileImagePlan plan : changedTiles) { plan.fragments = BleImageOptimizer.planImageFragments(plan.bmp, IMAGE_FRAGMENT_SIZE, reserveLastByte); } int updateId = nextImageUpdateId++; + imageUpdateStats.put(updateId, new ImageUpdateStats(paintMs, changedTiles.size())); int messageCount = 0; for (TileImagePlan plan : changedTiles) { messageCount += plan.fragments.size(); } int messageNumber = 1; - if (!synchronizeCommits) { - TileImagePlan plan = changedTiles.get(0); - for (BleProtocol.ImageFragment fragment : plan.fragments) { - enqueueImageFragmentLocked(plan, fragment, fingerprint, updateId, messageNumber++, messageCount, nextTransportSeq++, true, true); + if (!synchronizedCommits) { + for (TileImagePlan plan : changedTiles) { + for (BleProtocol.ImageFragment fragment : plan.fragments) { + enqueueImageFragmentLocked(plan, fragment, fingerprint, updateId, messageNumber++, messageCount, nextTransportSeq++, true, true); + } } } else { for (TileImagePlan plan : changedTiles) { @@ -808,7 +867,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { } logLine("queue image update#" + updateId + " fingerprint=" + fingerprint - + " changedTiles=" + changedTiles.size() + " messages=" + messageCount); + + " changedTiles=" + changedTiles.size() + " messages=" + messageCount + + " synchronizedCommits=" + synchronizedCommits); } private void enqueueImageFragmentLocked( @@ -958,6 +1018,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { displayedFingerprint = ""; displayedTileBmps = emptyTileSet(); } else if ("image".equals(message.kind)) { + imageUpdateStats.remove(message.imageUpdateId); clearMessagesOfKindLocked("image"); displayedFingerprint = ""; displayedTileBmps = emptyTileSet(); @@ -975,6 +1036,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { OutboundMessage message = pendingIterator.next(); if (kind.equals(message.kind)) { pendingIterator.remove(); + if ("image".equals(kind)) { + imageUpdateStats.remove(message.imageUpdateId); + } releaseMagicLocked(message, "cleared pending " + kind); } } @@ -983,6 +1047,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { OutboundMessage message = inFlightIterator.next(); if (kind.equals(message.kind)) { inFlightIterator.remove(); + if ("image".equals(kind)) { + imageUpdateStats.remove(message.imageUpdateId); + } releaseMagicLocked(message, "cleared inflight " + kind); } } @@ -1179,6 +1246,20 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { }); } + private void emitFrameMetrics(int paintMs, int transmitMs, int tileCount) { + final FaceclawBleCommunicatorListener current = listener; + if (current == null) { + return; + } + mainHandler.post(() -> { + try { + current.onFrameMetrics(paintMs, transmitMs, tileCount); + } catch (Throwable t) { + Log.w(TAG, "listener onFrameMetrics failed", t); + } + }); + } + private void maybeEmitEvenAppConflictLocked(String reason) { if (!"write failed".equals(reason)) { return; @@ -1379,6 +1460,17 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { } } + private static final class ImageUpdateStats { + final int paintMs; + final int tileCount; + long firstWriteStartedAtMs; + + ImageUpdateStats(int paintMs, int tileCount) { + this.paintMs = Math.max(0, paintMs); + this.tileCount = tileCount; + } + } + private static final class OutboundMessage { final String kind; final String label; @@ -1394,6 +1486,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { int imageMessageNumber; int imageMessageCount; long sentAtMs; + long writeStartedAtMs; long ackDeadlineAtMs; byte[] ackPayload = new byte[0]; diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicatorListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicatorListener.java index 4e4277f3..ffc5b045 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicatorListener.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicatorListener.java @@ -6,4 +6,5 @@ public interface FaceclawBleCommunicatorListener { void onRingEvent(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode); void onBatteryState(int headsetBattery, int headsetCharging); void onEvenAppConflict(String message); + void onFrameMetrics(int paintMs, int transmitMs, int tileCount); } diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.java index be089fdd..f3b69dff 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.java @@ -75,7 +75,7 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen continue; } CharSequence title = extras.getCharSequence(Notification.EXTRA_TITLE); - if (expectedTitle.contentEquals(title)) { + if (title != null && expectedTitle.contentEquals(title)) { return true; } } diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSystemStatusIconProvider.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSystemStatusIconProvider.java new file mode 100644 index 00000000..34209e16 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSystemStatusIconProvider.java @@ -0,0 +1,193 @@ +package com.faceclaw.app; + +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.Network; +import android.net.NetworkCapabilities; +import android.net.wifi.WifiInfo; +import android.net.wifi.WifiManager; +import android.os.Build; +import android.telephony.SignalStrength; +import android.telephony.TelephonyManager; +import android.util.Log; + +import java.io.ByteArrayOutputStream; +import java.lang.reflect.Method; + +public final class FaceclawSystemStatusIconProvider { + private static final String TAG = "FaceclawStatusIcons"; + private static final int ICON_SIZE = 24; + + private FaceclawSystemStatusIconProvider() { + } + + public static byte[] getSystemStatusIconGrays(Context context, int iconSize) { + if (context == null) { + return new byte[0]; + } + Context appContext = context.getApplicationContext(); + int size = Math.max(1, Math.min(96, iconSize)); + ByteArrayOutputStream out = new ByteArrayOutputStream(size * size * 3); + + int wifiLevel = getWifiLevel(appContext); + if (wifiLevel >= 0) { + append(out, scaleIcon(drawWifiIcon(wifiLevel), size)); + } + + int cellLevel = getCellLevel(appContext); + if (cellLevel >= 0) { + append(out, scaleIcon(drawCellIcon(cellLevel), size)); + } + + if (isHotspotEnabled(appContext)) { + append(out, scaleIcon(drawHotspotIcon(), size)); + } + + return out.toByteArray(); + } + + private static int getWifiLevel(Context context) { + try { + ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + Network activeNetwork = connectivityManager == null ? null : connectivityManager.getActiveNetwork(); + NetworkCapabilities capabilities = activeNetwork == null ? null : connectivityManager.getNetworkCapabilities(activeNetwork); + if (capabilities == null || !capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { + return -1; + } + WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); + WifiInfo info = wifiManager == null ? null : wifiManager.getConnectionInfo(); + if (info == null || !isValidRssi(info.getRssi())) { + return -1; + } + return Math.max(0, Math.min(4, WifiManager.calculateSignalLevel(info.getRssi(), 5))); + } catch (Throwable t) { + Log.w(TAG, "failed to read Wi-Fi status", t); + return -1; + } + } + + private static int getCellLevel(Context context) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) { + return -1; + } + try { + TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); + SignalStrength signalStrength = telephonyManager == null ? null : telephonyManager.getSignalStrength(); + if (signalStrength == null) { + return -1; + } + return Math.max(0, Math.min(4, signalStrength.getLevel())); + } catch (SecurityException e) { + // Most Android versions require READ_PHONE_STATE for this. We skip it + // rather than prompting for a broad phone permission just for an icon. + return -1; + } catch (Throwable t) { + Log.w(TAG, "failed to read cellular status", t); + return -1; + } + } + + private static boolean isHotspotEnabled(Context context) { + try { + WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); + if (wifiManager == null) { + return false; + } + Method method = wifiManager.getClass().getDeclaredMethod("getWifiApState"); + method.setAccessible(true); + Object result = method.invoke(wifiManager); + int state = result instanceof Integer ? (Integer) result : -1; + return state == 13 || state == 12; // WIFI_AP_STATE_ENABLED / ENABLING + } catch (Throwable ignored) { + return false; + } + } + + private static boolean isValidRssi(int rssi) { + return rssi > -127 && rssi < 0; + } + + private static byte[] drawWifiIcon(int level) { + byte[] icon = new byte[ICON_SIZE * ICON_SIZE]; + int cx = 12; + int cy = 18; + fillRect(icon, cx - 1, cy - 1, 3, 3, 230); + int[] radii = new int[] {5, 9, 13, 17}; + for (int index = 0; index < radii.length; index++) { + if (level <= index) { + continue; + } + drawWifiArc(icon, cx, cy, radii[index], 210); + } + return icon; + } + + private static void drawWifiArc(byte[] icon, int cx, int cy, int radius, int value) { + int inner = (radius - 1) * (radius - 1); + int outer = (radius + 1) * (radius + 1); + for (int y = 0; y < ICON_SIZE; y++) { + for (int x = 0; x < ICON_SIZE; x++) { + int dx = x - cx; + int dy = y - cy; + int dist = dx * dx + dy * dy; + if (dy <= 0 && dist >= inner && dist <= outer && Math.abs(dx) <= radius) { + setPixel(icon, x, y, value); + } + } + } + } + + private static byte[] drawCellIcon(int level) { + byte[] icon = new byte[ICON_SIZE * ICON_SIZE]; + for (int bar = 0; bar < 4; bar++) { + int x = 5 + bar * 4; + int height = 5 + bar * 4; + int value = level > bar ? 220 : 55; + fillRect(icon, x, 20 - height, 3, height, value); + } + return icon; + } + + private static byte[] drawHotspotIcon() { + byte[] icon = new byte[ICON_SIZE * ICON_SIZE]; + fillRect(icon, 11, 17, 3, 3, 230); + drawWifiArc(icon, 12, 19, 7, 210); + drawWifiArc(icon, 12, 19, 12, 210); + fillRect(icon, 6, 5, 12, 2, 170); + return icon; + } + + private static byte[] scaleIcon(byte[] source, int size) { + if (size == ICON_SIZE) { + return source; + } + byte[] scaled = new byte[size * size]; + for (int y = 0; y < size; y++) { + int sy = Math.min(ICON_SIZE - 1, (y * ICON_SIZE) / size); + for (int x = 0; x < size; x++) { + int sx = Math.min(ICON_SIZE - 1, (x * ICON_SIZE) / size); + scaled[y * size + x] = source[sy * ICON_SIZE + sx]; + } + } + return scaled; + } + + private static void fillRect(byte[] icon, int x, int y, int width, int height, int value) { + for (int row = y; row < y + height; row++) { + for (int col = x; col < x + width; col++) { + setPixel(icon, col, row, value); + } + } + } + + private static void setPixel(byte[] icon, int x, int y, int value) { + if (x < 0 || y < 0 || x >= ICON_SIZE || y >= ICON_SIZE) { + return; + } + icon[y * ICON_SIZE + x] = (byte) Math.max(0, Math.min(255, value)); + } + + private static void append(ByteArrayOutputStream out, byte[] bytes) { + out.write(bytes, 0, bytes.length); + } +} diff --git a/README b/README index fb225375..8a5c7fdf 100644 --- a/README +++ b/README @@ -33,3 +33,18 @@ You will need a JDK environment (v21), with the JAVA_HOME environment variable pointed at it. You will also need a reasonably up to date npm and nodejs installed. + +## Features + + * Extreme slowness and accelerated battery drain, due to lack of compression + and additional factors in the G2 BLE image sending protocol + * Dual-language NativeScript architecture with Java for the multithreaded + Android API and bluetooth stack bits, Typescript for the bits you want to + hack on + * Dashboard with four configurable tiles, Nightscout integration, media player + integration, Android notification icons, and battery levels for the phone + and the G2 headset. (No battery indicator for the ring, currently.) + * Connection management with auto-reconnect, autodetection of conflict with + the official Even Realities app, and some amount of experience/iteration + on the issues that come with keeping it runnign all day + * On-phone screen mirroring and simulated ring input diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index 0e53f37a..fcf9359a 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -1,15 +1,16 @@ -import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image"; import { EventSourceType, EventSourceTypeName, OsEventTypeList, OsEventTypeName, } from "g2-kit/lite"; import { ImageSource } from "@nativescript/core"; import { loadDeviceAddresses } from "./device-addresses"; import { ensureBlePermissions } from "./android-permissions"; -import { FaceclawCommunicatorBridge, type RawInputEvent } from "../native/faceclaw-communicator"; +import { FaceclawCommunicatorBridge, type FrameMetrics, type RawInputEvent } from "../native/faceclaw-communicator"; import { startForegroundNotification, stopForegroundNotification, updateForegroundNotification } from "../native/foreground-service"; import { mediaControllerBridge } from "../native/media-controller"; import { nightscoutBridge } from "../native/nightscout"; import { openEvenAppSettings, readEvenAppNotificationState } from "../native/even-app-conflict"; import { grayImageToPreviewSource } from "../native/gray-image-preview"; import { + applyDashboardScreenTimeout, + consumeDashboardTiledWakePaint, drawDashboard, getDashboardNightscoutSettings, getDashboardSystemCardName, @@ -43,6 +44,7 @@ type LogLevel = "debug"|"info"|"warn"|"error"; const CONTAINER_NAME = "dashboard"; const DASHBOARD_INTERVAL_MS = 60_000; +const SCREEN_TIMEOUT_CHECK_MS = 1_000; 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."; @@ -101,18 +103,18 @@ class DashboardController { private activeTextSettingEditorKind: TextSettingEditorKind | null = null; private evenNotificationActive = false; private evenAppConflictMessage = ""; - private displayPreview: ImageSource | null = grayImageToPreviewSource( - new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0), - ); + private displayPreview: ImageSource | null = null; private readonly listeners = new Set(); private communicator: FaceclawCommunicatorBridge | null = null; private dashboardTimer: ReturnType | null = null; + private screenTimeoutTimer: ReturnType | null = null; private offState: (() => void) | null = null; private offLog: (() => void) | null = null; private offRing: (() => void) | null = null; private offBattery: (() => void) | null = null; private offEvenAppConflict: (() => void) | null = null; + private offFrameMetrics: (() => void) | null = null; private offMedia: (() => void) | null = null; private offNightscout: (() => void) | null = null; private lastInput = "waiting..."; @@ -297,6 +299,11 @@ class DashboardController { this.appendLog(message); this.emit(); }); + this.offFrameMetrics = communicator.onFrameMetrics((metrics) => { + if (this.phase === "connected") { + this.setStatus(`Connected. Last frame: ${this.formatFrameMetrics(metrics)}.`); + } + }); this.offMedia = mediaControllerBridge.onStateChange(() => { if (this.phase === "connected" && this.communicator) { void this.requestRender("interval").catch((error) => { @@ -325,6 +332,14 @@ class DashboardController { this.appendLog(`dashboard update failed: ${message}`); }); }, DASHBOARD_INTERVAL_MS); + this.screenTimeoutTimer = setInterval(() => { + if (this.phase !== "connected" || !this.communicator) return; + if (!applyDashboardScreenTimeout()) return; + void this.requestRender("interval").catch((error) => { + const message = this.formatError(error); + this.appendLog(`screen timeout render failed: ${message}`); + }); + }, SCREEN_TIMEOUT_CHECK_MS); } catch (error) { const message = this.formatError(error); this.offState?.(); @@ -337,6 +352,8 @@ class DashboardController { this.offBattery = null; this.offEvenAppConflict?.(); this.offEvenAppConflict = null; + this.offFrameMetrics?.(); + this.offFrameMetrics = null; this.offMedia?.(); this.offMedia = null; this.offNightscout?.(); @@ -372,6 +389,8 @@ class DashboardController { this.offBattery = null; this.offEvenAppConflict?.(); this.offEvenAppConflict = null; + this.offFrameMetrics?.(); + this.offFrameMetrics = null; this.offMedia?.(); this.offMedia = null; this.offNightscout?.(); @@ -507,17 +526,18 @@ class DashboardController { } private async renderDashboard(reason: "initial" | "interval"): Promise { - const now = new Date(); + const paintStartedAtMs = Date.now(); const image = drawDashboard(); - this.setDisplayPreview(grayImageToPreviewSource(image)); - if (!this.communicator) return; + const paintMs = Date.now() - paintStartedAtMs; + const forceTiledCommit = consumeDashboardTiledWakePaint(); const fingerprint = image.fingerprint(); const tiles = image.toEvenHubTiles().map((tile) => tile.bmp); - await this.communicator.submitDashboardImage(tiles, fingerprint); + if (this.communicator) { + await this.communicator.submitDashboardImage(tiles, fingerprint, forceTiledCommit, paintMs); + } + this.setDisplayPreview(grayImageToPreviewSource(image)); - const renderedAt = formatForStatus(now); if (this.phase === "connected") { - this.setStatus(`Connected. Dashboard queued at ${renderedAt}.`); updateForegroundNotification(`Connected`); } this.appendLog(`${reason === "initial" ? "initial" : "scheduled"} dashboard image queued`); @@ -559,9 +579,14 @@ class DashboardController { } private clearDashboardTimer(): void { - if (!this.dashboardTimer) return; - clearInterval(this.dashboardTimer); - this.dashboardTimer = null; + if (this.dashboardTimer) { + clearInterval(this.dashboardTimer); + this.dashboardTimer = null; + } + if (this.screenTimeoutTimer) { + clearInterval(this.screenTimeoutTimer); + this.screenTimeoutTimer = null; + } } private setPhase(phase: ConnectionPhase): void { @@ -576,6 +601,10 @@ class DashboardController { this.emit(); } + private formatFrameMetrics(metrics: FrameMetrics): string { + return `paint=${Math.round(metrics.paintMs)}ms, transmit=${Math.round(metrics.transmitMs)}ms, tiles=${Math.round(metrics.tileCount)}`; + } + private appendLog(line: string): void { const stamped = `[${formatTimestamp(new Date())}] ${line}`; this.log = this.log ? `${this.log}\n${stamped}` : stamped; diff --git a/app/graphics/battery.ts b/app/graphics/battery.ts new file mode 100644 index 00000000..330d6b34 --- /dev/null +++ b/app/graphics/battery.ts @@ -0,0 +1,60 @@ +import { GrayImage, imageFromAsciiArt } from "./image"; + +const EMPTY_BATTERY_ICON = imageFromAsciiArt( + [ + " ################## ", + " #................# ", + " #................###", + " #................###", + " #................###", + " #................###", + " #................###", + " #................###", + " #................# ", + " ################## ", + ], + 120, +); + +const BATTERY_BOLT_ICON = imageFromAsciiArt( + [ + "....#..", + "...##..", + "..##...", + ".######", + "...##..", + "..##...", + "..#....", + ], + 255, +); + +export const BATTERY_ICON_WIDTH = EMPTY_BATTERY_ICON.width; + +const BATTERY_FILL_X = 3; +const BATTERY_FILL_Y = 2; +const BATTERY_FILL_WIDTH = 14; +const BATTERY_FILL_HEIGHT = 6; + +export function drawBattery(percentCharge: number, isCharging: boolean): GrayImage { + const icon = new GrayImage(EMPTY_BATTERY_ICON.width, EMPTY_BATTERY_ICON.height, 0); + icon.bitBlt(EMPTY_BATTERY_ICON, 0, 0); + const clamped = Math.max(0, Math.min(100, percentCharge)); + const fillWidth = Math.round((BATTERY_FILL_WIDTH * clamped) / 100); + icon.fillRect(BATTERY_FILL_X, BATTERY_FILL_Y, fillWidth, BATTERY_FILL_HEIGHT, 190); + if (isCharging) { + overlayImage(icon, BATTERY_BOLT_ICON, 7, 1); + } + return icon; +} + +function overlayImage(target: GrayImage, source: GrayImage, dx: number, dy: number): void { + for (let y = 0; y < source.height; y++) { + for (let x = 0; x < source.width; x++) { + const value = source.pixels[y * source.width + x] ?? 0; + if (value > 0) { + target.setPixel(dx + x, dy + y, value); + } + } + } +} diff --git a/app/native/faceclaw-communicator.ts b/app/native/faceclaw-communicator.ts index 0bfe2de6..1403d7fa 100644 --- a/app/native/faceclaw-communicator.ts +++ b/app/native/faceclaw-communicator.ts @@ -19,6 +19,12 @@ export type HeadsetBatteryState = { chargingStatus: number; }; +export type FrameMetrics = { + paintMs: number; + transmitMs: number; + tileCount: number; +}; + export type RawInputEvent = | { kind: "list-click"; @@ -51,6 +57,11 @@ function toJavaByteArray(bytes: Uint8Array): number[] { return out; } +function nonNegativeNumber(value: number): number { + const numeric = Number(value); + return Number.isFinite(numeric) ? Math.max(0, numeric) : 0; +} + export class FaceclawCommunicatorBridge { private readonly communicator: any; private readonly listenerProxy: any; @@ -59,6 +70,7 @@ export class FaceclawCommunicatorBridge { private readonly ringListeners = new Set<(event: RawInputEvent) => void>(); private readonly batteryListeners = new Set<(state: HeadsetBatteryState) => void>(); private readonly evenAppConflictListeners = new Set<(message: string) => void>(); + private readonly frameMetricsListeners = new Set<(metrics: FrameMetrics) => void>(); constructor(addresses: { right: string; left: string; ring?: string }) { const context = Utils.android.getApplicationContext(); @@ -117,6 +129,16 @@ export class FaceclawCommunicatorBridge { listener(String(message)); } }, + onFrameMetrics: (paintMs: number, transmitMs: number, tileCount: number) => { + const metrics = { + paintMs: nonNegativeNumber(paintMs), + transmitMs: nonNegativeNumber(transmitMs), + tileCount: nonNegativeNumber(tileCount), + }; + for (const listener of this.frameMetricsListeners) { + listener(metrics); + } + }, }); this.communicator.setListener(this.listenerProxy); } @@ -146,11 +168,16 @@ export class FaceclawCommunicatorBridge { return () => this.evenAppConflictListeners.delete(listener); } + onFrameMetrics(listener: (metrics: FrameMetrics) => void): () => void { + this.frameMetricsListeners.add(listener); + return () => this.frameMetricsListeners.delete(listener); + } + async start(): Promise { this.communicator.start(); } - async submitDashboardImage(tileBmps: Uint8Array[], fingerprint: string): Promise { + async submitDashboardImage(tileBmps: Uint8Array[], fingerprint: string, forceTiledCommit = false, paintMs = -1): Promise { if (tileBmps.length !== 4) { throw new Error(`expected 4 dashboard tiles, got ${tileBmps.length}`); } @@ -160,6 +187,8 @@ export class FaceclawCommunicatorBridge { toJavaByteArray(tileBmps[2]!), toJavaByteArray(tileBmps[3]!), fingerprint, + forceTiledCommit, + Math.round(nonNegativeNumber(paintMs)), ); } diff --git a/app/native/gray-image-preview.ts b/app/native/gray-image-preview.ts index c3f85a09..391dc621 100644 --- a/app/native/gray-image-preview.ts +++ b/app/native/gray-image-preview.ts @@ -1,8 +1,9 @@ -import { ImageSource, Screen, Utils } from "@nativescript/core"; +import { ImageSource } from "@nativescript/core"; import { type GrayImage } from "../graphics/image"; const PREVIEW_BRIGHTEN_GAMMA = 0.7; +const PREVIEW_COLOR_LUT = buildPreviewColorLookup(); export function grayImageToPreviewSource(image: GrayImage): ImageSource | null { if (!global.isAndroid) { @@ -11,8 +12,7 @@ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null { const colors = Array.create("int", image.width * image.height) as number[]; for (let i = 0; i < image.pixels.length; i++) { - const gray = brightenForPreview(image.pixels[i] ?? 0); - colors[i] = (255 << 24) | (gray << 16) | (gray << 8) | gray; + colors[i] = PREVIEW_COLOR_LUT[image.pixels[i] ?? 0]!; } const bitmap = android.graphics.Bitmap.createBitmap( @@ -21,16 +21,15 @@ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null { image.height, android.graphics.Bitmap.Config.ARGB_8888, ); - const targetWidth = Math.max(1, Math.round(Utils.layout.toDevicePixels(Screen.mainScreen.widthDIPs))); - const targetHeight = Math.max(1, Math.round((targetWidth * image.height) / image.width)); - const scaledBitmap = - bitmap.getWidth() === targetWidth && bitmap.getHeight() === targetHeight - ? bitmap - : android.graphics.Bitmap.createScaledBitmap(bitmap, targetWidth, targetHeight, false); - return new ImageSource(scaledBitmap); + return new ImageSource(bitmap); } -function brightenForPreview(gray: number): number { - const normalized = Math.max(0, Math.min(255, gray)) / 255; - return Math.max(0, Math.min(255, Math.round(255 * Math.pow(normalized, PREVIEW_BRIGHTEN_GAMMA)))); +function buildPreviewColorLookup(): number[] { + const colors: number[] = []; + for (let gray = 0; gray <= 255; gray++) { + const normalized = gray / 255; + const preview = Math.max(0, Math.min(255, Math.round(255 * Math.pow(normalized, PREVIEW_BRIGHTEN_GAMMA)))); + colors[gray] = (255 << 24) | (preview << 16) | (preview << 8) | preview; + } + return colors; } diff --git a/app/native/system-status-icons.ts b/app/native/system-status-icons.ts new file mode 100644 index 00000000..50150070 --- /dev/null +++ b/app/native/system-status-icons.ts @@ -0,0 +1,54 @@ +import { Utils } from "@nativescript/core"; + +import { GrayImage } from "../graphics/image"; + +declare const com: any; + +const ICON_SIZE = 24; +const ICON_CACHE_MS = 5_000; + +let cachedIcons: GrayImage[] = []; +let cachedAtMs = 0; + +export function readSystemStatusIcons(): GrayImage[] { + if (!global.isAndroid) return []; + + const now = Date.now(); + if (now - cachedAtMs < ICON_CACHE_MS) { + return cachedIcons.map(cloneGrayImage); + } + + const context = Utils.android.getApplicationContext(); + if (!context) return []; + + const bytes = toUint8Array( + com.faceclaw.app.FaceclawSystemStatusIconProvider.getSystemStatusIconGrays(context, ICON_SIZE), + ); + const iconByteLength = ICON_SIZE * ICON_SIZE; + const iconCount = Math.floor(bytes.length / iconByteLength); + const icons: GrayImage[] = []; + for (let index = 0; index < iconCount; index++) { + const icon = new GrayImage(ICON_SIZE, ICON_SIZE, 0); + icon.pixels.set(bytes.subarray(index * iconByteLength, (index + 1) * iconByteLength)); + icons.push(icon); + } + + cachedIcons = icons; + cachedAtMs = now; + return icons.map(cloneGrayImage); +} + +function toUint8Array(bytes: ArrayLike | null | undefined): Uint8Array { + if (!bytes) return new Uint8Array(0); + const out = new Uint8Array(bytes.length); + for (let i = 0; i < bytes.length; i++) { + out[i] = bytes[i]! & 0xff; + } + return out; +} + +function cloneGrayImage(image: GrayImage): GrayImage { + const clone = new GrayImage(image.width, image.height, 0); + clone.pixels.set(image.pixels); + return clone; +} diff --git a/app/ui/dashboard-settings.ts b/app/ui/dashboard-settings.ts index b7ac7025..e462f8a1 100644 --- a/app/ui/dashboard-settings.ts +++ b/app/ui/dashboard-settings.ts @@ -1,6 +1,6 @@ import { ApplicationSettings } from "@nativescript/core"; -export type DashboardSlotId = "top-right" | "bottom-left" | "bottom-right"; +export type DashboardSlotId = "bottom-left" | "bottom-right"; export type DashboardPluginId = "blank" | "input-debug-log" | "music-controller" | "nightscout"; export type DashboardSlotConfig = Record; @@ -12,20 +12,27 @@ export type SystemCardSettings = { showFaceclawLogo: boolean; showBatteryIndicators: boolean; showAndroidNotifications: boolean; + showSignalStrength: boolean; }; export type SystemCardSettingKey = keyof SystemCardSettings; +export type ScreenTimeoutSetting = "15s" | "30s" | "1m" | "never"; +export type WakeModeSetting = "tiled" | "synchronized"; export const DEFAULT_SYSTEM_CARD_NAME = "Faceclaw"; +export const DEFAULT_SCREEN_TIMEOUT: ScreenTimeoutSetting = "30s"; +export const DEFAULT_WAKE_MODE: WakeModeSetting = "tiled"; const SYSTEM_CARD_NAME_KEY = "dashboard.systemCardName"; +const SCREEN_TIMEOUT_KEY = "display.screenTimeout"; +const WAKE_MODE_KEY = "display.wakeMode"; const SYSTEM_CARD_SETTING_KEYS: Record = { showFaceclawLogo: "dashboard.systemCard.showFaceclawLogo", showBatteryIndicators: "dashboard.systemCard.showBatteryIndicators", showAndroidNotifications: "dashboard.systemCard.showAndroidNotifications", + showSignalStrength: "dashboard.systemCard.showSignalStrength", }; const NIGHTSCOUT_SITE_URL_KEY = "integrations.nightscout.siteUrl"; const NIGHTSCOUT_API_TOKEN_KEY = "integrations.nightscout.apiToken"; const SLOT_KEYS: Record = { - "top-right": "dashboard.slot.topRight", "bottom-left": "dashboard.slot.bottomLeft", "bottom-right": "dashboard.slot.bottomRight", }; @@ -38,8 +45,7 @@ const KNOWN_PLUGIN_IDS = new Set([ ]); const DEFAULT_SLOT_CONFIG: DashboardSlotConfig = { - "top-right": "input-debug-log", - "bottom-left": "blank", + "bottom-left": "input-debug-log", "bottom-right": "blank", }; @@ -47,10 +53,11 @@ const DEFAULT_SYSTEM_CARD_SETTINGS: SystemCardSettings = { showFaceclawLogo: true, showBatteryIndicators: true, showAndroidNotifications: true, + showSignalStrength: true, }; export function getDashboardSlotIds(): DashboardSlotId[] { - return ["top-right", "bottom-left", "bottom-right"]; + return ["bottom-left", "bottom-right"]; } export function getDefaultDashboardSlotConfig(): DashboardSlotConfig { @@ -100,6 +107,10 @@ export function loadSystemCardSettings(): SystemCardSettings { SYSTEM_CARD_SETTING_KEYS.showAndroidNotifications, DEFAULT_SYSTEM_CARD_SETTINGS.showAndroidNotifications, ), + showSignalStrength: ApplicationSettings.getBoolean( + SYSTEM_CARD_SETTING_KEYS.showSignalStrength, + DEFAULT_SYSTEM_CARD_SETTINGS.showSignalStrength, + ), }; } @@ -107,6 +118,65 @@ export function saveSystemCardSetting(key: SystemCardSettingKey, value: boolean) ApplicationSettings.setBoolean(SYSTEM_CARD_SETTING_KEYS[key], value); } +export function loadScreenTimeoutSetting(): ScreenTimeoutSetting { + return normalizeScreenTimeoutSetting(ApplicationSettings.getString(SCREEN_TIMEOUT_KEY, DEFAULT_SCREEN_TIMEOUT)); +} + +export function saveScreenTimeoutSetting(value: ScreenTimeoutSetting): ScreenTimeoutSetting { + const normalized = normalizeScreenTimeoutSetting(value); + ApplicationSettings.setString(SCREEN_TIMEOUT_KEY, normalized); + return normalized; +} + +export function loadWakeModeSetting(): WakeModeSetting { + return normalizeWakeModeSetting(ApplicationSettings.getString(WAKE_MODE_KEY, DEFAULT_WAKE_MODE)); +} + +export function saveWakeModeSetting(value: WakeModeSetting): WakeModeSetting { + const normalized = normalizeWakeModeSetting(value); + ApplicationSettings.setString(WAKE_MODE_KEY, normalized); + return normalized; +} + +export function nextScreenTimeoutSetting(value: ScreenTimeoutSetting): ScreenTimeoutSetting { + switch (value) { + case "15s": + return "30s"; + case "30s": + return "1m"; + case "1m": + return "never"; + case "never": + default: + return "15s"; + } +} + +export function screenTimeoutMs(value: ScreenTimeoutSetting): number | null { + switch (value) { + case "15s": + return 15_000; + case "30s": + return 30_000; + case "1m": + return 60_000; + case "never": + return null; + } +} + +export function screenTimeoutLabel(value: ScreenTimeoutSetting): string { + return value === "never" ? "Never" : value; +} + +export function nextWakeModeSetting(value: WakeModeSetting): WakeModeSetting { + return value === "tiled" ? "synchronized" : "tiled"; +} + +export function wakeModeLabel(value: WakeModeSetting): string { + return value === "tiled" ? "Tiled" : "Synchronized"; +} + export function loadNightscoutSettings(): NightscoutSettings { return { siteUrl: normalizeNightscoutSiteUrl(ApplicationSettings.getString(NIGHTSCOUT_SITE_URL_KEY, "")), @@ -152,3 +222,25 @@ function normalizeNightscoutSiteUrl(siteUrl: string | null | undefined): string function normalizeNightscoutApiToken(apiToken: string | null | undefined): string { return (apiToken ?? "").replace(/[\x00-\x1f]+/g, "").trim(); } + +function normalizeScreenTimeoutSetting(value: string | null | undefined): ScreenTimeoutSetting { + switch (value) { + case "15s": + case "30s": + case "1m": + case "never": + return value; + default: + return DEFAULT_SCREEN_TIMEOUT; + } +} + +function normalizeWakeModeSetting(value: string | null | undefined): WakeModeSetting { + switch (value) { + case "tiled": + case "synchronized": + return value; + default: + return DEFAULT_WAKE_MODE; + } +} diff --git a/app/ui/dashboard.ts b/app/ui/dashboard.ts index 112ed0d8..d01b0dbb 100644 --- a/app/ui/dashboard.ts +++ b/app/ui/dashboard.ts @@ -1,11 +1,13 @@ -import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage, imageFromAsciiArt } from "../graphics/image"; +import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image"; import { loadEmbeddedTerminus12, loadEmbeddedTerminus16 } from "../graphics/bdffont"; +import { BATTERY_ICON_WIDTH, drawBattery } from "../graphics/battery"; import { loadPngAsGrayImage } from "../graphics/imagefile"; import { type RawInputEvent } from "../native/faceclaw-communicator"; import { mediaControllerBridge } from "../native/media-controller"; import { nightscoutBridge } from "../native/nightscout"; import { readActiveNotificationIcons } from "../native/notification-icons"; import { readPhoneBatteryState } from "../native/phone-battery"; +import { readSystemStatusIcons } from "../native/system-status-icons"; import { OsEventTypeList, EventSourceType } from "g2-kit/lite"; import { getDashboardPlugin, @@ -20,19 +22,30 @@ import { isNightscoutSettingsConfigured, loadDashboardSlotConfig, loadNightscoutSettings, + loadScreenTimeoutSetting, loadSystemCardSettings, loadSystemCardName, + loadWakeModeSetting, + nextScreenTimeoutSetting, + nextWakeModeSetting, saveDashboardSlotPlugin, saveNightscoutApiToken, saveNightscoutSiteUrl, + saveScreenTimeoutSetting, + saveWakeModeSetting, + screenTimeoutLabel, + screenTimeoutMs, saveSystemCardSetting, saveSystemCardName, + wakeModeLabel, type DashboardSlotId, type NightscoutSettings, + type ScreenTimeoutSetting, type SystemCardSettingKey, + type WakeModeSetting, } from "./dashboard-settings"; import { Layer, LayerActions, LayerStack, type DashboardInputEvent, type LayerContext } from "./layers"; -import { MenuLayer } from "./menu"; +import { drawRightValueMenuItem, drawToggleMenuItem, MenuLayer } from "./menu"; type DashboardCardId = "system" | DashboardSlotId; export type DashboardBatteryLevels = { @@ -44,10 +57,13 @@ export type DashboardBatteryLevels = { let dashboardState = { logLines: [] as string[], screenOn: true, - selectedCard: "system" as DashboardCardId, + lastInputAtMs: Date.now(), slotConfig: loadDashboardSlotConfig(), systemCardName: loadSystemCardName(), systemCardSettings: loadSystemCardSettings(), + screenTimeout: loadScreenTimeoutSetting(), + wakeMode: loadWakeModeSetting(), + tiledWakePaintPending: false, nightscoutSettings: loadNightscoutSettings(), battery: { headset: null, @@ -71,37 +87,8 @@ const NOTIFICATION_ICON_SIZE = 24; const SYSTEM_CARD_ITEM_HEIGHT = 38; const SYSTEM_CARD_ITEM_GAP = 2; const BATTERY_ITEM_Y_OFFSET = 4; -const EMPTY_BATTERY_ICON = imageFromAsciiArt( - [ - " ################## ", - " #................# ", - " #................###", - " #................###", - " #................###", - " #................###", - " #................###", - " #................###", - " #................# ", - " ################## ", - ], - 120, -); -const BATTERY_BOLT_ICON = imageFromAsciiArt( - [ - "....#..", - "...##..", - "..##...", - ".######", - "...##..", - "..##...", - "..#....", - ], - 255, -); -const BATTERY_FILL_X = 3; -const BATTERY_FILL_Y = 2; -const BATTERY_FILL_WIDTH = 14; -const BATTERY_FILL_HEIGHT = 6; +const TOP_LEFT_MENU_LAYOUT = { x: 8, y: 8, width: 272, height: 128 }; +const TOP_RIGHT_MENU_LAYOUT = { x: 296, y: 8, width: 272, height: 128 }; function rawInputEventToInputEvent(event: RawInputEvent): DashboardInputEvent { if (event.kind === "sys-event") { @@ -144,6 +131,7 @@ function eventSourceToString(eventSource: number): "ring" | "left-arm" | "right- export async function receiveInput(event: RawInputEvent): Promise { const inputEvent = rawInputEventToInputEvent(event); + dashboardState.lastInputAtMs = Date.now(); dashboardState.logLines.push(eventToString(inputEvent)); await dashboardLayers.handleInput(inputEvent); } @@ -176,6 +164,12 @@ export function drawDashboard(): GrayImage { return dashboardLayers.paint(); } +export function consumeDashboardTiledWakePaint(): boolean { + const value = dashboardState.tiledWakePaintPending; + dashboardState.tiledWakePaintPending = false; + return value; +} + export function setDashboardSystemCardName(name: string): string { const savedName = saveSystemCardName(name); dashboardState.systemCardName = savedName; @@ -216,6 +210,15 @@ export function setDashboardSystemCardSetting(key: SystemCardSettingKey, value: saveSystemCardSetting(key, value); } +export function applyDashboardScreenTimeout(nowMs = Date.now()): boolean { + const timeoutMs = screenTimeoutMs(dashboardState.screenTimeout); + if (timeoutMs === null || !dashboardState.screenOn) return false; + if (nowMs - dashboardState.lastInputAtMs < timeoutMs) return false; + dashboardState.screenOn = false; + dashboardLayers.clearToBase(); + return true; +} + export function setDashboardBatteryLevels(levels: Partial): void { dashboardState.battery = { ...dashboardState.battery, @@ -264,29 +267,6 @@ function drawBatteryFlowItem(image: GrayImage, x: number, y: number, item: Extra image.bitBlt(battery, x + ((itemWidth - battery.width) / 2) | 0, y + 16 + BATTERY_ITEM_Y_OFFSET); } -function drawBattery(percentCharge: number, isCharging: boolean): GrayImage { - const icon = new GrayImage(EMPTY_BATTERY_ICON.width, EMPTY_BATTERY_ICON.height, 0); - icon.bitBlt(EMPTY_BATTERY_ICON, 0, 0); - const clamped = Math.max(0, Math.min(100, percentCharge)); - const fillWidth = Math.round((BATTERY_FILL_WIDTH * clamped) / 100); - icon.fillRect(BATTERY_FILL_X, BATTERY_FILL_Y, fillWidth, BATTERY_FILL_HEIGHT, 190); - if (isCharging) { - overlayImage(icon, BATTERY_BOLT_ICON, 7, 1); - } - return icon; -} - -function overlayImage(target: GrayImage, source: GrayImage, dx: number, dy: number): void { - for (let y = 0; y < source.height; y++) { - for (let x = 0; x < source.width; x++) { - const value = source.pixels[y * source.width + x] ?? 0; - if (value > 0) { - target.setPixel(dx + x, dy + y, value); - } - } - } -} - const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; @@ -315,13 +295,19 @@ function drawSystemCardFlowItems(image: GrayImage, bounds: DashboardPluginCardBo const top = bounds.y + 84; const right = bounds.x + bounds.width - 10; const bottom = bounds.y + bounds.height - 6; - const items = collectBatteryItems(); + const items: SystemCardFlowItem[] = []; if (dashboardState.systemCardSettings.showAndroidNotifications) { const maxNotificationIcons = Math.max(0, ((right - left) / Math.max(1, NOTIFICATION_ICON_SIZE + SYSTEM_CARD_ITEM_GAP)) | 0) * 2; for (const icon of readActiveNotificationIcons(maxNotificationIcons)) { items.push({ type: "notification", icon }); } } + items.push(...collectBatteryItems()); + if (dashboardState.systemCardSettings.showSignalStrength) { + for (const icon of readSystemStatusIcons()) { + items.push({ type: "notification", icon }); + } + } let itemX = left; let itemY = top; @@ -351,7 +337,7 @@ function systemCardFlowItemWidth(item: SystemCardFlowItem): number { if (item.type === "notification") { return NOTIFICATION_ICON_SIZE; } - return Math.max(EMPTY_BATTERY_ICON.width, dashboardFont.measureText(item.label)); + return Math.max(BATTERY_ICON_WIDTH, dashboardFont.measureText(item.label)); } class DashboardLayer implements Layer { @@ -366,7 +352,6 @@ class DashboardLayer implements Layer { const pluginState = getPluginState(); const systemBounds = getCardBounds("system"); - this.drawCardBorder(image, systemBounds, dashboardState.selectedCard === "system"); if (logo && dashboardState.systemCardSettings.showFaceclawLogo) { image.bitBlt(logo, systemBounds.x + 10, systemBounds.y + 10); } @@ -378,13 +363,10 @@ class DashboardLayer implements Layer { for (const slot of getDashboardSlotIds()) { const bounds = getCardBounds(slot); const pluginId = dashboardState.slotConfig[slot]; - if (!isBlankDashboardPlugin(pluginId)) { - this.drawCardBorder(image, bounds, dashboardState.selectedCard === slot); - } getDashboardPlugin(pluginId).renderCard({ image, bounds, - selected: dashboardState.selectedCard === slot, + selected: false, font: dashboardFont, state: pluginState, }); @@ -396,62 +378,43 @@ class DashboardLayer implements Layer { handleInput(event: DashboardInputEvent, ctx: { stack: LayerStack }): void { if (!dashboardState.screenOn) { if (event.type === "double-click") { - dashboardState.selectedCard = "system"; dashboardState.screenOn = true; + dashboardState.tiledWakePaintPending = dashboardState.wakeMode === "tiled"; + ctx.stack.push(createRootMenuLayer()); } return; } - if (event.type === "scroll-up") { - cycleSelection(-1); - return; - } - - if (event.type === "scroll-down") { - cycleSelection(1); - return; - } - if (event.type === "click") { - if (dashboardState.selectedCard === "system") { - ctx.stack.push(createRootMenuLayer()); - } else { - const pluginId = dashboardState.slotConfig[dashboardState.selectedCard]; - if (pluginId === "nightscout" && !isNightscoutSettingsConfigured(dashboardState.nightscoutSettings)) { - ctx.stack.push(createNightscoutSettingsMenuLayer()); - return; - } - const layer = getDashboardPlugin(pluginId).createFullscreenLayer?.( - getPluginState, - ); - if (layer) { - ctx.stack.push(layer); - } - } + ctx.stack.push(createRootMenuLayer()); return; } if (event.type === "double-click") { - dashboardState.selectedCard = "system"; dashboardState.screenOn = false; } } +} - private drawCardBorder(image: GrayImage, bounds: DashboardPluginCardBounds, selected: boolean): void { - if (selected) { - image.drawRect(bounds.x, bounds.y, bounds.width, bounds.height, 60); +class RootMenuLayer extends MenuLayer { + async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise { + if (event.type === "double-click") { + dashboardState.screenOn = false; + ctx.stack.clearToBase(); + return; } + await super.handleInput(event, ctx); } } function createRootMenuLayer(): MenuLayer { - return new MenuLayer( + return new RootMenuLayer( "Menu", [ { - label: "About", + label: "Apps", onSelect: (ctx) => { - ctx.stack.push(new AboutLayer()); + ctx.stack.push(createAppsMenuLayer()); }, }, { @@ -460,6 +423,40 @@ function createRootMenuLayer(): MenuLayer { ctx.stack.push(createSettingsMenuLayer()); }, }, + ...getDashboardSlotIds().map((slot) => ({ + label: pluginLabelForSlot(slot), + onSelect: (ctx: LayerContext) => { + openDashboardSlot(slot, ctx); + }, + render: ({ image, x, y }: { image: GrayImage; x: number; y: number }) => { + image.drawText(dashboardFont, x, y + 3, pluginLabelForSlot(slot), 200); + }, + })), + { + label: "System", + onSelect: (ctx) => { + ctx.stack.push(createSystemMenuLayer()); + }, + }, + ], + TOP_LEFT_MENU_LAYOUT, + ); +} + +function createAppsMenuLayer(): MenuLayer { + return new MenuLayer("Apps", [], TOP_LEFT_MENU_LAYOUT); +} + +function createSystemMenuLayer(): MenuLayer { + return new MenuLayer( + "System", + [ + { + label: "About", + onSelect: (ctx) => { + ctx.stack.push(new AboutLayer()); + }, + }, { label: "Quit / Disconnect", onSelect: async (ctx) => { @@ -468,19 +465,35 @@ function createRootMenuLayer(): MenuLayer { }, }, ], - { - x: 16, - y: 24, - width: 252, - height: 180, - }, + TOP_LEFT_MENU_LAYOUT, ); } +function openDashboardSlot(slot: DashboardSlotId, ctx: LayerContext): void { + const pluginId = dashboardState.slotConfig[slot]; + if (isBlankDashboardPlugin(pluginId)) { + return; + } + if (pluginId === "nightscout" && !isNightscoutSettingsConfigured(dashboardState.nightscoutSettings)) { + ctx.stack.push(createNightscoutSettingsMenuLayer()); + return; + } + const layer = getDashboardPlugin(pluginId).createFullscreenLayer?.(getPluginState); + if (layer) { + ctx.stack.push(layer); + } +} + function createSettingsMenuLayer(): MenuLayer { return new MenuLayer( "Settings", [ + { + label: "Display", + onSelect: (ctx) => { + ctx.stack.push(createDisplaySettingsMenuLayer()); + }, + }, { label: "Dashboard", onSelect: (ctx) => { @@ -494,15 +507,46 @@ function createSettingsMenuLayer(): MenuLayer { }, }, ], - { - x: 16, - y: 24, - width: 252, - height: 180, - }, + TOP_LEFT_MENU_LAYOUT, ); } +function createDisplaySettingsMenuLayer(): MenuLayer { + return new MenuLayer( + "Display", + [ + { + label: "Screen timeout", + onSelect: () => { + setScreenTimeout(nextScreenTimeoutSetting(dashboardState.screenTimeout)); + }, + render: ({ image, x, y, width }) => { + drawRightValueMenuItem(image, dashboardFont, x, y, width, "Screen timeout", screenTimeoutLabel(dashboardState.screenTimeout)); + }, + }, + { + label: "Wake mode", + onSelect: () => { + setWakeMode(nextWakeModeSetting(dashboardState.wakeMode)); + }, + render: ({ image, x, y, width }) => { + drawRightValueMenuItem(image, dashboardFont, x, y, width, "Wake mode", wakeModeLabel(dashboardState.wakeMode)); + }, + }, + ], + TOP_LEFT_MENU_LAYOUT, + ); +} + +function setScreenTimeout(value: ScreenTimeoutSetting): void { + dashboardState.screenTimeout = saveScreenTimeoutSetting(value); + dashboardState.lastInputAtMs = Date.now(); +} + +function setWakeMode(value: WakeModeSetting): void { + dashboardState.wakeMode = saveWakeModeSetting(value); +} + function createDashboardSettingsMenuLayer(): MenuLayer { return new MenuLayer( "Dashboard", @@ -519,16 +563,11 @@ function createDashboardSettingsMenuLayer(): MenuLayer { ctx.stack.push(createSlotPickerMenu(slot)); }, render: ({ image, x, y }) => { - image.drawText(dashboardFont, x, y + 4, `${slotMenuLabel(slot)}: ${pluginLabelForSlot(slot)}`, 200); + image.drawText(dashboardFont, x, y + 3, `${slotMenuLabel(slot)}: ${pluginLabelForSlot(slot)}`, 200); }, })), ], - { - x: 16, - y: 24, - width: 252, - height: 180, - }, + TOP_LEFT_MENU_LAYOUT, ); } @@ -543,19 +582,15 @@ function createSystemCardSettingsMenuLayer(): MenuLayer { ctx.stack.push(new EditSystemNameLayer()); }, render: ({ image, x, y }) => { - image.drawText(dashboardFont, x, y + 4, `Name: ${getDisplayedSystemCardName()}`, 200); + image.drawText(dashboardFont, x, y + 3, `Name: ${getDisplayedSystemCardName()}`, 200); }, }, createSystemCardToggleItem("Show Faceclaw Logo", "showFaceclawLogo"), createSystemCardToggleItem("Show Battery Indicators", "showBatteryIndicators"), createSystemCardToggleItem("Show Android Notifications", "showAndroidNotifications"), + createSystemCardToggleItem("Show Signal Strength", "showSignalStrength"), ], - { - x: 308, - y: 24, - width: 252, - height: 180, - }, + TOP_RIGHT_MENU_LAYOUT, true, ); } @@ -567,25 +602,11 @@ function createSystemCardToggleItem(label: string, key: SystemCardSettingKey) { setDashboardSystemCardSetting(key, !dashboardState.systemCardSettings[key]); }, render: ({ image, x, y, width, selected }: { image: GrayImage; x: number; y: number; width: number; selected: boolean }) => { - drawToggleMenuItem(image, x, y, width, label, dashboardState.systemCardSettings[key], selected); + drawToggleMenuItem(image, dashboardFont, x, y, width, label, dashboardState.systemCardSettings[key], selected); }, }; } -function drawToggleMenuItem(image: GrayImage, x: number, y: number, width: number, label: string, enabled: boolean, selected: boolean): void { - const switchWidth = 34; - const switchHeight = 16; - const switchX = x + width - switchWidth - 2; - const switchY = y + 2; - image.drawText(dashboardFont, x, y + 4, label, 200); - const offFill = selected ? 0 : 18; - image.fillRoundedRect(switchX, switchY, switchWidth, switchHeight, enabled ? 70 : offFill, 8); - image.drawRoundedRect(switchX, switchY, switchWidth, switchHeight, enabled ? 130 : 55, 8); - const knobSize = 12; - const knobX = enabled ? switchX + switchWidth - knobSize - 2 : switchX + 2; - image.fillRoundedRect(knobX, switchY + 2, knobSize, knobSize, enabled ? 230 : selected ? 170 : 90, 6); -} - function createIntegrationsMenuLayer(): MenuLayer { return new MenuLayer( "Integrations", @@ -597,12 +618,7 @@ function createIntegrationsMenuLayer(): MenuLayer { }, }, ], - { - x: 16, - y: 24, - width: 252, - height: 180, - }, + TOP_LEFT_MENU_LAYOUT, ); } @@ -617,7 +633,7 @@ function createNightscoutSettingsMenuLayer(): MenuLayer { ctx.stack.push(new EditTextSettingLayer("Edit Nightscout URL", () => dashboardState.nightscoutSettings.siteUrl || "(empty)")); }, render: ({ image, x, y }) => { - image.drawText(dashboardFont, x, y + 4, `URL: ${truncateSetting(dashboardState.nightscoutSettings.siteUrl)}`, 200); + image.drawText(dashboardFont, x, y + 3, `URL: ${truncateSetting(dashboardState.nightscoutSettings.siteUrl)}`, 200); }, }, { @@ -627,16 +643,11 @@ function createNightscoutSettingsMenuLayer(): MenuLayer { ctx.stack.push(new EditTextSettingLayer("Edit API token", () => maskToken(dashboardState.nightscoutSettings.apiToken))); }, render: ({ image, x, y }) => { - image.drawText(dashboardFont, x, y + 4, `Token: ${maskToken(dashboardState.nightscoutSettings.apiToken)}`, 200); + image.drawText(dashboardFont, x, y + 3, `Token: ${maskToken(dashboardState.nightscoutSettings.apiToken)}`, 200); }, }, ], - { - x: 16, - y: 24, - width: 252, - height: 180, - }, + TOP_LEFT_MENU_LAYOUT, ); } @@ -694,22 +705,14 @@ function createSlotPickerMenu(slot: DashboardSlotId): MenuLayer { onSelect: (ctx) => { dashboardState.slotConfig[slot] = plugin.id; saveDashboardSlotPlugin(slot, plugin.id); - if (dashboardState.selectedCard === slot && isBlankDashboardPlugin(plugin.id)) { - dashboardState.selectedCard = "system"; - } ctx.stack.pop(); }, render: ({ image, x, y }) => { const selected = dashboardState.slotConfig[slot] === plugin.id ? " *" : ""; - image.drawText(dashboardFont, x, y + 4, `${plugin.label}${selected}`, 200); + image.drawText(dashboardFont, x, y + 3, `${plugin.label}${selected}`, 200); }, })), - { - x: 16, - y: 24, - width: 252, - height: 180, - }, + TOP_LEFT_MENU_LAYOUT, ); } @@ -729,9 +732,9 @@ class AboutLayer implements Layer { "A lobster on top of a smiley face.", "Perching on the faces of giants.", "", - "System card opens menus.", - "Swipe cycles dashboard cards.", - "Plugin cards open fullscreen views.", + "The menu opens dashboard cards.", + "Settings configure card slots.", + "Double-click backs out of menus.", ]; for (let index = 0; index < aboutLines.length; index++) { image.drawText(dashboardFont, 22, 128 + index * 14, aboutLines[index]!, 180); @@ -759,8 +762,6 @@ function getPluginState(): DashboardPluginState { function getCardBounds(card: DashboardCardId): DashboardPluginCardBounds { switch (card) { case "system": - return { x: 0, y: 0, width: G2_LENS_WIDTH / 2 - 2, height: G2_LENS_HEIGHT / 2 - 2 }; - case "top-right": return { x: G2_LENS_WIDTH / 2 + 2, y: 0, @@ -784,24 +785,12 @@ function getCardBounds(card: DashboardCardId): DashboardPluginCardBounds { } } -function cycleSelection(direction: -1 | 1): void { - const order: DashboardCardId[] = [ - "system", - ...getDashboardSlotIds().filter((slot) => !isBlankDashboardPlugin(dashboardState.slotConfig[slot])), - ]; - const index = order.indexOf(dashboardState.selectedCard); - const currentIndex = index >= 0 ? index : 0; - dashboardState.selectedCard = order[(currentIndex + direction + order.length) % order.length]!; -} - function slotMenuLabel(slot: DashboardSlotId): string { switch (slot) { - case "top-right": - return "Top-right slot"; case "bottom-left": - return "Bottom-left slot"; + return "Bottom-left"; case "bottom-right": - return "Bottom-right slot"; + return "Bottom-right"; } } @@ -820,4 +809,5 @@ function maskToken(token: string): string { } const dashboardLayers = new LayerStack(new DashboardLayer(), dashboardFont, dashboardActions); +dashboardLayers.push(createRootMenuLayer()); diff --git a/app/ui/menu.ts b/app/ui/menu.ts index ed119d17..0ec4c967 100644 --- a/app/ui/menu.ts +++ b/app/ui/menu.ts @@ -1,13 +1,14 @@ import { GrayImage } from "../graphics/image"; import { wrapText } from "../graphics/textwrap"; +import { type BdfFont } from "../graphics/bdffont"; import { DashboardInputEvent, Layer, LayerContext, PaintBelow } from "./layers"; -const DEFAULT_MENU_X = 48; -const DEFAULT_MENU_Y = 24; -const DEFAULT_MENU_WIDTH = 480; -const DEFAULT_MENU_HEIGHT = 240; -const MENU_TITLE_HEIGHT = 24; -const MENU_ROW_HEIGHT = 28; +const DEFAULT_MENU_X = 8; +const DEFAULT_MENU_Y = 8; +const DEFAULT_MENU_WIDTH = 272; +const DEFAULT_MENU_HEIGHT = 128; +const MENU_TITLE_HEIGHT = 20; +const MENU_ROW_HEIGHT = 20; export type MenuLayout = { x: number; @@ -33,6 +34,43 @@ export type MenuItem = { render?: (args: MenuItemRenderArgs) => void; }; +export function drawToggleMenuItem( + image: GrayImage, + font: BdfFont, + x: number, + y: number, + width: number, + label: string, + enabled: boolean, + selected: boolean, +): void { + const switchWidth = 34; + const switchHeight = 16; + const switchX = x + width - switchWidth - 2; + const switchY = y + 2; + image.drawText(font, x, y + 3, label, 200); + const offFill = selected ? 0 : 18; + image.fillRoundedRect(switchX, switchY, switchWidth, switchHeight, enabled ? 70 : offFill, 8); + image.drawRoundedRect(switchX, switchY, switchWidth, switchHeight, enabled ? 130 : 55, 8); + const knobSize = 12; + const knobX = enabled ? switchX + switchWidth - knobSize - 2 : switchX + 2; + image.fillRoundedRect(knobX, switchY + 2, knobSize, knobSize, enabled ? 230 : selected ? 170 : 90, 6); +} + +export function drawRightValueMenuItem( + image: GrayImage, + font: BdfFont, + x: number, + y: number, + width: number, + label: string, + value: string, +): void { + image.drawText(font, x, y + 3, label, 200); + const valueX = x + width - font.measureText(value) - 2; + image.drawText(font, valueX, y + 3, value, 220); +} + export class MenuLayer implements Layer { private selectedIndex = 0; @@ -55,14 +93,14 @@ export class MenuLayer implements Layer { image.drawRoundedRect(x, y, width, height, 72); image.drawText(ctx.font, x + 18, y + 10, this.title, 220); - const bodyY = y + MENU_TITLE_HEIGHT + 12; + const bodyY = y + MENU_TITLE_HEIGHT + 8; for (let index = 0; index < this.items.length; index++) { const item = this.items[index]!; const y = bodyY + index * MENU_ROW_HEIGHT; const selected = index === this.selectedIndex; if (selected) { - image.fillRoundedRect(x + 12, y - 2, width - 24, MENU_ROW_HEIGHT - 4, 18); - image.drawRoundedRect(x + 12, y - 2, width - 24, MENU_ROW_HEIGHT - 4, 45); + image.fillRoundedRect(x + 12, y - 1, width - 24, MENU_ROW_HEIGHT - 3, 18); + image.drawRoundedRect(x + 12, y - 1, width - 24, MENU_ROW_HEIGHT - 3, 45); } if (item.render) { item.render({ @@ -70,13 +108,13 @@ export class MenuLayer implements Layer { x: x + 22, y, width: width - 44, - height: MENU_ROW_HEIGHT - 4, + height: MENU_ROW_HEIGHT - 3, selected, text: item.label, ctx, }); } else { - image.drawText(ctx.font, x + 22, y + 4, item.label, selected ? 255 : 200); + image.drawText(ctx.font, x + 22, y + 3, item.label, selected ? 255 : 200); } }