• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_RESCONFIG_IMPL_H
16 #define OHOS_RESOURCE_MANAGER_RESCONFIG_IMPL_H
17 
18 #include <stdint.h>
19 #include "res_locale.h"
20 #include "res_common.h"
21 #include "res_config.h"
22 
23 #ifdef SUPPORT_GRAPHICS
24 using icu::Locale;
25 #endif
26 namespace OHOS {
27 namespace Global {
28 namespace Resource {
29 class ResConfigImpl : public ResConfig {
30 public:
31     ResConfigImpl();
32 
33     /**
34      * Whether this resConfig more match request resConfig
35      * @param other the other resConfig
36      * @param request  the request resConfig
37      * @param density the input screen density
38      * @return true if this resConfig more match request resConfig than other resConfig, else false
39      */
40     bool IsMoreSuitable(const std::shared_ptr<ResConfigImpl> other, const std::shared_ptr<ResConfigImpl> request,
41         uint32_t density = 0) const;
42 
43     /**
44      * Set locale information
45      * @param language the locale language
46      * @param script  the locale script
47      * @param region  the locale region
48      * @return SUCCESS if set locale information success, else false
49      */
50     RState SetLocaleInfo(const char *language, const char *script, const char *region);
51 
52     /**
53      * Set locale information
54      * @param localeStr the locale info, for example, zh-Hans-CN.
55      * @return SUCCESS if set locale information success, else false
56      */
57     RState SetLocaleInfo(const char *localeStr);
58 
59 #ifdef SUPPORT_GRAPHICS
60     RState SetPreferredLocaleInfo(Locale &preferredLocaleInfo);
61 
62     RState SetLocaleInfo(Locale &localeInfo);
63 #endif
64     /**
65      * Set resConfig device type
66      * @param deviceType the device type
67      */
68     void SetDeviceType(DeviceType deviceType);
69 
70     /**
71      * Set resConfig direction
72      * @param direction the resConfig direction
73      */
74     void SetDirection(Direction direction);
75 
76     /**
77      * Set resConfig colorMode
78      * @param colorMode the resConfig colorMode
79      */
80     void SetColorMode(ColorMode colorMode);
81 
82     /**
83      * Set resConfig mcc
84      * @param mcc the resConfig mcc
85      */
86     void SetMcc(uint32_t mcc);
87 
88     /**
89      * Set resConfig mnc
90      * @param mnc the resConfig mnc
91      */
92     void SetMnc(uint32_t mnc);
93 
94     /**
95      * Set resConfig screenDensity
96      * @param screenDensity the resConfig screenDensity
97      */
98     void SetScreenDensity(float screenDensity);
99 
100     /**
101      * Set resConfig screenDensityDpi
102      * @param screenDensityDpi the resConfig screenDensityDpi
103      */
104     void SetScreenDensityDpi(ScreenDensity screenDensityDpi);
105 
106     /**
107      * Set resConfig themeId
108      * @param themeId the resConfig themeId
109      */
110     void SetThemeId(uint32_t themeId);
111 
112     /**
113      * Set resConfig themeIcon
114      * @param isIcon the resConfig themeIcon flag
115      */
116     void SetThemeIcon(bool isIcon);
117 
118 #ifdef SUPPORT_GRAPHICS
119     const ResLocale *GetResPreferredLocale() const;
120 
121     const Locale *GetPreferredLocaleInfo() const;
122 
123     const Locale *GetLocaleInfo() const;
124 #endif
125 
126     const ResLocale *GetResLocale() const;
127 
128     Direction GetDirection() const;
129 
130     float GetScreenDensity() const;
131 
132     ScreenDensity GetScreenDensityDpi() const;
133 
134     bool CopyLocaleAndPreferredLocale(ResConfig &other);
135 
136     bool isLocaleInfoSet();
137 
138     ColorMode GetColorMode() const;
139 
140     uint32_t GetMcc() const;
141 
142     uint32_t GetMnc() const;
143 
144     uint32_t GetThemeId() const;
145 
146     bool GetThemeIcon() const;
147 
148     DeviceType GetDeviceType() const;
149 
150     /**
151      * Whether this resConfig match other resConfig
152      * @param other the other resConfig
153      * @param isCheckDarkAdaptation whether check the color mode match the dark mode
154      * @return true if this resConfig match other resConfig, else false
155      */
156     bool Match(const std::shared_ptr<ResConfigImpl> other, bool isCheckDarkAdaptation = true) const;
157 
158     /**
159      * Copy other resConfig to this resConfig
160      * @param other the other resConfig
161      * @param isRead the isAppDarkRes_ is read-only
162      * @return true if copy other resConfig to this resConfig success, else false
163      */
164     bool Copy(ResConfig &other, bool isRead = false);
165 
166     /**
167      * Complete the local script
168      */
169     void CompleteScript();
170 
171     /**
172      * Whether this resLocal script completed
173      * @return true if resLocal script completed, else false
174      */
175     bool IsCompletedScript() const;
176 
177     /**
178      * Set resConfig input device
179      * @param inputDevice the resConfig input device
180      */
181     void SetInputDevice(InputDevice inputDevice);
182 
183     /**
184      * Get resConfig input device
185      * @return the resConfig input device
186      */
187     InputDevice GetInputDevice() const;
188 
189     /**
190      * Matching screen density by screen density ratio
191      * @return ScreenDensity
192      */
193     ScreenDensity ConvertDensity(float density);
194 
195     virtual ~ResConfigImpl();
196 
197     /**
198      * Set colorMode is setted by app
199      * @param isAppColorMode whether colorMode is setted by app
200      */
201     void SetAppColorMode(bool isAppColorMode);
202 
203     /**
204      * Get whether colorMode is setted by app
205      * @return True if colorMode is setted by app
206      */
207     bool GetAppColorMode() const;
208 
209     /**
210      * Set whether the app has dark resources
211      * @param isAppDarkRes whether the app has dark resources
212      */
213     void SetAppDarkRes(bool isAppDarkRes);
214 
215     /**
216      * Get whether the app has dark resources
217      * @return True if the app has dark resources
218      */
219     bool GetAppDarkRes() const;
220 
221     bool MatchLocal(ResConfig& other) const;
222 
223     std::string ToString() const;
224 
225     std::string GetDeviceTypeStr() const;
226 
227     std::string GetScreenDensityStr() const;
228 private:
229     bool IsMoreSpecificThan(const std::shared_ptr<ResConfigImpl> other, uint32_t density = 0) const;
230 
231     bool CopyLocale(ResConfig &other);
232 
233 #ifdef SUPPORT_GRAPHICS
234     bool CopyPreferredLocale(ResConfig &other);
235 
236     bool CopyLocale(Locale **currentLocaleInfo, ResLocale **currentResLocale,
237         const Locale *otherLocaleInfo);
238 #endif
239 
240     bool IsMccMncMatch(uint32_t mcc,  uint32_t mnc) const;
241 
242     bool IsDirectionMatch(Direction direction) const;
243 
244     bool IsDeviceTypeMatch(DeviceType deviceType) const;
245 
246     bool IsColorModeMatch(ColorMode colorMode, bool isCheckDarkAdaptation) const;
247 
248     bool IsInputDeviceMatch(InputDevice inputDevice) const;
249 
250     int IsMccMncMoreSuitable(uint32_t otherMcc, uint32_t otherMnc, uint32_t requestMcc, uint32_t requestMnc) const;
251 
252     int IsDensityMoreSuitable(ScreenDensity otherDensity, ScreenDensity requestDensity, uint32_t density = 0) const;
253 
254     bool IsDensityMoreSuitable(int thisDistance, int otherDistance) const;
255 
256     int IsDensityMoreSpecificThan(ScreenDensity otherDensity, uint32_t density = 0) const;
257 
258 #ifdef SUPPORT_GRAPHICS
259     RState BuildResLocale(const char *language, const char *script, const char *region, ResLocale **resLocale);
260 
261     RState BuildLocaleInfo(const ResLocale *resLocale, Locale **localeInfo);
262 #endif
263 private:
264     ResLocale *resLocale_;
265     Direction direction_;
266     float density_;
267     ScreenDensity screenDensityDpi_;
268     ColorMode colorMode_;
269     uint32_t mcc_;
270     uint32_t mnc_;
271     DeviceType deviceType_;
272     InputDevice inputDevice_;
273 #ifdef SUPPORT_GRAPHICS
274     ResLocale *resPreferredLocale_;
275     Locale *preferredLocaleInfo_;
276     Locale *localeInfo_;
277 #endif
278     uint32_t themeId_;
279     bool isCompletedScript_;
280     bool isAppColorMode_;
281     bool isAppDarkRes_;
282     bool isThemeIcon_;
283 };
284 } // namespace Resource
285 } // namespace Global
286 } // namespace OHOS
287 #endif