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
GetColorModeStr(int32_t colormode)122 std::string GetColorModeStr(int32_t colormode)
123 {
124 std::string ret("no_color_mode");
125
126 switch (colormode) {
127 case Global::Resource::ColorMode::DARK:
128 ret = ConfigurationInner::COLOR_MODE_DARK;
129 break;
130 case Global::Resource::ColorMode::LIGHT:
131 ret = ConfigurationInner::COLOR_MODE_LIGHT;
132 break;
133 default:
134 break;
135 }
136
137 return ret;
138 }
139
GetDirectionStr(Global::Resource::Direction direction)140 std::string GetDirectionStr(Global::Resource::Direction direction)
141 {
142 std::string ret("no_direction");
143
144 switch (direction) {
145 case Global::Resource::Direction::DIRECTION_VERTICAL:
146 ret = ConfigurationInner::DIRECTION_VERTICAL;
147 break;
148 case Global::Resource::Direction::DIRECTION_HORIZONTAL:
149 ret = ConfigurationInner::DIRECTION_HORIZONTAL;
150 break;
151 default:
152 break;
153 }
154
155 return ret;
156 }
157
GetDirectionStr(int32_t height,int32_t width)158 std::string GetDirectionStr(int32_t height, int32_t width)
159 {
160 return GetDirectionStr(ConvertDirection(height, width));
161 }
162
GetDensityStr(Global::Resource::ScreenDensity density)163 std::string GetDensityStr(Global::Resource::ScreenDensity density)
164 {
165 std::string ret("no_screen_density");
166
167 static const std::vector<std::pair<Global::Resource::ScreenDensity, std::string>> resolutions = {
168 { Global::Resource::ScreenDensity::SCREEN_DENSITY_SDPI, "sdpi" },
169 { Global::Resource::ScreenDensity::SCREEN_DENSITY_MDPI, "mdpi" },
170 { Global::Resource::ScreenDensity::SCREEN_DENSITY_LDPI, "ldpi" },
171 { Global::Resource::ScreenDensity::SCREEN_DENSITY_XLDPI, "xldpi" },
172 { Global::Resource::ScreenDensity::SCREEN_DENSITY_XXLDPI, "xxldpi" },
173 { Global::Resource::ScreenDensity::SCREEN_DENSITY_XXXLDPI, "xxxldpi" },
174 };
175
176 for (const auto& [dpi, value] : resolutions) {
177 if (dpi == density) {
178 return value;
179 }
180 }
181
182 return ret;
183 }
184
GetDensityStr(float density)185 std::string GetDensityStr(float density)
186 {
187 return GetDensityStr(ConvertDensity(density));
188 }
189 } // namespace OHOS::AppExecFwk