1 /*
2 * Copyright (c) 2021-2022 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 "window_scene.h"
17 #include <new>
18 #include <configuration.h>
19
20 #include "static_call.h"
21 #include "window_impl.h"
22 #include "window_manager_hilog.h"
23
24 namespace OHOS {
25 namespace Rosen {
26 namespace {
27 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowScene"};
28 }
29
30 const std::string WindowScene::MAIN_WINDOW_ID = "main window";
31
~WindowScene()32 WindowScene::~WindowScene()
33 {
34 WLOGI("~WindowScene");
35 if (mainWindow_ != nullptr) {
36 mainWindow_->Destroy();
37 mainWindow_ = nullptr;
38 }
39 }
40
Init(DisplayId displayId,const std::shared_ptr<AbilityRuntime::Context> & context,sptr<IWindowLifeCycle> & listener,sptr<WindowOption> option)41 WMError WindowScene::Init(DisplayId displayId, const std::shared_ptr<AbilityRuntime::Context>& context,
42 sptr<IWindowLifeCycle>& listener, sptr<WindowOption> option)
43 {
44 WLOGFI("WindowScene init with normal option!");
45 displayId_ = displayId;
46 if (option == nullptr) {
47 option = new(std::nothrow) WindowOption();
48 if (option == nullptr) {
49 WLOGFW("alloc WindowOption failed");
50 return WMError::WM_ERROR_NULLPTR;
51 }
52 }
53 option->SetDisplayId(displayId);
54 option->SetWindowTag(WindowTag::MAIN_WINDOW);
55 if (context != nullptr) {
56 option->SetBundleName(context->GetBundleName());
57 }
58 mainWindow_ = SingletonContainer::Get<StaticCall>().CreateWindow(
59 GenerateMainWindowName(context), option, context);
60 if (mainWindow_ == nullptr) {
61 return WMError::WM_ERROR_NULLPTR;
62 }
63 mainWindow_->RegisterLifeCycleListener(listener);
64
65 return WMError::WM_OK;
66 }
67
Init(DisplayId displayId,const std::shared_ptr<AbilityRuntime::Context> & context,sptr<IWindowLifeCycle> & listener,sptr<WindowOption> option,const sptr<IRemoteObject> & iSession)68 WMError WindowScene::Init(DisplayId displayId, const std::shared_ptr<AbilityRuntime::Context>& context,
69 sptr<IWindowLifeCycle>& listener, sptr<WindowOption> option, const sptr<IRemoteObject>& iSession)
70 {
71 WLOGFI("WindowScene with window session!");
72 displayId_ = displayId;
73 if (option == nullptr || iSession == nullptr) {
74 WLOGFE("Cannot init scene with option or iSession null!");
75 return WMError::WM_ERROR_NULLPTR;
76 }
77 option->SetDisplayId(displayId);
78 option->SetWindowName(GenerateMainWindowName(context));
79 if (context != nullptr) {
80 option->SetBundleName(context->GetBundleName());
81 }
82 mainWindow_ = SingletonContainer::Get<StaticCall>().CreateWindow(option, context, iSession);
83 if (mainWindow_ == nullptr) {
84 WLOGFE("mainWindow is null after creat Window!");
85 return WMError::WM_ERROR_NULLPTR;
86 }
87 mainWindow_->RegisterLifeCycleListener(listener);
88
89 return WMError::WM_OK;
90 }
91
GenerateMainWindowName(const std::shared_ptr<AbilityRuntime::Context> & context) const92 std::string WindowScene::GenerateMainWindowName(const std::shared_ptr<AbilityRuntime::Context>& context) const
93 {
94 if (context == nullptr) {
95 return MAIN_WINDOW_ID + std::to_string(count++);
96 } else {
97 std::string windowName = context->GetBundleName() + std::to_string(count++);
98 std::size_t pos = windowName.find_last_of('.');
99 return (pos == std::string::npos) ? windowName : windowName.substr(pos + 1); // skip '.'
100 }
101 }
102
CreateWindow(const std::string & windowName,sptr<WindowOption> & option) const103 sptr<Window> WindowScene::CreateWindow(const std::string& windowName, sptr<WindowOption>& option) const
104 {
105 if (windowName.empty() || mainWindow_ == nullptr || option == nullptr) {
106 WLOGFE("WindowScene Name: %{public}s", windowName.c_str());
107 return nullptr;
108 }
109 option->SetParentId(mainWindow_->GetWindowId());
110 option->SetWindowTag(WindowTag::SUB_WINDOW);
111 WLOGFD("WindowScene Name: %{public}s, parentId: %{public}u", windowName.c_str(), mainWindow_->GetWindowId());
112 return SingletonContainer::Get<StaticCall>().CreateWindow(windowName, option, mainWindow_->GetContext());
113 }
114
GetMainWindow() const115 const sptr<Window>& WindowScene::GetMainWindow() const
116 {
117 return mainWindow_;
118 }
119
GetSubWindow()120 std::vector<sptr<Window>> WindowScene::GetSubWindow()
121 {
122 if (mainWindow_ == nullptr) {
123 WLOGFE("Get sub window failed, because main window is null");
124 return std::vector<sptr<Window>>();
125 }
126 uint32_t parentId = mainWindow_->GetWindowId();
127 return SingletonContainer::Get<StaticCall>().GetSubWindow(parentId);
128 }
129
GoForeground(uint32_t reason)130 WMError WindowScene::GoForeground(uint32_t reason)
131 {
132 WLOGFI("reason:%{public}u", reason);
133 if (mainWindow_ == nullptr) {
134 WLOGFE("Go foreground failed, because main window is null");
135 return WMError::WM_ERROR_NULLPTR;
136 }
137 return mainWindow_->Show(reason);
138 }
139
GoBackground(uint32_t reason)140 WMError WindowScene::GoBackground(uint32_t reason)
141 {
142 WLOGFI("reason:%{public}u", reason);
143 if (mainWindow_ == nullptr) {
144 WLOGFE("Go background failed, because main window is null");
145 return WMError::WM_ERROR_NULLPTR;
146 }
147 return mainWindow_->Hide(reason);
148 }
149
GoDestroy()150 WMError WindowScene::GoDestroy()
151 {
152 if (mainWindow_ == nullptr) {
153 return WMError::WM_ERROR_NULLPTR;
154 }
155
156 WMError ret = mainWindow_->Destroy();
157 if (ret != WMError::WM_OK) {
158 WLOGFE("WindowScene go destroy failed name: %{public}s", mainWindow_->GetWindowName().c_str());
159 return ret;
160 }
161 mainWindow_ = nullptr;
162 return WMError::WM_OK;
163 }
164
OnNewWant(const AAFwk::Want & want)165 WMError WindowScene::OnNewWant(const AAFwk::Want& want)
166 {
167 if (mainWindow_ == nullptr) {
168 WLOGFE("On new want failed, because main window is null");
169 return WMError::WM_ERROR_NULLPTR;
170 }
171 mainWindow_->OnNewWant(want);
172 return WMError::WM_OK;
173 }
174
SetSystemBarProperty(WindowType type,const SystemBarProperty & property) const175 WMError WindowScene::SetSystemBarProperty(WindowType type, const SystemBarProperty& property) const
176 {
177 if (mainWindow_ == nullptr) {
178 WLOGFE("Set systembar property failed, because main window is null");
179 return WMError::WM_ERROR_NULLPTR;
180 }
181 return mainWindow_->SetSystemBarProperty(type, property);
182 }
183
RequestFocus() const184 WMError WindowScene::RequestFocus() const
185 {
186 if (mainWindow_ == nullptr) {
187 WLOGFE("Request focus failed, because main window is null");
188 return WMError::WM_ERROR_NULLPTR;
189 }
190 return mainWindow_->RequestFocus();
191 }
192
UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration> & configuration)193 void WindowScene::UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
194 {
195 if (mainWindow_ == nullptr) {
196 WLOGFE("Update configuration failed, because main window is null");
197 return;
198 }
199 WLOGI("notify mainWindow winId:%{public}u", mainWindow_->GetWindowId());
200 mainWindow_->UpdateConfiguration(configuration);
201 }
202
GetContentInfo() const203 std::string WindowScene::GetContentInfo() const
204 {
205 if (mainWindow_ == nullptr) {
206 WLOGFE("Get content info failed, because main window is null");
207 return "";
208 }
209 return mainWindow_->GetContentInfo();
210 }
211
NotifyMemoryLevel(int32_t level)212 WMError WindowScene::NotifyMemoryLevel(int32_t level)
213 {
214 WLOGI("Notify memory level: %{public}d", level);
215 if (mainWindow_ == nullptr) {
216 WLOGFE("Notify memory level failed, because main window is null");
217 return WMError::WM_ERROR_NULLPTR;
218 }
219 return mainWindow_->NotifyMemoryLevel(level);
220 }
221 } // namespace Rosen
222 } // namespace OHOS
223