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 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_RESOURCE_CONFIGURATION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_RESOURCE_CONFIGURATION_H
18 
19 #include <string>
20 #include <vector>
21 
22 #include "base/json/json_util.h"
23 #include "base/utils/device_type.h"
24 #include "base/utils/device_config.h"
25 
26 namespace OHOS::Ace {
27 
28 class ResourceConfiguration {
29 public:
30     static constexpr uint32_t COLOR_MODE_UPDATED_FLAG = 0x00000001;
31     static constexpr uint32_t FONT_RATIO_UPDATED_FLAG = 0x00000002;
TestFlag(uint32_t flags,uint32_t flag)32     static bool TestFlag(uint32_t flags, uint32_t flag)
33     {
34         return (flags & flag);
35     }
36 
37 public:
SetDeviceType(const DeviceType & deviceType)38     void SetDeviceType(const DeviceType& deviceType)
39     {
40         deviceType_ = deviceType;
41     }
42 
GetDeviceType()43     DeviceType GetDeviceType() const
44     {
45         return deviceType_;
46     }
47 
SetOrientation(const DeviceOrientation & orientation)48     void SetOrientation(const DeviceOrientation& orientation)
49     {
50         orientation_ = orientation;
51     }
52 
GetOrientation()53     DeviceOrientation GetOrientation() const
54     {
55         return orientation_;
56     }
57 
SetDensity(const double & density)58     void SetDensity(const double& density)
59     {
60         density_ = density;
61     }
62 
GetDensity()63     double GetDensity() const
64     {
65         return density_;
66     }
67 
SetFontRatio(double fontRatio)68     void SetFontRatio(double fontRatio)
69     {
70         fontRatio_ = fontRatio;
71     }
72 
GetFontRatio()73     double GetFontRatio() const
74     {
75         return fontRatio_;
76     }
77 
SetColorMode(const ColorMode & colorMode)78     void SetColorMode(const ColorMode& colorMode)
79     {
80         colorMode_ = colorMode;
81     }
82 
GetColorMode()83     ColorMode GetColorMode() const
84     {
85         return colorMode_;
86     }
87 
SetDeviceAccess(bool isDeviceAccess)88     void SetDeviceAccess(bool isDeviceAccess)
89     {
90         isDeviceAccess_ = isDeviceAccess;
91     }
92 
GetDeviceAccess()93     bool GetDeviceAccess() const
94     {
95         return isDeviceAccess_;
96     }
97 
98     bool UpdateFromJsonString(const std::string jsonStr, uint32_t& updateFlags);
99 
SetColorModeIsSetByApp(bool colorModeIsSetByApp)100     void SetColorModeIsSetByApp(bool colorModeIsSetByApp)
101     {
102         colorModeIsSetByApp_ = colorModeIsSetByApp;
103     }
104 
GetColorModeIsSetByApp()105     bool GetColorModeIsSetByApp() const
106     {
107         return colorModeIsSetByApp_;
108     }
109 
SetMcc(uint32_t mcc)110     void SetMcc(uint32_t mcc)
111     {
112         mcc_ = mcc;
113     }
114 
GetMcc()115     uint32_t GetMcc() const
116     {
117         return mcc_;
118     }
119 
SetMnc(uint32_t mnc)120     void SetMnc(uint32_t mnc)
121     {
122         mnc_ = mnc;
123     }
124 
GetMnc()125     uint32_t GetMnc() const
126     {
127         return mnc_;
128     }
129 
SetAppHasDarkRes(bool hasDarkRes)130     void SetAppHasDarkRes(bool hasDarkRes)
131     {
132         appHasDarkRes_ = hasDarkRes;
133     }
134 
GetAppHasDarkRes()135     bool GetAppHasDarkRes() const
136     {
137         return appHasDarkRes_;
138     }
139 
SetPreferredLanguage(const std::string & preferredLanguage)140     void SetPreferredLanguage(const std::string& preferredLanguage)
141     {
142         preferredLanguage_ = preferredLanguage;
143     }
144 
GetPreferredLanguage()145     const std::string& GetPreferredLanguage() const
146     {
147         return preferredLanguage_;
148     }
149 
SetLanguage(const std::string & language)150     void SetLanguage(const std::string& language)
151     {
152         language_ = language;
153     }
154 
GetLanguage()155     const std::string& GetLanguage() const
156     {
157         return language_;
158     }
159 
160 private:
161     bool ParseJsonColorMode(const std::unique_ptr<JsonValue>& jsonConfig, uint32_t& updateFlags);
162     bool ParseJsonFontRatio(const std::unique_ptr<JsonValue>& jsonConfig, uint32_t& updateFlags);
163 
164 private:
165     DeviceType deviceType_ = DeviceType::PHONE;
166     DeviceOrientation orientation_ = DeviceOrientation::PORTRAIT;
167     double density_ = 1.0;
168     double fontRatio_ = 1.0;
169     bool isDeviceAccess_ = false;
170     ColorMode colorMode_ = ColorMode::LIGHT;
171     bool colorModeIsSetByApp_ = false;
172     bool appHasDarkRes_ = false;
173     uint32_t mcc_ = 0;
174     uint32_t mnc_ = 0;
175     std::string preferredLanguage_;
176     std::string language_;
177 };
178 
179 class ResourceInfo {
180 public:
SetResourceConfiguration(const ResourceConfiguration & resourceConfiguration)181     void SetResourceConfiguration(const ResourceConfiguration& resourceConfiguration)
182     {
183         resourceConfiguration_ = resourceConfiguration;
184     }
185 
GetResourceConfiguration()186     ResourceConfiguration GetResourceConfiguration() const
187     {
188         return resourceConfiguration_;
189     }
190 
SetResourceHandlers(const std::vector<int64_t> & resourcehandlers)191     void SetResourceHandlers(const std::vector<int64_t>& resourcehandlers)
192     {
193         resourcehandlers_ = resourcehandlers;
194     }
195 
GetResourceHandlers()196     std::vector<int64_t> GetResourceHandlers() const
197     {
198         return resourcehandlers_;
199     }
200 
SetHapPath(const std::string & hapPath)201     void SetHapPath(const std::string& hapPath)
202     {
203         hapPath_ = hapPath;
204     }
205 
GetHapPath()206     std::string GetHapPath() const
207     {
208         return hapPath_;
209     }
210 
SetPackagePath(const std::string & packagePath)211     void SetPackagePath(const std::string& packagePath)
212     {
213         packagePath_ = packagePath;
214     }
215 
GetPackagePath()216     std::string GetPackagePath() const
217     {
218         return packagePath_;
219     }
220 
221 #if defined(PREVIEW)
SetSystemPackagePath(const std::string & systemPackagePath)222     void SetSystemPackagePath(const std::string& systemPackagePath)
223     {
224         systemPackagePath_ = systemPackagePath;
225     }
226 
GetSystemPackagePath()227     std::string GetSystemPackagePath() const
228     {
229         return systemPackagePath_;
230     }
231 
SetHmsPackagePath(const std::string & hmsPackagePath)232     void SetHmsPackagePath(const std::string& hmsPackagePath)
233     {
234         hmsPackagePath_ = hmsPackagePath;
235     }
236 
GetHmsPackagePath()237     const std::string& GetHmsPackagePath() const
238     {
239         return hmsPackagePath_;
240     }
241 #endif
242 
SetThemeId(uint32_t themeId)243     void SetThemeId(uint32_t themeId)
244     {
245         themeId_ = static_cast<int32_t>(themeId);
246     }
247 
GetThemeId()248     int32_t GetThemeId() const
249     {
250         return themeId_;
251     }
252 
253 private:
254     ResourceConfiguration resourceConfiguration_;
255     std::vector<int64_t> resourcehandlers_;
256     std::string hapPath_;
257     std::string packagePath_;
258 #if defined(PREVIEW)
259     std::string systemPackagePath_;
260     std::string hmsPackagePath_;
261 #endif
262     int32_t themeId_ = -1;
263 };
264 
265 } // namespace OHOS::Ace
266 
267 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_RESOURCE_CONFIGURATION_H
268