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/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/tests/dismiss-all-notifications.test.cjs b/tests/dismiss-all-notifications.test.cjs new file mode 100644 index 00000000..c32b8d58 --- /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': {}, './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' }]); +});