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