1 /*
2 * Copyright (c) 2021-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 "resource_manager_impl.h"
17
18 #include <cmath>
19 #include <cstdarg>
20 #include <cstdlib>
21 #include <cstring>
22 #include <fcntl.h>
23 #include <regex>
24 #include <sstream>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <fstream>
28
29 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
30 #include "hitrace_meter.h"
31 #endif
32 #include "hilog_wrapper.h"
33 #include "res_config.h"
34 #include "securec.h"
35 #include "system_resource_manager.h"
36 #include "utils/common.h"
37 #include "utils/string_utils.h"
38 #include "utils/utils.h"
39 #include "tuple"
40
41 namespace OHOS {
42 namespace Global {
43 namespace Resource {
44 // default logLevel
45 #ifdef CONFIG_HILOG
46 LogLevel g_logLevel = LOG_INFO;
47 #endif
48
49 constexpr int HEX_ADECIMAL = 16;
50 const std::string FOREGROUND = "foreground";
51 const std::string BACKGROUND = "background";
52 const std::regex FLOAT_REGEX = std::regex("(\\+|-)?\\d+(\\.\\d+)? *(px|vp|fp)?");
53
AddSystemResource(ResourceManagerImpl * systemResourceManager)54 void ResourceManagerImpl::AddSystemResource(ResourceManagerImpl *systemResourceManager)
55 {
56 if (systemResourceManager != nullptr) {
57 this->hapManager_->AddSystemResource(systemResourceManager->hapManager_);
58 }
59 }
60
ResourceManagerImpl(bool isOverrideResMgr)61 ResourceManagerImpl::ResourceManagerImpl(bool isOverrideResMgr) : hapManager_(nullptr),
62 isOverrideResMgr_(isOverrideResMgr)
63 {
64 psueManager_ = std::make_shared<PsueManager>();
65 }
66
Init(bool isSystem)67 bool ResourceManagerImpl::Init(bool isSystem)
68 {
69 auto resConfig = std::make_shared<ResConfigImpl>();
70 if (resConfig == nullptr) {
71 RESMGR_HILOGE(RESMGR_TAG, "new ResConfigImpl failed when ResourceManagerImpl::Init");
72 return false;
73 }
74 hapManager_ = std::make_shared<HapManager>(resConfig, isSystem);
75 if (hapManager_ == nullptr) {
76 RESMGR_HILOGE(RESMGR_TAG, "new HapManager failed when ResourceManagerImpl::Init");
77 return false;
78 }
79 return true;
80 }
81
Init(std::shared_ptr<HapManager> hapManager)82 bool ResourceManagerImpl::Init(std::shared_ptr<HapManager> hapManager)
83 {
84 if (hapManager == nullptr) {
85 RESMGR_HILOGE(RESMGR_TAG, "ResourceManagerImpl::Init, hapManager is nullptr");
86 return false;
87 }
88 this->hapManager_ = hapManager;
89 return true;
90 }
91
GetStringById(uint32_t id,std::string & outValue)92 RState ResourceManagerImpl::GetStringById(uint32_t id, std::string &outValue)
93 {
94 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceById(id, isOverrideResMgr_);
95 if (idItem == nullptr) {
96 RESMGR_HILOGE(RESMGR_TAG, "GetStringById error id = %{public}d", id);
97 return ERROR_CODE_RES_ID_NOT_FOUND;
98 }
99 RState state = GetString(idItem, outValue);
100 if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
101 return ERROR_CODE_RES_NOT_FOUND_BY_ID;
102 }
103 return state;
104 }
105
GetStringByName(const char * name,std::string & outValue)106 RState ResourceManagerImpl::GetStringByName(const char *name, std::string &outValue)
107 {
108 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceByName(name, ResType::STRING, isOverrideResMgr_);
109 if (idItem == nullptr) {
110 RESMGR_HILOGE(RESMGR_TAG, "GetStringByName error name = %{public}s", name);
111 return ERROR_CODE_RES_NAME_NOT_FOUND;
112 }
113 RState state = GetString(idItem, outValue);
114 if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
115 return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
116 }
117 return state;
118 }
119
GetStringFormatById(std::string & outValue,uint32_t id,...)120 RState ResourceManagerImpl::GetStringFormatById(std::string &outValue, uint32_t id, ...)
121 {
122 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceById(id, isOverrideResMgr_);
123 std::string temp;
124 RState rState = GetString(idItem, temp);
125 if (rState != SUCCESS) {
126 return rState;
127 }
128 va_list args;
129 va_start(args, id);
130 outValue = FormatString(temp.c_str(), args);
131 va_end(args);
132 return SUCCESS;
133 }
134
GetStringFormatByName(std::string & outValue,const char * name,...)135 RState ResourceManagerImpl::GetStringFormatByName(std::string &outValue, const char *name, ...)
136 {
137 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceByName(name, ResType::STRING, isOverrideResMgr_);
138 std::string temp;
139 RState rState = GetString(idItem, temp);
140 if (rState != SUCCESS) {
141 return rState;
142 }
143 va_list args;
144 va_start(args, name);
145 outValue = FormatString(temp.c_str(), args);
146 va_end(args);
147 return SUCCESS;
148 }
149
GetStringFormatById(std::string & outValue,uint32_t id,va_list args)150 RState ResourceManagerImpl::GetStringFormatById(std::string &outValue, uint32_t id, va_list args)
151 {
152 RState state = GetStringById(id, outValue);
153 if (state != SUCCESS) {
154 return state;
155 }
156 std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> jsParams;
157 if (parseArgs(outValue, args, jsParams)) {
158 ResConfigImpl resConfig;
159 GetResConfig(resConfig);
160 if (!ReplacePlaceholderWithParams(outValue, resConfig, jsParams)) {
161 return ERROR_CODE_RES_ID_FORMAT_ERROR;
162 }
163 return SUCCESS;
164 }
165 return ERROR_CODE_INVALID_INPUT_PARAMETER;
166 }
167
GetStringFormatByName(std::string & outValue,const char * name,va_list args)168 RState ResourceManagerImpl::GetStringFormatByName(std::string &outValue, const char *name, va_list args)
169 {
170 RState state = GetStringByName(name, outValue);
171 if (state != SUCCESS) {
172 return state;
173 }
174 std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> jsParams;
175 if (parseArgs(outValue, args, jsParams)) {
176 ResConfigImpl resConfig;
177 GetResConfig(resConfig);
178 if (!ReplacePlaceholderWithParams(outValue, resConfig, jsParams)) {
179 return ERROR_CODE_RES_NAME_FORMAT_ERROR;
180 }
181 return SUCCESS;
182 }
183 return ERROR_CODE_INVALID_INPUT_PARAMETER;
184 }
185
GetString(const std::shared_ptr<IdItem> idItem,std::string & outValue)186 RState ResourceManagerImpl::GetString(const std::shared_ptr<IdItem> idItem, std::string &outValue)
187 {
188 // not found or type invalid
189 if (idItem == nullptr || idItem->resType_ != ResType::STRING) {
190 return NOT_FOUND;
191 }
192 RState ret = ResolveReference(idItem->value_, outValue);
193 if (isFakeLocale) {
194 ProcessPsuedoTranslate(outValue);
195 }
196 if (isBidirectionFakeLocale) {
197 outValue = psueManager_->BidirectionConvert(outValue);
198 }
199 if (ret != SUCCESS) {
200 return ret;
201 }
202 return SUCCESS;
203 }
204
GetStringArrayById(uint32_t id,std::vector<std::string> & outValue)205 RState ResourceManagerImpl::GetStringArrayById(uint32_t id, std::vector<std::string> &outValue)
206 {
207 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceById(id, isOverrideResMgr_);
208 if (idItem == nullptr) {
209 RESMGR_HILOGE(RESMGR_TAG, "GetStringArrayById error id = %{public}d", id);
210 return ERROR_CODE_RES_ID_NOT_FOUND;
211 }
212 RState state = GetStringArray(idItem, outValue);
213 if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
214 return ERROR_CODE_RES_NOT_FOUND_BY_ID;
215 }
216 return state;
217 }
218
GetStringArrayByName(const char * name,std::vector<std::string> & outValue)219 RState ResourceManagerImpl::GetStringArrayByName(const char *name, std::vector<std::string> &outValue)
220 {
221 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceByName(
222 name, ResType::STRINGARRAY, isOverrideResMgr_);
223 if (idItem == nullptr) {
224 RESMGR_HILOGE(RESMGR_TAG, "GetStringArrayByName error name = %{public}s", name);
225 return ERROR_CODE_RES_NAME_NOT_FOUND;
226 }
227 RState state = GetStringArray(idItem, outValue);
228 if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
229 return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
230 }
231 return state;
232 }
233
GetStringArray(const std::shared_ptr<IdItem> idItem,std::vector<std::string> & outValue)234 RState ResourceManagerImpl::GetStringArray(const std::shared_ptr<IdItem> idItem, std::vector<std::string> &outValue)
235 {
236 // not found or type invalid
237 if (idItem == nullptr || idItem->resType_ != ResType::STRINGARRAY) {
238 return NOT_FOUND;
239 }
240 outValue.clear();
241
242 for (size_t i = 0; i < idItem->values_.size(); ++i) {
243 std::string resolvedValue;
244 RState rrRet = ResolveReference(idItem->values_[i], resolvedValue);
245 if (rrRet != SUCCESS) {
246 RESMGR_HILOGD(RESMGR_TAG,
247 "GetStringArray ResolveReference failed, value:%{public}s", idItem->values_[i].c_str());
248 return rrRet;
249 }
250 outValue.push_back(resolvedValue);
251 }
252 if (isFakeLocale) {
253 for (auto &iter : outValue) {
254 ProcessPsuedoTranslate(iter);
255 }
256 }
257 if (isBidirectionFakeLocale) {
258 for (auto &iter : outValue) {
259 iter = psueManager_->BidirectionConvert(iter);
260 }
261 }
262 return SUCCESS;
263 }
264
GetPatternById(uint32_t id,std::map<std::string,std::string> & outValue)265 RState ResourceManagerImpl::GetPatternById(uint32_t id, std::map<std::string, std::string> &outValue)
266 {
267 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceById(id, isOverrideResMgr_);
268 if (idItem == nullptr) {
269 RESMGR_HILOGE(RESMGR_TAG, "GetPatternById error id = %{public}d", id);
270 return ERROR_CODE_RES_ID_NOT_FOUND;
271 }
272 RState state = GetPattern(idItem, outValue);
273 if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
274 return ERROR_CODE_RES_NOT_FOUND_BY_ID;
275 }
276 return state;
277 }
278
GetPatternByName(const char * name,std::map<std::string,std::string> & outValue)279 RState ResourceManagerImpl::GetPatternByName(const char *name, std::map<std::string, std::string> &outValue)
280 {
281 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceByName(name, ResType::PATTERN, isOverrideResMgr_);
282 if (idItem == nullptr) {
283 RESMGR_HILOGE(RESMGR_TAG, "GetPatternByName error name = %{public}s", name);
284 return ERROR_CODE_RES_NAME_NOT_FOUND;
285 }
286 RState state = GetPattern(idItem, outValue);
287 if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
288 return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
289 }
290 return state;
291 }
292
GetPattern(const std::shared_ptr<IdItem> idItem,std::map<std::string,std::string> & outValue)293 RState ResourceManagerImpl::GetPattern(const std::shared_ptr<IdItem> idItem, std::map<std::string,
294 std::string> &outValue)
295 {
296 //type invalid
297 if (idItem->resType_ != ResType::PATTERN) {
298 RESMGR_HILOGE(RESMGR_TAG,
299 "actual resType = %{public}d, expect resType = %{public}d", idItem->resType_, ResType::PATTERN);
300 return NOT_FOUND;
301 }
302 return ResolveParentReference(idItem, outValue);
303 }
304
GetPluralStringById(uint32_t id,int quantity,std::string & outValue)305 RState ResourceManagerImpl::GetPluralStringById(uint32_t id, int quantity, std::string &outValue)
306 {
307 const std::shared_ptr<HapResource::ValueUnderQualifierDir> vuqd = hapManager_->FindQualifierValueById(id,
308 isOverrideResMgr_);
309 return GetPluralString(vuqd, quantity, outValue);
310 }
311
GetPluralStringByName(const char * name,int quantity,std::string & outValue)312 RState ResourceManagerImpl::GetPluralStringByName(const char *name, int quantity, std::string &outValue)
313 {
314 const std::shared_ptr<HapResource::ValueUnderQualifierDir> vuqd =
315 hapManager_->FindQualifierValueByName(name, ResType::PLURALS, isOverrideResMgr_);
316 return GetPluralString(vuqd, quantity, outValue);
317 }
318
GetPluralStringByIdFormat(std::string & outValue,uint32_t id,int quantity,...)319 RState ResourceManagerImpl::GetPluralStringByIdFormat(std::string &outValue, uint32_t id, int quantity, ...)
320 {
321 const std::shared_ptr<HapResource::ValueUnderQualifierDir> vuqd = hapManager_->FindQualifierValueById(id,
322 isOverrideResMgr_);
323 if (vuqd == nullptr) {
324 RESMGR_HILOGE(RESMGR_TAG, "GetPluralStringByIdFormat error id = %{public}d", id);
325 return ERROR_CODE_RES_ID_NOT_FOUND;
326 }
327 std::string temp;
328 RState rState = GetPluralString(vuqd, quantity, temp);
329 if (rState == ERROR_CODE_RES_REF_TOO_MUCH) {
330 return rState;
331 }
332 if (rState != SUCCESS) {
333 return ERROR_CODE_RES_NOT_FOUND_BY_ID;
334 }
335
336 va_list args;
337 va_start(args, quantity);
338 outValue = FormatString(temp.c_str(), args);
339 va_end(args);
340
341 return SUCCESS;
342 }
343
GetPluralStringByNameFormat(std::string & outValue,const char * name,int quantity,...)344 RState ResourceManagerImpl::GetPluralStringByNameFormat(std::string &outValue, const char *name, int quantity, ...)
345 {
346 const std::shared_ptr<HapResource::ValueUnderQualifierDir> vuqd =
347 hapManager_->FindQualifierValueByName(name, ResType::PLURALS, isOverrideResMgr_);
348 if (vuqd == nullptr) {
349 RESMGR_HILOGE(RESMGR_TAG, "GetPluralStringByNameFormat error name = %{public}s", name);
350 return ERROR_CODE_RES_NAME_NOT_FOUND;
351 }
352 std::string temp;
353 RState rState = GetPluralString(vuqd, quantity, temp);
354 if (rState == ERROR_CODE_RES_REF_TOO_MUCH) {
355 return rState;
356 }
357 if (rState != SUCCESS) {
358 return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
359 }
360
361 va_list args;
362 va_start(args, quantity);
363 outValue = FormatString(temp.c_str(), args);
364 va_end(args);
365
366 return SUCCESS;
367 }
368
GetPluralString(const std::shared_ptr<HapResource::ValueUnderQualifierDir> vuqd,int quantity,std::string & outValue)369 RState ResourceManagerImpl::GetPluralString(const std::shared_ptr<HapResource::ValueUnderQualifierDir> vuqd,
370 int quantity, std::string &outValue)
371 {
372 // not found or type invalid
373 if (vuqd == nullptr) {
374 return NOT_FOUND;
375 }
376 auto idItem = vuqd->GetIdItem();
377 if (idItem == nullptr || idItem->resType_ != ResType::PLURALS) {
378 return NOT_FOUND;
379 }
380 std::map<std::string, std::string> map;
381
382 size_t startIdx = 0;
383 size_t loop = idItem->values_.size() / 2;
384 for (size_t i = 0; i < loop; ++i) {
385 std::string key(idItem->values_[startIdx + i * 2]); // 2 means keyappear in pairs
386 std::string value(idItem->values_[startIdx + i * 2 + 1]); // 2 means value appear in pairs
387 auto iter = map.find(key);
388 if (iter == map.end()) {
389 std::string resolvedValue;
390 RState rrRet = ResolveReference(value, resolvedValue);
391 if (rrRet != SUCCESS) {
392 RESMGR_HILOGD(RESMGR_TAG, "ResolveReference failed, value:%{public}s", value.c_str());
393 return rrRet;
394 }
395 map[key] = resolvedValue;
396 }
397 }
398
399 std::string converted = hapManager_->GetPluralRulesAndSelect(quantity, isOverrideResMgr_);
400 auto mapIter = map.find(converted);
401 if (mapIter == map.end()) {
402 mapIter = map.find("other");
403 if (mapIter == map.end()) {
404 return NOT_FOUND;
405 }
406 }
407 outValue = mapIter->second;
408 if (isFakeLocale) {
409 ProcessPsuedoTranslate(outValue);
410 }
411 if (isBidirectionFakeLocale) {
412 outValue = psueManager_->BidirectionConvert(outValue);
413 }
414 return SUCCESS;
415 }
416
ResolveReference(const std::string value,std::string & outValue)417 RState ResourceManagerImpl::ResolveReference(const std::string value, std::string &outValue)
418 {
419 int id;
420 ResType resType;
421 bool isRef = true;
422 int count = 0;
423 std::string refStr(value);
424 while (isRef) {
425 isRef = IdItem::IsRef(refStr, resType, id);
426 if (!isRef) {
427 outValue = refStr;
428 return SUCCESS;
429 }
430
431 if (IdItem::IsArrayOfType(resType)) {
432 // can't be array
433 RESMGR_HILOGD(RESMGR_TAG, "ref %{public}s can't be array", refStr.c_str());
434 return ERROR;
435 }
436 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceById(id, isOverrideResMgr_);
437 if (idItem == nullptr) {
438 RESMGR_HILOGE(RESMGR_TAG, "ref %s id not found", refStr.c_str());
439 return ERROR;
440 }
441 // unless compile bug
442 if (resType != idItem->resType_) {
443 RESMGR_HILOGE(RESMGR_TAG,
444 "impossible. ref %s type mismatch, found type: %d", refStr.c_str(), idItem->resType_);
445 return ERROR;
446 }
447
448 refStr = idItem->value_;
449
450 if (++count > MAX_DEPTH_REF_SEARCH) {
451 RESMGR_HILOGE(RESMGR_TAG, "ref %s has re-ref too much", value.c_str());
452 return ERROR_CODE_RES_REF_TOO_MUCH;
453 }
454 }
455 return SUCCESS;
456 }
457
GetThemeValues(const std::string & value,std::string & outValue)458 RState ResourceManagerImpl::GetThemeValues(const std::string &value, std::string &outValue)
459 {
460 ResConfigImpl resConfig;
461 GetResConfig(resConfig);
462 std::vector<std::shared_ptr<IdItem>> idItems;
463 if (ProcessReference(value, idItems) != SUCCESS) {
464 return NOT_FOUND;
465 }
466 outValue = ThemePackManager::GetThemePackManager()->FindThemeResource(bundleInfo, idItems, resConfig,
467 hapManager_->IsThemeSystemResEnableHap());
468 return outValue.empty() ? NOT_FOUND : SUCCESS;
469 }
470
ResolveParentReference(const std::shared_ptr<IdItem> idItem,std::map<std::string,std::string> & outValue)471 RState ResourceManagerImpl::ResolveParentReference(const std::shared_ptr<IdItem> idItem, std::map<std::string,
472 std::string> &outValue)
473 {
474 // only pattern and theme
475 // ref always at idx 0
476 // child will cover parent
477 outValue.clear();
478
479 bool haveParent = false;
480 int count = 0;
481 std::shared_ptr<IdItem> currItem = idItem;
482 do {
483 haveParent = currItem->HaveParent();
484 size_t startIdx = haveParent ? 1 : 0;
485 // add currItem values into map when key is absent
486 // this make sure child covers parent
487 size_t loop = currItem->values_.size() / 2;
488 for (size_t i = 0; i < loop; ++i) {
489 std::string key(currItem->values_[startIdx + i * 2]); // 2 means key appear in pairs
490 std::string value(currItem->values_[startIdx + i * 2 + 1]); // 2 means value appear in pairs
491 auto iter = outValue.find(key);
492 if (iter != outValue.end()) {
493 continue;
494 }
495 std::string resolvedValue;
496 if (GetThemeValues(value, resolvedValue) == SUCCESS) {
497 outValue[key] = resolvedValue;
498 continue;
499 }
500 RState rrRet = ResolveReference(value, resolvedValue);
501 if (rrRet != SUCCESS) {
502 RESMGR_HILOGD(RESMGR_TAG, "ResolveReference failed, value:%{public}s", value.c_str());
503 return ERROR;
504 }
505 outValue[key] = resolvedValue;
506 }
507 if (haveParent) {
508 // get parent
509 int id;
510 ResType resType;
511 bool isRef = IdItem::IsRef(currItem->values_[0], resType, id);
512 if (!isRef) {
513 RESMGR_HILOGE(RESMGR_TAG,
514 "something wrong, pls check HaveParent(). idItem: %{public}s", idItem->ToString().c_str());
515 return ERROR;
516 }
517 currItem = hapManager_->FindResourceById(id, isOverrideResMgr_);
518 if (currItem == nullptr) {
519 RESMGR_HILOGE(RESMGR_TAG, "ref %s id not found", idItem->values_[0].c_str());
520 return ERROR;
521 }
522 }
523
524 if (++count > MAX_DEPTH_REF_SEARCH) {
525 RESMGR_HILOGE(RESMGR_TAG, " %u has too many parents", idItem->id_);
526 return ERROR;
527 }
528 } while (haveParent);
529
530 return SUCCESS;
531 }
532
GetBooleanById(uint32_t id,bool & outValue)533 RState ResourceManagerImpl::GetBooleanById(uint32_t id, bool &outValue)
534 {
535 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceById(id, isOverrideResMgr_);
536 if (idItem == nullptr) {
537 RESMGR_HILOGE(RESMGR_TAG, "GetBooleanById error id = %{public}d", id);
538 return ERROR_CODE_RES_ID_NOT_FOUND;
539 }
540 RState state = GetBoolean(idItem, outValue);
541 if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
542 return ERROR_CODE_RES_NOT_FOUND_BY_ID;
543 }
544 return state;
545 }
546
GetBooleanByName(const char * name,bool & outValue)547 RState ResourceManagerImpl::GetBooleanByName(const char *name, bool &outValue)
548 {
549 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceByName(name, ResType::BOOLEAN, isOverrideResMgr_);
550 if (idItem == nullptr) {
551 RESMGR_HILOGE(RESMGR_TAG, "GetBooleanByName error name = %{public}s", name);
552 return ERROR_CODE_RES_NAME_NOT_FOUND;
553 }
554 RState state = GetBoolean(idItem, outValue);
555 if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
556 return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
557 }
558 return state;
559 }
560
GetBoolean(const std::shared_ptr<IdItem> idItem,bool & outValue)561 RState ResourceManagerImpl::GetBoolean(const std::shared_ptr<IdItem> idItem, bool &outValue)
562 {
563 if (idItem == nullptr || idItem->resType_ != ResType::BOOLEAN) {
564 return NOT_FOUND;
565 }
566 std::string temp;
567 RState state = ResolveReference(idItem->value_, temp);
568 if (state == SUCCESS) {
569 if (strcmp(temp.c_str(), "true") == 0) {
570 outValue = true;
571 return SUCCESS;
572 }
573 if (strcmp(temp.c_str(), "false") == 0) {
574 outValue = false;
575 return SUCCESS;
576 }
577 return ERROR;
578 }
579 return state;
580 }
581
GetThemeFloat(const std::shared_ptr<IdItem> idItem,float & outValue)582 RState ResourceManagerImpl::GetThemeFloat(const std::shared_ptr<IdItem> idItem, float &outValue)
583 {
584 ResConfigImpl resConfig;
585 GetResConfig(resConfig);
586 std::vector<std::shared_ptr<IdItem>> idItems;
587 idItems.emplace_back(idItem);
588 ProcessReference(idItem->value_, idItems);
589 std::string result = ThemePackManager::GetThemePackManager()->FindThemeResource(bundleInfo, idItems, resConfig);
590 if (result.empty()) {
591 return NOT_FOUND;
592 }
593 std::string unit;
594 RState state = ParseFloat(result.c_str(), outValue, unit);
595 return state == SUCCESS ? RecalculateFloat(unit, outValue) : state;
596 }
597
GetThemeFloat(const std::shared_ptr<IdItem> idItem,float & outValue,std::string & unit)598 RState ResourceManagerImpl::GetThemeFloat(const std::shared_ptr<IdItem> idItem, float &outValue, std::string &unit)
599 {
600 ResConfigImpl resConfig;
601 GetResConfig(resConfig);
602 std::vector<std::shared_ptr<IdItem>> idItems;
603 idItems.emplace_back(idItem);
604 ProcessReference(idItem->value_, idItems);
605 std::string result = ThemePackManager::GetThemePackManager()->FindThemeResource(bundleInfo, idItems, resConfig);
606 if (result.empty()) {
607 return NOT_FOUND;
608 }
609 RState state = ParseFloat(result.c_str(), outValue, unit);
610 return state == SUCCESS ? RecalculateFloat(unit, outValue) : state;
611 }
612
GetFloatById(uint32_t id,float & outValue)613 RState ResourceManagerImpl::GetFloatById(uint32_t id, float &outValue)
614 {
615 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceById(id, isOverrideResMgr_);
616 if (idItem == nullptr) {
617 RESMGR_HILOGE(RESMGR_TAG, "GetFloatById error id = %{public}d", id);
618 return ERROR_CODE_RES_ID_NOT_FOUND;
619 }
620
621 // find in theme pack
622 if (GetThemeFloat(idItem, outValue) == SUCCESS) {
623 return SUCCESS;
624 }
625
626 std::string unit;
627 RState state = GetFloat(idItem, outValue, unit);
628 if (state == SUCCESS) {
629 return RecalculateFloat(unit, outValue);
630 }
631 if (state != ERROR_CODE_RES_REF_TOO_MUCH) {
632 return ERROR_CODE_RES_NOT_FOUND_BY_ID;
633 }
634 return state;
635 }
636
GetFloatById(uint32_t id,float & outValue,std::string & unit)637 RState ResourceManagerImpl::GetFloatById(uint32_t id, float &outValue, std::string &unit)
638 {
639 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceById(id, isOverrideResMgr_);
640 if (idItem == nullptr) {
641 RESMGR_HILOGE(RESMGR_TAG, "GetFloatById error with unit id = %{public}d", id);
642 return ERROR_CODE_RES_ID_NOT_FOUND;
643 }
644
645 // find in theme pack
646 if (GetThemeFloat(idItem, outValue, unit) == SUCCESS) {
647 return SUCCESS;
648 }
649
650 RState state = GetFloat(idItem, outValue, unit);
651 if (state == SUCCESS) {
652 return state;
653 }
654 if (state != ERROR_CODE_RES_REF_TOO_MUCH) {
655 return ERROR_CODE_RES_NOT_FOUND_BY_ID;
656 }
657 return state;
658 }
659
GetFloatByName(const char * name,float & outValue)660 RState ResourceManagerImpl::GetFloatByName(const char *name, float &outValue)
661 {
662 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceByName(name, ResType::FLOAT, isOverrideResMgr_);
663 if (idItem == nullptr) {
664 RESMGR_HILOGE(RESMGR_TAG, "GetFloatByName error name = %{public}s", name);
665 return ERROR_CODE_RES_NAME_NOT_FOUND;
666 }
667
668 // find in theme pack
669 if (GetThemeFloat(idItem, outValue) == SUCCESS) {
670 return SUCCESS;
671 }
672
673 std::string unit;
674 RState state = GetFloat(idItem, outValue, unit);
675 if (state == SUCCESS) {
676 return RecalculateFloat(unit, outValue);
677 }
678 if (state != ERROR_CODE_RES_REF_TOO_MUCH) {
679 return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
680 }
681 return state;
682 }
683
GetFloatByName(const char * name,float & outValue,std::string & unit)684 RState ResourceManagerImpl::GetFloatByName(const char *name, float &outValue, std::string &unit)
685 {
686 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceByName(name, ResType::FLOAT, isOverrideResMgr_);
687 return GetFloat(idItem, outValue, unit);
688 }
689
RecalculateFloat(const std::string & unit,float & result)690 RState ResourceManagerImpl::RecalculateFloat(const std::string &unit, float &result)
691 {
692 ResConfigImpl rc;
693 GetResConfig(rc);
694 float density = rc.GetScreenDensity();
695 if (density == SCREEN_DENSITY_NOT_SET) {
696 RESMGR_HILOGD(RESMGR_TAG, "RecalculateFloat srcDensity SCREEN_DENSITY_NOT_SET ");
697 return SUCCESS;
698 }
699 if (unit == VIRTUAL_PIXEL) {
700 result = result * density;
701 } else if (unit == FONT_SIZE_PIXEL) {
702 float fontSizeDensity = density * ((fabs(fontRatio_) <= 1E-6) ? 1.0f : fontRatio_);
703 result = result * fontSizeDensity;
704 } else {
705 // no unit
706 }
707 return SUCCESS;
708 }
709
ParseFloat(const std::string & strValue,float & result,std::string & unit)710 RState ResourceManagerImpl::ParseFloat(const std::string &strValue, float &result, std::string &unit)
711 {
712 std::smatch floatMatch;
713 if (!regex_search(strValue, floatMatch, FLOAT_REGEX)) {
714 RESMGR_HILOGD(RESMGR_TAG, "not valid float value %{public}s", strValue.c_str());
715 return ERROR;
716 }
717 std::string matchString(floatMatch.str());
718 if (floatMatch.size() < 1) {
719 return ERROR;
720 }
721 unit = floatMatch[floatMatch.size() - 1];
722 std::istringstream stream(matchString.substr(0, matchString.length() - unit.length()));
723 stream >> result;
724 return SUCCESS;
725 }
726
GetFloat(const std::shared_ptr<IdItem> idItem,float & outValue,std::string & unit)727 RState ResourceManagerImpl::GetFloat(const std::shared_ptr<IdItem> idItem, float &outValue, std::string &unit)
728 {
729 if (idItem == nullptr || idItem->resType_ != ResType::FLOAT) {
730 return NOT_FOUND;
731 }
732 std::string temp;
733 RState state = ResolveReference(idItem->value_, temp);
734 if (state == SUCCESS) {
735 return ParseFloat(temp.c_str(), outValue, unit);
736 }
737 return state;
738 }
739
GetIntegerById(uint32_t id,int & outValue)740 RState ResourceManagerImpl::GetIntegerById(uint32_t id, int &outValue)
741 {
742 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceById(id, isOverrideResMgr_);
743 if (idItem == nullptr) {
744 RESMGR_HILOGE(RESMGR_TAG, "GetIntegerById error id = %{public}d", id);
745 return ERROR_CODE_RES_ID_NOT_FOUND;
746 }
747 RState state = GetInteger(idItem, outValue);
748 if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
749 return ERROR_CODE_RES_NOT_FOUND_BY_ID;
750 }
751 return state;
752 }
753
GetIntegerByName(const char * name,int & outValue)754 RState ResourceManagerImpl::GetIntegerByName(const char *name, int &outValue)
755 {
756 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceByName(name, ResType::INTEGER, isOverrideResMgr_);
757 if (idItem == nullptr) {
758 RESMGR_HILOGE(RESMGR_TAG, "GetIntegerByName error name = %{public}s", name);
759 return ERROR_CODE_RES_NAME_NOT_FOUND;
760 }
761 RState state = GetInteger(idItem, outValue);
762 if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
763 return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
764 }
765 return state;
766 }
767
GetInteger(const std::shared_ptr<IdItem> idItem,int & outValue)768 RState ResourceManagerImpl::GetInteger(const std::shared_ptr<IdItem> idItem, int &outValue)
769 {
770 if (idItem == nullptr || idItem->resType_ != ResType::INTEGER) {
771 return NOT_FOUND;
772 }
773 std::string temp;
774 RState state = ResolveReference(idItem->value_, temp);
775 if (state == SUCCESS) {
776 outValue = stoi(temp);
777 return SUCCESS;
778 }
779 return state;
780 }
781
ProcessReference(const std::string value,std::vector<std::shared_ptr<IdItem>> & idItems)782 RState ResourceManagerImpl::ProcessReference(const std::string value,
783 std::vector<std::shared_ptr<IdItem>> &idItems)
784 {
785 int id;
786 ResType resType;
787 bool isRef = true;
788 int count = 0;
789 std::string refStr(value);
790 while (isRef) {
791 isRef = IdItem::IsRef(refStr, resType, id);
792 if (!isRef) {
793 return SUCCESS;
794 }
795
796 if (IdItem::IsArrayOfType(resType)) {
797 // can't be array
798 RESMGR_HILOGD(RESMGR_TAG, "ref %{public}s can't be array", refStr.c_str());
799 return ERROR;
800 }
801 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceById(id, isOverrideResMgr_);
802 idItems.emplace_back(idItem);
803 if (idItem == nullptr) {
804 RESMGR_HILOGE(RESMGR_TAG, "ref %s id not found", refStr.c_str());
805 return ERROR;
806 }
807 // unless compile bug
808 if (resType != idItem->resType_) {
809 RESMGR_HILOGE(RESMGR_TAG,
810 "impossible. ref %s type mismatch, found type: %d", refStr.c_str(), idItem->resType_);
811 return ERROR;
812 }
813
814 refStr = idItem->value_;
815
816 if (++count > MAX_DEPTH_REF_SEARCH) {
817 RESMGR_HILOGE(RESMGR_TAG, "ref %s has re-ref too much", value.c_str());
818 return ERROR_CODE_RES_REF_TOO_MUCH;
819 }
820 }
821 return SUCCESS;
822 }
823
GetThemeColor(const std::shared_ptr<IdItem> idItem,uint32_t & outValue)824 RState ResourceManagerImpl::GetThemeColor(const std::shared_ptr<IdItem> idItem, uint32_t &outValue)
825 {
826 ResConfigImpl resConfig;
827 GetResConfig(resConfig);
828 std::vector<std::shared_ptr<IdItem> > idItems;
829 idItems.emplace_back(idItem);
830 RState state = ProcessReference(idItem->value_, idItems);
831 std::string result = ThemePackManager::GetThemePackManager()->FindThemeResource(bundleInfo, idItems, resConfig,
832 hapManager_->IsThemeSystemResEnableHap());
833 if (result.empty()) {
834 return ERROR_CODE_RES_ID_NOT_FOUND;
835 }
836 return state == SUCCESS ? Utils::ConvertColorToUInt32(result.c_str(), outValue) : state;
837 }
838
GetColorById(uint32_t id,uint32_t & outValue)839 RState ResourceManagerImpl::GetColorById(uint32_t id, uint32_t &outValue)
840 {
841 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceById(id, isOverrideResMgr_);
842 if (idItem == nullptr) {
843 RESMGR_HILOGE(RESMGR_TAG, "GetColorById error id = %{public}d", id);
844 return ERROR_CODE_RES_ID_NOT_FOUND;
845 }
846
847 // find in theme pack
848 if (GetThemeColor(idItem, outValue) == SUCCESS) {
849 return SUCCESS;
850 }
851
852 RState state = GetColor(idItem, outValue);
853 if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
854 return ERROR_CODE_RES_NOT_FOUND_BY_ID;
855 }
856 return state;
857 }
858
GetColorByName(const char * name,uint32_t & outValue)859 RState ResourceManagerImpl::GetColorByName(const char *name, uint32_t &outValue)
860 {
861 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceByName(name, ResType::COLOR, isOverrideResMgr_);
862 if (idItem == nullptr) {
863 RESMGR_HILOGE(RESMGR_TAG, "GetColorByName error name = %{public}s", name);
864 return ERROR_CODE_RES_NAME_NOT_FOUND;
865 }
866
867 // find in theme pack
868 if (GetThemeColor(idItem, outValue) == SUCCESS) {
869 return SUCCESS;
870 }
871
872 RState state = GetColor(idItem, outValue);
873 if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
874 return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
875 }
876 return state;
877 }
878
GetColor(const std::shared_ptr<IdItem> idItem,uint32_t & outValue)879 RState ResourceManagerImpl::GetColor(const std::shared_ptr<IdItem> idItem, uint32_t &outValue)
880 {
881 if (idItem == nullptr || idItem->resType_ != ResType::COLOR) {
882 return NOT_FOUND;
883 }
884 std::string temp;
885 RState state = ResolveReference(idItem->value_, temp);
886 if (state == SUCCESS) {
887 return Utils::ConvertColorToUInt32(temp.c_str(), outValue);
888 }
889 return state;
890 }
891
GetSymbolById(uint32_t id,uint32_t & outValue)892 RState ResourceManagerImpl::GetSymbolById(uint32_t id, uint32_t &outValue)
893 {
894 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceById(id, isOverrideResMgr_);
895 if (idItem == nullptr) {
896 RESMGR_HILOGE(RESMGR_TAG, "GetSymbolById error id = %{public}d", id);
897 return ERROR_CODE_RES_ID_NOT_FOUND;
898 }
899 RState state = GetSymbol(idItem, outValue);
900 if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
901 return ERROR_CODE_RES_NOT_FOUND_BY_ID;
902 }
903 return state;
904 }
905
GetSymbolByName(const char * name,uint32_t & outValue)906 RState ResourceManagerImpl::GetSymbolByName(const char *name, uint32_t &outValue)
907 {
908 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceByName(name, ResType::SYMBOL, isOverrideResMgr_);
909 if (idItem == nullptr) {
910 RESMGR_HILOGE(RESMGR_TAG, "GetSymbolByName error name = %{public}s", name);
911 return ERROR_CODE_RES_NAME_NOT_FOUND;
912 }
913 RState state = GetSymbol(idItem, outValue);
914 if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
915 return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
916 }
917 return state;
918 }
919
GetSymbol(const std::shared_ptr<IdItem> idItem,uint32_t & outValue)920 RState ResourceManagerImpl::GetSymbol(const std::shared_ptr<IdItem> idItem, uint32_t &outValue)
921 {
922 if (idItem == nullptr || idItem->resType_ != ResType::SYMBOL) {
923 return NOT_FOUND;
924 }
925 std::string temp;
926 RState state = ResolveReference(idItem->value_, temp);
927 if (state == SUCCESS) {
928 outValue = static_cast<uint32_t>(strtol(temp.c_str(), nullptr, HEX_ADECIMAL));
929 }
930 return state;
931 }
932
GetIntArrayById(uint32_t id,std::vector<int> & outValue)933 RState ResourceManagerImpl::GetIntArrayById(uint32_t id, std::vector<int> &outValue)
934 {
935 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceById(id, isOverrideResMgr_);
936 return GetIntArray(idItem, outValue);
937 }
938
GetIntArrayByName(const char * name,std::vector<int> & outValue)939 RState ResourceManagerImpl::GetIntArrayByName(const char *name, std::vector<int> &outValue)
940 {
941 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceByName(name, ResType::INTARRAY, isOverrideResMgr_);
942 return GetIntArray(idItem, outValue);
943 }
944
GetIntArray(const std::shared_ptr<IdItem> idItem,std::vector<int> & outValue)945 RState ResourceManagerImpl::GetIntArray(const std::shared_ptr<IdItem> idItem, std::vector<int> &outValue)
946 {
947 // not found or type invalid
948 if (idItem == nullptr || idItem->resType_ != ResType::INTARRAY) {
949 return NOT_FOUND;
950 }
951 outValue.clear();
952
953 for (size_t i = 0; i < idItem->values_.size(); ++i) {
954 std::string resolvedValue;
955 RState rrRet = ResolveReference(idItem->values_[i], resolvedValue);
956 if (rrRet != SUCCESS) {
957 RESMGR_HILOGD(RESMGR_TAG, "ResolveReference failed, value:%{public}s", idItem->values_[i].c_str());
958 return ERROR;
959 }
960 outValue.push_back(stoi(resolvedValue));
961 }
962 return SUCCESS;
963 }
964
GetThemeById(uint32_t id,std::map<std::string,std::string> & outValue)965 RState ResourceManagerImpl::GetThemeById(uint32_t id, std::map<std::string, std::string> &outValue)
966 {
967 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceById(id, isOverrideResMgr_);
968 if (idItem == nullptr) {
969 RESMGR_HILOGE(RESMGR_TAG, "GetThemeById error id = %{public}d", id);
970 return ERROR_CODE_RES_ID_NOT_FOUND;
971 }
972 RState state = GetTheme(idItem, outValue);
973 if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
974 return ERROR_CODE_RES_NOT_FOUND_BY_ID;
975 }
976 return state;
977 }
978
GetThemeByName(const char * name,std::map<std::string,std::string> & outValue)979 RState ResourceManagerImpl::GetThemeByName(const char *name, std::map<std::string, std::string> &outValue)
980 {
981 const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceByName(name, ResType::THEME, isOverrideResMgr_);
982 if (idItem == nullptr) {
983 RESMGR_HILOGE(RESMGR_TAG, "GetThemeByName error name = %{public}s", name);
984 return ERROR_CODE_RES_NAME_NOT_FOUND;
985 }
986 RState state = GetTheme(idItem, outValue);
987 if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
988 return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
989 }
990 return state;
991 }
992
GetTheme(const std::shared_ptr<IdItem> idItem,std::map<std::string,std::string> & outValue)993 RState ResourceManagerImpl::GetTheme(const std::shared_ptr<IdItem> idItem, std::map<std::string, std::string> &outValue)
994 {
995 //type invalid
996 if (idItem->resType_ != ResType::THEME) {
997 RESMGR_HILOGE(RESMGR_TAG,
998 "actual resType = %{public}d, expect resType = %{public}d", idItem->resType_, ResType::THEME);
999 return NOT_FOUND;
1000 }
1001 return ResolveParentReference(idItem, outValue);
1002 }
1003
GetProfileById(uint32_t id,std::string & outValue)1004 RState ResourceManagerImpl::GetProfileById(uint32_t id, std::string &outValue)
1005 {
1006 auto qd = hapManager_->FindQualifierValueById(id, isOverrideResMgr_);
1007 if (qd == nullptr) {
1008 RESMGR_HILOGE(RESMGR_TAG, "GetProfileById error id = %{public}d", id);
1009 return ERROR_CODE_RES_ID_NOT_FOUND;
1010 }
1011 RState state = hapManager_->GetFilePath(qd, ResType::PROF, outValue);
1012 return state == SUCCESS ? state : ERROR_CODE_RES_NOT_FOUND_BY_ID;
1013 }
1014
GetProfileByName(const char * name,std::string & outValue)1015 RState ResourceManagerImpl::GetProfileByName(const char *name, std::string &outValue)
1016 {
1017 auto qd = hapManager_->FindQualifierValueByName(name, ResType::PROF, isOverrideResMgr_);
1018 if (qd == nullptr) {
1019 RESMGR_HILOGE(RESMGR_TAG,
1020 "GetProfileByName error name = %{public}s", name);
1021 return ERROR_CODE_RES_NAME_NOT_FOUND;
1022 }
1023 RState state = hapManager_->GetFilePath(qd, ResType::PROF, outValue);
1024 return state == SUCCESS ? state : ERROR_CODE_RES_NOT_FOUND_BY_NAME;
1025 }
1026
GetMediaById(uint32_t id,std::string & outValue,uint32_t density)1027 RState ResourceManagerImpl::GetMediaById(uint32_t id, std::string &outValue, uint32_t density)
1028 {
1029 if (!IsDensityValid(density)) {
1030 RESMGR_HILOGE(RESMGR_TAG, "density invalid");
1031 return ERROR_CODE_INVALID_INPUT_PARAMETER;
1032 }
1033 auto qd = hapManager_->FindQualifierValueById(id, isOverrideResMgr_, density);
1034 if (qd == nullptr) {
1035 RESMGR_HILOGE(RESMGR_TAG, "GetMediaById error id = %{public}d", id);
1036 return ERROR_CODE_RES_ID_NOT_FOUND;
1037 }
1038 RState state = hapManager_->GetFilePath(qd, ResType::MEDIA, outValue);
1039 return state == SUCCESS ? state : ERROR_CODE_RES_NOT_FOUND_BY_ID;
1040 }
1041
GetMediaByName(const char * name,std::string & outValue,uint32_t density)1042 RState ResourceManagerImpl::GetMediaByName(const char *name, std::string &outValue, uint32_t density)
1043 {
1044 if (!IsDensityValid(density)) {
1045 RESMGR_HILOGE(RESMGR_TAG, "density invalid");
1046 return ERROR_CODE_INVALID_INPUT_PARAMETER;
1047 }
1048 auto qd = hapManager_->FindQualifierValueByName(name, ResType::MEDIA, isOverrideResMgr_, density);
1049 if (qd == nullptr) {
1050 RESMGR_HILOGE(RESMGR_TAG, "GetMediaByName error name = %{public}s", name);
1051 return ERROR_CODE_RES_NAME_NOT_FOUND;
1052 }
1053 RState state = hapManager_->GetFilePath(qd, ResType::MEDIA, outValue);
1054 return state == SUCCESS ? state : ERROR_CODE_RES_NOT_FOUND_BY_NAME;
1055 }
1056
GetRawFilePathByName(const std::string & name,std::string & outValue)1057 RState ResourceManagerImpl::GetRawFilePathByName(const std::string &name, std::string &outValue)
1058 {
1059 return hapManager_->FindRawFile(name, outValue);
1060 }
1061
GetRawFileDescriptor(const std::string & name,RawFileDescriptor & descriptor)1062 RState ResourceManagerImpl::GetRawFileDescriptor(const std::string &name, RawFileDescriptor &descriptor)
1063 {
1064 return hapManager_->FindRawFileDescriptorFromHap(name, descriptor);
1065 }
1066
CloseRawFileDescriptor(const std::string & name)1067 RState ResourceManagerImpl::CloseRawFileDescriptor(const std::string &name)
1068 {
1069 return hapManager_->CloseRawFileDescriptor(name);
1070 }
1071
ProcessPsuedoTranslate(std::string & outValue)1072 void ResourceManagerImpl::ProcessPsuedoTranslate(std::string &outValue)
1073 {
1074 auto len = outValue.length() + 1;
1075 char src[len];
1076 if (strcpy_s(src, len, outValue.c_str()) == EOK) {
1077 std::string resultMsg = psueManager_->Convert(src, outValue);
1078 if (resultMsg != "") {
1079 RESMGR_HILOGE(RESMGR_TAG, "Psuedo translate failed, value:%s", src);
1080 }
1081 }
1082 }
1083
~ResourceManagerImpl()1084 ResourceManagerImpl::~ResourceManagerImpl()
1085 {}
1086
AddResource(const char * path,const uint32_t & selectedTypes)1087 bool ResourceManagerImpl::AddResource(const char *path, const uint32_t &selectedTypes)
1088 {
1089 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
1090 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1091 #endif
1092 return this->hapManager_->AddResource(path, selectedTypes);
1093 }
1094
AddResource(const std::string & path,const std::vector<std::string> & overlayPaths)1095 bool ResourceManagerImpl::AddResource(const std::string &path, const std::vector<std::string> &overlayPaths)
1096 {
1097 return this->hapManager_->AddResource(path, overlayPaths);
1098 }
1099
RemoveResource(const std::string & path,const std::vector<std::string> & overlayPaths)1100 bool ResourceManagerImpl::RemoveResource(const std::string &path, const std::vector<std::string> &overlayPaths)
1101 {
1102 return this->hapManager_->RemoveResource(path, overlayPaths);
1103 }
1104
AddAppOverlay(const std::string & path)1105 bool ResourceManagerImpl::AddAppOverlay(const std::string &path)
1106 {
1107 return this->hapManager_->AddAppOverlay(path);
1108 }
1109
RemoveAppOverlay(const std::string & path)1110 bool ResourceManagerImpl::RemoveAppOverlay(const std::string &path)
1111 {
1112 return this->hapManager_->RemoveAppOverlay(path);
1113 }
1114
UpdateFakeLocaleFlag(ResConfig & resConfig)1115 RState ResourceManagerImpl::UpdateFakeLocaleFlag(ResConfig &resConfig)
1116 {
1117 #ifdef SUPPORT_GRAPHICS
1118 if (resConfig.GetLocaleInfo() == nullptr) {
1119 return LOCALEINFO_IS_NULL;
1120 }
1121 if (resConfig.GetLocaleInfo()->getLanguage() == nullptr) {
1122 return LOCALEINFO_IS_NULL;
1123 }
1124 const char* language = resConfig.GetLocaleInfo()->getLanguage();
1125 const char* region = resConfig.GetLocaleInfo()->getCountry();
1126 if (language != nullptr && region != nullptr) {
1127 std::string languageStr = language;
1128 std::string regionStr = region;
1129 if (languageStr == "en" && regionStr == "XA") {
1130 isFakeLocale = true;
1131 } else {
1132 isFakeLocale = false;
1133 }
1134 if (languageStr == "ar" && regionStr == "XB") {
1135 isBidirectionFakeLocale = true;
1136 } else {
1137 isBidirectionFakeLocale = false;
1138 }
1139 }
1140 #endif
1141 return SUCCESS;
1142 }
1143
UpdateResConfig(ResConfig & resConfig,bool isUpdateTheme)1144 RState ResourceManagerImpl::UpdateResConfig(ResConfig &resConfig, bool isUpdateTheme)
1145 {
1146 auto themePackManager = ThemePackManager::GetThemePackManager();
1147 if (themePackManager->UpdateThemeId(resConfig.GetThemeId())) {
1148 RESMGR_HILOGD(RESMGR_TAG, "The theme enabled");
1149 themePackManager->LoadThemeRes(bundleInfo.first, bundleInfo.second, userId);
1150 }
1151 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
1152 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1153 #endif
1154 RState state = UpdateFakeLocaleFlag(resConfig);
1155 if (state != SUCCESS) {
1156 return state;
1157 }
1158 return this->hapManager_->UpdateResConfig(resConfig);
1159 }
1160
UpdateOverrideResConfig(ResConfig & resConfig)1161 RState ResourceManagerImpl::UpdateOverrideResConfig(ResConfig &resConfig)
1162 {
1163 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
1164 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1165 #endif
1166 UpdateFakeLocaleFlag(resConfig);
1167 return this->hapManager_->UpdateOverrideResConfig(resConfig);
1168 }
1169
GetResConfig(ResConfig & resConfig)1170 void ResourceManagerImpl::GetResConfig(ResConfig &resConfig)
1171 {
1172 this->hapManager_->GetResConfig(resConfig);
1173 }
1174
GetOverrideResConfig(ResConfig & resConfig)1175 void ResourceManagerImpl::GetOverrideResConfig(ResConfig &resConfig)
1176 {
1177 this->hapManager_->GetOverrideResConfig(resConfig);
1178 }
1179
GetResourcePaths()1180 std::vector<std::string> ResourceManagerImpl::GetResourcePaths()
1181 {
1182 return this->hapManager_->GetResourcePaths();
1183 }
1184
IsDensityValid(uint32_t density)1185 bool ResourceManagerImpl::IsDensityValid(uint32_t density)
1186 {
1187 switch (density) {
1188 case SCREEN_DENSITY_NOT_SET:
1189 case SCREEN_DENSITY_SDPI:
1190 case SCREEN_DENSITY_MDPI:
1191 case SCREEN_DENSITY_LDPI:
1192 case SCREEN_DENSITY_XLDPI:
1193 case SCREEN_DENSITY_XXLDPI:
1194 case SCREEN_DENSITY_XXXLDPI:
1195 return true;
1196 default:
1197 return false;
1198 }
1199 }
1200
GetThemeMedia(const std::shared_ptr<IdItem> idItem,size_t & len,std::unique_ptr<uint8_t[]> & outValue,uint32_t density)1201 RState ResourceManagerImpl::GetThemeMedia(const std::shared_ptr<IdItem> idItem, size_t &len,
1202 std::unique_ptr<uint8_t[]> &outValue, uint32_t density)
1203 {
1204 ResConfigImpl resConfig;
1205 GetResConfig(resConfig);
1206 std::vector<std::shared_ptr<IdItem>> idItems;
1207 idItems.emplace_back(idItem);
1208 std::string result = ThemePackManager::GetThemePackManager()->FindThemeResource(bundleInfo, idItems, resConfig);
1209 outValue = Utils::LoadResourceFile(result, len);
1210 return result.empty() ? ERROR_CODE_RES_ID_NOT_FOUND : SUCCESS;
1211 }
1212
GetMediaDataById(uint32_t id,size_t & len,std::unique_ptr<uint8_t[]> & outValue,uint32_t density)1213 RState ResourceManagerImpl::GetMediaDataById(uint32_t id, size_t &len, std::unique_ptr<uint8_t[]> &outValue,
1214 uint32_t density)
1215 {
1216 if (!IsDensityValid(density)) {
1217 RESMGR_HILOGE(RESMGR_TAG, "density invalid");
1218 return ERROR_CODE_INVALID_INPUT_PARAMETER;
1219 }
1220 auto qd = hapManager_->FindQualifierValueById(id, isOverrideResMgr_, density);
1221 if (qd == nullptr) {
1222 RESMGR_HILOGE(RESMGR_TAG, "GetMediaDataById error id = %{public}d", id);
1223 return ERROR_CODE_RES_ID_NOT_FOUND;
1224 }
1225
1226 // find in theme
1227 const std::shared_ptr<IdItem> idItem = qd->GetIdItem();
1228 if (GetThemeMedia(idItem, len, outValue, density) == SUCCESS) {
1229 return SUCCESS;
1230 }
1231
1232 RState state = hapManager_->GetMediaData(qd, len, outValue);
1233 return state == SUCCESS ? state : ERROR_CODE_RES_NOT_FOUND_BY_ID;
1234 }
1235
GetMediaDataByName(const char * name,size_t & len,std::unique_ptr<uint8_t[]> & outValue,uint32_t density)1236 RState ResourceManagerImpl::GetMediaDataByName(const char *name, size_t &len, std::unique_ptr<uint8_t[]> &outValue,
1237 uint32_t density)
1238 {
1239 if (!IsDensityValid(density)) {
1240 RESMGR_HILOGE(RESMGR_TAG, "density invalid");
1241 return ERROR_CODE_INVALID_INPUT_PARAMETER;
1242 }
1243
1244 auto qd = hapManager_->FindQualifierValueByName(name, ResType::MEDIA, isOverrideResMgr_, density);
1245 if (qd == nullptr) {
1246 RESMGR_HILOGE(RESMGR_TAG,
1247 "GetMediaDataByName error name = %{public}s", name);
1248 return ERROR_CODE_RES_NAME_NOT_FOUND;
1249 }
1250
1251 const std::shared_ptr<IdItem> idItem = qd->GetIdItem();
1252 if (GetThemeMedia(idItem, len, outValue, density) == SUCCESS) {
1253 return SUCCESS;
1254 }
1255
1256 RState state = hapManager_->GetMediaData(qd, len, outValue);
1257 return state == SUCCESS ? state : ERROR_CODE_RES_NOT_FOUND_BY_NAME;
1258 }
1259
GetThemeMediaBase64(const std::shared_ptr<IdItem> idItem,std::string & outValue)1260 RState ResourceManagerImpl::GetThemeMediaBase64(const std::shared_ptr<IdItem> idItem, std::string &outValue)
1261 {
1262 ResConfigImpl resConfig;
1263 GetResConfig(resConfig);
1264 std::vector<std::shared_ptr<IdItem>> idItems;
1265 idItems.emplace_back(idItem);
1266 std::string result = ThemePackManager::GetThemePackManager()->FindThemeResource(bundleInfo, idItems, resConfig);
1267 if (result.empty()) {
1268 return NOT_FOUND;
1269 }
1270 return Utils::GetMediaBase64Data(result, outValue);
1271 }
1272
GetMediaBase64DataById(uint32_t id,std::string & outValue,uint32_t density)1273 RState ResourceManagerImpl::GetMediaBase64DataById(uint32_t id, std::string &outValue, uint32_t density)
1274 {
1275 if (!IsDensityValid(density)) {
1276 RESMGR_HILOGE(RESMGR_TAG, "density invalid");
1277 return ERROR_CODE_INVALID_INPUT_PARAMETER;
1278 }
1279
1280 auto qd = hapManager_->FindQualifierValueById(id, isOverrideResMgr_, density);
1281 if (qd == nullptr) {
1282 RESMGR_HILOGE(RESMGR_TAG, "GetMediaBase64DataById error id = %{public}d", id);
1283 return ERROR_CODE_RES_ID_NOT_FOUND;
1284 }
1285
1286 const std::shared_ptr<IdItem> idItem = qd->GetIdItem();
1287 if (GetThemeMediaBase64(idItem, outValue) == SUCCESS) {
1288 return SUCCESS;
1289 }
1290
1291 RState state = hapManager_->GetMediaBase64Data(qd, outValue);
1292 return state == SUCCESS ? state : ERROR_CODE_RES_NOT_FOUND_BY_ID;
1293 }
1294
GetMediaBase64DataByName(const char * name,std::string & outValue,uint32_t density)1295 RState ResourceManagerImpl::GetMediaBase64DataByName(const char *name, std::string &outValue, uint32_t density)
1296 {
1297 if (!IsDensityValid(density)) {
1298 RESMGR_HILOGE(RESMGR_TAG, "density invalid");
1299 return ERROR_CODE_INVALID_INPUT_PARAMETER;
1300 }
1301 auto qd = hapManager_->FindQualifierValueByName(name, ResType::MEDIA, isOverrideResMgr_, density);
1302 if (qd == nullptr) {
1303 RESMGR_HILOGE(RESMGR_TAG,
1304 "GetMediaBase64DataByName error name = %{public}s", name);
1305 return ERROR_CODE_RES_NAME_NOT_FOUND;
1306 }
1307
1308 const std::shared_ptr<IdItem> idItem = qd->GetIdItem();
1309 if (GetThemeMediaBase64(idItem, outValue) == SUCCESS) {
1310 return SUCCESS;
1311 }
1312
1313 RState state = hapManager_->GetMediaBase64Data(qd, outValue);
1314 return state == SUCCESS ? state : ERROR_CODE_RES_NOT_FOUND_BY_NAME;
1315 }
1316
GetProfileDataById(uint32_t id,size_t & len,std::unique_ptr<uint8_t[]> & outValue)1317 RState ResourceManagerImpl::GetProfileDataById(uint32_t id, size_t &len, std::unique_ptr<uint8_t[]> &outValue)
1318 {
1319 auto qd = hapManager_->FindQualifierValueById(id, isOverrideResMgr_);
1320 if (qd == nullptr) {
1321 RESMGR_HILOGE(RESMGR_TAG, "GetProfileDataById error id = %{public}d", id);
1322 return ERROR_CODE_RES_NOT_FOUND_BY_ID;
1323 }
1324 return hapManager_->GetProfileData(qd, len, outValue);
1325 }
1326
GetProfileDataByName(const char * name,size_t & len,std::unique_ptr<uint8_t[]> & outValue)1327 RState ResourceManagerImpl::GetProfileDataByName(const char *name, size_t &len, std::unique_ptr<uint8_t[]> &outValue)
1328 {
1329 auto qd = hapManager_->FindQualifierValueByName(name, ResType::PROF, isOverrideResMgr_);
1330 if (qd == nullptr) {
1331 RESMGR_HILOGE(RESMGR_TAG,
1332 "GetProfileDataByName error name = %{public}s", name);
1333 return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
1334 }
1335 return hapManager_->GetProfileData(qd, len, outValue);
1336 }
1337
GetRawFileFromHap(const std::string & rawFileName,size_t & len,std::unique_ptr<uint8_t[]> & outValue)1338 RState ResourceManagerImpl::GetRawFileFromHap(const std::string &rawFileName, size_t &len,
1339 std::unique_ptr<uint8_t[]> &outValue)
1340 {
1341 return hapManager_->FindRawFileFromHap(rawFileName, len, outValue);
1342 }
1343
GetRawFileDescriptorFromHap(const std::string & rawFileName,RawFileDescriptor & descriptor)1344 RState ResourceManagerImpl::GetRawFileDescriptorFromHap(const std::string &rawFileName, RawFileDescriptor &descriptor)
1345 {
1346 return hapManager_->FindRawFileDescriptorFromHap(rawFileName, descriptor);
1347 }
1348
IsLoadHap(std::string & hapPath)1349 RState ResourceManagerImpl::IsLoadHap(std::string &hapPath)
1350 {
1351 if (hapManager_->IsLoadHap(hapPath)) {
1352 return SUCCESS;
1353 }
1354 return NOT_FOUND;
1355 }
1356
IsFileExist(const std::string & path)1357 bool ResourceManagerImpl::IsFileExist(const std::string& path)
1358 {
1359 std::fstream inputFile;
1360 inputFile.open(path, std::ios::in);
1361 if (inputFile) {
1362 return true;
1363 }
1364 return false;
1365 }
1366
GetRawFileList(const std::string & rawDirPath,std::vector<std::string> & rawfileList)1367 RState ResourceManagerImpl::GetRawFileList(const std::string &rawDirPath, std::vector<std::string>& rawfileList)
1368 {
1369 return hapManager_->GetRawFileList(rawDirPath, rawfileList);
1370 }
1371
GetSuffix(const std::shared_ptr<HapResource::ValueUnderQualifierDir> qd)1372 std::string GetSuffix(const std::shared_ptr<HapResource::ValueUnderQualifierDir> qd)
1373 {
1374 const std::shared_ptr<IdItem> idItem = qd->GetIdItem();
1375 if (idItem == nullptr || idItem->resType_ != ResType::MEDIA) {
1376 return std::string();
1377 }
1378 std::string mediaPath = idItem->value_;
1379 auto pos = mediaPath.find_last_of('.');
1380 if (pos == std::string::npos) {
1381 return std::string();
1382 }
1383 return mediaPath.substr(pos + 1);
1384 }
1385
GetThemeIcon(const std::shared_ptr<IdItem> idItem,size_t & len,std::unique_ptr<uint8_t[]> & outValue,uint32_t density)1386 RState ResourceManagerImpl::GetThemeIcon(const std::shared_ptr<IdItem> idItem, size_t &len,
1387 std::unique_ptr<uint8_t[]> &outValue, uint32_t density)
1388 {
1389 std::string iconName = idItem->GetItemResName();
1390 std::string result = ThemePackManager::GetThemePackManager()->FindThemeIconResource(bundleInfo, iconName);
1391 if (result.empty()) {
1392 RESMGR_HILOGD(RESMGR_TAG,
1393 "GetThemeIcon FAILED bundlename = %{public}s, modulename = %{public}s, iconName = %{public}s",
1394 bundleInfo.first.c_str(), bundleInfo.second.c_str(), iconName.c_str());
1395 return ERROR_CODE_RES_ID_NOT_FOUND;
1396 }
1397 outValue = Utils::LoadResourceFile(result, len);
1398 return SUCCESS;
1399 }
1400
GetThemeDrawable(const std::shared_ptr<IdItem> idItem,size_t & len,std::unique_ptr<uint8_t[]> & outValue,uint32_t iconType,uint32_t density)1401 RState ResourceManagerImpl::GetThemeDrawable(const std::shared_ptr<IdItem> idItem, size_t &len,
1402 std::unique_ptr<uint8_t[]> &outValue, uint32_t iconType, uint32_t density)
1403 {
1404 if (iconType == 0 && GetThemeMedia(idItem, len, outValue, density) == SUCCESS) {
1405 return SUCCESS;
1406 } else if (iconType == 1 && GetThemeIcon(idItem, len, outValue, density) == SUCCESS) {
1407 return SUCCESS;
1408 } else {
1409 // other type
1410 }
1411 return ERROR_CODE_RES_NOT_FOUND_BY_ID;
1412 }
1413
GetDrawableInfoById(uint32_t id,std::string & type,size_t & len,std::unique_ptr<uint8_t[]> & outValue,uint32_t density)1414 RState ResourceManagerImpl::GetDrawableInfoById(uint32_t id, std::string &type, size_t &len,
1415 std::unique_ptr<uint8_t[]> &outValue, uint32_t density)
1416 {
1417 if (!IsDensityValid(density)) {
1418 RESMGR_HILOGE(RESMGR_TAG, "density invalid");
1419 return ERROR_CODE_INVALID_INPUT_PARAMETER;
1420 }
1421 auto qd = hapManager_->FindQualifierValueById(id, isOverrideResMgr_, density);
1422 if (qd == nullptr) {
1423 RESMGR_HILOGE(RESMGR_TAG, "GetDrawableInfoById id = %{public}d", id);
1424 return ERROR_CODE_RES_ID_NOT_FOUND;
1425 }
1426 type = GetSuffix(qd);
1427 if (type.empty()) {
1428 RESMGR_HILOGE(RESMGR_TAG, "failed to get resourceType");
1429 return ERROR_CODE_RES_NOT_FOUND_BY_ID;
1430 }
1431 return hapManager_->GetMediaData(qd, len, outValue);
1432 }
1433
GetDrawableInfoByName(const char * name,std::string & type,size_t & len,std::unique_ptr<uint8_t[]> & outValue,uint32_t density)1434 RState ResourceManagerImpl::GetDrawableInfoByName(const char *name, std::string &type, size_t &len,
1435 std::unique_ptr<uint8_t[]> &outValue, uint32_t density)
1436 {
1437 if (!IsDensityValid(density)) {
1438 RESMGR_HILOGE(RESMGR_TAG, "density invalid");
1439 return ERROR_CODE_INVALID_INPUT_PARAMETER;
1440 }
1441 auto qd = hapManager_->FindQualifierValueByName(name, ResType::MEDIA, isOverrideResMgr_, density);
1442 if (qd == nullptr) {
1443 RESMGR_HILOGE(RESMGR_TAG, "GetDrawableInfoByName error name = %{public}s", name);
1444 return ERROR_CODE_RES_NAME_NOT_FOUND;
1445 }
1446 type = GetSuffix(qd);
1447 if (type.empty()) {
1448 RESMGR_HILOGE(RESMGR_TAG, "failed to get resourceType");
1449 return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
1450 }
1451 return hapManager_->GetMediaData(qd, len, outValue);
1452 }
1453
GetDrawableInfoById(uint32_t id,std::tuple<std::string,size_t,std::string> & drawableInfo,std::unique_ptr<uint8_t[]> & outValue,uint32_t iconType,uint32_t density)1454 RState ResourceManagerImpl::GetDrawableInfoById(uint32_t id,
1455 std::tuple<std::string, size_t, std::string> &drawableInfo,
1456 std::unique_ptr<uint8_t[]> &outValue, uint32_t iconType, uint32_t density)
1457 {
1458 if (!IsDensityValid(density)) {
1459 RESMGR_HILOGE(RESMGR_TAG, "density invalid");
1460 return ERROR_CODE_INVALID_INPUT_PARAMETER;
1461 }
1462 auto qd = hapManager_->FindQualifierValueById(id, isOverrideResMgr_, density);
1463 if (qd == nullptr) {
1464 RESMGR_HILOGE(RESMGR_TAG, "GetDrawableInfoById error id = %{public}d", id);
1465 return ERROR_CODE_RES_ID_NOT_FOUND;
1466 }
1467 std::string type = GetSuffix(qd);
1468 if (type.empty()) {
1469 RESMGR_HILOGE(RESMGR_TAG, "failed to get resourceType");
1470 return ERROR_CODE_RES_NOT_FOUND_BY_ID;
1471 }
1472 size_t len = 0;
1473 // find in theme
1474 const std::shared_ptr<IdItem> idItem = qd->GetIdItem();
1475 std::string themeMask = ThemePackManager::GetThemePackManager()->GetMask();
1476 if (GetThemeDrawable(idItem, len, outValue, iconType, density) == SUCCESS) {
1477 drawableInfo = std::make_tuple(type, len, themeMask);
1478 return SUCCESS;
1479 }
1480
1481 RState state = hapManager_->GetMediaData(qd, len, outValue);
1482 drawableInfo = std::make_tuple(type, len, themeMask);
1483 return state;
1484 }
1485
GetDrawableInfoByName(const char * name,std::tuple<std::string,size_t,std::string> & drawableInfo,std::unique_ptr<uint8_t[]> & outValue,uint32_t iconType,uint32_t density)1486 RState ResourceManagerImpl::GetDrawableInfoByName(const char *name,
1487 std::tuple<std::string, size_t, std::string> &drawableInfo,
1488 std::unique_ptr<uint8_t[]> &outValue, uint32_t iconType, uint32_t density)
1489 {
1490 if (!IsDensityValid(density)) {
1491 RESMGR_HILOGE(RESMGR_TAG, "density invalid");
1492 return ERROR_CODE_INVALID_INPUT_PARAMETER;
1493 }
1494 auto qd = hapManager_->FindQualifierValueByName(name, ResType::MEDIA, isOverrideResMgr_, density);
1495 if (qd == nullptr) {
1496 RESMGR_HILOGE(RESMGR_TAG, "GetDrawableInfoByName error name = %{public}s", name);
1497 return ERROR_CODE_RES_NAME_NOT_FOUND;
1498 }
1499 std::string type = GetSuffix(qd);
1500 if (type.empty()) {
1501 RESMGR_HILOGE(RESMGR_TAG, "failed to get resourceType");
1502 return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
1503 }
1504 size_t len = 0;
1505 // find in theme
1506 std::string themeMask = ThemePackManager::GetThemePackManager()->GetMask();
1507 const std::shared_ptr<IdItem> idItem = qd->GetIdItem();
1508 if (GetThemeDrawable(idItem, len, outValue, iconType, density) == SUCCESS) {
1509 drawableInfo = std::make_tuple(type, len, themeMask);
1510 return SUCCESS;
1511 }
1512
1513 RState state = hapManager_->GetMediaData(qd, len, outValue);
1514 drawableInfo = std::make_tuple(type, len, themeMask);
1515 return state;
1516 }
1517
GetStringFormatById(uint32_t id,std::string & outValue,std::vector<std::tuple<ResourceManager::NapiValueType,std::string>> & jsParams)1518 RState ResourceManagerImpl::GetStringFormatById(uint32_t id, std::string &outValue,
1519 std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams)
1520 {
1521 RState state = GetStringById(id, outValue);
1522 if (state != SUCCESS) {
1523 return state;
1524 }
1525 ResConfigImpl resConfig;
1526 GetResConfig(resConfig);
1527 if (!ReplacePlaceholderWithParams(outValue, resConfig, jsParams)) {
1528 return ERROR_CODE_RES_ID_FORMAT_ERROR;
1529 }
1530 return SUCCESS;
1531 }
1532
GetStringFormatByName(const char * name,std::string & outValue,std::vector<std::tuple<ResourceManager::NapiValueType,std::string>> & jsParams)1533 RState ResourceManagerImpl::GetStringFormatByName(const char *name, std::string &outValue,
1534 std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams)
1535 {
1536 RState state = GetStringByName(name, outValue);
1537 if (state != SUCCESS) {
1538 return state;
1539 }
1540 ResConfigImpl resConfig;
1541 GetResConfig(resConfig);
1542 if (!ReplacePlaceholderWithParams(outValue, resConfig, jsParams)) {
1543 return ERROR_CODE_RES_NAME_FORMAT_ERROR;
1544 }
1545 return SUCCESS;
1546 }
1547
GetFormatPluralStringById(std::string & outValue,uint32_t id,int quantity,std::vector<std::tuple<ResourceManager::NapiValueType,std::string>> & jsParams)1548 RState ResourceManagerImpl::GetFormatPluralStringById(std::string &outValue, uint32_t id, int quantity,
1549 std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams)
1550 {
1551 const std::shared_ptr<HapResource::ValueUnderQualifierDir> vuqd = hapManager_->FindQualifierValueById(id,
1552 isOverrideResMgr_);
1553 if (vuqd == nullptr) {
1554 RESMGR_HILOGE(RESMGR_TAG, "GetFormatPluralStringById error id = %{public}d", id);
1555 return ERROR_CODE_RES_ID_NOT_FOUND;
1556 }
1557 RState rState = GetPluralString(vuqd, quantity, outValue);
1558 if (rState == ERROR_CODE_RES_REF_TOO_MUCH) {
1559 RESMGR_HILOGE(RESMGR_TAG, "find too much ref by plural id = %{public}d", id);
1560 return rState;
1561 }
1562 if (rState != SUCCESS) {
1563 RESMGR_HILOGE(RESMGR_TAG, "plural res not found, id = %{public}d", id);
1564 return ERROR_CODE_RES_NOT_FOUND_BY_ID;
1565 }
1566 ResConfigImpl resConfig;
1567 GetResConfig(resConfig);
1568 if (!ReplacePlaceholderWithParams(outValue, resConfig, jsParams)) {
1569 RESMGR_HILOGE(RESMGR_TAG, "format plural string error, id = %{public}d", id);
1570 return ERROR_CODE_RES_NAME_FORMAT_ERROR;
1571 }
1572 return SUCCESS;
1573 }
1574
GetFormatPluralStringByName(std::string & outValue,const char * name,int quantity,std::vector<std::tuple<ResourceManager::NapiValueType,std::string>> & jsParams)1575 RState ResourceManagerImpl::GetFormatPluralStringByName(std::string &outValue, const char *name, int quantity,
1576 std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams)
1577 {
1578 const std::shared_ptr<HapResource::ValueUnderQualifierDir> vuqd =
1579 hapManager_->FindQualifierValueByName(name, ResType::PLURALS, isOverrideResMgr_);
1580 if (vuqd == nullptr) {
1581 RESMGR_HILOGE(RESMGR_TAG, "GetFormatPluralStringByName error name = %{public}s", name);
1582 return ERROR_CODE_RES_NAME_NOT_FOUND;
1583 }
1584 RState rState = GetPluralString(vuqd, quantity, outValue);
1585 if (rState == ERROR_CODE_RES_REF_TOO_MUCH) {
1586 RESMGR_HILOGE(RESMGR_TAG, "find too much ref by plural name = %{public}s", name);
1587 return rState;
1588 }
1589 if (rState != SUCCESS) {
1590 RESMGR_HILOGE(RESMGR_TAG, "plural res not found, name = %{public}s", name);
1591 return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
1592 }
1593 ResConfigImpl resConfig;
1594 GetResConfig(resConfig);
1595 if (!ReplacePlaceholderWithParams(outValue, resConfig, jsParams)) {
1596 RESMGR_HILOGE(RESMGR_TAG, "format plural string error, name = %{public}s", name);
1597 return ERROR_CODE_RES_NAME_FORMAT_ERROR;
1598 }
1599 return SUCCESS;
1600 }
1601
GetResourceLimitKeys()1602 uint32_t ResourceManagerImpl::GetResourceLimitKeys()
1603 {
1604 if (hapManager_ == nullptr) {
1605 RESMGR_HILOGE(RESMGR_TAG, "resource manager get limit keys failed, hapManager_ is nullptr");
1606 return 0;
1607 }
1608 return hapManager_->GetResourceLimitKeys();
1609 }
1610
GetRawFdNdkFromHap(const std::string & name,RawFileDescriptor & descriptor)1611 RState ResourceManagerImpl::GetRawFdNdkFromHap(const std::string &name, RawFileDescriptor &descriptor)
1612 {
1613 return hapManager_->GetRawFd(name, descriptor);
1614 }
1615
GetResId(const std::string & resTypeName,uint32_t & resId)1616 RState ResourceManagerImpl::GetResId(const std::string &resTypeName, uint32_t &resId)
1617 {
1618 return hapManager_->GetResId(resTypeName, resId);
1619 }
1620
GetLocales(std::vector<std::string> & outValue,bool includeSystem)1621 void ResourceManagerImpl::GetLocales(std::vector<std::string> &outValue, bool includeSystem)
1622 {
1623 hapManager_->GetLocales(outValue, includeSystem);
1624 }
1625
GetThemeIconInfo(const std::string & iconName,size_t & len,std::unique_ptr<uint8_t[]> & outValue,const std::string & abilityName)1626 RState ResourceManagerImpl::GetThemeIconInfo(const std::string &iconName, size_t &len,
1627 std::unique_ptr<uint8_t[]> &outValue, const std::string &abilityName)
1628 {
1629 std::string result = ThemePackManager::GetThemePackManager()->FindThemeIconResource(
1630 bundleInfo, iconName, abilityName);
1631 if (result.empty()) {
1632 RESMGR_HILOGD(RESMGR_TAG, "GetThemeIconInfo FAILED bundlename = %{public}s,", result.c_str());
1633 return ERROR_CODE_RES_ID_NOT_FOUND;
1634 }
1635 outValue = Utils::LoadResourceFile(result, len);
1636 if (outValue == nullptr) {
1637 RESMGR_HILOGD(RESMGR_TAG, "LoadResourceFile FAILED");
1638 return ERROR_CODE_RES_ID_NOT_FOUND;
1639 }
1640 return SUCCESS;
1641 }
1642
GetThemeIcons(uint32_t resId,std::pair<std::unique_ptr<uint8_t[]>,size_t> & foregroundInfo,std::pair<std::unique_ptr<uint8_t[]>,size_t> & backgroundInfo,uint32_t density,const std::string & abilityName)1643 RState ResourceManagerImpl::GetThemeIcons(uint32_t resId, std::pair<std::unique_ptr<uint8_t[]>, size_t>
1644 &foregroundInfo, std::pair<std::unique_ptr<uint8_t[]>, size_t> &backgroundInfo, uint32_t density,
1645 const std::string &abilityName)
1646 {
1647 RState foreState = GetThemeIconInfo(FOREGROUND, foregroundInfo.second, foregroundInfo.first, abilityName);
1648 RState backState = GetThemeIconInfo(BACKGROUND, backgroundInfo.second, backgroundInfo.first, abilityName);
1649 if (foreState == SUCCESS && backState == SUCCESS) {
1650 return SUCCESS;
1651 }
1652 return ERROR_CODE_RES_ID_NOT_FOUND;
1653 }
1654
GetDynamicIcon(const std::string & resName,std::pair<std::unique_ptr<uint8_t[]>,size_t> & iconInfo,uint32_t density)1655 RState ResourceManagerImpl::GetDynamicIcon(const std::string &resName,
1656 std::pair<std::unique_ptr<uint8_t[]>, size_t> &iconInfo, uint32_t density)
1657 {
1658 return GetThemeIconInfo(resName, iconInfo.second, iconInfo.first);
1659 }
1660
GetThemeMask()1661 std::string ResourceManagerImpl::GetThemeMask()
1662 {
1663 return ThemePackManager::GetThemePackManager()->GetMask();
1664 }
1665
HasIconInTheme(const std::string & bundleName)1666 bool ResourceManagerImpl::HasIconInTheme(const std::string &bundleName)
1667 {
1668 return ThemePackManager::GetThemePackManager()->HasIconInTheme(bundleName);
1669 }
1670
GetOtherIconsInfo(const std::string & iconName,std::unique_ptr<uint8_t[]> & outValue,size_t & len,bool isGlobalMask)1671 RState ResourceManagerImpl::GetOtherIconsInfo(const std::string &iconName,
1672 std::unique_ptr<uint8_t[]> &outValue, size_t &len, bool isGlobalMask)
1673 {
1674 std::string iconTag;
1675 if (iconName.find("icon_mask") != std::string::npos && isGlobalMask) {
1676 iconTag = "global_" + iconName;
1677 } else {
1678 iconTag = "other_icons_" + iconName;
1679 }
1680 RState result = ThemePackManager::GetThemePackManager()->GetThemeIconFromCache(iconTag, outValue, len);
1681 if (result == SUCCESS) {
1682 return SUCCESS;
1683 }
1684 return ThemePackManager::GetThemePackManager()->GetOtherIconsInfo(iconName, outValue, len, isGlobalMask);
1685 }
1686
IsRawDirFromHap(const std::string & pathName,bool & outValue)1687 RState ResourceManagerImpl::IsRawDirFromHap(const std::string &pathName, bool &outValue)
1688 {
1689 return hapManager_->IsRawDirFromHap(pathName, outValue);
1690 }
1691
GetHapManager()1692 std::shared_ptr<HapManager> ResourceManagerImpl::GetHapManager()
1693 {
1694 return hapManager_;
1695 }
1696
GetOverrideResourceManager(std::shared_ptr<ResConfig> overrideResConfig)1697 std::shared_ptr<ResourceManager> ResourceManagerImpl::GetOverrideResourceManager(
1698 std::shared_ptr<ResConfig> overrideResConfig)
1699 {
1700 ResourceManagerImpl *impl = new (std::nothrow) ResourceManagerImpl(true);
1701 if (impl == nullptr) {
1702 RESMGR_HILOGE(RESMGR_TAG, "new ResourceManagerImpl failed when GetOverrideResourceManager");
1703 return nullptr;
1704 }
1705
1706 if (!impl->Init(this->GetHapManager())) {
1707 delete (impl);
1708 return nullptr;
1709 }
1710
1711 std::shared_ptr<ResourceManager> overrideResMgr(impl);
1712 if (overrideResMgr == nullptr) {
1713 RESMGR_HILOGE(RESMGR_TAG, "GetOverrideResourceManager failed bundleName = %{public}s, moduleName = %{public}s",
1714 this->bundleInfo.first.c_str(), this->bundleInfo.second.c_str());
1715 return nullptr;
1716 }
1717
1718 overrideResMgr->bundleInfo.first = this->bundleInfo.first;
1719 overrideResMgr->bundleInfo.second = this->bundleInfo.second;
1720 if (overrideResConfig && overrideResMgr->UpdateOverrideResConfig(*overrideResConfig) != SUCCESS) {
1721 RESMGR_HILOGE(RESMGR_TAG, "GetOverrideResourceManager UpdateOverrideResConfig failed bundleName = %{public}s, \
1722 moduleName = %{public}s", this->bundleInfo.first.c_str(), this->bundleInfo.second.c_str());
1723 return nullptr;
1724 }
1725
1726 return overrideResMgr;
1727 }
1728 } // namespace Resource
1729 } // namespace Global
1730 } // namespace OHOS
1731