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_impl.h"
17
18 #include <cmath>
19
20 #include <ability_manager_client.h>
21
22 #include "color_parser.h"
23 #include "display_manager.h"
24 #include "singleton_container.h"
25 #include "window_adapter.h"
26 #include "window_agent.h"
27 #include "window_helper.h"
28 #include "window_manager_hilog.h"
29 #include "wm_common.h"
30 #include "wm_common_inner.h"
31
32 namespace OHOS {
33 namespace Rosen {
34 namespace {
35 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowImpl"};
36 }
37
38 const WindowImpl::ColorSpaceConvertMap WindowImpl::colorSpaceConvertMap[] = {
39 { ColorSpace::COLOR_SPACE_DEFAULT, COLOR_GAMUT_SRGB },
40 { ColorSpace::COLOR_SPACE_WIDE_GAMUT, COLOR_GAMUT_DCI_P3 },
41 };
42
43 std::map<std::string, std::pair<uint32_t, sptr<Window>>> WindowImpl::windowMap_;
44 std::map<uint32_t, std::vector<sptr<WindowImpl>>> WindowImpl::subWindowMap_;
45 std::map<uint32_t, std::vector<sptr<WindowImpl>>> WindowImpl::appFloatingWindowMap_;
46
WindowImpl(const sptr<WindowOption> & option)47 WindowImpl::WindowImpl(const sptr<WindowOption>& option)
48 {
49 property_ = new WindowProperty();
50 property_->SetWindowName(option->GetWindowName());
51 property_->SetWindowRect(option->GetWindowRect());
52 property_->SetWindowType(option->GetWindowType());
53 property_->SetWindowMode(option->GetWindowMode());
54 property_->SetWindowBackgroundBlur(option->GetWindowBackgroundBlur());
55 property_->SetAlpha(option->GetAlpha());
56 property_->SetFullScreen(option->GetWindowMode() == WindowMode::WINDOW_MODE_FULLSCREEN);
57 property_->SetFocusable(option->GetFocusable());
58 property_->SetTouchable(option->GetTouchable());
59 property_->SetDisplayId(option->GetDisplayId());
60 property_->SetCallingWindow(option->GetCallingWindow());
61 property_->SetWindowFlags(option->GetWindowFlags());
62 property_->SetHitOffset(option->GetHitOffset());
63 property_->SetRequestedOrientation(option->GetRequestedOrientation());
64 windowTag_ = option->GetWindowTag();
65 property_->SetTurnScreenOn(option->IsTurnScreenOn());
66 property_->SetKeepScreenOn(option->IsKeepScreenOn());
67 property_->SetBrightness(option->GetBrightness());
68 AdjustWindowAnimationFlag();
69 auto& sysBarPropMap = option->GetSystemBarProperty();
70 for (auto it : sysBarPropMap) {
71 property_->SetSystemBarProperty(it.first, it.second);
72 }
73 name_ = option->GetWindowName();
74 callback_->onCallback = std::bind(&WindowImpl::OnVsync, this, std::placeholders::_1);
75
76 struct RSSurfaceNodeConfig rsSurfaceNodeConfig;
77 rsSurfaceNodeConfig.SurfaceNodeName = property_->GetWindowName();
78 surfaceNode_ = RSSurfaceNode::Create(rsSurfaceNodeConfig);
79 }
80
~WindowImpl()81 WindowImpl::~WindowImpl()
82 {
83 Destroy();
84 }
85
Find(const std::string & name)86 sptr<Window> WindowImpl::Find(const std::string& name)
87 {
88 auto iter = windowMap_.find(name);
89 if (iter == windowMap_.end()) {
90 return nullptr;
91 }
92 return iter->second.second;
93 }
94
GetContext() const95 const std::shared_ptr<AbilityRuntime::Context> WindowImpl::GetContext() const
96 {
97 return context_;
98 }
99
FindTopWindow(uint32_t topWinId)100 sptr<Window> WindowImpl::FindTopWindow(uint32_t topWinId)
101 {
102 if (windowMap_.empty()) {
103 WLOGFE("Please create mainWindow First!");
104 return nullptr;
105 }
106 for (auto iter = windowMap_.begin(); iter != windowMap_.end(); iter++) {
107 if (topWinId == iter->second.first) {
108 WLOGFI("FindTopWindow id: %{public}d", topWinId);
109 return iter->second.second;
110 }
111 }
112 WLOGFE("Cannot find topWindow!");
113 return nullptr;
114 }
115
GetTopWindowWithId(uint32_t mainWinId)116 sptr<Window> WindowImpl::GetTopWindowWithId(uint32_t mainWinId)
117 {
118 uint32_t topWinId = INVALID_WINDOW_ID;
119 WMError ret = SingletonContainer::Get<WindowAdapter>().GetTopWindowId(mainWinId, topWinId);
120 if (ret != WMError::WM_OK) {
121 WLOGFE("GetTopWindowId failed with errCode:%{public}d", static_cast<int32_t>(ret));
122 return nullptr;
123 }
124 return FindTopWindow(topWinId);
125 }
126
GetTopWindowWithContext(const std::shared_ptr<AbilityRuntime::Context> & context)127 sptr<Window> WindowImpl::GetTopWindowWithContext(const std::shared_ptr<AbilityRuntime::Context>& context)
128 {
129 if (windowMap_.empty()) {
130 WLOGFE("Please create mainWindow First!");
131 return nullptr;
132 }
133 uint32_t mainWinId = INVALID_WINDOW_ID;
134 for (auto iter = windowMap_.begin(); iter != windowMap_.end(); iter++) {
135 auto win = iter->second.second;
136 if (context.get() == win->GetContext().get() && WindowHelper::IsMainWindow(win->GetType())) {
137 mainWinId = win->GetWindowId();
138 WLOGFI("GetTopWindow Find MainWinId:%{public}d with context %{public}p!", mainWinId, context.get());
139 break;
140 }
141 }
142 WLOGFI("GetTopWindowfinal MainWinId:%{public}u!", mainWinId);
143 if (mainWinId == INVALID_WINDOW_ID) {
144 WLOGFE("Cannot find topWindow!");
145 return nullptr;
146 }
147 uint32_t topWinId = INVALID_WINDOW_ID;
148 WMError ret = SingletonContainer::Get<WindowAdapter>().GetTopWindowId(mainWinId, topWinId);
149 if (ret != WMError::WM_OK) {
150 WLOGFE("GetTopWindowId failed with errCode:%{public}d", static_cast<int32_t>(ret));
151 return nullptr;
152 }
153 return FindTopWindow(topWinId);
154 }
155
GetSubWindow(uint32_t parentId)156 std::vector<sptr<Window>> WindowImpl::GetSubWindow(uint32_t parentId)
157 {
158 if (subWindowMap_.find(parentId) == subWindowMap_.end()) {
159 WLOGFE("Cannot parentWindow with id: %{public}d!", parentId);
160 return std::vector<sptr<Window>>();
161 }
162 return std::vector<sptr<Window>>(subWindowMap_[parentId].begin(), subWindowMap_[parentId].end());
163 }
164
GetSurfaceNode() const165 std::shared_ptr<RSSurfaceNode> WindowImpl::GetSurfaceNode() const
166 {
167 return surfaceNode_;
168 }
169
GetRect() const170 Rect WindowImpl::GetRect() const
171 {
172 return property_->GetWindowRect();
173 }
174
GetType() const175 WindowType WindowImpl::GetType() const
176 {
177 return property_->GetWindowType();
178 }
179
GetMode() const180 WindowMode WindowImpl::GetMode() const
181 {
182 return property_->GetWindowMode();
183 }
184
GetWindowBackgroundBlur() const185 WindowBlurLevel WindowImpl::GetWindowBackgroundBlur() const
186 {
187 return property_->GetWindowBackgroundBlur();
188 }
189
GetAlpha() const190 float WindowImpl::GetAlpha() const
191 {
192 return property_->GetAlpha();
193 }
194
GetShowState() const195 bool WindowImpl::GetShowState() const
196 {
197 return state_ == WindowState::STATE_SHOWN;
198 }
199
SetFocusable(bool isFocusable)200 WMError WindowImpl::SetFocusable(bool isFocusable)
201 {
202 if (!IsWindowValid()) {
203 return WMError::WM_ERROR_INVALID_WINDOW;
204 }
205 property_->SetFocusable(isFocusable);
206 if (state_ == WindowState::STATE_SHOWN) {
207 return UpdateProperty(PropertyChangeAction::ACTION_UPDATE_FOCUSABLE);
208 }
209 return WMError::WM_OK;
210 }
211
GetFocusable() const212 bool WindowImpl::GetFocusable() const
213 {
214 return property_->GetFocusable();
215 }
216
SetTouchable(bool isTouchable)217 WMError WindowImpl::SetTouchable(bool isTouchable)
218 {
219 if (!IsWindowValid()) {
220 return WMError::WM_ERROR_INVALID_WINDOW;
221 }
222 property_->SetTouchable(isTouchable);
223 if (state_ == WindowState::STATE_SHOWN) {
224 return UpdateProperty(PropertyChangeAction::ACTION_UPDATE_TOUCHABLE);
225 }
226 return WMError::WM_OK;
227 }
228
GetTouchable() const229 bool WindowImpl::GetTouchable() const
230 {
231 return property_->GetTouchable();
232 }
233
GetWindowName() const234 const std::string& WindowImpl::GetWindowName() const
235 {
236 return name_;
237 }
238
GetWindowId() const239 uint32_t WindowImpl::GetWindowId() const
240 {
241 return property_->GetWindowId();
242 }
243
GetWindowFlags() const244 uint32_t WindowImpl::GetWindowFlags() const
245 {
246 return property_->GetWindowFlags();
247 }
248
GetSystemBarPropertyByType(WindowType type) const249 SystemBarProperty WindowImpl::GetSystemBarPropertyByType(WindowType type) const
250 {
251 auto curProperties = property_->GetSystemBarProperty();
252 return curProperties[type];
253 }
254
GetAvoidAreaByType(AvoidAreaType type,AvoidArea & avoidArea)255 WMError WindowImpl::GetAvoidAreaByType(AvoidAreaType type, AvoidArea& avoidArea)
256 {
257 WLOGFI("GetAvoidAreaByType Search Type: %{public}u", static_cast<uint32_t>(type));
258 std::vector<Rect> avoidAreaVec;
259 uint32_t windowId = property_->GetWindowId();
260 WMError ret = SingletonContainer::Get<WindowAdapter>().GetAvoidAreaByType(windowId, type, avoidAreaVec);
261 if (ret != WMError::WM_OK || avoidAreaVec.size() != 4) { // 4: the avoid area num (left, top, right, bottom)
262 WLOGFE("GetAvoidAreaByType errCode:%{public}d winId:%{public}u Type is :%{public}u." \
263 "Or avoidArea Size != 4. Current size of avoid area: %{public}u", static_cast<int32_t>(ret),
264 property_->GetWindowId(), static_cast<uint32_t>(type), static_cast<uint32_t>(avoidAreaVec.size()));
265 return ret;
266 }
267 avoidArea = {avoidAreaVec[0], avoidAreaVec[1], avoidAreaVec[2], avoidAreaVec[3]}; // 0:left 1:top 2:right 3:bottom
268 return ret;
269 }
270
SetWindowType(WindowType type)271 WMError WindowImpl::SetWindowType(WindowType type)
272 {
273 WLOGFI("window id: %{public}u, type:%{public}u", property_->GetWindowId(), static_cast<uint32_t>(type));
274 if (!IsWindowValid()) {
275 return WMError::WM_ERROR_INVALID_WINDOW;
276 }
277 if (state_ == WindowState::STATE_CREATED) {
278 if (!(WindowHelper::IsAppWindow(type) || WindowHelper::IsSystemWindow(type))) {
279 WLOGFE("window type is invalid %{public}d", type);
280 return WMError::WM_ERROR_INVALID_PARAM;
281 }
282 property_->SetWindowType(type);
283 AdjustWindowAnimationFlag();
284 return WMError::WM_OK;
285 }
286 if (property_->GetWindowType() != type) {
287 return WMError::WM_ERROR_INVALID_PARAM;
288 }
289 return WMError::WM_OK;
290 }
291
SetWindowMode(WindowMode mode)292 WMError WindowImpl::SetWindowMode(WindowMode mode)
293 {
294 WLOGFI("[Client] Window %{public}u mode %{public}u", property_->GetWindowId(), static_cast<uint32_t>(mode));
295 if (!IsWindowValid()) {
296 return WMError::WM_ERROR_INVALID_WINDOW;
297 }
298 if (state_ == WindowState::STATE_CREATED || state_ == WindowState::STATE_HIDDEN) {
299 UpdateMode(mode);
300 } else if (state_ == WindowState::STATE_SHOWN) {
301 property_->SetWindowMode(mode);
302 WMError ret = UpdateProperty(PropertyChangeAction::ACTION_UPDATE_MODE);
303 if (ret != WMError::WM_OK) {
304 return ret;
305 }
306 // set client window mode if success.
307 UpdateMode(mode);
308 }
309 if (property_->GetWindowMode() != mode) {
310 WLOGFE("set window mode filed! id: %{public}d", property_->GetWindowId());
311 return WMError::WM_ERROR_INVALID_PARAM;
312 }
313 return WMError::WM_OK;
314 }
315
SetWindowBackgroundBlur(WindowBlurLevel level)316 WMError WindowImpl::SetWindowBackgroundBlur(WindowBlurLevel level)
317 {
318 WLOGFI("[Client] Window %{public}d blurlevel %{public}u", property_->GetWindowId(), static_cast<uint32_t>(level));
319 if (!IsWindowValid()) {
320 return WMError::WM_ERROR_INVALID_WINDOW;
321 }
322 property_->SetWindowBackgroundBlur(level);
323 return SingletonContainer::Get<WindowAdapter>().SetWindowBackgroundBlur(property_->GetWindowId(), level);
324 }
325
SetAlpha(float alpha)326 WMError WindowImpl::SetAlpha(float alpha)
327 {
328 WLOGFI("[Client] Window %{public}d alpha %{public}f", property_->GetWindowId(), alpha);
329 if (!IsWindowValid()) {
330 return WMError::WM_ERROR_INVALID_WINDOW;
331 }
332 property_->SetAlpha(alpha);
333 return SingletonContainer::Get<WindowAdapter>().SetAlpha(property_->GetWindowId(), alpha);
334 }
335
AddWindowFlag(WindowFlag flag)336 WMError WindowImpl::AddWindowFlag(WindowFlag flag)
337 {
338 if (flag == WindowFlag::WINDOW_FLAG_SHOW_WHEN_LOCKED && state_ != WindowState::STATE_CREATED) {
339 WLOGFE("Only support add show when locked when window create, id: %{public}u", property_->GetWindowId());
340 return WMError::WM_ERROR_INVALID_WINDOW;
341 }
342
343 uint32_t updateFlags = property_->GetWindowFlags() | (static_cast<uint32_t>(flag));
344 return SetWindowFlags(updateFlags);
345 }
346
RemoveWindowFlag(WindowFlag flag)347 WMError WindowImpl::RemoveWindowFlag(WindowFlag flag)
348 {
349 if (flag == WindowFlag::WINDOW_FLAG_SHOW_WHEN_LOCKED && state_ != WindowState::STATE_CREATED) {
350 WLOGFE("Only support remove show when locked when window create, id: %{public}u", property_->GetWindowId());
351 return WMError::WM_ERROR_INVALID_WINDOW;
352 }
353
354 uint32_t updateFlags = property_->GetWindowFlags() & (~(static_cast<uint32_t>(flag)));
355 return SetWindowFlags(updateFlags);
356 }
357
SetWindowFlags(uint32_t flags)358 WMError WindowImpl::SetWindowFlags(uint32_t flags)
359 {
360 WLOGFI("[Client] Window %{public}d flags %{public}u", property_->GetWindowId(), flags);
361 if (!IsWindowValid()) {
362 return WMError::WM_ERROR_INVALID_WINDOW;
363 }
364 if (property_->GetWindowFlags() == flags) {
365 return WMError::WM_OK;
366 }
367 property_->SetWindowFlags(flags);
368 if (state_ == WindowState::STATE_CREATED || state_ == WindowState::STATE_HIDDEN) {
369 return WMError::WM_OK;
370 }
371 WMError ret = UpdateProperty(PropertyChangeAction::ACTION_UPDATE_FLAGS);
372 if (ret != WMError::WM_OK) {
373 WLOGFE("SetWindowFlags errCode:%{public}d winId:%{public}d",
374 static_cast<int32_t>(ret), property_->GetWindowId());
375 }
376 return ret;
377 }
378
SetUIContent(const std::string & contentInfo,NativeEngine * engine,NativeValue * storage,bool isdistributed,AppExecFwk::Ability * ability)379 WMError WindowImpl::SetUIContent(const std::string& contentInfo,
380 NativeEngine* engine, NativeValue* storage, bool isdistributed, AppExecFwk::Ability* ability)
381 {
382 WLOGFI("SetUIContent contentInfo: %{public}s", contentInfo.c_str());
383 if (ability != nullptr) {
384 uiContent_ = Ace::UIContent::Create(ability);
385 } else {
386 uiContent_ = Ace::UIContent::Create(context_.get(), engine);
387 }
388 if (uiContent_ == nullptr) {
389 WLOGFE("fail to SetUIContent id: %{public}d", property_->GetWindowId());
390 return WMError::WM_ERROR_NULLPTR;
391 }
392 if (WindowHelper::IsMainWindow(property_->GetWindowType())) {
393 property_->SetDecorEnable(true);
394 } else {
395 property_->SetDecorEnable(false);
396 }
397 if (isdistributed) {
398 uiContent_->Restore(this, contentInfo, storage);
399 } else {
400 uiContent_->Initialize(this, contentInfo, storage);
401 }
402 if (state_ == WindowState::STATE_SHOWN) {
403 Ace::ViewportConfig config;
404 Rect rect = GetRect();
405 config.SetSize(rect.width_, rect.height_);
406 config.SetPosition(rect.posX_, rect.posY_);
407 auto display = DisplayManager::GetInstance().GetDisplayById(property_->GetDisplayId());
408 if (display == nullptr) {
409 WLOGFE("get display failed displayId:%{public}" PRIu64", window id:%{public}u", property_->GetDisplayId(),
410 property_->GetWindowId());
411 return WMError::WM_ERROR_NULLPTR;
412 }
413 float virtualPixelRatio = display->GetVirtualPixelRatio();
414 config.SetDensity(virtualPixelRatio);
415 uiContent_->UpdateViewportConfig(config, WindowSizeChangeReason::UNDEFINED);
416 WLOGFI("notify uiContent window size change end");
417 }
418 return WMError::WM_OK;
419 }
420
GetUIContent() const421 Ace::UIContent* WindowImpl::GetUIContent() const
422 {
423 return uiContent_.get();
424 }
425
GetContentInfo()426 std::string WindowImpl::GetContentInfo()
427 {
428 WLOGFI("GetContentInfo");
429 if (uiContent_ == nullptr) {
430 WLOGFE("fail to GetContentInfo id: %{public}d", property_->GetWindowId());
431 return "";
432 }
433 return uiContent_->GetContentInfo();
434 }
435
GetColorSpaceFromSurfaceGamut(SurfaceColorGamut surfaceColorGamut)436 ColorSpace WindowImpl::GetColorSpaceFromSurfaceGamut(SurfaceColorGamut surfaceColorGamut)
437 {
438 for (auto item: colorSpaceConvertMap) {
439 if (item.sufaceColorGamut == surfaceColorGamut) {
440 return item.colorSpace;
441 }
442 }
443 return ColorSpace::COLOR_SPACE_DEFAULT;
444 }
445
GetSurfaceGamutFromColorSpace(ColorSpace colorSpace)446 SurfaceColorGamut WindowImpl::GetSurfaceGamutFromColorSpace(ColorSpace colorSpace)
447 {
448 for (auto item: colorSpaceConvertMap) {
449 if (item.colorSpace == colorSpace) {
450 return item.sufaceColorGamut;
451 }
452 }
453 return SurfaceColorGamut::COLOR_GAMUT_SRGB;
454 }
455
IsSupportWideGamut()456 bool WindowImpl::IsSupportWideGamut()
457 {
458 return true;
459 }
460
SetColorSpace(ColorSpace colorSpace)461 void WindowImpl::SetColorSpace(ColorSpace colorSpace)
462 {
463 auto surfaceGamut = GetSurfaceGamutFromColorSpace(colorSpace);
464 surfaceNode_->SetColorSpace(surfaceGamut);
465 }
466
GetColorSpace()467 ColorSpace WindowImpl::GetColorSpace()
468 {
469 auto surfaceGamut = surfaceNode_->GetColorSpace();
470 return GetColorSpaceFromSurfaceGamut(surfaceGamut);
471 }
472
DumpInfo(const std::vector<std::string> & params,std::vector<std::string> & info)473 void WindowImpl::DumpInfo(const std::vector<std::string>& params, std::vector<std::string>& info)
474 {
475 WLOGFI("Ace:DumpInfo");
476 if (uiContent_ != nullptr) {
477 uiContent_->DumpInfo(params, info);
478 }
479 }
480
SetSystemBarProperty(WindowType type,const SystemBarProperty & property)481 WMError WindowImpl::SetSystemBarProperty(WindowType type, const SystemBarProperty& property)
482 {
483 WLOGFI("[Client] Window %{public}d SetSystemBarProperty type %{public}d " \
484 "enable:%{public}d, backgroundColor:%{public}x, contentColor:%{public}x ",
485 property_->GetWindowId(), static_cast<uint32_t>(type), property.enable_,
486 property.backgroundColor_, property.contentColor_);
487 if (!IsWindowValid()) {
488 return WMError::WM_ERROR_INVALID_WINDOW;
489 }
490 if (GetSystemBarPropertyByType(type) == property) {
491 return WMError::WM_OK;
492 }
493 property_->SetSystemBarProperty(type, property);
494 if (state_ == WindowState::STATE_CREATED || state_ == WindowState::STATE_HIDDEN) {
495 return WMError::WM_OK;
496 }
497 WMError ret = UpdateProperty(PropertyChangeAction::ACTION_UPDATE_OTHER_PROPS);
498 if (ret != WMError::WM_OK) {
499 WLOGFE("SetSystemBarProperty errCode:%{public}d winId:%{public}d",
500 static_cast<int32_t>(ret), property_->GetWindowId());
501 }
502 return ret;
503 }
504
SetLayoutFullScreen(bool status)505 WMError WindowImpl::SetLayoutFullScreen(bool status)
506 {
507 WLOGFI("[Client] Window %{public}d SetLayoutFullScreen: %{public}d", property_->GetWindowId(), status);
508 if (!IsWindowValid()) {
509 return WMError::WM_ERROR_INVALID_WINDOW;
510 }
511 WMError ret = SetWindowMode(WindowMode::WINDOW_MODE_FULLSCREEN);
512 if (ret != WMError::WM_OK) {
513 WLOGFE("SetWindowMode errCode:%{public}d winId:%{public}d",
514 static_cast<int32_t>(ret), property_->GetWindowId());
515 return ret;
516 }
517 if (status) {
518 ret = RemoveWindowFlag(WindowFlag::WINDOW_FLAG_NEED_AVOID);
519 if (ret != WMError::WM_OK) {
520 WLOGFE("RemoveWindowFlag errCode:%{public}d winId:%{public}d",
521 static_cast<int32_t>(ret), property_->GetWindowId());
522 return ret;
523 }
524 } else {
525 ret = AddWindowFlag(WindowFlag::WINDOW_FLAG_NEED_AVOID);
526 if (ret != WMError::WM_OK) {
527 WLOGFE("AddWindowFlag errCode:%{public}d winId:%{public}d",
528 static_cast<int32_t>(ret), property_->GetWindowId());
529 return ret;
530 }
531 }
532 return ret;
533 }
534
SetFullScreen(bool status)535 WMError WindowImpl::SetFullScreen(bool status)
536 {
537 WLOGFI("[Client] Window %{public}d SetFullScreen: %{public}d", property_->GetWindowId(), status);
538 WMError ret = SetLayoutFullScreen(status);
539 if (ret != WMError::WM_OK) {
540 WLOGFE("SetLayoutFullScreen errCode:%{public}d winId:%{public}d",
541 static_cast<int32_t>(ret), property_->GetWindowId());
542 return ret;
543 }
544 SystemBarProperty statusProperty = GetSystemBarPropertyByType(
545 WindowType::WINDOW_TYPE_STATUS_BAR);
546 SystemBarProperty naviProperty = GetSystemBarPropertyByType(
547 WindowType::WINDOW_TYPE_NAVIGATION_BAR);
548 if (status) {
549 statusProperty.enable_ = false;
550 naviProperty.enable_ = false;
551 } else {
552 statusProperty.enable_ = true;
553 naviProperty.enable_ = true;
554 }
555 ret = SetSystemBarProperty(WindowType::WINDOW_TYPE_STATUS_BAR, statusProperty);
556 if (ret != WMError::WM_OK) {
557 WLOGFE("SetSystemBarProperty errCode:%{public}d winId:%{public}d",
558 static_cast<int32_t>(ret), property_->GetWindowId());
559 return ret;
560 }
561 ret = SetSystemBarProperty(WindowType::WINDOW_TYPE_NAVIGATION_BAR, naviProperty);
562 if (ret != WMError::WM_OK) {
563 WLOGFE("SetSystemBarProperty errCode:%{public}d winId:%{public}d",
564 static_cast<int32_t>(ret), property_->GetWindowId());
565 }
566 return ret;
567 }
568
MapFloatingWindowToAppIfNeeded()569 void WindowImpl::MapFloatingWindowToAppIfNeeded()
570 {
571 if (GetType() != WindowType::WINDOW_TYPE_FLOAT || context_.get() == nullptr) {
572 return;
573 }
574
575 for (auto& winPair : windowMap_) {
576 auto win = winPair.second.second;
577 if (win->GetType() == WindowType::WINDOW_TYPE_APP_MAIN_WINDOW &&
578 context_.get() == win->GetContext().get()) {
579 appFloatingWindowMap_[win->GetWindowId()].push_back(this);
580 WLOGFI("Map FloatingWindow %{public}d to AppMainWindow %{public}d", GetWindowId(), win->GetWindowId());
581 return;
582 }
583 }
584 }
585
UpdateProperty(PropertyChangeAction action)586 WMError WindowImpl::UpdateProperty(PropertyChangeAction action)
587 {
588 return SingletonContainer::Get<WindowAdapter>().UpdateProperty(property_, action);
589 }
590
Create(const std::string & parentName,const std::shared_ptr<AbilityRuntime::Context> & context)591 WMError WindowImpl::Create(const std::string& parentName, const std::shared_ptr<AbilityRuntime::Context>& context)
592 {
593 WLOGFI("[Client] Window Create");
594 // check window name, same window names are forbidden
595 if (windowMap_.find(name_) != windowMap_.end()) {
596 WLOGFE("WindowName(%{public}s) already exists.", name_.c_str());
597 return WMError::WM_ERROR_INVALID_PARAM;
598 }
599 // check parent name, if create sub window and there is not exist parent Window, then return
600 if (parentName != "") {
601 if (windowMap_.find(parentName) == windowMap_.end()) {
602 WLOGFE("ParentName is empty or valid. ParentName is %{public}s", parentName.c_str());
603 return WMError::WM_ERROR_INVALID_PARAM;
604 } else {
605 uint32_t parentId = windowMap_[parentName].first;
606 property_->SetParentId(parentId);
607 }
608 }
609
610 sptr<WindowImpl> window(this);
611 sptr<IWindow> windowAgent(new WindowAgent(window));
612 uint32_t windowId = 0;
613 WMError ret = SingletonContainer::Get<WindowAdapter>().CreateWindow(windowAgent, property_, surfaceNode_,
614 windowId);
615 property_->SetWindowId(windowId);
616
617 if (ret != WMError::WM_OK) {
618 WLOGFE("create window failed with errCode:%{public}d", static_cast<int32_t>(ret));
619 return ret;
620 }
621 context_ = context;
622 abilityContext_ = AbilityRuntime::Context::ConvertTo<AbilityRuntime::AbilityContext>(context);
623 if (abilityContext_ != nullptr) {
624 ret = SingletonContainer::Get<WindowAdapter>().SaveAbilityToken(abilityContext_->GetToken(), windowId);
625 if (ret != WMError::WM_OK) {
626 WLOGFE("SaveAbilityToken failed with errCode:%{public}d", static_cast<int32_t>(ret));
627 return ret;
628 }
629 }
630 windowMap_.insert(std::make_pair(name_, std::pair<uint32_t, sptr<Window>>(windowId, this)));
631 if (parentName != "") { // add to subWindowMap_
632 subWindowMap_[property_->GetParentId()].push_back(this);
633 }
634
635 MapFloatingWindowToAppIfNeeded();
636
637 state_ = WindowState::STATE_CREATED;
638 InputTransferStation::GetInstance().AddInputWindow(this);
639 return ret;
640 }
641
DestroyFloatingWindow()642 void WindowImpl::DestroyFloatingWindow()
643 {
644 // remove from appFloatingWindowMap_
645 for (auto& floatingWindows: appFloatingWindowMap_) {
646 for (auto iter = floatingWindows.second.begin(); iter != floatingWindows.second.end(); ++iter) {
647 if ((*iter)->GetWindowId() == GetWindowId()) {
648 floatingWindows.second.erase(iter);
649 break;
650 }
651 }
652 }
653
654 // Destroy app floating window if exist
655 if (appFloatingWindowMap_.count(GetWindowId()) > 0) {
656 for (auto& floatingWindow : appFloatingWindowMap_.at(GetWindowId())) {
657 floatingWindow->Destroy();
658 }
659 appFloatingWindowMap_.erase(GetWindowId());
660 }
661 }
662
DestroySubWindow()663 void WindowImpl::DestroySubWindow()
664 {
665 if (subWindowMap_.count(property_->GetParentId()) > 0) { // remove from subWindowMap_
666 std::vector<sptr<WindowImpl>>& subWindows = subWindowMap_.at(property_->GetParentId());
667 for (auto iter = subWindows.begin(); iter < subWindows.end(); ++iter) {
668 if ((*iter)->GetWindowId() == GetWindowId()) {
669 subWindows.erase(iter);
670 break;
671 }
672 }
673 }
674
675 if (subWindowMap_.count(GetWindowId()) > 0) { // remove from subWindowMap_ and windowMap_
676 std::vector<sptr<WindowImpl>>& subWindows = subWindowMap_.at(GetWindowId());
677 for (auto& subWindow : subWindows) {
678 subWindow->Destroy(false);
679 }
680 subWindowMap_[GetWindowId()].clear();
681 subWindowMap_.erase(GetWindowId());
682 }
683 }
684
Destroy()685 WMError WindowImpl::Destroy()
686 {
687 return Destroy(true);
688 }
689
Destroy(bool needNotifyServer)690 WMError WindowImpl::Destroy(bool needNotifyServer)
691 {
692 if (!IsWindowValid()) {
693 return WMError::WM_OK;
694 }
695
696 WLOGFI("[Client] Window %{public}d Destroy", property_->GetWindowId());
697 InputTransferStation::GetInstance().RemoveInputWindow(this);
698 WMError ret = WMError::WM_OK;
699 if (needNotifyServer) {
700 NotifyBeforeDestroy(GetWindowName());
701 if (subWindowMap_.count(GetWindowId()) > 0) {
702 for (auto& subWindow : subWindowMap_.at(GetWindowId())) {
703 NotifyBeforeSubWindowDestroy(subWindow);
704 }
705 }
706 ret = SingletonContainer::Get<WindowAdapter>().DestroyWindow(property_->GetWindowId());
707 if (ret != WMError::WM_OK) {
708 WLOGFE("destroy window failed with errCode:%{public}d", static_cast<int32_t>(ret));
709 return ret;
710 }
711 } else {
712 WLOGFI("Do not need to notify server to destroy window");
713 }
714
715 windowMap_.erase(GetWindowName());
716 DestroySubWindow();
717 DestroyFloatingWindow();
718 {
719 std::lock_guard<std::recursive_mutex> lock(mutex_);
720 state_ = WindowState::STATE_DESTROYED;
721 VsyncStation::GetInstance().RemoveCallback(VsyncStation::CallbackType::CALLBACK_FRAME, callback_);
722 }
723 return ret;
724 }
725
Show(uint32_t reason)726 WMError WindowImpl::Show(uint32_t reason)
727 {
728 WLOGFI("[Client] Window [name:%{public}s, id:%{public}u] Show", name_.c_str(), property_->GetWindowId());
729 if (!IsWindowValid()) {
730 return WMError::WM_ERROR_INVALID_WINDOW;
731 }
732
733 if ((GetWindowFlags() & static_cast<uint32_t>(WindowFlag::WINDOW_FLAG_SHOW_WHEN_LOCKED)) &&
734 WindowHelper::IsSplitWindowMode(GetMode())) {
735 WLOGFE("show when locked window does not support split mode, windowId: %{public}u", property_->GetWindowId());
736 return WMError::WM_ERROR_INVALID_WINDOW;
737 }
738
739 WindowStateChangeReason stateChangeReason = static_cast<WindowStateChangeReason>(reason);
740 if (stateChangeReason == WindowStateChangeReason::KEYGUARD) {
741 state_ = WindowState::STATE_SHOWN;
742 NotifyAfterForeground();
743 return WMError::WM_OK;
744 }
745 if (state_ == WindowState::STATE_SHOWN) {
746 if (property_->GetWindowType() == WindowType::WINDOW_TYPE_DESKTOP) {
747 WLOGFI("desktop window [id:%{public}d] is shown, minimize all app windows", property_->GetWindowId());
748 SingletonContainer::Get<WindowAdapter>().MinimizeAllAppWindows(property_->GetDisplayId());
749 } else {
750 WLOGFI("window is already shown id: %{public}d, raise to top", property_->GetWindowId());
751 SingletonContainer::Get<WindowAdapter>().ProcessPointDown(property_->GetWindowId());
752 }
753 for (auto& listener : lifecycleListeners_) {
754 if (listener != nullptr) {
755 listener->AfterForeground();
756 }
757 }
758 return WMError::WM_OK;
759 }
760 SetDefaultOption();
761 WMError ret = SingletonContainer::Get<WindowAdapter>().AddWindow(property_);
762 if (ret == WMError::WM_OK || ret == WMError::WM_ERROR_DEATH_RECIPIENT) {
763 state_ = WindowState::STATE_SHOWN;
764 NotifyAfterForeground();
765 } else {
766 WLOGFE("show errCode:%{public}d for winId:%{public}d", static_cast<int32_t>(ret), property_->GetWindowId());
767 }
768 return ret;
769 }
770
Hide(uint32_t reason)771 WMError WindowImpl::Hide(uint32_t reason)
772 {
773 WLOGFI("[Client] Window [name:%{public}s, id:%{public}d] Hide", name_.c_str(), property_->GetWindowId());
774 if (!IsWindowValid()) {
775 return WMError::WM_ERROR_INVALID_WINDOW;
776 }
777 WindowStateChangeReason stateChangeReason = static_cast<WindowStateChangeReason>(reason);
778 if (stateChangeReason == WindowStateChangeReason::KEYGUARD) {
779 state_ = WindowState::STATE_FROZEN;
780 NotifyAfterBackground();
781 return WMError::WM_OK;
782 }
783 if (state_ == WindowState::STATE_HIDDEN || state_ == WindowState::STATE_CREATED) {
784 WLOGFI("window is already hidden id: %{public}d", property_->GetWindowId());
785 return WMError::WM_OK;
786 }
787 WMError ret = SingletonContainer::Get<WindowAdapter>().RemoveWindow(property_->GetWindowId());
788 if (ret != WMError::WM_OK) {
789 WLOGFE("hide errCode:%{public}d for winId:%{public}d", static_cast<int32_t>(ret), property_->GetWindowId());
790 return ret;
791 }
792 state_ = WindowState::STATE_HIDDEN;
793 NotifyAfterBackground();
794 return ret;
795 }
796
MoveTo(int32_t x,int32_t y)797 WMError WindowImpl::MoveTo(int32_t x, int32_t y)
798 {
799 if (!IsWindowValid()) {
800 return WMError::WM_ERROR_INVALID_WINDOW;
801 }
802
803 Rect rect = GetRect();
804 Rect moveRect = { x, y, rect.width_, rect.height_ };
805 if (state_ == WindowState::STATE_HIDDEN || state_ == WindowState::STATE_CREATED) {
806 WLOGFI("window is hidden or created! id: %{public}d, oriPos: [%{public}d, %{public}d, "
807 "movePos: [%{public}d, %{public}d]", property_->GetWindowId(), rect.posX_, rect.posY_, x, y);
808 property_->SetWindowRect(moveRect);
809 return WMError::WM_OK;
810 }
811 property_->SetWindowRect(moveRect);
812 property_->SetWindowSizeChangeReason(WindowSizeChangeReason::MOVE);
813 return UpdateProperty(PropertyChangeAction::ACTION_UPDATE_RECT);
814 }
815
Resize(uint32_t width,uint32_t height)816 WMError WindowImpl::Resize(uint32_t width, uint32_t height)
817 {
818 if (!IsWindowValid()) {
819 return WMError::WM_ERROR_INVALID_WINDOW;
820 }
821
822 Rect rect = GetRect();
823 Rect resizeRect = { rect.posX_, rect.posY_, width, height };
824 if (state_ == WindowState::STATE_HIDDEN || state_ == WindowState::STATE_CREATED) {
825 WLOGFI("window is hidden or created! id: %{public}d, oriRect: [%{public}u, %{public}u], "
826 "resizeRect: [%{public}u, %{public}u]", property_->GetWindowId(), rect.width_,
827 rect.height_, width, height);
828 property_->SetWindowRect(resizeRect);
829 return WMError::WM_OK;
830 }
831 property_->SetWindowRect(resizeRect);
832 property_->SetWindowSizeChangeReason(WindowSizeChangeReason::RESIZE);
833 return UpdateProperty(PropertyChangeAction::ACTION_UPDATE_RECT);
834 }
835
SetKeepScreenOn(bool keepScreenOn)836 WMError WindowImpl::SetKeepScreenOn(bool keepScreenOn)
837 {
838 if (!IsWindowValid()) {
839 return WMError::WM_ERROR_INVALID_WINDOW;
840 }
841 property_->SetKeepScreenOn(keepScreenOn);
842 if (state_ == WindowState::STATE_SHOWN) {
843 return UpdateProperty(PropertyChangeAction::ACTION_UPDATE_KEEP_SCREEN_ON);
844 }
845 return WMError::WM_OK;
846 }
847
IsKeepScreenOn() const848 bool WindowImpl::IsKeepScreenOn() const
849 {
850 return property_->IsKeepScreenOn();
851 }
852
SetTurnScreenOn(bool turnScreenOn)853 WMError WindowImpl::SetTurnScreenOn(bool turnScreenOn)
854 {
855 if (!IsWindowValid()) {
856 return WMError::WM_ERROR_INVALID_WINDOW;
857 }
858 property_->SetTurnScreenOn(turnScreenOn);
859 if (state_ == WindowState::STATE_SHOWN) {
860 return UpdateProperty(PropertyChangeAction::ACTION_UPDATE_TURN_SCREEN_ON);
861 }
862 return WMError::WM_OK;
863 }
864
IsTurnScreenOn() const865 bool WindowImpl::IsTurnScreenOn() const
866 {
867 return property_->IsTurnScreenOn();
868 }
869
SetBackgroundColor(uint32_t color)870 WMError WindowImpl::SetBackgroundColor(uint32_t color)
871 {
872 if (uiContent_ != nullptr) {
873 uiContent_->SetBackgroundColor(color);
874 return WMError::WM_OK;
875 }
876 WLOGFI("uiContent is nullptr, windowId: %{public}u, use FA mode", GetWindowId());
877 if (aceAbilityHandler_ != nullptr) {
878 aceAbilityHandler_->SetBackgroundColor(color);
879 return WMError::WM_OK;
880 }
881 WLOGFE("FA mode could not set background color: %{public}u", GetWindowId());
882 return WMError::WM_ERROR_INVALID_OPERATION;
883 }
884
GetBackgroundColor() const885 uint32_t WindowImpl::GetBackgroundColor() const
886 {
887 if (uiContent_ != nullptr) {
888 return uiContent_->GetBackgroundColor();
889 }
890 WLOGFI("uiContent is nullptr, windowId: %{public}u, use FA mode", GetWindowId());
891 if (aceAbilityHandler_ != nullptr) {
892 return aceAbilityHandler_->GetBackgroundColor();
893 }
894 WLOGFE("FA mode does not get background color: %{public}u", GetWindowId());
895 return 0xffffffff; // means no background color been set, default color is white
896 }
897
SetBackgroundColor(const std::string & color)898 WMError WindowImpl::SetBackgroundColor(const std::string& color)
899 {
900 if (!IsWindowValid()) {
901 return WMError::WM_ERROR_INVALID_WINDOW;
902 }
903 uint32_t colorValue;
904 if (ColorParser::Parse(color, colorValue)) {
905 return SetBackgroundColor(colorValue);
906 }
907 WLOGFE("invalid color string: %{public}s", color.c_str());
908 return WMError::WM_ERROR_INVALID_PARAM;
909 }
910
SetTransparent(bool isTransparent)911 WMError WindowImpl::SetTransparent(bool isTransparent)
912 {
913 if (!IsWindowValid()) {
914 return WMError::WM_ERROR_INVALID_WINDOW;
915 }
916 ColorParam backgroundColor;
917 backgroundColor.value = GetBackgroundColor();
918 if (isTransparent) {
919 backgroundColor.argb.alpha = 0x00; // 0x00: completely transparent
920 return SetBackgroundColor(backgroundColor.value);
921 } else {
922 backgroundColor.value = GetBackgroundColor();
923 if (backgroundColor.argb.alpha == 0x00) {
924 backgroundColor.argb.alpha = 0xff; // 0xff: completely opaque
925 return SetBackgroundColor(backgroundColor.value);
926 }
927 }
928 return WMError::WM_OK;
929 }
930
IsTransparent() const931 bool WindowImpl::IsTransparent() const
932 {
933 ColorParam backgroundColor;
934 backgroundColor.value = GetBackgroundColor();
935 WLOGFI("color: %{public}u, alpha: %{public}u", backgroundColor.value, backgroundColor.argb.alpha);
936 return backgroundColor.argb.alpha == 0x00; // 0x00: completely transparent
937 }
938
SetBrightness(float brightness)939 WMError WindowImpl::SetBrightness(float brightness)
940 {
941 if (!IsWindowValid()) {
942 return WMError::WM_ERROR_INVALID_WINDOW;
943 }
944 if (brightness < MINIMUM_BRIGHTNESS || brightness > MAXIMUM_BRIGHTNESS) {
945 WLOGFE("invalid brightness value: %{public}f", brightness);
946 return WMError::WM_ERROR_INVALID_PARAM;
947 }
948 if (!WindowHelper::IsAppWindow(GetType())) {
949 WLOGFE("non app window does not support set brightness, type: %{public}u", GetType());
950 return WMError::WM_ERROR_INVALID_TYPE;
951 }
952 property_->SetBrightness(brightness);
953 if (state_ == WindowState::STATE_SHOWN) {
954 return UpdateProperty(PropertyChangeAction::ACTION_UPDATE_SET_BRIGHTNESS);
955 }
956 return WMError::WM_OK;
957 }
958
GetBrightness() const959 float WindowImpl::GetBrightness() const
960 {
961 return property_->GetBrightness();
962 }
963
SetCallingWindow(uint32_t windowId)964 WMError WindowImpl::SetCallingWindow(uint32_t windowId)
965 {
966 if (!IsWindowValid()) {
967 return WMError::WM_ERROR_INVALID_WINDOW;
968 }
969 property_->SetCallingWindow(windowId);
970 return UpdateProperty(PropertyChangeAction::ACTION_UPDATE_CALLING_WINDOW);
971 }
972
SetPrivacyMode(bool isPrivacyMode)973 void WindowImpl::SetPrivacyMode(bool isPrivacyMode)
974 {
975 property_->SetPrivacyMode(isPrivacyMode);
976 surfaceNode_->SetSecurityLayer(isPrivacyMode);
977 }
978
IsPrivacyMode() const979 bool WindowImpl::IsPrivacyMode() const
980 {
981 return property_->GetPrivacyMode();
982 }
983
Drag(const Rect & rect)984 WMError WindowImpl::Drag(const Rect& rect)
985 {
986 if (!IsWindowValid()) {
987 return WMError::WM_ERROR_INVALID_WINDOW;
988 }
989 property_->SetWindowRect(rect);
990 property_->SetWindowSizeChangeReason(WindowSizeChangeReason::DRAG);
991 return UpdateProperty(PropertyChangeAction::ACTION_UPDATE_RECT);
992 }
993
IsDecorEnable() const994 bool WindowImpl::IsDecorEnable() const
995 {
996 return property_->GetDecorEnable();
997 }
998
Maximize()999 WMError WindowImpl::Maximize()
1000 {
1001 WLOGFI("[Client] Window %{public}d Maximize", property_->GetWindowId());
1002 if (!IsWindowValid()) {
1003 return WMError::WM_ERROR_INVALID_WINDOW;
1004 }
1005 if (WindowHelper::IsMainWindow(property_->GetWindowType())) {
1006 WMError ret = SingletonContainer::Get<WindowAdapter>().MaxmizeWindow(property_->GetWindowId());
1007 if (ret == WMError::WM_OK) {
1008 UpdateMode(WindowMode::WINDOW_MODE_FULLSCREEN);
1009 }
1010 return ret;
1011 } else {
1012 WLOGFI("Maximize Window failed. The window is not main window");
1013 return WMError::WM_ERROR_INVALID_PARAM;
1014 }
1015 }
1016
Minimize()1017 WMError WindowImpl::Minimize()
1018 {
1019 WLOGFI("[Client] Window %{public}d Minimize", property_->GetWindowId());
1020 if (!IsWindowValid()) {
1021 return WMError::WM_ERROR_INVALID_WINDOW;
1022 }
1023 if (WindowHelper::IsMainWindow(property_->GetWindowType())) {
1024 if (abilityContext_ != nullptr) {
1025 AAFwk::AbilityManagerClient::GetInstance()->MinimizeAbility(abilityContext_->GetToken(), true);
1026 } else {
1027 Hide();
1028 }
1029 }
1030 return WMError::WM_OK;
1031 }
1032
Recover()1033 WMError WindowImpl::Recover()
1034 {
1035 WLOGFI("[Client] Window %{public}d Normalize", property_->GetWindowId());
1036 if (!IsWindowValid()) {
1037 return WMError::WM_ERROR_INVALID_WINDOW;
1038 }
1039 if (WindowHelper::IsMainWindow(property_->GetWindowType())) {
1040 SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
1041 }
1042 return WMError::WM_OK;
1043 }
1044
Close()1045 WMError WindowImpl::Close()
1046 {
1047 WLOGFI("[Client] Window %{public}d Close", property_->GetWindowId());
1048 if (!IsWindowValid()) {
1049 return WMError::WM_ERROR_INVALID_WINDOW;
1050 }
1051 if (WindowHelper::IsMainWindow(property_->GetWindowType())) {
1052 if (abilityContext_ != nullptr) {
1053 abilityContext_->CloseAbility();
1054 } else {
1055 Destroy();
1056 }
1057 }
1058 return WMError::WM_OK;
1059 }
1060
StartMove()1061 void WindowImpl::StartMove()
1062 {
1063 if (!WindowHelper::IsMainFloatingWindow(GetType(), GetMode())) {
1064 WLOGFI("[StartMove] current window can not be moved, windowId %{public}u", GetWindowId());
1065 return;
1066 }
1067 startMoveFlag_ = true;
1068 WLOGFI("[StartMove] windowId %{public}u", GetWindowId());
1069 }
1070
RequestFocus() const1071 WMError WindowImpl::RequestFocus() const
1072 {
1073 if (!IsWindowValid()) {
1074 return WMError::WM_ERROR_INVALID_WINDOW;
1075 }
1076 return SingletonContainer::Get<WindowAdapter>().RequestFocus(property_->GetWindowId());
1077 }
1078
AddInputEventListener(const std::shared_ptr<MMI::IInputEventConsumer> & inputEventListener)1079 void WindowImpl::AddInputEventListener(const std::shared_ptr<MMI::IInputEventConsumer>& inputEventListener)
1080 {
1081 InputTransferStation::GetInstance().SetInputListener(GetWindowId(), inputEventListener);
1082 }
1083
RegisterLifeCycleListener(sptr<IWindowLifeCycle> & listener)1084 void WindowImpl::RegisterLifeCycleListener(sptr<IWindowLifeCycle>& listener)
1085 {
1086 if (listener == nullptr) {
1087 return;
1088 }
1089 std::lock_guard<std::recursive_mutex> lock(mutex_);
1090 if (std::find(lifecycleListeners_.begin(), lifecycleListeners_.end(), listener) != lifecycleListeners_.end()) {
1091 WLOGFE("Listener already registered");
1092 return;
1093 }
1094 lifecycleListeners_.emplace_back(listener);
1095 }
1096
RegisterWindowChangeListener(sptr<IWindowChangeListener> & listener)1097 void WindowImpl::RegisterWindowChangeListener(sptr<IWindowChangeListener>& listener)
1098 {
1099 if (listener == nullptr) {
1100 return;
1101 }
1102 std::lock_guard<std::recursive_mutex> lock(mutex_);
1103 if (std::find(windowChangeListeners_.begin(), windowChangeListeners_.end(), listener) !=
1104 windowChangeListeners_.end()) {
1105 WLOGFE("Listener already registered");
1106 return;
1107 }
1108 windowChangeListeners_.emplace_back(listener);
1109 }
1110
UnregisterLifeCycleListener(sptr<IWindowLifeCycle> & listener)1111 void WindowImpl::UnregisterLifeCycleListener(sptr<IWindowLifeCycle>& listener)
1112 {
1113 std::lock_guard<std::recursive_mutex> lock(mutex_);
1114 lifecycleListeners_.erase(std::remove_if(lifecycleListeners_.begin(), lifecycleListeners_.end(),
1115 [listener](sptr<IWindowLifeCycle> registeredListener) {
1116 return registeredListener == listener;
1117 }), lifecycleListeners_.end());
1118 }
1119
UnregisterWindowChangeListener(sptr<IWindowChangeListener> & listener)1120 void WindowImpl::UnregisterWindowChangeListener(sptr<IWindowChangeListener>& listener)
1121 {
1122 std::lock_guard<std::recursive_mutex> lock(mutex_);
1123 windowChangeListeners_.erase(std::remove_if(windowChangeListeners_.begin(), windowChangeListeners_.end(),
1124 [listener](sptr<IWindowChangeListener> registeredListener) {
1125 return registeredListener == listener;
1126 }), windowChangeListeners_.end());
1127 }
1128
RegisterAvoidAreaChangeListener(sptr<IAvoidAreaChangedListener> & listener)1129 void WindowImpl::RegisterAvoidAreaChangeListener(sptr<IAvoidAreaChangedListener>& listener)
1130 {
1131 if (listener == nullptr) {
1132 WLOGFE("RegisterAvoidAreaChangeListener failed. AvoidAreaChangeListener is nullptr");
1133 return;
1134 }
1135 std::lock_guard<std::recursive_mutex> lock(mutex_);
1136 if (std::find(avoidAreaChangeListeners_.begin(), avoidAreaChangeListeners_.end(), listener) !=
1137 avoidAreaChangeListeners_.end()) {
1138 WLOGFE("Listener already registered");
1139 return;
1140 }
1141 avoidAreaChangeListeners_.emplace_back(listener);
1142 }
1143
UnregisterAvoidAreaChangeListener(sptr<IAvoidAreaChangedListener> & listener)1144 void WindowImpl::UnregisterAvoidAreaChangeListener(sptr<IAvoidAreaChangedListener>& listener)
1145 {
1146 std::lock_guard<std::recursive_mutex> lock(mutex_);
1147 avoidAreaChangeListeners_.erase(std::remove_if(avoidAreaChangeListeners_.begin(), avoidAreaChangeListeners_.end(),
1148 [listener](sptr<IAvoidAreaChangedListener> registeredListener) {
1149 return registeredListener == listener;
1150 }), avoidAreaChangeListeners_.end());
1151 }
1152
RegisterDragListener(const sptr<IWindowDragListener> & listener)1153 void WindowImpl::RegisterDragListener(const sptr<IWindowDragListener>& listener)
1154 {
1155 if (listener == nullptr) {
1156 return;
1157 }
1158 std::lock_guard<std::recursive_mutex> lock(mutex_);
1159 if (std::find(windowDragListeners_.begin(), windowDragListeners_.end(), listener) != windowDragListeners_.end()) {
1160 WLOGFE("Listener already registered");
1161 return;
1162 }
1163 windowDragListeners_.emplace_back(listener);
1164 }
1165
UnregisterDragListener(const sptr<IWindowDragListener> & listener)1166 void WindowImpl::UnregisterDragListener(const sptr<IWindowDragListener>& listener)
1167 {
1168 std::lock_guard<std::recursive_mutex> lock(mutex_);
1169 auto iter = std::find(windowDragListeners_.begin(), windowDragListeners_.end(), listener);
1170 if (iter == windowDragListeners_.end()) {
1171 WLOGFE("could not find this listener");
1172 return;
1173 }
1174 windowDragListeners_.erase(iter);
1175 }
1176
RegisterDisplayMoveListener(sptr<IDisplayMoveListener> & listener)1177 void WindowImpl::RegisterDisplayMoveListener(sptr<IDisplayMoveListener>& listener)
1178 {
1179 if (listener == nullptr) {
1180 return;
1181 }
1182 std::lock_guard<std::recursive_mutex> lock(mutex_);
1183 if (std::find(displayMoveListeners_.begin(), displayMoveListeners_.end(), listener) !=
1184 displayMoveListeners_.end()) {
1185 WLOGFE("Listener already registered");
1186 return;
1187 }
1188 displayMoveListeners_.emplace_back(listener);
1189 }
1190
UnregisterDisplayMoveListener(sptr<IDisplayMoveListener> & listener)1191 void WindowImpl::UnregisterDisplayMoveListener(sptr<IDisplayMoveListener>& listener)
1192 {
1193 std::lock_guard<std::recursive_mutex> lock(mutex_);
1194 auto iter = std::find(displayMoveListeners_.begin(), displayMoveListeners_.end(), listener);
1195 if (iter == displayMoveListeners_.end()) {
1196 WLOGFE("could not find the listener");
1197 return;
1198 }
1199 displayMoveListeners_.erase(iter);
1200 }
1201
RegisterWindowDestroyedListener(const NotifyNativeWinDestroyFunc & func)1202 void WindowImpl::RegisterWindowDestroyedListener(const NotifyNativeWinDestroyFunc& func)
1203 {
1204 WLOGFI("JS RegisterWindowDestroyedListener the listener");
1205 notifyNativefunc_ = std::move(func);
1206 }
1207
RegisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener> & listener)1208 void WindowImpl::RegisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener>& listener)
1209 {
1210 if (listener == nullptr) {
1211 WLOGFE(" listener is nullptr");
1212 return;
1213 }
1214 std::lock_guard<std::recursive_mutex> lock(mutex_);
1215 if (std::find(occupiedAreaChangeListeners_.begin(), occupiedAreaChangeListeners_.end(), listener) !=
1216 occupiedAreaChangeListeners_.end()) {
1217 WLOGFE("Listener already registered");
1218 return;
1219 }
1220 occupiedAreaChangeListeners_.emplace_back(listener);
1221 }
1222
UnregisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener> & listener)1223 void WindowImpl::UnregisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener>& listener)
1224 {
1225 std::lock_guard<std::recursive_mutex> lock(mutex_);
1226 occupiedAreaChangeListeners_.erase(std::remove_if(occupiedAreaChangeListeners_.begin(),
1227 occupiedAreaChangeListeners_.end(), [listener](sptr<IOccupiedAreaChangeListener> registeredListener) {
1228 return registeredListener == listener;
1229 }), occupiedAreaChangeListeners_.end());
1230 }
1231
SetAceAbilityHandler(const sptr<IAceAbilityHandler> & handler)1232 void WindowImpl::SetAceAbilityHandler(const sptr<IAceAbilityHandler>& handler)
1233 {
1234 if (handler == nullptr) {
1235 WLOGFI("ace ability handler is nullptr");
1236 }
1237 std::lock_guard<std::recursive_mutex> lock(mutex_);
1238 aceAbilityHandler_ = handler;
1239 }
1240
UpdateRect(const struct Rect & rect,WindowSizeChangeReason reason)1241 void WindowImpl::UpdateRect(const struct Rect& rect, WindowSizeChangeReason reason)
1242 {
1243 auto display = DisplayManager::GetInstance().GetDisplayById(property_->GetDisplayId());
1244 if (display == nullptr) {
1245 WLOGFE("get display failed displayId:%{public}" PRIu64", window id:%{public}u", property_->GetDisplayId(),
1246 property_->GetWindowId());
1247 return;
1248 }
1249 float virtualPixelRatio = display->GetVirtualPixelRatio();
1250 WLOGFI("winId:%{public}u, rect[%{public}d, %{public}d, %{public}u, %{public}u], vpr:%{public}f, reason:%{public}u",
1251 GetWindowId(), rect.posX_, rect.posY_, rect.width_, rect.height_, virtualPixelRatio, reason);
1252 property_->SetWindowRect(rect);
1253 WLOGFI("sizeChange callback size: %{public}u", windowChangeListeners_.size());
1254 for (auto& listener : windowChangeListeners_) {
1255 if (listener != nullptr) {
1256 listener->OnSizeChange(rect, reason);
1257 }
1258 }
1259
1260 if (uiContent_ != nullptr) {
1261 Ace::ViewportConfig config;
1262 config.SetSize(rect.width_, rect.height_);
1263 config.SetPosition(rect.posX_, rect.posY_);
1264 config.SetDensity(virtualPixelRatio);
1265 uiContent_->UpdateViewportConfig(config, reason);
1266 WLOGFI("notify uiContent window size change end");
1267 }
1268 }
1269
UpdateMode(WindowMode mode)1270 void WindowImpl::UpdateMode(WindowMode mode)
1271 {
1272 WLOGI("UpdateMode %{public}d", mode);
1273 property_->SetWindowMode(mode);
1274 for (auto& listener : windowChangeListeners_) {
1275 if (listener != nullptr) {
1276 listener->OnModeChange(mode);
1277 }
1278 }
1279
1280 if (uiContent_ != nullptr) {
1281 uiContent_->UpdateWindowMode(mode);
1282 WLOGFI("notify uiContent window mode change end");
1283 }
1284 }
1285
ConsumeKeyEvent(std::shared_ptr<MMI::KeyEvent> & keyEvent)1286 void WindowImpl::ConsumeKeyEvent(std::shared_ptr<MMI::KeyEvent>& keyEvent)
1287 {
1288 int32_t keyCode = keyEvent->GetKeyCode();
1289 int32_t keyAction = keyEvent->GetKeyAction();
1290 WLOGFI("ConsumeKeyEvent: enter GetKeyCode: %{public}d, action: %{public}d", keyCode, keyAction);
1291 if (keyCode == MMI::KeyEvent::KEYCODE_BACK) {
1292 if (keyAction != MMI::KeyEvent::KEY_ACTION_UP) {
1293 return;
1294 }
1295 if (uiContent_ != nullptr && uiContent_->ProcessBackPressed()) {
1296 WLOGI("ConsumeKeyEvent keyEvent is consumed");
1297 return;
1298 }
1299 if (abilityContext_ != nullptr) {
1300 WLOGI("ConsumeKeyEvent ability TerminateSelf");
1301 abilityContext_->TerminateSelf();
1302 } else {
1303 WLOGI("ConsumeKeyEvent destroy window");
1304 Destroy();
1305 }
1306 } else {
1307 if (uiContent_ == nullptr) {
1308 WLOGE("ConsumeKeyEvent uiContent is nullptr");
1309 return;
1310 }
1311 if (!uiContent_->ProcessKeyEvent(keyEvent)) {
1312 WLOGI("ConsumeKeyEvent no consumer window exit");
1313 }
1314 }
1315 }
1316
HandleMoveEvent(int32_t posX,int32_t posY,int32_t pointId)1317 void WindowImpl::HandleMoveEvent(int32_t posX, int32_t posY, int32_t pointId)
1318 {
1319 if (!startMoveFlag_ || (pointId != startPointerId_)) {
1320 return;
1321 }
1322 int32_t targetX = startPointRect_.posX_ + (posX - startPointPosX_);
1323 int32_t targetY = startPointRect_.posY_ + (posY - startPointPosY_);
1324 auto res = MoveTo(targetX, targetY);
1325 if (res != WMError::WM_OK) {
1326 WLOGFE("move window: %{public}u failed", GetWindowId());
1327 }
1328 }
1329
HandleDragEvent(int32_t posX,int32_t posY,int32_t pointId)1330 void WindowImpl::HandleDragEvent(int32_t posX, int32_t posY, int32_t pointId)
1331 {
1332 if (!startDragFlag_ || (pointId != startPointerId_)) {
1333 return;
1334 }
1335 int32_t diffX = posX - startPointPosX_;
1336 int32_t diffY = posY - startPointPosY_;
1337 Rect newRect = startPointRect_;
1338
1339 Rect hotZoneRect;
1340 if ((startPointPosX_ > startRectExceptCorner_.posX_ &&
1341 (startPointPosX_ < startRectExceptCorner_.posX_ + static_cast<int32_t>(startRectExceptCorner_.width_))) &&
1342 (startPointPosY_ > startRectExceptCorner_.posY_ &&
1343 (startPointPosY_ < startRectExceptCorner_.posY_ + static_cast<int32_t>(startRectExceptCorner_.height_)))) {
1344 hotZoneRect = startRectExceptFrame_; // drag type: left/right/top/bottom
1345 } else {
1346 hotZoneRect = startRectExceptCorner_; // drag type: left_top/right_top/left_bottom/right_bottom
1347 }
1348
1349 if (startPointPosX_ <= hotZoneRect.posX_) {
1350 if (diffX > static_cast<int32_t>(startPointRect_.width_)) {
1351 diffX = static_cast<int32_t>(startPointRect_.width_);
1352 }
1353 newRect.posX_ += diffX;
1354 newRect.width_ = static_cast<uint32_t>(static_cast<int32_t>(newRect.width_) - diffX);
1355 } else if (startPointPosX_ >= hotZoneRect.posX_ + static_cast<int32_t>(hotZoneRect.width_)) {
1356 if (diffX < 0 && (-diffX > static_cast<int32_t>(startPointRect_.width_))) {
1357 diffX = -(static_cast<int32_t>(startPointRect_.width_));
1358 }
1359 newRect.width_ = static_cast<uint32_t>(static_cast<int32_t>(newRect.width_) + diffX);
1360 }
1361 if (startPointPosY_ <= hotZoneRect.posY_) {
1362 if (diffY > static_cast<int32_t>(startPointRect_.height_)) {
1363 diffY = static_cast<int32_t>(startPointRect_.height_);
1364 }
1365 newRect.posY_ += diffY;
1366 newRect.height_ = static_cast<uint32_t>(static_cast<int32_t>(newRect.height_) - diffY);
1367 } else if (startPointPosY_ >= hotZoneRect.posY_ + static_cast<int32_t>(hotZoneRect.height_)) {
1368 if (diffY < 0 && (-diffY > static_cast<int32_t>(startPointRect_.height_))) {
1369 diffY = -(static_cast<int32_t>(startPointRect_.height_));
1370 }
1371 newRect.height_ = static_cast<uint32_t>(static_cast<int32_t>(newRect.height_) + diffY);
1372 }
1373 auto res = Drag(newRect);
1374 if (res != WMError::WM_OK) {
1375 WLOGFE("drag window: %{public}u failed", GetWindowId());
1376 }
1377 }
1378
EndMoveOrDragWindow(int32_t pointId)1379 void WindowImpl::EndMoveOrDragWindow(int32_t pointId)
1380 {
1381 if (pointId != startPointerId_) {
1382 return;
1383 }
1384
1385 if (startDragFlag_) {
1386 SingletonContainer::Get<WindowAdapter>().ProcessPointUp(GetWindowId());
1387 startDragFlag_ = false;
1388 }
1389
1390 if (startMoveFlag_) {
1391 if (GetType() == WindowType::WINDOW_TYPE_DOCK_SLICE) {
1392 SingletonContainer::Get<WindowAdapter>().ProcessPointUp(GetWindowId());
1393 }
1394 startMoveFlag_ = false;
1395 }
1396 pointEventStarted_ = false;
1397 }
1398
ReadyToMoveOrDragWindow(int32_t globalX,int32_t globalY,int32_t pointId,const Rect & rect)1399 void WindowImpl::ReadyToMoveOrDragWindow(int32_t globalX, int32_t globalY, int32_t pointId, const Rect& rect)
1400 {
1401 if (pointEventStarted_) {
1402 return;
1403 }
1404 startPointRect_ = rect;
1405 startPointPosX_ = globalX;
1406 startPointPosY_ = globalY;
1407 startPointerId_ = pointId;
1408 pointEventStarted_ = true;
1409
1410 // calculate window inner rect except frame
1411 auto display = DisplayManager::GetInstance().GetDisplayById(property_->GetDisplayId());
1412 if (display == nullptr) {
1413 WLOGFE("get display failed displayId:%{public}" PRIu64", window id:%{public}u", property_->GetDisplayId(),
1414 property_->GetWindowId());
1415 return;
1416 }
1417 float virtualPixelRatio = display->GetVirtualPixelRatio();
1418
1419 startRectExceptFrame_.posX_ = startPointRect_.posX_ +
1420 static_cast<int32_t>(WINDOW_FRAME_WIDTH * virtualPixelRatio);
1421 startRectExceptFrame_.posY_ = startPointRect_.posY_ +
1422 static_cast<int32_t>(WINDOW_FRAME_WIDTH * virtualPixelRatio);
1423 startRectExceptFrame_.width_ = startPointRect_.width_ -
1424 static_cast<uint32_t>((WINDOW_FRAME_WIDTH + WINDOW_FRAME_WIDTH) * virtualPixelRatio);
1425 startRectExceptFrame_.height_ = startPointRect_.height_ -
1426 static_cast<uint32_t>((WINDOW_FRAME_WIDTH + WINDOW_FRAME_WIDTH) * virtualPixelRatio);
1427
1428 startRectExceptCorner_.posX_ = startPointRect_.posX_ +
1429 static_cast<int32_t>(WINDOW_FRAME_CORNER_WIDTH * virtualPixelRatio);
1430 startRectExceptCorner_.posY_ = startPointRect_.posY_ +
1431 static_cast<int32_t>(WINDOW_FRAME_CORNER_WIDTH * virtualPixelRatio);
1432 startRectExceptCorner_.width_ = startPointRect_.width_ -
1433 static_cast<uint32_t>((WINDOW_FRAME_CORNER_WIDTH + WINDOW_FRAME_CORNER_WIDTH) * virtualPixelRatio);
1434 startRectExceptCorner_.height_ = startPointRect_.height_ -
1435 static_cast<uint32_t>((WINDOW_FRAME_CORNER_WIDTH + WINDOW_FRAME_CORNER_WIDTH) * virtualPixelRatio);
1436
1437 if (GetType() == WindowType::WINDOW_TYPE_DOCK_SLICE) {
1438 startMoveFlag_ = true;
1439 SingletonContainer::Get<WindowAdapter>().ProcessPointDown(property_->GetWindowId(), true);
1440 } else if (!WindowHelper::IsPointInTargetRect(startPointPosX_, startPointPosY_, startRectExceptFrame_) ||
1441 (WindowHelper::IsPointInTargetRect(startPointPosX_, startPointPosY_, startRectExceptFrame_) &&
1442 (!WindowHelper::IsPointInWindowExceptCorner(startPointPosX_, startPointPosY_, startRectExceptCorner_)))) {
1443 startDragFlag_ = true;
1444 SingletonContainer::Get<WindowAdapter>().ProcessPointDown(property_->GetWindowId(), true);
1445 }
1446 return;
1447 }
1448
ConsumeMoveOrDragEvent(std::shared_ptr<MMI::PointerEvent> & pointerEvent)1449 void WindowImpl::ConsumeMoveOrDragEvent(std::shared_ptr<MMI::PointerEvent>& pointerEvent)
1450 {
1451 MMI::PointerEvent::PointerItem pointerItem;
1452 int32_t pointId = pointerEvent->GetPointerId();
1453 if (!pointerEvent->GetPointerItem(pointId, pointerItem)) {
1454 WLOGFW("Point item is invalid");
1455 return;
1456 }
1457 int32_t pointGlobalX = pointerItem.GetGlobalX();
1458 int32_t pointGlobalY = pointerItem.GetGlobalY();
1459 int32_t action = pointerEvent->GetPointerAction();
1460 switch (action) {
1461 // Ready to move or drag
1462 case MMI::PointerEvent::POINTER_ACTION_DOWN:
1463 case MMI::PointerEvent::POINTER_ACTION_BUTTON_DOWN: {
1464 Rect rect = GetRect();
1465 ReadyToMoveOrDragWindow(pointGlobalX, pointGlobalY, pointId, rect);
1466 WLOGFI("[Point Down]: windowId: %{public}u, action: %{public}d, hasPointStarted: %{public}d, "
1467 "startMove: %{public}d, startDrag: %{public}d, pointPos: [%{public}d, %{public}d], "
1468 "winRect: [%{public}d, %{public}d, %{public}u, %{public}u]",
1469 GetWindowId(), action, pointEventStarted_, startMoveFlag_, startDragFlag_,
1470 pointGlobalX, pointGlobalY, rect.posX_, rect.posY_, rect.width_, rect.height_);
1471 break;
1472 }
1473 // Start to move or darg
1474 case MMI::PointerEvent::POINTER_ACTION_MOVE: {
1475 HandleMoveEvent(pointGlobalX, pointGlobalY, pointId);
1476 HandleDragEvent(pointGlobalX, pointGlobalY, pointId);
1477 break;
1478 }
1479 // End move or drag
1480 case MMI::PointerEvent::POINTER_ACTION_UP:
1481 case MMI::PointerEvent::POINTER_ACTION_BUTTON_UP:
1482 case MMI::PointerEvent::POINTER_ACTION_CANCEL: {
1483 EndMoveOrDragWindow(pointId);
1484 WLOGFI("[Point Up/Cancel]: windowId: %{public}u, action: %{public}d, startMove: %{public}d, "
1485 "startDrag: %{public}d", GetWindowId(), action, startMoveFlag_, startDragFlag_);
1486 break;
1487 }
1488 default:
1489 break;
1490 }
1491 }
1492
IsPointerEventConsumed()1493 bool WindowImpl::IsPointerEventConsumed()
1494 {
1495 return startDragFlag_ || startMoveFlag_;
1496 }
1497
AdjustWindowAnimationFlag()1498 void WindowImpl::AdjustWindowAnimationFlag()
1499 {
1500 WindowType winType = property_->GetWindowType();
1501 if (!WindowHelper::IsAppWindow(winType)) {
1502 property_->SetAnimationFlag(static_cast<uint32_t>(WindowAnimation::NONE));
1503 }
1504 }
1505
ConsumePointerEvent(std::shared_ptr<MMI::PointerEvent> & pointerEvent)1506 void WindowImpl::ConsumePointerEvent(std::shared_ptr<MMI::PointerEvent>& pointerEvent)
1507 {
1508 int32_t action = pointerEvent->GetPointerAction();
1509 if (action == MMI::PointerEvent::POINTER_ACTION_DOWN || action == MMI::PointerEvent::POINTER_ACTION_BUTTON_DOWN) {
1510 WLOGI("WMS process point down, window: [name:%{public}s, id:%{public}u], action: %{public}d",
1511 name_.c_str(), GetWindowId(), action);
1512 if (GetType() == WindowType::WINDOW_TYPE_LAUNCHER_RECENT) {
1513 MMI::PointerEvent::PointerItem pointerItem;
1514 if (!pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointerItem)) {
1515 WLOGFW("Point item is invalid");
1516 return;
1517 }
1518 if (!WindowHelper::IsPointInTargetRect(pointerItem.GetGlobalX(), pointerItem.GetGlobalY(), GetRect())) {
1519 NotifyListenerAfterUnfocused();
1520 return;
1521 }
1522 }
1523 SingletonContainer::Get<WindowAdapter>().ProcessPointDown(property_->GetWindowId());
1524 }
1525
1526 if (WindowHelper::IsMainFloatingWindow(GetType(), GetMode()) ||
1527 GetType() == WindowType::WINDOW_TYPE_DOCK_SLICE) {
1528 ConsumeMoveOrDragEvent(pointerEvent);
1529 }
1530
1531 if (IsPointerEventConsumed()) {
1532 return;
1533 }
1534 if (uiContent_ == nullptr) {
1535 WLOGE("ConsumePointerEvent uiContent is nullptr, windowId: %{public}u", GetWindowId());
1536 return;
1537 }
1538 WLOGFI("Transfer pointer event to ACE");
1539 uiContent_->ProcessPointerEvent(pointerEvent);
1540 }
1541
OnVsync(int64_t timeStamp)1542 void WindowImpl::OnVsync(int64_t timeStamp)
1543 {
1544 uiContent_->ProcessVsyncEvent(static_cast<uint64_t>(timeStamp));
1545 }
1546
RequestFrame()1547 void WindowImpl::RequestFrame()
1548 {
1549 std::lock_guard<std::recursive_mutex> lock(mutex_);
1550 if (state_ == WindowState::STATE_DESTROYED) {
1551 WLOGFE("RequestFrame failed, window is destroyed");
1552 return;
1553 }
1554 VsyncStation::GetInstance().RequestVsync(VsyncStation::CallbackType::CALLBACK_FRAME, callback_);
1555 }
1556
UpdateFocusStatus(bool focused)1557 void WindowImpl::UpdateFocusStatus(bool focused)
1558 {
1559 WLOGFI("window focus status: %{public}d, id: %{public}d", focused, property_->GetWindowId());
1560 if (focused) {
1561 NotifyAfterFocused();
1562 } else {
1563 NotifyAfterUnfocused();
1564 }
1565 }
1566
UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration> & configuration)1567 void WindowImpl::UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
1568 {
1569 if (uiContent_ != nullptr) {
1570 WLOGFD("notify ace winId:%{public}d", GetWindowId());
1571 uiContent_->UpdateConfiguration(configuration);
1572 }
1573 if (subWindowMap_.count(GetWindowId()) == 0) {
1574 return;
1575 }
1576 for (auto& subWindow : subWindowMap_.at(GetWindowId())) {
1577 subWindow->UpdateConfiguration(configuration);
1578 }
1579 }
1580
UpdateAvoidArea(const std::vector<Rect> & avoidArea)1581 void WindowImpl::UpdateAvoidArea(const std::vector<Rect>& avoidArea)
1582 {
1583 WLOGFI("Window Update AvoidArea, id: %{public}d", property_->GetWindowId());
1584 for (auto& listener : avoidAreaChangeListeners_) {
1585 if (listener != nullptr) {
1586 listener->OnAvoidAreaChanged(avoidArea);
1587 }
1588 }
1589 }
1590
UpdateWindowState(WindowState state)1591 void WindowImpl::UpdateWindowState(WindowState state)
1592 {
1593 WLOGFI("[Client] Window %{public}u, WindowState to set:%{public}u", GetWindowId(), state);
1594 if (!IsWindowValid()) {
1595 return;
1596 }
1597 switch (state) {
1598 case WindowState::STATE_FROZEN: {
1599 if (abilityContext_ != nullptr && windowTag_ == WindowTag::MAIN_WINDOW) {
1600 WLOGFD("DoAbilityBackground KEYGUARD, id: %{public}d", GetWindowId());
1601 AAFwk::AbilityManagerClient::GetInstance()->DoAbilityBackground(abilityContext_->GetToken(),
1602 static_cast<uint32_t>(WindowStateChangeReason::KEYGUARD));
1603 } else {
1604 state_ = WindowState::STATE_FROZEN;
1605 NotifyAfterBackground();
1606 }
1607 break;
1608 }
1609 case WindowState::STATE_UNFROZEN: {
1610 if (abilityContext_ != nullptr && windowTag_ == WindowTag::MAIN_WINDOW) {
1611 WLOGFD("DoAbilityForeground KEYGUARD, id: %{public}d", GetWindowId());
1612 AAFwk::AbilityManagerClient::GetInstance()->DoAbilityForeground(abilityContext_->GetToken(),
1613 static_cast<uint32_t>(WindowStateChangeReason::KEYGUARD));
1614 } else {
1615 state_ = WindowState::STATE_SHOWN;
1616 NotifyAfterForeground();
1617 }
1618 break;
1619 }
1620 default: {
1621 WLOGFE("windowState to set is invalid");
1622 break;
1623 }
1624 }
1625 }
1626
UpdateDragEvent(const PointInfo & point,DragEvent event)1627 void WindowImpl::UpdateDragEvent(const PointInfo& point, DragEvent event)
1628 {
1629 std::vector<sptr<IWindowDragListener>> windowDragListeners;
1630 {
1631 std::lock_guard<std::recursive_mutex> lock(mutex_);
1632 windowDragListeners = windowDragListeners_;
1633 }
1634 for (auto& iter : windowDragListeners) {
1635 iter->OnDrag(point.x - GetRect().posX_, point.y - GetRect().posY_, event);
1636 }
1637 }
1638
UpdateDisplayId(DisplayId from,DisplayId to)1639 void WindowImpl::UpdateDisplayId(DisplayId from, DisplayId to)
1640 {
1641 WLOGFD("update displayId. win %{public}d", GetWindowId());
1642 for (auto& listener : displayMoveListeners_) {
1643 if (listener != nullptr) {
1644 listener->OnDisplayMove(from, to);
1645 }
1646 }
1647 property_->SetDisplayId(to);
1648 }
1649
UpdateOccupiedAreaChangeInfo(const sptr<OccupiedAreaChangeInfo> & info)1650 void WindowImpl::UpdateOccupiedAreaChangeInfo(const sptr<OccupiedAreaChangeInfo>& info)
1651 {
1652 WLOGFI("Window Update OccupiedArea, id: %{public}d", property_->GetWindowId());
1653 for (auto& listener : occupiedAreaChangeListeners_) {
1654 if (listener != nullptr) {
1655 listener->OnSizeChange(info);
1656 }
1657 }
1658 }
1659
UpdateActiveStatus(bool isActive)1660 void WindowImpl::UpdateActiveStatus(bool isActive)
1661 {
1662 WLOGFI("window active status: %{public}d, id: %{public}u", isActive, property_->GetWindowId());
1663 if (isActive) {
1664 NotifyAfterActive();
1665 } else {
1666 NotifyAfterInactive();
1667 }
1668 }
1669
SetDefaultOption()1670 void WindowImpl::SetDefaultOption()
1671 {
1672 auto display = DisplayManager::GetInstance().GetDisplayById(property_->GetDisplayId());
1673 if (display == nullptr) {
1674 WLOGFE("get display failed displayId:%{public}" PRIu64", window id:%{public}u", property_->GetDisplayId(),
1675 property_->GetWindowId());
1676 return;
1677 }
1678 uint32_t width = static_cast<uint32_t>(display->GetWidth());
1679 uint32_t height = static_cast<uint32_t>(display->GetHeight());
1680 WLOGFI("width:%{public}u, height:%{public}u, displayId:%{public}" PRIu64"",
1681 width, height, property_->GetDisplayId());
1682
1683 Rect rect;
1684 switch (property_->GetWindowType()) {
1685 case WindowType::WINDOW_TYPE_STATUS_BAR: {
1686 property_->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
1687 property_->SetFocusable(false);
1688 break;
1689 }
1690 case WindowType::WINDOW_TYPE_NAVIGATION_BAR: {
1691 property_->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
1692 property_->SetFocusable(false);
1693 break;
1694 }
1695 case WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW: {
1696 uint32_t alarmWidth = static_cast<uint32_t>((static_cast<float>(width) *
1697 SYSTEM_ALARM_WINDOW_WIDTH_RATIO));
1698 uint32_t alarmHeight = static_cast<uint32_t>((static_cast<float>(height) *
1699 SYSTEM_ALARM_WINDOW_HEIGHT_RATIO));
1700
1701 rect = { static_cast<int32_t>((width - alarmWidth) / 2), static_cast<int32_t>((height - alarmHeight) / 2),
1702 alarmWidth, alarmHeight };
1703 property_->SetWindowRect(rect);
1704 property_->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
1705 break;
1706 }
1707 case WindowType::WINDOW_TYPE_KEYGUARD: {
1708 RemoveWindowFlag(WindowFlag::WINDOW_FLAG_NEED_AVOID);
1709 property_->SetWindowMode(WindowMode::WINDOW_MODE_FULLSCREEN);
1710 break;
1711 }
1712 case WindowType::WINDOW_TYPE_DRAGGING_EFFECT: {
1713 property_->SetWindowFlags(0);
1714 break;
1715 }
1716 case WindowType::WINDOW_TYPE_TOAST:
1717 case WindowType::WINDOW_TYPE_FLOAT:
1718 case WindowType::WINDOW_TYPE_SEARCHING_BAR: {
1719 property_->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
1720 break;
1721 }
1722 case WindowType::WINDOW_TYPE_VOLUME_OVERLAY: {
1723 property_->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
1724 property_->SetFocusable(false);
1725 break;
1726 }
1727 case WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT: {
1728 property_->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
1729 property_->SetFocusable(false);
1730 break;
1731 }
1732 case WindowType::WINDOW_TYPE_BOOT_ANIMATION:
1733 case WindowType::WINDOW_TYPE_POINTER: {
1734 property_->SetFocusable(false);
1735 break;
1736 }
1737 default:
1738 break;
1739 }
1740 }
IsWindowValid() const1741 bool WindowImpl::IsWindowValid() const
1742 {
1743 bool res = ((state_ > WindowState::STATE_INITIAL) && (state_ < WindowState::STATE_BOTTOM));
1744 if (!res) {
1745 WLOGFI("window is already destroyed or not created! id: %{public}u", GetWindowId());
1746 }
1747 return res;
1748 }
1749
IsLayoutFullScreen() const1750 bool WindowImpl::IsLayoutFullScreen() const
1751 {
1752 uint32_t flags = GetWindowFlags();
1753 auto mode = GetMode();
1754 bool needAvoid = (flags & static_cast<uint32_t>(WindowFlag::WINDOW_FLAG_NEED_AVOID));
1755 return (mode == WindowMode::WINDOW_MODE_FULLSCREEN && !needAvoid);
1756 }
1757
IsFullScreen() const1758 bool WindowImpl::IsFullScreen() const
1759 {
1760 auto statusProperty = GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_STATUS_BAR);
1761 auto naviProperty = GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_NAVIGATION_BAR);
1762 return (IsLayoutFullScreen() && !statusProperty.enable_ && !naviProperty.enable_);
1763 }
1764
SetRequestedOrientation(Orientation orientation)1765 void WindowImpl::SetRequestedOrientation(Orientation orientation)
1766 {
1767 if (property_->GetRequestedOrientation() == orientation) {
1768 return;
1769 }
1770 property_->SetRequestedOrientation(orientation);
1771 if (state_ == WindowState::STATE_SHOWN) {
1772 UpdateProperty(PropertyChangeAction::ACTION_UPDATE_ORIENTATION);
1773 }
1774 }
1775
GetRequestedOrientation()1776 Orientation WindowImpl::GetRequestedOrientation()
1777 {
1778 return property_->GetRequestedOrientation();
1779 }
1780 }
1781 }
1782