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