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