forked from Blendman974/faceclaw
[verified] feat: follow phone Do Not Disturb for notification wake
This commit is contained in:
1 parent
54ba6b4f10
commit
9ca3574ae7
14 files changed
+574
-20
No files matched your search
@@ -16,6 +16,7 @@ import { mediaControllerBridge } from "../native/media-controller";
|
||||
import { nightscoutBridge } from "../native/nightscout-bridge";
|
||||
import { weatherBridge } from "../native/weather";
|
||||
import { ALL_NOTIFICATIONS, onAndroidNotificationPosted, onAndroidNotificationRemoved, readActiveNotifications } from "../native/notification-icons";
|
||||
import { isPhoneDoNotDisturbEnabled } from "../native/phone-do-not-disturb";
|
||||
import { watchNotificationListenerBinding } from "../native/notification-access";
|
||||
import { openEvenAppSettings, readEvenAppNotificationState } from "../native/even-app-conflict";
|
||||
import { defaultRingName, setGlassesRingLink } from "../native/ring-status";
|
||||
@@ -364,7 +365,7 @@ class DashboardController {
|
||||
private offAndroidNotification: (() => void) | null = null;
|
||||
private offAndroidNotificationRemoved: (() => void) | null = null;
|
||||
/** New notifications wait here briefly before popping up (see NotificationPopupDelay). */
|
||||
private readonly notificationPopups = new NotificationPopupDelay((notificationKey) => this.popUpAndroidNotification(notificationKey));
|
||||
private readonly notificationPopups = new NotificationPopupDelay((notificationKey, canWakeScreen) => this.popUpAndroidNotification(notificationKey, canWakeScreen));
|
||||
private lastInput = "waiting...";
|
||||
private lastSys = "none yet";
|
||||
private shellRenderInProgress = false;
|
||||
@@ -472,7 +473,7 @@ class DashboardController {
|
||||
app.boot?.(this.buildAppContext(app));
|
||||
}
|
||||
timerEngine.onChange(() => this.refreshAlarmReliabilityWarning());
|
||||
this.offAndroidNotification = onAndroidNotificationPosted((notificationKey) => this.handleAndroidNotificationPosted(notificationKey));
|
||||
this.offAndroidNotification = onAndroidNotificationPosted((notificationKey, canWakeScreen) => this.handleAndroidNotificationPosted(notificationKey, canWakeScreen));
|
||||
this.offAndroidNotificationRemoved = onAndroidNotificationRemoved((notificationKey) => this.handleAndroidNotificationRemoved(notificationKey));
|
||||
watchNotificationListenerBinding();
|
||||
readActiveNotifications(ALL_NOTIFICATIONS);
|
||||
@@ -3008,12 +3009,13 @@ class DashboardController {
|
||||
this.schedulePreviewUpdate();
|
||||
}
|
||||
|
||||
private handleAndroidNotificationPosted(notificationKey: string): void {
|
||||
private handleAndroidNotificationPosted(notificationKey: string, canWakeScreen = true): void {
|
||||
// The tray shows the notification at once; its popup waits out a short
|
||||
// delay, so one withdrawn right away (see NotificationPopupDelay) never shows.
|
||||
const notification = readActiveNotifications(ALL_NOTIFICATIONS).find((item) => item.key === notificationKey);
|
||||
if (notification && shouldPopUpNewNotification(notification.packageName)) {
|
||||
this.notificationPopups.post(notificationKey, notificationPopupDelayMs(notification));
|
||||
this.notificationPopups.post(notificationKey, notificationPopupDelayMs(notification),
|
||||
canWakeScreen && !isPhoneDoNotDisturbEnabled());
|
||||
}
|
||||
this.requestShellRender();
|
||||
}
|
||||
@@ -3031,10 +3033,13 @@ class DashboardController {
|
||||
}
|
||||
|
||||
/** A posted notification waited out its popup delay: show it, if it is still there. */
|
||||
private popUpAndroidNotification(notificationKey: string): void {
|
||||
private popUpAndroidNotification(notificationKey: string, canWakeScreen = true): void {
|
||||
try {
|
||||
const notification = readActiveNotifications(ALL_NOTIFICATIONS).find((item) => item.key === notificationKey);
|
||||
if (!notification || !shouldPopUpNewNotification(notification.packageName)) return;
|
||||
// Keep the arrival-time restriction and re-check DND immediately before
|
||||
// waking: a delayed popup must neither outlive DND nor bypass a new DND.
|
||||
if (!shell.isScreenOn() && (!canWakeScreen || isPhoneDoNotDisturbEnabled())) return;
|
||||
// New notifications open a shell modal over the app viewport; if the
|
||||
// screen was off, wake for it and go back to sleep when it is closed.
|
||||
// Waking while already on would steal focus, so only wake from sleep.
|
||||
|
||||
@@ -26,7 +26,7 @@ let cachedAtMs = 0;
|
||||
const keyedIconCache = new Map<string, { icon: GrayImage | null; atMs: number }>();
|
||||
const KEYED_ICON_CACHE_MAX = 128;
|
||||
let notificationListenerProxy: any = null;
|
||||
const notificationPostedListeners = new Set<(notificationKey: string) => void>();
|
||||
const notificationPostedListeners = new Set<(notificationKey: string, canWakeScreen?: boolean) => void>();
|
||||
const notificationRemovedListeners = new Set<(notificationKey: string) => void>();
|
||||
let listenerStateObserver: any = null;
|
||||
const listenerStateListeners = new Set<() => void>();
|
||||
@@ -358,7 +358,7 @@ export function dismissAllNotifications(): boolean {
|
||||
return Boolean(com.faceclaw.app.FaceclawMediaNotificationListenerService.dismissAllNotifications());
|
||||
}
|
||||
|
||||
export function onAndroidNotificationPosted(listener: (notificationKey: string) => void): () => void {
|
||||
export function onAndroidNotificationPosted(listener: (notificationKey: string, canWakeScreen?: boolean) => void): () => void {
|
||||
return addNotificationListener(notificationPostedListeners, listener);
|
||||
}
|
||||
|
||||
@@ -412,8 +412,8 @@ function onListenerStateChanged(listener: () => void): () => void {
|
||||
}
|
||||
|
||||
function addNotificationListener(
|
||||
listeners: Set<(notificationKey: string) => void>,
|
||||
listener: (notificationKey: string) => void,
|
||||
listeners: Set<(notificationKey: string, canWakeScreen?: boolean) => void>,
|
||||
listener: (notificationKey: string, canWakeScreen?: boolean) => void,
|
||||
): () => void {
|
||||
listeners.add(listener);
|
||||
ensureNativeNotificationListener();
|
||||
@@ -458,8 +458,8 @@ function normalizeNotification(value: any): AndroidNotification | null {
|
||||
function ensureNativeNotificationListener(): void {
|
||||
if (!global.isAndroid || notificationListenerProxy) return;
|
||||
notificationListenerProxy = new com.faceclaw.app.FaceclawNotificationListener({
|
||||
onNotificationPosted: (notificationKey: string) => {
|
||||
dispatchNotificationEvent(notificationPostedListeners, notificationKey);
|
||||
onNotificationPosted: (notificationKey: string, canWakeScreen?: boolean) => {
|
||||
dispatchNotificationEvent(notificationPostedListeners, notificationKey, canWakeScreen);
|
||||
},
|
||||
onNotificationRemoved: (notificationKey: string) => {
|
||||
dispatchNotificationEvent(notificationRemovedListeners, notificationKey);
|
||||
@@ -470,13 +470,21 @@ function ensureNativeNotificationListener(): void {
|
||||
);
|
||||
}
|
||||
|
||||
function dispatchNotificationEvent(listeners: Set<(notificationKey: string) => void>, notificationKey: string): void {
|
||||
function dispatchNotificationEvent(
|
||||
listeners: Set<(notificationKey: string, canWakeScreen?: boolean) => void>,
|
||||
notificationKey: string,
|
||||
canWakeScreen = true,
|
||||
): void {
|
||||
invalidateIconCaches();
|
||||
const key = String(notificationKey);
|
||||
const snapshot = Array.from(listeners);
|
||||
setTimeout(() => {
|
||||
for (const listener of snapshot) {
|
||||
listener(key);
|
||||
if (listeners === notificationPostedListeners) {
|
||||
listener(key, canWakeScreen);
|
||||
} else {
|
||||
listener(key);
|
||||
}
|
||||
}
|
||||
}, 0);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Utils } from "@nativescript/core";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
/** Read the phone's current interruption filter; never change its DND policy. */
|
||||
export function isPhoneDoNotDisturbEnabled(): boolean {
|
||||
if (!global.isAndroid) return false;
|
||||
try {
|
||||
return Boolean(com.faceclaw.app.FaceclawMediaNotificationListenerService.isPhoneDoNotDisturbEnabled(
|
||||
Utils.android.getApplicationContext(),
|
||||
));
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -33,11 +33,11 @@ export const scheduleTimeout: ScheduleFn = (run, delayMs) => {
|
||||
};
|
||||
|
||||
export class NotificationPopupDelay {
|
||||
private readonly pending = new Map<string, { dueAtMs: number; cancel: () => void }>();
|
||||
private readonly pending = new Map<string, { dueAtMs: number; cancel: () => void; wakePolicy: { canWakeScreen: boolean } }>();
|
||||
|
||||
/** `ready` receives each notification that survived its delay, to re-check and pop up. */
|
||||
constructor(
|
||||
private readonly ready: (notificationKey: string) => void,
|
||||
private readonly ready: (notificationKey: string, canWakeScreen: boolean) => void,
|
||||
private readonly schedule: ScheduleFn = scheduleTimeout,
|
||||
private readonly now: () => number = Date.now,
|
||||
) {}
|
||||
@@ -49,18 +49,22 @@ export class NotificationPopupDelay {
|
||||
* foreground-service placeholder into a real message (same key) gets the
|
||||
* shorter delay.
|
||||
*/
|
||||
post(notificationKey: string, delayMs: number): void {
|
||||
post(notificationKey: string, delayMs: number, canWakeScreen = true): void {
|
||||
const dueAtMs = this.now() + delayMs;
|
||||
const existing = this.pending.get(notificationKey);
|
||||
// Share this object with an existing timer: a repost may restrict wake,
|
||||
// but cannot turn a DND-silenced pending notification into a fresh alert.
|
||||
const wakePolicy = existing?.wakePolicy ?? { canWakeScreen: true };
|
||||
wakePolicy.canWakeScreen = wakePolicy.canWakeScreen && canWakeScreen;
|
||||
if (existing) {
|
||||
if (existing.dueAtMs <= dueAtMs) return;
|
||||
existing.cancel();
|
||||
}
|
||||
const cancel = this.schedule(() => {
|
||||
this.pending.delete(notificationKey);
|
||||
this.ready(notificationKey);
|
||||
this.ready(notificationKey, wakePolicy.canWakeScreen);
|
||||
}, delayMs);
|
||||
this.pending.set(notificationKey, { dueAtMs, cancel });
|
||||
this.pending.set(notificationKey, { dueAtMs, cancel, wakePolicy });
|
||||
}
|
||||
|
||||
/** The notification went away before popping up; returns whether it was waiting. */
|
||||
|
||||
@@ -75,6 +75,7 @@ import { ShellModalLayer } from "./modal-layer";
|
||||
import { NotificationModalQueue } from "./notification-modal-queue";
|
||||
import { NotificationPopupTimeout } from "./notification-popup-timeout";
|
||||
import { ALL_NOTIFICATIONS, readActiveNotifications } from "../../native/notification-icons";
|
||||
import { isPhoneDoNotDisturbEnabled } from "../../native/phone-do-not-disturb";
|
||||
import { ToolDebugMenuLayer } from "./tool-debug-layer";
|
||||
import type { InProcessWindow } from "./in-process-window";
|
||||
import { BrightnessPickerLayer } from "./brightness-picker-layer";
|
||||
@@ -939,6 +940,7 @@ class Shell {
|
||||
* shown: in the notification modal, waking the screen for it if it was off.
|
||||
*/
|
||||
postAppNotification(notification: AppNotification): void {
|
||||
if (!this.screenOn && isPhoneDoNotDisturbEnabled()) return;
|
||||
this.appNotifications.set(notification.key, notification);
|
||||
const wokeScreen = this.screenOn ? false : this.wake("sidebar");
|
||||
this.openNotificationModal(notification.key, wokeScreen);
|
||||
|
||||
Reference in new issue
Block a user