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