forked from Blendman974/faceclaw
Correctness: - Pair-glasses flows no longer strand the app in the manual-disconnected state: the pre-scan disconnect is followed by resumeAutoReconnect(), and the config page's scan hand-off drops the live link too. - The mixed-arms serial warning is reachable now: pairs() groups by the canonical serial key, and a lone left + lone right with different serials get the mismatch warning on both rows (it previously required two different serials inside one serial-keyed group, which cannot happen). - A selected pair that degrades to one arm (prune while the page is open) is deselected, and Save never pads a missing arm from stored addresses. - DiscoveryAggregator.ingest no longer lets a bonded re-emit or split advertisement overwrite the note/name/flag learned from a full advertisement, and reclassifies split reports using remembered fields so a nameless half doesn't drop the arm. - estimateProximity ignores the advertised TX Power Level (radiated power, not measured-power-at-1m); the calibration table is the only reference. - Saving a ring without a glasses pair updates the stored identity's ring name/address instead of leaving the previous ring on record. - Ring detection requires the stock "EVEN R1" name prefix in both the TS classifier and the Java pre-filter; a bare "R1" substring matched unrelated devices, and ring rows are always selectable. - One-shot scans keep every sample (buildAddressSet merges them through DiscoveryAggregator), and flash-flow address resolution takes left/right from one serial-joined complete pair instead of latest-per-role. Lifecycle: - The pairing scan and its refresh timer pause on app suspend and resume with the app, and the model's app-event hooks are detached when the page is left for good. Cleanup: - One MAC normalizer (even-advertisement.normalizeMacAddress, re-exported by device-addresses), used for the ring "Yours" comparison too. - One serial-equality definition (serialKey/serialsMatch); the containment fallback in isPreviouslyPaired is gone. - Shared formatErrorMessage replaces five per-view-model copies; shared hex-util replaces the duplicate TS hex codecs; one Java bytesToHex in FaceclawFirmwareUtil replaces three hand-rolled encoders. - glassesRow/ringRow share a presentation-base helper; dead row address fields, the constant glassesSectionVisibility, the identical-branch status ternary, and zoneRank/ZONE_ORDER are removed. - .gitignore now actually covers .test-build/ as tests/README claims. UI: - Dark-mode styles for the pairing page: readable instructions, dark-gray cards with consistent light text; light mode keeps its current look. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016ZqzwmRQ3HQxasBz5rYyZV
306 lines
9.3 KiB
TypeScript
306 lines
9.3 KiB
TypeScript
import { Frame, Observable } from "@nativescript/core";
|
|
|
|
import { ensureBlePermissions } from "../g2/android-permissions";
|
|
import { isValidMacAddress, loadDeviceAddresses } from "../g2/device-addresses";
|
|
import {
|
|
downloadAndExtractEvenHubFonts,
|
|
hasExtractedEvenHubFonts,
|
|
type FirmwareProgress,
|
|
} from "../g2/firmware-builder";
|
|
import { classifyOnboardingFirmware, FLASHABLE_STOCK_VERSION_TEXT } from "../g2/firmware-compat";
|
|
import { DeviceInfoProbe, DeviceInfoState } from "../native/device-info-probe";
|
|
import { setOnboardingCompleted, setPreviewOnlyMode } from "./onboarding-state";
|
|
import { formatErrorMessage } from "../util/format-error";
|
|
|
|
type CheckPhase = "checking" | "fonts" | "custom" | "flashable" | "newer" | "error";
|
|
|
|
export class OnboardingFirmwareCheckViewModel extends Observable {
|
|
private _phase: CheckPhase = "checking";
|
|
private _headline = "Checking Firmware";
|
|
private _status = "";
|
|
private _busy = true;
|
|
|
|
private probeInstance: DeviceInfoProbe | null = null;
|
|
|
|
constructor() {
|
|
super();
|
|
void this.check();
|
|
}
|
|
|
|
// --- observable properties -------------------------------------------------
|
|
|
|
get headline(): string {
|
|
return this._headline;
|
|
}
|
|
|
|
set headline(value: string) {
|
|
if (this._headline !== value) {
|
|
this._headline = value;
|
|
this.notifyPropertyChange("headline", value);
|
|
}
|
|
}
|
|
|
|
get status(): string {
|
|
return this._status;
|
|
}
|
|
|
|
set status(value: string) {
|
|
if (this._status !== value) {
|
|
this._status = value;
|
|
this.notifyPropertyChange("status", value);
|
|
}
|
|
}
|
|
|
|
get busy(): boolean {
|
|
return this._busy;
|
|
}
|
|
|
|
set busy(value: boolean) {
|
|
if (this._busy !== value) {
|
|
this._busy = value;
|
|
this.notifyPropertyChange("busy", value);
|
|
this.notifyPropertyChange("busyVisibility", this.busyVisibility);
|
|
}
|
|
}
|
|
|
|
get busyVisibility(): "visible" | "collapse" {
|
|
return this._busy ? "visible" : "collapse";
|
|
}
|
|
|
|
get primaryLabel(): string {
|
|
switch (this._phase) {
|
|
case "custom":
|
|
return "Finish";
|
|
case "flashable":
|
|
return "Install Firmware";
|
|
case "newer":
|
|
return "Proceed Anyway";
|
|
case "error":
|
|
return "Retry";
|
|
default:
|
|
return "";
|
|
}
|
|
}
|
|
|
|
get secondaryLabel(): string {
|
|
return "Back";
|
|
}
|
|
|
|
get primaryVisibility(): "visible" | "collapse" {
|
|
return this.primaryLabel ? "visible" : "collapse";
|
|
}
|
|
|
|
get secondaryVisibility(): "visible" | "collapse" {
|
|
// Once the custom firmware is confirmed present, Finish is the only action.
|
|
return this._phase === "custom" || this._phase === "fonts" ? "collapse" : "visible";
|
|
}
|
|
|
|
// --- button handlers -------------------------------------------------------
|
|
|
|
onPrimaryTap(): void {
|
|
switch (this._phase) {
|
|
case "custom":
|
|
this.finish();
|
|
return;
|
|
case "flashable":
|
|
case "newer":
|
|
this.goToFlashing();
|
|
return;
|
|
case "error":
|
|
void this.check();
|
|
return;
|
|
default:
|
|
return;
|
|
}
|
|
}
|
|
|
|
onSecondaryTap(): void {
|
|
this.disposeProbe();
|
|
const frame = Frame.topmost();
|
|
if (frame?.canGoBack()) {
|
|
frame.goBack();
|
|
return;
|
|
}
|
|
frame?.navigate({ moduleName: "phone-ui/onboarding-unpair-page", clearHistory: true });
|
|
}
|
|
|
|
// --- probe flow ------------------------------------------------------------
|
|
|
|
private async check(): Promise<void> {
|
|
if (!global.isAndroid) {
|
|
this.toError("Firmware checking is only available on Android.");
|
|
return;
|
|
}
|
|
this.setPhase("checking");
|
|
this.headline = "Checking Firmware";
|
|
this.busy = true;
|
|
this.status = "Connecting to your glasses to read their firmware version...";
|
|
|
|
try {
|
|
await ensureBlePermissions();
|
|
const stored = loadDeviceAddresses();
|
|
if (!isValidMacAddress(stored.right)) {
|
|
this.toError("No glasses address is configured. Go back and set the device addresses.");
|
|
return;
|
|
}
|
|
|
|
this.disposeProbe();
|
|
const probe = new DeviceInfoProbe(stored.right);
|
|
this.probeInstance = probe;
|
|
probe.onStateChange((state) => this.reportProbeState(state));
|
|
|
|
const info = await probe.run();
|
|
this.probeInstance = null;
|
|
|
|
const { kind, version } = classifyOnboardingFirmware(info);
|
|
this.applyClassification(kind, version, info.capabilities.trim());
|
|
} catch (error) {
|
|
this.probeInstance = null;
|
|
this.toError(this.formatError(error));
|
|
}
|
|
}
|
|
|
|
private reportProbeState(state: DeviceInfoState): void {
|
|
if (state === "connecting") {
|
|
this.status = "Connecting to your glasses...";
|
|
} else if (state === "querying") {
|
|
this.status = "Reading the firmware version...";
|
|
}
|
|
}
|
|
|
|
private applyClassification(kind: string, version: string, capabilities: string): void {
|
|
this.busy = false;
|
|
switch (kind) {
|
|
case "custom":
|
|
if (!hasExtractedEvenHubFonts()) {
|
|
void this.extractFontsForCustomFirmware(version, capabilities);
|
|
break;
|
|
}
|
|
this.showCustomReady(version, capabilities, false);
|
|
break;
|
|
case "flashable":
|
|
this.setPhase("flashable");
|
|
this.headline = "Ready to Install";
|
|
this.status =
|
|
`Your glasses run stock firmware ${version}. This is compatible — tap Install Firmware to flash ` +
|
|
"Faceclaw's custom firmware.";
|
|
break;
|
|
case "newer":
|
|
this.setPhase("newer");
|
|
this.headline = "Unrecognized Firmware";
|
|
this.status =
|
|
`Your glasses run stock firmware ${version}, which is newer than the ${FLASHABLE_STOCK_VERSION_TEXT} ` +
|
|
"release Faceclaw's custom image is built from. Flashing may not work correctly and carries extra risk. " +
|
|
"You can proceed anyway, or go back.";
|
|
break;
|
|
default:
|
|
// "unknown" — connected but no version. Treat as a probe failure (hard block).
|
|
this.toError(
|
|
"Connected, but couldn't read a firmware version. Make sure the glasses are on and the Even app is " +
|
|
"disconnected, then retry.",
|
|
);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private async extractFontsForCustomFirmware(version: string, capabilities: string): Promise<void> {
|
|
this.setPhase("fonts");
|
|
this.headline = "Preparing G2 Fonts";
|
|
this.busy = true;
|
|
this.status =
|
|
"Custom firmware is already installed, but its phone-side fonts are missing. " +
|
|
"Downloading the official firmware to extract them; your glasses will not be reflashed.";
|
|
try {
|
|
await downloadAndExtractEvenHubFonts((progress) => this.reportFontProgress(progress));
|
|
this.showCustomReady(version, capabilities, true);
|
|
} catch (error) {
|
|
this.toError(
|
|
`Custom firmware is installed, but the G2 fonts could not be prepared: ${this.formatError(error)}`,
|
|
"Couldn't Prepare Fonts",
|
|
);
|
|
}
|
|
}
|
|
|
|
private reportFontProgress(progress: FirmwareProgress): void {
|
|
switch (progress.phase) {
|
|
case "downloading":
|
|
this.status = "Downloading the official firmware from Even's CDN...";
|
|
break;
|
|
case "verifying-base":
|
|
this.status = "Verifying the downloaded firmware...";
|
|
break;
|
|
case "extracting-fonts":
|
|
this.status = "Extracting the G2 fonts for EvenHub apps...";
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
private showCustomReady(version: string, capabilities: string, extractedFonts: boolean): void {
|
|
this.busy = false;
|
|
this.setPhase("custom");
|
|
this.headline = extractedFonts ? "Fonts Ready" : "Custom Firmware Detected";
|
|
this.status =
|
|
`Your glasses already run Faceclaw's custom firmware${version ? ` (version ${version})` : ""}` +
|
|
`${capabilities ? `, extensions: ${capabilities}` : ""}. ` +
|
|
(extractedFonts
|
|
? "The phone-side G2 fonts were extracted successfully. No flashing was needed — you're all set."
|
|
: "The phone-side G2 fonts are present. No flashing needed — you're all set.");
|
|
}
|
|
|
|
// --- terminal actions ------------------------------------------------------
|
|
|
|
private goToFlashing(): void {
|
|
this.disposeProbe();
|
|
Frame.topmost()?.navigate({
|
|
moduleName: "phone-ui/onboarding-flash-page",
|
|
context: { mode: "install", fromOnboarding: true },
|
|
});
|
|
}
|
|
|
|
private finish(): void {
|
|
setPreviewOnlyMode(false);
|
|
setOnboardingCompleted(true);
|
|
this.disposeProbe();
|
|
Frame.topmost()?.navigate({
|
|
moduleName: "phone-ui/main-page",
|
|
clearHistory: true,
|
|
});
|
|
}
|
|
|
|
private toError(message: string, headline = "Couldn't Check Firmware"): void {
|
|
this.disposeProbe();
|
|
this.busy = false;
|
|
this.status = message;
|
|
this.setPhase("error");
|
|
this.headline = headline;
|
|
}
|
|
|
|
private setPhase(phase: CheckPhase): void {
|
|
if (this._phase !== phase) {
|
|
this._phase = phase;
|
|
this.notifyPropertyChange("phase", phase);
|
|
}
|
|
this.notifyPropertyChange("primaryLabel", this.primaryLabel);
|
|
this.notifyPropertyChange("secondaryLabel", this.secondaryLabel);
|
|
this.notifyPropertyChange("primaryVisibility", this.primaryVisibility);
|
|
this.notifyPropertyChange("secondaryVisibility", this.secondaryVisibility);
|
|
}
|
|
|
|
private disposeProbe(): void {
|
|
if (this.probeInstance) {
|
|
try {
|
|
this.probeInstance.close();
|
|
} catch {
|
|
// ignore
|
|
}
|
|
this.probeInstance = null;
|
|
}
|
|
}
|
|
|
|
private formatError(error: unknown): string {
|
|
return formatErrorMessage(error);
|
|
}
|
|
}
|