1 /*
2 * Copyright (c) 2022-2024 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 #include "configuration_convertor.h"
17
18 #include "ability_base_log_wrapper.h"
19 #include "configuration.h"
20
21 namespace OHOS::AppExecFwk {
22 constexpr float DPI_BASE = 160.0;
23
ConvertColorMode(std::string colormode)24 Global::Resource::ColorMode ConvertColorMode(std::string colormode)
25 {
26 auto resolution = Global::Resource::ColorMode::COLOR_MODE_NOT_SET;
27
28 static const std::vector<std::pair<std::string, Global::Resource::ColorMode>> resolutions = {
29 { "dark", Global::Resource::ColorMode::DARK },
30 { "light", Global::Resource::ColorMode::LIGHT },
31 };
32
33 for (const auto& [tempColorMode, value] : resolutions) {
34 if (tempColorMode == colormode) {
35 resolution = value;
36 break;
37 }
38 }
39
40 return resolution;
41 }
42
ConvertTimeFormat(std::string timeformat)43 Global::Resource::TimeFormat ConvertTimeFormat(std::string timeformat)
44 {
45 auto resolution = Global::Resource::TimeFormat::HOUR_NOT_SET;
46
47 static const std::vector<std::pair<std::string, Global::Resource::TimeFormat>> resolutions = {
48 { "false", Global::Resource::TimeFormat::HOUR_12 },
49 { "true", Global::Resource::TimeFormat::HOUR_24 },
50 };
51
52 for (const auto& [tempTimeFormat, value] : resolutions) {
53 if (tempTimeFormat == timeformat) {
54 resolution = value;
55 break;
56 }
57 }
58
59 return resolution;
60 }
61
ConvertDirection(int32_t height,int32_t width)62 Global::Resource::Direction ConvertDirection(int32_t height, int32_t width)
63 {
64 return height >= width ? Global::Resource::Direction::DIRECTION_VERTICAL :
65 Global::Resource::Direction::DIRECTION_HORIZONTAL;
66 }
67
ConvertDirection(std::string direction)68 Global::Resource::Direction ConvertDirection(std::string direction)
69 {
70 auto resolution = Global::Resource::Direction::DIRECTION_NOT_SET;
71
72 static const std::vector<std::pair<std::string, Global::Resource::Direction>> resolutions = {
73 { "vertical", Global::Resource::Direction::DIRECTION_VERTICAL },
74 { "horizontal", Global::Resource::Direction::DIRECTION_HORIZONTAL },
75 };
76
77 for (const auto& [tempDirection, value] : resolutions) {
78 if (tempDirection == direction) {
79 resolution = value;
80 break;
81 }
82 }
83
84 return resolution;
85 }
86
ConvertDensity(float density)87 Global::Resource::ScreenDensity ConvertDensity(float density)
88 {
89 static const std::vector<std::pair<float, Global::Resource::ScreenDensity>> resolutions = {
90 { 0.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_NOT_SET },
91 { 120.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_SDPI },
92 { 160.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_MDPI },
93 { 240.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_LDPI },
94 { 320.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_XLDPI },
95 { 480.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_XXLDPI },
96 { 640.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_XXXLDPI },
97 };
98
99 float deviceDpi = density * DPI_BASE;
100 auto resolution = Global::Resource::ScreenDensity::SCREEN_DENSITY_NOT_SET;
101 for (const auto& [dpi, value] : resolutions) {
102 resolution = value;
103 if (deviceDpi <= dpi) {
104 break;
105 }
106 }
107 return resolution;
108 }
109
ConvertDensity(std::string density)110 Global::Resource::ScreenDensity ConvertDensity(std::string density)
111 {
112 auto resolution = Global::Resource::ScreenDensity::SCREEN_DENSITY_NOT_SET;
113
114 static const std::vector<std::pair<std::string, Global::Resource::ScreenDensity>> resolutions = {
115 { "sdpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_SDPI },
116 { "mdpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_MDPI },
117 { "ldpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_LDPI },
118 { "xldpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_XLDPI },
119 { "xxldpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_XXLDPI },
120 { "xxxldpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_XXXLDPI },
121 };
122
123 for (const auto& [tempdensity, value] : resolutions) {
124 if (tempdensity == density) {
125 resolution = value;
126 break;
127 }
128 }
129
130 return resolution;
131 }
132
ConvertDisplayId(std::string displayId)133 int32_t ConvertDisplayId(std::string displayId)
134 {
135 if (displayId == ConfigurationInner::EMPTY_STRING) {
136 return -1;
137 }
138
139 int32_t id = -1;
140 try {
141 id = std::stoi(displayId);
142 } catch (...) {
143 ABILITYBASE_LOGE("failed to convert to int: %{public}s", displayId.c_str());
144 }
145 return id;
146 }
147
ConvertHasPointerDevice(std::string hasPointerDevice)148 Global::Resource::InputDevice ConvertHasPointerDevice(std::string hasPointerDevice)
149 {
150 return hasPointerDevice == "true" ? Global::Resource::InputDevice::INPUTDEVICE_POINTINGDEVICE :
151 Global::Resource::InputDevice::INPUTDEVICE_NOT_SET;
152 }
153
ConvertDeviceType(std::string deviceType)154 Global::Resource::DeviceType ConvertDeviceType(std::string deviceType)
155 {
156 static const std::unordered_map<std::string, Global::Resource::DeviceType> deviceTypes = {
157 {"default", Global::Resource::DeviceType::DEVICE_PHONE},
158 {"phone", Global::Resource::DeviceType::DEVICE_PHONE},
159 {"tablet", Global::Resource::DeviceType::DEVICE_TABLET},
160 {"car", Global::Resource::DeviceType::DEVICE_CAR},
161 {"tv", Global::Resource::DeviceType::DEVICE_TV},
162 {"watch", Global::Resource::DeviceType::DEVICE_WEARABLE},
163 {"2in1", Global::Resource::DeviceType::DEVICE_TWOINONE},
164 {"wearable", Global::Resource::DeviceType::DEVICE_WEARABLE}
165 };
166
167 if (deviceTypes.find(deviceType) != deviceTypes.end()) {
168 return deviceTypes.at(deviceType);
169 }
170
171 return Global::Resource::DeviceType::DEVICE_PHONE;
172 }
173
GetColorModeStr(int32_t colormode)174 std::string GetColorModeStr(int32_t colormode)
175 {
176 std::string ret("no_color_mode");
177
178 switch (colormode) {
179 case Global::Resource::ColorMode::DARK:
180 ret = ConfigurationInner::COLOR_MODE_DARK;
181 break;
182 case Global::Resource::ColorMode::LIGHT:
183 ret = ConfigurationInner::COLOR_MODE_LIGHT;
184 break;
185 case Global::Resource::ColorMode::COLOR_MODE_NOT_SET:
186 ret = ConfigurationInner::COLOR_MODE_AUTO;
187 break;
188 default:
189 break;
190 }
191
192 return ret;
193 }
194
GetDirectionStr(Global::Resource::Direction direction)195 std::string GetDirectionStr(Global::Resource::Direction direction)
196 {
197 std::string ret("no_direction");
198
199 switch (direction) {
200 case Global::Resource::Direction::DIRECTION_VERTICAL:
201 ret = ConfigurationInner::DIRECTION_VERTICAL;
202 break;
203 case Global::Resource::Direction::DIRECTION_HORIZONTAL:
204 ret = ConfigurationInner::DIRECTION_HORIZONTAL;
205 break;
206 default:
207 break;
208 }
209
210 return ret;
211 }
212
GetDirectionStr(int32_t height,int32_t width)213 std::string GetDirectionStr(int32_t height, int32_t width)
214 {
215 return GetDirectionStr(ConvertDirection(height, width));
216 }
217
GetDirectionStr(int32_t orientation)218 std::string GetDirectionStr(int32_t orientation)
219 {
220 std::string ret("no_direction");
221
222 static const std::vector<std::pair<int32_t, std::string>> resolutions = {
223 { 0, ConfigurationInner::DIRECTION_VERTICAL },
224 { 1, ConfigurationInner::DIRECTION_HORIZONTAL },
225 { 2, ConfigurationInner::DIRECTION_VERTICAL },
226 { 3, ConfigurationInner::DIRECTION_HORIZONTAL },
227 };
228
229 for (const auto& [tempOrientation, value] : resolutions) {
230 if (tempOrientation == orientation) {
231 ret = value;
232 break;
233 }
234 }
235 return ret;
236 }
237
GetDensityStr(Global::Resource::ScreenDensity density)238 std::string GetDensityStr(Global::Resource::ScreenDensity density)
239 {
240 std::string ret("no_screen_density");
241
242 static const std::vector<std::pair<Global::Resource::ScreenDensity, std::string>> resolutions = {
243 { Global::Resource::ScreenDensity::SCREEN_DENSITY_SDPI, "sdpi" },
244 { Global::Resource::ScreenDensity::SCREEN_DENSITY_MDPI, "mdpi" },
245 { Global::Resource::ScreenDensity::SCREEN_DENSITY_LDPI, "ldpi" },
246 { Global::Resource::ScreenDensity::SCREEN_DENSITY_XLDPI, "xldpi" },
247 { Global::Resource::ScreenDensity::SCREEN_DENSITY_XXLDPI, "xxldpi" },
248 { Global::Resource::ScreenDensity::SCREEN_DENSITY_XXXLDPI, "xxxldpi" },
249 };
250
251 for (const auto& [dpi, value] : resolutions) {
252 if (dpi == density) {
253 return value;
254 }
255 }
256
257 return ret;
258 }
259
GetDensityStr(float density)260 std::string GetDensityStr(float density)
261 {
262 return GetDensityStr(ConvertDensity(density));
263 }
264 } // namespace OHOS::AppExecFwk