1 /*
2  * Copyright (c) 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 "animation_config.h"
17 #include "charger_log.h"
18 #include "config_policy_utils.h"
19 #include "string_ex.h"
20 
21 #include <fstream>
22 #include <sstream>
23 #include <unistd.h>
24 
25 namespace OHOS {
26 namespace PowerMgr {
27 namespace {
28 constexpr const char* ANIMATION_CONFIG_PATH = "/system/etc/charger/resources/animation.json";
29 constexpr const char* CHARGER_ANIMATION_NAME = "animation";
30 constexpr const char* LACKPOWER_CHARGING_NAME = "lackpowerChargingPrompt";
31 constexpr const char* LACKPOWER_NOT_CHARGING_NAME = "lackpowerNotChargingPrompt";
32 constexpr const char* ANIMATION_COM = "components";
33 constexpr const char* ANIMATION_COM_LABEL = "UILabel";
34 constexpr const char* ANIMATION_COM_IMAGEVIEW = "UIImageView";
35 } // namespace
36 
ParseConfig()37 bool AnimationConfig::ParseConfig()
38 {
39     std::ifstream ifs(ANIMATION_CONFIG_PATH);
40     if (!ifs.is_open()) {
41         BATTERY_HILOGE(FEATURE_CHARGING, "open json file failed");
42         return false;
43     }
44     configObj_.clear();
45     configObj_ = nlohmann::json::parse(ifs, nullptr, false);
46     ifs.close();
47     if (configObj_.is_discarded()) {
48         BATTERY_HILOGE(FEATURE_CHARGING, "parse config file error");
49         return false;
50     }
51 
52     auto animation = configObj_[CHARGER_ANIMATION_NAME];
53     ParseAnimationConfig(animation);
54     auto chargingPrompt = configObj_[LACKPOWER_CHARGING_NAME];
55     ParseLackPowerChargingConfig(chargingPrompt);
56     auto notChargingPrompt = configObj_[LACKPOWER_NOT_CHARGING_NAME];
57     ParseLackPowerNotChargingConfig(notChargingPrompt);
58 
59     return true;
60 }
61 
ParseAnimationLabel(nlohmann::json & component,LabelComponentInfo & info)62 void AnimationConfig::ParseAnimationLabel(nlohmann::json& component, LabelComponentInfo& info)
63 {
64     info.common.type = ANIMATION_COM_LABEL;
65     if (component.find("id") != component.end()) {
66         info.common.id = component.at("id").get<std::string>();
67     }
68     if (component.find("text") != component.end()) {
69         info.text = component.at("text").get<std::string>();
70     }
71     if (component.find("x") != component.end()) {
72         info.common.x = component.at("x").get<int>();
73     }
74     if (component.find("y") != component.end()) {
75         info.common.y = component.at("y").get<int>();
76     }
77     if (component.find("w") != component.end()) {
78         info.common.w = component.at("w").get<int>();
79     }
80     if (component.find("h") != component.end()) {
81         info.common.h = component.at("h").get<int>();
82     }
83     if (component.find("fontSize") != component.end()) {
84         info.fontSize = component.at("fontSize").get<int8_t>();
85     }
86     if (component.find("fontColor") != component.end()) {
87         info.fontColor = component.at("fontColor").get<std::string>();
88     }
89     if (component.find("bgColor") != component.end()) {
90         info.bgColor = component.at("bgColor").get<std::string>();
91     }
92     if (component.find("align") != component.end()) {
93         info.align = component.at("align").get<std::string>();
94     }
95 }
96 
ParseAnimationImage(nlohmann::json & component,ImageComponentInfo & info)97 void AnimationConfig::ParseAnimationImage(nlohmann::json& component, ImageComponentInfo& info)
98 {
99     info.common.type = ANIMATION_COM_IMAGEVIEW;
100     if (component.find("id") != component.end()) {
101         info.common.id = component.at("id").get<std::string>();
102     }
103     if (component.find("x") != component.end()) {
104         info.common.x = component.at("x").get<int>();
105     }
106     if (component.find("y") != component.end()) {
107         info.common.y = component.at("y").get<int>();
108     }
109     if (component.find("w") != component.end()) {
110         info.common.w = component.at("w").get<int>();
111     }
112     if (component.find("h") != component.end()) {
113         info.common.h = component.at("h").get<int>();
114     }
115     if (component.find("resPath") != component.end()) {
116         info.resPath = component.at("resPath").get<std::string>();
117     }
118     if (component.find("imgCnt") != component.end()) {
119         info.imgCnt = component.at("imgCnt").get<int>();
120     }
121     if (component.find("updInterval") != component.end()) {
122         info.updInterval = component.at("updInterval").get<int>();
123     }
124     if (component.find("filePrefix") != component.end()) {
125         info.filePrefix = component.at("filePrefix").get<std::string>();
126     }
127 }
128 
ParseAnimationConfig(nlohmann::json & jsonObj)129 void AnimationConfig::ParseAnimationConfig(nlohmann::json& jsonObj)
130 {
131     auto components = jsonObj[ANIMATION_COM];
132     for (auto& component : components) {
133         if (component.find("type") != component.end()) {
134             auto type = component.at("type").get<std::string>();
135             if (type == ANIMATION_COM_IMAGEVIEW) {
136                 ParseAnimationImage(component, animationInfo_.first);
137             } else if (type == ANIMATION_COM_LABEL) {
138                 ParseAnimationLabel(component, animationInfo_.second);
139             }
140         }
141     }
142 }
143 
ParseLackPowerChargingConfig(nlohmann::json & jsonObj)144 void AnimationConfig::ParseLackPowerChargingConfig(nlohmann::json& jsonObj)
145 {
146     auto components = jsonObj[ANIMATION_COM];
147     for (auto& component : components) {
148         if (component.find("type") != component.end()) {
149             auto type = component.at("type").get<std::string>();
150             if (type == ANIMATION_COM_LABEL) {
151                 ParseAnimationLabel(component, chargingInfo_);
152             }
153         }
154     }
155 }
156 
ParseLackPowerNotChargingConfig(nlohmann::json & jsonObj)157 void AnimationConfig::ParseLackPowerNotChargingConfig(nlohmann::json& jsonObj)
158 {
159     auto components = jsonObj[ANIMATION_COM];
160     for (auto& component : components) {
161         if (component.find("type") != component.end()) {
162             auto type = component.at("type").get<std::string>();
163             if (type == ANIMATION_COM_LABEL) {
164                 ParseAnimationLabel(component, notChargingInfo_);
165             }
166         }
167     }
168 }
169 
GetCharingAnimationInfo()170 std::pair<ImageComponentInfo, LabelComponentInfo> AnimationConfig::GetCharingAnimationInfo()
171 {
172     return animationInfo_;
173 }
174 
GetCharingPromptInfo()175 LabelComponentInfo AnimationConfig::GetCharingPromptInfo()
176 {
177     return chargingInfo_;
178 }
179 
GetNotCharingPromptInfo()180 LabelComponentInfo AnimationConfig::GetNotCharingPromptInfo()
181 {
182     return notChargingInfo_;
183 }
184 } // namespace PowerMgr
185 } // namespace OHOS
186