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