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 #ifndef OHOS_THEME_PACK_MANAGER_H 16 #define OHOS_THEME_PACK_MANAGER_H 17 18 #include <mutex> 19 #include <unordered_map> 20 21 #include "hap_resource.h" 22 #include "res_common.h" 23 #include "res_config_impl.h" 24 #include "res_desc.h" 25 #include "resource_manager.h" 26 #include "theme_pack_config.h" 27 #include "theme_pack_resource.h" 28 29 namespace OHOS { 30 namespace Global { 31 namespace Resource { 32 class ThemePackManager { 33 public: 34 static std::shared_ptr<ThemePackManager> GetThemePackManager(); 35 36 ~ThemePackManager(); 37 38 /** 39 * Load the theme resource. 40 * 41 * @param bundleName the hap bundleName 42 * @param moduleName the hap moduleName 43 * @param userId the uesr id 44 */ 45 void LoadThemeRes(const std::string &bundleName, const std::string &moduleName, int32_t userId); 46 47 /** 48 * Load the theme resource. 49 * 50 * @param bundleName the hap bundleName 51 * @param moduleName the hap moduleName 52 * @param userId the uesr id 53 */ 54 void LoadSAThemeRes(const std::string &bundleName, const std::string &moduleName, 55 int32_t userId, std::vector<std::string> &rootDirs, std::vector<std::string> &iconDirs); 56 57 /** 58 * Load the skin dir resource in theme pack. 59 * 60 * @param bundleName the hap bundleName 61 * @param moduleName the hap moduleName 62 * @param rootDirs the theme skins dirs 63 */ 64 void LoadThemeSkinResource(const std::string &bundleName, const std::string &moduleName, 65 const std::vector<std::string> &rootDirs, int32_t userId); 66 67 /** 68 * Load the icons dir resource int theme pack. 69 * 70 * @param bundleName the bundleName 71 * @param moduleName the moduleName 72 * @param rootDirs the theme icons dirs 73 */ 74 void LoadThemeIconsResource(const std::string &bundleName, const std::string &moduleName, 75 const std::vector<std::string> &rootDirs, int32_t userId); 76 77 /** 78 * Get the theme resource related to bundlename, modulename, resType, resName and resConfig. 79 * 80 * @param bundleInfo which contains bundlename, modulename 81 * @param resType the resoucre type 82 * @param resName the resource name 83 * @param resConfig the resource config 84 * @param userId the user id 85 */ 86 const std::string GetThemeResource(const std::pair<std::string, std::string> &bundleInfo, 87 const ResType &resType, const std::string &resName, const ResConfigImpl &resConfig, int32_t userId); 88 89 /** 90 * Find the best theme resource related to bundlename, modulename, idItems and resConfig. 91 * 92 * @param bundleInfo which contains bundlename, modulename 93 * @param idItems which used to process the reference resource 94 * @param resConfig the resource config 95 * @param userId the user id 96 * @param isThemeSystemResEnable true is theme system res enable 97 * @return the best resource or empty 98 */ 99 const std::string FindThemeResource(const std::pair<std::string, std::string> &bundleInfo, 100 const std::vector<std::shared_ptr<IdItem>> &idItems, const ResConfigImpl &resConfig, int32_t userId, 101 bool isThemeSystemResEnable = false); 102 103 /** 104 * Find the best icon resource related to bundlename, modulename, resource name. 105 * 106 * @param bundleInfo which contains bundlename, modulename 107 * @param iconName the icon resource name 108 * @param userId the user id 109 * @param abilityName the hap abilityName 110 * @return the best resource or empty 111 */ 112 const std::string FindThemeIconResource(const std::pair<std::string, std::string> &bundleInfo, 113 const std::string &iconName, int32_t userId, const std::string &abilityName = ""); 114 GetMask()115 inline const std::string GetMask() const 116 { 117 return themeMask; 118 } 119 120 const std::string ReplaceUserIdInPath(const std::string &originalPath, int32_t userId); 121 122 bool UpdateThemeId(uint32_t newThemeId); 123 124 bool IsFirstLoadResource(); 125 126 /** 127 * Whether an icon exists in the theme 128 * 129 * @param bundleName the hap bundleName 130 * @param userId the user id 131 * @return true if icon exists, else no exists 132 */ 133 bool HasIconInTheme(const std::string &bundleName, int32_t userId); 134 135 /** 136 * Get icons info in other icons by icon name 137 * 138 * @param iconName the icon name 139 * @param outValue the obtain resource wirte to 140 * @param len the data len wirte to 141 * @param isGlobalMask true if the global mask, else other icons 142 * @param userId the user id 143 * @return SUCCESS if the theme icon get success, else failed 144 */ 145 RState GetOtherIconsInfo(const std::string &iconName, 146 std::unique_ptr<uint8_t[]> &outValue, size_t &len, bool isGlobalMask, int32_t userId); 147 148 /** 149 * Get the theme icon from cache 150 * 151 * @param iconTag the tag of icon info 152 * @param outValue the obtain resource wirte to 153 * @param len the data len wirte to 154 * @return SUCCESS if the theme icon get success, else failed 155 */ 156 RState GetThemeIconFromCache(const std::string &iconTag, std::unique_ptr<uint8_t[]> &outValue, size_t &len); 157 158 /** 159 * Whether to update theme by the user id 160 * 161 * @param userId the current user id 162 * @return true if update theme by the user id, else not update 163 */ 164 bool IsUpdateByUserId(int32_t userId); 165 166 void LoadThemeIconRes(const std::string &bundleName, const std::string &moduleName, int32_t userId); 167 168 private: 169 ThemePackManager(); 170 std::string themeMask; 171 void ChangeSkinResourceStatus(int32_t userId); 172 void ChangeIconResourceStatus(int32_t userId); 173 void ClearSkinResource(); 174 void ClearIconResource(); 175 const std::string GetMaskString(const std::string &path); 176 std::vector<std::shared_ptr<ThemeResource>> skinResource_; 177 std::vector<std::shared_ptr<ThemeResource>> iconResource_; 178 std::vector<std::tuple<std::string, std::unique_ptr<uint8_t[]>, size_t>> iconMaskValues_; 179 std::vector<std::shared_ptr<ThemeResource::ThemeValue> > GetThemeResourceList( 180 const std::pair<std::string, std::string> &bundInfo, const ResType &resType, const std::string &resName, 181 int32_t userId); 182 183 const std::shared_ptr<ThemeResource::ThemeQualifierValue> GetThemeQualifierValue( 184 const std::pair<std::string, std::string> &bundInfo, const ResType &resType, 185 const std::string &resName, const ResConfigImpl &resConfig, int32_t userId); 186 187 const std::shared_ptr<ThemeResource::ThemeQualifierValue> GetBestMatchThemeResource( 188 const std::vector<std::shared_ptr<ThemeResource::ThemeValue> > &candidates, 189 const ResConfigImpl &resConfig); 190 191 std::vector<std::string> GetRootDir(const std::string &strCurrentDir); 192 bool IsSameResourceByUserId(const std::string &path, int32_t userId); 193 void UpdateUserId(int32_t userId); 194 std::mutex lockSkin_; 195 std::mutex lockIcon_; 196 std::mutex lockThemeId_; 197 std::mutex lockIconValue_; 198 std::mutex lockUserId_; 199 uint32_t themeId_{0}; 200 bool isFirstCreate = true; 201 int32_t currentUserId_ = 0; 202 bool isLogFlag_ = false; 203 }; 204 } // namespace Resource 205 } // namespace Global 206 } // namespace OHOS 207 #endif