1 /*
2 * Copyright (c) 2024 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 "ui/base/ace_type.h"
17 #include "ui/resource/resource_parser.h"
18
19 #include "base/utils/string_utils.h"
20 #include "base/utils/system_properties.h"
21 #include "core/common/container.h"
22 #include "core/common/resource/resource_manager.h"
23 #include "core/common/resource/resource_wrapper.h"
24
25 namespace OHOS::Ace::Kit {
CreateResourceWrapper(const ResourceInfo & info)26 static RefPtr<Ace::ResourceAdapter> CreateResourceWrapper(const ResourceInfo& info)
27 {
28 auto bundleName = info.bundleName;
29 auto moduleName = info.moduleName;
30
31 RefPtr<Ace::ResourceAdapter> resourceAdapter = nullptr;
32 if (bundleName.has_value() && moduleName.has_value()) {
33 auto resourceObject = AceType::MakeRefPtr<Ace::ResourceObject>(
34 bundleName.value_or(""), moduleName.value_or(""), Container::CurrentIdSafely());
35 resourceAdapter = Ace::ResourceManager::GetInstance().GetOrCreateResourceAdapter(resourceObject);
36 } else {
37 resourceAdapter = Ace::ResourceManager::GetInstance().GetResourceAdapter(Container::CurrentIdSafely());
38 }
39 return resourceAdapter;
40 }
41
GetDimension(const ResourceInfo & resourceInfo,Ace::CalcDimension & dimension)42 bool ResourceParser::GetDimension(const ResourceInfo& resourceInfo, Ace::CalcDimension& dimension)
43 {
44 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
45 if (!resourceWrapper) {
46 return false;
47 }
48
49 auto resType = resourceInfo.type;
50 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
51 if (resourceInfo.params.empty()) {
52 return false;
53 }
54
55 auto param = resourceInfo.params[0];
56 if (resType == static_cast<int32_t>(ResourceType::STRING)) {
57 auto value = resourceWrapper->GetStringByName(param);
58 return StringUtils::StringToCalcDimensionNG(value, dimension, false, DimensionUnit::VP);
59 }
60 if (resType == static_cast<int32_t>(ResourceType::INTEGER)) {
61 auto value = std::to_string(resourceWrapper->GetIntByName(param));
62 return StringUtils::StringToDimensionWithUnitNG(value, dimension, DimensionUnit::VP);
63 }
64 dimension = resourceWrapper->GetDimensionByName(param);
65 return true;
66 } else {
67 if (resType == static_cast<int32_t>(ResourceType::STRING)) {
68 auto value = resourceWrapper->GetString(static_cast<uint32_t>(resourceInfo.resId));
69 return StringUtils::StringToCalcDimensionNG(value, dimension, false, DimensionUnit::VP);
70 }
71 if (resType == static_cast<int32_t>(ResourceType::INTEGER)) {
72 auto value = std::to_string(resourceWrapper->GetInt(static_cast<uint32_t>(resourceInfo.resId)));
73 return StringUtils::StringToDimensionWithUnitNG(value, dimension, DimensionUnit::VP);
74 }
75 if (resType == static_cast<int32_t>(ResourceType::FLOAT)) {
76 dimension = resourceWrapper->GetDimension(static_cast<uint32_t>(resourceInfo.resId));
77 return true;
78 }
79 }
80 return false;
81 }
82
GetColor(const ResourceInfo & resourceInfo,Ace::Color & color)83 bool ResourceParser::GetColor(const ResourceInfo& resourceInfo, Ace::Color& color)
84 {
85 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
86 if (!resourceWrapper) {
87 return false;
88 }
89
90 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
91 color = resourceWrapper->GetColorByName(resourceInfo.params[0]);
92 } else {
93 color = resourceWrapper->GetColor(resourceInfo.resId);
94 }
95 return true;
96 }
97
GetString(const ResourceInfo & resourceInfo,std::string & str)98 bool ResourceParser::GetString(const ResourceInfo& resourceInfo, std::string& str)
99 {
100 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
101 if (!resourceWrapper) {
102 return false;
103 }
104
105 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
106 str = resourceWrapper->GetStringByName(resourceInfo.params[0]);
107 } else {
108 str = resourceWrapper->GetString(resourceInfo.resId);
109 }
110 return true;
111 }
112
GetMediaPath(const ResourceInfo & resourceInfo,std::string & mediaPath)113 bool ResourceParser::GetMediaPath(const ResourceInfo& resourceInfo, std::string& mediaPath)
114 {
115 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
116 if (!resourceWrapper) {
117 return false;
118 }
119
120 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
121 mediaPath = resourceWrapper->GetMediaPathByName(resourceInfo.params[0]);
122 } else {
123 mediaPath = resourceWrapper->GetMediaPath(resourceInfo.resId);
124 }
125 return true;
126 }
127
GetInt(const ResourceInfo & resourceInfo,int32_t & intRes)128 bool ResourceParser::GetInt(const ResourceInfo& resourceInfo, int32_t& intRes)
129 {
130 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
131 if (!resourceWrapper) {
132 return false;
133 }
134
135 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
136 intRes = resourceWrapper->GetIntByName(resourceInfo.params[0]);
137 } else {
138 intRes = resourceWrapper->GetInt(resourceInfo.resId);
139 }
140 return true;
141 }
142
GetDouble(const ResourceInfo & resourceInfo,double & doubleRes)143 bool ResourceParser::GetDouble(const ResourceInfo& resourceInfo, double& doubleRes)
144 {
145 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
146 if (!resourceWrapper) {
147 return false;
148 }
149
150 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
151 doubleRes = resourceWrapper->GetDoubleByName(resourceInfo.params[0]);
152 } else {
153 doubleRes = resourceWrapper->GetDouble(resourceInfo.resId);
154 }
155 return true;
156 }
157
GetPluralString(const ResourceInfo & resourceInfo,int count,std::string & str)158 bool ResourceParser::GetPluralString(const ResourceInfo& resourceInfo, int count, std::string& str)
159 {
160 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
161 if (!resourceWrapper) {
162 return false;
163 }
164
165 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
166 str = resourceWrapper->GetPluralStringByName(resourceInfo.params[0], count);
167 } else {
168 str = resourceWrapper->GetPluralString(resourceInfo.resId, count);
169 }
170 return true;
171 }
172
GetBoolean(const ResourceInfo & resourceInfo,bool & boolRes)173 bool ResourceParser::GetBoolean(const ResourceInfo& resourceInfo, bool& boolRes)
174 {
175 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
176 if (!resourceWrapper) {
177 return false;
178 }
179
180 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
181 boolRes = resourceWrapper->GetBooleanByName(resourceInfo.params[0]);
182 } else {
183 boolRes = resourceWrapper->GetBoolean(resourceInfo.resId);
184 }
185 return true;
186 }
187
GetIntArray(const ResourceInfo & resourceInfo,std::vector<uint32_t> & intArray)188 bool ResourceParser::GetIntArray(const ResourceInfo& resourceInfo, std::vector<uint32_t>& intArray)
189 {
190 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
191 if (!resourceWrapper) {
192 return false;
193 }
194
195 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
196 intArray = resourceWrapper->GetIntArrayByName(resourceInfo.params[0]);
197 } else {
198 intArray = resourceWrapper->GetIntArray(resourceInfo.resId);
199 }
200 return true;
201 }
202
GetStringArray(const ResourceInfo & resourceInfo,std::vector<std::string> & strArray)203 bool ResourceParser::GetStringArray(const ResourceInfo& resourceInfo, std::vector<std::string>& strArray)
204 {
205 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
206 if (!resourceWrapper) {
207 return false;
208 }
209
210 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
211 strArray = resourceWrapper->GetStringArrayByName(resourceInfo.params[0]);
212 } else {
213 strArray = resourceWrapper->GetStringArray(resourceInfo.resId);
214 }
215 return true;
216 }
217
GetSymbol(const ResourceInfo & resourceInfo,uint32_t & symbolRes)218 bool ResourceParser::GetSymbol(const ResourceInfo& resourceInfo, uint32_t& symbolRes)
219 {
220 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
221 if (!resourceWrapper) {
222 return false;
223 }
224
225 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
226 symbolRes = resourceWrapper->GetSymbolByName(resourceInfo.params[0].c_str());
227 } else {
228 symbolRes = resourceWrapper->GetSymbolById(resourceInfo.resId);
229 }
230 return true;
231 }
232
GetMediaData(const ResourceInfo & resourceInfo,size_t & len,std::unique_ptr<uint8_t[]> & dest)233 bool ResourceParser::GetMediaData(const ResourceInfo& resourceInfo, size_t& len, std::unique_ptr<uint8_t[]>& dest)
234 {
235 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
236 if (!resourceWrapper) {
237 return false;
238 }
239
240 // resourceId is invalid
241 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
242 return resourceWrapper->GetMediaData(resourceInfo.params[0].c_str(), len, dest);
243 }
244 return resourceWrapper->GetMediaData(resourceInfo.resId, len, dest);
245 }
246
247 } // namespace OHOS::Ace::Kit
248