• 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 
ConvertDirection(int32_t height,int32_t width)42 Global::Resource::Direction ConvertDirection(int32_t height, int32_t width)
43 {
44     return height >= width ? Global::Resource::Direction::DIRECTION_VERTICAL :
45         Global::Resource::Direction::DIRECTION_HORIZONTAL;
46 }
47 
ConvertDirection(std::string direction)48 Global::Resource::Direction ConvertDirection(std::string direction)
49 {
50     auto resolution = Global::Resource::Direction::DIRECTION_NOT_SET;
51 
52     static const std::vector<std::pair<std::string, Global::Resource::Direction>> resolutions = {
53         { "vertical", Global::Resource::Direction::DIRECTION_VERTICAL },
54         { "horizontal", Global::Resource::Direction::DIRECTION_HORIZONTAL },
55     };
56 
57     for (const auto& [tempDirection, value] : resolutions) {
58         if (tempDirection == direction) {
59             resolution = value;
60             break;
61         }
62     }
63 
64     return resolution;
65 }
66 
ConvertDensity(float density)67 Global::Resource::ScreenDensity ConvertDensity(float density)
68 {
69     static const std::vector<std::pair<float, Global::Resource::ScreenDensity>> resolutions = {
70         { 0.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_NOT_SET },
71         { 120.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_SDPI },
72         { 160.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_MDPI },
73         { 240.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_LDPI },
74         { 320.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_XLDPI },
75         { 480.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_XXLDPI },
76         { 640.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_XXXLDPI },
77     };
78 
79     float deviceDpi = density * DPI_BASE;
80     auto resolution = Global::Resource::ScreenDensity::SCREEN_DENSITY_NOT_SET;
81     for (const auto& [dpi, value] : resolutions) {
82         resolution = value;
83         if (deviceDpi <= dpi) {
84             break;
85         }
86     }
87     return resolution;
88 }
89 
ConvertDensity(std::string density)90 Global::Resource::ScreenDensity ConvertDensity(std::string density)
91 {
92     auto resolution = Global::Resource::ScreenDensity::SCREEN_DENSITY_NOT_SET;
93 
94     static const std::vector<std::pair<std::string, Global::Resource::ScreenDensity>> resolutions = {
95         { "sdpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_SDPI },
96         { "mdpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_MDPI },
97         { "ldpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_LDPI },
98         { "xldpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_XLDPI },
99         { "xxldpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_XXLDPI },
100         { "xxxldpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_XXXLDPI },
101     };
102 
103     for (const auto& [tempdensity, value] : resolutions) {
104         if (tempdensity == density) {
105             resolution = value;
106             break;
107         }
108     }
109 
110     return resolution;
111 }
112 
ConvertDisplayId(std::string displayId)113 int32_t ConvertDisplayId(std::string displayId)
114 {
115     if (displayId == ConfigurationInner::EMPTY_STRING) {
116         return -1;
117     }
118 
119     return std::stoi(displayId);
120 }
121 
ConvertHasPointerDevice(std::string hasPointerDevice)122 Global::Resource::InputDevice ConvertHasPointerDevice(std::string hasPointerDevice)
123 {
124     return hasPointerDevice == "true" ? Global::Resource::InputDevice::INPUTDEVICE_POINTINGDEVICE :
125         Global::Resource::InputDevice::INPUTDEVICE_NOT_SET;
126 }
127 
ConvertDeviceType(std::string deviceType)128 Global::Resource::DeviceType ConvertDeviceType(std::string deviceType)
129 {
130     static const std::unordered_map<std::string, Global::Resource::DeviceType> deviceTypes = {
131         {"default", Global::Resource::DeviceType::DEVICE_PHONE},
132         {"phone", Global::Resource::DeviceType::DEVICE_PHONE},
133         {"tablet", Global::Resource::DeviceType::DEVICE_TABLET},
134         {"car", Global::Resource::DeviceType::DEVICE_CAR},
135         {"tv", Global::Resource::DeviceType::DEVICE_TV},
136         {"watch", Global::Resource::DeviceType::DEVICE_WEARABLE},
137     };
138 
139     if (deviceTypes.find(deviceType) != deviceTypes.end()) {
140         return deviceTypes.at(deviceType);
141     }
142 
143     return Global::Resource::DeviceType::DEVICE_PHONE;
144 }
145 
GetColorModeStr(int32_t colormode)146 std::string GetColorModeStr(int32_t colormode)
147 {
148     std::string ret("no_color_mode");
149 
150     switch (colormode) {
151         case Global::Resource::ColorMode::DARK:
152             ret = ConfigurationInner::COLOR_MODE_DARK;
153             break;
154         case Global::Resource::ColorMode::LIGHT:
155             ret = ConfigurationInner::COLOR_MODE_LIGHT;
156             break;
157         default:
158             break;
159     }
160 
161     return ret;
162 }
163 
GetDirectionStr(Global::Resource::Direction direction)164 std::string GetDirectionStr(Global::Resource::Direction direction)
165 {
166     std::string ret("no_direction");
167 
168     switch (direction) {
169         case Global::Resource::Direction::DIRECTION_VERTICAL:
170             ret = ConfigurationInner::DIRECTION_VERTICAL;
171             break;
172         case Global::Resource::Direction::DIRECTION_HORIZONTAL:
173             ret = ConfigurationInner::DIRECTION_HORIZONTAL;
174             break;
175         default:
176             break;
177     }
178 
179     return ret;
180 }
181 
GetDirectionStr(int32_t height,int32_t width)182 std::string GetDirectionStr(int32_t height, int32_t width)
183 {
184     return GetDirectionStr(ConvertDirection(height, width));
185 }
186 
GetDensityStr(Global::Resource::ScreenDensity density)187 std::string GetDensityStr(Global::Resource::ScreenDensity density)
188 {
189     std::string ret("no_screen_density");
190 
191     static const std::vector<std::pair<Global::Resource::ScreenDensity, std::string>> resolutions = {
192         { Global::Resource::ScreenDensity::SCREEN_DENSITY_SDPI, "sdpi" },
193         { Global::Resource::ScreenDensity::SCREEN_DENSITY_MDPI, "mdpi" },
194         { Global::Resource::ScreenDensity::SCREEN_DENSITY_LDPI, "ldpi" },
195         { Global::Resource::ScreenDensity::SCREEN_DENSITY_XLDPI, "xldpi" },
196         { Global::Resource::ScreenDensity::SCREEN_DENSITY_XXLDPI, "xxldpi" },
197         { Global::Resource::ScreenDensity::SCREEN_DENSITY_XXXLDPI, "xxxldpi" },
198     };
199 
200     for (const auto& [dpi, value] : resolutions) {
201         if (dpi == density) {
202             return value;
203         }
204     }
205 
206     return ret;
207 }
208 
GetDensityStr(float density)209 std::string GetDensityStr(float density)
210 {
211     return GetDensityStr(ConvertDensity(density));
212 }
213 } // namespace OHOS::AppExecFwk