• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "napi_common_configuration.h"
17 
18 #include "configuration_convertor.h"
19 #include "hilog_wrapper.h"
20 #include "napi_common_util.h"
21 
22 namespace OHOS {
23 namespace AppExecFwk {
24 EXTERN_C_START
25 
InnerWrapConfigurationString(napi_env env,napi_value jsObject,const std::string & key,const std::string & value)26 bool InnerWrapConfigurationString(
27     napi_env env, napi_value jsObject, const std::string &key, const std::string &value)
28 {
29     if (!value.empty()) {
30         HILOG_INFO("%{public}s called. key=%{public}s, value=%{private}s", __func__, key.c_str(), value.c_str());
31         napi_value jsValue = WrapStringToJS(env, value);
32         if (jsValue != nullptr) {
33             NAPI_CALL_BASE(env, napi_set_named_property(env, jsObject, key.c_str(), jsValue), false);
34             return true;
35         }
36     }
37     return false;
38 }
39 
WrapConfiguration(napi_env env,const AppExecFwk::Configuration & configuration)40 napi_value WrapConfiguration(napi_env env, const AppExecFwk::Configuration &configuration)
41 {
42     HILOG_INFO("%{public}s called, config size %{public}d", __func__, static_cast<int>(configuration.GetItemSize()));
43     napi_value jsObject = nullptr;
44     NAPI_CALL(env, napi_create_object(env, &jsObject));
45 
46     napi_value jsValue = nullptr;
47     jsValue = WrapStringToJS(env, configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE));
48     SetPropertyValueByPropertyName(env, jsObject, "language", jsValue);
49 
50     jsValue = WrapInt32ToJS(
51         env, ConvertColorMode(configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE)));
52     SetPropertyValueByPropertyName(env, jsObject, "colorMode", jsValue);
53 
54     int32_t displayId = ConvertDisplayId(configuration.GetItem(ConfigurationInner::APPLICATION_DISPLAYID));
55 
56     std::string direction = configuration.GetItem(displayId, ConfigurationInner::APPLICATION_DIRECTION);
57     jsValue = WrapInt32ToJS(env, ConvertDirection(direction));
58     SetPropertyValueByPropertyName(env, jsObject, "direction", jsValue);
59 
60     std::string density = configuration.GetItem(displayId, ConfigurationInner::APPLICATION_DENSITYDPI);
61     jsValue = WrapInt32ToJS(env, ConvertDensity(density));
62     SetPropertyValueByPropertyName(env, jsObject, "screenDensity", jsValue);
63 
64     jsValue = WrapInt32ToJS(env, displayId);
65     SetPropertyValueByPropertyName(env, jsObject, "displayId", jsValue);
66 
67     std::string hasPointerDevice = configuration.GetItem(AAFwk::GlobalConfigurationKey::INPUT_POINTER_DEVICE);
68     jsValue = WrapBoolToJS(env, hasPointerDevice == "true" ? true : false);
69     SetPropertyValueByPropertyName(env, jsObject, "hasPointerDevice", jsValue);
70 
71     return jsObject;
72 }
73 
UnwrapConfiguration(napi_env env,napi_value param,Configuration & config)74 bool UnwrapConfiguration(napi_env env, napi_value param, Configuration &config)
75 {
76     HILOG_INFO("%{public}s called.", __func__);
77 
78     if (!IsTypeForNapiValue(env, param, napi_object)) {
79         HILOG_INFO("%{public}s called. Params is invalid.", __func__);
80         return false;
81     }
82 
83     std::string language {""};
84     if (UnwrapStringByPropertyName(env, param, "language", language)) {
85         HILOG_DEBUG("The parsed language part %{public}s", language.c_str());
86         if (!config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, language)) {
87             HILOG_ERROR("language Parsing failed");
88             return false;
89         }
90     }
91 
92     int32_t colormode = -1;
93     if (UnwrapInt32ByPropertyName(env, param, "colorMode", colormode)) {
94         HILOG_DEBUG("The parsed colormode part %{public}d", colormode);
95         if (colormode != Global::Resource::DARK && colormode != Global::Resource::LIGHT) {
96             HILOG_ERROR("Set colorMode to unsupported value.");
97             return false;
98         }
99         if (!config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE, GetColorModeStr(colormode))) {
100             HILOG_ERROR("colorMode parsing failed");
101             return false;
102         }
103     }
104 
105     return true;
106 }
107 EXTERN_C_END
108 }  // namespace AppExecFwk
109 }  // namespace OHOS
110