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

+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++) {