1 /*
2 * Copyright (c) 2023 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_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 auto resolution = Global::Resource::ScreenDensity::SCREEN_DENSITY_NOT_SET;
56
57 static const std::vector<std::pair<std::string, Global::Resource::ScreenDensity>> resolutions = {
58 { "sdpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_SDPI },
59 { "mdpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_MDPI },
60 { "ldpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_LDPI },
61 { "xldpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_XLDPI },
62 { "xxldpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_XXLDPI },
63 { "xxxldpi", Global::Resource::ScreenDensity::SCREEN_DENSITY_XXXLDPI },
64 };
65
66 for (const auto &[tempdensity, value] : resolutions) {
67 if (tempdensity == density) {
68 resolution = value;
69 break;
70 }
71 }
72
73 return resolution;
74 }
75
ConvertDirection(const std::string & direction)76 Global::Resource::Direction ConvertDirection(const std::string &direction)
77 {
78 auto resolution = Global::Resource::Direction::DIRECTION_NOT_SET;
79
80 static const std::vector<std::pair<std::string, Global::Resource::Direction>> resolutions = {
81 { "vertical", Global::Resource::Direction::DIRECTION_VERTICAL },
82 { "horizontal", Global::Resource::Direction::DIRECTION_HORIZONTAL },
83 };
84
85 for (const auto &[tempDirection, value] : resolutions) {
86 if (tempDirection == direction) {
87 resolution = value;
88 break;
89 }
90 }
91
92 return resolution;
93 }
94
CreateJsConfiguration(NativeEngine & engine,const AppExecFwk::Configuration & configuration)95 NativeValue *CreateJsConfiguration(NativeEngine &engine, const AppExecFwk::Configuration &configuration)
96 {
97 NativeValue *objValue = engine.CreateObject();
98 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
99 if (object == nullptr) {
100 HILOG_ERROR("Native object is nullptr.");
101 return objValue;
102 }
103
104 object->SetProperty("language", CreateJsValue(engine,
105 configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE)));
106
107 object->SetProperty("colorMode", CreateJsValue(engine,
108 ConvertColorMode(configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE))));
109
110 std::string direction = configuration.GetItem(AppExecFwk::ConfigurationInner::APPLICATION_DIRECTION);
111 object->SetProperty("direction", CreateJsValue(engine, ConvertDirection(direction)));
112
113 std::string density = configuration.GetItem(AppExecFwk::ConfigurationInner::APPLICATION_DENSITYDPI);
114 object->SetProperty("screenDensity", CreateJsValue(engine, ConvertDensity(density)));
115
116 int32_t displayId = ConvertDisplayId(configuration.GetItem(AppExecFwk::ConfigurationInner::APPLICATION_DISPLAYID));
117 object->SetProperty("displayId", CreateJsValue(engine, displayId));
118
119 std::string hasPointerDevice = configuration.GetItem(AAFwk::GlobalConfigurationKey::INPUT_POINTER_DEVICE);
120 object->SetProperty("hasPointerDevice", CreateJsValue(engine, hasPointerDevice == "true" ? true : false));
121
122 return objValue;
123 }
124
CreateJsApplicationInfo(NativeEngine & engine,const AppExecFwk::ApplicationInfo & applicationInfo)125 NativeValue *CreateJsApplicationInfo(NativeEngine &engine, const AppExecFwk::ApplicationInfo &applicationInfo)
126 {
127 NativeValue *objValue = engine.CreateObject();
128 if (objValue == nullptr) {
129 HILOG_ERROR("Create object failed.");
130 return nullptr;
131 }
132
133 AppExecFwk::CommonFunc::ConvertApplicationInfo(reinterpret_cast<napi_env>(&engine),
134 reinterpret_cast<napi_value>(objValue), applicationInfo);
135 return objValue;
136 }
137
CreateJsHapModuleInfo(NativeEngine & engine,const AppExecFwk::HapModuleInfo & hapModuleInfo)138 NativeValue *CreateJsHapModuleInfo(NativeEngine &engine, const AppExecFwk::HapModuleInfo &hapModuleInfo)
139 {
140 NativeValue *objValue = engine.CreateObject();
141 if (objValue == nullptr) {
142 HILOG_ERROR("Create object failed.");
143 return nullptr;
144 }
145
146 AppExecFwk::CommonFunc::ConvertHapModuleInfo(reinterpret_cast<napi_env>(&engine), hapModuleInfo,
147 reinterpret_cast<napi_value>(objValue));
148 return objValue;
149 }
150
CreateJsAbilityInfo(NativeEngine & engine,const AppExecFwk::AbilityInfo & abilityInfo)151 NativeValue *CreateJsAbilityInfo(NativeEngine &engine, const AppExecFwk::AbilityInfo &abilityInfo)
152 {
153 NativeValue *objValue = engine.CreateObject();
154 if (objValue == nullptr) {
155 HILOG_ERROR("Create object failed.");
156 return nullptr;
157 }
158
159 AppExecFwk::CommonFunc::ConvertAbilityInfo(reinterpret_cast<napi_env>(&engine), abilityInfo,
160 reinterpret_cast<napi_value>(objValue));
161 return objValue;
162 }
163 } // namespace AbilityRuntime
164 } // namespace OHOS
165