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