• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "js_data_converter.h"
17 
18 #include "common_func.h"
19 #include "hilog_tag_wrapper.h"
20 #include "js_runtime.h"
21 #include "js_runtime_utils.h"
22 
23 namespace OHOS {
24 namespace AbilityRuntime {
ConvertColorMode(const std::string & colormode)25 Global::Resource::ColorMode ConvertColorMode(const std::string &colormode)
26 {
27     auto resolution = Global::Resource::ColorMode::COLOR_MODE_NOT_SET;
28 
29     static const std::vector<std::pair<std::string, Global::Resource::ColorMode>> resolutions = {
30         { "dark", Global::Resource::ColorMode::DARK },
31         { "light", Global::Resource::ColorMode::LIGHT },
32     };
33 
34     for (const auto &[tempColorMode, value] : resolutions) {
35         if (tempColorMode == colormode) {
36             resolution = value;
37             break;
38         }
39     }
40 
41     return resolution;
42 }
43 
ConvertDisplayId(const std::string & displayId)44 int32_t ConvertDisplayId(const std::string &displayId)
45 {
46     if (displayId == AppExecFwk::ConfigurationInner::EMPTY_STRING) {
47         return -1;
48     }
49 
50     return std::stoi(displayId);
51 }
52 
ConvertDensity(const std::string & density)53 Global::Resource::ScreenDensity ConvertDensity(const std::string &density)
54 {
55     TAG_LOGD(AAFwkTag::ABILITY_SIM, "called");
56     auto resolution = Global::Resource::ScreenDensity::SCREEN_DENSITY_NOT_SET;
57 
58     static const std::vector<std::pair<std::string, Global::Resource::ScreenDensity>> resolutions = {
59         { "sdpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_SDPI },
60         { "mdpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_MDPI },
61         { "ldpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_LDPI },
62         { "xldpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_XLDPI },
63         { "xxldpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_XXLDPI },
64         { "xxxldpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_XXXLDPI },
65     };
66 
67     for (const auto &[tempdensity, value] : resolutions) {
68         if (tempdensity == density) {
69             resolution = value;
70             break;
71         }
72     }
73 
74     return resolution;
75 }
76 
ConvertDirection(const std::string & direction)77 Global::Resource::Direction ConvertDirection(const std::string &direction)
78 {
79     auto resolution = Global::Resource::Direction::DIRECTION_NOT_SET;
80 
81     static const std::vector<std::pair<std::string, Global::Resource::Direction>> resolutions = {
82         { "vertical", Global::Resource::Direction::DIRECTION_VERTICAL },
83         { "horizontal", Global::Resource::Direction::DIRECTION_HORIZONTAL },
84     };
85 
86     for (const auto &[tempDirection, value] : resolutions) {
87         if (tempDirection == direction) {
88             resolution = value;
89             break;
90         }
91     }
92 
93     return resolution;
94 }
95 
ConvertDeviceType(const std::string & deviceType)96 Global::Resource::DeviceType ConvertDeviceType(const std::string &deviceType)
97 {
98     static const std::unordered_map<std::string, Global::Resource::DeviceType> deviceTypes = {
99         {"default", Global::Resource::DeviceType::DEVICE_PHONE},
100         {"phone", Global::Resource::DeviceType::DEVICE_PHONE},
101         {"tablet", Global::Resource::DeviceType::DEVICE_TABLET},
102         {"car", Global::Resource::DeviceType::DEVICE_CAR},
103         {"tv", Global::Resource::DeviceType::DEVICE_TV},
104         {"watch", Global::Resource::DeviceType::DEVICE_WEARABLE},
105         {"2in1", Global::Resource::DeviceType::DEVICE_TWOINONE},
106         {"wearable", Global::Resource::DeviceType::DEVICE_WEARABLE}
107     };
108 
109     if (deviceTypes.find(deviceType) != deviceTypes.end()) {
110         return deviceTypes.at(deviceType);
111     }
112 
113     return Global::Resource::DeviceType::DEVICE_PHONE;
114 }
115 
ConvertDeviceType(DeviceType type)116 Global::Resource::DeviceType ConvertDeviceType(DeviceType type)
117 {
118     switch (type) {
119         case DeviceType::PHONE:
120             return Global::Resource::DeviceType::DEVICE_PHONE;
121         case DeviceType::TV:
122             return Global::Resource::DeviceType::DEVICE_TV;
123         case DeviceType::WATCH:
124             return Global::Resource::DeviceType::DEVICE_WEARABLE;
125         case DeviceType::CAR:
126             return Global::Resource::DeviceType::DEVICE_CAR;
127         case DeviceType::TABLET:
128             return Global::Resource::DeviceType::DEVICE_TABLET;
129         case DeviceType::TWO_IN_ONE:
130             return Global::Resource::DeviceType::DEVICE_TWOINONE;
131         case DeviceType::WEARABLE:
132             return Global::Resource::DeviceType::DEVICE_WEARABLE;
133         default:
134             return Global::Resource::DeviceType::DEVICE_NOT_SET;
135     }
136 }
137 
ConvertDirection(DeviceOrientation orientation)138 Global::Resource::Direction ConvertDirection(DeviceOrientation orientation)
139 {
140     switch (orientation) {
141         case DeviceOrientation::PORTRAIT:
142             return Global::Resource::Direction::DIRECTION_VERTICAL;
143         case DeviceOrientation::LANDSCAPE:
144             return Global::Resource::Direction::DIRECTION_HORIZONTAL;
145         default:
146             return Global::Resource::Direction::DIRECTION_NOT_SET;
147     }
148 }
149 
ConvertDensity(double density)150 Global::Resource::ScreenDensity ConvertDensity(double density)
151 {
152     static const std::vector<std::pair<double, Global::Resource::ScreenDensity>> resolutions = {
153         { 0.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_NOT_SET },
154         { 120.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_SDPI },
155         { 160.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_MDPI },
156         { 240.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_LDPI },
157         { 320.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_XLDPI },
158         { 480.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_XXLDPI },
159         { 640.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_XXXLDPI },
160     };
161     double deviceDpi = density * DPI_BASE;
162     auto resolution = Global::Resource::ScreenDensity::SCREEN_DENSITY_NOT_SET;
163     constexpr double epsilon = 0.001f;
164     for (const auto& [dpi, value] : resolutions) {
165         resolution = value;
166         if ((deviceDpi - dpi) < epsilon) {
167             break;
168         }
169     }
170     return resolution;
171 }
172 
ConvertColorMode(ColorMode colorMode)173 Global::Resource::ColorMode ConvertColorMode(ColorMode colorMode)
174 {
175     switch (colorMode) {
176         case ColorMode::DARK:
177             return Global::Resource::ColorMode::DARK;
178         case ColorMode::LIGHT:
179             return Global::Resource::ColorMode::LIGHT;
180         default:
181             return Global::Resource::ColorMode::COLOR_MODE_NOT_SET;
182     }
183 }
184 
CreateJsConfiguration(napi_env env,const AppExecFwk::Configuration & configuration)185 napi_value CreateJsConfiguration(napi_env env, const AppExecFwk::Configuration &configuration)
186 {
187     napi_value object = nullptr;
188     napi_create_object(env, &object);
189     if (object == nullptr) {
190         TAG_LOGE(AAFwkTag::ABILITY_SIM, "null Native object");
191         return object;
192     }
193 
194     napi_set_named_property(env, object, "language", CreateJsValue(env,
195         configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE)));
196 
197     napi_set_named_property(env, object, "colorMode", CreateJsValue(env,
198         ConvertColorMode(configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE))));
199 
200     std::string direction = configuration.GetItem(AppExecFwk::ConfigurationInner::APPLICATION_DIRECTION);
201     napi_set_named_property(env, object, "direction", CreateJsValue(env, ConvertDirection(direction)));
202 
203     std::string density = configuration.GetItem(AppExecFwk::ConfigurationInner::APPLICATION_DENSITYDPI);
204     napi_set_named_property(env, object, "screenDensity", CreateJsValue(env, ConvertDensity(density)));
205 
206     int32_t displayId = ConvertDisplayId(configuration.GetItem(AppExecFwk::ConfigurationInner::APPLICATION_DISPLAYID));
207     napi_set_named_property(env, object, "displayId", CreateJsValue(env, displayId));
208 
209     std::string hasPointerDevice = configuration.GetItem(AAFwk::GlobalConfigurationKey::INPUT_POINTER_DEVICE);
210     napi_set_named_property(
211         env, object, "hasPointerDevice", CreateJsValue(env, hasPointerDevice == "true" ? true : false));
212 
213     return object;
214 }
215 
CreateJsApplicationInfo(napi_env env,const AppExecFwk::ApplicationInfo & applicationInfo)216 napi_value CreateJsApplicationInfo(napi_env env, const AppExecFwk::ApplicationInfo &applicationInfo)
217 {
218     napi_value object = nullptr;
219     napi_create_object(env, &object);
220     if (object == nullptr) {
221         TAG_LOGE(AAFwkTag::ABILITY_SIM, "Create object failed");
222         return nullptr;
223     }
224 
225     AppExecFwk::CommonFunc::ConvertApplicationInfo(env, object, applicationInfo);
226     return object;
227 }
228 
CreateJsHapModuleInfo(napi_env env,const AppExecFwk::HapModuleInfo & hapModuleInfo)229 napi_value CreateJsHapModuleInfo(napi_env env, const AppExecFwk::HapModuleInfo &hapModuleInfo)
230 {
231     napi_value object = nullptr;
232     napi_create_object(env, &object);
233     if (object == nullptr) {
234         TAG_LOGE(AAFwkTag::ABILITY_SIM, "Create object failed");
235         return nullptr;
236     }
237 
238     AppExecFwk::CommonFunc::ConvertHapModuleInfo(env, hapModuleInfo, object);
239     return object;
240 }
241 
CreateJsAbilityInfo(napi_env env,const AppExecFwk::AbilityInfo & abilityInfo)242 napi_value CreateJsAbilityInfo(napi_env env, const AppExecFwk::AbilityInfo &abilityInfo)
243 {
244     napi_value object = nullptr;
245     napi_create_object(env, &object);
246     if (object == nullptr) {
247         TAG_LOGE(AAFwkTag::ABILITY_SIM, "Create object failed");
248         return nullptr;
249     }
250 
251     AppExecFwk::CommonFunc::ConvertAbilityInfo(env, abilityInfo, object);
252     return object;
253 }
254 } // namespace AbilityRuntime
255 } // namespace OHOS
256