倒计时到期添加震动 3261/1 3261/head 3262/target/1
authorluozheng <1732036298@qq.com>
Tue, 10 Mar 2026 07:11:16 +0000 (15:11 +0800)
committerluozheng <1732036298@qq.com>
Tue, 10 Mar 2026 07:11:16 +0000 (15:11 +0800)
code/sdk/application/wearable/fd272/nativeapp/nativeui/countdown/include/CountDownModel.h
code/sdk/application/wearable/fd272/nativeapp/nativeui/countdown/src/CountDownModel.cpp

index 070b942..41810b9 100644 (file)
@@ -68,6 +68,11 @@ namespace OHOS
     public:\r
         static CountDownModel *GetInstance(); // shared_ptr:当没有引用指向这个实例时会自动释放内存\r
 \r
+        //震动定时器\r
+        void CountDown_VibratorTimerStart(uint32_t interval = 1000);\r
+        void CountDown_VibratorTimerStop(void);\r
+        bool isNeedVibratorTimerStop();\r
+\r
         bool CountDownTimerIsEnd(); // 计时器是否结束\r
         bool GetChanageToStartCountDownView();\r
         void SetChanageToStartCountDownView(bool val);\r
index 69d9c59..cc66131 100644 (file)
@@ -21,6 +21,7 @@
 #include <custom_utils.h>\r
 #include <errcode.h>\r
 #include <custom_save_runtime.h>\r
+#include "vibrator_service.h"\r
 namespace OHOS\r
 {\r
     /* -------------------------------------------------------------------------- */\r
@@ -383,6 +384,7 @@ namespace OHOS
             CountDown_StopTimer();                                 // 停止定时器\r
             uapi_set_screen_on_api();                              // 点亮屏幕\r
             CountDownModel::GetInstance()->RequestStartRingtone(); // 播放铃声\r
+            CountDownModel::GetInstance()->CountDown_VibratorTimerStart(); // 触发震动\r
 \r
             StartCountDownView::GetInstance()->GetPlayOrPauseToggleButton()->SetVisible(false);\r
             p_StartCountDownView->GetRepeatButton()->SetVisible(true);\r
@@ -631,6 +633,92 @@ namespace OHOS
         }\r
     }\r
 \r
+    static osTimerId_t g_vibrator_timer = nullptr;\r
+    static void CountDown_VibratorTimerCallbackProc(void *arg)\r
+    {\r
+        WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "CountDown_VibratorTimerCallbackProc");\r
+        if(CountDownModel::GetInstance()->isNeedVibratorTimerStop())\r
+        {\r
+            CountDownModel::GetInstance()->CountDown_VibratorTimerStop();\r
+            return;\r
+        }\r
+        // 触发震动\r
+        custom_VibratorFunc_int(0);\r
+        custom_VibratorFunc_int(0);\r
+    }\r
+\r
+    static void CountDown_VibratorTimerCallback(void *arg)\r
+    {\r
+        GraphicService::GetInstance()->PostGraphicEvent(std::bind(CountDown_VibratorTimerCallbackProc, arg));\r
+    }\r
+\r
+    void CountDownModel::CountDown_VibratorTimerStart(uint32_t interval)\r
+    {\r
+        // 检查定时器是否存在\r
+        if (g_vibrator_timer != nullptr)\r
+        {\r
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDown_VibratorTimerStart Timer already exists!!");\r
+            return;\r
+        }\r
+        g_vibrator_timer = osTimerNew((osTimerFunc_t)CountDown_VibratorTimerCallback, osTimerPeriodic, nullptr, nullptr);\r
+        // 检查定时器创建是否成功\r
+        if (g_vibrator_timer == nullptr)\r
+        {\r
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDown_VibratorTimerStart Timer create failed!!");\r
+            return;\r
+        }\r
+\r
+        int tick = osMs2Tick(interval);\r
+        osStatus_t ret = osTimerStart(g_vibrator_timer, tick);\r
+        if (ret != osOK)\r
+        {\r
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[CountDown_VibratorTimerStart] osTimerStart failed, ret = %lx!!", ret);\r
+            if (g_vibrator_timer != nullptr)\r
+            {\r
+                ret = osTimerDelete(g_vibrator_timer);\r
+            }\r
+            g_vibrator_timer = nullptr;\r
+            return;\r
+        }\r
+        return;\r
+    }\r
+\r
+    void CountDownModel::CountDown_VibratorTimerStop(void)\r
+    {\r
+        // 检查定时器是否存在\r
+        if (g_vibrator_timer != nullptr)\r
+        {\r
+            osTimerStop(g_vibrator_timer);\r
+            // 删除定时器并置为 nullptr\r
+            osStatus_t ret = osTimerDelete(g_vibrator_timer);\r
+            if (ret != osOK)\r
+            {\r
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDown_VibratorTimerStop osTimerDelete failed, ret = %lx!!", ret);\r
+            }\r
+            g_vibrator_timer = nullptr;\r
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "stop vibrator timer success!!");\r
+        }\r
+    }\r
+\r
+    bool CountDownModel::isNeedVibratorTimerStop()\r
+    {\r
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "isNeedVibratorTimerStop");\r
+        if (playerCtr.get() == nullptr)\r
+        {\r
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[CountDownModel] playerCtr.get() == nullptr");\r
+            return OHOS_FAILURE;\r
+        }\r
+        int32_t state = 0;\r
+        playerCtr->GetPlayerState(state);\r
+        if (state != PlayerStates::PLAYER_STARTED)\r
+        {\r
+            return true;\r
+        }\r
+        return false;\r
+    }\r
+\r
+\r
+\r
     /* ----------------------- CountDownRingPlayerListener ---------------------- */\r
 \r
     void CountDownRingPlayerListener::SetOwner(std::shared_ptr<Player> player)\r