More conservating locking around Android bluetooth APIs, to try to address SIGSEGV seen on Galaxy Tab A9+

This commit is contained in:
James Babcock committed 2026-04-29 10:12:00 -07:00
1 parent aa0b1d6874
commit 7cbcbf7c90
5 files changed
+282 -115

No files matched your search

+45 -7
View File
@@ -7,8 +7,9 @@ import { startForegroundNotification, stopForegroundNotification, updateForegrou
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 { grayImageToPreviewSource, grayImageToPreviewSourceWithBitmapFactory } from "../native/gray-image-preview";
import { voiceControlBridge } from "../native/voice-control";
import { type GrayImage } from "../graphics/image";
import {
applyDashboardScreenTimeout,
consumeDashboardTiledWakePaint,
@@ -53,6 +54,7 @@ const SCREEN_TIMEOUT_CHECK_MS = 1_000;
const STOPWATCH_RENDER_INTERVAL_MS = 100;
const FOREGROUND_NOTIFICATION_MIN_UPDATE_MS = 30_000;
const FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS = 6_000;
const CONNECTED_PREVIEW_MIN_UPDATE_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.";
@@ -134,6 +136,8 @@ class DashboardController {
private renderQueued = false;
private queuedRenderReason: "initial" | "interval" = "interval";
private lastForegroundNotificationUpdateAtMs = 0;
private lastConnectedPreviewUpdateAtMs = 0;
private connectedPreviewUpdateSeq = 0;
constructor() {
setDashboardActions({
@@ -202,7 +206,7 @@ class DashboardController {
});
} else {
const image = drawDashboard();
this.setDisplayPreview(grayImageToPreviewSource(image));
this.updateDisplayPreviewFromImage(image);
}
if (savedName !== name) {
this.emit();
@@ -551,7 +555,7 @@ class DashboardController {
});
} else {
const image = drawDashboard();
this.setDisplayPreview(grayImageToPreviewSource(image));
this.updateDisplayPreviewFromImage(image);
}
}
@@ -563,7 +567,7 @@ class DashboardController {
return;
}
const image = drawDashboard();
this.setDisplayPreview(grayImageToPreviewSource(image));
this.updateDisplayPreviewFromImage(image);
}
private async requestRender(reason: "initial" | "interval"): Promise<void> {
@@ -593,14 +597,22 @@ class DashboardController {
const forceTiledCommit = consumeDashboardTiledWakePaint();
const fingerprint = image.fingerprint();
const tiles = image.toEvenHubTiles().map((tile) => tile.bmp);
this.setDisplayPreview(grayImageToPreviewSource(image));
const updatePreviewAfterTransmit = this.phase === "connected";
if (!updatePreviewAfterTransmit) {
this.updateDisplayPreviewFromImage(image);
}
if (this.communicator) {
await this.communicator.submitDashboardImage(tiles, fingerprint, forceTiledCommit, paintMs);
await this.communicator.waitForNextFrameMetrics(FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS);
if (updatePreviewAfterTransmit) {
await this.updateConnectedDisplayPreviewFromImage(image);
}
}
this.updateConnectedForegroundNotification();
this.appendLog(`${reason === "initial" ? "initial" : "scheduled"} dashboard image queued`);
if (reason === "initial") {
this.appendLog("initial dashboard image queued");
}
}
private async handleInputEvent(event: RawInputEvent): Promise<void> {
@@ -651,7 +663,7 @@ class DashboardController {
}
if (changed) {
const image = drawDashboard();
this.setDisplayPreview(grayImageToPreviewSource(image));
this.updateDisplayPreviewFromImage(image);
}
}
@@ -730,6 +742,32 @@ class DashboardController {
this.emit();
}
private updateDisplayPreviewFromImage(image: GrayImage): void {
this.setDisplayPreview(grayImageToPreviewSource(image));
}
private async updateConnectedDisplayPreviewFromImage(image: GrayImage): Promise<void> {
if (!this.communicator) return;
const now = Date.now();
if (
this.lastConnectedPreviewUpdateAtMs > 0 &&
now - this.lastConnectedPreviewUpdateAtMs < CONNECTED_PREVIEW_MIN_UPDATE_MS
) {
return;
}
this.lastConnectedPreviewUpdateAtMs = now;
const previewUpdateId = ++this.connectedPreviewUpdateSeq;
console.log(`[preview#${previewUpdateId}] create start`);
const preview = await grayImageToPreviewSourceWithBitmapFactory(
image,
(createColors, width, height) =>
this.communicator!.createPreviewBitmapWithColorFactory(createColors, width, height),
);
console.log(`[preview#${previewUpdateId}] create done; setDisplayPreview start`);
this.setDisplayPreview(preview);
console.log(`[preview#${previewUpdateId}] setDisplayPreview done`);
}
private formatError(error: unknown): string {
const raw = (error as Error)?.message ?? String(error);
const sanitized = raw.replace(/[\x00-\x1f]+/g, " ").replace(/\s+/g, " ").trim();
+51 -10
View File
@@ -2,6 +2,8 @@ import { Utils } from "@nativescript/core";
declare const com: any;
const FRAME_METRICS_LISTENER_MIN_INTERVAL_MS = 5_000;
export type CommunicatorPhase =
| "disconnected"
| "connecting"
@@ -73,6 +75,7 @@ export class FaceclawCommunicatorBridge {
private readonly batteryListeners = new Set<(state: HeadsetBatteryState) => void>();
private readonly evenAppConflictListeners = new Set<(message: string) => void>();
private readonly frameMetricsListeners = new Set<(metrics: FrameMetrics) => void>();
private lastFrameMetricsListenerEmitAtMs = 0;
constructor(addresses: { right: string; left: string; ring?: string }) {
const context = Utils.android.getApplicationContext();
@@ -132,7 +135,14 @@ export class FaceclawCommunicatorBridge {
for (const waiter of waiters) {
setTimeout(() => waiter(metrics), 0);
}
this.emitAsync(this.frameMetricsListeners, metrics);
const now = Date.now();
if (
this.lastFrameMetricsListenerEmitAtMs === 0 ||
now - this.lastFrameMetricsListenerEmitAtMs >= FRAME_METRICS_LISTENER_MIN_INTERVAL_MS
) {
this.lastFrameMetricsListenerEmitAtMs = now;
this.emitAsync(this.frameMetricsListeners, metrics);
}
},
});
this.communicator.setListener(this.listenerProxy);
@@ -167,6 +177,15 @@ export class FaceclawCommunicatorBridge {
return result;
}
private withNativeBridgeLock<T>(operation: string, callback: () => T): T {
this.communicator.acquireNativeBridgeLock(operation);
try {
return callback();
} finally {
this.communicator.releaseNativeBridgeLock(operation);
}
}
onLog(listener: (line: string) => void): () => void {
this.logListeners.add(listener);
return () => this.logListeners.delete(listener);
@@ -227,18 +246,40 @@ export class FaceclawCommunicatorBridge {
}
const tileSnapshots = tileBmps.map((tile) => new Uint8Array(tile));
await this.enqueueJavaCall(() => {
this.communicator.submitDashboardImage4(
toJavaByteArray(tileSnapshots[0]!),
toJavaByteArray(tileSnapshots[1]!),
toJavaByteArray(tileSnapshots[2]!),
toJavaByteArray(tileSnapshots[3]!),
fingerprint,
forceTiledCommit,
Math.round(nonNegativeNumber(paintMs)),
);
this.withNativeBridgeLock("submitDashboardImage4 bridge arrays", () => {
this.communicator.submitDashboardImage4(
toJavaByteArray(tileSnapshots[0]!),
toJavaByteArray(tileSnapshots[1]!),
toJavaByteArray(tileSnapshots[2]!),
toJavaByteArray(tileSnapshots[3]!),
fingerprint,
forceTiledCommit,
Math.round(nonNegativeNumber(paintMs)),
);
});
});
}
async createPreviewBitmap(colors: number[], width: number, height: number): Promise<any> {
return this.enqueueJavaCall(() =>
this.withNativeBridgeLock("createPreviewBitmap bridge", () =>
this.communicator.createPreviewBitmap(colors, width, height),
),
);
}
async createPreviewBitmapWithColorFactory(
createColors: () => number[],
width: number,
height: number,
): Promise<any> {
return this.enqueueJavaCall(() =>
this.withNativeBridgeLock("createPreviewBitmap bridge colors", () =>
this.communicator.createPreviewBitmap(createColors(), width, height),
),
);
}
async disconnect(): Promise<void> {
await this.enqueueJavaCall(() => this.communicator.disconnect());
}
+21 -4
View File
@@ -10,10 +10,7 @@ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null {
return null;
}
const colors = Array.create("int", image.width * image.height) as number[];
for (let i = 0; i < image.pixels.length; i++) {
colors[i] = PREVIEW_COLOR_LUT[image.pixels[i] ?? 0]!;
}
const colors = grayImageToPreviewColors(image);
const bitmap = android.graphics.Bitmap.createBitmap(
colors,
@@ -24,6 +21,26 @@ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null {
return new ImageSource(bitmap);
}
export async function grayImageToPreviewSourceWithBitmapFactory(
image: GrayImage,
createBitmap: (createColors: () => number[], width: number, height: number) => Promise<any>,
): Promise<ImageSource | null> {
if (!global.isAndroid) {
return null;
}
const bitmap = await createBitmap(() => grayImageToPreviewColors(image), image.width, image.height);
return new ImageSource(bitmap);
}
function grayImageToPreviewColors(image: GrayImage): number[] {
const colors = Array.create("int", image.width * image.height) as number[];
for (let i = 0; i < image.pixels.length; i++) {
colors[i] = PREVIEW_COLOR_LUT[image.pixels[i] ?? 0]!;
}
return colors;
}
function buildPreviewColorLookup(): number[] {
const colors: number[] = [];
for (let gray = 0; gray <= 255; gray++) {