This commit is contained in:
James Babcock committed 2026-10-10 17:34:46 -07:00
commit 5df873e5b1
73 files changed
+3906 -98

No files matched your search

+84
View File
@@ -0,0 +1,84 @@
/**
* The app-wide things analytics reports (the reliability counts come from
* Kotlin, drained in analytics.ts):
* - Minimal: which devices are paired, and which were in use each day;
* - Full: which kinds of API key are set, how many terminal and T3 Code
* connections there are, the UI font, and every fixed-choice setting.
* Never anything a person typed in: no keys, addresses, names or URLs.
*/
import { loadEnvironments } from "../apps/t3code/t3-environments";
import { loadConnections } from "../apps/terminal/connections";
import { loadDeviceAddresses } from "../g2/device-addresses";
import { onGlassesPresenceChanged } from "../g2/glasses-presence";
import { isPreinstalledFont } from "../graphics/installed-fonts";
import { getUiFontSelection } from "../graphics/ui-fonts";
import { getStringSetting } from "../native/settings-store";
import { isPreviewOnlyMode } from "../phone-ui/onboarding-state";
import { fixedChoiceSettingValues, ringConnectionModeSetting } from "../ui/dashboard-settings";
import { addInputListener } from "../ui/input-monitor";
import { flagAnalyticsEvent, registerAnalyticsSnapshot, startAnalytics } from "./analytics";
import type { Snapshot } from "./analytics-store";
/** Settings holding API keys and tokens, reported only as set or not, by kind. */
const API_KEY_SETTINGS: Record<string, string> = {
anthropic: "llm.anthropicApiKey",
openai: "voice.openAiApiKey",
elevenlabs: "voice.elevenLabsApiKey",
soniox: "voice.sonioxApiKey",
mapbox: "maps.mapboxApiKey",
roam: "integrations.roam.apiToken",
nightscout: "integrations.nightscout.apiToken",
"agent-bridge": "assistant.bridgeToken",
evenhub: "integrations.evenhub.token",
};
let started = false;
/** Registers the sources above and starts analytics. Main thread, at boot. */
export function startAppAnalytics(): void {
if (started) return;
started = true;
registerAnalyticsSnapshot("minimal", deviceSnapshot);
registerAnalyticsSnapshot("full", configurationSnapshot);
onGlassesPresenceChanged((presence) => {
if (presence.connected) flagAnalyticsEvent("minimal", "device.g2.used");
});
addInputListener((event) => {
if (!("source" in event)) return;
if (event.source === "ring") flagAnalyticsEvent("minimal", "device.r1.used");
else if (event.source === "left-arm" || event.source === "right-arm") flagAnalyticsEvent("minimal", "device.g2.touchpad-used");
});
startAnalytics();
}
function deviceSnapshot(): Snapshot {
const addresses = loadDeviceAddresses();
return {
"device.g2.paired": Boolean(addresses.left && addresses.right),
"device.g2.preview-only": isPreviewOnlyMode(),
"device.r1.paired": Boolean(addresses.ring),
"device.r1.connection": ringConnectionModeSetting.get(),
};
}
function configurationSnapshot(): Snapshot {
const snapshot: Snapshot = {};
for (const [kind, storageKey] of Object.entries(API_KEY_SETTINGS)) {
snapshot[`apikey.${kind}`] = getStringSetting(storageKey, "").trim() !== "";
}
snapshot["config.nightscout-site"] = getStringSetting("integrations.nightscout.siteUrl", "").trim() !== "";
snapshot["config.terminal-connections"] = loadConnections().length;
snapshot["config.t3code-environments"] = loadEnvironments().length;
const font = getUiFontSelection();
snapshot["config.ui-font"] =
font.kind === "bitmap"
? font.face
: `${isPreinstalledFont(font.file) ? font.file.replace(/\.ttf$/i, "") : "installed"}:${font.size}`;
for (const [storageKey, value] of Object.entries(fixedChoiceSettingValues())) {
snapshot[`setting.${storageKey}`] = value;
}
return snapshot;
}
+410
View File
@@ -0,0 +1,410 @@
/**
* Opt-in analytics, held on the phone between daily uploads to
* stats.faceclaw.org. This is the platform-free core: storage, clock, the
* data-collection setting and the network are passed in (analytics.ts wires
* them up), so the tests can drive it directly.
*
* Data is recorded at a level, and only while the data-collection setting is
* at that level or above:
* - "minimal": the app version and build, which devices are paired and in
* use, and connection-reliability counts;
* - "full": also API-key presence (never the keys) and settings chosen from
* fixed options.
* Turning the setting down discards what was kept above the new level;
* turning it off deletes everything, including the install id, and stops all
* writes and uploads.
*
* Counters accumulate in pending.json (per UTC day). Once a day the pending
* counters and a snapshot of the current state are sealed into an outbox
* report, and every outbox report is posted; each stays until the server
* accepts it, under a report id the server deduplicates on. The report
* format is stats-server/PROTOCOL.md.
*/
export type AnalyticsLevel = "minimal" | "full";
export type DataCollectionLevel = "none" | AnalyticsLevel;
export type SnapshotValue = boolean | number | string;
export type Snapshot = Record<string, SnapshotValue>;
export interface AppInfo {
version: string;
platform: string;
build: string;
}
/** Named text files in the analytics directory. */
export interface AnalyticsStorage {
read(name: string): string | null;
write(name: string, text: string): void;
remove(name: string): void;
list(): string[];
/** Deletes the directory and everything in it. */
removeAll(): void;
}
export interface AnalyticsDeps {
storage: AnalyticsStorage;
/** The data-collection setting, read fresh on every use. */
level(): DataCollectionLevel;
appInfo(): AppInfo;
now(): number;
randomId(): string;
/** The current state to report at `level` (each level's keys, not cumulative). */
snapshot(level: AnalyticsLevel): Snapshot;
/** POSTs a report body; resolves to the HTTP status, rejects on network failure. */
send(body: string): Promise<number>;
log?(message: string): void;
}
/** UTC day (YYYY-MM-DD) → counter name → count. */
type Counters = Record<string, Record<string, number>>;
type ByLevel<T> = { minimal: T; full: T };
interface PendingFile {
format: 1;
/** The app version the counts were made under; a different one seals them. */
version: string;
counters: ByLevel<Counters>;
}
interface OutboxReport {
format: 1;
reportId: string;
createdAt: string;
app: AppInfo;
counters: ByLevel<Counters>;
snapshot: ByLevel<Snapshot>;
}
interface StateFile {
format: 1;
installId: string;
lastSealAt: number;
/** When to retry after a failed upload; 0 when none failed. */
retryAt: number;
}
export const REPORT_SCHEMA_VERSION = 1;
export const DAY_MS = 24 * 60 * 60 * 1000;
export const RETRY_MS = 60 * 60 * 1000;
/** Unsent reports kept while the server is unreachable; the oldest go first. */
export const MAX_OUTBOX_REPORTS = 30;
/** Distinct counter names per day and level, so a runaway name can't grow the file. */
export const MAX_NAMES_PER_DAY = 300;
const FLUSH_DELAY_MS = 15_000;
const PENDING_FILE = "pending.json";
const STATE_FILE = "state.json";
const OUTBOX_PREFIX = "outbox-";
/** Counter names and snapshot keys the server accepts (stats-server/src/report.ts). */
const COUNTER_NAME = /^[a-z0-9][a-z0-9_.:-]{0,119}$/;
const SNAPSHOT_KEY = /^[A-Za-z0-9][A-Za-z0-9_.:-]{0,119}$/;
const MAX_SNAPSHOT_STRING = 200;
export class AnalyticsStore {
private pending: PendingFile | null = null;
private dirty = false;
private flushTimer: ReturnType<typeof setTimeout> | null = null;
private uploading = false;
/** Bumped by discardAll, so an upload in flight knows not to write its state back. */
private generation = 0;
constructor(private readonly deps: AnalyticsDeps) {}
/** Whether data at `level` is being kept. */
allows(level: AnalyticsLevel): boolean {
const setting = this.deps.level();
return setting === "full" || (setting === "minimal" && level === "minimal");
}
/** Adds `amount` to today's count of `name`. */
count(level: AnalyticsLevel, name: string, amount = 1): void {
if (!this.allows(level) || !(amount > 0)) return;
if (!COUNTER_NAME.test(name)) {
this.log(`analytics: dropping bad counter name ${name}`);
return;
}
const day = this.dayCounters(level);
if (day[name] === undefined && Object.keys(day).length >= MAX_NAMES_PER_DAY) return;
day[name] = (day[name] ?? 0) + Math.round(amount);
this.markDirty();
}
/** Records that `name` happened today (a count of 1, however often it happens). */
flag(level: AnalyticsLevel, name: string): void {
if (!this.allows(level)) return;
if (!COUNTER_NAME.test(name)) {
this.log(`analytics: dropping bad counter name ${name}`);
return;
}
const day = this.dayCounters(level);
if (day[name] !== undefined || Object.keys(day).length >= MAX_NAMES_PER_DAY) return;
day[name] = 1;
this.markDirty();
}
/**
* Brings the files in line with the setting: off deletes everything,
* minimal drops what was kept for full. Called at start and whenever the
* setting changes.
*/
applyLevel(): void {
const setting = this.deps.level();
if (setting === "none") {
this.discardAll();
return;
}
if (setting === "full") return;
const pending = this.loadPending();
if (Object.keys(pending.counters.full).length) {
pending.counters.full = {};
this.writePending(pending);
}
for (const name of this.outboxNames()) {
const report = this.readJson(name) as OutboxReport | null;
if (!report) continue;
if (Object.keys(report.counters.full).length || Object.keys(report.snapshot.full).length) {
report.counters.full = {};
report.snapshot.full = {};
this.deps.storage.write(name, JSON.stringify(report));
}
}
}
/** Writes pending counts now, if any changed. */
flush(): void {
if (this.flushTimer !== null) {
clearTimeout(this.flushTimer);
this.flushTimer = null;
}
if (!this.dirty || !this.pending) return;
this.dirty = false;
if (this.deps.level() === "none") {
this.discardAll();
return;
}
this.writePending(this.pending);
}
/**
* The daily upload: once a day, seal the pending counts into a report and
* post every unsent report; after a failure, retry hourly. Does nothing,
* and makes no connection, while the setting is off.
*/
async uploadIfDue(): Promise<void> {
if (this.uploading) return;
if (this.deps.level() === "none") {
this.discardAll();
return;
}
this.uploading = true;
const generation = this.generation;
try {
const now = this.deps.now();
const state = this.loadState();
let sealed = false;
// A clock set backwards shouldn't stall the schedule until it catches up.
if (now - state.lastSealAt >= DAY_MS || now < state.lastSealAt) {
this.seal();
state.lastSealAt = now;
this.writeState(state);
sealed = true;
}
if (!sealed && !(state.retryAt > 0 && now >= state.retryAt)) return;
for (const name of this.outboxNames()) {
const report = this.readJson(name) as OutboxReport | null;
if (!report) {
this.deps.storage.remove(name);
continue;
}
let status: number | null;
try {
status = await this.deps.send(this.reportBody(report, state.installId));
} catch (error) {
this.log(`analytics: upload failed: ${error}`);
status = null;
}
// The setting may have been turned off (and maybe on again) while the
// request was out; then everything this upload knew about is gone.
if (this.deps.level() === "none") {
this.discardAll();
return;
}
if (this.generation !== generation) return;
if (status !== null && status >= 200 && status < 300) {
this.deps.storage.remove(name);
} else if (status === 400 || status === 413) {
// Rejected as malformed or too large: sending it again won't help.
// (Anything else, like a 404 from a misconfigured server, is retried.)
this.log(`analytics: server rejected report ${report.reportId} (HTTP ${status})`);
this.deps.storage.remove(name);
} else {
state.retryAt = now + RETRY_MS;
this.writeState(state);
return;
}
}
if (state.retryAt !== 0) {
state.retryAt = 0;
this.writeState(state);
}
} finally {
this.uploading = false;
}
}
/** Deletes everything kept, including the install id. */
discardAll(): void {
if (this.flushTimer !== null) {
clearTimeout(this.flushTimer);
this.flushTimer = null;
}
this.pending = null;
this.dirty = false;
this.generation++;
this.deps.storage.removeAll();
}
/** The JSON body posted for `report`, including only what the current setting allows. */
reportBody(report: OutboxReport, installId: string): string {
const level: AnalyticsLevel = this.deps.level() === "full" ? "full" : "minimal";
const counters: Counters = {};
for (const source of level === "full" ? [report.counters.minimal, report.counters.full] : [report.counters.minimal]) {
for (const [day, counts] of Object.entries(source)) {
const target = (counters[day] ??= {});
for (const [name, count] of Object.entries(counts)) target[name] = (target[name] ?? 0) + count;
}
}
return JSON.stringify({
schema: REPORT_SCHEMA_VERSION,
reportId: report.reportId,
installId,
level,
createdAt: report.createdAt,
app: report.app,
counters,
snapshot: level === "full" ? { ...report.snapshot.minimal, ...report.snapshot.full } : report.snapshot.minimal,
});
}
private dayCounters(level: AnalyticsLevel): Record<string, number> {
const day = new Date(this.deps.now()).toISOString().slice(0, 10);
return (this.loadPending().counters[level][day] ??= {});
}
private markDirty(): void {
this.dirty = true;
this.flushTimer ??= setTimeout(() => {
this.flushTimer = null;
this.flush();
}, FLUSH_DELAY_MS);
}
private loadPending(): PendingFile {
if (this.pending) return this.pending;
const version = this.deps.appInfo().version;
const stored = this.readJson(PENDING_FILE) as PendingFile | null;
if (stored && stored.format === 1 && stored.counters?.minimal && stored.counters?.full) {
this.pending = stored;
if (stored.version !== version && hasCounts(stored)) {
// Counts from before an upgrade go out under the version they were made with.
this.seal();
}
}
return (this.pending ??= emptyPending(version));
}
/** Moves the pending counts, with a snapshot of the current state, into a new outbox report. */
private seal(): void {
const pending = this.loadPending();
const app = { ...this.deps.appInfo(), version: pending.version };
const report: OutboxReport = {
format: 1,
reportId: this.deps.randomId(),
createdAt: new Date(this.deps.now()).toISOString(),
app,
counters: pending.counters,
snapshot: {
minimal: this.takeSnapshot("minimal"),
full: this.allows("full") ? this.takeSnapshot("full") : {},
},
};
this.deps.storage.write(`${OUTBOX_PREFIX}${report.createdAt.replace(/[^0-9]/g, "")}-${report.reportId}.json`, JSON.stringify(report));
this.pending = emptyPending(this.deps.appInfo().version);
this.dirty = false;
this.writePending(this.pending);
const outbox = this.outboxNames();
for (const name of outbox.slice(0, Math.max(0, outbox.length - MAX_OUTBOX_REPORTS))) this.deps.storage.remove(name);
}
private takeSnapshot(level: AnalyticsLevel): Snapshot {
const snapshot: Snapshot = {};
let entries: [string, SnapshotValue][];
try {
entries = Object.entries(this.deps.snapshot(level));
} catch (error) {
this.log(`analytics: ${level} snapshot failed: ${error}`);
return snapshot;
}
for (const [key, value] of entries) {
const ok =
SNAPSHOT_KEY.test(key) &&
(typeof value === "boolean" ||
(typeof value === "number" && Number.isFinite(value)) ||
(typeof value === "string" && value.length <= MAX_SNAPSHOT_STRING));
if (ok) snapshot[key] = value;
else this.log(`analytics: dropping bad snapshot entry ${key}`);
}
return snapshot;
}
/** Outbox file names, oldest first (the names start with the seal time). */
private outboxNames(): string[] {
return this.deps.storage
.list()
.filter((name) => name.startsWith(OUTBOX_PREFIX))
.sort();
}
private loadState(): StateFile {
const stored = this.readJson(STATE_FILE) as StateFile | null;
if (stored && stored.format === 1 && typeof stored.installId === "string") return stored;
const state: StateFile = { format: 1, installId: this.deps.randomId(), lastSealAt: 0, retryAt: 0 };
this.writeState(state);
return state;
}
private writeState(state: StateFile): void {
this.deps.storage.write(STATE_FILE, JSON.stringify(state));
}
private writePending(pending: PendingFile): void {
this.deps.storage.write(PENDING_FILE, JSON.stringify(pending));
}
private readJson(name: string): unknown {
const text = this.deps.storage.read(name);
if (text === null) return null;
try {
return JSON.parse(text);
} catch {
this.log(`analytics: discarding unreadable ${name}`);
this.deps.storage.remove(name);
return null;
}
}
private log(message: string): void {
this.deps.log?.(message);
}
}
function emptyPending(version: string): PendingFile {
return { format: 1, version, counters: { minimal: {}, full: {} } };
}
function hasCounts(pending: PendingFile): boolean {
return Object.keys(pending.counters.minimal).length > 0 || Object.keys(pending.counters.full).length > 0;
}
+182
View File
@@ -0,0 +1,182 @@
/**
* The app's analytics API. What is collected, when it's kept and when it's
* sent is described in analytics-store.ts; the snapshot providers (devices,
* API-key presence, settings) are in analytics-snapshots.ts.
*
* Record from the main thread only. The store is created by startAnalytics()
* at boot, which app workers never run, so calls made in a worker are
* dropped. Kotlin-side connection events go through ConnectionCounters
* (drained here) instead.
*/
import { File, Folder, knownFolders, path as fsPath } from "@nativescript/core";
import { buildKind } from "../native/build-info";
import { drainConnectionCounters } from "../native/connection-counters";
import { onSettingsStoreChanged } from "../native/settings-store";
import { DATA_COLLECTION_KEY, dataCollectionSetting } from "../ui/dashboard-settings";
import { fetchTextWithUserAgent } from "../util/http";
import { FACECLAW_VERSION } from "../version";
import {
AnalyticsStore,
type AnalyticsLevel,
type AnalyticsStorage,
type Snapshot,
} from "./analytics-store";
export type { AnalyticsLevel, Snapshot } from "./analytics-store";
declare const global: any;
const REPORT_URL = "https://stats.faceclaw.org/v1/report";
const REQUEST_TIMEOUT_MS = 30_000;
/** How often to see whether the daily upload is due (it usually isn't). */
const UPLOAD_CHECK_INTERVAL_MS = 60 * 60 * 1000;
/** The first check, a little after boot or after analytics is turned on. */
const FIRST_UPLOAD_CHECK_DELAY_MS = 2 * 60 * 1000;
/** How often the Kotlin connection counts are moved into the store. */
const DRAIN_INTERVAL_MS = 5 * 60 * 1000;
let store: AnalyticsStore | null = null;
let firstCheckTimer: ReturnType<typeof setTimeout> | null = null;
const snapshotProviders: { level: AnalyticsLevel; provide: () => Snapshot }[] = [];
/**
* Adds `amount` to today's count of `name`, if the data-collection setting
* includes `level`. Names are lowercase, dot-separated (e.g. "g2.connect").
*/
export function countAnalyticsEvent(level: AnalyticsLevel, name: string, amount = 1): void {
store?.count(level, name, amount);
}
/** Records that `name` happened today: a count of 1 however often it happens. */
export function flagAnalyticsEvent(level: AnalyticsLevel, name: string): void {
store?.flag(level, name);
}
/**
* Adds state to report with each daily upload, when the setting includes
* `level`. Called when the report is sealed; keys must be namespaced like
* "device.g2.paired", and values booleans, numbers or short strings.
*/
export function registerAnalyticsSnapshot(level: AnalyticsLevel, provide: () => Snapshot): void {
snapshotProviders.push({ level, provide });
}
/** Starts the store, the daily upload schedule and the counter drain. Main thread, once, at boot. */
export function startAnalytics(): void {
if (store) return;
const analytics = new AnalyticsStore({
storage: createStorage(),
level: () => dataCollectionSetting.get(),
appInfo: () => ({ version: FACECLAW_VERSION, platform: global.isIOS ? "ios" : "android", build: buildKind() }),
now: () => Date.now(),
randomId: randomUuid,
snapshot: collectSnapshot,
send: sendReport,
log: (message) => console.log(message),
});
store = analytics;
analytics.applyLevel();
onSettingsStoreChanged((key) => {
if (key !== DATA_COLLECTION_KEY) return;
analytics.applyLevel();
if (dataCollectionSetting.get() !== "none") scheduleFirstCheck(analytics);
});
setInterval(() => drainKotlinCounters(analytics), DRAIN_INTERVAL_MS);
setInterval(() => void checkUpload(analytics), UPLOAD_CHECK_INTERVAL_MS);
scheduleFirstCheck(analytics);
}
function scheduleFirstCheck(analytics: AnalyticsStore): void {
if (firstCheckTimer !== null) clearTimeout(firstCheckTimer);
firstCheckTimer = setTimeout(() => {
firstCheckTimer = null;
void checkUpload(analytics);
}, FIRST_UPLOAD_CHECK_DELAY_MS);
}
async function checkUpload(analytics: AnalyticsStore): Promise<void> {
drainKotlinCounters(analytics);
try {
await analytics.uploadIfDue();
} catch (error) {
console.warn(`analytics: upload check failed: ${error}`);
}
}
function drainKotlinCounters(analytics: AnalyticsStore): void {
let counts: Record<string, number>;
try {
counts = drainConnectionCounters();
} catch (error) {
console.warn(`analytics: could not read connection counters: ${error}`);
return;
}
// Drained either way, so counts from while analytics was off don't pile up.
for (const [name, count] of Object.entries(counts)) analytics.count("minimal", name, count);
analytics.flush();
}
function collectSnapshot(level: AnalyticsLevel): Snapshot {
const snapshot: Snapshot = {};
for (const provider of snapshotProviders) {
if (provider.level !== level) continue;
try {
Object.assign(snapshot, provider.provide());
} catch (error) {
console.warn(`analytics: snapshot provider failed: ${error}`);
}
}
return snapshot;
}
async function sendReport(body: string): Promise<number> {
let timeout: ReturnType<typeof setTimeout> | undefined;
const timedOut = new Promise<never>((_resolve, reject) => {
timeout = setTimeout(() => reject(new Error("timed out")), REQUEST_TIMEOUT_MS);
});
try {
const response = await Promise.race([
fetchTextWithUserAgent(REPORT_URL, { method: "POST", headers: { "Content-Type": "application/json" }, body }),
timedOut,
]);
return response.status;
} finally {
clearTimeout(timeout);
}
}
/**
* Files in an `analytics` directory: the app's private files directory on
* Android; on iOS, Library rather than Documents, which the Files app shows.
*/
function createStorage(): AnalyticsStorage {
const base = global.isIOS ? knownFolders.ios.library().path : knownFolders.documents().path;
const dir = fsPath.join(base, "analytics");
const filePath = (name: string) => fsPath.join(dir, name);
return {
read: (name) => (File.exists(filePath(name)) ? File.fromPath(filePath(name)).readTextSync() : null),
write: (name, text) => {
Folder.fromPath(dir);
File.fromPath(filePath(name)).writeTextSync(text);
},
remove: (name) => {
if (File.exists(filePath(name))) File.fromPath(filePath(name)).removeSync();
},
list: () => (Folder.exists(dir) ? Folder.fromPath(dir).getEntitiesSync().map((entity) => entity.name) : []),
removeAll: () => {
if (Folder.exists(dir)) Folder.fromPath(dir).removeSync();
},
};
}
function randomUuid(): string {
const bytes = new Uint8Array(16);
const crypto = (globalThis as { crypto?: { getRandomValues?: (array: Uint8Array) => Uint8Array } }).crypto;
if (crypto?.getRandomValues) crypto.getRandomValues(bytes);
else for (let index = 0; index < bytes.length; index++) bytes[index] = Math.floor(Math.random() * 256);
bytes[6] = (bytes[6]! & 0x0f) | 0x40;
bytes[8] = (bytes[8]! & 0x3f) | 0x80;
const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
}
+2
View File
@@ -26,6 +26,7 @@ import developerApp from "./developer";
import evenhubApp from "./evenhub";
import glanceboardApp from "./glanceboard";
import settingsApp from "./settings";
import onboardingApp from "./onboarding";
/**
* Every app, in launcher-grid order (the launcher itself is first but hidden
@@ -60,4 +61,5 @@ export const ALL_APPS: readonly AppDefinition[] = [
evenhubApp,
glanceboardApp,
settingsApp,
onboardingApp,
];
@@ -0,0 +1,75 @@
import type { GrayImage } from "../../graphics/image";
import { truncateText } from "../../graphics/textwrap";
import { dataCollectionSetting, dataCollectionSummary, type DataCollectionLevel } from "../../ui/dashboard-settings";
import type { InputEvent } from "../../ui/gestures";
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
import { LIST_ROW_TEXT_INSET, listRowHeight } from "../../ui/metrics";
import { onboardingFont } from "./onboarding-fonts";
import { drawPageHeading, PAGE_X, type OnboardingNav, type OnboardingPage } from "./onboarding-page";
const CONTENT_GAP = 10;
const BOTTOM_MARGIN = 8;
const LABEL_GAP = 24;
/** None / Minimal / Full, each with a one-line summary; a tap saves the selected level. */
export class DataCollectionPage implements OnboardingPage {
/** Width of the level-name column, measured each paint. */
private labelColumnWidth = 0;
private readonly menu = new Menu<DataCollectionLevel>({
items: dataCollectionSetting.values,
wrap: false,
rowGap: 2,
highlight: { radius: 8 },
getHeight: () => listRowHeight(onboardingFont("body")),
draw: (args) => this.drawRow(args),
});
enter(): void {
this.menu.select(dataCollectionSetting.values.indexOf(dataCollectionSetting.get()));
}
paint(image: GrayImage, focused: boolean): void {
const top = drawPageHeading(
image,
"Data collection",
"Choose what Faceclaw may send its developers to help fix problems and improve the app. " +
"You can change this later in Settings > Privacy.",
3,
) + CONTENT_GAP;
const body = onboardingFont("body");
this.labelColumnWidth =
Math.max(...dataCollectionSetting.values.map((level) => body.measureText(dataCollectionSetting.displayValue(level)))) +
LABEL_GAP;
this.menu.paint(
image,
{ x: PAGE_X - 10, y: top, width: image.width - 2 * (PAGE_X - 10), height: image.height - top - BOTTOM_MARGIN },
focused,
);
}
handleInput(event: InputEvent, nav: OnboardingNav): void {
switch (event.type) {
case "scroll-up":
case "scroll-down":
void this.menu.handleInput(event);
return;
case "click": {
const level = this.menu.selectedItem;
if (level !== null) dataCollectionSetting.set(level);
nav.next();
return;
}
default:
return;
}
}
private drawRow({ image, item: level, x, y, width, height, selected }: MenuDrawArgs<DataCollectionLevel>): void {
const body = onboardingFont("body");
const detail = onboardingFont("detail");
image.drawText(body, x + 10, y + LIST_ROW_TEXT_INSET, dataCollectionSetting.displayValue(level), selected ? 255 : 200);
const summaryX = x + 10 + this.labelColumnWidth;
const summary = truncateText(detail, dataCollectionSummary(level), x + width - 10 - summaryX);
image.drawText(detail, summaryX, y + Math.round((height - detail.lineHeight) / 2), summary, selected ? 210 : 150);
}
}
+17
View File
@@ -0,0 +1,17 @@
import { type AppDefinition } from "../app-definition";
import { createOnboardingAppWindow, ONBOARDING_SURFACE_ID, ONBOARDING_WINDOW_ID } from "./onboarding-app";
import { ONBOARDING_APP_ID } from "./onboarding-progress";
/**
* First-run setup on the glasses: welcome, text size, data-collection
* preference. The connection controllers open it on their own when it is
* due (claimGlassesOnboardingAutoLaunch); the launcher entry reruns it.
*/
const onboardingApp: AppDefinition = {
appId: ONBOARDING_APP_ID,
title: "Onboarding",
icon: "sparkles",
launch: (ctx) => ctx.launchInProcessApp(ONBOARDING_WINDOW_ID, ONBOARDING_SURFACE_ID, createOnboardingAppWindow),
};
export default onboardingApp;
+98
View File
@@ -0,0 +1,98 @@
import { GrayImage } from "../../graphics/image";
import type { InputEvent } from "../../ui/gestures";
import type { Layer, LayerActions, LayerContext } from "../../ui/layers";
import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
import { shell } from "../../ui/shell/shell";
import { findSoundEffect, playSoundEffect } from "../../ui/sound-effects";
import { DataCollectionPage } from "./data-collection-page";
import { markGlassesOnboardingFinished, ONBOARDING_APP_ID } from "./onboarding-progress";
import type { OnboardingPage } from "./onboarding-page";
import { TextSizePage } from "./text-size-page";
import { WelcomePage } from "./welcome-page";
export const ONBOARDING_WINDOW_ID = "onboarding";
export const ONBOARDING_SURFACE_ID = "window:onboarding";
/**
* The on-glasses onboarding: a fixed sequence of pages, opened automatically
* on the first connection after phone-side setup and after each upgrade
* (see onboarding-progress.ts), and from the launcher any time. Finishing
* the last page records this version as onboarded and closes the window;
* closing it early leaves onboarding due.
*/
export function createOnboardingAppWindow(options: InProcessAppOptions): InProcessWindow {
const pages: OnboardingPage[] = [new WelcomePage(), new TextSizePage(), new DataCollectionPage()];
const app = createInProcessWindow({
appId: ONBOARDING_APP_ID,
windowId: ONBOARDING_WINDOW_ID,
title: "Onboarding",
iconLetter: "On",
icon: "sparkles",
closeable: true,
actions: options.actions,
baseLayer: new OnboardingLayer(pages, () => {
markGlassesOnboardingFinished();
shell.closeWindow(ONBOARDING_WINDOW_ID);
}),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
reconfigureSurface: options.reconfigureSurface,
onClosed: options.onClosed,
});
playWelcomeSound(options.actions);
return app;
}
/** The celebratory jingle that greets the welcome page (formerly played on the first connection). */
function playWelcomeSound(actions: LayerActions): void {
const effect = findSoundEffect("questcomplete");
if (!effect) return;
playSoundEffect(
effect,
(payload) => actions.playBuzzerSequence(payload),
(ms) => new Promise((resolve) => setTimeout(resolve, ms)),
).catch((error) => console.warn(`onboarding welcome sound failed: ${error}`));
}
/**
* Hosts the current page. Double-click steps back a page, or leaves the app
* from the first one (the standard yield to the app switcher); every other
* gesture goes to the page.
*/
class OnboardingLayer implements Layer {
private index = 0;
constructor(
private readonly pages: readonly OnboardingPage[],
private readonly finish: () => void,
) {
pages[0]!.enter?.();
}
paint(ctx: LayerContext): GrayImage {
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
this.pages[this.index]!.paint(image, ctx.stack.isFocused());
return image;
}
async handleInput(event: InputEvent): Promise<void> {
if (event.type === "double-click") {
if (this.index === 0) shell.yieldFocusToSidebar();
else this.show(this.index - 1);
return;
}
await this.pages[this.index]!.handleInput(event, { next: () => this.next() });
}
private next(): void {
if (this.index + 1 < this.pages.length) this.show(this.index + 1);
else this.finish();
}
private show(index: number): void {
this.index = index;
this.pages[index]!.enter?.();
}
}
+38
View File
@@ -0,0 +1,38 @@
/**
* The onboarding's own fonts: bundled Roboto at fixed sizes, deliberately
* not the user's UI font, so the pages are readable before (and while) the
* text size is being chosen. Bitmap Terminus stands in where TTF rendering
* is unavailable.
*/
import { getFont, getTerminusFont } from "../../graphics/bdffont";
import type { UiFont } from "../../graphics/image";
import { ensurePreinstalledFonts, installedFontPath } from "../../graphics/installed-fonts";
import { TtfFont } from "../../graphics/ttf-font";
export type OnboardingFontRole = "hero" | "title" | "body" | "detail";
const ROLE_FONTS: Record<OnboardingFontRole, { file: string; size: number; fallback: () => UiFont }> = {
/** The welcome page's "Faceclaw". */
hero: { file: "Roboto-Regular.ttf", size: 44, fallback: () => getFont("terminus32") },
/** Page titles. */
title: { file: "Roboto-Regular.ttf", size: 26, fallback: () => getFont("terminus24") },
/** Prompts and choices. */
body: { file: "Roboto-Regular.ttf", size: 20, fallback: () => getTerminusFont(20, false) ?? getFont("terminus16") },
/** Explanations under titles and beside choices. */
detail: { file: "Roboto-Regular.ttf", size: 16, fallback: () => getFont("terminus16") },
};
let fontsEnsured = false;
export function onboardingFont(role: OnboardingFontRole): UiFont {
if (!fontsEnsured) {
fontsEnsured = true;
try {
ensurePreinstalledFonts(); // onboarding may be the first thing to draw TTF text
} catch (error) {
console.warn(`onboarding font preinstall failed: ${error}`);
}
}
const spec = ROLE_FONTS[role];
return TtfFont.load(installedFontPath(spec.file), spec.size) ?? spec.fallback();
}
+48
View File
@@ -0,0 +1,48 @@
import type { GrayImage } from "../../graphics/image";
import { wrapText } from "../../graphics/textwrap";
import type { InputEvent } from "../../ui/gestures";
import { onboardingFont } from "./onboarding-fonts";
/** Page-to-flow navigation, handed to a page's input handler. */
export type OnboardingNav = {
/** Go to the next page, or finish onboarding from the last one. */
next: () => void;
};
/**
* One onboarding step. The flow owns the page list, back navigation
* (double-click) and finishing; a page paints itself into the window's
* viewport and handles every other gesture.
*/
export interface OnboardingPage {
/** The page became current, going forward or back. */
enter?(): void;
paint(image: GrayImage, focused: boolean): void;
handleInput(event: InputEvent, nav: OnboardingNav): void | Promise<void>;
}
/** Left margin and title position shared by the pages (the UI-wide (18, 10) title spot). */
export const PAGE_X = 18;
export const TITLE_Y = 10;
/**
* Draw a page's title and its wrapped explanation (at most `maxLines`
* lines) in the onboarding fonts. Returns the y just below the explanation.
*/
export function drawPageHeading(
image: GrayImage,
title: string,
explanation: string,
maxLines = 2,
): number {
const titleFont = onboardingFont("title");
const detailFont = onboardingFont("detail");
image.drawText(titleFont, PAGE_X, TITLE_Y, title, 230);
let y = TITLE_Y + titleFont.lineHeight + 2;
const lines = wrapText(detailFont, explanation, image.width - 2 * PAGE_X).slice(0, maxLines);
for (const line of lines) {
image.drawText(detailFont, PAGE_X, y, line, 160);
y += detailFont.lineHeight;
}
return y;
}
@@ -0,0 +1,34 @@
/**
* Whether the on-glasses onboarding is due: it is recorded as finished per
* Faceclaw version, so it runs on the first connection after phone-side
* setup and again after any upgrade. The key shares the phone onboarding's
* "onboarding." prefix so scripts/resetOnboarding.sh clears it too.
*/
import { getStringSetting, setStringSetting } from "../../native/settings-store";
import { FACECLAW_VERSION } from "../../version";
export const ONBOARDING_APP_ID = "onboarding";
const FINISHED_VERSION_KEY = "onboarding.glassesFinishedVersion";
export function isGlassesOnboardingDue(): boolean {
return getStringSetting(FINISHED_VERSION_KEY, "") !== FACECLAW_VERSION;
}
export function markGlassesOnboardingFinished(): void {
setStringSetting(FINISHED_VERSION_KEY, FACECLAW_VERSION);
}
let autoLaunchChecked = false;
/**
* Asked by the connection controllers on each rendered frame (the session
* is warm by then, so the onboarding's welcome jingle won't be dropped).
* True at most once per run, and only when onboarding is due: closing it
* unfinished brings it back on the next app start, not on every reconnect.
*/
export function claimGlassesOnboardingAutoLaunch(): boolean {
if (autoLaunchChecked) return false;
autoLaunchChecked = true;
return isGlassesOnboardingDue();
}
+159
View File
@@ -0,0 +1,159 @@
import { GrayImage, type UiFont } from "../../graphics/image";
import { installedFontPath } from "../../graphics/installed-fonts";
import { wrapText } from "../../graphics/textwrap";
import { TtfFont } from "../../graphics/ttf-font";
import {
fontSelectionLabel,
getDefaultSmallFont,
getUiFontSelection,
setUiFontSelection,
uiFontSizeAllowed,
} from "../../graphics/ui-fonts";
import { FONT_SIZE_CHOICES } from "../../ui/font-picker";
import type { InputEvent } from "../../ui/gestures";
import { Menu, MENU_HIGHLIGHT_FILL, MENU_HIGHLIGHT_STROKE, type MenuBox, type MenuDrawArgs } from "../../ui/menu-core";
import { LIST_ROW_TEXT_INSET, listRowHeight } from "../../ui/metrics";
import { onboardingFont } from "./onboarding-fonts";
import { drawPageHeading, PAGE_X, type OnboardingNav, type OnboardingPage } from "./onboarding-page";
const CONTENT_GAP = 10;
const BOTTOM_MARGIN = 8;
const SIZE_COLUMN_WIDTH = 84;
const COLUMN_GAP = 12;
const PREVIEW_PADDING = 10;
const ROW_GAP = 2;
const PREVIEW_TEXT = "This is how text will look on your glasses.";
/** Drawn while they fit, so smaller sizes visibly show more rows. */
const PREVIEW_ROWS = ["Timers", "Weather", "Music", "Settings", "Files"];
/** The UI font's face, as far as this page is concerned: resizable (TTF) or not (bitmap). */
type Face =
| { kind: "sized"; file: string; path: string; sizes: number[] }
| { kind: "fixed"; label: string };
/**
* The UI font's size, over a preview of list rows and body text in the
* chosen size. The face and weight stay as they are (they live in the
* Settings font picker); a bitmap face has only one size, so the page just
* says so. The size is saved on tap, not while scrolling.
*/
export class TextSizePage implements OnboardingPage {
private face: Face = { kind: "fixed", label: "Terminus" };
private readonly sizeMenu = new Menu<number>({
wrap: false,
rowGap: ROW_GAP,
highlight: { radius: 8 },
getHeight: () => listRowHeight(onboardingFont("body")),
draw: (args) => this.drawSizeRow(args),
});
enter(): void {
// Re-read on every visit: the face may have changed in Settings meanwhile.
const selection = getUiFontSelection();
if (selection.kind === "ttf") {
const path = installedFontPath(selection.file);
const sizes = FONT_SIZE_CHOICES.filter((size) => uiFontSizeAllowed(path, size));
if (sizes.length) {
this.face = { kind: "sized", file: selection.file, path, sizes };
this.sizeMenu.setItems(sizes, nearestIndex(sizes, selection.size));
return;
}
}
// An unloadable TTF selection renders as Terminus, so call it that.
this.face = { kind: "fixed", label: selection.kind === "bitmap" ? fontSelectionLabel(selection) : "Terminus" };
}
paint(image: GrayImage, focused: boolean): void {
if (this.face.kind === "fixed") {
const top = drawPageHeading(
image,
"Text size",
`Your UI font, ${this.face.label}, comes in one size. To choose a size, pick a different font in ` +
"Settings > Customization. Tap to continue.",
3,
) + CONTENT_GAP;
drawPreview(image, getDefaultSmallFont(), {
x: PAGE_X - 10,
y: top,
width: image.width - 2 * (PAGE_X - 10),
height: image.height - top - BOTTOM_MARGIN,
});
return;
}
const top = drawPageHeading(
image,
"Text size",
"Swipe to choose a size, then tap to continue. The typeface and weight can be changed in Settings.",
) + CONTENT_GAP;
const height = image.height - top - BOTTOM_MARGIN;
const menuBox = { x: PAGE_X - 10, y: top, width: SIZE_COLUMN_WIDTH, height };
this.sizeMenu.paint(image, menuBox, focused);
const previewX = menuBox.x + menuBox.width + COLUMN_GAP;
const size = this.sizeMenu.selectedItem;
const font = (size !== null ? TtfFont.load(this.face.path, size) : null) ?? getDefaultSmallFont();
drawPreview(image, font, { x: previewX, y: top, width: image.width - (PAGE_X - 10) - previewX, height });
}
handleInput(event: InputEvent, nav: OnboardingNav): void {
switch (event.type) {
case "scroll-up":
case "scroll-down":
if (this.face.kind === "sized") void this.sizeMenu.handleInput(event);
return;
case "click":
this.save();
nav.next();
return;
default:
return;
}
}
private save(): void {
const size = this.sizeMenu.selectedItem;
if (this.face.kind !== "sized" || size === null) return;
const current = getUiFontSelection();
if (current.kind === "ttf" && current.file === this.face.file && current.size === size) return;
setUiFontSelection({ kind: "ttf", file: this.face.file, size });
}
private drawSizeRow({ image, item: size, x, y, selected }: MenuDrawArgs<number>): void {
image.drawText(onboardingFont("body"), x + 10, y + LIST_ROW_TEXT_INSET, `${size} px`, selected ? 255 : 190);
}
}
/** A framed sample in `font`: a sentence, then menu rows with the first one selected, as many as fit. */
function drawPreview(image: GrayImage, font: UiFont, box: MenuBox): void {
image.drawRoundedRect(box.x, box.y, box.width, box.height, 60, 8);
const left = box.x + PREVIEW_PADDING;
const width = box.width - 2 * PREVIEW_PADDING;
const bottom = box.y + box.height - PREVIEW_PADDING;
let y = box.y + PREVIEW_PADDING;
for (const line of wrapText(font, PREVIEW_TEXT, width)) {
if (y + font.lineHeight > bottom) return;
image.drawText(font, left, y, line, 220);
y += font.lineHeight;
}
y += PREVIEW_PADDING;
const rowHeight = listRowHeight(font);
for (let index = 0; index < PREVIEW_ROWS.length; index++) {
if (y + rowHeight - ROW_GAP > bottom) return;
const label = PREVIEW_ROWS[index]!;
if (index === 0) {
const row = new GrayImage(width, rowHeight - ROW_GAP, 0);
row.drawText(font, 10, LIST_ROW_TEXT_INSET, label, 255);
image.drawMenuSelection(row, left, y, MENU_HIGHLIGHT_FILL, MENU_HIGHLIGHT_STROKE, 8, 0);
} else {
image.drawText(font, left + 10, y + LIST_ROW_TEXT_INSET, label, 200);
}
y += rowHeight;
}
}
function nearestIndex(values: readonly number[], target: number): number {
let best = 0;
for (let index = 1; index < values.length; index++) {
if (Math.abs(values[index]! - target) < Math.abs(values[best]! - target)) best = index;
}
return best;
}
+51
View File
@@ -0,0 +1,51 @@
import type { GrayImage, UiFont } from "../../graphics/image";
import { getFullLogo } from "../../graphics/logo";
import { wrapText } from "../../graphics/textwrap";
import type { InputEvent } from "../../ui/gestures";
import { FACECLAW_VERSION } from "../../version";
import { onboardingFont } from "./onboarding-fonts";
import { PAGE_X, type OnboardingNav, type OnboardingPage } from "./onboarding-page";
const LOGO_MAX_HEIGHT = 180;
const LOGO_MARGIN = 12;
const LOGO_TEXT_GAP = 28;
const PROMPT_GAP = 22;
const PROMPT = "Tap the ring or touchpad to continue";
type TextLine = { font: UiFont; text: string; value: number; spaceBefore: number };
/** The splash: logo beside the name, version and how to go on, centred as one group. */
export class WelcomePage implements OnboardingPage {
paint(image: GrayImage): void {
const { width, height } = image;
const logo = getFullLogo(Math.min(LOGO_MAX_HEIGHT, height - 2 * LOGO_MARGIN));
const logoWidth = logo ? logo.width + LOGO_TEXT_GAP : 0;
const body = onboardingFont("body");
const lines: TextLine[] = [
{ font: onboardingFont("hero"), text: "Faceclaw", value: 240, spaceBefore: 0 },
{ font: body, text: `Version ${FACECLAW_VERSION}`, value: 150, spaceBefore: 0 },
...wrapText(body, PROMPT, width - 2 * PAGE_X - logoWidth).map((text, index) => ({
font: body,
text,
value: 215,
spaceBefore: index === 0 ? PROMPT_GAP : 0,
})),
];
const textWidth = Math.max(...lines.map((line) => line.font.measureText(line.text)));
const textHeight = lines.reduce((total, line) => total + line.spaceBefore + line.font.lineHeight, 0);
const left = Math.max(PAGE_X, Math.round((width - logoWidth - textWidth) / 2));
if (logo) {
image.bitBlt(logo, left, Math.round((height - logo.height) / 2), { transparentZero: true });
}
let y = Math.round((height - textHeight) / 2);
for (const line of lines) {
y += line.spaceBefore;
image.drawText(line.font, left + logoWidth, y, line.text, line.value);
y += line.font.lineHeight;
}
}
handleInput(event: InputEvent, nav: OnboardingNav): void {
if (event.type === "click") nav.next();
}
}
+5
View File
@@ -6,5 +6,10 @@ import { registerPhoneRotation } from './native/phone-rotation'
installNativeUserAgent()
registerShareIntentHandler()
registerPhoneRotation()
// Opt-in analytics (app/analytics/), started once the app is up rather than
// loading its imports on the boot path.
Application.on(Application.launchEvent, () => {
setTimeout(() => require('./analytics/analytics-sources').startAppAnalytics(), 0)
})
Application.run({ moduleName: 'app-root' })
+4
View File
@@ -4,6 +4,10 @@ declare const FaceclawConfigPort: any
// Apply transferred preferences before settings getters or app workers load.
FaceclawConfigPort.processPendingRequest()
require('./phone-ui/onboarding-register.ios')
// Opt-in analytics (app/analytics/), started once the app is up.
Application.on(Application.launchEvent, () => {
setTimeout(() => require('./analytics/analytics-sources').startAppAnalytics(), 0)
})
const { hasCompletedOnboarding } = require('./phone-ui/onboarding-state') as typeof import('./phone-ui/onboarding-state')
Application.run({ create: () => {
const frame = new Frame()
+8 -26
View File
@@ -45,10 +45,10 @@ const MIRROR_TOUCH_GESTURES: Record<Exclude<MirrorTouchKind, "tap">, WearRemoteI
"swipe-left": "swipe-left",
"swipe-right": "swipe-right",
};
import { findSoundEffect, playSoundEffect } from "../ui/sound-effects";
import { GlanceHost } from "./glance-host";
import { type GlanceEvent } from "./glance-state";
import { isPreviewOnlyMode, isWelcomeSoundPending, setWelcomeSoundPending } from "../phone-ui/onboarding-state";
import { isPreviewOnlyMode } from "../phone-ui/onboarding-state";
import { claimGlassesOnboardingAutoLaunch, ONBOARDING_APP_ID } from "../apps/onboarding/onboarding-progress";
import { beginRenderPass, endRenderPass } from "../util/render-freshness";
import { voiceControlBridge } from "../native/voice-control";
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image";
@@ -278,9 +278,6 @@ class DashboardController {
// drop-off right after a low reading can be explained as a flat battery.
private lastHeadsetBattery: number | null = null;
private readonly listeners = new Set<DashboardListener>();
// Set at connect time from the persisted flag; the one-time post-onboarding
// welcome sound plays on the first rendered frame (proof the session is warm).
private welcomeSoundArmed = false;
private communicator: FaceclawCommunicatorBridge | null = null;
// Headless stand-in for the compositor when no glasses are paired
@@ -1413,7 +1410,6 @@ class DashboardController {
this.teardownPreviewDisplay();
this.lastInput = "waiting...";
this.lastSys = "none yet";
this.welcomeSoundArmed = isWelcomeSoundPending();
this.firmwareWarningMessage = "";
this.glassesWorn = null;
this.wakeAfterChargingReconnect = false;
@@ -1580,11 +1576,12 @@ class DashboardController {
// parked by the previous session (or by an EvenHub suspend) resumes.
this.resumeVoiceCapture();
// A rendered frame means the session is warmed up (fixedLayoutCreated),
// so the buzzer won't be dropped. Play the one-time welcome sound now.
if (this.welcomeSoundArmed) {
this.welcomeSoundArmed = false;
setWelcomeSoundPending(false);
void this.playWelcomeSound();
// so the onboarding's welcome jingle won't be dropped. Open the
// on-glasses onboarding if this version hasn't been through it.
if (claimGlassesOnboardingAutoLaunch()) {
this.launchApp(ONBOARDING_APP_ID).catch((error) => {
this.appendLog(`onboarding launch failed: ${this.formatError(error)}`);
});
}
}
});
@@ -2741,21 +2738,6 @@ class DashboardController {
await this.communicator.playBuzzerSequence(payload);
}
/** One-time celebratory jingle on the first connection after onboarding. */
private async playWelcomeSound(): Promise<void> {
const effect = findSoundEffect("questcomplete");
if (!effect) return;
try {
await playSoundEffect(
effect,
(payload) => this.playBuzzerSequence(payload),
(ms) => new Promise((resolve) => setTimeout(resolve, ms)),
);
} catch (error) {
this.appendLog(`welcome sound failed: ${this.formatError(error)}`);
}
}
private clearDashboardTimer(): void {
this.cancelEvenHubSuspendTimer();
if (this.shellRefreshTimer) {
+8 -12
View File
@@ -48,8 +48,7 @@ import { DISPLAY_MODE_VALUES, displayModeLabel, displayModeSetting, onAnySetting
previewColorSetting, lockScreenEnabledSetting, getBrightnessPreferences, getStringSettingById,
nightscoutSiteUrlSetting, nightscoutApiTokenSetting } from '../ui/dashboard-settings'
import type { PhoneGesture } from '../phone-ui/phone-gestures'
import { isWelcomeSoundPending, setWelcomeSoundPending } from '../phone-ui/onboarding-state'
import { findSoundEffect, playSoundEffect } from '../ui/sound-effects'
import { claimGlassesOnboardingAutoLaunch, ONBOARDING_APP_ID } from '../apps/onboarding/onboarding-progress'
const SHELL_SURFACE_ID = 'shell'
const SHELL_SURFACE_Z_ORDER = 1
@@ -681,7 +680,7 @@ export class IosPreviewController {
private emitState(): void {
if (this.state.phase !== 'connected') resetRingInputFilter()
this.pushBrightness()
this.maybePlayWelcomeSound(this.state)
this.maybeLaunchOnboarding(this.state)
if (this.state.phase !== 'connected' && this.state.phase !== 'connecting') this.glassesWorn = null
if (this.state.phase !== 'connected') iosVoiceInput.handleSessionEnded()
shell.setBatteryLevels({ headset: this.state.battery, headsetCharging: this.state.charging })
@@ -770,7 +769,7 @@ export class IosPreviewController {
if (this.communicator !== communicator) return
this.state = { ...this.state, frames: this.state.frames + 1 }
this.onActivity()
this.maybePlayWelcomeSound(this.state)
this.maybeLaunchOnboarding(this.state)
if (this.active) this.onConnectionState({ ...this.state })
this.schedulePreviewUpdate()
}),
@@ -878,16 +877,13 @@ export class IosPreviewController {
this.glance.reset()
await communicator.close().catch(error => this.logBluetooth(`Close: ${String(error)}`))
}
private maybePlayWelcomeSound(state: SessionState): void {
private maybeLaunchOnboarding(state: SessionState): void {
const firstNewFrame = state.frames > this.acknowledgedFrames
this.acknowledgedFrames = state.frames
// Match Android: wait for an acknowledged frame before consuming the jingle.
if (!firstNewFrame || state.phase !== 'connected' || !isWelcomeSoundPending()) return
setWelcomeSoundPending(false)
const effect = findSoundEffect('questcomplete')
if (effect) void playSoundEffect(effect, payload => this.actions.playBuzzerSequence(payload),
ms => new Promise(resolve => setTimeout(resolve, ms)))
.catch(error => this.logBluetooth(`Welcome sound failed: ${error}`))
// Match Android: wait for an acknowledged frame (a warm session, so the
// onboarding's welcome jingle isn't dropped) before opening it.
if (!firstNewFrame || state.phase !== 'connected' || !claimGlassesOnboardingAutoLaunch()) return
void this.launchApp(ONBOARDING_APP_ID)
}
startVoiceInput(): void { if (!this.glassesLocked) shell.startVoiceInput() }
private async prepareVoiceCapture(): Promise<boolean> {
+3
View File
@@ -23,6 +23,7 @@
* Transport is app/native/wear-bridge.ts (Java FaceclawWearBridge); the
* message shapes are documented in wear/PROTOCOL.md.
*/
import { flagAnalyticsEvent } from "../analytics/analytics";
import { type AppDefinition } from "../apps/app-definition";
import { getInstalledEvenHubApps, installedEvenHubAppId } from "../apps/evenhub/installed-apps";
import { WEAR_PATHS, wearBridge, type WearMessage } from "../native/wear-bridge";
@@ -167,6 +168,7 @@ export class WearRemote {
// with it (no placeholder).
if (changed) this.watchBatteryPollsUnanswered = 0;
if (connection.reachable) {
flagAnalyticsEvent("minimal", "device.wear.reachable");
this.publishNow(true);
this.pollWatchBattery(true);
} else if (changed) {
@@ -326,6 +328,7 @@ export class WearRemote {
return;
}
flagAnalyticsEvent("minimal", "device.wear.used");
switch (message.path) {
case WEAR_PATHS.input:
await this.handleInput(message.payload, ack);
+2
View File
@@ -95,6 +95,8 @@ export const ICON_SVGS = {
'<svg viewBox="0 0 24 24" fill="none"><path d="M14 2v6a2 2 0 0 0 .245.96l5.51 10.08A2 2 0 0 1 18 22H6a2 2 0 0 1-1.755-2.96l5.51-10.08A2 2 0 0 0 10 8V2"/><path d="M6.453 15h11.094"/><path d="M8.5 2h7"/></svg>',
wrench:
'<svg viewBox="0 0 24 24" fill="none"><path d="M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76z"/></svg>',
sparkles:
'<svg viewBox="0 0 24 24" fill="none"><path d="m12 3-1.912 5.813a2 2 0 0 1-1.275 1.275L3 12l5.813 1.912a2 2 0 0 1 1.275 1.275L12 21l1.912-5.813a2 2 0 0 1 1.275-1.275L21 12l-5.813-1.912a2 2 0 0 1-1.275-1.275L12 3Z"/><path d="M5 3v4"/><path d="M19 17v4"/><path d="M3 5h4"/><path d="M17 19h4"/></svg>',
settings:
'<svg viewBox="0 0 24 24" fill="none"><path d="M9.671 4.136a2.34 2.34 0 0 1 4.659 0 2.34 2.34 0 0 0 3.319 1.915 2.34 2.34 0 0 1 2.33 4.033 2.34 2.34 0 0 0 0 3.831 2.34 2.34 0 0 1-2.33 4.033 2.34 2.34 0 0 0-3.319 1.915 2.34 2.34 0 0 1-4.659 0 2.34 2.34 0 0 0-3.32-1.915 2.34 2.34 0 0 1-2.33-4.033 2.34 2.34 0 0 0 0-3.831A2.34 2.34 0 0 1 6.35 6.051a2.34 2.34 0 0 0 3.319-1.915"/><circle cx="12" cy="12" r="3"/></svg>',
mic:
+8 -2
View File
@@ -6,7 +6,12 @@ declare const interop: any;
const UPNG = require("upng-js");
export function loadPngAsGrayImage(path: string): GrayImage {
/**
* Decode a PNG to grayscale, premultiplied by alpha so transparency reads as
* black. With `invert`, the PNG is dark art on a light or transparent ground
* (e.g. the phone-side logo) and its ink is drawn white instead.
*/
export function loadPngAsGrayImage(path: string, options: { invert?: boolean } = {}): GrayImage {
const file = resolveFile(path);
const raw = file.readSync();
const pngBuffer = toArrayBuffer(raw);
@@ -25,7 +30,8 @@ export function loadPngAsGrayImage(path: string): GrayImage {
const g = rgba[offset + 1] ?? 0;
const b = rgba[offset + 2] ?? 0;
const a = (rgba[offset + 3] ?? 0) / 255;
const gray = Math.round((0.2126 * r + 0.7152 * g + 0.0722 * b) * a);
const luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
const gray = Math.round((options.invert ? 255 - luminance : luminance) * a);
image.pixels[y * decoded.width + x] = gray;
}
}
+5
View File
@@ -44,6 +44,11 @@ export type InstalledFont = {
monospace: boolean;
};
/** Whether `fileName` is one of the fonts the app ships, rather than one the user installed. */
export function isPreinstalledFont(fileName: string): boolean {
return (PREINSTALLED_FONT_FILES as readonly string[]).includes(fileName);
}
/** The app-internal fonts directory (created on first use). */
export function installedFontsDir(): string {
return knownFolders.documents().getFolder("fonts").path;
+54
View File
@@ -14,3 +14,57 @@ export function getDashboardLogo(): GrayImage | null {
}
return cachedDashboardLogo;
}
const FULL_LOGO_PATH = "images/faceclaw-logo.png";
let fullLogoSource: GrayImage | null | undefined;
const scaledFullLogos = new Map<number, GrayImage>();
/**
* The full Faceclaw logo (the lobster over the smiley face), white on black,
* scaled to `height` pixels tall for splash screens. The source is the phone
* UI's black-on-transparent PNG, inverted, and area-averaged down so the
* edges stay antialiased.
*/
export function getFullLogo(height: number): GrayImage | null {
const targetHeight = Math.max(1, Math.round(height));
const cached = scaledFullLogos.get(targetHeight);
if (cached) return cached;
if (fullLogoSource === undefined) {
try {
fullLogoSource = loadPngAsGrayImage(FULL_LOGO_PATH, { invert: true });
} catch (error) {
console.warn(`Could not load ${FULL_LOGO_PATH}: ${error}`);
fullLogoSource = null;
}
}
if (!fullLogoSource) return null;
const scale = Math.min(1, targetHeight / fullLogoSource.height);
const scaled = downscaleArea(
fullLogoSource,
Math.max(1, Math.round(fullLogoSource.width * scale)),
Math.max(1, Math.round(fullLogoSource.height * scale)),
);
scaledFullLogos.set(targetHeight, scaled);
return scaled;
}
/** Box-filter downscale: each output pixel averages the source pixels it covers. */
function downscaleArea(source: GrayImage, width: number, height: number): GrayImage {
if (width === source.width && height === source.height) return source;
const output = new GrayImage(width, height, 0);
for (let y = 0; y < height; y++) {
const y0 = Math.floor((y * source.height) / height);
const y1 = Math.max(y0 + 1, Math.floor(((y + 1) * source.height) / height));
for (let x = 0; x < width; x++) {
const x0 = Math.floor((x * source.width) / width);
const x1 = Math.max(x0 + 1, Math.floor(((x + 1) * source.width) / width));
let sum = 0;
for (let sy = y0; sy < y1; sy++) {
const row = sy * source.width;
for (let sx = x0; sx < x1; sx++) sum += source.pixels[row + sx]!;
}
output.pixels[y * width + x] = Math.round(sum / ((y1 - y0) * (x1 - x0)));
}
}
return output;
}
+19
View File
@@ -0,0 +1,19 @@
import type { BuildKind } from './build-info'
declare const NSBundle: any
declare const NSProcessInfo: any
let cached: BuildKind | null = null
/**
* App Store (and TestFlight) builds carry no provisioning profile; anything
* installed from Xcode or as an ad hoc build does. Simulator builds have none
* either, so they're recognized by the simulator's environment.
*/
export function buildKind(): BuildKind {
if (cached) return cached
if (NSProcessInfo.processInfo.environment.objectForKey('SIMULATOR_DEVICE_NAME')) cached = 'debug'
else if (NSBundle.mainBundle.pathForResourceOfType('embedded', 'mobileprovision')) cached = 'self-built'
else cached = 'official'
return cached
}
+54
View File
@@ -0,0 +1,54 @@
/**
* Which kind of build this is, for analytics: an official release, or one
* compiled by somebody else. Official releases are signed with the key
* scripts/release.sh uses; anything else was built by whoever installed it.
*/
import { Utils } from "@nativescript/core";
declare const android: any;
declare const java: any;
/**
* `debug` is a debuggable build (build.sh / build_and_run.sh); `self-built`
* is a release build signed with someone else's key.
*/
export type BuildKind = "official" | "self-built" | "debug" | "unknown";
/** SHA-256 of the release signing certificate (`apksigner verify --print-certs dist/Faceclaw-*.apk`). */
const OFFICIAL_SIGNING_CERT_SHA256 = "c1efe42d74f3fc4e9a13bcfc8c99e018344a55525e5b300fa960c89431e382ad";
let cached: BuildKind | null = null;
export function buildKind(): BuildKind {
cached ??= detectBuildKind();
return cached;
}
function detectBuildKind(): BuildKind {
try {
const context = Utils.android.getApplicationContext();
const packageManager = context.getPackageManager();
const PackageManager = android.content.pm.PackageManager;
const signatures =
android.os.Build.VERSION.SDK_INT >= 28
? packageManager
.getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNING_CERTIFICATES)
.signingInfo.getApkContentsSigners()
: packageManager.getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES).signatures;
for (let index = 0; index < signatures.length; index++) {
const digest = java.security.MessageDigest.getInstance("SHA-256").digest(signatures[index].toByteArray());
if (hex(digest) === OFFICIAL_SIGNING_CERT_SHA256) return "official";
}
const debuggable = (context.getApplicationInfo().flags & android.content.pm.ApplicationInfo.FLAG_DEBUGGABLE) !== 0;
return debuggable ? "debug" : "self-built";
} catch (error) {
console.warn(`build-info: could not read the signing certificate: ${error}`);
return "unknown";
}
}
function hex(bytes: any): string {
let text = "";
for (let index = 0; index < bytes.length; index++) text += ((bytes[index] & 0xff) + 0x100).toString(16).slice(1);
return text;
}
+10
View File
@@ -0,0 +1,10 @@
declare const FaceclawKitConnectionCounters: any
export function drainConnectionCounters(): Record<string, number> {
const counts: Record<string, number> = {}
const parsed = JSON.parse(String(FaceclawKitConnectionCounters.shared.drain())) as Record<string, unknown>
for (const [name, count] of Object.entries(parsed)) {
if (typeof count === 'number' && count > 0) counts[name] = count
}
return counts
}
+14
View File
@@ -0,0 +1,14 @@
declare const com: any;
/**
* Connection-reliability event counts since the last call, from the shared
* Kotlin session core (ConnectionCounters.kt), which keeps them process-wide.
*/
export function drainConnectionCounters(): Record<string, number> {
const counts: Record<string, number> = {};
const parsed = JSON.parse(String(com.faceclaw.app.ConnectionCounters.drain())) as Record<string, unknown>;
for (const [name, count] of Object.entries(parsed)) {
if (typeof count === "number" && count > 0) counts[name] = count;
}
return counts;
}
+4 -19
View File
@@ -2,32 +2,17 @@ import { ApplicationSettings } from "@nativescript/core";
const ONBOARDING_COMPLETE_KEY = "onboarding.complete";
const PREVIEW_ONLY_KEY = "onboarding.previewOnly";
const WELCOME_SOUND_PENDING_KEY = "onboarding.welcomeSoundPending";
export function hasCompletedOnboarding(): boolean {
return ApplicationSettings.getBoolean(ONBOARDING_COMPLETE_KEY, false);
}
export function setOnboardingCompleted(completed: boolean): void {
const wasCompleted = hasCompletedOnboarding();
ApplicationSettings.setBoolean(ONBOARDING_COMPLETE_KEY, completed);
// Arm the one-time welcome sound on the first-ever completion (not on the
// idempotent re-completions that happen when installing from the main menu).
if (completed && !wasCompleted) {
setWelcomeSoundPending(true);
}
}
/**
* True when onboarding just completed and the celebratory sound hasn't played
* yet. Consumed (and cleared) on the first successful glasses connection.
* Phone-side onboarding. The on-glasses onboarding that follows it on the
* first connection is tracked separately (app/apps/onboarding/).
*/
export function isWelcomeSoundPending(): boolean {
return ApplicationSettings.getBoolean(WELCOME_SOUND_PENDING_KEY, false);
}
export function setWelcomeSoundPending(pending: boolean): void {
ApplicationSettings.setBoolean(WELCOME_SOUND_PENDING_KEY, pending);
export function setOnboardingCompleted(completed: boolean): void {
ApplicationSettings.setBoolean(ONBOARDING_COMPLETE_KEY, completed);
}
/**
+62
View File
@@ -23,6 +23,7 @@ import { MENU_ANIMATION_KEY } from "./menu-animation-pref";
import { LIST_ROW_TEXT_INSET, lineStep } from "./metrics";
import { Layer, type LayerContext } from "./layers";
import { GrayImage } from "~/graphics/image";
import type { DataCollectionLevel } from "../analytics/analytics-store";
export type NightscoutSettings = {
siteUrl: string;
@@ -47,6 +48,11 @@ type ConfigSettingOptions<TValue, TId extends string> = {
formatValue?: (value: TValue) => string;
/** Extended description shown in the Settings panel when the row is selected. */
description?: string;
/**
* Boolean and enum settings only take fixed values, so Full analytics
* reports them (fixedChoiceSettingValues); false keeps this one out.
*/
reportInAnalytics?: boolean;
};
// Fired after any setting changes, in any isolate (storage lives in the Java
@@ -74,6 +80,21 @@ onSettingsStoreChanged(() => {
}, 0);
});
// Boolean and enum settings by storage key, for fixedChoiceSettingValues.
// Settings built repeatedly (per-app or per-slot) just replace themselves.
const fixedChoiceSettings = new Map<string, ConfigSetting<boolean> | ConfigSetting<string>>();
/**
* The current value of every boolean and enum setting this isolate has
* loaded, keyed by storage key: what Full analytics reports as settings.
* Free-text settings never appear.
*/
export function fixedChoiceSettingValues(): Record<string, boolean | string> {
const values: Record<string, boolean | string> = {};
for (const [storageKey, setting] of fixedChoiceSettings) values[storageKey] = setting.get();
return values;
}
export abstract class ConfigSetting<TValue, TId extends string = string> {
readonly id: TId;
readonly label: string;
@@ -104,6 +125,7 @@ export class ConfigSettingBoolean<TId extends string = string> extends ConfigSet
// Widens ConfigSetting's protected constructor.
public constructor(options: ConfigSettingOptions<boolean, TId>) {
super(options);
if (options.reportInAnalytics !== false) fixedChoiceSettings.set(options.storageKey, this);
}
get(): boolean {
@@ -134,6 +156,7 @@ export class ConfigSettingEnum<TValue extends string, TId extends string = strin
constructor(options: ConfigSettingEnumOptions<TValue, TId>) {
super(options);
if (options.reportInAnalytics !== false) fixedChoiceSettings.set(options.storageKey, this);
this.values = options.values;
this.disabledPredicate = options.isDisabled ?? (() => false);
if (options.normalize) {
@@ -660,6 +683,45 @@ export const navigateVerticalPositionSetting = appVerticalPositionSetting("navig
export const terminalDisplayModeSetting = appDisplayModeSetting("terminal", "default");
export const terminalVerticalPositionSetting = appVerticalPositionSetting("terminal");
export type { DataCollectionLevel };
/** The data-collection setting's key, for observers (app/analytics/analytics.ts). */
export const DATA_COLLECTION_KEY = "privacy.dataCollection";
const DATA_COLLECTION_LABELS: Record<DataCollectionLevel, string> = {
none: "None",
minimal: "Minimal",
full: "Full",
};
// What each level collects is set out in app/analytics/analytics-store.ts and
// the privacy policy (PRIVACY); keep these in step with both.
const DATA_COLLECTION_SUMMARIES: Record<DataCollectionLevel, string> = {
none: "Send nothing",
minimal: "Version, devices, connection reliability",
full: "Also settings and API-key presence",
};
/** One-line summary of what a data-collection level sends (onboarding, settings). */
export function dataCollectionSummary(level: DataCollectionLevel): string {
return DATA_COLLECTION_SUMMARIES[level];
}
/** Chosen in the on-glasses onboarding; changeable in Settings > Privacy. */
export const dataCollectionSetting = new ConfigSettingEnum<DataCollectionLevel>({
id: "data-collection",
label: "Data collection",
storageKey: DATA_COLLECTION_KEY,
defaultValue: "none",
values: ["none", "minimal", "full"],
formatValue: (value) => DATA_COLLECTION_LABELS[value],
description:
"Anonymous statistics Faceclaw may send its developers once a day, to help fix problems and improve the " +
"app. None: send nothing, and keep nothing on the phone. Minimal: the app version, which devices are " +
"paired and in use, and how reliable the connection to them is. Full: also settings chosen from fixed " +
"options, and which kinds of API key are set (never the keys themselves).",
});
export const voiceControlEnabledSetting = new ConfigSettingBoolean({
id: "voice-control-enabled",
label: "Enable",
+8
View File
@@ -45,6 +45,7 @@ import {
autoBrightnessMinSetting,
autoBrightnessMaxSetting,
autoBrightnessCurveSetting,
dataCollectionSetting,
glassesBatteryVisibilitySetting,
phoneBatteryVisibilitySetting,
ringBatteryVisibilitySetting,
@@ -191,6 +192,13 @@ function settingsSections(): SettingsSection[] {
toggleSettingMenuItem(showBleBandwidthSetting),
],
},
{
label: "Privacy",
items: [
// First chosen in the on-glasses onboarding (app/apps/onboarding/).
enumSettingMenuItem(dataCollectionSetting),
],
},
{
label: "About",
// The version/license blurb (renderDetail) draws above the bundled
+5 -4
View File
@@ -31,7 +31,8 @@ import { Menu, type MenuDrawArgs } from "./menu-core";
import { LIST_ROW_TEXT_INSET, listRowHeight } from "./metrics";
import type { Layer, LayerContext } from "./layers";
const SIZE_CHOICES = [8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28] as const;
/** Pixel sizes offered for TTF faces (also by the onboarding's text-size page). */
export const FONT_SIZE_CHOICES = [8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28] as const;
const DEFAULT_TTF_SIZE = 16;
const PREVIEW_TEXT = "The quick brown fox jumps over 0123456789";
const INHERIT_UI_FONT_LABEL = "Same as UI font";
@@ -279,7 +280,7 @@ export class FontPickerLayer implements Layer {
const allowed = this.options.sizeAllowed;
if (!allowed || allowed(font.path, size)) return size;
let best = -1;
for (const candidate of SIZE_CHOICES) {
for (const candidate of FONT_SIZE_CHOICES) {
if (!allowed(font.path, candidate)) continue;
if (best < 0 || Math.abs(candidate - size) < Math.abs(best - size)) best = candidate;
}
@@ -302,7 +303,7 @@ export class FontPickerLayer implements Layer {
private openSizeMenu(ctx: LayerContext): void {
if (this.draft.kind !== "ttf") return;
const draft = this.draft;
const sizes = SIZE_CHOICES.filter((size) =>
const sizes = FONT_SIZE_CHOICES.filter((size) =>
this.options.sizeAllowed === undefined || this.options.sizeAllowed(draft.font.path, size),
);
const items = sizes.map((size): MenuItem => ({
@@ -312,7 +313,7 @@ export class FontPickerLayer implements Layer {
innerCtx.stack.pop();
},
}));
openModalMenu(ctx, "Size", items, Math.max(0, sizes.indexOf(draft.size as (typeof SIZE_CHOICES)[number])));
openModalMenu(ctx, "Size", items, Math.max(0, sizes.indexOf(draft.size as (typeof FONT_SIZE_CHOICES)[number])));
}
}