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_RESOURCE_MANAGER_RESOURCEMANAGERIMPL_H 16 #define OHOS_RESOURCE_MANAGER_RESOURCEMANAGERIMPL_H 17 18 #include <map> 19 #include <string> 20 #include <vector> 21 #include "hap_manager.h" 22 #include "resource_manager.h" 23 #include "utils/psue_manager.h" 24 25 namespace OHOS { 26 namespace Global { 27 namespace Resource { 28 class ResourceManagerImpl : public ResourceManager { 29 public: 30 ResourceManagerImpl(); 31 32 ~ResourceManagerImpl(); 33 34 bool Init(); 35 36 /** 37 * Add resource path to hap paths 38 * @param path the resource path 39 * @return true if add resource path success, else false 40 */ 41 virtual bool AddResource(const char *path); 42 43 /** 44 * Add resource path to overlay paths 45 * @param path the resource path 46 * @param overlayPaths the exist overlay resource path 47 * @return true if add resource path success, else false 48 */ 49 virtual bool AddResource(const std::string &path, const std::vector<std::string> &overlayPaths); 50 51 /** 52 * Update the resConfig 53 * @param resConfig the resource config 54 * @return SUCCESS if the resConfig updated success, else HAP_INIT_FAILED 55 */ 56 virtual RState UpdateResConfig(ResConfig &resConfig); 57 58 /** 59 * Get the resConfig 60 * @param resConfig the resource config 61 */ 62 virtual void GetResConfig(ResConfig &resConfig); 63 64 /** 65 * Get string resource by Id 66 * @param id the resource Id 67 * @param outValue the string resource write to 68 * @return SUCCESS if resource exist, else NOT_FOUND 69 */ 70 virtual RState GetStringById(uint32_t id, std::string &outValue); 71 72 /** 73 * Get string by resource name 74 * @param name the resource name 75 * @param outValue the resource write to 76 * @return SUCCESS if resource exist, else NOT_FOUND 77 */ 78 virtual RState GetStringByName(const char *name, std::string &outValue); 79 80 /** 81 * Get formatstring by resource id 82 * @param id the resource id 83 * @param outValue the resource write to 84 * @return SUCCESS if resource exist, else NOT_FOUND 85 */ 86 virtual RState GetStringFormatById(std::string &outValue, uint32_t id, ...); 87 88 /** 89 * Get formatstring by resource name 90 * @param name the resource name 91 * @param outValue the resource write to 92 * @return SUCCESS if resource exist, else NOT_FOUND 93 */ 94 virtual RState GetStringFormatByName(std::string &outValue, const char *name, ...); 95 96 /** 97 * Get the STRINGARRAY resource by resource id 98 * @param id the resource id 99 * @param outValue the resource write to 100 * @return SUCCESS if resource exist, else NOT_FOUND 101 */ 102 virtual RState GetStringArrayById(uint32_t id, std::vector<std::string> &outValue); 103 104 /** 105 * Get the STRINGARRAY resource by resource name 106 * @param name the resource name 107 * @param outValue the resource write to 108 * @return SUCCESS if resource exist, else NOT_FOUND 109 */ 110 virtual RState GetStringArrayByName(const char *name, std::vector<std::string> &outValue); 111 112 /** 113 * Get the PATTERN resource by resource id 114 * @param id the resource id 115 * @param outValue the resource write to 116 * @return SUCCESS if resource exist, else NOT_FOUND 117 */ 118 virtual RState GetPatternById(uint32_t id, std::map<std::string, std::string> &outValue); 119 120 /** 121 * Get the PATTERN resource by resource name 122 * @param name the resource name 123 * @param outValue the resource write to 124 * @return SUCCESS if resource exist, else NOT_FOUND 125 */ 126 virtual RState GetPatternByName(const char *name, std::map<std::string, std::string> &outValue); 127 128 /** 129 * Get the plural string by resource id 130 * @param id the resource id 131 * @param quantity the language quantity 132 * @param outValue the resource write to 133 * @return SUCCESS if resource exist, else NOT_FOUND 134 */ 135 virtual RState GetPluralStringById(uint32_t id, int quantity, std::string &outValue); 136 137 /** 138 * Get the plural string by resource name 139 * @param name the resource name 140 * @param quantity the language quantity 141 * @param outValue the resource write to 142 * @return SUCCESS if resource exist, else NOT_FOUND 143 */ 144 virtual RState GetPluralStringByName(const char *name, int quantity, std::string &outValue); 145 146 /** 147 * Get the plural format string by resource id 148 * @param outValue the resource write to 149 * @param id the resource id 150 * @param quantity the language quantity 151 * @return SUCCESS if resource exist, else NOT_FOUND 152 */ 153 virtual RState GetPluralStringByIdFormat(std::string &outValue, uint32_t id, int quantity, ...); 154 155 /** 156 * Get the plural format string by resource name 157 * @param outValue the resource write to 158 * @param id the resource id 159 * @param quantity the language quantity 160 * @return SUCCESS if resource exist, else NOT_FOUND 161 */ 162 virtual RState GetPluralStringByNameFormat(std::string &outValue, const char *name, int quantity, ...); 163 164 /** 165 * Get the THEME resource by resource id 166 * @param id the resource id 167 * @param outValue the resource write to 168 * @return SUCCESS if resource exist, else NOT_FOUND 169 */ 170 virtual RState GetThemeById(uint32_t id, std::map<std::string, std::string> &outValue); 171 172 /** 173 * Get the THEME resource by resource name 174 * @param name the resource name 175 * @param outValue the resource write to 176 * @return SUCCESS if resource exist, else NOT_FOUND 177 */ 178 virtual RState GetThemeByName(const char *name, std::map<std::string, std::string> &outValue); 179 180 /** 181 * Get the BOOLEAN resource by resource id 182 * @param id the resource id 183 * @param outValue the obtain boolean value write to 184 * @return SUCCESS if resource exist, else NOT_FOUND 185 */ 186 virtual RState GetBooleanById(uint32_t id, bool &outValue); 187 188 /** 189 * Get the BOOLEAN resource by resource name 190 * @param name the resource name 191 * @param outValue the obtain boolean value write to 192 * @return SUCCESS if resource exist, else NOT_FOUND 193 */ 194 virtual RState GetBooleanByName(const char *name, bool &outValue); 195 196 /** 197 * Get the INTEGER resource by resource id 198 * @param id the resource id 199 * @param outValue the obtain Integer value write to 200 * @return SUCCESS if resource exist, else NOT_FOUND 201 */ 202 virtual RState GetIntegerById(uint32_t id, int &outValue); 203 204 /** 205 * Get the INTEGER resource by resource name 206 * @param name the resource name 207 * @param outValue the obtain Integer value write to 208 * @return SUCCESS if resource exist, else NOT_FOUND 209 */ 210 virtual RState GetIntegerByName(const char *name, int &outValue); 211 212 /** 213 * Get the FLOAT resource by resource id 214 * @param id the resource id 215 * @param outValue the obtain float value write to 216 * @return SUCCESS if resource exist, else NOT_FOUND 217 */ 218 virtual RState GetFloatById(uint32_t id, float &outValue); 219 220 /** 221 * Get the FLOAT resource by resource id 222 * @param id the resource id 223 * @param outValue the obtain float value write to 224 * @param unit the unit do not in parsing 225 * @return SUCCESS if resource exist, else NOT_FOUND 226 */ 227 virtual RState GetFloatById(uint32_t id, float &outValue, std::string &unit); 228 229 /** 230 * Get the FLOAT resource by resource name 231 * @param name the resource name 232 * @param outValue the obtain float value write to 233 * @return SUCCESS if resource exist, else NOT_FOUND 234 */ 235 virtual RState GetFloatByName(const char *name, float &outValue); 236 237 /** 238 * Get the FLOAT resource by resource id 239 * @param id the resource id 240 * @param outValue the obtain float value write to 241 * @param unit the string do not in parsing 242 * @return SUCCESS if resource exist, else NOT_FOUND 243 */ 244 virtual RState GetFloatByName(const char *name, float &outValue, std::string &unit); 245 246 /** 247 * Get the INTARRAY resource by resource id 248 * @param id the resource id 249 * @param outValue the obtain resource value convert to vector<int> write to 250 * @return SUCCESS if resource exist, else NOT_FOUND 251 */ 252 virtual RState GetIntArrayById(uint32_t id, std::vector<int> &outValue); 253 254 /** 255 * Get the INTARRAY resource by resource name 256 * @param name the resource name 257 * @param outValue the obtain resource value convert to vector<int> write to 258 * @return SUCCESS if resource exist, else NOT_FOUND 259 */ 260 virtual RState GetIntArrayByName(const char *name, std::vector<int> &outValue); 261 262 /** 263 * Get the COLOR resource by resource id 264 * @param id the resource id 265 * @param outValue the obtain resource value convert to uint32_t write to 266 * @return SUCCESS if resource exist, else NOT_FOUND 267 */ 268 virtual RState GetColorById(uint32_t id, uint32_t &outValue); 269 270 /** 271 * Get the COLOR resource by resource name 272 * @param name the resource name 273 * @param outValue the obtain resource value convert to uint32_t write to 274 * @return SUCCESS if resource exist, else NOT_FOUND 275 */ 276 virtual RState GetColorByName(const char *name, uint32_t &outValue); 277 278 /** 279 * Get the PROF resource by resource id 280 * @param id the resource id 281 * @param outValue the obtain resource path write to 282 * @return SUCCESS if resource exist, else NOT_FOUND 283 */ 284 virtual RState GetProfileById(uint32_t id, std::string &outValue); 285 286 /** 287 * Get the PROF resource by resource name 288 * @param name the resource name 289 * @param outValue the obtain resource path write to 290 * @return SUCCESS if resource exist, else NOT_FOUND 291 */ 292 virtual RState GetProfileByName(const char *name, std::string &outValue); 293 294 /** 295 * Get the MEDIA resource by resource id 296 * @param id the resource id 297 * @param outValue the obtain resource path write to 298 * @return SUCCESS if resource exist, else NOT_FOUND 299 */ 300 virtual RState GetMediaById(uint32_t id, std::string &outValue); 301 302 /** 303 * Get the MEDIA resource by resource id with density 304 * @param id the resource id 305 * @param density the screen density, within the area of OHOS::Global::Resource::ScreenDensity 306 * @param outValue the obtain resource path write to 307 * @return SUCCESS if resource exist, else NOT_FOUND 308 */ 309 virtual RState GetMediaById(uint32_t id, uint32_t density, std::string &outValue); 310 311 /** 312 * Get the MEDIA resource by resource name 313 * @param name the resource name 314 * @param outValue the obtain resource path write to 315 * @return SUCCESS if resource exist, else NOT_FOUND 316 */ 317 virtual RState GetMediaByName(const char *name, std::string &outValue); 318 319 /** 320 * Get the MEDIA resource by resource name with density 321 * @param name the resource name 322 * @param density the screen density, within the area of OHOS::Global::Resource::ScreenDensity 323 * @param outValue the obtain resource path write to 324 * @return SUCCESS if resource exist, else NOT_FOUND 325 */ 326 virtual RState GetMediaByName(const char *name, uint32_t density, std::string &outValue); 327 328 /** 329 * Get the raw file path by resource name 330 * @param name the resource name 331 * @param outValue the obtain resource path write to 332 * @return SUCCESS if resource exist, else NOT_FOUND 333 */ 334 virtual RState GetRawFilePathByName(const std::string &name, std::string &outValue); 335 336 /** 337 * Get the rawFile descriptor by resource name 338 * @param name the resource name 339 * @param descriptor the obtain raw file member fd, length, offet write to 340 * @return SUCCESS if resource exist, else ERROR 341 */ 342 virtual RState GetRawFileDescriptor(const std::string &name, RawFileDescriptor &descriptor); 343 344 /** 345 * Close rawFile descriptor by resource name 346 * @param name the resource name 347 * @return SUCCESS if close the rawFile descriptor, else ERROR 348 */ 349 virtual RState CloseRawFileDescriptor(const std::string &name); 350 351 /** 352 * Get all resource paths 353 * @return The vector of resource paths 354 */ 355 std::vector<std::string> GetResourcePaths(); 356 357 /** 358 * Get the MEDIA base64 data resource by resource id with density 359 * @param id the resource id 360 * @param density the screen density, within the area of OHOS::Global::Resource::ScreenDensity 361 * @param base64Data the media base64 data 362 * @return SUCCESS if resource exist, else NOT_FOUND 363 */ 364 virtual RState GetMediaBase64ByIdData(uint32_t id, uint32_t density, std::string &base64Data); 365 366 /** 367 * Get the MEDIA base64 data resource by resource id with density 368 * @param name the resource name 369 * @param density the screen density, within the area of OHOS::Global::Resource::ScreenDensity 370 * @param base64Data the media base64 data 371 * @return SUCCESS if resource exist, else NOT_FOUND 372 */ 373 virtual RState GetMediaBase64ByNameData(const char *name, uint32_t density, std::string &base64Data); 374 375 /** 376 * Get the MEDIA data by resource id 377 * @param id the resource id 378 * @param len the data len write to 379 * @param outValue the obtain resource path write to 380 * @return SUCCESS if resource exist, else NOT_FOUND 381 */ 382 virtual RState GetMediaDataById(uint32_t id, size_t &len, std::unique_ptr<uint8_t[]> &outValue); 383 384 /** 385 * Get the MEDIA data by resource name 386 * @param name the resource name 387 * @param len the data len write to 388 * @param outValue the obtain resource path write to 389 * @return SUCCESS if resource exist, else NOT_FOUND 390 */ 391 virtual RState GetMediaDataByName(const char *name, size_t &len, std::unique_ptr<uint8_t[]> &outValue); 392 393 /** 394 * Get the MEDIA data by resource id with density 395 * @param id the resource id 396 * @param density the screen density, within the area of OHOS::Global::Resource::ScreenDensity 397 * @param len the data len write to 398 * @param outValue the obtain resource path write to 399 * @return SUCCESS if resource exist, else NOT_FOUND 400 */ 401 virtual RState GetMediaDataById(uint32_t id, uint32_t density, size_t &len, std::unique_ptr<uint8_t[]> &outValue); 402 403 /** 404 * Get the MEDIA data by resource name with density 405 * @param name the resource name 406 * @param density the screen density, within the area of OHOS::Global::Resource::ScreenDensity 407 * @param len the data len write to 408 * @param outValue the obtain resource path write to 409 * @return SUCCESS if resource exist, else NOT_FOUND 410 */ 411 virtual RState GetMediaDataByName(const char *name, uint32_t density, size_t &len, 412 std::unique_ptr<uint8_t[]> &outValue); 413 414 /** 415 * Get the MEDIA base64 data resource by resource id 416 * @param id the resource id 417 * @param outValue the media base64 data 418 * @return SUCCESS if resource exist, else NOT_FOUND 419 */ 420 virtual RState GetMediaBase64DataById(uint32_t id, std::string &outValue); 421 422 /** 423 * Get the MEDIA base64 data resource by resource id 424 * @param name the resource name 425 * @param outValue the media base64 data 426 * @return SUCCESS if resource exist, else NOT_FOUND 427 */ 428 virtual RState GetMediaBase64DataByName(const char *name, std::string &outValue); 429 430 /** 431 * Get the MEDIA base64 data resource by resource id with density 432 * @param id the resource id 433 * @param density the screen density, within the area of OHOS::Global::Resource::ScreenDensity 434 * @param outValue the media base64 data 435 * @return SUCCESS if resource exist, else NOT_FOUND 436 */ 437 virtual RState GetMediaBase64DataById(uint32_t id, uint32_t density, std::string &outValue); 438 439 /** 440 * Get the MEDIA base64 data resource by resource id with density 441 * @param name the resource name 442 * @param outValue the media base64 data 443 * @return SUCCESS if resource exist, else NOT_FOUND 444 */ 445 virtual RState GetMediaBase64DataByName(const char *name, uint32_t density, std::string &outValue); 446 447 /** 448 * Get the PROF resource by resource id 449 * @param name the resource id 450 * @param len the data len write to 451 * @param outValue the obtain resource path write to 452 * @return SUCCESS if resource exist, else NOT_FOUND 453 */ 454 virtual RState GetProfileDataById(uint32_t id, size_t &len, std::unique_ptr<uint8_t[]> &outValue); 455 456 /** 457 * Get the PROF resource by resource name 458 * @param name the resource name 459 * @param len the data len write to 460 * @param outValue the obtain resource path write to 461 * @return SUCCESS if resource exist, else NOT_FOUND 462 */ 463 virtual RState GetProfileDataByName(const char *name, size_t &len, std::unique_ptr<uint8_t[]> &outValue); 464 465 /** 466 * Get the rawFile base64 from hap by rawFile name 467 * @param rawFileName the rawFile name 468 * @param rawFile the raw file infomation about offset, length, buffer write to 469 * @return SUCCESS if resource exist, else NOT_FOUND 470 */ 471 virtual RState GetRawFileFromHap(const std::string &rawFileName, std::unique_ptr<RawFile> &rawFile); 472 473 /** 474 * Get the rawFile Descriptor from hap by rawFile name 475 * @param rawFileName the rawFile name 476 * @param descriptor the raw file member fd, length, offet write to 477 * @return SUCCESS if resource exist, else NOT_FOUND 478 */ 479 virtual RState GetRawFileDescriptorFromHap(const std::string &rawFileName, RawFileDescriptor &descriptor); 480 481 /** 482 * Is load hap 483 */ 484 virtual RState IsLoadHap(); 485 486 private: 487 RState GetString(const IdItem *idItem, std::string &outValue); 488 489 RState GetStringArray(const IdItem *idItem, std::vector<std::string> &outValue); 490 491 RState GetPattern(const IdItem *idItem, std::map<std::string, std::string> &outValue); 492 493 RState GetTheme(const IdItem *idItem, std::map<std::string, std::string> &outValue); 494 495 RState GetPluralString(const HapResource::ValueUnderQualifierDir *vuqd, int quantity, std::string &outValue); 496 497 RState ResolveReference(const std::string value, std::string &outValue); 498 499 RState GetBoolean(const IdItem *idItem, bool &outValue); 500 501 RState ParseFloat(const std::string &strValue, float &result, std::string &unit); 502 503 RState RecalculateFloat(const std::string &unit, float &result); 504 505 RState GetFloat(const IdItem *idItem, float &outValue, std::string &unit); 506 507 RState GetInteger(const IdItem *idItem, int &outValue); 508 509 RState GetColor(const IdItem *idItem, uint32_t &outValue); 510 511 RState GetIntArray(const IdItem *idItem, std::vector<int> &outValue); 512 513 RState GetRawFile(const HapResource::ValueUnderQualifierDir *vuqd, const ResType resType, std::string &outValue); 514 515 void ProcessPsuedoTranslate(std::string &outValue); 516 517 RState ResolveParentReference(const IdItem *idItem, std::map<std::string, std::string> &outValue); 518 519 bool IsDensityValid(uint32_t density); 520 521 bool IsFileExist(const std::string& path); 522 523 HapManager *hapManager_; 524 525 float fontRatio_ = 0.0f; 526 527 bool isFakeLocale = false; 528 529 PsueManager *psueManager_; 530 531 const std::string VIRTUAL_PIXEL = "vp"; 532 533 const std::string FONT_SIZE_PIXEL = "fp"; 534 535 const std::string SYSTEM_RESOURCE_PATH = "/data/storage/el1/bundle/ohos.global.systemres" \ 536 "/ohos.global.systemres/assets/entry/resources.index"; 537 538 const std::string SYSTEM_RESOURCE_PATH_COMPRESSED = "/data/storage/el1/bundle/" \ 539 "systemResources/SystemResources.hap"; 540 541 const std::string SYSTEM_RESOURCE_OVERLAY_PATH = "/data/storage/el1/bundle/" \ 542 "ohos.global.systemres.overlay/ohos.global.systemres.overlay/assets/entry/resources.index"; 543 544 const std::string SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED = "/data/storage/el1/bundle/" \ 545 "ohos.global.systemres.overlay/ohos.global.systemres.overlay/SystemResourcesOverlay.hap"; 546 547 std::unordered_map<std::string, RawFileDescriptor> rawFileDescriptor_; 548 }; 549 } // namespace Resource 550 } // namespace Global 551 } // namespace OHOS 552 #endif