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