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 "core/components/theme/resource_adapter.h"
26 #include "adapter/ohos/osal/resource_theme_style.h"
27 
28 namespace OHOS::Ace {
29 class ResourceAdapterImplV2 : public ResourceAdapter {
30     DECLARE_ACE_TYPE(ResourceAdapterImplV2, ResourceAdapter);
31 
32 public:
33     ResourceAdapterImplV2() = default;
34     explicit ResourceAdapterImplV2(std::shared_ptr<Global::Resource::ResourceManager> resourceManager);
35     ResourceAdapterImplV2(
36         std::shared_ptr<Global::Resource::ResourceManager> resourceManager, const ResourceInfo& resourceInfo);
37     ~ResourceAdapterImplV2() override = default;
38 
39     void Init(const ResourceInfo& resourceInfo) override;
40     void UpdateConfig(const ResourceConfiguration& config, bool themeFlag = false) override;
41 
42     RefPtr<ThemeStyle> GetTheme(int32_t themeId) override;
43 
44     Color GetColor(uint32_t resId) override;
45     Color GetColorByName(const std::string& resName) override;
46     Dimension GetDimension(uint32_t resId) override;
47     Dimension GetDimensionByName(const std::string& resName) override;
48     std::string GetString(uint32_t resId) override;
49     std::string GetStringByName(const std::string& resName) override;
50     std::string GetPluralString(uint32_t resId, int quantity) override;
51     std::string GetPluralStringByName(const std::string& resName, int quantity) override;
52     std::vector<std::string> GetStringArray(uint32_t resId) const override;
53     std::vector<std::string> GetStringArrayByName(const std::string& resName) const override;
54     double GetDouble(uint32_t resId) override;
55     double GetDoubleByName(const std::string& resName) override;
56     int32_t GetInt(uint32_t resId) override;
57     int32_t GetIntByName(const std::string& resName) override;
58     std::vector<uint32_t> GetIntArray(uint32_t resId) const override;
59     std::vector<uint32_t> GetIntArrayByName(const std::string& resName) const override;
60     bool GetBoolean(uint32_t resId) const override;
61     bool GetBooleanByName(const std::string& resName) const override;
62     std::shared_ptr<Media::PixelMap> GetPixelMap(uint32_t resId) override;
63     std::string GetMediaPath(uint32_t resId) override;
64     std::string GetMediaPathByName(const std::string& resName) override;
65     std::string GetRawfile(const std::string& fileName) override;
66     bool GetRawFileData(const std::string& rawFile, size_t& len, std::unique_ptr<uint8_t[]>& dest) override;
67     bool GetRawFileData(const std::string& rawFile, size_t& len, std::unique_ptr<uint8_t[]>& dest,
68         const std::string& bundleName, const std::string& moduleName) override;
69     bool GetMediaData(uint32_t resId, size_t& len, std::unique_ptr<uint8_t[]>& dest) override;
70     bool GetMediaData(uint32_t resId, size_t& len, std::unique_ptr<uint8_t[]>& dest, const std::string& bundleName,
71         const std::string& moduleName) override;
72     bool GetMediaData(const std::string& resName, size_t& len, std::unique_ptr<uint8_t[]>& dest) override;
73     bool GetMediaData(const std::string& resName, size_t& len, std::unique_ptr<uint8_t[]>& dest,
74         const std::string& bundleName, const std::string& moduleName) override;
UpdateResourceManager(const std::string & bundleName,const std::string & moduleName)75     void UpdateResourceManager(const std::string& bundleName, const std::string& moduleName) override {};
76     bool GetRawFileDescription(const std::string& rawfileName, RawfileDescription& rawfileDescription) const override;
77     bool CloseRawFileDescription(const std::string &rawfileName) const override;
78     bool GetMediaById(const int32_t& resId, std::string& mediaPath) const override;
79     uint32_t GetResourceLimitKeys() const override;
80     uint32_t GetSymbolByName(const char* resName) const override;
81     uint32_t GetSymbolById(uint32_t resId) const override;
82     RefPtr<ThemeStyle> GetPatternByName(const std::string& patternName) override;
83     void UpdateColorMode(ColorMode colorMode) override;
84     ColorMode GetResourceColorMode() const override;
85     void SetAppHasDarkRes(bool hasDarkRes);
86     RefPtr<ResourceAdapter> GetOverrideResourceAdapter(
87         const ResourceConfiguration& config, const ConfigurationChange& configurationChange) override;
88 
89 private:
90     std::string GetActualResourceName(const std::string& resName) const;
91     bool NeedUpdateResConfig(const std::shared_ptr<Global::Resource::ResConfig>& oldResConfig,
92         const std::shared_ptr<Global::Resource::ResConfig>& newResConfig);
93 
GetResourceManager()94     inline std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const
95     {
96         return sysResourceManager_;
97     }
98     RefPtr<TaskExecutor> GetTaskExecutor();
99 
100     std::shared_ptr<Global::Resource::ResourceManager> sysResourceManager_;
101     std::string packagePathStr_;
102     std::shared_ptr<Global::Resource::ResConfig> resConfig_;
103     bool appHasDarkRes_ = false;
104     std::mutex updateResConfigMutex_;
105     ACE_DISALLOW_COPY_AND_MOVE(ResourceAdapterImplV2);
106     void PreloadTheme(int32_t themeId, RefPtr<ResourceThemeStyle> theme);
107 };
108 } // namespace OHOS::Ace
109 
110 #endif
111