forked from Blendman974/faceclaw
Merge notification display filtering
This commit is contained in:
7 files changed
+86
-16
No files matched your search
@@ -2,7 +2,7 @@ import type { GrayImage } from '../graphics/image'
|
|||||||
import { pebbleIcon } from '../graphics/pebble-icons'
|
import { pebbleIcon } from '../graphics/pebble-icons'
|
||||||
import { AncsClient, ANCS_CONNECT_MESSAGE, type AncsState } from '../g2/ancs-client'
|
import { AncsClient, ANCS_CONNECT_MESSAGE, type AncsState } from '../g2/ancs-client'
|
||||||
import { notificationAppIcon, notificationCategoryIcon } from './notification-app-icons'
|
import { notificationAppIcon, notificationCategoryIcon } from './notification-app-icons'
|
||||||
import { rememberNotificationSources } from './notification-sources'
|
import { rememberNotificationSources, shouldShowNotificationOnGlasses } from './notification-sources'
|
||||||
import type { AndroidNotification } from './notification-types'
|
import type { AndroidNotification } from './notification-types'
|
||||||
export type { AndroidNotification, AndroidNotificationAction } from './notification-types'
|
export type { AndroidNotification, AndroidNotificationAction } from './notification-types'
|
||||||
export type { NotificationIconsResult, NotificationIconResult, NotificationImageResult } from './notification-icons'
|
export type { NotificationIconsResult, NotificationIconResult, NotificationImageResult } from './notification-icons'
|
||||||
@@ -24,6 +24,11 @@ export function onIosNotificationPopup(listener: (key: string) => void): () => v
|
|||||||
export function iosNotificationMessage(): string { return active?.statusMessage ?? ANCS_CONNECT_MESSAGE }
|
export function iosNotificationMessage(): string { return active?.statusMessage ?? ANCS_CONNECT_MESSAGE }
|
||||||
export function iosNotificationState(): AncsState { return active?.state ?? 'disconnected' }
|
export function iosNotificationState(): AncsState { return active?.state ?? 'disconnected' }
|
||||||
export function readActiveNotifications(maxNotifications = 50) { return active?.read(maxNotifications) ?? [] }
|
export function readActiveNotifications(maxNotifications = 50) { return active?.read(maxNotifications) ?? [] }
|
||||||
|
export function readVisibleNotifications(maxNotifications = 50) {
|
||||||
|
return readActiveNotifications(ALL_NOTIFICATIONS)
|
||||||
|
.filter(notification => shouldShowNotificationOnGlasses(notification.packageName))
|
||||||
|
.slice(0, Math.max(0, maxNotifications))
|
||||||
|
}
|
||||||
export function invokeNotificationAction(key: string, index: number): boolean { return active?.action(key,index) ?? false }
|
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 dismissNotification(key: string): boolean { return active?.dismiss(key) ?? false }
|
||||||
export function dismissAllNotifications(): boolean {
|
export function dismissAllNotifications(): boolean {
|
||||||
@@ -54,7 +59,7 @@ function notificationIcon(notification: AndroidNotification): GrayImage {
|
|||||||
export function readActiveNotificationIcons(maxIcons: number, _allowStale: boolean) {
|
export function readActiveNotificationIcons(maxIcons: number, _allowStale: boolean) {
|
||||||
// One icon per source, standing for its first notification.
|
// One icon per source, standing for its first notification.
|
||||||
const sources = new Map<string, AndroidNotification>()
|
const sources = new Map<string, AndroidNotification>()
|
||||||
for (const n of readActiveNotifications(ALL_NOTIFICATIONS)) if (!sources.has(n.packageName)) sources.set(n.packageName, n)
|
for (const n of readVisibleNotifications(ALL_NOTIFICATIONS)) if (!sources.has(n.packageName)) sources.set(n.packageName, n)
|
||||||
const shown = Array.from(sources.values()).slice(0,Math.max(0,maxIcons))
|
const shown = Array.from(sources.values()).slice(0,Math.max(0,maxIcons))
|
||||||
return {icons: shown.map(notificationIcon),keys: shown.map(n => n.key),stale:false}
|
return {icons: shown.map(notificationIcon),keys: shown.map(n => n.key),stale:false}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { GrayImage } from "../graphics/image";
|
import { GrayImage } from "../graphics/image";
|
||||||
import { logCurrent, spanCurrent } from "./frame-timings";
|
import { logCurrent, spanCurrent } from "./frame-timings";
|
||||||
import { toUint8Array } from "../util/array-util";
|
import { toUint8Array } from "../util/array-util";
|
||||||
import { rememberNotificationSources } from "./notification-sources";
|
import { rememberNotificationSources, shouldShowNotificationOnGlasses } from "./notification-sources";
|
||||||
import { PHOTO_TONE, grayImageFromPacket } from "./image-files";
|
import { PHOTO_TONE, grayImageFromPacket } from "./image-files";
|
||||||
import { NOTIFICATION_ICON_SIZE } from "./notification-types";
|
import { NOTIFICATION_ICON_SIZE } from "./notification-types";
|
||||||
|
|
||||||
@@ -21,6 +21,7 @@ const ICON_CACHE_MS = 60_000;
|
|||||||
|
|
||||||
let cachedIcons: GrayImage[] = [];
|
let cachedIcons: GrayImage[] = [];
|
||||||
let cachedKeys: string[] = [];
|
let cachedKeys: string[] = [];
|
||||||
|
let cachedPackages = new Map<string, string>();
|
||||||
let cachedAtMs = 0;
|
let cachedAtMs = 0;
|
||||||
const keyedIconCache = new Map<string, { icon: GrayImage | null; atMs: number }>();
|
const keyedIconCache = new Map<string, { icon: GrayImage | null; atMs: number }>();
|
||||||
const KEYED_ICON_CACHE_MAX = 128;
|
const KEYED_ICON_CACHE_MAX = 128;
|
||||||
@@ -95,6 +96,7 @@ export function readActiveNotificationIcons(maxIcons: number, allowStale: boolea
|
|||||||
keys.push(index < keyList.size() ? String(keyList.get(index)) : "");
|
keys.push(index < keyList.size() ? String(keyList.get(index)) : "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cachedPackages = new Map(readActiveNotifications(ALL_NOTIFICATIONS).map(n => [n.key, n.packageName]));
|
||||||
cachedIcons = icons;
|
cachedIcons = icons;
|
||||||
cachedKeys = keys;
|
cachedKeys = keys;
|
||||||
cachedAtMs = now;
|
cachedAtMs = now;
|
||||||
@@ -103,7 +105,12 @@ export function readActiveNotificationIcons(maxIcons: number, allowStale: boolea
|
|||||||
|
|
||||||
/** Copies of the first maxIcons cached icons, with their keys. */
|
/** Copies of the first maxIcons cached icons, with their keys. */
|
||||||
function cachedIconsResult(maxIcons: number, stale: boolean): NotificationIconsResult {
|
function cachedIconsResult(maxIcons: number, stale: boolean): NotificationIconsResult {
|
||||||
return { icons: cachedIcons.slice(0, maxIcons).map(icon => icon.clone()), keys: cachedKeys.slice(0, maxIcons), stale };
|
// Reapply preferences even to a warm/stale cache: toggles take effect immediately.
|
||||||
|
const indices = cachedKeys.map((_, index) => index).filter(index => {
|
||||||
|
const packageName = cachedPackages.get(cachedKeys[index]!);
|
||||||
|
return packageName !== undefined && shouldShowNotificationOnGlasses(packageName);
|
||||||
|
}).slice(0, maxIcons);
|
||||||
|
return { icons: indices.map(index => cachedIcons[index]!.clone()), keys: indices.map(index => cachedKeys[index]!), stale };
|
||||||
}
|
}
|
||||||
|
|
||||||
export type NotificationIconResult = {
|
export type NotificationIconResult = {
|
||||||
@@ -318,6 +325,13 @@ export function readActiveNotifications(maxNotifications = 50): AndroidNotificat
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Glasses display only; discover all sources and filter before the display limit. */
|
||||||
|
export function readVisibleNotifications(maxNotifications = 50): AndroidNotification[] {
|
||||||
|
return readActiveNotifications(ALL_NOTIFICATIONS)
|
||||||
|
.filter(notification => shouldShowNotificationOnGlasses(notification.packageName))
|
||||||
|
.slice(0, Math.max(0, maxNotifications));
|
||||||
|
}
|
||||||
|
|
||||||
export function invokeNotificationAction(notificationKey: string, actionIndex: number): boolean {
|
export function invokeNotificationAction(notificationKey: string, actionIndex: number): boolean {
|
||||||
if (!global.isAndroid || !notificationKey) return false;
|
if (!global.isAndroid || !notificationKey) return false;
|
||||||
invalidateIconCaches();
|
invalidateIconCaches();
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ export class NotificationFilterLayer implements Layer {
|
|||||||
this.menu = new MenuLayer("Notification filter", items, {
|
this.menu = new MenuLayer("Notification filter", items, {
|
||||||
x: 8, y: 8, width: width - 16,
|
x: 8, y: 8, width: width - 16,
|
||||||
opaque: true, showBorder: false,
|
opaque: true, showBorder: false,
|
||||||
footer: "On = show popups on glasses",
|
footer: "On = show notifications on glasses",
|
||||||
}).selectItem(Math.max(0, index));
|
}).selectItem(Math.max(0, index));
|
||||||
} else {
|
} else {
|
||||||
this.menu.setItems(items, index >= 0 ? index : undefined);
|
this.menu.setItems(items, index >= 0 ? index : undefined);
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import {
|
|||||||
ALL_NOTIFICATIONS,
|
ALL_NOTIFICATIONS,
|
||||||
dismissNotification,
|
dismissNotification,
|
||||||
invokeNotificationAction,
|
invokeNotificationAction,
|
||||||
readActiveNotifications,
|
readVisibleNotifications,
|
||||||
readNotificationCustomView,
|
readNotificationCustomView,
|
||||||
readNotificationIconByKey,
|
readNotificationIconByKey,
|
||||||
readNotificationLargeIcon,
|
readNotificationLargeIcon,
|
||||||
@@ -141,7 +141,7 @@ export class NotificationsListLayer implements Layer {
|
|||||||
const image = new GrayImage(width, height, 0);
|
const image = new GrayImage(width, height, 0);
|
||||||
const cardWidth = width - 2 * CARD_X;
|
const cardWidth = width - 2 * CARD_X;
|
||||||
const cardTextWidth = cardWidth - CARD_TEXT_X - 14;
|
const cardTextWidth = cardWidth - CARD_TEXT_X - 14;
|
||||||
const notifications = readActiveNotifications(MAX_NOTIFICATIONS);
|
const notifications = readVisibleNotifications(MAX_NOTIFICATIONS);
|
||||||
const selectedIndex = this.resolveSelectedIndex(notifications);
|
const selectedIndex = this.resolveSelectedIndex(notifications);
|
||||||
const layouts = notifications.map((notification, index) =>
|
const layouts = notifications.map((notification, index) =>
|
||||||
buildNotificationCardLayout(font, notification, index === selectedIndex, cardTextWidth),
|
buildNotificationCardLayout(font, notification, index === selectedIndex, cardTextWidth),
|
||||||
@@ -185,7 +185,7 @@ export class NotificationsListLayer implements Layer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
handleInput(event: InputEvent, ctx: LayerContext): void {
|
handleInput(event: InputEvent, ctx: LayerContext): void {
|
||||||
const notifications = readActiveNotifications(MAX_NOTIFICATIONS);
|
const notifications = readVisibleNotifications(MAX_NOTIFICATIONS);
|
||||||
const selectedIndex = this.resolveSelectedIndex(notifications);
|
const selectedIndex = this.resolveSelectedIndex(notifications);
|
||||||
if (event.type === "double-click") {
|
if (event.type === "double-click") {
|
||||||
// At the app's root this is intercepted by the yield wrapper; reached
|
// At the app's root this is intercepted by the yield wrapper; reached
|
||||||
@@ -252,7 +252,7 @@ export class SingleNotificationLayer implements Layer {
|
|||||||
const font = getDefaultSmallFont();
|
const font = getDefaultSmallFont();
|
||||||
const { width, height } = ctx.stack.getBaseSize();
|
const { width, height } = ctx.stack.getBaseSize();
|
||||||
const image = new GrayImage(width, height, 0);
|
const image = new GrayImage(width, height, 0);
|
||||||
const notification = readActiveNotifications(ALL_NOTIFICATIONS).find((item) => item.key === this.notificationKey);
|
const notification = readVisibleNotifications(ALL_NOTIFICATIONS).find((item) => item.key === this.notificationKey);
|
||||||
|
|
||||||
if (!notification) {
|
if (!notification) {
|
||||||
return this.closeUnavailableNotification(ctx, paintBelow);
|
return this.closeUnavailableNotification(ctx, paintBelow);
|
||||||
@@ -273,7 +273,7 @@ export class SingleNotificationLayer implements Layer {
|
|||||||
else await menu.handleInput(event);
|
else await menu.handleInput(event);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const notification = readActiveNotifications(ALL_NOTIFICATIONS).find((item) => item.key === this.notificationKey);
|
const notification = readVisibleNotifications(ALL_NOTIFICATIONS).find((item) => item.key === this.notificationKey);
|
||||||
if (!notification) {
|
if (!notification) {
|
||||||
this.closeUnavailableNotification(ctx);
|
this.closeUnavailableNotification(ctx);
|
||||||
return;
|
return;
|
||||||
@@ -297,7 +297,7 @@ export class SingleNotificationLayer implements Layer {
|
|||||||
this.close(ctx);
|
this.close(ctx);
|
||||||
} else if (item.kind === "action") {
|
} else if (item.kind === "action") {
|
||||||
invokeNotificationAction(this.notificationKey, item.action.index);
|
invokeNotificationAction(this.notificationKey, item.action.index);
|
||||||
if (!readActiveNotifications(ALL_NOTIFICATIONS).some((active) => active.key === this.notificationKey)) {
|
if (!readVisibleNotifications(ALL_NOTIFICATIONS).some((active) => active.key === this.notificationKey)) {
|
||||||
this.closeUnavailableNotification(ctx);
|
this.closeUnavailableNotification(ctx);
|
||||||
}
|
}
|
||||||
} else if (item.kind === "dismiss") {
|
} else if (item.kind === "dismiss") {
|
||||||
@@ -310,7 +310,7 @@ export class SingleNotificationLayer implements Layer {
|
|||||||
menu: new Menu<ConfirmationItem>({
|
menu: new Menu<ConfirmationItem>({
|
||||||
items: [
|
items: [
|
||||||
{ label: "Cancel", run: (runCtx) => this.close(runCtx) },
|
{ label: "Cancel", run: (runCtx) => this.close(runCtx) },
|
||||||
{ label: "Turn off popups", run: (runCtx) => {
|
{ label: "Hide on glasses", run: (runCtx) => {
|
||||||
setNotificationSourceEnabled(notification, false);
|
setNotificationSourceEnabled(notification, false);
|
||||||
this.close(runCtx);
|
this.close(runCtx);
|
||||||
} },
|
} },
|
||||||
@@ -341,7 +341,7 @@ export class SingleNotificationLayer implements Layer {
|
|||||||
};
|
};
|
||||||
menu.paint(image, menuBox, ctx.stack.isFocused());
|
menu.paint(image, menuBox, ctx.stack.isFocused());
|
||||||
const top = menuBox.y + menuBox.height + CONFIRM_PROMPT_GAP;
|
const top = menuBox.y + menuBox.height + CONFIRM_PROMPT_GAP;
|
||||||
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 prompt = `Hide notifications on glasses from ${source.appName || source.packageName}? You can turn them back on in the Notifications app's Notification filter.`;
|
||||||
const lines = wrapText(font, prompt, width - 2 * CONFIRM_X);
|
const lines = wrapText(font, prompt, width - 2 * CONFIRM_X);
|
||||||
const maxLines = Math.max(1, Math.floor((height - top - 12) / lineStep(font)));
|
const maxLines = Math.max(1, Math.floor((height - top - 12) / lineStep(font)));
|
||||||
for (let index = 0; index < Math.min(lines.length, maxLines); index++) {
|
for (let index = 0; index < Math.min(lines.length, maxLines); index++) {
|
||||||
|
|||||||
@@ -19,5 +19,17 @@ Hardware validation (pending; no glasses tested):
|
|||||||
6. Restart and repeat notifications from enabled and disabled sources. Check that
|
6. Restart and repeat notifications from enabled and disabled sources. Check that
|
||||||
bulk actions and filtering never dismiss notifications on the phone.
|
bulk actions and filtering never dismiss notifications on the phone.
|
||||||
|
|
||||||
Only popup delivery on glasses is filtered. The notification tray and phone
|
The same source preferences now filter glasses popups, the Notifications list,
|
||||||
notification lifecycle retain their existing behavior.
|
and the notification tray/status icons. Filtering happens before display limits;
|
||||||
|
source discovery and the phone notification lifecycle remain unfiltered.
|
||||||
|
Cached tray icons reapply the current preferences on each read, including stale
|
||||||
|
reads, so a changed toggle cannot leave a muted icon visible.
|
||||||
|
|
||||||
|
Additional software coverage: Android list limits and warm/stale icon caches,
|
||||||
|
Disable all / re-enable without a new post, and iOS list/tray filtering.
|
||||||
|
|
||||||
|
Additional hardware validation (pending): with mixed enabled/disabled sources,
|
||||||
|
check the list and status bar together. Toggle a source while its notification
|
||||||
|
is already active, then Disable all and Enable all. Verify immediate removal /
|
||||||
|
restoration without a new notification, including after restart, and verify
|
||||||
|
notifications remain available in the phone notification shade.
|
||||||
@@ -9,7 +9,7 @@ const generator = require('../scripts/generate-pebble-icons.cjs');
|
|||||||
function setup(notifications) {
|
function setup(notifications) {
|
||||||
const load = loader({}, {
|
const load = loader({}, {
|
||||||
'../g2/ancs-client': { AncsClient: class {}, ANCS_CONNECT_MESSAGE: '' },
|
'../g2/ancs-client': { AncsClient: class {}, ANCS_CONNECT_MESSAGE: '' },
|
||||||
'./notification-sources': { rememberNotificationSources() {} },
|
'./notification-sources': { rememberNotificationSources() {}, shouldShowNotificationOnGlasses: packageName => packageName !== "muted" },
|
||||||
});
|
});
|
||||||
const icons = load('app/native/notification-icons.ios.ts');
|
const icons = load('app/native/notification-icons.ios.ts');
|
||||||
icons.bindIosNotifications({ read: () => notifications });
|
icons.bindIosNotifications({ read: () => notifications });
|
||||||
@@ -106,3 +106,11 @@ test('the icon generator inverts Pebble art: black ink lights up, white fills hi
|
|||||||
const decoded = loader({}, { './pebble-icons-data': loader({}, {})(dataFile) })('app/graphics/pebble-icons.ts').pebbleIcon('probe');
|
const decoded = loader({}, { './pebble-icons-data': loader({}, {})(dataFile) })('app/graphics/pebble-icons.ts').pebbleIcon('probe');
|
||||||
assert.deepEqual([...decoded.pixels], [...pixels]);
|
assert.deepEqual([...decoded.pixels], [...pixels]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('muted sources are hidden before list and icon limits without removing phone notifications', () => {
|
||||||
|
const notifications = [notification('hidden', 'muted', '0'), notification('shown', 'allowed', '0')];
|
||||||
|
const { icons } = setup(notifications);
|
||||||
|
assert.deepEqual([...icons.readVisibleNotifications(1)].map(n => n.key), ['shown']);
|
||||||
|
assert.deepEqual([...icons.readActiveNotificationIcons(1, false).keys], ['shown']);
|
||||||
|
assert.equal(icons.readActiveNotifications(10).length, 2);
|
||||||
|
});
|
||||||
@@ -70,6 +70,7 @@ function ui(fontSize = 12) {
|
|||||||
const native = {
|
const native = {
|
||||||
ALL_NOTIFICATIONS: 0x7fffffff,
|
ALL_NOTIFICATIONS: 0x7fffffff,
|
||||||
readActiveNotifications: () => { prefs.rememberNotificationSources(active); return active; },
|
readActiveNotifications: () => { prefs.rememberNotificationSources(active); return active; },
|
||||||
|
readVisibleNotifications: () => { prefs.rememberNotificationSources(active); return active.filter(n => prefs.shouldShowNotificationOnGlasses(n.packageName)); },
|
||||||
dismissNotification: () => { dismissals++; active = []; return true; },
|
dismissNotification: () => { dismissals++; active = []; return true; },
|
||||||
readNotificationIconByKey: () => ({ icon: null, stale: false }),
|
readNotificationIconByKey: () => ({ icon: null, stale: false }),
|
||||||
};
|
};
|
||||||
@@ -317,3 +318,33 @@ test('controller filters before waking or opening a popup, and still refreshes t
|
|||||||
elapse();
|
elapse();
|
||||||
assert.equal(popups, 1);
|
assert.equal(popups, 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Android display filters before limits and reapplies preferences to cached tray icons', () => {
|
||||||
|
const prefs = store();
|
||||||
|
const active = [{ ...mail, key: 'hidden' }, { packageName: 'allowed', appName: 'Allowed', key: 'shown' }];
|
||||||
|
const file = ts.createSourceFile('icons.ts', source('app/native/notification-icons.ts'), ts.ScriptTarget.Latest, true);
|
||||||
|
const functions = file.statements.filter(node => ts.isFunctionDeclaration(node) &&
|
||||||
|
['readVisibleNotifications', 'cachedIconsResult'].includes(node.name?.text)).map(node => node.getText(file));
|
||||||
|
const { readVisibleNotifications, cachedIconsResult } = evaluate(functions.join('\n') + '\nexport { cachedIconsResult };', null, {
|
||||||
|
ALL_NOTIFICATIONS: 0x7fffffff,
|
||||||
|
readActiveNotifications: () => { prefs.rememberNotificationSources(active); return active; },
|
||||||
|
shouldShowNotificationOnGlasses: prefs.shouldShowNotificationOnGlasses,
|
||||||
|
cachedKeys: ['hidden', 'shown'],
|
||||||
|
cachedPackages: new Map(active.map(n => [n.key, n.packageName])),
|
||||||
|
cachedIcons: [{ clone: () => 'mail-icon' }, { clone: () => 'allowed-icon' }],
|
||||||
|
});
|
||||||
|
prefs.setNotificationSourceEnabled(mail, false);
|
||||||
|
assert.deepEqual(Array.from(readVisibleNotifications(1), n => n.key), ['shown']);
|
||||||
|
for (const stale of [false, true]) {
|
||||||
|
const result = cachedIconsResult(1, stale);
|
||||||
|
assert.deepEqual(Array.from(result.keys), ['shown']);
|
||||||
|
assert.deepEqual(Array.from(result.icons), ['allowed-icon']);
|
||||||
|
assert.equal(result.stale, stale);
|
||||||
|
}
|
||||||
|
prefs.setAllNotificationSourcesEnabled(false);
|
||||||
|
assert.equal(cachedIconsResult(10, false).keys.length, 0);
|
||||||
|
assert.equal(readVisibleNotifications(50).length, 0);
|
||||||
|
prefs.setNotificationSourceEnabled(mail, true);
|
||||||
|
assert.deepEqual(Array.from(cachedIconsResult(1, false).keys), ['hidden']);
|
||||||
|
assert.equal(active.length, 2);
|
||||||
|
});
|
||||||
Reference in new issue
Block a user