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_HAPMANAGER_H 16 #define OHOS_RESOURCE_MANAGER_HAPMANAGER_H 17 18 #include "res_config_impl.h" 19 #include "hap_resource.h" 20 #include "res_desc.h" 21 #include "resource_manager.h" 22 #include "lock.h" 23 24 #ifdef SUPPORT_GRAPHICS 25 #include <unicode/plurrule.h> 26 #endif 27 28 namespace OHOS { 29 namespace Global { 30 namespace Resource { 31 class HapManager { 32 public: 33 /** 34 * The constructor of HapManager 35 */ 36 explicit HapManager(ResConfigImpl *resConfig); 37 38 /** 39 * The destructor of HapManager 40 */ 41 ~HapManager(); 42 43 /** 44 * Update the resConfig 45 * @param resConfig the resource config 46 * @return SUCCESS if the resConfig updated success, else HAP_INIT_FAILED 47 */ 48 RState UpdateResConfig(ResConfig &resConfig); 49 50 /** 51 * Get the resConfig 52 * @param resConfig the resource config 53 */ 54 void GetResConfig(ResConfig &resConfig); 55 56 /** 57 * Add resource path to hap paths 58 * @param path the resource path 59 * @return true if add resource path success, else false 60 */ 61 bool AddResource(const char *path); 62 63 /** 64 * Add resource path to overlay paths 65 * @param path the resource path 66 * @param overlayPaths the exist overlay resource path 67 * @return true if add resource path success, else false 68 */ 69 bool AddResource(const std::string &path, const std::vector<std::string> &overlayPaths); 70 71 /** 72 * Find resource by resource id 73 * @param id the resource id 74 * @return the resources related to resource id 75 */ 76 const IdItem *FindResourceById(uint32_t id); 77 78 /** 79 * Find resource by resource name 80 * @param name the resource name 81 * @param resType the resource type 82 * @return the resources related to resource name 83 */ 84 const IdItem *FindResourceByName(const char *name, const ResType resType); 85 86 /** 87 * Find best resource path by resource id 88 * @param id the resource id 89 * @param density the input screen density 90 * @return the best resource path 91 */ 92 const HapResource::ValueUnderQualifierDir *FindQualifierValueById(uint32_t id, uint32_t density = 0); 93 94 /** 95 * Find best resource path by resource name 96 * @param name the resource name 97 * @param resType the resource type 98 * @param density the input screen density 99 * @return the best resource path 100 */ 101 const HapResource::ValueUnderQualifierDir *FindQualifierValueByName(const char *name, const ResType resType, 102 uint32_t density = 0); 103 104 /** 105 * Find the raw file path 106 * @param name the resource name 107 * @param outValue raw file path 108 * @return SUCCESS if find the raw file path success, else NOT_FOUND 109 */ 110 RState FindRawFile(const std::string &name, std::string &outValue); 111 112 /** 113 * Get the language pluralRule related to quantity 114 * @param quantity the language quantity 115 * @return the language pluralRule related to quantity 116 */ 117 std::string GetPluralRulesAndSelect(int quantity); 118 119 /** 120 * Get resource paths vector 121 */ 122 std::vector<std::string> GetResourcePaths(); 123 124 /** 125 * Get the media data 126 * @param qd the QualifierDir 127 * @param len the data len 128 * @param outValue the media data 129 * @return SUCCESS if get the media data success, else NOT_FOUND 130 */ 131 RState GetMediaData(const HapResource::ValueUnderQualifierDir *qd, size_t& len, 132 std::unique_ptr<uint8_t[]> &outValue); 133 134 /** 135 * Get the mediabase64 data 136 * @param qd the QualifierDir 137 * @param outValue the mediabase64 data 138 * @return SUCCESS if get the mediabase64 data success, else NOT_FOUND 139 */ 140 RState GetMediaBase64Data(const HapResource::ValueUnderQualifierDir *qd, std::string &outValue); 141 142 /** 143 * Get the Profile data 144 * @param qd the QualifierDir 145 * @param len the data len write to 146 * @param outValue the profile data 147 * @return SUCCESS if get the profile data success, else NOT_FOUND 148 */ 149 RState GetProfileData(const HapResource::ValueUnderQualifierDir *qd, size_t &len, 150 std::unique_ptr<uint8_t[]> &outValue); 151 152 /** 153 * Find raw file from hap 154 * @param rawFileName the rawFileName 155 * @param rawFile the raw file infomation about offset, length, data write to 156 * @return SUCCESS if resource exist, else NOT_FOUND 157 */ 158 RState FindRawFileFromHap(const std::string &rawFileName, std::unique_ptr<ResourceManager::RawFile> &rawFile); 159 160 /** 161 * Is load hap 162 */ 163 bool IsLoadHap(); 164 165 private: 166 void UpdateResConfigImpl(ResConfigImpl &resConfig); 167 168 void GetResConfigImpl(ResConfigImpl &resConfig); 169 170 std::vector<const HapResource::IdValues *> GetResourceList(uint32_t ident) const; 171 172 std::vector<const HapResource::IdValues *> GetResourceListByName(const char *name, const ResType resType) const; 173 174 const HapResource::ValueUnderQualifierDir *GetBestMatchResource(std::vector<const HapResource::IdValues *> 175 candidates, uint32_t density); 176 177 bool AddResourcePath(const char *path); 178 179 // when resConfig_ updated we must call ReloadAll() 180 RState ReloadAll(); 181 182 static bool Init(); 183 184 static bool icuInitialized; 185 186 // app res config 187 ResConfigImpl *resConfig_; 188 189 // set of hap Resources 190 std::vector<HapResource *> hapResources_; 191 192 // set of loaded hap path 193 std::unordered_map<std::string, std::vector<std::string>> loadedHapPaths_; 194 195 #ifdef SUPPORT_GRAPHICS 196 // key is language 197 std::vector<std::pair<std::string, icu::PluralRules *>> plurRulesCache_; 198 #endif 199 200 Lock lock_; 201 }; 202 } // namespace Resource 203 } // namespace Global 204 } // namespace OHOS 205 #endif