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 <cstdint>
18 #include <sys/types.h>
19 #include <unistd.h>
20 
21 #include "common/graphic_startup.h"
22 #include "common/screen.h"
23 #include "config_policy_utils.h"
24 
25 namespace OHOS {
26 namespace PowerMgr {
27 namespace {
28 constexpr const char* FONT_PATH = "/system/etc/charger/resources/";
29 constexpr const char* TEXT_FONT = "ChargerAnimation_Sans_SC_Regular_Small.ttf";
30 constexpr uint32_t WHITE_BGCOLOR = 0x000000ff;
31 constexpr int N_HEX = 16;
32 }; // namespace
33 
InitConfig()34 bool ChargerAnimation::InitConfig()
35 {
36     OHOS::GraphicStartUp::Init();
37     ChargerGraphicEngine::GetInstance().Init(WHITE_BGCOLOR, OHOS::ColorMode::ARGB8888, FONT_PATH, TEXT_FONT);
38     InitRootView();
39     InitAllComponents();
40     return true;
41 }
42 
43 template <class T>
String2Int(const std::string & str,int base=N_HEX)44 T String2Int(const std::string& str, int base = N_HEX)
45 {
46     char* end = nullptr;
47     if (str.empty()) {
48         errno = EINVAL;
49         return 0;
50     }
51     if (((str[0] == '0') && (str[1] == 'x')) || (str[1] == 'X')) {
52         base = N_HEX;
53     }
54     T result = strtoull(str.c_str(), &end, base);
55     return result;
56 }
57 
StrToColor(const std::string & hexColor)58 static OHOS::ColorType StrToColor(const std::string& hexColor)
59 {
60     std::size_t startPos = 1uL;
61     auto getNextField = [&startPos, &hexColor]() {
62         constexpr std::size_t width = 2ul;
63         uint8_t ret = (startPos > hexColor.size()) ? 0 : String2Int<uint8_t>(hexColor.substr(startPos, width));
64         startPos += width;
65         return ret;
66     };
67     auto r = getNextField();
68     auto g = getNextField();
69     auto b = getNextField();
70     auto a = getNextField();
71     return OHOS::Color::GetColorFromRGBA(r, g, b, a);
72 }
73 
InitLabel(LabelComponentInfo & info)74 static std::unique_ptr<UILabel> InitLabel(LabelComponentInfo& info)
75 {
76     auto label = std::make_unique<UILabel>();
77     label->SetViewId(info.common.id.c_str());
78     label->SetPosition(info.common.x, info.common.y, info.common.w, info.common.h);
79     label->SetText(info.text.c_str());
80     label->SetFont(TEXT_FONT, info.fontSize);
81     label->SetStyle(OHOS::STYLE_TEXT_COLOR, StrToColor(info.fontColor.c_str()).full);
82     label->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, StrToColor(info.bgColor.c_str()).full);
83     label->SetVisible(info.common.visible);
84     return label;
85 }
86 
InitImageView(ImageComponentInfo & info)87 static std::shared_ptr<UIImageView> InitImageView(ImageComponentInfo& info)
88 {
89     auto animatorImage = std::make_shared<UIImageView>();
90     animatorImage->SetViewId(info.common.id.c_str());
91     animatorImage->SetPosition(info.common.x, info.common.y, info.common.w, info.common.h);
92     animatorImage->SetVisible(info.common.visible);
93     return animatorImage;
94 }
95 
InitAllComponents()96 bool ChargerAnimation::InitAllComponents()
97 {
98     auto animationConfig = std::make_shared<AnimationConfig>();
99     auto ret = animationConfig->ParseConfig();
100     if (!ret) {
101         return false;
102     }
103     auto [animationImageInfo, animationLabelInfo] = animationConfig->GetCharingAnimationInfo();
104     auto chargingLabelInfo = animationConfig->GetCharingPromptInfo();
105     auto notChargingLabelInfo = animationConfig->GetNotCharingPromptInfo();
106 
107     lackPower_ = InitLabel(chargingLabelInfo);
108     lackPowerNotCharge_ = InitLabel(notChargingLabelInfo);
109     percentLabel_ = InitLabel(animationLabelInfo);
110     animatorImage_ = InitImageView(animationImageInfo);
111     auto container = std::make_unique<UIViewGroup>();
112     auto width = ChargerGraphicEngine::GetInstance().GetScreenWidth();
113     auto height = ChargerGraphicEngine::GetInstance().GetScreenHeight();
114     container->SetPosition(0, 0, width, height);
115     container->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, OHOS::Color::Black().full);
116     container->Add(lackPower_.get());
117     container->Add(lackPowerNotCharge_.get());
118     container->Add(animatorImage_.get());
119     container->Add(percentLabel_.get());
120     container->SetVisible(true);
121     OHOS::RootView::GetInstance()->Add(container.release());
122     animatorCallback_ = std::make_shared<ChargerAnimatorCallback>(animatorImage_, animationImageInfo);
123     animatorCallback_->Init();
124     return true;
125 }
126 
InitRootView()127 void ChargerAnimation::InitRootView()
128 {
129     OHOS::RootView::GetInstance()->SetPosition(0, 0);
130     OHOS::RootView::GetInstance()->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, OHOS::Color::Black().full);
131     OHOS::RootView::GetInstance()->Resize(
132         OHOS::Screen::GetInstance().GetWidth(), OHOS::Screen::GetInstance().GetHeight());
133     OHOS::RootView::GetInstance()->Invalidate();
134 }
135 
AnimationStart(const int32_t & capacity)136 void ChargerAnimation::AnimationStart(const int32_t& capacity)
137 {
138     if (animationState_ != ANIMATION_START) {
139         animatorCallback_->Start();
140         animationState_ = ANIMATION_START;
141     }
142     CapacityDisplay(capacity);
143     BATTERY_HILOGD(FEATURE_CHARGING, "Animation has been started");
144 }
145 
AnimationStop()146 void ChargerAnimation::AnimationStop()
147 {
148     if (animationState_ != ANIMATION_STOP) {
149         animatorCallback_->Stop();
150         CapacityDestroy();
151         animationState_ = ANIMATION_STOP;
152     }
153     BATTERY_HILOGD(FEATURE_CHARGING, "Animation has been stopped");
154 }
155 
CapacityDisplay(const int32_t & capacity)156 void ChargerAnimation::CapacityDisplay(const int32_t& capacity)
157 {
158     percentLabel_->SetText((std::to_string(capacity) + '%').c_str());
159     percentLabel_->SetVisible(true);
160 }
161 
CapacityDestroy()162 void ChargerAnimation::CapacityDestroy()
163 {
164     percentLabel_->SetVisible(false);
165 }
166 
LackPowerChargingPromptStart()167 void ChargerAnimation::LackPowerChargingPromptStart()
168 {
169     if (chargingPromptState_ != LACKPOWER_CHARGING_PROMPT_START) {
170         lackPower_->SetVisible(true);
171         chargingPromptState_ = LACKPOWER_CHARGING_PROMPT_START;
172     }
173 }
174 
LackPowerChargingPromptStop()175 void ChargerAnimation::LackPowerChargingPromptStop()
176 {
177     if (chargingPromptState_ != LACKPOWER_CHARGING_PROMPT_STOP) {
178         lackPower_->SetVisible(false);
179         chargingPromptState_ = LACKPOWER_CHARGING_PROMPT_STOP;
180     }
181 }
182 
LackPowerNotChargingPromptStart()183 void ChargerAnimation::LackPowerNotChargingPromptStart()
184 {
185     if (notChargingPromptState_ != LACKPOWER_NOTCHARGING_PROMPT_START) {
186         lackPowerNotCharge_->SetVisible(true);
187         notChargingPromptState_ = LACKPOWER_NOTCHARGING_PROMPT_START;
188     }
189 }
190 
LackPowerNotChargingPromptStop()191 void ChargerAnimation::LackPowerNotChargingPromptStop()
192 {
193     if (notChargingPromptState_ != LACKPOWER_NOTCHARGING_PROMPT_STOP) {
194         lackPowerNotCharge_->SetVisible(false);
195         notChargingPromptState_ = LACKPOWER_NOTCHARGING_PROMPT_STOP;
196     }
197 }
198 } // namespace PowerMgr
199 } // namespace OHOS
200