From 03de1c67393fcb99b5987514be6f470349c57c0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Milants?= Date: Sun, 4 Apr 2021 12:10:47 +0200 Subject: [PATCH 1/3] Add support for notification title. The notification buffer must contain the title and the message separated by a '\0' character. If the buffer does not contain any \0, the whole buffer is considered to be the message of the notification. A default title will be displayed in the notification app. --- src/components/ble/AlertNotificationClient.cpp | 1 + src/components/ble/AlertNotificationService.cpp | 1 + src/components/ble/MusicService.cpp | 1 - src/components/ble/NavigationService.cpp | 1 - src/components/ble/NotificationManager.cpp | 16 ++++++++++++++++ src/components/ble/NotificationManager.h | 4 ++++ src/displayapp/screens/Notifications.cpp | 15 ++++++++------- 7 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/components/ble/AlertNotificationClient.cpp b/src/components/ble/AlertNotificationClient.cpp index e7a18626..cfb3272d 100644 --- a/src/components/ble/AlertNotificationClient.cpp +++ b/src/components/ble/AlertNotificationClient.cpp @@ -165,6 +165,7 @@ void AlertNotificationClient::OnNotification(ble_gap_event *event) { NotificationManager::Notification notif; os_mbuf_copydata(event->notify_rx.om, headerSize, messageSize - 1, notif.message.data()); notif.message[messageSize - 1] = '\0'; + notif.size = messageSize; notif.category = Pinetime::Controllers::NotificationManager::Categories::SimpleAlert; notificationManager.Push(std::move(notif)); diff --git a/src/components/ble/AlertNotificationService.cpp b/src/components/ble/AlertNotificationService.cpp index 0639119c..c3187c1d 100644 --- a/src/components/ble/AlertNotificationService.cpp +++ b/src/components/ble/AlertNotificationService.cpp @@ -75,6 +75,7 @@ int AlertNotificationService::OnAlert(uint16_t conn_handle, uint16_t attr_handle os_mbuf_copydata(ctxt->om, headerSize, messageSize-1, notif.message.data()); os_mbuf_copydata(ctxt->om, 0, 1, &category); notif.message[messageSize-1] = '\0'; + notif.size = messageSize; // TODO convert all ANS categories to NotificationController categories switch(category) { diff --git a/src/components/ble/MusicService.cpp b/src/components/ble/MusicService.cpp index bd6e27fb..e37e3831 100644 --- a/src/components/ble/MusicService.cpp +++ b/src/components/ble/MusicService.cpp @@ -191,7 +191,6 @@ int Pinetime::Controllers::MusicService::OnCommand(uint16_t conn_handle, uint16_ data[notifSize] = '\0'; os_mbuf_copydata(ctxt->om, 0, notifSize, data); char *s = (char *) &data[0]; - NRF_LOG_INFO("DATA : %s", s); if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *) &msArtistCharUuid) == 0) { artistName = s; } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *) &msTrackCharUuid) == 0) { diff --git a/src/components/ble/NavigationService.cpp b/src/components/ble/NavigationService.cpp index 3c1fd162..545c44da 100644 --- a/src/components/ble/NavigationService.cpp +++ b/src/components/ble/NavigationService.cpp @@ -101,7 +101,6 @@ int Pinetime::Controllers::NavigationService::OnCommand(uint16_t conn_handle, ui data[notifSize] = '\0'; os_mbuf_copydata(ctxt->om, 0, notifSize, data); char *s = (char *) &data[0]; - NRF_LOG_INFO("DATA : %s", s); if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *) &navFlagCharUuid) == 0) { m_flag = s; } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *) &navNarrativeCharUuid) == 0) { diff --git a/src/components/ble/NotificationManager.cpp b/src/components/ble/NotificationManager.cpp index 36abf026..88e83b92 100644 --- a/src/components/ble/NotificationManager.cpp +++ b/src/components/ble/NotificationManager.cpp @@ -87,3 +87,19 @@ size_t NotificationManager::NbNotifications() const { return std::count_if(notifications.begin(), notifications.end(), [](const Notification& n){ return n.valid;}); } +const char* NotificationManager::Notification::Message() const { + const char* itField = std::find(message.begin(), message.begin()+size-1, '\0'); + if(itField != message.begin()+size-1) { + const char* ptr = (itField)+1; + return ptr; + } + return const_cast(message.data()); +} + +const char* NotificationManager::Notification::Title() const { + const char * itField = std::find(message.begin(), message.begin()+size-1, '\0'); + if(itField != message.begin()+size-1) { + return message.data(); + } + return {}; +} diff --git a/src/components/ble/NotificationManager.h b/src/components/ble/NotificationManager.h index 075a9a45..486bba15 100644 --- a/src/components/ble/NotificationManager.h +++ b/src/components/ble/NotificationManager.h @@ -17,8 +17,12 @@ namespace Pinetime { Id id; bool valid = false; uint8_t index; + uint8_t size; std::array message; Categories category = Categories::Unknown; + + const char* Message() const; + const char* Title() const; }; Notification::Id nextId {0}; diff --git a/src/displayapp/screens/Notifications.cpp b/src/displayapp/screens/Notifications.cpp index c903ed0f..457d1e06 100644 --- a/src/displayapp/screens/Notifications.cpp +++ b/src/displayapp/screens/Notifications.cpp @@ -17,8 +17,8 @@ Notifications::Notifications(DisplayApp *app, auto notification = notificationManager.GetLastNotification(); if(notification.valid) { currentId = notification.id; - currentItem = std::make_unique("\nNotification", - notification.message.data(), + currentItem = std::make_unique(notification.Title(), + notification.Message(), notification.index, notification.category, notificationManager.NbNotifications(), @@ -87,8 +87,8 @@ bool Notifications::OnTouchEvent(Pinetime::Applications::TouchEvents event) { currentId = previousNotification.id; currentItem.reset(nullptr); app->SetFullRefresh(DisplayApp::FullRefreshDirections::Up); - currentItem = std::make_unique("\nNotification", - previousNotification.message.data(), + currentItem = std::make_unique(previousNotification.Title(), + previousNotification.Message(), previousNotification.index, previousNotification.category, notificationManager.NbNotifications(), @@ -109,8 +109,8 @@ bool Notifications::OnTouchEvent(Pinetime::Applications::TouchEvents event) { currentId = nextNotification.id; currentItem.reset(nullptr); app->SetFullRefresh(DisplayApp::FullRefreshDirections::Down); - currentItem = std::make_unique("\nNotification", - nextNotification.message.data(), + currentItem = std::make_unique(nextNotification.Title(), + nextNotification.Message(), nextNotification.index, nextNotification.category, notificationManager.NbNotifications(), @@ -178,8 +178,9 @@ namespace { 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_hex(0x888888)); + if(title == nullptr) title = "Notification"; lv_label_set_text(alert_type, title); - lv_obj_align(alert_type, NULL, LV_ALIGN_IN_TOP_LEFT, 0, -4); + lv_obj_align(alert_type, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 16); ///////// switch(category) { From 1b71a10bebb63a1b93583d20d657d7accb6b3ddf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Milants?= Date: Sun, 4 Apr 2021 13:42:22 +0200 Subject: [PATCH 2/3] Notification app : disable swipe when the app is in 'preview' mode. In this mode, only the new notification should be displayed, there is no point to allow navigating to past notifications. Works as a workaround for the crash that occurs when you swipe the notification app in preview mode (https://github.com/JF002/InfiniTime/issues/250). --- src/displayapp/screens/Notifications.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/displayapp/screens/Notifications.cpp b/src/displayapp/screens/Notifications.cpp index 457d1e06..5600c0ec 100644 --- a/src/displayapp/screens/Notifications.cpp +++ b/src/displayapp/screens/Notifications.cpp @@ -73,6 +73,8 @@ bool Notifications::Refresh() { } bool Notifications::OnTouchEvent(Pinetime::Applications::TouchEvents event) { + if(mode != Modes::Normal) return true; + switch (event) { case Pinetime::Applications::TouchEvents::SwipeUp: { Controllers::NotificationManager::Notification previousNotification; From 3934e9bef20c5c2ad393e20cfff3a5a1b0d24569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Milants?= Date: Sun, 4 Apr 2021 15:19:37 +0200 Subject: [PATCH 3/3] Ignore notification with empty message. --- src/components/ble/AlertNotificationClient.cpp | 7 +++++-- src/components/ble/AlertNotificationService.cpp | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/components/ble/AlertNotificationClient.cpp b/src/components/ble/AlertNotificationClient.cpp index cfb3272d..9efede39 100644 --- a/src/components/ble/AlertNotificationClient.cpp +++ b/src/components/ble/AlertNotificationClient.cpp @@ -158,8 +158,11 @@ void AlertNotificationClient::OnNotification(ble_gap_event *event) { const auto maxMessageSize{NotificationManager::MaximumMessageSize()}; const auto maxBufferSize{maxMessageSize + headerSize}; - const auto dbgPacketLen = OS_MBUF_PKTLEN(event->notify_rx.om); - size_t bufferSize = std::min(dbgPacketLen + stringTerminatorSize, maxBufferSize); + // Ignore notifications with empty message + const auto packetLen = OS_MBUF_PKTLEN(event->notify_rx.om); + if(packetLen <= headerSize) return; + + size_t bufferSize = std::min(packetLen + stringTerminatorSize, maxBufferSize); auto messageSize = std::min(maxMessageSize, (bufferSize - headerSize)); NotificationManager::Notification notif; diff --git a/src/components/ble/AlertNotificationService.cpp b/src/components/ble/AlertNotificationService.cpp index c3187c1d..d91e2090 100644 --- a/src/components/ble/AlertNotificationService.cpp +++ b/src/components/ble/AlertNotificationService.cpp @@ -66,8 +66,11 @@ int AlertNotificationService::OnAlert(uint16_t conn_handle, uint16_t attr_handle const auto maxMessageSize {NotificationManager::MaximumMessageSize()}; const auto maxBufferSize{maxMessageSize + headerSize}; - const auto dbgPacketLen = OS_MBUF_PKTLEN(ctxt->om); - size_t bufferSize = std::min(dbgPacketLen + stringTerminatorSize, maxBufferSize); + // Ignore notifications with empty message + const auto packetLen = OS_MBUF_PKTLEN(ctxt->om); + if(packetLen <= headerSize) return 0; + + size_t bufferSize = std::min(packetLen + stringTerminatorSize, maxBufferSize); auto messageSize = std::min(maxMessageSize, (bufferSize-headerSize)); Categories category;