1 /*
2 * Copyright (c) 2022-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 "window_scene.h"
17
18 #include <configuration.h>
19 #include "window_impl.h"
20 #include "window_manager_hilog.h"
21 #include "window_model.h"
22 #include "window_display.h"
23
24 namespace OHOS {
25 namespace Rosen {
26 const std::string WindowScene::MAIN_WINDOW_ID = "main window";
27 namespace {
28 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowScene"};
29 }
~WindowScene()30 WindowScene::~WindowScene()
31 {
32 }
33
Init(DisplayId displayId,const std::shared_ptr<AbilityRuntime::Context> & context,sptr<IWindowLifeCycle> & listener,sptr<WindowOption> option)34 WMError WindowScene::Init(DisplayId displayId, const std::shared_ptr<AbilityRuntime::Context>& context,
35 sptr<IWindowLifeCycle>& listener, sptr<WindowOption> option)
36 {
37 WLOGFD("WindowScene with window session!");
38 displayId_ = displayId;
39 if (option == nullptr) {
40 option = new(std::nothrow) WindowOption();
41 if (option == nullptr) {
42 WLOGFE("alloc WindowOption failed");
43 return WMError::WM_ERROR_NULLPTR;
44 }
45 }
46 option->SetDisplayId(displayId);
47 option->SetWindowTag(WindowTag::MAIN_WINDOW);
48
49 mainWindow_ = Window::Create(GenerateMainWindowName(context), option, context);
50 if (mainWindow_ == nullptr) {
51 WLOGFE("mainWindow_ is NULL");
52 return WMError::WM_ERROR_NULLPTR;
53 }
54
55 Previewer::PreviewerWindowModel& windowModel = Previewer::PreviewerWindow::GetInstance().GetWindowParams();
56 Ace::ViewportConfig config;
57 config.SetSize(windowModel.originWidth, windowModel.originHeight);
58 config.SetPosition(0, 0);
59 config.SetOrientation(static_cast<int32_t>(Previewer::PreviewerWindow::TransOrientation(windowModel.orientation)));
60 config.SetDensity(windowModel.density);
61 mainWindow_->SetViewportConfig(config);
62
63 Previewer::PreviewerWindow::GetInstance().SetWindowObject(mainWindow_.GetRefPtr());
64 mainWindow_->RegisterLifeCycleListener(listener);
65
66 Previewer::PreviewerDisplay::GetInstance().RegisterStatusChangedCallback(
67 [this](FoldStatus status) {
68 WLOGFI("FoldStatus changed to %{public}d", status);
69 if (mainWindow_ == nullptr) {
70 WLOGFE("mainWindow_ is NULL");
71 return WMError::WM_ERROR_NULLPTR;
72 }
73 Previewer::PreviewerDisplay::GetInstance().SetFoldStatus(status);
74 return WMError::WM_OK;
75 }
76 );
77
78 return WMError::WM_OK;
79 }
80
GenerateMainWindowName(const std::shared_ptr<AbilityRuntime::Context> & context) const81 std::string WindowScene::GenerateMainWindowName(const std::shared_ptr<AbilityRuntime::Context>& context) const
82 {
83 if (context == nullptr) {
84 return MAIN_WINDOW_ID + std::to_string(count++);
85 } else {
86 auto options = context->GetOptions();
87 std::string windowName = options.bundleName + std::to_string(count++);
88 std::size_t pos = windowName.find_last_of('.');
89 return (pos == std::string::npos) ? windowName : windowName.substr(pos + 1); // skip '.'
90 }
91 }
92
CreateWindow(const std::string & windowName,sptr<WindowOption> & option) const93 sptr<Window> WindowScene::CreateWindow(const std::string& windowName, sptr<WindowOption>& option) const
94 {
95 return nullptr;
96 }
97
GetMainWindow() const98 const sptr<Window>& WindowScene::GetMainWindow() const
99 {
100 return mainWindow_;
101 }
102
GetSubWindow()103 std::vector<sptr<Window>> WindowScene::GetSubWindow()
104 {
105 return std::vector<sptr<Window>>();
106 }
107
GoDestroy()108 WMError WindowScene::GoDestroy()
109 {
110 if (mainWindow_ == nullptr) {
111 return WMError::WM_ERROR_NULLPTR;
112 }
113
114 WMError ret = mainWindow_->Destroy();
115 if (ret != WMError::WM_OK) {
116 WLOGFE("WindowScene go destroy failed name: %{public}s", mainWindow_->GetWindowName().c_str());
117 return ret;
118 }
119 mainWindow_ = nullptr;
120 return WMError::WM_OK;
121 }
122
UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration> & configuration)123 void WindowScene::UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
124 {
125 if (mainWindow_ == nullptr) {
126 WLOGFE("Update configuration failed, because main window is null");
127 return;
128 }
129 WLOGFI("notify mainWindow winId:%{public}u", mainWindow_->GetWindowId());
130 mainWindow_->UpdateConfiguration(configuration);
131 }
132 } // namespace Rosen
133 } // namespace OHOS
134