• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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 "adapter/ohos/osal/resource_adapter_impl_v2.h"
17 
18 #include <dirent.h>
19 
20 #include "drawable_descriptor.h"
21 #include "resource_adapter_impl_v2.h"
22 
23 #include "adapter/ohos/entrance/ace_container.h"
24 #include "adapter/ohos/osal/resource_convertor.h"
25 #include "core/common/resource/resource_manager.h"
26 #include "core/pipeline_ng/pipeline_context.h"
27 
28 namespace OHOS::Ace {
29 namespace {
30 constexpr uint32_t OHOS_THEME_ID = 125829872; // ohos_theme
31 const Color ERROR_VALUE_COLOR = Color(0xff000000);
32 
CheckThemeId(int32_t & themeId)33 void CheckThemeId(int32_t& themeId)
34 {
35     if (themeId >= 0) {
36         return;
37     }
38     themeId = OHOS_THEME_ID;
39 }
40 
41 const char* PATTERN_MAP[] = {
42     THEME_PATTERN_BUTTON,
43     THEME_PATTERN_CAMERA,
44     THEME_PATTERN_LIST_ITEM,
45     THEME_PATTERN_ARC_LIST,
46     THEME_PATTERN_ARC_LIST_ITEM,
47     THEME_PATTERN_PICKER,
48     THEME_PATTERN_PROGRESS,
49     THEME_PATTERN_SELECT,
50     THEME_PATTERN_STEPPER,
51     THEME_PATTERN_TEXT,
52     THEME_PATTERN_TEXTFIELD,
53     THEME_PATTERN_TEXT_OVERLAY,
54     THEME_PATTERN_CONTAINER_MODAL
55 };
56 
57 // PRELOAD_LIST contain themes that should be preloaded asynchronously
58 const char* PRELOAD_LIST[] = {
59     THEME_BLUR_STYLE_COMMON,
60     THEME_PATTERN_ICON,
61     THEME_PATTERN_SHADOW
62 };
63 
64 constexpr char RESOURCE_TOKEN_PATTERN[] = "\\[.+?\\]\\.(\\S+?\\.\\S+)";
65 
IsDirExist(const std::string & path)66 bool IsDirExist(const std::string& path)
67 {
68     char realPath[PATH_MAX] = { 0x00 };
69     CHECK_NULL_RETURN(realpath(path.c_str(), realPath), false);
70     DIR* dir = opendir(realPath);
71     CHECK_NULL_RETURN(dir, false);
72     closedir(dir);
73     return true;
74 }
75 
ParseDimensionUnit(const std::string & unit)76 DimensionUnit ParseDimensionUnit(const std::string& unit)
77 {
78     if (unit == "px") {
79         return DimensionUnit::PX;
80     } else if (unit == "fp") {
81         return DimensionUnit::FP;
82     } else if (unit == "lpx") {
83         return DimensionUnit::LPX;
84     } else if (unit == "%") {
85         return DimensionUnit::PERCENT;
86     } else {
87         return DimensionUnit::VP;
88     }
89 };
90 } // namespace
91 
CreateV2()92 RefPtr<ResourceAdapter> ResourceAdapter::CreateV2()
93 {
94     return AceType::MakeRefPtr<ResourceAdapterImplV2>();
95 }
96 
CreateNewResourceAdapter(const std::string & bundleName,const std::string & moduleName)97 RefPtr<ResourceAdapter> ResourceAdapter::CreateNewResourceAdapter(
98     const std::string& bundleName, const std::string& moduleName)
99 {
100     auto container = Container::CurrentSafely();
101     CHECK_NULL_RETURN(container, nullptr);
102     auto aceContainer = AceType::DynamicCast<Platform::AceContainer>(container);
103     CHECK_NULL_RETURN(aceContainer, nullptr);
104 
105     RefPtr<ResourceAdapter> newResourceAdapter = nullptr;
106     auto context = aceContainer->GetAbilityContextByModule(bundleName, moduleName);
107     if (context) {
108         auto resourceManager = context->GetResourceManager();
109         auto resourceAdapterV2 = AceType::MakeRefPtr<ResourceAdapterImplV2>(resourceManager);
110         resourceAdapterV2->SetAppHasDarkRes(aceContainer->GetResourceConfiguration().GetAppHasDarkRes());
111         newResourceAdapter = resourceAdapterV2;
112     } else {
113         newResourceAdapter = ResourceAdapter::CreateV2();
114         auto resourceInfo = aceContainer->GetResourceInfo();
115         newResourceAdapter->Init(resourceInfo);
116     }
117 
118     auto resConfig = aceContainer->GetResourceConfiguration();
119     auto pipelineContext = NG::PipelineContext::GetCurrentContext();
120     if (pipelineContext && pipelineContext->GetLocalColorMode() != ColorMode::COLOR_MODE_UNDEFINED) {
121         auto localColorMode = pipelineContext->GetLocalColorMode();
122         resConfig.SetColorMode(localColorMode);
123     }
124     newResourceAdapter->UpdateConfig(resConfig);
125 
126     return newResourceAdapter;
127 }
128 
ResourceAdapterImplV2(std::shared_ptr<Global::Resource::ResourceManager> resourceManager)129 ResourceAdapterImplV2::ResourceAdapterImplV2(std::shared_ptr<Global::Resource::ResourceManager> resourceManager)
130 {
131     sysResourceManager_ = resourceManager;
132 }
133 
ResourceAdapterImplV2(std::shared_ptr<Global::Resource::ResourceManager> resourceManager,int32_t instanceId)134 ResourceAdapterImplV2::ResourceAdapterImplV2(
135     std::shared_ptr<Global::Resource::ResourceManager> resourceManager, int32_t instanceId)
136 {
137     sysResourceManager_ = resourceManager;
138     std::shared_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
139     resourceManager->GetResConfig(*resConfig);
140     resConfig_ = resConfig;
141     appHasDarkRes_ = resConfig->GetAppDarkRes();
142     auto container = Platform::AceContainer::GetContainer(instanceId);
143     if (container) {
144         std::string hapPath = container->GetHapPath();
145         std::string resPath = container->GetPackagePathStr();
146         packagePathStr_ = (hapPath.empty() || IsDirExist(resPath)) ? resPath : std::string();
147     }
148 }
149 
ResourceAdapterImplV2(std::shared_ptr<Global::Resource::ResourceManager> resourceManager,const ResourceInfo & resourceInfo)150 ResourceAdapterImplV2::ResourceAdapterImplV2(
151     std::shared_ptr<Global::Resource::ResourceManager> resourceManager, const ResourceInfo& resourceInfo)
152 {
153     std::string resPath = resourceInfo.GetPackagePath();
154     std::string hapPath = resourceInfo.GetHapPath();
155     std::string resIndexPath = hapPath.empty() ? (resPath + "resources.index") : hapPath;
156     packagePathStr_ = (hapPath.empty() || IsDirExist(resPath)) ? resPath : std::string();
157 
158     auto resConfig = ConvertConfigToGlobal(resourceInfo.GetResourceConfiguration());
159     sysResourceManager_ = resourceManager;
160     if (resConfig != nullptr) {
161         sysResourceManager_->UpdateResConfig(*resConfig);
162     }
163     resConfig_ = resConfig;
164     appHasDarkRes_ = resourceInfo.GetResourceConfiguration().GetAppHasDarkRes();
165 }
166 
Init(const ResourceInfo & resourceInfo)167 void ResourceAdapterImplV2::Init(const ResourceInfo& resourceInfo)
168 {
169     std::string resPath = resourceInfo.GetPackagePath();
170     std::string hapPath = resourceInfo.GetHapPath();
171     auto resConfig = ConvertConfigToGlobal(resourceInfo.GetResourceConfiguration());
172     std::shared_ptr<Global::Resource::ResourceManager> newResMgr(Global::Resource::CreateResourceManager());
173     std::string resIndexPath = hapPath.empty() ? (resPath + "resources.index") : hapPath;
174     newResMgr->AddResource(resIndexPath.c_str());
175     if (resConfig != nullptr) {
176         newResMgr->UpdateResConfig(*resConfig);
177     }
178     sysResourceManager_ = newResMgr;
179     packagePathStr_ = (hapPath.empty() || IsDirExist(resPath)) ? resPath : std::string();
180     resConfig_ = resConfig;
181     appHasDarkRes_ = resourceInfo.GetResourceConfiguration().GetAppHasDarkRes();
182 }
183 
LocaleDiff(const std::shared_ptr<Global::Resource::ResConfig> & oldResConfig,const std::shared_ptr<Global::Resource::ResConfig> & newResConfig)184 bool LocaleDiff(const std::shared_ptr<Global::Resource::ResConfig>& oldResConfig,
185     const std::shared_ptr<Global::Resource::ResConfig>& newResConfig)
186 {
187     auto oldLocaleInfo = oldResConfig->GetLocaleInfo();
188     auto newLocaleInfo = newResConfig->GetLocaleInfo();
189     if ((!oldLocaleInfo && newLocaleInfo) || (oldLocaleInfo && !newLocaleInfo)) {
190         return true;
191     }
192     if (oldLocaleInfo && newLocaleInfo) {
193         if (!StringUtils::CStringEqual(oldLocaleInfo->getLanguage(), newLocaleInfo->getLanguage()) ||
194             !StringUtils::CStringEqual(oldLocaleInfo->getScript(), newLocaleInfo->getScript()) ||
195             !StringUtils::CStringEqual(oldLocaleInfo->getCountry(), newLocaleInfo->getCountry())) {
196             return true;
197         }
198     }
199     auto oldPreferredLocaleInfo = oldResConfig->GetPreferredLocaleInfo();
200     auto newPreferredLocaleInfo = newResConfig->GetPreferredLocaleInfo();
201     if ((!oldPreferredLocaleInfo && newPreferredLocaleInfo) || (oldPreferredLocaleInfo && !newPreferredLocaleInfo)) {
202         return true;
203     }
204     if (oldPreferredLocaleInfo && newPreferredLocaleInfo) {
205         if (!StringUtils::CStringEqual(oldPreferredLocaleInfo->getLanguage(), newPreferredLocaleInfo->getLanguage()) ||
206             !StringUtils::CStringEqual(oldPreferredLocaleInfo->getScript(), newPreferredLocaleInfo->getScript()) ||
207             !StringUtils::CStringEqual(oldPreferredLocaleInfo->getCountry(), newPreferredLocaleInfo->getCountry())) {
208             return true;
209         }
210     }
211     return false;
212 }
213 
NeedUpdateResConfig(const std::shared_ptr<Global::Resource::ResConfig> & oldResConfig,const std::shared_ptr<Global::Resource::ResConfig> & newResConfig)214 bool ResourceAdapterImplV2::NeedUpdateResConfig(const std::shared_ptr<Global::Resource::ResConfig>& oldResConfig,
215     const std::shared_ptr<Global::Resource::ResConfig>& newResConfig)
216 {
217     CHECK_NULL_RETURN(oldResConfig, true);
218 
219     return LocaleDiff(oldResConfig, newResConfig) || oldResConfig->GetDeviceType() != newResConfig->GetDeviceType() ||
220            oldResConfig->GetDirection() != newResConfig->GetDirection() ||
221            oldResConfig->GetScreenDensity() != newResConfig->GetScreenDensity() ||
222            oldResConfig->GetColorMode() != newResConfig->GetColorMode() ||
223            oldResConfig->GetInputDevice() != newResConfig->GetInputDevice();
224 }
225 
UpdateConfig(const ResourceConfiguration & config,bool themeFlag)226 void ResourceAdapterImplV2::UpdateConfig(const ResourceConfiguration& config, bool themeFlag)
227 {
228     std::lock_guard<std::mutex> lock(updateResConfigMutex_);
229     auto resConfig = ConvertConfigToGlobal(config);
230     auto needUpdateResConfig = NeedUpdateResConfig(resConfig_, resConfig) || themeFlag;
231     if (sysResourceManager_ && resConfig != nullptr && needUpdateResConfig) {
232         sysResourceManager_->UpdateResConfig(*resConfig, themeFlag);
233     }
234     resConfig_ = resConfig;
235 }
236 
GetTheme(int32_t themeId)237 RefPtr<ThemeStyle> ResourceAdapterImplV2::GetTheme(int32_t themeId)
238 {
239     CheckThemeId(themeId);
240     auto theme = AceType::MakeRefPtr<ResourceThemeStyle>(AceType::Claim(this));
241 
242     constexpr char flag[] = "ohos_"; // fit with resource/base/theme.json and pattern.json
243     {
244         auto manager = GetResourceManager();
245         if (manager) {
246             auto ret = manager->GetThemeById(themeId, theme->rawAttrs_);
247             for (size_t i = 0; i < sizeof(PATTERN_MAP) / sizeof(PATTERN_MAP[0]); i++) {
248                 ResourceThemeStyle::RawAttrMap attrMap;
249                 std::string patternTag = PATTERN_MAP[i];
250                 std::string patternName = std::string(flag) + PATTERN_MAP[i];
251                 ret = manager->GetPatternByName(patternName.c_str(), attrMap);
252                 if (attrMap.empty()) {
253                     continue;
254                 }
255                 theme->patternAttrs_[patternTag] = attrMap;
256             }
257         }
258     }
259 
260     if (theme->patternAttrs_.empty() && theme->rawAttrs_.empty()) {
261         return nullptr;
262     }
263 
264     theme->ParseContent();
265     theme->patternAttrs_.clear();
266 
267     PreloadTheme(themeId, theme);
268     return theme;
269 }
270 
PreloadTheme(int32_t themeId,RefPtr<ResourceThemeStyle> theme)271 void ResourceAdapterImplV2::PreloadTheme(int32_t themeId, RefPtr<ResourceThemeStyle> theme)
272 {
273     auto container = Container::CurrentSafely();
274     CHECK_NULL_VOID(container);
275     auto manager = GetResourceManager();
276     CHECK_NULL_VOID(manager);
277     auto taskExecutor = GetTaskExecutor();
278     CHECK_NULL_VOID(taskExecutor);
279 
280     // post an asynchronous task to preload themes in PRELOAD_LIST
281     auto task = [manager, resourceThemeStyle = WeakPtr<ResourceThemeStyle>(theme),
282         weak = WeakClaim(this)]() -> void {
283         auto themeStyle = resourceThemeStyle.Upgrade();
284         CHECK_NULL_VOID(themeStyle);
285         auto adapter = weak.Upgrade();
286         CHECK_NULL_VOID(adapter);
287         for (size_t i = 0; i < sizeof(PRELOAD_LIST) / sizeof(PRELOAD_LIST[0]); ++i) {
288             std::string patternName = PRELOAD_LIST[i];
289             themeStyle->PushBackCheckThemeStyleVector(patternName);
290             auto style = adapter->GetPatternByName(patternName);
291             if (style) {
292                 ResValueWrapper value = { .type = ThemeConstantsType::PATTERN, .value = style };
293                 themeStyle->SetAttr(patternName, value);
294             }
295         }
296 
297         themeStyle->SetPromiseValue();
298     };
299 
300     // isolation of loading card themes
301     if (!container->IsFormRender()) {
302         taskExecutor->PostTask(task, TaskExecutor::TaskType::BACKGROUND, "ArkUILoadTheme");
303     }
304 }
305 
GetTaskExecutor()306 RefPtr<TaskExecutor> ResourceAdapterImplV2::GetTaskExecutor()
307 {
308     auto context = NG::PipelineContext::GetCurrentContextSafely();
309     CHECK_NULL_RETURN(context, nullptr);
310     return context->GetTaskExecutor();
311 }
312 
GetPatternByName(const std::string & patternName)313 RefPtr<ThemeStyle> ResourceAdapterImplV2::GetPatternByName(const std::string& patternName)
314 {
315     auto patternStyle = AceType::MakeRefPtr<ResourceThemeStyle>(AceType::Claim(this));
316     patternStyle->SetName(patternName);
317     constexpr char flag[] = "ohos_"; // fit with resource/base/theme.json and pattern.json
318     auto manager = GetResourceManager();
319     if (manager) {
320         ResourceThemeStyle::RawAttrMap attrMap;
321         std::string patternTag = std::string(flag) + patternName;
322         auto state = manager->GetPatternByName(patternTag.c_str(), attrMap);
323         if (state != Global::Resource::SUCCESS) {
324             TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get pattern by name error, name=%{public}s", patternTag.c_str());
325             state = manager->GetPatternByName(patternName.c_str(), attrMap);
326             if (state != Global::Resource::SUCCESS) {
327                 TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get pattern by name error, name=%{public}s", patternName.c_str());
328                 auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
329                 ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
330                     patternName, "Pattern", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
331             } else if (attrMap.empty()) {
332                 TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get pattern %{public}s empty!", patternName.c_str());
333             }
334         } else if (attrMap.empty()) {
335             TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get pattern %{public}s empty!", patternTag.c_str());
336         }
337         patternStyle->rawAttrs_ = attrMap;
338         patternStyle->ParseContent();
339     }
340     return patternStyle;
341 }
342 
GetColor(uint32_t resId)343 Color ResourceAdapterImplV2::GetColor(uint32_t resId)
344 {
345     uint32_t result = 0;
346     auto manager = GetResourceManager();
347     CHECK_NULL_RETURN(manager, Color(result));
348     auto state = manager->GetColorById(resId, result);
349     if (state != Global::Resource::SUCCESS) {
350         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get color by id error, id=%{public}u", resId);
351         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
352         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
353             std::to_string(resId), "Color", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
354         return ERROR_VALUE_COLOR;
355     }
356     return Color(result);
357 }
358 
GetColorByName(const std::string & resName)359 Color ResourceAdapterImplV2::GetColorByName(const std::string& resName)
360 {
361     uint32_t result = 0;
362     auto actualResName = GetActualResourceName(resName);
363     auto manager = GetResourceManager();
364     CHECK_NULL_RETURN(manager, Color(result));
365     auto state = manager->GetColorByName(actualResName.c_str(), result);
366     if (state != Global::Resource::SUCCESS) {
367         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get color by name error, name=%{public}s, errorCode=%{public}d",
368             resName.c_str(), state);
369         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
370         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
371             resName, "Color", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
372     }
373     return Color(result);
374 }
375 
GetDimension(uint32_t resId)376 Dimension ResourceAdapterImplV2::GetDimension(uint32_t resId)
377 {
378     float dimensionFloat = 0.0f;
379 #ifdef NG_BUILD
380     std::string unit;
381     auto manager = GetResourceManager();
382     if (manager) {
383         auto state = manager->GetFloatById(resId, dimensionFloat, unit);
384         if (state != Global::Resource::SUCCESS) {
385             TAG_LOGW(AceLogTag::ACE_RESOURCE, "NG Get dimension by id error, id=%{public}u, errorCode=%{public}d",
386                 resId, state);
387             auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
388             ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
389                 std::to_string(resId), "Dimension", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
390         }
391     }
392     return Dimension(static_cast<double>(dimensionFloat), ParseDimensionUnit(unit));
393 #else
394     if (Container::IsCurrentUseNewPipeline()) {
395         std::string unit;
396         auto manager = GetResourceManager();
397         if (manager) {
398             auto state = manager->GetFloatById(resId, dimensionFloat, unit);
399             if (state != Global::Resource::SUCCESS) {
400                 TAG_LOGW(AceLogTag::ACE_RESOURCE, "NG: Get dimension by id error, id=%{public}u, errorCode=%{public}d",
401                     resId, state);
402                 auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
403                 ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
404                     std::to_string(resId), "Dimension", host ? host->GetTag().c_str() : "",
405                     GetCurrentTimestamp(), state));
406             }
407         }
408         return Dimension(static_cast<double>(dimensionFloat), ParseDimensionUnit(unit));
409     }
410 
411     auto manager = GetResourceManager();
412     CHECK_NULL_RETURN(manager, Dimension(static_cast<double>(dimensionFloat)));
413     auto state = manager->GetFloatById(resId, dimensionFloat);
414     if (state != Global::Resource::SUCCESS) {
415         TAG_LOGW(
416             AceLogTag::ACE_RESOURCE, "Get dimension by id error, id=%{public}u, errorCode=%{public}d", resId, state);
417         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
418         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
419             std::to_string(resId), "Dimension", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
420     }
421     return Dimension(static_cast<double>(dimensionFloat));
422 #endif
423 }
424 
GetDimensionByName(const std::string & resName)425 Dimension ResourceAdapterImplV2::GetDimensionByName(const std::string& resName)
426 {
427     float dimensionFloat = 0.0f;
428     auto actualResName = GetActualResourceName(resName);
429     auto manager = GetResourceManager();
430     CHECK_NULL_RETURN(manager, Dimension());
431     std::string unit;
432     auto state = manager->GetFloatByName(actualResName.c_str(), dimensionFloat, unit);
433     if (state != Global::Resource::SUCCESS) {
434         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get dimension by name error, resName=%{public}s, errorCode=%{public}d",
435             resName.c_str(), state);
436         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
437         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
438             resName, "Dimension", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
439     }
440     return Dimension(static_cast<double>(dimensionFloat), ParseDimensionUnit(unit));
441 }
442 
GetString(uint32_t resId)443 std::string ResourceAdapterImplV2::GetString(uint32_t resId)
444 {
445     std::string strResult = "";
446     auto manager = GetResourceManager();
447     CHECK_NULL_RETURN(manager, strResult);
448     auto state = manager->GetStringById(resId, strResult);
449     if (state != Global::Resource::SUCCESS) {
450         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get string by id error, id=%{public}u, errorCode=%{public}d", resId, state);
451         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
452         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
453             std::to_string(resId), "String", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
454     }
455     return strResult;
456 }
457 
GetStringByName(const std::string & resName)458 std::string ResourceAdapterImplV2::GetStringByName(const std::string& resName)
459 {
460     std::string strResult = "";
461     auto actualResName = GetActualResourceName(resName);
462     auto manager = GetResourceManager();
463     CHECK_NULL_RETURN(manager, strResult);
464     auto state = manager->GetStringByName(actualResName.c_str(), strResult);
465     if (state != Global::Resource::SUCCESS) {
466         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get string by name error, resName=%{public}s, errorCode=%{public}d",
467             resName.c_str(), state);
468         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
469         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
470             resName, "String", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
471     }
472     return strResult;
473 }
474 
GetPluralString(uint32_t resId,int quantity)475 std::string ResourceAdapterImplV2::GetPluralString(uint32_t resId, int quantity)
476 {
477     std::string strResult = "";
478     auto manager = GetResourceManager();
479     CHECK_NULL_RETURN(manager, strResult);
480     auto state = manager->GetPluralStringById(resId, quantity, strResult);
481     if (state != Global::Resource::SUCCESS) {
482         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get plural string by id error, id=%{public}u, errorCode=%{public}d", resId,
483             state);
484         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
485         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
486             std::to_string(resId), "PluralString", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
487     }
488     return strResult;
489 }
490 
GetPluralStringByName(const std::string & resName,int quantity)491 std::string ResourceAdapterImplV2::GetPluralStringByName(const std::string& resName, int quantity)
492 {
493     std::string strResult = "";
494     auto actualResName = GetActualResourceName(resName);
495     auto manager = GetResourceManager();
496     CHECK_NULL_RETURN(manager, strResult);
497     auto state = manager->GetPluralStringByName(actualResName.c_str(), quantity, strResult);
498     if (state != Global::Resource::SUCCESS) {
499         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get plural string by name error, resName=%{public}s, errorCode=%{public}d",
500             resName.c_str(), state);
501         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
502         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
503             resName, "PluralString", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
504     }
505     return strResult;
506 }
507 
GetStringArray(uint32_t resId) const508 std::vector<std::string> ResourceAdapterImplV2::GetStringArray(uint32_t resId) const
509 {
510     std::vector<std::string> strResults;
511     auto manager = GetResourceManager();
512     CHECK_NULL_RETURN(manager, strResults);
513     auto state = manager->GetStringArrayById(resId, strResults);
514     if (state != Global::Resource::SUCCESS) {
515         TAG_LOGW(
516             AceLogTag::ACE_RESOURCE, "Get stringArray by id error, id=%{public}u, errorCode=%{public}d", resId, state);
517         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
518         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
519             std::to_string(resId), "StringArray", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
520     }
521     return strResults;
522 }
523 
GetStringArrayByName(const std::string & resName) const524 std::vector<std::string> ResourceAdapterImplV2::GetStringArrayByName(const std::string& resName) const
525 {
526     std::vector<std::string> strResults;
527     auto actualResName = GetActualResourceName(resName);
528     auto manager = GetResourceManager();
529     CHECK_NULL_RETURN(manager, strResults);
530     auto state = manager->GetStringArrayByName(actualResName.c_str(), strResults);
531     if (state != Global::Resource::SUCCESS) {
532         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get stringArray by name error, resName=%{public}s, errorCode=%{public}d",
533             resName.c_str(), state);
534         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
535         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
536             resName, "StringArray", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
537     }
538     return strResults;
539 }
540 
GetDouble(uint32_t resId)541 double ResourceAdapterImplV2::GetDouble(uint32_t resId)
542 {
543     float result = 0.0f;
544     auto manager = GetResourceManager();
545     CHECK_NULL_RETURN(manager, static_cast<double>(result));
546     auto state = manager->GetFloatById(resId, result);
547     if (state != Global::Resource::SUCCESS) {
548         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get double by id error, id=%{public}u, errorCode=%{public}d", resId, state);
549         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
550         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
551             std::to_string(resId), "Double", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
552     }
553     return static_cast<double>(result);
554 }
555 
GetDoubleByName(const std::string & resName)556 double ResourceAdapterImplV2::GetDoubleByName(const std::string& resName)
557 {
558     float result = 0.0f;
559     auto actualResName = GetActualResourceName(resName);
560     auto manager = GetResourceManager();
561     CHECK_NULL_RETURN(manager, static_cast<double>(result));
562     auto state = manager->GetFloatByName(actualResName.c_str(), result);
563     if (state != Global::Resource::SUCCESS) {
564         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get double by name error, resName=%{public}s, errorCode=%{public}d",
565             resName.c_str(), state);
566         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
567         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
568             resName, "Double", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
569     }
570     return static_cast<double>(result);
571 }
572 
GetInt(uint32_t resId)573 int32_t ResourceAdapterImplV2::GetInt(uint32_t resId)
574 {
575     int32_t result = 0;
576     auto manager = GetResourceManager();
577     CHECK_NULL_RETURN(manager, result);
578     auto state = manager->GetIntegerById(resId, result);
579     if (state != Global::Resource::SUCCESS) {
580         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get int by id error, id=%{public}u, errorCode=%{public}d", resId, state);
581         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
582         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
583             std::to_string(resId), "Int", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
584     }
585     return result;
586 }
587 
GetIntByName(const std::string & resName)588 int32_t ResourceAdapterImplV2::GetIntByName(const std::string& resName)
589 {
590     int32_t result = 0;
591     auto actualResName = GetActualResourceName(resName);
592     auto manager = GetResourceManager();
593     CHECK_NULL_RETURN(manager, result);
594     auto state = manager->GetIntegerByName(actualResName.c_str(), result);
595     if (state != Global::Resource::SUCCESS) {
596         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get int by name error, resName=%{public}s, errorCode=%{public}d",
597             resName.c_str(), state);
598         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
599         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
600             resName, "Int", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
601     }
602     return result;
603 }
604 
GetIntArray(uint32_t resId) const605 std::vector<uint32_t> ResourceAdapterImplV2::GetIntArray(uint32_t resId) const
606 {
607     std::vector<int> intVectorResult;
608     {
609         auto manager = GetResourceManager();
610         if (manager) {
611             auto state = manager->GetIntArrayById(resId, intVectorResult);
612             if (state != Global::Resource::SUCCESS) {
613                 TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get intArray by id error, id=%{public}u, errorCode=%{public}d",
614                     resId, state);
615                 auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
616                 ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
617                     std::to_string(resId), "IntArray", host ? host->GetTag().c_str() : "",
618                     GetCurrentTimestamp(), state));
619             }
620         }
621     }
622 
623     std::vector<uint32_t> result(intVectorResult.size());
624     std::transform(
625         intVectorResult.begin(), intVectorResult.end(), result.begin(), [](int x) { return static_cast<uint32_t>(x); });
626     return result;
627 }
628 
GetIntArrayByName(const std::string & resName) const629 std::vector<uint32_t> ResourceAdapterImplV2::GetIntArrayByName(const std::string& resName) const
630 {
631     std::vector<int> intVectorResult;
632     auto actualResName = GetActualResourceName(resName);
633     auto manager = GetResourceManager();
634     CHECK_NULL_RETURN(manager, {});
635     auto state = manager->GetIntArrayByName(actualResName.c_str(), intVectorResult);
636     if (state != Global::Resource::SUCCESS) {
637         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get intArray by name error, resName=%{public}s, errorCode=%{public}d",
638             resName.c_str(), state);
639         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
640         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
641             resName, "IntArray", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
642     }
643 
644     std::vector<uint32_t> result(intVectorResult.size());
645     std::transform(
646         intVectorResult.begin(), intVectorResult.end(), result.begin(), [](int x) { return static_cast<uint32_t>(x); });
647     return result;
648 }
649 
GetBoolean(uint32_t resId) const650 bool ResourceAdapterImplV2::GetBoolean(uint32_t resId) const
651 {
652     bool result = false;
653     auto manager = GetResourceManager();
654     CHECK_NULL_RETURN(manager, result);
655     auto state = manager->GetBooleanById(resId, result);
656     if (state != Global::Resource::SUCCESS) {
657         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get boolean by id error, id=%{public}u, errorCode=%{public}d", resId, state);
658         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
659         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
660             std::to_string(resId), "Boolean", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
661     }
662     return result;
663 }
664 
GetBooleanByName(const std::string & resName) const665 bool ResourceAdapterImplV2::GetBooleanByName(const std::string& resName) const
666 {
667     bool result = false;
668     auto actualResName = GetActualResourceName(resName);
669     auto manager = GetResourceManager();
670     CHECK_NULL_RETURN(manager, result);
671     auto state = manager->GetBooleanByName(actualResName.c_str(), result);
672     if (state != Global::Resource::SUCCESS) {
673         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get boolean by name error, resName=%{public}s, errorCode=%{public}d",
674             resName.c_str(), state);
675         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
676         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
677             resName, "Boolean", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
678     }
679     return result;
680 }
681 
GetPixelMap(uint32_t resId)682 std::shared_ptr<Media::PixelMap> ResourceAdapterImplV2::GetPixelMap(uint32_t resId)
683 {
684     auto manager = GetResourceManager();
685 
686     CHECK_NULL_RETURN(manager, nullptr);
687     Napi::DrawableDescriptor::DrawableType drawableType;
688     Global::Resource::RState state;
689     auto drawableDescriptor =
690         Napi::DrawableDescriptorFactory::Create(resId, sysResourceManager_, state, drawableType, 0);
691     if (state != Global::Resource::SUCCESS) {
692         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Failed to Create drawableDescriptor by %{public}d, errorCode=%{public}d",
693             resId, state);
694         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
695         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
696             std::to_string(resId), "PixelMap", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
697         return nullptr;
698     }
699     CHECK_NULL_RETURN(drawableDescriptor, nullptr);
700     return drawableDescriptor->GetPixelMap();
701 }
702 
GetMediaPath(uint32_t resId)703 std::string ResourceAdapterImplV2::GetMediaPath(uint32_t resId)
704 {
705     std::string mediaPath = "";
706     auto manager = GetResourceManager();
707     CHECK_NULL_RETURN(manager, "");
708     auto state = manager->GetMediaById(resId, mediaPath);
709     if (state != Global::Resource::SUCCESS) {
710         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get media by id error, id=%{public}u, errorCode=%{public}u", resId, state);
711         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
712         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
713             std::to_string(resId), "MediaPath", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
714         return "";
715     }
716     if (SystemProperties::GetUnZipHap()) {
717         return "file:///" + mediaPath;
718     }
719     auto pos = mediaPath.find_last_of('.');
720     if (pos == std::string::npos) {
721         return "";
722     }
723     return "resource:///" + std::to_string(resId) + mediaPath.substr(pos);
724 }
725 
GetMediaPathByName(const std::string & resName)726 std::string ResourceAdapterImplV2::GetMediaPathByName(const std::string& resName)
727 {
728     std::string mediaPath = "";
729     auto actualResName = GetActualResourceName(resName);
730     {
731         auto manager = GetResourceManager();
732         CHECK_NULL_RETURN(manager, "");
733         auto state = manager->GetMediaByName(actualResName.c_str(), mediaPath);
734         if (state != Global::Resource::SUCCESS) {
735             TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get media path by name error, resName=%{public}s, errorCode=%{public}u",
736                 resName.c_str(), state);
737             auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
738             ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
739                 resName, "MediaPath", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
740             return "";
741         }
742     }
743     if (SystemProperties::GetUnZipHap()) {
744         return "file:///" + mediaPath;
745     }
746     auto pos = mediaPath.find_last_of('.');
747     if (pos == std::string::npos) {
748         return "";
749     }
750     return "resource:///" + actualResName + mediaPath.substr(pos);
751 }
752 
GetRawfile(const std::string & fileName)753 std::string ResourceAdapterImplV2::GetRawfile(const std::string& fileName)
754 {
755     // as web component not support resource format: resource://RAWFILE/{fileName}, use old format
756     if (!packagePathStr_.empty()) {
757         std::string outPath;
758         auto manager = GetResourceManager();
759         CHECK_NULL_RETURN(manager, "");
760         // Adapt to the input like: "file:///index.html?a=1", before the new solution comes.
761         auto it = std::find_if(fileName.begin(), fileName.end(), [](char c) { return (c == '#') || (c == '?'); });
762         std::string params;
763         std::string newFileName = fileName;
764         if (it != fileName.end()) {
765             newFileName = std::string(fileName.begin(), it);
766             params = std::string(it, fileName.end());
767         }
768         auto state = manager->GetRawFilePathByName(newFileName, outPath);
769         if (state != Global::Resource::SUCCESS) {
770             TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get rawFile error, filename:%{public}s, error:%{public}u",
771                 fileName.c_str(), state);
772             auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
773             ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
774                 fileName, "RawFile", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
775             return "";
776         }
777         return "file:///" + outPath + params;
778     }
779     std::regex regex(RESOURCE_TOKEN_PATTERN);
780     std::smatch results;
781     std::string newFIleName = fileName;
782     if (std::regex_match(fileName, results, regex)) {
783         newFIleName = results[1];
784     }
785     return "resource://RAWFILE/" + newFIleName;
786 }
787 
GetRawFileData(const std::string & rawFile,size_t & len,std::unique_ptr<uint8_t[]> & dest)788 bool ResourceAdapterImplV2::GetRawFileData(const std::string& rawFile, size_t& len, std::unique_ptr<uint8_t[]>& dest)
789 {
790     auto manager = GetResourceManager();
791     CHECK_NULL_RETURN(manager, false);
792     auto state = manager->GetRawFileFromHap(rawFile, len, dest);
793     if (state != Global::Resource::SUCCESS || !dest) {
794         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get rawFile from hap error, raw filename:%{public}s, error:%{public}u",
795             rawFile.c_str(), state);
796         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
797         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
798             rawFile, "RawFile", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
799         return false;
800     }
801     return true;
802 }
803 
GetRawFileData(const std::string & rawFile,size_t & len,std::unique_ptr<uint8_t[]> & dest,const std::string & bundleName,const std::string & moduleName)804 bool ResourceAdapterImplV2::GetRawFileData(const std::string& rawFile, size_t& len, std::unique_ptr<uint8_t[]>& dest,
805     const std::string& bundleName, const std::string& moduleName)
806 {
807     auto manager = GetResourceManager();
808     CHECK_NULL_RETURN(manager, false);
809     auto state = manager->GetRawFileFromHap(rawFile, len, dest);
810     if (state != Global::Resource::SUCCESS || !dest) {
811         TAG_LOGW(AceLogTag::ACE_RESOURCE,
812             "Get rawFile from hap error, raw filename:%{public}s, bundleName:%{public}s, moduleName:%{public}s, "
813             "error:%{public}u",
814             rawFile.c_str(), bundleName.c_str(), moduleName.c_str(), state);
815         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
816         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
817             rawFile, "RawFile", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
818         return false;
819     }
820     return true;
821 }
822 
GetMediaData(uint32_t resId,size_t & len,std::unique_ptr<uint8_t[]> & dest)823 bool ResourceAdapterImplV2::GetMediaData(uint32_t resId, size_t& len, std::unique_ptr<uint8_t[]>& dest)
824 {
825     auto manager = GetResourceManager();
826     CHECK_NULL_RETURN(manager, false);
827     auto state = manager->GetMediaDataById(resId, len, dest);
828     if (state != Global::Resource::SUCCESS) {
829         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get media data by id error, id:%{public}u, error:%{public}u", resId, state);
830         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
831         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
832             std::to_string(resId), "MediaData", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
833         return false;
834     }
835     return true;
836 }
837 
GetMediaData(uint32_t resId,size_t & len,std::unique_ptr<uint8_t[]> & dest,const std::string & bundleName,const std::string & moduleName)838 bool ResourceAdapterImplV2::GetMediaData(uint32_t resId, size_t& len, std::unique_ptr<uint8_t[]>& dest,
839     const std::string& bundleName, const std::string& moduleName)
840 {
841     auto manager = GetResourceManager();
842     CHECK_NULL_RETURN(manager, false);
843     auto state = manager->GetMediaDataById(resId, len, dest);
844     if (state != Global::Resource::SUCCESS) {
845         TAG_LOGW(AceLogTag::ACE_RESOURCE,
846             "Get media data by id error, id:%{public}u, bundleName:%{public}s, moduleName:%{public}s, error:%{public}u",
847             resId, bundleName.c_str(), moduleName.c_str(), state);
848         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
849         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
850             std::to_string(resId), "MediaData", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
851         return false;
852     }
853     return true;
854 }
855 
GetMediaData(const std::string & resName,size_t & len,std::unique_ptr<uint8_t[]> & dest)856 bool ResourceAdapterImplV2::GetMediaData(const std::string& resName, size_t& len, std::unique_ptr<uint8_t[]>& dest)
857 {
858     auto manager = GetResourceManager();
859     CHECK_NULL_RETURN(manager, false);
860     auto state = manager->GetMediaDataByName(resName.c_str(), len, dest);
861     if (state != Global::Resource::SUCCESS) {
862         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get media data by name error, resName:%{public}s, error:%{public}u",
863             resName.c_str(), state);
864         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
865         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
866             resName, "MediaData", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
867         return false;
868     }
869     return true;
870 }
871 
GetMediaData(const std::string & resName,size_t & len,std::unique_ptr<uint8_t[]> & dest,const std::string & bundleName,const std::string & moduleName)872 bool ResourceAdapterImplV2::GetMediaData(const std::string& resName, size_t& len, std::unique_ptr<uint8_t[]>& dest,
873     const std::string& bundleName, const std::string& moduleName)
874 {
875     auto manager = GetResourceManager();
876     CHECK_NULL_RETURN(manager, false);
877     auto state = manager->GetMediaDataByName(resName.c_str(), len, dest);
878     if (state != Global::Resource::SUCCESS) {
879         TAG_LOGW(AceLogTag::ACE_RESOURCE,
880             "Get media data by name error, resName:%{public}s, bundleName:%{public}s, moduleName:%{public}s, "
881             "error:%{public}u",
882             resName.c_str(), bundleName.c_str(), moduleName.c_str(), state);
883         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
884         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
885             resName, "MediaData", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
886         return false;
887     }
888     return true;
889 }
890 
GetRawFileDescription(const std::string & rawfileName,RawfileDescription & rawfileDescription) const891 bool ResourceAdapterImplV2::GetRawFileDescription(
892     const std::string& rawfileName, RawfileDescription& rawfileDescription) const
893 {
894     OHOS::Global::Resource::ResourceManager::RawFileDescriptor descriptor;
895     auto manager = GetResourceManager();
896     CHECK_NULL_RETURN(manager, false);
897     auto state = manager->GetRawFileDescriptorFromHap(rawfileName, descriptor);
898     if (state != Global::Resource::SUCCESS) {
899         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get raw file description error, rawFileName:%{public}s, error:%{public}u",
900             rawfileName.c_str(), state);
901         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
902         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
903             rawfileName, "RawFileDescription", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
904         return false;
905     }
906     rawfileDescription.fd = descriptor.fd;
907     rawfileDescription.offset = descriptor.offset;
908     rawfileDescription.length = descriptor.length;
909     return true;
910 }
911 
CloseRawFileDescription(const std::string & rawfileName) const912 bool ResourceAdapterImplV2::CloseRawFileDescription(const std::string &rawfileName) const
913 {
914     auto manager = GetResourceManager();
915     CHECK_NULL_RETURN(manager, false);
916     auto state = manager->CloseRawFileDescriptor(rawfileName);
917     if (state != Global::Resource::SUCCESS) {
918         LOGE("Close RawFile Description error, error:%{public}u", state);
919         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
920         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
921             rawfileName, "RawFileDescription", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
922         return false;
923     }
924     return true;
925 }
926 
GetRawFD(const std::string & rawfileName,RawfileDescription & rawfileDescription) const927 bool ResourceAdapterImplV2::GetRawFD(const std::string& rawfileName, RawfileDescription& rawfileDescription) const
928 {
929     OHOS::Global::Resource::ResourceManager::RawFileDescriptor descriptor;
930     auto manager = GetResourceManager();
931     CHECK_NULL_RETURN(manager, false);
932     auto state = manager->GetRawFdNdkFromHap(rawfileName, descriptor);
933     if (state != Global::Resource::SUCCESS) {
934         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get raw fd(no cache) error, rawFileName:%{public}s, error:%{public}u",
935             rawfileName.c_str(), state);
936         return false;
937     }
938     rawfileDescription.fd = descriptor.fd;
939     rawfileDescription.offset = descriptor.offset;
940     rawfileDescription.length = descriptor.length;
941     return true;
942 }
943 
GetMediaById(const int32_t & resId,std::string & mediaPath) const944 bool ResourceAdapterImplV2::GetMediaById(const int32_t& resId, std::string& mediaPath) const
945 {
946     auto manager = GetResourceManager();
947     CHECK_NULL_RETURN(manager, false);
948     auto state = manager->GetMediaById(resId, mediaPath);
949     if (state != Global::Resource::SUCCESS) {
950         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get media by id error, resId:%{public}d, error:%{public}u", resId, state);
951         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
952         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
953             std::to_string(resId), "Media", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
954         return false;
955     }
956     return true;
957 }
958 
GetActualResourceName(const std::string & resName) const959 std::string ResourceAdapterImplV2::GetActualResourceName(const std::string& resName) const
960 {
961     auto index = resName.find_last_of('.');
962     if (index == std::string::npos) {
963         return {};
964     }
965     return resName.substr(index + 1, resName.length() - index - 1);
966 }
967 
GetResourceLimitKeys() const968 uint32_t ResourceAdapterImplV2::GetResourceLimitKeys() const
969 {
970     auto manager = GetResourceManager();
971     CHECK_NULL_RETURN(manager, 0);
972     return manager->GetResourceLimitKeys();
973 }
974 
GetSymbolByName(const char * resName) const975 uint32_t ResourceAdapterImplV2::GetSymbolByName(const char* resName) const
976 {
977     uint32_t result = 0;
978     auto actualResName = GetActualResourceName(resName);
979     auto manager = GetResourceManager();
980     CHECK_NULL_RETURN(manager, -1);
981     auto state = manager->GetSymbolByName(actualResName.c_str(), result);
982     if (state != Global::Resource::SUCCESS) {
983         TAG_LOGW(AceLogTag::ACE_RESOURCE, "Get symbol by name error, name=%{public}s, errorCode=%{public}d",
984             resName, state);
985         auto host = NG::ViewStackProcessor::GetInstance()->GetMainElementNode();
986         ResourceManager::GetInstance().AddResourceLoadError(ResourceErrorInfo(host ? host->GetId(): -1,
987             resName, "Symbol", host ? host->GetTag().c_str() : "", GetCurrentTimestamp(), state));
988     }
989     return result;
990 }
991 
GetSymbolById(uint32_t resId) const992 uint32_t ResourceAdapterImplV2::GetSymbolById(uint32_t resId) const
993 {
994     uint32_t result = 0;
995     auto manager = GetResourceManager();
996     CHECK_NULL_RETURN(manager, -1);
997     manager->GetSymbolById(resId, result);
998     return result;
999 }
1000 
UpdateColorMode(ColorMode colorMode)1001 void ResourceAdapterImplV2::UpdateColorMode(ColorMode colorMode)
1002 {
1003     RefPtr<Container> container = Container::Current();
1004     CHECK_NULL_VOID(container);
1005     auto aceContainer = AceType::DynamicCast<Platform::AceContainer>(container);
1006     CHECK_NULL_VOID(aceContainer);
1007 
1008     auto resConfig = aceContainer->GetResourceConfiguration();
1009     resConfig.SetColorMode(colorMode);
1010     UpdateConfig(resConfig, false);
1011 }
1012 
GetResourceColorMode() const1013 ColorMode ResourceAdapterImplV2::GetResourceColorMode() const
1014 {
1015     CHECK_NULL_RETURN(resConfig_, ColorMode::LIGHT);
1016     if (resConfig_->GetColorMode() == OHOS::Global::Resource::ColorMode::DARK && !resConfig_->GetAppColorMode() &&
1017         !appHasDarkRes_) {
1018         return ColorMode::LIGHT;
1019     }
1020     return resConfig_->GetColorMode() == OHOS::Global::Resource::ColorMode::DARK ? ColorMode::DARK : ColorMode::LIGHT;
1021 }
1022 
SetAppHasDarkRes(bool hasDarkRes)1023 void ResourceAdapterImplV2::SetAppHasDarkRes(bool hasDarkRes)
1024 {
1025     appHasDarkRes_ = hasDarkRes;
1026 }
1027 
GetOverrideResourceAdapter(const ResourceConfiguration & config,const ConfigurationChange & configurationChange)1028 RefPtr<ResourceAdapter> ResourceAdapterImplV2::GetOverrideResourceAdapter(
1029     const ResourceConfiguration& config, const ConfigurationChange& configurationChange)
1030 {
1031     CHECK_NULL_RETURN(sysResourceManager_, nullptr);
1032     std::shared_ptr<Global::Resource::ResConfig> overrideResConfig(Global::Resource::CreateResConfig());
1033     sysResourceManager_->GetOverrideResConfig(*overrideResConfig);
1034     if (configurationChange.colorModeUpdate) {
1035         overrideResConfig->SetColorMode(ConvertColorModeToGlobal(config.GetColorMode()));
1036     }
1037     if (configurationChange.directionUpdate) {
1038         overrideResConfig->SetDirection(ConvertDirectionToGlobal(config.GetOrientation()));
1039     }
1040     if (configurationChange.dpiUpdate) {
1041         overrideResConfig->SetScreenDensity(config.GetDensity());
1042     }
1043     auto overrideResMgr = sysResourceManager_->GetOverrideResourceManager(overrideResConfig);
1044     return AceType::MakeRefPtr<ResourceAdapterImplV2>(overrideResMgr);
1045 }
1046 } // namespace OHOS::Ace
1047