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