• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_ADAPTER_OHOS_OSAL_RESOURCE_ADAPTER_IMPL_V2_H
17 #define FOUNDATION_ACE_ADAPTER_OHOS_OSAL_RESOURCE_ADAPTER_IMPL_V2_H
18 
19 #include <mutex>
20 
21 #include "resource_manager.h"
22 #include "base/thread/task_executor.h"
23 #include "base/image/pixel_map.h"
24 #include "base/utils/device_config.h"
25 #include "base/utils/string_utils.h"
26 #include "core/components/theme/resource_adapter.h"
27 #include "adapter/ohos/osal/resource_theme_style.h"
28 
29 namespace OHOS::Ace {
30 class ResourceAdapterImplV2 : public ResourceAdapter {
31     DECLARE_ACE_TYPE(ResourceAdapterImplV2, ResourceAdapter);
32 
33 public:
34     ResourceAdapterImplV2() = default;
35     explicit ResourceAdapterImplV2(std::shared_ptr<Global::Resource::ResourceManager> resourceManager);
36     ResourceAdapterImplV2(std::shared_ptr<Global::Resource::ResourceManager> resourceManager, int32_t instanceId);
37     ResourceAdapterImplV2(
38         std::shared_ptr<Global::Resource::ResourceManager> resourceManager, const ResourceInfo& resourceInfo);
39     ~ResourceAdapterImplV2() override = default;
40 
41     void Init(const ResourceInfo& resourceInfo) override;
42     void UpdateConfig(const ResourceConfiguration& config, bool themeFlag = false) override;
43 
44     RefPtr<ThemeStyle> GetTheme(int32_t themeId) override;
45 
46     Color GetColor(uint32_t resId) override;
47     Color GetColorByName(const std::string& resName) override;
48     Dimension GetDimension(uint32_t resId) override;
49     Dimension GetDimensionByName(const std::string& resName) override;
50     std::string GetString(uint32_t resId) override;
51     std::string GetStringByName(const std::string& resName) override;
52     std::string GetPluralString(uint32_t resId, int quantity) override;
53     std::string GetPluralStringByName(const std::string& resName, int quantity) override;
54     std::vector<std::string> GetStringArray(uint32_t resId) const override;
55     std::vector<std::string> GetStringArrayByName(const std::string& resName) const override;
56     double GetDouble(uint32_t resId) override;
57     double GetDoubleByName(const std::string& resName) override;
58     int32_t GetInt(uint32_t resId) override;
59     int32_t GetIntByName(const std::string& resName) override;
60     std::vector<uint32_t> GetIntArray(uint32_t resId) const override;
61     std::vector<uint32_t> GetIntArrayByName(const std::string& resName) const override;
62     bool GetBoolean(uint32_t resId) const override;
63     bool GetBooleanByName(const std::string& resName) const override;
64     std::shared_ptr<Media::PixelMap> GetPixelMap(uint32_t resId) override;
65     std::string GetMediaPath(uint32_t resId) override;
66     std::string GetMediaPathByName(const std::string& resName) override;
67     std::string GetRawfile(const std::string& fileName) override;
68     bool GetRawFileData(const std::string& rawFile, size_t& len, std::unique_ptr<uint8_t[]>& dest) override;
69     bool GetRawFileData(const std::string& rawFile, size_t& len, std::unique_ptr<uint8_t[]>& dest,
70         const std::string& bundleName, const std::string& moduleName) override;
71     bool GetMediaData(uint32_t resId, size_t& len, std::unique_ptr<uint8_t[]>& dest) override;
72     bool GetMediaData(uint32_t resId, size_t& len, std::unique_ptr<uint8_t[]>& dest, const std::string& bundleName,
73         const std::string& moduleName) override;
74     bool GetMediaData(const std::string& resName, size_t& len, std::unique_ptr<uint8_t[]>& dest) override;
75     bool GetMediaData(const std::string& resName, size_t& len, std::unique_ptr<uint8_t[]>& dest,
76         const std::string& bundleName, const std::string& moduleName) override;
UpdateResourceManager(const std::string & bundleName,const std::string & moduleName)77     void UpdateResourceManager(const std::string& bundleName, const std::string& moduleName) override {};
78     bool GetRawFileDescription(const std::string& rawfileName, RawfileDescription& rawfileDescription) const override;
79     bool CloseRawFileDescription(const std::string &rawfileName) const override;
80     bool GetRawFD(const std::string& rawfileName, RawfileDescription& rawfileDescription) const override;
81     bool GetMediaById(const int32_t& resId, std::string& mediaPath) const override;
82     uint32_t GetResourceLimitKeys() const override;
83     uint32_t GetSymbolByName(const char* resName) const override;
84     uint32_t GetSymbolById(uint32_t resId) const override;
85     RefPtr<ThemeStyle> GetPatternByName(const std::string& patternName) override;
86     void UpdateColorMode(ColorMode colorMode) override;
87     ColorMode GetResourceColorMode() const override;
88     void SetAppHasDarkRes(bool hasDarkRes);
89     RefPtr<ResourceAdapter> GetOverrideResourceAdapter(
90         const ResourceConfiguration& config, const ConfigurationChange& configurationChange) override;
91     bool ExistDarkResById(const std::string& resourceId) override;
92     bool ExistDarkResByName(const std::string& resourceName, const std::string& resourceType) override;
93     uint32_t GetResId(const std::string &resTypeName) const override;
94 
95 private:
96     std::unordered_map<std::string, uint32_t> patternNameMap_;
97     std::string GetActualResourceName(const std::string& resName) const;
98     bool NeedUpdateResConfig(const std::shared_ptr<Global::Resource::ResConfig>& oldResConfig,
99         const std::shared_ptr<Global::Resource::ResConfig>& newResConfig);
100 
GetResourceManager()101     inline std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const
102     {
103         return sysResourceManager_;
104     }
105     RefPtr<TaskExecutor> GetTaskExecutor();
106 
107     std::shared_ptr<Global::Resource::ResourceManager> sysResourceManager_;
108     std::string packagePathStr_;
109     std::shared_ptr<Global::Resource::ResConfig> resConfig_;
110     bool appHasDarkRes_ = false;
111     std::mutex updateResConfigMutex_;
112     ACE_DISALLOW_COPY_AND_MOVE(ResourceAdapterImplV2);
113     void PreloadTheme(int32_t themeId, RefPtr<ResourceThemeStyle> theme);
114 };
115 } // namespace OHOS::Ace
116 
117 #endif
118