diff --git a/App_Resources/Android/app.gradle b/App_Resources/Android/app.gradle index 6044f239..fa5876b6 100644 --- a/App_Resources/Android/app.gradle +++ b/App_Resources/Android/app.gradle @@ -5,6 +5,9 @@ dependencies { // implementation 'androidx.multidex:multidex:2.0.1' // WebSocket transport for the g2mirror terminal-mirroring client. implementation 'com.squareup.okhttp3:okhttp:4.12.0' + // WebViewCompat.addDocumentStartJavaScript, for injecting the EvenHub bridge + // shim into remotely hosted apps (developer "Load app from URL"). + implementation 'androidx.webkit:webkit:1.12.1' } def faceclawSherpaOnnxVersion = "1.13.0" diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubDocumentStart.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubDocumentStart.java new file mode 100644 index 00000000..2f9c0e90 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubDocumentStart.java @@ -0,0 +1,50 @@ +package com.faceclaw.app; + +import android.util.Log; +import android.webkit.WebView; + +import androidx.webkit.WebViewCompat; +import androidx.webkit.WebViewFeature; + +import java.util.Collections; +import java.util.Set; + +/** + * Injects the EvenHub bridge shim at document start into a WebView that loads + * a page over the real network (the developer "Load app from URL" flow). + * + * Packaged apps get the shim spliced into the HTML they are served + * (FaceclawEvenHubWebViewClient), which is not an option for a page fetched + * from a live server. WebViewCompat.addDocumentStartJavaScript is the + * supported way to run script before any page script, and unlike an + * onPageStarted evaluateJavascript it is guaranteed to win the race — apps + * poll for window.flutter_inappwebview with a few seconds' patience, but the + * timer shim must be in place before the app captures setTimeout. + * + * Requires WebView 83+; the caller falls back to onPageStarted injection when + * this returns false. + */ +public final class FaceclawEvenHubDocumentStart { + private static final String TAG = "FaceclawEvenHub"; + + private FaceclawEvenHubDocumentStart() {} + + /** + * @param originRule an allowed-origin rule, e.g. "https://example.com" or "*". + * @return whether the script was registered. + */ + public static boolean install(WebView webView, String script, String originRule) { + if (!WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT)) { + Log.w(TAG, "document-start scripts unsupported; falling back to onPageStarted"); + return false; + } + try { + Set rules = Collections.singleton(originRule); + WebViewCompat.addDocumentStartJavaScript(webView, script, rules); + return true; + } catch (Throwable error) { + Log.w(TAG, "addDocumentStartJavaScript failed for " + originRule, error); + return false; + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewClient.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewClient.java index 4d89a8bf..f8c21348 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewClient.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewClient.java @@ -28,6 +28,12 @@ import java.util.Locale; * shouldInterceptRequest runs on a WebView-internal thread; it only touches * files, never the NativeScript runtime. Requests to other hosts fall * through to the network (the manifest whitelist is not yet enforced). + * + * A developer-loaded app ("Load app from URL") has no local root: host is + * empty, nothing is intercepted, and the page loads normally from its server. + * The shim is registered as a document-start script instead + * (FaceclawEvenHubDocumentStart); injectOnPageStarted is the fallback for + * WebView versions that lack that API. */ public class FaceclawEvenHubWebViewClient extends WebViewClient { private static final String TAG = "FaceclawEvenHub"; @@ -35,19 +41,28 @@ public class FaceclawEvenHubWebViewClient extends WebViewClient { private final String rootDir; private final String host; private final String injectScript; + private final boolean injectOnPageStarted; private final FaceclawEvenHubListener listener; private final Handler mainHandler = new Handler(Looper.getMainLooper()); public FaceclawEvenHubWebViewClient( - String rootDir, String host, String injectScript, FaceclawEvenHubListener listener) { + String rootDir, + String host, + String injectScript, + boolean injectOnPageStarted, + FaceclawEvenHubListener listener) { this.rootDir = rootDir; this.host = host; this.injectScript = injectScript; + this.injectOnPageStarted = injectOnPageStarted; this.listener = listener; } @Override public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { + if (host == null || host.isEmpty()) { + return null; // Remote app: everything comes from the network. + } Uri url = request.getUrl(); if (url == null || !host.equals(url.getHost())) { return null; // External hosts: normal network handling. @@ -79,6 +94,16 @@ public class FaceclawEvenHubWebViewClient extends WebViewClient { } } + @Override + public void onPageStarted(WebView view, String url, android.graphics.Bitmap favicon) { + super.onPageStarted(view, url, favicon); + if (!injectOnPageStarted) return; + // Best-effort: the document has committed but the parser has not run + // the page's own scripts yet, so this usually lands first. Only used + // when document-start scripts are unavailable. + view.evaluateJavascript(injectScript, null); + } + @Override public void onPageFinished(WebView view, final String url) { mainHandler.post(new Runnable() { diff --git a/app/apps/all-apps.ts b/app/apps/all-apps.ts index aef8d27f..54d3cb31 100644 --- a/app/apps/all-apps.ts +++ b/app/apps/all-apps.ts @@ -16,7 +16,7 @@ import blocksApp from "./blocks"; import minesweeperApp from "./minesweeper"; import freecellApp from "./freecell"; import pinballApp from "./pinball"; -import debugTestsApp from "./debug-tests"; +import developerApp from "./developer"; import evenhubApp from "./evenhub"; import settingsApp from "./settings"; @@ -43,7 +43,7 @@ export const ALL_APPS: readonly AppDefinition[] = [ minesweeperApp, freecellApp, pinballApp, - debugTestsApp, + developerApp, evenhubApp, settingsApp, ]; diff --git a/app/apps/debug-tests/debug-tests-app.ts b/app/apps/debug-tests/debug-tests-app.ts deleted file mode 100644 index 16d1e2c7..00000000 --- a/app/apps/debug-tests/debug-tests-app.ts +++ /dev/null @@ -1,70 +0,0 @@ -import { MenuLayer } from "../../ui/menu"; -import { ScreenTestLayer } from "./screen-test"; -import { BuzzerDemoLayer } from "./buzzer-demo"; -import { AccelerometerDemoLayer } from "./accelerometer-demo"; -import { ResourceUsageLayer } from "./resource-usage"; -import { appViewportSize } from "../../ui/shell/geometry"; -import { - createInProcessWindow, - YieldAtRootLayer, - type InProcessAppOptions, - type InProcessWindow, -} from "../../ui/shell/in-process-window"; - -export const DEBUG_TESTS_WINDOW_ID = "debug-tests"; -export const DEBUG_TESTS_SURFACE_ID = "window:debug-tests"; - -/** Debug/diagnostic pages (screen test, buzzer demo) as an in-process app. */ -export function createDebugTestsAppWindow(options: InProcessAppOptions): InProcessWindow { - const menu = new MenuLayer( - "Debug tests", - [ - { - label: "Dither test", - onSelect: (ctx) => { - ctx.stack.push(new ScreenTestLayer()); - }, - }, - { - label: "Buzzer demo", - onSelect: (ctx) => { - ctx.stack.push(new BuzzerDemoLayer()); - }, - }, - { - label: "Accelerometer demo", - onSelect: (ctx) => { - ctx.stack.push(new AccelerometerDemoLayer(DEBUG_TESTS_WINDOW_ID, ctx.actions.requestRender)); - }, - }, - { - label: "Show resource usage", - onSelect: (ctx) => { - ctx.stack.push(new ResourceUsageLayer(DEBUG_TESTS_WINDOW_ID, ctx.actions.requestRender)); - }, - }, - ], - { - x: 8, - y: 8, - width: 272, - showBorder: false, - minHeight: 0, - maxHeight: appViewportSize("min").height - 16, - }, - ); - return createInProcessWindow({ - appId: "debug-tests", - windowId: DEBUG_TESTS_WINDOW_ID, - title: "Debug tests", - iconLetter: "Db", - icon: "flask-conical", - closeable: true, - actions: options.actions, - baseLayer: new YieldAtRootLayer(menu), - submitFrame: options.submitFrame, - setSurfaceVisible: options.setSurfaceVisible, - removeSurface: options.removeSurface, - onClosed: options.onClosed, - }); -} diff --git a/app/apps/debug-tests/index.ts b/app/apps/debug-tests/index.ts deleted file mode 100644 index 88d66e7b..00000000 --- a/app/apps/debug-tests/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { type AppDefinition } from "../app-definition"; -import { createDebugTestsAppWindow, DEBUG_TESTS_SURFACE_ID, DEBUG_TESTS_WINDOW_ID } from "./debug-tests-app"; - -const debugTestsApp: AppDefinition = { - appId: "debug-tests", - title: "Debug tests", - icon: "flask-conical", - launch: (ctx) => ctx.launchInProcessApp(DEBUG_TESTS_WINDOW_ID, DEBUG_TESTS_SURFACE_ID, createDebugTestsAppWindow), -}; - -export default debugTestsApp; diff --git a/app/apps/debug-tests/accelerometer-demo.ts b/app/apps/developer/accelerometer-demo.ts similarity index 98% rename from app/apps/debug-tests/accelerometer-demo.ts rename to app/apps/developer/accelerometer-demo.ts index 3d5c205d..56d14700 100644 --- a/app/apps/debug-tests/accelerometer-demo.ts +++ b/app/apps/developer/accelerometer-demo.ts @@ -16,7 +16,7 @@ const RECONCILE_INTERVAL_MS = 400; /** * Accelerometer demo: turns the IMU stream on while this page is visible (the - * Debug tests window is foregrounded and the screen is on), off otherwise, and + * Developer window is foregrounded and the screen is on), off otherwise, and * shows the latest x/y/z reading with a simple per-axis bar. * * The IMU stream is experimental — the firmware may never populate it — so the diff --git a/app/apps/debug-tests/buzzer-demo.ts b/app/apps/developer/buzzer-demo.ts similarity index 100% rename from app/apps/debug-tests/buzzer-demo.ts rename to app/apps/developer/buzzer-demo.ts diff --git a/app/apps/developer/developer-app.ts b/app/apps/developer/developer-app.ts new file mode 100644 index 00000000..44326911 --- /dev/null +++ b/app/apps/developer/developer-app.ts @@ -0,0 +1,131 @@ +import { MenuLayer, drawSubmenuIndicator, type MenuItem } from "../../ui/menu"; +import { ScreenTestLayer } from "./screen-test"; +import { BuzzerDemoLayer } from "./buzzer-demo"; +import { AccelerometerDemoLayer } from "./accelerometer-demo"; +import { ResourceUsageLayer } from "./resource-usage"; +import { LoadAppFromQrLayer, LoadAppFromUrlLayer } from "./load-app"; +import { type AppContext } from "../app-definition"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; +import { appViewportSize } from "../../ui/shell/geometry"; +import { LIST_ROW_TEXT_INSET } from "../../ui/metrics"; +import { + createInProcessWindow, + YieldAtRootLayer, + type InProcessAppOptions, + type InProcessWindow, +} from "../../ui/shell/in-process-window"; + +export const DEVELOPER_WINDOW_ID = "developer"; +export const DEVELOPER_SURFACE_ID = "window:developer"; + +const MENU_LAYOUT = { + x: 8, + y: 8, + width: 272, + showBorder: false, + minHeight: 0, + maxHeight: appViewportSize("min").height - 16, +}; + +/** A row that opens a nested page: the label plus a right-edge ">". */ +function submenuItem(label: string, onSelect: MenuItem["onSelect"]): MenuItem { + return { + label, + onSelect, + render: ({ image, x, y, width, height, selected, disabled, text }) => { + const font = getDefaultSmallFont(); + const value = disabled ? 70 : selected ? 255 : 200; + image.drawText(font, x, y + LIST_ROW_TEXT_INSET, text, value); + drawSubmenuIndicator(image, font, x, y, width, height, value); + }, + }; +} + +/** The diagnostic demos, one level down from the root menu. */ +function debugTestsMenu(): MenuLayer { + return new MenuLayer( + "Debug tests", + [ + { + label: "Dither test", + onSelect: (ctx) => { + ctx.stack.push(new ScreenTestLayer()); + }, + }, + { + label: "Buzzer demo", + onSelect: (ctx) => { + ctx.stack.push(new BuzzerDemoLayer()); + }, + }, + { + label: "Accelerometer demo", + onSelect: (ctx) => { + ctx.stack.push(new AccelerometerDemoLayer(DEVELOPER_WINDOW_ID, ctx.actions.requestRender)); + }, + }, + ], + MENU_LAYOUT, + ); +} + +/** + * The Developer app: tools for building and debugging on the glasses. The two + * "Load app" entries run an EvenHub app straight off a web server, without + * packaging it into an .ehpk first; the diagnostic demos live one level down + * under "Debug tests". + */ +export function createDeveloperAppWindow(appContext: AppContext, options: InProcessAppOptions): InProcessWindow { + const menu = new MenuLayer( + "Developer", + [ + { + label: "Load app from URL", + onSelect: (ctx) => { + const layer = new LoadAppFromUrlLayer(appContext); + ctx.stack.push(layer); + layer.open(ctx); + }, + }, + { + label: "Load app from QR code", + onSelect: (ctx) => { + const layer = new LoadAppFromQrLayer(appContext); + ctx.stack.push(layer); + layer.open(ctx); + }, + }, + { + label: "Show resource usage", + onSelect: (ctx) => { + ctx.stack.push(new ResourceUsageLayer(DEVELOPER_WINDOW_ID, ctx.actions.requestRender)); + }, + }, + submenuItem("Debug tests", (ctx) => { + ctx.stack.push(debugTestsMenu()); + }), + ], + MENU_LAYOUT, + ); + let created: InProcessWindow | null = null; + created = createInProcessWindow({ + appId: "developer", + windowId: DEVELOPER_WINDOW_ID, + title: "Developer", + iconLetter: "Dv", + icon: "wrench", + closeable: true, + actions: options.actions, + // Dictating a URL is the one thing worth speaking at in this app; the + // load pages take the text and every other page ignores it. + receiveTextInput: (text) => { + if (created?.stack.receiveTextInput(text)) created.requestRender(); + }, + baseLayer: new YieldAtRootLayer(menu), + submitFrame: options.submitFrame, + setSurfaceVisible: options.setSurfaceVisible, + removeSurface: options.removeSurface, + onClosed: options.onClosed, + }); + return created; +} diff --git a/app/apps/developer/index.ts b/app/apps/developer/index.ts new file mode 100644 index 00000000..72db110a --- /dev/null +++ b/app/apps/developer/index.ts @@ -0,0 +1,14 @@ +import { type AppDefinition } from "../app-definition"; +import { createDeveloperAppWindow, DEVELOPER_SURFACE_ID, DEVELOPER_WINDOW_ID } from "./developer-app"; + +const developerApp: AppDefinition = { + appId: "developer", + title: "Developer", + icon: "wrench", + launch: (ctx) => + ctx.launchInProcessApp(DEVELOPER_WINDOW_ID, DEVELOPER_SURFACE_ID, (options) => + createDeveloperAppWindow(ctx, options), + ), +}; + +export default developerApp; diff --git a/app/apps/developer/load-app.ts b/app/apps/developer/load-app.ts new file mode 100644 index 00000000..d69d8b63 --- /dev/null +++ b/app/apps/developer/load-app.ts @@ -0,0 +1,207 @@ +import { GrayImage } from "../../graphics/image"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; +import { wrapText } from "../../graphics/textwrap"; +import { type AppContext } from "../app-definition"; +import { isLoadableAppUrl, openEvenHubUrl } from "../evenhub"; +import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK } from "../../ui/gestures"; +import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers"; +import { lineStep } from "../../ui/metrics"; +import { developerAppUrlSetting } from "../../ui/dashboard-settings"; +import { isQrScannerAvailable, scanQrCode } from "../../native/qr-scan"; + +const MARGIN = 20; + +/** + * "Load app from URL": the glasses side of the phone text editor. Entering the + * page opens the editor on developerAppUrlSetting, the typed URL echoes here + * as it changes, and a click (or the phone keyboard's done key) launches it as + * an EvenHub app. The draft persists, so reloading after an edit-and-rebuild is + * one click. + */ +export class LoadAppFromUrlLayer implements Layer { + private error = ""; + private launching = false; + + constructor(private readonly ctx: AppContext) {} + + /** Opens the phone editor; call right after pushing the layer. */ + open(ctx: LayerContext): void { + void ctx.actions.startTextSettingsEdit([developerAppUrlSetting], "Load app from URL", () => { + void this.launch(ctx); + }); + } + + /** Leaving the page by any route closes the phone editor with it. */ + onRemoved(): void { + void this.ctx.actions.endTextSettingEdit(); + } + + /** Dictated text fills the draft, same as typing it on the phone. */ + receiveTextInput(text: string): void { + developerAppUrlSetting.set(text); + this.error = ""; + } + + paint(ctx: LayerContext): GrayImage { + const font = getDefaultSmallFont(); + const { width, height } = ctx.stack.getBaseSize(); + const image = new GrayImage(width, height, 0); + const step = lineStep(font); + const textWidth = width - 2 * MARGIN; + + image.drawText(font, MARGIN, 8, "Load app from URL", 220); + let y = 8 + step + 6; + image.drawText(font, MARGIN, y, "Type the app's URL in the phone app", 150); + y += step; + image.drawText(font, MARGIN, y, "(or use voice input from the menu).", 150); + y += step + 8; + + const url = developerAppUrlSetting.get(); + for (const line of wrapText(font, url || "(empty)", textWidth).slice(0, 3)) { + image.drawText(font, MARGIN, y, line, url ? 230 : 110); + y += step; + } + if (this.error) { + y += 4; + for (const line of wrapText(font, this.error, textWidth).slice(0, 2)) { + image.drawText(font, MARGIN, y, line, 150); + y += step; + } + } + + const footer = this.launching + ? "Loading..." + : `${GESTURE_CLICK} load ${GESTURE_DOUBLE_CLICK} cancel`; + image.drawText(font, MARGIN, height - font.lineHeight - 12, footer, 110); + return image; + } + + handleInput(event: DashboardInputEvent, ctx: LayerContext): void { + switch (event.type) { + case "click": + void this.launch(ctx); + return; + case "double-click": + ctx.stack.pop(); + return; + default: + return; + } + } + + private async launch(ctx: LayerContext): Promise { + if (this.launching) return; + const url = developerAppUrlSetting.get(); + if (!isLoadableAppUrl(url)) { + this.error = url ? "Not an http:// or https:// URL." : "Enter a URL first."; + ctx.actions.requestRender(); + return; + } + this.launching = true; + this.error = ""; + ctx.actions.requestRender(); + try { + await openEvenHubUrl(this.ctx, url); + ctx.stack.pop(); + } catch (error) { + this.error = cleanError(error); + this.ctx.appendLog(`evenhub url launch failed: ${this.error}`); + } finally { + this.launching = false; + ctx.actions.requestRender(); + } + } +} + +/** + * "Load app from QR code": hand off to an installed scanner app, then launch + * whatever URL it decodes. The scanned URL is kept in developerAppUrlSetting so + * the URL page can reload it later without rescanning. + */ +export class LoadAppFromQrLayer implements Layer { + private status = "Starting scanner..."; + private busy = false; + + constructor(private readonly ctx: AppContext) {} + + /** Launches the scanner; call right after pushing the layer. */ + open(ctx: LayerContext): void { + if (!isQrScannerAvailable()) { + this.status = "No QR scanner app is installed on the phone."; + ctx.actions.requestRender(); + return; + } + this.busy = true; + void this.scan(ctx); + } + + paint(ctx: LayerContext): GrayImage { + const font = getDefaultSmallFont(); + const { width, height } = ctx.stack.getBaseSize(); + const image = new GrayImage(width, height, 0); + const step = lineStep(font); + + image.drawText(font, MARGIN, 8, "Load app from QR code", 220); + let y = 8 + step + 6; + for (const line of wrapText(font, this.status, width - 2 * MARGIN).slice(0, 5)) { + image.drawText(font, MARGIN, y, line, 170); + y += step; + } + + const footer = this.busy ? `${GESTURE_DOUBLE_CLICK} back` : `${GESTURE_CLICK} scan again ${GESTURE_DOUBLE_CLICK} back`; + image.drawText(font, MARGIN, height - font.lineHeight - 12, footer, 110); + return image; + } + + handleInput(event: DashboardInputEvent, ctx: LayerContext): void { + switch (event.type) { + case "click": + if (!this.busy && isQrScannerAvailable()) { + this.busy = true; + this.status = "Starting scanner..."; + ctx.actions.requestRender(); + void this.scan(ctx); + } + return; + case "double-click": + ctx.stack.pop(); + return; + default: + return; + } + } + + private async scan(ctx: LayerContext): Promise { + try { + this.status = "Point the phone camera at the QR code."; + ctx.actions.requestRender(); + const scanned = await scanQrCode(); + if (scanned === null) { + this.status = "Scan cancelled."; + return; + } + if (!isLoadableAppUrl(scanned)) { + this.status = `Not a URL: ${scanned}`; + return; + } + developerAppUrlSetting.set(scanned); + this.status = `Loading ${scanned}`; + ctx.actions.requestRender(); + await openEvenHubUrl(this.ctx, scanned); + ctx.stack.pop(); + } catch (error) { + this.status = cleanError(error); + this.ctx.appendLog(`evenhub qr launch failed: ${this.status}`); + } finally { + this.busy = false; + ctx.actions.requestRender(); + } + } +} + +function cleanError(error: unknown): string { + return String((error as Error)?.message ?? error) + .replace(/[\x00-\x1f]+/g, " ") + .replace(/\s+/g, " ") + .trim(); +} diff --git a/app/apps/debug-tests/resource-usage.ts b/app/apps/developer/resource-usage.ts similarity index 100% rename from app/apps/debug-tests/resource-usage.ts rename to app/apps/developer/resource-usage.ts diff --git a/app/apps/debug-tests/screen-test.ts b/app/apps/developer/screen-test.ts similarity index 98% rename from app/apps/debug-tests/screen-test.ts rename to app/apps/developer/screen-test.ts index 20ecf0f0..9678f0ef 100644 --- a/app/apps/debug-tests/screen-test.ts +++ b/app/apps/developer/screen-test.ts @@ -17,7 +17,7 @@ export class ScreenTestLayer implements Layer { private scrollRow = 0; paint(ctx: LayerContext): GrayImage { - // Sized to the hosting stack (the Debug tests app viewport). + // Sized to the hosting stack (the Developer app viewport). const { width, height } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); const font = getDefaultSmallFont(); diff --git a/app/apps/evenhub/index.ts b/app/apps/evenhub/index.ts index c7d6aeca..073294d0 100644 --- a/app/apps/evenhub/index.ts +++ b/app/apps/evenhub/index.ts @@ -10,7 +10,7 @@ import { type AppContext, type AppDefinition } from "../app-definition"; import { shell } from "../../ui/shell/shell"; import { EvenHubStoreLayer } from "./store-layer"; -import { launchPackage } from "./manager"; +import { launchPackage, launchUrl, normalizeAppUrl } from "./manager"; import { createInProcessWindow, type InProcessWindow } from "../../ui/shell/in-process-window"; import { renderEvenRealitiesLogo } from "../../graphics/even-realities-logo"; import { makeImageWindowIcon, windowIcon } from "../../ui/shell/chrome-layer"; @@ -26,6 +26,20 @@ export function openEvenHubPackage(ctx: AppContext, ehpkPath: string): Promise { + return launchUrl(ctx, url); +} + +/** Whether a string can be loaded by openEvenHubUrl, for validating input. */ +export function isLoadableAppUrl(url: string): boolean { + return normalizeAppUrl(url) !== null; +} + const evenhubApp: AppDefinition = { appId: "evenhub", title: "EvenHub", diff --git a/app/apps/evenhub/manager.ts b/app/apps/evenhub/manager.ts index 38ac7565..12b45346 100644 --- a/app/apps/evenhub/manager.ts +++ b/app/apps/evenhub/manager.ts @@ -18,7 +18,8 @@ import { readBinaryFile, writeBinaryFile, } from "../../native/file-access"; -import { parseEhpk, parseManifest, utf8Decode } from "./ehpk"; +import { parseEhpk, parseManifest, utf8Decode, type EvenHubManifest } from "./ehpk"; +import { type EvenHubPermission } from "./permissions"; import { EvenHubSession } from "./session"; import { createEvenHubWindow } from "./evenhub-window"; import { createEvenHubWebView, type EvenHubWebView } from "./webview"; @@ -96,8 +97,23 @@ export async function launchPackage( } } + await startApp(ctx, manifest, `${baseDir}/dist`, "", appId); +} + +/** + * 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, +): Promise { const windowId = `evenhub:app:${nextSerial++}`; - const session = new EvenHubSession(manifest, `${baseDir}/dist`, ctx.appendLog); + const session = new EvenHubSession(manifest, distDir, ctx.appendLog, remoteUrl); const webView = createEvenHubWebView(session); session.attachWebView({ evaluateJs: webView.evaluateJs, @@ -119,6 +135,61 @@ export async function launchPackage( ); } +/** + * 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), { diff --git a/app/apps/evenhub/session.ts b/app/apps/evenhub/session.ts index 7dadcdb9..88f1ac6a 100644 --- a/app/apps/evenhub/session.ts +++ b/app/apps/evenhub/session.ts @@ -314,6 +314,12 @@ export type EvenHubWindowHooks = { export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenHubCompassClient { readonly manifest: EvenHubManifest; readonly distDir: string; + /** + * For a developer-loaded app served from a live web server: the absolute + * URL to open. Empty for a packaged app, which is served offline out of + * distDir instead (see webview.ts). + */ + readonly remoteUrl: string; private page: EvenHubPage | null = null; private pageCreated = false; @@ -352,9 +358,15 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH private readonly grantedApiKeys = new Set(); private log: (message: string) => void; - constructor(manifest: EvenHubManifest, distDir: string, log: (message: string) => void) { + constructor( + manifest: EvenHubManifest, + distDir: string, + log: (message: string) => void, + remoteUrl = "", + ) { this.manifest = manifest; this.distDir = distDir; + this.remoteUrl = remoteUrl; this.log = log; } diff --git a/app/apps/evenhub/webview.ts b/app/apps/evenhub/webview.ts index 5429e872..ea2a4a7f 100644 --- a/app/apps/evenhub/webview.ts +++ b/app/apps/evenhub/webview.ts @@ -10,6 +10,11 @@ * from a fake per-app origin plus document-start injection of the * flutter_inappwebview bridge shim) and so the page keeps running JS while the * host app is backgrounded (see FaceclawEvenHubWebView / the host). + * + * A session with a remoteUrl (the Developer app's "Load app from URL") skips + * the offline serving entirely and loads that URL from the network; the shim + * is registered as a document-start script instead of being spliced into the + * served HTML. */ import { Application, Utils } from "@nativescript/core"; import { EVENHUB_BRIDGE_INJECT_SCRIPT, buildFaceclawExtensionsScript, type EvenHubSession } from "./session"; @@ -33,6 +38,15 @@ function originHost(session: EvenHubSession): string { return `${safe}.evenhub.invalid`; } +/** + * The allowed-origin rule for a remote app's document-start script: the + * loaded URL's own origin, so the shim isn't handed to third-party frames. + */ +function originRule(url: string): string { + const match = /^(https?:\/\/[^/?#]+)/i.exec(url); + return match ? match[1]! : "*"; +} + /** * Build the app's WebView, wire it to the session, attach it to the keep-alive * host, and start loading. Must run on the main thread. @@ -56,12 +70,26 @@ export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView { }, }); - const host = originHost(session); + const remoteUrl = session.remoteUrl; + // Remote apps load from their own server, so nothing is intercepted and the + // fake per-app origin is unused. + const host = remoteUrl ? "" : originHost(session); // Stock bridge shim first, then the Faceclaw extensions global (both at // document-start, before any app JS). const injectScript = EVENHUB_BRIDGE_INJECT_SCRIPT + buildFaceclawExtensionsScript(`Faceclaw/${FACECLAW_VERSION}`); + const documentStartInstalled = remoteUrl + ? com.faceclaw.app.FaceclawEvenHubDocumentStart.install(webView, injectScript, originRule(remoteUrl)) === true + : false; webView.setWebViewClient( - new com.faceclaw.app.FaceclawEvenHubWebViewClient(session.distDir, host, injectScript, listener), + new com.faceclaw.app.FaceclawEvenHubWebViewClient( + session.distDir, + host, + injectScript, + // onPageStarted injection is the fallback when the document-start API + // is unavailable; packaged apps get the shim spliced into their HTML. + !!remoteUrl && !documentStartInstalled, + listener, + ), ); webView.addJavascriptInterface(new com.faceclaw.app.FaceclawEvenHubJsBridge(listener), "__faceclawEvenHub"); @@ -79,7 +107,7 @@ export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView { const nativeHost = com.faceclaw.app.FaceclawEvenHubWebViewHost.getInstance(); nativeHost.attach(activity, webView); - webView.loadUrl(`https://${host}/${session.manifest.entrypoint}`); + webView.loadUrl(remoteUrl || `https://${host}/${session.manifest.entrypoint}`); return { native: webView, diff --git a/app/graphics/icons.ts b/app/graphics/icons.ts index 6deba979..e0ae594b 100644 --- a/app/graphics/icons.ts +++ b/app/graphics/icons.ts @@ -66,6 +66,8 @@ export const ICON_SVGS = { '', "flask-conical": '', + wrench: + '', settings: '', mic: diff --git a/app/native/qr-scan.ts b/app/native/qr-scan.ts new file mode 100644 index 00000000..9b56d988 --- /dev/null +++ b/app/native/qr-scan.ts @@ -0,0 +1,76 @@ +import { Application } from "@nativescript/core"; + +declare const android: any; +declare const global: any; + +/** + * QR scanning by delegating to whatever scanner app the phone has installed, + * via the de-facto-standard ZXing "SCAN" intent (Barcode Scanner, Binary Eye, + * QR & Barcode Scanner, and others register for it). Faceclaw ships no decoder + * and needs no camera permission of its own: the scanner app owns the camera + * and hands back the decoded text. + * + * The cost is that scanning only works if such an app is installed; + * isQrScannerAvailable reports that up front so the caller can say so instead + * of launching into nothing. (resolveActivity can see other apps only because + * the manifest already holds QUERY_ALL_PACKAGES for notification icons; + * without it Android 11+ would need a entry for the action.) + */ + +const SCAN_ACTION = "com.google.zxing.client.android.SCAN"; +// Arbitrary, only has to be distinct from other startActivityForResult callers. +const SCAN_REQUEST_CODE = 0x51d0; + +function buildScanIntent(): any { + const intent = new android.content.Intent(SCAN_ACTION); + intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); + // Ask the scanner not to keep its own history of what we scanned. + intent.putExtra("SAVE_HISTORY", false); + return intent; +} + +/** Whether any installed app answers the scan intent. */ +export function isQrScannerAvailable(): boolean { + if (!global.isAndroid) return false; + const activity = Application.android?.foregroundActivity ?? Application.android?.startActivity; + if (!activity) return false; + try { + return buildScanIntent().resolveActivity(activity.getPackageManager()) !== null; + } catch { + return false; + } +} + +/** + * Launch the scanner and resolve with the decoded text, or null if the user + * backed out. Rejects when no scanner app is installed or the activity can't + * be reached. + */ +export function scanQrCode(): Promise { + if (!global.isAndroid) return Promise.reject(new Error("QR scanning needs Android.")); + const activity = Application.android?.foregroundActivity ?? Application.android?.startActivity; + if (!activity) return Promise.reject(new Error("No foreground activity to scan from.")); + const intent = buildScanIntent(); + if (intent.resolveActivity(activity.getPackageManager()) === null) { + return Promise.reject(new Error("No QR scanner app installed.")); + } + return new Promise((resolve, reject) => { + const onResult = (args: { requestCode: number; resultCode: number; intent: any }) => { + if (args.requestCode !== SCAN_REQUEST_CODE) return; + Application.android.off(Application.android.activityResultEvent, onResult); + if (args.resultCode !== android.app.Activity.RESULT_OK || !args.intent) { + resolve(null); + return; + } + const text = args.intent.getStringExtra("SCAN_RESULT"); + resolve(text === null || text === undefined ? null : String(text)); + }; + Application.android.on(Application.android.activityResultEvent, onResult); + try { + activity.startActivityForResult(intent, SCAN_REQUEST_CODE); + } catch (error) { + Application.android.off(Application.android.activityResultEvent, onResult); + reject(error instanceof Error ? error : new Error(String(error))); + } + }); +} diff --git a/app/ui/dashboard-settings.ts b/app/ui/dashboard-settings.ts index 392b892c..a529a4b0 100644 --- a/app/ui/dashboard-settings.ts +++ b/app/ui/dashboard-settings.ts @@ -503,6 +503,23 @@ export const terminalNewConnectionSetting = new ConfigSettingString({ normalize: (value) => (value ?? "").replace(/[\x00-\x1f]+/g, "").trim(), }); +/** + * Staging buffer for the Developer app's "Load app from URL" flow: the app + * opens the phone text editor on this setting, the user types (or scans, or + * dictates) the URL, and the app reads it back when the load is confirmed on + * the glasses. Kept across launches so a reload after an edit-and-rebuild only + * takes a click. Deliberately not listed in the Settings app. + */ +export const developerAppUrlSetting = new ConfigSettingString({ + id: "developer-app-url", + label: "App URL", + storageKey: "developer.appUrl", + defaultValue: "", + editorTitle: "EvenHub app URL (http:// or https://)", + glassesEditTitle: "Load app from URL", + normalize: (value) => (value ?? "").replace(/[\x00-\x1f]+/g, "").trim(), +}); + export const terminalLaunchPresetsSetting = new ConfigSettingString({ id: "terminal-launch-presets", label: "Launch presets", diff --git a/app/ui/layers.ts b/app/ui/layers.ts index d3a0255b..7dc658a1 100644 --- a/app/ui/layers.ts +++ b/app/ui/layers.ts @@ -98,6 +98,13 @@ export interface Layer { handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise | void; /** Called when the layer leaves the stack by any path (pop or clearToBase). */ onRemoved?(): void; + /** + * Accept a finished text string aimed at this window (voice input, and + * anything else the shell routes through sendTextToForegroundWindow). + * Layers that have somewhere to put text implement it; the rest don't, and + * the shell then has no app destination to offer. + */ + receiveTextInput?(text: string, ctx: LayerContext): void; } export class LayerStack { @@ -191,6 +198,15 @@ export class LayerStack { await this.layers[this.layers.length - 1]!.handleInput(event, this.ctx); } + /** Hand text to the top layer; false if it doesn't take text input. */ + receiveTextInput(text: string): boolean { + const layer = this.layers[this.layers.length - 1]!; + if (!layer.receiveTextInput) return false; + layer.receiveTextInput(text, this.ctx); + return true; + } + + private paintLayer(index: number): Plane[] { const layer = this.layers[index]!; let canvas: GrayImage | null = null; diff --git a/app/ui/shell/in-process-window.ts b/app/ui/shell/in-process-window.ts index 40ba89e7..ddb8ede1 100644 --- a/app/ui/shell/in-process-window.ts +++ b/app/ui/shell/in-process-window.ts @@ -37,6 +37,12 @@ export type InProcessWindowOptions = { menuItems?: () => MenuItem[]; /** Shared actions; requestRender is rebound to this window's render. */ actions: LayerActions; + /** + * Accept text aimed at this window (voice input). Supply it for apps with a + * layer that takes dictation; leaving it out is what tells the shell not to + * offer "Type Into App" for this window. + */ + receiveTextInput?: (text: string) => void; baseLayer: Layer; submitFrame: (planes: Plane[], paintMs: number, frameId: number) => Promise; setSurfaceVisible: (visible: boolean) => void; @@ -189,6 +195,7 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces await render(frameId); }, requestRender, + receiveTextInput: options.receiveTextInput, setForeground: (foreground) => { options.setSurfaceVisible(foreground); },