倒计时优化 3265/3
authorluozheng <1732036298@qq.com>
Thu, 12 Mar 2026 03:56:55 +0000 (11:56 +0800)
committerluozheng <1732036298@qq.com>
Thu, 12 Mar 2026 03:56:55 +0000 (11:56 +0800)
code/sdk/application/wearable/n9/nativeapp/nativeui/countdown/include/CountDownModel.h
code/sdk/application/wearable/n9/nativeapp/nativeui/countdown/include/MainCountDownPresenter.h
code/sdk/application/wearable/n9/nativeapp/nativeui/countdown/src/CountDownModel.cpp
code/sdk/application/wearable/n9/nativeapp/nativeui/countdown/src/MainCountDownPresenter.cpp

index 41c1aa3..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
@@ -115,13 +120,13 @@ namespace OHOS
         bool GetisPaused(void) const {\r
             return isPaused;\r
         }\r
-        uint32_t get_current_time();//获取当前系统时间戳\r
+        uint32_t get_current_time(); // 获取当前系统时间戳(ms)\r
     public:\r
         static int g_CountDownTimes;\r
         static int totalSeconds;    //要到计时总秒数\r
-        static uint32_t startTime;  //开始时间戳\r
-        static uint32_t pauseTime;  //暂停时间戳\r
-        static uint32_t endTime;    //结束时间戳\r
+        static uint32_t startTime;  // 开始时间戳(ms)\r
+        static uint32_t pauseTime;  // 暂停时间戳(ms)\r
+        static uint32_t endTime;    // 结束时间戳(ms)\r
 \r
     private:\r
         CountDownModel() {}\r
index 226a9c0..fcae7c2 100644 (file)
@@ -26,7 +26,7 @@ namespace OHOS
         COUNTDOWN_MAX,\r
     } CountDownViewId;\r
 \r
-    static constexpr const int COUNTDOWN_TIMER_DURATION = 1000; // 计时器的定时器周期\r
+    static constexpr const int COUNTDOWN_TIMER_DURATION = 500; // 计时器的定时器周期(ms)\r
     class MainCountDownView;\r
     class MainCountDownPresenter : public Presenter<MainCountDownView>,\r
                                    public UIView::OnClickListener,\r
index f326026..97e00cf 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
@@ -459,22 +461,25 @@ namespace OHOS
         bool isRunning = CountDownModel::GetInstance()->GetisRunning();\r
         bool isPaused = CountDownModel::GetInstance()->GetisPaused();\r
 \r
-        if (isRunning &&!isPaused) \r
+        if (isRunning && !isPaused) \r
         {\r
-            uint32_t currentTime =  CountDownModel::GetInstance()->get_current_time();\r
-            CountDownModel::g_CountDownTimes = CountDownModel::totalSeconds - (currentTime - CountDownModel::startTime);\r
-            WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "当前时间戳:%u,倒计时剩余时间:%d", currentTime ,CountDownModel::g_CountDownTimes);\r
-            if (CountDownModel::g_CountDownTimes <= 0 && currentTime == CountDownModel::endTime) {\r
+            uint32_t currentTime = CountDownModel::GetInstance()->get_current_time();\r
+            if (currentTime >= CountDownModel::endTime) {\r
+                CountDownModel::g_CountDownTimes = 0;\r
                 // 这里可以添加倒计时结束后的操作,比如发出提示音等\r
                 GraphicService::GetInstance()->PostGraphicEvent(std::bind(CountDown_TimerCallbackProc, data));\r
+                return;\r
             }\r
+            uint32_t remainingMs = CountDownModel::endTime - currentTime;\r
+            CountDownModel::g_CountDownTimes = static_cast<int32_t>((remainingMs + 999) / 1000); // ceil to seconds\r
+            WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "当前时间戳(ms):%u,倒计时剩余时间(s):%d", currentTime, CountDownModel::g_CountDownTimes);\r
         }\r
     }\r
 \r
     uint32_t CountDownModel::get_current_time()\r
     {\r
         uint32_t time;\r
-        if (uapi_get_timestampe_ss(&time) != ERRCODE_SUCC) {\r
+        if (uapi_get_timestampe_ms(&time) != ERRCODE_SUCC) {\r
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to get timestamp");\r
             return 0;\r
         }\r
@@ -487,8 +492,8 @@ namespace OHOS
         isPaused = false;\r
         CountDownModel::totalSeconds = CountDownModel::g_CountDownTimes;\r
         CountDownModel::startTime = get_current_time();         //获取开始时间戳\r
-        CountDownModel::endTime = CountDownModel::startTime + g_CountDownTimes; //获取结束时间戳\r
-        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "startTime:%u,endTime:%u",CountDownModel::startTime,CountDownModel::endTime);\r
+        CountDownModel::endTime = CountDownModel::startTime + static_cast<uint32_t>(g_CountDownTimes) * 1000; //获取结束时间戳(ms)\r
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "startTime(ms):%u,endTime(ms):%u",CountDownModel::startTime,CountDownModel::endTime);\r
 \r
     }\r
     \r
@@ -506,8 +511,14 @@ namespace OHOS
             uint32_t currentTime = get_current_time();\r
             uint32_t pauseDuration = currentTime - CountDownModel::pauseTime;\r
             CountDownModel::endTime = CountDownModel::endTime + pauseDuration;\r
-            CountDownModel::totalSeconds = CountDownModel::totalSeconds + pauseDuration;\r
-            WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "startTime:%u,pauseDuration:%u,endTime:%u",CountDownModel::startTime,pauseDuration,CountDownModel::endTime);\r
+            if (currentTime >= CountDownModel::endTime) {\r
+                CountDownModel::g_CountDownTimes = 0;\r
+            } else {\r
+                uint32_t remainingMs = CountDownModel::endTime - currentTime;\r
+                CountDownModel::g_CountDownTimes = static_cast<int32_t>((remainingMs + 999) / 1000); // ceil to seconds\r
+            }\r
+            WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "startTime(ms):%u,pauseDuration(ms):%u,endTime(ms):%u",\r
+                CountDownModel::startTime, pauseDuration, CountDownModel::endTime);\r
         }\r
     }\r
     \r
@@ -622,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
index ba3ceb2..639caad 100644 (file)
@@ -269,6 +269,10 @@ namespace OHOS
             p_CountDownModel->SetCountDownTimerIsPause(false);\r
             p_CountDownModel->CountDown_StartTimer(COUNTDOWN_TIMER_DURATION);\r
             p_CountDownModel->CountDown_ResumeTimer(COUNTDOWN_TIMER_DURATION);\r
+            if (p_StartCountDownView->GetShowCountDownTimelabel()) {\r
+                p_StartCountDownView->GetShowCountDownTimelabel()->SetText(\r
+                    CountDownModel::GetInstance()->DisplayCountDownDuration());\r
+            }\r
             /*视图恢复*/\r
             p_StartCountDownView->SetImageAndLabel(true);\r
             p_StartCountDownView->ShowEndView(false);\r