forked from Blendman974/faceclaw
Add notification filters
This commit is contained in:
1 parent
5c3b71aeea
commit
3d39d27456
8 files changed
+205
-18
No files matched your search
@@ -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 [];
|
||||
}
|
||||
|
||||
@@ -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())));
|
||||
}
|
||||
Reference in new issue
Block a user