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 #include "cj_environment.h"
17 
18 #include "core/common/ace_application_info.h"
19 #include "core/common/container.h"
20 #include "core/common/environment/environment_proxy.h"
21 #include "frameworks/base/i18n/localization.h"
22 #include "bridge/cj_frontend/interfaces/cj_ffi/utils.h"
23 
24 using namespace OHOS::Ace;
25 using namespace OHOS::FFI;
26 using namespace OHOS::Ace::Framework;
27 
28 extern "C" {
FfiOHOSAceFrameworkEnvironmentGetAccessibilityEnabled()29 NativeOptionBool FfiOHOSAceFrameworkEnvironmentGetAccessibilityEnabled()
30 {
31     NativeOptionBool result {.hasValue = false, .value = false };
32 #if defined(PREVIEW)
33     return result;
34 #else
35     auto container = Container::Current();
36     if (!container) {
37         LOGW("container is null");
38         return result;
39     }
40     auto executor = container->GetTaskExecutor();
41 
42     result.hasValue = true;
43     std::string val = EnvironmentProxy::GetInstance()->GetEnvironment(executor)->GetAccessibilityEnabled();
44 #endif
45     result.value = val.c_str();
46     return result;
47 }
48 
FfiOHOSAceFrameworkEnvironmentGetColorMode()49 NativeOptionInt32 FfiOHOSAceFrameworkEnvironmentGetColorMode()
50 {
51     NativeOptionInt32 result {
52         .hasValue = true,
53         .value = static_cast<int32_t>(SystemProperties::GetColorMode())
54     };
55     return result;
56 }
57 
FfiOHOSAceFrameworkEnvironmentGetFontScale()58 NativeOptionFloat32 FfiOHOSAceFrameworkEnvironmentGetFontScale()
59 {
60     NativeOptionFloat32 result {.hasValue = false, .value = 0.0};
61 
62     auto container = Container::Current();
63     if (!container) {
64         LOGW("container is null");
65         return result;
66     }
67     auto context = container->GetPipelineContext();
68     result.hasValue = true;
69     result.value = context->GetFontScale();
70     return result;
71 }
72 
FfiOHOSAceFrameworkEnvironmentGetFontWeightScale()73 NativeOptionFloat32 FfiOHOSAceFrameworkEnvironmentGetFontWeightScale()
74 {
75     NativeOptionFloat32 result {
76         .hasValue = true,
77         .value = SystemProperties::GetFontWeightScale()
78     };
79     return result;
80 }
81 
FfiOHOSAceFrameworkEnvironmentGetLayoutDirection()82 NativeOptionInt32 FfiOHOSAceFrameworkEnvironmentGetLayoutDirection()
83 {
84     auto isRTL = AceApplicationInfo::GetInstance().IsRightToLeft();
85     NativeOptionInt32 result {
86         .hasValue = true,
87         .value =  isRTL ? 0 : 1
88     };
89     return result;
90 }
91 
FfiOHOSAceFrameworkEnvironmentGetLanguageCode()92 NativeOptionCString FfiOHOSAceFrameworkEnvironmentGetLanguageCode()
93 {
94     NativeOptionCString result {.hasValue = false, .value = Utils::MallocCString("").value};
95     auto container = Container::Current();
96     if (!container) {
97         LOGW("Initialize is not complete, cannot get the language");
98         return result;
99     }
100     auto location = Localization::GetInstance();
101     auto language = location->GetLanguage();
102     result.hasValue = true;
103     result.value = Utils::MallocCString(language).value;
104     return result;
105 }
106 }