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
@@ -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