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 WLOGFI("~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 displayId_ = displayId;
45 if (option == nullptr) {
46 option = new(std::nothrow) WindowOption();
47 if (option == nullptr) {
48 WLOGFW("alloc WindowOption failed");
49 return WMError::WM_ERROR_NULLPTR;
50 }
51 }
52 option->SetDisplayId(displayId);
53 option->SetWindowTag(WindowTag::MAIN_WINDOW);
54
55 mainWindow_ = SingletonContainer::Get<StaticCall>().CreateWindow(
56 GenerateMainWindowName(context), option, context);
57 if (mainWindow_ == nullptr) {
58 return WMError::WM_ERROR_NULLPTR;
59 }
60 mainWindow_->RegisterLifeCycleListener(listener);
61
62 return WMError::WM_OK;
63 }
64
GenerateMainWindowName(const std::shared_ptr<AbilityRuntime::Context> & context) const65 std::string WindowScene::GenerateMainWindowName(const std::shared_ptr<AbilityRuntime::Context>& context) const
66 {
67 if (context == nullptr) {
68 return MAIN_WINDOW_ID + std::to_string(count++);
69 } else {
70 std::string windowName = context->GetBundleName() + std::to_string(count++);
71 std::size_t pos = windowName.find_last_of('.');
72 return (pos == std::string::npos) ? windowName : windowName.substr(pos + 1); // skip '.'
73 }
74 }
75
CreateWindow(const std::string & windowName,sptr<WindowOption> & option) const76 sptr<Window> WindowScene::CreateWindow(const std::string& windowName, sptr<WindowOption>& option) const
77 {
78 if (windowName.empty() || mainWindow_ == nullptr || option == nullptr) {
79 WLOGFE("WindowScene Name: %{public}s", windowName.c_str());
80 return nullptr;
81 }
82 option->SetParentId(mainWindow_->GetWindowId());
83 option->SetWindowTag(WindowTag::SUB_WINDOW);
84 return SingletonContainer::Get<StaticCall>().CreateWindow(windowName, option, mainWindow_->GetContext());
85 }
86
GetMainWindow() const87 const sptr<Window>& WindowScene::GetMainWindow() const
88 {
89 return mainWindow_;
90 }
91
GetSubWindow()92 std::vector<sptr<Window>> WindowScene::GetSubWindow()
93 {
94 if (mainWindow_ == nullptr) {
95 WLOGFE("Get sub window failed, because main window is null");
96 return std::vector<sptr<Window>>();
97 }
98 uint32_t parentId = mainWindow_->GetWindowId();
99 return SingletonContainer::Get<StaticCall>().GetSubWindow(parentId);
100 }
101
GoForeground(uint32_t reason)102 WMError WindowScene::GoForeground(uint32_t reason)
103 {
104 WLOGFI("reason:%{public}u", reason);
105 if (mainWindow_ == nullptr) {
106 WLOGFE("Go foreground failed, because main window is null");
107 return WMError::WM_ERROR_NULLPTR;
108 }
109 return mainWindow_->Show(reason);
110 }
111
GoBackground(uint32_t reason)112 WMError WindowScene::GoBackground(uint32_t reason)
113 {
114 WLOGFI("reason:%{public}u", reason);
115 if (mainWindow_ == nullptr) {
116 WLOGFE("Go background failed, because main window is null");
117 return WMError::WM_ERROR_NULLPTR;
118 }
119 return mainWindow_->Hide(reason);
120 }
121
GoDestroy()122 WMError WindowScene::GoDestroy()
123 {
124 if (mainWindow_ == nullptr) {
125 return WMError::WM_ERROR_NULLPTR;
126 }
127
128 WMError ret = mainWindow_->Destroy();
129 if (ret != WMError::WM_OK) {
130 WLOGFE("WindowScene go destroy failed name: %{public}s", mainWindow_->GetWindowName().c_str());
131 return ret;
132 }
133 mainWindow_ = nullptr;
134 return WMError::WM_OK;
135 }
136
OnNewWant(const AAFwk::Want & want)137 WMError WindowScene::OnNewWant(const AAFwk::Want& want)
138 {
139 if (mainWindow_ == nullptr) {
140 WLOGFE("On new want failed, because main window is null");
141 return WMError::WM_ERROR_NULLPTR;
142 }
143 mainWindow_->OnNewWant(want);
144 return WMError::WM_OK;
145 }
146
SetSystemBarProperty(WindowType type,const SystemBarProperty & property) const147 WMError WindowScene::SetSystemBarProperty(WindowType type, const SystemBarProperty& property) const
148 {
149 if (mainWindow_ == nullptr) {
150 WLOGFE("Set systembar property failed, because main window is null");
151 return WMError::WM_ERROR_NULLPTR;
152 }
153 return mainWindow_->SetSystemBarProperty(type, property);
154 }
155
RequestFocus() const156 WMError WindowScene::RequestFocus() const
157 {
158 if (mainWindow_ == nullptr) {
159 WLOGFE("Request focus failed, because main window is null");
160 return WMError::WM_ERROR_NULLPTR;
161 }
162 return mainWindow_->RequestFocus();
163 }
164
UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration> & configuration)165 void WindowScene::UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
166 {
167 if (mainWindow_ == nullptr) {
168 WLOGFE("Update configuration failed, because main window is null");
169 return;
170 }
171 WLOGFI("notify mainWindow winId:%{public}u", mainWindow_->GetWindowId());
172 mainWindow_->UpdateConfiguration(configuration);
173 }
174
GetContentInfo() const175 std::string WindowScene::GetContentInfo() const
176 {
177 if (mainWindow_ == nullptr) {
178 WLOGFE("Get content info failed, because main window is null");
179 return "";
180 }
181 return mainWindow_->GetContentInfo();
182 }
183
NotifyMemoryLevel(int32_t level) const184 WMError WindowScene::NotifyMemoryLevel(int32_t level) const
185 {
186 WLOGFI("Notify memory level: %{public}d", level);
187 if (mainWindow_ == nullptr) {
188 WLOGFE("Notify memory level failed, because main window is null");
189 return WMError::WM_ERROR_NULLPTR;
190 }
191 return mainWindow_->NotifyMemoryLevel(level);
192 }
193 } // namespace Rosen
194 } // namespace OHOS
195