• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 
17 #include "cj_utils_ffi.h"
18 
19 #include "securec.h"
20 #include "hilog_tag_wrapper.h"
21 #include "cj_macro.h"
22 
CreateCStringFromString(const std::string & source)23 char* CreateCStringFromString(const std::string& source)
24 {
25     if (source.size() == 0) {
26         return nullptr;
27     }
28     size_t length = source.size() + 1;
29     auto res = static_cast<char*>(malloc(length));
30     if (res == nullptr) {
31         TAG_LOGE(AAFwkTag::DEFAULT, "null res");
32         return nullptr;
33     }
34     if (strcpy_s(res, length, source.c_str()) != 0) {
35         free(res);
36         TAG_LOGE(AAFwkTag::DEFAULT, "Strcpy failed");
37         return nullptr;
38     }
39     return res;
40 }
41 
VectorToCArrString(const std::vector<std::string> & vec)42 char** VectorToCArrString(const std::vector<std::string>& vec)
43 {
44     if (vec.size() == 0) {
45         return nullptr;
46     }
47     char** result = static_cast<char**>(malloc(sizeof(char*) * vec.size()));
48     if (result == nullptr) {
49         return nullptr;
50     }
51     for (size_t i = 0; i < vec.size(); i++) {
52         result[i] = CreateCStringFromString(vec[i]);
53     }
54     return result;
55 }
56 
ConvertColorMode(std::string colormode)57 int32_t ConvertColorMode(std::string colormode)
58 {
59     auto resolution = -1;
60     static const std::vector<std::pair<std::string, int32_t>> resolutions = {
61         { "dark", 0 },
62         { "light", 1 },
63     };
64     for (const auto& [tempColorMode, value] : resolutions) {
65         if (tempColorMode == colormode) {
66             resolution = value;
67             break;
68         }
69     }
70     return resolution;
71 }
72 
ConvertDirection(std::string direction)73 int32_t ConvertDirection(std::string direction)
74 {
75     auto resolution = -1;
76     static const std::vector<std::pair<std::string, int32_t>> resolutions = {
77         { "vertical", 0 },
78         { "horizontal", 1 },
79     };
80     for (const auto& [tempDirection, value] : resolutions) {
81         if (tempDirection == direction) {
82             resolution = value;
83             break;
84         }
85     }
86     return resolution;
87 }
88 
ConvertDensity(std::string density)89 int32_t ConvertDensity(std::string density)
90 {
91     auto resolution = 0;
92     static const std::vector<std::pair<std::string, int32_t>> resolutions = {
93         { "sdpi", 120 },
94         { "mdpi", 160 },
95         { "ldpi", 240 },
96         { "xldpi", 320 },
97         { "xxldpi", 480 },
98         { "xxxldpi", 640 },
99     };
100     for (const auto& [tempdensity, value] : resolutions) {
101         if (tempdensity == density) {
102             resolution = value;
103             break;
104         }
105     }
106     return resolution;
107 }
108 
ConvertDisplayId(std::string displayId)109 int32_t ConvertDisplayId(std::string displayId)
110 {
111     if (displayId == OHOS::AppExecFwk::ConfigurationInner::EMPTY_STRING) {
112         return -1;
113     }
114     return std::stoi(displayId);
115 }
116 
117 namespace OHOS {
118 namespace AbilityRuntime {
119 
CreateCConfiguration(const OHOS::AppExecFwk::Configuration & configuration)120 CConfiguration CreateCConfiguration(const OHOS::AppExecFwk::Configuration &configuration)
121 {
122     CConfiguration cfg;
123     cfg.language = CreateCStringFromString(configuration.GetItem(
124         OHOS::AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE));
125     cfg.colorMode = ConvertColorMode(configuration.GetItem(OHOS::AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE));
126     std::string direction = configuration.GetItem(OHOS::AppExecFwk::ConfigurationInner::APPLICATION_DIRECTION);
127     cfg.direction = ConvertDirection(direction);
128     std::string density = configuration.GetItem(OHOS::AppExecFwk::ConfigurationInner::APPLICATION_DENSITYDPI);
129     cfg.screenDensity = ConvertDensity(density);
130     cfg.displayId = ConvertDisplayId(configuration.GetItem(
131         OHOS::AppExecFwk::ConfigurationInner::APPLICATION_DISPLAYID));
132     std::string hasPointerDevice = configuration.GetItem(OHOS::AAFwk::GlobalConfigurationKey::INPUT_POINTER_DEVICE);
133     cfg.hasPointerDevice = hasPointerDevice == "true" ? true : false;
134     std::string fontSizeScale = configuration.GetItem(OHOS::AAFwk::GlobalConfigurationKey::SYSTEM_FONT_SIZE_SCALE);
135     cfg.fontSizeScale = fontSizeScale == "" ? 1.0 : std::stod(fontSizeScale);
136     std::string fontWeightScale = configuration.GetItem(
137         OHOS::AAFwk::GlobalConfigurationKey::SYSTEM_FONT_WEIGHT_SCALE);
138     cfg.fontWeightScale = fontWeightScale == "" ? 1.0 : std::stod(fontWeightScale);
139     cfg.mcc = CreateCStringFromString(configuration.GetItem(OHOS::AAFwk::GlobalConfigurationKey::SYSTEM_MCC));
140     cfg.mnc = CreateCStringFromString(configuration.GetItem(OHOS::AAFwk::GlobalConfigurationKey::SYSTEM_MNC));
141     return cfg;
142 }
143 
144 extern "C" {
OHOS_ConvertConfiguration(void * param)145 CJ_EXPORT CConfiguration OHOS_ConvertConfiguration(void* param)
146 {
147     CConfiguration cCfg = {};
148     auto config = reinterpret_cast<AppExecFwk::Configuration*>(param);
149     if (config == nullptr) {
150         return cCfg;
151     }
152     return CreateCConfiguration(*config);
153 }
154 }
155 }
156 }