From 7cb92f475c6d78596f964f6e0703facc556c1272 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E6=99=BA=E5=AE=87?= Date: Mon, 2 Sep 2024 20:30:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81dpi=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E8=B5=84=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I269046d8e7aa064054915a9728684b4d1012e8f8 Signed-off-by: 谢智宇 --- frameworks/resmgr/BUILD.gn | 1 + frameworks/resmgr/include/hap_manager.h | 1 + frameworks/resmgr/include/hap_resource.h | 10 +++ .../resmgr/include/hap_resource_manager.h | 70 ++++++++++++++++ frameworks/resmgr/src/hap_manager.cpp | 80 ++++++++++++++----- frameworks/resmgr/src/hap_resource.cpp | 8 ++ .../resmgr/src/hap_resource_manager.cpp | 74 +++++++++++++++++ 7 files changed, 226 insertions(+), 18 deletions(-) create mode 100644 frameworks/resmgr/include/hap_resource_manager.h create mode 100644 frameworks/resmgr/src/hap_resource_manager.cpp diff --git a/frameworks/resmgr/BUILD.gn b/frameworks/resmgr/BUILD.gn index 451d788..12ea2c6 100644 --- a/frameworks/resmgr/BUILD.gn +++ b/frameworks/resmgr/BUILD.gn @@ -34,6 +34,7 @@ config("resmgr_config") { manager_sources = [ "src/hap_manager.cpp", "src/hap_resource.cpp", + "src/hap_resource_manager.cpp", "src/locale_matcher.cpp", "src/lock.cpp", "src/res_config_impl.cpp", diff --git a/frameworks/resmgr/include/hap_manager.h b/frameworks/resmgr/include/hap_manager.h index 9aaa56f..c2a1cc8 100644 --- a/frameworks/resmgr/include/hap_manager.h +++ b/frameworks/resmgr/include/hap_manager.h @@ -17,6 +17,7 @@ #include "res_config_impl.h" #include "hap_resource.h" +#include "hap_resource_manager.h" #include "res_desc.h" #include "resource_manager.h" #include "lock.h" diff --git a/frameworks/resmgr/include/hap_resource.h b/frameworks/resmgr/include/hap_resource.h index 6e7956f..d1e6d55 100644 --- a/frameworks/resmgr/include/hap_resource.h +++ b/frameworks/resmgr/include/hap_resource.h @@ -329,6 +329,13 @@ public: bool isSystem = false, bool isOverlay = false); bool IsThemeSystemResEnable() const; + + /** + * update dark mode resconfig + * + * @param defaultConfig default config + */ + void UpdateDarkConfig(std::shared_ptr &defaultConfig); private: void UpdateOverlayInfo(std::unordered_map> &nameTypeId); @@ -382,6 +389,9 @@ private: // judge the hqf is enabled or not. bool isPatch_ = false; + + // judge the resource is adapt dark mode or not. + bool hasDarkRes_ = false; }; } // namespace Resource } // namespace Global diff --git a/frameworks/resmgr/include/hap_resource_manager.h b/frameworks/resmgr/include/hap_resource_manager.h new file mode 100644 index 0000000..a974ada --- /dev/null +++ b/frameworks/resmgr/include/hap_resource_manager.h @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2024-2024 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef OHOS_RESOURCE_MANAGER_HAPRESOURCEMANAGER_H +#define OHOS_RESOURCE_MANAGER_HAPRESOURCEMANAGER_H + +#include +#include "hap_resource.h" +#include "res_config_impl.h" +#include "res_desc.h" +#include "resource_manager.h" + +#ifdef SUPPORT_GRAPHICS +#include +#endif + +namespace OHOS { +namespace Global { +namespace Resource { +class HapResourceManager { +public: + HapResourceManager() {} + ~HapResourceManager() {} + + static std::shared_ptr GetInstance(); + + /** + * Put hapResource into the map of HapResourceManager + * + * @param path the resources.index file path + * @param hapResource hapResource + * @return th resource + */ + std::shared_ptr PutAndGetResource(const std::string path, std::shared_ptr hapResource); + + /** + * Put patch resource + * + * @return Whether patch resource is put successfully + */ + bool PutPatchResource(const std::string path, std::string patchPath); + + /** + * get resource from map + * + * @return the resource + */ + std::shared_ptr getHapResource(const std::string path); + +private: + static std::recursive_mutex mutex_; + static std::shared_ptr instance_; + std::shared_mutex mutexRw_; + std::unordered_map> hapResourceMap_; +}; +} // namespace Resource +} // namespace Global +} // namespace OHOS +#endif \ No newline at end of file diff --git a/frameworks/resmgr/src/hap_manager.cpp b/frameworks/resmgr/src/hap_manager.cpp index a4f4a5e..42e61d7 100644 --- a/frameworks/resmgr/src/hap_manager.cpp +++ b/frameworks/resmgr/src/hap_manager.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #include "hap_manager.h" +#include "hap_resource_manager.h" #include #include @@ -386,16 +387,50 @@ bool HapManager::AddResource(const std::string &path, const std::vector> result = HapResource::LoadOverlays(path, overlayPaths, - resConfig_, isSystem_); - if (result.size() > 0) { - for (auto iter = result.begin(); iter != result.end(); iter++) { - this->hapResources_.push_back(iter->second); + + std::vector tempOverlays; + bool isPathLoad = false; + std::shared_ptr hapResource = HapResourceManager::GetInstance()->getHapResource(path); + if (hapResource) { + hapResources_.push_back(hapResource); + hapResource->UpdateDarkConfig(this->resConfig_); + isPathLoad = true; + } + for (const std::string& overlayPath : overlayPaths) { + std::shared_ptr overlayResource = HapResourceManager::GetInstance()->getHapResource(overlayPath); + if (overlayResource) { + hapResources_.push_back(overlayResource); + overlayResource->UpdateDarkConfig(this->resConfig_); + } else { + tempOverlays.push_back(overlayPath); } - return true; } - return false; + + std::unordered_map> result; + if (!isPathLoad || tempOverlays.size() > 0) { + result = HapResource::LoadOverlays(path, tempOverlays, resConfig_, isSystem_); + if (result.size() == 0) { + return false; + } + } + + if (!isPathLoad && result.find(path) != result.end()) { + std::shared_ptr pResource = + HapResourceManager::GetInstance()->PutAndGetResource(path, result[path]); + hapResources_.push_back(pResource); + result[path]->UpdateDarkConfig(this->resConfig_); + } + + for (auto iter = result.begin(); iter != result.end(); iter++) { + if (iter->second->IsOverlayResource()) { + std::shared_ptr pResource = + HapResourceManager::GetInstance()->PutAndGetResource(iter->first, iter->second); + hapResources_.push_back(pResource); + iter->second->UpdateDarkConfig(this->resConfig_); + } + } + loadedHapPaths_[path] = overlayPaths; + return true; } std::string HapManager::GetValidAppPath() @@ -492,12 +527,28 @@ bool HapManager::AddResourcePath(const char *path, const uint32_t &selectedTypes if (it != loadedHapPaths_.end()) { return false; } - const std::shared_ptr pResource = HapResource::Load(path, resConfig_, isSystem_, false, selectedTypes); + if (selectedTypes == SELECT_ALL) { + std::shared_ptr hapResource = HapResourceManager::GetInstance()->getHapResource(sPath); + if (hapResource) { + RESMGR_HILOGD(RESMGR_TAG, "resource is loaded, path is %{public}s", sPath.c_str()); + hapResources_.push_back(hapResource); + hapResource->UpdateDarkConfig(this->resConfig_); + this->loadedHapPaths_[sPath] = std::vector(); + return true; + } + } + + std::shared_ptr pResource = HapResource::Load(path, resConfig_, isSystem_, false, selectedTypes); if (pResource == nullptr) { return false; } - this->hapResources_.push_back(pResource); + if (selectedTypes == SELECT_ALL) { + pResource = HapResourceManager::GetInstance()->PutAndGetResource(sPath, pResource); + } this->loadedHapPaths_[sPath] = std::vector(); + this->hapResources_.push_back(pResource); + pResource->UpdateDarkConfig(this->resConfig_); + return true; } @@ -510,14 +561,7 @@ bool HapManager::AddPatchResourcePath(const char *path, const char *patchPath) return false; } std::string sPatchPath(patchPath); - for (auto iter = hapResources_.begin(); iter != hapResources_.end(); iter++) { - if ((*iter)->GetIndexPath() == sPath) { - (*iter)->SetPatchPath(sPatchPath); - (*iter)->SetIsPatch(true); - return true; - } - } - return false; + return HapResourceManager::GetInstance()->PutPatchResource(sPath, sPatchPath); } RState HapManager::ReloadAll() diff --git a/frameworks/resmgr/src/hap_resource.cpp b/frameworks/resmgr/src/hap_resource.cpp index eaa908d..47705c8 100644 --- a/frameworks/resmgr/src/hap_resource.cpp +++ b/frameworks/resmgr/src/hap_resource.cpp @@ -533,6 +533,14 @@ void HapResource::IsAppDarkRes(const std::shared_ptrGetFolder(); if (folder.find("dark") != std::string::npos) { defaultConfig->SetAppDarkRes(true); + hasDarkRes_ = true; + } +} + +void HapResource::UpdateDarkConfig(std::shared_ptr &defaultConfig) +{ + if (hasDarkRes_) { + defaultConfig->SetAppDarkRes(hasDarkRes_); } } } // namespace Resource diff --git a/frameworks/resmgr/src/hap_resource_manager.cpp b/frameworks/resmgr/src/hap_resource_manager.cpp new file mode 100644 index 0000000..b83af37 --- /dev/null +++ b/frameworks/resmgr/src/hap_resource_manager.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2024-2024 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "hap_resource_manager.h" +#include "hilog_wrapper.h" + +namespace OHOS { +namespace Global { +namespace Resource { +std::shared_ptr HapResourceManager::instance_ = nullptr; +std::recursive_mutex HapResourceManager::mutex_; + +std::shared_ptr HapResourceManager::GetInstance() +{ + if (instance_ == nullptr) { + std::lock_guard lock(mutex_); + if (instance_ == nullptr) { + instance_ = std::make_shared(); + } + } + return instance_; +} + +std::shared_ptr HapResourceManager::PutAndGetResource(const std::string path, + std::shared_ptr pResource) +{ + std::unique_lock lock(mutexRw_); + auto iter = hapResourceMap_.find(path); + if (iter == hapResourceMap_.end() || !iter->second.lock()) { + hapResourceMap_[path] = pResource; + return pResource; + } else { + return iter->second.lock(); + } +} + +bool HapResourceManager::PutPatchResource(const std::string path, std::string patchPath) +{ + std::unique_lock lock(mutexRw_); + if (hapResourceMap_.find(path) != hapResourceMap_.end()) { + std::shared_ptr hapResource = hapResourceMap_[path].lock(); + if (hapResource) { + hapResource->SetPatchPath(patchPath); + hapResource->SetIsPatch(true); + return true; + } + } + return false; +} + +std::shared_ptr HapResourceManager::getHapResource(const std::string path) +{ + std::shared_lock lock(mutexRw_); + auto iter = hapResourceMap_.find(path); + if (iter != hapResourceMap_.end()) { + return iter->second.lock(); + } + return nullptr; +} +} // namespace Resource +} // namespace Global +} // namespace OHOS \ No newline at end of file -- Gitee