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