1 /*
2  * Copyright (c) 2023-2024 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_FRAMEWORKS_CORE_COMMON_RESOURCE_RESOURCE_WRAPPER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_RESOURCE_RESOURCE_WRAPPER_H
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "base/geometry/dimension.h"
24 #include "base/image/pixel_map.h"
25 #include "base/memory/ace_type.h"
26 #include "base/memory/referenced.h"
27 #include "base/utils/device_config.h"
28 #include "base/utils/system_properties.h"
29 #include "core/components/common/properties/color.h"
30 #include "core/components/theme/resource_adapter.h"
31 #include "core/components/theme/theme_constants.h"
32 
33 namespace OHOS::Ace {
34 class ACE_FORCE_EXPORT ResourceWrapper : public AceType {
35     DECLARE_ACE_TYPE(ResourceWrapper, AceType);
36 
37 public:
ResourceWrapper(RefPtr<ThemeConstants> & themeConstants,RefPtr<ResourceAdapter> & resourceAdapter)38     ResourceWrapper(RefPtr<ThemeConstants>& themeConstants, RefPtr<ResourceAdapter>& resourceAdapter)
39         : themeConstants_(themeConstants), resourceAdapter_(resourceAdapter)
40     {
41         if (!themeConstants_ && !resourceAdapter_) {
42             TAG_LOGW(AceLogTag::ACE_RESOURCE, "ThemeConstants and ResourceAdapter are both nullptr.");
43         }
44     }
45 
46     ResourceWrapper(
47         RefPtr<ThemeConstants>& themeConstants, RefPtr<ResourceAdapter>& resourceAdapter, ColorMode colorMode);
48     ~ResourceWrapper() override;
49 
GetColor(uint32_t key)50     Color GetColor(uint32_t key) const
51     {
52         if (SystemProperties::GetResourceDecoupling()) {
53             CHECK_NULL_RETURN(resourceAdapter_, Color());
54             return resourceAdapter_->GetColor(key);
55         }
56         CHECK_NULL_RETURN(themeConstants_, Color());
57         return themeConstants_->GetColor(key);
58     }
59 
GetColorByName(const std::string & resName)60     Color GetColorByName(const std::string& resName) const
61     {
62         if (SystemProperties::GetResourceDecoupling()) {
63             CHECK_NULL_RETURN(resourceAdapter_, Color());
64             return resourceAdapter_->GetColorByName(resName);
65         }
66         CHECK_NULL_RETURN(themeConstants_, Color());
67         return themeConstants_->GetColorByName(resName);
68     }
69 
GetDimension(uint32_t key)70     Dimension GetDimension(uint32_t key) const
71     {
72         if (SystemProperties::GetResourceDecoupling()) {
73             CHECK_NULL_RETURN(resourceAdapter_, Dimension());
74             return resourceAdapter_->GetDimension(key);
75         }
76         CHECK_NULL_RETURN(themeConstants_, Dimension());
77         return themeConstants_->GetDimension(key);
78     }
79 
GetDimensionByName(const std::string & resName)80     Dimension GetDimensionByName(const std::string& resName) const
81     {
82         if (SystemProperties::GetResourceDecoupling()) {
83             CHECK_NULL_RETURN(resourceAdapter_, Dimension());
84             return resourceAdapter_->GetDimensionByName(resName);
85         }
86         CHECK_NULL_RETURN(themeConstants_, Dimension());
87         return themeConstants_->GetDimensionByName(resName);
88     }
89 
GetInt(uint32_t key)90     int32_t GetInt(uint32_t key) const
91     {
92         if (SystemProperties::GetResourceDecoupling()) {
93             CHECK_NULL_RETURN(resourceAdapter_, 0);
94             return resourceAdapter_->GetInt(key);
95         }
96         CHECK_NULL_RETURN(themeConstants_, 0);
97         return themeConstants_->GetInt(key);
98     }
99 
GetIntByName(const std::string & resName)100     int32_t GetIntByName(const std::string& resName) const
101     {
102         if (SystemProperties::GetResourceDecoupling()) {
103             CHECK_NULL_RETURN(resourceAdapter_, 0);
104             return resourceAdapter_->GetIntByName(resName);
105         }
106         CHECK_NULL_RETURN(themeConstants_, 0);
107         return themeConstants_->GetIntByName(resName);
108     }
109 
GetDouble(uint32_t key)110     double GetDouble(uint32_t key) const
111     {
112         if (SystemProperties::GetResourceDecoupling()) {
113             CHECK_NULL_RETURN(resourceAdapter_, 0.0f);
114             return resourceAdapter_->GetDouble(key);
115         }
116         CHECK_NULL_RETURN(themeConstants_, 0.0f);
117         return themeConstants_->GetDouble(key);
118     }
119 
GetDoubleByName(const std::string & resName)120     double GetDoubleByName(const std::string& resName) const
121     {
122         if (SystemProperties::GetResourceDecoupling()) {
123             CHECK_NULL_RETURN(resourceAdapter_, 0.0f);
124             return resourceAdapter_->GetDoubleByName(resName);
125         }
126         CHECK_NULL_RETURN(themeConstants_, 0.0f);
127         return themeConstants_->GetDoubleByName(resName);
128     }
129 
GetString(uint32_t key)130     std::string GetString(uint32_t key) const
131     {
132         if (SystemProperties::GetResourceDecoupling()) {
133             CHECK_NULL_RETURN(resourceAdapter_, "");
134             return resourceAdapter_->GetString(key);
135         }
136         CHECK_NULL_RETURN(themeConstants_, "");
137         return themeConstants_->GetString(key);
138     }
139 
GetStringByName(const std::string & resName)140     std::string GetStringByName(const std::string& resName) const
141     {
142         if (SystemProperties::GetResourceDecoupling()) {
143             CHECK_NULL_RETURN(resourceAdapter_, "");
144             return resourceAdapter_->GetStringByName(resName);
145         }
146         CHECK_NULL_RETURN(themeConstants_, "");
147         return themeConstants_->GetStringByName(resName);
148     }
149 
GetPluralString(uint32_t key,int count)150     std::string GetPluralString(uint32_t key, int count) const
151     {
152         if (SystemProperties::GetResourceDecoupling()) {
153             CHECK_NULL_RETURN(resourceAdapter_, "");
154             return resourceAdapter_->GetPluralString(key, count);
155         }
156         CHECK_NULL_RETURN(themeConstants_, "");
157         return themeConstants_->GetPluralString(key, count);
158     }
159 
GetPluralStringByName(const std::string & resName,int count)160     std::string GetPluralStringByName(const std::string& resName, int count) const
161     {
162         if (SystemProperties::GetResourceDecoupling()) {
163             CHECK_NULL_RETURN(resourceAdapter_, "");
164             return resourceAdapter_->GetPluralStringByName(resName, count);
165         }
166         CHECK_NULL_RETURN(themeConstants_, "");
167         return themeConstants_->GetPluralStringByName(resName, count);
168     }
169 
GetBoolean(uint32_t key)170     bool GetBoolean(uint32_t key) const
171     {
172         if (SystemProperties::GetResourceDecoupling()) {
173             CHECK_NULL_RETURN(resourceAdapter_, false);
174             return resourceAdapter_->GetBoolean(key);
175         }
176         CHECK_NULL_RETURN(themeConstants_, false);
177         return themeConstants_->GetBoolean(key);
178     }
179 
GetBooleanByName(const std::string & resName)180     bool GetBooleanByName(const std::string& resName) const
181     {
182         if (SystemProperties::GetResourceDecoupling()) {
183             CHECK_NULL_RETURN(resourceAdapter_, false);
184             return resourceAdapter_->GetBooleanByName(resName);
185         }
186         CHECK_NULL_RETURN(themeConstants_, false);
187         return themeConstants_->GetBooleanByName(resName);
188     }
189 
GetIntArray(uint32_t key)190     std::vector<uint32_t> GetIntArray(uint32_t key) const
191     {
192         if (SystemProperties::GetResourceDecoupling()) {
193             CHECK_NULL_RETURN(resourceAdapter_, std::vector<uint32_t>());
194             return resourceAdapter_->GetIntArray(key);
195         }
196         CHECK_NULL_RETURN(themeConstants_, std::vector<uint32_t>());
197         return themeConstants_->GetIntArray(key);
198     }
199 
GetIntArrayByName(const std::string & resName)200     std::vector<uint32_t> GetIntArrayByName(const std::string& resName) const
201     {
202         if (SystemProperties::GetResourceDecoupling()) {
203             CHECK_NULL_RETURN(resourceAdapter_, std::vector<uint32_t>());
204             return resourceAdapter_->GetIntArrayByName(resName);
205         }
206         CHECK_NULL_RETURN(themeConstants_, std::vector<uint32_t>());
207         return themeConstants_->GetIntArrayByName(resName);
208     }
209 
GetPixelMap(uint32_t key)210     std::shared_ptr<Media::PixelMap> GetPixelMap(uint32_t key) const
211     {
212         if (SystemProperties::GetResourceDecoupling()) {
213             CHECK_NULL_RETURN(resourceAdapter_, nullptr);
214             return resourceAdapter_->GetPixelMap(key);
215         }
216         CHECK_NULL_RETURN(themeConstants_, nullptr);
217         return themeConstants_->GetPixelMap(key);
218     }
219 
GetStringArray(uint32_t key)220     std::vector<std::string> GetStringArray(uint32_t key) const
221     {
222         if (SystemProperties::GetResourceDecoupling()) {
223             CHECK_NULL_RETURN(resourceAdapter_, std::vector<std::string>());
224             return resourceAdapter_->GetStringArray(key);
225         }
226         CHECK_NULL_RETURN(themeConstants_, std::vector<std::string>());
227         return themeConstants_->GetStringArray(key);
228     }
229 
GetStringArrayByName(const std::string & resName)230     std::vector<std::string> GetStringArrayByName(const std::string& resName) const
231     {
232         if (SystemProperties::GetResourceDecoupling()) {
233             CHECK_NULL_RETURN(resourceAdapter_, std::vector<std::string>());
234             return resourceAdapter_->GetStringArrayByName(resName);
235         }
236         CHECK_NULL_RETURN(themeConstants_, std::vector<std::string>());
237         return themeConstants_->GetStringArrayByName(resName);
238     }
239 
GetMediaPath(uint32_t key)240     std::string GetMediaPath(uint32_t key) const
241     {
242         if (SystemProperties::GetResourceDecoupling()) {
243             CHECK_NULL_RETURN(resourceAdapter_, "");
244             return resourceAdapter_->GetMediaPath(key);
245         }
246         CHECK_NULL_RETURN(themeConstants_, "");
247         return themeConstants_->GetMediaPath(key);
248     }
249 
GetMediaPathByName(const std::string & resName)250     std::string GetMediaPathByName(const std::string& resName) const
251     {
252         if (SystemProperties::GetResourceDecoupling()) {
253             CHECK_NULL_RETURN(resourceAdapter_, "");
254             return resourceAdapter_->GetMediaPathByName(resName);
255         }
256         CHECK_NULL_RETURN(themeConstants_, "");
257         return themeConstants_->GetMediaPathByName(resName);
258     }
259 
GetRawfile(const std::string & fileName)260     std::string GetRawfile(const std::string& fileName) const
261     {
262         if (SystemProperties::GetResourceDecoupling()) {
263             CHECK_NULL_RETURN(resourceAdapter_, "");
264             return resourceAdapter_->GetRawfile(fileName);
265         }
266         CHECK_NULL_RETURN(themeConstants_, "");
267         return themeConstants_->GetRawfile(fileName);
268     }
269 
GetRawFileDescription(const std::string & rawfileName,RawfileDescription & rawfileDescription)270     bool GetRawFileDescription(const std::string& rawfileName, RawfileDescription& rawfileDescription) const
271     {
272         if (SystemProperties::GetResourceDecoupling()) {
273             CHECK_NULL_RETURN(resourceAdapter_, false);
274             return resourceAdapter_->GetRawFileDescription(rawfileName, rawfileDescription);
275         }
276         CHECK_NULL_RETURN(themeConstants_, false);
277         return themeConstants_->GetRawFileDescription(rawfileName, rawfileDescription);
278     }
279 
CloseRawFileDescription(const std::string & rawfileName)280     bool CloseRawFileDescription(const std::string& rawfileName) const
281     {
282         if (SystemProperties::GetResourceDecoupling()) {
283             CHECK_NULL_RETURN(resourceAdapter_, false);
284             return resourceAdapter_->CloseRawFileDescription(rawfileName);
285         }
286         CHECK_NULL_RETURN(themeConstants_, false);
287         return themeConstants_->CloseRawFileDescription(rawfileName);
288     }
289 
GetMediaById(const int32_t & resId,std::string & mediaPath)290     bool GetMediaById(const int32_t& resId, std::string& mediaPath) const
291     {
292         if (SystemProperties::GetResourceDecoupling()) {
293             CHECK_NULL_RETURN(resourceAdapter_, false);
294             return resourceAdapter_->GetMediaById(resId, mediaPath);
295         }
296         CHECK_NULL_RETURN(themeConstants_, false);
297         return themeConstants_->GetMediaById(resId, mediaPath);
298     }
299 
300     template<class T>
GetMediaResource(T & resId,std::ostream & dest)301     bool GetMediaResource(T& resId, std::ostream& dest) const
302     {
303         if (SystemProperties::GetResourceDecoupling()) {
304             CHECK_NULL_RETURN(resourceAdapter_, false);
305             return resourceAdapter_->GetResource(resId, dest);
306         }
307         CHECK_NULL_RETURN(themeConstants_, false);
308         return themeConstants_->GetMediaResource<T>(resId, dest);
309     }
310 
311     template<class T>
GetMediaData(T & resId,size_t & len,std::unique_ptr<uint8_t[]> & dest)312     bool GetMediaData(T& resId, size_t& len, std::unique_ptr<uint8_t[]>& dest)
313     {
314         if (SystemProperties::GetResourceDecoupling()) {
315             CHECK_NULL_RETURN(resourceAdapter_, false);
316             return resourceAdapter_->GetMediaData(resId, len, dest);
317         }
318         CHECK_NULL_RETURN(themeConstants_, false);
319         return themeConstants_->GetMediaData<T>(resId, len, dest);
320     }
321 
322     template<class T>
GetMediaData(T & resId,size_t & len,std::unique_ptr<uint8_t[]> & dest,const std::string & bundleName,const std::string & moduleName)323     bool GetMediaData(T& resId, size_t& len, std::unique_ptr<uint8_t[]>& dest, const std::string& bundleName,
324         const std::string& moduleName)
325     {
326         if (SystemProperties::GetResourceDecoupling()) {
327             CHECK_NULL_RETURN(resourceAdapter_, false);
328             return resourceAdapter_->GetMediaData(resId, len, dest, bundleName, moduleName);
329         }
330         CHECK_NULL_RETURN(themeConstants_, false);
331         return themeConstants_->GetMediaData<T>(resId, len, dest, bundleName, moduleName);
332     }
333 
GetRawFileData(const std::string & rawFile,size_t & len,std::unique_ptr<uint8_t[]> & dest)334     bool GetRawFileData(const std::string& rawFile, size_t& len, std::unique_ptr<uint8_t[]>& dest)
335     {
336         if (SystemProperties::GetResourceDecoupling()) {
337             CHECK_NULL_RETURN(resourceAdapter_, false);
338             return resourceAdapter_->GetRawFileData(rawFile, len, dest);
339         }
340         CHECK_NULL_RETURN(themeConstants_, false);
341         return themeConstants_->GetRawFileData(rawFile, len, dest);
342     }
343 
GetRawFileData(const std::string & rawFile,size_t & len,std::unique_ptr<uint8_t[]> & dest,const std::string & bundleName,const std::string & moduleName)344     bool GetRawFileData(const std::string& rawFile, size_t& len, std::unique_ptr<uint8_t[]>& dest,
345         const std::string& bundleName, const std::string& moduleName)
346     {
347         if (SystemProperties::GetResourceDecoupling()) {
348             CHECK_NULL_RETURN(resourceAdapter_, false);
349             return resourceAdapter_->GetRawFileData(rawFile, len, dest, bundleName, moduleName);
350         }
351         CHECK_NULL_RETURN(themeConstants_, false);
352         return themeConstants_->GetRawFileData(rawFile, len, dest, bundleName, moduleName);
353     }
354 
GetResourceIdByName(const std::string & resName,const std::string & resType,uint32_t & resId)355     bool GetResourceIdByName(const std::string& resName, const std::string& resType, uint32_t& resId) const
356     {
357         if (SystemProperties::GetResourceDecoupling()) {
358             CHECK_NULL_RETURN(resourceAdapter_, false);
359             return resourceAdapter_->GetIdByName(resName, resType, resId);
360         }
361         CHECK_NULL_RETURN(themeConstants_, false);
362         return themeConstants_->GetResourceIdByName(resName, resType, resId);
363     }
364 
GetSymbolByName(const char * name)365     uint32_t GetSymbolByName(const char *name) const
366     {
367         CHECK_NULL_RETURN(resourceAdapter_, 0);
368         return resourceAdapter_->GetSymbolByName(name);
369     }
370 
GetSymbolById(uint32_t resId)371     uint32_t GetSymbolById(uint32_t resId) const
372     {
373         CHECK_NULL_RETURN(resourceAdapter_, 0);
374         return resourceAdapter_->GetSymbolById(resId);
375     }
376 
377 private:
378     RefPtr<ThemeConstants> themeConstants_;
379     RefPtr<ResourceAdapter> resourceAdapter_;
380     ColorMode localColorMode_ = ColorMode::COLOR_MODE_UNDEFINED;
381 };
382 } // namespace OHOS::Ace
383 
384 #endif