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 #ifdef SUPPORT_GRAPHICS
113     const ResLocale *GetResPreferredLocale() const;
114 
115     const Locale *GetPreferredLocaleInfo() const;
116 
117     const Locale *GetLocaleInfo() const;
118 #endif
119 
120     const ResLocale *GetResLocale() const;
121 
122     Direction GetDirection() const;
123 
124     float GetScreenDensity() const;
125 
126     ScreenDensity GetScreenDensityDpi() const;
127 
128     bool CopyLocaleAndPreferredLocale(ResConfig &other);
129 
130     bool isLocaleInfoSet();
131 
132     ColorMode GetColorMode() const;
133 
134     uint32_t GetMcc() const;
135 
136     uint32_t GetMnc() const;
137 
138     uint32_t GetThemeId() const;
139 
140     DeviceType GetDeviceType() const;
141 
142     /**
143      * Whether this resConfig match other resConfig
144      * @param other the other resConfig
145      * @param isCheckDarkAdaptation whether check the color mode match the dark mode
146      * @return true if this resConfig match other resConfig, else false
147      */
148     bool Match(const std::shared_ptr<ResConfigImpl> other, bool isCheckDarkAdaptation = true) const;
149 
150     /**
151      * Copy other resConfig to this resConfig
152      * @param other the other resConfig
153      * @param isRead the isAppDarkRes_ is read-only
154      * @return true if copy other resConfig to this resConfig success, else false
155      */
156     bool Copy(ResConfig &other, bool isRead = false);
157 
158     /**
159      * Complete the local script
160      */
161     void CompleteScript();
162 
163     /**
164      * Whether this resLocal script completed
165      * @return true if resLocal script completed, else false
166      */
167     bool IsCompletedScript() const;
168 
169     /**
170      * Set resConfig input device
171      * @param inputDevice the resConfig input device
172      */
173     void SetInputDevice(InputDevice inputDevice);
174 
175     /**
176      * Get resConfig input device
177      * @return the resConfig input device
178      */
179     InputDevice GetInputDevice() const;
180 
181     /**
182      * Matching screen density by screen density ratio
183      * @return ScreenDensity
184      */
185     ScreenDensity ConvertDensity(float density);
186 
187     virtual ~ResConfigImpl();
188 
189     /**
190      * Set colorMode is setted by app
191      * @param isAppColorMode whether colorMode is setted by app
192      */
193     void SetAppColorMode(bool isAppColorMode);
194 
195     /**
196      * Get whether colorMode is setted by app
197      * @return True if colorMode is setted by app
198      */
199     bool GetAppColorMode() const;
200 
201     /**
202      * Set whether the app has dark resources
203      * @param isAppDarkRes whether the app has dark resources
204      */
205     void SetAppDarkRes(bool isAppDarkRes);
206 
207     /**
208      * Get whether the app has dark resources
209      * @return True if the app has dark resources
210      */
211     bool GetAppDarkRes() const;
212 
213 private:
214     bool IsMoreSpecificThan(const std::shared_ptr<ResConfigImpl> other, uint32_t density = 0) const;
215 
216     bool CopyLocale(ResConfig &other);
217 
218 #ifdef SUPPORT_GRAPHICS
219     bool CopyPreferredLocale(ResConfig &other);
220 
221     bool CopyLocale(Locale **currentLocaleInfo, ResLocale **currentResLocale,
222         const Locale *otherLocaleInfo);
223 #endif
224 
225     bool IsMccMncMatch(uint32_t mcc,  uint32_t mnc) const;
226 
227     bool IsDirectionMatch(Direction direction) const;
228 
229     bool IsDeviceTypeMatch(DeviceType deviceType) const;
230 
231     bool IsColorModeMatch(ColorMode colorMode, bool isCheckDarkAdaptation) const;
232 
233     bool IsInputDeviceMatch(InputDevice inputDevice) const;
234 
235     int IsMccMncMoreSuitable(uint32_t otherMcc, uint32_t otherMnc, uint32_t requestMcc, uint32_t requestMnc) const;
236 
237     int IsDensityMoreSuitable(ScreenDensity otherDensity, ScreenDensity requestDensity, uint32_t density = 0) const;
238 
239     bool IsDensityMoreSuitable(int thisDistance, int otherDistance) const;
240 
241     int IsDensityMoreSpecificThan(ScreenDensity otherDensity, uint32_t density = 0) const;
242 
243 #ifdef SUPPORT_GRAPHICS
244     RState BuildResLocale(const char *language, const char *script, const char *region, ResLocale **resLocale);
245 
246     RState BuildLocaleInfo(const ResLocale *resLocale, Locale **localeInfo);
247 #endif
248 private:
249     ResLocale *resLocale_;
250     Direction direction_;
251     float density_;
252     ScreenDensity screenDensityDpi_;
253     ColorMode colorMode_;
254     uint32_t mcc_;
255     uint32_t mnc_;
256     DeviceType deviceType_;
257     InputDevice inputDevice_;
258 #ifdef SUPPORT_GRAPHICS
259     ResLocale *resPreferredLocale_;
260     Locale *preferredLocaleInfo_;
261     Locale *localeInfo_;
262 #endif
263     bool isCompletedScript_;
264     bool isAppColorMode_;
265     bool isAppDarkRes_;
266     uint32_t themeId_;
267 };
268 } // namespace Resource
269 } // namespace Global
270 } // namespace OHOS
271 #endif