1 /*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "charger_animation.h"
17 #include <sys/types.h>
18
19 #include "common/graphic_startup.h"
20 #include "common/screen.h"
21 #include "components/root_view.h"
22 #include "graphic_engine.h"
23
24 #include "component/img_view_adapter.h"
25 #include "component/text_label_adapter.h"
26 #include "layout/layout_parser.h"
27 #include "config_policy_utils.h"
28
29 namespace OHOS {
30 namespace PowerMgr {
31 namespace {
32 constexpr const char* UI_CONFIG_FILE = "etc/charger/resources/animation.json";
33 constexpr const char* VENDOR_UI_CONFIG_FILE = "/vendor/etc/charger/resources/animation.json";
34 constexpr const char* SYSTEM_UI_CONFIG_FILE = "/system/etc/charger/resources/animation.json";
35 constexpr const char* FONT_PATH = "/system/etc/charger/resources/";
36 constexpr const char* CHARGER_ANIMATION_PAGE_ID = "Charger:Animation";
37 constexpr const char* CHARGER_ANIMATION_COM_ID = "Charging_Animation_Image";
38 constexpr const char* CHARGER_PERCENT_COM_ID = "Charging_Percent_Label";
39 constexpr const char* LACKPOWER_CHARGING_STATE_PAGE_ID = "Charger:Lackpower_Charging_Prompt";
40 constexpr const char* LACKPOWER_CHARGING_STATE_COM_ID = "LackPower_Charging_Label";
41 constexpr const char* LACKPOWER_NOT_CHARGING_STATE_PAGE_ID = "Charger:Lackpower_Not_Charging_Prompt";
42 constexpr const char* LACKPOWER_NOT_CHARGING_STATE_COM_ID = "LackPower_Not_Charging_Label";
43 constexpr uint32_t WHITE_BGCOLOR = 0x000000ff;
44 }; // namespace
45
InitConfig()46 bool ChargerAnimation::InitConfig()
47 {
48 // graphic engine init
49 OHOS::GraphicStartUp::Init();
50 Updater::GraphicEngine::GetInstance().Init(WHITE_BGCOLOR, OHOS::ColorMode::ARGB8888, FONT_PATH);
51 InitRootView();
52
53 std::vector<std::string> ruleFiles;
54 char buf[MAX_PATH_LEN];
55 char* path = GetOneCfgFile(UI_CONFIG_FILE, buf, MAX_PATH_LEN);
56 if (path != nullptr && *path != '\0') {
57 ruleFiles.push_back(path);
58 }
59 ruleFiles.push_back(VENDOR_UI_CONFIG_FILE);
60 ruleFiles.push_back(SYSTEM_UI_CONFIG_FILE);
61
62 std::vector<Updater::UxPageInfo> pageInfos {};
63 bool isMatch = false;
64 for (auto configPathItem : ruleFiles) {
65 std::vector<std::string> layoutFiles {configPathItem};
66 if (Updater::LayoutParser::GetInstance().LoadLayout(layoutFiles, pageInfos)) {
67 isMatch = true;
68 break;
69 }
70 BATTERY_HILOGE(FEATURE_CHARGING, "load layout %{public}s error", configPathItem.c_str());
71 }
72
73 if (!isMatch) {
74 return false;
75 }
76
77 if (!pgMgr_.Init(pageInfos, CHARGER_ANIMATION_PAGE_ID)) {
78 BATTERY_HILOGE(FEATURE_CHARGING, "page manager init error");
79 return false;
80 }
81 return true;
82 }
83
InitRootView()84 void ChargerAnimation::InitRootView()
85 {
86 OHOS::RootView::GetInstance()->SetPosition(0, 0);
87 OHOS::RootView::GetInstance()->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, OHOS::Color::Black().full);
88 OHOS::RootView::GetInstance()->Resize(
89 OHOS::Screen::GetInstance().GetWidth(), OHOS::Screen::GetInstance().GetHeight());
90 OHOS::RootView::GetInstance()->Invalidate();
91 }
92
AnimationStart(const int32_t & capacity)93 void ChargerAnimation::AnimationStart(const int32_t& capacity)
94 {
95 if (animationState_ != ANIMATION_START) {
96 pgMgr_.ShowPage(CHARGER_ANIMATION_PAGE_ID);
97 pgMgr_[CHARGER_ANIMATION_PAGE_ID][CHARGER_ANIMATION_COM_ID].As<Updater::ImgViewAdapter>()->Start();
98 animationState_ = ANIMATION_START;
99 }
100 CapacityDisplay(capacity);
101 BATTERY_HILOGD(FEATURE_CHARGING, "Animation has been started");
102 }
103
AnimationStop()104 void ChargerAnimation::AnimationStop()
105 {
106 if (animationState_ != ANIMATION_STOP) {
107 pgMgr_[CHARGER_ANIMATION_PAGE_ID][CHARGER_ANIMATION_COM_ID].As<Updater::ImgViewAdapter>()->Stop();
108 pgMgr_[CHARGER_ANIMATION_PAGE_ID].SetVisible(false);
109 CapacityDestroy();
110 animationState_ = ANIMATION_STOP;
111 }
112 BATTERY_HILOGD(FEATURE_CHARGING, "Animation has been stopped");
113 }
114
CapacityDisplay(const int32_t & capacity)115 void ChargerAnimation::CapacityDisplay(const int32_t& capacity)
116 {
117 pgMgr_[CHARGER_ANIMATION_PAGE_ID][CHARGER_PERCENT_COM_ID].As<Updater::TextLabelAdapter>()->SetText(
118 std::to_string(capacity) + '%');
119 }
120
CapacityDestroy()121 void ChargerAnimation::CapacityDestroy()
122 {
123 pgMgr_[CHARGER_ANIMATION_PAGE_ID][CHARGER_PERCENT_COM_ID]->SetVisible(false);
124 }
125
LackPowerChargingPromptStart()126 void ChargerAnimation::LackPowerChargingPromptStart()
127 {
128 if (chargingPromptState_ != LACKPOWER_CHARGING_PROMPT_START) {
129 pgMgr_.ShowPage(LACKPOWER_CHARGING_STATE_PAGE_ID);
130 auto lackPowerChargingTag = pgMgr_[LACKPOWER_CHARGING_STATE_PAGE_ID][LACKPOWER_CHARGING_STATE_COM_ID]
131 .As<Updater::TextLabelAdapter>()
132 ->GetText();
133 pgMgr_[LACKPOWER_CHARGING_STATE_PAGE_ID][LACKPOWER_CHARGING_STATE_COM_ID]
134 .As<Updater::TextLabelAdapter>()
135 ->SetText(lackPowerChargingTag);
136 pgMgr_[LACKPOWER_CHARGING_STATE_PAGE_ID][LACKPOWER_CHARGING_STATE_COM_ID]->SetVisible(true);
137 chargingPromptState_ = LACKPOWER_CHARGING_PROMPT_START;
138 }
139 }
140
LackPowerChargingPromptStop()141 void ChargerAnimation::LackPowerChargingPromptStop()
142 {
143 if (chargingPromptState_ != LACKPOWER_CHARGING_PROMPT_STOP) {
144 pgMgr_[LACKPOWER_CHARGING_STATE_PAGE_ID][LACKPOWER_CHARGING_STATE_COM_ID]->SetVisible(false);
145 chargingPromptState_ = LACKPOWER_CHARGING_PROMPT_STOP;
146 }
147 }
148
LackPowerNotChargingPromptStart()149 void ChargerAnimation::LackPowerNotChargingPromptStart()
150 {
151 if (notChargingPromptState_ != LACKPOWER_NOTCHARGING_PROMPT_START) {
152 pgMgr_.ShowPage(LACKPOWER_NOT_CHARGING_STATE_PAGE_ID);
153 auto lackPowerNotChargingTag = pgMgr_[LACKPOWER_NOT_CHARGING_STATE_PAGE_ID][LACKPOWER_NOT_CHARGING_STATE_COM_ID]
154 .As<Updater::TextLabelAdapter>()
155 ->GetText();
156 pgMgr_[LACKPOWER_NOT_CHARGING_STATE_PAGE_ID][LACKPOWER_NOT_CHARGING_STATE_COM_ID]
157 .As<Updater::TextLabelAdapter>()
158 ->SetText(lackPowerNotChargingTag);
159 pgMgr_[LACKPOWER_NOT_CHARGING_STATE_PAGE_ID][LACKPOWER_NOT_CHARGING_STATE_COM_ID]->SetVisible(true);
160 notChargingPromptState_ = LACKPOWER_NOTCHARGING_PROMPT_START;
161 }
162 }
163
LackPowerNotChargingPromptStop()164 void ChargerAnimation::LackPowerNotChargingPromptStop()
165 {
166 if (notChargingPromptState_ != LACKPOWER_NOTCHARGING_PROMPT_STOP) {
167 pgMgr_[LACKPOWER_NOT_CHARGING_STATE_PAGE_ID][LACKPOWER_NOT_CHARGING_STATE_COM_ID]->SetVisible(false);
168 notChargingPromptState_ = LACKPOWER_NOTCHARGING_PROMPT_STOP;
169 }
170 }
171 } // namespace PowerMgr
172 } // namespace OHOS
173