• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 <configuration.h>
17 
18 #include "window_impl.h"
19 #include "window_manager_hilog.h"
20 #include "window_scene.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 const std::string WindowScene::MAIN_WINDOW_ID = "main window";
25 
~WindowScene()26 WindowScene::~WindowScene()
27 {
28 }
29 
Init(DisplayId displayId,const std::shared_ptr<AbilityRuntime::Context> & context,sptr<IWindowLifeCycle> & listener,sptr<WindowOption> option)30 WMError WindowScene::Init(DisplayId displayId, const std::shared_ptr<AbilityRuntime::Context>& context,
31     sptr<IWindowLifeCycle>& listener, sptr<WindowOption> option)
32 {
33     displayId_ = displayId;
34     if (option == nullptr) {
35         option = new(std::nothrow) WindowOption();
36         if (option == nullptr) {
37             WLOGFW("alloc WindowOption failed");
38             return WMError::WM_ERROR_NULLPTR;
39         }
40     }
41     option->SetDisplayId(displayId);
42     option->SetWindowTag(WindowTag::MAIN_WINDOW);
43 
44     mainWindow_ = Window::Create(GenerateMainWindowName(context), option, context);
45     if (mainWindow_ == nullptr) {
46         return WMError::WM_ERROR_NULLPTR;
47     }
48     mainWindow_->RegisterLifeCycleListener(listener);
49 
50     return WMError::WM_OK;
51 }
52 
GenerateMainWindowName(const std::shared_ptr<AbilityRuntime::Context> & context) const53 std::string WindowScene::GenerateMainWindowName(const std::shared_ptr<AbilityRuntime::Context>& context) const
54 {
55     if (context == nullptr) {
56         return MAIN_WINDOW_ID + std::to_string(count++);
57     } else {
58         std::string windowName = "MainWinodw" + std::to_string(count++);
59         std::size_t pos = windowName.find_last_of('.');
60         return (pos == std::string::npos) ? windowName : windowName.substr(pos + 1); // skip '.'
61     }
62 }
63 
CreateWindow(const std::string & windowName,sptr<WindowOption> & option) const64 sptr<Window> WindowScene::CreateWindow(const std::string& windowName, sptr<WindowOption>& option) const
65 {
66     if (windowName.empty() || mainWindow_ == nullptr || option == nullptr) {
67         WLOGFE("WindowScene Name: %{public}s", windowName.c_str());
68         return nullptr;
69     }
70     option->SetParentId(mainWindow_->GetWindowId());
71     option->SetWindowTag(WindowTag::SUB_WINDOW);
72     return Window::Create(windowName, option, mainWindow_->GetContext());
73 }
74 
GetMainWindow() const75 const sptr<Window>& WindowScene::GetMainWindow() const
76 {
77     return mainWindow_;
78 }
79 
GetSubWindow()80 std::vector<sptr<Window>> WindowScene::GetSubWindow()
81 {
82     if (mainWindow_ == nullptr) {
83         WLOGFE("Get sub window failed, because main window is null");
84         return std::vector<sptr<Window>>();
85     }
86     uint32_t parentId = mainWindow_->GetWindowId();
87     return Window::GetSubWindow(parentId);
88 }
89 
GoDestroy()90 WMError WindowScene::GoDestroy()
91 {
92     if (mainWindow_ == nullptr) {
93         return WMError::WM_ERROR_NULLPTR;
94     }
95 
96     WMError ret = mainWindow_->Destroy();
97     if (ret != WMError::WM_OK) {
98         WLOGFE("WindowScene go destroy failed name: %{public}s", mainWindow_->GetWindowName().c_str());
99         return ret;
100     }
101     mainWindow_ = nullptr;
102     return WMError::WM_OK;
103 }
104 
UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration> & configuration)105 void WindowScene::UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
106 {
107     if (mainWindow_ == nullptr) {
108         WLOGFE("Update configuration failed, because main window is null");
109         return;
110     }
111     WLOGFI("notify mainWindow winId:%{public}u", mainWindow_->GetWindowId());
112     mainWindow_->UpdateConfiguration(configuration);
113 }
114 } // namespace Rosen
115 } // namespace OHOS
116