Repository navigation
Expand file tree
/
Copy pathnotifications.ts
More file actions
396 lines (357 loc) · 14.5 KB
/
Copy pathnotifications.ts
File metadata and controls
396 lines (357 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import { clamp } from "~/util/numeric-util";
import { formatRelativeTime } from "~/util/date-util";
import { BdfFont, getDefaultSmallFont } from "../graphics/bdffont";
import { truncateText } from "../graphics/textwrap";
import { GrayImage } from "../graphics/image";
import { wrapText } from "../graphics/textwrap";
import { GESTURE_DOUBLE_CLICK } from "./gestures";
import {
dismissNotification,
invokeNotificationAction,
readActiveNotifications,
readNotificationIconByKey,
type AndroidNotification,
type AndroidNotificationAction,
} from "../native/notification-icons";
import { isNotificationListenerEnabled } from "../native/notification-access";
import { noteStaleDataUsed, renderPassAllowsStaleData } from "../util/render-freshness";
import { type DashboardInputEvent, type Layer, type LayerContext, type PaintBelow } from "./layers";
const PAGE_X = 12;
const PAGE_Y = 12;
const LIST_TOP = 38;
const CARD_X = 20;
const ICON_SIZE = 24;
const ICON_TEXT_GAP = 8;
const CARD_TEXT_X = 10 + ICON_SIZE + ICON_TEXT_GAP;
const LINE_HEIGHT = 14;
const CARD_GAP = 6;
const MAX_NOTIFICATIONS = 50;
// Right-hand action menu of the detail view.
const DETAIL_MENU_WIDTH = 148;
const DETAIL_CONTENT_X = 24;
/** Icon for a paint pass: allow-stale, reporting staleness to the render loop. */
function iconForNotification(key: string): GrayImage | null {
const { icon, stale } = readNotificationIconByKey(key, renderPassAllowsStaleData());
if (stale) {
noteStaleDataUsed();
}
return icon;
}
type CardLayout = {
notification: AndroidNotification;
height: number;
lines: string[];
};
type DetailMenuItem =
| { kind: "back"; label: string }
| { kind: "action"; label: string; action: AndroidNotificationAction }
| { kind: "dismiss"; label: string };
export type SingleNotificationLayerOrigin = "notifications-list" | "new-notification-modal";
type SingleNotificationLayerOptions = {
origin: SingleNotificationLayerOrigin;
/** Close hook for the modal origin (the layer is the modal stack's base, so pop() cannot close it). */
closeModal?: (ctx: LayerContext) => void;
};
/**
* Scrollable list of active Android notifications. Sized to its hosting
* stack (the Notifications app viewport). Selecting one pushes the detail
* view.
*/
export class NotificationsListLayer implements Layer {
private selectedKey = "";
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const cardWidth = width - 2 * CARD_X;
const cardTextWidth = cardWidth - CARD_TEXT_X - 14;
const notifications = readActiveNotifications(MAX_NOTIFICATIONS);
const selectedIndex = this.resolveSelectedIndex(notifications);
const layouts = notifications.map((notification, index) =>
buildNotificationCardLayout(font, notification, index === selectedIndex, cardTextWidth),
);
image.drawText(font, PAGE_X + 12, PAGE_Y + 9, "Notifications", 220);
image.drawText(font, width - 96, PAGE_Y + 9, `${selectedIndex + 1}/${notifications.length}`, 150);
if (!notifications.length) {
const message = isNotificationListenerEnabled()
? "No current Android notifications."
: "Grant permission on your phone to view notifications on the glasses.";
const messageLines = wrapText(font, message, width - 48);
for (let index = 0; index < messageLines.length; index++) {
image.drawText(font, 24, 72 + index * LINE_HEIGHT, messageLines[index]!, 190);
}
image.drawText(font, 24, height - 36, `${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
const focused = ctx.stack.isFocused();
const listBottom = height;
const scrollY = scrollForSelected(layouts, selectedIndex, listBottom - LIST_TOP);
let cursorY = LIST_TOP - scrollY;
for (let index = 0; index < layouts.length; index++) {
const layout = layouts[index]!;
if (cursorY + layout.height >= LIST_TOP && cursorY <= listBottom) {
// 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);
}
cursorY += layout.height + CARD_GAP;
if (cursorY > listBottom + 80) break;
}
return image;
}
handleInput(event: DashboardInputEvent, ctx: LayerContext): void {
const notifications = readActiveNotifications(MAX_NOTIFICATIONS);
const selectedIndex = this.resolveSelectedIndex(notifications);
if (event.type === "double-click") {
// At the app's root this is intercepted by the yield wrapper; reached
// only if hosted somewhere deeper, where popping is right.
ctx.stack.pop();
return;
}
if (!notifications.length) return;
if (event.type === "scroll-up") {
this.selectedKey = notifications[Math.max(0, selectedIndex - 1)]!.key;
return;
}
if (event.type === "scroll-down") {
this.selectedKey = notifications[Math.min(notifications.length - 1, selectedIndex + 1)]!.key;
return;
}
if (event.type === "click") {
ctx.stack.push(new SingleNotificationLayer(notifications[selectedIndex]!.key, {
origin: "notifications-list",
}));
}
}
private resolveSelectedIndex(notifications: AndroidNotification[]): number {
if (!notifications.length) {
this.selectedKey = "";
return -1;
}
let index = notifications.findIndex((notification) => notification.key === this.selectedKey);
if (index < 0) {
index = 0;
this.selectedKey = notifications[0]!.key;
}
return index;
}
}
/**
* One notification's full content plus its action menu. Sized to its hosting
* stack: the Notifications app viewport, or the interior of the shell's
* new-notification modal.
*/
export class SingleNotificationLayer implements Layer {
private selectedMenuIndex = 0;
constructor(
private readonly notificationKey: string,
private readonly options: SingleNotificationLayerOptions,
) {}
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const notification = readActiveNotifications(MAX_NOTIFICATIONS).find((item) => item.key === this.notificationKey);
if (!notification) {
return this.closeUnavailableNotification(ctx, paintBelow);
}
const menu = buildDetailMenu(notification);
this.selectedMenuIndex = clamp(this.selectedMenuIndex, 0, Math.max(0, menu.length - 1));
drawDetailContent(image, font, notification, iconForNotification(notification.key), width, height);
drawDetailMenu(image, font, menu, this.selectedMenuIndex, width);
return image;
}
handleInput(event: DashboardInputEvent, ctx: LayerContext): void {
const notification = readActiveNotifications(MAX_NOTIFICATIONS).find((item) => item.key === this.notificationKey);
if (!notification) {
this.closeUnavailableNotification(ctx);
return;
}
const menu = buildDetailMenu(notification);
if (event.type === "double-click") {
this.close(ctx);
return;
}
if (event.type === "scroll-up") {
this.selectedMenuIndex = Math.max(0, this.selectedMenuIndex - 1);
return;
}
if (event.type === "scroll-down") {
this.selectedMenuIndex = Math.min(menu.length - 1, this.selectedMenuIndex + 1);
return;
}
if (event.type !== "click") return;
const item = menu[this.selectedMenuIndex]!;
if (item.kind === "back") {
this.close(ctx);
} else if (item.kind === "action") {
invokeNotificationAction(this.notificationKey, item.action.index);
if (!readActiveNotifications(MAX_NOTIFICATIONS).some((item) => item.key === this.notificationKey)) {
this.closeUnavailableNotification(ctx);
}
} else if (item.kind === "dismiss") {
dismissNotification(this.notificationKey);
this.closeUnavailableNotification(ctx);
}
}
/** Leave the detail view, whatever hosts it. */
private close(ctx: LayerContext): void {
if (this.options.origin === "new-notification-modal") {
this.options.closeModal?.(ctx);
} else {
ctx.stack.pop();
}
}
private closeUnavailableNotification(ctx: LayerContext, paintBelow?: PaintBelow): GrayImage {
this.close(ctx);
const { width, height } = ctx.stack.getBaseSize();
return paintBelow ? paintBelow() : new GrayImage(width, height, 0);
}
}
function buildNotificationCardLayout(
font: BdfFont,
notification: AndroidNotification,
selected: boolean,
cardTextWidth: number,
): CardLayout {
const lines: string[] = [];
const time = formatRelativeTime(notification.postTime);
const title = notificationTitle(notification);
const timeSuffix = time ? ` ${time}` : "";
lines.push(truncateText(font, `${title}${timeSuffix}`, cardTextWidth));
const appName = notification.appName || notification.packageName;
const body = primaryNotificationBody(notification);
if (selected) {
if (appName && appName !== title) {
lines.push(truncateText(font, appName, cardTextWidth));
}
if (body) {
lines.push(...wrapText(font, body, cardTextWidth).slice(0, 4));
}
} else if (body) {
lines.push(truncateText(font, wrapText(font, body, cardTextWidth)[0]!, cardTextWidth));
}
if (notification.actions.length) {
lines.push(`${notification.actions.length} quick action${notification.actions.length === 1 ? "" : "s"}`);
}
return {
notification,
lines,
height: Math.max(42, 12 + lines.length * LINE_HEIGHT),
};
}
function drawNotificationCard(
image: GrayImage,
font: BdfFont,
layout: CardLayout,
x: number,
y: number,
width: number,
selected: boolean,
focused: boolean,
icon: GrayImage | null,
): void {
// Match the shared menu highlight: faint border + black fill when
// unselected; bright border when selected; the non-black fill only appears
// when the app also has focus (selected-but-unfocused stays black-filled).
const fill = selected && focused ? 15 : 1;
const stroke = selected ? 110 : 38;
image.fillRoundedRect(x, y, width, layout.height, fill, 8);
image.drawRoundedRect(x, y, width, layout.height, stroke, 8);
if (icon) {
image.bitBlt(icon, x + 10, y + 8, { transparentZero: true });
}
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 * LINE_HEIGHT, layout.lines[index]!, value);
}
}
function scrollForSelected(layouts: CardLayout[], selectedIndex: number, viewportHeight: number): number {
if (selectedIndex < 0) return 0;
let selectedTop = 0;
for (let index = 0; index < selectedIndex; index++) {
selectedTop += layouts[index]!.height + CARD_GAP;
}
const selectedBottom = selectedTop + layouts[selectedIndex]!.height;
const contentHeight = layouts.reduce((sum, layout) => sum + layout.height + CARD_GAP, 0);
const maxScroll = Math.max(0, contentHeight - viewportHeight);
const centered = selectedTop - Math.max(0, (viewportHeight - (selectedBottom - selectedTop)) / 2);
return clamp(centered | 0, 0, maxScroll);
}
function drawDetailContent(
image: GrayImage,
font: BdfFont,
notification: AndroidNotification,
icon: GrayImage | null,
width: number,
height: number,
): void {
const contentX = DETAIL_CONTENT_X;
const menuX = width - DETAIL_MENU_WIDTH - 24;
const contentWidth = menuX - contentX - 20;
image.drawText(font, PAGE_X + 12, PAGE_Y + 9, "Notification", 220);
let appLineX = contentX;
if (icon) {
image.bitBlt(icon, contentX, 36, { transparentZero: true });
appLineX = contentX + ICON_SIZE + ICON_TEXT_GAP;
}
image.drawText(font, appLineX, 42, `${notification.appName || notification.packageName} ${formatRelativeTime(notification.postTime)}`, 150);
const lines: string[] = [];
lines.push(...wrapText(font, notification.title || "(untitled)", contentWidth));
const body = detailNotificationBody(notification);
if (body) {
lines.push("");
lines.push(...wrapText(font, body, contentWidth));
}
const meta = [notification.subText, notification.infoText, notification.summaryText].filter(Boolean).join(" ");
if (meta) {
lines.push("");
lines.push(...wrapText(font, meta, contentWidth));
}
const maxLines = Math.max(1, ((height - 64 - 14) / LINE_HEIGHT) | 0);
for (let index = 0; index < Math.min(lines.length, maxLines); index++) {
const line = lines[index]!;
image.drawText(font, contentX, 64 + index * LINE_HEIGHT, line, index === 0 ? 230 : 190);
}
if (lines.length > maxLines) {
image.drawText(font, contentX, height - 24, "...", 140);
}
}
function drawDetailMenu(image: GrayImage, font: BdfFont, menu: DetailMenuItem[], selectedIndex: number, width: number): void {
const menuX = width - DETAIL_MENU_WIDTH - 24;
const menuY = 24;
for (let index = 0; index < menu.length; index++) {
const y = menuY + index * 22;
const selected = index === selectedIndex;
if (selected) {
image.fillRoundedRect(menuX - 8, y - 2, DETAIL_MENU_WIDTH, 19, 18, 6);
image.drawRoundedRect(menuX - 8, y - 2, DETAIL_MENU_WIDTH, 19, 60, 6);
}
const label = truncateText(font, menu[index]!.label, DETAIL_MENU_WIDTH - 12);
image.drawText(font, menuX, y + 2, label, selected ? 255 : 185);
}
}
function buildDetailMenu(notification: AndroidNotification): DetailMenuItem[] {
return [
{ kind: "back", label: "Back" },
...notification.actions.map((action): DetailMenuItem => ({
kind: "action",
label: action.enabled ? action.title : `${action.title} (unavailable)`,
action,
})),
{ kind: "dismiss", label: "Dismiss" },
];
}
function primaryNotificationBody(notification: AndroidNotification): string {
const title = notificationTitle(notification);
const body = notification.bigText || notification.text || notification.lines.join(" / ") || notification.summaryText || "";
return body === title ? "" : body;
}
function detailNotificationBody(notification: AndroidNotification): string {
const lines = notification.lines.length ? notification.lines.join("\n") : "";
return [notification.bigText || notification.text, lines].filter(Boolean).join("\n");
}
function notificationTitle(notification: AndroidNotification): string {
return notification.title || notification.text || notification.summaryText || notification.appName || notification.packageName || "(untitled)";
}