• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     };
158 
159     if (deviceTypes.find(deviceType) != deviceTypes.end()) {
160         return deviceTypes.at(deviceType);
161     }
162 
163     return Global::Resource::DeviceType::DEVICE_PHONE;
164 }
165 
GetColorModeStr(int32_t colormode)166 std::string GetColorModeStr(int32_t colormode)
167 {
168     std::string ret("no_color_mode");
169 
170     switch (colormode) {
171         case Global::Resource::ColorMode::DARK:
172             ret = ConfigurationInner::COLOR_MODE_DARK;
173             break;
174         case Global::Resource::ColorMode::LIGHT:
175             ret = ConfigurationInner::COLOR_MODE_LIGHT;
176             break;
177         case Global::Resource::ColorMode::COLOR_MODE_NOT_SET:
178             ret = ConfigurationInner::COLOR_MODE_AUTO;
179             break;
180         default:
181             break;
182     }
183 
184     return ret;
185 }
186 
GetDirectionStr(Global::Resource::Direction direction)187 std::string GetDirectionStr(Global::Resource::Direction direction)
188 {
189     std::string ret("no_direction");
190 
191     switch (direction) {
192         case Global::Resource::Direction::DIRECTION_VERTICAL:
193             ret = ConfigurationInner::DIRECTION_VERTICAL;
194             break;
195         case Global::Resource::Direction::DIRECTION_HORIZONTAL:
196             ret = ConfigurationInner::DIRECTION_HORIZONTAL;
197             break;
198         default:
199             break;
200     }
201 
202     return ret;
203 }
204 
GetDirectionStr(int32_t height,int32_t width)205 std::string GetDirectionStr(int32_t height, int32_t width)
206 {
207     return GetDirectionStr(ConvertDirection(height, width));
208 }
209 
GetDensityStr(Global::Resource::ScreenDensity density)210 std::string GetDensityStr(Global::Resource::ScreenDensity density)
211 {
212     std::string ret("no_screen_density");
213 
214     static const std::vector<std::pair<Global::Resource::ScreenDensity, std::string>> resolutions = {
215         { Global::Resource::ScreenDensity::SCREEN_DENSITY_SDPI, "sdpi" },
216         { Global::Resource::ScreenDensity::SCREEN_DENSITY_MDPI, "mdpi" },
217         { Global::Resource::ScreenDensity::SCREEN_DENSITY_LDPI, "ldpi" },
218         { Global::Resource::ScreenDensity::SCREEN_DENSITY_XLDPI, "xldpi" },
219         { Global::Resource::ScreenDensity::SCREEN_DENSITY_XXLDPI, "xxldpi" },
220         { Global::Resource::ScreenDensity::SCREEN_DENSITY_XXXLDPI, "xxxldpi" },
221     };
222 
223     for (const auto& [dpi, value] : resolutions) {
224         if (dpi == density) {
225             return value;
226         }
227     }
228 
229     return ret;
230 }
231 
GetDensityStr(float density)232 std::string GetDensityStr(float density)
233 {
234     return GetDensityStr(ConvertDensity(density));
235 }
236 } // namespace OHOS::AppExecFwk