• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
AddSystemResource(const std::shared_ptr<ResourceManagerImpl> & systemResourceManager)61 bool ResourceManagerImpl::AddSystemResource(const std::shared_ptr<ResourceManagerImpl> &systemResourceManager)
62 {
63     if (systemResourceManager != nullptr) {
64         this->systemResourceManager_ = systemResourceManager;
65         this->hapManager_->AddSystemResource(systemResourceManager->hapManager_);
66         return true;
67     }
68     return false;
69 }
70 
ResourceManagerImpl(bool isOverrideResMgr)71 ResourceManagerImpl::ResourceManagerImpl(bool isOverrideResMgr) : hapManager_(nullptr),
72     isOverrideResMgr_(isOverrideResMgr)
73 {
74     psueManager_ = std::make_shared<PsueManager>();
75 }
76 
Init(bool isSystem)77 bool ResourceManagerImpl::Init(bool isSystem)
78 {
79     auto resConfig = std::make_shared<ResConfigImpl>();
80     if (resConfig == nullptr) {
81         RESMGR_HILOGE(RESMGR_TAG, "new ResConfigImpl failed when ResourceManagerImpl::Init");
82         return false;
83     }
84     hapManager_ = std::make_shared<HapManager>(resConfig, isSystem);
85     if (hapManager_ == nullptr) {
86         RESMGR_HILOGE(RESMGR_TAG, "new HapManager failed when ResourceManagerImpl::Init");
87         return false;
88     }
89     hapManager_->SetOverride(isOverrideResMgr_);
90     isSystemResMgr_ = isSystem;
91     return true;
92 }
93 
Init(std::shared_ptr<HapManager> hapManager)94 bool ResourceManagerImpl::Init(std::shared_ptr<HapManager> hapManager)
95 {
96     if (hapManager == nullptr) {
97         RESMGR_HILOGE(RESMGR_TAG, "ResourceManagerImpl::Init, hapManager is nullptr");
98         return false;
99     }
100     auto resConfig = std::make_shared<ResConfigImpl>();
101     if (resConfig == nullptr) {
102         RESMGR_HILOGD(RESMGR_TAG, "new ResConfigImpl failed when ResourceManagerImpl::Init");
103         return false;
104     }
105     hapManager->GetResConfig(*resConfig);
106     hapManager_ = std::make_shared<HapManager>(resConfig, hapManager->GetHapResource(),
107         hapManager->GetLoadedHapPaths(), hapManager->IsSystem());
108     if (hapManager_ == nullptr) {
109         RESMGR_HILOGE(RESMGR_TAG, "new HapManager failed from hapManager");
110         return false;
111     }
112     hapManager_->SetOverride(isOverrideResMgr_);
113     return true;
114 }
115 
GetStringById(uint32_t id,std::string & outValue)116 RState ResourceManagerImpl::GetStringById(uint32_t id, std::string &outValue)
117 {
118     const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceById(id, isOverrideResMgr_);
119     if (idItem == nullptr) {
120         RESMGR_HILOGE(RESMGR_TAG, "GetStringById error id = %{public}d", id);
121         return ERROR_CODE_RES_ID_NOT_FOUND;
122     }
123     RState state = GetString(idItem, outValue);
124     if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
125         return ERROR_CODE_RES_NOT_FOUND_BY_ID;
126     }
127     return state;
128 }
129 
GetStringByName(const char * name,std::string & outValue)130 RState ResourceManagerImpl::GetStringByName(const char *name, std::string &outValue)
131 {
132     const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceByName(name, ResType::STRING, isOverrideResMgr_);
133     if (idItem == nullptr) {
134         RESMGR_HILOGE(RESMGR_TAG, "GetStringByName error name = %{public}s", name);
135         return ERROR_CODE_RES_NAME_NOT_FOUND;
136     }
137     RState state = GetString(idItem, outValue);
138     if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
139         return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
140     }
141     return state;
142 }
143 
GetStringFormatById(std::string & outValue,uint32_t id,...)144 RState ResourceManagerImpl::GetStringFormatById(std::string &outValue, uint32_t id, ...)
145 {
146     const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceById(id, isOverrideResMgr_);
147     std::string temp;
148     RState rState = GetString(idItem, temp);
149     if (rState != SUCCESS) {
150         return rState;
151     }
152     va_list args;
153     va_start(args, id);
154     outValue = FormatString(temp.c_str(), args);
155     va_end(args);
156     return SUCCESS;
157 }
158 
GetStringFormatByName(std::string & outValue,const char * name,...)159 RState ResourceManagerImpl::GetStringFormatByName(std::string &outValue, const char *name, ...)
160 {
161     const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceByName(name, ResType::STRING, isOverrideResMgr_);
162     std::string temp;
163     RState rState = GetString(idItem, temp);
164     if (rState != SUCCESS) {
165         return rState;
166     }
167     va_list args;
168     va_start(args, name);
169     outValue = FormatString(temp.c_str(), args);
170     va_end(args);
171     return SUCCESS;
172 }
173 
GetStringFormatById(std::string & outValue,uint32_t id,va_list args)174 RState ResourceManagerImpl::GetStringFormatById(std::string &outValue, uint32_t id, va_list args)
175 {
176     RState state = GetStringById(id, outValue);
177     if (state != SUCCESS) {
178         return state;
179     }
180     std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> jsParams;
181     if (parseArgs(outValue, args, jsParams)) {
182         ResConfigImpl resConfig;
183         GetResConfig(resConfig);
184         if (!ReplacePlaceholderWithParams(outValue, resConfig, jsParams)) {
185             return ERROR_CODE_RES_ID_FORMAT_ERROR;
186         }
187         return SUCCESS;
188     }
189     return ERROR_CODE_INVALID_INPUT_PARAMETER;
190 }
191 
GetStringFormatByName(std::string & outValue,const char * name,va_list args)192 RState ResourceManagerImpl::GetStringFormatByName(std::string &outValue, const char *name, va_list args)
193 {
194     RState state = GetStringByName(name, outValue);
195     if (state != SUCCESS) {
196         return state;
197     }
198     std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> jsParams;
199     if (parseArgs(outValue, args, jsParams)) {
200         ResConfigImpl resConfig;
201         GetResConfig(resConfig);
202         if (!ReplacePlaceholderWithParams(outValue, resConfig, jsParams)) {
203             return ERROR_CODE_RES_NAME_FORMAT_ERROR;
204         }
205         return SUCCESS;
206     }
207     return ERROR_CODE_INVALID_INPUT_PARAMETER;
208 }
209 
GetString(const std::shared_ptr<IdItem> idItem,std::string & outValue)210 RState ResourceManagerImpl::GetString(const std::shared_ptr<IdItem> idItem, std::string &outValue)
211 {
212     // not found or type invalid
213     if (idItem == nullptr || idItem->resType_ != ResType::STRING) {
214         return NOT_FOUND;
215     }
216     RState ret = ResolveReference(idItem->value_, outValue);
217     if (isFakeLocale) {
218         ProcessPsuedoTranslate(outValue);
219     }
220     if (isBidirectionFakeLocale) {
221         outValue = psueManager_->BidirectionConvert(outValue);
222     }
223     if (ret != SUCCESS) {
224         return ret;
225     }
226     return SUCCESS;
227 }
228 
GetStringArrayById(uint32_t id,std::vector<std::string> & outValue)229 RState ResourceManagerImpl::GetStringArrayById(uint32_t id, std::vector<std::string> &outValue)
230 {
231     const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceById(id, isOverrideResMgr_);
232     if (idItem == nullptr) {
233         RESMGR_HILOGE(RESMGR_TAG, "GetStringArrayById error id = %{public}d", id);
234         return ERROR_CODE_RES_ID_NOT_FOUND;
235     }
236     RState state = GetStringArray(idItem, outValue);
237     if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
238         return ERROR_CODE_RES_NOT_FOUND_BY_ID;
239     }
240     return state;
241 }
242 
GetStringArrayByName(const char * name,std::vector<std::string> & outValue)243 RState ResourceManagerImpl::GetStringArrayByName(const char *name, std::vector<std::string> &outValue)
244 {
245     const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceByName(
246         name, ResType::STRINGARRAY, isOverrideResMgr_);
247     if (idItem == nullptr) {
248         RESMGR_HILOGE(RESMGR_TAG, "GetStringArrayByName error name = %{public}s", name);
249         return ERROR_CODE_RES_NAME_NOT_FOUND;
250     }
251     RState state = GetStringArray(idItem, outValue);
252     if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
253         return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
254     }
255     return state;
256 }
257 
GetStringArray(const std::shared_ptr<IdItem> idItem,std::vector<std::string> & outValue)258 RState ResourceManagerImpl::GetStringArray(const std::shared_ptr<IdItem> idItem, std::vector<std::string> &outValue)
259 {
260     // not found or type invalid
261     if (idItem == nullptr || idItem->resType_ != ResType::STRINGARRAY) {
262         return NOT_FOUND;
263     }
264     outValue.clear();
265 
266     for (size_t i = 0; i < idItem->values_.size(); ++i) {
267         std::string resolvedValue;
268         RState rrRet = ResolveReference(idItem->values_[i], resolvedValue);
269         if (rrRet != SUCCESS) {
270             RESMGR_HILOGD(RESMGR_TAG,
271                 "GetStringArray ResolveReference failed, value:%{public}s", idItem->values_[i].c_str());
272             return rrRet;
273         }
274         outValue.push_back(resolvedValue);
275     }
276     if (isFakeLocale) {
277         for (auto &iter : outValue) {
278             ProcessPsuedoTranslate(iter);
279         }
280     }
281     if (isBidirectionFakeLocale) {
282         for (auto &iter : outValue) {
283             iter = psueManager_->BidirectionConvert(iter);
284         }
285     }
286     return SUCCESS;
287 }
288 
GetPatternById(uint32_t id,std::map<std::string,std::string> & outValue)289 RState ResourceManagerImpl::GetPatternById(uint32_t id, std::map<std::string, std::string> &outValue)
290 {
291     const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceById(id, isOverrideResMgr_);
292     if (idItem == nullptr) {
293         RESMGR_HILOGE(RESMGR_TAG, "GetPatternById error id = %{public}d", id);
294         return ERROR_CODE_RES_ID_NOT_FOUND;
295     }
296     RState state = GetPattern(idItem, outValue);
297     if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
298         return ERROR_CODE_RES_NOT_FOUND_BY_ID;
299     }
300     return state;
301 }
302 
GetPatternDataById(uint32_t id,std::map<std::string,ResData> & outValue)303 RState ResourceManagerImpl::GetPatternDataById(uint32_t id, std::map<std::string, ResData> &outValue)
304 {
305     const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceById(id, isOverrideResMgr_);
306     if (idItem == nullptr) {
307         RESMGR_HILOGE(RESMGR_TAG, "GetPatternDataById error id = %{public}d", id);
308         return ERROR_CODE_RES_ID_NOT_FOUND;
309     }
310     RState state = GetPatternData(idItem, outValue);
311     if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
312         return ERROR_CODE_RES_NOT_FOUND_BY_ID;
313     }
314     return state;
315 }
316 
GetPatternByName(const char * name,std::map<std::string,std::string> & outValue)317 RState ResourceManagerImpl::GetPatternByName(const char *name, std::map<std::string, std::string> &outValue)
318 {
319     const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceByName(name, ResType::PATTERN, isOverrideResMgr_);
320     if (idItem == nullptr) {
321         RESMGR_HILOGE(RESMGR_TAG, "GetPatternByName error name = %{public}s", name);
322         return ERROR_CODE_RES_NAME_NOT_FOUND;
323     }
324     RState state = GetPattern(idItem, outValue);
325     if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
326         return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
327     }
328     return state;
329 }
330 
GetPatternDataByName(const char * name,std::map<std::string,ResData> & outValue)331 RState ResourceManagerImpl::GetPatternDataByName(const char *name, std::map<std::string, ResData> &outValue)
332 {
333     const std::shared_ptr<IdItem> idItem = hapManager_->FindResourceByName(name, ResType::PATTERN, isOverrideResMgr_);
334     if (idItem == nullptr) {
335         RESMGR_HILOGE(RESMGR_TAG, "GetPatternDataByName error name = %{public}s", name);
336         return ERROR_CODE_RES_NAME_NOT_FOUND;
337     }
338     RState state = GetPatternData(idItem, outValue);
339     if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
340         return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
341     }
342     return state;
343 }
344 
GetPattern(const std::shared_ptr<IdItem> idItem,std::map<std::string,std::string> & outValue)345 RState ResourceManagerImpl::GetPattern(const std::shared_ptr<IdItem> idItem, std::map<std::string,
346     std::string> &outValue)
347 {
348     //type invalid
349     if (idItem->resType_ != ResType::PATTERN) {
350         RESMGR_HILOGE(RESMGR_TAG,
351             "actual resType = %{public}d, expect resType = %{public}d", idItem->resType_, ResType::PATTERN);
352         return NOT_FOUND;
353     }
354     return ResolveParentReference(idItem, outValue);
355 }
356 
GetPatternData(const std::shared_ptr<IdItem> idItem,std::map<std::string,ResData> & outValue)357 RState ResourceManagerImpl::GetPatternData(const std::shared_ptr<IdItem> idItem,
358     std::map<std::string, ResData> &outValue)
359 {
360     //type invalid
361     if (idItem->resType_ != ResType::PATTERN) {
362         RESMGR_HILOGE(RESMGR_TAG,
363             "actual resType = %{public}d, expect resType = %{public}d", idItem->resType_, ResType::PATTERN);
364         return NOT_FOUND;
365     }
366     return ResolveResData(idItem, outValue);
367 }
368 
GetPluralStringById(uint32_t id,int quantity,std::string & outValue)369 RState ResourceManagerImpl::GetPluralStringById(uint32_t id, int quantity, std::string &outValue)
370 {
371     const std::shared_ptr<HapResource::ValueUnderQualifierDir> vuqd = hapManager_->FindQualifierValueById(id,
372         isOverrideResMgr_);
373     Quantity intQuantity = { true, quantity, 0.0 };
374     return GetPluralString(vuqd, intQuantity, outValue);
375 }
376 
GetPluralStringByName(const char * name,int quantity,std::string & outValue)377 RState ResourceManagerImpl::GetPluralStringByName(const char *name, int quantity, std::string &outValue)
378 {
379     const std::shared_ptr<HapResource::ValueUnderQualifierDir> vuqd =
380         hapManager_->FindQualifierValueByName(name, ResType::PLURALS, isOverrideResMgr_);
381         Quantity intQuantity = { true, quantity, 0.0 };
382     return GetPluralString(vuqd, intQuantity, outValue);
383 }
384 
GetPluralStringByIdFormat(std::string & outValue,uint32_t id,int quantity,...)385 RState ResourceManagerImpl::GetPluralStringByIdFormat(std::string &outValue, uint32_t id, int quantity, ...)
386 {
387     const std::shared_ptr<HapResource::ValueUnderQualifierDir> vuqd = hapManager_->FindQualifierValueById(id,
388         isOverrideResMgr_);
389     if (vuqd == nullptr) {
390         RESMGR_HILOGE(RESMGR_TAG, "GetPluralStringByIdFormat error id = %{public}d", id);
391         return ERROR_CODE_RES_ID_NOT_FOUND;
392     }
393     std::string temp;
394     Quantity intQuantity = { true, quantity, 0.0 };
395     RState rState = GetPluralString(vuqd, intQuantity, temp);
396     if (rState == ERROR_CODE_RES_REF_TOO_MUCH) {
397         return rState;
398     }
399     if (rState != SUCCESS) {
400         return ERROR_CODE_RES_NOT_FOUND_BY_ID;
401     }
402 
403     va_list args;
404     va_start(args, quantity);
405     outValue = FormatString(temp.c_str(), args);
406     va_end(args);
407 
408     return SUCCESS;
409 }
410 
GetPluralStringByNameFormat(std::string & outValue,const char * name,int quantity,...)411 RState ResourceManagerImpl::GetPluralStringByNameFormat(std::string &outValue, const char *name, int quantity, ...)
412 {
413     const std::shared_ptr<HapResource::ValueUnderQualifierDir> vuqd =
414         hapManager_->FindQualifierValueByName(name, ResType::PLURALS, isOverrideResMgr_);
415     if (vuqd == nullptr) {
416         RESMGR_HILOGE(RESMGR_TAG, "GetPluralStringByNameFormat error name = %{public}s", name);
417         return ERROR_CODE_RES_NAME_NOT_FOUND;
418     }
419     std::string temp;
420     Quantity intQuantity = { true, quantity, 0.0 };
421     RState rState = GetPluralString(vuqd, intQuantity, temp);
422     if (rState == ERROR_CODE_RES_REF_TOO_MUCH) {
423         return rState;
424     }
425     if (rState != SUCCESS) {
426         return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
427     }
428 
429     va_list args;
430     va_start(args, quantity);
431     outValue = FormatString(temp.c_str(), args);
432     va_end(args);
433 
434     return SUCCESS;
435 }
436 
GetPluralString(const std::shared_ptr<HapResource::ValueUnderQualifierDir> vuqd,Quantity quantity,std::string & outValue)437 RState ResourceManagerImpl::GetPluralString(const std::shared_ptr<HapResource::ValueUnderQualifierDir> vuqd,
438     Quantity quantity, std::string &outValue)
439 {
440     // not found or type invalid
441     if (vuqd == nullptr) {
442         return NOT_FOUND;
443     }
444     auto idItem = vuqd->GetIdItem();
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_HILOGE(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_HILOGE(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_HILOGE(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_HILOGE(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_HILOGE(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_HILOGE(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_HILOGE(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 qd = hapManager_->FindQualifierValueById(id, isOverrideResMgr_);
1247     if (qd == 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(qd, 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 qd = hapManager_->FindQualifierValueByName(name, ResType::PROF, isOverrideResMgr_);
1258     if (qd == nullptr) {
1259         RESMGR_HILOGE(RESMGR_TAG,
1260             "GetProfileByName error name = %{public}s", name);
1261         return ERROR_CODE_RES_NAME_NOT_FOUND;
1262     }
1263     RState state = hapManager_->GetFilePath(qd, 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 qd = hapManager_->FindQualifierValueById(id, isOverrideResMgr_, density);
1274     if (qd == 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(qd, 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 qd = hapManager_->FindQualifierValueByName(name, ResType::MEDIA, isOverrideResMgr_, density);
1289     if (qd == nullptr) {
1290         RESMGR_HILOGE(RESMGR_TAG, "GetMediaByName error name = %{public}s", name);
1291         return ERROR_CODE_RES_NAME_NOT_FOUND;
1292     }
1293     RState state = hapManager_->GetFilePath(qd, 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(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1331 #endif
1332 
1333 #if defined(__ARKUI_CROSS__)
1334     if (!isSystemResMgr_ && std::string(path).find("/systemres/resources.index") != std::string::npos) {
1335         ResourceManagerImpl* systemResourceManager = SystemResourceManager::GetSystemResourceManager();
1336         if (systemResourceManager != nullptr) {
1337             systemResourceManager->AddResource(path);
1338             AddSystemResource(systemResourceManager);
1339             return true;
1340         }
1341     }
1342 #endif
1343     return this->hapManager_->AddResource(path, selectedTypes, forceReload);
1344 }
1345 
AddPatchResource(const char * path,const char * patchPath)1346 bool ResourceManagerImpl::AddPatchResource(const char *path, const char *patchPath)
1347 {
1348     return this->hapManager_->AddPatchResource(path, patchPath);
1349 }
1350 
AddResource(const std::string & path,const std::vector<std::string> & overlayPaths)1351 bool ResourceManagerImpl::AddResource(const std::string &path, const std::vector<std::string> &overlayPaths)
1352 {
1353     return this->hapManager_->AddResource(path, overlayPaths);
1354 }
1355 
RemoveResource(const std::string & path,const std::vector<std::string> & overlayPaths)1356 bool ResourceManagerImpl::RemoveResource(const std::string &path, const std::vector<std::string> &overlayPaths)
1357 {
1358     return this->hapManager_->RemoveResource(path, overlayPaths);
1359 }
1360 
AddAppOverlay(const std::string & path)1361 bool ResourceManagerImpl::AddAppOverlay(const std::string &path)
1362 {
1363     return this->hapManager_->AddAppOverlay(path);
1364 }
1365 
RemoveAppOverlay(const std::string & path)1366 bool ResourceManagerImpl::RemoveAppOverlay(const std::string &path)
1367 {
1368     return this->hapManager_->RemoveAppOverlay(path);
1369 }
1370 
UpdateFakeLocaleFlag(ResConfig & resConfig)1371 RState ResourceManagerImpl::UpdateFakeLocaleFlag(ResConfig &resConfig)
1372 {
1373 #ifdef SUPPORT_GRAPHICS
1374     if (resConfig.GetLocaleInfo() == nullptr) {
1375         return LOCALEINFO_IS_NULL;
1376     }
1377     if (resConfig.GetLocaleInfo()->getLanguage() == nullptr) {
1378         return LOCALEINFO_IS_NULL;
1379     }
1380     const char* language = resConfig.GetLocaleInfo()->getLanguage();
1381     const char* region = resConfig.GetLocaleInfo()->getCountry();
1382     if (language != nullptr && region != nullptr) {
1383         std::string languageStr = language;
1384         std::string regionStr = region;
1385         if (languageStr == "en" && regionStr == "XA") {
1386             isFakeLocale = true;
1387         } else {
1388             isFakeLocale = false;
1389         }
1390         if (languageStr == "ar" && regionStr == "XB") {
1391             isBidirectionFakeLocale = true;
1392         } else {
1393             isBidirectionFakeLocale = false;
1394         }
1395     }
1396 #endif
1397     return SUCCESS;
1398 }
1399 
UpdateResConfig(ResConfig & resConfig,bool isUpdateTheme)1400 RState ResourceManagerImpl::UpdateResConfig(ResConfig &resConfig, bool isUpdateTheme)
1401 {
1402     auto themePackManager = ThemePackManager::GetThemePackManager();
1403     if (themePackManager->UpdateThemeId(resConfig.GetThemeId())) {
1404         if (resConfig.GetThemeIcon()) {
1405             RESMGR_HILOGD(RESMGR_TAG, "The themeIcon enabled");
1406             themePackManager->LoadThemeIconRes(bundleInfo.first, bundleInfo.second, userId);
1407         } else {
1408             RESMGR_HILOGD(RESMGR_TAG, "The theme enabled");
1409             themePackManager->LoadThemeRes(bundleInfo.first, bundleInfo.second, userId);
1410         }
1411     }
1412 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
1413     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1414 #endif
1415     RState state = UpdateFakeLocaleFlag(resConfig);
1416     if (state != SUCCESS) {
1417         return state;
1418     }
1419     return this->hapManager_->UpdateResConfig(resConfig);
1420 }
1421 
UpdateOverrideResConfig(ResConfig & resConfig)1422 RState ResourceManagerImpl::UpdateOverrideResConfig(ResConfig &resConfig)
1423 {
1424 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
1425     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1426 #endif
1427     UpdateFakeLocaleFlag(resConfig);
1428     return this->hapManager_->UpdateOverrideResConfig(resConfig);
1429 }
1430 
GetResConfig(ResConfig & resConfig)1431 void ResourceManagerImpl::GetResConfig(ResConfig &resConfig)
1432 {
1433     this->hapManager_->GetResConfig(resConfig);
1434 }
1435 
GetResConfigById(uint32_t resId,ResConfig & resConfig,uint32_t density)1436 RState ResourceManagerImpl::GetResConfigById(uint32_t resId, ResConfig &resConfig, uint32_t density)
1437 {
1438     if (!IsDensityValid(density)) {
1439         RESMGR_HILOGD(RESMGR_TAG, "density invalid");
1440         return ERROR_CODE_INVALID_INPUT_PARAMETER;
1441     }
1442 
1443     return this->hapManager_->GetResConfigById(resId, resConfig, isOverrideResMgr_, density);
1444 }
1445 
GetResConfigByName(const std::string & name,const ResType type,ResConfig & resConfig,uint32_t density)1446 RState ResourceManagerImpl::GetResConfigByName(const std::string &name, const ResType type,
1447     ResConfig &resConfig, uint32_t density)
1448 {
1449     if (!IsDensityValid(density)) {
1450         RESMGR_HILOGD(RESMGR_TAG, "density invalid");
1451         return ERROR_CODE_INVALID_INPUT_PARAMETER;
1452     }
1453 
1454     return this->hapManager_->GetResConfigByName(name, type, resConfig, isOverrideResMgr_, density);
1455 }
1456 
GetOverrideResConfig(ResConfig & resConfig)1457 void ResourceManagerImpl::GetOverrideResConfig(ResConfig &resConfig)
1458 {
1459     this->hapManager_->GetOverrideResConfig(resConfig);
1460 }
1461 
GetResourcePaths()1462 std::vector<std::string> ResourceManagerImpl::GetResourcePaths()
1463 {
1464     return this->hapManager_->GetResourcePaths();
1465 }
1466 
IsDensityValid(uint32_t density)1467 bool ResourceManagerImpl::IsDensityValid(uint32_t density)
1468 {
1469     switch (density) {
1470         case SCREEN_DENSITY_NOT_SET:
1471         case SCREEN_DENSITY_SDPI:
1472         case SCREEN_DENSITY_MDPI:
1473         case SCREEN_DENSITY_LDPI:
1474         case SCREEN_DENSITY_XLDPI:
1475         case SCREEN_DENSITY_XXLDPI:
1476         case SCREEN_DENSITY_XXXLDPI:
1477             return true;
1478         default:
1479             return false;
1480     }
1481 }
1482 
GetThemeMedia(const std::shared_ptr<IdItem> idItem,size_t & len,std::unique_ptr<uint8_t[]> & outValue,uint32_t density)1483 RState ResourceManagerImpl::GetThemeMedia(const std::shared_ptr<IdItem> idItem, size_t &len,
1484     std::unique_ptr<uint8_t[]> &outValue, uint32_t density)
1485 {
1486     ResConfigImpl resConfig;
1487     GetResConfig(resConfig);
1488     std::vector<std::shared_ptr<IdItem>> idItems;
1489     idItems.emplace_back(idItem);
1490     std::string result = ThemePackManager::GetThemePackManager()->FindThemeResource(
1491         bundleInfo, idItems, resConfig, userId);
1492     outValue = Utils::LoadResourceFile(result, len);
1493     return result.empty() ? ERROR_CODE_RES_ID_NOT_FOUND : SUCCESS;
1494 }
1495 
GetMediaDataById(uint32_t id,size_t & len,std::unique_ptr<uint8_t[]> & outValue,uint32_t density)1496 RState ResourceManagerImpl::GetMediaDataById(uint32_t id, size_t &len, std::unique_ptr<uint8_t[]> &outValue,
1497     uint32_t density)
1498 {
1499     if (!IsDensityValid(density)) {
1500         RESMGR_HILOGE(RESMGR_TAG, "density invalid");
1501         return ERROR_CODE_INVALID_INPUT_PARAMETER;
1502     }
1503     auto qd = hapManager_->FindQualifierValueById(id, isOverrideResMgr_, density);
1504     if (qd == nullptr) {
1505         RESMGR_HILOGE(RESMGR_TAG, "GetMediaDataById error id = %{public}d", id);
1506         return ERROR_CODE_RES_ID_NOT_FOUND;
1507     }
1508 
1509     // find in theme
1510     const std::shared_ptr<IdItem> idItem = qd->GetIdItem();
1511     if (GetThemeMedia(idItem, len, outValue, density) == SUCCESS) {
1512         return SUCCESS;
1513     }
1514 
1515     RState state = hapManager_->GetMediaData(qd, len, outValue);
1516     return state == SUCCESS ? state : ERROR_CODE_RES_NOT_FOUND_BY_ID;
1517 }
1518 
GetMediaDataByName(const char * name,size_t & len,std::unique_ptr<uint8_t[]> & outValue,uint32_t density)1519 RState ResourceManagerImpl::GetMediaDataByName(const char *name, size_t &len, std::unique_ptr<uint8_t[]> &outValue,
1520     uint32_t density)
1521 {
1522     if (!IsDensityValid(density)) {
1523         RESMGR_HILOGE(RESMGR_TAG, "density invalid");
1524         return ERROR_CODE_INVALID_INPUT_PARAMETER;
1525     }
1526 
1527     auto qd = hapManager_->FindQualifierValueByName(name, ResType::MEDIA, isOverrideResMgr_, density);
1528     if (qd == nullptr) {
1529         RESMGR_HILOGE(RESMGR_TAG,
1530             "GetMediaDataByName error name = %{public}s", name);
1531         return ERROR_CODE_RES_NAME_NOT_FOUND;
1532     }
1533 
1534     const std::shared_ptr<IdItem> idItem = qd->GetIdItem();
1535     if (GetThemeMedia(idItem, len, outValue, density) == SUCCESS) {
1536         return SUCCESS;
1537     }
1538 
1539     RState state = hapManager_->GetMediaData(qd, len, outValue);
1540     return state == SUCCESS ? state : ERROR_CODE_RES_NOT_FOUND_BY_NAME;
1541 }
1542 
GetThemeMediaBase64(const std::shared_ptr<IdItem> idItem,std::string & outValue)1543 RState ResourceManagerImpl::GetThemeMediaBase64(const std::shared_ptr<IdItem> idItem, std::string &outValue)
1544 {
1545     ResConfigImpl resConfig;
1546     GetResConfig(resConfig);
1547     std::vector<std::shared_ptr<IdItem>> idItems;
1548     idItems.emplace_back(idItem);
1549     std::string result = ThemePackManager::GetThemePackManager()->FindThemeResource(
1550         bundleInfo, idItems, resConfig, userId);
1551     if (result.empty()) {
1552         return NOT_FOUND;
1553     }
1554     return Utils::GetMediaBase64Data(result, outValue);
1555 }
1556 
GetMediaBase64DataById(uint32_t id,std::string & outValue,uint32_t density)1557 RState ResourceManagerImpl::GetMediaBase64DataById(uint32_t id, std::string &outValue, uint32_t density)
1558 {
1559     if (!IsDensityValid(density)) {
1560         RESMGR_HILOGE(RESMGR_TAG, "density invalid");
1561         return ERROR_CODE_INVALID_INPUT_PARAMETER;
1562     }
1563 
1564     auto qd = hapManager_->FindQualifierValueById(id, isOverrideResMgr_, density);
1565     if (qd == nullptr) {
1566         RESMGR_HILOGE(RESMGR_TAG, "GetMediaBase64DataById error id = %{public}d", id);
1567         return ERROR_CODE_RES_ID_NOT_FOUND;
1568     }
1569 
1570     const std::shared_ptr<IdItem> idItem = qd->GetIdItem();
1571     if (GetThemeMediaBase64(idItem, outValue) == SUCCESS) {
1572         return SUCCESS;
1573     }
1574 
1575     RState state = hapManager_->GetMediaBase64Data(qd, outValue);
1576     return state == SUCCESS ? state : ERROR_CODE_RES_NOT_FOUND_BY_ID;
1577 }
1578 
GetMediaBase64DataByName(const char * name,std::string & outValue,uint32_t density)1579 RState ResourceManagerImpl::GetMediaBase64DataByName(const char *name, std::string &outValue, uint32_t density)
1580 {
1581     if (!IsDensityValid(density)) {
1582         RESMGR_HILOGE(RESMGR_TAG, "density invalid");
1583         return ERROR_CODE_INVALID_INPUT_PARAMETER;
1584     }
1585     auto qd = hapManager_->FindQualifierValueByName(name, ResType::MEDIA, isOverrideResMgr_, density);
1586     if (qd == nullptr) {
1587         RESMGR_HILOGE(RESMGR_TAG,
1588             "GetMediaBase64DataByName error name = %{public}s", name);
1589         return ERROR_CODE_RES_NAME_NOT_FOUND;
1590     }
1591 
1592     const std::shared_ptr<IdItem> idItem = qd->GetIdItem();
1593     if (GetThemeMediaBase64(idItem, outValue) == SUCCESS) {
1594         return SUCCESS;
1595     }
1596 
1597     RState state = hapManager_->GetMediaBase64Data(qd, outValue);
1598     return state == SUCCESS ? state : ERROR_CODE_RES_NOT_FOUND_BY_NAME;
1599 }
1600 
GetProfileDataById(uint32_t id,size_t & len,std::unique_ptr<uint8_t[]> & outValue)1601 RState ResourceManagerImpl::GetProfileDataById(uint32_t id, size_t &len, std::unique_ptr<uint8_t[]> &outValue)
1602 {
1603     auto qd = hapManager_->FindQualifierValueById(id, isOverrideResMgr_);
1604     if (qd == nullptr) {
1605         RESMGR_HILOGE(RESMGR_TAG, "GetProfileDataById error id = %{public}d", id);
1606         return ERROR_CODE_RES_NOT_FOUND_BY_ID;
1607     }
1608     return hapManager_->GetProfileData(qd, len, outValue);
1609 }
1610 
GetProfileDataByName(const char * name,size_t & len,std::unique_ptr<uint8_t[]> & outValue)1611 RState ResourceManagerImpl::GetProfileDataByName(const char *name, size_t &len, std::unique_ptr<uint8_t[]> &outValue)
1612 {
1613     auto qd = hapManager_->FindQualifierValueByName(name, ResType::PROF, isOverrideResMgr_);
1614     if (qd == nullptr) {
1615         RESMGR_HILOGE(RESMGR_TAG,
1616             "GetProfileDataByName error name = %{public}s", name);
1617         return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
1618     }
1619     return hapManager_->GetProfileData(qd, len, outValue);
1620 }
1621 
GetRawFileFromHap(const std::string & rawFileName,size_t & len,std::unique_ptr<uint8_t[]> & outValue)1622 RState ResourceManagerImpl::GetRawFileFromHap(const std::string &rawFileName, size_t &len,
1623     std::unique_ptr<uint8_t[]> &outValue)
1624 {
1625     return hapManager_->FindRawFileFromHap(rawFileName, len, outValue);
1626 }
1627 
GetRawFileDescriptorFromHap(const std::string & rawFileName,RawFileDescriptor & descriptor)1628 RState ResourceManagerImpl::GetRawFileDescriptorFromHap(const std::string &rawFileName, RawFileDescriptor &descriptor)
1629 {
1630     return hapManager_->FindRawFileDescriptorFromHap(rawFileName, descriptor);
1631 }
1632 
IsLoadHap(std::string & hapPath)1633 RState ResourceManagerImpl::IsLoadHap(std::string &hapPath)
1634 {
1635     if (hapManager_->IsLoadHap(hapPath)) {
1636         return SUCCESS;
1637     }
1638     return NOT_FOUND;
1639 }
1640 
GetRawFileList(const std::string & rawDirPath,std::vector<std::string> & rawfileList)1641 RState ResourceManagerImpl::GetRawFileList(const std::string &rawDirPath, std::vector<std::string>& rawfileList)
1642 {
1643     return hapManager_->GetRawFileList(rawDirPath, rawfileList);
1644 }
1645 
GetSuffix(const std::shared_ptr<HapResource::ValueUnderQualifierDir> qd)1646 std::string GetSuffix(const std::shared_ptr<HapResource::ValueUnderQualifierDir> qd)
1647 {
1648     const std::shared_ptr<IdItem> idItem = qd->GetIdItem();
1649     if (idItem == nullptr || idItem->resType_ != ResType::MEDIA) {
1650         return std::string();
1651     }
1652     std::string mediaPath = idItem->value_;
1653     auto pos = mediaPath.find_last_of('.');
1654     if (pos == std::string::npos) {
1655         return std::string();
1656     }
1657     return mediaPath.substr(pos + 1);
1658 }
1659 
GetThemeIcon(const std::shared_ptr<IdItem> idItem,size_t & len,std::unique_ptr<uint8_t[]> & outValue,uint32_t density)1660 RState ResourceManagerImpl::GetThemeIcon(const std::shared_ptr<IdItem> idItem, size_t &len,
1661     std::unique_ptr<uint8_t[]> &outValue, uint32_t density)
1662 {
1663     std::string iconName = idItem->GetItemResName();
1664     std::string result = ThemePackManager::GetThemePackManager()->FindThemeIconResource(
1665         bundleInfo, iconName, userId);
1666     if (result.empty()) {
1667         RESMGR_HILOGD(RESMGR_TAG,
1668             "GetThemeIcon FAILED bundlename = %{public}s, modulename = %{public}s, iconName = %{public}s",
1669             bundleInfo.first.c_str(), bundleInfo.second.c_str(), iconName.c_str());
1670         return ERROR_CODE_RES_ID_NOT_FOUND;
1671     }
1672     outValue = Utils::LoadResourceFile(result, len);
1673     return SUCCESS;
1674 }
1675 
GetThemeDrawable(const std::shared_ptr<IdItem> idItem,size_t & len,std::unique_ptr<uint8_t[]> & outValue,uint32_t iconType,uint32_t density)1676 RState ResourceManagerImpl::GetThemeDrawable(const std::shared_ptr<IdItem> idItem, size_t &len,
1677     std::unique_ptr<uint8_t[]> &outValue, uint32_t iconType, uint32_t density)
1678 {
1679     if (iconType == 0 && GetThemeMedia(idItem, len, outValue, density) == SUCCESS) {
1680         return SUCCESS;
1681     } else if (iconType == 1 && GetThemeIcon(idItem, len, outValue, density) == SUCCESS) {
1682         return SUCCESS;
1683     } else {
1684         // other type
1685     }
1686     return ERROR_CODE_RES_NOT_FOUND_BY_ID;
1687 }
1688 
GetDrawableInfoById(uint32_t id,std::string & type,size_t & len,std::unique_ptr<uint8_t[]> & outValue,uint32_t density)1689 RState ResourceManagerImpl::GetDrawableInfoById(uint32_t id, std::string &type, size_t &len,
1690     std::unique_ptr<uint8_t[]> &outValue, uint32_t density)
1691 {
1692     if (!IsDensityValid(density)) {
1693         RESMGR_HILOGE(RESMGR_TAG, "density invalid");
1694         return ERROR_CODE_INVALID_INPUT_PARAMETER;
1695     }
1696     auto qd = hapManager_->FindQualifierValueById(id, isOverrideResMgr_, density);
1697     if (qd == nullptr) {
1698         RESMGR_HILOGE(RESMGR_TAG, "GetDrawableInfoById id = %{public}d", id);
1699         return ERROR_CODE_RES_ID_NOT_FOUND;
1700     }
1701     type = GetSuffix(qd);
1702     if (type.empty()) {
1703         RESMGR_HILOGE(RESMGR_TAG, "failed to get resourceType");
1704         return ERROR_CODE_RES_NOT_FOUND_BY_ID;
1705     }
1706     return hapManager_->GetMediaData(qd, len, outValue);
1707 }
1708 
GetDrawableInfoByName(const char * name,std::string & type,size_t & len,std::unique_ptr<uint8_t[]> & outValue,uint32_t density)1709 RState ResourceManagerImpl::GetDrawableInfoByName(const char *name, std::string &type, size_t &len,
1710     std::unique_ptr<uint8_t[]> &outValue, uint32_t density)
1711 {
1712     if (!IsDensityValid(density)) {
1713         RESMGR_HILOGE(RESMGR_TAG, "density invalid");
1714         return ERROR_CODE_INVALID_INPUT_PARAMETER;
1715     }
1716     auto qd = hapManager_->FindQualifierValueByName(name, ResType::MEDIA, isOverrideResMgr_, density);
1717     if (qd == nullptr) {
1718         RESMGR_HILOGE(RESMGR_TAG, "GetDrawableInfoByName error name = %{public}s", name);
1719         return ERROR_CODE_RES_NAME_NOT_FOUND;
1720     }
1721     type = GetSuffix(qd);
1722     if (type.empty()) {
1723         RESMGR_HILOGE(RESMGR_TAG, "failed to get resourceType");
1724         return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
1725     }
1726     return hapManager_->GetMediaData(qd, len, outValue);
1727 }
1728 
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)1729 RState ResourceManagerImpl::GetDrawableInfoById(uint32_t id,
1730     std::tuple<std::string, size_t, std::string> &drawableInfo,
1731     std::unique_ptr<uint8_t[]> &outValue, uint32_t iconType, uint32_t density)
1732 {
1733     if (!IsDensityValid(density)) {
1734         RESMGR_HILOGE(RESMGR_TAG, "density invalid");
1735         return ERROR_CODE_INVALID_INPUT_PARAMETER;
1736     }
1737     auto qd = hapManager_->FindQualifierValueById(id, isOverrideResMgr_, density);
1738     if (qd == nullptr) {
1739         RESMGR_HILOGE(RESMGR_TAG, "GetDrawableInfoById error id = %{public}d", id);
1740         return ERROR_CODE_RES_ID_NOT_FOUND;
1741     }
1742     std::string type = GetSuffix(qd);
1743     if (type.empty()) {
1744         RESMGR_HILOGE(RESMGR_TAG, "failed to get resourceType");
1745         return ERROR_CODE_RES_NOT_FOUND_BY_ID;
1746     }
1747     size_t len = 0;
1748     // find in theme
1749     const std::shared_ptr<IdItem> idItem = qd->GetIdItem();
1750     std::string themeMask = ThemePackManager::GetThemePackManager()->GetMask();
1751     if (GetThemeDrawable(idItem, len, outValue, iconType, density) == SUCCESS) {
1752         drawableInfo = std::make_tuple(type, len, themeMask);
1753         return SUCCESS;
1754     }
1755 
1756     RState state = hapManager_->GetMediaData(qd, len, outValue);
1757     drawableInfo = std::make_tuple(type, len, themeMask);
1758     return state;
1759 }
1760 
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)1761 RState ResourceManagerImpl::GetDrawableInfoByName(const char *name,
1762     std::tuple<std::string, size_t, std::string> &drawableInfo,
1763     std::unique_ptr<uint8_t[]> &outValue, uint32_t iconType, uint32_t density)
1764 {
1765     if (!IsDensityValid(density)) {
1766         RESMGR_HILOGE(RESMGR_TAG, "density invalid");
1767         return ERROR_CODE_INVALID_INPUT_PARAMETER;
1768     }
1769     auto qd = hapManager_->FindQualifierValueByName(name, ResType::MEDIA, isOverrideResMgr_, density);
1770     if (qd == nullptr) {
1771         RESMGR_HILOGE(RESMGR_TAG, "GetDrawableInfoByName error name = %{public}s", name);
1772         return ERROR_CODE_RES_NAME_NOT_FOUND;
1773     }
1774     std::string type = GetSuffix(qd);
1775     if (type.empty()) {
1776         RESMGR_HILOGE(RESMGR_TAG, "failed to get resourceType");
1777         return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
1778     }
1779     size_t len = 0;
1780     // find in theme
1781     std::string themeMask = ThemePackManager::GetThemePackManager()->GetMask();
1782     const std::shared_ptr<IdItem> idItem = qd->GetIdItem();
1783     if (GetThemeDrawable(idItem, len, outValue, iconType, density) == SUCCESS) {
1784         drawableInfo = std::make_tuple(type, len, themeMask);
1785         return SUCCESS;
1786     }
1787 
1788     RState state = hapManager_->GetMediaData(qd, len, outValue);
1789     drawableInfo = std::make_tuple(type, len, themeMask);
1790     return state;
1791 }
1792 
GetStringFormatById(uint32_t id,std::string & outValue,std::vector<std::tuple<ResourceManager::NapiValueType,std::string>> & jsParams)1793 RState ResourceManagerImpl::GetStringFormatById(uint32_t id, std::string &outValue,
1794     std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams)
1795 {
1796     RState state = GetStringById(id, outValue);
1797     if (state != SUCCESS) {
1798         return state;
1799     }
1800     ResConfigImpl resConfig;
1801     GetResConfig(resConfig);
1802     if (!ReplacePlaceholderWithParams(outValue, resConfig, jsParams)) {
1803         return ERROR_CODE_RES_ID_FORMAT_ERROR;
1804     }
1805     return SUCCESS;
1806 }
1807 
GetStringFormatByName(const char * name,std::string & outValue,std::vector<std::tuple<ResourceManager::NapiValueType,std::string>> & jsParams)1808 RState ResourceManagerImpl::GetStringFormatByName(const char *name, std::string &outValue,
1809     std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams)
1810 {
1811     RState state = GetStringByName(name, outValue);
1812     if (state != SUCCESS) {
1813         return state;
1814     }
1815     ResConfigImpl resConfig;
1816     GetResConfig(resConfig);
1817     if (!ReplacePlaceholderWithParams(outValue, resConfig, jsParams)) {
1818         return ERROR_CODE_RES_NAME_FORMAT_ERROR;
1819     }
1820     return SUCCESS;
1821 }
1822 
GetFormatPluralStringById(std::string & outValue,uint32_t id,int quantity,std::vector<std::tuple<ResourceManager::NapiValueType,std::string>> & jsParams)1823 RState ResourceManagerImpl::GetFormatPluralStringById(std::string &outValue, uint32_t id, int quantity,
1824     std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams)
1825 {
1826     Quantity intqQantity = { true, quantity, 0.0 };
1827     return GetFormatPluralStringById(outValue, id, intqQantity, jsParams);
1828 }
1829 
GetFormatPluralStringByName(std::string & outValue,const char * name,int quantity,std::vector<std::tuple<ResourceManager::NapiValueType,std::string>> & jsParams)1830 RState ResourceManagerImpl::GetFormatPluralStringByName(std::string &outValue, const char *name, int quantity,
1831     std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams)
1832 {
1833     Quantity intQuantity = { true, quantity, 0.0 };
1834     return GetFormatPluralStringByName(outValue, name, intQuantity, jsParams);
1835 }
1836 
GetFormatPluralStringById(std::string & outValue,uint32_t id,Quantity quantity,va_list args)1837 RState ResourceManagerImpl::GetFormatPluralStringById(std::string &outValue, uint32_t id, Quantity quantity,
1838     va_list args)
1839 {
1840     const std::shared_ptr<HapResource::ValueUnderQualifierDir> vuqd = hapManager_->FindQualifierValueById(id,
1841         isOverrideResMgr_);
1842     if (vuqd == nullptr) {
1843         RESMGR_HILOGE(RESMGR_TAG, "GetFormatPluralStringById error id = %{public}d", id);
1844         return ERROR_CODE_RES_ID_NOT_FOUND;
1845     }
1846     RState rState = GetPluralString(vuqd, quantity, outValue);
1847     if (rState == ERROR_CODE_RES_REF_TOO_MUCH) {
1848         RESMGR_HILOGE(RESMGR_TAG, "find too much ref by plural id = %{public}d", id);
1849         return rState;
1850     }
1851     if (rState != SUCCESS) {
1852         RESMGR_HILOGE(RESMGR_TAG, "plural res not found, id = %{public}d", id);
1853         return ERROR_CODE_RES_NOT_FOUND_BY_ID;
1854     }
1855 
1856     std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> params;
1857     if (parseArgs(outValue, args, params)) {
1858         ResConfigImpl resConfig;
1859         GetResConfig(resConfig);
1860         if (!ReplacePlaceholderWithParams(outValue, resConfig, params)) {
1861             RESMGR_HILOGE(RESMGR_TAG, "format plural string error, id = %{public}d", id);
1862             return ERROR_CODE_RES_ID_FORMAT_ERROR;
1863         }
1864         return SUCCESS;
1865     }
1866     return ERROR_CODE_INVALID_INPUT_PARAMETER;
1867 }
1868 
GetFormatPluralStringByName(std::string & outValue,const char * name,Quantity quantity,va_list args)1869 RState ResourceManagerImpl::GetFormatPluralStringByName(std::string &outValue, const char *name, Quantity quantity,
1870     va_list args)
1871 {
1872     const std::shared_ptr<HapResource::ValueUnderQualifierDir> vuqd =
1873         hapManager_->FindQualifierValueByName(name, ResType::PLURALS, isOverrideResMgr_);
1874     if (vuqd == nullptr) {
1875         RESMGR_HILOGE(RESMGR_TAG, "GetFormatPluralStringByName error name = %{public}s", name);
1876         return ERROR_CODE_RES_NAME_NOT_FOUND;
1877     }
1878     RState rState = GetPluralString(vuqd, quantity, outValue);
1879     if (rState == ERROR_CODE_RES_REF_TOO_MUCH) {
1880         RESMGR_HILOGE(RESMGR_TAG, "find too much ref by plural name = %{public}s", name);
1881         return rState;
1882     }
1883     if (rState != SUCCESS) {
1884         RESMGR_HILOGE(RESMGR_TAG, "plural res not found, name = %{public}s", name);
1885         return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
1886     }
1887     std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> params;
1888     if (parseArgs(outValue, args, params)) {
1889         ResConfigImpl resConfig;
1890         GetResConfig(resConfig);
1891         if (!ReplacePlaceholderWithParams(outValue, resConfig, params)) {
1892             RESMGR_HILOGE(RESMGR_TAG, "format plural string error, name = %{public}s", name);
1893             return ERROR_CODE_RES_NAME_FORMAT_ERROR;
1894         }
1895         return SUCCESS;
1896     }
1897     return ERROR_CODE_INVALID_INPUT_PARAMETER;
1898 }
1899 
GetFormatPluralStringById(std::string & outValue,uint32_t id,Quantity quantity,std::vector<std::tuple<ResourceManager::NapiValueType,std::string>> & jsParams)1900 RState ResourceManagerImpl::GetFormatPluralStringById(std::string &outValue, uint32_t id, Quantity quantity,
1901     std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams)
1902 {
1903     const std::shared_ptr<HapResource::ValueUnderQualifierDir> vuqd = hapManager_->FindQualifierValueById(id,
1904         isOverrideResMgr_);
1905     if (vuqd == nullptr) {
1906         RESMGR_HILOGE(RESMGR_TAG, "GetFormatPluralStringById error id = %{public}d", id);
1907         return ERROR_CODE_RES_ID_NOT_FOUND;
1908     }
1909     RState rState = GetPluralString(vuqd, quantity, outValue);
1910     if (rState == ERROR_CODE_RES_REF_TOO_MUCH) {
1911         RESMGR_HILOGE(RESMGR_TAG, "find too much ref by plural id = %{public}d", id);
1912         return rState;
1913     }
1914     if (rState != SUCCESS) {
1915         RESMGR_HILOGE(RESMGR_TAG, "plural res not found, id = %{public}d", id);
1916         return ERROR_CODE_RES_NOT_FOUND_BY_ID;
1917     }
1918     ResConfigImpl resConfig;
1919     GetResConfig(resConfig);
1920     if (!ReplacePlaceholderWithParams(outValue, resConfig, jsParams)) {
1921         RESMGR_HILOGE(RESMGR_TAG, "format plural string error, id = %{public}d", id);
1922         return ERROR_CODE_RES_ID_FORMAT_ERROR;
1923     }
1924     return SUCCESS;
1925 }
1926 
GetFormatPluralStringByName(std::string & outValue,const char * name,Quantity quantity,std::vector<std::tuple<ResourceManager::NapiValueType,std::string>> & jsParams)1927 RState ResourceManagerImpl::GetFormatPluralStringByName(std::string &outValue, const char *name, Quantity quantity,
1928     std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams)
1929 {
1930     const std::shared_ptr<HapResource::ValueUnderQualifierDir> vuqd =
1931         hapManager_->FindQualifierValueByName(name, ResType::PLURALS, isOverrideResMgr_);
1932     if (vuqd == nullptr) {
1933         RESMGR_HILOGE(RESMGR_TAG, "GetFormatPluralStringByName error name = %{public}s", name);
1934         return ERROR_CODE_RES_NAME_NOT_FOUND;
1935     }
1936     RState rState = GetPluralString(vuqd, quantity, outValue);
1937     if (rState == ERROR_CODE_RES_REF_TOO_MUCH) {
1938         RESMGR_HILOGE(RESMGR_TAG, "find too much ref by plural name = %{public}s", name);
1939         return rState;
1940     }
1941     if (rState != SUCCESS) {
1942         RESMGR_HILOGE(RESMGR_TAG, "plural res not found, name = %{public}s", name);
1943         return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
1944     }
1945     ResConfigImpl resConfig;
1946     GetResConfig(resConfig);
1947     if (!ReplacePlaceholderWithParams(outValue, resConfig, jsParams)) {
1948         RESMGR_HILOGE(RESMGR_TAG, "format plural string error, name = %{public}s", name);
1949         return ERROR_CODE_RES_NAME_FORMAT_ERROR;
1950     }
1951     return SUCCESS;
1952 }
1953 
GetResourceLimitKeys()1954 uint32_t ResourceManagerImpl::GetResourceLimitKeys()
1955 {
1956     if (hapManager_ == nullptr) {
1957         RESMGR_HILOGE(RESMGR_TAG, "resource manager get limit keys failed, hapManager_ is nullptr");
1958         return 0;
1959     }
1960     return hapManager_->GetResourceLimitKeys();
1961 }
1962 
GetRawFdNdkFromHap(const std::string & name,RawFileDescriptor & descriptor)1963 RState ResourceManagerImpl::GetRawFdNdkFromHap(const std::string &name, RawFileDescriptor &descriptor)
1964 {
1965     return hapManager_->GetRawFd(name, descriptor);
1966 }
1967 
GetResId(const std::string & resTypeName,uint32_t & resId)1968 RState ResourceManagerImpl::GetResId(const std::string &resTypeName, uint32_t &resId)
1969 {
1970     return hapManager_->GetResId(resTypeName, resId);
1971 }
1972 
GetLocales(std::vector<std::string> & outValue,bool includeSystem)1973 void ResourceManagerImpl::GetLocales(std::vector<std::string> &outValue, bool includeSystem)
1974 {
1975     hapManager_->GetLocales(outValue, includeSystem);
1976 }
1977 
GetThemeIconInfo(const std::string & iconName,size_t & len,std::unique_ptr<uint8_t[]> & outValue,const std::string & abilityName)1978 RState ResourceManagerImpl::GetThemeIconInfo(const std::string &iconName, size_t &len,
1979     std::unique_ptr<uint8_t[]> &outValue, const std::string &abilityName)
1980 {
1981     std::string result = ThemePackManager::GetThemePackManager()->FindThemeIconResource(
1982         bundleInfo, iconName, userId, abilityName);
1983     if (result.empty()) {
1984         RESMGR_HILOGD(RESMGR_TAG, "GetThemeIconInfo FAILED bundlename = %{public}s,", result.c_str());
1985         return ERROR_CODE_RES_ID_NOT_FOUND;
1986     }
1987     outValue = Utils::LoadResourceFile(result, len);
1988     if (outValue == nullptr) {
1989         RESMGR_HILOGD(RESMGR_TAG, "LoadResourceFile FAILED");
1990         return ERROR_CODE_RES_ID_NOT_FOUND;
1991     }
1992     return SUCCESS;
1993 }
1994 
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)1995 RState ResourceManagerImpl::GetThemeIcons(uint32_t resId, std::pair<std::unique_ptr<uint8_t[]>, size_t>
1996     &foregroundInfo, std::pair<std::unique_ptr<uint8_t[]>, size_t> &backgroundInfo, uint32_t density,
1997     const std::string &abilityName)
1998 {
1999     RState foreState = GetThemeIconInfo(FOREGROUND, foregroundInfo.second, foregroundInfo.first, abilityName);
2000     RState backState = GetThemeIconInfo(BACKGROUND, backgroundInfo.second, backgroundInfo.first, abilityName);
2001     if (foreState == SUCCESS && backState == SUCCESS) {
2002         RESMGR_HILOGW(RESMGR_TAG,
2003             "GetThemeIcons bundleName = %{public}s, abilityName = %{public}s, fLen = %{public}zu, bLen = %{public}zu",
2004             bundleInfo.first.c_str(), abilityName.c_str(), foregroundInfo.second, backgroundInfo.second);
2005         return SUCCESS;
2006     }
2007     return ERROR_CODE_RES_ID_NOT_FOUND;
2008 }
2009 
GetDynamicIcon(const std::string & resName,std::pair<std::unique_ptr<uint8_t[]>,size_t> & iconInfo,uint32_t density)2010 RState ResourceManagerImpl::GetDynamicIcon(const std::string &resName,
2011     std::pair<std::unique_ptr<uint8_t[]>, size_t> &iconInfo, uint32_t density)
2012 {
2013     return GetThemeIconInfo(resName, iconInfo.second, iconInfo.first);
2014 }
2015 
GetThemeMask()2016 std::string ResourceManagerImpl::GetThemeMask()
2017 {
2018     return ThemePackManager::GetThemePackManager()->GetMask();
2019 }
2020 
HasIconInTheme(const std::string & bundleName)2021 bool ResourceManagerImpl::HasIconInTheme(const std::string &bundleName)
2022 {
2023     return ThemePackManager::GetThemePackManager()->HasIconInTheme(bundleName, userId);
2024 }
2025 
GetOtherIconsInfo(const std::string & iconName,std::unique_ptr<uint8_t[]> & outValue,size_t & len,bool isGlobalMask)2026 RState ResourceManagerImpl::GetOtherIconsInfo(const std::string &iconName,
2027     std::unique_ptr<uint8_t[]> &outValue, size_t &len, bool isGlobalMask)
2028 {
2029     std::string iconTag;
2030     if (iconName.find("icon_mask") != std::string::npos && isGlobalMask) {
2031         iconTag = "global_" + iconName;
2032     } else {
2033         iconTag = "other_icons_" + iconName;
2034     }
2035     RState result = ThemePackManager::GetThemePackManager()->GetThemeIconFromCache(iconTag, outValue, len);
2036     if (result == SUCCESS) {
2037         return SUCCESS;
2038     }
2039     return ThemePackManager::GetThemePackManager()->GetOtherIconsInfo(iconName, outValue, len, isGlobalMask, userId);
2040 }
2041 
IsRawDirFromHap(const std::string & pathName,bool & outValue)2042 RState ResourceManagerImpl::IsRawDirFromHap(const std::string &pathName, bool &outValue)
2043 {
2044     return hapManager_->IsRawDirFromHap(pathName, outValue);
2045 }
2046 
GetHapManager()2047 std::shared_ptr<HapManager> ResourceManagerImpl::GetHapManager()
2048 {
2049     return hapManager_;
2050 }
2051 
GetOverrideResourceManager(std::shared_ptr<ResConfig> overrideResConfig)2052 std::shared_ptr<ResourceManager> ResourceManagerImpl::GetOverrideResourceManager(
2053     std::shared_ptr<ResConfig> overrideResConfig)
2054 {
2055     ResourceManagerImpl *impl = new (std::nothrow) ResourceManagerImpl(true);
2056     if (impl == nullptr) {
2057         RESMGR_HILOGE(RESMGR_TAG, "new ResourceManagerImpl failed when GetOverrideResourceManager");
2058         return nullptr;
2059     }
2060 
2061     if (!impl->Init(this->GetHapManager())) {
2062         delete (impl);
2063         return nullptr;
2064     }
2065 
2066     std::shared_ptr<ResourceManager> overrideResMgr(impl);
2067     if (overrideResMgr == nullptr) {
2068         RESMGR_HILOGE(RESMGR_TAG, "GetOverrideResourceManager failed bundleName = %{public}s, moduleName = %{public}s",
2069             this->bundleInfo.first.c_str(), this->bundleInfo.second.c_str());
2070         return nullptr;
2071     }
2072 
2073     overrideResMgr->bundleInfo.first = this->bundleInfo.first;
2074     overrideResMgr->bundleInfo.second = this->bundleInfo.second;
2075     if (overrideResConfig && overrideResMgr->UpdateOverrideResConfig(*overrideResConfig) != SUCCESS) {
2076         RESMGR_HILOGE(RESMGR_TAG, "GetOverrideResourceManager UpdateOverrideResConfig failed bundleName = %{public}s, \
2077             moduleName = %{public}s", this->bundleInfo.first.c_str(), this->bundleInfo.second.c_str());
2078         return nullptr;
2079     }
2080 
2081     return overrideResMgr;
2082 }
2083 } // namespace Resource
2084 } // namespace Global
2085 } // namespace OHOS
2086