适配早安闹钟小睡功能
authorCaisking <3449750381@qq.com>
Wed, 7 Aug 2024 03:43:33 +0000 (11:43 +0800)
committerCaisking <3449750381@qq.com>
Wed, 7 Aug 2024 03:43:33 +0000 (11:43 +0800)
code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepModel.h
code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp
code/sdk/application/wearable/service/msg_center/include/notify/sleep_notification.h
code/sdk/application/wearable/service/msg_center/src/msg_center_customer.cpp
code/sdk/application/wearable/service/msg_center/src/notify/sleep_notification.cpp

index 8aec312..88c848f 100644 (file)
@@ -115,6 +115,10 @@ namespace OHOS
 
         void UpdateSleepView();
 
+        /*定时器相关*/
+        void Sleep_StartTimer(uint32_t interval);
+        void Sleep_StopTimer(void);
+
     private:
         SleepModel();
         SleepModel(const SleepModel &);
index 6d7cea9..be9e708 100644 (file)
 #include <string.h>
 #include <dirent.h>
 
+#include "broadcast_feature.h"
+#include "broadcast_service.h"
+#include <cmsis_os2.h>
+#include <service/graphic_service.h>
+#include "thread_adapter.h"
+#include "cmsis_os.h"
+
 extern "C" errcode_t uapi_get_TIME_STR(uint32_t time, char *p_time, char date_separator, char time_separator, date_format_t format);
 extern "C" int get_sleep_time_from_config_model(void);
 extern "C" int get_wake_up_time_from_config_model();
@@ -672,4 +679,75 @@ namespace OHOS
     {
         return sleepstate_;
     }
+    /* ---------------------------------- 睡眠定时器 --------------------------------- */
+
+    // 声明定时器变量
+    static osTimerId_t g_timer = NULL;
+
+    /*定时器到期时的处理*/
+    static void Sleep_TimerCallbackProc(int data)
+    {
+        // 发起新闹钟
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Sleep_TimerCallbackProc");
+        SleepModel::GetInstance().Sleep_StopTimer();        // 关闭定时器
+        CustomEventPublish(TOPIC_EVENT_SLEEPNOTE, NULL, 0); // 发送睡眠弹窗
+    }
+
+    static void Sleep_TimerCallback(int data)
+    {
+        GraphicService::GetInstance()->PostGraphicEvent(std::bind(Sleep_TimerCallbackProc, data));
+    }
+
+    void SleepModel::Sleep_StartTimer(uint32_t interval)
+    {
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepModel::Sleep_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)Sleep_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;
+    }
+
+    void SleepModel::Sleep_StopTimer(void)
+    {
+        // 检查定时器是否存在
+        if (g_timer != NULL)
+        {
+            // 删除定时器并置为 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
index c5bb150..1d1bfe7 100644 (file)
@@ -1,3 +1,9 @@
+/*** 
+ * @Date: 2024-07-02 10:15:59
+ * @LastEditors: Caisking 3449750381@qq.com
+ * @LastEditTime: 2024-08-07 10:50:45
+ * @FilePath: /FD230/code/sdk/application/wearable/service/msg_center/include/notify/sleep_notification.h
+ */
 
 #ifndef SLEEP_NOTIFICATION
 #define SLEEP_NOTIFICATION
@@ -73,6 +79,7 @@ namespace OHOS
         bool GetSleepNotifyFlag(void);
         void SetSleepNotifyFlag(bool flag);
 
+
     private:
         SleepViewEventListener();
         ~SleepViewEventListener();
index 1f8eca0..3db7fb3 100644 (file)
@@ -694,10 +694,12 @@ void MsgCenterNotifyMessageProc(const Topic topic, Request* req)
 //睡眠提醒通知
 void MsgCenterNotifySleepProc(const Topic topic, Request* req)
 {
-    unused(topic);
     unused(req);
+    unused(topic);
+        
     WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "MsgCenterNotifySleepProc %d!",SleepModel::GetInstance().get_sleep_state());
 
+    /*早安闹钟*/
     if(SleepModel::GetInstance().get_sleep_state()==SLEEPSTATE::WAKE)
     {
         NotificationManager::GetInstance()->SetNotifyPriority(PRIORITY_TWO);
@@ -707,7 +709,9 @@ void MsgCenterNotifySleepProc(const Topic topic, Request* req)
     {
         NotificationManager::GetInstance()->SetNotifyPriority(PRIORITY_THREE);
     }
+ 
     NotificationManager::GetInstance()->ShowNotify(showSleepNotify, (uint16_t)topic, true);
+    
 }
 #endif
 
index 4caa98e..9e1a8aa 100644 (file)
@@ -67,6 +67,8 @@ namespace OHOS
             return nullptr;
         }
         SleepGroupView->SetOnDragListener(dragListener);
+        // 注册释放notify
+        NotificationManager::GetInstance()->RegisterNotifyCleanupFunction(FreeSleepView);
         /*获取最新sleep*/
         SLEEPSTATE sleepsate = SleepModel::GetInstance().get_sleep_state();
         UIResourceTextId textId = STR_MORNNING;
@@ -150,7 +152,7 @@ namespace OHOS
 
         int32_t timeout = 5000; // 亮屏操作 5000ms 5秒关闭
         SendMsgToUIService(SCREEN_TURN_ON_TO_UI, 0, &timeout, sizeof(int32_t));
-        NotificationManager::GetInstance()->RegisterNotifyCleanupFunction(FreeSleepView);
+
         return SleepGroupView;
     }
 
@@ -264,16 +266,25 @@ namespace OHOS
     {
         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SleepViewEventListener::OnRelease");
         UNUSED(event);
+        /* 知道了 按键*/
         if (strcmp(view.GetViewId(), DELETEBUTTON) == 0)
         {
             NotificationManager::GetInstance()->StopNotify();
         }
+        /*关闭闹钟*/
         if (strcmp(view.GetViewId(), CLOSEBUTTON) == 0)
         {
-            SleepRingPlayStop();
-            // SetSleepNotifyFlag(false);
+            AlarmClockModel::GetInstance()->PopInterCancelMusic();
+            NotificationManager::GetInstance()->StopNotify();
+        }
+        /*小睡按键*/
+        if (strcmp(view.GetViewId(), NOPBUTTON) == 0)
+        {
+            AlarmClockModel::GetInstance()->PopInterCancelMusic();
+            SleepModel::GetInstance().Sleep_StartTimer(300000); // 开启一个5分钟的定时器
             NotificationManager::GetInstance()->StopNotify();
         }
+
         return true;
     }
     bool SleepViewEventListener::OnDrag(UIView &view, const DragEvent &event)
@@ -288,8 +299,9 @@ namespace OHOS
         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SleepViewEventListener::OnDragEnd");
         if (event.GetDragDirection() == DragEvent::DIRECTION_LEFT_TO_RIGHT)
         {
-            SleepRingPlayStop();
-            // SetSleepNotifyFlag(false);
+            // 如果是早安闹钟需要关闭闹钟
+            if (SLEEPSTATE::WAKE == SleepModel::GetInstance().get_sleep_state())
+                AlarmClockModel::GetInstance()->PopInterCancelMusic();
             NotificationManager::GetInstance()->StopNotify();
         }
         return true;
@@ -545,4 +557,5 @@ namespace OHOS
         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OnRewindToComplete test");
         // 此处实现代码处理进度控制完成的事件
     }
+
 }
\ No newline at end of file