#694消息提醒弹出会一直显示,需要手动点击关闭 1142/2 1142/head
authorCaisking <3449750381@qq.com>
Thu, 10 Oct 2024 08:00:41 +0000 (16:00 +0800)
committerCaisking <3449750381@qq.com>
Thu, 10 Oct 2024 08:00:41 +0000 (16:00 +0800)
code/sdk/application/wearable/fd230app/nativeui/message/src/MessageModel.cpp
code/sdk/application/wearable/service/msg_center/include/notify/message_notification.h
code/sdk/application/wearable/service/msg_center/src/notify/message_notification.cpp

index d413c2d..0142b4b 100644 (file)
@@ -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<std::mutex> lock(messageMutex);
-      shouldDisplayMessage = true; // 标记需要显示新消息
-
-      // 设置最后更新时间
-      lastUpdateTime = std::chrono::steady_clock::now();
-    }
-  }
-
-  // 检查是否需要显示新消息
-  {
-    std::lock_guard<std::mutex> 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<std::chrono::milliseconds>(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<std::mutex> lock(messageMutex);  
+      shouldDisplayMessage = true; // 标记需要显示新消息  
+
+      // 设置最后更新时间  
+      lastUpdateTime = std::chrono::steady_clock::now();  
+    }  
+    
+    // 直接尝试显示消息(用于单条消息的情况)  
+    {  
+      std::lock_guard<std::mutex> 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<std::chrono::milliseconds>(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<std::chrono::milliseconds>(timeDiff).count(),   
+                       std::chrono::duration_cast<std::chrono::milliseconds>(updateDelay).count());  
+      }  
+    }  
+  } else {  
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "No new message to display");  
+  }  
 }
 
 void saveancsdata() {
index 977cc2c..02ebf71 100644 (file)
@@ -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();
index 162c7f9..d468f22 100644 (file)
 #include "message_notification.h"
 #include "string"
 #include "vibrator_service.h"
+#include "ConfigModel.h"
+#include "iostream"
+#include "uicomponents/UIComponents.h"
+#include <broadcast_feature.h>
+#include <cmsis_os.h>
+#include <cmsis_os2.h>
+#include <custom_runtime.h>
+#include <custom_utils.h>
+#include <ohos_types.h>
+#include <service/graphic_service.h>
+#include <ui_service.h>
 #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<UIView::OnTouchListener *>(&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