+/***
+ * @Date: 2024-07-17 14:09:04
+ * @LastEditors: Caisking 3449750381@qq.com
+ * @LastEditTime: 2024-07-20 15:26:53
+ * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/clock/include/MainClockView.h
+ */
/*
* Copyright (c) CompanyNameMagicTag 2021-2021. All rights reserved.
* Description: MainClockView
#include <string>
#include <time.h>
#include "View.h"
-#include "components/ui_label.h"
-#include "components/root_view.h"
#include "components/ui_view_group.h"
#include "components/ui_image_view.h"
+#include "components/ui_label.h"
+#include "components/ui_label_ext.h"
#include "components/ui_label_button.h"
+#include "components/ui_box_progress.h"
+#include "components/ui_image_animator.h"
+#include "components/root_view.h"
+
#include "components/ui_sweep_clock.h"
#include "BaseView.h"
#include "ChangeSliceListener.h"
#include "time.h"
#include "sys/time.h"
-namespace OHOS {
-class MainClockView : public UICardPage {
-public:
- MainClockView();
- virtual ~MainClockView();
- static MainClockView *GetInstance(void);
- bool InitView(void);
- void PreLoad(void) override;
- void UnLoad(void) override;
- void RefreshStep(int step);
- void RefreshHeartRate(int value);
- void ClockAnimatorStart(void);
- void ClockAnimatorStop(void);
- void UpdateTime(void);
+namespace OHOS
+{
+ /*表盘颜色配置*/
+ enum ColorTypeSet
+ {
+ COLOR_WHITE = 0,
+ COLOR_BLACK,
+ COLOR_GRAY,
+ COLOR_GREEN,
+ COLOR_ORANGE,
+ COLOR_BLUE,
+ COLOR_DEFAULT = COLOR_WHITE // 默认颜色
+ };
+ /*表盘电量配置*/
+ enum BatteryDisplayType
+ {
+ BATTERY_DISPLAY_NONE = 0, // 不显示图标
+ BATTERY_DISPLAY_BLACK, // 显示黑色
+ BATTERY_DISPLAY_WHITE, // 显示白色
+ BATTERY_DISPLAY_DEFAULT = BATTERY_DISPLAY_WHITE // 默认:白色
+ };
+ /*表盘时间配置*/
+ enum TimeDisplayPosition
+ {
+ TIME_DISPLAY_ABOVE = 0, // 显示在表盘上方
+ TIME_DISPLAY_BELOW, // 显示在表盘下方
+ TIME_DISPLAY_DEFAULT = TIME_DISPLAY_BELOW // 默认:下方
+ };
+
+ class MainClockView : public UICardPage
+ {
+ public:
+ MainClockView();
+ virtual ~MainClockView();
+ static MainClockView *GetInstance(void);
+ bool InitView(void);
+ void PreLoad(void) override;
+ void UnLoad(void) override;
+ /*刷新数据*/
+ void RefreshStep(int step);
+ void RefreshHeartRate(int value);
+ void ClockAnimatorStart(void);
+ void ClockAnimatorStop(void);
+ /*更新数据*/
+ void UpdateTime(void);
+ void UpdataBattery(void);
+ void SetViewColor(int64_t color);
+ void UpdateBatteryColor(void);
+ /*配置文件*/
+ void SetDialPath(std::string dialpath);
+ bool InitialConfig(int Id);
+ /*配置函数*/
+ bool InitBackground(const char *filename);
+ bool InitTimeView(TimeDisplayPosition type); // 时间
+ bool InitBatteryView(BatteryDisplayType type); // 电量
+ bool InitColor(ColorTypeSet type); // 颜色
+ private:
+ void InitStepValue();
+ bool InitLabelView(void);
+ bool InitCenterCircleView(void);
+ bool InitHandView(void);
+ void InitDateTime(void);
+ bool InitClock(void);
+ bool LoadClockImage(Image *view, uint32_t resId);
+ bool LoadClockImage(UIImageView *view, uint32_t resId);
+
+ /*表盘背景*/
+ UIImageView *curFace{nullptr};
+ /*时间相关*/
+ UILabel *timerlabel{nullptr};
+ UILabel *weekLabel{nullptr};
+ UILabel *datelabel{nullptr};
+ /*电量相关*/
+ UIImageView *batteryiamge{nullptr};
+ UIBoxProgress *batteryprogress{nullptr};
+ UILabel *batterylabel{nullptr};
+ UIImageView *batteryicon{nullptr};
+ /*数据相关*/
+ UILabel *stepLabel{nullptr};
+ UILabel *heartlabel{nullptr};
+
+ /*指针表盘相关 暂不使用*/
+ UISweepClock *mainSweepClock{nullptr};
+ Image *secondHand{nullptr};
+ Image *minuteHand{nullptr};
+ Image *hourHand{nullptr};
+ UIImageView *heartRate{nullptr};
+ Image *hand24Hour{nullptr};
+ UIImageView *weekDay{nullptr};
+ UIImageView *progress{nullptr};
+ Image *bigCircle{nullptr};
+ Image *medCircle{nullptr};
+ Image *smallCircle{nullptr};
+
+ bool viewiInitStatus{false};
+ bool resLoadStatus{false};
-private:
- void InitStepValue();
- bool InitLabelView(void);
- bool InitCenterCircleView(void);
- bool InitHandView(void);
- void InitDateTime(void);
- bool InitSweepClock(void);
- bool LoadClockImage(Image *view, uint32_t resId);
- bool LoadClockImage(UIImageView *view, uint32_t resId);
- UIImageView *curFace{nullptr};
- UILabelButton *stepLabel{nullptr};
- UILabel *timerlabel{nullptr};
- UILabel *weekLabel{nullptr};
- UILabel *datelabel{nullptr};
- UISweepClock *mainSweepClock{nullptr};
- Image *secondHand{nullptr};
- Image *minuteHand{nullptr};
- Image *hourHand{nullptr};
- UIImageView *heartRate{nullptr};
- Image *hand24Hour{nullptr};
- UIImageView *weekDay{nullptr};
- UIImageView *progress{nullptr};
- Image *bigCircle{nullptr};
- Image *medCircle{nullptr};
- Image *smallCircle{nullptr};
- bool viewiInitStatus{false};
- bool resLoadStatus{false};
-};
+ std::string dialFile; // 表盘背景文件名
+ ColorTypeSet colorType = COLOR_DEFAULT; // 颜色类型
+ BatteryDisplayType batterySetting = BATTERY_DISPLAY_DEFAULT; // 电池设置
+ TimeDisplayPosition timePosition = TIME_DISPLAY_DEFAULT; // 时间位置
+ int64_t color_ = Color::White().full; // 电量颜色
+ int32_t power_ = -1; // 电量
+ bool isBatteryIconVisible{false}; // 电池暂停
+ };
} // namespace OHOS
#endif // MAIN_CLOCK_VIEW_H
+/***
+ * @Date: 2024-07-17 14:09:09
+ * @LastEditors: Caisking 3449750381@qq.com
+ * @LastEditTime: 2024-07-22 09:33:10
+ * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/clock/src/MainClockView.cpp
+ */
/*
* Copyright (c) CompanyNameMagicTag 2021-2021. All rights reserved.
* Description: MainClockView
#include <iostream>
#include <sstream>
#include <iomanip>
+#include <string>
#include "custom_runtime.h"
#include "main/include/PullUpDownView.h"
+#include "battery_hal.h"
#include "message/include/MessageControlView.h"
-std::string weekdays[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
+extern uint8_t battery_get_voltage(float *voltage);
+
+#define TIMEVIEW_UP 0
+#define TIMEVIEW_DOWN 1
+
+std::string Enweekdays[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
+std::string Cnweekdays[] = {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
+
+static constexpr const char *CHAGE_BATTERY = "/user/res/Battery/Charge_Icon.bin";
+static constexpr const char *WHITE_BATTERY = "/user/res/Battery/Battery-E.bin";
+static constexpr const char *BLACK_BATTERY = "/user/res/Battery/Charge_Icon.bin";
+
+// 定义一些常量
+constexpr int HOURS_IN_DAY = 24;
+constexpr int MINUTES_IN_HOUR = 60;
+constexpr int HOUR_FORMAT_12 = 12;
+constexpr int BATTERY_LOW_THRESHOLD = 10;
+constexpr int BATTERY_MEDIUM_THRESHOLD = 20;
+
namespace OHOS
{
- static MainClockView *g_pMainClockView = nullptr;
-
+#if 0
static constexpr uint16_t FONT_DEFAULT_SIZE = 25;
static constexpr uint16_t FONT_TEXT_SIZE = 24;
static constexpr uint16_t STEP_LABEL_X = 100;
static constexpr uint16_t SECOND_POINT_POSITION_Y = 16;
static constexpr uint16_t POINT_CENTER_X = 25;
static constexpr uint16_t POINT_CENTER_Y = 211;
- static constexpr uint16_t STEP_CHAR_NUM = 255;
static constexpr int16_t STEP_LABLE_WIDTH = 75;
static constexpr int16_t STEP_LABLE_SIZE = 35;
static constexpr uint16_t WEEKLABEL_POSITION_X = 181;
static constexpr int16_t MIN_TO_HOUR = 60;
static constexpr int16_t HOURLY_SYSTEM = 24;
static constexpr int16_t INTERVAL = 30;
-
+#endif
+ static constexpr uint16_t STEP_CHAR_NUM = 255;
+ static MainClockView *g_pMainClockView = nullptr;
MainClockView::MainClockView()
{
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MainClockView::MainClockView");
MainClockView::~MainClockView()
{
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MainClockView::~MainClockView");
- /* -------------------------------------------------------------------------- */
delete curFace; // 背景
curFace = nullptr;
delete timerlabel; // 时间
timerlabel = nullptr;
delete weekLabel; // 星期
weekLabel = nullptr;
- delete datelabel; // 日期
- datelabel = nullptr;
- /* -------------------------------------------------------------------------- */
-#ifdef DEFULTDIAL
- mainSweepClock->RemoveAll();
- delete stepLabel;
+ delete batteryiamge; // 电量框
+ batteryiamge = nullptr;
+ delete batteryprogress; // 电量进度条
+ batteryprogress = nullptr;
+ delete batterylabel; // 电量label
+ batterylabel = nullptr;
+ delete batteryicon; // 充电图标
+ batteryicon = nullptr;
+ delete stepLabel; // 步数
stepLabel = nullptr;
- delete secondHand;
- secondHand = nullptr;
- delete minuteHand;
- minuteHand = nullptr;
- delete hourHand;
- hourHand = nullptr;
- delete heartRate;
- heartRate = nullptr;
- delete hand24Hour;
- hand24Hour = nullptr;
- delete weekDay;
- weekDay = nullptr;
- delete progress;
- progress = nullptr;
- delete bigCircle;
- bigCircle = nullptr;
- delete medCircle;
- medCircle = nullptr;
- delete smallCircle;
- smallCircle = nullptr;
- Remove(mainSweepClock);
- delete mainSweepClock;
- mainSweepClock = nullptr;
+ delete heartlabel; // 心率
+ heartlabel = nullptr;
+
g_pMainClockView = nullptr;
-#endif
}
MainClockView *MainClockView::GetInstance(void)
{
return g_pMainClockView;
}
+ /*设置表盘背景*/
+ void MainClockView::SetDialPath(std::string dialpath)
+ {
+ if (dialpath.empty())
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Error: The specified dial path does not exist:");
+ return;
+ }
+ dialFile.clear();
+ dialFile = dialpath;
+ }
+ /*
+ 从配置文件读取设置
+ 配置文件名:表盘ID
+ 第一个字节:电池图标样式
+ 第二个字节:时间label位置
+ 第三个字节:字体颜色
+ */
+ bool MainClockView::InitialConfig(int Id)
+ {
+ std::string filename = APP_CUSTOM_DIAL_PATH + std::to_string(Id) + ".bin";
+
+ // 打开文件
+ std::ifstream configFile(filename, std::ios::binary);
+ if (!configFile.is_open())
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Unable to open config file: %s", filename.c_str());
+ return false; // 文件打开失败
+ }
+
+ // 读取配置
+ char settings[3]; // 读取3个字节
+ configFile.read(settings, sizeof(settings));
+ if (configFile.gcount() != sizeof(settings))
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to read config file or insufficient file content!");
+ configFile.close();
+ return false; // 读取失败
+ }
- bool MainClockView::InitSweepClock(void)
+ // 为成员变量赋值
+ batterySetting = static_cast<BatteryDisplayType>(settings[0]); // 第一个字节:电池图标样式
+ timePosition = static_cast<TimeDisplayPosition>(settings[1]); // 第二个字节:时间label位置
+ colorType = static_cast<ColorTypeSet>(settings[2]); // 第三个字节:字体颜色
+
+ // 关闭文件
+ configFile.close();
+
+ return true; // 成功读取配置
+ }
+
+ bool MainClockView::InitClock(void)
+ {
+ InitBackground(dialFile.c_str());
+ InitTimeView(timePosition);
+ InitBatteryView(batterySetting);
+ InitColor(colorType);
+ return true;
+ }
+ /*初始化表盘背景*/
+ bool MainClockView::InitBackground(const char *filename)
{
- // 背景
curFace = new UIImageView();
if (curFace == nullptr)
{
return false;
}
curFace->SetPosition(0, 0);
- ImageInfo *image = ImageCacheManager::GetInstance().LoadSingleRes(DEFAULT_DIALBACK_PATH, false);
+ ImageInfo *image = ImageCacheManager::GetInstance().LoadSingleRes(filename, false);
if (image == nullptr)
{
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "LoadAllDialFromRam loadimage %s fail",
- DEFAULT_DIALBACK_PATH);
+ filename);
+ curFace->SetSrc(DEFAULT_DIALBACK_PATH);
+ Add(curFace);
return false;
}
- #ifndef CUSTOM_Z1
curFace->SetSrc(image);
- #endif
Add(curFace);
+ return true;
+ }
+
+ /*
+ 时间显示 [时间 日期 星期]
+ 0 显示在表盘上方
+ 1 显示在表盘下方
- // 时间
- timerlabel = new UILabel();
- if (timerlabel == nullptr)
+ 默认:下方
+ */
+ bool MainClockView::InitTimeView(TimeDisplayPosition type)
+ {
+ // 创建UILabel的助手函数
+ auto createLabel = [](UILabel *&label, const std::string &labelName) -> bool
{
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "timerlabel new fail");
- return false;
+ label = new UILabel();
+ if (label == nullptr)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%s creation failed", labelName.c_str());
+ return false;
+ }
+ label->SetStyle(STYLE_BACKGROUND_COLOR, OPA_TRANSPARENT);
+ label->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);
+ label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
+ label->SetStyle(STYLE_BACKGROUND_COLOR, OPA_OPAQUE);
+ return true;
+ };
+
+ // 创建时间、星期、日期标签
+ if (!createLabel(timerlabel, "timerlabel") ||
+ !createLabel(weekLabel, "weekLabel") ||
+ !createLabel(datelabel, "datelabel"))
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Label creation failed, exiting InitTimeView");
+ return false; // 分配失败
+ }
+
+ // 设置标签的位置和字体大小
+ switch (type)
+ {
+ case TIME_DISPLAY_ABOVE:
+ timerlabel->SetPosition(104, 98, 200, 60);
+ timerlabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 100);
+ datelabel->SetPosition(133, 206, 200, 60);
+ datelabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 26);
+ weekLabel->SetPosition(242, 206, 200, 60);
+ weekLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 26);
+ break;
+
+ case TIME_DISPLAY_BELOW:
+ default: // 默认情况下显示在下方
+ WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "type is unknown, defaulting to TIMEVIEW_DOWN");
+ timerlabel->SetPosition(147, 301, 200, 60);
+ timerlabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 70);
+ datelabel->SetPosition(137, 387, 200, 60);
+ datelabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 26);
+ weekLabel->SetPosition(252, 387, 200, 60);
+ weekLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 26);
+ break;
}
- timerlabel->SetPosition(150, 290, 200, 150);
- timerlabel->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
- timerlabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 65);
- timerlabel->SetStyle(STYLE_BACKGROUND_COLOR, OPA_TRANSPARENT);
- timerlabel->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);
- timerlabel->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
+
Add(timerlabel);
+ Add(datelabel);
+ Add(weekLabel);
+ return true;
+ }
+ /*
+ 界面颜色显示
+ 0 白
+ 1 黑
+ 2 灰
+ 3 绿
+ 4 橙
+ 5 蓝
+
+ 默认:白
+ */
+ bool MainClockView::InitColor(ColorTypeSet type)
+ {
+ if (!timerlabel || !datelabel || !weekLabel)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "label is null");
+ return false;
+ }
+ int16 textColor;
+
+ // 根据传入的颜色类型设置文本颜色
+ switch (type)
+ {
+ case COLOR_BLACK:
+ timerlabel->SetTextColor(Color::Black());
+ datelabel->SetTextColor(Color::Black());
+ weekLabel->SetTextColor(Color::Black());
+ break;
+ case COLOR_GRAY:
+ timerlabel->SetTextColor(Color::Gray());
+ datelabel->SetTextColor(Color::Gray());
+ weekLabel->SetTextColor(Color::Gray());
+ break;
+ case COLOR_GREEN:
+ timerlabel->SetTextColor(Color::Green());
+ datelabel->SetTextColor(Color::Green());
+ weekLabel->SetTextColor(Color::Green());
+ break;
+ case COLOR_ORANGE:
+ timerlabel->SetTextColor(Color::Orange());
+ datelabel->SetTextColor(Color::Orange());
+ weekLabel->SetTextColor(Color::Orange());
+ break;
+ case COLOR_BLUE:
+ timerlabel->SetTextColor(Color::Blue());
+ datelabel->SetTextColor(Color::Blue());
+ weekLabel->SetTextColor(Color::Blue());
+ break;
+ case COLOR_WHITE:
+ default: // 默认情况下使用白色
+ timerlabel->SetTextColor(Color::White());
+ datelabel->SetTextColor(Color::White());
+ weekLabel->SetTextColor(Color::White());
+ break;
+ }
+ return true;
+ }
+ /*
+ 电量显示 [电池框图片 电池进度条 电池标签]
+ 0 不显示图标
+ 1 显示黑色
+ 2 显示白色
+
+ 默认:白色
+ */
+ bool MainClockView::InitBatteryView(BatteryDisplayType type)
+ {
+ // 不显示电池 icon
+ if (type == BATTERY_DISPLAY_NONE)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "no show battery");
+ return false;
+ }
- // 星期
- weekLabel = new UILabel();
- if (weekLabel == nullptr)
+ // 创建电池框图像
+ batteryiamge = new UIImageView();
+ if (!batteryiamge)
{
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "weekLabel new fail");
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "batteryiamge creation failed");
return false;
}
- weekLabel->SetPosition(180, 375, TEXT_WIDTH, TEXT_HEIGHT);
- weekLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 35);
- weekLabel->SetStyle(STYLE_BACKGROUND_COLOR, OPA_TRANSPARENT);
- weekLabel->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);
- weekLabel->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
- Add(weekLabel);
- datelabel = new UILabel();
- if (datelabel == nullptr)
+ // 创建电池图标图像
+ batteryicon = new UIImageView();
+ if (!batteryicon)
{
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "datelabel new fail");
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "batteryicon creation failed");
return false;
}
- datelabel->SetPosition(250, 375, TEXT_WIDTH, TEXT_HEIGHT);
- datelabel->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
- datelabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 35);
- datelabel->SetStyle(STYLE_BACKGROUND_COLOR, OPA_OPAQUE);
- datelabel->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);
- datelabel->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
- Add(datelabel);
- InitDateTime();
+ // 创建电池进度条
+ batteryprogress = new UIBoxProgress();
+ if (!batteryprogress)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "batteryprogress creation failed");
+ return false;
+ }
+
+ // 创建电池标签
+ batterylabel = new UILabel();
+ if (!batterylabel)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "batterylabel creation failed");
+ return false;
+ }
+
+ // 设置位置
+ batteryicon->SetPosition(185, 52, 100, 100);
+ batteryiamge->SetPosition(173, 52, 100, 100);
+ batteryprogress->SetPosition(179, 59, 30, 18);
+ batteryprogress->SetViewId("progress");
+ batteryprogress->SetStyle(STYLE_BACKGROUND_OPA, 0xff);
+ batterylabel->SetPosition(228, 54, 200, 60);
+ batterylabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 26);
+
+ // 设置图标和颜色
+ switch (type)
+ {
+ case BATTERY_DISPLAY_BLACK:
+ batteryiamge->SetSrc(BLACK_BATTERY);
+ batterylabel->SetTextColor(Color::Black());
+ batteryprogress->SetBackgroundStyle(STYLE_TEXT_COLOR, 0xff000000);
+ break;
+ case BATTERY_DISPLAY_WHITE:
+ default: // 默认不做额外操作,保持白色
+ batteryiamge->SetSrc(WHITE_BATTERY);
+ batterylabel->SetTextColor(Color::White());
+ batteryprogress->SetBackgroundStyle(STYLE_TEXT_COLOR, 0xffffffff);
+ break;
+ }
+ batteryicon->SetSrc(CHAGE_BATTERY);
+ batteryicon->SetVisible(false); // 设置不可见
+
+ Add(batteryiamge);
+ Add(batterylabel);
+ Add(batteryprogress);
+ Add(batteryicon);
return true;
}
viewiInitStatus = true;
}
- if(!resLoadStatus)
+ if (!resLoadStatus)
{
- if(PullUpDownView::GetInstance()!=nullptr)
+ if (PullUpDownView::GetInstance() != nullptr)
{
PullUpDownView::GetInstance()->PreLoad();
}
- if(MessageControlView::GetInstance()!=nullptr)
+ if (MessageControlView::GetInstance() != nullptr)
{
MessageControlView::GetInstance()->PreLoad();
}
{
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MainClockView::UnLoad");
- if(resLoadStatus)
+ if (resLoadStatus)
{
- if(PullUpDownView::GetInstance()!=nullptr)
+ if (PullUpDownView::GetInstance() != nullptr)
{
PullUpDownView::GetInstance()->UnLoad();
}
- if(MessageControlView::GetInstance()!=nullptr)
+ if (MessageControlView::GetInstance() != nullptr)
{
MessageControlView::GetInstance()->UnLoad();
}
bool MainClockView::InitView(void)
{
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MainClockView::InitView");
- if (InitSweepClock() == false)
+ if (InitClock() == false)
{
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitClock is error");
return false;
}
+ InitDateTime(); // 初始化数据
SetCoverable(true);
return true;
}
UpdateTime();
}
+ /* 更新时间 */
void MainClockView::UpdateTime(void)
- {
- struct tm local_time={0};
+ {
+ struct tm local_time = {0};
time_t seconds = time(nullptr);
- uint8_t hour = 0 ;
- uint8_t minute = 0 ;
- if (localtime_r(&seconds, &local_time) != NULL) {
- hour = local_time.tm_hour;
- minute = local_time.tm_min;
- }else{
+
+ if (localtime_r(&seconds, &local_time) == nullptr)
+ {
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "get localtime error!");
- hour = 0 ;
- minute = 0;
- }
-
- if (hour > 24) hour = 0;
- if (minute >= 60) minute = 0;
-
- char *phour = nullptr;
- char timebuf[64]={0};
- if(uapi_dev_runtime_get_time_hour_show_model() == 12 )
- {
- if(hour < 12)
- {
- phour="AM";
- }else if (hour == 12)
- {
- phour="PM";
- }else{
- phour="PM";
- hour = hour - 12;
- }
+ return;
}
- if (phour == nullptr)
+
+ // 处理小时和分钟
+ uint8_t hour = local_time.tm_hour % HOURS_IN_DAY;
+ uint8_t minute = local_time.tm_min % MINUTES_IN_HOUR;
+
+ // 处理12小时制显示
+ char timebuf[64] = {0};
+ const char *phour = (uapi_dev_runtime_get_time_hour_show_model() == HOUR_FORMAT_12) ? (hour < HOUR_FORMAT_12 ? "AM" : "PM") : nullptr;
+
+ if (phour)
+ {
+ if (hour > HOUR_FORMAT_12)
+ hour -= HOUR_FORMAT_12;
+ sprintf_s(timebuf, sizeof(timebuf), "%s %02d:%02d", phour, hour, minute);
+ }
+ else
{
- sprintf_s(timebuf,sizeof(timebuf),"%02d:%02d",hour,minute);
- }else{
- sprintf_s(timebuf,sizeof(timebuf),"%02d:%02d",hour,minute);
- // sprintf_s(timebuf,sizeof(timebuf),"%s%02d:%02d",phour,hour,minute);
- }
+ sprintf_s(timebuf, sizeof(timebuf), "%02d:%02d", hour, minute);
+ }
// 更新界面上的文本
- timerlabel->SetText(timebuf);
- // 更新日期需要nextday的消息
+ if (timerlabel != nullptr)
+ timerlabel->SetText(timebuf);
+
+ // 更新星期和日期
+ std::string weekStr = Cnweekdays[local_time.tm_wday];
+ std::string dateStr = std::to_string(local_time.tm_mon + 1) + "月" +
+ std::to_string(local_time.tm_mday) + "日";
+
+ if (weekLabel != nullptr)
+ weekLabel->SetText(weekStr.c_str());
+ if (datelabel != nullptr)
+ datelabel->SetText(dateStr.c_str());
+
+ // 更新电量
+ if (batterySetting == BATTERY_DISPLAY_NONE)
+ return;
+
+ UpdateBatteryColor();
+
+ float voltage = 0.0F;
+ int power = battery_get_voltage(&voltage);
+ if (power == power_)
+ return;
+
+ power_ = power;
+ if (batteryprogress != nullptr && batterylabel != nullptr)
+ {
+ batteryprogress->SetValue(power_);
+ std::string batteryStr = std::to_string(power_) + "%";
+ batterylabel->SetText(batteryStr.c_str());
+ }
+ }
+
+ void MainClockView::UpdateBatteryColor(void)
+ {
+ bool isCharging = get_charge_stat(); // 获取充电状态
+
+ if (isCharging)
+ {
+ if (!isBatteryIconVisible)
+ {
+ batteryicon->SetVisible(true);
+ isBatteryIconVisible = true;
+ }
+ SetViewColor(Color::Green().full); // 充电状态为绿色
+ return;
+ }
+ else
+ {
+ if (isBatteryIconVisible)
+ {
+ batteryicon->SetVisible(false);
+ isBatteryIconVisible = false;
+ }
+
+ if (power_ <= BATTERY_LOW_THRESHOLD)
+ {
+ SetViewColor(Color::Red().full);
+ }
+ else if (power_ <= BATTERY_MEDIUM_THRESHOLD)
+ {
+ SetViewColor(Color::Yellow().full);
+ }
+ else
+ {
+ if (batterySetting == BATTERY_DISPLAY_BLACK)
+ {
+ SetViewColor(Color::Black().full);
+ }
+ else
+ {
+ SetViewColor(Color::White().full);
+ }
+ }
+ }
+ }
+
+ void MainClockView::SetViewColor(int64_t color)
+ {
+ if (color == color_)
+ {
+ return;
+ }
+ color_ = color;
+ batteryprogress->SetForegroundStyle(STYLE_BACKGROUND_COLOR, color_);
}
- /* -------------------------------------------------------------------------- */
+ /*刷新步数*/
void MainClockView::RefreshStep(int step)
{
-#if DEFULTDIAL
+ if (stepLabel == nullptr)
+ return;
+
char stepChar[STEP_CHAR_NUM] = {0};
(void)sprintf_s(stepChar, STEP_CHAR_NUM, "%d", step);
stepLabel->SetText(stepChar);
-#endif
}
+ /*刷新心率*/
void MainClockView::RefreshHeartRate(int value)
{
-#if DEFULTDIAL
+ if (heartlabel == nullptr)
+ return;
+
if (value == 0)
{
- datelabel->SetText("--");
+ heartlabel->SetText("--");
return;
}
char heartRateChar[STEP_CHAR_NUM] = {0};
(void)sprintf_s(heartRateChar, STEP_CHAR_NUM, "%d", value);
- datelabel->SetText(heartRateChar);
-#endif
+ heartlabel->SetText(heartRateChar);
}
-#ifdef DEFULTDIAL
+
+ /*初始化步数*/
void MainClockView::InitStepValue()
{
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MainClockView::InitStepValue");
return;
}
stepLabel->SetText(stepChar);
- stepLabel->SetStyleForState(STYLE_BACKGROUND_OPA, OPA_TRANSPARENT, UIButton::RELEASED);
- stepLabel->SetStyleForState(STYLE_BACKGROUND_OPA, OPA_TRANSPARENT, UIButton::PRESSED);
- stepLabel->SetStyleForState(STYLE_BACKGROUND_OPA, OPA_TRANSPARENT, UIButton::INACTIVE);
- }
- bool MainClockView::InitLabelView(void)
- {
- UIView::OnClickListener *clickListener = (UIView::OnClickListener *)OHOS::MainPresenterSample::GetInstance();
- stepLabel = new UILabelButton();
- if (stepLabel == nullptr)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "stepLabel new fail");
- return false;
- }
- stepLabel->SetPosition(STEP_LABEL_X, STEP_LABEL_Y);
- stepLabel->Resize(STEP_LABLE_WIDTH, STEP_LABLE_SIZE);
- stepLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
- stepLabel->SetStyle(STYLE_BACKGROUND_OPA, OPA_TRANSPARENT);
- stepLabel->SetViewId(MAIN_CLICK_ACTIVITY);
- stepLabel->SetTouchable(true);
- stepLabel->SetOnClickListener(clickListener);
- InitStepValue();
-
- datelabel = new UILabel();
- if (datelabel == nullptr)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "datelabel new fail");
- return false;
- }
- datelabel->SetPosition(HEARTRATELABEL_POSITION_X, HEARTRATELABEL_POSITION_Y, TEXT_WIDTH, TEXT_HEIGHT);
- datelabel->SetText("--");
- datelabel->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
- datelabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
- datelabel->SetStyle(STYLE_BACKGROUND_COLOR, OPA_OPAQUE);
-
- weekLabel = new UILabel();
- if (weekLabel == nullptr)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "weekLabel new fail");
- return false;
- }
- weekLabel->SetPosition(WEEKLABEL_POSITION_X, WEEKLABEL_POSITION_Y, TEXT_WIDTH, TEXT_HEIGHT);
- weekLabel->SetText("2");
- weekLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_TEXT_SIZE);
- weekLabel->SetStyle(STYLE_BACKGROUND_COLOR, OPA_TRANSPARENT);
- return true;
}
+ /*初始化指针图片*/
bool MainClockView::InitCenterCircleView(void)
{
bool MainClockView::InitHandView(void)
{
-
secondHand = new Image();
if (secondHand == nullptr)
{
view->SetSrc(image);
return true;
}
-#endif
}