From f158d64e74c978e5c6dbfb8e0230454af6e23dbd Mon Sep 17 00:00:00 2001 From: Caisking <3449750381@qq.com> Date: Thu, 31 Oct 2024 16:29:57 +0800 Subject: [PATCH] =?utf8?q?#1081=20=E7=9D=A1=E7=9C=A0=E6=A8=A1=E5=BC=8F--?= =?utf8?q?=E6=97=A9=E4=B8=8A=E8=BF=87=E4=BA=86=E7=9D=A1=E7=9C=A0=E6=A8=A1?= =?utf8?q?=E5=BC=8F=E6=97=B6=E9=97=B4=EF=BC=8C=E6=89=8B=E8=A1=A8=E7=AB=AF?= =?utf8?q?=E7=9D=A1=E7=9C=A0=E6=A8=A1=E5=BC=8F=E5=BC=80=E5=90=AF=E7=9A=84?= =?utf8?q?=E6=8F=90=E7=A4=BA=E5=BC=B9=E7=AA=97=E8=BF=98=E6=98=AF=E4=BC=9A?= =?utf8?q?=E5=AD=98=E5=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../include/notify/sleep_notification.h | 4 + .../src/notify/sleep_notification.cpp | 89 ++++++++++++++++++- 2 files changed, 92 insertions(+), 1 deletion(-) 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 1d1bfe78e..229fa3409 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 @@ -42,6 +42,7 @@ namespace OHOS void StartSleepRing(); void OnInterrupt(int32_t type, int32_t hint) override; + private: std::shared_ptr 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(); 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 3fa03c4dd..fd7f745f4 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 @@ -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 -- 2.17.1