• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "battery_mgr_cjson_utils.h"
18 #include "charger_log.h"
19 #include "config_policy_utils.h"
20 #include "string_ex.h"
21 
22 #include <fstream>
23 #include <sstream>
24 #include <unistd.h>
25 
26 namespace OHOS {
27 namespace PowerMgr {
28 namespace {
29 constexpr const char* ANIMATION_CONFIG_PATH = "/system/etc/charger/resources/animation.json";
30 constexpr const char* CHARGER_ANIMATION_NAME = "animation";
31 constexpr const char* LACKPOWER_CHARGING_NAME = "lackpowerChargingPrompt";
32 constexpr const char* LACKPOWER_NOT_CHARGING_NAME = "lackpowerNotChargingPrompt";
33 constexpr const char* ANIMATION_COM = "components";
34 constexpr const char* ANIMATION_COM_LABEL = "UILabel";
35 constexpr const char* ANIMATION_COM_IMAGEVIEW = "UIImageView";
36 } // namespace
37 
ParseConfig()38 bool AnimationConfig::ParseConfig()
39 {
40     std::ifstream ifs(ANIMATION_CONFIG_PATH);
41     if (!ifs.is_open()) {
42         BATTERY_HILOGE(FEATURE_CHARGING, "open json file failed");
43         return false;
44     }
45 
46     std::string content((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
47     configObj_ = cJSON_Parse(content.c_str());
48     ifs.close();
49     if (!configObj_) {
50         BATTERY_HILOGE(FEATURE_CHARGING, "parse config file error");
51         return false;
52     }
53     if (BatteryMgrJsonUtils::IsEmptyJsonParse(configObj_)) {
54         cJSON_Delete(configObj_);
55         configObj_ = nullptr;
56         BATTERY_HILOGW(FEATURE_CHARGING, "cJSON parse result is empty, animation config is %{public}s",
57             content.c_str());
58         return false;
59     }
60     auto animation = cJSON_GetObjectItemCaseSensitive(configObj_, CHARGER_ANIMATION_NAME);
61     ParseAnimationConfig(animation);
62     auto chargingPrompt = cJSON_GetObjectItemCaseSensitive(configObj_, LACKPOWER_CHARGING_NAME);
63     ParseLackPowerChargingConfig(chargingPrompt);
64     auto notChargingPrompt = cJSON_GetObjectItemCaseSensitive(configObj_, LACKPOWER_NOT_CHARGING_NAME);
65     ParseLackPowerNotChargingConfig(notChargingPrompt);
66     cJSON_Delete(configObj_);
67     configObj_ = nullptr;
68     return true;
69 }
70 
ParseAnimationLabel(cJSON * component,LabelComponentInfo & info)71 void AnimationConfig::ParseAnimationLabel(cJSON* component, LabelComponentInfo& info)
72 {
73     info.common.type = ANIMATION_COM_LABEL;
74     cJSON* idItem = cJSON_GetObjectItemCaseSensitive(component, "id");
75     if (BatteryMgrJsonUtils::IsValidJsonString(idItem)) {
76         info.common.id = idItem->valuestring;
77     }
78     cJSON* textItem = cJSON_GetObjectItemCaseSensitive(component, "text");
79     if (BatteryMgrJsonUtils::IsValidJsonString(textItem)) {
80         info.text = textItem->valuestring;
81     }
82     cJSON* xitem = cJSON_GetObjectItemCaseSensitive(component, "x");
83     if (BatteryMgrJsonUtils::IsValidJsonNumber(xitem)) {
84         info.common.x = xitem->valueint;
85     }
86     cJSON* yItem = cJSON_GetObjectItemCaseSensitive(component, "y");
87     if (BatteryMgrJsonUtils::IsValidJsonNumber(yItem)) {
88         info.common.y = yItem->valueint;
89     }
90     cJSON* wItem = cJSON_GetObjectItemCaseSensitive(component, "w");
91     if (BatteryMgrJsonUtils::IsValidJsonNumber(wItem)) {
92         info.common.w = wItem->valueint;
93     }
94     cJSON* hItem = cJSON_GetObjectItemCaseSensitive(component, "h");
95     if (BatteryMgrJsonUtils::IsValidJsonNumber(hItem)) {
96         info.common.h = hItem->valueint;
97     }
98     cJSON* fontSizeItem = cJSON_GetObjectItemCaseSensitive(component, "fontSize");
99     if (BatteryMgrJsonUtils::IsValidJsonNumber(fontSizeItem)) {
100         info.fontSize = static_cast<int8_t>(fontSizeItem->valueint);
101     }
102     cJSON* fontColorItem = cJSON_GetObjectItemCaseSensitive(component, "fontColor");
103     if (BatteryMgrJsonUtils::IsValidJsonString(fontColorItem)) {
104         info.fontColor = fontColorItem->valuestring;
105     }
106     cJSON* bgColorItem = cJSON_GetObjectItemCaseSensitive(component, "bgColor");
107     if (BatteryMgrJsonUtils::IsValidJsonString(bgColorItem)) {
108         info.bgColor = bgColorItem->valuestring;
109     }
110     cJSON* alignItem = cJSON_GetObjectItemCaseSensitive(component, "align");
111     if (BatteryMgrJsonUtils::IsValidJsonString(alignItem)) {
112         info.align = alignItem->valuestring;
113     }
114 }
115 
ParseAnimationImage(cJSON * component,ImageComponentInfo & info)116 void AnimationConfig::ParseAnimationImage(cJSON* component, ImageComponentInfo& info)
117 {
118     info.common.type = ANIMATION_COM_IMAGEVIEW;
119     cJSON* idItem = cJSON_GetObjectItemCaseSensitive(component, "id");
120     if (BatteryMgrJsonUtils::IsValidJsonString(idItem)) {
121         info.common.id = idItem->valuestring;
122     }
123     cJSON* xitem = cJSON_GetObjectItemCaseSensitive(component, "x");
124     if (BatteryMgrJsonUtils::IsValidJsonNumber(xitem)) {
125         info.common.x = xitem->valueint;
126     }
127     cJSON* yItem = cJSON_GetObjectItemCaseSensitive(component, "y");
128     if (BatteryMgrJsonUtils::IsValidJsonNumber(yItem)) {
129         info.common.y = yItem->valueint;
130     }
131     cJSON* wItem = cJSON_GetObjectItemCaseSensitive(component, "w");
132     if (BatteryMgrJsonUtils::IsValidJsonNumber(wItem)) {
133         info.common.w = wItem->valueint;
134     }
135     cJSON* hItem = cJSON_GetObjectItemCaseSensitive(component, "h");
136     if (BatteryMgrJsonUtils::IsValidJsonNumber(hItem)) {
137         info.common.h = hItem->valueint;
138     }
139     cJSON* resPathItem = cJSON_GetObjectItemCaseSensitive(component, "resPath");
140     if (BatteryMgrJsonUtils::IsValidJsonString(resPathItem)) {
141         info.resPath = resPathItem->valuestring;
142     }
143     cJSON* imgCntItem = cJSON_GetObjectItemCaseSensitive(component, "imgCnt");
144     if (BatteryMgrJsonUtils::IsValidJsonNumber(imgCntItem)) {
145         info.imgCnt = imgCntItem->valueint;
146     }
147     cJSON* updIntervalItem = cJSON_GetObjectItemCaseSensitive(component, "updInterval");
148     if (BatteryMgrJsonUtils::IsValidJsonNumber(updIntervalItem)) {
149         info.updInterval = updIntervalItem->valueint;
150     }
151     cJSON* filePrefixItem = cJSON_GetObjectItemCaseSensitive(component, "filePrefix");
152     if (BatteryMgrJsonUtils::IsValidJsonString(filePrefixItem)) {
153         info.filePrefix = filePrefixItem->valuestring;
154     }
155 }
156 
ParseAnimationConfig(cJSON * jsonObj)157 void AnimationConfig::ParseAnimationConfig(cJSON* jsonObj)
158 {
159     if (!BatteryMgrJsonUtils::IsValidJsonObject(jsonObj)) {
160         BATTERY_HILOGW(FEATURE_CHARGING, "AnimationConfig is invalid");
161         return;
162     }
163     auto components = cJSON_GetObjectItemCaseSensitive(jsonObj, ANIMATION_COM);
164     if (!BatteryMgrJsonUtils::IsJsonArrayOrJsonObject(components)) {
165         BATTERY_HILOGW(FEATURE_CHARGING, "The animationConfig component is not an array or object");
166         return;
167     }
168     cJSON* component = nullptr;
169     cJSON_ArrayForEach(component, components) {
170         auto typeItem = cJSON_GetObjectItemCaseSensitive(component, "type");
171         if (!BatteryMgrJsonUtils::IsValidJsonString(typeItem)) {
172             continue;
173         }
174         if (strcmp(typeItem->valuestring, ANIMATION_COM_IMAGEVIEW) == 0) {
175             ParseAnimationImage(component, animationInfo_.first);
176         } else if (strcmp(typeItem->valuestring, ANIMATION_COM_LABEL) == 0) {
177             ParseAnimationLabel(component, animationInfo_.second);
178         }
179     }
180 }
181 
ParseLackPowerChargingConfig(cJSON * jsonObj)182 void AnimationConfig::ParseLackPowerChargingConfig(cJSON* jsonObj)
183 {
184     if (!BatteryMgrJsonUtils::IsValidJsonObject(jsonObj)) {
185         BATTERY_HILOGW(FEATURE_CHARGING, "LackPowerChargingConfig is invalid");
186         return;
187     }
188     auto components = cJSON_GetObjectItemCaseSensitive(jsonObj, ANIMATION_COM);
189     if (!BatteryMgrJsonUtils::IsJsonArrayOrJsonObject(components)) {
190         BATTERY_HILOGW(FEATURE_CHARGING, "The lackPowerChargingConfig component is not an array or object");
191         return;
192     }
193     cJSON* component = nullptr;
194     cJSON_ArrayForEach(component, components) {
195         auto typeItem = cJSON_GetObjectItemCaseSensitive(component, "type");
196         if (!BatteryMgrJsonUtils::IsValidJsonString(typeItem)) {
197             continue;
198         }
199         if (strcmp(typeItem->valuestring, ANIMATION_COM_LABEL) == 0) {
200             ParseAnimationLabel(component, chargingInfo_);
201         }
202     }
203 }
204 
ParseLackPowerNotChargingConfig(cJSON * jsonObj)205 void AnimationConfig::ParseLackPowerNotChargingConfig(cJSON* jsonObj)
206 {
207     if (!BatteryMgrJsonUtils::IsValidJsonObject(jsonObj)) {
208         BATTERY_HILOGW(FEATURE_CHARGING, "LackPowerNotChargingConfig is invalid");
209         return;
210     }
211     auto components = cJSON_GetObjectItemCaseSensitive(jsonObj, ANIMATION_COM);
212     if (!BatteryMgrJsonUtils::IsJsonArrayOrJsonObject(components)) {
213         BATTERY_HILOGW(FEATURE_CHARGING, "The lackPowerNotChargingConfig component is not an array or object");
214         return;
215     }
216     cJSON* component = nullptr;
217     cJSON_ArrayForEach(component, components) {
218         auto typeItem = cJSON_GetObjectItemCaseSensitive(component, "type");
219         if (!BatteryMgrJsonUtils::IsValidJsonString(typeItem)) {
220             continue;
221         }
222         if (strcmp(typeItem->valuestring, ANIMATION_COM_LABEL) == 0) {
223             ParseAnimationLabel(component, notChargingInfo_);
224         }
225     }
226 }
227 
GetCharingAnimationInfo()228 std::pair<ImageComponentInfo, LabelComponentInfo> AnimationConfig::GetCharingAnimationInfo()
229 {
230     return animationInfo_;
231 }
232 
GetCharingPromptInfo()233 LabelComponentInfo AnimationConfig::GetCharingPromptInfo()
234 {
235     return chargingInfo_;
236 }
237 
GetNotCharingPromptInfo()238 LabelComponentInfo AnimationConfig::GetNotCharingPromptInfo()
239 {
240     return notChargingInfo_;
241 }
242 } // namespace PowerMgr
243 } // namespace OHOS
244