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 "extension/extension_business_info.h"
31 #include "fold_screen_state_internel.h"
32 #include "input_transfer_station.h"
33 #include "singleton.h"
34 #include "singleton_container.h"
35
36 #include "intention_event_manager.h"
37 #include "window_manager_hilog.h"
38
39 namespace OHOS {
40 namespace Rosen {
41 namespace {
42 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "RootScene" };
43 const std::string INPUT_AND_VSYNC_THREAD = "InputAndVsyncThread";
44 constexpr int32_t API_VERSION_18 = 18;
45
46 class BundleStatusCallback : public IRemoteStub<AppExecFwk::IBundleStatusCallback> {
47 public:
BundleStatusCallback(RootScene * rootScene)48 explicit BundleStatusCallback(RootScene* rootScene) : rootScene_(rootScene) {}
49 virtual ~BundleStatusCallback() = default;
50
OnBundleStateChanged(const uint8_t installType,const int32_t resultCode,const std::string & resultMsg,const std::string & bundleName)51 void OnBundleStateChanged(const uint8_t installType,
52 const int32_t resultCode, const std::string& resultMsg, const std::string& bundleName) override {}
53
OnBundleAdded(const std::string & bundleName,const int userId)54 void OnBundleAdded(const std::string& bundleName, const int userId) override
55 {
56 rootScene_->OnBundleUpdated(bundleName);
57 }
58
OnBundleUpdated(const std::string & bundleName,const int userId)59 void OnBundleUpdated(const std::string& bundleName, const int userId) override
60 {
61 rootScene_->OnBundleUpdated(bundleName);
62 }
63
OnBundleRemoved(const std::string & bundleName,const int userId)64 void OnBundleRemoved(const std::string& bundleName, const int userId) override {}
65
66 private:
67 RootScene* rootScene_;
68 };
69 } // namespace
70
71 sptr<RootScene> RootScene::staticRootScene_;
72 std::function<void(const std::shared_ptr<AppExecFwk::Configuration>&)> RootScene::configurationUpdatedCallback_;
73
RootScene()74 RootScene::RootScene()
75 {
76 launcherService_ = new AppExecFwk::LauncherService();
77 if (!launcherService_->RegisterCallback(new BundleStatusCallback(this))) {
78 WLOGFE("Failed to register bundle status callback.");
79 }
80
81 NodeId nodeId = 0;
82 vsyncStation_ = std::make_shared<VsyncStation>(nodeId);
83 handler_ = std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::GetMainEventRunner());
84 }
85
~RootScene()86 RootScene::~RootScene()
87 {
88 TLOGI(WmsLogTag::WMS_MAIN, "destroyed");
89 uiContent_ = nullptr;
90 }
91
LoadContent(const std::string & contentUrl,napi_env env,napi_value storage,AbilityRuntime::Context * context)92 void RootScene::LoadContent(const std::string& contentUrl, napi_env env, napi_value storage,
93 AbilityRuntime::Context* context)
94 {
95 if (context == nullptr) {
96 WLOGFE("context is nullptr!");
97 return;
98 }
99 uiContent_ = Ace::UIContent::Create(context, reinterpret_cast<NativeEngine*>(env));
100 if (uiContent_ == nullptr) {
101 WLOGFE("uiContent_ is nullptr!");
102 return;
103 }
104
105 context_ = context->weak_from_this();
106 uiContent_->Initialize(this, contentUrl, storage);
107 uiContent_->Foreground();
108 uiContent_->SetFrameLayoutFinishCallback(std::move(frameLayoutFinishCb_));
109 uiContent_->AddFocusActiveChangeCallback(notifyWatchFocusActiveChangeCallback_);
110 RegisterInputEventListener();
111 }
112
SetDisplayOrientation(int32_t orientation)113 void RootScene::SetDisplayOrientation(int32_t orientation)
114 {
115 orientation_ = orientation;
116 }
117
UpdateViewportConfig(const Rect & rect,WindowSizeChangeReason reason)118 void RootScene::UpdateViewportConfig(const Rect& rect, WindowSizeChangeReason reason)
119 {
120 if (updateRootSceneRectCallback_ != nullptr) {
121 updateRootSceneRectCallback_(rect);
122 }
123
124 if (uiContent_ == nullptr) {
125 WLOGFE("uiContent_ is nullptr!");
126 return;
127 }
128 Ace::ViewportConfig config;
129 config.SetSize(rect.width_, rect.height_);
130 config.SetPosition(rect.posX_, rect.posY_);
131 config.SetDensity(density_);
132 config.SetOrientation(orientation_);
133 config.SetDisplayId(GetDisplayId());
134 uiContent_->UpdateViewportConfig(config, reason);
135 }
136
UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration> & configuration)137 void RootScene::UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
138 {
139 if (uiContent_) {
140 WLOGFD("in");
141 uiContent_->UpdateConfiguration(configuration);
142 if (configuration == nullptr) {
143 return;
144 }
145 std::string colorMode = configuration->GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE);
146 bool isDark = (colorMode == AppExecFwk::ConfigurationInner::COLOR_MODE_DARK);
147 bool ret = RSInterfaces::GetInstance().SetGlobalDarkColorMode(isDark);
148 if (ret == false) {
149 WLOGFE("SetGlobalDarkColorMode fail with colorMode : %{public}s", colorMode.c_str());
150 }
151 }
152 }
153
UpdateConfigurationForSpecified(const std::shared_ptr<AppExecFwk::Configuration> & configuration,const std::shared_ptr<Global::Resource::ResourceManager> & resourceManager)154 void RootScene::UpdateConfigurationForSpecified(const std::shared_ptr<AppExecFwk::Configuration>& configuration,
155 const std::shared_ptr<Global::Resource::ResourceManager>& resourceManager)
156 {
157 if (uiContent_ == nullptr) {
158 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "uiContent is null, winId: %{public}u", GetWindowId());
159 return;
160 }
161 uiContent_->UpdateConfiguration(configuration, resourceManager);
162 if (configuration == nullptr) {
163 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "config is null, winId: %{public}u", GetWindowId());
164 return;
165 }
166 std::string colorMode = configuration->GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE);
167 bool isDark = (colorMode == AppExecFwk::ConfigurationInner::COLOR_MODE_DARK);
168 if (!RSInterfaces::GetInstance().SetGlobalDarkColorMode(isDark)) {
169 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "set dark color mode failed, winId: %{public}u, colorMode: %{public}s",
170 GetWindowId(), colorMode.c_str());
171 }
172 }
173
UpdateConfigurationForAll(const std::shared_ptr<AppExecFwk::Configuration> & configuration,const std::vector<std::shared_ptr<AbilityRuntime::Context>> & ignoreWindowContexts)174 void RootScene::UpdateConfigurationForAll(const std::shared_ptr<AppExecFwk::Configuration>& configuration,
175 const std::vector<std::shared_ptr<AbilityRuntime::Context>>& ignoreWindowContexts)
176 {
177 TLOGI(WmsLogTag::WMS_ATTRIBUTE, "in");
178 if (staticRootScene_) {
179 auto context = staticRootScene_->GetContext();
180 if (context == nullptr) {
181 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "context is null, winId: %{public}u", staticRootScene_->GetWindowId());
182 return;
183 }
184 if (std::count(ignoreWindowContexts.begin(), ignoreWindowContexts.end(), context) == 0) {
185 staticRootScene_->UpdateConfiguration(configuration);
186 if (configurationUpdatedCallback_) {
187 configurationUpdatedCallback_(configuration);
188 }
189 }
190 }
191 }
192
UpdateConfigurationSync(const std::shared_ptr<AppExecFwk::Configuration> & configuration)193 void RootScene::UpdateConfigurationSync(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
194 {
195 if (uiContent_ == nullptr) {
196 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "uiContent is null, winId: %{public}d", GetWindowId());
197 return;
198 }
199 TLOGI(WmsLogTag::WMS_ATTRIBUTE, "winId: %{public}d", GetWindowId());
200 uiContent_->UpdateConfigurationSyncForAll(configuration);
201 }
202
UpdateConfigurationSyncForAll(const std::shared_ptr<AppExecFwk::Configuration> & configuration)203 void RootScene::UpdateConfigurationSyncForAll(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
204 {
205 TLOGD(WmsLogTag::WMS_ATTRIBUTE, "in");
206 if (staticRootScene_ != nullptr) {
207 staticRootScene_->UpdateConfigurationSync(configuration);
208 }
209 }
210
RegisterInputEventListener()211 void RootScene::RegisterInputEventListener()
212 {
213 auto mainEventRunner = AppExecFwk::EventRunner::GetMainEventRunner();
214 if (mainEventRunner) {
215 WLOGFD("MainEventRunner is available");
216 eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(mainEventRunner);
217 } else {
218 WLOGFD("MainEventRunner is not available");
219 eventHandler_ = AppExecFwk::EventHandler::Current();
220 if (!eventHandler_) {
221 eventHandler_ =
222 std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::Create(INPUT_AND_VSYNC_THREAD));
223 }
224 }
225 if (!(DelayedSingleton<IntentionEventManager>::GetInstance()->EnableInputEventListener(
226 uiContent_.get(), eventHandler_))) {
227 WLOGFE("EnableInputEventListener fail");
228 }
229 InputTransferStation::GetInstance().MarkRegisterToMMI();
230 }
231
RequestVsync(const std::shared_ptr<VsyncCallback> & vsyncCallback)232 void RootScene::RequestVsync(const std::shared_ptr<VsyncCallback>& vsyncCallback)
233 {
234 vsyncStation_->RequestVsync(vsyncCallback);
235 }
236
GetVSyncPeriod()237 int64_t RootScene::GetVSyncPeriod()
238 {
239 return vsyncStation_->GetVSyncPeriod();
240 }
241
FlushFrameRate(uint32_t rate,int32_t animatorExpectedFrameRate,uint32_t rateType)242 void RootScene::FlushFrameRate(uint32_t rate, int32_t animatorExpectedFrameRate, uint32_t rateType)
243 {
244 vsyncStation_->FlushFrameRate(rate, animatorExpectedFrameRate, rateType);
245 }
246
IsLastFrameLayoutFinished()247 bool RootScene::IsLastFrameLayoutFinished()
248 {
249 int32_t requestTimes = vsyncStation_->GetRequestVsyncTimes();
250 TLOGI(WmsLogTag::WMS_LAYOUT, "vsync request times: %{public}d", requestTimes);
251 return requestTimes <= 0;
252 }
253
OnFlushUIParams()254 void RootScene::OnFlushUIParams()
255 {
256 vsyncStation_->DecreaseRequestVsyncTimes();
257 }
258
OnBundleUpdated(const std::string & bundleName)259 void RootScene::OnBundleUpdated(const std::string& bundleName)
260 {
261 WLOGFD("bundle %{public}s updated", bundleName.c_str());
262 if (uiContent_) {
263 uiContent_->UpdateResource();
264 }
265 }
266
SetOnConfigurationUpdatedCallback(const std::function<void (const std::shared_ptr<AppExecFwk::Configuration> &)> & callback)267 void RootScene::SetOnConfigurationUpdatedCallback(
268 const std::function<void(const std::shared_ptr<AppExecFwk::Configuration>&)>& callback)
269 {
270 configurationUpdatedCallback_ = callback;
271 }
272
SetFrameLayoutFinishCallback(std::function<void ()> && callback)273 void RootScene::SetFrameLayoutFinishCallback(std::function<void()>&& callback)
274 {
275 frameLayoutFinishCb_ = callback;
276 if (uiContent_) {
277 uiContent_->SetFrameLayoutFinishCallback(std::move(frameLayoutFinishCb_));
278 }
279 TLOGI(WmsLogTag::WMS_LAYOUT, "end");
280 }
281
SetUiDvsyncSwitch(bool dvsyncSwitch)282 void RootScene::SetUiDvsyncSwitch(bool dvsyncSwitch)
283 {
284 vsyncStation_->SetUiDvsyncSwitch(dvsyncSwitch);
285 }
286
GetAvoidAreaByType(AvoidAreaType type,AvoidArea & avoidArea,const Rect & rect,int32_t apiVersion)287 WMError RootScene::GetAvoidAreaByType(AvoidAreaType type, AvoidArea& avoidArea, const Rect& rect, int32_t apiVersion)
288 {
289 if (getSessionAvoidAreaByTypeCallback_ == nullptr) {
290 TLOGE(WmsLogTag::WMS_IMMS, "getSessionAvoidAreaByTypeCallback is nullptr");
291 return WMError::WM_ERROR_NULLPTR;
292 }
293 if (apiVersion != API_VERSION_INVALID && apiVersion < API_VERSION_18) {
294 TLOGI(WmsLogTag::WMS_IMMS, "root scene UIExtension type %{public}u api %{public}d not supported",
295 type, apiVersion);
296 return WMError::WM_DO_NOTHING;
297 }
298 avoidArea = getSessionAvoidAreaByTypeCallback_(type);
299 TLOGI(WmsLogTag::WMS_IMMS, "root scene type %{public}u area %{public}s", type, avoidArea.ToString().c_str());
300 return WMError::WM_OK;
301 }
302
RegisterGetSessionAvoidAreaByTypeCallback(GetSessionAvoidAreaByTypeCallback && callback)303 void RootScene::RegisterGetSessionAvoidAreaByTypeCallback(GetSessionAvoidAreaByTypeCallback&& callback)
304 {
305 getSessionAvoidAreaByTypeCallback_ = std::move(callback);
306 }
307
RegisterUpdateRootSceneRectCallback(UpdateRootSceneRectCallback && callback)308 void RootScene::RegisterUpdateRootSceneRectCallback(UpdateRootSceneRectCallback&& callback)
309 {
310 updateRootSceneRectCallback_ = std::move(callback);
311 }
312
RegisterUpdateRootSceneAvoidAreaCallback(UpdateRootSceneAvoidAreaCallback && callback)313 void RootScene::RegisterUpdateRootSceneAvoidAreaCallback(UpdateRootSceneAvoidAreaCallback&& callback)
314 {
315 updateRootSceneAvoidAreaCallback_ = std::move(callback);
316 }
317
RegisterAvoidAreaChangeListener(const sptr<IAvoidAreaChangedListener> & listener)318 WMError RootScene::RegisterAvoidAreaChangeListener(const sptr<IAvoidAreaChangedListener>& listener)
319 {
320 if (listener == nullptr) {
321 TLOGE(WmsLogTag::WMS_IMMS, "listener is null");
322 return WMError::WM_ERROR_NULLPTR;
323 }
324 bool firstInserted = false;
325 {
326 std::lock_guard<std::mutex> lock(mutex_);
327 if (avoidAreaChangeListeners_.find(listener) == avoidAreaChangeListeners_.end()) {
328 TLOGI(WmsLogTag::WMS_IMMS, "register success");
329 avoidAreaChangeListeners_.insert(listener);
330 firstInserted = true;
331 }
332 }
333 if (firstInserted) {
334 updateRootSceneAvoidAreaCallback_();
335 }
336 return WMError::WM_OK;
337 }
338
UnregisterAvoidAreaChangeListener(const sptr<IAvoidAreaChangedListener> & listener)339 WMError RootScene::UnregisterAvoidAreaChangeListener(const sptr<IAvoidAreaChangedListener>& listener)
340 {
341 if (listener == nullptr) {
342 TLOGE(WmsLogTag::WMS_IMMS, "listener is null");
343 return WMError::WM_ERROR_NULLPTR;
344 }
345 TLOGI(WmsLogTag::WMS_IMMS, "unregister success");
346 std::lock_guard<std::mutex> lock(mutex_);
347 avoidAreaChangeListeners_.erase(listener);
348 return WMError::WM_OK;
349 }
350
NotifyAvoidAreaChangeForRoot(const sptr<AvoidArea> & avoidArea,AvoidAreaType type)351 void RootScene::NotifyAvoidAreaChangeForRoot(const sptr<AvoidArea>& avoidArea, AvoidAreaType type)
352 {
353 TLOGI(WmsLogTag::WMS_IMMS, "type %{public}d area %{public}s.", type, avoidArea->ToString().c_str());
354 std::lock_guard<std::mutex> lock(mutex_);
355 for (auto& listener : avoidAreaChangeListeners_) {
356 if (listener != nullptr) {
357 listener->OnAvoidAreaChanged(*avoidArea, type);
358 }
359 }
360 }
361
RegisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener> & listener)362 WMError RootScene::RegisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener>& listener)
363 {
364 if (listener == nullptr) {
365 TLOGE(WmsLogTag::WMS_KEYBOARD, "listener is null");
366 return WMError::WM_ERROR_NULLPTR;
367 }
368 std::lock_guard<std::mutex> lock(occupiedAreaMutex_);
369 if (occupiedAreaChangeListeners_.find(listener) == occupiedAreaChangeListeners_.end()) {
370 TLOGI(WmsLogTag::WMS_KEYBOARD, "register success");
371 occupiedAreaChangeListeners_.insert(listener);
372 }
373 return WMError::WM_OK;
374 }
375
UnregisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener> & listener)376 WMError RootScene::UnregisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener>& listener)
377 {
378 if (listener == nullptr) {
379 TLOGE(WmsLogTag::WMS_KEYBOARD, "listener is null");
380 return WMError::WM_ERROR_NULLPTR;
381 }
382 TLOGI(WmsLogTag::WMS_KEYBOARD, "unregister success");
383 std::lock_guard<std::mutex> lock(occupiedAreaMutex_);
384 occupiedAreaChangeListeners_.erase(listener);
385 return WMError::WM_OK;
386 }
387
NotifyOccupiedAreaChangeForRoot(const sptr<OccupiedAreaChangeInfo> & info)388 void RootScene::NotifyOccupiedAreaChangeForRoot(const sptr<OccupiedAreaChangeInfo>& info)
389 {
390 if (info == nullptr) {
391 TLOGI(WmsLogTag::WMS_KEYBOARD, "occupied area info is null");
392 return;
393 }
394 if (handler_ == nullptr) {
395 TLOGI(WmsLogTag::WMS_KEYBOARD, "handler_ is null, notify occupied area for root failed.");
396 return;
397 }
398 TLOGI(WmsLogTag::WMS_KEYBOARD, "occupiedRect: %{public}s, textField PositionY_: %{public}f, Height_: %{public}f",
399 info->rect_.ToString().c_str(), info->textFieldPositionY_, info->textFieldHeight_);
400 auto task = [weak = wptr(this), info]() {
401 auto window = weak.promote();
402 if (!window) {
403 TLOGE(WmsLogTag::WMS_KEYBOARD, "window is null");
404 return;
405 }
406 std::lock_guard<std::mutex> lock(window->occupiedAreaMutex_);
407 for (const auto& listener : window->occupiedAreaChangeListeners_) {
408 if (listener != nullptr) {
409 listener->OnSizeChange(info);
410 }
411 }
412 };
413 handler_->PostTask(task, __func__);
414 }
415
RegisterNotifyWatchFocusActiveChangeCallback(NotifyWatchFocusActiveChangeCallback && callback)416 void RootScene::RegisterNotifyWatchFocusActiveChangeCallback(NotifyWatchFocusActiveChangeCallback&& callback)
417 {
418 notifyWatchFocusActiveChangeCallback_ = std::move(callback);
419 }
420
GetRSNodeByStringID(const std::string & stringId)421 std::shared_ptr<Rosen::RSNode> RootScene::GetRSNodeByStringID(const std::string& stringId)
422 {
423 TLOGI(WmsLogTag::WMS_LAYOUT, "id: %{public}s", stringId.c_str());
424 if (uiContent_ == nullptr) {
425 TLOGE(WmsLogTag::WMS_LAYOUT, "uiContent is null, winId: %{public}d", GetWindowId());
426 return nullptr;
427 }
428 TLOGI(WmsLogTag::WMS_LAYOUT, "end");
429 return uiContent_->GetRSNodeByStringID(stringId);
430 }
431
SetTopWindowBoundaryByID(const std::string & stringId)432 void RootScene::SetTopWindowBoundaryByID(const std::string& stringId)
433 {
434 TLOGI(WmsLogTag::WMS_LAYOUT, "id: %{public}s", stringId.c_str());
435 if (uiContent_ == nullptr) {
436 TLOGE(WmsLogTag::WMS_LAYOUT, "uiContent is null, winId: %{public}d", GetWindowId());
437 return;
438 }
439 TLOGI(WmsLogTag::WMS_LAYOUT, "end");
440 uiContent_->SetTopWindowBoundaryByID(stringId);
441 }
442
GetExtensionConfig(AAFwk::WantParams & want) const443 void RootScene::GetExtensionConfig(AAFwk::WantParams& want) const
444 {
445 int32_t rootHostWindowType = static_cast<int32_t>(WindowType::WINDOW_TYPE_SCENE_BOARD);
446 want.SetParam(Extension::ROOT_HOST_WINDOW_TYPE_FIELD, AAFwk::Integer::Box(rootHostWindowType));
447 }
448 } // namespace Rosen
449 } // namespace OHOS
450