-/*\r
- * Copyright (c) 2023 CompanyNameMagicTag.\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
+\r
\r
#include "saveele/include/SaveBatView.h"\r
\r
#include "battery_hal.h"\r
#include "string"\r
#include "custom_utils.h"\r
-\r
+#include "UiConfig.h"\r
+#include "wearable_log.h"\r
extern "C"\r
{\r
void uapi_exit_power_saving_mode(void);\r
void uapi_enter_power_saving_mode(void);\r
}\r
\r
+std::string Cnweekday[] = {"周天", "周一", "周二", "周三", "周四", "周五", "周六"};\r
+\r
+static constexpr const char *WHITE_BATTERY = "/user/res/Battery/Battery-E.bin";\r
+static constexpr const char *SAVE_BUTTON = "/user/res/Save_Ele/finger.bin";\r
+\r
namespace OHOS\r
{\r
static SaveBatView *g_pSaveBatView{nullptr};\r
- std::string WeekString(int wday)\r
- {\r
- std::string weekDays[] = {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};\r
- return weekDays[wday];\r
- }\r
+\r
SaveBatView::SaveBatView()\r
{\r
/*开启省电模式*/\r
{\r
/*关闭省电模式*/\r
uapi_exit_power_saving_mode();\r
- DeInit();\r
+\r
+ UIViewGroup *tmpGroup = dynamic_cast<UIViewGroup *>(container_);\r
+ UIViewGroup::RemoveAndDeleteAllRecursively(tmpGroup);\r
+\r
g_pSaveBatView = nullptr;\r
}\r
\r
SaveBatView *SaveBatView::GetInstance()\r
{\r
-\r
return g_pSaveBatView;\r
}\r
+\r
+ /* 更新主界面数据 */\r
void SaveBatView::RefreshView()\r
{\r
+ /*\r
+ 日期样式 04-22 周五\r
+ 时间样式 09:10\r
+ */\r
+\r
// 获取当前时间\r
- struct tm pTM;\r
time_t seconds;\r
time(&seconds);\r
+ struct tm pTM;\r
localtime_r(&seconds, &pTM);\r
\r
// 解析时间\r
int hour = pTM.tm_hour;\r
int minute = pTM.tm_min;\r
+ int day = pTM.tm_mday;\r
+ int month = pTM.tm_mon + 1; // 月份范围 [1, 12]\r
+ std::string weekStr = Cnweekday[pTM.tm_wday];\r
\r
- // 构建日期字符串\r
- std::string yearStr = std::to_string(pTM.tm_year + 1900);\r
- std::string monthStr = (pTM.tm_mon + 1 < 10) ? "0" + std::to_string(pTM.tm_mon + 1) : std::to_string(pTM.tm_mon + 1);\r
- std::string weekStr = WeekString(pTM.tm_wday);\r
- std::string dayStr = (pTM.tm_mday < 10) ? "0" + std::to_string(pTM.tm_mday) : std::to_string(pTM.tm_mday);\r
- std::string dateStr = monthStr + "月" + dayStr + "日";\r
+ // 构建日期和时间字符串\r
+ std::string dateStr =\r
+ (month < 10 ? "0" : "") + std::to_string(month) + "-" +\r
+ (day < 10 ? "0" : "") + std::to_string(day)+" "+weekStr;\r
\r
- // 将时间和日期转换为字符串\r
- std::string stringtime = (hour < 10 ? "0" : "") + std::to_string(hour) + ":" + (minute < 10 ? "0" : "") + std::to_string(minute);\r
+ std::string stringtime =\r
+ (hour < 10 ? "0" : "") + std::to_string(hour) + ":" +\r
+ (minute < 10 ? "0" : "") + std::to_string(minute);\r
\r
// 获取电量值\r
float voltage = 0.0F;\r
- uint8_t value = 0;\r
- value = battery_get_voltage(&voltage);\r
+ uint8_t value = battery_get_voltage(&voltage);\r
\r
// 更新界面组件\r
timelabel->SetText(stringtime.c_str());\r
datelabel->SetText(dateStr.c_str());\r
- weeklabel->SetText(weekStr.c_str());\r
- elelabel->SetText((std::to_string(value) + "%").c_str());\r
- progress->SetValue(value);\r
+ batterylabel->SetText((std::to_string(value) + "%").c_str());\r
+ batteryprogress->SetValue(value);\r
}\r
-\r
- void SaveBatView::Init()\r
+ void SaveBatView::InitBatteryView()\r
{\r
- if (container_ == nullptr)\r
+ // 创建电池框图像\r
+ batteryiamge = new UIImageView();\r
+ if (!batteryiamge)\r
{\r
- container_ = new UIViewGroup();\r
- container_->SetPosition(0, 0, 466, 466);\r
- container_->SetStyle(STYLE_BACKGROUND_OPA, 0);\r
- }\r
- /* --------------------------------- 电池标签初始化 -------------------------------- */\r
- if (elelabel == nullptr)\r
- {\r
- elelabel = new UILabel();\r
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "batteryiamge creation failed");\r
+ return;\r
}\r
- elelabel->SetPosition(239, 52); // widget's left position and top position\r
- elelabel->Resize(200, 60); // widget's width and height\r
- elelabel->SetViewId("label1"); // Set style\r
- elelabel->SetStyle(STYLE_TEXT_FONT, 27);\r
- elelabel->SetText("10%");\r
- elelabel->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);\r
- elelabel->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);\r
- elelabel->SetRollSpeed(0);\r
- elelabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 27);\r
- container_->Add(elelabel);\r
- elelabel->SetVisible(true);\r
- /* -------------------------------- 电池框图标初始化 -------------------------------- */\r
- if (eleicon == nullptr)\r
+\r
+ // 创建电池进度条\r
+ batteryprogress = new UIBoxProgress();\r
+ if (!batteryprogress)\r
{\r
- eleicon = new UIImageView();\r
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "batteryprogress creation failed");\r
+ return;\r
}\r
- eleicon->SetPosition(179, 52); // widget's left position and top position\r
- eleicon->Resize(48, 30); // widget's width and height\r
- eleicon->SetViewId("image7"); // Set style\r
- // Set additional property\r
- char *image7imagesPath = "/user/res/Save_Ele/Battery-E.bin";\r
- eleicon->SetSrc(image7imagesPath);\r
- // Add to rootView\r
- container_->Add(eleicon);\r
- eleicon->SetVisible(true);\r
- /* -------------------------------- 电池进度条初始化 -------------------------------- */\r
- // Add BoxProgress to App\r
- if (progress == nullptr)\r
+\r
+ // 创建电池标签\r
+ batterylabel = new UILabel();\r
+ if (!batterylabel)\r
{\r
- progress = new UIBoxProgress();\r
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "batterylabel creation failed");\r
+ return;\r
}\r
- progress->SetPosition(184, 58); // widget's left position and top position\r
- progress->Resize(31, 19); // widget's width and height\r
- progress->SetViewId("progress1"); // Set style\r
- progress->SetValidHeight(19);\r
- progress->SetValidWidth(31);\r
- progress->SetValue(20);\r
- // Set BackgroundStyle\r
- progress->SetBackgroundStyle(STYLE_BACKGROUND_OPA, 0x00);\r
- progress->SetBackgroundStyle(STYLE_TEXT_COLOR, 0xff000000);\r
- // Set ForegroundStyle\r
- progress->SetForegroundStyle(STYLE_BACKGROUND_COLOR, 0xff2195F6);\r
- progress->SetForegroundStyle(STYLE_TEXT_COLOR, 0xff000000);\r
- progress->SetForegroundStyle(STYLE_LINE_COLOR, 0xff45A5FF);\r
- char *progress1ForeImageSrc = "/user/res/Battery/Battery_Nomoal.bin";\r
- char *progress1BackImageSrc = nullptr;\r
- progress->SetImage(progress1ForeImageSrc, progress1BackImageSrc);\r
- // Add to rootView\r
- container_->Add(progress);\r
- /* --------------------------------- 时间标签初始化 -------------------------------- */\r
- // Add Label to App\r
+\r
+ // 设置位置\r
+ batteryiamge->SetPosition(173, 52, 100, 100);\r
+ batteryprogress->SetPosition(179, 59, 30, 18);\r
+ batteryprogress->SetViewId("batteryprogress");\r
+ batteryprogress->SetStyle(STYLE_BACKGROUND_OPA, 0xff);\r
+ batterylabel->SetPosition(228, 54, 200, 60);\r
+ batterylabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 26);\r
+\r
+ batteryiamge->SetSrc(WHITE_BATTERY);\r
+ batterylabel->SetTextColor(Color::White());\r
+ batteryprogress->SetForegroundStyle(STYLE_BACKGROUND_COLOR, 0xffffc300);\r
+\r
+ container_->Add(batteryiamge);\r
+ container_->Add(batterylabel);\r
+ container_->Add(batteryprogress);\r
+ }\r
+ void SaveBatView::InitTimeView()\r
+ {\r
+ /*时间time*/\r
+ timelabel = new UILabel();\r
if (timelabel == nullptr)\r
{\r
- timelabel = new UILabel();\r
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "timelabel creation failed");\r
+ return;\r
}\r
- timelabel->SetPosition(118, 148); // widget's left position and top position\r
- timelabel->Resize(231, 115); // widget's width and height\r
- timelabel->SetViewId("label2"); // Set style\r
+ timelabel->SetPosition(0, 148);\r
+ timelabel->Resize(466, 100);\r
+ timelabel->SetViewId("label1");\r
timelabel->SetStyle(STYLE_TEXT_FONT, 100);\r
- timelabel->SetStyle(STYLE_LINE_HEIGHT, 115);\r
- // Set additional property\r
- timelabel->SetText("23:59");\r
- timelabel->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);\r
- timelabel->SetTextColor(Color::GetColorFromRGBA(0x4f, 0xc3, 0x4c, 0xff));\r
+ timelabel->SetText("11:00");\r
+ timelabel->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS);\r
+ timelabel->SetTextColor(Color::GetColorFromRGBA(0x49, 0xef, 0x13, 0xff));\r
timelabel->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);\r
timelabel->SetRollSpeed(0);\r
timelabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 100);\r
- // Add to rootView\r
container_->Add(timelabel);\r
- timelabel->SetVisible(true);\r
- /* --------------------------------- 日期标签初始化 -------------------------------- */\r
- // Add Label to App\r
+\r
+ /*日期*/\r
+ datelabel = new UILabel();\r
if (datelabel == nullptr)\r
{\r
- datelabel = new UILabel();\r
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "datelabel creation failed");\r
+ return;\r
}\r
- datelabel->SetPosition(125, 268); // widget's left position and top position\r
- datelabel->Resize(210, 35); // widget's width and height\r
- datelabel->SetViewId("label3"); // Set style\r
+ datelabel->SetPosition(83, 258);\r
+ datelabel->Resize(300, 60);\r
+ datelabel->SetViewId("label2");\r
datelabel->SetStyle(STYLE_TEXT_FONT, 30);\r
- datelabel->SetStyle(STYLE_LETTER_SPACE, 2);\r
- datelabel->SetStyle(STYLE_LINE_HEIGHT, 35);\r
- // Set additional property\r
- datelabel->SetText("11月3号");\r
- datelabel->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);\r
+\r
+ datelabel->SetText("04-22 周五");\r
+ datelabel->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS);\r
datelabel->SetTextColor(Color::GetColorFromRGBA(0x66, 0x66, 0x66, 0xff));\r
datelabel->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);\r
datelabel->SetRollSpeed(0);\r
datelabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30);\r
- // Add to rootView\r
container_->Add(datelabel);\r
- datelabel->SetVisible(true);\r
+ }\r
\r
- /* --------------------------------- 星期标签初始化 -------------------------------- */\r
- if (weeklabel == nullptr)\r
- {\r
- weeklabel = new UILabel();\r
- }\r
- weeklabel->SetPosition(280, 268); // widget's left position and top position\r
- weeklabel->Resize(280, 35); // widget's width and height\r
- weeklabel->SetViewId("label3"); // Set style\r
- weeklabel->SetStyle(STYLE_TEXT_FONT, 30);\r
- weeklabel->SetStyle(STYLE_LETTER_SPACE, 2);\r
- weeklabel->SetStyle(STYLE_LINE_HEIGHT, 35);\r
-\r
- // Set additional property\r
- weeklabel->SetText("星期五");\r
- weeklabel->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);\r
- weeklabel->SetTextColor(Color::GetColorFromRGBA(0x66, 0x66, 0x66, 0xff));\r
- weeklabel->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);\r
- weeklabel->SetRollSpeed(0);\r
- weeklabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30);\r
- // Add to rootView\r
- container_->Add(weeklabel);\r
- weeklabel->SetVisible(true);\r
-\r
- /* --------------------------------- 退出按键初始化 -------------------------------- */\r
+ void SaveBatView::InitButtonView()\r
+ {\r
+ // 初始化按键\r
+ button_exit = new UIButton();\r
if (button_exit == nullptr)\r
{\r
- button_exit = new UIButton();\r
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "button_exit creation failed");\r
+ return;\r
}\r
- button_exit->SetPosition(204, 350); // widget's left position and top position\r
- button_exit->Resize(100, 100); // widget's width and height\r
- button_exit->SetViewId("button_exit"); // Set style\r
+ button_exit->SetPosition(204, 350);\r
+ button_exit->Resize(100, 100);\r
+ button_exit->SetViewId("button_exit");\r
button_exit->SetStyle(STYLE_BACKGROUND_COLOR, 0xff000000);\r
button_exit->SetStyle(STYLE_BACKGROUND_OPA, 0x00);\r
\r
- // Set additional property\r
- char *button1defaultImgSrc = "/user/res/Save_Ele/finger.bin";\r
- char *button1triggeredImgSrc = nullptr;\r
- button_exit->SetImageSrc(button1defaultImgSrc, button1defaultImgSrc);\r
+ button_exit->SetImageSrc(SAVE_BUTTON, SAVE_BUTTON);\r
button_exit->SetStyleForState(STYLE_BACKGROUND_COLOR, 0xff000000, UIButton::PRESSED);\r
\r
- // Add the CallBack about events\r
- UIView::OnLongPressListener *button1LongPress = static_cast<UIView::OnLongPressListener *>(SaveBatPresenter::GetInstance());\r
- button_exit->SetOnLongPressListener(button1LongPress);\r
- // Add to rootView\r
+ button_exit->SetOnLongPressListener(presenter_);\r
container_->Add(button_exit);\r
- button_exit->SetVisible(true);\r
- }\r
-\r
- void SaveBatView::DeInit()\r
- {\r
- if (container_ != nullptr)\r
- {\r
- container_->RemoveAll();\r
- delete container_;\r
- container_ = nullptr;\r
- }\r
- if (elelabel != nullptr)\r
- {\r
- delete elelabel;\r
- elelabel = nullptr;\r
- }\r
- if (eleicon != nullptr)\r
- {\r
- delete eleicon;\r
- eleicon = nullptr;\r
- }\r
- if (weeklabel != nullptr)\r
- {\r
- delete weeklabel;\r
- weeklabel = nullptr;\r
- }\r
- if (timelabel != nullptr)\r
- {\r
- delete timelabel;\r
- timelabel = nullptr;\r
- }\r
- if (datelabel != nullptr)\r
- {\r
- delete datelabel;\r
- datelabel = nullptr;\r
- }\r
- if (button_exit != nullptr)\r
- {\r
- delete button_exit;\r
- button_exit = nullptr;\r
- }\r
}\r
\r
- UIView *SaveBatView::GetView()\r
- {\r
- Init();\r
- return container_;\r
- }\r
void SaveBatView::OnStart()\r
{\r
container_ = new UIViewGroup();\r
container_->SetTouchable(true);\r
container_->SetDraggable(true);\r
\r
- GetView();\r
+ InitBatteryView();\r
+ InitTimeView();\r
+ InitButtonView();\r
+\r
RootView::GetInstance()->Add(container_);\r
}\r
\r
void SaveBatView::OnStop()\r
{\r
View<SaveBatPresenter>::OnStop();\r
- RootView::GetInstance()->RemoveAll();\r
-\r
+ if (container_ != nullptr)\r
+ {\r
+ container_->RemoveAll();\r
+ delete container_;\r
+ container_ = nullptr;\r
+ }\r
return;\r
}\r
}
\ No newline at end of file