forked from Blendman974/faceclaw
Notification listener rebinding
This commit is contained in:
1 parent
75e0b15d62
commit
bf2addd287
6 files changed
+296
-68
No files matched your search
@@ -2,3 +2,6 @@ import { iosNotificationState, iosNotificationMessage } from './notification-ico
|
||||
export function isNotificationListenerEnabled(): boolean { return iosNotificationState() === 'ready' }
|
||||
export function requestNotificationListenerAccess(): void { /* iOS exposes no deep link to this Bluetooth setting. */ }
|
||||
export function notificationEmptyMessage(): string { return iosNotificationMessage() }
|
||||
export type NotificationListenerState = "connected" | "no-access" | "connecting" | "stuck"
|
||||
export function notificationListenerState(): NotificationListenerState { return isNotificationListenerEnabled() ? "connected" : "no-access" }
|
||||
export function watchNotificationListenerBinding(): void { /* ANCS has no listener binding to watch. */ }
|
||||
@@ -32,7 +32,44 @@ export function requestNotificationListenerAccess(): void {
|
||||
com.faceclaw.app.FaceclawEvenAppDetector.openNotificationAccessSettings(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether Android has the listener connected, and if not, why not:
|
||||
* "connecting" is access granted but not (yet, or again) bound; "stuck" is
|
||||
* still unbound after the native watchdog's forced rebinds, which only the
|
||||
* user toggling notification access is sure to fix.
|
||||
*/
|
||||
export type NotificationListenerState = "connected" | "no-access" | "connecting" | "stuck";
|
||||
|
||||
export function notificationListenerState(): NotificationListenerState {
|
||||
if (!global.isAndroid) return "connected";
|
||||
const context = Utils.android.getApplicationContext();
|
||||
if (!context) return "no-access";
|
||||
return String(
|
||||
com.faceclaw.app.FaceclawMediaNotificationListenerService.getListenerState(context),
|
||||
) as NotificationListenerState;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the native watchdog that rebinds the listener when Android drops it
|
||||
* while access is granted (see FaceclawMediaNotificationListenerService
|
||||
* .watchListenerBinding). Called once at boot.
|
||||
*/
|
||||
export function watchNotificationListenerBinding(): void {
|
||||
if (!global.isAndroid) return;
|
||||
const context = Utils.android.getApplicationContext();
|
||||
if (!context) return;
|
||||
com.faceclaw.app.FaceclawMediaNotificationListenerService.watchListenerBinding(context);
|
||||
}
|
||||
|
||||
export function notificationEmptyMessage(): string {
|
||||
return isNotificationListenerEnabled() ? "No current Android notifications."
|
||||
: "Grant permission on your phone to view notifications on the glasses.";
|
||||
switch (notificationListenerState()) {
|
||||
case "no-access":
|
||||
return "Grant permission on your phone to view notifications on the glasses.";
|
||||
case "connecting":
|
||||
return "Connecting to Android notifications...";
|
||||
case "stuck":
|
||||
return "Android stopped sending notifications to Faceclaw. Turn Faceclaw's notification access off and on again in phone settings.";
|
||||
case "connected":
|
||||
return "No current Android notifications.";
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,8 @@ const PICTURE_CACHE_MAX = 8;
|
||||
let notificationListenerProxy: any = null;
|
||||
const notificationPostedListeners = new Set<(notificationKey: string) => void>();
|
||||
const notificationRemovedListeners = new Set<(notificationKey: string) => void>();
|
||||
let listenerStateObserver: any = null;
|
||||
const listenerStateListeners = new Set<() => void>();
|
||||
|
||||
function invalidateIconCaches(): void {
|
||||
cachedAtMs = 0;
|
||||
@@ -274,13 +276,43 @@ export function onAndroidNotificationRemoved(listener: (notificationKey: string)
|
||||
return addNotificationListener(notificationRemovedListeners, listener);
|
||||
}
|
||||
|
||||
/** Any change to the listed notifications (a post or a removal), for views that just repaint. */
|
||||
/**
|
||||
* Any change to the listed notifications (a post, a removal, or the listener
|
||||
* connecting or going missing), for views that just repaint.
|
||||
*/
|
||||
export function onAndroidNotificationsChanged(listener: () => void): () => void {
|
||||
const offPosted = onAndroidNotificationPosted(listener);
|
||||
const offRemoved = onAndroidNotificationRemoved(listener);
|
||||
const offState = onListenerStateChanged(listener);
|
||||
return () => {
|
||||
offPosted();
|
||||
offRemoved();
|
||||
offState();
|
||||
};
|
||||
}
|
||||
|
||||
function onListenerStateChanged(listener: () => void): () => void {
|
||||
listenerStateListeners.add(listener);
|
||||
if (global.isAndroid && !listenerStateObserver) {
|
||||
listenerStateObserver = new java.lang.Runnable({
|
||||
run: () => {
|
||||
invalidateIconCaches();
|
||||
const snapshot = Array.from(listenerStateListeners);
|
||||
setTimeout(() => {
|
||||
for (const each of snapshot) {
|
||||
each();
|
||||
}
|
||||
}, 0);
|
||||
},
|
||||
});
|
||||
com.faceclaw.app.FaceclawMediaNotificationListenerService.addListenerStateObserver(listenerStateObserver);
|
||||
}
|
||||
return () => {
|
||||
listenerStateListeners.delete(listener);
|
||||
if (listenerStateListeners.size === 0 && listenerStateObserver) {
|
||||
com.faceclaw.app.FaceclawMediaNotificationListenerService.removeListenerStateObserver(listenerStateObserver);
|
||||
listenerStateObserver = null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user