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
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_resource_bridge.h"
17
18 #include <cstdint>
19
20 #include "jsnapi_expo.h"
21
22 #include "base/utils/device_config.h"
23 #include "base/utils/system_properties.h"
24 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_api_bridge.h"
25 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
26 #include "core/common/resource/resource_manager.h"
27 #include "core/components/common/properties/color.h"
28
29 namespace OHOS::Ace::NG {
30 namespace {
MapJsColorModeToColorMode(int32_t jsColorMode)31 ColorMode MapJsColorModeToColorMode(int32_t jsColorMode)
32 {
33 switch (jsColorMode) {
34 case 1: // 1 is the ThemeColorMode.LIGHT
35 return ColorMode::LIGHT;
36 case 2: // 2 is the ThemeColorMode.DARK
37 return ColorMode::DARK;
38 default:
39 return ColorMode::COLOR_MODE_UNDEFINED;
40 }
41 return ColorMode::COLOR_MODE_UNDEFINED;
42 }
43
44 #if defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM)
UpdateColorModeForThemeConstants(const ColorMode & colorMode)45 void UpdateColorModeForThemeConstants(const ColorMode& colorMode)
46 {
47 auto container = Container::Current();
48 CHECK_NULL_VOID(container);
49 auto resConfig = container->GetResourceConfiguration();
50 resConfig.SetColorMode(colorMode);
51
52 auto themeManager = PipelineBase::CurrentThemeManager();
53 CHECK_NULL_VOID(themeManager);
54 auto themeConstants = themeManager->GetThemeConstants();
55 CHECK_NULL_VOID(themeConstants);
56 themeConstants->UpdateConfig(resConfig);
57 }
58 #endif
59 } // namespace
60
UpdateColorMode(ArkUIRuntimeCallInfo * runtimeCallInfo)61 ArkUINativeModuleValue ResourceBridge::UpdateColorMode(ArkUIRuntimeCallInfo* runtimeCallInfo)
62 {
63 EcmaVM* vm = runtimeCallInfo->GetVM();
64 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
65 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
66 ColorMode colorModeValue = ColorMode::COLOR_MODE_UNDEFINED;
67 if (firstArg->IsNumber()) {
68 int32_t firstArgValue = firstArg->Int32Value(vm);
69 colorModeValue = MapJsColorModeToColorMode(firstArgValue);
70 }
71 if (colorModeValue != ColorMode::COLOR_MODE_UNDEFINED) {
72 auto pipeline = NG::PipelineContext::GetCurrentContextSafely();
73 CHECK_NULL_RETURN(pipeline, panda::JSValueRef::Undefined(vm));
74 #if defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM)
75 UpdateColorModeForThemeConstants(colorModeValue);
76 #else
77 ResourceManager::GetInstance().UpdateColorMode(
78 pipeline->GetBundleName(), pipeline->GetModuleName(), pipeline->GetInstanceId(), colorModeValue);
79 #endif
80 pipeline->SetLocalColorMode(colorModeValue);
81 }
82 return panda::JSValueRef::Undefined(vm);
83 }
84
Restore(ArkUIRuntimeCallInfo * runtimeCallInfo)85 ArkUINativeModuleValue ResourceBridge::Restore(ArkUIRuntimeCallInfo* runtimeCallInfo)
86 {
87 EcmaVM* vm = runtimeCallInfo->GetVM();
88 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
89
90 auto pipeline = NG::PipelineContext::GetCurrentContextSafely();
91 CHECK_NULL_RETURN(pipeline, panda::JSValueRef::Undefined(vm));
92 pipeline->SetLocalColorMode(ColorMode::COLOR_MODE_UNDEFINED);
93
94 auto colorModeValue = pipeline->GetColorMode();
95 #if defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM)
96 UpdateColorModeForThemeConstants(colorModeValue);
97 #else
98 ResourceManager::GetInstance().UpdateColorMode(
99 pipeline->GetBundleName(), pipeline->GetModuleName(), pipeline->GetInstanceId(), colorModeValue);
100 #endif
101 return panda::JSValueRef::Undefined(vm);
102 }
103
GetColorValue(ArkUIRuntimeCallInfo * runtimeCallInfo)104 ArkUINativeModuleValue ResourceBridge::GetColorValue(ArkUIRuntimeCallInfo* runtimeCallInfo)
105 {
106 EcmaVM* vm = runtimeCallInfo->GetVM();
107 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
108 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
109 Color color;
110 if (ArkTSUtils::ParseJsColorAlpha(vm, firstArg, color)) {
111 uint32_t colorValue = color.GetValue();
112 return panda::NumberRef::New(vm, colorValue);
113 }
114 return panda::JSValueRef::Undefined(vm);
115 }
116
GetStringValue(ArkUIRuntimeCallInfo * runtimeCallInfo)117 ArkUINativeModuleValue ResourceBridge::GetStringValue(ArkUIRuntimeCallInfo* runtimeCallInfo)
118 {
119 EcmaVM* vm = runtimeCallInfo->GetVM();
120 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
121 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
122 std::string result;
123 if (ArkTSUtils::ParseJsString(vm, firstArg, result)) {
124 return panda::StringRef::NewFromUtf8(vm, result.c_str());
125 }
126 return panda::JSValueRef::Undefined(vm);
127 }
128
GetNumberValue(ArkUIRuntimeCallInfo * runtimeCallInfo)129 ArkUINativeModuleValue ResourceBridge::GetNumberValue(ArkUIRuntimeCallInfo* runtimeCallInfo)
130 {
131 EcmaVM* vm = runtimeCallInfo->GetVM();
132 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
133 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
134 double result;
135 if (ArkTSUtils::ParseJsDouble(vm, firstArg, result)) {
136 return panda::NumberRef::New(vm, result);
137 }
138 return panda::JSValueRef::Undefined(vm);
139 }
140
ClearCache(ArkUIRuntimeCallInfo * runtimeCallInfo)141 ArkUINativeModuleValue ResourceBridge::ClearCache(ArkUIRuntimeCallInfo* runtimeCallInfo)
142 {
143 EcmaVM* vm = runtimeCallInfo->GetVM();
144 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
145 ResourceManager::GetInstance().Reset();
146 return panda::JSValueRef::Undefined(vm);
147 }
148 } // namespace OHOS::Ace::NG
149