Clear notification popups when they're dismissed on the phone, and avoid showing some notifications that are spurious

This commit is contained in:
James Babcock committed 2026-10-14 02:05:23 -07:00
1 parent 42c2d5cd55
commit bf92a5a2ce
17 files changed
+422 -70

No files matched your search

+11
View File
@@ -74,6 +74,17 @@ export class NotificationModalQueue<M> {
this.openNext(current.wokeScreen);
}
/**
* The notification went away on the phone (its source cancelled it, or it
* was dismissed there). Drops it from the queue, and returns the open modal
* if that is the one showing it, for the host to close (via closed()).
*/
withdrawn(notificationKey: string): M | null {
const index = this.queued.findIndex((entry) => entry.notificationKey === notificationKey);
if (index >= 0) this.queued.splice(index, 1);
return this.current?.notificationKey === notificationKey ? this.current.modal : null;
}
/** Notifications waiting behind the open modal (for diagnostics and tests). */
queuedKeys(): string[] {
return this.queued.map((entry) => entry.notificationKey);
+79
View File
@@ -0,0 +1,79 @@
/**
* Holds each newly posted notification back briefly before it pops up on the
* glasses, so one that its source withdraws right away never shows (nor wakes
* the screen). Google Messages, for one, posts a "Messages is working in the
* background" foreground-service notification whenever it handles an incoming
* message or a background sync, then cancels it a second or two later, after
* posting the message's own notification under a different key (if any).
*/
/** Delay for ordinary notifications: enough to skip one withdrawn or replaced as soon as it is posted. */
export const NOTIFICATION_POPUP_DELAY_MS = 500;
/**
* Delay for foreground-service notifications. They are "app is working"
* status, often gone within seconds; Android itself defers showing them for
* 10s for the same reason. Messages' lasts up to ~2s (Pixel usage-stats timings).
*/
export const FOREGROUND_SERVICE_POPUP_DELAY_MS = 5000;
/** Categories that must pop up promptly even from a foreground service (an incoming call, a ringing alarm). */
const URGENT_CATEGORIES = new Set(["call", "alarm"]);
export function notificationPopupDelayMs(notification: { foregroundService?: boolean; category: string }): number {
return notification.foregroundService && !URGENT_CATEGORIES.has(notification.category)
? FOREGROUND_SERVICE_POPUP_DELAY_MS : NOTIFICATION_POPUP_DELAY_MS;
}
/** Runs `run` after `delayMs`; returns a function that cancels it. */
export type ScheduleFn = (run: () => void, delayMs: number) => () => void;
const scheduleTimeout: ScheduleFn = (run, delayMs) => {
const timer = setTimeout(run, delayMs);
return () => clearTimeout(timer);
};
export class NotificationPopupDelay {
private readonly pending = new Map<string, { dueAtMs: number; cancel: () => void }>();
/** `ready` receives each notification that survived its delay, to re-check and pop up. */
constructor(
private readonly ready: (notificationKey: string) => void,
private readonly schedule: ScheduleFn = scheduleTimeout,
private readonly now: () => number = Date.now,
) {}
/**
* A notification was posted, to pop up after `delayMs`. A repost while it
* waits can only bring the popup forward: a notification its source keeps
* updating is not held back indefinitely, and one rewritten from a
* foreground-service placeholder into a real message (same key) gets the
* shorter delay.
*/
post(notificationKey: string, delayMs: number): void {
const dueAtMs = this.now() + delayMs;
const existing = this.pending.get(notificationKey);
if (existing) {
if (existing.dueAtMs <= dueAtMs) return;
existing.cancel();
}
const cancel = this.schedule(() => {
this.pending.delete(notificationKey);
this.ready(notificationKey);
}, delayMs);
this.pending.set(notificationKey, { dueAtMs, cancel });
}
/** The notification went away before popping up; returns whether it was waiting. */
withdraw(notificationKey: string): boolean {
const existing = this.pending.get(notificationKey);
if (!existing) return false;
existing.cancel();
this.pending.delete(notificationKey);
return true;
}
/** Notifications waiting out their delay (for diagnostics and tests). */
pendingKeys(): string[] {
return Array.from(this.pending.keys());
}
}
+33 -15
View File
@@ -239,6 +239,9 @@ export type TrayWidget = {
openWindow: () => { window: ShellWindow; opened: boolean } | null;
};
/** A new-notification popup: the shell modal, and the notification view inside it. */
type NotificationPopup = { modal: ShellModalLayer; content: SingleNotificationLayer };
/**
* The app switcher's selection while it is on a status bar item rather than
* a window's icon (bottom switcher only; see Shell.moveSelection): a
@@ -434,7 +437,7 @@ class Shell {
private readonly trayIcons = new Map<string, { icon: GrayImage; widget?: TrayWidget }>();
private activeVoiceLayer: VoiceInputLayer | null = null;
private activeKeyboardLayer: KeyboardInputLayer | null = null;
private readonly notificationModals = new NotificationModalQueue<ShellModalLayer>({
private readonly notificationModals = new NotificationModalQueue<NotificationPopup>({
open: (notificationKey) => this.pushNotificationModal(notificationKey),
isAvailable: (notificationKey) => {
const notification = readActiveNotifications(ALL_NOTIFICATIONS).find((item) => item.key === notificationKey);
@@ -853,25 +856,40 @@ class Shell {
this.config.requestShellRender();
}
private pushNotificationModal(notificationKey: string): ShellModalLayer {
const modal: ShellModalLayer = new ShellModalLayer(
new SingleNotificationLayer(notificationKey, {
origin: "new-notification-modal",
onClose: () => this.closeNotificationModal(modal),
}),
this.config.actions,
() => this.notificationModals.removed(modal, this.screenOn),
);
this.stack.push(modal);
return modal;
/**
* A notification went away on the phone: cancelled by its source (e.g. a
* placeholder replaced moments later by the real notification under another
* key), or dismissed. A popup queued for it is dropped, and one showing it
* closes so the next one opens, as if the user had closed it -- unless it
* is asking to confirm turning off its source, which outlives the
* notification it dismissed.
*/
notificationRemoved(notificationKey: string): void {
const popup = this.notificationModals.withdrawn(notificationKey);
if (popup && !popup.content.outlivesNotification()) {
this.closeNotificationModal(popup);
}
}
private closeNotificationModal(modal: ShellModalLayer): void {
private pushNotificationModal(notificationKey: string): NotificationPopup {
const content = new SingleNotificationLayer(notificationKey, {
origin: "new-notification-modal",
onClose: () => this.closeNotificationModal(popup),
});
const popup: NotificationPopup = {
content,
modal: new ShellModalLayer(content, this.config.actions, () => this.notificationModals.removed(popup, this.screenOn)),
};
this.stack.push(popup.modal);
return popup;
}
private closeNotificationModal(popup: NotificationPopup): void {
// The queue first: it opens the next notification or puts the screen back
// to sleep, and then ignores the removal below. remove() rather than a
// pop, so a menu opened over the modal doesn't leave it stuck.
this.notificationModals.closed(modal);
this.stack.remove(modal);
this.notificationModals.closed(popup);
this.stack.remove(popup.modal);
this.config.requestShellRender();
}