• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 #include "marshalling_helper.h"
22 #include "window_adapter.h"
23 #include "window_manager_agent.h"
24 #include "window_manager_hilog.h"
25 #include "wm_common.h"
26 #ifdef EFFICIENCY_MANAGER_ENABLE
27 #include "suspend_manager_client.h"
28 #endif // EFFICIENCY_MANAGER_ENABLE
29 
30 namespace OHOS {
31 namespace Rosen {
32 namespace {
33     constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowManager"};
34 }
35 
36 WM_IMPLEMENT_SINGLE_INSTANCE(WindowManager)
37 
38 class WindowManager::Impl {
39 public:
Impl(std::recursive_mutex & mutex)40     explicit Impl(std::recursive_mutex& mutex) : mutex_(mutex) {}
41     void NotifyFocused(uint32_t windowId, const sptr<IRemoteObject>& abilityToken,
42         WindowType windowType, DisplayId displayId);
43     void NotifyUnfocused(uint32_t windowId, const sptr<IRemoteObject>& abilityToken,
44         WindowType windowType, DisplayId displayId);
45     void NotifyFocused(const sptr<FocusChangeInfo>& focusChangeInfo);
46     void NotifyUnfocused(const sptr<FocusChangeInfo>& focusChangeInfo);
47     void NotifySystemBarChanged(DisplayId displayId, const SystemBarRegionTints& tints);
48     void NotifyAccessibilityWindowInfo(const std::vector<sptr<AccessibilityWindowInfo>>& infos, WindowUpdateType type);
49     void NotifyWindowVisibilityInfoChanged(const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfos);
50     void UpdateCameraFloatWindowStatus(uint32_t accessTokenId, bool isShowing);
51     void NotifyWaterMarkFlagChangedResult(bool showWaterMark);
52     void NotifyGestureNavigationEnabledResult(bool enable);
53 
54     static inline SingletonDelegator<WindowManager> delegator_;
55 
56     std::recursive_mutex& mutex_;
57     std::vector<sptr<IFocusChangedListener>> focusChangedListeners_;
58     sptr<WindowManagerAgent> focusChangedListenerAgent_;
59     std::vector<sptr<ISystemBarChangedListener>> systemBarChangedListeners_;
60     sptr<WindowManagerAgent> systemBarChangedListenerAgent_;
61     std::vector<sptr<IWindowUpdateListener>> windowUpdateListeners_;
62     sptr<WindowManagerAgent> windowUpdateListenerAgent_;
63     std::vector<sptr<IVisibilityChangedListener>> windowVisibilityListeners_;
64     sptr<WindowManagerAgent> windowVisibilityListenerAgent_;
65     std::vector<sptr<ICameraFloatWindowChangedListener>> cameraFloatWindowChangedListeners_;
66     sptr<WindowManagerAgent> cameraFloatWindowChangedListenerAgent_;
67     std::vector<sptr<IWaterMarkFlagChangedListener>> waterMarkFlagChangeListeners_;
68     sptr<WindowManagerAgent> waterMarkFlagChangeAgent_;
69     std::vector<sptr<IGestureNavigationEnabledChangedListener>> gestureNavigationEnabledListeners_;
70     sptr<WindowManagerAgent> gestureNavigationEnabledAgent_;
71 };
72 
NotifyFocused(const sptr<FocusChangeInfo> & focusChangeInfo)73 void WindowManager::Impl::NotifyFocused(const sptr<FocusChangeInfo>& focusChangeInfo)
74 {
75     WLOGFD("NotifyFocused [%{public}u; %{public}" PRIu64"; %{public}d; %{public}d; %{public}u]",
76         focusChangeInfo->windowId_, focusChangeInfo->displayId_, focusChangeInfo->pid_, focusChangeInfo->uid_,
77         static_cast<uint32_t>(focusChangeInfo->windowType_));
78     std::vector<sptr<IFocusChangedListener>> focusChangeListeners;
79     {
80         std::lock_guard<std::recursive_mutex> lock(mutex_);
81         focusChangeListeners = focusChangedListeners_;
82     }
83     for (auto& listener : focusChangeListeners) {
84         listener->OnFocused(focusChangeInfo);
85     }
86 }
87 
NotifyUnfocused(const sptr<FocusChangeInfo> & focusChangeInfo)88 void WindowManager::Impl::NotifyUnfocused(const sptr<FocusChangeInfo>& focusChangeInfo)
89 {
90     WLOGFD("NotifyUnfocused [%{public}u; %{public}" PRIu64"; %{public}d; %{public}d; %{public}u]",
91         focusChangeInfo->windowId_, focusChangeInfo->displayId_, focusChangeInfo->pid_, focusChangeInfo->uid_,
92         static_cast<uint32_t>(focusChangeInfo->windowType_));
93     std::vector<sptr<IFocusChangedListener>> focusChangeListeners;
94     {
95         std::lock_guard<std::recursive_mutex> lock(mutex_);
96         focusChangeListeners = focusChangedListeners_;
97     }
98     for (auto& listener : focusChangeListeners) {
99         listener->OnUnfocused(focusChangeInfo);
100     }
101 }
102 
NotifySystemBarChanged(DisplayId displayId,const SystemBarRegionTints & tints)103 void WindowManager::Impl::NotifySystemBarChanged(DisplayId displayId, const SystemBarRegionTints& tints)
104 {
105     for (auto tint : tints) {
106         WLOGFD("type:%{public}d, enable:%{public}d," \
107             "backgroundColor:%{public}x, contentColor:%{public}x " \
108             "region:[%{public}d, %{public}d, %{public}d, %{public}d]",
109             tint.type_, tint.prop_.enable_, tint.prop_.backgroundColor_, tint.prop_.contentColor_,
110             tint.region_.posX_, tint.region_.posY_, tint.region_.width_, tint.region_.height_);
111     }
112     std::vector<sptr<ISystemBarChangedListener>> systemBarChangeListeners;
113     {
114         std::lock_guard<std::recursive_mutex> lock(mutex_);
115         systemBarChangeListeners = systemBarChangedListeners_;
116     }
117     for (auto& listener : systemBarChangeListeners) {
118         listener->OnSystemBarPropertyChange(displayId, tints);
119     }
120 }
121 
NotifyAccessibilityWindowInfo(const std::vector<sptr<AccessibilityWindowInfo>> & infos,WindowUpdateType type)122 void WindowManager::Impl::NotifyAccessibilityWindowInfo(const std::vector<sptr<AccessibilityWindowInfo>>& infos,
123     WindowUpdateType type)
124 {
125     if (infos.empty()) {
126         WLOGFE("infos is empty");
127         return;
128     }
129     for (auto& info : infos) {
130         WLOGFD("NotifyAccessibilityWindowInfo: wid[%{public}u], innerWid_[%{public}u], uiNodeId_[%{public}u]," \
131             "rect[%{public}d %{public}d %{public}d %{public}d]," \
132             "isFocused[%{public}d], isDecorEnable[%{public}d], displayId[%{public}" PRIu64"], layer[%{public}u]," \
133             "mode[%{public}u], type[%{public}u, updateType[%{public}d]",
134             info->wid_, info->innerWid_, info->uiNodeId_, info->windowRect_.width_, info->windowRect_.height_,
135             info->windowRect_.posX_, info->windowRect_.posY_, info->focused_, info->isDecorEnable_, info->displayId_,
136             info->layer_, info->mode_, info->type_, type);
137     }
138 
139     std::vector<sptr<IWindowUpdateListener>> windowUpdateListeners;
140     {
141         std::lock_guard<std::recursive_mutex> lock(mutex_);
142         windowUpdateListeners = windowUpdateListeners_;
143     }
144     for (auto& listener : windowUpdateListeners) {
145         listener->OnWindowUpdate(infos, type);
146     }
147 }
148 
NotifyWindowVisibilityInfoChanged(const std::vector<sptr<WindowVisibilityInfo>> & windowVisibilityInfos)149 void WindowManager::Impl::NotifyWindowVisibilityInfoChanged(
150     const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfos)
151 {
152     std::vector<sptr<IVisibilityChangedListener>> visibilityChangeListeners;
153     {
154         std::lock_guard<std::recursive_mutex> lock(mutex_);
155         visibilityChangeListeners = windowVisibilityListeners_;
156     }
157     for (auto& listener : visibilityChangeListeners) {
158         WLOGI("Notify WindowVisibilityInfo to caller");
159         listener->OnWindowVisibilityChanged(windowVisibilityInfos);
160     }
161 }
162 
UpdateCameraFloatWindowStatus(uint32_t accessTokenId,bool isShowing)163 void WindowManager::Impl::UpdateCameraFloatWindowStatus(uint32_t accessTokenId, bool isShowing)
164 {
165     WLOGFD("Camera float window, accessTokenId = %{public}u, isShowing = %{public}u", accessTokenId, isShowing);
166     std::vector<sptr<ICameraFloatWindowChangedListener>> cameraFloatWindowChangeListeners;
167     {
168         std::lock_guard<std::recursive_mutex> lock(mutex_);
169         cameraFloatWindowChangeListeners = cameraFloatWindowChangedListeners_;
170     }
171     for (auto& listener : cameraFloatWindowChangeListeners) {
172         listener->OnCameraFloatWindowChange(accessTokenId, isShowing);
173     }
174 }
175 
NotifyWaterMarkFlagChangedResult(bool showWaterMark)176 void WindowManager::Impl::NotifyWaterMarkFlagChangedResult(bool showWaterMark)
177 {
178     WLOGFI("Notify water mark flag changed result, showWaterMark = %{public}d", showWaterMark);
179     std::vector<sptr<IWaterMarkFlagChangedListener>> waterMarkFlagChangeListeners;
180     {
181         std::lock_guard<std::recursive_mutex> lock(mutex_);
182         waterMarkFlagChangeListeners = waterMarkFlagChangeListeners_;
183     }
184     for (auto& listener : waterMarkFlagChangeListeners) {
185         listener->OnWaterMarkFlagUpdate(showWaterMark);
186     }
187 }
188 
NotifyGestureNavigationEnabledResult(bool enable)189 void WindowManager::Impl::NotifyGestureNavigationEnabledResult(bool enable)
190 {
191     WLOGFI("Notify gesture navigation enable result, enable = %{public}d", enable);
192     std::vector<sptr<IGestureNavigationEnabledChangedListener>> gestureNavigationEnabledListeners;
193     {
194         std::lock_guard<std::recursive_mutex> lock(mutex_);
195         gestureNavigationEnabledListeners = gestureNavigationEnabledListeners_;
196     }
197     for (auto& listener : gestureNavigationEnabledListeners) {
198         listener->OnGestureNavigationEnabledUpdate(enable);
199     }
200 }
201 
WindowManager()202 WindowManager::WindowManager() : pImpl_(std::make_unique<Impl>(mutex_)) {}
203 
~WindowManager()204 WindowManager::~WindowManager()
205 {
206     std::lock_guard<std::recursive_mutex> lock(mutex_);
207     destroyed_ = true;
208 }
209 
RegisterFocusChangedListener(const sptr<IFocusChangedListener> & listener)210 WMError WindowManager::RegisterFocusChangedListener(const sptr<IFocusChangedListener>& listener)
211 {
212     if (listener == nullptr) {
213         WLOGFE("listener could not be null");
214         return WMError::WM_ERROR_NULLPTR;
215     }
216 
217     std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
218     WMError ret = WMError::WM_OK;
219     if (pImpl_->focusChangedListenerAgent_ == nullptr) {
220         pImpl_->focusChangedListenerAgent_ = new WindowManagerAgent();
221         ret = SingletonContainer::Get<WindowAdapter>().RegisterWindowManagerAgent(
222             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_FOCUS, pImpl_->focusChangedListenerAgent_);
223     }
224     if (ret != WMError::WM_OK) {
225         WLOGFW("RegisterWindowManagerAgent failed !");
226         pImpl_->focusChangedListenerAgent_ = nullptr;
227     } else {
228         auto iter = std::find(pImpl_->focusChangedListeners_.begin(), pImpl_->focusChangedListeners_.end(), listener);
229         if (iter != pImpl_->focusChangedListeners_.end()) {
230             WLOGFW("Listener is already registered.");
231             return WMError::WM_OK;
232         }
233         pImpl_->focusChangedListeners_.push_back(listener);
234     }
235     return ret;
236 }
237 
UnregisterFocusChangedListener(const sptr<IFocusChangedListener> & listener)238 WMError WindowManager::UnregisterFocusChangedListener(const sptr<IFocusChangedListener>& listener)
239 {
240     if (listener == nullptr) {
241         WLOGFE("listener could not be null");
242         return WMError::WM_ERROR_NULLPTR;
243     }
244 
245     std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
246     auto iter = std::find(pImpl_->focusChangedListeners_.begin(), pImpl_->focusChangedListeners_.end(), listener);
247     if (iter == pImpl_->focusChangedListeners_.end()) {
248         WLOGFE("could not find this listener");
249         return WMError::WM_OK;
250     }
251     pImpl_->focusChangedListeners_.erase(iter);
252     WMError ret = WMError::WM_OK;
253     if (pImpl_->focusChangedListeners_.empty() && pImpl_->focusChangedListenerAgent_ != nullptr) {
254         ret = SingletonContainer::Get<WindowAdapter>().UnregisterWindowManagerAgent(
255             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_FOCUS, pImpl_->focusChangedListenerAgent_);
256         if (ret == WMError::WM_OK) {
257             pImpl_->focusChangedListenerAgent_ = nullptr;
258         }
259     }
260     return ret;
261 }
262 
RegisterSystemBarChangedListener(const sptr<ISystemBarChangedListener> & listener)263 WMError WindowManager::RegisterSystemBarChangedListener(const sptr<ISystemBarChangedListener>& listener)
264 {
265     if (listener == nullptr) {
266         WLOGFE("listener could not be null");
267         return WMError::WM_ERROR_NULLPTR;
268     }
269 
270     std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
271     WMError ret = WMError::WM_OK;
272     if (pImpl_->systemBarChangedListenerAgent_ == nullptr) {
273         pImpl_->systemBarChangedListenerAgent_ = new WindowManagerAgent();
274         ret = SingletonContainer::Get<WindowAdapter>().RegisterWindowManagerAgent(
275             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_SYSTEM_BAR, pImpl_->systemBarChangedListenerAgent_);
276     }
277     if (ret != WMError::WM_OK) {
278         WLOGFW("RegisterWindowManagerAgent failed !");
279         pImpl_->systemBarChangedListenerAgent_ = nullptr;
280     } else {
281         auto iter = std::find(pImpl_->systemBarChangedListeners_.begin(), pImpl_->systemBarChangedListeners_.end(),
282             listener);
283         if (iter != pImpl_->systemBarChangedListeners_.end()) {
284             WLOGFW("Listener is already registered.");
285             return WMError::WM_OK;
286         }
287         pImpl_->systemBarChangedListeners_.push_back(listener);
288     }
289     return ret;
290 }
291 
UnregisterSystemBarChangedListener(const sptr<ISystemBarChangedListener> & listener)292 WMError WindowManager::UnregisterSystemBarChangedListener(const sptr<ISystemBarChangedListener>& listener)
293 {
294     if (listener == nullptr) {
295         WLOGFE("listener could not be null");
296         return WMError::WM_ERROR_NULLPTR;
297     }
298 
299     std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
300     auto iter = std::find(pImpl_->systemBarChangedListeners_.begin(), pImpl_->systemBarChangedListeners_.end(),
301         listener);
302     if (iter == pImpl_->systemBarChangedListeners_.end()) {
303         WLOGFE("could not find this listener");
304         return WMError::WM_OK;
305     }
306     pImpl_->systemBarChangedListeners_.erase(iter);
307     WMError ret = WMError::WM_OK;
308     if (pImpl_->systemBarChangedListeners_.empty() && pImpl_->systemBarChangedListenerAgent_ != nullptr) {
309         ret = SingletonContainer::Get<WindowAdapter>().UnregisterWindowManagerAgent(
310             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_SYSTEM_BAR, pImpl_->systemBarChangedListenerAgent_);
311         if (ret == WMError::WM_OK) {
312             pImpl_->systemBarChangedListenerAgent_ = nullptr;
313         }
314     }
315     return ret;
316 }
317 
MinimizeAllAppWindows(DisplayId displayId)318 WMError WindowManager::MinimizeAllAppWindows(DisplayId displayId)
319 {
320     WLOGFD("displayId %{public}" PRIu64"", displayId);
321     return SingletonContainer::Get<WindowAdapter>().MinimizeAllAppWindows(displayId);
322 }
323 
ToggleShownStateForAllAppWindows()324 WMError WindowManager::ToggleShownStateForAllAppWindows()
325 {
326     WLOGFD("ToggleShownStateForAllAppWindows");
327     return SingletonContainer::Get<WindowAdapter>().ToggleShownStateForAllAppWindows();
328 }
329 
SetWindowLayoutMode(WindowLayoutMode mode)330 WMError WindowManager::SetWindowLayoutMode(WindowLayoutMode mode)
331 {
332     WLOGFD("set window layout mode: %{public}u", mode);
333     WMError ret  = SingletonContainer::Get<WindowAdapter>().SetWindowLayoutMode(mode);
334     if (ret != WMError::WM_OK) {
335         WLOGFE("set layout mode failed");
336     }
337     return ret;
338 }
339 
RegisterWindowUpdateListener(const sptr<IWindowUpdateListener> & listener)340 WMError WindowManager::RegisterWindowUpdateListener(const sptr<IWindowUpdateListener> &listener)
341 {
342     if (listener == nullptr) {
343         WLOGFE("listener could not be null");
344         return WMError::WM_ERROR_NULLPTR;
345     }
346     std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
347     WMError ret = WMError::WM_OK;
348     if (pImpl_->windowUpdateListenerAgent_ == nullptr) {
349         pImpl_->windowUpdateListenerAgent_ = new WindowManagerAgent();
350         ret = SingletonContainer::Get<WindowAdapter>().RegisterWindowManagerAgent(
351             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_UPDATE, pImpl_->windowUpdateListenerAgent_);
352     }
353     if (ret != WMError::WM_OK) {
354         WLOGFW("RegisterWindowManagerAgent failed !");
355         pImpl_->windowUpdateListenerAgent_ = nullptr;
356     } else {
357         auto iter = std::find(pImpl_->windowUpdateListeners_.begin(), pImpl_->windowUpdateListeners_.end(), listener);
358         if (iter != pImpl_->windowUpdateListeners_.end()) {
359             WLOGI("Listener is already registered.");
360             return WMError::WM_OK;
361         }
362         pImpl_->windowUpdateListeners_.emplace_back(listener);
363     }
364     return ret;
365 }
366 
UnregisterWindowUpdateListener(const sptr<IWindowUpdateListener> & listener)367 WMError WindowManager::UnregisterWindowUpdateListener(const sptr<IWindowUpdateListener>& listener)
368 {
369     if (listener == nullptr) {
370         WLOGFE("listener could not be null");
371         return WMError::WM_ERROR_NULLPTR;
372     }
373     std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
374     auto iter = std::find(pImpl_->windowUpdateListeners_.begin(), pImpl_->windowUpdateListeners_.end(), listener);
375     if (iter == pImpl_->windowUpdateListeners_.end()) {
376         WLOGFE("could not find this listener");
377         return WMError::WM_OK;
378     }
379     pImpl_->windowUpdateListeners_.erase(iter);
380     WMError ret = WMError::WM_OK;
381     if (pImpl_->windowUpdateListeners_.empty() && pImpl_->windowUpdateListenerAgent_ != nullptr) {
382         ret = SingletonContainer::Get<WindowAdapter>().UnregisterWindowManagerAgent(
383             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_UPDATE, pImpl_->windowUpdateListenerAgent_);
384         if (ret == WMError::WM_OK) {
385             pImpl_->windowUpdateListenerAgent_ = nullptr;
386         }
387     }
388     return ret;
389 }
390 
RegisterVisibilityChangedListener(const sptr<IVisibilityChangedListener> & listener)391 WMError WindowManager::RegisterVisibilityChangedListener(const sptr<IVisibilityChangedListener>& listener)
392 {
393     if (listener == nullptr) {
394         WLOGFE("listener could not be null");
395         return WMError::WM_ERROR_NULLPTR;
396     }
397     std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
398     WMError ret = WMError::WM_OK;
399     if (pImpl_->windowVisibilityListenerAgent_ == nullptr) {
400         pImpl_->windowVisibilityListenerAgent_ = new WindowManagerAgent();
401         ret = SingletonContainer::Get<WindowAdapter>().RegisterWindowManagerAgent(
402             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_VISIBILITY,
403             pImpl_->windowVisibilityListenerAgent_);
404     }
405     if (ret != WMError::WM_OK) {
406         WLOGFW("RegisterWindowManagerAgent failed !");
407         pImpl_->windowVisibilityListenerAgent_ = nullptr;
408     } else {
409         auto iter = std::find(pImpl_->windowVisibilityListeners_.begin(), pImpl_->windowVisibilityListeners_.end(),
410             listener);
411         if (iter != pImpl_->windowVisibilityListeners_.end()) {
412             WLOGFW("Listener is already registered.");
413             return WMError::WM_OK;
414         }
415         pImpl_->windowVisibilityListeners_.emplace_back(listener);
416     }
417     return ret;
418 }
419 
UnregisterVisibilityChangedListener(const sptr<IVisibilityChangedListener> & listener)420 WMError WindowManager::UnregisterVisibilityChangedListener(const sptr<IVisibilityChangedListener>& listener)
421 {
422     if (listener == nullptr) {
423         WLOGFE("listener could not be null");
424         return WMError::WM_ERROR_NULLPTR;
425     }
426     std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
427     pImpl_->windowVisibilityListeners_.erase(std::remove_if(pImpl_->windowVisibilityListeners_.begin(),
428         pImpl_->windowVisibilityListeners_.end(), [listener](sptr<IVisibilityChangedListener> registeredListener) {
429             return registeredListener == listener;
430         }), pImpl_->windowVisibilityListeners_.end());
431 
432     WMError ret = WMError::WM_OK;
433     if (pImpl_->windowVisibilityListeners_.empty() && pImpl_->windowVisibilityListenerAgent_ != nullptr) {
434         ret = SingletonContainer::Get<WindowAdapter>().UnregisterWindowManagerAgent(
435             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_VISIBILITY,
436             pImpl_->windowVisibilityListenerAgent_);
437         if (ret == WMError::WM_OK) {
438             pImpl_->windowVisibilityListenerAgent_ = nullptr;
439         }
440     }
441     return ret;
442 }
443 
RegisterCameraFloatWindowChangedListener(const sptr<ICameraFloatWindowChangedListener> & listener)444 WMError WindowManager::RegisterCameraFloatWindowChangedListener(const sptr<ICameraFloatWindowChangedListener>& listener)
445 {
446     if (listener == nullptr) {
447         WLOGFE("listener could not be null");
448         return WMError::WM_ERROR_NULLPTR;
449     }
450 
451     std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
452     WMError ret = WMError::WM_OK;
453     if (pImpl_->cameraFloatWindowChangedListenerAgent_ == nullptr) {
454         pImpl_->cameraFloatWindowChangedListenerAgent_ = new WindowManagerAgent();
455         ret = SingletonContainer::Get<WindowAdapter>().RegisterWindowManagerAgent(
456             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_FLOAT,
457             pImpl_->cameraFloatWindowChangedListenerAgent_);
458     }
459     if (ret != WMError::WM_OK) {
460         WLOGFW("RegisterWindowManagerAgent failed !");
461         pImpl_->cameraFloatWindowChangedListenerAgent_ = nullptr;
462     } else {
463         auto iter = std::find(pImpl_->cameraFloatWindowChangedListeners_.begin(),
464             pImpl_->cameraFloatWindowChangedListeners_.end(), listener);
465         if (iter != pImpl_->cameraFloatWindowChangedListeners_.end()) {
466             WLOGFW("Listener is already registered.");
467             return WMError::WM_OK;
468         }
469         pImpl_->cameraFloatWindowChangedListeners_.push_back(listener);
470     }
471     return ret;
472 }
473 
UnregisterCameraFloatWindowChangedListener(const sptr<ICameraFloatWindowChangedListener> & listener)474 WMError WindowManager::UnregisterCameraFloatWindowChangedListener(
475     const sptr<ICameraFloatWindowChangedListener>& listener)
476 {
477     if (listener == nullptr) {
478         WLOGFE("listener could not be null");
479         return WMError::WM_ERROR_NULLPTR;
480     }
481 
482     std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
483     auto iter = std::find(pImpl_->cameraFloatWindowChangedListeners_.begin(),
484         pImpl_->cameraFloatWindowChangedListeners_.end(), listener);
485     if (iter == pImpl_->cameraFloatWindowChangedListeners_.end()) {
486         WLOGFE("could not find this listener");
487         return WMError::WM_OK;
488     }
489     pImpl_->cameraFloatWindowChangedListeners_.erase(iter);
490     WMError ret = WMError::WM_OK;
491     if (pImpl_->cameraFloatWindowChangedListeners_.empty() &&
492         pImpl_->cameraFloatWindowChangedListenerAgent_ != nullptr) {
493         ret = SingletonContainer::Get<WindowAdapter>().UnregisterWindowManagerAgent(
494             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_FLOAT,
495             pImpl_->cameraFloatWindowChangedListenerAgent_);
496         if (ret == WMError::WM_OK) {
497             pImpl_->cameraFloatWindowChangedListenerAgent_ = nullptr;
498         }
499     }
500     return ret;
501 }
502 
RegisterWaterMarkFlagChangedListener(const sptr<IWaterMarkFlagChangedListener> & listener)503 WMError WindowManager::RegisterWaterMarkFlagChangedListener(const sptr<IWaterMarkFlagChangedListener>& listener)
504 {
505     if (listener == nullptr) {
506         WLOGFE("listener could not be null");
507         return WMError::WM_ERROR_NULLPTR;
508     }
509 
510     std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
511     WMError ret = WMError::WM_OK;
512     if (pImpl_->waterMarkFlagChangeAgent_ == nullptr) {
513         pImpl_->waterMarkFlagChangeAgent_ = new WindowManagerAgent();
514         ret = SingletonContainer::Get<WindowAdapter>().RegisterWindowManagerAgent(
515             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WATER_MARK_FLAG,
516             pImpl_->waterMarkFlagChangeAgent_);
517     }
518     if (ret != WMError::WM_OK) {
519         WLOGFW("RegisterWindowManagerAgent failed !");
520         pImpl_->waterMarkFlagChangeAgent_ = nullptr;
521     } else {
522         auto iter = std::find(pImpl_->waterMarkFlagChangeListeners_.begin(),
523             pImpl_->waterMarkFlagChangeListeners_.end(), listener);
524         if (iter != pImpl_->waterMarkFlagChangeListeners_.end()) {
525             WLOGFW("Listener is already registered.");
526             return WMError::WM_OK;
527         }
528         pImpl_->waterMarkFlagChangeListeners_.push_back(listener);
529     }
530     WLOGFD("Try to registerWaterMarkFlagChangedListener && result : %{public}u", static_cast<uint32_t>(ret));
531     return ret;
532 }
533 
UnregisterWaterMarkFlagChangedListener(const sptr<IWaterMarkFlagChangedListener> & listener)534 WMError WindowManager::UnregisterWaterMarkFlagChangedListener(const sptr<IWaterMarkFlagChangedListener>& listener)
535 {
536     if (listener == nullptr) {
537         WLOGFE("listener could not be null");
538         return WMError::WM_ERROR_NULLPTR;
539     }
540 
541     std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
542     auto iter = std::find(pImpl_->waterMarkFlagChangeListeners_.begin(),
543         pImpl_->waterMarkFlagChangeListeners_.end(), listener);
544     if (iter == pImpl_->waterMarkFlagChangeListeners_.end()) {
545         WLOGFE("could not find this listener");
546         return WMError::WM_OK;
547     }
548     pImpl_->waterMarkFlagChangeListeners_.erase(iter);
549     WMError ret = WMError::WM_OK;
550     if (pImpl_->waterMarkFlagChangeListeners_.empty() &&
551         pImpl_->waterMarkFlagChangeAgent_ != nullptr) {
552         ret = SingletonContainer::Get<WindowAdapter>().UnregisterWindowManagerAgent(
553             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WATER_MARK_FLAG,
554             pImpl_->waterMarkFlagChangeAgent_);
555         if (ret == WMError::WM_OK) {
556             pImpl_->waterMarkFlagChangeAgent_ = nullptr;
557         }
558     }
559     WLOGFD("Try to unregisterWaterMarkFlagChangedListener && result : %{public}u", static_cast<uint32_t>(ret));
560     return ret;
561 }
562 
RegisterGestureNavigationEnabledChangedListener(const sptr<IGestureNavigationEnabledChangedListener> & listener)563 WMError WindowManager::RegisterGestureNavigationEnabledChangedListener(
564     const sptr<IGestureNavigationEnabledChangedListener>& listener)
565 {
566     if (listener == nullptr) {
567         WLOGFE("listener could not be null");
568         return WMError::WM_ERROR_NULLPTR;
569     }
570 
571     std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
572     WMError ret = WMError::WM_OK;
573     if (pImpl_->gestureNavigationEnabledAgent_ == nullptr) {
574         pImpl_->gestureNavigationEnabledAgent_ = new (std::nothrow)WindowManagerAgent();
575         if (pImpl_->gestureNavigationEnabledAgent_ != nullptr) {
576             ret = SingletonContainer::Get<WindowAdapter>().RegisterWindowManagerAgent(
577                 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_GESTURE_NAVIGATION_ENABLED,
578                 pImpl_->gestureNavigationEnabledAgent_);
579         } else {
580             WLOGFE("Create windowManagerAgent object failed !");
581             ret = WMError::WM_ERROR_NULLPTR;
582         }
583     }
584     if (ret != WMError::WM_OK) {
585         WLOGFE("RegisterWindowManagerAgent failed !");
586         pImpl_->gestureNavigationEnabledAgent_ = nullptr;
587     } else {
588         auto iter = std::find(pImpl_->gestureNavigationEnabledListeners_.begin(),
589             pImpl_->gestureNavigationEnabledListeners_.end(), listener);
590         if (iter != pImpl_->gestureNavigationEnabledListeners_.end()) {
591             WLOGFW("Listener is already registered.");
592             return WMError::WM_OK;
593         }
594         pImpl_->gestureNavigationEnabledListeners_.push_back(listener);
595     }
596     WLOGFD("Try to registerGestureNavigationEnabledChangedListener and result is %{public}u",
597         static_cast<uint32_t>(ret));
598     return ret;
599 }
600 
UnregisterGestureNavigationEnabledChangedListener(const sptr<IGestureNavigationEnabledChangedListener> & listener)601 WMError WindowManager::UnregisterGestureNavigationEnabledChangedListener(
602     const sptr<IGestureNavigationEnabledChangedListener>& listener)
603 {
604     if (listener == nullptr) {
605         WLOGFE("listener could not be null");
606         return WMError::WM_ERROR_NULLPTR;
607     }
608 
609     std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
610     auto iter = std::find(pImpl_->gestureNavigationEnabledListeners_.begin(),
611         pImpl_->gestureNavigationEnabledListeners_.end(), listener);
612     if (iter == pImpl_->gestureNavigationEnabledListeners_.end()) {
613         WLOGFE("could not find this listener");
614         return WMError::WM_ERROR_INVALID_PARAM;
615     }
616     pImpl_->gestureNavigationEnabledListeners_.erase(iter);
617     WMError ret = WMError::WM_OK;
618     if (pImpl_->gestureNavigationEnabledListeners_.empty() &&
619         pImpl_->gestureNavigationEnabledAgent_ != nullptr) {
620         ret = SingletonContainer::Get<WindowAdapter>().UnregisterWindowManagerAgent(
621             WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_GESTURE_NAVIGATION_ENABLED,
622             pImpl_->gestureNavigationEnabledAgent_);
623         if (ret == WMError::WM_OK) {
624             pImpl_->gestureNavigationEnabledAgent_ = nullptr;
625         }
626     }
627     WLOGFD("Try to unregisterGestureNavigationEnabledChangedListener and result is %{public}u",
628         static_cast<uint32_t>(ret));
629     return ret;
630 }
631 
GetFocusWindowInfo(FocusChangeInfo & focusInfo)632 void WindowManager::GetFocusWindowInfo(FocusChangeInfo& focusInfo)
633 {
634     SingletonContainer::Get<WindowAdapter>().GetFocusWindowInfo(focusInfo);
635 }
636 
UpdateFocusChangeInfo(const sptr<FocusChangeInfo> & focusChangeInfo,bool focused) const637 void WindowManager::UpdateFocusChangeInfo(const sptr<FocusChangeInfo>& focusChangeInfo, bool focused) const
638 {
639     if (focusChangeInfo == nullptr) {
640         WLOGFE("focusChangeInfo is nullptr.");
641         return;
642     }
643     WLOGFD("window focus change: %{public}d, id: %{public}u", focused, focusChangeInfo->windowId_);
644     if (focused) {
645 #ifdef EFFICIENCY_MANAGER_ENABLE
646         SuspendManager::SuspendManagerClient::GetInstance().ThawOneApplication(focusChangeInfo->uid_,
647             "", "THAW_BY_FOCUS_CHANGED");
648 #endif // EFFICIENCY_MANAGER_ENABLE
649         pImpl_->NotifyFocused(focusChangeInfo);
650     } else {
651         pImpl_->NotifyUnfocused(focusChangeInfo);
652     }
653 }
654 
UpdateSystemBarRegionTints(DisplayId displayId,const SystemBarRegionTints & tints) const655 void WindowManager::UpdateSystemBarRegionTints(DisplayId displayId,
656     const SystemBarRegionTints& tints) const
657 {
658     pImpl_->NotifySystemBarChanged(displayId, tints);
659 }
660 
NotifyAccessibilityWindowInfo(const std::vector<sptr<AccessibilityWindowInfo>> & infos,WindowUpdateType type) const661 void WindowManager::NotifyAccessibilityWindowInfo(const std::vector<sptr<AccessibilityWindowInfo>>& infos,
662     WindowUpdateType type) const
663 {
664     pImpl_->NotifyAccessibilityWindowInfo(infos, type);
665 }
666 
UpdateWindowVisibilityInfo(const std::vector<sptr<WindowVisibilityInfo>> & windowVisibilityInfos) const667 void WindowManager::UpdateWindowVisibilityInfo(
668     const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfos) const
669 {
670     pImpl_->NotifyWindowVisibilityInfoChanged(windowVisibilityInfos);
671 }
672 
GetAccessibilityWindowInfo(std::vector<sptr<AccessibilityWindowInfo>> & infos) const673 WMError WindowManager::GetAccessibilityWindowInfo(std::vector<sptr<AccessibilityWindowInfo>>& infos) const
674 {
675     WMError ret = SingletonContainer::Get<WindowAdapter>().GetAccessibilityWindowInfo(infos);
676     if (ret != WMError::WM_OK) {
677         WLOGFE("get window info failed");
678     }
679     return ret;
680 }
681 
GetVisibilityWindowInfo(std::vector<sptr<WindowVisibilityInfo>> & infos) const682 WMError WindowManager::GetVisibilityWindowInfo(std::vector<sptr<WindowVisibilityInfo>>& infos) const
683 {
684     WMError ret = SingletonContainer::Get<WindowAdapter>().GetVisibilityWindowInfo(infos);
685     if (ret != WMError::WM_OK) {
686         WLOGFE("get window visibility info failed");
687     }
688     return ret;
689 }
690 
SetGestureNavigaionEnabled(bool enable) const691 WMError WindowManager::SetGestureNavigaionEnabled(bool enable) const
692 {
693     WMError ret = SingletonContainer::Get<WindowAdapter>().SetGestureNavigaionEnabled(enable);
694     if (ret != WMError::WM_OK) {
695         WLOGFE("set gesture navigaion enabled failed");
696     }
697     return ret;
698 }
699 
700 
UpdateCameraFloatWindowStatus(uint32_t accessTokenId,bool isShowing) const701 void WindowManager::UpdateCameraFloatWindowStatus(uint32_t accessTokenId, bool isShowing) const
702 {
703     pImpl_->UpdateCameraFloatWindowStatus(accessTokenId, isShowing);
704 }
705 
NotifyWaterMarkFlagChangedResult(bool showWaterMark) const706 void WindowManager::NotifyWaterMarkFlagChangedResult(bool showWaterMark) const
707 {
708     pImpl_->NotifyWaterMarkFlagChangedResult(showWaterMark);
709 }
710 
NotifyGestureNavigationEnabledResult(bool enable) const711 void WindowManager::NotifyGestureNavigationEnabledResult(bool enable) const
712 {
713     pImpl_->NotifyGestureNavigationEnabledResult(enable);
714 }
715 
716 
OnRemoteDied()717 void WindowManager::OnRemoteDied()
718 {
719     WLOGI("wms is died");
720     std::lock_guard<std::recursive_mutex> lock(mutex_);
721     if (destroyed_) {
722         WLOGE("Already destroyed");
723         return;
724     }
725     pImpl_->focusChangedListenerAgent_ = nullptr;
726     pImpl_->systemBarChangedListenerAgent_ = nullptr;
727     pImpl_->windowUpdateListenerAgent_ = nullptr;
728     pImpl_->windowVisibilityListenerAgent_ = nullptr;
729     pImpl_->cameraFloatWindowChangedListenerAgent_ = nullptr;
730 }
731 } // namespace Rosen
732 } // namespace OHOS
733