1 /* 2 * Copyright (c) 2021-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_COMPONENTS_THEME_RESOURCE_ADAPTER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_THEME_RESOURCE_ADAPTER_H 18 19 #include "base/image/pixel_map.h" 20 #include "base/utils/resource_configuration.h" 21 #include "core/common/resource/resource_configuration.h" 22 #include "core/components/theme/theme_style.h" 23 namespace OHOS::Ace { 24 25 struct RawfileDescription { 26 // the rawfile fd 27 int fd = 0; 28 // the offset from where the rawfile starts in the hap 29 long offset = 0; 30 // the length of the rawfile in the hap 31 long length = 0; 32 }; 33 34 class ResourceAdapter : public virtual AceType { 35 DECLARE_ACE_TYPE(ResourceAdapter, AceType); 36 37 public: 38 ResourceAdapter() = default; 39 ~ResourceAdapter() override = default; 40 41 static RefPtr<ResourceAdapter> Create(); 42 static RefPtr<ResourceAdapter> CreateV2(); 43 Init(const ResourceInfo & resourceInfo)44 virtual void Init(const ResourceInfo& resourceInfo) {} 45 46 virtual void UpdateConfig(const ResourceConfiguration& config, bool themeFlag = false) {} 47 GetTheme(int32_t themeId)48 virtual RefPtr<ThemeStyle> GetTheme(int32_t themeId) 49 { 50 return nullptr; 51 } 52 53 virtual Color GetColor(uint32_t resId) = 0; 54 GetColorByName(const std::string & resName)55 virtual Color GetColorByName(const std::string& resName) 56 { 57 return {}; 58 } 59 60 virtual Dimension GetDimension(uint32_t resId) = 0; 61 GetDimensionByName(const std::string & resName)62 virtual Dimension GetDimensionByName(const std::string& resName) 63 { 64 return {}; 65 } 66 67 virtual std::string GetString(uint32_t resId) = 0; 68 GetStringByName(const std::string & resName)69 virtual std::string GetStringByName(const std::string& resName) 70 { 71 return {}; 72 } 73 74 virtual std::vector<std::string> GetStringArray(uint32_t resId) const = 0; 75 GetStringArrayByName(const std::string & resName)76 virtual std::vector<std::string> GetStringArrayByName(const std::string& resName) const 77 { 78 return {}; 79 } 80 81 virtual double GetDouble(uint32_t resId) = 0; 82 GetDoubleByName(const std::string & resName)83 virtual double GetDoubleByName(const std::string& resName) 84 { 85 return 0.0; 86 } 87 88 virtual int32_t GetInt(uint32_t resId) = 0; 89 GetIntByName(const std::string & resName)90 virtual int32_t GetIntByName(const std::string& resName) 91 { 92 return 0; 93 } 94 GetPluralString(uint32_t resId,int quantity)95 virtual std::string GetPluralString(uint32_t resId, int quantity) 96 { 97 return {}; 98 } 99 GetPluralStringByName(const std::string & resName,int quantity)100 virtual std::string GetPluralStringByName(const std::string& resName, int quantity) 101 { 102 return {}; 103 } 104 GetPixelMap(uint32_t resId)105 virtual std::shared_ptr<Media::PixelMap> GetPixelMap(uint32_t resId) 106 { 107 return nullptr; 108 } 109 GetMediaPath(uint32_t resId)110 virtual std::string GetMediaPath(uint32_t resId) 111 { 112 return {}; 113 } 114 GetMediaPathByName(const std::string & resName)115 virtual std::string GetMediaPathByName(const std::string& resName) 116 { 117 return {}; 118 } 119 GetRawfile(const std::string & fileName)120 virtual std::string GetRawfile(const std::string& fileName) 121 { 122 return {}; 123 } 124 GetRawFileData(const std::string & rawFile,size_t & len,std::unique_ptr<uint8_t[]> & dest)125 virtual bool GetRawFileData(const std::string& rawFile, size_t& len, std::unique_ptr<uint8_t[]>& dest) 126 { 127 return false; 128 } 129 GetRawFileData(const std::string & rawFile,size_t & len,std::unique_ptr<uint8_t[]> & dest,const std::string & bundleName,const std::string & moduleName)130 virtual bool GetRawFileData(const std::string& rawFile, size_t& len, std::unique_ptr<uint8_t[]>& dest, 131 const std::string& bundleName, const std::string& moduleName) 132 { 133 return false; 134 } 135 GetMediaData(uint32_t resId,size_t & len,std::unique_ptr<uint8_t[]> & dest)136 virtual bool GetMediaData(uint32_t resId, size_t& len, std::unique_ptr<uint8_t[]>& dest) 137 { 138 return false; 139 } 140 GetMediaData(uint32_t resId,size_t & len,std::unique_ptr<uint8_t[]> & dest,const std::string & bundleName,const std::string & moduleName)141 virtual bool GetMediaData(uint32_t resId, size_t& len, std::unique_ptr<uint8_t[]>& dest, 142 const std::string& bundleName, const std::string& moduleName) 143 { 144 return false; 145 } 146 GetMediaData(const std::string & resName,size_t & len,std::unique_ptr<uint8_t[]> & dest)147 virtual bool GetMediaData(const std::string& resName, size_t& len, std::unique_ptr<uint8_t[]>& dest) 148 { 149 return false; 150 } 151 GetMediaData(const std::string & resName,size_t & len,std::unique_ptr<uint8_t[]> & dest,const std::string & bundleName,const std::string & moduleName)152 virtual bool GetMediaData(const std::string& resName, size_t& len, std::unique_ptr<uint8_t[]>& dest, 153 const std::string& bundleName, const std::string& moduleName) 154 { 155 return false; 156 } 157 GetBoolean(uint32_t resId)158 virtual bool GetBoolean(uint32_t resId) const 159 { 160 return false; 161 } 162 GetBooleanByName(const std::string & resName)163 virtual bool GetBooleanByName(const std::string& resName) const 164 { 165 return false; 166 } 167 GetIntArray(uint32_t resId)168 virtual std::vector<uint32_t> GetIntArray(uint32_t resId) const 169 { 170 return {}; 171 } 172 GetIntArrayByName(const std::string & resName)173 virtual std::vector<uint32_t> GetIntArrayByName(const std::string& resName) const 174 { 175 return {}; 176 } 177 GetResource(uint32_t resId,std::ostream & dest)178 virtual bool GetResource(uint32_t resId, std::ostream& dest) const 179 { 180 return false; 181 } 182 GetResource(const std::string & resId,std::ostream & dest)183 virtual bool GetResource(const std::string& resId, std::ostream& dest) const 184 { 185 return false; 186 } 187 GetIdByName(const std::string & resName,const std::string & resType,uint32_t & resId)188 virtual bool GetIdByName(const std::string& resName, const std::string& resType, uint32_t& resId) const 189 { 190 return false; 191 } 192 UpdateResourceManager(const std::string & bundleName,const std::string & moduleName)193 virtual void UpdateResourceManager(const std::string& bundleName, const std::string& moduleName) {} 194 CloseRawFileDescription(const std::string & rawfileName)195 virtual bool CloseRawFileDescription(const std::string& rawfileName) const 196 { 197 return false; 198 } 199 GetRawFileDescription(const std::string & rawfileName,RawfileDescription & rawfileDescription)200 virtual bool GetRawFileDescription(const std::string& rawfileName, RawfileDescription& rawfileDescription) const 201 { 202 return false; 203 } 204 205 /* 206 * Get rawfile file description which will not be cached. 207 * NOTE: false value will be returned if not found. 208 * @param[in] rawfileName Target rawfile. 209 * @return success or not to close file info. 210 */ GetRawFD(const std::string &,RawfileDescription &)211 virtual bool GetRawFD(const std::string& /* rawfileName */, RawfileDescription& /* rawfileDescription */) const 212 { 213 return false; 214 } 215 GetMediaById(const int32_t & resId,std::string & mediaPath)216 virtual bool GetMediaById(const int32_t& resId, std::string& mediaPath) const 217 { 218 return false; 219 } 220 GetResourceLimitKeys()221 virtual uint32_t GetResourceLimitKeys() const 222 { 223 return 0; 224 } 225 GetSymbolByName(const char * name)226 virtual uint32_t GetSymbolByName(const char *name) const 227 { 228 return -1; 229 } 230 GetSymbolById(uint32_t resId)231 virtual uint32_t GetSymbolById(uint32_t resId) const 232 { 233 return -1; 234 } 235 GetPatternByName(const std::string & patternName)236 virtual RefPtr<ThemeStyle> GetPatternByName(const std::string& patternName) 237 { 238 return nullptr; 239 } 240 241 static RefPtr<ResourceAdapter> CreateNewResourceAdapter( 242 const std::string& bundleName, const std::string& moduleName); 243 UpdateColorMode(ColorMode colorMode)244 virtual void UpdateColorMode(ColorMode colorMode) {} 245 GetResourceColorMode()246 virtual ColorMode GetResourceColorMode() const 247 { 248 return ColorMode::LIGHT; 249 } 250 GetOverrideResourceAdapter(const ResourceConfiguration & config,const ConfigurationChange & configurationChange)251 virtual RefPtr<ResourceAdapter> GetOverrideResourceAdapter( 252 const ResourceConfiguration& config, const ConfigurationChange& configurationChange) 253 { 254 return nullptr; 255 } 256 }; 257 258 } // namespace OHOS::Ace 259 260 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_THEME_RESOURCE_ADAPTER_H 261