• 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 
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 
150 private:
151     bool ParseJsonColorMode(const std::unique_ptr<JsonValue>& jsonConfig, uint32_t& updateFlags);
152     bool ParseJsonFontRatio(const std::unique_ptr<JsonValue>& jsonConfig, uint32_t& updateFlags);
153 
154 private:
155     DeviceType deviceType_ = DeviceType::PHONE;
156     DeviceOrientation orientation_ = DeviceOrientation::PORTRAIT;
157     double density_ = 1.0;
158     double fontRatio_ = 1.0;
159     bool isDeviceAccess_ = false;
160     ColorMode colorMode_ = ColorMode::LIGHT;
161     bool colorModeIsSetByApp_ = false;
162     bool appHasDarkRes_ = false;
163     uint32_t mcc_ = 0;
164     uint32_t mnc_ = 0;
165     std::string preferredLanguage_;
166 };
167 
168 class ResourceInfo {
169 public:
SetResourceConfiguration(const ResourceConfiguration & resourceConfiguration)170     void SetResourceConfiguration(const ResourceConfiguration& resourceConfiguration)
171     {
172         resourceConfiguration_ = resourceConfiguration;
173     }
174 
GetResourceConfiguration()175     ResourceConfiguration GetResourceConfiguration() const
176     {
177         return resourceConfiguration_;
178     }
179 
SetResourceHandlers(const std::vector<int64_t> & resourcehandlers)180     void SetResourceHandlers(const std::vector<int64_t>& resourcehandlers)
181     {
182         resourcehandlers_ = resourcehandlers;
183     }
184 
GetResourceHandlers()185     std::vector<int64_t> GetResourceHandlers() const
186     {
187         return resourcehandlers_;
188     }
189 
SetHapPath(const std::string & hapPath)190     void SetHapPath(const std::string& hapPath)
191     {
192         hapPath_ = hapPath;
193     }
194 
GetHapPath()195     std::string GetHapPath() const
196     {
197         return hapPath_;
198     }
199 
SetPackagePath(const std::string & packagePath)200     void SetPackagePath(const std::string& packagePath)
201     {
202         packagePath_ = packagePath;
203     }
204 
GetPackagePath()205     std::string GetPackagePath() const
206     {
207         return packagePath_;
208     }
209 
210 #if defined(PREVIEW)
SetSystemPackagePath(const std::string & systemPackagePath)211     void SetSystemPackagePath(const std::string& systemPackagePath)
212     {
213         systemPackagePath_ = systemPackagePath;
214     }
215 
GetSystemPackagePath()216     std::string GetSystemPackagePath() const
217     {
218         return systemPackagePath_;
219     }
220 
SetHmsPackagePath(const std::string & hmsPackagePath)221     void SetHmsPackagePath(const std::string& hmsPackagePath)
222     {
223         hmsPackagePath_ = hmsPackagePath;
224     }
225 
GetHmsPackagePath()226     const std::string& GetHmsPackagePath() const
227     {
228         return hmsPackagePath_;
229     }
230 #endif
231 
SetThemeId(uint32_t themeId)232     void SetThemeId(uint32_t themeId)
233     {
234         themeId_ = static_cast<int32_t>(themeId);
235     }
236 
GetThemeId()237     int32_t GetThemeId() const
238     {
239         return themeId_;
240     }
241 
242 private:
243     ResourceConfiguration resourceConfiguration_;
244     std::vector<int64_t> resourcehandlers_;
245     std::string hapPath_;
246     std::string packagePath_;
247 #if defined(PREVIEW)
248     std::string systemPackagePath_;
249     std::string hmsPackagePath_;
250 #endif
251     int32_t themeId_ = -1;
252 };
253 
254 } // namespace OHOS::Ace
255 
256 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_RESOURCE_CONFIGURATION_H
257