1 /*
2  * Copyright (c) 2023 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 "adapter/preview/entrance/ui_content_impl.h"
17 
18 #include <ui/rs_surface_node.h>
19 #include <ui/rs_ui_director.h>
20 
21 #include "include/core/SkFontMgr.h"
22 
23 #include "adapter/ohos/entrance/ace_new_pipe_judgement.h"
24 #include "adapter/ohos/entrance/platform_event_callback.h"
25 #include "adapter/preview/entrance/ace_application_info.h"
26 #include "adapter/preview/entrance/ace_container.h"
27 #include "adapter/preview/entrance/clipboard/clipboard_impl.h"
28 #include "adapter/preview/entrance/clipboard/clipboard_proxy_impl.h"
29 #include "adapter/preview/entrance/event_dispatcher.h"
30 #include "adapter/preview/entrance/rs_dir_asset_provider.h"
31 #include "adapter/preview/external/multimodalinput/axis_event.h"
32 #include "adapter/preview/external/multimodalinput/key_event.h"
33 #include "adapter/preview/external/multimodalinput/pointer_event.h"
34 #include "adapter/preview/inspector/inspector_client.h"
35 #include "base/log/log_wrapper.h"
36 #include "frameworks/base/log/log.h"
37 #include "frameworks/base/utils/utils.h"
38 #include "frameworks/bridge/common/utils/utils.h"
39 #include "frameworks/bridge/js_frontend/js_frontend.h"
40 #include "frameworks/core/common/ace_engine.h"
41 #ifdef INIT_ICU_DATA_PATH
42 #include "unicode/putil.h"
43 #endif
44 
45 #include "frameworks/simulator/common/include/context.h"
46 
47 namespace OHOS::Ace {
48 
49 using namespace Platform;
50 
51 namespace {
52 
53 #ifdef WINDOWS_PLATFORM
54 constexpr char DELIMITER[] = "\\";
55 constexpr char ASSET_PATH_SHARE_STAGE[] = "resources\\base\\profile";
56 #else
57 constexpr char DELIMITER[] = "/";
58 constexpr char ASSET_PATH_SHARE_STAGE[] = "resources/base/profile";
59 #endif
60 
ConvertAvoidArea(const OHOS::Rosen::AvoidArea & avoidArea)61 NG::SafeAreaInsets ConvertAvoidArea(const OHOS::Rosen::AvoidArea& avoidArea)
62 {
63     return NG::SafeAreaInsets({ avoidArea.leftRect_.posX_, avoidArea.leftRect_.posX_ + avoidArea.leftRect_.width_ },
64         { avoidArea.topRect_.posY_, avoidArea.topRect_.posY_ + avoidArea.topRect_.height_ },
65         { avoidArea.rightRect_.posX_, avoidArea.rightRect_.posX_ + avoidArea.rightRect_.width_ },
66         { avoidArea.bottomRect_.posY_, avoidArea.bottomRect_.posY_ + avoidArea.bottomRect_.height_ });
67 }
68 
SetFontMgrConfig(const std::string & containerSdkPath)69 void SetFontMgrConfig(const std::string& containerSdkPath)
70 {
71     // To check if use ohos or container fonts.
72     std::string runtimeOS = "OHOS_Container";
73     std::string containerFontBasePath = containerSdkPath + DELIMITER + "resources" + DELIMITER + "fonts" + DELIMITER;
74     RSDirAssetProvider dirAsset(containerFontBasePath);
75     std::vector<std::string> fileList;
76     dirAsset.GetAssetList("", fileList);
77     if (containerSdkPath.empty() || fileList.empty()) {
78         runtimeOS = "OHOS";
79         containerFontBasePath = "";
80     }
81     SkFontMgr::SetFontMgrConfig(runtimeOS, containerFontBasePath);
82 }
83 
84 } // namespace
85 
86 using ContentFinishCallback = std::function<void()>;
87 using ContentStartAbilityCallback = std::function<void(const std::string& address)>;
88 class ContentEventCallback final : public PlatformEventCallback {
89 public:
ContentEventCallback(ContentFinishCallback onFinish)90     explicit ContentEventCallback(ContentFinishCallback onFinish) : onFinish_(onFinish) {}
ContentEventCallback(ContentFinishCallback onFinish,ContentStartAbilityCallback onStartAbility)91     ContentEventCallback(ContentFinishCallback onFinish, ContentStartAbilityCallback onStartAbility)
92         : onFinish_(onFinish), onStartAbility_(onStartAbility)
93     {}
94     ~ContentEventCallback() override = default;
95 
OnFinish() const96     void OnFinish() const override
97     {
98         LOGI("UIContent OnFinish");
99         CHECK_NULL_VOID(onFinish_);
100         onFinish_();
101     }
102 
OnStartAbility(const std::string & address)103     void OnStartAbility(const std::string& address) override
104     {
105         LOGI("UIContent OnStartAbility");
106         CHECK_NULL_VOID(onStartAbility_);
107         onStartAbility_(address);
108     }
109 
OnStatusBarBgColorChanged(uint32_t color)110     void OnStatusBarBgColorChanged(uint32_t color) override
111     {
112         LOGI("UIContent OnStatusBarBgColorChanged");
113     }
114 
115 private:
116     ContentFinishCallback onFinish_;
117     ContentStartAbilityCallback onStartAbility_;
118 };
119 
120 class DragWindowListener : public OHOS::Rosen::IWindowDragListener {
121 public:
DragWindowListener(int32_t instanceId)122     explicit DragWindowListener(int32_t instanceId) : instanceId_(instanceId) {}
123     ~DragWindowListener() = default;
OnDrag(int32_t x,int32_t y,OHOS::Rosen::DragEvent event)124     void OnDrag(int32_t x, int32_t y, OHOS::Rosen::DragEvent event)
125     {
126         LOGI("[Engine Log] The feature is not supported on the previewer, and instanceId_ = %{public}d", instanceId_);
127     }
128 
129 private:
130     int32_t instanceId_ = -1;
131 };
132 
133 class TouchOutsideListener : public OHOS::Rosen::ITouchOutsideListener {
134 public:
TouchOutsideListener(int32_t instanceId)135     explicit TouchOutsideListener(int32_t instanceId) : instanceId_(instanceId) {}
136     ~TouchOutsideListener() = default;
137 
OnTouchOutside() const138     void OnTouchOutside() const
139     {
140         LOGI("[Engine Log] The feature is not supported on the previewer, and instanceId_ = %{public}d", instanceId_);
141     }
142 
143 private:
144     int32_t instanceId_ = -1;
145 };
146 
147 class IIgnoreViewSafeAreaListener : public OHOS::Rosen::IIgnoreViewSafeAreaListener {
148 public:
IIgnoreViewSafeAreaListener(int32_t instanceId)149     explicit IIgnoreViewSafeAreaListener(int32_t instanceId) : instanceId_(instanceId) {}
150     ~IIgnoreViewSafeAreaListener() = default;
151 
SetIgnoreViewSafeArea(bool ignoreViewSafeArea)152     void SetIgnoreViewSafeArea(bool ignoreViewSafeArea)
153     {
154         LOGD("[instanceId_:%{public}d]: SetIgnoreViewSafeArea:%{public}u", instanceId_, ignoreViewSafeArea);
155         auto container = AceEngine::Get().GetContainer(instanceId_);
156         CHECK_NULL_VOID(container);
157         auto pipelineContext = container->GetPipelineContext();
158         auto taskExecutor = container->GetTaskExecutor();
159         CHECK_NULL_VOID(taskExecutor);
160         taskExecutor->PostSyncTask(
161             [&pipelineContext, container, ignoreSafeArea = ignoreViewSafeArea]() {
162                 pipelineContext->SetIgnoreViewSafeArea(ignoreSafeArea);
163             },
164             TaskExecutor::TaskType::UI, "ArkUISetIgnoreViewSafeArea");
165     }
166 
167 private:
168     int32_t instanceId_ = -1;
169 };
170 
171 class AvoidAreaChangedListener : public OHOS::Rosen::IAvoidAreaChangedListener {
172 public:
AvoidAreaChangedListener(int32_t instanceId)173     explicit AvoidAreaChangedListener(int32_t instanceId) : instanceId_(instanceId) {}
174     ~AvoidAreaChangedListener() = default;
175 
OnAvoidAreaChanged(const OHOS::Rosen::AvoidArea avoidArea,OHOS::Rosen::AvoidAreaType type)176     void OnAvoidAreaChanged(const OHOS::Rosen::AvoidArea avoidArea, OHOS::Rosen::AvoidAreaType type)
177     {
178         LOGD("Avoid area changed, type:%{public}d, topRect: avoidArea:x:%{public}d, y:%{public}d, "
179              "width:%{public}d, height:%{public}d; bottomRect: avoidArea:x:%{public}d, y:%{public}d, "
180              "width:%{public}d, height:%{public}d",
181             type, avoidArea.topRect_.posX_, avoidArea.topRect_.posY_, (int32_t)avoidArea.topRect_.width_,
182             (int32_t)avoidArea.topRect_.height_, avoidArea.bottomRect_.posX_, avoidArea.bottomRect_.posY_,
183             (int32_t)avoidArea.bottomRect_.width_, (int32_t)avoidArea.bottomRect_.height_);
184         auto container = Platform::AceContainer::GetContainerInstance(instanceId_);
185         CHECK_NULL_VOID(container);
186         auto pipeline = container->GetPipelineContext();
187         CHECK_NULL_VOID(pipeline);
188         auto taskExecutor = container->GetTaskExecutor();
189         CHECK_NULL_VOID(taskExecutor);
190         if (type == Rosen::AvoidAreaType::TYPE_SYSTEM) {
191             systemSafeArea_ = ConvertAvoidArea(avoidArea);
192         } else if (type == Rosen::AvoidAreaType::TYPE_NAVIGATION_INDICATOR) {
193             navigationBar_ = ConvertAvoidArea(avoidArea);
194         } else if (type == Rosen::AvoidAreaType::TYPE_CUTOUT) {
195             cutoutSafeArea_ = ConvertAvoidArea(avoidArea);
196         }
197         auto safeArea = systemSafeArea_;
198         auto navSafeArea = navigationBar_;
199         auto cutoutSafeArea = cutoutSafeArea_;
200         ContainerScope scope(instanceId_);
201         taskExecutor->PostTask(
202             [pipeline, safeArea, navSafeArea, cutoutSafeArea, type, avoidArea] {
203                 if (type == Rosen::AvoidAreaType::TYPE_SYSTEM) {
204                     pipeline->UpdateSystemSafeArea(safeArea);
205                 } else if (type == Rosen::AvoidAreaType::TYPE_NAVIGATION_INDICATOR) {
206                     pipeline->UpdateNavSafeArea(navSafeArea);
207                 } else if (type == Rosen::AvoidAreaType::TYPE_CUTOUT && pipeline->GetUseCutout()) {
208                     pipeline->UpdateCutoutSafeArea(cutoutSafeArea);
209                 }
210                 // for ui extension component
211                 pipeline->UpdateOriginAvoidArea(avoidArea, static_cast<uint32_t>(type));
212             },
213             TaskExecutor::TaskType::UI, "ArkUIUpdateOriginAvoidArea");
214     }
215 
216 private:
217     NG::SafeAreaInsets systemSafeArea_;
218     NG::SafeAreaInsets navigationBar_;
219     NG::SafeAreaInsets cutoutSafeArea_;
220     int32_t instanceId_ = -1;
221 };
222 
OHOS_ACE_CreateUIContent(void * context,void * runtime)223 extern "C" ACE_FORCE_EXPORT void* OHOS_ACE_CreateUIContent(void* context, void* runtime)
224 {
225     return new UIContentImpl(reinterpret_cast<OHOS::AbilityRuntime::Context*>(context), runtime);
226 }
227 
OHOS_ACE_CreateFormContent(void * context,void * runtime,bool isCard)228 extern "C" ACE_FORCE_EXPORT void* OHOS_ACE_CreateFormContent(void* context, void* runtime, bool isCard)
229 {
230     return new UIContentImpl(reinterpret_cast<OHOS::AbilityRuntime::Context*>(context), runtime, isCard);
231 }
232 
OHOS_ACE_CreateSubWindowUIContent(void * ability)233 extern "C" ACE_FORCE_EXPORT void* OHOS_ACE_CreateSubWindowUIContent(void* ability)
234 {
235     return new UIContentImpl(reinterpret_cast<OHOS::AppExecFwk::Ability*>(ability));
236 }
237 
UIContentImpl(OHOS::AbilityRuntime::Context * context,void * runtime)238 UIContentImpl::UIContentImpl(OHOS::AbilityRuntime::Context* context, void* runtime)
239     : instanceId_(ACE_INSTANCE_ID), runtime_(runtime)
240 {
241     // 基于Options的方式传递参数
242     auto options = context->GetOptions();
243     assetPath_ = options.assetPath;
244     systemResourcesPath_ = options.systemResourcePath;
245     appResourcesPath_ = options.appResourcePath;
246     containerSdkPath_ = options.containerSdkPath;
247     language_ = options.language;
248     region_ = options.region;
249     script_ = options.script;
250     themeId_ = options.themeId;
251     deviceWidth_ = options.deviceWidth;
252     deviceHeight_ = options.deviceHeight;
253     isRound_ = options.isRound;
254     onRouterChange_ = options.onRouterChange;
255     deviceConfig_.orientation = static_cast<DeviceOrientation>(options.deviceConfig.orientation);
256     deviceConfig_.deviceType = static_cast<DeviceType>(options.deviceConfig.deviceType);
257     deviceConfig_.colorMode = static_cast<ColorMode>(options.deviceConfig.colorMode);
258     deviceConfig_.density = options.deviceConfig.density;
259     deviceConfig_.fontRatio = options.deviceConfig.fontRatio;
260 
261     bundleName_ = options.bundleName;
262     compatibleVersion_ = options.compatibleVersion;
263     installationFree_ = options.installationFree;
264     labelId_ = options.labelId;
265     moduleName_ = options.moduleName;
266     compileMode_ = options.compileMode;
267     pageProfile_ = options.pageProfile;
268     const std::string profilePrefix = "$profile:";
269     if (pageProfile_.compare(0, profilePrefix.size(), profilePrefix) == 0) {
270         pageProfile_ = pageProfile_.substr(profilePrefix.length()).append(".json");
271     }
272     targetVersion_ = options.targetVersion;
273     auto releaseType = options.releaseType;
274     bool enablePartialUpdate = options.enablePartialUpdate;
275     useNewPipeline_ = AceNewPipeJudgement::QueryAceNewPipeEnabledStage(
276         "", compatibleVersion_, targetVersion_, releaseType, !enablePartialUpdate);
277 }
278 
UIContentImpl(OHOS::AbilityRuntime::Context * context,void * runtime,bool isCard)279 UIContentImpl::UIContentImpl(OHOS::AbilityRuntime::Context* context, void* runtime, bool isCard)
280     : instanceId_(ACE_INSTANCE_ID), runtime_(runtime), isFormRender_(isCard)
281 {
282     LOGI("The constructor is used to support ets card, isFormRender_ = %{public}d", isFormRender_);
283     if (context) {
284         auto options = context->GetOptions();
285         bundleName_ = options.bundleName;
286         moduleName_ = options.moduleName;
287     }
288 }
289 
UIContentImpl(OHOS::AppExecFwk::Ability * ability)290 UIContentImpl::UIContentImpl(OHOS::AppExecFwk::Ability* ability) : instanceId_(ACE_INSTANCE_ID) {}
291 
DestroyUIDirector()292 void UIContentImpl::DestroyUIDirector()
293 {
294     auto container = AceContainer::GetContainerInstance(instanceId_);
295     CHECK_NULL_VOID(container);
296     auto pipelineContext = AceType::DynamicCast<PipelineContext>(container->GetPipelineContext());
297     CHECK_NULL_VOID(pipelineContext);
298     auto rsUIDirector = pipelineContext->GetRSUIDirector();
299     CHECK_NULL_VOID(rsUIDirector);
300     rsUIDirector->Destroy();
301 }
302 
DestroyCallback() const303 void UIContentImpl::DestroyCallback() const
304 {
305     auto container = AceContainer::GetContainerInstance(instanceId_);
306     CHECK_NULL_VOID(container);
307     auto pipelineContext = container->GetPipelineContext();
308     CHECK_NULL_VOID(pipelineContext);
309     pipelineContext->SetNextFrameLayoutCallback(nullptr);
310 }
311 
Initialize(OHOS::Rosen::Window * window,const std::string & url,napi_value storage)312 UIContentErrorCode UIContentImpl::Initialize(OHOS::Rosen::Window* window, const std::string& url,
313     napi_value storage)
314 {
315     auto errorCode = UIContentErrorCode::NO_ERRORS;
316     errorCode = CommonInitialize(window, url, storage);
317     CHECK_ERROR_CODE_RETURN(errorCode);
318     errorCode = AceContainer::RunPage(instanceId_, url, "");
319     return errorCode;
320 }
321 
GetContentInfo(ContentInfoType type) const322 std::string UIContentImpl::GetContentInfo(ContentInfoType type) const
323 {
324     return AceContainer::GetContentInfo(instanceId_, type);
325 }
326 
CommonInitialize(OHOS::Rosen::Window * window,const std::string & contentInfo,napi_value storage)327 UIContentErrorCode UIContentImpl::CommonInitialize(OHOS::Rosen::Window* window,
328     const std::string& contentInfo, napi_value storage)
329 {
330     static std::once_flag onceFlag;
331     std::call_once(onceFlag, []() {
332 #ifdef INIT_ICU_DATA_PATH
333         std::string icuPath = ".";
334         u_setDataDirectory(icuPath.c_str());
335 #endif
336         Container::UpdateCurrent(INSTANCE_ID_PLATFORM);
337         ClipboardProxy::GetInstance()->SetDelegate(std::make_unique<Platform::ClipboardProxyImpl>());
338     });
339     rsWindow_ = window;
340 
341     AceApplicationInfo::GetInstance().SetLocale(language_, region_, script_, "");
342     AceApplicationInfo::GetInstance().SetApiTargetVersion(targetVersion_);
343     SetFontMgrConfig(containerSdkPath_);
344     EventDispatcher::GetInstance().Initialize();
345     SystemProperties::SetExtSurfaceEnabled(!containerSdkPath_.empty());
346     SystemProperties::InitDeviceInfo(deviceWidth_, deviceHeight_,
347         deviceConfig_.orientation == DeviceOrientation::PORTRAIT ? 0 : 1, deviceConfig_.density, isRound_);
348     SystemProperties::InitDeviceType(deviceConfig_.deviceType);
349     SystemProperties::SetColorMode(deviceConfig_.colorMode);
350     LOGI("CreateContainer with JSDECLARATIVE frontend, set MinPlatformVersion to %{public}d", compatibleVersion_);
351     AceContainer::CreateContainer(instanceId_, FrontendType::DECLARATIVE_JS, useNewPipeline_);
352     auto container = AceContainer::GetContainerInstance(instanceId_);
353     CHECK_NULL_RETURN(container, UIContentErrorCode::NULL_POINTER);
354     container->SetContainerSdkPath(containerSdkPath_);
355     container->SetIsFRSCardContainer(false);
356     container->SetBundleName(bundleName_);
357     container->SetModuleName(moduleName_);
358     LOGI("Save bundle %{public}s, module %{public}s", bundleName_.c_str(), moduleName_.c_str());
359     if (runtime_) {
360         container->GetSettings().SetUsingSharedRuntime(true);
361         container->SetSharedRuntime(runtime_);
362     } else {
363         container->GetSettings().SetUsingSharedRuntime(false);
364     }
365     container->SetInstallationFree(installationFree_);
366     container->SetLabelId(labelId_);
367     auto config = container->GetResourceConfiguration();
368     config.SetDeviceType(SystemProperties::GetDeviceType());
369     config.SetOrientation(SystemProperties::GetDeviceOrientation());
370     config.SetDensity(SystemProperties::GetResolution());
371     config.SetColorMode(SystemProperties::GetColorMode());
372     config.SetFontRatio(deviceConfig_.fontRatio);
373     container->SetResourceConfiguration(config);
374     container->SetPageProfile(pageProfile_);
375     container->SetApiTargetVersion(targetVersion_);
376     std::vector<std::string> paths;
377     paths.push_back(assetPath_);
378     std::string appResourcesPath(appResourcesPath_);
379     if (!OHOS::Ace::Framework::EndWith(appResourcesPath, DELIMITER)) {
380         appResourcesPath.append(DELIMITER);
381     }
382     paths.push_back(appResourcesPath);
383     paths.push_back(appResourcesPath + ASSET_PATH_SHARE_STAGE);
384     if (!containerSdkPath_.empty()) {
385         paths.push_back(containerSdkPath_);
386     }
387     AceContainer::AddAssetPath(instanceId_, "", paths);
388     AceContainer::SetResourcesPathAndThemeStyle(
389         instanceId_, systemResourcesPath_, containerSdkPath_, appResourcesPath_, themeId_, deviceConfig_.colorMode);
390 
391     auto view = AceViewPreview::CreateView(instanceId_, false, container->GetSettings().usePlatformAsUIThread);
392     UIEnvCallback callback = [window = rsWindow_, id = instanceId_](
393                                  const OHOS::Ace::RefPtr<PipelineContext>& context) mutable {
394         CHECK_NULL_VOID(context);
395         CHECK_NULL_VOID(window);
396         auto director = OHOS::Rosen::RSUIDirector::Create();
397         CHECK_NULL_VOID(director);
398         director->SetRSSurfaceNode(window->GetSurfaceNode());
399         auto container = AceContainer::GetContainerInstance(id);
400         CHECK_NULL_VOID(container);
401         auto func = [taskExecutor = container->GetTaskExecutor(), id](
402             const std::function<void()>& task, uint32_t delay) {
403             CHECK_NULL_VOID(taskExecutor);
404             ContainerScope scope(id);
405             taskExecutor->PostDelayedTask(
406                 task, TaskExecutor::TaskType::UI, delay, "ArkUIRenderServiceTask", PriorityType::HIGH);
407         };
408         director->SetUITaskRunner(func, id);
409         director->Init();
410         context->SetRSUIDirector(director);
411     };
412     AceContainer::SetView(view, rsWindow_, deviceConfig_.density, deviceWidth_, deviceHeight_, callback);
413     // Drive the native engine with the platform thread.
414     container->RunNativeEngineLoop();
415     auto pipelineContext = container->GetPipelineContext();
416     if (pipelineContext) {
417         pipelineContext->SetMinPlatformVersion(compatibleVersion_);
418         pipelineContext->SetDisplayWindowRectInfo(
419             Rect(Offset(0.0f, 0.0f), Size(deviceWidth_, deviceHeight_)));
420     }
421     container->InitializeAppConfig(assetPath_, bundleName_, moduleName_, compileMode_);
422     AceContainer::AddRouterChangeCallback(instanceId_, onRouterChange_);
423     // Should make it possible to update surface changes by using viewWidth and viewHeight.
424     view->NotifySurfaceChanged(deviceWidth_, deviceHeight_);
425     view->NotifyDensityChanged(deviceConfig_.density);
426     avoidAreaChangedListener_ = new AvoidAreaChangedListener(instanceId_);
427     rsWindow_->RegisterAvoidAreaChangeListener(avoidAreaChangedListener_);
428     ignoreViewSafeAreaListener_ = new IIgnoreViewSafeAreaListener(instanceId_);
429     window->RegisterIgnoreViewSafeAreaListener(ignoreViewSafeAreaListener_);
430     OHOS::Rosen::AvoidArea avoidArea;
431     rsWindow_->GetAvoidAreaByType(OHOS::Rosen::AvoidAreaType::TYPE_SYSTEM, avoidArea);
432     avoidAreaChangedListener_->OnAvoidAreaChanged(avoidArea, OHOS::Rosen::AvoidAreaType::TYPE_SYSTEM);
433     rsWindow_->GetAvoidAreaByType(OHOS::Rosen::AvoidAreaType::TYPE_NAVIGATION_INDICATOR, avoidArea);
434     avoidAreaChangedListener_->OnAvoidAreaChanged(avoidArea, OHOS::Rosen::AvoidAreaType::TYPE_NAVIGATION_INDICATOR);
435     return UIContentErrorCode::NO_ERRORS;
436 }
437 
Destroy()438 void UIContentImpl::Destroy()
439 {
440     LOGI("UIContentImpl: window destroy");
441     AceContainer::DestroyContainer(instanceId_);
442 }
443 
GetBackgroundColor()444 uint32_t UIContentImpl::GetBackgroundColor()
445 {
446     auto container = AceContainer::GetContainerInstance(instanceId_);
447     CHECK_NULL_RETURN(container, 0x000000);
448     auto taskExecutor = container->GetTaskExecutor();
449     CHECK_NULL_RETURN(taskExecutor, 0x000000);
450     ContainerScope scope(instanceId_);
451     uint32_t bgColor = 0x000000;
452     taskExecutor->PostSyncTask(
453         [&bgColor, container]() {
454             CHECK_NULL_VOID(container);
455             auto pipelineContext = container->GetPipelineContext();
456             CHECK_NULL_VOID(pipelineContext);
457             bgColor = pipelineContext->GetAppBgColor().GetValue();
458         },
459         TaskExecutor::TaskType::UI, "ArkUIGetAppBackgroundColor");
460 
461     return bgColor;
462 }
463 
SetBackgroundColor(uint32_t color)464 void UIContentImpl::SetBackgroundColor(uint32_t color)
465 {
466     LOGI("UIContentImpl: SetBackgroundColor color is %{public}u", color);
467     auto container = AceContainer::GetContainerInstance(instanceId_);
468     CHECK_NULL_VOID(container);
469     ContainerScope scope(instanceId_);
470     auto taskExecutor = container->GetTaskExecutor();
471     CHECK_NULL_VOID(taskExecutor);
472     taskExecutor->PostSyncTask(
473         [container, bgColor = color]() {
474             auto pipelineContext = container->GetPipelineContext();
475             CHECK_NULL_VOID(pipelineContext);
476             pipelineContext->SetAppBgColor(Color(bgColor));
477         },
478         TaskExecutor::TaskType::UI, "ArkUISetAppBackgroundColor");
479 }
480 
ProcessBackPressed()481 bool UIContentImpl::ProcessBackPressed()
482 {
483     LOGI("Process Back Pressed Event");
484     return EventDispatcher::GetInstance().DispatchBackPressedEvent();
485 }
486 
ProcessPointerEvent(const std::shared_ptr<OHOS::MMI::PointerEvent> & pointerEvent)487 bool UIContentImpl::ProcessPointerEvent(const std::shared_ptr<OHOS::MMI::PointerEvent>& pointerEvent)
488 {
489     return EventDispatcher::GetInstance().DispatchTouchEvent(pointerEvent);
490 }
491 
ProcessPointerEventWithCallback(const std::shared_ptr<OHOS::MMI::PointerEvent> & pointerEvent,const std::function<void ()> & callback)492 bool UIContentImpl::ProcessPointerEventWithCallback(
493     const std::shared_ptr<OHOS::MMI::PointerEvent>& pointerEvent, const std::function<void()>& callback)
494 {
495     auto result = EventDispatcher::GetInstance().DispatchTouchEvent(pointerEvent);
496     if (callback) {
497         callback();
498     }
499     return result;
500 }
501 
ProcessKeyEvent(const std::shared_ptr<OHOS::MMI::KeyEvent> & keyEvent,bool isPreIme)502 bool UIContentImpl::ProcessKeyEvent(const std::shared_ptr<OHOS::MMI::KeyEvent>& keyEvent, bool isPreIme)
503 {
504     // previewer never gets preIme-key event because there is no window
505     return EventDispatcher::GetInstance().DispatchKeyEvent(keyEvent);
506 }
507 
ProcessAxisEvent(const std::shared_ptr<OHOS::MMI::AxisEvent> & axisEvent)508 bool UIContentImpl::ProcessAxisEvent(const std::shared_ptr<OHOS::MMI::AxisEvent>& axisEvent)
509 {
510     return false;
511 }
512 
ProcessVsyncEvent(uint64_t timeStampNanos)513 bool UIContentImpl::ProcessVsyncEvent(uint64_t timeStampNanos)
514 {
515     return false;
516 }
517 
UpdateConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration> & config)518 void UIContentImpl::UpdateConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config) {}
519 
UpdateViewportConfig(const ViewportConfig & config,OHOS::Rosen::WindowSizeChangeReason reason,const std::shared_ptr<OHOS::Rosen::RSTransaction> & rsTransaction,const std::map<OHOS::Rosen::AvoidAreaType,OHOS::Rosen::AvoidArea> & avoidAreas)520 void UIContentImpl::UpdateViewportConfig(const ViewportConfig& config, OHOS::Rosen::WindowSizeChangeReason reason,
521     const std::shared_ptr<OHOS::Rosen::RSTransaction>& rsTransaction,
522     const std::map<OHOS::Rosen::AvoidAreaType, OHOS::Rosen::AvoidArea>& avoidAreas)
523 {
524     LOGI("ViewportConfig: %{public}s", config.ToString().c_str());
525     auto container = AceContainer::GetContainerInstance(instanceId_);
526     CHECK_NULL_VOID(container);
527     auto context = container->GetPipelineContext();
528     CHECK_NULL_VOID(context);
529     context->SetDisplayWindowRectInfo(
530         Rect(Offset(config.Left(), config.Top()), Size(config.Width(), config.Height())));
531     auto viewPtr = AceType::DynamicCast<AceViewPreview>(container->GetAceView());
532     CHECK_NULL_VOID(viewPtr);
533     SystemProperties::InitDeviceInfo(
534         config.Width(), config.Height(), config.Height() >= config.Width() ? 0 : 1, config.Density(), false);
535     deviceConfig_.orientation =
536         config.Height() >= config.Width() ? DeviceOrientation::PORTRAIT : DeviceOrientation::LANDSCAPE;
537     deviceConfig_.density = config.Density();
538     // Unlike the real machine, previewer require updating device configuration information to change window size.
539     container->UpdateDeviceConfig(deviceConfig_);
540     viewPtr->NotifyDensityChanged(config.Density());
541     viewPtr->NotifySurfaceChanged(config.Width(), config.Height());
542 }
543 
DumpInfo(const std::vector<std::string> & params,std::vector<std::string> & info)544 void UIContentImpl::DumpInfo(const std::vector<std::string>& params, std::vector<std::string>& info)
545 {
546     auto container = AceContainer::GetContainerInstance(instanceId_);
547     CHECK_NULL_VOID(container);
548     container->Dump(params, info);
549 }
550 
SetNextFrameLayoutCallback(std::function<void ()> && callback)551 void UIContentImpl::SetNextFrameLayoutCallback(std::function<void()>&& callback)
552 {
553     CHECK_NULL_VOID(callback);
554     auto container = AceContainer::GetContainerInstance(instanceId_);
555     CHECK_NULL_VOID(container);
556     auto pipelineContext = container->GetPipelineContext();
557     CHECK_NULL_VOID(pipelineContext);
558     pipelineContext->SetNextFrameLayoutCallback(std::move(callback));
559 }
560 
NotifyMemoryLevel(int32_t level)561 void UIContentImpl::NotifyMemoryLevel(int32_t level)
562 {
563     LOGI("Receive Memory level notification, level: %{public}d", level);
564     auto container = AceContainer::GetContainerInstance(instanceId_);
565     CHECK_NULL_VOID(container);
566     auto pipelineContext = container->GetPipelineContext();
567     CHECK_NULL_VOID(pipelineContext);
568     ContainerScope scope(instanceId_);
569     pipelineContext->NotifyMemoryLevel(level);
570 }
571 
CreateModalUIExtension(const AAFwk::Want & want,const ModalUIExtensionCallbacks & callbacks,const ModalUIExtensionConfig & config)572 int32_t UIContentImpl::CreateModalUIExtension(
573     const AAFwk::Want& want, const ModalUIExtensionCallbacks& callbacks, const ModalUIExtensionConfig& config)
574 {
575     return 0;
576 }
577 
CloseModalUIExtension(int32_t sessionId)578 void UIContentImpl::CloseModalUIExtension(int32_t sessionId) {}
579 
SetStatusBarItemColor(uint32_t color)580 void UIContentImpl::SetStatusBarItemColor(uint32_t color)
581 {
582     ContainerScope scope(instanceId_);
583     auto container = Platform::AceContainer::GetContainer(instanceId_);
584     CHECK_NULL_VOID(container);
585     auto appBar = container->GetAppBar();
586     CHECK_NULL_VOID(appBar);
587     appBar->SetStatusBarItemColor(IsDarkColor(color));
588 }
589 } // namespace OHOS::Ace
590