-
-
Notifications
You must be signed in to change notification settings - Fork 952
/
Notifications.cpp
374 lines (332 loc) · 15.3 KB
/
Notifications.cpp
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
#include "displayapp/screens/Notifications.h"
#include "displayapp/DisplayApp.h"
#include "components/ble/MusicService.h"
#include "components/ble/AlertNotificationService.h"
#include "displayapp/screens/Symbols.h"
#include <algorithm>
#include "displayapp/InfiniTimeTheme.h"
using namespace Pinetime::Applications::Screens;
extern lv_font_t jetbrains_mono_extrabold_compressed;
extern lv_font_t jetbrains_mono_bold_20;
Notifications::Notifications(DisplayApp* app,
Pinetime::Controllers::NotificationManager& notificationManager,
Pinetime::Controllers::AlertNotificationService& alertNotificationService,
Pinetime::Controllers::MotorController& motorController,
System::SystemTask& systemTask,
Modes mode)
: app {app},
notificationManager {notificationManager},
alertNotificationService {alertNotificationService},
motorController {motorController},
wakeLock(systemTask),
mode {mode} {
notificationManager.ClearNewNotificationFlag();
auto notification = notificationManager.GetLastNotification();
if (notification.valid) {
currentId = notification.id;
currentItem = std::make_unique<NotificationItem>(notification.Title(),
notification.Message(),
1,
notification.category,
notificationManager.NbNotifications(),
alertNotificationService,
motorController);
validDisplay = true;
} else {
currentItem = std::make_unique<NotificationItem>(alertNotificationService, motorController);
validDisplay = false;
}
if (mode == Modes::Preview) {
wakeLock.Lock();
if (notification.category == Controllers::NotificationManager::Categories::IncomingCall) {
motorController.StartRinging();
} else {
motorController.RunForDuration(35);
}
timeoutLine = lv_line_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_line_width(timeoutLine, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3);
lv_obj_set_style_local_line_color(timeoutLine, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
lv_obj_set_style_local_line_rounded(timeoutLine, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, true);
lv_line_set_points(timeoutLine, timeoutLinePoints, 2);
timeoutTickCountStart = xTaskGetTickCount();
interacted = false;
}
taskRefresh = lv_task_create(RefreshTaskCallback, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, this);
}
Notifications::~Notifications() {
lv_task_del(taskRefresh);
// make sure we stop any vibrations before exiting
motorController.StopRinging();
lv_obj_clean(lv_scr_act());
}
void Notifications::Refresh() {
if (mode == Modes::Preview && timeoutLine != nullptr) {
TickType_t tick = xTaskGetTickCount();
int32_t pos = LV_HOR_RES - ((tick - timeoutTickCountStart) / (timeoutLength / LV_HOR_RES));
if (pos <= 0) {
running = false;
} else {
timeoutLinePoints[1].x = pos;
lv_line_set_points(timeoutLine, timeoutLinePoints, 2);
}
} else if (mode == Modes::Preview && dismissingNotification) {
running = false;
} else if (dismissingNotification) {
dismissingNotification = false;
auto notification = notificationManager.Get(currentId);
if (!notification.valid) {
notification = notificationManager.GetLastNotification();
}
currentId = notification.id;
if (!notification.valid) {
validDisplay = false;
}
currentItem.reset(nullptr);
if (afterDismissNextMessageFromAbove) {
app->SetFullRefresh(DisplayApp::FullRefreshDirections::Down);
} else {
app->SetFullRefresh(DisplayApp::FullRefreshDirections::Up);
}
if (validDisplay) {
Controllers::NotificationManager::Notification::Idx currentIdx = notificationManager.IndexOf(currentId);
currentItem = std::make_unique<NotificationItem>(notification.Title(),
notification.Message(),
currentIdx + 1,
notification.category,
notificationManager.NbNotifications(),
alertNotificationService,
motorController);
} else {
running = false;
}
}
running = running && currentItem->IsRunning();
}
void Notifications::OnPreviewInteraction() {
wakeLock.Release();
motorController.StopRinging();
if (timeoutLine != nullptr) {
lv_obj_del(timeoutLine);
timeoutLine = nullptr;
}
}
void Notifications::DismissToBlack() {
currentItem.reset(nullptr);
app->SetFullRefresh(DisplayApp::FullRefreshDirections::RightAnim);
// create black transition screen to let the notification dismiss to blackness
lv_obj_t* blackBox = lv_obj_create(lv_scr_act(), nullptr);
lv_obj_set_size(blackBox, LV_HOR_RES, LV_VER_RES);
lv_obj_set_style_local_bg_color(blackBox, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
dismissingNotification = true;
}
void Notifications::OnPreviewDismiss() {
notificationManager.Dismiss(currentId);
if (timeoutLine != nullptr) {
lv_obj_del(timeoutLine);
timeoutLine = nullptr;
}
DismissToBlack();
}
bool Notifications::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
if (mode != Modes::Normal) {
if (!interacted && event == TouchEvents::Tap) {
interacted = true;
OnPreviewInteraction();
return true;
} else if (event == Pinetime::Applications::TouchEvents::SwipeRight) {
OnPreviewDismiss();
return true;
}
return false;
}
switch (event) {
case Pinetime::Applications::TouchEvents::SwipeRight:
if (validDisplay) {
auto previousMessage = notificationManager.GetPrevious(currentId);
auto nextMessage = notificationManager.GetNext(currentId);
afterDismissNextMessageFromAbove = previousMessage.valid;
notificationManager.Dismiss(currentId);
if (previousMessage.valid) {
currentId = previousMessage.id;
} else if (nextMessage.valid) {
currentId = nextMessage.id;
} else {
// don't update id, notification manager will try to fetch
// but not find it. Refresh will try to load latest message
// or dismiss to watchface
}
DismissToBlack();
return true;
}
return false;
case Pinetime::Applications::TouchEvents::SwipeDown: {
Controllers::NotificationManager::Notification previousNotification;
if (validDisplay) {
previousNotification = notificationManager.GetPrevious(currentId);
} else {
previousNotification = notificationManager.GetLastNotification();
}
if (!previousNotification.valid) {
return true;
}
currentId = previousNotification.id;
Controllers::NotificationManager::Notification::Idx currentIdx = notificationManager.IndexOf(currentId);
validDisplay = true;
currentItem.reset(nullptr);
app->SetFullRefresh(DisplayApp::FullRefreshDirections::Down);
currentItem = std::make_unique<NotificationItem>(previousNotification.Title(),
previousNotification.Message(),
currentIdx + 1,
previousNotification.category,
notificationManager.NbNotifications(),
alertNotificationService,
motorController);
}
return true;
case Pinetime::Applications::TouchEvents::SwipeUp: {
Controllers::NotificationManager::Notification nextNotification;
if (validDisplay) {
nextNotification = notificationManager.GetNext(currentId);
} else {
nextNotification = notificationManager.GetLastNotification();
}
if (!nextNotification.valid) {
running = false;
return false;
}
currentId = nextNotification.id;
Controllers::NotificationManager::Notification::Idx currentIdx = notificationManager.IndexOf(currentId);
validDisplay = true;
currentItem.reset(nullptr);
app->SetFullRefresh(DisplayApp::FullRefreshDirections::Up);
currentItem = std::make_unique<NotificationItem>(nextNotification.Title(),
nextNotification.Message(),
currentIdx + 1,
nextNotification.category,
notificationManager.NbNotifications(),
alertNotificationService,
motorController);
}
return true;
default:
return false;
}
}
namespace {
void CallEventHandler(lv_obj_t* obj, lv_event_t event) {
auto* item = static_cast<Notifications::NotificationItem*>(obj->user_data);
item->OnCallButtonEvent(obj, event);
}
}
Notifications::NotificationItem::NotificationItem(Pinetime::Controllers::AlertNotificationService& alertNotificationService,
Pinetime::Controllers::MotorController& motorController)
: NotificationItem("Notification",
"No notification to display",
0,
Controllers::NotificationManager::Categories::Unknown,
0,
alertNotificationService,
motorController) {
}
Notifications::NotificationItem::NotificationItem(const char* title,
const char* msg,
uint8_t notifNr,
Controllers::NotificationManager::Categories category,
uint8_t notifNb,
Pinetime::Controllers::AlertNotificationService& alertNotificationService,
Pinetime::Controllers::MotorController& motorController)
: alertNotificationService {alertNotificationService}, motorController {motorController} {
container = lv_cont_create(lv_scr_act(), nullptr);
lv_obj_set_size(container, LV_HOR_RES, LV_VER_RES);
lv_obj_set_style_local_bg_color(container, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
lv_obj_set_style_local_pad_all(container, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
lv_obj_set_style_local_pad_inner(container, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
lv_obj_set_style_local_border_width(container, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
subject_container = lv_cont_create(container, nullptr);
lv_obj_set_style_local_bg_color(subject_container, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, Colors::bgAlt);
lv_obj_set_style_local_pad_all(subject_container, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 10);
lv_obj_set_style_local_pad_inner(subject_container, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 5);
lv_obj_set_style_local_border_width(subject_container, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
lv_obj_set_pos(subject_container, 0, 50);
lv_obj_set_size(subject_container, LV_HOR_RES, LV_VER_RES - 50);
lv_cont_set_layout(subject_container, LV_LAYOUT_COLUMN_LEFT);
lv_cont_set_fit(subject_container, LV_FIT_NONE);
lv_obj_t* alert_count = lv_label_create(container, nullptr);
lv_label_set_text_fmt(alert_count, "%i/%i", notifNr, notifNb);
lv_obj_align(alert_count, nullptr, LV_ALIGN_IN_TOP_RIGHT, 0, 16);
lv_obj_t* alert_type = lv_label_create(container, nullptr);
lv_obj_set_style_local_text_color(alert_type, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::orange);
if (title == nullptr) {
lv_label_set_text_static(alert_type, "Notification");
} else {
// copy title to label and replace newlines with spaces
lv_label_set_text(alert_type, title);
char* pchar = strchr(lv_label_get_text(alert_type), '\n');
while (pchar != nullptr) {
*pchar = ' ';
pchar = strchr(pchar + 1, '\n');
}
lv_label_refr_text(alert_type);
}
lv_label_set_long_mode(alert_type, LV_LABEL_LONG_SROLL_CIRC);
lv_obj_set_width(alert_type, 180);
lv_obj_align(alert_type, nullptr, LV_ALIGN_IN_TOP_LEFT, 0, 16);
lv_obj_t* alert_subject = lv_label_create(subject_container, nullptr);
lv_label_set_long_mode(alert_subject, LV_LABEL_LONG_BREAK);
lv_obj_set_width(alert_subject, LV_HOR_RES - 20);
switch (category) {
default:
lv_label_set_text(alert_subject, msg);
break;
case Controllers::NotificationManager::Categories::IncomingCall: {
lv_obj_set_height(subject_container, 108);
lv_label_set_text_static(alert_subject, "Incoming call from");
lv_obj_t* alert_caller = lv_label_create(subject_container, nullptr);
lv_obj_align(alert_caller, alert_subject, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
lv_label_set_long_mode(alert_caller, LV_LABEL_LONG_BREAK);
lv_obj_set_width(alert_caller, LV_HOR_RES - 20);
lv_label_set_text(alert_caller, msg);
bt_accept = lv_btn_create(container, nullptr);
bt_accept->user_data = this;
lv_obj_set_event_cb(bt_accept, CallEventHandler);
lv_obj_set_size(bt_accept, 76, 76);
lv_obj_align(bt_accept, nullptr, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0);
label_accept = lv_label_create(bt_accept, nullptr);
lv_label_set_text_static(label_accept, Symbols::phone);
lv_obj_set_style_local_bg_color(bt_accept, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::highlight);
bt_reject = lv_btn_create(container, nullptr);
bt_reject->user_data = this;
lv_obj_set_event_cb(bt_reject, CallEventHandler);
lv_obj_set_size(bt_reject, 76, 76);
lv_obj_align(bt_reject, nullptr, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
label_reject = lv_label_create(bt_reject, nullptr);
lv_label_set_text_static(label_reject, Symbols::phoneSlash);
lv_obj_set_style_local_bg_color(bt_reject, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
bt_mute = lv_btn_create(container, nullptr);
bt_mute->user_data = this;
lv_obj_set_event_cb(bt_mute, CallEventHandler);
lv_obj_set_size(bt_mute, 76, 76);
lv_obj_align(bt_mute, nullptr, LV_ALIGN_IN_BOTTOM_RIGHT, 0, 0);
label_mute = lv_label_create(bt_mute, nullptr);
lv_label_set_text_static(label_mute, Symbols::volumMute);
lv_obj_set_style_local_bg_color(bt_mute, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::lightGray);
} break;
}
}
void Notifications::NotificationItem::OnCallButtonEvent(lv_obj_t* obj, lv_event_t event) {
if (event != LV_EVENT_CLICKED) {
return;
}
motorController.StopRinging();
if (obj == bt_accept) {
alertNotificationService.AcceptIncomingCall();
} else if (obj == bt_reject) {
alertNotificationService.RejectIncomingCall();
} else if (obj == bt_mute) {
alertNotificationService.MuteIncomingCall();
}
running = false;
}
Notifications::NotificationItem::~NotificationItem() {
lv_obj_clean(lv_scr_act());
}