diff --git a/App_Resources/Android/src/main/AndroidManifest.xml b/App_Resources/Android/src/main/AndroidManifest.xml index 5dfb457e..4b6764f3 100644 --- a/App_Resources/Android/src/main/AndroidManifest.xml +++ b/App_Resources/Android/src/main/AndroidManifest.xml @@ -48,6 +48,11 @@ + + + + + emittedGroupKeys = new HashSet<>(); int emitted = 0; for (StatusBarNotification statusBarNotification : notifications) { if (!shouldShowNotificationIcon(service, statusBarNotification)) { continue; } + String dedupeGroupKey = getNotificationDedupeGroupKey(statusBarNotification); + if (dedupeGroupKey != null && emittedGroupKeys.contains(dedupeGroupKey)) { + continue; + } Drawable drawable = loadNotificationIcon(service, statusBarNotification.getNotification()); if (drawable == null) { continue; } appendIconGrayBytes(drawable, size, out); + if (dedupeGroupKey != null) { + emittedGroupKeys.add(dedupeGroupKey); + } emitted += 1; if (emitted >= limit) { break; @@ -123,7 +141,108 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen return out.toByteArray(); } + public static String getActiveNotificationsJson(int maxNotifications) { + FaceclawMediaNotificationListenerService service = activeService; + int limit = Math.max(0, Math.min(100, maxNotifications)); + if (service == null || limit == 0) { + return "[]"; + } + + StatusBarNotification[] notifications; + try { + notifications = service.getActiveNotifications(); + } catch (SecurityException e) { + Log.w(TAG, "notification access denied while reading notifications", e); + return "[]"; + } catch (Throwable t) { + Log.w(TAG, "failed to read notifications", t); + return "[]"; + } + if (notifications == null || notifications.length == 0) { + return "[]"; + } + + Arrays.sort(notifications, new Comparator() { + @Override + public int compare(StatusBarNotification a, StatusBarNotification b) { + long left = a == null ? 0 : a.getPostTime(); + long right = b == null ? 0 : b.getPostTime(); + return Long.compare(right, left); + } + }); + + JSONArray out = new JSONArray(); + for (StatusBarNotification statusBarNotification : notifications) { + if (out.length() >= limit) { + break; + } + if (!shouldShowNotificationInList(service, statusBarNotification)) { + continue; + } + try { + out.put(buildNotificationJson(service, statusBarNotification)); + } catch (Throwable t) { + Log.w(TAG, "failed to serialize notification", t); + } + } + return out.toString(); + } + + public static boolean invokeNotificationAction(String key, int actionIndex) { + FaceclawMediaNotificationListenerService service = activeService; + StatusBarNotification statusBarNotification = findActiveNotificationByKey(service, key); + if (statusBarNotification == null || statusBarNotification.getNotification() == null) { + return false; + } + Notification.Action[] actions = statusBarNotification.getNotification().actions; + if (actions == null || actionIndex < 0 || actionIndex >= actions.length) { + return false; + } + PendingIntent intent = actions[actionIndex].actionIntent; + if (intent == null) { + return false; + } + try { + intent.send(); + return true; + } catch (PendingIntent.CanceledException e) { + Log.w(TAG, "notification action pending intent was canceled", e); + return false; + } catch (Throwable t) { + Log.w(TAG, "failed to invoke notification action", t); + return false; + } + } + + public static boolean dismissNotification(String key) { + FaceclawMediaNotificationListenerService service = activeService; + if (service == null || key == null || key.isEmpty()) { + return false; + } + try { + service.cancelNotification(key); + return true; + } catch (SecurityException e) { + Log.w(TAG, "notification access denied while dismissing notification", e); + return false; + } catch (Throwable t) { + Log.w(TAG, "failed to dismiss notification", t); + return false; + } + } + private static boolean shouldShowNotificationIcon(FaceclawMediaNotificationListenerService service, StatusBarNotification statusBarNotification) { + if (!shouldShowNotificationInList(service, statusBarNotification)) { + return false; + } + Notification notification = statusBarNotification.getNotification(); + if ((notification.flags & Notification.FLAG_GROUP_SUMMARY) != 0) { + return false; + } + return true; + } + + private static boolean shouldShowNotificationInList(FaceclawMediaNotificationListenerService service, StatusBarNotification statusBarNotification) { if (statusBarNotification == null || statusBarNotification.getNotification() == null) { return false; } @@ -131,9 +250,6 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen return false; } Notification notification = statusBarNotification.getNotification(); - if ((notification.flags & Notification.FLAG_GROUP_SUMMARY) != 0) { - return false; - } if (Notification.CATEGORY_TRANSPORT.equals(notification.category)) { return false; } @@ -154,6 +270,19 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen return importance > NotificationManager.IMPORTANCE_MIN; } + private static String getNotificationDedupeGroupKey(StatusBarNotification statusBarNotification) { + Notification notification = statusBarNotification.getNotification(); + if (notification.getGroup() == null && statusBarNotification.getOverrideGroupKey() == null) { + return null; + } + String groupKey = statusBarNotification.getGroupKey(); + if (groupKey == null || groupKey.isEmpty()) { + return null; + } + // Group children often share the same small icon. Emit only one icon for the group. + return groupKey; + } + private static Drawable loadNotificationIcon(FaceclawMediaNotificationListenerService service, Notification notification) { try { Icon smallIcon = notification.getSmallIcon(); @@ -193,4 +322,128 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen } bitmap.recycle(); } + + private static StatusBarNotification findActiveNotificationByKey(FaceclawMediaNotificationListenerService service, String key) { + if (service == null || key == null || key.isEmpty()) { + return null; + } + StatusBarNotification[] notifications; + try { + notifications = service.getActiveNotifications(); + } catch (Throwable t) { + Log.w(TAG, "failed to find active notification", t); + return null; + } + if (notifications == null) { + return null; + } + for (StatusBarNotification statusBarNotification : notifications) { + if (statusBarNotification != null && key.equals(statusBarNotification.getKey())) { + return statusBarNotification; + } + } + return null; + } + + private static JSONObject buildNotificationJson(FaceclawMediaNotificationListenerService service, StatusBarNotification statusBarNotification) + throws JSONException { + Notification notification = statusBarNotification.getNotification(); + Bundle extras = notification.extras; + JSONObject out = new JSONObject(); + out.put("key", statusBarNotification.getKey()); + out.put("packageName", statusBarNotification.getPackageName()); + out.put("appName", getNotificationAppName(service, statusBarNotification)); + out.put("postTime", statusBarNotification.getPostTime()); + out.put("when", notification.when); + putString(out, "category", notification.category); + if (extras != null) { + putCharSequence(out, "title", firstNonEmpty( + extras.getCharSequence(Notification.EXTRA_TITLE_BIG), + extras.getCharSequence(Notification.EXTRA_TITLE) + )); + putCharSequence(out, "text", extras.getCharSequence(Notification.EXTRA_TEXT)); + putCharSequence(out, "bigText", extras.getCharSequence(Notification.EXTRA_BIG_TEXT)); + putCharSequence(out, "subText", extras.getCharSequence(Notification.EXTRA_SUB_TEXT)); + putCharSequence(out, "infoText", extras.getCharSequence(Notification.EXTRA_INFO_TEXT)); + putCharSequence(out, "summaryText", extras.getCharSequence(Notification.EXTRA_SUMMARY_TEXT)); + CharSequence[] textLines = extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES); + JSONArray lines = new JSONArray(); + if (textLines != null) { + for (CharSequence line : textLines) { + String text = charSequenceToString(line); + if (!text.isEmpty()) { + lines.put(text); + } + } + } + out.put("lines", lines); + } else { + out.put("lines", new JSONArray()); + } + + JSONArray actionsJson = new JSONArray(); + Notification.Action[] actions = notification.actions; + if (actions != null) { + for (int index = 0; index < actions.length; index++) { + Notification.Action action = actions[index]; + if (action == null) { + continue; + } + String title = charSequenceToString(action.title); + if (title.isEmpty()) { + continue; + } + JSONObject actionJson = new JSONObject(); + actionJson.put("index", index); + actionJson.put("title", title); + actionJson.put("enabled", action.actionIntent != null); + actionsJson.put(actionJson); + } + } + out.put("actions", actionsJson); + return out; + } + + private static String getNotificationAppName(FaceclawMediaNotificationListenerService service, StatusBarNotification statusBarNotification) { + Notification notification = statusBarNotification.getNotification(); + Bundle extras = notification.extras; + if (extras != null) { + String substituteName = charSequenceToString(extras.getCharSequence(EXTRA_SUBSTITUTE_APP_NAME)); + if (!substituteName.isEmpty()) { + return substituteName; + } + } + return getAppLabel(service, statusBarNotification.getPackageName()); + } + + private static String getAppLabel(FaceclawMediaNotificationListenerService service, String packageName) { + if (packageName == null || packageName.isEmpty()) { + return ""; + } + try { + CharSequence label = service + .getPackageManager() + .getApplicationLabel(service.getPackageManager().getApplicationInfo(packageName, 0)); + String text = charSequenceToString(label); + return text.isEmpty() ? packageName : text; + } catch (Throwable t) { + return packageName; + } + } + + private static void putString(JSONObject out, String key, String value) throws JSONException { + out.put(key, value == null ? "" : value); + } + + private static void putCharSequence(JSONObject out, String key, CharSequence value) throws JSONException { + out.put(key, charSequenceToString(value)); + } + + private static CharSequence firstNonEmpty(CharSequence first, CharSequence second) { + return charSequenceToString(first).isEmpty() ? second : first; + } + + private static String charSequenceToString(CharSequence value) { + return value == null ? "" : value.toString(); + } } diff --git a/app/app.ts b/app/app.ts index 83d01785..d9bd4f6f 100644 --- a/app/app.ts +++ b/app/app.ts @@ -5,6 +5,9 @@ purpose of the file is to pass control to the app’s first module. */ import { Application } from '@nativescript/core' +import { registerShareIntentHandler } from './native/share-intents' + +registerShareIntentHandler() Application.run({ moduleName: 'app-root' }) diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index 1ac5ec14..0f14ab0e 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -18,6 +18,7 @@ import { getDashboardSystemCardName, isDashboardVoiceControlEnabled, noteDashboardPhoneTextInput, + openTelepromptDocument, receiveInput, resetDashboardSleepTimerAndWake, setDashboardBatteryLevels, @@ -483,6 +484,17 @@ class DashboardController { await this.handleInputEvent(event); } + async openTelepromptDocument(text: string): Promise { + openTelepromptDocument(text); + this.appendLog(`teleprompt document received (${text.length} chars)`); + if (this.phase === "connected" && this.communicator) { + await this.requestRender("interval"); + return; + } + const image = drawDashboard(); + this.updateDisplayPreviewFromImage(image); + } + private startSystemNameEdit(): void { this.editingSystemCardName = true; this.activeTextSettingEditorKind = "dashboard-name"; diff --git a/app/graphics/textwrap.ts b/app/graphics/textwrap.ts index 60788c54..51043eb3 100644 --- a/app/graphics/textwrap.ts +++ b/app/graphics/textwrap.ts @@ -1,5 +1,10 @@ import { BdfFont } from "./bdffont"; +export type WrapTextOptions = { + preserveLeadingWhitespace?: boolean; + breakLongWords?: boolean; +}; + export function findWrapOpportunities(text: string): number[] { const offsets: number[] = []; for (let i = 0; i < text.length; ) { @@ -29,7 +34,7 @@ export function findWrapOpportunities(text: string): number[] { return offsets; } -export function wrapText(font: BdfFont, text: string, targetWidth: number): string[] { +export function wrapText(font: BdfFont, text: string, targetWidth: number, opts: WrapTextOptions = {}): string[] { if (targetWidth <= 0) { return text.length ? text.split("\n") : [""]; } @@ -37,20 +42,23 @@ export function wrapText(font: BdfFont, text: string, targetWidth: number): stri const paragraphs = text.replace(/\r/g, "").split("\n"); const lines: string[] = []; for (const paragraph of paragraphs) { - lines.push(...wrapParagraph(font, paragraph, targetWidth)); + lines.push(...wrapParagraph(font, paragraph, targetWidth, opts)); } return lines.length ? lines : [""]; } -function wrapParagraph(font: BdfFont, paragraph: string, targetWidth: number): string[] { +function wrapParagraph(font: BdfFont, paragraph: string, targetWidth: number, opts: WrapTextOptions): string[] { if (paragraph.length === 0) { return [""]; } const widths = measureOffsets(font, paragraph); const wrapOffsets = findWrapOpportunities(paragraph); + if (wrapOffsets[wrapOffsets.length - 1] !== paragraph.length) { + wrapOffsets.push(paragraph.length); + } const lines: string[] = []; - let start = skipLeadingWhitespace(paragraph, 0); + let start = opts.preserveLeadingWhitespace ? 0 : skipLeadingWhitespace(paragraph, 0); while (start < paragraph.length) { let best = -1; @@ -63,16 +71,21 @@ function wrapParagraph(font: BdfFont, paragraph: string, targetWidth: number): s break; } + const splitOffset = furthestFittingOffset(widths, start, targetWidth); + if (opts.breakLongWords && splitOffset > start && (best < 0 || splitOffset > trimEndOffset(paragraph, best))) { + best = splitOffset; + } if (best < 0) { - best = furthestFittingOffset(widths, start, targetWidth); + best = splitOffset; } if (best <= start) { best = nextOffset(widths, start); } - const line = paragraph.slice(start, best).trimEnd(); + const lineEnd = trimEndOffset(paragraph, best); + const line = paragraph.slice(start, lineEnd); lines.push(line); - start = skipLeadingWhitespace(paragraph, best); + start = opts.preserveLeadingWhitespace && lines.length === 1 ? best : skipLeadingWhitespace(paragraph, best); } return lines.length ? lines : [""]; @@ -130,6 +143,30 @@ function skipLeadingWhitespace(text: string, offset: number): number { return cursor; } +function trimEndOffset(text: string, offset: number): number { + let cursor = offset; + while (cursor > 0) { + const previous = previousOffset(text, cursor); + const char = text.slice(previous, cursor); + if (!isWhitespace(char) || char === "\n") { + break; + } + cursor = previous; + } + return cursor; +} + +function previousOffset(text: string, offset: number): number { + let cursor = 0; + let previous = 0; + while (cursor < offset) { + previous = cursor; + const codePoint = text.codePointAt(cursor) ?? 0; + cursor += String.fromCodePoint(codePoint).length; + } + return previous; +} + function isWhitespace(char: string): boolean { return /\s/.test(char); } diff --git a/app/native/faceclaw-communicator.ts b/app/native/faceclaw-communicator.ts index 9f0adf9d..e3abd898 100644 --- a/app/native/faceclaw-communicator.ts +++ b/app/native/faceclaw-communicator.ts @@ -2,8 +2,6 @@ import { Utils } from "@nativescript/core"; declare const com: any; -const FRAME_METRICS_LISTENER_MIN_INTERVAL_MS = 5_000; - export type CommunicatorPhase = | "disconnected" | "connecting" @@ -75,7 +73,6 @@ export class FaceclawCommunicatorBridge { private readonly batteryListeners = new Set<(state: HeadsetBatteryState) => void>(); private readonly evenAppConflictListeners = new Set<(message: string) => void>(); private readonly frameMetricsListeners = new Set<(metrics: FrameMetrics) => void>(); - private lastFrameMetricsListenerEmitAtMs = 0; constructor(addresses: { right: string; left: string; ring?: string }) { const context = Utils.android.getApplicationContext(); @@ -135,14 +132,7 @@ export class FaceclawCommunicatorBridge { for (const waiter of waiters) { setTimeout(() => waiter(metrics), 0); } - const now = Date.now(); - if ( - this.lastFrameMetricsListenerEmitAtMs === 0 || - now - this.lastFrameMetricsListenerEmitAtMs >= FRAME_METRICS_LISTENER_MIN_INTERVAL_MS - ) { - this.lastFrameMetricsListenerEmitAtMs = now; - this.emitAsync(this.frameMetricsListeners, metrics); - } + this.emitAsync(this.frameMetricsListeners, metrics); }, }); this.communicator.setListener(this.listenerProxy); diff --git a/app/native/notification-icons.ts b/app/native/notification-icons.ts index 8c4590cf..e38d6c87 100644 --- a/app/native/notification-icons.ts +++ b/app/native/notification-icons.ts @@ -9,6 +9,29 @@ const ICON_CACHE_MS = 5_000; let cachedIcons: GrayImage[] = []; let cachedAtMs = 0; +export type AndroidNotificationAction = { + index: number; + title: string; + enabled: boolean; +}; + +export type AndroidNotification = { + key: string; + packageName: string; + appName: string; + title: string; + text: string; + bigText: string; + subText: string; + infoText: string; + summaryText: string; + category: string; + lines: string[]; + postTime: number; + when: number; + actions: AndroidNotificationAction[]; +}; + export function readActiveNotificationIcons(maxIcons: number): GrayImage[] { if (!global.isAndroid || maxIcons <= 0) return []; @@ -36,3 +59,76 @@ export function readActiveNotificationIcons(maxIcons: number): GrayImage[] { cachedAtMs = now; return icons.map(icon => icon.clone()); } + +export function readActiveNotifications(maxNotifications = 50): AndroidNotification[] { + if (!global.isAndroid || maxNotifications <= 0) return []; + try { + const json = String( + com.faceclaw.app.FaceclawMediaNotificationListenerService.getActiveNotificationsJson( + Math.max(0, Math.round(maxNotifications)), + ), + ); + const parsed = JSON.parse(json); + if (!Array.isArray(parsed)) return []; + return parsed.map(normalizeNotification).filter((item): item is AndroidNotification => Boolean(item)); + } catch { + return []; + } +} + +export function invokeNotificationAction(notificationKey: string, actionIndex: number): boolean { + if (!global.isAndroid || !notificationKey) return false; + cachedAtMs = 0; + return Boolean( + com.faceclaw.app.FaceclawMediaNotificationListenerService.invokeNotificationAction( + notificationKey, + Math.round(actionIndex), + ), + ); +} + +export function dismissNotification(notificationKey: string): boolean { + if (!global.isAndroid || !notificationKey) return false; + cachedAtMs = 0; + return Boolean( + com.faceclaw.app.FaceclawMediaNotificationListenerService.dismissNotification(notificationKey), + ); +} + +function normalizeNotification(value: any): AndroidNotification | null { + if (!value || typeof value !== "object") return null; + const key = String(value.key ?? ""); + if (!key) return null; + const actions = Array.isArray(value.actions) + ? value.actions.map(normalizeAction).filter((item): item is AndroidNotificationAction => Boolean(item)) + : []; + return { + key, + packageName: String(value.packageName ?? ""), + appName: String(value.appName ?? value.packageName ?? ""), + title: String(value.title ?? ""), + text: String(value.text ?? ""), + bigText: String(value.bigText ?? ""), + subText: String(value.subText ?? ""), + infoText: String(value.infoText ?? ""), + summaryText: String(value.summaryText ?? ""), + category: String(value.category ?? ""), + lines: Array.isArray(value.lines) ? value.lines.map((line: unknown) => String(line)).filter(Boolean) : [], + postTime: Number(value.postTime) || 0, + when: Number(value.when) || 0, + actions, + }; +} + +function normalizeAction(value: any): AndroidNotificationAction | null { + if (!value || typeof value !== "object") return null; + const title = String(value.title ?? ""); + if (!title) return null; + const index = Number(value.index); + if (!Number.isFinite(index)) return null; + return { + index, + title, + enabled: Boolean(value.enabled), + }; +} diff --git a/app/native/share-intents.ts b/app/native/share-intents.ts new file mode 100644 index 00000000..a134ea91 --- /dev/null +++ b/app/native/share-intents.ts @@ -0,0 +1,94 @@ +import { Application, isAndroid } from "@nativescript/core"; + +const MAX_SHARED_TEXT_CHARS = 200_000; + +export function registerShareIntentHandler(): void { + if (!isAndroid) { + return; + } + + Application.on(Application.launchEvent, (args) => { + const intent = (args as { android?: android.content.Intent }).android; + if (intent) { + void handleShareIntent(intent); + } + }); + + Application.android.on(Application.android.activityNewIntentEvent, (args) => { + const intent = (args as { intent?: android.content.Intent }).intent; + if (intent) { + Application.android.foregroundActivity?.setIntent(intent); + void handleShareIntent(intent); + } + }); +} + +async function handleShareIntent(intent: android.content.Intent): Promise { + const text = readPlainTextShare(intent); + if (text === null) { + return; + } + const { dashboardController } = await import("../g2/dashboard-controller"); + await dashboardController.openTelepromptDocument(text); +} + +function readPlainTextShare(intent: android.content.Intent): string | null { + if (intent.getAction() !== android.content.Intent.ACTION_SEND) { + return null; + } + + const mimeType = intent.getType(); + if (mimeType !== null && !mimeType.toLowerCase().startsWith("text/")) { + return null; + } + + const directText = intent.getCharSequenceExtra(android.content.Intent.EXTRA_TEXT); + if (directText !== null) { + return limitSharedText(String(directText)); + } + + const stream = intent.getParcelableExtra(android.content.Intent.EXTRA_STREAM) as android.net.Uri | null; + if (stream === null) { + return null; + } + + return readTextUri(stream); +} + +function readTextUri(uri: android.net.Uri): string | null { + const activity = Application.android.foregroundActivity ?? Application.android.startActivity; + const resolver = activity?.getContentResolver(); + if (!resolver) { + return null; + } + + const inputStream = resolver.openInputStream(uri); + if (inputStream === null) { + return null; + } + + const reader = new java.io.BufferedReader( + new java.io.InputStreamReader(inputStream, java.nio.charset.Charset.forName("UTF-8")), + ); + try { + const lines: string[] = []; + let totalLength = 0; + let line: string | null; + while ((line = reader.readLine()) !== null) { + const next = lines.length === 0 ? line : `\n${line}`; + if (totalLength + next.length > MAX_SHARED_TEXT_CHARS) { + lines.push(next.slice(0, MAX_SHARED_TEXT_CHARS - totalLength)); + break; + } + lines.push(next); + totalLength += next.length; + } + return lines.join(""); + } finally { + reader.close(); + } +} + +function limitSharedText(text: string): string { + return text.length <= MAX_SHARED_TEXT_CHARS ? text : text.slice(0, MAX_SHARED_TEXT_CHARS); +} diff --git a/app/ui/apps/teleprompt.ts b/app/ui/apps/teleprompt.ts new file mode 100644 index 00000000..db44a4f3 --- /dev/null +++ b/app/ui/apps/teleprompt.ts @@ -0,0 +1,96 @@ +import { wrapText } from "../../graphics/textwrap"; +import { type BdfFont } from "../../graphics/bdffont"; +import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../../graphics/image"; +import { Layer, type DashboardInputEvent, type LayerContext } from "../layers"; + +const MARGIN_X = 18; +const TITLE_Y = 16; +const BODY_X = 18; +const BODY_Y = 44; +const BODY_WIDTH = G2_LENS_WIDTH - BODY_X - 12; +const LINE_STEP = 14; +const FOOTER_Y = 252; +const BODY_LINE_COUNT = Math.floor((FOOTER_Y - BODY_Y) / LINE_STEP); +const PAGE_STEP = Math.max(1, BODY_LINE_COUNT - 1); + +export class TelepromptLayer implements Layer { + private lines: string[] | null = null; + private firstLine = 0; + + constructor(private readonly documentText: string | null) {} + + paint(ctx: LayerContext): GrayImage { + const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0); + image.drawRect(12, 12, G2_LENS_WIDTH - 24, G2_LENS_HEIGHT - 24, 52); + image.drawText(ctx.font, MARGIN_X + 4, TITLE_Y, "Teleprompt", 220); + + if (this.documentText === null) { + const message = [ + "Open a plain text document on your phone and Share it to Faceclaw.", + ]; + for (let index = 0; index < message.length; index++) { + image.drawText(ctx.font, BODY_X, BODY_Y + index * LINE_STEP, message[index]!, 200); + } + return image; + } + + const lines = this.getLines(ctx.font); + const visibleLines = lines.slice(this.firstLine, this.firstLine + BODY_LINE_COUNT); + for (let index = 0; index < visibleLines.length; index++) { + image.drawText(ctx.font, BODY_X, BODY_Y + index * LINE_STEP, visibleLines[index]!, 230); + } + + const currentPage = Math.floor(this.firstLine / PAGE_STEP) + 1; + const totalPages = totalPageCount(lines.length); + image.drawText(ctx.font, BODY_X, FOOTER_Y, `Page ${currentPage}/${totalPages}`, 110); + return image; + } + + handleInput(event: DashboardInputEvent, ctx: LayerContext): void { + switch (event.type) { + case "scroll-down": + this.scrollBy(PAGE_STEP); + return; + case "scroll-up": + this.scrollBy(-PAGE_STEP); + return; + case "double-click": + ctx.stack.pop(); + return; + default: + return; + } + } + + private scrollBy(delta: number): void { + const lines = this.lines ?? []; + if (lines.length <= BODY_LINE_COUNT) { + this.firstLine = 0; + return; + } + const maxFirstLine = Math.max(0, lines.length - 1); + this.firstLine = Math.max(0, Math.min(maxFirstLine, this.firstLine + delta)); + } + + private getLines(font: BdfFont): string[] { + if (this.lines === null) { + this.lines = wrapTextForTeleprompt(font, this.documentText ?? ""); + } + return this.lines; + } +} + +function wrapTextForTeleprompt(font: BdfFont, text: string): string[] { + const normalized = text.replace(/\t/g, " ").replace(/\r/g, ""); + return wrapText(font, normalized, BODY_WIDTH, { + preserveLeadingWhitespace: true, + breakLongWords: true, + }); +} + +function totalPageCount(lineCount: number): number { + if (lineCount <= BODY_LINE_COUNT) { + return 1; + } + return Math.ceil((lineCount - BODY_LINE_COUNT) / PAGE_STEP) + 1; +} diff --git a/app/ui/dashboard.ts b/app/ui/dashboard.ts index 504a6453..4c2715f2 100644 --- a/app/ui/dashboard.ts +++ b/app/ui/dashboard.ts @@ -50,7 +50,9 @@ import { import { Layer, LayerActions, LayerStack, type DashboardInputEvent, type LayerContext } from "./layers"; import { drawRightValueMenuItem, drawToggleMenuItem, MenuLayer } from "./menu"; import { StopwatchLayer } from "./apps/stopwatch"; +import { NotificationsListLayer } from "./notifications"; import { TranscribeLayer } from "./apps/transcribe"; +import { TelepromptLayer } from "./apps/teleprompt"; type DashboardCardId = "system" | DashboardSlotId; export type DashboardBatteryLevels = { @@ -70,6 +72,7 @@ let dashboardState = { wakeMode: loadWakeModeSetting(), voiceControlEnabled: loadVoiceControlEnabled(), tiledWakePaintPending: false, + telepromptDocumentText: null as string | null, nightscoutSettings: loadNightscoutSettings(), battery: { headset: null, @@ -253,6 +256,17 @@ export function noteDashboardPhoneTextInput(nowMs = Date.now()): void { dashboardState.lastInputAtMs = nowMs; } +export function openTelepromptDocument(text?: string): void { + if (text !== undefined) { + dashboardState.telepromptDocumentText = text; + } + dashboardState.lastInputAtMs = Date.now(); + dashboardState.screenOn = true; + dashboardLayers.clearToBase(); + dashboardLayers.push(createRootMenuLayer()); + dashboardLayers.push(new TelepromptLayer(dashboardState.telepromptDocumentText)); +} + export function resetDashboardSleepTimerAndWake(nowMs = Date.now()): boolean { dashboardState.lastInputAtMs = nowMs; if (dashboardState.screenOn) return false; @@ -448,7 +462,7 @@ class RootMenuLayer extends MenuLayer { function createRootMenuLayer(): MenuLayer { return new RootMenuLayer( - "Menu", + null, [ { label: "Apps", @@ -456,6 +470,15 @@ function createRootMenuLayer(): MenuLayer { ctx.stack.push(createAppsMenuLayer()); }, }, + { + label: "Notifications", + onSelect: (ctx) => { + ctx.stack.push(new NotificationsListLayer((ctx) => { + ctx.stack.clearToBase(); + ctx.stack.push(createRootMenuLayer()); + })); + }, + }, { label: "Settings", onSelect: (ctx) => { @@ -488,6 +511,12 @@ function createAppsMenuLayer(): MenuLayer { return new MenuLayer( "Apps", [ + { + label: "Teleprompt", + onSelect: (ctx) => { + ctx.stack.push(new TelepromptLayer(dashboardState.telepromptDocumentText)); + }, + }, { label: "Stopwatch", onSelect: (ctx) => { @@ -580,7 +609,7 @@ function createSettingsMenuLayer(): MenuLayer { function createDisplaySettingsMenuLayer(): MenuLayer { return new MenuLayer( - "Display", + "Settings > Display", [ { label: "Screen timeout", @@ -616,7 +645,7 @@ function setWakeMode(value: WakeModeSetting): void { function createVoiceSettingsMenuLayer(): MenuLayer { return new MenuLayer( - "Voice", + "Settings > Voice", [ { label: "Enable", @@ -634,7 +663,7 @@ function createVoiceSettingsMenuLayer(): MenuLayer { function createDashboardSettingsMenuLayer(): MenuLayer { return new MenuLayer( - "Dashboard", + "Settings > Dashboard", [ { label: "System Card", @@ -658,7 +687,7 @@ function createDashboardSettingsMenuLayer(): MenuLayer { function createSystemCardSettingsMenuLayer(): MenuLayer { return new MenuLayer( - "System Card", + "Settings > Dashboard > System Card", [ { label: "Name", @@ -693,7 +722,7 @@ function createSystemCardToggleItem(label: string, key: SystemCardSettingKey) { function createIntegrationsMenuLayer(): MenuLayer { return new MenuLayer( - "Integrations", + "Settings > Integrations", [ { label: "Nightscout", @@ -708,7 +737,7 @@ function createIntegrationsMenuLayer(): MenuLayer { function createNightscoutSettingsMenuLayer(): MenuLayer { return new MenuLayer( - "Nightscout", + "Settings > Integrations > Nightscout", [ { label: "Site URL", @@ -783,7 +812,7 @@ class EditTextSettingLayer implements Layer { function createSlotPickerMenu(slot: DashboardSlotId): MenuLayer { return new MenuLayer( - slotMenuLabel(slot), + `Settings > Dashboard > ${slotMenuLabel(slot)}`, listDashboardPlugins().map((plugin) => ({ label: plugin.label, onSelect: (ctx) => { diff --git a/app/ui/menu.ts b/app/ui/menu.ts index 0ec4c967..80b81067 100644 --- a/app/ui/menu.ts +++ b/app/ui/menu.ts @@ -7,8 +7,11 @@ const DEFAULT_MENU_X = 8; const DEFAULT_MENU_Y = 8; const DEFAULT_MENU_WIDTH = 272; const DEFAULT_MENU_HEIGHT = 128; -const MENU_TITLE_HEIGHT = 20; +const MENU_TITLE_HEIGHT = 16; const MENU_ROW_HEIGHT = 20; +const MENU_HIGHLIGHT_Y_OFFSET = 0; +const MENU_TOGGLE_SWITCH_Y_OFFSET = -1; +const MENU_HIGHLIGHT_HEIGHT = MENU_ROW_HEIGHT - 1; export type MenuLayout = { x: number; @@ -47,7 +50,7 @@ export function drawToggleMenuItem( const switchWidth = 34; const switchHeight = 16; const switchX = x + width - switchWidth - 2; - const switchY = y + 2; + const switchY = y + MENU_TOGGLE_SWITCH_Y_OFFSET; image.drawText(font, x, y + 3, label, 200); const offFill = selected ? 0 : 18; image.fillRoundedRect(switchX, switchY, switchWidth, switchHeight, enabled ? 70 : offFill, 8); @@ -75,7 +78,7 @@ export class MenuLayer implements Layer { private selectedIndex = 0; constructor( - private readonly title: string, + private readonly title: string | null, private readonly items: MenuItem[], private readonly layout: MenuLayout = { x: DEFAULT_MENU_X, @@ -91,16 +94,18 @@ export class MenuLayer implements Layer { const image = paintBelow(); image.fillRoundedRect(x, y, width, height, 0); image.drawRoundedRect(x, y, width, height, 72); - image.drawText(ctx.font, x + 18, y + 10, this.title, 220); + if (this.title) { + image.drawText(ctx.font, x + 12, y + 8, this.title, 220); + } - const bodyY = y + MENU_TITLE_HEIGHT + 8; + const bodyY = y + (this.title ? MENU_TITLE_HEIGHT : 0) + 8; for (let index = 0; index < this.items.length; index++) { const item = this.items[index]!; const y = bodyY + index * MENU_ROW_HEIGHT; const selected = index === this.selectedIndex; if (selected) { - image.fillRoundedRect(x + 12, y - 1, width - 24, MENU_ROW_HEIGHT - 3, 18); - image.drawRoundedRect(x + 12, y - 1, width - 24, MENU_ROW_HEIGHT - 3, 45); + image.fillRoundedRect(x + 12, y + MENU_HIGHLIGHT_Y_OFFSET, width - 24, MENU_HIGHLIGHT_HEIGHT, 18); + image.drawRoundedRect(x + 12, y + MENU_HIGHLIGHT_Y_OFFSET, width - 24, MENU_HIGHLIGHT_HEIGHT, 45); } if (item.render) { item.render({ diff --git a/app/ui/notifications.ts b/app/ui/notifications.ts new file mode 100644 index 00000000..a99159ee --- /dev/null +++ b/app/ui/notifications.ts @@ -0,0 +1,335 @@ +import { BdfFont } from "../graphics/bdffont"; +import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image"; +import { wrapText } from "../graphics/textwrap"; +import { + dismissNotification, + invokeNotificationAction, + readActiveNotifications, + type AndroidNotification, + type AndroidNotificationAction, +} from "../native/notification-icons"; +import { type DashboardInputEvent, type Layer, type LayerContext, type PaintBelow } from "./layers"; + +const PAGE_X = 12; +const PAGE_Y = 12; +const PAGE_WIDTH = G2_LENS_WIDTH - 24; +const PAGE_HEIGHT = G2_LENS_HEIGHT - 24; +const LIST_TOP = 38; +const LIST_BOTTOM = 248; +const CARD_X = 20; +const CARD_WIDTH = G2_LENS_WIDTH - 40; +const CARD_TEXT_WIDTH = CARD_WIDTH - 24; +const LINE_HEIGHT = 14; +const CARD_GAP = 6; +const MAX_NOTIFICATIONS = 50; + +type CardLayout = { + notification: AndroidNotification; + height: number; + lines: string[]; +}; + +type DetailMenuItem = + | { kind: "back"; label: string } + | { kind: "action"; label: string; action: AndroidNotificationAction } + | { kind: "dismiss"; label: string }; + +export class NotificationsListLayer implements Layer { + private selectedKey = ""; + + constructor(private readonly exitToRootMenu?: (ctx: LayerContext) => void) {} + + paint(ctx: LayerContext): GrayImage { + const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0); + const notifications = readActiveNotifications(MAX_NOTIFICATIONS); + const selectedIndex = this.resolveSelectedIndex(notifications); + const layouts = notifications.map((notification, index) => + buildNotificationCardLayout(ctx.font, notification, index === selectedIndex), + ); + + image.drawRect(PAGE_X, PAGE_Y, PAGE_WIDTH, PAGE_HEIGHT, 52); + image.drawText(ctx.font, PAGE_X + 12, PAGE_Y + 9, "Notifications", 220); + image.drawText(ctx.font, G2_LENS_WIDTH - 96, PAGE_Y + 9, `${notifications.length} active`, 150); + + if (!notifications.length) { + image.drawText(ctx.font, 24, 72, "No current Android notifications.", 190); + image.drawText(ctx.font, 24, 252, "Double-click to return to dashboard", 110); + return image; + } + + const scrollY = scrollForSelected(layouts, selectedIndex, LIST_BOTTOM - LIST_TOP); + let cursorY = LIST_TOP - scrollY; + for (let index = 0; index < layouts.length; index++) { + const layout = layouts[index]!; + if (cursorY + layout.height >= LIST_TOP && cursorY <= LIST_BOTTOM) { + drawNotificationCard(image, ctx.font, layout, CARD_X, cursorY, CARD_WIDTH, index === selectedIndex); + } + cursorY += layout.height + CARD_GAP; + if (cursorY > LIST_BOTTOM + 80) break; + } + + const footer = `${selectedIndex + 1}/${notifications.length} tap: open double: dashboard`; + image.drawText(ctx.font, PAGE_X + 12, 258, footer, 110); + return image; + } + + handleInput(event: DashboardInputEvent, ctx: LayerContext): void { + const notifications = readActiveNotifications(MAX_NOTIFICATIONS); + const selectedIndex = this.resolveSelectedIndex(notifications); + if (event.type === "double-click") { + if (this.exitToRootMenu) { + this.exitToRootMenu(ctx); + } else { + ctx.stack.clearToBase(); + } + return; + } + if (!notifications.length) return; + + if (event.type === "scroll-up") { + this.selectedKey = notifications[Math.max(0, selectedIndex - 1)]!.key; + return; + } + if (event.type === "scroll-down") { + this.selectedKey = notifications[Math.min(notifications.length - 1, selectedIndex + 1)]!.key; + return; + } + if (event.type === "click") { + ctx.stack.push(new SingleNotificationLayer(notifications[selectedIndex]!.key)); + } + } + + private resolveSelectedIndex(notifications: AndroidNotification[]): number { + if (!notifications.length) { + this.selectedKey = ""; + return -1; + } + let index = notifications.findIndex((notification) => notification.key === this.selectedKey); + if (index < 0) { + index = 0; + this.selectedKey = notifications[0]!.key; + } + return index; + } +} + +class SingleNotificationLayer implements Layer { + private selectedMenuIndex = 0; + + constructor(private readonly notificationKey: string) {} + + paint(ctx: LayerContext, _paintBelow: PaintBelow): GrayImage { + const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0); + const notification = readActiveNotifications(MAX_NOTIFICATIONS).find((item) => item.key === this.notificationKey); + + image.drawRect(PAGE_X, PAGE_Y, PAGE_WIDTH, PAGE_HEIGHT, 52); + if (!notification) { + image.drawText(ctx.font, PAGE_X + 12, PAGE_Y + 9, "Notification", 220); + image.drawText(ctx.font, 24, 72, "This notification is no longer active.", 190); + image.drawText(ctx.font, 24, 252, "Double-click to go back", 110); + return image; + } + + const menu = buildDetailMenu(notification); + this.selectedMenuIndex = clamp(this.selectedMenuIndex, 0, Math.max(0, menu.length - 1)); + drawDetailContent(image, ctx.font, notification); + drawDetailMenu(image, ctx.font, menu, this.selectedMenuIndex); + return image; + } + + handleInput(event: DashboardInputEvent, ctx: LayerContext): void { + const notification = readActiveNotifications(MAX_NOTIFICATIONS).find((item) => item.key === this.notificationKey); + const menu = notification ? buildDetailMenu(notification) : [{ kind: "back", label: "Back" } as DetailMenuItem]; + + if (event.type === "double-click") { + ctx.stack.pop(); + return; + } + if (event.type === "scroll-up") { + this.selectedMenuIndex = Math.max(0, this.selectedMenuIndex - 1); + return; + } + if (event.type === "scroll-down") { + this.selectedMenuIndex = Math.min(menu.length - 1, this.selectedMenuIndex + 1); + return; + } + if (event.type !== "click") return; + + const item = menu[this.selectedMenuIndex]!; + if (item.kind === "back") { + ctx.stack.pop(); + } else if (item.kind === "action") { + invokeNotificationAction(this.notificationKey, item.action.index); + } else if (item.kind === "dismiss") { + dismissNotification(this.notificationKey); + ctx.stack.pop(); + } + } +} + +function buildNotificationCardLayout(font: BdfFont, notification: AndroidNotification, selected: boolean): CardLayout { + const lines: string[] = []; + const time = formatRelativeTime(notification.postTime); + const title = notificationTitle(notification); + const timeSuffix = time ? ` ${time}` : ""; + lines.push(fitLine(font, `${title}${timeSuffix}`, CARD_TEXT_WIDTH)); + + const appName = notification.appName || notification.packageName; + const body = primaryNotificationBody(notification); + + if (selected) { + if (appName && appName !== title) { + lines.push(fitLine(font, appName, CARD_TEXT_WIDTH)); + } + if (body) { + lines.push(...wrapText(font, body, CARD_TEXT_WIDTH).slice(0, 4)); + } + } else if (body) { + console.log(`buildNotificationCardLayout: CARD_TEXT_WIDTH=${CARD_TEXT_WIDTH}`); + console.log(`buildNotificationCardLayout: wrapText(...) = ${JSON.stringify(wrapText(font, body, CARD_TEXT_WIDTH))}`); + console.log(`buildNotificationCardLayout: fitLine(...) = ${fitLine(font, wrapText(font, body, CARD_TEXT_WIDTH)[0]!, CARD_TEXT_WIDTH)}`); + lines.push(fitLine(font, wrapText(font, body, CARD_TEXT_WIDTH)[0]!, CARD_TEXT_WIDTH)); + } + if (notification.actions.length) { + lines.push(`${notification.actions.length} quick action${notification.actions.length === 1 ? "" : "s"}`); + } + console.log(`buildNotificationCardLayout: title=${title}, appName=${appName}, body=${body}, lines=${JSON.stringify(lines)}`); + return { + notification, + lines, + height: Math.max(42, 12 + lines.length * LINE_HEIGHT), + }; +} + +function drawNotificationCard( + image: GrayImage, + font: BdfFont, + layout: CardLayout, + x: number, + y: number, + width: number, + selected: boolean, +): void { + const fill = selected ? 15 : 0; + const stroke = selected ? 110 : 38; + image.fillRoundedRect(x, y, width, layout.height, fill, 8); + image.drawRoundedRect(x, y, width, layout.height, stroke, 8); + for (let index = 0; index < layout.lines.length; index++) { + const value = index === 0 ? 140 : selected ? 235 : 185; + image.drawText(font, x + 10, y + 7 + index * LINE_HEIGHT, layout.lines[index]!, value); + } +} + +function scrollForSelected(layouts: CardLayout[], selectedIndex: number, viewportHeight: number): number { + if (selectedIndex < 0) return 0; + let selectedTop = 0; + for (let index = 0; index < selectedIndex; index++) { + selectedTop += layouts[index]!.height + CARD_GAP; + } + const selectedBottom = selectedTop + layouts[selectedIndex]!.height; + const contentHeight = layouts.reduce((sum, layout) => sum + layout.height + CARD_GAP, 0); + const maxScroll = Math.max(0, contentHeight - viewportHeight); + const centered = selectedTop - Math.max(0, (viewportHeight - (selectedBottom - selectedTop)) / 2); + return clamp(centered | 0, 0, maxScroll); +} + +function drawDetailContent(image: GrayImage, font: BdfFont, notification: AndroidNotification): void { + const contentX = 24; + const contentWidth = 360; + image.drawText(font, PAGE_X + 12, PAGE_Y + 9, "Notification", 220); + image.drawText(font, contentX, 42, `${notification.appName || notification.packageName} ${formatRelativeTime(notification.postTime)}`, 150); + + const lines: string[] = []; + lines.push(...wrapText(font, notification.title || "(untitled)", contentWidth)); + const body = detailNotificationBody(notification); + if (body) { + lines.push(""); + lines.push(...wrapText(font, body, contentWidth)); + } + const meta = [notification.subText, notification.infoText, notification.summaryText].filter(Boolean).join(" "); + if (meta) { + lines.push(""); + lines.push(...wrapText(font, meta, contentWidth)); + } + + for (let index = 0; index < Math.min(lines.length, 14); index++) { + const line = lines[index]!; + image.drawText(font, contentX, 64 + index * LINE_HEIGHT, line, index === 0 ? 230 : 190); + } + if (lines.length > 14) { + image.drawText(font, contentX, 260, "...", 140); + } +} + +function drawDetailMenu(image: GrayImage, font: BdfFont, menu: DetailMenuItem[], selectedIndex: number): void { + const menuX = 404; + const menuY = 40; + const menuWidth = 148; + image.drawText(font, menuX, 24, "Menu", 170); + for (let index = 0; index < menu.length; index++) { + const y = menuY + index * 22; + const selected = index === selectedIndex; + if (selected) { + image.fillRoundedRect(menuX - 8, y - 2, menuWidth, 19, 18, 6); + image.drawRoundedRect(menuX - 8, y - 2, menuWidth, 19, 60, 6); + } + const label = truncateToWidth(font, menu[index]!.label, menuWidth - 12); + image.drawText(font, menuX, y + 2, label, selected ? 255 : 185); + } + image.drawText(font, 404, 258, "tap: select", 110); +} + +function buildDetailMenu(notification: AndroidNotification): DetailMenuItem[] { + return [ + { kind: "back", label: "Back" }, + ...notification.actions.map((action): DetailMenuItem => ({ + kind: "action", + label: action.enabled ? action.title : `${action.title} (unavailable)`, + action, + })), + { kind: "dismiss", label: "Dismiss" }, + ]; +} + +function primaryNotificationBody(notification: AndroidNotification): string { + const title = notificationTitle(notification); + const body = notification.bigText || notification.text || notification.lines.join(" / ") || notification.summaryText || ""; + return body === title ? "" : body; +} + +function detailNotificationBody(notification: AndroidNotification): string { + const lines = notification.lines.length ? notification.lines.join("\n") : ""; + return [notification.bigText || notification.text, lines].filter(Boolean).join("\n"); +} + +function formatRelativeTime(timestamp: number): string { + if (!timestamp) return ""; + const ageMs = Math.max(0, Date.now() - timestamp); + const minutes = Math.floor(ageMs / 60_000); + if (minutes < 1) return "now"; + if (minutes < 60) return `${minutes}m`; + const hours = Math.floor(minutes / 60); + if (hours < 24) return `${hours}h`; + return `${Math.floor(hours / 24)}d`; +} + +function truncateToWidth(font: BdfFont, text: string, width: number): string { + if (font.measureText(text) <= width) return text; + let out = text; + while (out.length > 1 && font.measureText(`${out}...`) > width) { + out = out.slice(0, -1); + } + return `${out}...`; +} + +function notificationTitle(notification: AndroidNotification): string { + return notification.title || notification.text || notification.summaryText || notification.appName || notification.packageName || "(untitled)"; +} + +function fitLine(font: BdfFont, text: string, width: number): string { + return truncateToWidth(font, text, width); +} + +function clamp(value: number, min: number, max: number): number { + return Math.max(min, Math.min(max, value)); +}