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_manager.h"
17 
18 #include <algorithm>
19 #include <cinttypes>
20 #include <shared_mutex>
21 
22 #include "input_manager.h"
23 
24 #include "marshalling_helper.h"
25 #include "window_adapter.h"
26 #include "window_manager_agent.h"
27 #include "window_manager_hilog.h"
28 #include "window_display_change_adapter.h"
29 #include "wm_common.h"
30 
31 namespace OHOS {
32 namespace Rosen {
33 namespace {
34 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowManager"};
35 struct WindowChecker : public MMI::IWindowChecker {
36 public:
37     WindowChecker() = default;
38     ~WindowChecker() = default;
39     int32_t CheckWindowId(int32_t windowId) const override;
40 };
41 }
42 
43 WM_IMPLEMENT_SINGLE_INSTANCE(WindowManager)
44 
45 class WindowManager::Impl {
46 public:
47     void NotifyWMSConnected(int32_t userId, int32_t screenId);
48     void NotifyWMSDisconnected(int32_t userId, int32_t screenId);
49     void NotifyFocused(uint32_t windowId, const sptr<IRemoteObject>& abilityToken,
50         WindowType windowType, DisplayId displayId);
51     void NotifyUnfocused(uint32_t windowId, const sptr<IRemoteObject>& abilityToken,
52         WindowType windowType, DisplayId displayId);
53     void NotifyFocused(const sptr<FocusChangeInfo>& focusChangeInfo);
54     void NotifyWindowModeChange(WindowModeType type);
55     void NotifyUnfocused(const sptr<FocusChangeInfo>& focusChangeInfo);
56     void NotifySystemBarChanged(DisplayId displayId, const SystemBarRegionTints& tints);
57     void NotifyAccessibilityWindowInfo(const std::vector<sptr<AccessibilityWindowInfo>>& infos, WindowUpdateType type);
58     void NotifyWindowVisibilityInfoChanged(const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfos);
59     void NotifyWindowDrawingContentInfoChanged(const std::vector<sptr<WindowDrawingContentInfo>>&
60         windowDrawingContentInfos);
61     void UpdateCameraFloatWindowStatus(uint32_t accessTokenId, bool isShowing);
62     void NotifyWaterMarkFlagChangedResult(bool showWaterMark);
63     void NotifyVisibleWindowNumChanged(const std::vector<VisibleWindowNumInfo>& visibleWindowNumInfo);
64     void NotifyGestureNavigationEnabledResult(bool enable);
65     void NotifyDisplayInfoChanged(const sptr<IRemoteObject>& token, DisplayId displayId,
66         float density, DisplayOrientation orientation);
67     void NotifyWindowStyleChange(WindowStyleType type);
68     void NotifyWindowPidVisibilityChanged(const sptr<WindowPidVisibilityInfo>& info);
69 
70     static inline SingletonDelegator<WindowManager> delegator_;
71 
72     std::shared_mutex listenerMutex_;
73     sptr<IWMSConnectionChangedListener> wmsConnectionChangedListener_;
74     std::vector<sptr<IFocusChangedListener>> focusChangedListeners_;
75     sptr<WindowManagerAgent> focusChangedListenerAgent_;
76     std::vector<sptr<IWindowModeChangedListener>> windowModeListeners_;
77     sptr<WindowManagerAgent> windowModeListenerAgent_;
78     std::vector<sptr<ISystemBarChangedListener>> systemBarChangedListeners_;
79     sptr<WindowManagerAgent> systemBarChangedListenerAgent_;
80     std::vector<sptr<IWindowUpdateListener>> windowUpdateListeners_;
81     sptr<WindowManagerAgent> windowUpdateListenerAgent_;
82     std::vector<sptr<IVisibilityChangedListener>> windowVisibilityListeners_;
83     sptr<WindowManagerAgent> windowVisibilityListenerAgent_;
84     std::vector<sptr<IDrawingContentChangedListener>> windowDrawingContentListeners_;
85     sptr<WindowManagerAgent> windowDrawingContentListenerAgent_;
86     std::vector<sptr<ICameraFloatWindowChangedListener>> cameraFloatWindowChangedListeners_;
87     sptr<WindowManagerAgent> cameraFloatWindowChangedListenerAgent_;
88     std::vector<sptr<IWaterMarkFlagChangedListener>> waterMarkFlagChangeListeners_;
89     sptr<WindowManagerAgent> waterMarkFlagChangeAgent_;
90     std::vector<sptr<IGestureNavigationEnabledChangedListener>> gestureNavigationEnabledListeners_;
91     sptr<WindowManagerAgent> gestureNavigationEnabledAgent_;
92     std::vector<sptr<IVisibleWindowNumChangedListener>> visibleWindowNumChangedListeners_;
93     sptr<WindowManagerAgent> visibleWindowNumChangedListenerAgent_;
94     std::vector<sptr<IWindowStyleChangedListener>> windowStyleListeners_;
95     sptr<WindowManagerAgent> windowStyleListenerAgent_;
96     std::map<sptr<IRemoteObject>,
97         std::vector<sptr<WindowDisplayChangeAdapter>>> displayInfoChangedListeners_;
98     std::vector<sptr<IWindowPidVisibilityChangedListener>> windowPidVisibilityListeners_;
99     sptr<WindowManagerAgent> windowPidVisibilityListenerAgent_;
100 };
101 
NotifyWMSConnected(int32_t userId, int32_t screenId)102 void WindowManager::Impl::NotifyWMSConnected(int32_t userId, int32_t screenId)
103 {
104     TLOGI(WmsLogTag::WMS_MULTI_USER, "WMS connected [userId:%{public}d; screenId:%{public}d]", userId, screenId);
105     sptr<IWMSConnectionChangedListener> wmsConnectionChangedListener;
106     {
107         std::shared_lock<std::shared_mutex> lock(listenerMutex_);
108         wmsConnectionChangedListener = wmsConnectionChangedListener_;
109     }
110     if (wmsConnectionChangedListener != nullptr) {
111         wmsConnectionChangedListener->OnConnected(userId, screenId);
112     }
113 }
114 
NotifyWMSDisconnected(int32_t userId, int32_t screenId)115 void WindowManager::Impl::NotifyWMSDisconnected(int32_t userId, int32_t screenId)
116 {
117     TLOGI(WmsLogTag::WMS_MULTI_USER, "WMS disconnected [userId:%{public}d; screenId:%{public}d]", userId, screenId);
118     sptr<IWMSConnectionChangedListener> wmsConnectionChangedListener;
119     {
120         std::shared_lock<std::shared_mutex> lock(listenerMutex_);
121         wmsConnectionChangedListener = wmsConnectionChangedListener_;
122     }
123     if (wmsConnectionChangedListener != nullptr) {
124         wmsConnectionChangedListener->OnDisconnected(userId, screenId);
125     }
126 }
127 
NotifyFocused(const sptr<FocusChangeInfo>& focusChangeInfo)128 void WindowManager::Impl::NotifyFocused(const sptr<FocusChangeInfo>& focusChangeInfo)
129 {
130     TLOGD(WmsLogTag::WMS_FOCUS, "NotifyFocused [%{public}u; %{public}" PRIu64"; %{public}d; %{public}d; %{public}u]",
131         focusChangeInfo->windowId_, focusChangeInfo->displayId_, focusChangeInfo->pid_, focusChangeInfo->uid_,
132         static_cast<uint32_t>(focusChangeInfo->windowType_));
133     std::vector<sptr<IFocusChangedListener>> focusChangeListeners;
134     {
135         std::shared_lock<std::shared_mutex> lock(listenerMutex_);
136         focusChangeListeners = focusChangedListeners_;
137     }
138     for (auto& listener : focusChangeListeners) {
139         listener->OnFocused(focusChangeInfo);
140     }
141 }
142 
NotifyUnfocused(const sptr<FocusChangeInfo>& focusChangeInfo)143 void WindowManager::Impl::NotifyUnfocused(const sptr<FocusChangeInfo>& focusChangeInfo)
144 {
145     TLOGD(WmsLogTag::WMS_FOCUS, "NotifyUnfocused [%{public}u; %{public}" PRIu64"; %{public}d; %{public}d; %{public}u]",
146         focusChangeInfo->windowId_, focusChangeInfo->displayId_, focusChangeInfo->pid_, focusChangeInfo->uid_,
147         static_cast<uint32_t>(focusChangeInfo->windowType_));
148     std::vector<sptr<IFocusChangedListener>> focusChangeListeners;
149     {
150         std::shared_lock<std::shared_mutex> lock(listenerMutex_);
151         focusChangeListeners = focusChangedListeners_;
152     }
153     for (auto& listener : focusChangeListeners) {
154         listener->OnUnfocused(focusChangeInfo);
155     }
156 }
157 
NotifyWindowModeChange(WindowModeType type)158 void WindowManager::Impl::NotifyWindowModeChange(WindowModeType type)
159 {
160     TLOGI(WmsLogTag::WMS_MAIN, "WindowManager::Impl UpdateWindowModeTypeInfo type: %{public}d",
161         static_cast<uint8_t>(type));
162     std::vector<sptr<IWindowModeChangedListener>> windowModeListeners;
163     {
164         std::shared_lock<std::shared_mutex> lock(listenerMutex_);
165         windowModeListeners = windowModeListeners_;
166     }
167     for (auto &listener : windowModeListeners) {
168         listener->OnWindowModeUpdate(type);
169     }
170 }
171 
NotifySystemBarChanged(DisplayId displayId, const SystemBarRegionTints& tints)172 void WindowManager::Impl::NotifySystemBarChanged(DisplayId displayId, const SystemBarRegionTints& tints)
173 {
174     for (auto tint : tints) {
175         WLOGFD("type:%{public}d, enable:%{public}d," \
176             "backgroundColor:%{public}x, contentColor:%{public}x " \
177             "region:[%{public}d, %{public}d, %{public}d, %{public}d]",
178             tint.type_, tint.prop_.enable_, tint.prop_.backgroundColor_, tint.prop_.contentColor_,
179             tint.region_.posX_, tint.region_.posY_, tint.region_.width_, tint.region_.height_);
180     }
181     std::vector<sptr<ISystemBarChangedListener>> systemBarChangeListeners;
182     {
183         std::shared_lock<std::shared_mutex> lock(listenerMutex_);
184         systemBarChangeListeners = systemBarChangedListeners_;
185     }
186     for (auto& listener : systemBarChangeListeners) {
187         listener->OnSystemBarPropertyChange(displayId, tints);
188     }
189 }
190 
NotifyAccessibilityWindowInfo(const std::vector<sptr<AccessibilityWindowInfo>>& infos, WindowUpdateType type)191 void WindowManager::Impl::NotifyAccessibilityWindowInfo(const std::vector<sptr<AccessibilityWindowInfo>>& infos,
192     WindowUpdateType type)
193 {
194     if (infos.empty()) {
195         WLOGFE("infos is empty");
196         return;
197     }
198     for (auto& info : infos) {
199         if (info == nullptr) {
200             TLOGD(WmsLogTag::WMS_MAIN, "info is nullptr");
201             continue;
202         }
203         TLOGD(WmsLogTag::WMS_MAIN, "NotifyAccessibilityWindowInfo: wid[%{public}u], innerWid_[%{public}u]," \
204             "uiNodeId_[%{public}u], rect[%{public}d %{public}d %{public}d %{public}d]," \
205             "isFocused[%{public}d], isDecorEnable[%{public}d], displayId[%{public}" PRIu64"], layer[%{public}u]," \
206             "mode[%{public}u], type[%{public}u, updateType[%{public}d], bundle[%{public}s]",
207             info->wid_, info->innerWid_, info->uiNodeId_, info->windowRect_.width_, info->windowRect_.height_,
208             info->windowRect_.posX_, info->windowRect_.posY_, info->focused_, info->isDecorEnable_, info->displayId_,
209             info->layer_, info->mode_, info->type_, type, info->bundleName_.c_str());
210         for (const auto& rect : info->touchHotAreas_) {
211             TLOGD(WmsLogTag::WMS_MAIN, "window touch hot areas rect[x=%{public}d,y=%{public}d," \
212             "w=%{public}d,h=%{public}d]", rect.posX_, rect.posY_, rect.width_, rect.height_);
213         }
214     }
215 
216     std::vector<sptr<IWindowUpdateListener>> windowUpdateListeners;
217     {
218         std::shared_lock<std::shared_mutex> lock(listenerMutex_);
219         windowUpdateListeners = windowUpdateListeners_;
220     }
221     for (auto& listener : windowUpdateListeners) {
222         listener->OnWindowUpdate(infos, type);
223     }
224 }
225 
NotifyWindowVisibilityInfoChanged( const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfos)226 void WindowManager::Impl::NotifyWindowVisibilityInfoChanged(
227     const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfos)
228 {
229     std::vector<sptr<IVisibilityChangedListener>> visibilityChangeListeners;
230     {
231         std::shared_lock<std::shared_mutex> lock(listenerMutex_);
232         visibilityChangeListeners = windowVisibilityListeners_;
233     }
234     for (auto& listener : visibilityChangeListeners) {
235         WLOGD("Notify WindowVisibilityInfo to caller");
236         listener->OnWindowVisibilityChanged(windowVisibilityInfos);
237     }
238 }
239 
NotifyWindowDrawingContentInfoChanged( const std::vector<sptr<WindowDrawingContentInfo>>& windowDrawingContentInfos)240 void WindowManager::Impl::NotifyWindowDrawingContentInfoChanged(
241     const std::vector<sptr<WindowDrawingContentInfo>>& windowDrawingContentInfos)
242 {
243     std::vector<sptr<IDrawingContentChangedListener>> windowDrawingContentChangeListeners;
244     {
245         std::shared_lock<std::shared_mutex> lock(listenerMutex_);
246         windowDrawingContentChangeListeners = windowDrawingContentListeners_;
247     }
248     for (auto& listener : windowDrawingContentChangeListeners) {
249         WLOGFD("Notify windowDrawingContentInfo to caller");
250         listener->OnWindowDrawingContentChanged(windowDrawingContentInfos);
251     }
252 }
253 
UpdateCameraFloatWindowStatus(uint32_t accessTokenId, bool isShowing)254 void WindowManager::Impl::UpdateCameraFloatWindowStatus(uint32_t accessTokenId, bool isShowing)
255 {
256     TLOGD(WmsLogTag::DEFAULT,
257         "Camera float window, accessTokenId = %{private}u, isShowing = %{public}u", accessTokenId, isShowing);
258     std::vector<sptr<ICameraFloatWindowChangedListener>> cameraFloatWindowChangeListeners;
259     {
260         std::shared_lock<std::shared_mutex> lock(listenerMutex_);
261         cameraFloatWindowChangeListeners = cameraFloatWindowChangedListeners_;
262     }
263     for (auto& listener : cameraFloatWindowChangeListeners) {
264         listener->OnCameraFloatWindowChange(accessTokenId, isShowing);
265     }
266 }
267 
NotifyWaterMarkFlagChangedResult(bool showWaterMark)268 void WindowManager::Impl::NotifyWaterMarkFlagChangedResult(bool showWaterMark)
269 {
270     WLOGFI("Notify water mark flag changed result, showWaterMark = %{public}d", showWaterMark);
271     std::vector<sptr<IWaterMarkFlagChangedListener>> waterMarkFlagChangeListeners;
272     {
273         std::shared_lock<std::shared_mutex> lock(listenerMutex_);
274         waterMarkFlagChangeListeners = waterMarkFlagChangeListeners_;
275     }
276     for (auto& listener : waterMarkFlagChangeListeners) {
277         listener->OnWaterMarkFlagUpdate(showWaterMark);
278     }
279 }
280 
NotifyGestureNavigationEnabledResult(bool enable)281 void WindowManager::Impl::NotifyGestureNavigationEnabledResult(bool enable)
282 {
283     WLOGFI("Notify gesture navigation enable result, enable = %{public}d", enable);
284     std::vector<sptr<IGestureNavigationEnabledChangedListener>> gestureNavigationEnabledListeners;
285     {
286         std::shared_lock<std::shared_mutex> lock(listenerMutex_);
287         gestureNavigationEnabledListeners = gestureNavigationEnabledListeners_;
288     }
289     for (auto& listener : gestureNavigationEnabledListeners) {
290         listener->OnGestureNavigationEnabledUpdate(enable);
291     }
292 }
293 
NotifyVisibleWindowNumChanged( const std::vector<VisibleWindowNumInfo>& visibleWindowNumInfo)294 void WindowManager::Impl::NotifyVisibleWindowNumChanged(
295     const std::vector<VisibleWindowNumInfo>& visibleWindowNumInfo)
296 {
297     std::vector<sptr<IVisibleWindowNumChangedListener>> visibleWindowNumChangedListeners;
298     {
299         std::shared_lock<std::shared_mutex> lock(listenerMutex_);
300         visibleWindowNumChangedListeners = visibleWindowNumChangedListeners_;
301     }
302     for (auto& listener : visibleWindowNumChangedListeners) {
303         if (listener == nullptr) {
304             continue;
305         }
306         listener->OnVisibleWindowNumChange(visibleWindowNumInfo);
307     }
308 }
309 
NotifyDisplayInfoChanged(const sptr<IRemoteObject>& token, DisplayId displayId, float density, DisplayOrientation orientation)310 void WindowManager::Impl::NotifyDisplayInfoChanged(const sptr<IRemoteObject>& token, DisplayId displayId,
311     float density, DisplayOrientation orientation)
312 {
313     auto iter = displayInfoChangedListeners_.end();
314     std::vector<sptr<WindowDisplayChangeAdapter>> displayInfoChangedListeners;
315     {
316         std::unique_lock<std::shared_mutex> lock(listenerMutex_);
317         iter = displayInfoChangedListeners_.find(token);
318         if (iter == displayInfoChangedListeners_.end()) {
319             TLOGI(WmsLogTag::DMS, "can not find token in listener list, need not notify the change of display info");
320             return;
321         }
322         displayInfoChangedListeners = iter->second;
323     }
324 
325     for (auto& listener : displayInfoChangedListeners) {
326         listener->OnDisplayInfoChange(token, displayId, density, orientation);
327     }
328 }
329 
NotifyWindowStyleChange(WindowStyleType type)330 void WindowManager::Impl::NotifyWindowStyleChange(WindowStyleType type)
331 {
332     TLOGI(WmsLogTag::WMS_MAIN, "WindowStyleChange type: %{public}d",
333           static_cast<uint8_t>(type));
334     std::vector<sptr<IWindowStyleChangedListener>> windowStyleListeners;
335     {
336         std::unique_lock<std::shared_mutex> lock(listenerMutex_);
337         windowStyleListeners = windowStyleListeners_;
338     }
339     for (auto &listener : windowStyleListeners) {
340         TLOGI(WmsLogTag::WMS_MAIN, "WindowStyleChange type: %{public}d",
341               static_cast<uint8_t>(type));
342         listener->OnWindowStyleUpdate(type);
343     }
344 }
345 
NotifyWindowPidVisibilityChanged( const sptr<WindowPidVisibilityInfo>& info)346 void WindowManager::Impl::NotifyWindowPidVisibilityChanged(
347     const sptr<WindowPidVisibilityInfo>& info)
348 {
349     std::vector<sptr<IWindowPidVisibilityChangedListener>> windowPidVisibilityListeners;
350     {
351         std::unique_lock<std::shared_mutex> lock(listenerMutex_);
352         windowPidVisibilityListeners = windowPidVisibilityListeners_;
353     }
354     for (auto &listener : windowPidVisibilityListeners) {
355         if (listener != nullptr) {
356             listener->NotifyWindowPidVisibilityChanged(info);
357         }
358     }
359 }
360 
WindowManager()361 WindowManager::WindowManager() : pImpl_(std::make_unique<Impl>())
362 {
363 }
364 
CheckWindowId(int32_t windowId) const365 int32_t WindowChecker::CheckWindowId(int32_t windowId) const
366 {
367     int32_t pid = INVALID_PID;
368     WMError ret = SingletonContainer::Get<WindowAdapter>().CheckWindowId(windowId, pid);
369     if (ret != WMError::WM_OK) {
370         WLOGFE("Window(%{public}d) do not allow styles to be set", windowId);
371     }
372     return pid;
373 }
374 
~WindowManager()375 WindowManager::~WindowManager()
376 {
377     std::lock_guard<std::recursive_mutex> lock(mutex_);
378     destroyed_ = true;
379 }
380 
RegisterWMSConnectionChangedListener(const sptr<IWMSConnectionChangedListener>& listener)381 WMError WindowManager::RegisterWMSConnectionChangedListener(const sptr<IWMSConnectionChangedListener>& listener)
382 {
383     int32_t clientUserId = GetUserIdByUid(getuid());
384     if (clientUserId != SYSTEM_USERID) {
385         TLOGW(WmsLogTag::WMS_MULTI_USER, "Not u0 user, permission denied");
386         return WMError::WM_ERROR_INVALID_PERMISSION;
387     }
388     if (listener == nullptr) {
389         TLOGE(WmsLogTag::WMS_MULTI_USER, "WMS connection changed listener registered could not be null");
390         return WMError::WM_ERROR_NULLPTR;
391     }
392     TLOGI(WmsLogTag::WMS_MULTI_USER, "Register enter");
393     {
394         std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
395         if (pImpl_->wmsConnectionChangedListener_) {
396             TLOGI(WmsLogTag::WMS_MULTI_USER, "wmsConnectionChangedListener is already registered, do nothing");
397             return WMError::WM_OK;
398         }
399         pImpl_->wmsConnectionChangedListener_ = listener;
400     }
401     auto ret = SingletonContainer::Get<WindowAdapter>().RegisterWMSConnectionChangedListener(
402         [this](int32_t userId, int32_t screenId, bool isConnected) {
403             this->OnWMSConnectionChanged(userId, screenId, isConnected);
404         });
405     if (ret != WMError::WM_OK) {
406         pImpl_->wmsConnectionChangedListener_ = nullptr;
407     }
408     return ret;
409 }
410 
UnregisterWMSConnectionChangedListener()411 WMError WindowManager::UnregisterWMSConnectionChangedListener()
412 {
413     TLOGI(WmsLogTag::WMS_MULTI_USER, "Unregister enter");
414     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
415     pImpl_->wmsConnectionChangedListener_ = nullptr;
416     return WMError::WM_OK;
417 }
418 
RegisterFocusChangedListener(const sptr<IFocusChangedListener>& listener)419 WMError WindowManager::RegisterFocusChangedListener(const sptr<IFocusChangedListener>& listener)
420 {
421     if (listener == nullptr) {
422         WLOGFE("listener could not be null");
423         return WMError::WM_ERROR_NULLPTR;
424     }
425 
426     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
427     WMError ret = WMError::WM_OK;
428     if (pImpl_->focusChangedListenerAgent_ == nullptr) {
429         pImpl_->focusChangedListenerAgent_ = new WindowManagerAgent();
430     }
431     ret = SingletonContainer::Get<WindowAdapter>().RegisterWindowManagerAgent(
432         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_FOCUS, pImpl_->focusChangedListenerAgent_);
433     if (ret != WMError::WM_OK) {
434         WLOGFW("RegisterWindowManagerAgent failed!");
435         pImpl_->focusChangedListenerAgent_ = nullptr;
436     } else {
437         auto iter = std::find(pImpl_->focusChangedListeners_.begin(), pImpl_->focusChangedListeners_.end(), listener);
438         if (iter != pImpl_->focusChangedListeners_.end()) {
439             WLOGFW("Listener is already registered.");
440             return WMError::WM_OK;
441         }
442         pImpl_->focusChangedListeners_.push_back(listener);
443     }
444     return ret;
445 }
446 
UnregisterFocusChangedListener(const sptr<IFocusChangedListener>& listener)447 WMError WindowManager::UnregisterFocusChangedListener(const sptr<IFocusChangedListener>& listener)
448 {
449     if (listener == nullptr) {
450         WLOGFE("listener could not be null");
451         return WMError::WM_ERROR_NULLPTR;
452     }
453 
454     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
455     auto iter = std::find(pImpl_->focusChangedListeners_.begin(), pImpl_->focusChangedListeners_.end(), listener);
456     if (iter == pImpl_->focusChangedListeners_.end()) {
457         WLOGFE("could not find this listener");
458         return WMError::WM_OK;
459     }
460     pImpl_->focusChangedListeners_.erase(iter);
461     WMError ret = WMError::WM_OK;
462     if (pImpl_->focusChangedListeners_.empty() && pImpl_->focusChangedListenerAgent_ != nullptr) {
463         ret = SingletonContainer::Get<WindowAdapter>().UnregisterWindowManagerAgent(
464             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_FOCUS, pImpl_->focusChangedListenerAgent_);
465         if (ret == WMError::WM_OK) {
466             pImpl_->focusChangedListenerAgent_ = nullptr;
467         }
468     }
469     return ret;
470 }
471 
RegisterWindowModeChangedListener(const sptr<IWindowModeChangedListener>& listener)472 WMError WindowManager::RegisterWindowModeChangedListener(const sptr<IWindowModeChangedListener>& listener)
473 {
474     if (listener == nullptr) {
475         TLOGE(WmsLogTag::WMS_MAIN, "listener could not be null");
476         return WMError::WM_ERROR_NULLPTR;
477     }
478 
479     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
480     WMError ret = WMError::WM_OK;
481     if (pImpl_->windowModeListenerAgent_ == nullptr) {
482         pImpl_->windowModeListenerAgent_ = new WindowManagerAgent();
483     }
484     ret = SingletonContainer::Get<WindowAdapter>().RegisterWindowManagerAgent(
485         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_MODE, pImpl_->windowModeListenerAgent_);
486     if (ret != WMError::WM_OK) {
487         TLOGW(WmsLogTag::WMS_MAIN, "RegisterWindowManagerAgent failed!");
488         pImpl_->windowModeListenerAgent_ = nullptr;
489         return ret;
490     }
491     auto iter = std::find(pImpl_->windowModeListeners_.begin(), pImpl_->windowModeListeners_.end(), listener);
492     if (iter != pImpl_->windowModeListeners_.end()) {
493         TLOGW(WmsLogTag::WMS_MAIN, "Listener is already registered.");
494         return WMError::WM_OK;
495     }
496     pImpl_->windowModeListeners_.push_back(listener);
497     return ret;
498 }
499 
UnregisterWindowModeChangedListener(const sptr<IWindowModeChangedListener>& listener)500 WMError WindowManager::UnregisterWindowModeChangedListener(const sptr<IWindowModeChangedListener>& listener)
501 {
502     if (listener == nullptr) {
503         TLOGE(WmsLogTag::WMS_MAIN, "listener could not be null");
504         return WMError::WM_ERROR_NULLPTR;
505     }
506 
507     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
508     auto iter = std::find(pImpl_->windowModeListeners_.begin(), pImpl_->windowModeListeners_.end(), listener);
509     if (iter == pImpl_->windowModeListeners_.end()) {
510         TLOGE(WmsLogTag::WMS_MAIN, "could not find this listener");
511         return WMError::WM_OK;
512     }
513     pImpl_->windowModeListeners_.erase(iter);
514     WMError ret = WMError::WM_OK;
515     if (pImpl_->windowModeListeners_.empty() && pImpl_->windowModeListenerAgent_ != nullptr) {
516         ret = SingletonContainer::Get<WindowAdapter>().UnregisterWindowManagerAgent(
517             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_MODE, pImpl_->windowModeListenerAgent_);
518         if (ret == WMError::WM_OK) {
519             pImpl_->windowModeListenerAgent_ = nullptr;
520         }
521     }
522     return ret;
523 }
524 
RegisterSystemBarChangedListener(const sptr<ISystemBarChangedListener>& listener)525 WMError WindowManager::RegisterSystemBarChangedListener(const sptr<ISystemBarChangedListener>& listener)
526 {
527     if (listener == nullptr) {
528         WLOGFE("listener could not be null");
529         return WMError::WM_ERROR_NULLPTR;
530     }
531 
532     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
533     WMError ret = WMError::WM_OK;
534     if (pImpl_->systemBarChangedListenerAgent_ == nullptr) {
535         pImpl_->systemBarChangedListenerAgent_ = new WindowManagerAgent();
536     }
537     ret = SingletonContainer::Get<WindowAdapter>().RegisterWindowManagerAgent(
538         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_SYSTEM_BAR, pImpl_->systemBarChangedListenerAgent_);
539     if (ret != WMError::WM_OK) {
540         WLOGFW("RegisterWindowManagerAgent failed!");
541         pImpl_->systemBarChangedListenerAgent_ = nullptr;
542     } else {
543         auto iter = std::find(pImpl_->systemBarChangedListeners_.begin(), pImpl_->systemBarChangedListeners_.end(),
544             listener);
545         if (iter != pImpl_->systemBarChangedListeners_.end()) {
546             WLOGFW("Listener is already registered.");
547             return WMError::WM_OK;
548         }
549         pImpl_->systemBarChangedListeners_.push_back(listener);
550     }
551     return ret;
552 }
553 
UnregisterSystemBarChangedListener(const sptr<ISystemBarChangedListener>& listener)554 WMError WindowManager::UnregisterSystemBarChangedListener(const sptr<ISystemBarChangedListener>& listener)
555 {
556     if (listener == nullptr) {
557         WLOGFE("listener could not be null");
558         return WMError::WM_ERROR_NULLPTR;
559     }
560 
561     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
562     auto iter = std::find(pImpl_->systemBarChangedListeners_.begin(), pImpl_->systemBarChangedListeners_.end(),
563         listener);
564     if (iter == pImpl_->systemBarChangedListeners_.end()) {
565         WLOGFE("could not find this listener");
566         return WMError::WM_OK;
567     }
568     pImpl_->systemBarChangedListeners_.erase(iter);
569     WMError ret = WMError::WM_OK;
570     if (pImpl_->systemBarChangedListeners_.empty() && pImpl_->systemBarChangedListenerAgent_ != nullptr) {
571         ret = SingletonContainer::Get<WindowAdapter>().UnregisterWindowManagerAgent(
572             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_SYSTEM_BAR, pImpl_->systemBarChangedListenerAgent_);
573         if (ret == WMError::WM_OK) {
574             pImpl_->systemBarChangedListenerAgent_ = nullptr;
575         }
576     }
577     return ret;
578 }
579 
MinimizeAllAppWindows(DisplayId displayId)580 WMError WindowManager::MinimizeAllAppWindows(DisplayId displayId)
581 {
582     WLOGFD("displayId %{public}" PRIu64"", displayId);
583     return SingletonContainer::Get<WindowAdapter>().MinimizeAllAppWindows(displayId);
584 }
585 
ToggleShownStateForAllAppWindows()586 WMError WindowManager::ToggleShownStateForAllAppWindows()
587 {
588     WLOGFD("ToggleShownStateForAllAppWindows");
589     return SingletonContainer::Get<WindowAdapter>().ToggleShownStateForAllAppWindows();
590 }
591 
SetWindowLayoutMode(WindowLayoutMode mode)592 WMError WindowManager::SetWindowLayoutMode(WindowLayoutMode mode)
593 {
594     WLOGFD("set window layout mode: %{public}u", mode);
595     WMError ret  = SingletonContainer::Get<WindowAdapter>().SetWindowLayoutMode(mode);
596     if (ret != WMError::WM_OK) {
597         WLOGFE("set layout mode failed");
598     }
599     return ret;
600 }
601 
RegisterWindowUpdateListener(const sptr<IWindowUpdateListener>& listener)602 WMError WindowManager::RegisterWindowUpdateListener(const sptr<IWindowUpdateListener>& listener)
603 {
604     if (listener == nullptr) {
605         WLOGFE("listener could not be null");
606         return WMError::WM_ERROR_NULLPTR;
607     }
608     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
609     WMError ret = WMError::WM_OK;
610     if (pImpl_->windowUpdateListenerAgent_ == nullptr) {
611         pImpl_->windowUpdateListenerAgent_ = new WindowManagerAgent();
612     }
613     ret = SingletonContainer::Get<WindowAdapter>().RegisterWindowManagerAgent(
614         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_UPDATE, pImpl_->windowUpdateListenerAgent_);
615     if (ret != WMError::WM_OK) {
616         WLOGFW("RegisterWindowManagerAgent failed!");
617         pImpl_->windowUpdateListenerAgent_ = nullptr;
618     } else {
619         auto iter = std::find(pImpl_->windowUpdateListeners_.begin(), pImpl_->windowUpdateListeners_.end(), listener);
620         if (iter != pImpl_->windowUpdateListeners_.end()) {
621             WLOGI("Listener is already registered.");
622             return WMError::WM_OK;
623         }
624         pImpl_->windowUpdateListeners_.emplace_back(listener);
625     }
626     return ret;
627 }
628 
UnregisterWindowUpdateListener(const sptr<IWindowUpdateListener>& listener)629 WMError WindowManager::UnregisterWindowUpdateListener(const sptr<IWindowUpdateListener>& listener)
630 {
631     if (listener == nullptr) {
632         WLOGFE("listener could not be null");
633         return WMError::WM_ERROR_NULLPTR;
634     }
635     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
636     auto iter = std::find(pImpl_->windowUpdateListeners_.begin(), pImpl_->windowUpdateListeners_.end(), listener);
637     if (iter == pImpl_->windowUpdateListeners_.end()) {
638         WLOGFE("could not find this listener");
639         return WMError::WM_OK;
640     }
641     pImpl_->windowUpdateListeners_.erase(iter);
642     WMError ret = WMError::WM_OK;
643     if (pImpl_->windowUpdateListeners_.empty() && pImpl_->windowUpdateListenerAgent_ != nullptr) {
644         ret = SingletonContainer::Get<WindowAdapter>().UnregisterWindowManagerAgent(
645             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_UPDATE, pImpl_->windowUpdateListenerAgent_);
646         if (ret == WMError::WM_OK) {
647             pImpl_->windowUpdateListenerAgent_ = nullptr;
648         }
649     }
650     return ret;
651 }
652 
RegisterVisibilityChangedListener(const sptr<IVisibilityChangedListener>& listener)653 WMError WindowManager::RegisterVisibilityChangedListener(const sptr<IVisibilityChangedListener>& listener)
654 {
655     if (listener == nullptr) {
656         WLOGFE("listener could not be null");
657         return WMError::WM_ERROR_NULLPTR;
658     }
659     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
660     WMError ret = WMError::WM_OK;
661     if (pImpl_->windowVisibilityListenerAgent_ == nullptr) {
662         pImpl_->windowVisibilityListenerAgent_ = new WindowManagerAgent();
663     }
664     ret = SingletonContainer::Get<WindowAdapter>().RegisterWindowManagerAgent(
665         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_VISIBILITY,
666         pImpl_->windowVisibilityListenerAgent_);
667     if (ret != WMError::WM_OK) {
668         WLOGFW("RegisterWindowManagerAgent failed!");
669         pImpl_->windowVisibilityListenerAgent_ = nullptr;
670     } else {
671         auto iter = std::find(pImpl_->windowVisibilityListeners_.begin(), pImpl_->windowVisibilityListeners_.end(),
672             listener);
673         if (iter != pImpl_->windowVisibilityListeners_.end()) {
674             WLOGFW("Listener is already registered.");
675             return WMError::WM_OK;
676         }
677         pImpl_->windowVisibilityListeners_.emplace_back(listener);
678     }
679     return ret;
680 }
681 
UnregisterVisibilityChangedListener(const sptr<IVisibilityChangedListener>& listener)682 WMError WindowManager::UnregisterVisibilityChangedListener(const sptr<IVisibilityChangedListener>& listener)
683 {
684     if (listener == nullptr) {
685         WLOGFE("listener could not be null");
686         return WMError::WM_ERROR_NULLPTR;
687     }
688     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
689     pImpl_->windowVisibilityListeners_.erase(std::remove_if(pImpl_->windowVisibilityListeners_.begin(),
690         pImpl_->windowVisibilityListeners_.end(), [listener](sptr<IVisibilityChangedListener> registeredListener) {
691             return registeredListener == listener;
692         }), pImpl_->windowVisibilityListeners_.end());
693 
694     WMError ret = WMError::WM_OK;
695     if (pImpl_->windowVisibilityListeners_.empty() && pImpl_->windowVisibilityListenerAgent_ != nullptr) {
696         ret = SingletonContainer::Get<WindowAdapter>().UnregisterWindowManagerAgent(
697             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_VISIBILITY,
698             pImpl_->windowVisibilityListenerAgent_);
699         if (ret == WMError::WM_OK) {
700             pImpl_->windowVisibilityListenerAgent_ = nullptr;
701         }
702     }
703     return ret;
704 }
705 
RegisterCameraFloatWindowChangedListener(const sptr<ICameraFloatWindowChangedListener>& listener)706 WMError WindowManager::RegisterCameraFloatWindowChangedListener(const sptr<ICameraFloatWindowChangedListener>& listener)
707 {
708     if (listener == nullptr) {
709         WLOGFE("listener could not be null");
710         return WMError::WM_ERROR_NULLPTR;
711     }
712 
713     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
714     WMError ret = WMError::WM_OK;
715     if (pImpl_->cameraFloatWindowChangedListenerAgent_ == nullptr) {
716         pImpl_->cameraFloatWindowChangedListenerAgent_ = new WindowManagerAgent();
717     }
718     ret = SingletonContainer::Get<WindowAdapter>().RegisterWindowManagerAgent(
719         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_FLOAT,
720         pImpl_->cameraFloatWindowChangedListenerAgent_);
721     if (ret != WMError::WM_OK) {
722         WLOGFW("RegisterWindowManagerAgent failed!");
723         pImpl_->cameraFloatWindowChangedListenerAgent_ = nullptr;
724     } else {
725         auto iter = std::find(pImpl_->cameraFloatWindowChangedListeners_.begin(),
726             pImpl_->cameraFloatWindowChangedListeners_.end(), listener);
727         if (iter != pImpl_->cameraFloatWindowChangedListeners_.end()) {
728             WLOGFW("Listener is already registered.");
729             return WMError::WM_OK;
730         }
731         pImpl_->cameraFloatWindowChangedListeners_.push_back(listener);
732     }
733     return ret;
734 }
735 
UnregisterCameraFloatWindowChangedListener( const sptr<ICameraFloatWindowChangedListener>& listener)736 WMError WindowManager::UnregisterCameraFloatWindowChangedListener(
737     const sptr<ICameraFloatWindowChangedListener>& listener)
738 {
739     if (listener == nullptr) {
740         WLOGFE("listener could not be null");
741         return WMError::WM_ERROR_NULLPTR;
742     }
743 
744     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
745     auto iter = std::find(pImpl_->cameraFloatWindowChangedListeners_.begin(),
746         pImpl_->cameraFloatWindowChangedListeners_.end(), listener);
747     if (iter == pImpl_->cameraFloatWindowChangedListeners_.end()) {
748         WLOGFE("could not find this listener");
749         return WMError::WM_OK;
750     }
751     pImpl_->cameraFloatWindowChangedListeners_.erase(iter);
752     WMError ret = WMError::WM_OK;
753     if (pImpl_->cameraFloatWindowChangedListeners_.empty() &&
754         pImpl_->cameraFloatWindowChangedListenerAgent_ != nullptr) {
755         ret = SingletonContainer::Get<WindowAdapter>().UnregisterWindowManagerAgent(
756             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_FLOAT,
757             pImpl_->cameraFloatWindowChangedListenerAgent_);
758         if (ret == WMError::WM_OK) {
759             pImpl_->cameraFloatWindowChangedListenerAgent_ = nullptr;
760         }
761     }
762     return ret;
763 }
764 
RegisterWaterMarkFlagChangedListener(const sptr<IWaterMarkFlagChangedListener>& listener)765 WMError WindowManager::RegisterWaterMarkFlagChangedListener(const sptr<IWaterMarkFlagChangedListener>& listener)
766 {
767     if (listener == nullptr) {
768         WLOGFE("listener could not be null");
769         return WMError::WM_ERROR_NULLPTR;
770     }
771 
772     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
773     WMError ret = WMError::WM_OK;
774     if (pImpl_->waterMarkFlagChangeAgent_ == nullptr) {
775         pImpl_->waterMarkFlagChangeAgent_ = new WindowManagerAgent();
776     }
777     ret = SingletonContainer::Get<WindowAdapter>().RegisterWindowManagerAgent(
778         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WATER_MARK_FLAG,
779         pImpl_->waterMarkFlagChangeAgent_);
780     if (ret != WMError::WM_OK) {
781         WLOGFW("RegisterWindowManagerAgent failed!");
782         pImpl_->waterMarkFlagChangeAgent_ = nullptr;
783     } else {
784         auto iter = std::find(pImpl_->waterMarkFlagChangeListeners_.begin(),
785             pImpl_->waterMarkFlagChangeListeners_.end(), listener);
786         if (iter != pImpl_->waterMarkFlagChangeListeners_.end()) {
787             WLOGFW("Listener is already registered.");
788             return WMError::WM_OK;
789         }
790         pImpl_->waterMarkFlagChangeListeners_.push_back(listener);
791     }
792     WLOGFD("Try to registerWaterMarkFlagChangedListener && result : %{public}u", static_cast<uint32_t>(ret));
793     return ret;
794 }
795 
UnregisterWaterMarkFlagChangedListener(const sptr<IWaterMarkFlagChangedListener>& listener)796 WMError WindowManager::UnregisterWaterMarkFlagChangedListener(const sptr<IWaterMarkFlagChangedListener>& listener)
797 {
798     if (listener == nullptr) {
799         WLOGFE("listener could not be null");
800         return WMError::WM_ERROR_NULLPTR;
801     }
802 
803     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
804     auto iter = std::find(pImpl_->waterMarkFlagChangeListeners_.begin(),
805         pImpl_->waterMarkFlagChangeListeners_.end(), listener);
806     if (iter == pImpl_->waterMarkFlagChangeListeners_.end()) {
807         WLOGFE("could not find this listener");
808         return WMError::WM_OK;
809     }
810     pImpl_->waterMarkFlagChangeListeners_.erase(iter);
811     WMError ret = WMError::WM_OK;
812     if (pImpl_->waterMarkFlagChangeListeners_.empty() &&
813         pImpl_->waterMarkFlagChangeAgent_ != nullptr) {
814         ret = SingletonContainer::Get<WindowAdapter>().UnregisterWindowManagerAgent(
815             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WATER_MARK_FLAG,
816             pImpl_->waterMarkFlagChangeAgent_);
817         if (ret == WMError::WM_OK) {
818             pImpl_->waterMarkFlagChangeAgent_ = nullptr;
819         }
820     }
821     WLOGFD("Try to unregisterWaterMarkFlagChangedListener && result : %{public}u", static_cast<uint32_t>(ret));
822     return ret;
823 }
824 
RegisterGestureNavigationEnabledChangedListener( const sptr<IGestureNavigationEnabledChangedListener>& listener)825 WMError WindowManager::RegisterGestureNavigationEnabledChangedListener(
826     const sptr<IGestureNavigationEnabledChangedListener>& listener)
827 {
828     if (listener == nullptr) {
829         WLOGFE("listener could not be null");
830         return WMError::WM_ERROR_NULLPTR;
831     }
832 
833     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
834     WMError ret = WMError::WM_OK;
835     if (pImpl_->gestureNavigationEnabledAgent_ == nullptr) {
836         pImpl_->gestureNavigationEnabledAgent_ = new (std::nothrow)WindowManagerAgent();
837     }
838     if (pImpl_->gestureNavigationEnabledAgent_ != nullptr) {
839         ret = SingletonContainer::Get<WindowAdapter>().RegisterWindowManagerAgent(
840             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_GESTURE_NAVIGATION_ENABLED,
841             pImpl_->gestureNavigationEnabledAgent_);
842     } else {
843         WLOGFE("Create windowManagerAgent object failed!");
844         ret = WMError::WM_ERROR_NULLPTR;
845     }
846     if (ret != WMError::WM_OK) {
847         WLOGFE("RegisterWindowManagerAgent failed!");
848         pImpl_->gestureNavigationEnabledAgent_ = nullptr;
849     } else {
850         auto iter = std::find(pImpl_->gestureNavigationEnabledListeners_.begin(),
851             pImpl_->gestureNavigationEnabledListeners_.end(), listener);
852         if (iter != pImpl_->gestureNavigationEnabledListeners_.end()) {
853             WLOGFW("Listener is already registered.");
854             return WMError::WM_OK;
855         }
856         pImpl_->gestureNavigationEnabledListeners_.push_back(listener);
857     }
858     WLOGFD("Try to registerGestureNavigationEnabledChangedListener and result is %{public}u",
859         static_cast<uint32_t>(ret));
860     return ret;
861 }
862 
UnregisterGestureNavigationEnabledChangedListener( const sptr<IGestureNavigationEnabledChangedListener>& listener)863 WMError WindowManager::UnregisterGestureNavigationEnabledChangedListener(
864     const sptr<IGestureNavigationEnabledChangedListener>& listener)
865 {
866     if (listener == nullptr) {
867         WLOGFE("listener could not be null");
868         return WMError::WM_ERROR_NULLPTR;
869     }
870 
871     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
872     auto iter = std::find(pImpl_->gestureNavigationEnabledListeners_.begin(),
873         pImpl_->gestureNavigationEnabledListeners_.end(), listener);
874     if (iter == pImpl_->gestureNavigationEnabledListeners_.end()) {
875         WLOGFE("could not find this listener");
876         return WMError::WM_ERROR_INVALID_PARAM;
877     }
878     pImpl_->gestureNavigationEnabledListeners_.erase(iter);
879     WMError ret = WMError::WM_OK;
880     if (pImpl_->gestureNavigationEnabledListeners_.empty() &&
881         pImpl_->gestureNavigationEnabledAgent_ != nullptr) {
882         ret = SingletonContainer::Get<WindowAdapter>().UnregisterWindowManagerAgent(
883             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_GESTURE_NAVIGATION_ENABLED,
884             pImpl_->gestureNavigationEnabledAgent_);
885         if (ret == WMError::WM_OK) {
886             pImpl_->gestureNavigationEnabledAgent_ = nullptr;
887         }
888     }
889     WLOGFD("Try to unregisterGestureNavigationEnabledChangedListener and result is %{public}u",
890         static_cast<uint32_t>(ret));
891     return ret;
892 }
893 
RegisterDisplayInfoChangedListener(const sptr<IRemoteObject>& token, const sptr<IDisplayInfoChangedListener>& listener)894 WMError WindowManager::RegisterDisplayInfoChangedListener(const sptr<IRemoteObject>& token,
895     const sptr<IDisplayInfoChangedListener>& listener)
896 {
897     if (token == nullptr) {
898         TLOGE(WmsLogTag::DMS, "ability token could not be null");
899         return WMError::WM_ERROR_NULLPTR;
900     }
901 
902     if (listener == nullptr) {
903         TLOGE(WmsLogTag::DMS, "listener could not be null");
904         return WMError::WM_ERROR_NULLPTR;
905     }
906 
907     sptr<WindowDisplayChangeAdapter> listenerAdapter = new (std::nothrow) WindowDisplayChangeAdapter(token, listener);
908     if (listenerAdapter == nullptr) {
909         TLOGE(WmsLogTag::DMS, "create listener adapter failed.");
910         return WMError::WM_ERROR_NO_MEM;
911     }
912     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
913     auto iter = pImpl_->displayInfoChangedListeners_.find(token);
914     if (iter == pImpl_->displayInfoChangedListeners_.end()) {
915         pImpl_->displayInfoChangedListeners_.insert({token, {listenerAdapter}});
916     } else {
917         auto listerneIter = std::find_if(iter->second.begin(), iter->second.end(),
918             [&listener](const sptr<WindowDisplayChangeAdapter>& item) {
919                 return listener == item->GetListener();
920             });
921         if (listerneIter != iter->second.end()) {
922             TLOGW(WmsLogTag::DMS, "listener is already registered.");
923         } else {
924             iter->second.push_back(listenerAdapter);
925         }
926     }
927     TLOGD(WmsLogTag::DMS, "try to registerDisplayInfoChangedListener success");
928     return WMError::WM_OK;
929 }
930 
UnregisterDisplayInfoChangedListener(const sptr<IRemoteObject>& token, const sptr<IDisplayInfoChangedListener>& listener)931 WMError WindowManager::UnregisterDisplayInfoChangedListener(const sptr<IRemoteObject>& token,
932     const sptr<IDisplayInfoChangedListener>& listener)
933 {
934     if (token == nullptr) {
935         TLOGE(WmsLogTag::DMS, "ability token could not be null");
936         return WMError::WM_ERROR_NULLPTR;
937     }
938 
939     if (listener == nullptr) {
940         TLOGE(WmsLogTag::DMS, "listener could not be null");
941         return WMError::WM_ERROR_NULLPTR;
942     }
943 
944     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
945     auto iter = pImpl_->displayInfoChangedListeners_.find(token);
946     if (iter == pImpl_->displayInfoChangedListeners_.end()) {
947         TLOGW(WmsLogTag::DMS, "can not find the ability token");
948     } else {
949         auto listerneIter = std::find_if(iter->second.begin(), iter->second.end(),
950             [&listener](sptr<WindowDisplayChangeAdapter>& item) {
951                 return listener == item->GetListener();
952             });
953         if (listerneIter == iter->second.end()) {
954             TLOGW(WmsLogTag::DMS, "can not find the listener.");
955         } else {
956             iter->second.erase(listerneIter);
957             if (iter->second.empty()) {
958                 pImpl_->displayInfoChangedListeners_.erase(iter);
959             }
960         }
961     }
962     TLOGD(WmsLogTag::DMS, "try to unregisterDisplayInfoChangedListener success");
963     return WMError::WM_OK;
964 }
965 
RegisterWindowPidVisibilityChangedListener( const sptr<IWindowPidVisibilityChangedListener>& listener)966 WMError WindowManager::RegisterWindowPidVisibilityChangedListener(
967     const sptr<IWindowPidVisibilityChangedListener>& listener)
968 {
969     if (listener == nullptr) {
970         TLOGE(WmsLogTag::WMS_LIFE, "listener could not be null");
971         return WMError::WM_ERROR_NULLPTR;
972     }
973     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
974     WMError ret = WMError::WM_OK;
975     if (pImpl_->windowPidVisibilityListenerAgent_ == nullptr) {
976         pImpl_->windowPidVisibilityListenerAgent_ = sptr<WindowManagerAgent>::MakeSptr();
977     }
978     ret = SingletonContainer::Get<WindowAdapter>().RegisterWindowManagerAgent(
979         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_PID_VISIBILITY,
980         pImpl_->windowPidVisibilityListenerAgent_);
981     if (ret != WMError::WM_OK) {
982         TLOGE(WmsLogTag::WMS_LIFE, "RegisterWindowManagerAgent failed!");
983         pImpl_->windowPidVisibilityListenerAgent_ = nullptr;
984     } else {
985         auto iter = std::find(pImpl_->windowPidVisibilityListeners_.begin(),
986             pImpl_->windowPidVisibilityListeners_.end(), listener);
987         if (iter != pImpl_->windowPidVisibilityListeners_.end()) {
988             WLOGI("Listener is already registered.");
989             return WMError::WM_OK;
990         }
991         pImpl_->windowPidVisibilityListeners_.emplace_back(listener);
992     }
993     return ret;
994 }
995 
UnregisterWindowPidVisibilityChangedListener( const sptr<IWindowPidVisibilityChangedListener>& listener)996 WMError WindowManager::UnregisterWindowPidVisibilityChangedListener(
997     const sptr<IWindowPidVisibilityChangedListener>& listener)
998 {
999     if (listener == nullptr) {
1000         TLOGE(WmsLogTag::WMS_LIFE, "listener could not be null");
1001         return WMError::WM_ERROR_NULLPTR;
1002     }
1003     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
1004     auto iter = std::find(pImpl_->windowPidVisibilityListeners_.begin(),
1005         pImpl_->windowPidVisibilityListeners_.end(), listener);
1006     if (iter == pImpl_->windowPidVisibilityListeners_.end()) {
1007         TLOGE(WmsLogTag::WMS_LIFE, "could not find this listener");
1008         return WMError::WM_OK;
1009     }
1010     pImpl_->windowPidVisibilityListeners_.erase(iter);
1011     WMError ret = WMError::WM_OK;
1012     if (pImpl_->windowPidVisibilityListeners_.empty() && pImpl_->windowPidVisibilityListenerAgent_ != nullptr) {
1013         ret = SingletonContainer::Get<WindowAdapter>().UnregisterWindowManagerAgent(
1014             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_PID_VISIBILITY,
1015             pImpl_->windowPidVisibilityListenerAgent_);
1016         if (ret == WMError::WM_OK) {
1017             pImpl_->windowPidVisibilityListenerAgent_ = nullptr;
1018         }
1019     }
1020     return ret;
1021 }
1022 
NotifyDisplayInfoChange(const sptr<IRemoteObject>& token, DisplayId displayId, float density, DisplayOrientation orientation)1023 WMError WindowManager::NotifyDisplayInfoChange(const sptr<IRemoteObject>& token, DisplayId displayId,
1024     float density, DisplayOrientation orientation)
1025 {
1026     TLOGD(WmsLogTag::DMS, "notify display info change, displayid = %{public}" PRIu64", density=%{public}f," \
1027         "orientation = %{public}d", displayId, density, orientation);
1028     if (token == nullptr) {
1029         TLOGE(WmsLogTag::DMS, "notify display info change failed, token is nullptr");
1030         return WMError::WM_ERROR_INVALID_PARAM;
1031     }
1032     pImpl_->NotifyDisplayInfoChanged(token, displayId, density, orientation);
1033     return WMError::WM_OK;
1034 }
1035 
GetFocusWindowInfo(FocusChangeInfo& focusInfo)1036 void WindowManager::GetFocusWindowInfo(FocusChangeInfo& focusInfo)
1037 {
1038     SingletonContainer::Get<WindowAdapter>().GetFocusWindowInfo(focusInfo);
1039 }
1040 
OnWMSConnectionChanged(int32_t userId, int32_t screenId, bool isConnected) const1041 void WindowManager::OnWMSConnectionChanged(int32_t userId, int32_t screenId, bool isConnected) const
1042 {
1043     if (isConnected) {
1044         pImpl_->NotifyWMSConnected(userId, screenId);
1045     } else {
1046         pImpl_->NotifyWMSDisconnected(userId, screenId);
1047     }
1048 }
1049 
UpdateFocusChangeInfo(const sptr<FocusChangeInfo>& focusChangeInfo, bool focused) const1050 void WindowManager::UpdateFocusChangeInfo(const sptr<FocusChangeInfo>& focusChangeInfo, bool focused) const
1051 {
1052     if (focusChangeInfo == nullptr) {
1053         WLOGFE("focusChangeInfo is nullptr.");
1054         return;
1055     }
1056     TLOGD(WmsLogTag::WMS_FOCUS, "window focus change: %{public}d, id: %{public}u", focused, focusChangeInfo->windowId_);
1057     if (focused) {
1058         pImpl_->NotifyFocused(focusChangeInfo);
1059     } else {
1060         pImpl_->NotifyUnfocused(focusChangeInfo);
1061     }
1062 }
1063 
UpdateWindowModeTypeInfo(WindowModeType type) const1064 void WindowManager::UpdateWindowModeTypeInfo(WindowModeType type) const
1065 {
1066     pImpl_->NotifyWindowModeChange(type);
1067 }
1068 
1069 
GetWindowModeType(WindowModeType& windowModeType) const1070 WMError WindowManager::GetWindowModeType(WindowModeType& windowModeType) const
1071 {
1072     WMError ret = SingletonContainer::Get<WindowAdapter>().GetWindowModeType(windowModeType);
1073     if (ret != WMError::WM_OK) {
1074         WLOGFE("get window mode type failed");
1075     }
1076     return ret;
1077 }
1078 
UpdateSystemBarRegionTints(DisplayId displayId, const SystemBarRegionTints& tints) const1079 void WindowManager::UpdateSystemBarRegionTints(DisplayId displayId,
1080     const SystemBarRegionTints& tints) const
1081 {
1082     pImpl_->NotifySystemBarChanged(displayId, tints);
1083 }
1084 
NotifyAccessibilityWindowInfo(const std::vector<sptr<AccessibilityWindowInfo>>& infos, WindowUpdateType type) const1085 void WindowManager::NotifyAccessibilityWindowInfo(const std::vector<sptr<AccessibilityWindowInfo>>& infos,
1086     WindowUpdateType type) const
1087 {
1088     pImpl_->NotifyAccessibilityWindowInfo(infos, type);
1089 }
1090 
UpdateWindowVisibilityInfo( const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfos) const1091 void WindowManager::UpdateWindowVisibilityInfo(
1092     const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfos) const
1093 {
1094     pImpl_->NotifyWindowVisibilityInfoChanged(windowVisibilityInfos);
1095 }
1096 
UpdateWindowDrawingContentInfo( const std::vector<sptr<WindowDrawingContentInfo>>& windowDrawingContentInfos) const1097 void WindowManager::UpdateWindowDrawingContentInfo(
1098     const std::vector<sptr<WindowDrawingContentInfo>>& windowDrawingContentInfos) const
1099 {
1100     pImpl_->NotifyWindowDrawingContentInfoChanged(windowDrawingContentInfos);
1101 }
1102 
GetAccessibilityWindowInfo(std::vector<sptr<AccessibilityWindowInfo>>& infos) const1103 WMError WindowManager::GetAccessibilityWindowInfo(std::vector<sptr<AccessibilityWindowInfo>>& infos) const
1104 {
1105     WMError ret = SingletonContainer::Get<WindowAdapter>().GetAccessibilityWindowInfo(infos);
1106     if (ret != WMError::WM_OK) {
1107         WLOGFE("get window info failed");
1108     }
1109     return ret;
1110 }
1111 
GetUnreliableWindowInfo(int32_t windowId, std::vector<sptr<UnreliableWindowInfo>>& infos) const1112 WMError WindowManager::GetUnreliableWindowInfo(int32_t windowId,
1113     std::vector<sptr<UnreliableWindowInfo>>& infos) const
1114 {
1115     WMError ret = SingletonContainer::Get<WindowAdapter>().GetUnreliableWindowInfo(windowId, infos);
1116     if (ret != WMError::WM_OK) {
1117         TLOGE(WmsLogTag::DEFAULT, "get unreliable window info failed");
1118     }
1119     return ret;
1120 }
1121 
GetVisibilityWindowInfo(std::vector<sptr<WindowVisibilityInfo>>& infos) const1122 WMError WindowManager::GetVisibilityWindowInfo(std::vector<sptr<WindowVisibilityInfo>>& infos) const
1123 {
1124     WMError ret = SingletonContainer::Get<WindowAdapter>().GetVisibilityWindowInfo(infos);
1125     if (ret != WMError::WM_OK) {
1126         WLOGFE("get window visibility info failed");
1127     }
1128     return ret;
1129 }
1130 
DumpSessionAll(std::vector<std::string>& infos)1131 WMError WindowManager::DumpSessionAll(std::vector<std::string>& infos)
1132 {
1133     WMError ret = SingletonContainer::Get<WindowAdapter>().DumpSessionAll(infos);
1134     if (ret != WMError::WM_OK) {
1135         WLOGFE("dump session all failed");
1136     }
1137     return ret;
1138 }
1139 
DumpSessionWithId(int32_t persistentId, std::vector<std::string>& infos)1140 WMError WindowManager::DumpSessionWithId(int32_t persistentId, std::vector<std::string>& infos)
1141 {
1142     WMError ret = SingletonContainer::Get<WindowAdapter>().DumpSessionWithId(persistentId, infos);
1143     if (ret != WMError::WM_OK) {
1144         WLOGFE("dump session with id failed");
1145     }
1146     return ret;
1147 }
1148 
GetUIContentRemoteObj(int32_t windowId, sptr<IRemoteObject>& uiContentRemoteObj)1149 WMError WindowManager::GetUIContentRemoteObj(int32_t windowId, sptr<IRemoteObject>& uiContentRemoteObj)
1150 {
1151     WMError ret = SingletonContainer::Get<WindowAdapter>().GetUIContentRemoteObj(windowId, uiContentRemoteObj);
1152     if (ret != WMError::WM_OK) {
1153         TLOGE(WmsLogTag::DEFAULT, "Failed to get UIContentRemoteObj. PersistentId=%{public}d; ret=%{public}u",
1154             windowId, static_cast<uint32_t>(ret));
1155     }
1156     return ret;
1157 }
1158 
SetGestureNavigationEnabled(bool enable) const1159 WMError WindowManager::SetGestureNavigationEnabled(bool enable) const
1160 {
1161     WMError ret = SingletonContainer::Get<WindowAdapter>().SetGestureNavigationEnabled(enable);
1162     if (ret != WMError::WM_OK) {
1163         WLOGFE("set gesture navigation enabled failed");
1164     }
1165     return ret;
1166 }
1167 
NotifyWindowExtensionVisibilityChange(int32_t pid, int32_t uid, bool visible)1168 WMError WindowManager::NotifyWindowExtensionVisibilityChange(int32_t pid, int32_t uid, bool visible)
1169 {
1170     WMError ret = SingletonContainer::Get<WindowAdapter>().NotifyWindowExtensionVisibilityChange(pid, uid, visible);
1171     if (ret != WMError::WM_OK) {
1172         WLOGFE("notify WindowExtension visibility change failed");
1173     }
1174     return ret;
1175 }
1176 
UpdateCameraFloatWindowStatus(uint32_t accessTokenId, bool isShowing) const1177 void WindowManager::UpdateCameraFloatWindowStatus(uint32_t accessTokenId, bool isShowing) const
1178 {
1179     pImpl_->UpdateCameraFloatWindowStatus(accessTokenId, isShowing);
1180 }
1181 
NotifyWaterMarkFlagChangedResult(bool showWaterMark) const1182 void WindowManager::NotifyWaterMarkFlagChangedResult(bool showWaterMark) const
1183 {
1184     pImpl_->NotifyWaterMarkFlagChangedResult(showWaterMark);
1185 }
1186 
NotifyGestureNavigationEnabledResult(bool enable) const1187 void WindowManager::NotifyGestureNavigationEnabledResult(bool enable) const
1188 {
1189     pImpl_->NotifyGestureNavigationEnabledResult(enable);
1190 }
1191 
NotifyWindowPidVisibilityChanged(const sptr<WindowPidVisibilityInfo>& info) const1192 void WindowManager::NotifyWindowPidVisibilityChanged(const sptr<WindowPidVisibilityInfo>& info) const
1193 {
1194     pImpl_->NotifyWindowPidVisibilityChanged(info);
1195 }
1196 
RaiseWindowToTop(int32_t persistentId)1197 WMError WindowManager::RaiseWindowToTop(int32_t persistentId)
1198 {
1199     WMError ret = SingletonContainer::Get<WindowAdapter>().RaiseWindowToTop(persistentId);
1200     if (ret != WMError::WM_OK) {
1201         WLOGFE("raise window to top failed");
1202     }
1203     return ret;
1204 }
1205 
NotifyWindowStyleChange(WindowStyleType type)1206 WMError WindowManager::NotifyWindowStyleChange(WindowStyleType type)
1207 {
1208     pImpl_->NotifyWindowStyleChange(type);
1209     return WMError::WM_OK;
1210 }
1211 
RegisterDrawingContentChangedListener(const sptr<IDrawingContentChangedListener>& listener)1212 WMError WindowManager::RegisterDrawingContentChangedListener(const sptr<IDrawingContentChangedListener>& listener)
1213 {
1214     if (listener == nullptr) {
1215         WLOGFE("listener could not be null");
1216         return WMError::WM_ERROR_NULLPTR;
1217     }
1218     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
1219     WMError ret = WMError::WM_OK;
1220     if (pImpl_->windowDrawingContentListenerAgent_ == nullptr) {
1221         pImpl_->windowDrawingContentListenerAgent_ = new WindowManagerAgent();
1222     }
1223     ret = SingletonContainer::Get<WindowAdapter>().RegisterWindowManagerAgent(
1224         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_DRAWING_STATE,
1225         pImpl_->windowDrawingContentListenerAgent_);
1226     if (ret != WMError::WM_OK) {
1227         WLOGFW("RegisterWindowManagerAgent failed!");
1228         pImpl_->windowDrawingContentListenerAgent_ = nullptr;
1229     } else {
1230         auto iter = std::find(pImpl_->windowDrawingContentListeners_.begin(),
1231             pImpl_->windowDrawingContentListeners_.end(), listener);
1232         if (iter != pImpl_->windowDrawingContentListeners_.end()) {
1233             WLOGFW("Listener is already registered.");
1234             return WMError::WM_OK;
1235         }
1236         pImpl_->windowDrawingContentListeners_.emplace_back(listener);
1237     }
1238     return ret;
1239 }
1240 
UnregisterDrawingContentChangedListener(const sptr<IDrawingContentChangedListener>& listener)1241 WMError WindowManager::UnregisterDrawingContentChangedListener(const sptr<IDrawingContentChangedListener>& listener)
1242 {
1243     if (listener == nullptr) {
1244         WLOGFE("listener could not be null");
1245         return WMError::WM_ERROR_NULLPTR;
1246     }
1247     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
1248     pImpl_->windowDrawingContentListeners_.erase(std::remove_if(pImpl_->windowDrawingContentListeners_.begin(),
1249         pImpl_->windowDrawingContentListeners_.end(),
1250         [listener](sptr<IDrawingContentChangedListener> registeredListener) { return registeredListener == listener; }),
1251         pImpl_->windowDrawingContentListeners_.end());
1252 
1253     WMError ret = WMError::WM_OK;
1254     if (pImpl_->windowDrawingContentListeners_.empty() && pImpl_->windowDrawingContentListenerAgent_ != nullptr) {
1255         ret = SingletonContainer::Get<WindowAdapter>().UnregisterWindowManagerAgent(
1256             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_DRAWING_STATE,
1257             pImpl_->windowDrawingContentListenerAgent_);
1258         if (ret == WMError::WM_OK) {
1259             pImpl_->windowDrawingContentListenerAgent_ = nullptr;
1260         }
1261     }
1262     return ret;
1263 }
1264 
ShiftAppWindowFocus(int32_t sourcePersistentId, int32_t targetPersistentId)1265 WMError WindowManager::ShiftAppWindowFocus(int32_t sourcePersistentId, int32_t targetPersistentId)
1266 {
1267     WMError ret = SingletonContainer::Get<WindowAdapter>().ShiftAppWindowFocus(sourcePersistentId, targetPersistentId);
1268     if (ret != WMError::WM_OK) {
1269         WLOGFE("shift application window focus failed");
1270     }
1271     return ret;
1272 }
1273 
RegisterVisibleWindowNumChangedListener(const sptr<IVisibleWindowNumChangedListener>& listener)1274 WMError WindowManager::RegisterVisibleWindowNumChangedListener(const sptr<IVisibleWindowNumChangedListener>& listener)
1275 {
1276     if (listener == nullptr) {
1277         TLOGE(WmsLogTag::WMS_MAIN, "listener could not be null");
1278         return WMError::WM_ERROR_NULLPTR;
1279     }
1280     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
1281     WMError ret = WMError::WM_OK;
1282     if (pImpl_->visibleWindowNumChangedListenerAgent_ == nullptr) {
1283         pImpl_->visibleWindowNumChangedListenerAgent_ = new WindowManagerAgent();
1284     }
1285     ret = SingletonContainer::Get<WindowAdapter>().RegisterWindowManagerAgent(
1286         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_VISIBLE_WINDOW_NUM,
1287         pImpl_->visibleWindowNumChangedListenerAgent_);
1288     if (ret != WMError::WM_OK) {
1289         TLOGE(WmsLogTag::WMS_MAIN, "RegisterWindowManagerAgent failed!");
1290         pImpl_->visibleWindowNumChangedListenerAgent_ = nullptr;
1291     } else {
1292         auto iter = std::find(pImpl_->visibleWindowNumChangedListeners_.begin(),
1293             pImpl_->visibleWindowNumChangedListeners_.end(), listener);
1294         if (iter != pImpl_->visibleWindowNumChangedListeners_.end()) {
1295             TLOGE(WmsLogTag::WMS_MAIN, "Listener is already registered.");
1296             return WMError::WM_OK;
1297         }
1298         pImpl_->visibleWindowNumChangedListeners_.emplace_back(listener);
1299     }
1300     return ret;
1301 }
1302 
GetSnapshotByWindowId(int32_t windowId, std::shared_ptr<Media::PixelMap>& pixelMap)1303 WMError WindowManager::GetSnapshotByWindowId(int32_t windowId, std::shared_ptr<Media::PixelMap>& pixelMap)
1304 {
1305     return SingletonContainer::Get<WindowAdapter>().GetSnapshotByWindowId(windowId, pixelMap);
1306 }
1307 
UnregisterVisibleWindowNumChangedListener(const sptr<IVisibleWindowNumChangedListener>& listener)1308 WMError WindowManager::UnregisterVisibleWindowNumChangedListener(const sptr<IVisibleWindowNumChangedListener>& listener)
1309 {
1310     if (listener == nullptr) {
1311         TLOGE(WmsLogTag::WMS_MAIN, "listener could not be null");
1312         return WMError::WM_ERROR_NULLPTR;
1313     }
1314     std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
1315     auto iter = std::find(pImpl_->visibleWindowNumChangedListeners_.begin(),
1316         pImpl_->visibleWindowNumChangedListeners_.end(), listener);
1317     if (iter == pImpl_->visibleWindowNumChangedListeners_.end()) {
1318         TLOGE(WmsLogTag::WMS_MAIN, "could not find this listener");
1319         return WMError::WM_OK;
1320     }
1321 
1322     WMError ret = WMError::WM_OK;
1323     if (pImpl_->visibleWindowNumChangedListeners_.empty() && pImpl_->visibleWindowNumChangedListenerAgent_ != nullptr) {
1324         ret = SingletonContainer::Get<WindowAdapter>().UnregisterWindowManagerAgent(
1325             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_VISIBLE_WINDOW_NUM,
1326             pImpl_->visibleWindowNumChangedListenerAgent_);
1327         if (ret == WMError::WM_OK) {
1328             pImpl_->visibleWindowNumChangedListenerAgent_ = nullptr;
1329         }
1330     }
1331     return ret;
1332 }
1333 
UpdateVisibleWindowNum(const std::vector<VisibleWindowNumInfo>& visibleWindowNumInfo)1334 void WindowManager::UpdateVisibleWindowNum(const std::vector<VisibleWindowNumInfo>& visibleWindowNumInfo)
1335 {
1336     pImpl_->NotifyVisibleWindowNumChanged(visibleWindowNumInfo);
1337 }
1338 
1339 
RegisterWindowStyleChangedListener(const sptr<IWindowStyleChangedListener>& listener)1340 WMError WindowManager::RegisterWindowStyleChangedListener(const sptr<IWindowStyleChangedListener>& listener)
1341 {
1342     TLOGI(WmsLogTag::WMS_MAIN, "start register");
1343     if (listener == nullptr) {
1344         TLOGE(WmsLogTag::WMS_MAIN, "listener could not be null");
1345         return WMError::WM_ERROR_NULLPTR;
1346     }
1347     {
1348         std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
1349         if (pImpl_->windowStyleListenerAgent_ == nullptr) {
1350             pImpl_->windowStyleListenerAgent_ = new WindowManagerAgent();
1351         }
1352         auto iter = std::find(pImpl_->windowStyleListeners_.begin(), pImpl_->windowStyleListeners_.end(), listener);
1353         if (iter != pImpl_->windowStyleListeners_.end()) {
1354             TLOGW(WmsLogTag::WMS_MAIN, "Listener is already registered.");
1355             return WMError::WM_OK;
1356         }
1357         pImpl_->windowStyleListeners_.push_back(listener);
1358     }
1359     WMError ret = WMError::WM_OK;
1360     ret = SingletonContainer::Get<WindowAdapter>().RegisterWindowManagerAgent(
1361         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_STYLE, pImpl_->windowStyleListenerAgent_);
1362     if (ret != WMError::WM_OK) {
1363         TLOGW(WmsLogTag::WMS_MAIN, "RegisterWindowManagerAgent failed!");
1364         std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
1365         pImpl_->windowStyleListenerAgent_ = nullptr;
1366         auto iter = std::find(pImpl_->windowStyleListeners_.begin(), pImpl_->windowStyleListeners_.end(), listener);
1367         if (iter != pImpl_->windowStyleListeners_.end()) {
1368             pImpl_->windowStyleListeners_.erase(iter);
1369         }
1370     }
1371     return ret;
1372 }
1373 
UnregisterWindowStyleChangedListener(const sptr<IWindowStyleChangedListener>& listener)1374 WMError WindowManager::UnregisterWindowStyleChangedListener(const sptr<IWindowStyleChangedListener>& listener)
1375 {
1376     TLOGI(WmsLogTag::WMS_MAIN, "start unregister");
1377     if (listener == nullptr) {
1378         TLOGE(WmsLogTag::WMS_MAIN, "listener could not be null");
1379         return WMError::WM_ERROR_NULLPTR;
1380     }
1381     {
1382         std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
1383         auto iter = std::find(pImpl_->windowStyleListeners_.begin(), pImpl_->windowStyleListeners_.end(), listener);
1384         if (iter == pImpl_->windowStyleListeners_.end()) {
1385             TLOGE(WmsLogTag::WMS_MAIN, "could not find this listener");
1386             return WMError::WM_OK;
1387         }
1388         pImpl_->windowStyleListeners_.erase(iter);
1389     }
1390     WMError ret = WMError::WM_OK;
1391     if (pImpl_->windowStyleListeners_.empty() && pImpl_->windowStyleListenerAgent_ != nullptr) {
1392         ret = SingletonContainer::Get<WindowAdapter>().UnregisterWindowManagerAgent(
1393             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_STYLE, pImpl_->windowStyleListenerAgent_);
1394         if (ret == WMError::WM_OK) {
1395             std::unique_lock<std::shared_mutex> lock(pImpl_->listenerMutex_);
1396             pImpl_->windowStyleListenerAgent_ = nullptr;
1397         }
1398     }
1399     return ret;
1400 }
1401 
GetWindowStyleType()1402 WindowStyleType WindowManager::GetWindowStyleType()
1403 {
1404     WindowStyleType styleType;
1405     if (SingletonContainer::Get<WindowAdapter>().GetWindowStyleType(styleType) == WMError::WM_OK) {
1406         return styleType;
1407     }
1408     return styleType;
1409 }
1410 
SkipSnapshotForAppProcess(int32_t pid, bool skip)1411 WMError WindowManager::SkipSnapshotForAppProcess(int32_t pid, bool skip)
1412 {
1413     WMError ret = SingletonContainer::Get<WindowAdapter>().SkipSnapshotForAppProcess(pid, skip);
1414     if (ret != WMError::WM_OK) {
1415         TLOGE(WmsLogTag::DEFAULT, "skip failed");
1416     }
1417     return ret;
1418 }
1419 
SetProcessWatermark(int32_t pid, const std::string& watermarkName, bool isEnabled)1420 WMError WindowManager::SetProcessWatermark(int32_t pid, const std::string& watermarkName, bool isEnabled)
1421 {
1422     WMError ret = SingletonContainer::Get<WindowAdapter>().SetProcessWatermark(pid, watermarkName, isEnabled);
1423     if (ret != WMError::WM_OK) {
1424         TLOGE(WmsLogTag::DEFAULT, "failed");
1425     }
1426     return ret;
1427 }
1428 
GetWindowIdsByCoordinate(DisplayId displayId, int32_t windowNumber, int32_t x, int32_t y, std::vector<int32_t>& windowIds) const1429 WMError WindowManager::GetWindowIdsByCoordinate(DisplayId displayId, int32_t windowNumber,
1430     int32_t x, int32_t y, std::vector<int32_t>& windowIds) const
1431 {
1432     WMError ret = SingletonContainer::Get<WindowAdapter>().GetWindowIdsByCoordinate(
1433         displayId, windowNumber, x, y, windowIds);
1434     if (ret != WMError::WM_OK) {
1435         TLOGE(WmsLogTag::DEFAULT, "get windowIds by coordinate failed");
1436     }
1437     return ret;
1438 }
1439 
ReleaseForegroundSessionScreenLock()1440 WMError WindowManager::ReleaseForegroundSessionScreenLock()
1441 {
1442     WMError ret = SingletonContainer::Get<WindowAdapter>().ReleaseForegroundSessionScreenLock();
1443     if (ret != WMError::WM_OK) {
1444         TLOGE(WmsLogTag::DEFAULT, "release screen lock failed");
1445     }
1446     return ret;
1447 }
1448 } // namespace Rosen
1449 } // namespace OHOS
1450