315 lines
9.6 KiB
C++
315 lines
9.6 KiB
C++
/*----------------------------------------------------------------------------
|
|
* Copyright (c) TJD Technologies Co., Ltd. 2024. All rights reserved.
|
|
*
|
|
* Description: TjdUiAppPowerSavingDialView.cpp
|
|
*
|
|
* Author: luziquan@ss-tjd.com
|
|
*
|
|
* Create: 2024-11-18
|
|
*--------------------------------------------------------------------------*/
|
|
|
|
#include "TjdUiAppBatterySaverView.h"
|
|
#include "TjdUiImageIds.h"
|
|
#include "TjdUiMemManage.h"
|
|
#include "TjdUiMultiLanguageExt.h"
|
|
#include "TjdUiUtils.h"
|
|
#include "common/image_cache_manager.h"
|
|
#include "graphic_service.h"
|
|
#include "sys_config.h"
|
|
|
|
namespace TJD {
|
|
|
|
#define ENABLE_PRINT_INFO 1
|
|
#if ENABLE_PRINT_INFO
|
|
#define static_print_info(...) sys_ui_log_i(__VA_ARGS__) // 一般信息打印宏控制
|
|
#define static_print_warn(...) sys_ui_log_w(__VA_ARGS__) // 警告信息打印一般常开
|
|
#define static_print_error(...) sys_ui_log_e(__VA_ARGS__) // 错误信息打印一般常开
|
|
#define static_print_debug(...) sys_ui_log_d(__VA_ARGS__) // 调试信息打印一般常开
|
|
#else
|
|
#define static_print_info(...)
|
|
#define static_print_warn(...)
|
|
#define static_print_error(...)
|
|
#define static_print_debug(...)
|
|
#endif
|
|
|
|
#define BATTERY_SAVER_BIN_PATH TJD_IMAGE_PATH "img_battery_saver.bin"
|
|
|
|
// clang-format off
|
|
static inline int16_t HorizontalCenter(int16_t width, int16_t parentWidth) { return (parentWidth - width) / 2; }
|
|
|
|
static inline void InitLabelHorCenter(OHOS::UILabel &label, uint8_t size, int16_t y, int16_t target, const char *text)
|
|
{
|
|
label.SetFont(TJD_VECTOR_FONT_FILENAME, size);
|
|
label.SetText(text);
|
|
label.SetLineBreakMode(OHOS::UILabel::LINE_BREAK_ADAPT);
|
|
label.SetAlign(OHOS::TEXT_ALIGNMENT_CENTER, OHOS::TEXT_ALIGNMENT_CENTER);
|
|
label.SetPosition(HorizontalCenter(label.GetWidth(), target), y);
|
|
}
|
|
// clang-format on
|
|
|
|
#define IMAGE_BIN_PATH TJD_IMAGE_PATH "img_record.bin"
|
|
|
|
static TjdUiAppBatterySaverView *g_powerSavingDialView = nullptr;
|
|
|
|
BatterySaverViewIndex TjdUiAppBatterySaverView::currentViewIndex_ = BatterySaverViewIndex::BATTERY_SAVER_UNKNOWN;
|
|
|
|
TjdUiAppBatterySaverView::TjdUiAppBatterySaverView() { g_powerSavingDialView = this; }
|
|
|
|
TjdUiAppBatterySaverView::~TjdUiAppBatterySaverView() { g_powerSavingDialView = nullptr; }
|
|
|
|
TjdUiAppBatterySaverView *TjdUiAppBatterySaverView::GetInstance(void) { return g_powerSavingDialView; }
|
|
|
|
void TjdUiAppBatterySaverView::OnStart()
|
|
{
|
|
OHOS::ImageCacheManager::GetInstance().LoadAllInMultiRes((uint8_t *)BATTERY_SAVER_BIN_PATH);
|
|
|
|
if (mainView_ == nullptr) {
|
|
mainView_ = new OHOS::UIViewGroup();
|
|
}
|
|
mainView_->SetPosition(0, 0, OHOS::HORIZONTAL_RESOLUTION, OHOS::VERTICAL_RESOLUTION);
|
|
InitTargetView(BatterySaverViewIndex::BATTERY_SAVER_MAIN);
|
|
InitTargetView(BatterySaverViewIndex::BATTERY_SAVER_LONG_PRESS);
|
|
|
|
AddViewToRootContainer(mainView_);
|
|
}
|
|
|
|
void TjdUiAppBatterySaverView::OnStop()
|
|
{
|
|
if (mainView_ != nullptr) {
|
|
mainView_->RemoveAll();
|
|
delete mainView_;
|
|
mainView_ = nullptr;
|
|
}
|
|
for (int i = 0; i < BatterySaverViewIndex::BATTERY_SAVER_UNKNOWN; i++) {
|
|
if (viewManager_[i] != nullptr) {
|
|
delete viewManager_[i];
|
|
viewManager_[i] = nullptr;
|
|
}
|
|
}
|
|
OHOS::ImageCacheManager::GetInstance().UnloadAllInMultiRes(BATTERY_SAVER_BIN_PATH);
|
|
}
|
|
|
|
void TjdUiAppBatterySaverView::InitTargetView(BatterySaverViewIndex index)
|
|
{
|
|
if (viewManager_[index] != nullptr) {
|
|
return;
|
|
}
|
|
|
|
// clang-format off
|
|
switch (index) {
|
|
case BATTERY_SAVER_MAIN: viewManager_[index] = new BatterySaverMainView(); break;
|
|
case BATTERY_SAVER_LONG_PRESS: viewManager_[index] = new BatterySaverLongPressView(); break;
|
|
default: break;
|
|
}
|
|
// clang-format on
|
|
|
|
if (viewManager_[index] == nullptr) {
|
|
return;
|
|
}
|
|
|
|
viewManager_[index]->SetPosition(0, 0);
|
|
viewManager_[index]->SetVisible(false);
|
|
mainView_->Add(viewManager_[index]);
|
|
}
|
|
|
|
void TjdUiAppBatterySaverView::ShowView(BatterySaverViewIndex showIndex)
|
|
{
|
|
if (showIndex < 0 || showIndex >= BatterySaverViewIndex::BATTERY_SAVER_UNKNOWN) {
|
|
return;
|
|
}
|
|
|
|
InitTargetView(showIndex);
|
|
|
|
if (currentViewIndex_ >= 0 && currentViewIndex_ < BatterySaverViewIndex::BATTERY_SAVER_UNKNOWN &&
|
|
viewManager_[currentViewIndex_] != nullptr) {
|
|
viewManager_[currentViewIndex_]->HideView();
|
|
}
|
|
|
|
if (viewManager_[showIndex] != nullptr) {
|
|
viewManager_[showIndex]->ShowView();
|
|
}
|
|
|
|
currentViewIndex_ = showIndex;
|
|
}
|
|
|
|
void TjdUiAppBatterySaverView::SetData(int month, int day, int hour, int minute, int week, int step)
|
|
{
|
|
InitTargetView(BatterySaverViewIndex::BATTERY_SAVER_MAIN);
|
|
auto *mainView = dynamic_cast<BatterySaverMainView *>(viewManager_[BatterySaverViewIndex::BATTERY_SAVER_MAIN]);
|
|
if (mainView == nullptr) {
|
|
return;
|
|
}
|
|
|
|
if (lastHour_ == -1 || lastMinute_ == -1 || hour != lastHour_ || minute != lastMinute_) {
|
|
mainView->SetTime(hour, minute);
|
|
lastHour_ = hour;
|
|
lastMinute_ = minute;
|
|
}
|
|
|
|
if (lastMonth_ == -1 || lastDay_ == -1 || month != lastMonth_ || day != lastDay_) {
|
|
mainView->SetDate(month, day);
|
|
lastMonth_ = month;
|
|
lastDay_ = day;
|
|
}
|
|
|
|
if (lastWeek_ == -1 || week != lastWeek_) {
|
|
mainView->SetWeek(week);
|
|
lastWeek_ = week;
|
|
}
|
|
|
|
if (lastStep_ == -1 || step != lastStep_) {
|
|
mainView->SetStep(step);
|
|
lastStep_ = step;
|
|
}
|
|
}
|
|
|
|
void TjdUiAppBatterySaverView::SetPowerValue(uint16_t value)
|
|
{
|
|
InitTargetView(BatterySaverViewIndex::BATTERY_SAVER_LONG_PRESS);
|
|
auto *longPressView =
|
|
dynamic_cast<BatterySaverLongPressView *>(viewManager_[BatterySaverViewIndex::BATTERY_SAVER_LONG_PRESS]);
|
|
if (longPressView == nullptr) {
|
|
return;
|
|
}
|
|
|
|
longPressView->DragPowerValue(value);
|
|
}
|
|
|
|
void TjdUiAppBatterySaverView::SetPowerState(const char *state)
|
|
{
|
|
InitTargetView(BatterySaverViewIndex::BATTERY_SAVER_LONG_PRESS);
|
|
auto *longPressView =
|
|
dynamic_cast<BatterySaverLongPressView *>(viewManager_[BatterySaverViewIndex::BATTERY_SAVER_LONG_PRESS]);
|
|
if (longPressView == nullptr) {
|
|
return;
|
|
}
|
|
|
|
longPressView->DragPowerState(state);
|
|
}
|
|
|
|
#pragma region 主页面
|
|
BatterySaverMainView::BatterySaverMainView()
|
|
{
|
|
Resize(OHOS::HORIZONTAL_RESOLUTION, OHOS::VERTICAL_RESOLUTION);
|
|
OHOS::ImageCacheManager &image = OHOS::ImageCacheManager::GetInstance();
|
|
auto stepIconRes = image.LoadOneInMultiRes(IMG_BATTERY_SAVER_PAOBU, BATTERY_SAVER_BIN_PATH);
|
|
|
|
InitLabelHorCenter(time_, 146, 65, 466, "00:00");
|
|
InitLabelHorCenter(date_, 44, 260, 466, "00-00");
|
|
date_.SetX(100);
|
|
InitLabelHorCenter(week_, 36, 265, 466, "周一");
|
|
week_.SetX(260);
|
|
|
|
stepIcon_.SetPosition(151, 341, 36, 33);
|
|
if (stepIconRes != nullptr) {
|
|
stepIcon_.SetSrc(stepIconRes);
|
|
}
|
|
|
|
InitLabelHorCenter(step_, 36, 331, 466, "00000");
|
|
step_.SetX(199);
|
|
|
|
Add(&time_);
|
|
Add(&date_);
|
|
Add(&week_);
|
|
Add(&step_);
|
|
Add(&stepIcon_);
|
|
}
|
|
|
|
BatterySaverMainView::~BatterySaverMainView() { RemoveAll(); }
|
|
|
|
void BatterySaverMainView::SetTime(int hour, int minute)
|
|
{
|
|
char timeStr[6] = {0};
|
|
snprintf(timeStr, sizeof(timeStr), "%02d:%02d", hour, minute);
|
|
time_.SetText(timeStr);
|
|
}
|
|
|
|
void BatterySaverMainView::SetDate(int month, int day)
|
|
{
|
|
char dateStr[6] = {0};
|
|
snprintf(dateStr, sizeof(dateStr), "%02d-%02d", month, day);
|
|
date_.SetText(dateStr);
|
|
}
|
|
|
|
void BatterySaverMainView::SetWeek(int week)
|
|
{
|
|
const char *weekStr[] = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
|
|
week_.SetText(weekStr[week]);
|
|
}
|
|
|
|
void BatterySaverMainView::SetStep(int step)
|
|
{
|
|
char stepStr[6] = {0};
|
|
snprintf(stepStr, sizeof(stepStr), "%05d", step);
|
|
step_.SetText(stepStr);
|
|
}
|
|
|
|
#pragma endregion
|
|
|
|
#pragma region 长按页面
|
|
BatterySaverLongPressView::BatterySaverLongPressView()
|
|
{
|
|
Resize(OHOS::HORIZONTAL_RESOLUTION, OHOS::VERTICAL_RESOLUTION);
|
|
OHOS::ImageCacheManager &image = OHOS::ImageCacheManager::GetInstance();
|
|
auto batIconRes = image.LoadOneInMultiRes(IMG_BATTERY_SAVER_BAT, BATTERY_SAVER_BIN_PATH);
|
|
auto exitIconRes = image.LoadOneInMultiRes(IMG_BATTERY_SAVER_POSITION, BATTERY_SAVER_BIN_PATH);
|
|
|
|
canvas_.SetPosition(133, 92, 200, 200);
|
|
batIcon_.SetPosition(211, 164, 44, 62);
|
|
if (batIconRes != nullptr) {
|
|
batIcon_.SetSrc(batIconRes);
|
|
}
|
|
exitIcon_.SetPosition(391, 93, 43, 60);
|
|
if (exitIconRes != nullptr) {
|
|
exitIcon_.SetSrc(exitIconRes);
|
|
}
|
|
InitLabelHorCenter(state_, 34, 329, 466, " 长按3S退出省电模式");
|
|
|
|
DragPowerValue(0);
|
|
|
|
Add(&canvas_);
|
|
Add(&batIcon_);
|
|
Add(&exitIcon_);
|
|
Add(&state_);
|
|
}
|
|
|
|
BatterySaverLongPressView::~BatterySaverLongPressView() { RemoveAll(); }
|
|
|
|
void BatterySaverLongPressView::DragPowerValue(uint16_t value)
|
|
{
|
|
canvas_.Clear();
|
|
OHOS::PaintExt paint;
|
|
paint.SetStyle(OHOS::Paint::PaintStyle::STROKE_STYLE);
|
|
paint.SetStrokeWidth(15);
|
|
paint.SetAntialiased(true);
|
|
OHOS::ColorType color = {
|
|
.full = 0xFF000000,
|
|
};
|
|
paint.SetStrokeColor(color);
|
|
OHOS::ColorStop stops[2] = {{0, 0x4C00C48F}, {1, 0x4C00FFBA}};
|
|
int16_t cx = 100;
|
|
int16_t cy = 100;
|
|
uint16_t radius = 92;
|
|
|
|
OHOS::RadialGradient radial = {cx - 24.5, cy, 0, 360, 90};
|
|
paint.SetRadialGradient(radial, stops, 2);
|
|
paint.SetCapType(OHOS::CapType::CAP_ROUND);
|
|
canvas_.DrawArc({cx, cy}, radius, 0, 360, paint);
|
|
|
|
if (value == 0) {
|
|
return;
|
|
}
|
|
stops[0] = {0, 0xFF00C48F};
|
|
stops[1] = {1, 0xFF00C48F};
|
|
paint.SetRadialGradient(radial, stops, 2);
|
|
canvas_.DrawArc({cx, cy}, radius, 0, value, paint);
|
|
}
|
|
|
|
void BatterySaverLongPressView::DragPowerState(const char *state)
|
|
{
|
|
state_.SetText(state);
|
|
state_.SetX(HorizontalCenter(state_.GetWidth(), OHOS::HORIZONTAL_RESOLUTION));
|
|
}
|
|
#pragma endregion
|
|
|
|
} // namespace TJD
|