123b3eb3cSopenharmony_ci/* 223b3eb3cSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License. 523b3eb3cSopenharmony_ci * You may obtain a copy of the License at 623b3eb3cSopenharmony_ci * 723b3eb3cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 823b3eb3cSopenharmony_ci * 923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and 1323b3eb3cSopenharmony_ci * limitations under the License. 1423b3eb3cSopenharmony_ci */ 1523b3eb3cSopenharmony_ci 1623b3eb3cSopenharmony_ci#include "adapter/ohos/osal/resource_adapter_impl.h" 1723b3eb3cSopenharmony_ci 1823b3eb3cSopenharmony_ci#include <dirent.h> 1923b3eb3cSopenharmony_ci 2023b3eb3cSopenharmony_ci#include "drawable_descriptor.h" 2123b3eb3cSopenharmony_ci 2223b3eb3cSopenharmony_ci#include "adapter/ohos/entrance/ace_container.h" 2323b3eb3cSopenharmony_ci#include "adapter/ohos/osal/resource_convertor.h" 2423b3eb3cSopenharmony_ci#include "adapter/ohos/osal/resource_theme_style.h" 2523b3eb3cSopenharmony_cinamespace OHOS::Ace { 2623b3eb3cSopenharmony_cinamespace { 2723b3eb3cSopenharmony_ci 2823b3eb3cSopenharmony_ciconstexpr uint32_t OHOS_THEME_ID = 125829872; // ohos_theme 2923b3eb3cSopenharmony_ci 3023b3eb3cSopenharmony_civoid CheckThemeId(int32_t& themeId) 3123b3eb3cSopenharmony_ci{ 3223b3eb3cSopenharmony_ci if (themeId >= 0) { 3323b3eb3cSopenharmony_ci return; 3423b3eb3cSopenharmony_ci } 3523b3eb3cSopenharmony_ci themeId = OHOS_THEME_ID; 3623b3eb3cSopenharmony_ci} 3723b3eb3cSopenharmony_ci 3823b3eb3cSopenharmony_ciconst char* PATTERN_MAP[] = { 3923b3eb3cSopenharmony_ci THEME_PATTERN_BUTTON, 4023b3eb3cSopenharmony_ci THEME_PATTERN_CHECKBOX, 4123b3eb3cSopenharmony_ci THEME_PATTERN_DATA_PANEL, 4223b3eb3cSopenharmony_ci THEME_PATTERN_RADIO, 4323b3eb3cSopenharmony_ci THEME_PATTERN_SWIPER, 4423b3eb3cSopenharmony_ci THEME_PATTERN_SWITCH, 4523b3eb3cSopenharmony_ci THEME_PATTERN_TOOLBAR, 4623b3eb3cSopenharmony_ci THEME_PATTERN_TOGGLE, 4723b3eb3cSopenharmony_ci THEME_PATTERN_TOAST, 4823b3eb3cSopenharmony_ci THEME_PATTERN_DIALOG, 4923b3eb3cSopenharmony_ci THEME_PATTERN_DRAG_BAR, 5023b3eb3cSopenharmony_ci THEME_PATTERN_CLOSE_ICON, 5123b3eb3cSopenharmony_ci THEME_PATTERN_SEMI_MODAL, 5223b3eb3cSopenharmony_ci THEME_PATTERN_BADGE, 5323b3eb3cSopenharmony_ci THEME_PATTERN_CALENDAR, 5423b3eb3cSopenharmony_ci THEME_PATTERN_CAMERA, 5523b3eb3cSopenharmony_ci THEME_PATTERN_CARD, 5623b3eb3cSopenharmony_ci THEME_PATTERN_CLOCK, 5723b3eb3cSopenharmony_ci THEME_PATTERN_COUNTER, 5823b3eb3cSopenharmony_ci THEME_PATTERN_DIVIDER, 5923b3eb3cSopenharmony_ci THEME_PATTERN_FOCUS_ANIMATION, 6023b3eb3cSopenharmony_ci THEME_PATTERN_GRID, 6123b3eb3cSopenharmony_ci THEME_PATTERN_HYPERLINK, 6223b3eb3cSopenharmony_ci THEME_PATTERN_IMAGE, 6323b3eb3cSopenharmony_ci THEME_PATTERN_LIST, 6423b3eb3cSopenharmony_ci THEME_PATTERN_LIST_ITEM, 6523b3eb3cSopenharmony_ci THEME_PATTERN_MARQUEE, 6623b3eb3cSopenharmony_ci THEME_PATTERN_NAVIGATION_BAR, 6723b3eb3cSopenharmony_ci THEME_PATTERN_PICKER, 6823b3eb3cSopenharmony_ci THEME_PATTERN_PIECE, 6923b3eb3cSopenharmony_ci THEME_PATTERN_POPUP, 7023b3eb3cSopenharmony_ci THEME_PATTERN_PROGRESS, 7123b3eb3cSopenharmony_ci THEME_PATTERN_QRCODE, 7223b3eb3cSopenharmony_ci THEME_PATTERN_RATING, 7323b3eb3cSopenharmony_ci THEME_PATTERN_REFRESH, 7423b3eb3cSopenharmony_ci THEME_PATTERN_SCROLL_BAR, 7523b3eb3cSopenharmony_ci THEME_PATTERN_SEARCH, 7623b3eb3cSopenharmony_ci THEME_PATTERN_SELECT, 7723b3eb3cSopenharmony_ci THEME_PATTERN_SLIDER, 7823b3eb3cSopenharmony_ci THEME_PATTERN_STEPPER, 7923b3eb3cSopenharmony_ci THEME_PATTERN_TAB, 8023b3eb3cSopenharmony_ci THEME_PATTERN_TEXT, 8123b3eb3cSopenharmony_ci THEME_PATTERN_TEXTFIELD, 8223b3eb3cSopenharmony_ci THEME_PATTERN_TEXT_OVERLAY, 8323b3eb3cSopenharmony_ci THEME_PATTERN_VIDEO, 8423b3eb3cSopenharmony_ci THEME_PATTERN_ICON, 8523b3eb3cSopenharmony_ci THEME_PATTERN_INDEXER, 8623b3eb3cSopenharmony_ci THEME_PATTERN_APP_BAR, 8723b3eb3cSopenharmony_ci THEME_PATTERN_ADVANCED_PATTERN, 8823b3eb3cSopenharmony_ci THEME_PATTERN_SECURITY_COMPONENT, 8923b3eb3cSopenharmony_ci THEME_PATTERN_FORM, 9023b3eb3cSopenharmony_ci THEME_PATTERN_SIDE_BAR, 9123b3eb3cSopenharmony_ci THEME_PATTERN_RICH_EDITOR, 9223b3eb3cSopenharmony_ci THEME_PATTERN_PATTERN_LOCK, 9323b3eb3cSopenharmony_ci THEME_PATTERN_GAUGE, 9423b3eb3cSopenharmony_ci THEME_PATTERN_SHEET, 9523b3eb3cSopenharmony_ci THEME_BLUR_STYLE_COMMON, 9623b3eb3cSopenharmony_ci THEME_PATTERN_SHADOW, 9723b3eb3cSopenharmony_ci THEME_PATTERN_CONTAINER_MODAL, 9823b3eb3cSopenharmony_ci THEME_PATTERN_SCROLLABLE, 9923b3eb3cSopenharmony_ci THEME_PATTERN_LINEAR_INDICATOR 10023b3eb3cSopenharmony_ci}; 10123b3eb3cSopenharmony_ci 10223b3eb3cSopenharmony_cibool IsDirExist(const std::string& path) 10323b3eb3cSopenharmony_ci{ 10423b3eb3cSopenharmony_ci char realPath[PATH_MAX] = { 0x00 }; 10523b3eb3cSopenharmony_ci CHECK_NULL_RETURN(realpath(path.c_str(), realPath), false); 10623b3eb3cSopenharmony_ci DIR* dir = opendir(realPath); 10723b3eb3cSopenharmony_ci CHECK_NULL_RETURN(dir, false); 10823b3eb3cSopenharmony_ci closedir(dir); 10923b3eb3cSopenharmony_ci return true; 11023b3eb3cSopenharmony_ci} 11123b3eb3cSopenharmony_ci 11223b3eb3cSopenharmony_ciDimensionUnit ParseDimensionUnit(const std::string& unit) 11323b3eb3cSopenharmony_ci{ 11423b3eb3cSopenharmony_ci if (unit == "px") { 11523b3eb3cSopenharmony_ci return DimensionUnit::PX; 11623b3eb3cSopenharmony_ci } else if (unit == "fp") { 11723b3eb3cSopenharmony_ci return DimensionUnit::FP; 11823b3eb3cSopenharmony_ci } else if (unit == "lpx") { 11923b3eb3cSopenharmony_ci return DimensionUnit::LPX; 12023b3eb3cSopenharmony_ci } else if (unit == "%") { 12123b3eb3cSopenharmony_ci return DimensionUnit::PERCENT; 12223b3eb3cSopenharmony_ci } else { 12323b3eb3cSopenharmony_ci return DimensionUnit::VP; 12423b3eb3cSopenharmony_ci } 12523b3eb3cSopenharmony_ci}; 12623b3eb3cSopenharmony_ci 12723b3eb3cSopenharmony_ci} // namespace 12823b3eb3cSopenharmony_ci 12923b3eb3cSopenharmony_ciRefPtr<ResourceAdapter> ResourceAdapter::Create() 13023b3eb3cSopenharmony_ci{ 13123b3eb3cSopenharmony_ci return AceType::MakeRefPtr<ResourceAdapterImpl>(); 13223b3eb3cSopenharmony_ci} 13323b3eb3cSopenharmony_ci 13423b3eb3cSopenharmony_civoid ResourceAdapterImpl::Init(const ResourceInfo& resourceInfo) 13523b3eb3cSopenharmony_ci{ 13623b3eb3cSopenharmony_ci std::string resPath = resourceInfo.GetPackagePath(); 13723b3eb3cSopenharmony_ci std::string hapPath = resourceInfo.GetHapPath(); 13823b3eb3cSopenharmony_ci auto resConfig = ConvertConfigToGlobal(resourceInfo.GetResourceConfiguration()); 13923b3eb3cSopenharmony_ci std::shared_ptr<Global::Resource::ResourceManager> newResMgr(Global::Resource::CreateResourceManager()); 14023b3eb3cSopenharmony_ci std::string resIndexPath = hapPath.empty() ? (resPath + "resources.index") : hapPath; 14123b3eb3cSopenharmony_ci auto resRet = newResMgr->AddResource(resIndexPath.c_str()); 14223b3eb3cSopenharmony_ci 14323b3eb3cSopenharmony_ci if (resConfig != nullptr) { 14423b3eb3cSopenharmony_ci auto configRet = newResMgr->UpdateResConfig(*resConfig); 14523b3eb3cSopenharmony_ci LOGI("AddRes result=%{public}d, UpdateResConfig result=%{public}d, ori=%{public}d, dpi=%{public}f, " 14623b3eb3cSopenharmony_ci "device=%{public}d, colorMode=%{public}d, inputDevice=%{public}d", 14723b3eb3cSopenharmony_ci resRet, configRet, resConfig->GetDirection(), resConfig->GetScreenDensity(), resConfig->GetDeviceType(), 14823b3eb3cSopenharmony_ci resConfig->GetColorMode(), resConfig->GetInputDevice()); 14923b3eb3cSopenharmony_ci } 15023b3eb3cSopenharmony_ci sysResourceManager_ = newResMgr; 15123b3eb3cSopenharmony_ci { 15223b3eb3cSopenharmony_ci std::unique_lock<std::shared_mutex> lock(resourceMutex_); 15323b3eb3cSopenharmony_ci resourceManager_ = sysResourceManager_; 15423b3eb3cSopenharmony_ci } 15523b3eb3cSopenharmony_ci packagePathStr_ = (hapPath.empty() || IsDirExist(resPath)) ? resPath : std::string(); 15623b3eb3cSopenharmony_ci resConfig_ = resConfig; 15723b3eb3cSopenharmony_ci} 15823b3eb3cSopenharmony_ci 15923b3eb3cSopenharmony_civoid ResourceAdapterImpl::UpdateConfig(const ResourceConfiguration& config, bool themeFlag) 16023b3eb3cSopenharmony_ci{ 16123b3eb3cSopenharmony_ci auto resConfig = ConvertConfigToGlobal(config); 16223b3eb3cSopenharmony_ci LOGI("UpdateConfig ori=%{public}d, dpi=%{public}f, device=%{public}d, " 16323b3eb3cSopenharmony_ci "colorMode=%{public}d, inputDevice=%{public}d", 16423b3eb3cSopenharmony_ci resConfig->GetDirection(), resConfig->GetScreenDensity(), resConfig->GetDeviceType(), resConfig->GetColorMode(), 16523b3eb3cSopenharmony_ci resConfig->GetInputDevice()); 16623b3eb3cSopenharmony_ci if (sysResourceManager_ && resConfig != nullptr) { 16723b3eb3cSopenharmony_ci sysResourceManager_->UpdateResConfig(*resConfig, themeFlag); 16823b3eb3cSopenharmony_ci } 16923b3eb3cSopenharmony_ci for (auto& resMgr : resourceManagers_) { 17023b3eb3cSopenharmony_ci if (resConfig != nullptr) { 17123b3eb3cSopenharmony_ci resMgr.second->UpdateResConfig(*resConfig, themeFlag); 17223b3eb3cSopenharmony_ci } 17323b3eb3cSopenharmony_ci } 17423b3eb3cSopenharmony_ci resConfig_ = resConfig; 17523b3eb3cSopenharmony_ci} 17623b3eb3cSopenharmony_ci 17723b3eb3cSopenharmony_ciRefPtr<ThemeStyle> ResourceAdapterImpl::GetTheme(int32_t themeId) 17823b3eb3cSopenharmony_ci{ 17923b3eb3cSopenharmony_ci CheckThemeId(themeId); 18023b3eb3cSopenharmony_ci auto theme = AceType::MakeRefPtr<ResourceThemeStyle>(AceType::Claim(this)); 18123b3eb3cSopenharmony_ci constexpr char flag[] = "ohos_"; // fit with resource/base/theme.json and pattern.json 18223b3eb3cSopenharmony_ci { 18323b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 18423b3eb3cSopenharmony_ci if (manager) { 18523b3eb3cSopenharmony_ci auto ret = manager->GetThemeById(themeId, theme->rawAttrs_); 18623b3eb3cSopenharmony_ci for (size_t i = 0; i < sizeof(PATTERN_MAP) / sizeof(PATTERN_MAP[0]); i++) { 18723b3eb3cSopenharmony_ci ResourceThemeStyle::RawAttrMap attrMap; 18823b3eb3cSopenharmony_ci std::string patternTag = PATTERN_MAP[i]; 18923b3eb3cSopenharmony_ci std::string patternName = std::string(flag) + PATTERN_MAP[i]; 19023b3eb3cSopenharmony_ci ret = manager->GetPatternByName(patternName.c_str(), attrMap); 19123b3eb3cSopenharmony_ci if (attrMap.empty()) { 19223b3eb3cSopenharmony_ci continue; 19323b3eb3cSopenharmony_ci } 19423b3eb3cSopenharmony_ci theme->patternAttrs_[patternTag] = attrMap; 19523b3eb3cSopenharmony_ci } 19623b3eb3cSopenharmony_ci LOGI("themeId=%{public}d, ret=%{public}d, attr size=%{public}zu, pattern size=%{public}zu", themeId, ret, 19723b3eb3cSopenharmony_ci theme->rawAttrs_.size(), theme->patternAttrs_.size()); 19823b3eb3cSopenharmony_ci } 19923b3eb3cSopenharmony_ci } 20023b3eb3cSopenharmony_ci 20123b3eb3cSopenharmony_ci if (theme->patternAttrs_.empty() && theme->rawAttrs_.empty()) { 20223b3eb3cSopenharmony_ci LOGW("theme resource get failed, use default theme config."); 20323b3eb3cSopenharmony_ci return nullptr; 20423b3eb3cSopenharmony_ci } 20523b3eb3cSopenharmony_ci 20623b3eb3cSopenharmony_ci theme->ParseContent(); 20723b3eb3cSopenharmony_ci theme->patternAttrs_.clear(); 20823b3eb3cSopenharmony_ci return theme; 20923b3eb3cSopenharmony_ci} 21023b3eb3cSopenharmony_ci 21123b3eb3cSopenharmony_ciColor ResourceAdapterImpl::GetColor(uint32_t resId) 21223b3eb3cSopenharmony_ci{ 21323b3eb3cSopenharmony_ci uint32_t result = 0; 21423b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 21523b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, Color(result)); 21623b3eb3cSopenharmony_ci auto state = manager->GetColorById(resId, result); 21723b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 21823b3eb3cSopenharmony_ci LOGE("GetColor error, id=%{public}u", resId); 21923b3eb3cSopenharmony_ci } 22023b3eb3cSopenharmony_ci return Color(result); 22123b3eb3cSopenharmony_ci} 22223b3eb3cSopenharmony_ci 22323b3eb3cSopenharmony_ciColor ResourceAdapterImpl::GetColorByName(const std::string& resName) 22423b3eb3cSopenharmony_ci{ 22523b3eb3cSopenharmony_ci uint32_t result = 0; 22623b3eb3cSopenharmony_ci auto actualResName = GetActualResourceName(resName); 22723b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 22823b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, Color(result)); 22923b3eb3cSopenharmony_ci auto state = manager->GetColorByName(actualResName.c_str(), result); 23023b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 23123b3eb3cSopenharmony_ci LOGE("GetColor error, resName=%{public}s", resName.c_str()); 23223b3eb3cSopenharmony_ci } 23323b3eb3cSopenharmony_ci return Color(result); 23423b3eb3cSopenharmony_ci} 23523b3eb3cSopenharmony_ci 23623b3eb3cSopenharmony_ciDimension ResourceAdapterImpl::GetDimension(uint32_t resId) 23723b3eb3cSopenharmony_ci{ 23823b3eb3cSopenharmony_ci float dimensionFloat = 0.0f; 23923b3eb3cSopenharmony_ci#ifdef NG_BUILD 24023b3eb3cSopenharmony_ci std::string unit; 24123b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 24223b3eb3cSopenharmony_ci if (manager) { 24323b3eb3cSopenharmony_ci auto state = manager->GetFloatById(resId, dimensionFloat, unit); 24423b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 24523b3eb3cSopenharmony_ci LOGE("NG: GetDimension error, id=%{public}u", resId); 24623b3eb3cSopenharmony_ci } 24723b3eb3cSopenharmony_ci } 24823b3eb3cSopenharmony_ci return Dimension(static_cast<double>(dimensionFloat), ParseDimensionUnit(unit)); 24923b3eb3cSopenharmony_ci#else 25023b3eb3cSopenharmony_ci if (Container::IsCurrentUseNewPipeline()) { 25123b3eb3cSopenharmony_ci std::string unit; 25223b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 25323b3eb3cSopenharmony_ci if (manager) { 25423b3eb3cSopenharmony_ci auto state = manager->GetFloatById(resId, dimensionFloat, unit); 25523b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 25623b3eb3cSopenharmony_ci LOGE("NG: GetDimension error, id=%{public}u", resId); 25723b3eb3cSopenharmony_ci } 25823b3eb3cSopenharmony_ci } 25923b3eb3cSopenharmony_ci return Dimension(static_cast<double>(dimensionFloat), ParseDimensionUnit(unit)); 26023b3eb3cSopenharmony_ci } 26123b3eb3cSopenharmony_ci 26223b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 26323b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, Dimension(static_cast<double>(dimensionFloat))); 26423b3eb3cSopenharmony_ci auto state = manager->GetFloatById(resId, dimensionFloat); 26523b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 26623b3eb3cSopenharmony_ci LOGE("GetDimension error, id=%{public}u", resId); 26723b3eb3cSopenharmony_ci } 26823b3eb3cSopenharmony_ci return Dimension(static_cast<double>(dimensionFloat)); 26923b3eb3cSopenharmony_ci#endif 27023b3eb3cSopenharmony_ci} 27123b3eb3cSopenharmony_ci 27223b3eb3cSopenharmony_ciDimension ResourceAdapterImpl::GetDimensionByName(const std::string& resName) 27323b3eb3cSopenharmony_ci{ 27423b3eb3cSopenharmony_ci float dimensionFloat = 0.0f; 27523b3eb3cSopenharmony_ci auto actualResName = GetActualResourceName(resName); 27623b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 27723b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, Dimension()); 27823b3eb3cSopenharmony_ci std::string unit; 27923b3eb3cSopenharmony_ci auto state = manager->GetFloatByName(actualResName.c_str(), dimensionFloat, unit); 28023b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 28123b3eb3cSopenharmony_ci LOGE("GetDimension error, resName=%{public}s", resName.c_str()); 28223b3eb3cSopenharmony_ci } 28323b3eb3cSopenharmony_ci return Dimension(static_cast<double>(dimensionFloat), ParseDimensionUnit(unit)); 28423b3eb3cSopenharmony_ci} 28523b3eb3cSopenharmony_ci 28623b3eb3cSopenharmony_cistd::string ResourceAdapterImpl::GetString(uint32_t resId) 28723b3eb3cSopenharmony_ci{ 28823b3eb3cSopenharmony_ci std::string strResult = ""; 28923b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 29023b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, strResult); 29123b3eb3cSopenharmony_ci manager->GetStringById(resId, strResult); 29223b3eb3cSopenharmony_ci return strResult; 29323b3eb3cSopenharmony_ci} 29423b3eb3cSopenharmony_ci 29523b3eb3cSopenharmony_cistd::string ResourceAdapterImpl::GetStringByName(const std::string& resName) 29623b3eb3cSopenharmony_ci{ 29723b3eb3cSopenharmony_ci std::string strResult = ""; 29823b3eb3cSopenharmony_ci auto actualResName = GetActualResourceName(resName); 29923b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 30023b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, strResult); 30123b3eb3cSopenharmony_ci manager->GetStringByName(actualResName.c_str(), strResult); 30223b3eb3cSopenharmony_ci return strResult; 30323b3eb3cSopenharmony_ci} 30423b3eb3cSopenharmony_ci 30523b3eb3cSopenharmony_cistd::string ResourceAdapterImpl::GetPluralString(uint32_t resId, int quantity) 30623b3eb3cSopenharmony_ci{ 30723b3eb3cSopenharmony_ci std::string strResult = ""; 30823b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 30923b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, strResult); 31023b3eb3cSopenharmony_ci auto state = manager->GetPluralStringById(resId, quantity, strResult); 31123b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 31223b3eb3cSopenharmony_ci LOGE("GetPluralString error, id=%{public}u", resId); 31323b3eb3cSopenharmony_ci } 31423b3eb3cSopenharmony_ci return strResult; 31523b3eb3cSopenharmony_ci} 31623b3eb3cSopenharmony_ci 31723b3eb3cSopenharmony_cistd::string ResourceAdapterImpl::GetPluralStringByName(const std::string& resName, int quantity) 31823b3eb3cSopenharmony_ci{ 31923b3eb3cSopenharmony_ci std::string strResult = ""; 32023b3eb3cSopenharmony_ci auto actualResName = GetActualResourceName(resName); 32123b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 32223b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, strResult); 32323b3eb3cSopenharmony_ci auto state = manager->GetPluralStringByName(actualResName.c_str(), quantity, strResult); 32423b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 32523b3eb3cSopenharmony_ci LOGE("GetPluralString error, resName=%{public}s", resName.c_str()); 32623b3eb3cSopenharmony_ci } 32723b3eb3cSopenharmony_ci return strResult; 32823b3eb3cSopenharmony_ci} 32923b3eb3cSopenharmony_ci 33023b3eb3cSopenharmony_cistd::vector<std::string> ResourceAdapterImpl::GetStringArray(uint32_t resId) const 33123b3eb3cSopenharmony_ci{ 33223b3eb3cSopenharmony_ci std::vector<std::string> strResults; 33323b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 33423b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, strResults); 33523b3eb3cSopenharmony_ci manager->GetStringArrayById(resId, strResults); 33623b3eb3cSopenharmony_ci return strResults; 33723b3eb3cSopenharmony_ci} 33823b3eb3cSopenharmony_ci 33923b3eb3cSopenharmony_cistd::vector<std::string> ResourceAdapterImpl::GetStringArrayByName(const std::string& resName) const 34023b3eb3cSopenharmony_ci{ 34123b3eb3cSopenharmony_ci std::vector<std::string> strResults; 34223b3eb3cSopenharmony_ci auto actualResName = GetActualResourceName(resName); 34323b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 34423b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, strResults); 34523b3eb3cSopenharmony_ci auto state = manager->GetStringArrayByName(actualResName.c_str(), strResults); 34623b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 34723b3eb3cSopenharmony_ci LOGE("GetStringArray error, resName=%{public}s", resName.c_str()); 34823b3eb3cSopenharmony_ci } 34923b3eb3cSopenharmony_ci return strResults; 35023b3eb3cSopenharmony_ci} 35123b3eb3cSopenharmony_ci 35223b3eb3cSopenharmony_cidouble ResourceAdapterImpl::GetDouble(uint32_t resId) 35323b3eb3cSopenharmony_ci{ 35423b3eb3cSopenharmony_ci float result = 0.0f; 35523b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 35623b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, static_cast<double>(result)); 35723b3eb3cSopenharmony_ci auto state = manager->GetFloatById(resId, result); 35823b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 35923b3eb3cSopenharmony_ci LOGE("GetDouble error, id=%{public}u", resId); 36023b3eb3cSopenharmony_ci } 36123b3eb3cSopenharmony_ci return static_cast<double>(result); 36223b3eb3cSopenharmony_ci} 36323b3eb3cSopenharmony_ci 36423b3eb3cSopenharmony_cidouble ResourceAdapterImpl::GetDoubleByName(const std::string& resName) 36523b3eb3cSopenharmony_ci{ 36623b3eb3cSopenharmony_ci float result = 0.0f; 36723b3eb3cSopenharmony_ci auto actualResName = GetActualResourceName(resName); 36823b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 36923b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, static_cast<double>(result)); 37023b3eb3cSopenharmony_ci auto state = manager->GetFloatByName(actualResName.c_str(), result); 37123b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 37223b3eb3cSopenharmony_ci LOGE("GetDouble error, resName=%{public}s", resName.c_str()); 37323b3eb3cSopenharmony_ci } 37423b3eb3cSopenharmony_ci return static_cast<double>(result); 37523b3eb3cSopenharmony_ci} 37623b3eb3cSopenharmony_ci 37723b3eb3cSopenharmony_ciint32_t ResourceAdapterImpl::GetInt(uint32_t resId) 37823b3eb3cSopenharmony_ci{ 37923b3eb3cSopenharmony_ci int32_t result = 0; 38023b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 38123b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, result); 38223b3eb3cSopenharmony_ci auto state = manager->GetIntegerById(resId, result); 38323b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 38423b3eb3cSopenharmony_ci LOGE("GetInt error, id=%{public}u", resId); 38523b3eb3cSopenharmony_ci } 38623b3eb3cSopenharmony_ci return result; 38723b3eb3cSopenharmony_ci} 38823b3eb3cSopenharmony_ci 38923b3eb3cSopenharmony_ciint32_t ResourceAdapterImpl::GetIntByName(const std::string& resName) 39023b3eb3cSopenharmony_ci{ 39123b3eb3cSopenharmony_ci int32_t result = 0; 39223b3eb3cSopenharmony_ci auto actualResName = GetActualResourceName(resName); 39323b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 39423b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, result); 39523b3eb3cSopenharmony_ci auto state = manager->GetIntegerByName(actualResName.c_str(), result); 39623b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 39723b3eb3cSopenharmony_ci LOGE("GetInt error, resName=%{public}s", resName.c_str()); 39823b3eb3cSopenharmony_ci } 39923b3eb3cSopenharmony_ci return result; 40023b3eb3cSopenharmony_ci} 40123b3eb3cSopenharmony_ci 40223b3eb3cSopenharmony_cistd::vector<uint32_t> ResourceAdapterImpl::GetIntArray(uint32_t resId) const 40323b3eb3cSopenharmony_ci{ 40423b3eb3cSopenharmony_ci std::vector<int> intVectorResult; 40523b3eb3cSopenharmony_ci { 40623b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 40723b3eb3cSopenharmony_ci if (manager) { 40823b3eb3cSopenharmony_ci auto state = manager->GetIntArrayById(resId, intVectorResult); 40923b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 41023b3eb3cSopenharmony_ci LOGE("GetIntArray error, id=%{public}u", resId); 41123b3eb3cSopenharmony_ci } 41223b3eb3cSopenharmony_ci } 41323b3eb3cSopenharmony_ci } 41423b3eb3cSopenharmony_ci 41523b3eb3cSopenharmony_ci std::vector<uint32_t> result; 41623b3eb3cSopenharmony_ci std::transform( 41723b3eb3cSopenharmony_ci intVectorResult.begin(), intVectorResult.end(), result.begin(), [](int x) { return static_cast<uint32_t>(x); }); 41823b3eb3cSopenharmony_ci return result; 41923b3eb3cSopenharmony_ci} 42023b3eb3cSopenharmony_ci 42123b3eb3cSopenharmony_cistd::vector<uint32_t> ResourceAdapterImpl::GetIntArrayByName(const std::string& resName) const 42223b3eb3cSopenharmony_ci{ 42323b3eb3cSopenharmony_ci std::vector<int> intVectorResult; 42423b3eb3cSopenharmony_ci auto actualResName = GetActualResourceName(resName); 42523b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 42623b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, {}); 42723b3eb3cSopenharmony_ci auto state = manager->GetIntArrayByName(actualResName.c_str(), intVectorResult); 42823b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 42923b3eb3cSopenharmony_ci LOGE("GetIntArray error, resName=%{public}s", resName.c_str()); 43023b3eb3cSopenharmony_ci } 43123b3eb3cSopenharmony_ci 43223b3eb3cSopenharmony_ci std::vector<uint32_t> result; 43323b3eb3cSopenharmony_ci std::transform( 43423b3eb3cSopenharmony_ci intVectorResult.begin(), intVectorResult.end(), result.begin(), [](int x) { return static_cast<uint32_t>(x); }); 43523b3eb3cSopenharmony_ci return result; 43623b3eb3cSopenharmony_ci} 43723b3eb3cSopenharmony_ci 43823b3eb3cSopenharmony_cibool ResourceAdapterImpl::GetBoolean(uint32_t resId) const 43923b3eb3cSopenharmony_ci{ 44023b3eb3cSopenharmony_ci bool result = false; 44123b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 44223b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, result); 44323b3eb3cSopenharmony_ci auto state = manager->GetBooleanById(resId, result); 44423b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 44523b3eb3cSopenharmony_ci LOGE("GetBoolean error, id=%{public}u", resId); 44623b3eb3cSopenharmony_ci } 44723b3eb3cSopenharmony_ci return result; 44823b3eb3cSopenharmony_ci} 44923b3eb3cSopenharmony_ci 45023b3eb3cSopenharmony_cibool ResourceAdapterImpl::GetBooleanByName(const std::string& resName) const 45123b3eb3cSopenharmony_ci{ 45223b3eb3cSopenharmony_ci bool result = false; 45323b3eb3cSopenharmony_ci auto actualResName = GetActualResourceName(resName); 45423b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 45523b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, result); 45623b3eb3cSopenharmony_ci auto state = manager->GetBooleanByName(actualResName.c_str(), result); 45723b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 45823b3eb3cSopenharmony_ci LOGE("GetBoolean error, resName=%{public}s", resName.c_str()); 45923b3eb3cSopenharmony_ci } 46023b3eb3cSopenharmony_ci return result; 46123b3eb3cSopenharmony_ci} 46223b3eb3cSopenharmony_ci 46323b3eb3cSopenharmony_cistd::shared_ptr<Media::PixelMap> ResourceAdapterImpl::GetPixelMap(uint32_t resId) 46423b3eb3cSopenharmony_ci{ 46523b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 46623b3eb3cSopenharmony_ci 46723b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, nullptr); 46823b3eb3cSopenharmony_ci Napi::DrawableDescriptor::DrawableType drawableType; 46923b3eb3cSopenharmony_ci Global::Resource::RState state; 47023b3eb3cSopenharmony_ci auto drawableDescriptor = Napi::DrawableDescriptorFactory::Create(resId, resourceManager_, state, drawableType, 0); 47123b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 47223b3eb3cSopenharmony_ci LOGE("Failed to Create drawableDescriptor by %{public}d", resId); 47323b3eb3cSopenharmony_ci return nullptr; 47423b3eb3cSopenharmony_ci } 47523b3eb3cSopenharmony_ci CHECK_NULL_RETURN(drawableDescriptor, nullptr); 47623b3eb3cSopenharmony_ci return drawableDescriptor->GetPixelMap(); 47723b3eb3cSopenharmony_ci} 47823b3eb3cSopenharmony_ci 47923b3eb3cSopenharmony_cistd::string ResourceAdapterImpl::GetMediaPath(uint32_t resId) 48023b3eb3cSopenharmony_ci{ 48123b3eb3cSopenharmony_ci std::string mediaPath = ""; 48223b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 48323b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, ""); 48423b3eb3cSopenharmony_ci auto state = manager->GetMediaById(resId, mediaPath); 48523b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 48623b3eb3cSopenharmony_ci LOGE("GetMediaById error, id=%{public}u, errorCode=%{public}u", resId, state); 48723b3eb3cSopenharmony_ci return ""; 48823b3eb3cSopenharmony_ci } 48923b3eb3cSopenharmony_ci if (SystemProperties::GetUnZipHap()) { 49023b3eb3cSopenharmony_ci return "file:///" + mediaPath; 49123b3eb3cSopenharmony_ci } 49223b3eb3cSopenharmony_ci auto pos = mediaPath.find_last_of('.'); 49323b3eb3cSopenharmony_ci if (pos == std::string::npos) { 49423b3eb3cSopenharmony_ci LOGE("GetMediaById error, return mediaPath[%{private}s] format error", mediaPath.c_str()); 49523b3eb3cSopenharmony_ci return ""; 49623b3eb3cSopenharmony_ci } 49723b3eb3cSopenharmony_ci return "resource:///" + std::to_string(resId) + mediaPath.substr(pos); 49823b3eb3cSopenharmony_ci} 49923b3eb3cSopenharmony_ci 50023b3eb3cSopenharmony_cistd::string ResourceAdapterImpl::GetMediaPathByName(const std::string& resName) 50123b3eb3cSopenharmony_ci{ 50223b3eb3cSopenharmony_ci std::string mediaPath = ""; 50323b3eb3cSopenharmony_ci auto actualResName = GetActualResourceName(resName); 50423b3eb3cSopenharmony_ci { 50523b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 50623b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, ""); 50723b3eb3cSopenharmony_ci auto state = manager->GetMediaByName(actualResName.c_str(), mediaPath); 50823b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 50923b3eb3cSopenharmony_ci LOGE("GetMediaPathByName error, resName=%{public}s, errorCode=%{public}u", resName.c_str(), state); 51023b3eb3cSopenharmony_ci return ""; 51123b3eb3cSopenharmony_ci } 51223b3eb3cSopenharmony_ci } 51323b3eb3cSopenharmony_ci if (SystemProperties::GetUnZipHap()) { 51423b3eb3cSopenharmony_ci return "file:///" + mediaPath; 51523b3eb3cSopenharmony_ci } 51623b3eb3cSopenharmony_ci auto pos = mediaPath.find_last_of('.'); 51723b3eb3cSopenharmony_ci if (pos == std::string::npos) { 51823b3eb3cSopenharmony_ci LOGE("GetMediaById error, return mediaPath[%{private}s] format error", mediaPath.c_str()); 51923b3eb3cSopenharmony_ci return ""; 52023b3eb3cSopenharmony_ci } 52123b3eb3cSopenharmony_ci return "resource:///" + actualResName + mediaPath.substr(pos); 52223b3eb3cSopenharmony_ci} 52323b3eb3cSopenharmony_ci 52423b3eb3cSopenharmony_cistd::string ResourceAdapterImpl::GetRawfile(const std::string& fileName) 52523b3eb3cSopenharmony_ci{ 52623b3eb3cSopenharmony_ci // as web component not support resource format: resource://RAWFILE/{fileName}, use old format 52723b3eb3cSopenharmony_ci if (!packagePathStr_.empty()) { 52823b3eb3cSopenharmony_ci std::string outPath; 52923b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 53023b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, ""); 53123b3eb3cSopenharmony_ci // Adapt to the input like: "file:///index.html?a=1", before the new solution comes. 53223b3eb3cSopenharmony_ci auto it = std::find_if(fileName.begin(), fileName.end(), [](char c) { return (c == '#') || (c == '?'); }); 53323b3eb3cSopenharmony_ci std::string params; 53423b3eb3cSopenharmony_ci std::string newFileName = fileName; 53523b3eb3cSopenharmony_ci if (it != fileName.end()) { 53623b3eb3cSopenharmony_ci newFileName = std::string(fileName.begin(), it); 53723b3eb3cSopenharmony_ci params = std::string(it, fileName.end()); 53823b3eb3cSopenharmony_ci } 53923b3eb3cSopenharmony_ci auto state = manager->GetRawFilePathByName(newFileName, outPath); 54023b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 54123b3eb3cSopenharmony_ci LOGE("GetRawfile error, filename:%{public}s, error:%{public}u", fileName.c_str(), state); 54223b3eb3cSopenharmony_ci return ""; 54323b3eb3cSopenharmony_ci } 54423b3eb3cSopenharmony_ci return "file:///" + outPath + params; 54523b3eb3cSopenharmony_ci } 54623b3eb3cSopenharmony_ci return "resource://RAWFILE/" + fileName; 54723b3eb3cSopenharmony_ci} 54823b3eb3cSopenharmony_ci 54923b3eb3cSopenharmony_cibool ResourceAdapterImpl::GetRawFileData(const std::string& rawFile, size_t& len, std::unique_ptr<uint8_t[]>& dest) 55023b3eb3cSopenharmony_ci{ 55123b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 55223b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, false); 55323b3eb3cSopenharmony_ci auto state = manager->GetRawFileFromHap(rawFile, len, dest); 55423b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS || !dest) { 55523b3eb3cSopenharmony_ci LOGW("GetRawFileFromHap error, raw filename:%{public}s, error:%{public}u", rawFile.c_str(), state); 55623b3eb3cSopenharmony_ci return false; 55723b3eb3cSopenharmony_ci } 55823b3eb3cSopenharmony_ci return true; 55923b3eb3cSopenharmony_ci} 56023b3eb3cSopenharmony_ci 56123b3eb3cSopenharmony_cibool ResourceAdapterImpl::GetRawFileData(const std::string& rawFile, size_t& len, std::unique_ptr<uint8_t[]>& dest, 56223b3eb3cSopenharmony_ci const std::string& bundleName, const std::string& moduleName) 56323b3eb3cSopenharmony_ci{ 56423b3eb3cSopenharmony_ci UpdateResourceManager(bundleName, moduleName); 56523b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 56623b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, false); 56723b3eb3cSopenharmony_ci auto state = manager->GetRawFileFromHap(rawFile, len, dest); 56823b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS || !dest) { 56923b3eb3cSopenharmony_ci LOGW("GetRawFileFromHap error, raw filename:%{public}s, bundleName:%{public}s, moduleName:%{public}s, " 57023b3eb3cSopenharmony_ci "error:%{public}u", 57123b3eb3cSopenharmony_ci rawFile.c_str(), bundleName.c_str(), moduleName.c_str(), state); 57223b3eb3cSopenharmony_ci return false; 57323b3eb3cSopenharmony_ci } 57423b3eb3cSopenharmony_ci return true; 57523b3eb3cSopenharmony_ci} 57623b3eb3cSopenharmony_ci 57723b3eb3cSopenharmony_cibool ResourceAdapterImpl::GetMediaData(uint32_t resId, size_t& len, std::unique_ptr<uint8_t[]>& dest) 57823b3eb3cSopenharmony_ci{ 57923b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 58023b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, false); 58123b3eb3cSopenharmony_ci auto state = manager->GetMediaDataById(resId, len, dest); 58223b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 58323b3eb3cSopenharmony_ci LOGW("GetMediaDataById error, id=%{public}u, error:%{public}u", resId, state); 58423b3eb3cSopenharmony_ci return false; 58523b3eb3cSopenharmony_ci } 58623b3eb3cSopenharmony_ci return true; 58723b3eb3cSopenharmony_ci} 58823b3eb3cSopenharmony_ci 58923b3eb3cSopenharmony_cibool ResourceAdapterImpl::GetMediaData(uint32_t resId, size_t& len, std::unique_ptr<uint8_t[]>& dest, 59023b3eb3cSopenharmony_ci const std::string& bundleName, const std::string& moduleName) 59123b3eb3cSopenharmony_ci{ 59223b3eb3cSopenharmony_ci UpdateResourceManager(bundleName, moduleName); 59323b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 59423b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, false); 59523b3eb3cSopenharmony_ci auto state = manager->GetMediaDataById(resId, len, dest); 59623b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 59723b3eb3cSopenharmony_ci LOGW("GetMediaDataById error, id=%{public}u, bundleName:%{public}s, moduleName:%{public}s, error:%{public}u", 59823b3eb3cSopenharmony_ci resId, bundleName.c_str(), moduleName.c_str(), state); 59923b3eb3cSopenharmony_ci return false; 60023b3eb3cSopenharmony_ci } 60123b3eb3cSopenharmony_ci return true; 60223b3eb3cSopenharmony_ci} 60323b3eb3cSopenharmony_ci 60423b3eb3cSopenharmony_cibool ResourceAdapterImpl::GetMediaData(const std::string& resName, size_t& len, std::unique_ptr<uint8_t[]>& dest) 60523b3eb3cSopenharmony_ci{ 60623b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 60723b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, false); 60823b3eb3cSopenharmony_ci auto state = manager->GetMediaDataByName(resName.c_str(), len, dest); 60923b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 61023b3eb3cSopenharmony_ci LOGW("GetMediaDataByName error, res=%{public}s, error:%{public}u", resName.c_str(), state); 61123b3eb3cSopenharmony_ci return false; 61223b3eb3cSopenharmony_ci } 61323b3eb3cSopenharmony_ci return true; 61423b3eb3cSopenharmony_ci} 61523b3eb3cSopenharmony_ci 61623b3eb3cSopenharmony_cibool ResourceAdapterImpl::GetMediaData(const std::string& resName, size_t& len, std::unique_ptr<uint8_t[]>& dest, 61723b3eb3cSopenharmony_ci const std::string& bundleName, const std::string& moduleName) 61823b3eb3cSopenharmony_ci{ 61923b3eb3cSopenharmony_ci UpdateResourceManager(bundleName, moduleName); 62023b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 62123b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, false); 62223b3eb3cSopenharmony_ci auto state = manager->GetMediaDataByName(resName.c_str(), len, dest); 62323b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 62423b3eb3cSopenharmony_ci LOGW("GetMediaDataByName error, res=%{public}s, bundleName:%{public}s, moduleName:%{public}s, error:%{public}u", 62523b3eb3cSopenharmony_ci resName.c_str(), bundleName.c_str(), moduleName.c_str(), state); 62623b3eb3cSopenharmony_ci return false; 62723b3eb3cSopenharmony_ci } 62823b3eb3cSopenharmony_ci return true; 62923b3eb3cSopenharmony_ci} 63023b3eb3cSopenharmony_ci 63123b3eb3cSopenharmony_civoid ResourceAdapterImpl::UpdateResourceManager(const std::string& bundleName, const std::string& moduleName) 63223b3eb3cSopenharmony_ci{ 63323b3eb3cSopenharmony_ci std::unique_lock<std::shared_mutex> lcok(resourceMutex_); 63423b3eb3cSopenharmony_ci if (bundleName.empty() || moduleName.empty()) { 63523b3eb3cSopenharmony_ci resourceManager_ = sysResourceManager_; 63623b3eb3cSopenharmony_ci return; 63723b3eb3cSopenharmony_ci } 63823b3eb3cSopenharmony_ci 63923b3eb3cSopenharmony_ci auto resourceMgrIter = resourceManagers_.find({ bundleName, moduleName }); 64023b3eb3cSopenharmony_ci if (resourceMgrIter != resourceManagers_.end()) { 64123b3eb3cSopenharmony_ci resourceManager_ = resourceMgrIter->second; 64223b3eb3cSopenharmony_ci return; 64323b3eb3cSopenharmony_ci } else { 64423b3eb3cSopenharmony_ci auto container = Container::Current(); 64523b3eb3cSopenharmony_ci CHECK_NULL_VOID(container); 64623b3eb3cSopenharmony_ci auto aceContainer = AceType::DynamicCast<Platform::AceContainer>(container); 64723b3eb3cSopenharmony_ci CHECK_NULL_VOID(aceContainer); 64823b3eb3cSopenharmony_ci auto context = aceContainer->GetAbilityContextByModule(bundleName, moduleName); 64923b3eb3cSopenharmony_ci CHECK_NULL_VOID(context); 65023b3eb3cSopenharmony_ci resourceManagers_[{ bundleName, moduleName }] = context->GetResourceManager(); 65123b3eb3cSopenharmony_ci resourceManager_ = context->GetResourceManager(); 65223b3eb3cSopenharmony_ci if (resourceManager_ && resConfig_ != nullptr) { 65323b3eb3cSopenharmony_ci resourceManager_->UpdateResConfig(*resConfig_); 65423b3eb3cSopenharmony_ci } 65523b3eb3cSopenharmony_ci } 65623b3eb3cSopenharmony_ci} 65723b3eb3cSopenharmony_ci 65823b3eb3cSopenharmony_cibool ResourceAdapterImpl::GetRawFileDescription( 65923b3eb3cSopenharmony_ci const std::string& rawfileName, RawfileDescription& rawfileDescription) const 66023b3eb3cSopenharmony_ci{ 66123b3eb3cSopenharmony_ci OHOS::Global::Resource::ResourceManager::RawFileDescriptor descriptor; 66223b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 66323b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, false); 66423b3eb3cSopenharmony_ci auto state = manager->GetRawFileDescriptorFromHap(rawfileName, descriptor); 66523b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 66623b3eb3cSopenharmony_ci LOGE("GetRawfileDescription error, rawfileName=%{public}s, error:%{public}u", rawfileName.c_str(), state); 66723b3eb3cSopenharmony_ci return false; 66823b3eb3cSopenharmony_ci } 66923b3eb3cSopenharmony_ci rawfileDescription.fd = descriptor.fd; 67023b3eb3cSopenharmony_ci rawfileDescription.offset = descriptor.offset; 67123b3eb3cSopenharmony_ci rawfileDescription.length = descriptor.length; 67223b3eb3cSopenharmony_ci return true; 67323b3eb3cSopenharmony_ci} 67423b3eb3cSopenharmony_ci 67523b3eb3cSopenharmony_cibool ResourceAdapterImpl::GetMediaById(const int32_t& resId, std::string& mediaPath) const 67623b3eb3cSopenharmony_ci{ 67723b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 67823b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, false); 67923b3eb3cSopenharmony_ci auto state = manager->GetMediaById(resId, mediaPath); 68023b3eb3cSopenharmony_ci if (state != Global::Resource::SUCCESS) { 68123b3eb3cSopenharmony_ci LOGE("GetMediaById error, resId=%{public}d, error:%{public}u", resId, state); 68223b3eb3cSopenharmony_ci return false; 68323b3eb3cSopenharmony_ci } 68423b3eb3cSopenharmony_ci return true; 68523b3eb3cSopenharmony_ci} 68623b3eb3cSopenharmony_ci 68723b3eb3cSopenharmony_cistd::string ResourceAdapterImpl::GetActualResourceName(const std::string& resName) const 68823b3eb3cSopenharmony_ci{ 68923b3eb3cSopenharmony_ci auto index = resName.find_last_of('.'); 69023b3eb3cSopenharmony_ci if (index == std::string::npos) { 69123b3eb3cSopenharmony_ci LOGE("GetActualResourceName error, incorrect resName format."); 69223b3eb3cSopenharmony_ci return {}; 69323b3eb3cSopenharmony_ci } 69423b3eb3cSopenharmony_ci return resName.substr(index + 1, resName.length() - index - 1); 69523b3eb3cSopenharmony_ci} 69623b3eb3cSopenharmony_ci 69723b3eb3cSopenharmony_ciuint32_t ResourceAdapterImpl::GetResourceLimitKeys() const 69823b3eb3cSopenharmony_ci{ 69923b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 70023b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, 0); 70123b3eb3cSopenharmony_ci return manager->GetResourceLimitKeys(); 70223b3eb3cSopenharmony_ci} 70323b3eb3cSopenharmony_ci 70423b3eb3cSopenharmony_ciuint32_t ResourceAdapterImpl::GetSymbolById(uint32_t resId) const 70523b3eb3cSopenharmony_ci{ 70623b3eb3cSopenharmony_ci uint32_t result = 0; 70723b3eb3cSopenharmony_ci auto manager = GetResourceManager(); 70823b3eb3cSopenharmony_ci CHECK_NULL_RETURN(manager, -1); 70923b3eb3cSopenharmony_ci manager->GetSymbolById(resId, result); 71023b3eb3cSopenharmony_ci return result; 71123b3eb3cSopenharmony_ci} 71223b3eb3cSopenharmony_ci 71323b3eb3cSopenharmony_ci} // namespace OHOS::Ace 714