1 /* 2 * Copyright (c) 2021-2022 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 #ifndef OHOS_MOCK_RESOURCE_MANAGER_RESOURCEMANAGERIMPL_H 16 #define OHOS_MOCK_RESOURCE_MANAGER_RESOURCEMANAGERIMPL_H 17 18 #include <map> 19 #include <string> 20 #include <vector> 21 #include "resource_manager.h" 22 23 namespace OHOS { 24 namespace Global { 25 namespace Resource { 26 class ResourceManagerImpl : public ResourceManager { 27 public: 28 ResourceManagerImpl(); 29 30 ~ResourceManagerImpl(); 31 32 bool Init(); 33 34 /** 35 * Add resource path to hap paths 36 * @param path the resource path 37 * @return true if add resource path success, else false 38 */ 39 virtual bool AddResource(const char *path); 40 41 /** 42 * Add resource path to overlay paths 43 * @param path the resource path 44 * @param overlayPaths the exist overlay resource path 45 * @return true if add resource path success, else false 46 */ 47 virtual bool AddResource(const std::string &path, const std::vector<std::string> &overlayPaths); 48 49 /** 50 * Remove resource path to overlay paths 51 * @param path the resource path 52 * @param overlayPaths the exist overlay resource path 53 * @return true if add resource path success, else false 54 */ 55 virtual bool RemoveResource(const std::string &path, const std::vector<std::string> &overlayPaths); 56 57 /** 58 * Update the resConfig 59 * @param resConfig the resource config 60 * @return SUCCESS if the resConfig updated success, else HAP_INIT_FAILED 61 */ 62 virtual RState UpdateResConfig(ResConfig &resConfig); 63 64 /** 65 * Get the resConfig 66 * @param resConfig the resource config 67 */ 68 virtual void GetResConfig(ResConfig &resConfig); 69 70 /** 71 * Get string resource by Id 72 * @param id the resource Id 73 * @param outValue the string resource write to 74 * @return SUCCESS if resource exist, else NOT_FOUND 75 */ 76 virtual RState GetStringById(uint32_t id, std::string &outValue); 77 78 /** 79 * Get string by resource name 80 * @param name the resource name 81 * @param outValue the resource write to 82 * @return SUCCESS if resource exist, else NOT_FOUND 83 */ 84 virtual RState GetStringByName(const char *name, std::string &outValue); 85 86 /** 87 * Get string format by resource id 88 * @param id the resource id 89 * @param outValue the resource write to 90 * @return SUCCESS if resource exist, else NOT_FOUND 91 */ 92 virtual RState GetStringFormatById(std::string &outValue, uint32_t id, ...); 93 94 /** 95 * Get string format by resource name 96 * @param name the resource name 97 * @param outValue the resource write to 98 * @return SUCCESS if resource exist, else NOT_FOUND 99 */ 100 virtual RState GetStringFormatByName(std::string &outValue, const char *name, ...); 101 102 /** 103 * Get the STRINGARRAY resource by resource id 104 * @param id the resource id 105 * @param outValue the resource write to 106 * @return SUCCESS if resource exist, else NOT_FOUND 107 */ 108 virtual RState GetStringArrayById(uint32_t id, std::vector<std::string> &outValue); 109 110 /** 111 * Get the STRINGARRAY resource by resource name 112 * @param name the resource name 113 * @param outValue the resource write to 114 * @return SUCCESS if resource exist, else NOT_FOUND 115 */ 116 virtual RState GetStringArrayByName(const char *name, std::vector<std::string> &outValue); 117 118 /** 119 * Get the PATTERN resource by resource id 120 * @param id the resource id 121 * @param outValue the resource write to 122 * @return SUCCESS if resource exist, else NOT_FOUND 123 */ 124 virtual RState GetPatternById(uint32_t id, std::map<std::string, std::string> &outValue); 125 126 /** 127 * Get the PATTERN resource by resource name 128 * @param name the resource name 129 * @param outValue the resource write to 130 * @return SUCCESS if resource exist, else NOT_FOUND 131 */ 132 virtual RState GetPatternByName(const char *name, std::map<std::string, std::string> &outValue); 133 134 /** 135 * Get the plural string by resource id 136 * @param id the resource id 137 * @param quantity the language quantity 138 * @param outValue the resource write to 139 * @return SUCCESS if resource exist, else NOT_FOUND 140 */ 141 virtual RState GetPluralStringById(uint32_t id, int quantity, std::string &outValue); 142 143 /** 144 * Get the plural string by resource name 145 * @param name the resource name 146 * @param quantity the language quantity 147 * @param outValue the resource write to 148 * @return SUCCESS if resource exist, else NOT_FOUND 149 */ 150 virtual RState GetPluralStringByName(const char *name, int quantity, std::string &outValue); 151 152 /** 153 * Get the plural format string by resource id 154 * @param outValue the resource write to 155 * @param id the resource id 156 * @param quantity the language quantity 157 * @return SUCCESS if resource exist, else NOT_FOUND 158 */ 159 virtual RState GetPluralStringByIdFormat(std::string &outValue, uint32_t id, int quantity, ...); 160 161 /** 162 * Get the plural format string by resource name 163 * @param outValue the resource write to 164 * @param id the resource id 165 * @param quantity the language quantity 166 * @return SUCCESS if resource exist, else NOT_FOUND 167 */ 168 virtual RState GetPluralStringByNameFormat(std::string &outValue, const char *name, int quantity, ...); 169 170 /** 171 * Get the THEME resource by resource id 172 * @param id the resource id 173 * @param outValue the resource write to 174 * @return SUCCESS if resource exist, else NOT_FOUND 175 */ 176 virtual RState GetThemeById(uint32_t id, std::map<std::string, std::string> &outValue); 177 178 /** 179 * Get the THEME resource by resource name 180 * @param name the resource name 181 * @param outValue the resource write to 182 * @return SUCCESS if resource exist, else NOT_FOUND 183 */ 184 virtual RState GetThemeByName(const char *name, std::map<std::string, std::string> &outValue); 185 186 /** 187 * Get the BOOLEAN resource by resource id 188 * @param id the resource id 189 * @param outValue the obtain boolean value write to 190 * @return SUCCESS if resource exist, else NOT_FOUND 191 */ 192 virtual RState GetBooleanById(uint32_t id, bool &outValue); 193 194 /** 195 * Get the BOOLEAN resource by resource name 196 * @param name the resource name 197 * @param outValue the obtain boolean value write to 198 * @return SUCCESS if resource exist, else NOT_FOUND 199 */ 200 virtual RState GetBooleanByName(const char *name, bool &outValue); 201 202 /** 203 * Get the INTEGER resource by resource id 204 * @param id the resource id 205 * @param outValue the obtain Integer value write to 206 * @return SUCCESS if resource exist, else NOT_FOUND 207 */ 208 virtual RState GetIntegerById(uint32_t id, int &outValue); 209 210 /** 211 * Get the INTEGER resource by resource name 212 * @param name the resource name 213 * @param outValue the obtain Integer value write to 214 * @return SUCCESS if resource exist, else NOT_FOUND 215 */ 216 virtual RState GetIntegerByName(const char *name, int &outValue); 217 218 /** 219 * Get the FLOAT resource by resource id 220 * @param id the resource id 221 * @param outValue the obtain float value write to 222 * @return SUCCESS if resource exist, else NOT_FOUND 223 */ 224 virtual RState GetFloatById(uint32_t id, float &outValue); 225 226 /** 227 * Get the FLOAT resource by resource id 228 * @param id the resource id 229 * @param outValue the obtain float value write to 230 * @param unit the unit do not in parsing 231 * @return SUCCESS if resource exist, else NOT_FOUND 232 */ 233 virtual RState GetFloatById(uint32_t id, float &outValue, std::string &unit); 234 235 /** 236 * Get the FLOAT resource by resource name 237 * @param name the resource name 238 * @param outValue the obtain float value write to 239 * @return SUCCESS if resource exist, else NOT_FOUND 240 */ 241 virtual RState GetFloatByName(const char *name, float &outValue); 242 243 /** 244 * Get the FLOAT resource by resource id 245 * @param id the resource id 246 * @param outValue the obtain float value write to 247 * @param unit the string do not in parsing 248 * @return SUCCESS if resource exist, else NOT_FOUND 249 */ 250 virtual RState GetFloatByName(const char *name, float &outValue, std::string &unit); 251 252 /** 253 * Get the INTARRAY resource by resource id 254 * @param id the resource id 255 * @param outValue the obtain resource value convert to vector<int> write to 256 * @return SUCCESS if resource exist, else NOT_FOUND 257 */ 258 virtual RState GetIntArrayById(uint32_t id, std::vector<int> &outValue); 259 260 /** 261 * Get the INTARRAY resource by resource name 262 * @param name the resource name 263 * @param outValue the obtain resource value convert to vector<int> write to 264 * @return SUCCESS if resource exist, else NOT_FOUND 265 */ 266 virtual RState GetIntArrayByName(const char *name, std::vector<int> &outValue); 267 268 /** 269 * Get the COLOR resource by resource id 270 * @param id the resource id 271 * @param outValue the obtain resource value convert to uint32_t write to 272 * @return SUCCESS if resource exist, else NOT_FOUND 273 */ 274 virtual RState GetColorById(uint32_t id, uint32_t &outValue); 275 276 /** 277 * Get the COLOR resource by resource name 278 * @param name the resource name 279 * @param outValue the obtain resource value convert to uint32_t write to 280 * @return SUCCESS if resource exist, else NOT_FOUND 281 */ 282 virtual RState GetColorByName(const char *name, uint32_t &outValue); 283 284 /** 285 * Get the PROF resource by resource id 286 * @param id the resource id 287 * @param outValue the obtain resource path write to 288 * @return SUCCESS if resource exist, else NOT_FOUND 289 */ 290 virtual RState GetProfileById(uint32_t id, std::string &outValue); 291 292 /** 293 * Get the PROF resource by resource name 294 * @param name the resource name 295 * @param outValue the obtain resource path write to 296 * @return SUCCESS if resource exist, else NOT_FOUND 297 */ 298 virtual RState GetProfileByName(const char *name, std::string &outValue); 299 300 /** 301 * Get the MEDIA resource by resource id 302 * @param id the resource id 303 * @param outValue the obtain resource path write to 304 * @param density the screen density, within the area of OHOS::Global::Resource::ScreenDensity 305 * @return SUCCESS if resource exist, else NOT_FOUND 306 */ 307 virtual RState GetMediaById(uint32_t id, std::string &outValue, uint32_t density = 0); 308 309 /** 310 * Get the MEDIA resource by resource name 311 * @param name the resource name 312 * @param outValue the obtain resource path write to 313 * @param density the screen density, within the area of OHOS::Global::Resource::ScreenDensity 314 * @return SUCCESS if resource exist, else NOT_FOUND 315 */ 316 virtual RState GetMediaByName(const char *name, std::string &outValue, uint32_t density = 0); 317 318 /** 319 * Get the raw file path by resource name 320 * @param name the resource name 321 * @param outValue the obtain resource path write to 322 * @return SUCCESS if resource exist, else NOT_FOUND 323 */ 324 virtual RState GetRawFilePathByName(const std::string &name, std::string &outValue); 325 326 /** 327 * Get the rawFile descriptor by resource name 328 * @param name the resource name 329 * @param descriptor the obtain raw file member fd, length, offet write to 330 * @return SUCCESS if resource exist, else ERROR 331 */ 332 virtual RState GetRawFileDescriptor(const std::string &name, RawFileDescriptor &descriptor); 333 334 /** 335 * Close rawFile descriptor by resource name 336 * @param name the resource name 337 * @return SUCCESS if close the rawFile descriptor, else ERROR 338 */ 339 virtual RState CloseRawFileDescriptor(const std::string &name); 340 341 /** 342 * Get all resource paths 343 * @return The vector of resource paths 344 */ 345 std::vector<std::string> GetResourcePaths(); 346 347 /** 348 * Get the MEDIA data by resource id 349 * @param id the resource id 350 * @param len the data len write to 351 * @param outValue the obtain resource path write to 352 * @param density the screen density, within the area of OHOS::Global::Resource::ScreenDensity 353 * @return SUCCESS if resource exist, else NOT_FOUND 354 */ 355 virtual RState GetMediaDataById(uint32_t id, size_t &len, std::unique_ptr<uint8_t[]> &outValue, 356 uint32_t density = 0); 357 358 /** 359 * Get the MEDIA data by resource name 360 * @param name the resource name 361 * @param len the data len write to 362 * @param outValue the obtain resource path write to 363 * @param density the screen density, within the area of OHOS::Global::Resource::ScreenDensity 364 * @return SUCCESS if resource exist, else NOT_FOUND 365 */ 366 virtual RState GetMediaDataByName(const char *name, size_t &len, std::unique_ptr<uint8_t[]> &outValue, 367 uint32_t density = 0); 368 369 /** 370 * Get the MEDIA base64 data resource by resource id 371 * @param id the resource id 372 * @param outValue the media base64 data 373 * @param density the screen density, within the area of OHOS::Global::Resource::ScreenDensity 374 * @return SUCCESS if resource exist, else NOT_FOUND 375 */ 376 virtual RState GetMediaBase64DataById(uint32_t id, std::string &outValue, uint32_t density = 0); 377 378 /** 379 * Get the MEDIA base64 data resource by resource id 380 * @param name the resource name 381 * @param outValue the media base64 data 382 * @param density the screen density, within the area of OHOS::Global::Resource::ScreenDensity 383 * @return SUCCESS if resource exist, else NOT_FOUND 384 */ 385 virtual RState GetMediaBase64DataByName(const char *name, std::string &outValue, uint32_t density = 0); 386 387 /** 388 * Get the PROF resource by resource id 389 * @param name the resource id 390 * @param len the data len write to 391 * @param outValue the obtain resource path write to 392 * @return SUCCESS if resource exist, else NOT_FOUND 393 */ 394 virtual RState GetProfileDataById(uint32_t id, size_t &len, std::unique_ptr<uint8_t[]> &outValue); 395 396 /** 397 * Get the PROF resource by resource name 398 * @param name the resource name 399 * @param len the data len write to 400 * @param outValue the obtain resource path write to 401 * @return SUCCESS if resource exist, else NOT_FOUND 402 */ 403 virtual RState GetProfileDataByName(const char *name, size_t &len, std::unique_ptr<uint8_t[]> &outValue); 404 405 /** 406 * Get the rawFile base64 from hap by rawFile name 407 * @param rawFileName the rawFile name 408 * @param len the data len write to 409 * @param outValue the obtain resource path write to 410 * @return SUCCESS if resource exist, else NOT_FOUND 411 */ 412 virtual RState GetRawFileFromHap(const std::string &rawFileName, size_t &len, 413 std::unique_ptr<uint8_t[]> &outValue); 414 415 /** 416 * Get the rawFile Descriptor from hap by rawFile name 417 * @param rawFileName the rawFile name 418 * @param descriptor the raw file member fd, length, offet write to 419 * @return SUCCESS if resource exist, else NOT_FOUND 420 */ 421 virtual RState GetRawFileDescriptorFromHap(const std::string &rawFileName, RawFileDescriptor &descriptor); 422 423 /** 424 * Is load hap 425 * @param hapPath the hap path 426 */ 427 virtual RState IsLoadHap(std::string &hapPath); 428 429 /** 430 * Get the raw file list 431 * @param rawDirPath the rawfile directory path 432 * @param rawfileList the rawfile list write to 433 * @return SUCCESS if resource exist, else not found 434 */ 435 virtual RState GetRawFileList(const std::string &rawDirPath, std::vector<std::string>& rawfileList); 436 437 /** 438 * Get the drawable information for given resId, mainly about type, len, buffer 439 * @param id the resource id 440 * @param type the drawable type 441 * @param len the drawable buffer length 442 * @param outValue the drawable buffer write to 443 * @param density the drawable density 444 * @return SUCCESS if resource exist, else not found 445 */ 446 virtual RState GetDrawableInfoById(uint32_t id, std::string &type, size_t &len, 447 std::unique_ptr<uint8_t[]> &outValue, uint32_t density = 0); 448 449 /** 450 * Get the drawable information for given resName, mainly about type, len, buffer 451 * @param name the resource Name 452 * @param type the drawable type 453 * @param len the drawable buffer length 454 * @param outValue the drawable buffer write to 455 * @param density the drawable density 456 * @return SUCCESS if resource exist, else not found 457 */ 458 virtual RState GetDrawableInfoByName(const char *name, std::string &type, size_t &len, 459 std::unique_ptr<uint8_t[]> &outValue, uint32_t density = 0); 460 461 /** 462 * Get string format by resource id 463 * @param id the resource id 464 * @param outValue the resource write to 465 * @param jsParams the formatting string resource js parameters, the tuple first parameter represents the type, 466 * napi_number is denoted by NAPI_NUMBER, napi_string is denoted by NAPI_STRING, 467 * the tuple second parameter represents the value 468 * @return SUCCESS if resource exists and was formatted successfully, else ERROR 469 */ 470 virtual RState GetStringFormatById(uint32_t id, std::string &outValue, 471 std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams); 472 473 /** 474 * Get string format by resource name 475 * @param name the resource name 476 * @param outValue the resource write to 477 * @param jsParams the formatting string resource js parameters, the tuple first parameter represents the type, 478 * napi_number is denoted by NAPI_NUMBER, napi_string is denoted by NAPI_STRING, 479 * the tuple second parameter represents the value 480 * @return SUCCESS if resource exists and was formatted successfully, else ERROR 481 */ 482 virtual RState GetStringFormatByName(const char *name, std::string &outValue, 483 std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams); 484 485 /** 486 * Get the resource limit keys value which every binary bit corresponds to existing limit key {@link KeyType} 487 * 488 * @return the resource limit keys 489 */ 490 virtual uint32_t GetResourceLimitKeys(); 491 492 /** 493 * Add the overlay resource for current application 494 * @param path the overlay resource path 495 * @return true if add resource path success, else false 496 */ 497 virtual bool AddAppOverlay(const std::string &path); 498 499 /** 500 * Remove the overlay resource for current application 501 * @param path the overlay resource path 502 * @return true if add resource path success, else false 503 */ 504 virtual bool RemoveAppOverlay(const std::string &path); 505 }; 506 } // namespace Resource 507 } // namespace Global 508 } // namespace OHOS 509 #endif