• 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 "inner_window.h"
17 
18 #include "ui_service_mgr_client.h"
19 #include "window_manager_hilog.h"
20 #include "surface_draw.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 namespace {
25     constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "InnerWindow"};
26     const std::string IMAGE_PLACE_HOLDER_PNG_PATH = "/etc/window/resources/bg_place_holder.png";
27 }
28 WM_IMPLEMENT_SINGLE_INSTANCE(PlaceHolderWindow)
WM_IMPLEMENT_SINGLE_INSTANCE(DividerWindow)29 WM_IMPLEMENT_SINGLE_INSTANCE(DividerWindow)
30 
31 void PlaceholderWindowListener::OnTouchOutside() const
32 {
33     WLOGFD("place holder touch outside");
34     PlaceHolderWindow::GetInstance().Destroy();
35 }
36 
AfterUnfocused()37 void PlaceholderWindowListener::AfterUnfocused()
38 {
39     WLOGFD("place holder after unfocused");
40     PlaceHolderWindow::GetInstance().Destroy();
41 }
42 
OnInputEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent) const43 bool PlaceholderInputEventConsumer::OnInputEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent) const
44 {
45     WLOGFD("place holder get key event");
46     PlaceHolderWindow::GetInstance().Destroy();
47     return true;
48 }
49 
OnInputEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent) const50 bool PlaceholderInputEventConsumer::OnInputEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent) const
51 {
52     WLOGFD("place holder get point event");
53     PlaceHolderWindow::GetInstance().Destroy();
54     return true;
55 }
56 
OnInputEvent(const std::shared_ptr<MMI::AxisEvent> & axisEvent) const57 bool PlaceholderInputEventConsumer::OnInputEvent(const std::shared_ptr<MMI::AxisEvent>& axisEvent) const
58 {
59     // do nothing
60     return false;
61 }
62 
Create(std::string name,DisplayId displyId,Rect rect,WindowMode mode)63 void PlaceHolderWindow::Create(std::string name, DisplayId displyId, Rect rect, WindowMode mode)
64 {
65     WLOGFD("create inner display id: %{public}" PRIu64"", displyId);
66     if (window_ != nullptr) {
67         WLOGFW("window has created.");
68         return;
69     }
70     sptr<WindowOption> option = new (std::nothrow) WindowOption();
71     if (option == nullptr) {
72         WLOGFE("window option is nullptr.");
73         return;
74     }
75     option->SetFocusable(false);
76     option->SetWindowMode(mode);
77     option->SetWindowRect(rect);
78     option->SetWindowType(WindowType::WINDOW_TYPE_PLACEHOLDER);
79     window_ = Window::Create(name, option);
80     if (window_ == nullptr) {
81         WLOGFE("window is nullptr.");
82         return;
83     }
84     window_->AddWindowFlag(WindowFlag::WINDOW_FLAG_FORBID_SPLIT_MOVE);
85     RegisterWindowListener();
86     SetInputEventConsumer();
87     if (!OHOS::Rosen::SurfaceDraw::DrawImage(window_->GetSurfaceNode(), rect.width_, rect.height_,
88         IMAGE_PLACE_HOLDER_PNG_PATH)) {
89         WLOGE("draw surface failed");
90         return;
91     }
92     window_->Show();
93     WLOGFD("create palce holder Window end");
94 }
95 
RegisterWindowListener()96 void PlaceHolderWindow::RegisterWindowListener()
97 {
98     if (window_ == nullptr) {
99         WLOGFE("Window is nullptr, register window listener failed.");
100         return;
101     }
102     if (windowListener_ == nullptr) {
103         windowListener_ = new (std::nothrow) PlaceholderWindowListener();
104     }
105     window_->RegisterTouchOutsideListener(windowListener_);
106     window_->RegisterLifeCycleListener(windowListener_);
107 }
108 
UnRegisterWindowListener()109 void PlaceHolderWindow::UnRegisterWindowListener()
110 {
111     if (window_ == nullptr || windowListener_ == nullptr) {
112         WLOGFE("Window or listener is nullptr, unregister window listener failed.");
113         return;
114     }
115     window_->UnregisterTouchOutsideListener(windowListener_);
116     window_->UnregisterLifeCycleListener(windowListener_);
117 }
118 
SetInputEventConsumer()119 void PlaceHolderWindow::SetInputEventConsumer()
120 {
121     if (window_ == nullptr) {
122         WLOGFE("Window is nullptr, set window input event consumer failed.");
123         return;
124     }
125     if (inputEventConsumer_ == nullptr) {
126         inputEventConsumer_ = std::make_shared<PlaceholderInputEventConsumer>();
127     }
128     window_->SetInputEventConsumer(inputEventConsumer_);
129 }
130 
Destroy()131 void PlaceHolderWindow::Destroy()
132 {
133     WLOGFI("destroy place holder window begin.");
134     if (window_ != nullptr) {
135         WLOGFI("destroy place holder window not nullptr.");
136         UnRegisterWindowListener();
137         window_->SetInputEventConsumer(nullptr);
138         window_->Destroy();
139     }
140     window_ = nullptr;
141     WLOGFI("destroy place holder window end.");
142 }
143 
~DividerWindow()144 DividerWindow::~DividerWindow()
145 {
146     Destroy();
147 }
148 
Create(std::string name,DisplayId displayId,const Rect rect,WindowMode mode)149 void DividerWindow::Create(std::string name, DisplayId displayId, const Rect rect, WindowMode mode)
150 {
151     displayId_ = displayId;
152     WLOGFD("create divider dialog display id: %{public}" PRIu64"", displayId_);
153     auto dialogCallback = [this](int32_t id, const std::string& event, const std::string& params) {
154         WLOGFD("divider dialog window get param: %{public}s", params.c_str());
155     };
156     Ace::UIServiceMgrClient::GetInstance()->ShowDialog(name, params_, WindowType::WINDOW_TYPE_DOCK_SLICE,
157         rect.posX_, rect.posY_, rect.width_, rect.height_, dialogCallback, &dialogId_);
158     WLOGFD("create divider dialog window id: %{public}d success", dialogId_);
159 }
160 
Destroy()161 void DividerWindow::Destroy()
162 {
163     if (dialogId_ == IVALID_DIALOG_WINDOW_ID) {
164         return;
165     }
166     WLOGFD("destroy divider dialog window id:: %{public}d.", dialogId_);
167     Ace::UIServiceMgrClient::GetInstance()->CancelDialog(dialogId_);
168     dialogId_ = IVALID_DIALOG_WINDOW_ID;
169 }
170 
Update(uint32_t width,uint32_t height)171 void DividerWindow::Update(uint32_t width, uint32_t height)
172 {
173     if (dialogId_ == IVALID_DIALOG_WINDOW_ID) {
174         return;
175     }
176     WLOGFD("update divider dialog window dialog id:%{public}d width:%{public}u height:%{public}u.",
177         dialogId_, width, height);
178     std::stringstream sstream;
179     sstream << "{\"width\":" << std::to_string(width) << "," << "\"height\":" << std::to_string(height) << "}";
180     // data is json file format
181     std::string data = sstream.str();
182     Ace::UIServiceMgrClient::GetInstance()->UpdateDialog(dialogId_, data);
183 }
184 } // Rosen
185 } // OHOS