From 1c71f21e0812210b57dfa4c3e57360b625a41dd0 Mon Sep 17 00:00:00 2001 From: Caisking <3449750381@qq.com> Date: Thu, 10 Oct 2024 16:00:41 +0800 Subject: [PATCH] =?utf8?q?#694=E6=B6=88=E6=81=AF=E6=8F=90=E9=86=92?= =?utf8?q?=E5=BC=B9=E5=87=BA=E4=BC=9A=E4=B8=80=E7=9B=B4=E6=98=BE=E7=A4=BA?= =?utf8?q?=EF=BC=8C=E9=9C=80=E8=A6=81=E6=89=8B=E5=8A=A8=E7=82=B9=E5=87=BB?= =?utf8?q?=E5=85=B3=E9=97=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/message/src/MessageModel.cpp | 179 +++++++++--------- .../include/notify/message_notification.h | 2 + .../src/notify/message_notification.cpp | 107 ++++++++++- 3 files changed, 195 insertions(+), 93 deletions(-) diff --git a/code/sdk/application/wearable/fd230app/nativeui/message/src/MessageModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/message/src/MessageModel.cpp index d413c2d56..0142b4bcc 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/message/src/MessageModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/message/src/MessageModel.cpp @@ -116,97 +116,94 @@ static bool shouldDisplayMessage = false; static ancs_data_info_t *lastMessageInfo = NULL; static std::mutex messageMutex; // 互斥锁 -static void Notify(void *args, const Topic topic, Request *request) { - UNUSED(args); - UNUSED(topic); - - if (request == NULL) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Received NULL request"); - return; - } - - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Received request %d", request->msgId); - - if (request->msgId == TOPIC_EVENT_MESSAGE_UPDATA) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, - "Processing TOPIC_EVENT_MESSAGE_UPDATA"); - - // 创建新的消息信息 - ancs_data_info_t *messageInfo = new ancs_data_info_t(); - if (messageInfo == NULL) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, - "Failed to allocate memory for ancs_data_info_t"); - return; - } - - // 复制请求数据到新的消息信息 - int rets = memcpy_s(messageInfo, sizeof(ancs_data_info_t), - (ancs_data_info_t *)request->data, request->len); - if (rets != EOK) { - delete messageInfo; - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, - "memcpy_s ancs_data_info_t failed, ret = %d", rets); - return; - } - - // 更新到模型里面的数组 - MessageModel::GetInstance().AddMessageData(messageInfo); - - // 加锁以保护共享变量 - { - std::lock_guard lock(messageMutex); - shouldDisplayMessage = true; // 标记需要显示新消息 - - // 设置最后更新时间 - lastUpdateTime = std::chrono::steady_clock::now(); - } - } - - // 检查是否需要显示新消息 - { - std::lock_guard lock(messageMutex); // 加锁以保护共享变量 - - if (shouldDisplayMessage) { - // 检查是否达到延迟时间 - auto now = std::chrono::steady_clock::now(); - auto timeDiff = now - lastUpdateTime; - WEARABLE_LOGD( - WEARABLE_LOG_MODULE_APP, "Time since last update: %lld milliseconds", - std::chrono::duration_cast(timeDiff) - .count()); - - if (timeDiff >= updateDelay) { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, - "Displaying new message after time delay"); - - // 发送信息进行弹窗 - if (uapi_dev_runtime_get_train_status() || - uapi_dev_runtime_get_saveele_status() || - uapi_dev_runtime_get_respiratory_training_status()) { - // 增加训练模式下,不弹窗 - // 省电模式,不弹窗 - // 呼吸训练,不弹窗 - return; - } - - // 发送弹窗通知 会自动查询到数组的最新一个 - CustomEventPublish(TOPIC_EVENT_MESSAGENOTE, NULL, 0); - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Message popup sent"); - - // 发送完毕,重置标志 - shouldDisplayMessage = false; // 重置状态 - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, - "Reset shouldDisplayMessage to false"); - } else { - WEARABLE_LOGD( - WEARABLE_LOG_MODULE_APP, - "Message display delayed, waiting for next opportunity %d %d", - timeDiff, updateDelay); - } - } else { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "No new message to display"); - } - } +static void Notify(void *args, const Topic topic, Request *request) { + UNUSED(args); + UNUSED(topic); + + if (request == NULL) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Received NULL request"); + return; + } + + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Received request %d", request->msgId); + + if (request->msgId == TOPIC_EVENT_MESSAGE_UPDATA) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "Processing TOPIC_EVENT_MESSAGE_UPDATA"); + + // 创建新的消息信息 + ancs_data_info_t *messageInfo = new ancs_data_info_t(); + if (messageInfo == NULL) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "Failed to allocate memory for ancs_data_info_t"); + return; + } + + // 复制请求数据到新的消息信息 + int rets = memcpy_s(messageInfo, sizeof(ancs_data_info_t), + (ancs_data_info_t *)request->data, request->len); + if (rets != EOK) { + delete messageInfo; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "memcpy_s ancs_data_info_t failed, ret = %d", rets); + return; + } + + // 更新到模型里面的数组 + MessageModel::GetInstance().AddMessageData(messageInfo); + + // 加锁以保护共享变量 + { + std::lock_guard lock(messageMutex); + shouldDisplayMessage = true; // 标记需要显示新消息 + + // 设置最后更新时间 + lastUpdateTime = std::chrono::steady_clock::now(); + } + + // 直接尝试显示消息(用于单条消息的情况) + { + std::lock_guard lock(messageMutex); // 加锁以保护共享变量 + + // 检查是否达到延迟时间 + auto now = std::chrono::steady_clock::now(); + auto timeDiff = now - lastUpdateTime; + WEARABLE_LOGD( + WEARABLE_LOG_MODULE_APP, "Time since last update: %lld milliseconds", + std::chrono::duration_cast(timeDiff) + .count()); + + if (timeDiff >= updateDelay) { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, + "Displaying new message after time delay"); + + // 发送信息进行弹窗 + if (uapi_dev_runtime_get_train_status() || + uapi_dev_runtime_get_saveele_status() || + uapi_dev_runtime_get_respiratory_training_status()) { + // 增加训练模式下,不弹窗 + // 省电模式,不弹窗 + // 呼吸训练,不弹窗 + return; + } + + // 发送弹窗通知 会自动查询到数组的最新一个 + CustomEventPublish(TOPIC_EVENT_MESSAGENOTE, NULL, 0); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Message popup sent"); + + // 发送完毕,重置标志 + shouldDisplayMessage = false; // 重置状态 + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, + "Reset shouldDisplayMessage to false"); + } else { + WEARABLE_LOGE( WEARABLE_LOG_MODULE_APP, "Message display delayed, waiting for next opportunity %lld ms %lld ms", + std::chrono::duration_cast(timeDiff).count(), + std::chrono::duration_cast(updateDelay).count()); + } + } + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "No new message to display"); + } } void saveancsdata() { diff --git a/code/sdk/application/wearable/service/msg_center/include/notify/message_notification.h b/code/sdk/application/wearable/service/msg_center/include/notify/message_notification.h index 977cc2cd5..02ebf71f0 100644 --- a/code/sdk/application/wearable/service/msg_center/include/notify/message_notification.h +++ b/code/sdk/application/wearable/service/msg_center/include/notify/message_notification.h @@ -21,6 +21,8 @@ namespace OHOS bool OnDrag(UIView& view, const DragEvent& event) override; bool OnDragEnd(UIView& view, const DragEvent& event) override; + void Message_StartTimer(uint32_t interval = 1000 * 3); + void Message_StopTimer(void); private: MessageViewEventListener(); ~MessageViewEventListener(); diff --git a/code/sdk/application/wearable/service/msg_center/src/notify/message_notification.cpp b/code/sdk/application/wearable/service/msg_center/src/notify/message_notification.cpp index 162c7f910..d468f229a 100644 --- a/code/sdk/application/wearable/service/msg_center/src/notify/message_notification.cpp +++ b/code/sdk/application/wearable/service/msg_center/src/notify/message_notification.cpp @@ -13,6 +13,17 @@ #include "message_notification.h" #include "string" #include "vibrator_service.h" +#include "ConfigModel.h" +#include "iostream" +#include "uicomponents/UIComponents.h" +#include +#include +#include +#include +#include +#include +#include +#include #include "../../../../../../../drivers/peripheral/config/include/ConfigModel.h" #include "../../../../../../../drivers/peripheral/utils/custom_runtime.h" @@ -27,6 +38,7 @@ namespace OHOS UILabelButtonExt *deleteButton{nullptr}; // 删除按键 static std::string titleStr, timeStr, messageStr; + const char *buttonImgSrc = "/user/res/466x100button.bin"; #define DELETEBUTTON "delete_button" @@ -188,7 +200,6 @@ namespace OHOS deleteButton->SetStyleForState(STYLE_BORDER_COLOR, 0xff0b1928, UIButton::PRESSED); deleteButton->SetVisible(true); #endif - const char *buttonImgSrc = "/user/res/466x100button.bin"; deleteButton->SetImageSrc(buttonImgSrc, buttonImgSrc); UIView::OnTouchListener *TouchListener = static_cast(&MessageViewEventListener::GetInstance()); if (TouchListener == nullptr) @@ -204,13 +215,16 @@ namespace OHOS MessageGroupView->Add(messageLabel); MessageGroupView->Add(deleteButton); + ScreenTurnOn(3000); // 亮屏操作 3000ms 3秒关闭 + if (true != OHOS::ConfigModel::GetInstance()->GetDnotdisturb() && (!uapi_sleep_mode_flag_get())) { // 震动提醒 ui_api_vibrate_once_func(); } + + MessageViewEventListener::GetInstance().Message_StartTimer(); // 开启一个3s后弹窗自动销毁定时器 - ScreenTurnOn(3000); // 亮屏操作 3000ms 3秒关闭 WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Create MessageGroupView succeed!!"); NotificationManager::GetInstance()->RegisterNotifyCleanupFunction(FreeMessageView); return MessageGroupView; @@ -219,6 +233,10 @@ namespace OHOS void FreeMessageView(void) { WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "FreeMessageView(void)"); + + //销毁定时器 + MessageViewEventListener::GetInstance().Message_StopTimer(); + if (MessageGroupView != nullptr) { MessageGroupView->RemoveAll(); @@ -243,4 +261,89 @@ namespace OHOS ScreenTurnOff(); return; } + + /* -------------------------------------------------------------------------- */ + + // 声明定时器变量 + static osTimerId_t g_timer = NULL; + + /*定时器到期时的处理*/ + static void Message_TimerCallbackProc(int data) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Message_TimerCallbackProc"); + NotificationManager::GetInstance()->StopNotify(); + // 点击关闭之后通知list进行刷新 + SendMsgToUIService(UI_UPDATE_MESSAGE_DATA, 0, nullptr, 0); + + } + + /* + * @brief 回调函数 + */ + static void Message_TimerCallback(int data) + { + GraphicService::GetInstance()->PostGraphicEvent(std::bind(Message_TimerCallbackProc, data)); + } + + /* + * @brief 开启定时器函数 + * @param interval 时间间隔,单位ms + */ + void MessageViewEventListener::Message_StartTimer(uint32_t interval) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MessageViewEventListener::Message_StartTimer"); + /* -------------------------- 1.检查定时器是否已经存在,如果存在则返回 ------------------------- */ + if (g_timer != NULL) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDownModel::CountDown_StartTimer Timer already exists!!"); + return; + } + + /* --------------------------------- 2.创建定时器 -------------------------------- */ + g_timer = osTimerNew((osTimerFunc_t)Message_TimerCallback, osTimerOnce, NULL, NULL); + // 检查定时器创建是否成功 + if (g_timer == NULL) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDownModel::CountDown_StartTimer Timer create failed!!"); + return; + } + + /* ----------------------------- 3.将毫秒转换为系统时钟节拍数 ---------------------------- */ + int tick = osMs2Tick(interval); + + /* --------------------------------- 4.启动定时器 -------------------------------- */ + osStatus_t ret = osTimerStart(g_timer, tick); + /* -------------------- 5.检查定时器启动是否成功,如果启动失败,删除定时器并置为 NULL -------------------- */ + if (ret != osOK) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[CountDown_StartTimer] osTimerDelete failed, ret = %lx!!", ret); + if (g_timer != NULL) + { + ret = osTimerDelete(g_timer); + } + g_timer = NULL; + return; + } + return; + } + + /* + * @brief 停止定时器 + */ + void MessageViewEventListener::Message_StopTimer(void) + { + // 检查定时器是否存在 + if (g_timer != NULL) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MessageViewEventListener::Message_StopTimer"); + osTimerStop(g_timer); + // 删除定时器并置为 NULL + osStatus_t ret = osTimerDelete(g_timer); + if (ret != osOK) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "osTimerDelete failed, ret = %lx!!", ret); + } + g_timer = NULL; + } + } } \ No newline at end of file -- 2.17.1