diff --git a/frameworks/resmgr/include/hap_manager.h b/frameworks/resmgr/include/hap_manager.h index 5e9db8270ed82ddfec83618ee0e9c30b944778c7..29ccd7c38884647bc78edd2b55b73a6a47b72081 100644 --- a/frameworks/resmgr/include/hap_manager.h +++ b/frameworks/resmgr/include/hap_manager.h @@ -440,6 +440,13 @@ public: isOverride_ = isOverride; } + /** + * Update the app config for system resource manager + * + * @param isAppDarkRes true if app set drak res, else not. + * @param isThemeSystemResEnable true if theme system resource is enabled, else not. + */ + void UpdateAppConfigForSysResManager(bool isAppDarkRes, bool isThemeSystemResEnable); private: void UpdateResConfigImpl(ResConfigImpl &resConfig); @@ -486,6 +493,10 @@ private: bool isSystem_; bool isOverride_{ false }; + + bool isThemeSystemResEnable_{ false }; + + bool isUpdateAppConfig_{ true }; }; } // namespace Resource } // namespace Global diff --git a/frameworks/resmgr/include/res_config_impl.h b/frameworks/resmgr/include/res_config_impl.h index 05f3c1b050d180ffdcae243b2339c3e74b67663a..43fce36568b66be53c636b86f7a54f06047781b5 100644 --- a/frameworks/resmgr/include/res_config_impl.h +++ b/frameworks/resmgr/include/res_config_impl.h @@ -231,6 +231,13 @@ public: std::string GetDeviceTypeStr() const; std::string GetScreenDensityStr() const; + + /** + * Get whether the resconfig is invalid + * + * @return True if the resconfig is invalid + */ + bool IsInvalidResConfig(); private: bool IsMoreSpecificThan(const std::shared_ptr other, uint32_t density = 0) const; diff --git a/frameworks/resmgr/include/resource_manager_impl.h b/frameworks/resmgr/include/resource_manager_impl.h index aac5ae2ac6597c3ba279fc52f20272bfbbe74e80..39c0a1f6f081b1e00ffd360dd871160181a00414 100644 --- a/frameworks/resmgr/include/resource_manager_impl.h +++ b/frameworks/resmgr/include/resource_manager_impl.h @@ -23,7 +23,6 @@ #include "resource_manager.h" #include "res_config_impl.h" #include "utils/psue_manager.h" -#include "theme_pack_manager.h" namespace OHOS { namespace Global { @@ -845,6 +844,13 @@ public: virtual RState GetFormatPluralStringByName(std::string &outValue, const char *name, Quantity quantity, std::vector> &jsParams); + /** + * Get the hap manager + * + * @return the hap manager + */ + std::shared_ptr GetHapManager(); + private: RState GetString(const std::shared_ptr idItem, std::string &outValue); @@ -917,14 +923,10 @@ private: RState ProcessItem(std::shared_ptr idItem, std::map &outValue); -#if defined(__IDE_PREVIEW__) - void UpdateSystemResourceResConfig(ResConfig &resConfig); -#endif + void UpdateSystemResourceResConfig(); std::string ReadParameter(const char *paramKey, const int paramLength); - std::shared_ptr GetHapManager(); - std::shared_ptr hapManager_; std::shared_ptr systemResourceManager_{nullptr}; diff --git a/frameworks/resmgr/include/system_resource_manager.h b/frameworks/resmgr/include/system_resource_manager.h index 91243f7fa623420d1a7f06e86cfe6a0198169d9e..5da66df6474d4b3553da0bf056eb8d28eee777e3 100755 --- a/frameworks/resmgr/include/system_resource_manager.h +++ b/frameworks/resmgr/include/system_resource_manager.h @@ -55,6 +55,29 @@ public: */ static bool AddSystemResource(ResourceManagerImpl *resourceManager); + /** + * create system resource manager + * + * @return the new system resource manager + */ + EXPORT_FUNC static std::shared_ptr CreateSysResourceManager(); + + /** + * update system resource manager resconfig + * + * @param resConfig the current resconfig + * @param isThemeSystemResEnable if true means is theme system res enable + */ + static void UpdateSysResConfig(ResConfigImpl &resConfig, bool isThemeSystemResEnable); + +#if defined(__ARKUI_CROSS__) || defined(__IDE_PREVIEW__) + /** + * add system resource for preview + * + * @param resMgr the system resource manager + */ + static void AddSystemResourceForPreview(ResourceManagerImpl* resMgr); +#endif private: static std::mutex mutex_; @@ -79,7 +102,24 @@ private: static bool LoadSystemResource(ResourceManagerImpl *impl, bool isSandbox = true); static ResourceManagerImpl *CreateSystemResourceManager(bool isSandbox); + static std::shared_ptr CreateSystemResourceManager(); + + static void SaveResConfig(ResConfigImpl &resConfig, bool isThemeSystemResEnable); + + static void UpdateResConfig(ResConfigImpl &resConfig, bool isThemeSystemResEnable); + + static bool InitResourceManager(ResourceManagerImpl *impl); + + static std::mutex sysResMgrMutex_; + + static std::shared_ptr sysResMgr_; + + static std::shared_ptr resConfig_; + + static bool isUpdateAppConfig_; + + static bool isThemeSystemResEnable_; }; } // namespace Resource } // namespace Global diff --git a/frameworks/resmgr/src/hap_manager.cpp b/frameworks/resmgr/src/hap_manager.cpp index 4f38df75630ddb70204d3a13d74f08ee7c2114bc..23fd35186687b88a061c66e55fcc17efc4630877 100644 --- a/frameworks/resmgr/src/hap_manager.cpp +++ b/frameworks/resmgr/src/hap_manager.cpp @@ -445,6 +445,10 @@ bool HapManager::AddResource(const char *path, const uint32_t &selectedTypes, bo if (pResource->HasDarkRes()) { this->resConfig_->SetAppDarkRes(true); } + if (!pResource->IsSystemResource() && !pResource->IsOverlayResource() + && pResource->IsThemeSystemResEnable()) { + this->isThemeSystemResEnable_ = true; + } return true; } @@ -480,6 +484,10 @@ bool HapManager::AddResource(const std::string &path, const std::vectorHasDarkRes()) { this->resConfig_->SetAppDarkRes(true); } + if (!result[path]->IsSystemResource() && !result[path]->IsOverlayResource() + && result[path]->IsThemeSystemResEnable()) { + this->isThemeSystemResEnable_ = true; + } } for (auto iter = overlayPaths.rbegin(); iter != overlayPaths.rend(); iter++) { if (result.find(*iter) != result.end()) { @@ -1271,15 +1279,7 @@ RState HapManager::IsRawDirFromHap(const std::string &pathName, bool &outValue) bool HapManager::IsThemeSystemResEnableHap() { ReadLock lock(this->mutex_); - for (auto iter = hapResources_.begin(); iter != hapResources_.end(); iter++) { - if ((*iter)->IsSystemResource() || (*iter)->IsOverlayResource()) { - continue; - } - if ((*iter)->IsThemeSystemResEnable()) { - return true; - } - } - return false; + return this->isThemeSystemResEnable_; } bool HapManager::IsSystem() @@ -1291,6 +1291,16 @@ std::unordered_map> HapManager::GetLoadedH { return this->loadedHapPaths_; } + +void HapManager::UpdateAppConfigForSysResManager(bool isAppDarkRes, bool isThemeSystemResEnable) +{ + WriteLock lock(this->mutex_); + if (this->isSystem_ && this->isUpdateAppConfig_) { + this->isUpdateAppConfig_ = false; + this->resConfig_->SetAppDarkRes(isAppDarkRes); + this->isThemeSystemResEnable_ = isThemeSystemResEnable; + } +} } // namespace Resource } // namespace Global } // namespace OHOS diff --git a/frameworks/resmgr/src/res_config_impl.cpp b/frameworks/resmgr/src/res_config_impl.cpp index b76ad63c2076ee8b620580b0afe777de8a7dce0a..118d35374ccbb01613a3b00d06daca86653194cc 100644 --- a/frameworks/resmgr/src/res_config_impl.cpp +++ b/frameworks/resmgr/src/res_config_impl.cpp @@ -1036,6 +1036,13 @@ bool ResConfigImpl::GetAppDarkRes() const { return this->isAppDarkRes_; } + +bool ResConfigImpl::IsInvalidResConfig() +{ + return this->colorMode_ == LIGHT && this->direction_ == DIRECTION_NOT_SET + && this->density_ == SCREEN_DENSITY_NOT_SET + && this->screenDensityDpi_ == SCREEN_DENSITY_NOT_SET; +} } // namespace Resource } // namespace Global } // namespace OHOS \ No newline at end of file diff --git a/frameworks/resmgr/src/resource_manager_impl.cpp b/frameworks/resmgr/src/resource_manager_impl.cpp index bb54f3dc788b15ed97e35d3ff416c4bff95f4101..5f7205c1208b95436bf23914dedd6e4530855216 100644 --- a/frameworks/resmgr/src/resource_manager_impl.cpp +++ b/frameworks/resmgr/src/resource_manager_impl.cpp @@ -39,6 +39,7 @@ #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__) #include "parameter.h" #endif +#include "theme_pack_manager.h" namespace OHOS { namespace Global { @@ -116,6 +117,8 @@ bool ResourceManagerImpl::Init(std::shared_ptr hapManager) return false; } hapManager_->SetOverride(isOverrideResMgr_); + hapManager_->UpdateAppConfigForSysResManager( + resConfig->GetAppDarkRes(), hapManager->IsThemeSystemResEnableHap()); return true; } @@ -1421,19 +1424,21 @@ RState ResourceManagerImpl::UpdateResConfig(ResConfig &resConfig, bool isUpdateT if (state != SUCCESS) { return state; } -#if defined(__IDE_PREVIEW__) - UpdateSystemResourceResConfig(resConfig); -#endif - return this->hapManager_->UpdateResConfig(resConfig); + state = this->hapManager_->UpdateResConfig(resConfig); + UpdateSystemResourceResConfig(); + return state; } -#if defined(__IDE_PREVIEW__) -void ResourceManagerImpl::UpdateSystemResourceResConfig(ResConfig &resConfig) +void ResourceManagerImpl::UpdateSystemResourceResConfig() { if (isSystemResMgr_) { return; } + ResConfigImpl resConfig; + GetResConfig(resConfig); + SystemResourceManager::UpdateSysResConfig(resConfig, this->hapManager_->IsThemeSystemResEnableHap()); +#if defined(__IDE_PREVIEW__) ResourceManagerImpl* systemResourceManager = SystemResourceManager::GetSystemResourceManager(); if (systemResourceManager != nullptr) { ResConfigImpl sysResConfig; @@ -1441,8 +1446,8 @@ void ResourceManagerImpl::UpdateSystemResourceResConfig(ResConfig &resConfig) sysResConfig.SetDeviceType(resConfig.GetDeviceType()); systemResourceManager->GetHapManager()->UpdateResConfig(sysResConfig); } -} #endif +} RState ResourceManagerImpl::UpdateOverrideResConfig(ResConfig &resConfig) { diff --git a/frameworks/resmgr/src/system_resource_manager.cpp b/frameworks/resmgr/src/system_resource_manager.cpp index 6086ea7a85ebc8a1306b037ae7665335f6b75d2e..98bb21a2ad669d0e2e1055d4f28bc60b5448f1a0 100755 --- a/frameworks/resmgr/src/system_resource_manager.cpp +++ b/frameworks/resmgr/src/system_resource_manager.cpp @@ -46,6 +46,16 @@ std::weak_ptr SystemResourceManager::weakResourceManager_; std::mutex SystemResourceManager::mutex_; +std::shared_ptr SystemResourceManager::sysResMgr_ = nullptr; + +std::mutex SystemResourceManager::sysResMgrMutex_; + +std::shared_ptr SystemResourceManager::resConfig_ = nullptr; + +bool SystemResourceManager::isUpdateAppConfig_ = true; + +bool SystemResourceManager::isThemeSystemResEnable_ = false; + SystemResourceManager::SystemResourceManager() {} @@ -66,6 +76,23 @@ ResourceManagerImpl *SystemResourceManager::GetSystemResourceManagerNoSandBox() return CreateSystemResourceManager(false); } +bool SystemResourceManager::InitResourceManager(ResourceManagerImpl *impl) +{ + if (!impl->Init(true)) { + RESMGR_HILOGE(RESMGR_TAG, "init failed"); + return false; + } + std::shared_ptr sysResMgr = weakResourceManager_.lock(); + if (!sysResMgr) { + sysResMgr = CreateSystemResourceManager(); + } + if (!impl->AddSystemResource(sysResMgr)) { + RESMGR_HILOGE(RESMGR_TAG, "add system resource failed"); + return false; + } + return true; +} + ResourceManagerImpl *SystemResourceManager::CreateSystemResourceManager(bool isSandbox) { std::lock_guard lock(mutex_); @@ -75,15 +102,7 @@ ResourceManagerImpl *SystemResourceManager::CreateSystemResourceManager(bool isS RESMGR_HILOGE(RESMGR_TAG, "new ResourceManagerImpl failed when CreateSystemResourceManager"); return nullptr; } - if (!impl->Init(true)) { - delete impl; - return nullptr; - } - std::shared_ptr sysResMgr = weakResourceManager_.lock(); - if (!sysResMgr) { - sysResMgr = CreateSystemResourceManager(); - } - if (!impl->AddSystemResource(sysResMgr)) { + if (!InitResourceManager(impl)) { delete impl; return nullptr; } @@ -171,6 +190,87 @@ std::shared_ptr SystemResourceManager::CreateSystemResource weakResourceManager_ = sysResMgr; return sysResMgr; } + +std::shared_ptr SystemResourceManager::CreateSysResourceManager() +{ + std::lock_guard lock(sysResMgrMutex_); + if (sysResMgr_ != nullptr) { + return sysResMgr_; + } + sysResMgr_ = std::make_shared(); + if (sysResMgr_ == nullptr) { + RESMGR_HILOGE(RESMGR_TAG, "new ResourceManagerImpl failed when CreateSysResourceManager"); + return nullptr; + } + + if (!InitResourceManager(sysResMgr_.get())) { + RESMGR_HILOGE(RESMGR_TAG, "init sys resource manager failed"); + return nullptr; + } + +#if defined(__ARKUI_CROSS__) || defined(__IDE_PREVIEW__) + AddSystemResourceForPreview(resourceManager_); +#endif + if (resConfig_ != nullptr) { + UpdateResConfig(*resConfig_, isThemeSystemResEnable_); + } + return sysResMgr_; +} + +void SystemResourceManager::SaveResConfig(ResConfigImpl &resConfig, bool isThemeSystemResEnable) +{ + if (resConfig_ == nullptr) { + resConfig_ = std::make_shared(); + } + + if (resConfig_ == nullptr) { + return; + } + + resConfig_->Copy(resConfig, true); + if (isUpdateAppConfig_) { + isUpdateAppConfig_ = false; + isThemeSystemResEnable_ = isThemeSystemResEnable; + } +} + +void SystemResourceManager::UpdateResConfig(ResConfigImpl &resConfig, bool isThemeSystemResEnable) +{ + if (sysResMgr_ == nullptr) { + return; + } + auto hapManager = sysResMgr_->GetHapManager(); + if (hapManager == nullptr) { + RESMGR_HILOGE(RESMGR_TAG, "sys resource manager hapManager is null"); + return; + } + hapManager->UpdateResConfig(resConfig); + hapManager->UpdateAppConfigForSysResManager(resConfig.GetAppDarkRes(), isThemeSystemResEnable); +} + +void SystemResourceManager::UpdateSysResConfig(ResConfigImpl &resConfig, bool isThemeSystemResEnable) +{ + std::lock_guard lock(sysResMgrMutex_); + if (resConfig.IsInvalidResConfig()) { + return; + } + + if (sysResMgr_ == nullptr) { + SaveResConfig(resConfig, isThemeSystemResEnable); + return; + } + UpdateResConfig(resConfig, isThemeSystemResEnable); +} + +#if defined(__ARKUI_CROSS__) || defined(__IDE_PREVIEW__) +void SystemResourceManager::AddSystemResourceForPreview(ResourceManagerImpl* resMgr) +{ + if (resMgr == nullptr || sysResMgr_ == nullptr) { + return; + } + sysResMgr_->AddSystemResource(resMgr); +} +#endif } // namespace Resource } // namespace Global } // namespace OHOS diff --git a/frameworks/resmgr/test/unittest/common/system_resource_manager_test.cpp b/frameworks/resmgr/test/unittest/common/system_resource_manager_test.cpp index ac9e8c2ca84820d4b25aa74e7dbab50e6c075a9d..0a96f18278c999f5980abef47ac7a28f0bd4e110 100644 --- a/frameworks/resmgr/test/unittest/common/system_resource_manager_test.cpp +++ b/frameworks/resmgr/test/unittest/common/system_resource_manager_test.cpp @@ -40,7 +40,7 @@ void SystemResourceManagerTest::SetUp() void SystemResourceManagerTest::TearDown() {} -TEST_F(SystemResourceManagerTest, CreateSystemResourceTest) +HWTEST_F(SystemResourceManagerTest, CreateSystemResourceTest, TestSize.Level1) { { std::shared_ptr resMgr(CreateResourceManager()); @@ -49,13 +49,13 @@ TEST_F(SystemResourceManagerTest, CreateSystemResourceTest) EXPECT_EQ(SystemResourceManager::weakResourceManager_.use_count(), 0); } -TEST_F(SystemResourceManagerTest, CreateSystemResourceTest002) +HWTEST_F(SystemResourceManagerTest, CreateSystemResourceTest002, TestSize.Level1) { std::shared_ptr sysResMgr = std::make_shared(); EXPECT_TRUE(sysResMgr != nullptr); } -TEST_F(SystemResourceManagerTest, GetSystemResourceManagerNoSandBoxTest) +HWTEST_F(SystemResourceManagerTest, GetSystemResourceManagerNoSandBoxTest, TestSize.Level1) { { SystemResourceManager::GetSystemResourceManagerNoSandBox(); @@ -66,7 +66,7 @@ TEST_F(SystemResourceManagerTest, GetSystemResourceManagerNoSandBoxTest) EXPECT_EQ(SystemResourceManager::weakResourceManager_.use_count(), 0); } -TEST_F(SystemResourceManagerTest, DoubleCreateSystemResourceTest) +HWTEST_F(SystemResourceManagerTest, DoubleCreateSystemResourceTest, TestSize.Level1) { std::weak_ptr weakResource; { @@ -83,7 +83,7 @@ TEST_F(SystemResourceManagerTest, DoubleCreateSystemResourceTest) EXPECT_EQ(weakResource.use_count(), 0); } -TEST_F(SystemResourceManagerTest, SystemResourceCreateBeforeGet) +HWTEST_F(SystemResourceManagerTest, SystemResourceCreateBeforeGet, TestSize.Level1) { std::weak_ptr weakResource; { @@ -102,7 +102,7 @@ TEST_F(SystemResourceManagerTest, SystemResourceCreateBeforeGet) EXPECT_EQ(weakResource.use_count(), 0); } -TEST_F(SystemResourceManagerTest, SystemResourceCreateAfterGet) +HWTEST_F(SystemResourceManagerTest, SystemResourceCreateAfterGet, TestSize.Level1) { std::weak_ptr weakResource; { @@ -121,10 +121,126 @@ TEST_F(SystemResourceManagerTest, SystemResourceCreateAfterGet) EXPECT_EQ(weakResource.use_count(), 0); } -TEST_F(SystemResourceManagerTest, AddSystemResourceTest) +HWTEST_F(SystemResourceManagerTest, AddSystemResourceTest, TestSize.Level1) { ResourceManagerImpl *appResMgr = nullptr; bool result = SystemResourceManager::AddSystemResource(appResMgr); EXPECT_FALSE(result); } + +HWTEST_F(SystemResourceManagerTest, CreateSysResourceManagerTest001, TestSize.Level1) +{ + std::shared_ptr resMgr(CreateResourceManager()); + ASSERT_TRUE(resMgr != nullptr); + auto sysResMgr = SystemResourceManager::CreateSysResourceManager(); + ASSERT_TRUE(sysResMgr != nullptr); + bool ret = resMgr->AddResource(FormatFullPath(g_resFilePath).c_str()); + ASSERT_TRUE(ret); + ResConfigImpl rc; + rc.SetLocaleInfo("zh", nullptr, "CN"); + rc.SetColorMode(ColorMode::DARK); + resMgr->UpdateResConfig(rc); + std::string rmOutValue; + resMgr->GetStringByName("ohos_desc_write_calendar", rmOutValue); + std::string sysOutValue; + sysResMgr->GetStringByName("ohos_desc_write_calendar", sysOutValue); + EXPECT_EQ(rmOutValue, sysOutValue); +} + +HWTEST_F(SystemResourceManagerTest, CreateSysResourceManagerTest002, TestSize.Level1) +{ + std::shared_ptr resMgr(CreateResourceManager()); + ASSERT_TRUE(resMgr != nullptr); + auto sysResMgr = SystemResourceManager::CreateSysResourceManager(); + ASSERT_TRUE(sysResMgr != nullptr); + bool ret = resMgr->AddResource(FormatFullPath(g_resFilePath).c_str()); + ASSERT_TRUE(ret); + ResConfigImpl rc; + rc.SetLocaleInfo("en", nullptr, "US"); + rc.SetColorMode(ColorMode::DARK); + resMgr->UpdateResConfig(rc); + uint32_t rmOutValue; + resMgr->GetColorByName("ohos_id_color_foregroud", rmOutValue); + uint32_t sysOutValue; + sysResMgr->GetColorByName("ohos_id_color_foregroud", sysOutValue); + EXPECT_EQ(rmOutValue, sysOutValue); +} + +HWTEST_F(SystemResourceManagerTest, CreateSysResourceManagerTest003, TestSize.Level1) +{ + std::shared_ptr resMgr(CreateResourceManager()); + ASSERT_TRUE(resMgr != nullptr); + auto sysResMgr = SystemResourceManager::CreateSysResourceManager(); + ASSERT_TRUE(sysResMgr != nullptr); + bool ret = resMgr->AddResource(FormatFullPath(g_resFilePath).c_str()); + ASSERT_TRUE(ret); + ResConfigImpl rc; + rc.SetLocaleInfo("en", nullptr, "US"); + rc.SetScreenDensity(2.00); + rc.SetDeviceType(DeviceType::DEVICE_WEARABLE); + resMgr->UpdateResConfig(rc); + uint32_t rmOutValue; + resMgr->GetColorByName("ohos_id_color_text_primary_contrary", rmOutValue); + uint32_t sysOutValue; + sysResMgr->GetColorByName("ohos_id_color_text_primary_contrary", sysOutValue); + EXPECT_EQ(rmOutValue, sysOutValue); +} + +HWTEST_F(SystemResourceManagerTest, CreateSysResourceManagerTest004, TestSize.Level1) +{ + std::shared_ptr resMgr(CreateResourceManager()); + ASSERT_TRUE(resMgr != nullptr); + auto sysResMgr = SystemResourceManager::CreateSysResourceManager(); + ASSERT_TRUE(sysResMgr != nullptr); + bool ret = resMgr->AddResource(FormatFullPath(g_resFilePath).c_str()); + ASSERT_TRUE(ret); + ResConfigImpl rc; + rc.SetLocaleInfo("en", nullptr, "US"); + rc.SetScreenDensity(3.25); + resMgr->UpdateResConfig(rc); + float rmOutValue; + resMgr->GetFloatByName("ohos_fa_corner_radius_card", rmOutValue); + float sysOutValue; + sysResMgr->GetFloatByName("ohos_fa_corner_radius_card", sysOutValue); + EXPECT_EQ(rmOutValue, sysOutValue); +} + +HWTEST_F(SystemResourceManagerTest, CreateSysResourceManagerTest005, TestSize.Level1) +{ + std::shared_ptr resMgr(CreateResourceManager()); + ASSERT_TRUE(resMgr != nullptr); + auto sysResMgr = SystemResourceManager::CreateSysResourceManager(); + ASSERT_TRUE(sysResMgr != nullptr); + bool ret = resMgr->AddResource(FormatFullPath(g_resFilePath).c_str()); + ASSERT_TRUE(ret); + ResConfigImpl rc; + rc.SetLocaleInfo("en", nullptr, "US"); + rc.SetDirection(Direction::DIRECTION_VERTICAL); + resMgr->UpdateResConfig(rc); + uint32_t rmOutValue; + resMgr->GetSymbolByName("ohos_wifi", rmOutValue); + uint32_t sysOutValue; + sysResMgr->GetSymbolByName("ohos_wifi", sysOutValue); + EXPECT_EQ(rmOutValue, sysOutValue); +} + +HWTEST_F(SystemResourceManagerTest, CreateSysResourceManagerTest006, TestSize.Level1) +{ + std::shared_ptr resMgr(CreateResourceManager()); + ASSERT_TRUE(resMgr != nullptr); + auto sysResMgr = SystemResourceManager::CreateSysResourceManager(); + ASSERT_TRUE(sysResMgr != nullptr); + bool ret = resMgr->AddResource(FormatFullPath(g_resFilePath).c_str()); + ASSERT_TRUE(ret); + ResConfigImpl rc; + rc.SetLocaleInfo("en", nullptr, "US"); + rc.SetMcc(460); + rc.SetMnc(001); + resMgr->UpdateResConfig(rc); + int rmOutValue; + resMgr->GetIntegerByName("ohos_id_alpha_content_primary", rmOutValue); + int sysOutValue; + sysResMgr->GetIntegerByName("ohos_id_alpha_content_primary", sysOutValue); + EXPECT_EQ(rmOutValue, sysOutValue); +} } // namespace \ No newline at end of file diff --git a/interfaces/ets/ani/resourceManager/ets/@ohos.resourceManager.ets b/interfaces/ets/ani/resourceManager/ets/@ohos.resourceManager.ets index 9b3c293cd9b1011871435eda3bb6bb7f1c8a75d7..4d07f26197e091a833ff79a5bab8090e425b371d 100644 --- a/interfaces/ets/ani/resourceManager/ets/@ohos.resourceManager.ets +++ b/interfaces/ets/ani/resourceManager/ets/@ohos.resourceManager.ets @@ -717,7 +717,7 @@ export default namespace resourceManager { } } - export native function getSystemResourceManager(): ResourceManager; + export native function getSysResourceManager(): ResourceManager; export native function transferToDynamicResource(object: Object): Any; export native function transferToStaticResource(object: Any): Object; } diff --git a/interfaces/ets/ani/resourceManager/include/resourceManager.h b/interfaces/ets/ani/resourceManager/include/resourceManager.h index db5892cb62ae9493a75f73fc60f7407c3d116d24..4ff709887f1ac55fd0a0553e16d50629a66a391d 100644 --- a/interfaces/ets/ani/resourceManager/include/resourceManager.h +++ b/interfaces/ets/ani/resourceManager/include/resourceManager.h @@ -34,7 +34,7 @@ public: static ani_object CreateResMgr(ani_env* env, const std::string& bundleName, const std::shared_ptr& resMgr, std::shared_ptr context); - static ani_object GetSystemResourceManager(ani_env* env); + static ani_object GetSysResourceManager(ani_env* env); static ani_string GetStringSyncById(ani_env* env, ani_object object, ani_long resId); static ani_string GetFormatStringSyncById(ani_env *env, ani_object object, ani_long resId, ani_object args); diff --git a/interfaces/ets/ani/resourceManager/src/resourceManager.cpp b/interfaces/ets/ani/resourceManager/src/resourceManager.cpp index bcc9897dd277ce556984662fd81e91c2c491dccd..b924cd0ba96efffb69e6b7c7dde8374d78196591 100644 --- a/interfaces/ets/ani/resourceManager/src/resourceManager.cpp +++ b/interfaces/ets/ani/resourceManager/src/resourceManager.cpp @@ -22,6 +22,7 @@ #include "resource_manager_ani_utils.h" #include "resource_manager_data_context.h" #include "resource_manager.h" +#include "system_resource_manager.h" using namespace OHOS; using namespace Global; @@ -48,8 +49,6 @@ struct ArrayElement { ArrayElement(ElementType type, const std::string& str) : type(type), value(str) {} }; -static std::shared_ptr sysResMgr = nullptr; -static std::mutex sysMgrMutex; static std::array methods = { ani_native_function { "getStringSync", "J:Lstd/core/String;", reinterpret_cast(ResMgrAddon::GetStringSyncById) }, @@ -293,20 +292,9 @@ static ani_string CreateAniString(ani_env *env, ResMgrDataContext& context) return result; } -ani_object ResMgrAddon::GetSystemResourceManager(ani_env* env) +ani_object ResMgrAddon::GetSysResourceManager(ani_env* env) { - if (sysResMgr == nullptr) { - std::lock_guard lock(sysMgrMutex); - if (sysResMgr == nullptr) { - std::shared_ptr - systemResManager(Global::Resource::GetSystemResourceManager()); - if (systemResManager == nullptr) { - ResourceManagerAniUtils::AniThrow(env, ERROR_CODE_SYSTEM_RES_MANAGER_GET_FAILED); - return nullptr; - } - sysResMgr = systemResManager; - } - } + std::shared_ptr sysResMgr(SystemResourceManager::CreateSysResourceManager()); std::shared_ptr addon = std::make_shared(sysResMgr, true); return WrapResourceManager(env, addon); } @@ -1836,7 +1824,7 @@ ani_status ResMgrAddon::BindContext(ani_env* env) } std::array nsMethods = { - ani_native_function { "getSystemResourceManager", nullptr, reinterpret_cast(GetSystemResourceManager) }, + ani_native_function { "getSysResourceManager", nullptr, reinterpret_cast(GetSysResourceManager) }, ani_native_function{ "transferToDynamicResource", nullptr, reinterpret_cast(TransferToDynamicResource) }, ani_native_function{ "transferToStaticResource", nullptr, reinterpret_cast(TransferToStaticResource) }, diff --git a/interfaces/js/innerkits/core/BUILD.gn b/interfaces/js/innerkits/core/BUILD.gn index 4befa3b268002a2860c2585bc56c96eb64fbb8b5..b70e8de9153aabd7c7fae54cd6306255f9cf6e2b 100644 --- a/interfaces/js/innerkits/core/BUILD.gn +++ b/interfaces/js/innerkits/core/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2022 Huawei Device Co., Ltd. +# Copyright (c) 2022-2025 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -15,11 +15,11 @@ import("//build/ohos.gni") import("../../../../resmgr.gni") resmgr_napi_core_source = [ - "src/resource_manager_addon.cpp", - "src/resource_manager_napi_async_impl.cpp", - "src/resource_manager_napi_context.cpp", - "src/resource_manager_napi_sync_impl.cpp", - "src/resource_manager_napi_utils.cpp", + "src/resource_manager_addon.cpp", + "src/resource_manager_napi_async_impl.cpp", + "src/resource_manager_napi_context.cpp", + "src/resource_manager_napi_sync_impl.cpp", + "src/resource_manager_napi_utils.cpp", ] config("resmgr_napi_core_public_config_all") { @@ -42,9 +42,7 @@ config("resmgr_napi_core_public_config") { ohos_shared_library("resmgr_napi_core") { sources = resmgr_napi_core_source - sources += [ - "../../../../dfx/hisysevent_adapter/hisysevent_adapter.cpp", - ] + sources += [ "../../../../dfx/hisysevent_adapter/hisysevent_adapter.cpp" ] defines = [ "CONFIG_HILOG" ] @@ -60,6 +58,7 @@ ohos_shared_library("resmgr_napi_core") { "ace_engine:drawable_descriptor", "bounds_checking_function:libsec_shared", "bundle_framework:appexecfwk_base", + "cJSON:cjson", "c_utils:utils", "hilog:libhilog", "hisysevent:libhisysevent", @@ -94,9 +93,7 @@ group("resmgr_napi_core_preview") { ohos_shared_library("resmgr_napi_core_preview_inner") { if (is_mingw || is_mac || is_linux) { output_name = "resmgr_napi_core_preview" - defines = [ - "__IDE_PREVIEW__", - ] + defines = [ "__IDE_PREVIEW__" ] cflags = [ "-std=c++17", "-Wno-ignored-attributes", @@ -127,13 +124,14 @@ ohos_shared_library("resmgr_napi_core_preview_inner") { } external_deps = [ - "graphic_2d:librender_service_client", "ace_engine:drawable_descriptor", "bounds_checking_function:libsec_shared", + "cJSON:cjson_static", + "graphic_2d:librender_service_client", "hilog:libhilog", "napi:ace_napi", ] } subsystem_name = "global" part_name = "resource_management" -} \ No newline at end of file +} diff --git a/interfaces/js/innerkits/core/include/resource_manager_addon.h b/interfaces/js/innerkits/core/include/resource_manager_addon.h index a5d52105d9191febea88d11891759655b6c2876d..f38f4a6c616306e5120c28d995c1fd28a6b64bc0 100644 --- a/interfaces/js/innerkits/core/include/resource_manager_addon.h +++ b/interfaces/js/innerkits/core/include/resource_manager_addon.h @@ -51,6 +51,8 @@ public: static napi_value GetSystemResMgr(napi_env env); + static napi_value GetSysResourceManager(napi_env env); + static napi_value WrapResourceManager(napi_env env, std::shared_ptr &addon); ~ResourceManagerAddon(); diff --git a/interfaces/js/innerkits/core/src/resource_manager_addon.cpp b/interfaces/js/innerkits/core/src/resource_manager_addon.cpp index 1111c0465b63faa73a88b55bd2ee667df8e76092..45b0672d82fd1a01fc5e830ff86317cbbf1bec2b 100644 --- a/interfaces/js/innerkits/core/src/resource_manager_addon.cpp +++ b/interfaces/js/innerkits/core/src/resource_manager_addon.cpp @@ -16,6 +16,7 @@ #include "resource_manager_addon.h" #include "resource_manager_napi_utils.h" #include "hilog/log_cpp.h" +#include "system_resource_manager.h" namespace OHOS { namespace Global { @@ -92,6 +93,17 @@ napi_value ResourceManagerAddon::GetSystemResMgr(napi_env env) return WrapResourceManager(env, addon); } +napi_value ResourceManagerAddon::GetSysResourceManager(napi_env env) +{ + auto systemResManager = SystemResourceManager::CreateSysResourceManager(); + if (systemResManager == nullptr) { + ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_SYSTEM_RES_MANAGER_GET_FAILED); + return nullptr; + } + std::shared_ptr addon = std::make_shared(systemResManager, true); + return WrapResourceManager(env, addon); +} + ResourceManagerAddon::ResourceManagerAddon( const std::string& bundleName, const std::shared_ptr& resMgr, const std::shared_ptr& context, bool isSystem) diff --git a/interfaces/js/kits/src/resource_manager_napi.cpp b/interfaces/js/kits/src/resource_manager_napi.cpp index 99cfa1476d0a2717d689c5f89ff04715124db4ba..032327842d5a0d893d207aa10a3ca6d7eeffbe68 100644 --- a/interfaces/js/kits/src/resource_manager_napi.cpp +++ b/interfaces/js/kits/src/resource_manager_napi.cpp @@ -202,6 +202,11 @@ static napi_value GetSystemResourceManager(napi_env env, napi_callback_info info return ResourceManagerAddon::GetSystemResMgr(env); } +static napi_value GetSysResourceManager(napi_env env, napi_callback_info info) +{ + return ResourceManagerAddon::GetSysResourceManager(env); +} + static napi_status SetEnumItem(napi_env env, napi_value object, const char* name, int32_t value) { napi_status status; @@ -273,10 +278,12 @@ static napi_value ResMgrInit(napi_env env, napi_value exports) napi_property_descriptor creatorProp[] = { DECLARE_NAPI_FUNCTION("getResourceManager", GetResourceManager), DECLARE_NAPI_FUNCTION("getSystemResourceManager", GetSystemResourceManager), + DECLARE_NAPI_FUNCTION("getSysResourceManager", GetSysResourceManager), }; #else napi_property_descriptor creatorProp[] = { DECLARE_NAPI_FUNCTION("getSystemResourceManager", GetSystemResourceManager), + DECLARE_NAPI_FUNCTION("getSysResourceManager", GetSysResourceManager), }; #endif napi_status status = napi_define_properties(env, exports, sizeof(creatorProp) / sizeof(creatorProp[0]),