From f8c1eb0855aac92ed5eee9c76ff051cd7e31c99f Mon Sep 17 00:00:00 2001 From: Caisking <3449750381@qq.com> Date: Wed, 7 Aug 2024 11:43:33 +0800 Subject: [PATCH] =?utf8?q?=E9=80=82=E9=85=8D=E6=97=A9=E5=AE=89=E9=97=B9?= =?utf8?q?=E9=92=9F=E5=B0=8F=E7=9D=A1=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/sleep/include/SleepModel.h | 4 + .../nativeui/sleep/src/SleepModel.cpp | 78 +++++++++++++++++++ .../include/notify/sleep_notification.h | 7 ++ .../msg_center/src/msg_center_customer.cpp | 6 +- .../src/notify/sleep_notification.cpp | 23 ++++-- 5 files changed, 112 insertions(+), 6 deletions(-) diff --git a/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepModel.h b/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepModel.h index 8aec31223..88c848f37 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepModel.h +++ b/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepModel.h @@ -115,6 +115,10 @@ namespace OHOS void UpdateSleepView(); + /*定时器相关*/ + void Sleep_StartTimer(uint32_t interval); + void Sleep_StopTimer(void); + private: SleepModel(); SleepModel(const SleepModel &); diff --git a/code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp index 6d7cea9e4..be9e7086e 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp @@ -33,6 +33,13 @@ #include #include +#include "broadcast_feature.h" +#include "broadcast_service.h" +#include +#include +#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 diff --git a/code/sdk/application/wearable/service/msg_center/include/notify/sleep_notification.h b/code/sdk/application/wearable/service/msg_center/include/notify/sleep_notification.h index c5bb150f0..1d1bfe78e 100644 --- a/code/sdk/application/wearable/service/msg_center/include/notify/sleep_notification.h +++ b/code/sdk/application/wearable/service/msg_center/include/notify/sleep_notification.h @@ -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(); diff --git a/code/sdk/application/wearable/service/msg_center/src/msg_center_customer.cpp b/code/sdk/application/wearable/service/msg_center/src/msg_center_customer.cpp index 1f8eca030..3db7fb31c 100644 --- a/code/sdk/application/wearable/service/msg_center/src/msg_center_customer.cpp +++ b/code/sdk/application/wearable/service/msg_center/src/msg_center_customer.cpp @@ -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 diff --git a/code/sdk/application/wearable/service/msg_center/src/notify/sleep_notification.cpp b/code/sdk/application/wearable/service/msg_center/src/notify/sleep_notification.cpp index 4caa98e1b..9e1a8aa61 100644 --- a/code/sdk/application/wearable/service/msg_center/src/notify/sleep_notification.cpp +++ b/code/sdk/application/wearable/service/msg_center/src/notify/sleep_notification.cpp @@ -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 -- 2.17.1