forked from Blendman974/faceclaw
Merge notification filter preferences
This commit is contained in:
4 files changed
+135
-12
No files matched your search
@@ -1,6 +1,16 @@
|
|||||||
import { getStringSetting, setStringSetting } from "./settings-store";
|
import { getStringSetting, setStringSetting } from "./settings-store";
|
||||||
|
|
||||||
const SOURCES_KEY = "notifications.sources";
|
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 = {
|
export type NotificationSource = {
|
||||||
packageName: string;
|
packageName: string;
|
||||||
@@ -8,8 +18,15 @@ export type NotificationSource = {
|
|||||||
showOnGlasses: boolean;
|
showOnGlasses: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type SavedSource = Omit<NotificationSource, "showOnGlasses"> & { showOnGlasses?: boolean };
|
||||||
|
|
||||||
/** Sources outlive their active notifications so a muted app can always be re-enabled. */
|
/** Sources outlive their active notifications so a muted app can always be re-enabled. */
|
||||||
export function readNotificationSources(): NotificationSource[] {
|
export function readNotificationSources(): NotificationSource[] {
|
||||||
|
const enabled = newNotificationAppsEnabledByDefault();
|
||||||
|
return readSavedSources().map((source) => ({ ...source, showOnGlasses: source.showOnGlasses ?? enabled }));
|
||||||
|
}
|
||||||
|
|
||||||
|
function readSavedSources(): SavedSource[] {
|
||||||
try {
|
try {
|
||||||
const parsed = JSON.parse(getStringSetting(SOURCES_KEY, "[]"));
|
const parsed = JSON.parse(getStringSetting(SOURCES_KEY, "[]"));
|
||||||
if (!Array.isArray(parsed)) return [];
|
if (!Array.isArray(parsed)) return [];
|
||||||
@@ -17,7 +34,7 @@ export function readNotificationSources(): NotificationSource[] {
|
|||||||
.map((source) => ({
|
.map((source) => ({
|
||||||
packageName: source.packageName,
|
packageName: source.packageName,
|
||||||
appName: typeof source.appName === "string" && source.appName ? source.appName : 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 {
|
} catch {
|
||||||
return [];
|
return [];
|
||||||
@@ -25,7 +42,7 @@ export function readNotificationSources(): NotificationSource[] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function rememberNotificationSources(notifications: { packageName: string; appName: string }[]): void {
|
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;
|
let changed = false;
|
||||||
for (const notification of notifications) {
|
for (const notification of notifications) {
|
||||||
if (!notification.packageName) continue;
|
if (!notification.packageName) continue;
|
||||||
@@ -35,7 +52,7 @@ export function rememberNotificationSources(notifications: { packageName: string
|
|||||||
sources.set(notification.packageName, {
|
sources.set(notification.packageName, {
|
||||||
packageName: notification.packageName,
|
packageName: notification.packageName,
|
||||||
appName,
|
appName,
|
||||||
showOnGlasses: previous?.showOnGlasses ?? true,
|
showOnGlasses: previous?.showOnGlasses,
|
||||||
});
|
});
|
||||||
changed = true;
|
changed = true;
|
||||||
}
|
}
|
||||||
@@ -44,12 +61,13 @@ export function rememberNotificationSources(notifications: { packageName: string
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function shouldShowNotificationOnGlasses(packageName: string): boolean {
|
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 {
|
export function setNotificationSourceEnabled(source: { packageName: string; appName: string }, enabled: boolean): void {
|
||||||
if (!source.packageName) return;
|
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, {
|
sources.set(source.packageName, {
|
||||||
packageName: source.packageName,
|
packageName: source.packageName,
|
||||||
appName: source.appName || 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())));
|
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,
|
||||||
|
}))));
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@ import { getDefaultSmallFont } from "../graphics/ui-fonts";
|
|||||||
import { GrayImage } from "../graphics/image";
|
import { GrayImage } from "../graphics/image";
|
||||||
import { truncateText } from "../graphics/textwrap";
|
import { truncateText } from "../graphics/textwrap";
|
||||||
import { ALL_NOTIFICATIONS, readActiveNotifications } from "../native/notification-icons";
|
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 InputEvent } from "./gestures";
|
||||||
import { type Layer, type LayerContext, type PaintBelow } from "./layers";
|
import { type Layer, type LayerContext, type PaintBelow } from "./layers";
|
||||||
import { drawToggleMenuItem, MenuLayer, type MenuItem } from "./menu";
|
import { drawToggleMenuItem, MenuLayer, type MenuItem } from "./menu";
|
||||||
@@ -11,14 +11,26 @@ import { drawToggleMenuItem, MenuLayer, type MenuItem } from "./menu";
|
|||||||
export class NotificationFilterLayer implements Layer {
|
export class NotificationFilterLayer implements Layer {
|
||||||
private menu: MenuLayer | null = null;
|
private menu: MenuLayer | null = null;
|
||||||
private menuWidth = 0;
|
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 packages: string[] = [];
|
||||||
|
|
||||||
private currentMenu(ctx: LayerContext): MenuLayer {
|
private currentMenu(ctx: LayerContext): MenuLayer {
|
||||||
readActiveNotifications(ALL_NOTIFICATIONS);
|
readActiveNotifications(ALL_NOTIFICATIONS);
|
||||||
const sources = readNotificationSources().sort((a, b) =>
|
const sources = readNotificationSources().sort((a, b) =>
|
||||||
a.appName.localeCompare(b.appName) || a.packageName.localeCompare(b.packageName));
|
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<MenuItem>((source) => ({
|
||||||
label: source.appName,
|
label: source.appName,
|
||||||
onSelect: () => setNotificationSourceEnabled(source, !shouldShowNotificationOnGlasses(source.packageName)),
|
onSelect: () => setNotificationSourceEnabled(source, !shouldShowNotificationOnGlasses(source.packageName)),
|
||||||
render: ({ image, x, y, width, selected }) => {
|
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),
|
drawToggleMenuItem(image, font, x, y, width, truncateText(font, source.appName, width - 48),
|
||||||
shouldShowNotificationOnGlasses(source.packageName), selected);
|
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 selectedPackage = this.menu?.selectedIndex != null ? this.packages[this.menu.selectedIndex] : undefined;
|
||||||
const index = sources.findIndex((source) => source.packageName === selectedPackage);
|
this.packages = ["default", "enable-all", "disable-all", ...sources.map((source) => `source:${source.packageName}`)];
|
||||||
this.packages = sources.map((source) => source.packageName);
|
const index = selectedPackage === undefined ? 0 : this.packages.indexOf(selectedPackage);
|
||||||
|
|
||||||
const width = ctx.stack.getBaseSize().width;
|
const width = ctx.stack.getBaseSize().width;
|
||||||
if (!this.menu || width !== this.menuWidth) {
|
if (!this.menu || width !== this.menuWidth) {
|
||||||
|
|||||||
@@ -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.
|
||||||
@@ -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 () => {
|
test('filter toggles persist after notifications disappear and discovers new sources while open', async () => {
|
||||||
const app = ui();
|
const app = ui();
|
||||||
app.paint(app.filter);
|
app.paint(app.filter);
|
||||||
|
for (let i = 0; i < 3; i++) await app.input(app.filter, 'scroll-down');
|
||||||
await app.input(app.filter, 'click');
|
await app.input(app.filter, 'click');
|
||||||
assert.equal(app.prefs.shouldShowNotificationOnGlasses(mail.packageName), false);
|
assert.equal(app.prefs.shouldShowNotificationOnGlasses(mail.packageName), false);
|
||||||
app.active([{ packageName: 'com.alpha', appName: 'Alpha' }]);
|
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);
|
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 () => {
|
test('popup action wraps fully and stays visible with small and large fonts', async () => {
|
||||||
for (const size of [12, 20]) {
|
for (const size of [12, 20]) {
|
||||||
const app = ui(size);
|
const app = ui(size);
|
||||||
|
|||||||
Reference in new issue
Block a user