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_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/system_properties.h" 28 #include "core/components/common/properties/color.h" 29 #include "core/components/theme/resource_adapter.h" 30 #include "core/components/theme/theme_constants.h" 31 32 namespace OHOS::Ace { 33 class ResourceWrapper : public AceType { 34 DECLARE_ACE_TYPE(ResourceWrapper, AceType); 35 36 public: ResourceWrapper(RefPtr<ThemeConstants> & themeConstants,RefPtr<ResourceAdapter> & resourceAdapter)37 ResourceWrapper(RefPtr<ThemeConstants>& themeConstants, RefPtr<ResourceAdapter>& resourceAdapter) 38 : themeConstants_(themeConstants), resourceAdapter_(resourceAdapter) {}; 39 ~ResourceWrapper() = default; 40 GetColor(uint32_t key)41 Color GetColor(uint32_t key) const 42 { 43 if (SystemProperties::GetResourceDecoupling()) { 44 return resourceAdapter_->GetColor(key); 45 } 46 return themeConstants_->GetColor(key); 47 } 48 GetColorByName(const std::string & resName)49 Color GetColorByName(const std::string& resName) const 50 { 51 if (SystemProperties::GetResourceDecoupling()) { 52 return resourceAdapter_->GetColorByName(resName); 53 } 54 return themeConstants_->GetColorByName(resName); 55 } 56 GetDimension(uint32_t key)57 Dimension GetDimension(uint32_t key) const 58 { 59 if (SystemProperties::GetResourceDecoupling()) { 60 return resourceAdapter_->GetDimension(key); 61 } 62 return themeConstants_->GetDimension(key); 63 } 64 GetDimensionByName(const std::string & resName)65 Dimension GetDimensionByName(const std::string& resName) const 66 { 67 if (SystemProperties::GetResourceDecoupling()) { 68 return resourceAdapter_->GetDimensionByName(resName); 69 } 70 return themeConstants_->GetDimensionByName(resName); 71 } 72 GetInt(uint32_t key)73 int32_t GetInt(uint32_t key) const 74 { 75 if (SystemProperties::GetResourceDecoupling()) { 76 return resourceAdapter_->GetInt(key); 77 } 78 return themeConstants_->GetInt(key); 79 } 80 GetIntByName(const std::string & resName)81 int32_t GetIntByName(const std::string& resName) const 82 { 83 if (SystemProperties::GetResourceDecoupling()) { 84 return resourceAdapter_->GetIntByName(resName); 85 } 86 return themeConstants_->GetIntByName(resName); 87 } 88 GetDouble(uint32_t key)89 double GetDouble(uint32_t key) const 90 { 91 if (SystemProperties::GetResourceDecoupling()) { 92 return resourceAdapter_->GetDouble(key); 93 } 94 return themeConstants_->GetDouble(key); 95 } 96 GetDoubleByName(const std::string & resName)97 double GetDoubleByName(const std::string& resName) const 98 { 99 if (SystemProperties::GetResourceDecoupling()) { 100 return resourceAdapter_->GetDoubleByName(resName); 101 } 102 return themeConstants_->GetDoubleByName(resName); 103 } 104 GetString(uint32_t key)105 std::string GetString(uint32_t key) const 106 { 107 if (SystemProperties::GetResourceDecoupling()) { 108 return resourceAdapter_->GetString(key); 109 } 110 return themeConstants_->GetString(key); 111 } 112 GetStringByName(const std::string & resName)113 std::string GetStringByName(const std::string& resName) const 114 { 115 if (SystemProperties::GetResourceDecoupling()) { 116 return resourceAdapter_->GetStringByName(resName); 117 } 118 return themeConstants_->GetStringByName(resName); 119 } 120 GetPluralString(uint32_t key,int count)121 std::string GetPluralString(uint32_t key, int count) const 122 { 123 if (SystemProperties::GetResourceDecoupling()) { 124 return resourceAdapter_->GetPluralString(key, count); 125 } 126 return themeConstants_->GetPluralString(key, count); 127 } 128 GetPluralStringByName(const std::string & resName,int count)129 std::string GetPluralStringByName(const std::string& resName, int count) const 130 { 131 if (SystemProperties::GetResourceDecoupling()) { 132 return resourceAdapter_->GetPluralStringByName(resName, count); 133 } 134 return themeConstants_->GetPluralStringByName(resName, count); 135 } 136 GetBoolean(uint32_t key)137 bool GetBoolean(uint32_t key) const 138 { 139 if (SystemProperties::GetResourceDecoupling()) { 140 return resourceAdapter_->GetBoolean(key); 141 } 142 return themeConstants_->GetBoolean(key); 143 } 144 GetBooleanByName(const std::string & resName)145 bool GetBooleanByName(const std::string& resName) const 146 { 147 if (SystemProperties::GetResourceDecoupling()) { 148 return resourceAdapter_->GetBooleanByName(resName); 149 } 150 return themeConstants_->GetBooleanByName(resName); 151 } 152 GetIntArray(uint32_t key)153 std::vector<uint32_t> GetIntArray(uint32_t key) const 154 { 155 if (SystemProperties::GetResourceDecoupling()) { 156 return resourceAdapter_->GetIntArray(key); 157 } 158 return themeConstants_->GetIntArray(key); 159 } 160 GetIntArrayByName(const std::string & resName)161 std::vector<uint32_t> GetIntArrayByName(const std::string& resName) const 162 { 163 if (SystemProperties::GetResourceDecoupling()) { 164 return resourceAdapter_->GetIntArrayByName(resName); 165 } 166 return themeConstants_->GetIntArrayByName(resName); 167 } 168 GetPixelMap(uint32_t key)169 std::shared_ptr<Media::PixelMap> GetPixelMap(uint32_t key) const 170 { 171 if (SystemProperties::GetResourceDecoupling()) { 172 return resourceAdapter_->GetPixelMap(key); 173 } 174 return themeConstants_->GetPixelMap(key); 175 } 176 GetStringArray(uint32_t key)177 std::vector<std::string> GetStringArray(uint32_t key) const 178 { 179 if (SystemProperties::GetResourceDecoupling()) { 180 return resourceAdapter_->GetStringArray(key); 181 } 182 return themeConstants_->GetStringArray(key); 183 } 184 GetStringArrayByName(const std::string & resName)185 std::vector<std::string> GetStringArrayByName(const std::string& resName) const 186 { 187 if (SystemProperties::GetResourceDecoupling()) { 188 return resourceAdapter_->GetStringArrayByName(resName); 189 } 190 return themeConstants_->GetStringArrayByName(resName); 191 } 192 GetMediaPath(uint32_t key)193 std::string GetMediaPath(uint32_t key) const 194 { 195 if (SystemProperties::GetResourceDecoupling()) { 196 return resourceAdapter_->GetMediaPath(key); 197 } 198 return themeConstants_->GetMediaPath(key); 199 } 200 GetMediaPathByName(const std::string & resName)201 std::string GetMediaPathByName(const std::string& resName) const 202 { 203 if (SystemProperties::GetResourceDecoupling()) { 204 return resourceAdapter_->GetMediaPathByName(resName); 205 } 206 return themeConstants_->GetMediaPathByName(resName); 207 } 208 GetRawfile(const std::string & fileName)209 std::string GetRawfile(const std::string& fileName) const 210 { 211 if (SystemProperties::GetResourceDecoupling()) { 212 return resourceAdapter_->GetRawfile(fileName); 213 } 214 return themeConstants_->GetRawfile(fileName); 215 } 216 GetRawFileDescription(const std::string & rawfileName,RawfileDescription & rawfileDescription)217 bool GetRawFileDescription(const std::string& rawfileName, RawfileDescription& rawfileDescription) const 218 { 219 if (SystemProperties::GetResourceDecoupling()) { 220 return resourceAdapter_->GetRawFileDescription(rawfileName, rawfileDescription); 221 } 222 return themeConstants_->GetRawFileDescription(rawfileName, rawfileDescription); 223 } 224 GetMediaById(const int32_t & resId,std::string & mediaPath)225 bool GetMediaById(const int32_t& resId, std::string& mediaPath) const 226 { 227 if (SystemProperties::GetResourceDecoupling()) { 228 return resourceAdapter_->GetMediaById(resId, mediaPath); 229 } 230 return themeConstants_->GetMediaById(resId, mediaPath); 231 } 232 233 template<class T> GetMediaResource(T & resId,std::ostream & dest)234 bool GetMediaResource(T& resId, std::ostream& dest) const 235 { 236 if (SystemProperties::GetResourceDecoupling()) { 237 return resourceAdapter_->GetResource(resId, dest); 238 } 239 return themeConstants_->GetMediaResource<T>(resId, dest); 240 } 241 242 template<class T> GetMediaData(T & resId,size_t & len,std::unique_ptr<uint8_t[]> & dest)243 bool GetMediaData(T& resId, size_t& len, std::unique_ptr<uint8_t[]>& dest) 244 { 245 if (SystemProperties::GetResourceDecoupling()) { 246 return resourceAdapter_->GetMediaData(resId, len, dest); 247 } 248 return themeConstants_->GetMediaData<T>(resId, len, dest); 249 } 250 251 template<class T> GetMediaData(T & resId,size_t & len,std::unique_ptr<uint8_t[]> & dest,const std::string & bundleName,const std::string & moduleName)252 bool GetMediaData(T& resId, size_t& len, std::unique_ptr<uint8_t[]>& dest, const std::string& bundleName, 253 const std::string& moduleName) 254 { 255 if (SystemProperties::GetResourceDecoupling()) { 256 return resourceAdapter_->GetMediaData(resId, len, dest, bundleName, moduleName); 257 } 258 return themeConstants_->GetMediaData<T>(resId, len, dest, bundleName, moduleName); 259 } 260 GetRawFileData(const std::string & rawFile,size_t & len,std::unique_ptr<uint8_t[]> & dest)261 bool GetRawFileData(const std::string& rawFile, size_t& len, std::unique_ptr<uint8_t[]>& dest) 262 { 263 if (SystemProperties::GetResourceDecoupling()) { 264 return resourceAdapter_->GetRawFileData(rawFile, len, dest); 265 } 266 return themeConstants_->GetRawFileData(rawFile, len, dest); 267 } 268 GetRawFileData(const std::string & rawFile,size_t & len,std::unique_ptr<uint8_t[]> & dest,const std::string & bundleName,const std::string & moduleName)269 bool GetRawFileData(const std::string& rawFile, size_t& len, std::unique_ptr<uint8_t[]>& dest, 270 const std::string& bundleName, const std::string& moduleName) 271 { 272 if (SystemProperties::GetResourceDecoupling()) { 273 return resourceAdapter_->GetRawFileData(rawFile, len, dest, bundleName, moduleName); 274 } 275 return themeConstants_->GetRawFileData(rawFile, len, dest, bundleName, moduleName); 276 } 277 GetResourceIdByName(const std::string & resName,const std::string & resType,uint32_t & resId)278 bool GetResourceIdByName(const std::string& resName, const std::string& resType, uint32_t& resId) const 279 { 280 if (SystemProperties::GetResourceDecoupling()) { 281 return resourceAdapter_->GetIdByName(resName, resType, resId); 282 } 283 return themeConstants_->GetResourceIdByName(resName, resType, resId); 284 } 285 GetSymbolByName(const char * name)286 uint32_t GetSymbolByName(const char *name) const 287 { 288 return resourceAdapter_->GetSymbolByName(name); 289 } 290 GetSymbolById(uint32_t resId)291 uint32_t GetSymbolById(uint32_t resId) const 292 { 293 return resourceAdapter_->GetSymbolById(resId); 294 } 295 296 private: 297 RefPtr<ThemeConstants> themeConstants_; 298 RefPtr<ResourceAdapter> resourceAdapter_; 299 }; 300 } // namespace OHOS::Ace 301 302 #endif