From 794a1a4b60f72574e49ce401e8bf053e7ed89545 Mon Sep 17 00:00:00 2001 From: luozheng <1732036298@qq.com> Date: Mon, 30 Sep 2024 16:07:52 +0800 Subject: [PATCH] =?utf8?q?#880,=E5=9C=A8=E4=BA=8C=E7=BB=B4=E7=A0=81?= =?utf8?q?=E6=89=AB=E7=A0=81=E9=A1=B5=E9=9D=A2=E6=B7=BB=E5=8A=A0=20?= =?utf8?q?=E7=94=B5=E6=B1=A0=E7=99=BE=E5=88=86=E6=AF=94=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/main/src/MainPresenterSample.cpp | 7 +- .../watchQRcode/include/WatchQRCodeView.h | 11 +- .../watchQRcode/src/WatchQRCodePresenter.cpp | 7 +- .../watchQRcode/src/WatchQRCodeView.cpp | 162 ++++++++++++++++-- 4 files changed, 163 insertions(+), 24 deletions(-) diff --git a/code/sdk/application/wearable/fd230app/nativeui/main/src/MainPresenterSample.cpp b/code/sdk/application/wearable/fd230app/nativeui/main/src/MainPresenterSample.cpp index 166bf839b..d6dcb5cf5 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/main/src/MainPresenterSample.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/main/src/MainPresenterSample.cpp @@ -660,15 +660,12 @@ namespace OHOS } else //亮屏状态下每2s刷新一次下拉数据 { - MainPresenterSample::g_PullUpDownTimes = UPDATETIME; if (PullUpDownView::GetInstance() != nullptr) { PullUpDownView::GetInstance()->UpdateSignalStrengthStatusAndBatteryPosition(); } - else - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "PullUpDownView::GetInstance fail!!"); - } + if (MainPresenterSample::g_PullUpDownTimes != UPDATETIME) // 如果是亮屏后息屏再亮屏(此时息屏时间没超过 UPDATETIME)则恢复 + MainPresenterSample::g_PullUpDownTimes = UPDATETIME; } return; } diff --git a/code/sdk/application/wearable/fd230app/nativeui/watchQRcode/include/WatchQRCodeView.h b/code/sdk/application/wearable/fd230app/nativeui/watchQRcode/include/WatchQRCodeView.h index 44843ea90..5d5b47d6f 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/watchQRcode/include/WatchQRCodeView.h +++ b/code/sdk/application/wearable/fd230app/nativeui/watchQRcode/include/WatchQRCodeView.h @@ -2,7 +2,7 @@ * @Author: luozheng 1732036298@qq.com * @Date: 2024-06-12 11:44:04 * @LastEditors: luozheng 1732036298@qq.com - * @LastEditTime: 2024-07-17 16:21:55 + * @LastEditTime: 2024-09-30 15:48:53 * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/watchQRcode/include/WatchQRCodeView.h */ #ifndef WATCH_QR_CODE_VIEW_H @@ -14,6 +14,7 @@ #include "components/ui_label_ext.h" namespace OHOS { + static constexpr const char *WATCHQRCODEVIEWID = "WatchQRCodeView"; class WatchQRCodePresenter; class WatchQRCodeView : public View { @@ -28,6 +29,14 @@ namespace OHOS virtual void OnTearDownView() override; virtual void OnStop() override; + /* -------------------------------------------------------------------------- */ + static uint8_t refresh_time; + std::string get_voltage_and_version_string(void); + void update_label(void); + void start_refresh_timer(uint32_t interval = 2000); + void stop_refresh_timer(void); + /* -------------------------------------------------------------------------- */ + private: void WatchQRCodeViewInit(); bool WatchQRCodeInitViewGroup(void); diff --git a/code/sdk/application/wearable/fd230app/nativeui/watchQRcode/src/WatchQRCodePresenter.cpp b/code/sdk/application/wearable/fd230app/nativeui/watchQRcode/src/WatchQRCodePresenter.cpp index 35feb1913..f732f9804 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/watchQRcode/src/WatchQRCodePresenter.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/watchQRcode/src/WatchQRCodePresenter.cpp @@ -51,8 +51,13 @@ namespace OHOS bool WatchQRCodePresenter::OnClick(UIView &view, const ClickEvent &event) { WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WatchQRCodePresenter::OnDrag"); - UNUSED(view); UNUSED(event); + std::string viewId = view.GetViewId(); + if (viewId == WATCHQRCODEVIEWID) + { + if (view_) + view_->start_refresh_timer(); + } return true; } diff --git a/code/sdk/application/wearable/fd230app/nativeui/watchQRcode/src/WatchQRCodeView.cpp b/code/sdk/application/wearable/fd230app/nativeui/watchQRcode/src/WatchQRCodeView.cpp index 9f1d7ee64..f97b78347 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/watchQRcode/src/WatchQRCodeView.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/watchQRcode/src/WatchQRCodeView.cpp @@ -5,6 +5,9 @@ #include "ui_resource_string.h" #include "iostream" #include "string" +#include "graphic_service.h" +#include +#include namespace OHOS { @@ -51,18 +54,16 @@ namespace OHOS DeviceNameLabel = nullptr; } - if (WatchQRCodeGroupView != nullptr) - { - delete WatchQRCodeGroupView; - WatchQRCodeGroupView = nullptr; - } + delete WatchQRCodeGroupView; + WatchQRCodeGroupView = nullptr; } } extern "C" { #include "build_version.h" - +#include "battery_hal.h" + extern uint8_t battery_get_voltage(float *voltage); const char *get_version_string(void); } @@ -76,16 +77,12 @@ namespace OHOS return false; } WatchQRCodeGroupView->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight()); + WatchQRCodeGroupView->SetViewId(WATCHQRCODEVIEWID); WatchQRCodeGroupView->SetStyle(STYLE_BACKGROUND_OPA, 0xff); WatchQRCodeGroupView->SetTouchable(true); WatchQRCodeGroupView->SetDraggable(true); - UIView::OnDragListener *dragListener = static_cast(WatchQRCodePresenter::GetInstance()); - if (dragListener == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WatchQRCodeView::dragListener new fail!"); - return false; - } - WatchQRCodeGroupView->SetOnDragListener(dragListener); + WatchQRCodeGroupView->SetOnDragListener(presenter_); + WatchQRCodeGroupView->SetOnClickListener(presenter_); return true; } @@ -137,10 +134,8 @@ namespace OHOS WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WatchQRCodeView::DeviceNameLabel new label fail"); return false; } - std::string temp = get_version_string(); - std::string version_string = "V" + temp; - DeviceNameLabel->SetText(version_string.c_str()); - DeviceNameLabel->SetPosition(83, 402, 300, 35); + DeviceNameLabel->SetText(get_voltage_and_version_string().c_str()); + DeviceNameLabel->SetPosition(83, 390, 300, 35); DeviceNameLabel->SetStyle(STYLE_TEXT_FONT, 30); DeviceNameLabel->SetStyle(STYLE_LINE_HEIGHT, 35); DeviceNameLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); @@ -174,6 +169,7 @@ namespace OHOS { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WatchQRCodeView::OnStart"); View::OnStart(); + start_refresh_timer(); WatchQRCodeViewInit(); /* --------------------------------- 添加按键监听 --------------------------------- */ RootView::OnKeyActListener *keyactListener = (RootView::OnKeyActListener *)OHOS::WatchQRCodePresenter::GetInstance(); @@ -206,10 +202,142 @@ namespace OHOS { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WatchQRCodeView::OnStop"); View::OnStop(); + stop_refresh_timer(); RootView::GetInstance()->RemoveAll(); RootView::GetInstance()->ClearOnKeyActListener(); ClearAll(); return; } + std::string WatchQRCodeView::get_voltage_and_version_string(void) + { + // 电量: + float voltage = 0.0F; + uint8_t value = 0; + value = battery_get_voltage(&voltage); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "battery_get_voltage = %d", value); + + // 版本号: + std::string temp = get_version_string(); + std::string version_string = "V" + temp; + std::string str = "bat:" + std::to_string(value) + "%" + " " + version_string; + return str; + } + + void WatchQRCodeView::update_label(void) + { + if (DeviceNameLabel) + { + std::string str = get_voltage_and_version_string(); + DeviceNameLabel->SetText(str.c_str()); + DeviceNameLabel->Invalidate(); + } + } + + // 声明定时器变量 + static osTimerId_t g_timer = nullptr; + static uint8_t UPDATETIME_OUT = 30; // 30秒 + uint8_t WatchQRCodeView::refresh_time = UPDATETIME_OUT; + /*定时器到期时的处理*/ + static void QRCodeView_TimerCallbackProc(int data) + { + if (uapi_get_screen_state() == 0) // 如果在亮屏状态 + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "uapi_get_screen_state() == 0"); + if (WatchQRCodeView::GetInstance()) + { + WatchQRCodeView::GetInstance()->update_label(); + } + if (WatchQRCodeView::refresh_time != UPDATETIME_OUT) // 如果是亮屏后息屏再亮屏(此时息屏时间没超过 UPDATETIME_OUT)则恢复 + { + WatchQRCodeView::refresh_time = UPDATETIME_OUT; + } + } + else + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "uapi_get_screen_state() == 1"); + WatchQRCodeView::refresh_time -= 2; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%d", WatchQRCodeView::refresh_time); + if (WatchQRCodeView::refresh_time <= 0) // 息屏状态下超时删除定时器 + { + if (WatchQRCodeView::GetInstance()) + { + GraphicService::GetInstance()->PostGraphicEvent(std::bind(&WatchQRCodeView::stop_refresh_timer, WatchQRCodeView::GetInstance())); + } + } + else + { + if (WatchQRCodeView::GetInstance()) + { + WatchQRCodeView::GetInstance()->update_label(); + } + } + } + return; + } + + /* + * @brief 回调函数 + */ + static void QRCodeView_TimerCallback(int data) + { + GraphicService::GetInstance()->PostGraphicEvent(std::bind(QRCodeView_TimerCallbackProc, data)); + } + + /* + * @brief 开启定时器函数 + * @param interval 时间间隔,单位ms + */ + void WatchQRCodeView::start_refresh_timer(uint32_t interval) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "start_refresh_timer"); + /* -------------------------- 1.检查定时器是否已经存在,如果存在则返回 ------------------------- */ + if (g_timer != nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " Timer already exists!!"); + return; + } + /* --------------------------------- 2.创建定时器 -------------------------------- */ + g_timer = osTimerNew((osTimerFunc_t)QRCodeView_TimerCallback, osTimerPeriodic, nullptr, nullptr); + // 检查定时器创建是否成功 + if (g_timer == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Timer create failed!!"); + return; + } + /* ----------------------------- 3.将毫秒转换为系统时钟节拍数 ---------------------------- */ + int tick = osMs2Tick(interval); + /* --------------------------------- 4.启动定时器 -------------------------------- */ + osStatus_t ret = osTimerStart(g_timer, tick); + /* -------------------- 5.检查定时器启动是否成功,如果启动失败,删除定时器并置为 nullptr -------------------- */ + if (ret != osOK) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "osTimerDelete failed, ret = %lx!!", ret); + if (g_timer != nullptr) + { + ret = osTimerDelete(g_timer); + } + g_timer = nullptr; + return; + } + return; + } + + void WatchQRCodeView::stop_refresh_timer(void) + { + // 检查定时器是否存在 + if (g_timer != nullptr) + { + osTimerStop(g_timer); + // 删除定时器并置为 nullptr + osStatus_t ret = osTimerDelete(g_timer); + if (ret != osOK) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "osTimerDelete failed, ret = %lx!!", ret); + } + g_timer = nullptr; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "g_timer stop success"); + refresh_time = UPDATETIME_OUT; + } + } } -- 2.17.1