242 lines
8.3 KiB
C++
242 lines
8.3 KiB
C++
/*----------------------------------------------------------------------------
|
|
* Copyright (c) TJD Technologies Co., Ltd. 2024. All rights reserved.
|
|
*
|
|
* Description: TjdUiAppPowerSavingDialPresenter.cpp
|
|
*
|
|
* Author: luziquan@ss-tjd.com
|
|
*
|
|
* Create: 2024-11-18
|
|
*--------------------------------------------------------------------------*/
|
|
|
|
#include "TjdUiAppBatterySaverPresenter.h"
|
|
#include "NativeAbility.h"
|
|
#include "TjdUiAppBatterySaverModel.h"
|
|
#include "TjdUiAppBatterySaverView.h"
|
|
#include "TjdUiRegisterManager.h"
|
|
#include "TjdUiSettingCenter.h"
|
|
#include "common/key_code.h"
|
|
#include "dock/input_device.h"
|
|
#include "graphic_service.h"
|
|
#include "sys_config.h"
|
|
#include "ui_screennotify.h"
|
|
|
|
namespace TJD {
|
|
|
|
#define BATTERY_SAVER_GET_PAY_STR_MAX_LENGTH 20
|
|
|
|
#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
|
|
|
|
TJD_REGIST_SLICE(TJD_APP_BATTERY_SAVER, TjdUiAppBatterySaverView, TjdUiAppBatterySaverPresenter);
|
|
|
|
static TjdUiAppBatterySaverPresenter *g_powerSavingDialPresenter = nullptr;
|
|
|
|
TjdUiAppBatterySaverPresenter::TjdUiAppBatterySaverPresenter()
|
|
{
|
|
g_powerSavingDialPresenter = this;
|
|
SetPeriod(2000);
|
|
}
|
|
|
|
TjdUiAppBatterySaverPresenter::~TjdUiAppBatterySaverPresenter() { g_powerSavingDialPresenter = nullptr; }
|
|
|
|
TjdUiAppBatterySaverPresenter *TjdUiAppBatterySaverPresenter::GetInstance(void) { return g_powerSavingDialPresenter; }
|
|
|
|
static uint32_t g_interval = 5;
|
|
|
|
static void BackHomeOff(void)
|
|
{
|
|
#if defined(SUPPORT_POWER_MANAGER)
|
|
g_interval = get_back_to_home_interval();
|
|
set_back_to_home_interval(0xFFFFFFFF);
|
|
#endif
|
|
}
|
|
|
|
static void BackHomeOn(void)
|
|
{
|
|
#if defined(SUPPORT_POWER_MANAGER)
|
|
set_back_to_home_interval(g_interval);
|
|
#endif
|
|
}
|
|
|
|
static bool isScreenOff = false;
|
|
static void BSScreenStatusNotifyCallback(ScreenStatus status)
|
|
{
|
|
TjdUiAppBatterySaverView *batterySaverView = TjdUiAppBatterySaverView::GetInstance();
|
|
if (batterySaverView == nullptr) {
|
|
return;
|
|
}
|
|
if (status == ScreenStatus::SCREEN_STATUS_ON) {
|
|
if (isScreenOff) {
|
|
isScreenOff = false;
|
|
batterySaverView->ShowView(BatterySaverViewIndex::BATTERY_SAVER_MAIN);
|
|
TjdUiAppBatterySaverModel::GetInstance().SetLcdIdleMode(true);
|
|
}
|
|
} else if (status == ScreenStatus::SCREEN_STATUS_OFF) {
|
|
isScreenOff = true;
|
|
}
|
|
}
|
|
|
|
void TjdUiAppBatterySaverPresenter::OnStart()
|
|
{
|
|
TjdUiCommonOnKeyListener::GetInstance()->SetOnKeyActListener(this, KeyModelType::APP_KEY_TYPE);
|
|
registerNotifyScreenStatus(BSScreenStatusNotifyCallback);
|
|
TjdUiCommonOnKeyListener::GetInstance()->SetLongPressActIntercept(true);
|
|
|
|
TjdUiAppBatterySaverView *batterySaverView = TjdUiAppBatterySaverView::GetInstance();
|
|
if (batterySaverView == nullptr) {
|
|
return;
|
|
}
|
|
BatterySaverData data = TjdUiAppBatterySaverModel::GetInstance().GetCurrentData();
|
|
batterySaverView->SetData(data.month, data.day, data.hour, data.minute, data.week, data.step);
|
|
batterySaverView->ShowView(BatterySaverViewIndex::BATTERY_SAVER_MAIN);
|
|
|
|
BackHomeOff();
|
|
TjdUiSettingCenter::GetInstance().SetLowPowerMode(true);
|
|
}
|
|
|
|
void TjdUiAppBatterySaverPresenter::OnStop()
|
|
{
|
|
TjdUiCommonOnKeyListener::GetInstance()->ClearOnKeyActListener(KeyModelType::APP_KEY_TYPE);
|
|
StopAnimatorTimer();
|
|
deregisterNotifyScreenStatus(BSScreenStatusNotifyCallback);
|
|
TjdUiCommonOnKeyListener::GetInstance()->SetLongPressActIntercept(false);
|
|
BackHomeOn();
|
|
TjdUiSettingCenter::GetInstance().SetLowPowerMode(false);
|
|
}
|
|
|
|
void TjdUiAppBatterySaverPresenter::Notify(void)
|
|
{
|
|
TjdUiAppBatterySaverView *batterySaverView = TjdUiAppBatterySaverView::GetInstance();
|
|
if (batterySaverView == nullptr) {
|
|
return;
|
|
}
|
|
BatterySaverData data = TjdUiAppBatterySaverModel::GetInstance().GetCurrentData();
|
|
batterySaverView->SetData(data.month, data.day, data.hour, data.minute, data.week, data.step);
|
|
}
|
|
|
|
void TjdUiAppBatterySaverPresenter::StartAnimatorTimer(void)
|
|
{
|
|
if (animatorTimerId_ == nullptr) {
|
|
animatorTimerId_ = osTimerNew(
|
|
[](void *arg) {
|
|
GraphicService::GetInstance()->PostGraphicEvent([]() {
|
|
TjdUiAppBatterySaverPresenter *presenter = TjdUiAppBatterySaverPresenter::GetInstance();
|
|
if (presenter == nullptr) {
|
|
return;
|
|
}
|
|
presenter->AnimatorTimerCallback();
|
|
});
|
|
},
|
|
osTimerPeriodic, nullptr, nullptr);
|
|
if (animatorTimerId_ == nullptr) {
|
|
static_print_error("TjdUiAppBatterySaverPresenter Create timer fail.");
|
|
return;
|
|
}
|
|
}
|
|
|
|
int32_t ret = osTimerStart(animatorTimerId_, 1000);
|
|
if (ret != 0) {
|
|
static_print_error("TjdUiAppBatterySaverPresenter Start timer fail");
|
|
return;
|
|
}
|
|
isAnimatorRunning_ = true;
|
|
}
|
|
|
|
void TjdUiAppBatterySaverPresenter::StopAnimatorTimer(void)
|
|
{
|
|
animatorCount_ = 1;
|
|
if (animatorTimerId_ != nullptr) {
|
|
osTimerStop(animatorTimerId_);
|
|
}
|
|
isAnimatorRunning_ = false;
|
|
}
|
|
void TjdUiAppBatterySaverPresenter::AnimatorTimerCallback(void)
|
|
{
|
|
TjdUiAppBatterySaverView *batterySaverView = TjdUiAppBatterySaverView::GetInstance();
|
|
if (batterySaverView == nullptr) {
|
|
return;
|
|
}
|
|
batterySaverView->SetPowerValue(animatorCount_ * 120);
|
|
++animatorCount_;
|
|
if (animatorCount_ > 3) {
|
|
StopAnimatorTimer();
|
|
StartAnimatorSucessEventTimer();
|
|
batterySaverView->SetPowerState("已退出");
|
|
}
|
|
}
|
|
|
|
void TjdUiAppBatterySaverPresenter::StartAnimatorSucessEventTimer(void)
|
|
{
|
|
if (jumpTimerId_ == nullptr) {
|
|
jumpTimerId_ = osTimerNew(
|
|
[](void *arg) {
|
|
GraphicService::GetInstance()->PostGraphicEvent(
|
|
[]() { OHOS::NativeAbility::GetInstance().ChangeSlice(TJD_APP_VIEW_MAIN); });
|
|
},
|
|
osTimerOnce, nullptr, nullptr);
|
|
if (jumpTimerId_ == nullptr) {
|
|
static_print_error("TjdUiAppBatterySaverPresenter Create timer fail.");
|
|
return;
|
|
}
|
|
}
|
|
|
|
int32_t ret = osTimerStart(jumpTimerId_, 1500);
|
|
if (ret != 0) {
|
|
static_print_error("TjdUiAppBatterySaverPresenter Start timer fail");
|
|
return;
|
|
}
|
|
}
|
|
|
|
bool TjdUiAppBatterySaverPresenter::OnKeyAct(OHOS::UIView &view, const OHOS::KeyEvent &event)
|
|
{
|
|
if (event.GetKeyId() != static_cast<uint16_t>(OHOS::ZliteKeyCode::ZLITE_KEY_TJD_RIGHTUP)) {
|
|
return true;
|
|
}
|
|
const uint16_t keyState = event.GetState();
|
|
const BatterySaverViewIndex currentView = TjdUiAppBatterySaverView::currentViewIndex_;
|
|
TjdUiAppBatterySaverView *batterySaverView = TjdUiAppBatterySaverView::GetInstance();
|
|
if (batterySaverView == nullptr) {
|
|
return true;
|
|
}
|
|
if (currentView == BatterySaverViewIndex::BATTERY_SAVER_MAIN) {
|
|
if (keyState == OHOS::InputDevice::STATE_PRESS) {
|
|
batterySaverView->ShowView(BatterySaverViewIndex::BATTERY_SAVER_LONG_PRESS);
|
|
if (!isAnimatorRunning_) {
|
|
batterySaverView->SetPowerValue(0);
|
|
StartAnimatorTimer();
|
|
}
|
|
return true;
|
|
}
|
|
} else if (currentView == BatterySaverViewIndex::BATTERY_SAVER_LONG_PRESS) {
|
|
if (keyState == OHOS::InputDevice::STATE_PRESS) {
|
|
if (!isAnimatorRunning_) {
|
|
batterySaverView->SetPowerValue(0);
|
|
StartAnimatorTimer();
|
|
}
|
|
return true;
|
|
}
|
|
if (keyState == OHOS::InputDevice::STATE_RELEASE || keyState == OHOS::InputDevice::STATE_DOUBLE_CLICK ||
|
|
keyState == OHOS::InputDevice::STATE_LONGPRESS_1S_OVER ||
|
|
keyState == OHOS::InputDevice::STATE_LONGPRESS_2S_OVER ||
|
|
keyState == OHOS::InputDevice::STATE_LONGPRESS_3S_OVER) {
|
|
if (isAnimatorRunning_) {
|
|
batterySaverView->SetPowerValue(0);
|
|
StopAnimatorTimer();
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
} // namespace TJD
|