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 <iremote_stub.h>
22 #include <transaction/rs_interfaces.h>
23 #include <ui_content.h>
24 #include <ui/rs_node.h>
25 #include <viewport_config.h>
26
27 #include "ability_context.h"
28 #include "app_mgr_client.h"
29 #include "fold_screen_state_internel.h"
30 #include "input_transfer_station.h"
31 #include "singleton.h"
32 #include "singleton_container.h"
33
34 #include "anr_manager.h"
35 #include "intention_event_manager.h"
36 #include "window_manager_hilog.h"
37
38 namespace OHOS {
39 namespace Rosen {
40 namespace {
41 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "RootScene" };
42 const std::string INPUT_AND_VSYNC_THREAD = "InputAndVsyncThread";
43
44 class BundleStatusCallback : public IRemoteStub<AppExecFwk::IBundleStatusCallback> {
45 public:
BundleStatusCallback(RootScene * rootScene)46 explicit BundleStatusCallback(RootScene* rootScene) : rootScene_(rootScene) {}
47 virtual ~BundleStatusCallback() = default;
48
OnBundleStateChanged(const uint8_t installType,const int32_t resultCode,const std::string & resultMsg,const std::string & bundleName)49 void OnBundleStateChanged(const uint8_t installType,
50 const int32_t resultCode, const std::string& resultMsg, const std::string& bundleName) override {}
51
OnBundleAdded(const std::string & bundleName,const int userId)52 void OnBundleAdded(const std::string& bundleName, const int userId) override
53 {
54 rootScene_->OnBundleUpdated(bundleName);
55 }
56
OnBundleUpdated(const std::string & bundleName,const int userId)57 void OnBundleUpdated(const std::string& bundleName, const int userId) override
58 {
59 rootScene_->OnBundleUpdated(bundleName);
60 }
61
OnBundleRemoved(const std::string & bundleName,const int userId)62 void OnBundleRemoved(const std::string& bundleName, const int userId) override {}
63
64 private:
65 RootScene* rootScene_;
66 };
67 } // namespace
68
69 sptr<RootScene> RootScene::staticRootScene_;
70 std::function<void(const std::shared_ptr<AppExecFwk::Configuration>&)> RootScene::configurationUpdatedCallback_;
71
RootScene()72 RootScene::RootScene()
73 {
74 launcherService_ = new AppExecFwk::LauncherService();
75 if (!launcherService_->RegisterCallback(new BundleStatusCallback(this))) {
76 WLOGFE("Failed to register bundle status callback.");
77 }
78
79 NodeId nodeId = 0;
80 vsyncStation_ = std::make_shared<VsyncStation>(nodeId);
81 }
82
~RootScene()83 RootScene::~RootScene()
84 {
85 uiContent_ = nullptr;
86 }
87
LoadContent(const std::string & contentUrl,napi_env env,napi_value storage,AbilityRuntime::Context * context)88 void RootScene::LoadContent(const std::string& contentUrl, napi_env env, napi_value storage,
89 AbilityRuntime::Context* context)
90 {
91 if (context == nullptr) {
92 WLOGFE("context is nullptr!");
93 return;
94 }
95 uiContent_ = Ace::UIContent::Create(context, reinterpret_cast<NativeEngine*>(env));
96 if (uiContent_ == nullptr) {
97 WLOGFE("uiContent_ is nullptr!");
98 return;
99 }
100
101 context_ = context->weak_from_this();
102 uiContent_->Initialize(this, contentUrl, storage);
103 uiContent_->Foreground();
104 uiContent_->SetFrameLayoutFinishCallback(std::move(frameLayoutFinishCb_));
105 RegisterInputEventListener();
106 DelayedSingleton<ANRManager>::GetInstance()->Init();
107 DelayedSingleton<ANRManager>::GetInstance()->SetAnrObserver(([](int32_t pid) {
108 WLOGFD("Receive anr notice enter");
109 AppExecFwk::AppFaultDataBySA faultData;
110 faultData.faultType = AppExecFwk::FaultDataType::APP_FREEZE;
111 faultData.pid = pid;
112 faultData.errorObject.name = AppExecFwk::AppFreezeType::APP_INPUT_BLOCK;
113 faultData.errorObject.message = "User input does not respond normally, report by sceneBoard.";
114 faultData.errorObject.stack = "";
115 if (int32_t ret = DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance()->NotifyAppFaultBySA(faultData);
116 ret != 0) {
117 WLOGFE("NotifyAppFaultBySA failed, pid:%{public}d, errcode:%{public}d", pid, ret);
118 }
119 WLOGFD("Receive anr notice leave");
120 }));
121 DelayedSingleton<ANRManager>::GetInstance()->SetAppInfoGetter(
122 [](int32_t pid, std::string& bundleName, int32_t uid) {
123 int32_t ret = DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance()->GetBundleNameByPid(
124 pid, bundleName, uid);
125 if (ret != 0) {
126 WLOGFE("GetBundleNameByPid failed, pid:%{public}d, errcode:%{public}d", pid, ret);
127 }
128 });
129 }
130
SetDisplayOrientation(int32_t orientation)131 void RootScene::SetDisplayOrientation(int32_t orientation)
132 {
133 orientation_ = orientation;
134 }
135
UpdateViewportConfig(const Rect & rect,WindowSizeChangeReason reason)136 void RootScene::UpdateViewportConfig(const Rect& rect, WindowSizeChangeReason reason)
137 {
138 if (uiContent_ == nullptr) {
139 WLOGFE("uiContent_ is nullptr!");
140 return;
141 }
142 Ace::ViewportConfig config;
143 config.SetSize(rect.width_, rect.height_);
144 config.SetPosition(rect.posX_, rect.posY_);
145 config.SetDensity(density_);
146 config.SetOrientation(orientation_);
147 uiContent_->UpdateViewportConfig(config, reason);
148 }
149
UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration> & configuration)150 void RootScene::UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
151 {
152 if (uiContent_) {
153 WLOGFD("in");
154 uiContent_->UpdateConfiguration(configuration);
155 if (configuration == nullptr) {
156 return;
157 }
158 std::string colorMode = configuration->GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE);
159 bool isDark = (colorMode == AppExecFwk::ConfigurationInner::COLOR_MODE_DARK);
160 bool ret = RSInterfaces::GetInstance().SetGlobalDarkColorMode(isDark);
161 if (ret == false) {
162 WLOGFE("SetGlobalDarkColorMode fail with colorMode : %{public}s", colorMode.c_str());
163 }
164 }
165 }
166
UpdateConfigurationForAll(const std::shared_ptr<AppExecFwk::Configuration> & configuration)167 void RootScene::UpdateConfigurationForAll(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
168 {
169 WLOGFD("in");
170 if (staticRootScene_) {
171 staticRootScene_->UpdateConfiguration(configuration);
172 if (configurationUpdatedCallback_) {
173 configurationUpdatedCallback_(configuration);
174 }
175 }
176 }
177
UpdateConfigurationSync(const std::shared_ptr<AppExecFwk::Configuration> & configuration)178 void RootScene::UpdateConfigurationSync(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
179 {
180 if (uiContent_ == nullptr) {
181 TLOGE(WmsLogTag::WMS_IMMS, "uiContent is null, winId: %{public}d", GetWindowId());
182 return;
183 }
184 TLOGI(WmsLogTag::WMS_IMMS, "winId: %{public}d", GetWindowId());
185 uiContent_->UpdateConfigurationSyncForAll(configuration);
186 }
187
UpdateConfigurationSyncForAll(const std::shared_ptr<AppExecFwk::Configuration> & configuration)188 void RootScene::UpdateConfigurationSyncForAll(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
189 {
190 TLOGD(WmsLogTag::WMS_IMMS, "in");
191 if (staticRootScene_ != nullptr) {
192 staticRootScene_->UpdateConfigurationSync(configuration);
193 }
194 }
195
RegisterInputEventListener()196 void RootScene::RegisterInputEventListener()
197 {
198 auto mainEventRunner = AppExecFwk::EventRunner::GetMainEventRunner();
199 if (mainEventRunner) {
200 WLOGFD("MainEventRunner is available");
201 eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(mainEventRunner);
202 } else {
203 WLOGFD("MainEventRunner is not available");
204 eventHandler_ = AppExecFwk::EventHandler::Current();
205 if (!eventHandler_) {
206 eventHandler_ =
207 std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::Create(INPUT_AND_VSYNC_THREAD));
208 }
209 }
210 if (!(DelayedSingleton<IntentionEventManager>::GetInstance()->EnableInputEventListener(
211 uiContent_.get(), eventHandler_))) {
212 WLOGFE("EnableInputEventListener fail");
213 }
214 InputTransferStation::GetInstance().MarkRegisterToMMI();
215 }
216
RequestVsync(const std::shared_ptr<VsyncCallback> & vsyncCallback)217 void RootScene::RequestVsync(const std::shared_ptr<VsyncCallback>& vsyncCallback)
218 {
219 vsyncStation_->RequestVsync(vsyncCallback);
220 }
221
GetVSyncPeriod()222 int64_t RootScene::GetVSyncPeriod()
223 {
224 return vsyncStation_->GetVSyncPeriod();
225 }
226
FlushFrameRate(uint32_t rate,int32_t animatorExpectedFrameRate,uint32_t rateType)227 void RootScene::FlushFrameRate(uint32_t rate, int32_t animatorExpectedFrameRate, uint32_t rateType)
228 {
229 vsyncStation_->FlushFrameRate(rate, animatorExpectedFrameRate, rateType);
230 }
231
IsLastFrameLayoutFinished()232 bool RootScene::IsLastFrameLayoutFinished()
233 {
234 int32_t requestTimes = vsyncStation_->GetRequestVsyncTimes();
235 TLOGI(WmsLogTag::WMS_LAYOUT, "vsync request times: %{public}d", requestTimes);
236 return requestTimes <= 0;
237 }
238
OnFlushUIParams()239 void RootScene::OnFlushUIParams()
240 {
241 vsyncStation_->DecreaseRequestVsyncTimes();
242 }
243
OnBundleUpdated(const std::string & bundleName)244 void RootScene::OnBundleUpdated(const std::string& bundleName)
245 {
246 WLOGFD("bundle %{public}s updated", bundleName.c_str());
247 if (uiContent_) {
248 uiContent_->UpdateResource();
249 }
250 }
251
SetOnConfigurationUpdatedCallback(const std::function<void (const std::shared_ptr<AppExecFwk::Configuration> &)> & callback)252 void RootScene::SetOnConfigurationUpdatedCallback(
253 const std::function<void(const std::shared_ptr<AppExecFwk::Configuration>&)>& callback)
254 {
255 configurationUpdatedCallback_ = callback;
256 }
257
SetFrameLayoutFinishCallback(std::function<void ()> && callback)258 void RootScene::SetFrameLayoutFinishCallback(std::function<void()>&& callback)
259 {
260 frameLayoutFinishCb_ = callback;
261 if (uiContent_) {
262 uiContent_->SetFrameLayoutFinishCallback(std::move(frameLayoutFinishCb_));
263 }
264 TLOGI(WmsLogTag::WMS_LAYOUT, "end");
265 }
266
SetUiDvsyncSwitch(bool dvsyncSwitch)267 void RootScene::SetUiDvsyncSwitch(bool dvsyncSwitch)
268 {
269 vsyncStation_->SetUiDvsyncSwitch(dvsyncSwitch);
270 }
271
GetSessionRectByType(AvoidAreaType type,WSRect & rect)272 WMError RootScene::GetSessionRectByType(AvoidAreaType type, WSRect& rect)
273 {
274 if (getSessionRectCallback_ == nullptr) {
275 TLOGE(WmsLogTag::WMS_IMMS, "getSessionRectCallback is nullptr");
276 return WMError::WM_ERROR_NULLPTR;
277 }
278 rect = getSessionRectCallback_(type);
279 return WMError::WM_OK;
280 }
281
GetRSNodeByStringID(const std::string & stringId)282 std::shared_ptr<Rosen::RSNode> RootScene::GetRSNodeByStringID(const std::string& stringId)
283 {
284 TLOGI(WmsLogTag::WMS_LAYOUT, "id: %{public}s", stringId.c_str());
285 if (uiContent_ == nullptr) {
286 TLOGE(WmsLogTag::WMS_LAYOUT, "uiContent is null, winId: %{public}d", GetWindowId());
287 return nullptr;
288 }
289 TLOGI(WmsLogTag::WMS_LAYOUT, "end");
290 return uiContent_->GetRSNodeByStringID(stringId);
291 }
292
SetTopWindowBoundaryByID(const std::string & stringId)293 void RootScene::SetTopWindowBoundaryByID(const std::string& stringId)
294 {
295 TLOGI(WmsLogTag::WMS_LAYOUT, "id: %{public}s", stringId.c_str());
296 if (uiContent_ == nullptr) {
297 TLOGE(WmsLogTag::WMS_LAYOUT, "uiContent is null, winId: %{public}d", GetWindowId());
298 return;
299 }
300 TLOGI(WmsLogTag::WMS_LAYOUT, "end");
301 uiContent_->SetTopWindowBoundaryByID(stringId);
302 }
303 } // namespace Rosen
304 } // namespace OHOS
305