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