InfiniTime/src/displayapp/screens/Notifications.cpp

291 lines
12 KiB
C++
Raw Normal View History

#include "displayapp/screens/Notifications.h"
#include "displayapp/DisplayApp.h"
#include "components/ble/MusicService.h"
#include "components/ble/AlertNotificationService.h"
#include "displayapp/screens/Symbols.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,
2021-08-01 07:13:32 -04:00
Pinetime::Controllers::MotorController& motorController,
2022-01-01 09:22:35 -05:00
System::SystemTask& systemTask,
Modes mode)
2022-01-01 09:22:35 -05:00
: Screen(app),
notificationManager {notificationManager},
alertNotificationService {alertNotificationService},
motorController {motorController},
2022-01-01 09:22:35 -05:00
systemTask {systemTask},
mode {mode} {
notificationManager.ClearNewNotificationFlag();
auto notification = notificationManager.GetLastNotification();
if (notification.valid) {
currentId = notification.id;
currentItem = std::make_unique<NotificationItem>(notification.Title(),
notification.Message(),
notification.index,
notification.category,
notificationManager.NbNotifications(),
mode,
alertNotificationService,
motorController);
validDisplay = true;
} else {
2021-04-09 14:50:09 -04:00
currentItem = std::make_unique<NotificationItem>("Notification",
"No notification to display",
0,
notification.category,
notificationManager.NbNotifications(),
Modes::Preview,
alertNotificationService,
motorController);
}
if (mode == Modes::Preview) {
2022-01-01 09:22:35 -05:00
systemTask.PushMessage(System::Messages::DisableSleeping);
2021-08-01 07:13:32 -04:00
if (notification.category == Controllers::NotificationManager::Categories::IncomingCall) {
motorController.StartRinging();
} else {
motorController.RunForDuration(35);
2022-01-01 09:22:35 -05:00
}
2022-01-01 09:22:35 -05:00
timeoutLine = lv_line_create(lv_scr_act(), nullptr);
2022-01-01 09:22:35 -05:00
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;
}
2021-07-19 09:26:12 -04:00
taskRefresh = lv_task_create(RefreshTaskCallback, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, this);
}
Notifications::~Notifications() {
2021-07-19 09:26:12 -04:00
lv_task_del(taskRefresh);
2021-08-01 06:05:48 -04:00
// make sure we stop any vibrations before exiting
motorController.StopRinging();
2022-01-01 09:22:35 -05:00
systemTask.PushMessage(System::Messages::EnableSleeping);
lv_obj_clean(lv_scr_act());
}
2021-07-19 09:26:12 -04:00
void Notifications::Refresh() {
2021-08-01 06:05:48 -04:00
if (mode == Modes::Preview && timeoutLine != nullptr) {
2022-01-01 09:22:35 -05:00
TickType_t tick = xTaskGetTickCount();
int32_t pos = 240 - ((tick - timeoutTickCountStart) / (timeoutLength / 240));
if (pos <= 0) {
running = false;
2022-01-01 09:22:35 -05:00
} else {
timeoutLinePoints[1].x = pos;
lv_line_set_points(timeoutLine, timeoutLinePoints, 2);
}
}
running = currentItem->IsRunning() && running;
}
2022-01-01 09:22:35 -05:00
void Notifications::OnPreviewInteraction() {
systemTask.PushMessage(System::Messages::EnableSleeping);
motorController.StopRinging();
2022-01-01 09:22:35 -05:00
if (timeoutLine != nullptr) {
lv_obj_del(timeoutLine);
timeoutLine = nullptr;
}
}
bool Notifications::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
2021-08-01 07:13:32 -04:00
if (mode != Modes::Normal) {
2022-01-01 09:22:35 -05:00
if (!interacted && event == TouchEvents::Tap) {
interacted = true;
OnPreviewInteraction();
return true;
}
2021-08-01 07:13:32 -04:00
return false;
}
switch (event) {
case Pinetime::Applications::TouchEvents::SwipeDown: {
Controllers::NotificationManager::Notification previousNotification;
if (validDisplay)
previousNotification = notificationManager.GetPrevious(currentId);
else
previousNotification = notificationManager.GetLastNotification();
if (!previousNotification.valid)
return true;
validDisplay = true;
currentId = previousNotification.id;
currentItem.reset(nullptr);
app->SetFullRefresh(DisplayApp::FullRefreshDirections::Down);
currentItem = std::make_unique<NotificationItem>(previousNotification.Title(),
previousNotification.Message(),
previousNotification.index,
previousNotification.category,
notificationManager.NbNotifications(),
mode,
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;
}
validDisplay = true;
currentId = nextNotification.id;
currentItem.reset(nullptr);
app->SetFullRefresh(DisplayApp::FullRefreshDirections::Up);
currentItem = std::make_unique<NotificationItem>(nextNotification.Title(),
nextNotification.Message(),
nextNotification.index,
nextNotification.category,
notificationManager.NbNotifications(),
mode,
alertNotificationService,
motorController);
}
return true;
default:
return false;
}
}
namespace {
2021-08-01 06:05:48 -04:00
void CallEventHandler(lv_obj_t* obj, lv_event_t event) {
auto* item = static_cast<Notifications::NotificationItem*>(obj->user_data);
2021-08-01 06:05:48 -04:00
item->OnCallButtonEvent(obj, event);
}
}
Notifications::NotificationItem::NotificationItem(const char* title,
const char* msg,
uint8_t notifNr,
Controllers::NotificationManager::Categories category,
uint8_t notifNb,
Modes mode,
Pinetime::Controllers::AlertNotificationService& alertNotificationService,
Pinetime::Controllers::MotorController& motorController)
: mode {mode}, alertNotificationService {alertNotificationService}, motorController {motorController} {
2021-01-28 12:13:28 -05:00
lv_obj_t* container1 = lv_cont_create(lv_scr_act(), NULL);
lv_obj_set_style_local_bg_color(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x38, 0x38, 0x38));
2021-01-28 12:13:28 -05:00
lv_obj_set_style_local_pad_all(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 10);
lv_obj_set_style_local_pad_inner(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 5);
lv_obj_set_style_local_border_width(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
lv_obj_set_pos(container1, 0, 50);
2021-06-23 06:08:51 -04:00
lv_obj_set_size(container1, LV_HOR_RES, 190);
2021-01-28 12:13:28 -05:00
lv_cont_set_layout(container1, LV_LAYOUT_COLUMN_LEFT);
2021-06-23 06:08:51 -04:00
lv_cont_set_fit(container1, LV_FIT_NONE);
2021-01-28 12:13:28 -05:00
lv_obj_t* alert_count = lv_label_create(lv_scr_act(), nullptr);
2021-01-28 12:13:28 -05:00
lv_label_set_text_fmt(alert_count, "%i/%i", notifNr, notifNb);
lv_obj_align(alert_count, NULL, LV_ALIGN_IN_TOP_RIGHT, 0, 16);
lv_obj_t* alert_type = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_color(alert_type, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xb0, 0xb0, 0xb0));
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, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 16);
/////////
switch (category) {
default: {
lv_obj_t* alert_subject = lv_label_create(container1, nullptr);
lv_obj_set_style_local_text_color(alert_subject, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xff, 0xb0, 0x0));
lv_label_set_long_mode(alert_subject, LV_LABEL_LONG_BREAK);
lv_obj_set_width(alert_subject, LV_HOR_RES - 20);
lv_label_set_text(alert_subject, msg);
} break;
case Controllers::NotificationManager::Categories::IncomingCall: {
2021-06-23 06:08:51 -04:00
lv_obj_set_height(container1, 108);
lv_obj_t* alert_subject = lv_label_create(container1, nullptr);
lv_obj_set_style_local_text_color(alert_subject, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xff, 0xb0, 0x0));
lv_label_set_long_mode(alert_subject, LV_LABEL_LONG_BREAK);
lv_obj_set_width(alert_subject, LV_HOR_RES - 20);
lv_label_set_text_static(alert_subject, "Incoming call from");
lv_obj_t* alert_caller = lv_label_create(container1, 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);
2021-06-23 06:08:51 -04:00
bt_accept = lv_btn_create(lv_scr_act(), nullptr);
bt_accept->user_data = this;
2021-08-01 06:05:48 -04:00
lv_obj_set_event_cb(bt_accept, CallEventHandler);
2021-06-23 06:08:51 -04:00
lv_obj_set_size(bt_accept, 76, 76);
lv_obj_align(bt_accept, NULL, 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, LV_COLOR_MAKE(0x0, 0xb0, 0x0));
2021-06-23 06:08:51 -04:00
bt_reject = lv_btn_create(lv_scr_act(), nullptr);
bt_reject->user_data = this;
2021-08-01 06:05:48 -04:00
lv_obj_set_event_cb(bt_reject, CallEventHandler);
2021-06-23 06:08:51 -04:00
lv_obj_set_size(bt_reject, 76, 76);
lv_obj_align(bt_reject, NULL, 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);
2021-02-14 08:19:30 -05:00
2021-06-23 06:08:51 -04:00
bt_mute = lv_btn_create(lv_scr_act(), nullptr);
bt_mute->user_data = this;
2021-08-01 06:05:48 -04:00
lv_obj_set_event_cb(bt_mute, CallEventHandler);
2021-06-23 06:08:51 -04:00
lv_obj_set_size(bt_mute, 76, 76);
lv_obj_align(bt_mute, NULL, 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, LV_COLOR_MAKE(0xb0, 0xb0, 0xb0));
} break;
}
}
2021-08-01 06:05:48 -04:00
void Notifications::NotificationItem::OnCallButtonEvent(lv_obj_t* obj, lv_event_t event) {
if (event != LV_EVENT_CLICKED) {
return;
2021-08-01 06:05:48 -04:00
}
motorController.StopRinging();
2021-08-01 06:05:48 -04:00
if (obj == bt_accept) {
alertNotificationService.AcceptIncomingCall();
} else if (obj == bt_reject) {
alertNotificationService.RejectIncomingCall();
} else if (obj == bt_mute) {
alertNotificationService.MuteIncomingCall();
}
2021-08-01 06:05:48 -04:00
running = false;
}
Notifications::NotificationItem::~NotificationItem() {
lv_obj_clean(lv_scr_act());
}