-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathnotification_window.c
More file actions
1669 lines (1415 loc) · 62.5 KB
/
notification_window.c
File metadata and controls
1669 lines (1415 loc) · 62.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-FileCopyrightText: 2024 Google LLC */
/* SPDX-License-Identifier: Apache-2.0 */
#include "notification_window.h"
#include "notifications_presented_list.h"
#include "notification_window_private.h"
#include "applib/fonts/fonts.h"
#include "applib/ui/action_button.h"
#include "applib/ui/action_menu_window.h"
#include "applib/ui/app_window_stack.h"
#include "applib/ui/dialogs/dialog.h"
#include "applib/ui/dialogs/expandable_dialog.h"
#include "applib/ui/dialogs/simple_dialog.h"
#include "applib/ui/ui.h"
#include "applib/ui/window.h"
#include "applib/ui/window_manager.h"
#include "applib/ui/window_stack.h"
#include "apps/system/timeline/peek_layer.h"
#include "kernel/event_loop.h"
#include "kernel/pbl_malloc.h"
#include "kernel/ui/modals/modal_manager.h"
#include "os/mutex.h"
#include "process_state/app_state/app_state.h"
#include "resource/resource_ids.auto.h"
#include "pbl/services/analytics/analytics.h"
#include "pbl/services/bluetooth/bluetooth_persistent_storage.h"
#include "pbl/services/comm_session/session.h"
#include "pbl/services/evented_timer.h"
#include "pbl/services/i18n/i18n.h"
#include "pbl/services/light.h"
#include "pbl/services/regular_timer.h"
#include "pbl/services/blob_db/ios_notif_pref_db.h"
#include "pbl/services/blob_db/pin_db.h"
#include "pbl/services/blob_db/reminder_db.h"
#include "pbl/services/notifications/alerts.h"
#include "pbl/services/notifications/alerts_preferences.h"
#include "pbl/services/notifications/alerts_preferences_private.h"
#include "pbl/services/notifications/alerts_private.h"
#include "pbl/services/notifications/ancs/ancs_filtering.h"
#include "pbl/services/notifications/do_not_disturb.h"
#include "pbl/services/notifications/notification_storage.h"
#include "pbl/services/notifications/notification_types.h"
#include "pbl/services/notifications/notifications.h"
#include "pbl/services/timeline/attribute.h"
#include "pbl/services/timeline/notification_layout.h"
#include "pbl/services/timeline/swap_layer.h"
#include "pbl/services/timeline/timeline.h"
#include "pbl/services/timeline/timeline_actions.h"
#include "pbl/services/timeline/timeline_resources.h"
#include "system/logging.h"
#include "system/passert.h"
#include "util/math.h"
#include "util/trig.h"
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include "pbl/services/vibes/vibe_client.h"
#include "pbl/services/vibes/vibe_score.h"
#define NOTIFICATION_PRIORITY (ModalPriorityNotification)
#define NUM_MOOOK_SOFT_MID_FRAMES PBL_IF_RECT_ELSE(6, 4)
#define FIRST_PEEK_DELAY PBL_IF_RECT_ELSE(500, 200)
// pop timer for window. Refreshed during any point of activity (button clicks)
static const unsigned int QUICK_DND_HOLD_MS = 800;
T_STATIC NotificationWindowData s_notification_window_data;
T_STATIC bool s_in_use = false;
PebbleMutex *s_notification_window_mutex;
static bool prv_should_provide_action_menu_for_item(NotificationWindowData *data,
const TimelineItem *item);
static void prv_handle_notification_removed_common(Uuid *, NotificationType);
static bool prv_should_pop_due_to_inactivity(void);
static void prv_do_notification_vibe(NotificationWindowData *data, Uuid *id);
/////////////////////
// Helpers
/////////////////////
static AlertType prv_alert_type_for_notification_type(NotificationType type) {
switch (type) {
case NotificationMobile:
return AlertMobile;
case NotificationPhoneCall:
return AlertPhoneCall;
case NotificationOther:
return AlertOther;
case NotificationReminder:
return AlertReminder;
default:
return AlertInvalid;
}
}
static void prv_toggle_dnd_from_back_click(ClickRecognizerRef recognizer, void *data) {
do_not_disturb_manual_toggle_with_dialog();
}
static void prv_toggle_dnd_from_action_menu(ActionMenu *action_menu,
const ActionMenuItem *item,
void *context) {
// This function handles first-time use tutorial logic
do_not_disturb_toggle_manually_enabled(ManualDNDFirstUseSourceActionMenu);
}
static TimelineItem *prv_get_current_notification(NotificationWindowData *data) {
if (!notifications_presented_list_current()) {
return NULL;
}
LayoutLayer *current = swap_layer_get_current_layout(&data->swap_layer);
TimelineItem *item = (TimelineItem *)layout_get_context(current);
return item;
}
static void prv_draw_dnd_icon(struct Layer *layer, GContext *ctx) {
if (!s_in_use) {
return;
}
NotificationWindowData *data = &s_notification_window_data;
if (!data->dnd_icon_visible) {
return;
}
graphics_context_set_tint_color(ctx, status_bar_layer_get_foreground_color(&data->status_layer));
graphics_context_set_compositing_mode(ctx, GCompOpTint);
graphics_draw_bitmap_in_rect(ctx, &data->dnd_icon, &data->dnd_icon.bounds);
}
static void prv_update_status_layer(NotificationWindowData *data) {
const int notif_count = notifications_presented_list_count();
if (notif_count <= 1) {
// if less than one notification, clear the status bar info
status_bar_layer_reset_info(&data->status_layer);
} else {
// if more than one, then show the current index in relation to the total number
status_bar_layer_set_info_progress(&data->status_layer,
notifications_presented_list_current_idx() + 1,
notif_count);
}
}
static void prv_cleanup_timer(EventedTimerID *timer_id) {
if (*timer_id != EVENTED_TIMER_INVALID_ID) {
evented_timer_cancel(*timer_id);
*timer_id = EVENTED_TIMER_INVALID_ID;
}
}
static void prv_cancel_reminder_watchdog(NotificationWindowData *data) {
if (regular_timer_is_scheduled(&data->reminder_watchdog_timer_id)) {
regular_timer_remove_callback(&data->reminder_watchdog_timer_id);
}
}
static void prv_cleanup_timers(NotificationWindowData *data) {
prv_cleanup_timer(&data->pop_timer_id);
prv_cancel_reminder_watchdog(data);
prv_cleanup_timer(&data->peek_layer_timer);
}
static void prv_pop_notification_window(NotificationWindowData *data) {
if (data->window_frozen || !s_in_use) {
return;
}
// This calls through to our window_unload() callback, which cancels our timer and clears s_in_use
window_stack_remove(&data->window, true /* animated */);
}
static int prv_reminders_on_top_comparator(void *a, void *b) {
NotifList *notif_a = (NotifList*) a;
NotifList *notif_b = (NotifList*) b;
NotificationType type_a = notif_a->notif.type;
NotificationType type_b = notif_b->notif.type;
// Reminders come first, then everything else. More recent reminders should appear before older
// reminders and more recent notifications should appear before older notifications
if (type_b == NotificationReminder) {
return 1;
} else if (type_b != NotificationReminder && type_a != NotificationReminder) {
return 1;
} else {
return -1;
}
}
static void prv_notification_window_add_notification(Uuid *id, NotificationType type) {
if (do_not_disturb_is_active()) {
notifications_presented_list_add_sorted(id, type, prv_reminders_on_top_comparator, false);
} else {
notifications_presented_list_add(id, type);
}
}
static void prv_reload_swap_layer(NotificationWindowData *data) {
// If the action menu is on the screen, then don't reload the swap layer.
// The action menu's context is just a pointer to the swap layer's layout layer's context.
// Reloading the swap layer will give the action menu a bogus timeline item pointer.
// Also if the action menu is up, we don't need to reload the swap layer until the
// notification window appears again
if (data->action_menu && window_is_loaded((Window *)data->action_menu)) {
data->notifications_modified = true;
} else {
swap_layer_reload_data(&data->swap_layer);
}
}
/////////////////////
// Dismiss All
/////////////////////
static void prv_handle_dismiss_all_complete(bool succeeded, void *cb_data) {
NotificationWindowData *window_data = (NotificationWindowData*) cb_data;
window_data->window_frozen = false;
if (s_in_use && succeeded) {
prv_pop_notification_window(window_data);
}
}
static void prv_dismiss_all(void *data, ActionMenu *action_menu) {
NotificationWindowData *window_data = (NotificationWindowData*) data;
const int num_notifications = notifications_presented_list_count();
if (num_notifications == 0) {
return;
}
Uuid *first_id = notifications_presented_list_first();
NotificationInfo *notif_list = kernel_malloc_check(sizeof(NotificationInfo) * num_notifications);
for (int i = 0; i < num_notifications; i++) {
Uuid *id = notifications_presented_list_relative(first_id, i);
memcpy(¬if_list[i].id, id, sizeof(Uuid));
notif_list[i].type = notifications_presented_list_get_type(id);
}
PBL_LOG_DBG("Dismissing %d notifications", num_notifications);
window_data->window_frozen = true;
timeline_actions_dismiss_all(notif_list,
num_notifications,
action_menu,
prv_handle_dismiss_all_complete,
data);
kernel_free(notif_list);
}
static void prv_dismiss_all_action_cb(ActionMenu *action_menu,
const ActionMenuItem *item,
void *context) {
NotificationWindowData *window_data = (NotificationWindowData*) item->action_data;
prv_dismiss_all(window_data, action_menu);
}
static int64_t prv_interpolate_moook_peek_animation(int32_t normalized, int64_t from, int64_t to) {
return interpolate_moook_soft(normalized, from, to, NUM_MOOOK_SOFT_MID_FRAMES);
}
// scroll = true, then scroll the layer up dy
// scroll = false, then shrink the layer's size by dy
static Animation *prv_create_anim_frame(Layer *layer, int16_t dy, bool scroll) {
GRect *start = &layer->frame;
GRect stop = *start;
if (scroll) {
stop.origin.y += dy;
} else {
stop.size.h += dy;
}
PropertyAnimation *prop_anim = property_animation_create_layer_frame(layer, start, &stop);
Animation *animation = property_animation_get_animation(prop_anim);
animation_set_duration(animation, interpolate_moook_soft_duration(NUM_MOOOK_SOFT_MID_FRAMES));
animation_set_custom_interpolation(animation, prv_interpolate_moook_peek_animation);
return animation;
}
/////////////////////
// Peek Layer
/////////////////////
static void prv_peek_anim_stopped(Animation *animation, bool finished, void *context) {
NotificationWindowData *data = context;
data->first_notif_loaded = true;
peek_layer_destroy(data->peek_layer);
data->peek_layer = NULL;
TimelineItem *item = prv_get_current_notification(data);
layer_set_hidden((Layer *)&data->action_button_layer,
!prv_should_provide_action_menu_for_item(data, item));
// Fallback: if a pending vibe/backlight wasn't triggered in prv_hide_peek_layer (e.g., a new
// notification arrived after prv_hide_peek_layer ran but before this callback),
// trigger it now
if (data->pending_vibe) {
data->pending_vibe = false;
prv_do_notification_vibe(data, &data->pending_vibe_id);
}
if (data->pending_backlight) {
data->pending_backlight = false;
if (!data->color_preempted) {
data->color_preempted = true;
light_system_color_request();
}
light_enable_interaction();
}
}
static void prv_hide_peek_layer(void *context) {
NotificationWindowData *data = context;
// Execute pending vibration and backlight if delayed vibe was requested - do it as the
// notification starts sliding up to reveal the text
if (data->pending_vibe) {
data->pending_vibe = false;
prv_do_notification_vibe(data, &data->pending_vibe_id);
}
if (data->pending_backlight) {
data->pending_backlight = false;
if (!data->color_preempted) {
data->color_preempted = true;
light_system_color_request();
}
light_enable_interaction();
}
// get the frame of the swap_layer and set its destination
const GRect *swap_frame = &data->swap_layer.layer.frame;
const int16_t status_bar_height = data->status_layer.layer.frame.size.h;
int16_t swap_frame_animation_dy = status_bar_height - swap_frame->origin.y;
// duration of animation of both peek layer and swap layer moving up to the top
int16_t peek_frame_animation_dy = swap_frame_animation_dy;
#if PBL_ROUND
// Needed because the peek layer's background and the screen have different sizes,
// so the peek layer needs to move a different number of pixels vs the swap layer
const int16_t peek_circle_vertical_offset = (BANNER_CIRCLE_RADIUS - (DISP_ROWS / 2)) / 2;
peek_frame_animation_dy -= peek_circle_vertical_offset;
#endif
Animation *peek_up = prv_create_anim_frame((Layer *)data->peek_layer, peek_frame_animation_dy,
false /* scroll */);
Animation *swap_up = prv_create_anim_frame((Layer *)&data->swap_layer, swap_frame_animation_dy,
true /* scroll */);
Animation *spawn = animation_spawn_create(peek_up, swap_up, NULL);
AnimationHandlers anim_handlers = {
.started = NULL,
.stopped = prv_peek_anim_stopped,
};
animation_set_handlers(spawn, anim_handlers, data);
// move the icon to where it should be in the swap_layer's notification_layout
PeekLayer *peek_layer = data->peek_layer;
GRect frame = peek_layer->layer.frame;
frame.origin.x = (frame.size.w / 2) - (NOTIFICATION_TINY_RESOURCE_SIZE.w / 2);
frame.origin.y = CARD_ICON_UPPER_PADDING + status_bar_height;
frame.size = NOTIFICATION_TINY_RESOURCE_SIZE;
const bool align_in_frame = true;
peek_layer_set_scale_to_image(peek_layer, &data->peek_icon_info, TimelineResourceSizeTiny, frame,
align_in_frame);
// set peek_layer clips to true so I can resize the peek_layer's background
layer_set_clips(&peek_layer->layer, true);
peek_layer_play(peek_layer);
data->peek_animation = spawn;
animation_schedule(spawn);
}
static void prv_play_peek_layer(void *context) {
NotificationWindowData *data = context;
// play the peek layer unfold sequence
peek_layer_play(data->peek_layer);
const uint16_t PEEK_LAYER_HIDE_DELAY = PBL_IF_RECT_ELSE(500, 400);
data->peek_layer_timer = evented_timer_register_or_reschedule(data->peek_layer_timer,
PEEK_LAYER_HIDE_DELAY,
prv_hide_peek_layer,
data);
}
static void prv_show_peek_for_notification(NotificationWindowData *data, Uuid *id,
bool is_first_notification) {
// reload everything, doesn't matter since it will be covered by the peek layer
notification_window_focus_notification(id, false);
// if the peek animation is already in progress, we've done all we need
// data->peek_layer is only ever not null between the start and end of the peek
// animation; it's cleaned up by prv_peek_anim_stopped, and initialized here
if (data->peek_layer) {
return;
}
// get root layer of window and make the peek layer the full size
const GRect *peek_layer_frame = &window_get_root_layer(&data->window)->frame;
data->peek_layer = peek_layer_create(*peek_layer_frame);
if (!data->peek_layer) {
if (is_first_notification) {
// we don't have enough memory, no peek. Just push the modal window.
modal_window_push(&data->window, NOTIFICATION_PRIORITY, true /* animated */);
}
return;
}
// get the current layout so we can get the color and icon
LayoutLayer *layout = swap_layer_get_current_layout(&data->swap_layer);
if (!layout) {
return;
}
// Get color and icon
const LayoutColors *colors = layout_get_notification_colors(layout);
TimelineItem *item = prv_get_current_notification(data);
TimelineResourceId timeline_res_id;
const TimelineResourceId fallback_icon_id = notification_layout_get_fallback_icon_id(
item->header.type);
timeline_res_id = attribute_get_uint32(&item->attr_list, AttributeIdIconTiny,
fallback_icon_id);
data->peek_icon_info = (TimelineResourceInfo) {
.res_id = timeline_res_id,
.app_id = &data->notification_app_id, // This is set earlier when we reload the layout
.fallback_id = fallback_icon_id,
};
#if PBL_BW
const bool use_alternative_design = alerts_preferences_get_notification_alternative_design();
peek_layer_set_icon_with_invert(data->peek_layer, &data->peek_icon_info, use_alternative_design);
#else
peek_layer_set_icon(data->peek_layer, &data->peek_icon_info);
#endif
peek_layer_set_background_color(data->peek_layer, colors->bg_color);
// This is so that only the banner of the swap_layer is sticking out from the bottom
GRect swap_frame = ((Layer *)&data->swap_layer)->frame;
swap_frame.origin.y = swap_frame.origin.y + swap_frame.size.h -
PBL_IF_RECT_ELSE(LAYOUT_BANNER_HEIGHT_RECT, LAYOUT_TOP_BANNER_HEIGHT_ROUND);
layer_set_frame((Layer *)&data->swap_layer, &swap_frame);
// play the peek layer after the delay, more delay for the first notification
// because we're coming from the compositor modal transition
const uint16_t peek_layer_play_delay = is_first_notification ? FIRST_PEEK_DELAY : 100;
data->peek_layer_timer = evented_timer_register_or_reschedule(data->peek_layer_timer,
peek_layer_play_delay,
prv_play_peek_layer,
data);
// insert below status bar but above everything else.
Window *window = &data->window;
layer_add_child(window_get_root_layer(window), (Layer *)data->peek_layer);
layer_insert_below_sibling((Layer *)data->peek_layer, (Layer *)&data->status_layer);
}
///////////////////////
// SwapLayer Callbacks
///////////////////////
static void prv_remove_notification(NotificationWindowData *data, Uuid *notif_id,
const bool should_close_am) {
// We have to check if the current presented notification is the one being
// viewed. If it is, then we check if we have an action menu and close it.
// If we have an action menu and it's frozen we are waiting on an action result
// from that menu; this will cleanup the action menu on completion anyway so don't do it here
if (should_close_am && uuid_equal(notifications_presented_list_current(), notif_id) &&
data->action_menu && ((Window *)data->action_menu)->on_screen &&
!action_menu_is_frozen(data->action_menu)) {
action_menu_close(data->action_menu, true);
}
// Setting the next ID is handled by the service
notifications_presented_list_remove(notif_id);
if ((notifications_presented_list_current_idx() < 0) && s_in_use) {
prv_pop_notification_window(data);
return;
}
prv_reload_swap_layer(data);
}
static void prv_layout_removed_handler(SwapLayer *swap_layer, LayoutLayer *layout, void *context) {
TimelineItem *item = layout_get_context(layout);
timeline_item_destroy(item);
layout_destroy(layout);
}
T_STATIC LayoutLayer *prv_get_layout_handler(SwapLayer *swap_layer, int8_t rel_position,
void *context) {
NotificationWindowData *data = context;
Uuid *id = notifications_presented_list_relative(
notifications_presented_list_current(), rel_position);
// if no layers, don't return one
if (uuid_is_invalid(id)) {
return NULL;
}
NotificationType type = notifications_presented_list_get_type(id);
TimelineItem *item = task_zalloc_check(sizeof(TimelineItem));
if (type == NotificationMobile) {
if (!notification_storage_get(id, item)) {
PBL_LOG_ERR("Failed to read notification");
goto cleanup;
}
} else if (type == NotificationReminder) {
// validate reminder
int rv = reminder_db_read_item(item, id);
if (rv != S_SUCCESS) {
PBL_LOG_ERR("Failed to read reminder");
goto cleanup;
}
}
// Determine if the icon isn't a system resource (meaning we have to load its associated app id)
TimelineResourceId icon = attribute_get_uint32(&item->attr_list, AttributeIdIconTiny,
TIMELINE_RESOURCE_INVALID);
TimelineItem pin;
if (timeline_resources_is_system(icon) ||
pin_db_read_item_header(&pin, &item->header.parent_id) != S_SUCCESS) {
data->notification_app_id = (Uuid)UUID_INVALID;
} else {
data->notification_app_id = pin.header.parent_id;
}
const LayoutId layout_id = (type == NotificationMobile) ? LayoutIdNotification : LayoutIdReminder;
NotificationLayoutInfo layout_info = (NotificationLayoutInfo) {
.item = item,
.show_notification_timestamp = !prv_should_pop_due_to_inactivity()
};
const LayoutLayerConfig config = {
.frame = &data->window.layer.bounds,
.attributes = &item->attr_list,
.mode = LayoutLayerModeCard,
.app_id = &data->notification_app_id,
.context = &layout_info
};
NotificationLayout *notification_layout = (NotificationLayout *)layout_create(layout_id, &config);
return ¬ification_layout->layout;
cleanup:
timeline_item_destroy(item);
return NULL;
}
//////////////////////
// Timer Functions
//////////////////////
static bool prv_should_pop_due_to_inactivity(void) {
// If not a modal, then we are in the notification history app and the pop timer makes no sense
// If in DND mode we keep notifications on screen unless the user opted into auto-dismiss
return s_in_use && s_notification_window_data.is_modal &&
(!do_not_disturb_is_active() || alerts_preferences_dnd_get_auto_dismiss());
}
static void prv_pop_timer_callback(void *data) {
NotificationWindowData *window_data = data;
window_data->pop_timer_id = EVENTED_TIMER_INVALID_ID;
// It's possible that our timeout expired at the same time the window was dismissed
// through a button press or something like that. So, ignore this CALLBACK event posted
// by our timer if our window is already down (s_in_use false)
if (s_in_use) {
prv_pop_notification_window(window_data);
}
}
static void prv_refresh_pop_timer_with_timeout(NotificationWindowData *data, uint32_t timeout,
bool final) {
if (!prv_should_pop_due_to_inactivity()) {
return;
}
if (data->action_menu == NULL) {
// If the user has an action menu open, then we don't want to refresh the pop timeout,
// as they are still interacting with the Notification stack
data->pop_timer_is_final = final;
data->pop_timer_id = evented_timer_register_or_reschedule(data->pop_timer_id, timeout,
prv_pop_timer_callback, data);
}
}
static void prv_refresh_pop_timer(NotificationWindowData *data) {
if (data->pop_timer_is_final) {
return;
}
const uint32_t timeout_ms = alerts_get_notification_window_timeout_ms();
prv_refresh_pop_timer_with_timeout(data, timeout_ms, false);
}
static void prv_pop_notification_window_after_delay(NotificationWindowData *data,
uint32_t delay_ms) {
prv_refresh_pop_timer_with_timeout(data, delay_ms, true);
}
static time_t prv_get_stale_time(TimelineItem *item) {
// Reminders become stale 10 minutes after their start time, or when the event is over
return item->header.timestamp + MAX(10, item->header.duration) * SECONDS_PER_MINUTE;
}
static void prv_clear_if_stale_reminder(Uuid *id, NotificationType type, void *cb_data) {
NotificationWindowData *window_data = cb_data;
if (type != NotificationReminder) {
return;
}
TimelineItem reminder;
if (S_SUCCESS != reminder_db_read_item(&reminder, id)) {
return;
}
timeline_item_free_allocated_buffer(&reminder);
TimelineItem item;
if (S_SUCCESS != pin_db_get(&reminder.header.parent_id, &item)) {
return;
}
timeline_item_free_allocated_buffer(&item);
// Use the latest stale time to auto-hide the reminder.
const time_t reminder_stale_time = prv_get_stale_time(&reminder);
const time_t event_stale_time = prv_get_stale_time(&item);
const time_t stale_time = MAX(reminder_stale_time, event_stale_time);
const time_t now = rtc_get_time();
if (stale_time <= now && window_data->is_modal) {
PBL_LOG_INFO("Removing stale reminder from notification popup window");
prv_remove_notification(window_data, id, true /* close am */);
}
}
static void prv_clear_stale_reminders(void *data) {
notifications_presented_list_each(prv_clear_if_stale_reminder, data);
}
static void prv_clear_stale_reminders_timer_cb(void *data) {
// This functionality only exists for popups (modal windows) which currently all
// run on Kernel Main
launcher_task_add_callback(prv_clear_stale_reminders, data);
}
static void prv_setup_reminder_watchdog(NotificationWindowData *data) {
if (!data->is_modal || regular_timer_is_scheduled(&data->reminder_watchdog_timer_id) ||
!do_not_disturb_is_active()) {
return;
}
data->reminder_watchdog_timer_id = (const RegularTimerInfo) {
.cb = prv_clear_stale_reminders_timer_cb,
.cb_data = data,
};
regular_timer_add_minutes_callback(&data->reminder_watchdog_timer_id);
}
///////////////////////
// Clicks
///////////////////////
static bool prv_should_show_action_in_action_menu(NotificationWindowData *data,
const TimelineItem *item,
const TimelineItemAction *action) {
if (timeline_item_is_ancs_notif(item)) {
if (data->is_modal) {
// If we are in the modal popup show all available actions. We are fairly certain that the
// notification will still be in the notification center at this point so all ANCS actions
// should work
return true;
} else {
// If we are in the notifications app, only show non ANCS actions. Pre iOS9 we can't really
// know if the notification is still in the notification center or not, so we play it safe
// and only show non ACNS actions. Once iOS9 is more widespread we can look at updating this
return !timeline_item_action_is_ancs(action);
}
} else { // Android
// Show all actions unless the item has already been acted upon, in which case show none
return (!item->header.actioned &&
!item->header.dismissed &&
(data->is_modal ||
comm_session_has_capability(comm_session_get_system_session(),
CommSessionExtendedNotificationService)));
}
}
static bool prv_should_provide_action_menu_for_item(NotificationWindowData *data,
const TimelineItem *item) {
for (int i = 0; i < item->action_group.num_actions; i++) {
TimelineItemAction *action = &item->action_group.actions[i];
if (prv_should_show_action_in_action_menu(data, item, action)) {
return true;
}
}
return false;
}
static WindowStack *prv_get_window_stack(void) {
if (pebble_task_get_current() == PebbleTask_App) {
return app_state_get_window_stack();
}
return modal_manager_get_window_stack(ModalPriorityNotification);
}
static void prv_push_snooze_dialog(void) {
SimpleDialog *simple_dialog = simple_dialog_create("Snooze");
Dialog *dialog = simple_dialog_get_dialog(simple_dialog);
const char *msg = i18n_get("Snoozed", dialog);
dialog_set_text(dialog, msg);
dialog_set_icon(dialog, RESOURCE_ID_REMINDER_SNOOZE);
i18n_free(msg, dialog);
dialog_set_text_color(dialog, GColorWhite);
dialog_set_fullscreen(dialog, true);
dialog_set_background_color(dialog, GColorBlueMoon);
dialog_set_timeout(dialog, 1700);
simple_dialog_push(simple_dialog, prv_get_window_stack());
}
static void prv_snooze_reminder_cb(ActionMenu *action_menu,
const ActionMenuItem *action_menu_item,
void *context) {
NotificationWindowData *window_data = (NotificationWindowData *) action_menu_item->action_data;
TimelineItem *item = prv_get_current_notification(window_data);
// Snooze reminder.
// It's highly unlikely we'll get E_INVALID_OPERATION based on the snooze logic parameters.
if (reminders_snooze((Reminder *) item) == S_SUCCESS) {
prv_push_snooze_dialog();
}
// Dismiss reminder
const TimelineItemAction *action = timeline_item_find_dismiss_action(item);
if (action) {
timeline_invoke_action(item, action, NULL);
}
}
static void prv_push_muted_dialog(void) {
SimpleDialog *simple_dialog = simple_dialog_create("Muted");
Dialog *dialog = simple_dialog_get_dialog(simple_dialog);
const char *msg = i18n_get("Muted", dialog);
dialog_set_text(dialog, msg);
dialog_set_icon(dialog, RESOURCE_ID_RESULT_MUTE_LARGE);
dialog_set_timeout(dialog, DIALOG_TIMEOUT_DEFAULT);
i18n_free(msg, dialog);
simple_dialog_push(simple_dialog, prv_get_window_stack());
}
static void prv_mute_notification(const ActionMenuItem *action_menu_item,
uint8_t muted_bitfield) {
NotificationWindowData *window_data = action_menu_item->action_data;
TimelineItem *item = prv_get_current_notification(window_data);
const char *app_id = attribute_get_string(&item->attr_list, AttributeIdiOSAppIdentifier, "");
if (!*app_id) {
PBL_LOG_ERR("Could not mute notification. Unknown app_id");
return;
}
const int app_id_len = strlen(app_id);
iOSNotifPrefs *notif_prefs = ios_notif_pref_db_get_prefs((uint8_t *)app_id, app_id_len);
if (notif_prefs && attribute_find(¬if_prefs->attr_list, AttributeIdMuteDayOfWeek)) {
attribute_list_add_uint8(¬if_prefs->attr_list, AttributeIdMuteDayOfWeek, muted_bitfield);
ios_notif_pref_db_store_prefs((uint8_t *)app_id, app_id_len,
¬if_prefs->attr_list, ¬if_prefs->action_group);
TimelineItemAction *dismiss = timeline_item_find_dismiss_action(item);
if (dismiss) {
timeline_invoke_action(item, dismiss, NULL);
}
prv_push_muted_dialog();
} else {
// This is a very unlikely case. We store some default prefs which includes the mute
// attribute when we receive the notification so either someone deleted the entry
// in the DB or the mute attribute (neither of which should happen)
PBL_LOG_WRN("Could not mute notification. No prefs or mute attribute");
}
ios_notif_pref_db_free_prefs(notif_prefs);
}
static void prv_mute_notification_always(ActionMenu *action_menu,
const ActionMenuItem *action_menu_item,
void *context) {
prv_mute_notification(action_menu_item, MuteBitfield_Always);
}
static void prv_mute_notification_weekdays(ActionMenu *action_menu,
const ActionMenuItem *action_menu_item,
void *context) {
prv_mute_notification(action_menu_item, MuteBitfield_Weekdays);
}
static void prv_mute_notification_weekends(ActionMenu *action_menu,
const ActionMenuItem *action_menu_item,
void *context) {
prv_mute_notification(action_menu_item, MuteBitfield_Weekends);
}
static void prv_mute_notification_timed(const ActionMenuItem *action_menu_item, int duration_seconds) {
NotificationWindowData *window_data = action_menu_item->action_data;
TimelineItem *item = prv_get_current_notification(window_data);
const char *app_id = attribute_get_string(&item->attr_list, AttributeIdiOSAppIdentifier, "");
if (!app_id || !*app_id) {
PBL_LOG_ERR("Could not mute notification. Unknown app_id");
return;
}
const int app_id_len = strlen(app_id);
iOSNotifPrefs *notif_prefs = ios_notif_pref_db_get_prefs((uint8_t *)app_id, app_id_len);
Attribute *expiration_attr = notif_prefs ?
attribute_find(¬if_prefs->attr_list, AttributeIdMuteExpiration) : NULL;
if (notif_prefs && expiration_attr) {
const uint32_t expiration_time = rtc_get_time() + duration_seconds;
expiration_attr->uint32 = expiration_time;
ios_notif_pref_db_store_prefs((uint8_t *)app_id, app_id_len,
¬if_prefs->attr_list, ¬if_prefs->action_group);
TimelineItemAction *dismiss = timeline_item_find_dismiss_action(item);
if (dismiss) {
timeline_invoke_action(item, dismiss, NULL);
}
prv_push_muted_dialog();
} else {
PBL_LOG_WRN("Could not mute notification. No prefs or mute expiration attribute");
}
ios_notif_pref_db_free_prefs(notif_prefs);
}
static void prv_mute_notification_1_hour(ActionMenu *action_menu,
const ActionMenuItem *action_menu_item,
void *context) {
prv_mute_notification_timed(action_menu_item, 3600);
}
static void prv_mute_notification_today(ActionMenu *action_menu,
const ActionMenuItem *action_menu_item,
void *context) {
time_t now = rtc_get_time();
struct tm now_tm;
localtime_r(&now, &now_tm);
const int seconds_until_midnight = (24 * 3600) - (now_tm.tm_hour * 3600 + now_tm.tm_min * 60 + now_tm.tm_sec);
prv_mute_notification_timed(action_menu_item, seconds_until_midnight);
}
static bool prv_has_mute_action(TimelineItem *item) {
PebbleProtocolCapabilities capabilities;
bt_persistent_storage_get_cached_system_capabilities(&capabilities);
return timeline_item_is_ancs_notif(item) && capabilities.notification_filtering_support;
}
static ActionMenuLevel *prv_create_action_menu_for_item(TimelineItem *item,
NotificationWindowData *window_data,
TimelineItemActionSource source) {
// Determine action menu properties
int num_timeline_actions = 0;
TimelineItemAction *dismiss_action = NULL;
for (int i = 0; i < item->action_group.num_actions; i++) {
TimelineItemAction *action = &item->action_group.actions[i];
if (prv_should_show_action_in_action_menu(window_data, item, action)) {
num_timeline_actions++;
if (timeline_item_action_is_dismiss(action)) {
dismiss_action = action;
}
}
}
// Snooze is not needed for Reminders App items
Uuid items_originator_id;
timeline_get_originator_id(item, &items_originator_id);
const bool has_snooze_action = ((item->header.type == TimelineItemTypeReminder) &&
!uuid_equal(&(Uuid)UUID_REMINDERS_DATA_SOURCE, &items_originator_id) &&
reminders_can_snooze(item));
const bool has_dismiss_all_action = ((dismiss_action) &&
(notifications_presented_list_count() > 1));
const bool has_quiet_time_action = true; // Always true
const bool has_ancs_mute_action = prv_has_mute_action(item);
uint8_t num_local_actions = 0;
num_local_actions += (has_snooze_action) ? 1 : 0;
num_local_actions += (has_dismiss_all_action) ? 1 : 0;
num_local_actions += (has_quiet_time_action) ? 1 : 0;
num_local_actions += (has_ancs_mute_action) ? 1 : 0;
uint8_t num_item_specific_actions = num_timeline_actions;
num_item_specific_actions += (has_snooze_action) ? 1 : 0;
num_item_specific_actions += (has_ancs_mute_action) ? 1 : 0;
// Create root level
uint8_t num_actions = num_timeline_actions + num_local_actions;
uint8_t separator_index = num_actions > num_item_specific_actions ? num_item_specific_actions : 0;
ActionMenuLevel *root_level = timeline_actions_create_action_menu_root_level(num_actions,
separator_index,
source);
// Add actions in order
// [0] Dismiss (if applicable)
// [1] Snooze (if applicable)
// [2] Other mobile actions
// ... Other mobile actions
// [n] Other mobile actions
// [n + 1] ANCS Mute (if applicable)
// [n + 2] Dismiss all (if applicable)
// [n + 3] Toggle Quiet Time
if (dismiss_action) {
timeline_actions_add_action_to_root_level(dismiss_action, root_level);
}
if (has_snooze_action) {
action_menu_level_add_action(root_level,
i18n_get("Snooze", root_level),
prv_snooze_reminder_cb,
window_data);
}
for (int i = 0; i < item->action_group.num_actions; i++) {
TimelineItemAction *action = &item->action_group.actions[i];
if (prv_should_show_action_in_action_menu(window_data, item, action) &&
action != dismiss_action) {
timeline_actions_add_action_to_root_level(action, root_level);
}
}
if (has_ancs_mute_action) {
const char *app_id = attribute_get_string(&item->attr_list, AttributeIdiOSAppIdentifier, "");
iOSNotifPrefs *notif_prefs = ios_notif_pref_db_get_prefs((uint8_t *)app_id, strlen(app_id));
char *display_name = "";
if (notif_prefs) {
display_name = (char *)attribute_get_string(¬if_prefs->attr_list, AttributeIdAppName, "");
}
const char *mute_label = i18n_noop("Mute %s");
static char mute_label_buf[32];
snprintf(mute_label_buf, sizeof(mute_label_buf),
i18n_get(mute_label, root_level), display_name);
const uint8_t mute_option = ancs_filtering_get_mute_type(notif_prefs);
const bool is_mute_weekdays = mute_option == MuteBitfield_Weekdays;
const bool is_mute_weekends = mute_option == MuteBitfield_Weekends;
if (is_mute_weekdays || is_mute_weekends) {
action_menu_level_add_action(root_level,
mute_label_buf,
prv_mute_notification_always,
window_data);
} else {
const uint8_t number_mute_actions = 5;
ActionMenuLevel *mute_level = action_menu_level_create(number_mute_actions);
action_menu_level_add_child(root_level,
mute_level,
mute_label_buf);
action_menu_level_add_action(mute_level,
i18n_get("Mute 1 Hour", root_level),
prv_mute_notification_1_hour,
window_data);
action_menu_level_add_action(mute_level,
i18n_get("Mute Today", root_level),
prv_mute_notification_today,
window_data);
action_menu_level_add_action(mute_level,
i18n_get("Mute Always", root_level),
prv_mute_notification_always,
window_data);
action_menu_level_add_action(mute_level,
i18n_get("Mute Weekends", root_level),
prv_mute_notification_weekends,
window_data);
action_menu_level_add_action(mute_level,
i18n_get("Mute Weekdays", root_level),
prv_mute_notification_weekdays,
window_data);
}
ios_notif_pref_db_free_prefs(notif_prefs);
}
if (has_dismiss_all_action) {
action_menu_level_add_action(root_level,
i18n_get("Dismiss All", root_level),
prv_dismiss_all_action_cb,