#1081 睡眠模式--早上过了睡眠模式时间,手表端睡眠模式开启的提示弹窗还是会存在
authorCaisking <3449750381@qq.com>
Thu, 31 Oct 2024 08:29:57 +0000 (16:29 +0800)
committerCaisking <3449750381@qq.com>
Thu, 31 Oct 2024 08:29:57 +0000 (16:29 +0800)
code/sdk/application/wearable/service/msg_center/include/notify/sleep_notification.h
code/sdk/application/wearable/service/msg_center/src/notify/sleep_notification.cpp

index 1d1bfe7..229fa34 100644 (file)
@@ -42,6 +42,7 @@ namespace OHOS
         void StartSleepRing();
         void OnInterrupt(int32_t type, int32_t hint) override;
 
+
     private:
         std::shared_ptr<Player> player_{nullptr};
         bool isPaused_{false};
@@ -79,6 +80,9 @@ namespace OHOS
         bool GetSleepNotifyFlag(void);
         void SetSleepNotifyFlag(bool flag);
 
+        void Sleep_StartTimer(uint32_t interval = 1000 * 4);
+        void Sleep_StopTimer(void);
+
 
     private:
         SleepViewEventListener();
index 3fa03c4..fd7f745 100644 (file)
@@ -163,6 +163,8 @@ namespace OHOS
 
         ScreenTurnOn(100000); // 亮屏操作 100000ms 10秒关闭
 
+        SleepViewEventListener::GetInstance().Sleep_StartTimer(); // 开启一个4s后弹窗自动销毁定时器
+        
         return SleepGroupView;
     }
 
@@ -211,6 +213,10 @@ namespace OHOS
     void FreeSleepView(void)
     {
         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "FreeSleepView(void)");
+
+        //销毁定时器
+        SleepViewEventListener::GetInstance().Sleep_StopTimer(); 
+        
         if (SleepGroupView != nullptr)
         {
             SleepGroupView->RemoveAll();
@@ -297,7 +303,7 @@ namespace OHOS
         if (strcmp(view.GetViewId(), NOPBUTTON) == 0)
         {
             SleepViewEventListener::GetInstance().PopInterCancelMusic();
-            SleepModel::GetInstance().Sleep_StartTimer(300000); // 开启一个5分钟的定时器
+            // SleepModel::GetInstance().Sleep_StartTimer(300000); // 开启一个5分钟的定时器
             NotificationManager::GetInstance()->StopNotify();
         }
 
@@ -319,6 +325,7 @@ namespace OHOS
             if (SLEEPSTATE::WAKE == SleepModel::GetInstance().get_sleep_state())
                 SleepViewEventListener::GetInstance().PopInterCancelMusic();
             NotificationManager::GetInstance()->StopNotify();
+            
         }
         return true;
     }
@@ -584,5 +591,85 @@ namespace OHOS
         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OnRewindToComplete test");
         // 此处实现代码处理进度控制完成的事件
     }
+ /* -------------------------------------------------------------------------- */
+
+    // 声明定时器变量
+    static osTimerId_t g_timer = NULL;
+
+    /*定时器到期时的处理*/
+    static void Sleep_TimerCallbackProc(int data)
+    {
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Sleep_TimerCallbackProc");
+        NotificationManager::GetInstance()->StopNotify();
+    }
+
+    /*
+     * @brief 回调函数
+     */
+    static void Sleep_TimerCallback(int data)
+    {
+        GraphicService::GetInstance()->PostGraphicEvent(std::bind(Sleep_TimerCallbackProc, data));
+    }
 
+    /*
+     *  @brief 开启定时器函数
+     *  @param interval 时间间隔,单位ms
+     */
+    void SleepViewEventListener::Sleep_StartTimer(uint32_t interval)
+    {
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SleepViewEventListener::Sleep_StartTimer");
+        /* -------------------------- 1.检查定时器是否已经存在,如果存在则返回 ------------------------- */
+        if (g_timer != NULL)
+        {
+            WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "CountDownModel::CountDown_StartTimer Timer already exists!!");
+            return;
+        }
+
+        /* --------------------------------- 2.创建定时器 -------------------------------- */
+        g_timer = osTimerNew((osTimerFunc_t)Sleep_TimerCallback, osTimerOnce, NULL, NULL);
+        // 检查定时器创建是否成功
+        if (g_timer == NULL)
+        {
+            WEARABLE_LOGD(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_LOGD(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 SleepViewEventListener::Sleep_StopTimer(void)
+    {
+        // 检查定时器是否存在
+        if (g_timer != NULL)
+        {
+            WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SleepViewEventListener::Sleep_StopTimer");
+            osTimerStop(g_timer);
+            // 删除定时器并置为 NULL
+            osStatus_t ret = osTimerDelete(g_timer);
+            if (ret != osOK)
+            {
+                WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "osTimerDelete failed, ret = %lx!!", ret);
+            }
+            g_timer = NULL;
+        }
+    }
 }
\ No newline at end of file