• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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     int32_t id = -1;
139     try {
140         id = std::stoi(displayId);
141     } catch (...) {
142         ABILITYBASE_LOGE("failed to convert to int: %{public}s", displayId.c_str());
143     }
144     return id;
145 }
146 
ConvertHasPointerDevice(std::string hasPointerDevice)147 Global::Resource::InputDevice ConvertHasPointerDevice(std::string hasPointerDevice)
148 {
149     return hasPointerDevice == "true" ? Global::Resource::InputDevice::INPUTDEVICE_POINTINGDEVICE :
150         Global::Resource::InputDevice::INPUTDEVICE_NOT_SET;
151 }
152 
ConvertDeviceType(std::string deviceType)153 Global::Resource::DeviceType ConvertDeviceType(std::string deviceType)
154 {
155     static const std::unordered_map<std::string, Global::Resource::DeviceType> deviceTypes = {
156         {"default", Global::Resource::DeviceType::DEVICE_PHONE},
157         {"phone", Global::Resource::DeviceType::DEVICE_PHONE},
158         {"tablet", Global::Resource::DeviceType::DEVICE_TABLET},
159         {"car", Global::Resource::DeviceType::DEVICE_CAR},
160         {"tv", Global::Resource::DeviceType::DEVICE_TV},
161         {"watch", Global::Resource::DeviceType::DEVICE_WEARABLE},
162         {"2in1", Global::Resource::DeviceType::DEVICE_TWOINONE},
163         {"wearable", Global::Resource::DeviceType::DEVICE_WEARABLE}
164     };
165 
166     if (deviceTypes.find(deviceType) != deviceTypes.end()) {
167         return deviceTypes.at(deviceType);
168     }
169 
170     return Global::Resource::DeviceType::DEVICE_PHONE;
171 }
172 
GetColorModeStr(int32_t colormode)173 std::string GetColorModeStr(int32_t colormode)
174 {
175     std::string ret("no_color_mode");
176 
177     switch (colormode) {
178         case Global::Resource::ColorMode::DARK:
179             ret = ConfigurationInner::COLOR_MODE_DARK;
180             break;
181         case Global::Resource::ColorMode::LIGHT:
182             ret = ConfigurationInner::COLOR_MODE_LIGHT;
183             break;
184         case Global::Resource::ColorMode::COLOR_MODE_NOT_SET:
185             ret = ConfigurationInner::COLOR_MODE_AUTO;
186             break;
187         default:
188             break;
189     }
190 
191     return ret;
192 }
193 
GetDirectionStr(Global::Resource::Direction direction)194 std::string GetDirectionStr(Global::Resource::Direction direction)
195 {
196     std::string ret("no_direction");
197 
198     switch (direction) {
199         case Global::Resource::Direction::DIRECTION_VERTICAL:
200             ret = ConfigurationInner::DIRECTION_VERTICAL;
201             break;
202         case Global::Resource::Direction::DIRECTION_HORIZONTAL:
203             ret = ConfigurationInner::DIRECTION_HORIZONTAL;
204             break;
205         default:
206             break;
207     }
208 
209     return ret;
210 }
211 
GetDirectionStr(int32_t height,int32_t width)212 std::string GetDirectionStr(int32_t height, int32_t width)
213 {
214     return GetDirectionStr(ConvertDirection(height, width));
215 }
216 
GetDirectionStr(int32_t orientation)217 std::string GetDirectionStr(int32_t orientation)
218 {
219     std::string ret("no_direction");
220 
221     static const std::vector<std::pair<int32_t, std::string>> resolutions = {
222         { 0, ConfigurationInner::DIRECTION_VERTICAL },
223         { 1, ConfigurationInner::DIRECTION_HORIZONTAL },
224         { 2, ConfigurationInner::DIRECTION_VERTICAL },
225         { 3, ConfigurationInner::DIRECTION_HORIZONTAL },
226     };
227 
228     for (const auto& [tempOrientation, value] : resolutions) {
229         if (tempOrientation == orientation) {
230             ret = value;
231             break;
232         }
233     }
234     return ret;
235 }
236 
GetDensityStr(Global::Resource::ScreenDensity density)237 std::string GetDensityStr(Global::Resource::ScreenDensity density)
238 {
239     std::string ret("no_screen_density");
240 
241     static const std::vector<std::pair<Global::Resource::ScreenDensity, std::string>> resolutions = {
242         { Global::Resource::ScreenDensity::SCREEN_DENSITY_SDPI, "sdpi" },
243         { Global::Resource::ScreenDensity::SCREEN_DENSITY_MDPI, "mdpi" },
244         { Global::Resource::ScreenDensity::SCREEN_DENSITY_LDPI, "ldpi" },
245         { Global::Resource::ScreenDensity::SCREEN_DENSITY_XLDPI, "xldpi" },
246         { Global::Resource::ScreenDensity::SCREEN_DENSITY_XXLDPI, "xxldpi" },
247         { Global::Resource::ScreenDensity::SCREEN_DENSITY_XXXLDPI, "xxxldpi" },
248     };
249 
250     for (const auto& [dpi, value] : resolutions) {
251         if (dpi == density) {
252             return value;
253         }
254     }
255 
256     return ret;
257 }
258 
GetDensityStr(float density)259 std::string GetDensityStr(float density)
260 {
261     return GetDensityStr(ConvertDensity(density));
262 }
263 
DarkMode_ConvertEts2Native(const int32_t index)264 Global::Resource::ColorMode DarkMode_ConvertEts2Native(const int32_t index)
265 {
266     return static_cast<Global::Resource::ColorMode>(index - 1);
267 }
DarkMode_ConvertNative2Ets(const Global::Resource::ColorMode nativeValue)268 int32_t DarkMode_ConvertNative2Ets(const Global::Resource::ColorMode nativeValue)
269 {
270     return nativeValue + 1;
271 }
272 
273 // enum Direction {
274 //     DIRECTION_NOT_SET = -1,
275 //     DIRECTION_VERTICAL = 0,
276 //     DIRECTION_HORIZONTAL = 1
277 //   }
Direction_ConvertEts2Native(const int32_t index)278 Global::Resource::Direction Direction_ConvertEts2Native(const int32_t index)
279 {
280     return static_cast<Global::Resource::Direction>(index - 1);
281 }
Direction_ConvertNative2Ets(const Global::Resource::Direction nativeValue)282 int32_t Direction_ConvertNative2Ets(const Global::Resource::Direction nativeValue)
283 {
284     return nativeValue + 1;
285 }
286 
287 // enum ScreenDensity {
288 //     SCREEN_DENSITY_NOT_SET = 0,
289 //     SCREEN_DENSITY_SDPI = 120,
290 //     SCREEN_DENSITY_MDPI = 160,
291 //     SCREEN_DENSITY_LDPI = 240,
292 //     SCREEN_DENSITY_XLDPI = 320,
293 //     SCREEN_DENSITY_XXLDPI = 480,
294 //     SCREEN_DENSITY_XXXLDPI = 640
295 //   }
296 static constexpr std::array<int, 7> ScreenDensityArray = {0, 120, 160, 240, 320, 480, 640};
ScreenDensity_ConvertEts2Native(const int32_t index)297 Global::Resource::ScreenDensity ScreenDensity_ConvertEts2Native(const int32_t index)
298 {
299     if (index < 0 || index >= ScreenDensityArray.size()) {
300         return Global::Resource::ScreenDensity::SCREEN_DENSITY_NOT_SET;
301     }
302     return static_cast<Global::Resource::ScreenDensity>(ScreenDensityArray[index]);
303 }
ScreenDensity_ConvertNative2Ets(const Global::Resource::ScreenDensity nativeValue)304 int32_t ScreenDensity_ConvertNative2Ets(const Global::Resource::ScreenDensity nativeValue)
305 {
306     for (int32_t index = 0; index < static_cast<int32_t>(ScreenDensityArray.size()); index++) {
307         if (nativeValue == ScreenDensityArray[index]) {
308             return index;
309         }
310     }
311     return 0;
312 }
313 } // namespace OHOS::AppExecFwk