diff --git a/frameworks/resmgr/include/hap_manager.h b/frameworks/resmgr/include/hap_manager.h index abd90fe..6e254af 100644 --- a/frameworks/resmgr/include/hap_manager.h +++ b/frameworks/resmgr/include/hap_manager.h @@ -171,8 +171,8 @@ private: std::vector GetResourceListByName(const char *name, const ResType resType) const; - const HapResource::ValueUnderQualifierDir *GetBestMatchResource(std::vector candidates, - uint32_t density); + const HapResource::ValueUnderQualifierDir *GetBestMatchResource(std::vector + candidates, uint32_t density); bool AddResourcePath(const char *path); diff --git a/frameworks/resmgr/include/resource_manager_impl.h b/frameworks/resmgr/include/resource_manager_impl.h index d4d6134..5ea7453 100644 --- a/frameworks/resmgr/include/resource_manager_impl.h +++ b/frameworks/resmgr/include/resource_manager_impl.h @@ -539,7 +539,10 @@ private: "systemResources/SystemResources.hap"; const std::string SYSTEM_RESOURCE_OVERLAY_PATH = "/data/storage/el1/bundle/" \ - "ohos.global.systemres.overlay/ohos.global.systemres.overlay/assets/entry/resources.index"; + "ohos.global.systemres.overlay/ohos.global.systemres.overlay/assets/entry/resources.index"; + + const std::string SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED = "/data/storage/el1/bundle/" \ + "ohos.global.systemres.overlay/ohos.global.systemres.overlay/SystemResourcesOverlay.hap"; const float DEFAULT_DENSITY = 160.0f; diff --git a/frameworks/resmgr/src/hap_manager.cpp b/frameworks/resmgr/src/hap_manager.cpp index a4519df..98fb3e4 100644 --- a/frameworks/resmgr/src/hap_manager.cpp +++ b/frameworks/resmgr/src/hap_manager.cpp @@ -181,8 +181,8 @@ const HapResource::ValueUnderQualifierDir *HapManager::FindQualifierValueById(ui return this->GetBestMatchResource(candidates, density); } -const HapResource::ValueUnderQualifierDir *HapManager::GetBestMatchResource(std::vector candidates, - uint32_t density) +const HapResource::ValueUnderQualifierDir *HapManager::GetBestMatchResource(std::vector + candidates, uint32_t density) { const ResConfigImpl *bestResConfig = nullptr; const ResConfigImpl *bestOverlayResConfig = nullptr; @@ -211,7 +211,7 @@ const HapResource::ValueUnderQualifierDir *HapManager::GetBestMatchResource(std: overlayResult = paths[i]; } } else { - if (bestResConfig == nullptr) { + if (bestResConfig == nullptr) { bestResConfig = resConfig; result = paths[i]; continue; @@ -223,8 +223,8 @@ const HapResource::ValueUnderQualifierDir *HapManager::GetBestMatchResource(std: } } } - if (bestOverlayResConfig != nullptr && result->IsSystemResource()) { - if(bestOverlayResConfig->IsMoreSuitable(bestResConfig, currentResConfig, density)){ + if (bestOverlayResConfig != nullptr && result != nullptr && result->IsSystemResource()) { + if (bestOverlayResConfig->IsMoreSuitable(bestResConfig, currentResConfig, density)) { return overlayResult; } } diff --git a/frameworks/resmgr/src/hap_resource.cpp b/frameworks/resmgr/src/hap_resource.cpp index 657d88e..c56305f 100644 --- a/frameworks/resmgr/src/hap_resource.cpp +++ b/frameworks/resmgr/src/hap_resource.cpp @@ -122,10 +122,6 @@ void CanonicalizePath(const char *path, char *outPath, size_t len) const HapResource* HapResource::Load(const char *path, const ResConfigImpl* defaultConfig, bool system) { - if (strcmp(path, "/data/storage/el1/bundle/ohos.global.systemres/" - "ohos.global.systemres/assets/entry/resources.index") == 0) { - system = true; - } if (Utils::endWithTail(path, "hap")) { return LoadFromHap(path, defaultConfig, system); } else { @@ -227,7 +223,7 @@ const std::unordered_map HapResource::LoadOverlays(c { std::unordered_map result; do { - const HapResource *targetResource = LoadFromIndex(path.c_str(), defaultConfig); + const HapResource *targetResource = Load(path.c_str(), defaultConfig, true); if (targetResource == nullptr) { HILOG_ERROR("load target failed"); break; @@ -237,7 +233,7 @@ const std::unordered_map HapResource::LoadOverlays(c std::unordered_map> mapping = targetResource->BuildNameTypeIdMapping(); for (auto iter = overlayPaths.begin(); iter != overlayPaths.end(); iter++) { - const HapResource *overlayResource = LoadFromIndex(iter->c_str(), defaultConfig); + const HapResource *overlayResource = Load(iter->c_str(), defaultConfig); if (overlayResource == nullptr) { HILOG_ERROR("load overlay failed"); success = false; diff --git a/frameworks/resmgr/src/resource_manager_impl.cpp b/frameworks/resmgr/src/resource_manager_impl.cpp index 27cce08..7f1608b 100644 --- a/frameworks/resmgr/src/resource_manager_impl.cpp +++ b/frameworks/resmgr/src/resource_manager_impl.cpp @@ -80,18 +80,26 @@ bool ResourceManagerImpl::Init() HILOG_ERROR("new HapManager failed when ResourceManagerImpl::Init"); return false; } + if (Utils::isFileExist(SYSTEM_RESOURCE_OVERLAY_PATH) && Utils::isFileExist(SYSTEM_RESOURCE_PATH)) { + vector overlayPaths; + overlayPaths.push_back(SYSTEM_RESOURCE_OVERLAY_PATH); + AddResource(SYSTEM_RESOURCE_PATH.c_str(), overlayPaths); + return true; + } + + if (Utils::isFileExist(SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED) && + Utils::isFileExist(SYSTEM_RESOURCE_PATH_COMPRESSED)) { + vector overlayPaths; + overlayPaths.push_back(SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED); + AddResource(SYSTEM_RESOURCE_PATH_COMPRESSED.c_str(), overlayPaths); + return true; + } + if (Utils::isFileExist(SYSTEM_RESOURCE_PATH)) { AddResource(SYSTEM_RESOURCE_PATH.c_str()); } else { AddResource(SYSTEM_RESOURCE_PATH_COMPRESSED.c_str()); } - - bool overlay_hap_exist = Utils::isFileExist(SYSTEM_RESOURCE_OVERLAY_PATH); - if (overlay_hap_exist) { - vector overlayPaths; - overlayPaths.push_back(SYSTEM_RESOURCE_OVERLAY_PATH); - AddResource(SYSTEM_RESOURCE_PATH.c_str(), overlayPaths); - } return true; } diff --git a/frameworks/resmgr/test/unittest/common/resource_manager_test.cpp b/frameworks/resmgr/test/unittest/common/resource_manager_test.cpp index bb08e31..4c58f17 100644 --- a/frameworks/resmgr/test/unittest/common/resource_manager_test.cpp +++ b/frameworks/resmgr/test/unittest/common/resource_manager_test.cpp @@ -624,6 +624,19 @@ HWTEST_F(ResourceManagerTest, ResourceManagerAddResourceTest003, TestSize.Level1 ASSERT_TRUE(!ret); } +/* + * @tc.name: ResourceManagerAddResourceTest004 + * @tc.desc: Test AddResource function, file case. + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerAddResourceTest004, TestSize.Level1) +{ + std::vector overlayPaths; + overlayPaths.push_back(FormatFullPath(g_overlay_resFilePath).c_str()); + bool ret = ((ResourceManagerImpl *)rm)->AddResource(FormatFullPath(g_system_resFilePath).c_str(), overlayPaths); + ASSERT_TRUE(ret); +} + /* * @tc.name: ResourceManagerUpdateResConfigTest001 * @tc.desc: Test UpdateResConfig function @@ -845,6 +858,21 @@ HWTEST_F(ResourceManagerTest, ResourceManagerGetStringByIdTest003, TestSize.Leve ASSERT_EQ(ERROR_CODE_RES_ID_NOT_FOUND, state); } +/* + * @tc.name: ResourceManagerGetStringByIdTest004 + * @tc.desc: Test GetStringById function + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetStringByIdTest004, TestSize.Level1) +{ + AddResource("en", nullptr, nullptr); + std::vector overlayPaths; + overlayPaths.push_back(FormatFullPath(g_overlay_resFilePath).c_str()); + bool ret = ((ResourceManagerImpl* )rm)->AddResource(FormatFullPath(g_system_resFilePath).c_str(), overlayPaths); + ASSERT_TRUE(ret); + TestStringById("ohos_app_name", "SystemOverlay"); +} + /* * @tc.name: ResourceManagerGetStringByNameTest001 * @tc.desc: Test GetStringByName function diff --git a/frameworks/resmgr/test/unittest/common/resource_manager_test.h b/frameworks/resmgr/test/unittest/common/resource_manager_test.h index f4ca775..f17c18d 100644 --- a/frameworks/resmgr/test/unittest/common/resource_manager_test.h +++ b/frameworks/resmgr/test/unittest/common/resource_manager_test.h @@ -22,6 +22,7 @@ namespace Resource { int ResourceManagerAddResourceTest001(void); int ResourceManagerAddResourceTest002(void); int ResourceManagerAddResourceTest003(void); +int ResourceManagerAddResourceTest004(void); int ResourceManagerUpdateResConfigTest001(void); int ResourceManagerUpdateResConfigTest002(void); int ResourceManagerUpdateResConfigTest003(void); @@ -32,6 +33,7 @@ int ResourceManagerGetResConfigTest002(void); int ResourceManagerGetStringByIdTest001(void); int ResourceManagerGetStringByIdTest002(void); int ResourceManagerGetStringByIdTest003(void); +int ResourceManagerGetStringByIdTest004(void); int ResourceManagerGetStringByNameTest001(void); int ResourceManagerGetStringByNameTest002(void); int ResourceManagerGetStringByNameTest003(void); diff --git a/frameworks/resmgr/test/unittest/common/test_common.h b/frameworks/resmgr/test/unittest/common/test_common.h index c1a31d0..d3acecf 100644 --- a/frameworks/resmgr/test/unittest/common/test_common.h +++ b/frameworks/resmgr/test/unittest/common/test_common.h @@ -27,6 +27,10 @@ static const char *g_resFilePath = "all/assets/entry/resources.index"; static const char *g_hapPath = "all.hap"; +// this is overlay relative path +static const char *g_system_resFilePath = "system/assets/entry/resources.index"; +static const char *g_overlay_resFilePath = "overlay/assets/entry/resources.index"; + namespace OHOS { namespace Global { namespace Resource { diff --git a/interfaces/js/innerkits/core/include/resource_manager_addon.h b/interfaces/js/innerkits/core/include/resource_manager_addon.h index ed139f7..1ce5e8a 100644 --- a/interfaces/js/innerkits/core/include/resource_manager_addon.h +++ b/interfaces/js/innerkits/core/include/resource_manager_addon.h @@ -190,7 +190,7 @@ struct ResMgrAsyncContext { std::shared_ptr resource_; ResMgrAsyncContext() : work_(nullptr), resId_(0), param_(0), iValue_(0), fValue_(0.0f), bValue_(false), - createValueFunc_(nullptr), len_(0), deferred_(nullptr), callbackRef_(nullptr), success_(true) {} + createValueFunc_(nullptr), len_(0), deferred_(nullptr), callbackRef_(nullptr), success_(true), errCode_(0) {} void SetErrorMsg(const std::string &msg, bool withResId = false, int32_t errCode = 0); diff --git a/test/resource/data/overlay/assets/entry/resources.index b/test/resource/data/overlay/assets/entry/resources.index new file mode 100644 index 0000000..d35aa56 Binary files /dev/null and b/test/resource/data/overlay/assets/entry/resources.index differ diff --git a/test/resource/data/system/assets/entry/resources.index b/test/resource/data/system/assets/entry/resources.index new file mode 100644 index 0000000..64657b8 Binary files /dev/null and b/test/resource/data/system/assets/entry/resources.index differ diff --git a/test/resource/ohos_test.xml b/test/resource/ohos_test.xml index e163273..8221344 100644 --- a/test/resource/ohos_test.xml +++ b/test/resource/ohos_test.xml @@ -25,6 +25,8 @@