diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.kt index 19d5a5a0..7e5cad69 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.kt +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.kt @@ -1,14 +1,24 @@ package com.faceclaw.app import android.app.Activity +import android.content.Context import android.content.MutableContextWrapper +import android.graphics.Color +import android.graphics.Typeface import android.os.Build import android.os.Handler import android.os.Looper +import android.text.TextUtils +import android.util.TypedValue +import android.view.Gravity import android.view.View import android.view.ViewGroup import android.webkit.WebView import android.widget.FrameLayout +import android.widget.LinearLayout +import android.widget.TextView +import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsCompat import java.util.ArrayList import java.util.HashMap @@ -34,6 +44,12 @@ import java.util.HashMap * the chosen WebView to the top of the overlay); hiding sends it back behind. * All view work happens on the main thread (callers are on the NS/JS thread). * + * Layout: the activity is edge-to-edge, so the overlay tracks the window + * insets itself. A top bar (the shown app's name and a close button) sits + * under the status bar, and the WebViews fill the area between it and the + * navigation bar / keyboard. That area is the same whether or not the overlay + * is shown, so showing an app doesn't resize (and re-lay-out) its page. + * * Timer keep-alive: when the phone screen turns off, Chromium heavily throttles * the page's own setTimeout/setInterval (intensive background throttling clamps * them to ~1/sec), so timer-driven apps (e.g. snake's game loop) slow to a @@ -71,8 +87,13 @@ import java.util.HashMap */ class FaceclawEvenHubWebViewHost { private var overlay: FrameLayout? = null + /** Holds the WebViews, inside [overlay] below the top bar. */ + private var stack: FrameLayout? = null + private var titleView: TextView? = null private var overlayActivity: Activity? = null private var shown = false + private var shownTitle = "" + private var onClose: Runnable? = null private val mainHandler = Handler(Looper.getMainLooper()) private val webViews: MutableList = ArrayList() @@ -139,6 +160,12 @@ class FaceclawEvenHubWebViewHost { private const val TICK_JS = "window.__fcTick?__fcTick():null" + /** Top bar height, below the status bar (Material toolbar height). */ + private const val BAR_HEIGHT_DP = 56f + + /** Matches the NativeScript ActionBar (NativeScriptToolbarStyle). */ + private val BAR_COLOR = Color.parseColor("#424242") + @JvmStatic @Synchronized fun getInstance(): FaceclawEvenHubWebViewHost { @@ -152,36 +179,112 @@ class FaceclawEvenHubWebViewHost { val existing = overlay if (existing != null && overlayActivity === activity && existing.parent != null) return existing val content = activity.findViewById(android.R.id.content) + (existing?.parent as ViewGroup?)?.removeView(existing) val created = FrameLayout(activity) - if (existing != null) { - (existing.parent as ViewGroup?)?.removeView(existing) - for (web in webViews) { - existing.removeView(web) - rebaseContext(web, activity) - created.addView(web, FrameLayout.LayoutParams( - ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)) - } + created.setBackgroundColor(Color.BLACK) + val createdStack = FrameLayout(activity) + created.addView(createdStack, FrameLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)) + val bar = buildTopBar(activity) + created.addView(bar, FrameLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.TOP)) + for (web in webViews) { + (web.parent as ViewGroup?)?.removeView(web) + rebaseContext(web, activity) + createdStack.addView(web, FrameLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)) + } + // Insets are passed on unconsumed: before API 30 a consuming child + // would starve its later siblings (the NativeScript UI) of them. + ViewCompat.setOnApplyWindowInsetsListener(created) { _, insets -> + applyInsets(bar, createdStack, insets) + insets } overlay = created + stack = createdStack overlayActivity = activity // index 0 = behind the NativeScript content view (raised again below if shown). content.addView(created, 0, FrameLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)) + ViewCompat.requestApplyInsets(created) if (shown) created.bringToFront() return created } + /** The bar across the top of a shown app: the app's name, then a close button. */ + private fun buildTopBar(activity: Activity): LinearLayout { + val bar = LinearLayout(activity) + bar.orientation = LinearLayout.HORIZONTAL + bar.gravity = Gravity.CENTER_VERTICAL + bar.setBackgroundColor(BAR_COLOR) + // Swallow touches so they don't fall through to the WebViews. + bar.isClickable = true + + val title = TextView(activity) + title.text = shownTitle + title.setTextColor(Color.WHITE) + title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f) + title.typeface = Typeface.create("sans-serif-medium", Typeface.NORMAL) + title.maxLines = 1 + title.ellipsize = TextUtils.TruncateAt.END + bar.addView(title, LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f).apply { + marginStart = dp(activity, 16f) + marginEnd = dp(activity, 12f) + }) + + val close = TextView(activity) + close.text = "\u2715" + close.setTextColor(Color.WHITE) + close.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f) + close.gravity = Gravity.CENTER + close.contentDescription = "Close" + val ripple = TypedValue() + if (activity.theme.resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, ripple, true)) { + close.setBackgroundResource(ripple.resourceId) + } + close.setOnClickListener { onClose?.run() } + val closeSize = dp(activity, 48f) + bar.addView(close, LinearLayout.LayoutParams(closeSize, closeSize).apply { + marginEnd = dp(activity, 4f) + }) + titleView = title + return bar + } + + /** + * Put the bar under the status bar (its background filling in behind it) + * and the WebViews between the bar and the navigation bar or keyboard. + */ + private fun applyInsets(bar: View, webStack: View, insets: WindowInsetsCompat) { + val bars = insets.getInsets( + WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout()) + val ime = insets.getInsets(WindowInsetsCompat.Type.ime()) + val barHeight = dp(bar.context, BAR_HEIGHT_DP) + bar.setPadding(bars.left, bars.top, bars.right, 0) + bar.layoutParams = (bar.layoutParams as FrameLayout.LayoutParams).apply { + height = bars.top + barHeight + } + webStack.layoutParams = (webStack.layoutParams as FrameLayout.LayoutParams).apply { + setMargins(bars.left, bars.top + barHeight, bars.right, maxOf(bars.bottom, ime.bottom)) + } + } + + private fun dp(context: Context, value: Float): Int { + return TypedValue.applyDimension( + TypedValue.COMPLEX_UNIT_DIP, value, context.resources.displayMetrics).toInt() + } + private fun rebaseContext(web: WebView, activity: Activity) { (web.context as? MutableContextWrapper)?.baseContext = activity } /** Add a WebView to the host, full-size and rendering, hidden behind the UI. */ fun attach(activity: Activity, web: WebView) { - val o = ensureOverlay(activity) + ensureOverlay(activity) rebaseContext(web, activity) web.visibility = View.VISIBLE if (web.parent == null) { - o.addView(web, FrameLayout.LayoutParams( + stack!!.addView(web, FrameLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)) } // resumeTimers() is process-global; make sure nothing left timers paused. @@ -215,9 +318,15 @@ class FaceclawEvenHubWebViewHost { } } - /** Bring an app's WebView to the front of [activity] so it is visible on the phone. */ - fun showOnPhone(activity: Activity?, web: WebView) { + /** + * Bring an app's WebView to the front of [activity] so it is visible on the + * phone, titled [title]. The top bar's close button runs [onClose]. + */ + fun showOnPhone(activity: Activity?, web: WebView, title: String, onClose: Runnable) { val o = if (activity != null) ensureOverlay(activity) else overlay ?: return + shownTitle = title + this.onClose = onClose + titleView?.text = title web.bringToFront() o.bringToFront() shown = true @@ -226,6 +335,7 @@ class FaceclawEvenHubWebViewHost { /** Send the overlay back behind the NativeScript UI. */ fun hideOnPhone() { shown = false + onClose = null val o = overlay ?: return val content = o.parent as ViewGroup? if (content != null) { @@ -242,7 +352,7 @@ class FaceclawEvenHubWebViewHost { fun detach(web: WebView) { webViews.remove(web) tickers.remove(web)?.stop() - overlay?.removeView(web) + stack?.removeView(web) web.destroy() } } diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/PreviewBitmapUtil.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/PreviewBitmapUtil.kt index 54f31c42..6c98b9b0 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/PreviewBitmapUtil.kt +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/PreviewBitmapUtil.kt @@ -30,5 +30,23 @@ class PreviewBitmapUtil private constructor() { val colors = PreviewPalette.expand(bytes, 0, width * height, brightenGamma, green) return Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888) } + + /** + * A white-on-transparent bitmap of an 8bpp glasses icon (gray becomes + * alpha; black, which the glasses show as unlit, is transparent), for + * showing glasses artwork on the phone's dark action bar. + */ + @JvmStatic + fun whiteIconFromGray(gray: ByteBuffer?, width: Int, height: Int): Bitmap { + if (gray == null || width <= 0 || height <= 0 || gray.remaining() < width * height) { + throw IllegalArgumentException("invalid gray icon buffer") + } + val bytes = ByteArray(width * height) + gray.get(bytes) + val colors = IntArray(width * height) { i -> + ((bytes[i].toInt() and 0xff) shl 24) or 0x00ffffff + } + return Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888) + } } } diff --git a/app/app.css b/app/app.css index 49c6dbee..f493f6d4 100644 --- a/app/app.css +++ b/app/app.css @@ -16,8 +16,9 @@ ActionItem { color: #ffffff; } -/* The main page's custom titleView: title left, connection status and the - * warning triangle right-aligned beside the overflow menu. */ +/* The main page's custom titleView: title left; the EvenHub app icon, + * connection status and the warning triangle right-aligned beside the + * overflow menu. */ .action-bar-title { color: #ffffff; font-size: 18; @@ -32,6 +33,16 @@ ActionItem { vertical-align: center; } +/* The foreground EvenHub app's icon (opens its phone UI); the padding widens + * the tap target. */ +.action-bar-app-icon { + width: 32; + height: 32; + padding: 5.5; + margin-right: 8; + vertical-align: center; +} + /* The emoji brings its own yellow; the padding widens the tap target. */ .action-bar-warning { font-size: 18; diff --git a/app/apps/evenhub/manager.ts b/app/apps/evenhub/manager.ts index 7b3b327c..0b12dabe 100644 --- a/app/apps/evenhub/manager.ts +++ b/app/apps/evenhub/manager.ts @@ -6,11 +6,12 @@ * * 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"), overlaying the dashboard, and the Back - * button returns to it. Apps are kept alive until explicitly closed (no memory - * eviction yet). + * (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 } from "@nativescript/core"; +import { Application, type ImageSource } from "@nativescript/core"; import { type AppContext } from "../app-definition"; import { appFilesDirPath, @@ -23,6 +24,8 @@ 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, @@ -36,12 +39,31 @@ type RunningApp = { 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 { @@ -112,6 +134,7 @@ async function startApp( if (phoneShownWindowId === windowId) { webView.hideOnPhone(); phoneShownWindowId = null; + notifyPhoneShownChanged(); } webView.destroy(); cleanup?.(); @@ -205,15 +228,35 @@ export function showOnPhone(windowId: string): void { const app = running.get(windowId); if (!app) return; if (phoneShownWindowId && phoneShownWindowId !== windowId) hidePhone(); - app.webView.showOnPhone(); + app.webView.showOnPhone(hidePhone); phoneShownWindowId = windowId; + notifyPhoneShownChanged(); } -/** Return the phone to the dashboard (Back, or the shown app closing). */ +/** 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 { diff --git a/app/apps/evenhub/webview.ts b/app/apps/evenhub/webview.ts index 06c0f0d3..655822ac 100644 --- a/app/apps/evenhub/webview.ts +++ b/app/apps/evenhub/webview.ts @@ -22,11 +22,13 @@ import { FACECLAW_VERSION } from "../../version"; declare const android: any; declare const com: any; +declare const java: any; export type EvenHubWebView = { evaluateJs: (js: string) => void; destroy: () => void; - showOnPhone: () => void; + /** Show the app's phone UI; `onClose` runs when its top bar's close button is tapped. */ + showOnPhone: (onClose: () => void) => void; hideOnPhone: () => void; /** The native android.webkit.WebView, for the host. */ native: unknown; @@ -118,8 +120,13 @@ export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView { console.warn(`evenhub webview destroy failed: ${error}`); } }, - showOnPhone: () => - nativeHost.showOnPhone(Application.android?.foregroundActivity ?? Application.android?.startActivity ?? null, webView), + showOnPhone: (onClose: () => void) => + nativeHost.showOnPhone( + Application.android?.foregroundActivity ?? Application.android?.startActivity ?? null, + webView, + session.manifest.name, + new java.lang.Runnable({ run: onClose }), + ), hideOnPhone: () => nativeHost.hideOnPhone(), }; } diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index 1f1c3648..87b04040 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -78,7 +78,14 @@ import { installedEvenHubPackageId, uninstallEvenHubPackage, } from "../apps/evenhub/installed-apps"; -import { closeRunningPackage, launchInstalledPackage } from "../apps/evenhub/manager"; +import { + closeRunningPackage, + launchInstalledPackage, + onPhoneShownChanged, + phoneUiButton, + showOnPhone as showEvenHubPhoneUi, + type PhoneUiButton, +} from "../apps/evenhub/manager"; import { openEvenHubStoreForPackage } from "../apps/evenhub"; import { isInstalledPackagePresent } from "../apps/evenhub/updates"; import { wearerVerificationOptions } from "../apps/microphones/speakers"; @@ -133,6 +140,11 @@ export type DashboardSnapshot = { * nothing is paired, so "Disconnected" would be the wrong label. */ previewMode: boolean; + /** + * The foreground glasses window's EvenHub phone UI, when it can be opened + * (the action bar's app-icon button); null otherwise. + */ + evenHubPhoneUi: PhoneUiButton | null; }; type DashboardListener = (snapshot: DashboardSnapshot) => void; @@ -361,6 +373,8 @@ class DashboardController { launchApp: (appId) => this.launchApp(appId), requestShellRender: () => this.requestShellRender(), }); + // The action bar's EvenHub phone-UI button hides while that UI is showing. + onPhoneShownChanged(() => this.emit()); shell.configure({ actions: { ...sharedActions, @@ -1025,9 +1039,15 @@ class DashboardController { fontsMissingWarningVisible: this.fontsMissingWarningVisible, alarmReliabilityMessage: this.alarmReliabilityMessage, previewMode: this.isPreviewDisplayActive(), + evenHubPhoneUi: phoneUiButton(), }; } + /** Overlay an EvenHub app's phone UI (the action bar's app-icon button). */ + showEvenHubPhoneUi(windowId: string): void { + showEvenHubPhoneUi(windowId); + } + /** * Message to show in place of the display preview, or "" to show the preview. * diff --git a/app/native/gray-image-preview.ts b/app/native/gray-image-preview.ts index fc8b1656..e59c2655 100644 --- a/app/native/gray-image-preview.ts +++ b/app/native/gray-image-preview.ts @@ -27,3 +27,21 @@ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null { ); return new ImageSource(bitmap); } + +/** + * A glasses icon as a white-on-transparent phone image (gray becomes alpha), + * for the dark action bar. + */ +export function grayImageToWhiteIconSource(image: GrayImage): ImageSource | null { + if (!global.isAndroid) { + return null; + } + + const baked = image.withDrawsBaked(); + const bitmap = com.faceclaw.app.PreviewBitmapUtil.whiteIconFromGray( + copyToJavaByteBuffer(baked.pixels), + baked.width, + baked.height, + ); + return new ImageSource(bitmap); +} diff --git a/app/phone-ui/main-page.xml b/app/phone-ui/main-page.xml index e80b711b..8b7d5cfb 100644 --- a/app/phone-ui/main-page.xml +++ b/app/phone-ui/main-page.xml @@ -1,11 +1,13 @@ - - + + diff --git a/app/phone-ui/main-view-model.ts b/app/phone-ui/main-view-model.ts index 407a1948..04bdc9f3 100644 --- a/app/phone-ui/main-view-model.ts +++ b/app/phone-ui/main-view-model.ts @@ -25,6 +25,7 @@ import { isAutoReconnectSuppressed, resumeAutoReconnect } from "../g2/reconnect- import { isPreviewOnlyMode } from "./onboarding-state"; import { formatErrorMessage } from "../util/format-error"; import { G2_LENS_HEIGHT, G2_LENS_WIDTH } from "../graphics/image"; +import { type PhoneUiButton } from "../apps/evenhub/manager"; const LENS_ASPECT_RATIO = G2_LENS_WIDTH / G2_LENS_HEIGHT; @@ -63,6 +64,7 @@ export class MainViewModel extends RemoteControlsViewModel { private _alarmReliabilityMessage = ""; private _warningsModalVisible = false; private _previewMode = false; + private _evenHubPhoneUi: PhoneUiButton | null = null; private _phase: "disconnected" | "connecting" | "connected" | "charging" | "disconnecting" = "disconnected"; // A new view model is built on every navigation to the main page; these @@ -112,6 +114,7 @@ export class MainViewModel extends RemoteControlsViewModel { this.batteryOptimizationWarningVisible = snapshot.batteryOptimizationWarningVisible; this.fontsMissingWarningVisible = snapshot.fontsMissingWarningVisible; this.alarmReliabilityMessage = snapshot.alarmReliabilityMessage; + this.evenHubPhoneUi = snapshot.evenHubPhoneUi; this.refreshPadFocusLine(); })); // Brightness / display mode can change from the glasses' Settings app too. @@ -773,6 +776,28 @@ export class MainViewModel extends RemoteControlsViewModel { return this._warningsModalVisible ? "visible" : "collapse"; } + set evenHubPhoneUi(value: PhoneUiButton | null) { + const current = this._evenHubPhoneUi; + if (current?.windowId === value?.windowId && current?.icon === value?.icon) return; + this._evenHubPhoneUi = value; + this.notifyPropertyChange("evenHubPhoneUiIcon", this.evenHubPhoneUiIcon); + this.notifyPropertyChange("evenHubPhoneUiVisibility", this.evenHubPhoneUiVisibility); + } + + /** The foreground EvenHub app's icon, which opens its phone UI when tapped. */ + get evenHubPhoneUiIcon(): ImageSource | null { + return this._evenHubPhoneUi?.icon ?? null; + } + + get evenHubPhoneUiVisibility(): "visible" | "collapse" { + return this._evenHubPhoneUi?.icon ? "visible" : "collapse"; + } + + onEvenHubPhoneUiTap(): void { + const windowId = this._evenHubPhoneUi?.windowId; + if (windowId) dashboardController.showEvenHubPhoneUi(windowId); + } + onWarningIconTap(): void { this.setWarningsModalVisible(true); }