• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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      */
84     const std::string GetThemeResource(const std::pair<std::string, std::string> &bundleInfo,
85         const ResType &resType, const std::string &resName, const ResConfigImpl &resConfig);
86 
87     /**
88      * Find the best theme resource related to bundlename, modulename, idItems and resConfig.
89      *
90      * @param bundleInfo which contains bundlename, modulename
91      * @param idItems which used to process the reference resource
92      * @param resConfig the resource config
93      * @return the best resource or empty
94      */
95     const std::string FindThemeResource(const std::pair<std::string, std::string> &bundleInfo,
96         std::vector<std::shared_ptr<IdItem>> idItems, const ResConfigImpl &resConfig,
97         bool isThemeSystemResEnable = false);
98 
99     /**
100      * Find the best icon resource related to bundlename, modulename, resource name.
101      *
102      * @param bundleInfo which contains bundlename, modulename
103      * @param iconName the icon resource name
104      * @param abilityName the hap abilityName
105      * @return the best resource or empty
106      */
107     const std::string FindThemeIconResource(const std::pair<std::string, std::string> &bundleInfo,
108         const std::string &iconName, const std::string &abilityName = "");
109 
GetMask()110     inline const std::string GetMask() const
111     {
112         return themeMask;
113     }
114 
115     const std::string ReplaceUserIdInPath(const std::string &originalPath, int32_t userId);
116 
117     bool UpdateThemeId(uint32_t newThemeId);
118 
119     bool IsFirstLoadResource();
120 
121     /**
122      * Whether an icon exists in the theme
123      *
124      * @param bundleName the hap bundleName
125      * @return true if icon exists, else no exists
126      */
127     bool HasIconInTheme(const std::string &bundleName);
128 
129     /**
130      * Get icons info in other icons by icon name
131      *
132      * @param iconName the icon name
133      * @param outValue the obtain resource wirte to
134      * @param len the data len wirte to
135      * @param isGlobalMask true if the global mask, else other icons
136      * @return SUCCESS if the theme icon get success, else failed
137      */
138     RState GetOtherIconsInfo(const std::string &iconName,
139         std::unique_ptr<uint8_t[]> &outValue, size_t &len, bool isGlobalMask);
140 
141     /**
142      * Get the theme icon from cache
143      *
144      * @param iconTag the tag of icon info
145      * @param outValue the obtain resource wirte to
146      * @param len the data len wirte to
147      * @return SUCCESS if the theme icon get success, else failed
148      */
149     RState GetThemeIconFromCache(const std::string &iconTag, std::unique_ptr<uint8_t[]> &outValue, size_t &len);
150 
151     /**
152      * Whether to update theme by the user id
153      *
154      * @param userId the current user id
155      * @return true if update theme by the user id, else not update
156      */
157     bool IsUpdateByUserId(int32_t userId);
158 private:
159     ThemePackManager();
160     std::string themeMask;
161     void ClearSkinResource();
162     void ClearIconResource();
163     std::vector<std::shared_ptr<ThemeResource>> skinResource_;
164     std::vector<std::shared_ptr<ThemeResource>> iconResource_;
165     std::vector<std::tuple<std::string, std::unique_ptr<uint8_t[]>, size_t>> iconMaskValues_;
166     std::vector<std::shared_ptr<ThemeResource::ThemeValue> > GetThemeResourceList(
167         const std::pair<std::string, std::string> &bundInfo, const ResType &resType, const std::string &resName);
168 
169     const std::shared_ptr<ThemeResource::ThemeQualifierValue> GetThemeQualifierValue(
170         const std::pair<std::string, std::string> &bundInfo, const ResType &resType,
171         const std::string &resName, const ResConfigImpl &resConfig);
172 
173     const std::shared_ptr<ThemeResource::ThemeQualifierValue> GetBestMatchThemeResource(
174         const std::vector<std::shared_ptr<ThemeResource::ThemeValue> > &candidates,
175         const ResConfigImpl &resConfig);
176 
177     std::vector<std::string> GetRootDir(const std::string &strCurrentDir);
178     void UpdateUserId(int32_t userId);
179     Lock lockSkin_;
180     Lock lockIcon_;
181     Lock lockThemeId_;
182     Lock lockIconValue_;
183     Lock lockUserId_;
184     uint32_t themeId_{0};
185     bool isFirstCreate = true;
186     int32_t currentUserId_ = 100;
187 };
188 } // namespace Resource
189 } // namespace Global
190 } // namespace OHOS
191 #endif