forked from Blendman974/faceclaw
Add support for embedded icons and custom layouts in Android notifications
The practical upshot of this is that gmail, messages and similar apps show the sender's profile picture on the glasses, the Dexcom app shows a
This commit is contained in:
1 parent
032fc307e9
commit
032797f0f6
10 files changed
+633
-93
No files matched your search
@@ -244,8 +244,8 @@ function formatEvent(event: CalendarEvent): string {
|
||||
return `- ${event.title || "(untitled)"} — ${when}${location}`;
|
||||
}
|
||||
|
||||
function formatNotification(notification: { key: string; appName: string; title: string; text: string }): string {
|
||||
const title = notification.title || "(no title)";
|
||||
function formatNotification(notification: { key: string; appName: string; title: string; text: string; customText?: string[] }): string {
|
||||
const title = notification.title || notification.customText?.join(" ") || "(no title)";
|
||||
const body = notification.text ? `: ${notification.text}` : "";
|
||||
return `- [${notification.appName}] ${title}${body} (key: ${notification.key})`;
|
||||
}
|
||||
|
||||
@@ -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, NotificationPictureResult } from './notification-icons'
|
||||
export type { NotificationIconsResult, NotificationIconResult, NotificationImageResult } from './notification-icons'
|
||||
|
||||
export const ALL_NOTIFICATIONS = 0x7fffffff
|
||||
let active: AncsClient | null = null
|
||||
@@ -61,7 +61,13 @@ 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.
|
||||
// ANCS carries no attachments, avatars or custom layouts.
|
||||
export function readNotificationPicture(_notification: AndroidNotification, _maxWidth: number, _maxHeight: number, _allowStale: boolean) {
|
||||
return {picture: null,stale:false}
|
||||
return {image: null,stale:false}
|
||||
}
|
||||
export function readNotificationLargeIcon(_notification: AndroidNotification, _size: number, _allowStale: boolean) {
|
||||
return {image: null,stale:false}
|
||||
}
|
||||
export function readNotificationCustomView(_notification: AndroidNotification, _maxWidth: number, _maxHeight: number, _allowStale: boolean) {
|
||||
return {image: null,stale:false}
|
||||
}
|
||||
@@ -23,11 +23,6 @@ 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>();
|
||||
@@ -159,48 +154,86 @@ export function readNotificationIconByKey(key: string, allowStale: boolean): Not
|
||||
return { icon: icon?.clone() ?? null, stale: false };
|
||||
}
|
||||
|
||||
export type NotificationPictureResult = {
|
||||
export type NotificationImageResult = {
|
||||
/** Shared with the cache: blit it, don't draw on it. */
|
||||
picture: GrayImage | null;
|
||||
image: GrayImage | null;
|
||||
/** True when allowStale served an earlier fetch (or nothing) in place of a fetch for these arguments. */
|
||||
stale: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* Images belonging to one notification (its picture, large icon or custom
|
||||
* layout), fetched to fit a box, under the allow-stale contract of
|
||||
* readNotificationIconByKey. Cached per post of the notification: an ongoing
|
||||
* notification updated in place (Dexcom redraws its graph and reading 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.
|
||||
*/
|
||||
class NotificationImageCache {
|
||||
private readonly entries = new Map<string, {
|
||||
postTime: number; maxWidth: number; maxHeight: number; image: GrayImage | null; atMs: number;
|
||||
}>();
|
||||
|
||||
constructor(private readonly maxEntries: number, private readonly span: string) {}
|
||||
|
||||
read(
|
||||
notification: AndroidNotification,
|
||||
maxWidth: number,
|
||||
maxHeight: number,
|
||||
allowStale: boolean,
|
||||
fetch: (width: number, height: number) => GrayImage | null,
|
||||
): NotificationImageResult {
|
||||
const width = Math.round(maxWidth);
|
||||
const height = Math.round(maxHeight);
|
||||
if (!global.isAndroid || width <= 0 || height <= 0) return { image: null, stale: false };
|
||||
|
||||
const now = Date.now();
|
||||
const cached = this.entries.get(notification.key);
|
||||
if (
|
||||
cached && cached.postTime === notification.postTime && cached.maxWidth === width
|
||||
&& cached.maxHeight === height && now - cached.atMs < ICON_CACHE_MS
|
||||
) {
|
||||
return { image: cached.image, stale: false };
|
||||
}
|
||||
if (allowStale) {
|
||||
const fits = cached?.image && cached.image.width <= width && cached.image.height <= height;
|
||||
return { image: fits ? cached!.image : null, stale: true };
|
||||
}
|
||||
|
||||
const image = spanCurrent(this.span, () => fetch(width, height));
|
||||
this.entries.delete(notification.key);
|
||||
this.entries.set(notification.key, { postTime: notification.postTime, maxWidth: width, maxHeight: height, image, atMs: now });
|
||||
while (this.entries.size > this.maxEntries) {
|
||||
const oldestKey = this.entries.keys().next().value;
|
||||
if (oldestKey === undefined) break;
|
||||
this.entries.delete(oldestKey);
|
||||
}
|
||||
return { image, stale: false };
|
||||
}
|
||||
}
|
||||
|
||||
/** Pictures and custom layouts are the size of a detail view, not icons: keep just the recently viewed few. */
|
||||
const pictureCache = new NotificationImageCache(8, "fetch-notification-picture");
|
||||
const customViewCache = new NotificationImageCache(8, "fetch-notification-custom-view");
|
||||
/** Large icons show on every list card drawn, so keep a screenful and then some. */
|
||||
const largeIconCache = new NotificationImageCache(32, "fetch-notification-large-icon");
|
||||
/** Avatars and logos: photographic tone, but undithered, which at icon size reads as noise. */
|
||||
const LARGE_ICON_TONE = { gamma: PHOTO_TONE.gamma, dither: false };
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* light background (GrayPacket.hasLightBackground); see NotificationImageCache.
|
||||
*/
|
||||
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", () =>
|
||||
): NotificationImageResult {
|
||||
if (!notification.hasPicture) return { image: null, stale: false };
|
||||
return pictureCache.read(notification, maxWidth, maxHeight, allowStale, (width, height) =>
|
||||
grayImageFromPacket(
|
||||
com.faceclaw.app.FaceclawMediaNotificationListenerService.getNotificationPictureGray(
|
||||
notification.key,
|
||||
@@ -212,16 +245,56 @@ export function readNotificationPicture(
|
||||
// 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 };
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* A notification's large icon (hasLargeIcon: a sender's avatar, a site's
|
||||
* logo), scaled to fit size x size, a light background inverted as for
|
||||
* pictures; see NotificationImageCache.
|
||||
*/
|
||||
export function readNotificationLargeIcon(
|
||||
notification: AndroidNotification,
|
||||
size: number,
|
||||
allowStale: boolean,
|
||||
): NotificationImageResult {
|
||||
if (!notification.hasLargeIcon) return { image: null, stale: false };
|
||||
return largeIconCache.read(notification, size, size, allowStale, (width) =>
|
||||
grayImageFromPacket(
|
||||
com.faceclaw.app.FaceclawMediaNotificationListenerService.getNotificationLargeIconGray(
|
||||
notification.key,
|
||||
width,
|
||||
LARGE_ICON_TONE.gamma,
|
||||
LARGE_ICON_TONE.dither,
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* A notification's own layout (hasCustomView) drawn as the phone's shade
|
||||
* draws it, at most a pixel per dp and trimmed to what it draws, so usually
|
||||
* smaller than maxWidth x maxHeight; dark-on-light layouts come out
|
||||
* light-on-dark. See NotificationCustomViews.renderGray and
|
||||
* NotificationImageCache.
|
||||
*/
|
||||
export function readNotificationCustomView(
|
||||
notification: AndroidNotification,
|
||||
maxWidth: number,
|
||||
maxHeight: number,
|
||||
allowStale: boolean,
|
||||
): NotificationImageResult {
|
||||
if (!notification.hasCustomView) return { image: null, stale: false };
|
||||
return customViewCache.read(notification, maxWidth, maxHeight, allowStale, (width, height) =>
|
||||
grayImageFromPacket(
|
||||
// Flat UI tone: it is mostly text and icons.
|
||||
com.faceclaw.app.FaceclawMediaNotificationListenerService.getNotificationCustomViewGray(
|
||||
notification.key,
|
||||
width,
|
||||
height,
|
||||
1,
|
||||
false,
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
export function readActiveNotifications(maxNotifications = 50): AndroidNotification[] {
|
||||
@@ -354,6 +427,9 @@ function normalizeNotification(value: any): AndroidNotification | null {
|
||||
actions,
|
||||
foregroundService: Boolean(value.foregroundService),
|
||||
hasPicture: Boolean(value.hasPicture),
|
||||
hasLargeIcon: Boolean(value.hasLargeIcon),
|
||||
hasCustomView: Boolean(value.hasCustomView),
|
||||
customText: Array.isArray(value.customText) ? value.customText.map((text: unknown) => String(text)).filter(Boolean) : [],
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,15 @@ export type AndroidNotification = {
|
||||
foregroundService?: boolean;
|
||||
/** Carries a picture (Android BigPictureStyle), fetched separately by readNotificationPicture. */
|
||||
hasPicture?: boolean;
|
||||
/** Carries a large icon (Android: a sender's avatar or logo), fetched separately by readNotificationLargeIcon. */
|
||||
hasLargeIcon?: boolean;
|
||||
/** Has its own layout (Android RemoteViews), drawn by readNotificationCustomView. */
|
||||
hasCustomView?: boolean;
|
||||
/**
|
||||
* The custom layout's text, one entry per text view, when the notification has no title or
|
||||
* text of its own (Dexcom's glucose reading and units).
|
||||
*/
|
||||
customText?: string[];
|
||||
/** Full message size reported by iOS, even when the fetched text is bounded. */
|
||||
messageSize?: number;
|
||||
};
|
||||
+116
-41
@@ -9,10 +9,13 @@ import {
|
||||
dismissNotification,
|
||||
invokeNotificationAction,
|
||||
readActiveNotifications,
|
||||
readNotificationCustomView,
|
||||
readNotificationIconByKey,
|
||||
readNotificationLargeIcon,
|
||||
readNotificationPicture,
|
||||
type AndroidNotification,
|
||||
type AndroidNotificationAction,
|
||||
type NotificationImageResult,
|
||||
} from "../native/notification-icons";
|
||||
import { setNotificationSourceEnabled } from "../native/notification-sources";
|
||||
import { notificationEmptyMessage } from "../native/notification-access";
|
||||
@@ -45,13 +48,18 @@ 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. */
|
||||
/** Untitled text lines that attached images (custom layout, picture) leave room for above them, when there are that many; they take 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;
|
||||
/** Likewise a custom layout; Dexcom's reading is about 40 high. */
|
||||
const DETAIL_CUSTOM_VIEW_MIN_HEIGHT = 16;
|
||||
/** A large icon (sender's avatar, site's logo): at the right of a list card, left of the detail view's title. */
|
||||
const LARGE_ICON_SIZE = 32;
|
||||
const LARGE_ICON_GAP = 8;
|
||||
// Disable-source confirmation: actions on top, the explanation below them.
|
||||
const CONFIRM_X = 24;
|
||||
const CONFIRM_Y = 8;
|
||||
@@ -67,14 +75,27 @@ 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) {
|
||||
/** A notification image read for a paint pass (allow-stale like iconForNotification). */
|
||||
function imageForPaint(
|
||||
read: (allowStale: boolean) => NotificationImageResult,
|
||||
): NotificationImageResult {
|
||||
const result = read(renderPassAllowsStaleData());
|
||||
if (result.stale) {
|
||||
noteStaleDataUsed();
|
||||
}
|
||||
return picture;
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Attached picture for a paint pass, fitting maxWidth x maxHeight. */
|
||||
function pictureForNotification(notification: AndroidNotification, maxWidth: number, maxHeight: number): GrayImage | null {
|
||||
if (!notification.hasPicture || maxHeight < DETAIL_PICTURE_MIN_HEIGHT) return null;
|
||||
return imageForPaint((allowStale) => readNotificationPicture(notification, maxWidth, maxHeight, allowStale)).image;
|
||||
}
|
||||
|
||||
/** Large icon for a paint pass, LARGE_ICON_SIZE square at most. */
|
||||
function largeIconForNotification(notification: AndroidNotification): GrayImage | null {
|
||||
if (!notification.hasLargeIcon) return null;
|
||||
return imageForPaint((allowStale) => readNotificationLargeIcon(notification, LARGE_ICON_SIZE, allowStale)).image;
|
||||
}
|
||||
|
||||
type CardLayout = {
|
||||
@@ -152,7 +173,8 @@ export class NotificationsListLayer implements Layer {
|
||||
// Icons are only resolved for cards actually drawn, so a long list
|
||||
// does not fetch icons for everything below the fold.
|
||||
const icon = iconForNotification(layout.notification.key);
|
||||
drawNotificationCard(image, font, layout, CARD_X, cursorY, cardWidth, index === selectedIndex, focused, icon);
|
||||
const largeIcon = largeIconForNotification(layout.notification);
|
||||
drawNotificationCard(image, font, layout, CARD_X, cursorY, cardWidth, index === selectedIndex, focused, icon, largeIcon);
|
||||
}
|
||||
cursorY += layout.height + CARD_GAP;
|
||||
if (cursorY > listBottom + 80) break;
|
||||
@@ -356,8 +378,10 @@ function buildNotificationCardLayout(
|
||||
font: UiFont,
|
||||
notification: AndroidNotification,
|
||||
selected: boolean,
|
||||
cardTextWidth: number,
|
||||
fullTextWidth: number,
|
||||
): CardLayout {
|
||||
// The large icon sits at the card's right, beside all its text.
|
||||
const cardTextWidth = notification.hasLargeIcon ? fullTextWidth - LARGE_ICON_SIZE - LARGE_ICON_GAP : fullTextWidth;
|
||||
const lines: string[] = [];
|
||||
const time = formatRelativeTime(notification.postTime);
|
||||
const title = notificationTitle(notification);
|
||||
@@ -383,7 +407,7 @@ function buildNotificationCardLayout(
|
||||
return {
|
||||
notification,
|
||||
lines,
|
||||
height: Math.max(ICON_SIZE + 18, 12 + lines.length * lineStep(font)),
|
||||
height: Math.max(ICON_SIZE + 18, notification.hasLargeIcon ? LARGE_ICON_SIZE + 16 : 0, 12 + lines.length * lineStep(font)),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -397,6 +421,7 @@ function drawNotificationCard(
|
||||
selected: boolean,
|
||||
focused: boolean,
|
||||
icon: GrayImage | null,
|
||||
largeIcon: GrayImage | null,
|
||||
): void {
|
||||
// Match the shared menu highlight: faint border + black fill when
|
||||
// unselected; bright border when selected; the non-black fill only appears
|
||||
@@ -408,6 +433,9 @@ function drawNotificationCard(
|
||||
if (icon) {
|
||||
image.bitBlt(icon, x + 10, y + 8, { transparentZero: true });
|
||||
}
|
||||
if (largeIcon) {
|
||||
image.bitBlt(largeIcon, x + width - 10 - LARGE_ICON_SIZE + ((LARGE_ICON_SIZE - largeIcon.width) >> 1), y + 8);
|
||||
}
|
||||
for (let index = 0; index < layout.lines.length; index++) {
|
||||
const value = index === 0 ? 140 : selected ? 235 : 185;
|
||||
image.drawText(font, x + CARD_TEXT_X, y + 7 + index * lineStep(font), layout.lines[index]!, value);
|
||||
@@ -437,18 +465,35 @@ function drawDetailContent(
|
||||
): void {
|
||||
const contentX = NOTIFICATION_CONTENT_X;
|
||||
const contentWidth = notificationContentWidth(width);
|
||||
const bodyTop = drawNotificationHeader(image, font, icon, `${notification.appName || notification.packageName} ${formatRelativeTime(notification.postTime)}`);
|
||||
const step = lineStep(font);
|
||||
const bottom = height - DETAIL_PICTURE_BOTTOM_MARGIN;
|
||||
let top = drawNotificationHeader(image, font, icon, `${notification.appName || notification.packageName} ${formatRelativeTime(notification.postTime)}`);
|
||||
|
||||
// The title, beside the large icon (a sender's avatar, a site's logo) when
|
||||
// there is one. A picture or custom layout can be the whole notification
|
||||
// (Dexcom's has no text), so either stands in for the "(untitled)" placeholder.
|
||||
const hasImages = Boolean(notification.hasPicture || notification.hasCustomView);
|
||||
const title = notification.title || (hasImages ? "" : "(untitled)");
|
||||
const avatarSize = notification.hasLargeIcon ? LARGE_ICON_SIZE : 0;
|
||||
const titleX = avatarSize ? contentX + avatarSize + LARGE_ICON_GAP : contentX;
|
||||
const titleWidth = contentX + contentWidth - titleX;
|
||||
const titleLines = title ? wrapText(font, title, titleWidth) : [];
|
||||
let titleHeight = 0;
|
||||
if (avatarSize) {
|
||||
const avatar = largeIconForNotification(notification);
|
||||
if (avatar) image.bitBlt(avatar, contentX + ((avatarSize - avatar.width) >> 1), top + ((avatarSize - avatar.height) >> 1));
|
||||
titleHeight = avatarSize;
|
||||
}
|
||||
if (titleLines.length) {
|
||||
// A title shorter than the avatar is centered on it.
|
||||
const titleTop = top + Math.max(0, (avatarSize - titleLines.length * step) >> 1);
|
||||
const shown = drawClippedLines(image, font, titleLines, titleX, titleTop, titleWidth, ((bottom - top) / step) | 0, 230);
|
||||
titleHeight = Math.max(titleHeight, shown * step);
|
||||
}
|
||||
|
||||
const lines: string[] = [];
|
||||
// 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) {
|
||||
if (lines.length) lines.push("");
|
||||
lines.push(...wrapText(font, body, contentWidth));
|
||||
}
|
||||
const meta = [notification.subText, notification.infoText, notification.summaryText].filter(Boolean).join(" ");
|
||||
@@ -456,36 +501,65 @@ function drawDetailContent(
|
||||
if (lines.length) lines.push("");
|
||||
lines.push(...wrapText(font, meta, contentWidth));
|
||||
}
|
||||
// Text follows the title after a blank line (just a gap beside an avatar); images after a gap.
|
||||
if (titleHeight) {
|
||||
top += titleHeight + (lines.length && !avatarSize ? step : DETAIL_PICTURE_GAP);
|
||||
}
|
||||
|
||||
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));
|
||||
// The custom layout, then the picture, take what is left below the title,
|
||||
// or below the first few lines of text when there is no title.
|
||||
const reservedTextHeight = titleHeight ? 0 : Math.min(lines.length, DETAIL_PICTURE_TEXT_LINES) * step;
|
||||
const roomForImages = bottom - top - (reservedTextHeight ? reservedTextHeight + DETAIL_PICTURE_GAP : 0);
|
||||
let customView: GrayImage | null = null;
|
||||
if (notification.hasCustomView && roomForImages >= DETAIL_CUSTOM_VIEW_MIN_HEIGHT) {
|
||||
const result = imageForPaint((allowStale) => readNotificationCustomView(notification, contentWidth, roomForImages, allowStale));
|
||||
customView = result.image;
|
||||
if (!customView && !result.stale && notification.customText?.length) {
|
||||
// It couldn't be drawn: its text is better than nothing.
|
||||
lines.unshift(...wrapText(font, notification.customText.join(" "), contentWidth), ...(lines.length ? [""] : []));
|
||||
}
|
||||
image.bitBlt(picture, contentX, bodyTop + shownLines * step + (shownLines ? DETAIL_PICTURE_GAP : 0));
|
||||
return;
|
||||
}
|
||||
const picture = pictureForNotification(notification, contentWidth,
|
||||
roomForImages - (customView ? customView.height + DETAIL_PICTURE_GAP : 0));
|
||||
|
||||
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, textValue(index));
|
||||
// The text gets whatever the images leave, above them, with its last shown
|
||||
// line marked when some is cut off.
|
||||
const imagesHeight = (customView ? customView.height : 0) + (picture ? picture.height : 0)
|
||||
+ (customView && picture ? DETAIL_PICTURE_GAP : 0);
|
||||
const roomForText = bottom - top - (imagesHeight ? imagesHeight + DETAIL_PICTURE_GAP : 0);
|
||||
const shownLines = drawClippedLines(image, font, lines, contentX, top, contentWidth, (roomForText / step) | 0, 190);
|
||||
let imageTop = top + shownLines * step + (shownLines ? DETAIL_PICTURE_GAP : 0);
|
||||
if (customView) {
|
||||
image.bitBlt(customView, contentX, imageTop);
|
||||
imageTop += customView.height + DETAIL_PICTURE_GAP;
|
||||
}
|
||||
if (lines.length > maxLines) {
|
||||
image.drawText(font, contentX, height - 24, "...", 140);
|
||||
if (picture) {
|
||||
image.bitBlt(picture, contentX, imageTop);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw up to maxLines of lines from (x, y), marking the last one drawn with
|
||||
* "..." when some are left out. Returns how many were drawn.
|
||||
*/
|
||||
function drawClippedLines(
|
||||
image: GrayImage,
|
||||
font: UiFont,
|
||||
lines: string[],
|
||||
x: number,
|
||||
y: number,
|
||||
width: number,
|
||||
maxLines: number,
|
||||
value: number,
|
||||
): number {
|
||||
const shown = Math.min(lines.length, Math.max(0, maxLines));
|
||||
for (let index = 0; index < shown; index++) {
|
||||
const line = index === shown - 1 && lines.length > shown ? truncateText(font, lines[index] + "...", width) : lines[index]!;
|
||||
image.drawText(font, x, y + index * lineStep(font), line, value);
|
||||
}
|
||||
return shown;
|
||||
}
|
||||
|
||||
/** Left edge of a notification detail view's content column. */
|
||||
export const NOTIFICATION_CONTENT_X = DETAIL_CONTENT_X;
|
||||
|
||||
@@ -577,5 +651,6 @@ function detailNotificationBody(notification: AndroidNotification): string {
|
||||
}
|
||||
|
||||
function notificationTitle(notification: AndroidNotification): string {
|
||||
return notification.title || notification.text || notification.summaryText || notification.appName || notification.packageName || "(untitled)";
|
||||
return notification.title || notification.text || notification.customText?.join(" ") || notification.summaryText
|
||||
|| notification.appName || notification.packageName || "(untitled)";
|
||||
}
|
||||
Reference in new issue
Block a user