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 "root_scene.h"
17
18 #include <bundlemgr/launcher_service.h>
19 #include <event_handler.h>
20 #include <input_manager.h>
21 #include <int_wrapper.h>
22 #include <iremote_stub.h>
23 #include <transaction/rs_interfaces.h>
24 #include <ui_content.h>
25 #include <ui/rs_node.h>
26 #include <viewport_config.h>
27
28 #include "ability_context.h"
29 #include "app_mgr_client.h"
30 #include "display_manager.h"
31 #include "extension/extension_business_info.h"
32 #include "fold_screen_state_internel.h"
33 #include "input_transfer_station.h"
34 #include "rs_adapter.h"
35 #include "screen_session_manager_client.h"
36 #include "singleton.h"
37 #include "singleton_container.h"
38
39 #include "intention_event_manager.h"
40 #include "window_manager_hilog.h"
41
42 namespace OHOS {
43 namespace Rosen {
44 namespace {
45 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "RootScene" };
46 const std::string INPUT_AND_VSYNC_THREAD = "InputAndVsyncThread";
47 constexpr int32_t API_VERSION_18 = 18;
48
49 class BundleStatusCallback : public IRemoteStub<AppExecFwk::IBundleStatusCallback> {
50 public:
BundleStatusCallback(RootScene * rootScene)51 explicit BundleStatusCallback(RootScene* rootScene) : rootScene_(rootScene) {}
52 virtual ~BundleStatusCallback() = default;
53
OnBundleStateChanged(const uint8_t installType,const int32_t resultCode,const std::string & resultMsg,const std::string & bundleName)54 void OnBundleStateChanged(const uint8_t installType,
55 const int32_t resultCode, const std::string& resultMsg, const std::string& bundleName) override {}
56
OnBundleAdded(const std::string & bundleName,const int userId)57 void OnBundleAdded(const std::string& bundleName, const int userId) override
58 {
59 rootScene_->OnBundleUpdated(bundleName);
60 }
61
OnBundleUpdated(const std::string & bundleName,const int userId)62 void OnBundleUpdated(const std::string& bundleName, const int userId) override
63 {
64 rootScene_->OnBundleUpdated(bundleName);
65 }
66
OnBundleRemoved(const std::string & bundleName,const int userId)67 void OnBundleRemoved(const std::string& bundleName, const int userId) override {}
68
69 private:
70 RootScene* rootScene_;
71 };
72 } // namespace
73
74 sptr<RootScene> RootScene::staticRootScene_;
75 std::function<void(const std::shared_ptr<AppExecFwk::Configuration>&)> RootScene::configurationUpdatedCallback_;
76
RootScene()77 RootScene::RootScene()
78 {
79 launcherService_ = new AppExecFwk::LauncherService();
80 if (!launcherService_->RegisterCallback(new BundleStatusCallback(this))) {
81 WLOGFE("Failed to register bundle status callback.");
82 }
83
84 NodeId nodeId = 0;
85 vsyncStation_ = std::make_shared<VsyncStation>(nodeId);
86 handler_ = std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::GetMainEventRunner());
87 }
88
~RootScene()89 RootScene::~RootScene()
90 {
91 TLOGI(WmsLogTag::WMS_MAIN, "destroyed");
92 RemoveRootScene(DEFAULT_DISPLAY_ID);
93 uiContent_ = nullptr;
94 }
95
LoadContent(const std::string & contentUrl,napi_env env,napi_value storage,AbilityRuntime::Context * context)96 void RootScene::LoadContent(const std::string& contentUrl, napi_env env, napi_value storage,
97 AbilityRuntime::Context* context)
98 {
99 if (context == nullptr) {
100 WLOGFE("context is nullptr!");
101 return;
102 }
103 uiContent_ = Ace::UIContent::Create(context, reinterpret_cast<NativeEngine*>(env));
104 if (uiContent_ == nullptr) {
105 WLOGFE("uiContent_ is nullptr!");
106 return;
107 }
108
109 context_ = context->weak_from_this();
110 uiContent_->Initialize(this, contentUrl, storage);
111 uiContent_->Foreground();
112 uiContent_->SetFrameLayoutFinishCallback(std::move(frameLayoutFinishCb_));
113 uiContent_->AddFocusActiveChangeCallback(notifyWatchFocusActiveChangeCallback_);
114 wptr<Window> weakWindow(this);
115 AddRootScene(DEFAULT_DISPLAY_ID, weakWindow);
116 RegisterInputEventListener();
117 }
118
SetDisplayOrientation(int32_t orientation)119 void RootScene::SetDisplayOrientation(int32_t orientation)
120 {
121 orientation_ = orientation;
122 }
123
UpdateViewportConfig(const Rect & rect,WindowSizeChangeReason reason)124 void RootScene::UpdateViewportConfig(const Rect& rect, WindowSizeChangeReason reason)
125 {
126 if (updateRootSceneRectCallback_ != nullptr) {
127 updateRootSceneRectCallback_(rect);
128 }
129
130 if (uiContent_ == nullptr) {
131 TLOGD(WmsLogTag::DEFAULT, "uiContent_ is nullptr!");
132 return;
133 }
134 Ace::ViewportConfig config;
135 config.SetSize(rect.width_, rect.height_);
136 config.SetPosition(rect.posX_, rect.posY_);
137 config.SetDensity(density_);
138 config.SetOrientation(orientation_);
139 config.SetDisplayId(GetDisplayId());
140 uiContent_->UpdateViewportConfig(config, reason);
141 }
142
UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration> & configuration)143 void RootScene::UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
144 {
145 TLOGD(WmsLogTag::WMS_ATTRIBUTE, "root map size: %{public}u", static_cast<uint32_t>(rootSceneMap_.size()));
146 for (const auto& sceneMap : rootSceneMap_) {
147 if (sceneMap.second == nullptr) {
148 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "root window is null, display=%{public}" PRIu64, sceneMap.first);
149 continue;
150 }
151 auto window = sceneMap.second.promote();
152 if (window == nullptr) {
153 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "root win is null, display=%{public}" PRIu64, sceneMap.first);
154 continue;
155 }
156 auto uiContent = window->GetUIContent();
157 if (uiContent == nullptr) {
158 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "uiContent is null, root win=%{public}u, display=%{public}" PRIu64,
159 window->GetWindowId(), window->GetDisplayId());
160 continue;
161 }
162 TLOGD(WmsLogTag::WMS_ATTRIBUTE, "root win=%{public}u, display=%{public}" PRIu64,
163 window->GetWindowId(), window->GetDisplayId());
164 uiContent->UpdateConfiguration(configuration);
165 if (configuration == nullptr) {
166 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "config is null, root win=%{public}u, display=%{public}" PRIu64,
167 window->GetWindowId(), window->GetDisplayId());
168 continue;
169 }
170 std::string colorMode = configuration->GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE);
171 bool isDark = (colorMode == AppExecFwk::ConfigurationInner::COLOR_MODE_DARK);
172 bool ret = RSInterfaces::GetInstance().SetGlobalDarkColorMode(isDark);
173 if (ret == false) {
174 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "SetGlobalDarkColorMode fail, colorMode : %{public}s", colorMode.c_str());
175 }
176 }
177 }
178
UpdateConfigurationForSpecified(const std::shared_ptr<AppExecFwk::Configuration> & configuration,const std::shared_ptr<Global::Resource::ResourceManager> & resourceManager)179 void RootScene::UpdateConfigurationForSpecified(const std::shared_ptr<AppExecFwk::Configuration>& configuration,
180 const std::shared_ptr<Global::Resource::ResourceManager>& resourceManager)
181 {
182 if (uiContent_ == nullptr) {
183 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "uiContent null, root win=%{public}u, display=%{public}" PRIu64,
184 GetWindowId(), GetDisplayId());
185 return;
186 }
187 uiContent_->UpdateConfiguration(configuration, resourceManager);
188 if (configuration == nullptr) {
189 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "config is null, root win=%{public}u, display=%{public}" PRIu64,
190 GetWindowId(), GetDisplayId());
191 return;
192 }
193 TLOGD(WmsLogTag::WMS_ATTRIBUTE, "root win=%{public}u, display=%{public}" PRIu64,
194 GetWindowId(), GetDisplayId());
195 std::string colorMode = configuration->GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE);
196 bool isDark = (colorMode == AppExecFwk::ConfigurationInner::COLOR_MODE_DARK);
197 if (!RSInterfaces::GetInstance().SetGlobalDarkColorMode(isDark)) {
198 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "set dark color mode failed, winId: %{public}u, colorMode: %{public}s",
199 GetWindowId(), colorMode.c_str());
200 }
201 }
202
UpdateConfigurationForAll(const std::shared_ptr<AppExecFwk::Configuration> & configuration,const std::vector<std::shared_ptr<AbilityRuntime::Context>> & ignoreWindowContexts)203 void RootScene::UpdateConfigurationForAll(const std::shared_ptr<AppExecFwk::Configuration>& configuration,
204 const std::vector<std::shared_ptr<AbilityRuntime::Context>>& ignoreWindowContexts)
205 {
206 if (staticRootScene_) {
207 if (std::count(ignoreWindowContexts.begin(), ignoreWindowContexts.end(), staticRootScene_->GetContext()) == 0) {
208 TLOGD(WmsLogTag::WMS_ATTRIBUTE, "root win=%{public}u, display=%{public}" PRIu64,
209 staticRootScene_->GetWindowId(), staticRootScene_->GetDisplayId());
210 staticRootScene_->UpdateConfiguration(configuration);
211 if (configurationUpdatedCallback_) {
212 configurationUpdatedCallback_(configuration);
213 }
214 } else {
215 TLOGI(WmsLogTag::WMS_ATTRIBUTE, "skip root win=%{public}u, display=%{public}" PRIu64,
216 staticRootScene_->GetWindowId(), staticRootScene_->GetDisplayId());
217 }
218 } else {
219 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "root is null");
220 }
221 }
222
UpdateConfigurationSync(const std::shared_ptr<AppExecFwk::Configuration> & configuration)223 void RootScene::UpdateConfigurationSync(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
224 {
225 TLOGD(WmsLogTag::WMS_ATTRIBUTE, "root map size: %{public}u", static_cast<uint32_t>(rootSceneMap_.size()));
226 for (const auto& sceneMap : rootSceneMap_) {
227 if (sceneMap.second == nullptr) {
228 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "root window is null, display=%{public}" PRIu64, sceneMap.first);
229 continue;
230 }
231 auto window = sceneMap.second.promote();
232 if (window == nullptr) {
233 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "root win is null, display=%{public}" PRIu64, sceneMap.first);
234 continue;
235 }
236 auto uiContent = window->GetUIContent();
237 if (uiContent == nullptr) {
238 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "uiContent is null, root win=%{public}u, display=%{public}" PRIu64,
239 window->GetWindowId(), window->GetDisplayId());
240 continue;
241 }
242 TLOGD(WmsLogTag::WMS_ATTRIBUTE, "root win=%{public}u, display=%{public}" PRIu64,
243 window->GetWindowId(), window->GetDisplayId());
244 uiContent->UpdateConfigurationSyncForAll(configuration);
245 }
246 }
247
UpdateConfigurationSyncForAll(const std::shared_ptr<AppExecFwk::Configuration> & configuration)248 void RootScene::UpdateConfigurationSyncForAll(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
249 {
250 if (staticRootScene_ != nullptr) {
251 TLOGD(WmsLogTag::WMS_ATTRIBUTE, "root win=%{public}u, display=%{public}" PRIu64,
252 staticRootScene_->GetWindowId(), staticRootScene_->GetDisplayId());
253 staticRootScene_->UpdateConfigurationSync(configuration);
254 } else {
255 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "root is null");
256 }
257 }
258
RegisterInputEventListener()259 void RootScene::RegisterInputEventListener()
260 {
261 auto mainEventRunner = AppExecFwk::EventRunner::GetMainEventRunner();
262 if (mainEventRunner) {
263 WLOGFD("MainEventRunner is available");
264 eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(mainEventRunner);
265 } else {
266 WLOGFD("MainEventRunner is not available");
267 eventHandler_ = AppExecFwk::EventHandler::Current();
268 if (!eventHandler_) {
269 eventHandler_ =
270 std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::Create(INPUT_AND_VSYNC_THREAD));
271 }
272 }
273 if (!(DelayedSingleton<IntentionEventManager>::GetInstance()->EnableInputEventListener(
274 uiContent_.get(), eventHandler_, this))) {
275 WLOGFE("EnableInputEventListener fail");
276 }
277 InputTransferStation::GetInstance().MarkRegisterToMMI();
278 }
279
RequestVsync(const std::shared_ptr<VsyncCallback> & vsyncCallback)280 void RootScene::RequestVsync(const std::shared_ptr<VsyncCallback>& vsyncCallback)
281 {
282 vsyncStation_->RequestVsync(vsyncCallback);
283 }
284
GetVSyncPeriod()285 int64_t RootScene::GetVSyncPeriod()
286 {
287 return vsyncStation_->GetVSyncPeriod();
288 }
289
FlushFrameRate(uint32_t rate,int32_t animatorExpectedFrameRate,uint32_t rateType)290 void RootScene::FlushFrameRate(uint32_t rate, int32_t animatorExpectedFrameRate, uint32_t rateType)
291 {
292 vsyncStation_->FlushFrameRate(GetRSUIContext(), rate, animatorExpectedFrameRate, rateType);
293 }
294
IsLastFrameLayoutFinished()295 bool RootScene::IsLastFrameLayoutFinished()
296 {
297 int32_t requestTimes = vsyncStation_->GetRequestVsyncTimes();
298 TLOGD(WmsLogTag::WMS_LAYOUT, "vsync request times: %{public}d", requestTimes);
299 return requestTimes <= 0;
300 }
301
OnFlushUIParams()302 void RootScene::OnFlushUIParams()
303 {
304 vsyncStation_->DecreaseRequestVsyncTimes();
305 }
306
OnBundleUpdated(const std::string & bundleName)307 void RootScene::OnBundleUpdated(const std::string& bundleName)
308 {
309 WLOGFD("bundle %{public}s updated", bundleName.c_str());
310 if (uiContent_) {
311 uiContent_->UpdateResource();
312 }
313 }
314
SetOnConfigurationUpdatedCallback(const std::function<void (const std::shared_ptr<AppExecFwk::Configuration> &)> & callback)315 void RootScene::SetOnConfigurationUpdatedCallback(
316 const std::function<void(const std::shared_ptr<AppExecFwk::Configuration>&)>& callback)
317 {
318 configurationUpdatedCallback_ = callback;
319 }
320
SetFrameLayoutFinishCallback(std::function<void ()> && callback)321 void RootScene::SetFrameLayoutFinishCallback(std::function<void()>&& callback)
322 {
323 frameLayoutFinishCb_ = callback;
324 if (uiContent_) {
325 uiContent_->SetFrameLayoutFinishCallback(std::move(frameLayoutFinishCb_));
326 }
327 TLOGI(WmsLogTag::WMS_LAYOUT, "end");
328 }
329
SetUiDvsyncSwitch(bool dvsyncSwitch)330 void RootScene::SetUiDvsyncSwitch(bool dvsyncSwitch)
331 {
332 vsyncStation_->SetUiDvsyncSwitch(dvsyncSwitch);
333 }
334
SetTouchEvent(int32_t touchType)335 void RootScene::SetTouchEvent(int32_t touchType)
336 {
337 vsyncStation_->SetTouchEvent(touchType);
338 }
339
GetAvoidAreaByType(AvoidAreaType type,AvoidArea & avoidArea,const Rect & rect,int32_t apiVersion)340 WMError RootScene::GetAvoidAreaByType(AvoidAreaType type, AvoidArea& avoidArea, const Rect& rect, int32_t apiVersion)
341 {
342 if (getSessionAvoidAreaByTypeCallback_ == nullptr) {
343 TLOGE(WmsLogTag::WMS_IMMS, "getSessionAvoidAreaByTypeCallback is nullptr");
344 return WMError::WM_ERROR_NULLPTR;
345 }
346 if (apiVersion != API_VERSION_INVALID && apiVersion < API_VERSION_18) {
347 TLOGI(WmsLogTag::WMS_IMMS, "root scene UIExtension type %{public}u api %{public}d not supported",
348 type, apiVersion);
349 return WMError::WM_DO_NOTHING;
350 }
351 avoidArea = getSessionAvoidAreaByTypeCallback_(type);
352 TLOGI(WmsLogTag::WMS_IMMS, "root scene type %{public}u area %{public}s", type, avoidArea.ToString().c_str());
353 return WMError::WM_OK;
354 }
355
RegisterGetSessionAvoidAreaByTypeCallback(GetSessionAvoidAreaByTypeCallback && callback)356 void RootScene::RegisterGetSessionAvoidAreaByTypeCallback(GetSessionAvoidAreaByTypeCallback&& callback)
357 {
358 getSessionAvoidAreaByTypeCallback_ = std::move(callback);
359 }
360
GetStatusBarHeight() const361 uint32_t RootScene::GetStatusBarHeight() const
362 {
363 uint32_t height = 0;
364 if (getStatusBarHeightCallback_ == nullptr) {
365 TLOGE(WmsLogTag::WMS_IMMS, "getStatusBarHeightCallback_ is nullptr");
366 return height;
367 }
368 height = getStatusBarHeightCallback_();
369 TLOGI(WmsLogTag::WMS_IMMS, "root scene height %{public}u", height);
370 return height;
371 }
372
RegisterGetStatusBarHeightCallback(GetStatusBarHeightCallback && callback)373 void RootScene::RegisterGetStatusBarHeightCallback(GetStatusBarHeightCallback&& callback)
374 {
375 getStatusBarHeightCallback_ = std::move(callback);
376 }
377
RegisterUpdateRootSceneRectCallback(UpdateRootSceneRectCallback && callback)378 void RootScene::RegisterUpdateRootSceneRectCallback(UpdateRootSceneRectCallback&& callback)
379 {
380 updateRootSceneRectCallback_ = std::move(callback);
381 }
382
RegisterUpdateRootSceneAvoidAreaCallback(UpdateRootSceneAvoidAreaCallback && callback)383 void RootScene::RegisterUpdateRootSceneAvoidAreaCallback(UpdateRootSceneAvoidAreaCallback&& callback)
384 {
385 updateRootSceneAvoidAreaCallback_ = std::move(callback);
386 }
387
RegisterAvoidAreaChangeListener(const sptr<IAvoidAreaChangedListener> & listener)388 WMError RootScene::RegisterAvoidAreaChangeListener(const sptr<IAvoidAreaChangedListener>& listener)
389 {
390 if (listener == nullptr) {
391 TLOGE(WmsLogTag::WMS_IMMS, "listener is null");
392 return WMError::WM_ERROR_NULLPTR;
393 }
394 bool firstInserted = false;
395 {
396 std::lock_guard<std::mutex> lock(mutex_);
397 if (avoidAreaChangeListeners_.find(listener) == avoidAreaChangeListeners_.end()) {
398 TLOGI(WmsLogTag::WMS_IMMS, "register success");
399 avoidAreaChangeListeners_.insert(listener);
400 firstInserted = true;
401 }
402 }
403 if (firstInserted) {
404 updateRootSceneAvoidAreaCallback_();
405 }
406 return WMError::WM_OK;
407 }
408
UnregisterAvoidAreaChangeListener(const sptr<IAvoidAreaChangedListener> & listener)409 WMError RootScene::UnregisterAvoidAreaChangeListener(const sptr<IAvoidAreaChangedListener>& listener)
410 {
411 if (listener == nullptr) {
412 TLOGE(WmsLogTag::WMS_IMMS, "listener is null");
413 return WMError::WM_ERROR_NULLPTR;
414 }
415 TLOGI(WmsLogTag::WMS_IMMS, "unregister success");
416 std::lock_guard<std::mutex> lock(mutex_);
417 avoidAreaChangeListeners_.erase(listener);
418 return WMError::WM_OK;
419 }
420
NotifyAvoidAreaChangeForRoot(const sptr<AvoidArea> & avoidArea,AvoidAreaType type,const sptr<OccupiedAreaChangeInfo> & info)421 void RootScene::NotifyAvoidAreaChangeForRoot(const sptr<AvoidArea>& avoidArea, AvoidAreaType type,
422 const sptr<OccupiedAreaChangeInfo>& info)
423 {
424 AvoidArea area = {};
425 if (avoidArea != nullptr) {
426 area= *avoidArea;
427 TLOGI(WmsLogTag::WMS_IMMS, "type %{public}d area %{public}s.", type, avoidArea->ToString().c_str());
428 }
429 std::lock_guard<std::mutex> lock(mutex_);
430 for (auto& listener : avoidAreaChangeListeners_) {
431 if (listener != nullptr) {
432 listener->OnAvoidAreaChanged(area, type, info);
433 }
434 }
435 }
436
RegisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener> & listener)437 WMError RootScene::RegisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener>& listener)
438 {
439 if (listener == nullptr) {
440 TLOGE(WmsLogTag::WMS_KEYBOARD, "listener is null");
441 return WMError::WM_ERROR_NULLPTR;
442 }
443 std::lock_guard<std::mutex> lock(occupiedAreaMutex_);
444 if (occupiedAreaChangeListeners_.find(listener) == occupiedAreaChangeListeners_.end()) {
445 TLOGI(WmsLogTag::WMS_KEYBOARD, "register success");
446 occupiedAreaChangeListeners_.insert(listener);
447 }
448 return WMError::WM_OK;
449 }
450
UnregisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener> & listener)451 WMError RootScene::UnregisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener>& listener)
452 {
453 if (listener == nullptr) {
454 TLOGE(WmsLogTag::WMS_KEYBOARD, "listener is null");
455 return WMError::WM_ERROR_NULLPTR;
456 }
457 TLOGI(WmsLogTag::WMS_KEYBOARD, "unregister success");
458 std::lock_guard<std::mutex> lock(occupiedAreaMutex_);
459 occupiedAreaChangeListeners_.erase(listener);
460 return WMError::WM_OK;
461 }
462
NotifyOccupiedAreaChangeForRoot(const sptr<OccupiedAreaChangeInfo> & info)463 void RootScene::NotifyOccupiedAreaChangeForRoot(const sptr<OccupiedAreaChangeInfo>& info)
464 {
465 if (info == nullptr) {
466 TLOGI(WmsLogTag::WMS_KEYBOARD, "occupied area info is null");
467 return;
468 }
469 if (handler_ == nullptr) {
470 TLOGI(WmsLogTag::WMS_KEYBOARD, "handler_ is null, notify occupied area for root failed.");
471 return;
472 }
473 TLOGI(WmsLogTag::WMS_KEYBOARD, "occupiedRect: %{public}s, textField PositionY_: %{public}f, Height_: %{public}f",
474 info->rect_.ToString().c_str(), info->textFieldPositionY_, info->textFieldHeight_);
475 auto task = [weak = wptr(this), info]() {
476 auto window = weak.promote();
477 if (!window) {
478 TLOGE(WmsLogTag::WMS_KEYBOARD, "window is null");
479 return;
480 }
481 std::lock_guard<std::mutex> lock(window->occupiedAreaMutex_);
482 for (const auto& listener : window->occupiedAreaChangeListeners_) {
483 if (listener != nullptr) {
484 listener->OnSizeChange(info);
485 }
486 }
487 };
488 handler_->PostTask(task, __func__);
489 }
490
RegisterNotifyWatchFocusActiveChangeCallback(NotifyWatchFocusActiveChangeCallback && callback)491 void RootScene::RegisterNotifyWatchFocusActiveChangeCallback(NotifyWatchFocusActiveChangeCallback&& callback)
492 {
493 notifyWatchFocusActiveChangeCallback_ = std::move(callback);
494 }
495
GetRSNodeByStringID(const std::string & stringId)496 std::shared_ptr<Rosen::RSNode> RootScene::GetRSNodeByStringID(const std::string& stringId)
497 {
498 TLOGI(WmsLogTag::WMS_LAYOUT, "id: %{public}s", stringId.c_str());
499 if (uiContent_ == nullptr) {
500 TLOGE(WmsLogTag::WMS_LAYOUT, "uiContent is null, winId: %{public}d", GetWindowId());
501 return nullptr;
502 }
503 TLOGI(WmsLogTag::WMS_LAYOUT, "end");
504 return uiContent_->GetRSNodeByStringID(stringId);
505 }
506
SetTopWindowBoundaryByID(const std::string & stringId)507 void RootScene::SetTopWindowBoundaryByID(const std::string& stringId)
508 {
509 TLOGI(WmsLogTag::WMS_LAYOUT, "id: %{public}s", stringId.c_str());
510 if (uiContent_ == nullptr) {
511 TLOGE(WmsLogTag::WMS_LAYOUT, "uiContent is null, winId: %{public}d", GetWindowId());
512 return;
513 }
514 TLOGI(WmsLogTag::WMS_LAYOUT, "end");
515 uiContent_->SetTopWindowBoundaryByID(stringId);
516 }
517
GetExtensionConfig(AAFwk::WantParams & want) const518 void RootScene::GetExtensionConfig(AAFwk::WantParams& want) const
519 {
520 int32_t rootHostWindowType = static_cast<int32_t>(WindowType::WINDOW_TYPE_SCENE_BOARD);
521 want.SetParam(Extension::ROOT_HOST_WINDOW_TYPE_FIELD, AAFwk::Integer::Box(rootHostWindowType));
522 }
523
GetUIContentByDisplayId(DisplayId displayId)524 UIContentResult RootScene::GetUIContentByDisplayId(DisplayId displayId)
525 {
526 auto iter = rootSceneMap_.find(displayId);
527 if (iter == rootSceneMap_.end()) {
528 TLOGE(WmsLogTag::WMS_FOCUS, "Can not find rootScene, displayId: %{public}" PRIu64, displayId);
529 return std::make_pair(GetUIContent(), false);
530 }
531 if (iter->second != nullptr) {
532 auto window = iter->second.promote();
533 if (window != nullptr) {
534 return std::make_pair(window->GetUIContent(), true);
535 }
536 }
537 return std::make_pair(nullptr, true);
538 }
539
AddRootScene(DisplayId displayId,wptr<Window> window)540 void RootScene::AddRootScene(DisplayId displayId, wptr<Window> window)
541 {
542 auto iter = rootSceneMap_.find(displayId);
543 if (iter == rootSceneMap_.end()) {
544 rootSceneMap_.emplace(displayId, window);
545 TLOGI(WmsLogTag::WMS_FOCUS, "insert rootScene, displayId: %{public}" PRIu64, displayId);
546 } else {
547 iter->second = window;
548 TLOGI(WmsLogTag::WMS_FOCUS, "update rootScene, displayId: %{public}" PRIu64, displayId);
549 }
550 }
551
RemoveRootScene(DisplayId displayId)552 void RootScene::RemoveRootScene(DisplayId displayId)
553 {
554 TLOGI(WmsLogTag::WMS_FOCUS, "displayId: %{public}" PRIu64, displayId);
555 auto iter = rootSceneMap_.find(displayId);
556 if (iter == rootSceneMap_.end()) {
557 TLOGE(WmsLogTag::WMS_FOCUS, "Can not find rootScene, displayId: %{public}" PRIu64, displayId);
558 return;
559 }
560 rootSceneMap_.erase(displayId);
561 }
562
GetRSUIDirector() const563 std::shared_ptr<RSUIDirector> RootScene::GetRSUIDirector() const
564 {
565 RETURN_IF_RS_CLIENT_MULTI_INSTANCE_DISABLED(nullptr);
566 sptr<Display> display;
567 if (displayId_ == DISPLAY_ID_INVALID) {
568 display = DisplayManager::GetInstance().GetDefaultDisplay();
569 TLOGE(WmsLogTag::WMS_SCB, "displayId is invalid, use default display");
570 } else {
571 display = DisplayManager::GetInstance().GetDisplayById(displayId_);
572 }
573 if (!display) {
574 TLOGE(WmsLogTag::WMS_SCB, "display is null, displayId: %{public}" PRIu64, displayId_);
575 return nullptr;
576 }
577 auto screenId = display->GetScreenId();
578 auto rsUIDirector = ScreenSessionManagerClient::GetInstance().GetRSUIDirector(screenId);
579 TLOGD(WmsLogTag::WMS_SCB, "%{public}s, screenId: %{public}" PRIu64 ", windowId: %{public}d",
580 RSAdapterUtil::RSUIDirectorToStr(rsUIDirector).c_str(), screenId, GetWindowId());
581 return rsUIDirector;
582 }
583
GetRSUIContext() const584 std::shared_ptr<RSUIContext> RootScene::GetRSUIContext() const
585 {
586 RETURN_IF_RS_CLIENT_MULTI_INSTANCE_DISABLED(nullptr);
587 auto rsUIDirector = GetRSUIDirector();
588 auto rsUIContext = rsUIDirector ? rsUIDirector->GetRSUIContext() : nullptr;
589 TLOGD(WmsLogTag::WMS_SCB, "%{public}s, windowId: %{public}d",
590 RSAdapterUtil::RSUIContextToStr(rsUIContext).c_str(), GetWindowId());
591 return rsUIContext;
592 }
593 } // namespace Rosen
594 } // namespace OHOS
595