/** * Registry of running EvenHub apps. Each launched package gets its own glasses * window, session, and persistent WebView — they run concurrently and keep * driving their windows in the background (the shell delivers FOREGROUND_ENTER/ * EXIT as focus changes; the WebView host keeps them rendering while unseen). * * Glasses-first: launching does NOT take over the phone screen. The phone * stays on the Faceclaw dashboard; an app's phone UI is shown on demand * (window menu -> "Show phone UI", or the app-icon button in the phone's * action bar while the app is foreground on the glasses), overlaying the * dashboard, and the Back button or the overlay's close button returns to it. * Apps are kept alive until explicitly closed (no memory eviction yet). */ import { Application, type ImageSource } from "@nativescript/core"; import { type AppContext } from "../app-definition"; import { appFilesDirPath, deletePathRecursively, } from "../../native/file-access"; import { type EvenHubManifest } from "./ehpk"; import { unpackRuntime } from "./unpack-runtime"; import { type EvenHubPermission } from "./permissions"; import { EvenHubSession } from "./session"; import { createEvenHubWindow } from "./evenhub-window"; import { createEvenHubWebView, type EvenHubWebView } from "./webview"; import { shell } from "../../ui/shell/shell"; import { GrayImage } from "../../graphics/image"; import { grayImageToWhiteIconSource } from "../../native/gray-image-preview"; import { readEvenHubPackageManifest, installedEvenHubAppId, installedEvenHubPackagePath, type InstalledEvenHubApp, } from "./installed-apps"; type RunningApp = { windowId: string; appId: string; packageId: string; session: EvenHubSession; webView: EvenHubWebView; /** The app's icon for the phone action bar, rendered on first use. */ phoneIcon?: ImageSource | null; }; /** The phone action bar's button for opening the foreground app's phone UI. */ export type PhoneUiButton = { windowId: string; title: string; icon: ImageSource | null }; /** Pixel size the action-bar icon is rendered at (~21dp at xxhdpi). */ const PHONE_ICON_SIZE = 63; const running = new Map(); let nextSerial = 1; let phoneShownWindowId: string | null = null; let backHandlerRegistered = false; const phoneShownListeners = new Set<() => void>(); /** Called whenever an app's phone UI is shown or hidden. Returns an unsubscribe. */ export function onPhoneShownChanged(listener: () => void): () => void { phoneShownListeners.add(listener); return () => phoneShownListeners.delete(listener); } function notifyPhoneShownChanged(): void { for (const listener of phoneShownListeners) listener(); } /** Intercept Android Back while an app's phone UI is overlaying the dashboard. */ function ensureBackHandler(): void { if (backHandlerRegistered) return; backHandlerRegistered = true; Application.android?.on("activityBackPressed", (args: { cancel: boolean }) => { if (phoneShownWindowId) { args.cancel = true; hidePhone(); } }); } /** * Unpack an .ehpk and launch it as a new concurrent app: a glasses window plus * a persistent WebView. Does not disturb other running apps or the phone screen. */ export async function launchPackage( ctx: AppContext, ehpkPath: string, options: { appId?: string; singleton?: boolean } = {}, ): Promise { const appId = options.appId ?? "evenhub"; if (options.singleton) { const existing = Array.from(running.values()).find((app) => app.appId === appId); if (existing) { shell.focusWindow(existing.windowId); ctx.requestShellRender(); return; } } const manifest = readEvenHubPackageManifest(ehpkPath); if (!manifest) throw new Error('Could not read the EHPK manifest.'); // Unpack off the iOS UI thread; reserve a unique directory before awaiting. const safeId = manifest.packageId.replace(/[^A-Za-z0-9._-]/g, "_"); if (!safeId || safeId === '.' || safeId === '..') throw new Error('Invalid EHPK package ID'); const baseDir = `${appFilesDirPath()}/evenhub-apps/${safeId}${global.isIOS ? `-${Date.now()}-${nextSerial++}` : ''}`; const started = Date.now(); ctx.appendLog(`evenhub: unpacking ${manifest.name}`); try { const loadedManifest = await unpackRuntime(ehpkPath, baseDir); ctx.appendLog(`evenhub: unpacked ${loadedManifest.name} in ${Date.now() - started} ms`); await startApp(ctx, loadedManifest, `${baseDir}/dist`, "", appId, global.isIOS ? () => deletePathRecursively(baseDir) : undefined); } catch (error) { deletePathRecursively(baseDir); throw error; } } /** * Shared tail of every launch: build the session and its WebView, register the * pair, and open the glasses window. `distDir` serves a packaged app offline; * `remoteUrl`, when set, loads the app from the network instead. */ async function startApp( ctx: AppContext, manifest: EvenHubManifest, distDir: string, remoteUrl: string, appId: string, cleanup?: () => void, ): Promise { const windowId = `evenhub:app:${nextSerial++}`; const session = new EvenHubSession(manifest, distDir, ctx.appendLog, remoteUrl, payload => ctx.actions.playBuzzerSequence(payload)); const webView = createEvenHubWebView(session); session.attachWebView({ evaluateJs: webView.evaluateJs, destroy: () => { if (phoneShownWindowId === windowId) { webView.hideOnPhone(); phoneShownWindowId = null; notifyPhoneShownChanged(); } webView.destroy(); cleanup?.(); running.delete(windowId); }, }); running.set(windowId, { windowId, appId, packageId: manifest.packageId, session, webView }); ensureBackHandler(); ctx.appendLog(`evenhub: launching ${manifest.name} ${manifest.version} (${manifest.packageId})`); try { await ctx.launchInProcessApp(windowId, `window:${windowId}`, (options) => createEvenHubWindow(windowId, appId, session, options, () => showOnPhone(windowId)), ); } catch (error) { session.close(); throw error; } } /** * Launch an EvenHub app served by a live web server (the Developer app's * "Load app from URL" / QR flows), rather than one unpacked from an .ehpk. * * There is no manifest to read, so one is synthesized: the URL identifies the * app (a stable packageId keeps its localStorage across reloads) and every * permission is treated as declared, on the grounds that the user pointed * Faceclaw at this URL themselves. Loading the same URL again replaces the * running instance, which is what a reload after an edit should do. */ export async function launchUrl(ctx: AppContext, url: string): Promise { const normalized = normalizeAppUrl(url); if (!normalized) { ctx.appendLog(`evenhub: not an http(s) URL: ${url}`); throw new Error("Not an http:// or https:// URL."); } const manifest = synthesizeUrlManifest(normalized); closeRunningPackage(manifest.packageId); await startApp(ctx, manifest, "", normalized, "evenhub"); } /** Accept a bare "example.com/app" as https; reject anything not http(s). */ export function normalizeAppUrl(raw: string): string | null { const trimmed = raw.trim(); if (!trimmed) return null; const withScheme = /^[a-z][a-z0-9+.-]*:\/\//i.test(trimmed) ? trimmed : `https://${trimmed}`; if (!/^https?:\/\/[^/?#\s]+/i.test(withScheme)) return null; return withScheme; } /** Every permission name the host knows how to honor, granted to URL apps. */ const DEV_URL_PERMISSIONS: EvenHubPermission[] = [ { name: "network" }, { name: "location" }, { name: "g2-microphone" }, { name: "phone-microphone" }, ]; function synthesizeUrlManifest(url: string): EvenHubManifest { const match = /^https?:\/\/([^/?#]+)([^?#]*)/i.exec(url); const authority = match?.[1] ?? url; const path = (match?.[2] ?? "").replace(/\/index\.html?$/i, "").replace(/\/+$/, ""); const idSource = `${authority}${path}`.toLowerCase().replace(/[^a-z0-9.-]+/g, "-"); return { packageId: `dev.url.${idSource.slice(0, 96)}`, name: authority, version: "dev", entrypoint: "", networkWhitelist: [], permissions: DEV_URL_PERMISSIONS, privacyPolicyUrl: "", raw: { url }, }; } /** Launch an installed app as a launcher-addressable singleton. */ export function launchInstalledPackage(ctx: AppContext, app: InstalledEvenHubApp): Promise { return launchPackage(ctx, installedEvenHubPackagePath(app.packageId), { appId: installedEvenHubAppId(app.packageId), singleton: true, }); } /** Close every running instance before an installed package is removed. */ export function closeRunningPackage(packageId: string): void { const windowIds = Array.from(running.values()) .filter((app) => app.packageId === packageId) .map((app) => app.windowId); for (const windowId of windowIds) shell.closeWindow(windowId); } /** Overlay one app's phone UI over the dashboard (from its window menu). */ export function showOnPhone(windowId: string): void { const app = running.get(windowId); if (!app) return; if (phoneShownWindowId && phoneShownWindowId !== windowId) hidePhone(); app.webView.showOnPhone(hidePhone); phoneShownWindowId = windowId; notifyPhoneShownChanged(); } /** Return the phone to the dashboard (Back, the close button, or the shown app closing). */ export function hidePhone(): void { if (!phoneShownWindowId) return; running.get(phoneShownWindowId)?.webView.hideOnPhone(); phoneShownWindowId = null; notifyPhoneShownChanged(); } /** * The foreground glasses window, when it is an EvenHub app whose phone UI * isn't showing: the phone action bar then offers a button to show it. */ export function phoneUiButton(): PhoneUiButton | null { const window = shell.foregroundWindow(); const app = window ? running.get(window.windowId) : undefined; if (!window || !app || phoneShownWindowId === app.windowId) return null; if (app.phoneIcon === undefined) { // The same artwork as the glasses sidebar (installed icon, else the // generic package icon), drawn white. const image = new GrayImage(PHONE_ICON_SIZE, PHONE_ICON_SIZE, 0); window.drawIcon(image, 0, 0, PHONE_ICON_SIZE); app.phoneIcon = grayImageToWhiteIconSource(image); } return { windowId: app.windowId, title: window.title, icon: app.phoneIcon }; } export function runningAppCount(): number { return running.size; }