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
@@ -1,4 +1,4 @@
|
||||
import { isNotificationListenerEnabled, requestNotificationListenerAccess } from "../../native/notification-access";
|
||||
import { notificationListenerState, requestNotificationListenerAccess } from "../../native/notification-access";
|
||||
import { type AppDefinition } from "../app-definition";
|
||||
import { createNotificationsAppWindow, NOTIFICATIONS_SURFACE_ID, NOTIFICATIONS_WINDOW_ID } from "./notifications-app";
|
||||
|
||||
@@ -7,10 +7,11 @@ const notificationsApp: AppDefinition = {
|
||||
title: "Notifications",
|
||||
icon: "bell",
|
||||
launch: (ctx) => {
|
||||
// Without notification-listener access the tray reads as empty; prompt
|
||||
// the user on the phone so the on-glasses "grant permission" message is
|
||||
// actionable. The app still opens to show that message.
|
||||
if (!isNotificationListenerEnabled()) {
|
||||
// Without notification-listener access (or with a listener Android won't
|
||||
// rebind) the tray reads as empty; open the access screen on the phone so
|
||||
// the on-glasses message is actionable. The app still opens to show it.
|
||||
const listenerState = notificationListenerState();
|
||||
if (listenerState === "no-access" || listenerState === "stuck") {
|
||||
requestNotificationListenerAccess();
|
||||
}
|
||||
return ctx.launchInProcessApp(NOTIFICATIONS_WINDOW_ID, NOTIFICATIONS_SURFACE_ID, createNotificationsAppWindow);
|
||||
|
||||
@@ -17,6 +17,7 @@ import { nightscoutBridge } from "../native/nightscout-bridge";
|
||||
import { weatherBridge } from "../native/weather";
|
||||
import { ALL_NOTIFICATIONS, onAndroidNotificationPosted, onAndroidNotificationRemoved, readActiveNotifications } from "../native/notification-icons";
|
||||
import { shouldShowNotificationOnGlasses } from "../native/notification-sources";
|
||||
import { watchNotificationListenerBinding } from "../native/notification-access";
|
||||
import { openEvenAppSettings, readEvenAppNotificationState } from "../native/even-app-conflict";
|
||||
import { grayImageToPreviewSource } from "../native/gray-image-preview";
|
||||
import { firmwareIncompatibilityMessage, hasCompatibleFirmware } from "./firmware-compat";
|
||||
@@ -452,6 +453,7 @@ class DashboardController {
|
||||
timerEngine.onChange(() => this.refreshAlarmReliabilityWarning());
|
||||
this.offAndroidNotification = onAndroidNotificationPosted((notificationKey) => this.handleAndroidNotificationPosted(notificationKey));
|
||||
this.offAndroidNotificationRemoved = onAndroidNotificationRemoved((notificationKey) => this.handleAndroidNotificationRemoved(notificationKey));
|
||||
watchNotificationListenerBinding();
|
||||
readActiveNotifications(ALL_NOTIFICATIONS);
|
||||
// Settings toggled from the glasses can change what the phone UI shows
|
||||
// (e.g. the text-setting editor), so re-emit the snapshot on any change.
|
||||
|
||||
@@ -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