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 
CreateJsConfiguration(napi_env env,const AppExecFwk::Configuration & configuration)96 napi_value CreateJsConfiguration(napi_env env, const AppExecFwk::Configuration &configuration)
97 {
98     napi_value object = nullptr;
99     napi_create_object(env, &object);
100     if (object == nullptr) {
101         TAG_LOGE(AAFwkTag::ABILITY_SIM, "null Native object");
102         return object;
103     }
104 
105     napi_set_named_property(env, object, "language", CreateJsValue(env,
106         configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE)));
107 
108     napi_set_named_property(env, object, "colorMode", CreateJsValue(env,
109         ConvertColorMode(configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE))));
110 
111     std::string direction = configuration.GetItem(AppExecFwk::ConfigurationInner::APPLICATION_DIRECTION);
112     napi_set_named_property(env, object, "direction", CreateJsValue(env, ConvertDirection(direction)));
113 
114     std::string density = configuration.GetItem(AppExecFwk::ConfigurationInner::APPLICATION_DENSITYDPI);
115     napi_set_named_property(env, object, "screenDensity", CreateJsValue(env, ConvertDensity(density)));
116 
117     int32_t displayId = ConvertDisplayId(configuration.GetItem(AppExecFwk::ConfigurationInner::APPLICATION_DISPLAYID));
118     napi_set_named_property(env, object, "displayId", CreateJsValue(env, displayId));
119 
120     std::string hasPointerDevice = configuration.GetItem(AAFwk::GlobalConfigurationKey::INPUT_POINTER_DEVICE);
121     napi_set_named_property(
122         env, object, "hasPointerDevice", CreateJsValue(env, hasPointerDevice == "true" ? true : false));
123 
124     return object;
125 }
126 
CreateJsApplicationInfo(napi_env env,const AppExecFwk::ApplicationInfo & applicationInfo)127 napi_value CreateJsApplicationInfo(napi_env env, const AppExecFwk::ApplicationInfo &applicationInfo)
128 {
129     napi_value object = nullptr;
130     napi_create_object(env, &object);
131     if (object == nullptr) {
132         TAG_LOGE(AAFwkTag::ABILITY_SIM, "Create object failed");
133         return nullptr;
134     }
135 
136     AppExecFwk::CommonFunc::ConvertApplicationInfo(env, object, applicationInfo);
137     return object;
138 }
139 
CreateJsHapModuleInfo(napi_env env,const AppExecFwk::HapModuleInfo & hapModuleInfo)140 napi_value CreateJsHapModuleInfo(napi_env env, const AppExecFwk::HapModuleInfo &hapModuleInfo)
141 {
142     napi_value object = nullptr;
143     napi_create_object(env, &object);
144     if (object == nullptr) {
145         TAG_LOGE(AAFwkTag::ABILITY_SIM, "Create object failed");
146         return nullptr;
147     }
148 
149     AppExecFwk::CommonFunc::ConvertHapModuleInfo(env, hapModuleInfo, object);
150     return object;
151 }
152 
CreateJsAbilityInfo(napi_env env,const AppExecFwk::AbilityInfo & abilityInfo)153 napi_value CreateJsAbilityInfo(napi_env env, const AppExecFwk::AbilityInfo &abilityInfo)
154 {
155     napi_value object = nullptr;
156     napi_create_object(env, &object);
157     if (object == nullptr) {
158         TAG_LOGE(AAFwkTag::ABILITY_SIM, "Create object failed");
159         return nullptr;
160     }
161 
162     AppExecFwk::CommonFunc::ConvertAbilityInfo(env, abilityInfo, object);
163     return object;
164 }
165 } // namespace AbilityRuntime
166 } // namespace OHOS
167