From 1959e57d024ecaa0356b6fe689f74f4204ef21e6 Mon Sep 17 00:00:00 2001 From: Guillaume ETHEVE Date: Fri, 6 Nov 2026 04:21:43 +0100 Subject: [PATCH] feat: configure notification filtering and dismiss all notifications --- ...aceclawMediaNotificationListenerService.kt | 12 ++++ app/apps/notifications/notifications-app.ts | 11 ++- app/native/notification-icons.ios.ts | 10 +++ app/native/notification-icons.ts | 7 ++ app/native/notification-sources.ts | 35 ++++++++-- app/ui/notification-filter.ts | 26 +++++-- docs/validation-dismiss-all-notifications.md | 20 ++++++ docs/validation-notifications.md | 23 +++++++ tests/dismiss-all-notifications.test.cjs | 69 +++++++++++++++++++ tests/notification-filter.test.cjs | 63 +++++++++++++++++ 10 files changed, 263 insertions(+), 13 deletions(-) create mode 100644 docs/validation-dismiss-all-notifications.md create mode 100644 docs/validation-notifications.md create mode 100644 tests/dismiss-all-notifications.test.cjs diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.kt index dd03f339..74e8659c 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.kt +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.kt @@ -504,6 +504,18 @@ class FaceclawMediaNotificationListenerService : NotificationListenerService() { } } + @JvmStatic + fun dismissAllNotifications(): Boolean { + val service = activeService ?: return false + return try { + service.cancelAllNotifications() + true + } catch (t: Throwable) { + Log.w(TAG, "failed to dismiss all notifications", t) + false + } + } + private fun emitNotificationPosted(key: String?) { emitNotificationEvent(key) { listener, eventKey -> listener.onNotificationPosted(eventKey) } } diff --git a/app/apps/notifications/notifications-app.ts b/app/apps/notifications/notifications-app.ts index 65719b3f..32131651 100644 --- a/app/apps/notifications/notifications-app.ts +++ b/app/apps/notifications/notifications-app.ts @@ -1,6 +1,6 @@ import { NotificationsListLayer } from "../../ui/notifications"; import { NotificationFilterLayer } from "../../ui/notification-filter"; -import { onAndroidNotificationsChanged } from "../../native/notification-icons"; +import { ALL_NOTIFICATIONS, dismissAllNotifications, onAndroidNotificationsChanged, readActiveNotifications } from "../../native/notification-icons"; import { createInProcessWindow, YieldAtRootLayer, @@ -32,6 +32,15 @@ export function createNotificationsAppWindow(options: InProcessAppOptions): InPr ctx.stack.pop(); ctx.stack.push(new NotificationFilterLayer()); }, + }, { + label: "Dismiss all notifications", + description: "Dismiss notifications on the phone and glasses. System-protected notifications may remain.", + disabled: () => readActiveNotifications(ALL_NOTIFICATIONS).length === 0, + onSelect: (ctx) => { + ctx.stack.pop(); + dismissAllNotifications(); + created.requestRender(); + }, }], actions: options.actions, baseLayer: new YieldAtRootLayer(new NotificationsListLayer()), diff --git a/app/native/notification-icons.ios.ts b/app/native/notification-icons.ios.ts index 940656dc..7f15e255 100644 --- a/app/native/notification-icons.ios.ts +++ b/app/native/notification-icons.ios.ts @@ -26,6 +26,16 @@ export function iosNotificationState(): AncsState { return active?.state ?? 'dis export function readActiveNotifications(maxNotifications = 50) { return active?.read(maxNotifications) ?? [] } export function invokeNotificationAction(key: string, index: number): boolean { return active?.action(key,index) ?? false } export function dismissNotification(key: string): boolean { return active?.dismiss(key) ?? false } +export function dismissAllNotifications(): boolean { + const client = active + if (!client) return false + // Snapshot: removals may arrive while dismissal requests are being sent. + let accepted = true + for (const notification of [...client.read(ALL_NOTIFICATIONS)]) { + if (!client.dismiss(notification.key)) accepted = false + } + return accepted +} export function onAndroidNotificationPosted(listener: (key: string) => void): () => void { listeners.add(listener); return () => { listeners.delete(listener) } } diff --git a/app/native/notification-icons.ts b/app/native/notification-icons.ts index 822360b5..8cc0de8f 100644 --- a/app/native/notification-icons.ts +++ b/app/native/notification-icons.ts @@ -337,6 +337,13 @@ export function dismissNotification(notificationKey: string): boolean { ); } +/** Explicit user action: dismiss on the phone as well as on the glasses. */ +export function dismissAllNotifications(): boolean { + if (!global.isAndroid) return false; + invalidateIconCaches(); + return Boolean(com.faceclaw.app.FaceclawMediaNotificationListenerService.dismissAllNotifications()); +} + export function onAndroidNotificationPosted(listener: (notificationKey: string) => void): () => void { return addNotificationListener(notificationPostedListeners, listener); } diff --git a/app/native/notification-sources.ts b/app/native/notification-sources.ts index cd00a5bb..2875816b 100644 --- a/app/native/notification-sources.ts +++ b/app/native/notification-sources.ts @@ -1,6 +1,16 @@ import { getStringSetting, setStringSetting } from "./settings-store"; const SOURCES_KEY = "notifications.sources"; +const DEFAULT_KEY = "notifications.newAppsEnabledByDefault"; + +/** Unset preserves the behavior of existing installations. */ +export function newNotificationAppsEnabledByDefault(): boolean { + return getStringSetting(DEFAULT_KEY, "true") !== "false"; +} + +export function setNewNotificationAppsEnabledByDefault(enabled: boolean): void { + setStringSetting(DEFAULT_KEY, String(enabled)); +} export type NotificationSource = { packageName: string; @@ -8,8 +18,15 @@ export type NotificationSource = { showOnGlasses: boolean; }; +type SavedSource = Omit & { showOnGlasses?: boolean }; + /** Sources outlive their active notifications so a muted app can always be re-enabled. */ export function readNotificationSources(): NotificationSource[] { + const enabled = newNotificationAppsEnabledByDefault(); + return readSavedSources().map((source) => ({ ...source, showOnGlasses: source.showOnGlasses ?? enabled })); +} + +function readSavedSources(): SavedSource[] { try { const parsed = JSON.parse(getStringSetting(SOURCES_KEY, "[]")); if (!Array.isArray(parsed)) return []; @@ -17,7 +34,7 @@ export function readNotificationSources(): NotificationSource[] { .map((source) => ({ packageName: source.packageName, appName: typeof source.appName === "string" && source.appName ? source.appName : source.packageName, - showOnGlasses: source.showOnGlasses !== false, + showOnGlasses: typeof source.showOnGlasses === "boolean" ? source.showOnGlasses : undefined, })); } catch { return []; @@ -25,7 +42,7 @@ export function readNotificationSources(): NotificationSource[] { } export function rememberNotificationSources(notifications: { packageName: string; appName: string }[]): void { - const sources = new Map(readNotificationSources().map((source) => [source.packageName, source])); + const sources = new Map(readSavedSources().map((source) => [source.packageName, source])); let changed = false; for (const notification of notifications) { if (!notification.packageName) continue; @@ -35,7 +52,7 @@ export function rememberNotificationSources(notifications: { packageName: string sources.set(notification.packageName, { packageName: notification.packageName, appName, - showOnGlasses: previous?.showOnGlasses ?? true, + showOnGlasses: previous?.showOnGlasses, }); changed = true; } @@ -44,12 +61,13 @@ export function rememberNotificationSources(notifications: { packageName: string } export function shouldShowNotificationOnGlasses(packageName: string): boolean { - return readNotificationSources().find((source) => source.packageName === packageName)?.showOnGlasses ?? true; + return readNotificationSources().find((source) => source.packageName === packageName)?.showOnGlasses + ?? newNotificationAppsEnabledByDefault(); } 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])); + const sources = new Map(readSavedSources().map((item) => [item.packageName, item])); sources.set(source.packageName, { packageName: source.packageName, appName: source.appName || source.packageName, @@ -57,3 +75,10 @@ export function setNotificationSourceEnabled(source: { packageName: string; appN }); setStringSetting(SOURCES_KEY, JSON.stringify(Array.from(sources.values()))); } + +/** Set explicit choices only for sources already discovered; leave the future default alone. */ +export function setAllNotificationSourcesEnabled(enabled: boolean): void { + setStringSetting(SOURCES_KEY, JSON.stringify(readSavedSources().map((source) => ({ + ...source, showOnGlasses: enabled, + })))); +} diff --git a/app/ui/notification-filter.ts b/app/ui/notification-filter.ts index 123f5d9e..6864335f 100644 --- a/app/ui/notification-filter.ts +++ b/app/ui/notification-filter.ts @@ -2,7 +2,7 @@ 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 { newNotificationAppsEnabledByDefault, setNewNotificationAppsEnabledByDefault, setAllNotificationSourcesEnabled, 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"; @@ -11,14 +11,26 @@ import { drawToggleMenuItem, MenuLayer, type MenuItem } from "./menu"; export class NotificationFilterLayer implements Layer { private menu: MenuLayer | null = null; private menuWidth = 0; - /** The package behind each current row, parallel to the menu's items. */ + /** Stable row keys, including controls, parallel to the menu's items. */ private packages: string[] = []; 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 items: MenuItem[] = sources.map((source) => ({ + const items: MenuItem[] = [{ + label: "New apps enabled by default", + onSelect: () => setNewNotificationAppsEnabledByDefault(!newNotificationAppsEnabledByDefault()), + render: ({ image, x, y, width, selected }) => { + const font = getDefaultSmallFont(); + drawToggleMenuItem(image, font, x, y, width, + truncateText(font, "New apps enabled by default", width - 48), newNotificationAppsEnabledByDefault(), selected); + }, + }, { + label: "Enable all", onSelect: () => setAllNotificationSourcesEnabled(true), + }, { + label: "Disable all", onSelect: () => setAllNotificationSourcesEnabled(false), + }, ...sources.map((source) => ({ label: source.appName, onSelect: () => setNotificationSourceEnabled(source, !shouldShowNotificationOnGlasses(source.packageName)), render: ({ image, x, y, width, selected }) => { @@ -26,11 +38,11 @@ export class NotificationFilterLayer implements Layer { 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: () => {} }); + }))]; + if (!sources.length) items.push({ label: "No notification sources discovered yet.", disabled: true, onSelect: () => {} }); const selectedPackage = this.menu?.selectedIndex != null ? this.packages[this.menu.selectedIndex] : undefined; - const index = sources.findIndex((source) => source.packageName === selectedPackage); - this.packages = sources.map((source) => source.packageName); + this.packages = ["default", "enable-all", "disable-all", ...sources.map((source) => `source:${source.packageName}`)]; + const index = selectedPackage === undefined ? 0 : this.packages.indexOf(selectedPackage); const width = ctx.stack.getBaseSize().width; if (!this.menu || width !== this.menuWidth) { diff --git a/docs/validation-dismiss-all-notifications.md b/docs/validation-dismiss-all-notifications.md new file mode 100644 index 00000000..ca2ca286 --- /dev/null +++ b/docs/validation-dismiss-all-notifications.md @@ -0,0 +1,20 @@ +# Dismiss all notifications + +The Notifications app menu offers Dismiss all notifications, disabled when the +list is empty. This explicit action dismisses notifications on the phone too; +popup filtering and source/default preferences remain independent. + +Android uses NotificationListenerService.cancelAllNotifications, so dismissal +is not limited to the 50 items displayed by the list and Android controls which +notifications are removable. The normal removal callbacks update the list and +popups. Missing listener access returns false without changing preferences. +iOS uses the existing ANCS dismissal operation on a snapshot of all known keys; +notifications without a negative action may remain. + +Software tests cover menu action/disabled state, more than 50 notifications, +native bridge dispatch, disconnected access and ANCS bulk dismissal. +On a device, verify the permission-granted/revoked cases, protected or ongoing +notifications, notification groups, and consistency with the phone's tray. +Hardware validation remains pending. + +Reference: https://developer.android.com/reference/android/service/notification/NotificationListenerService#cancelAllNotifications() diff --git a/docs/validation-notifications.md b/docs/validation-notifications.md new file mode 100644 index 00000000..403bfbd5 --- /dev/null +++ b/docs/validation-notifications.md @@ -0,0 +1,23 @@ +# Notification filter validation + +Software: `npm test` and `bash lint.sh app/native/notification-sources.ts app/ui/notification-filter.ts`. +The notification suite covers legacy preferences, reloads of the settings store, +discovery, default changes without overwriting exceptions, both bulk actions, +empty lists, stable menu selection and popup suppression before wake. + +Hardware validation (pending; no glasses tested): + +1. On an existing installation, check that enabled and muted applications retain + their behavior before touching the new default. +2. Turn “New apps enabled by default” off; restart the app. Send a notification + from a previously unknown package: it must appear in the filter and on the + phone, without waking the glasses or showing a glasses popup. +3. Enable that package individually; send another notification and check its popup. +4. Change the default in both directions; verify individual exceptions survive. +5. Use “Disable all”, then discover another package: it follows the default. + Repeat with “Enable all” while the default is off. +6. Restart and repeat notifications from enabled and disabled sources. Check that + bulk actions and filtering never dismiss notifications on the phone. + +Only popup delivery on glasses is filtered. The notification tray and phone +notification lifecycle retain their existing behavior. diff --git a/tests/dismiss-all-notifications.test.cjs b/tests/dismiss-all-notifications.test.cjs new file mode 100644 index 00000000..7bc005a4 --- /dev/null +++ b/tests/dismiss-all-notifications.test.cjs @@ -0,0 +1,69 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { loader } = require('./helpers/load-typescript.cjs'); + +test('Notifications menu dismisses all, including beyond the visible list, and leaves filters untouched', () => { + let options, active = Array.from({ length: 80 }, (_, i) => ({ key: String(i) })); + let dismissals = 0, renders = 0, pops = 0; + const load = loader({}, { + '../../ui/notifications': { NotificationsListLayer: class {} }, + '../../ui/notification-filter': { NotificationFilterLayer: class {} }, + '../../native/notification-icons': { + ALL_NOTIFICATIONS: 0x7fffffff, + readActiveNotifications(max) { assert.equal(max, 0x7fffffff); return active; }, + dismissAllNotifications() { dismissals++; active = []; return true; }, + onAndroidNotificationsChanged: () => () => {}, + }, + '../../ui/shell/in-process-window': { + YieldAtRootLayer: class {}, + createInProcessWindow(value) { options = value; return { requestRender() { renders++; } }; }, + }, + }); + load('app/apps/notifications/notifications-app.ts').createNotificationsAppWindow({}); + const items = options.menuItems(); + assert.equal(items[0].label, 'Notification filter'); + const action = items.find(x => x.label === 'Dismiss all notifications'); + assert.equal(action.disabled(), false); + assert.equal(dismissals, 0); + action.onSelect({ stack: { pop() { pops++; } } }); + assert.equal(dismissals, 1); + assert.equal(pops, 1); + assert.equal(renders, 1); + assert.equal(action.disabled(), true); +}); + +test('Android bulk action uses the native listener and propagates unavailable access', () => { + let calls = 0, allowed = false; + const load = loader({ global: { isAndroid: true }, com: { faceclaw: { app: { + FaceclawMediaNotificationListenerService: { dismissAllNotifications() { calls++; return allowed; } }, + } } } }, { + '../graphics/image': {}, './frame-timings': {}, '../util/array-util': {}, + './notification-sources': {}, './image-files': { PHOTO_TONE: { gamma: 1 } }, './notification-types': { NOTIFICATION_ICON_SIZE: 24 }, + }); + const api = load('app/native/notification-icons.ts'); + assert.equal(api.dismissAllNotifications(), false); + allowed = true; + assert.equal(api.dismissAllNotifications(), true); + assert.equal(calls, 2); +}); + +test('iOS snapshots all ANCS keys and continues when a notification cannot be dismissed', () => { + const load = loader({}, { + '../graphics/pebble-icons': {}, '../g2/ancs-client': {}, './notification-app-icons': {}, './notification-sources': {}, + }); + const api = load('app/native/notification-icons.ios.ts'); + assert.equal(api.dismissAllNotifications(), false); + const active = Array.from({ length: 80 }, (_, i) => ({ key: String(i) })), attempted = []; + api.bindIosNotifications({ + read(max) { assert.equal(max, api.ALL_NOTIFICATIONS); return active; }, + dismiss(key) { + attempted.push(key); + if (key === '1') return false; + active.splice(active.findIndex(x => x.key === key), 1); + return true; + }, + }); + assert.equal(api.dismissAllNotifications(), false); + assert.equal(attempted.length, 80); + assert.deepEqual(active, [{ key: '1' }]); +}); diff --git a/tests/notification-filter.test.cjs b/tests/notification-filter.test.cjs index 40cda7c6..e5a0d713 100644 --- a/tests/notification-filter.test.cjs +++ b/tests/notification-filter.test.cjs @@ -165,6 +165,7 @@ test('cancel and back dismiss the popup without changing source preferences', as test('filter toggles persist after notifications disappear and discovers new sources while open', async () => { const app = ui(); app.paint(app.filter); + for (let i = 0; i < 3; i++) await app.input(app.filter, 'scroll-down'); await app.input(app.filter, 'click'); assert.equal(app.prefs.shouldShowNotificationOnGlasses(mail.packageName), false); app.active([{ packageName: 'com.alpha', appName: 'Alpha' }]); @@ -177,6 +178,68 @@ test('filter toggles persist after notifications disappear and discovers new sou assert.equal(app.prefs.shouldShowNotificationOnGlasses('com.alpha'), true); }); +test('default persists without overwriting explicit choices, including legacy saved sources', () => { + const settings = new Map([['notifications.sources', JSON.stringify([ + { ...mail, showOnGlasses: true }, + { packageName: 'muted', appName: 'Muted', showOnGlasses: false }, + ])]]); + let prefs = store(settings); + prefs.rememberNotificationSources([{ packageName: 'discovered', appName: 'Discovered' }]); + prefs.setNewNotificationAppsEnabledByDefault(false); + prefs = store(settings); + assert.equal(prefs.newNotificationAppsEnabledByDefault(), false); + assert.equal(prefs.shouldShowNotificationOnGlasses(mail.packageName), true); + assert.equal(prefs.shouldShowNotificationOnGlasses('muted'), false); + assert.equal(prefs.shouldShowNotificationOnGlasses('discovered'), false); + assert.equal(prefs.shouldShowNotificationOnGlasses('future'), false); + prefs.rememberNotificationSources([{ packageName: 'future', appName: 'Future' }]); + prefs.setNotificationSourceEnabled({ packageName: 'future', appName: 'Future' }, true); + prefs.setNewNotificationAppsEnabledByDefault(true); + prefs.setNewNotificationAppsEnabledByDefault(false); + assert.equal(store(settings).shouldShowNotificationOnGlasses('future'), true); +}); + +test('bulk choices persist for known sources without changing the default for future sources', () => { + const settings = new Map(); + let prefs = store(settings); + prefs.rememberNotificationSources([mail]); + prefs.setAllNotificationSourcesEnabled(false); + prefs = store(settings); + assert.equal(prefs.shouldShowNotificationOnGlasses(mail.packageName), false); + assert.equal(prefs.shouldShowNotificationOnGlasses('future'), true); + prefs.setNewNotificationAppsEnabledByDefault(false); + prefs.setAllNotificationSourcesEnabled(true); + prefs = store(settings); + assert.equal(prefs.shouldShowNotificationOnGlasses(mail.packageName), true); + assert.equal(prefs.shouldShowNotificationOnGlasses('future'), false); + prefs.rememberNotificationSources([{ packageName: 'future', appName: 'Future' }]); + assert.equal(prefs.shouldShowNotificationOnGlasses('future'), false); + prefs.setAllNotificationSourcesEnabled(false); + assert.ok(prefs.readNotificationSources().every(source => !source.showOnGlasses)); +}); + +test('filter controls work with an empty list and keep selection as sources arrive', async () => { + const app = ui(); + app.active([]); + const labels = app.paint(app.filter).texts.map(({ text }) => text); + assert.ok(labels.includes('New apps enabled by default')); + assert.ok(labels.includes('Enable all')); + assert.ok(labels.includes('Disable all')); + await app.input(app.filter, 'click'); + assert.equal(app.prefs.newNotificationAppsEnabledByDefault(), false); + app.active([mail]); + app.paint(app.filter); + assert.equal(app.prefs.shouldShowNotificationOnGlasses(mail.packageName), false); + await app.input(app.filter, 'scroll-down'); + await app.input(app.filter, 'click'); + assert.equal(app.prefs.shouldShowNotificationOnGlasses(mail.packageName), true); + assert.equal(app.prefs.newNotificationAppsEnabledByDefault(), false); + await app.input(app.filter, 'scroll-down'); + await app.input(app.filter, 'click'); + assert.equal(app.prefs.shouldShowNotificationOnGlasses(mail.packageName), false); + assert.equal(app.dismissals(), 0); +}); + test('popup action wraps fully and stays visible with small and large fonts', async () => { for (const size of [12, 20]) { const app = ui(size);