From 3d39d2745630233fad59bc2792fd798af9c07fcd Mon Sep 17 00:00:00 2001 From: James Babcock Date: Tue, 8 Sep 2026 02:20:55 -0700 Subject: [PATCH] Add notification filters --- ...eclawMediaNotificationListenerService.java | 2 +- CHANGELOG | 1 + app/apps/notifications/notifications-app.ts | 8 ++ app/g2/dashboard-controller.ts | 7 +- app/native/notification-icons.ts | 7 +- app/native/notification-sources.ts | 59 ++++++++++++ app/ui/notification-filter.ts | 50 +++++++++++ app/ui/notifications.ts | 89 ++++++++++++++++--- 8 files changed, 205 insertions(+), 18 deletions(-) create mode 100644 app/native/notification-sources.ts create mode 100644 app/ui/notification-filter.ts diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.java index 0e062a67..8ca97f00 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.java @@ -210,7 +210,7 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen public static String getActiveNotificationsJson(int maxNotifications) { FaceclawMediaNotificationListenerService service = activeService; - int limit = Math.max(0, Math.min(100, maxNotifications)); + int limit = Math.max(0, maxNotifications); if (service == null || limit == 0) { return "[]"; } diff --git a/CHANGELOG b/CHANGELOG index f0f292cc..322a8b8a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,6 +3,7 @@ ## 0.6.6 - Future * Navigate and terminal app: App-specific screen size/position setting * Transcribe app: Scrolling; automatic reconnection; insert newlines +* Notifications: Add notification filters ## 0.6.5 - 2026-09-04 * Changed context menu gestures: — for system, ●— for app context menu. diff --git a/app/apps/notifications/notifications-app.ts b/app/apps/notifications/notifications-app.ts index 168de962..a7f0bf75 100644 --- a/app/apps/notifications/notifications-app.ts +++ b/app/apps/notifications/notifications-app.ts @@ -1,4 +1,5 @@ import { NotificationsListLayer } from "../../ui/notifications"; +import { NotificationFilterLayer } from "../../ui/notification-filter"; import { onAndroidNotificationPosted } from "../../native/notification-icons"; import { createInProcessWindow, @@ -25,6 +26,13 @@ export function createNotificationsAppWindow(options: InProcessAppOptions): InPr iconLetter: "N", icon: "bell", closeable: true, + menuItems: () => [{ + label: "Notification filter", + onSelect: (ctx) => { + ctx.stack.pop(); + ctx.stack.push(new NotificationFilterLayer()); + }, + }], actions: options.actions, baseLayer: new YieldAtRootLayer(new NotificationsListLayer()), submitFrame: options.submitFrame, diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index 8e05551c..adb0c7ca 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -12,7 +12,8 @@ import * as frameTimings from "../native/frame-timings"; import { startForegroundNotification, stopForegroundNotification, updateForegroundNotification } from "../native/foreground-service"; import { mediaControllerBridge } from "../native/media-controller"; import { nightscoutBridge } from "../native/nightscout-bridge"; -import { onAndroidNotificationPosted } from "../native/notification-icons"; +import { ALL_NOTIFICATIONS, onAndroidNotificationPosted, readActiveNotifications } from "../native/notification-icons"; +import { shouldShowNotificationOnGlasses } from "../native/notification-sources"; import { openEvenAppSettings, readEvenAppNotificationState } from "../native/even-app-conflict"; import { grayImageToPreviewSource } from "../native/gray-image-preview"; import { firmwareIncompatibilityMessage } from "./firmware-compat"; @@ -374,6 +375,7 @@ class DashboardController { this.appendLog(`notification wake failed: ${this.formatError(error)}`); }); }); + readActiveNotifications(ALL_NOTIFICATIONS); // Settings toggled from the glasses can change what the phone UI shows // (e.g. the text-setting editor), so re-emit the snapshot on any change. onAnySettingChanged(() => { @@ -2464,7 +2466,8 @@ class DashboardController { } private async handleAndroidNotificationPosted(notificationKey: string): Promise { - if (!notificationKey) { + const notification = readActiveNotifications(ALL_NOTIFICATIONS).find((item) => item.key === notificationKey); + if (!notification || !shouldShowNotificationOnGlasses(notification.packageName)) { this.requestShellRender(); return; } diff --git a/app/native/notification-icons.ts b/app/native/notification-icons.ts index aa06623a..d3f58d22 100644 --- a/app/native/notification-icons.ts +++ b/app/native/notification-icons.ts @@ -1,10 +1,13 @@ import { GrayImage } from "../graphics/image"; import { logCurrent, spanCurrent } from "./frame-timings"; import { toUint8Array } from "../util/array-util"; +import { rememberNotificationSources } from "./notification-sources"; declare const com: any; const ICON_SIZE = 24; +/** Ask the native listener for every active source, including those below the list's display limit. */ +export const ALL_NOTIFICATIONS = 0x7fffffff; // Backstop TTL only: the icon caches are invalidated eagerly whenever a // notification is posted or dismissed (see invalidateIconCaches callers), so // the tray is kept fresh by invalidation, not by expiry. A short TTL just @@ -158,7 +161,9 @@ export function readActiveNotifications(maxNotifications = 50): AndroidNotificat ); const parsed = JSON.parse(json); if (!Array.isArray(parsed)) return []; - return parsed.map(normalizeNotification).filter((item): item is AndroidNotification => Boolean(item)); + const notifications = parsed.map(normalizeNotification).filter((item): item is AndroidNotification => Boolean(item)); + rememberNotificationSources(notifications); + return notifications; } catch { return []; } diff --git a/app/native/notification-sources.ts b/app/native/notification-sources.ts new file mode 100644 index 00000000..cd00a5bb --- /dev/null +++ b/app/native/notification-sources.ts @@ -0,0 +1,59 @@ +import { getStringSetting, setStringSetting } from "./settings-store"; + +const SOURCES_KEY = "notifications.sources"; + +export type NotificationSource = { + packageName: string; + appName: string; + showOnGlasses: boolean; +}; + +/** Sources outlive their active notifications so a muted app can always be re-enabled. */ +export function readNotificationSources(): NotificationSource[] { + try { + const parsed = JSON.parse(getStringSetting(SOURCES_KEY, "[]")); + if (!Array.isArray(parsed)) return []; + return parsed.filter((source) => source && typeof source.packageName === "string" && source.packageName) + .map((source) => ({ + packageName: source.packageName, + appName: typeof source.appName === "string" && source.appName ? source.appName : source.packageName, + showOnGlasses: source.showOnGlasses !== false, + })); + } catch { + return []; + } +} + +export function rememberNotificationSources(notifications: { packageName: string; appName: string }[]): void { + const sources = new Map(readNotificationSources().map((source) => [source.packageName, source])); + let changed = false; + for (const notification of notifications) { + if (!notification.packageName) continue; + const previous = sources.get(notification.packageName); + const appName = notification.appName || previous?.appName || notification.packageName; + if (!previous || previous.appName !== appName) { + sources.set(notification.packageName, { + packageName: notification.packageName, + appName, + showOnGlasses: previous?.showOnGlasses ?? true, + }); + changed = true; + } + } + if (changed) setStringSetting(SOURCES_KEY, JSON.stringify(Array.from(sources.values()))); +} + +export function shouldShowNotificationOnGlasses(packageName: string): boolean { + return readNotificationSources().find((source) => source.packageName === packageName)?.showOnGlasses ?? true; +} + +export function setNotificationSourceEnabled(source: { packageName: string; appName: string }, enabled: boolean): void { + if (!source.packageName) return; + const sources = new Map(readNotificationSources().map((item) => [item.packageName, item])); + sources.set(source.packageName, { + packageName: source.packageName, + appName: source.appName || source.packageName, + showOnGlasses: enabled, + }); + setStringSetting(SOURCES_KEY, JSON.stringify(Array.from(sources.values()))); +} diff --git a/app/ui/notification-filter.ts b/app/ui/notification-filter.ts new file mode 100644 index 00000000..93bb9144 --- /dev/null +++ b/app/ui/notification-filter.ts @@ -0,0 +1,50 @@ +import { getDefaultSmallFont } from "../graphics/ui-fonts"; +import { GrayImage } from "../graphics/image"; +import { truncateText } from "../graphics/textwrap"; +import { ALL_NOTIFICATIONS, readActiveNotifications } from "../native/notification-icons"; +import { readNotificationSources, setNotificationSourceEnabled, shouldShowNotificationOnGlasses } from "../native/notification-sources"; +import { type InputEvent } from "./gestures"; +import { type Layer, type LayerContext, type PaintBelow } from "./layers"; +import { drawToggleMenuItem, MenuLayer, type MenuItem } from "./menu"; + +/** Rebuild rows when new sources arrive, keeping the current selection by package. */ +export class NotificationFilterLayer implements Layer { + private menu: MenuLayer | null = null; + private packages = ""; + private selectedPackage = ""; + + private currentMenu(ctx: LayerContext): MenuLayer { + readActiveNotifications(ALL_NOTIFICATIONS); + const sources = readNotificationSources().sort((a, b) => + a.appName.localeCompare(b.appName) || a.packageName.localeCompare(b.packageName)); + const signature = JSON.stringify([ctx.stack.getBaseSize().width, sources.map((source) => [source.packageName, source.appName])]); + if (!this.menu || signature !== this.packages) { + this.packages = signature; + const items: MenuItem[] = sources.map((source) => ({ + label: source.appName, + onSelect: () => setNotificationSourceEnabled(source, !shouldShowNotificationOnGlasses(source.packageName)), + render: ({ image, x, y, width, selected }) => { + if (selected) this.selectedPackage = source.packageName; + const font = getDefaultSmallFont(); + drawToggleMenuItem(image, font, x, y, width, truncateText(font, source.appName, width - 48), + shouldShowNotificationOnGlasses(source.packageName), selected); + }, + })); + if (!items.length) items.push({ label: "No notification sources discovered yet.", disabled: true, onSelect: () => {} }); + this.menu = new MenuLayer("Notification filter", items, { + x: 8, y: 8, width: ctx.stack.getBaseSize().width - 16, + opaque: true, showBorder: false, + footer: "On = show popups on glasses", + }).selectItem(Math.max(0, sources.findIndex((source) => source.packageName === this.selectedPackage))); + } + return this.menu; + } + + paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage { + return this.currentMenu(ctx).paint(ctx, paintBelow); + } + + async handleInput(event: InputEvent, ctx: LayerContext): Promise { + await this.currentMenu(ctx).handleInput(event, ctx); + } +} diff --git a/app/ui/notifications.ts b/app/ui/notifications.ts index d60602c3..647ffb58 100644 --- a/app/ui/notifications.ts +++ b/app/ui/notifications.ts @@ -6,6 +6,7 @@ import { GrayImage, type UiFont } from "../graphics/image"; import { wrapText } from "../graphics/textwrap"; import { lineStep, listRowHeight } from "./metrics"; import { + ALL_NOTIFICATIONS, dismissNotification, invokeNotificationAction, readActiveNotifications, @@ -13,10 +14,12 @@ import { type AndroidNotification, type AndroidNotificationAction, } from "../native/notification-icons"; +import { setNotificationSourceEnabled } from "../native/notification-sources"; import { isNotificationListenerEnabled } from "../native/notification-access"; import { noteStaleDataUsed, renderPassAllowsStaleData } from "../util/render-freshness"; import { type InputEvent } from "./gestures"; import { type Layer, type LayerContext, type PaintBelow } from "./layers"; +import { MenuLayer } from "./menu"; const PAGE_X = 12; const PAGE_Y = 12; @@ -52,7 +55,8 @@ type CardLayout = { type DetailMenuItem = | { kind: "back"; label: string } | { kind: "action"; label: string; action: AndroidNotificationAction } - | { kind: "dismiss"; label: string }; + | { kind: "dismiss"; label: string } + | { kind: "disable-source"; label: string }; export type SingleNotificationLayerOrigin = "notifications-list" | "new-notification-modal"; @@ -167,6 +171,7 @@ export class NotificationsListLayer implements Layer { */ export class SingleNotificationLayer implements Layer { private selectedMenuIndex = 0; + private confirmation: { source: AndroidNotification; menu: MenuLayer } | null = null; constructor( private readonly notificationKey: string, @@ -174,29 +179,36 @@ export class SingleNotificationLayer implements Layer { ) {} paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage { + if (this.confirmation) return this.paintConfirmation(ctx); const font = getDefaultSmallFont(); const { width, height } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); - const notification = readActiveNotifications(MAX_NOTIFICATIONS).find((item) => item.key === this.notificationKey); + const notification = readActiveNotifications(ALL_NOTIFICATIONS).find((item) => item.key === this.notificationKey); if (!notification) { return this.closeUnavailableNotification(ctx, paintBelow); } - const menu = buildDetailMenu(notification); + const menu = buildDetailMenu(notification, this.options.origin); this.selectedMenuIndex = clamp(this.selectedMenuIndex, 0, Math.max(0, menu.length - 1)); drawDetailContent(image, font, notification, iconForNotification(notification.key), width, height); drawDetailMenu(image, font, menu, this.selectedMenuIndex, width); return image; } - handleInput(event: InputEvent, ctx: LayerContext): void { - const notification = readActiveNotifications(MAX_NOTIFICATIONS).find((item) => item.key === this.notificationKey); + async handleInput(event: InputEvent, ctx: LayerContext): Promise { + if (this.confirmation) { + if (event.type === "double-click") this.close(ctx); + else await this.confirmation.menu.handleInput(event, ctx); + return; + } + const notification = readActiveNotifications(ALL_NOTIFICATIONS).find((item) => item.key === this.notificationKey); if (!notification) { this.closeUnavailableNotification(ctx); return; } - const menu = buildDetailMenu(notification); + const menu = buildDetailMenu(notification, this.options.origin); + this.selectedMenuIndex = clamp(this.selectedMenuIndex, 0, menu.length - 1); if (event.type === "double-click") { this.close(ctx); @@ -217,15 +229,47 @@ export class SingleNotificationLayer implements Layer { this.close(ctx); } else if (item.kind === "action") { invokeNotificationAction(this.notificationKey, item.action.index); - if (!readActiveNotifications(MAX_NOTIFICATIONS).some((item) => item.key === this.notificationKey)) { + if (!readActiveNotifications(ALL_NOTIFICATIONS).some((item) => item.key === this.notificationKey)) { this.closeUnavailableNotification(ctx); } } else if (item.kind === "dismiss") { dismissNotification(this.notificationKey); this.closeUnavailableNotification(ctx); + } else if (item.kind === "disable-source") { + // Keep the modal alive for confirmation even after Android removes the notification. + this.confirmation = { + source: notification, + menu: new MenuLayer(null, [ + { label: "Cancel", onSelect: () => this.close(ctx) }, + { label: "Turn off popups", onSelect: () => { + setNotificationSourceEnabled(notification, false); + this.close(ctx); + } }, + ], { x: 12, y: 0, width: ctx.stack.getBaseSize().width - 24, minHeight: 0, showBorder: false }), + }; + dismissNotification(this.notificationKey); } } + private paintConfirmation(ctx: LayerContext): GrayImage { + const { source, menu } = this.confirmation!; + const font = getDefaultSmallFont(); + const { width, height } = ctx.stack.getBaseSize(); + const image = new GrayImage(width, height, 0); + // Put actions first so they remain reachable even with a large UI font. + menu.paint(ctx, () => image); + const top = 24 + 2 * listRowHeight(font); + const prompt = `Turn off notification popups from ${source.appName || source.packageName}? You can turn them back on in the Notifications app's Notification filter.`; + const lines = wrapText(font, prompt, width - 48); + const maxLines = Math.max(1, Math.floor((height - top - 12) / lineStep(font))); + for (let index = 0; index < Math.min(lines.length, maxLines); index++) { + const text = index === maxLines - 1 && lines.length > maxLines + ? truncateText(font, lines[index] + "...", width - 48) : lines[index]!; + image.drawText(font, 24, top + index * lineStep(font), text, 210); + } + return image; + } + /** Leave the detail view, whatever hosts it. */ private close(ctx: LayerContext): void { if (this.options.origin === "new-notification-modal") { @@ -365,20 +409,35 @@ function drawDetailContent( function drawDetailMenu(image: GrayImage, font: UiFont, menu: DetailMenuItem[], selectedIndex: number, width: number): void { const menuX = width - DETAIL_MENU_WIDTH - 24; const menuY = 24; - const rowH = listRowHeight(font); + const rows = menu.map((item) => { + const lines = item.kind === "disable-source" + ? wrapText(font, item.label, DETAIL_MENU_WIDTH - 16) + : [truncateText(font, item.label, DETAIL_MENU_WIDTH - 16)]; + return { lines, height: lines.length * lineStep(font) + 8 }; + }); + const selectedBottom = rows.slice(0, selectedIndex + 1).reduce((sum, row) => sum + row.height, 0); + const scrollY = Math.max(0, selectedBottom - (image.height - menuY - 12)); + let y = menuY - scrollY; for (let index = 0; index < menu.length; index++) { - const y = menuY + index * (rowH + 2); + const row = rows[index]!; + if (y < menuY) { + y += row.height; + continue; + } + if (y + row.height > image.height - 12) break; const selected = index === selectedIndex; if (selected) { - image.fillRoundedRect(menuX - 8, y - 2, DETAIL_MENU_WIDTH, rowH - 1, 18, 6); - image.drawRoundedRect(menuX - 8, y - 2, DETAIL_MENU_WIDTH, rowH - 1, 60, 6); + image.fillRoundedRect(menuX - 8, y - 2, DETAIL_MENU_WIDTH, row.height - 3, 18, 6); + image.drawRoundedRect(menuX - 8, y - 2, DETAIL_MENU_WIDTH, row.height - 3, 60, 6); } - const label = truncateText(font, menu[index]!.label, DETAIL_MENU_WIDTH - 12); - image.drawText(font, menuX, y + 2, label, selected ? 255 : 185); + for (let line = 0; line < row.lines.length; line++) { + image.drawText(font, menuX, y + 2 + line * lineStep(font), row.lines[line]!, selected ? 255 : 185); + } + y += row.height; } } -function buildDetailMenu(notification: AndroidNotification): DetailMenuItem[] { +function buildDetailMenu(notification: AndroidNotification, origin: SingleNotificationLayerOrigin): DetailMenuItem[] { return [ { kind: "back", label: "Back" }, ...notification.actions.map((action): DetailMenuItem => ({ @@ -387,6 +446,8 @@ function buildDetailMenu(notification: AndroidNotification): DetailMenuItem[] { action, })), { kind: "dismiss", label: "Dismiss" }, + ...(origin === "new-notification-modal" && notification.packageName + ? [{ kind: "disable-source" as const, label: "Don't show on glasses again" }] : []), ]; }