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