Show images attached to Android notifications

This commit is contained in:
James Babcock committed 2026-10-23 22:26:36 -07:00
1 parent bce2307fdb
commit 40082b3f5b
9 files changed
+332 -13

No files matched your search

+5 -1
View File
@@ -4,7 +4,7 @@ import { AncsClient, ANCS_CONNECT_MESSAGE, type AncsState } from '../g2/ancs-cli
import { rememberNotificationSources } from './notification-sources'
import type { AndroidNotification } from './notification-types'
export type { AndroidNotification, AndroidNotificationAction } from './notification-types'
export type { NotificationIconsResult, NotificationIconResult } from './notification-icons'
export type { NotificationIconsResult, NotificationIconResult, NotificationPictureResult } from './notification-icons'
export const ALL_NOTIFICATIONS = 0x7fffffff
let active: AncsClient | null = null
@@ -61,3 +61,7 @@ export function readNotificationIconByKey(key: string, _allowStale: boolean) {
const notification = readActiveNotifications(ALL_NOTIFICATIONS).find(n => n.key === key)
return {icon: notification ? categoryIcon(notification.category) : null,stale:false}
}
// ANCS carries no attachments.
export function readNotificationPicture(_notification: AndroidNotification, _maxWidth: number, _maxHeight: number, _allowStale: boolean) {
return {picture: null,stale:false}
}
+72
View File
@@ -2,6 +2,7 @@ import { GrayImage } from "../graphics/image";
import { logCurrent, spanCurrent } from "./frame-timings";
import { toUint8Array } from "../util/array-util";
import { rememberNotificationSources } from "./notification-sources";
import { PHOTO_TONE, grayImageFromPacket } from "./image-files";
declare const com: any;
declare const java: any;
@@ -22,6 +23,11 @@ let cachedKeys: string[] = [];
let cachedAtMs = 0;
const keyedIconCache = new Map<string, { icon: GrayImage | null; atMs: number }>();
const KEYED_ICON_CACHE_MAX = 128;
/** Pictures are the size of a detail view, not 24px icons: keep just the recently viewed few. */
const pictureCache = new Map<string, {
postTime: number; maxWidth: number; maxHeight: number; picture: GrayImage | null; atMs: number;
}>();
const PICTURE_CACHE_MAX = 8;
let notificationListenerProxy: any = null;
const notificationPostedListeners = new Set<(notificationKey: string) => void>();
const notificationRemovedListeners = new Set<(notificationKey: string) => void>();
@@ -151,6 +157,71 @@ export function readNotificationIconByKey(key: string, allowStale: boolean): Not
return { icon: icon?.clone() ?? null, stale: false };
}
export type NotificationPictureResult = {
/** Shared with the cache: blit it, don't draw on it. */
picture: GrayImage | null;
/** True when allowStale served an earlier fetch (or nothing) in place of a fetch for these arguments. */
stale: boolean;
};
/**
* The picture attached to a notification (hasPicture), scaled to fit
* maxWidth x maxHeight with the photo tone and inverted if it is drawn on a
* light background (GrayPacket.hasLightBackground), under the allow-stale contract of
* readNotificationIconByKey. Cached per post of the notification: an ongoing
* notification updated in place (Dexcom redraws its graph every few minutes)
* fires no posted event, but its postTime moves. ICON_CACHE_MS is the
* backstop here too. Under allowStale an earlier fetch stands in, if it fits.
*/
export function readNotificationPicture(
notification: AndroidNotification,
maxWidth: number,
maxHeight: number,
allowStale: boolean,
): NotificationPictureResult {
const width = Math.round(maxWidth);
const height = Math.round(maxHeight);
if (!global.isAndroid || !notification.hasPicture || width <= 0 || height <= 0) {
return { picture: null, stale: false };
}
const now = Date.now();
const cached = pictureCache.get(notification.key);
if (
cached && cached.postTime === notification.postTime && cached.maxWidth === width
&& cached.maxHeight === height && now - cached.atMs < ICON_CACHE_MS
) {
return { picture: cached.picture, stale: false };
}
if (allowStale) {
const fits = cached?.picture && cached.picture.width <= width && cached.picture.height <= height;
return { picture: fits ? cached!.picture : null, stale: true };
}
const picture = spanCurrent("fetch-notification-picture", () =>
grayImageFromPacket(
com.faceclaw.app.FaceclawMediaNotificationListenerService.getNotificationPictureGray(
notification.key,
width,
height,
PHOTO_TONE.gamma,
PHOTO_TONE.dither,
// A chart or screenshot made for a light theme would be a glaring
// lit panel on the glasses: show it light-on-dark.
true,
),
),
);
pictureCache.delete(notification.key);
pictureCache.set(notification.key, { postTime: notification.postTime, maxWidth: width, maxHeight: height, picture, atMs: now });
while (pictureCache.size > PICTURE_CACHE_MAX) {
const oldestKey = pictureCache.keys().next().value;
if (oldestKey === undefined) break;
pictureCache.delete(oldestKey);
}
return { picture, stale: false };
}
export function readActiveNotifications(maxNotifications = 50): AndroidNotification[] {
if (!global.isAndroid || maxNotifications <= 0) return [];
try {
@@ -250,6 +321,7 @@ function normalizeNotification(value: any): AndroidNotification | null {
when: Number(value.when) || 0,
actions,
foregroundService: Boolean(value.foregroundService),
hasPicture: Boolean(value.hasPicture),
};
}
+2
View File
@@ -7,6 +7,8 @@ export type AndroidNotification = {
actions: AndroidNotificationAction[]; dismissLabel?: string;
/** Posted by a foreground service (Android): a "this app is working" status notification. */
foregroundService?: boolean;
/** Carries a picture (Android BigPictureStyle), fetched separately by readNotificationPicture. */
hasPicture?: boolean;
/** Full message size reported by iOS, even when the fetched text is bounded. */
messageSize?: number;
};
+45 -4
View File
@@ -10,6 +10,7 @@ import {
invokeNotificationAction,
readActiveNotifications,
readNotificationIconByKey,
readNotificationPicture,
type AndroidNotification,
type AndroidNotificationAction,
} from "../native/notification-icons";
@@ -44,6 +45,13 @@ const DETAIL_TITLE_Y = 21;
/** Gaps below the title's line box and below the icon/app-name header row. */
const DETAIL_HEADER_GAP = 3;
const DETAIL_BODY_GAP = 4;
/** Text lines an attached picture leaves room for above it (when there are that many); it takes the rest. */
const DETAIL_PICTURE_TEXT_LINES = 2;
/** Gap between the text and an attached picture, and the space kept below the picture. */
const DETAIL_PICTURE_GAP = 6;
const DETAIL_PICTURE_BOTTOM_MARGIN = 12;
/** An attached picture with less height than this to fit in (a large UI font) is left out. */
const DETAIL_PICTURE_MIN_HEIGHT = 32;
// Disable-source confirmation: actions on top, the explanation below them.
const CONFIRM_X = 24;
const CONFIRM_Y = 8;
@@ -59,6 +67,16 @@ function iconForNotification(key: string): GrayImage | null {
return icon;
}
/** Attached picture for a paint pass, fitting maxWidth x maxHeight; allow-stale like iconForNotification. */
function pictureForNotification(notification: AndroidNotification, maxWidth: number, maxHeight: number): GrayImage | null {
if (!notification.hasPicture || maxHeight < DETAIL_PICTURE_MIN_HEIGHT) return null;
const { picture, stale } = readNotificationPicture(notification, maxWidth, maxHeight, renderPassAllowsStaleData());
if (stale) {
noteStaleDataUsed();
}
return picture;
}
type CardLayout = {
notification: AndroidNotification;
height: number;
@@ -422,23 +440,46 @@ function drawDetailContent(
const bodyTop = drawNotificationHeader(image, font, icon, `${notification.appName || notification.packageName} ${formatRelativeTime(notification.postTime)}`);
const lines: string[] = [];
lines.push(...wrapText(font, notification.title || "(untitled)", contentWidth));
// A picture can be the whole notification (Dexcom's has no text), so it
// stands in for the "(untitled)" placeholder.
const hasTitle = Boolean(notification.title) || !notification.hasPicture;
if (hasTitle) {
lines.push(...wrapText(font, notification.title || "(untitled)", contentWidth));
}
const body = detailNotificationBody(notification);
if (body) {
lines.push("");
if (lines.length) lines.push("");
lines.push(...wrapText(font, body, contentWidth));
}
const meta = [notification.subText, notification.infoText, notification.summaryText].filter(Boolean).join(" ");
if (meta) {
lines.push("");
if (lines.length) lines.push("");
lines.push(...wrapText(font, meta, contentWidth));
}
const step = lineStep(font);
const textValue = (index: number) => index === 0 && hasTitle ? 230 : 190;
const reservedTextHeight = Math.min(lines.length, DETAIL_PICTURE_TEXT_LINES) * step;
const picture = pictureForNotification(notification, contentWidth,
height - DETAIL_PICTURE_BOTTOM_MARGIN - bodyTop - reservedTextHeight - (reservedTextHeight ? DETAIL_PICTURE_GAP : 0));
if (picture) {
// The text gets whatever the picture leaves, above it, with its last
// shown line marked when some is cut off.
const roomForText = height - DETAIL_PICTURE_BOTTOM_MARGIN - picture.height - DETAIL_PICTURE_GAP - bodyTop;
const shownLines = Math.min(lines.length, Math.max(0, (roomForText / step) | 0));
for (let index = 0; index < shownLines; index++) {
const line = index === shownLines - 1 && lines.length > shownLines
? truncateText(font, lines[index] + "...", contentWidth) : lines[index]!;
image.drawText(font, contentX, bodyTop + index * step, line, textValue(index));
}
image.bitBlt(picture, contentX, bodyTop + shownLines * step + (shownLines ? DETAIL_PICTURE_GAP : 0));
return;
}
const maxLines = Math.max(1, ((height - bodyTop - step) / step) | 0);
for (let index = 0; index < Math.min(lines.length, maxLines); index++) {
const line = lines[index]!;
image.drawText(font, contentX, bodyTop + index * step, line, index === 0 ? 230 : 190);
image.drawText(font, contentX, bodyTop + index * step, line, textValue(index));
}
if (lines.length > maxLines) {
image.drawText(font, contentX, height - 24, "...", 140);