Turn on screen when Android notifications are received

This commit is contained in:
James Babcock committed 2026-06-01 00:40:55 -07:00
1 parent 468291aac3
commit 5a92637537
7 files changed
+205 -12

No files matched your search

@@ -335,8 +335,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
}
@Override
public void run() {
@Override public void run() {
logLine(String.format(Locale.US, "communicator start R=%s L=%s ring=%s", rightAddress, leftAddress, ringAddress));
while (true) {
try {
@@ -367,8 +366,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
logLine("communicator stop");
}
@Override
public void onNotification(String address, String characteristicUuid, byte[] data) {
@Override public void onNotification(String address, String characteristicUuid, byte[] data) {
if (address == null || characteristicUuid == null || data == null) {
return;
}
@@ -432,8 +430,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
}
}
@Override
public void onConnectionStateChange(String address, boolean connected) {
@Override public void onConnectionStateChange(String address, boolean connected) {
synchronized (lock) {
if (address == null) {
return;
@@ -464,7 +461,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
}
}
private void connectLoopOnce() throws InterruptedException { //{{{
private void connectLoopOnce() throws InterruptedException {
setStateDisplay("connecting", "Connecting to the glasses...");
try {
connectArm(rightAddress, true);
@@ -499,7 +496,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
logLine("connect failed: " + safeMessage(t));
handleTransportFailure("connect failed");
}
} //}}}
}
private boolean sleepDuringConnectSettling(long delayMs) throws InterruptedException {
long deadline = SystemClock.elapsedRealtime() + delayMs;
@@ -544,7 +541,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
}
}
private void sendPrelude() throws InterruptedException { //{{{
private void sendPrelude() throws InterruptedException {
synchronized (lock) {
clearAllMessagesLocked("prelude");
}
@@ -577,7 +574,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
throw new IllegalStateException("prelude ack timeout");
}
}
} //}}}
}
private long driveSession() {
//Log.d(TAG, "driveSession called (pendingMessages.size=" + pendingMessages.size() + " inFlightMessages.size=" + inFlightMessages.size() + ")");
@@ -870,6 +867,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
return false;
}
private void resolveAckLocked(int sid, int magic, byte[] pb) {
Iterator<OutboundMessage> iterator = inFlightMessages.iterator();
while (iterator.hasNext()) {
@@ -9,6 +9,8 @@ import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
@@ -18,6 +20,7 @@ import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import org.json.JSONArray;
import org.json.JSONException;
@@ -29,6 +32,9 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen
private static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
private static volatile FaceclawMediaNotificationListenerService activeService;
private static final Handler mainHandler = new Handler(Looper.getMainLooper());
private static final Set<FaceclawNotificationListener> notificationListeners = new CopyOnWriteArraySet<>();
private static final Set<String> activeNotificationWakeKeys = new HashSet<>();
@Override
public void onCreate() {
@@ -48,6 +54,7 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen
public void onListenerConnected() {
activeService = this;
super.onListenerConnected();
refreshActiveNotificationWakeKeys(this);
}
@Override
@@ -58,6 +65,36 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen
super.onListenerDisconnected();
}
@Override
public void onNotificationPosted(StatusBarNotification statusBarNotification) {
super.onNotificationPosted(statusBarNotification);
if (!shouldShowNotificationInList(this, statusBarNotification)) {
forgetActiveNotificationWakeKey(statusBarNotification);
return;
}
if (shouldEmitNotificationPosted(statusBarNotification)) {
emitNotificationPosted(statusBarNotification.getKey());
}
}
@Override
public void onNotificationRemoved(StatusBarNotification statusBarNotification) {
forgetActiveNotificationWakeKey(statusBarNotification);
super.onNotificationRemoved(statusBarNotification);
}
public static void addNotificationListener(FaceclawNotificationListener listener) {
if (listener != null) {
notificationListeners.add(listener);
}
}
public static void removeNotificationListener(FaceclawNotificationListener listener) {
if (listener != null) {
notificationListeners.remove(listener);
}
}
public static boolean hasActiveNotificationTitle(String expectedTitle) {
FaceclawMediaNotificationListenerService service = activeService;
if (service == null || expectedTitle == null || expectedTitle.isEmpty()) {
@@ -231,6 +268,81 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen
}
}
private static void emitNotificationPosted(String key) {
if (key == null || key.isEmpty() || notificationListeners.isEmpty()) {
return;
}
for (FaceclawNotificationListener listener : notificationListeners) {
mainHandler.post(() -> {
try {
listener.onNotificationPosted(key);
} catch (Throwable t) {
Log.w(TAG, "notification listener failed", t);
}
});
}
}
private static void refreshActiveNotificationWakeKeys(FaceclawMediaNotificationListenerService service) {
StatusBarNotification[] notifications;
try {
notifications = service.getActiveNotifications();
} catch (Throwable t) {
Log.w(TAG, "failed to refresh active notification wake keys", t);
return;
}
synchronized (activeNotificationWakeKeys) {
activeNotificationWakeKeys.clear();
if (notifications == null) {
return;
}
for (StatusBarNotification statusBarNotification : notifications) {
if (shouldShowNotificationInList(service, statusBarNotification)) {
String key = statusBarNotification.getKey();
if (key != null && !key.isEmpty()) {
activeNotificationWakeKeys.add(key);
}
}
}
}
}
private static boolean shouldEmitNotificationPosted(StatusBarNotification statusBarNotification) {
String key = statusBarNotification.getKey();
if (key == null || key.isEmpty()) {
return true;
}
boolean alreadyActive;
synchronized (activeNotificationWakeKeys) {
alreadyActive = activeNotificationWakeKeys.contains(key);
activeNotificationWakeKeys.add(key);
}
return !alreadyActive || !isPersistentNotification(statusBarNotification);
}
private static void forgetActiveNotificationWakeKey(StatusBarNotification statusBarNotification) {
if (statusBarNotification == null) {
return;
}
String key = statusBarNotification.getKey();
if (key == null || key.isEmpty()) {
return;
}
synchronized (activeNotificationWakeKeys) {
activeNotificationWakeKeys.remove(key);
}
}
private static boolean isPersistentNotification(StatusBarNotification statusBarNotification) {
Notification notification = statusBarNotification.getNotification();
if (notification == null) {
return false;
}
int persistentFlags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
return (notification.flags & persistentFlags) != 0;
}
private static boolean shouldShowNotificationIcon(FaceclawMediaNotificationListenerService service, StatusBarNotification statusBarNotification) {
if (!shouldShowNotificationInList(service, statusBarNotification)) {
return false;
@@ -0,0 +1,5 @@
package com.faceclaw.app;
public interface FaceclawNotificationListener {
void onNotificationPosted(String key);
}
+26
View File
@@ -6,6 +6,7 @@ import { FaceclawCommunicatorBridge, type FrameMetrics, type RawInputEvent } fro
import { startForegroundNotification, stopForegroundNotification, updateForegroundNotification } from "../native/foreground-service";
import { mediaControllerBridge } from "../native/media-controller";
import { nightscoutBridge } from "../native/nightscout";
import { onAndroidNotificationPosted } from "../native/notification-icons";
import { openEvenAppSettings, readEvenAppNotificationState } from "../native/even-app-conflict";
import { grayImageToPreviewSource, grayImageToPreviewSourceWithBitmapFactory } from "../native/gray-image-preview";
import { voiceControlBridge } from "../native/voice-control";
@@ -18,6 +19,7 @@ import {
getDashboardSystemCardName,
isDashboardVoiceControlEnabled,
noteDashboardPhoneTextInput,
openAndroidNotificationFromSleep,
openTelepromptDocument,
receiveInput,
resetDashboardSleepTimerAndWake,
@@ -137,6 +139,7 @@ class DashboardController {
private offNightscout: (() => void) | null = null;
private offVoiceStatus: (() => void) | null = null;
private offVoiceWakeWord: (() => void) | null = null;
private offAndroidNotification: (() => void) | null = null;
private lastInput = "waiting...";
private lastSys = "none yet";
private renderInProgress = false;
@@ -160,6 +163,11 @@ class DashboardController {
startDedicatedVoiceInput: (mode) => this.startDedicatedVoiceInput(mode),
stopDedicatedVoiceInput: () => this.stopDedicatedVoiceInput(),
});
this.offAndroidNotification = onAndroidNotificationPosted((notificationKey) => {
void this.handleAndroidNotificationPosted(notificationKey).catch((error) => {
this.appendLog(`notification wake failed: ${this.formatError(error)}`);
});
});
}
subscribe(listener: DashboardListener): () => void {
@@ -701,6 +709,24 @@ class DashboardController {
}
}
private async handleAndroidNotificationPosted(notificationKey: string): Promise<void> {
const changed = openAndroidNotificationFromSleep(notificationKey);
if (!changed) {
if (this.phase === "connected" && this.communicator) {
await this.requestRender("interval");
}
return;
}
this.appendLog("android notification woke dashboard");
if (this.phase === "connected" && this.communicator) {
await this.requestRender("initial");
return;
}
const image = drawDashboard();
this.updateDisplayPreviewFromImage(image);
}
private startVoiceControlIfEnabled(): void {
if (!this.communicator) return;
if (!isDashboardVoiceControlEnabled()) return;
+40
View File
@@ -8,6 +8,8 @@ const ICON_CACHE_MS = 5_000;
let cachedIcons: GrayImage[] = [];
let cachedAtMs = 0;
let notificationListenerProxy: any | null = null;
const notificationPostedListeners = new Set<(notificationKey: string) => void>();
export type AndroidNotificationAction = {
index: number;
@@ -95,6 +97,17 @@ export function dismissNotification(notificationKey: string): boolean {
);
}
export function onAndroidNotificationPosted(listener: (notificationKey: string) => void): () => void {
notificationPostedListeners.add(listener);
ensureNotificationPostedListener();
return () => {
notificationPostedListeners.delete(listener);
if (notificationPostedListeners.size === 0) {
removeNotificationPostedListener();
}
};
}
function normalizeNotification(value: any): AndroidNotification | null {
if (!value || typeof value !== "object") return null;
const key = String(value.key ?? "");
@@ -120,6 +133,33 @@ function normalizeNotification(value: any): AndroidNotification | null {
};
}
function ensureNotificationPostedListener(): void {
if (!global.isAndroid || notificationListenerProxy) return;
notificationListenerProxy = new com.faceclaw.app.FaceclawNotificationListener({
onNotificationPosted: (notificationKey: string) => {
cachedAtMs = 0;
const key = String(notificationKey);
const listeners = Array.from(notificationPostedListeners);
setTimeout(() => {
for (const listener of listeners) {
listener(key);
}
}, 0);
},
});
com.faceclaw.app.FaceclawMediaNotificationListenerService.addNotificationListener(
notificationListenerProxy,
);
}
function removeNotificationPostedListener(): void {
if (!global.isAndroid || !notificationListenerProxy) return;
com.faceclaw.app.FaceclawMediaNotificationListenerService.removeNotificationListener(
notificationListenerProxy,
);
notificationListenerProxy = null;
}
function normalizeAction(value: any): AndroidNotificationAction | null {
if (!value || typeof value !== "object") return null;
const title = String(value.title ?? "");
+13 -1
View File
@@ -50,7 +50,7 @@ import {
import { Layer, LayerActions, LayerStack, type DashboardInputEvent, type LayerContext } from "./layers";
import { drawRightValueMenuItem, drawToggleMenuItem, MenuLayer } from "./menu";
import { StopwatchLayer } from "./apps/stopwatch";
import { NotificationsListLayer } from "./notifications";
import { NotificationsListLayer, SingleNotificationLayer } from "./notifications";
import { TranscribeLayer } from "./apps/transcribe";
import { TelepromptLayer } from "./apps/teleprompt";
import { ScreenTestLayer } from "./apps/screen-test";
@@ -268,6 +268,18 @@ export function openTelepromptDocument(text?: string): void {
dashboardLayers.push(new TelepromptLayer(dashboardState.telepromptDocumentText));
}
export function openAndroidNotificationFromSleep(notificationKey: string, nowMs = Date.now()): boolean {
if (!notificationKey || dashboardState.screenOn) return false;
dashboardState.lastInputAtMs = nowMs;
dashboardState.screenOn = true;
dashboardState.tiledWakePaintPending = dashboardState.wakeMode === "tiled";
dashboardLayers.clearToBase();
dashboardLayers.push(createRootMenuLayer());
dashboardLayers.push(new SingleNotificationLayer(notificationKey));
return true;
}
export function resetDashboardSleepTimerAndWake(nowMs = Date.now()): boolean {
dashboardState.lastInputAtMs = nowMs;
if (dashboardState.screenOn) return false;
+1 -1
View File
@@ -113,7 +113,7 @@ export class NotificationsListLayer implements Layer {
}
}
class SingleNotificationLayer implements Layer {
export class SingleNotificationLayer implements Layer {
private selectedMenuIndex = 0;
constructor(private readonly notificationKey: string) {}