1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "window_manager_lite.h"
17
18 #include <algorithm>
19 #include <cinttypes>
20
21 #include "marshalling_helper.h"
22 #include "window_adapter_lite.h"
23 #include "window_manager_agent_lite.h"
24 #include "window_manager_hilog.h"
25 #include "wm_common.h"
26
27 namespace OHOS {
28 namespace Rosen {
29 namespace {
30 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowManagerLite"};
31 }
32
33 WM_IMPLEMENT_SINGLE_INSTANCE(WindowManagerLite)
34
35 class WindowManagerLite::Impl {
36 public:
Impl(std::recursive_mutex & mutex)37 explicit Impl(std::recursive_mutex& mutex) : mutex_(mutex) {}
38 void NotifyFocused(uint32_t windowId, const sptr<IRemoteObject>& abilityToken,
39 WindowType windowType, DisplayId displayId);
40 void NotifyUnfocused(uint32_t windowId, const sptr<IRemoteObject>& abilityToken,
41 WindowType windowType, DisplayId displayId);
42 void NotifyFocused(const sptr<FocusChangeInfo>& focusChangeInfo);
43 void NotifyUnfocused(const sptr<FocusChangeInfo>& focusChangeInfo);
44 void NotifyWindowVisibilityInfoChanged(const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfos);
45 void NotifyWindowVisibilityStateChanged(const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfos);
46 void PackWindowChangeInfo(const std::unordered_set<WindowInfoKey>& interestInfo,
47 const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfos,
48 std::vector<std::unordered_map<WindowInfoKey, WindowChangeInfoType>>& windowChangeInfos);
49 void NotifyWindowDrawingContentInfoChanged(const std::vector<sptr<WindowDrawingContentInfo>>&
50 windowDrawingContentInfos);
51 void NotifyWindowModeChange(WindowModeType type);
52 void UpdateCameraWindowStatus(uint32_t accessTokenId, bool isShowing);
53 void NotifyWMSConnected(int32_t userId, int32_t screenId);
54 void NotifyWMSDisconnected(int32_t userId, int32_t screenId);
55 void NotifyWindowStyleChange(WindowStyleType type);
56 void NotifyCallingWindowDisplayChanged(const CallingWindowInfo& callingWindowInfo);
57 void UpdatePiPWindowStateChanged(const std::string& bundleName, bool isForeground);
58 void NotifyAccessibilityWindowInfo(const std::vector<sptr<AccessibilityWindowInfo>>& infos,
59 WindowUpdateType type);
60
61 static inline SingletonDelegator<WindowManagerLite> delegator_;
62
63 std::recursive_mutex& mutex_;
64 std::vector<sptr<IFocusChangedListener>> focusChangedListeners_;
65 sptr<WindowManagerAgentLite> focusChangedListenerAgent_;
66 std::vector<sptr<IWindowUpdateListener>> windowUpdateListeners_;
67 sptr<WindowManagerAgentLite> windowUpdateListenerAgent_;
68 std::vector<sptr<IVisibilityChangedListener>> windowVisibilityListeners_;
69 sptr<WindowManagerAgentLite> windowVisibilityListenerAgent_;
70 std::vector<sptr<IWindowInfoChangedListener>> windowVisibilityStateListeners_;
71 sptr<WindowManagerAgentLite> windowVisibilityStateListenerAgent_;
72 std::vector<sptr<IDrawingContentChangedListener>> windowDrawingContentListeners_;
73 sptr<WindowManagerAgentLite> windowDrawingContentListenerAgent_;
74 std::vector<sptr<IWindowModeChangedListener>> windowModeListeners_;
75 sptr<WindowManagerAgentLite> windowModeListenerAgent_;
76 std::vector<sptr<ICameraWindowChangedListener>> cameraWindowChangedListeners_;
77 sptr<WindowManagerAgentLite> cameraWindowChangedListenerAgent_;
78 sptr<IWMSConnectionChangedListener> wmsConnectionChangedListener_;
79 std::vector<sptr<IWindowStyleChangedListener>> windowStyleListeners_;
80 sptr<WindowManagerAgentLite> windowStyleListenerAgent_;
81 std::vector<sptr<IPiPStateChangedListener>> pipStateChangedListeners_;
82 sptr<WindowManagerAgentLite> pipStateChangedListenerAgent_;
83 std::vector<sptr<IKeyboardCallingWindowDisplayChangedListener>> callingDisplayChangedListeners_;
84 sptr<WindowManagerAgentLite> callingDisplayListenerAgent_;
85 };
86
NotifyWMSConnected(int32_t userId,int32_t screenId)87 void WindowManagerLite::Impl::NotifyWMSConnected(int32_t userId, int32_t screenId)
88 {
89 TLOGD(WmsLogTag::WMS_MULTI_USER, "WMS connected [userId:%{public}d; screenId:%{public}d]", userId, screenId);
90 sptr<IWMSConnectionChangedListener> wmsConnectionChangedListener;
91 {
92 std::lock_guard<std::recursive_mutex> lock(mutex_);
93 wmsConnectionChangedListener = wmsConnectionChangedListener_;
94 }
95 if (wmsConnectionChangedListener != nullptr) {
96 wmsConnectionChangedListener->OnConnected(userId, screenId);
97 }
98 }
99
NotifyWMSDisconnected(int32_t userId,int32_t screenId)100 void WindowManagerLite::Impl::NotifyWMSDisconnected(int32_t userId, int32_t screenId)
101 {
102 TLOGI(WmsLogTag::WMS_MULTI_USER, "WMS disconnected [userId:%{public}d; screenId:%{public}d]", userId, screenId);
103 sptr<IWMSConnectionChangedListener> wmsConnectionChangedListener;
104 {
105 std::lock_guard<std::recursive_mutex> lock(mutex_);
106 wmsConnectionChangedListener = wmsConnectionChangedListener_;
107 }
108 if (wmsConnectionChangedListener != nullptr) {
109 wmsConnectionChangedListener->OnDisconnected(userId, screenId);
110 }
111 }
112
NotifyFocused(const sptr<FocusChangeInfo> & focusChangeInfo)113 void WindowManagerLite::Impl::NotifyFocused(const sptr<FocusChangeInfo>& focusChangeInfo)
114 {
115 if (focusChangeInfo == nullptr) {
116 return;
117 }
118 WLOGFD("[WMSFocus]NotifyFocused [%{public}u; %{public}" PRIu64"; %{public}d; %{public}d; %{public}u]",
119 focusChangeInfo->windowId_, focusChangeInfo->displayId_, focusChangeInfo->pid_, focusChangeInfo->uid_,
120 static_cast<uint32_t>(focusChangeInfo->windowType_));
121
122 std::vector<sptr<IFocusChangedListener>> focusChangeListeners;
123 {
124 std::lock_guard<std::recursive_mutex> lock(mutex_);
125 focusChangeListeners = focusChangedListeners_;
126 }
127 WLOGFD("NotifyFocused listeners: %{public}zu", focusChangeListeners.size());
128 for (auto& listener : focusChangeListeners) {
129 if (listener == nullptr) {
130 continue;
131 }
132 listener->OnFocused(focusChangeInfo);
133 }
134 }
135
NotifyUnfocused(const sptr<FocusChangeInfo> & focusChangeInfo)136 void WindowManagerLite::Impl::NotifyUnfocused(const sptr<FocusChangeInfo>& focusChangeInfo)
137 {
138 if (focusChangeInfo == nullptr) {
139 return;
140 }
141 WLOGFD("[WMSFocus]NotifyUnfocused [%{public}u; %{public}" PRIu64"; %{public}d; %{public}d; %{public}u]",
142 focusChangeInfo->windowId_, focusChangeInfo->displayId_, focusChangeInfo->pid_, focusChangeInfo->uid_,
143 static_cast<uint32_t>(focusChangeInfo->windowType_));
144
145 std::vector<sptr<IFocusChangedListener>> focusChangeListeners;
146 {
147 std::lock_guard<std::recursive_mutex> lock(mutex_);
148 focusChangeListeners = focusChangedListeners_;
149 }
150 WLOGFD("NotifyUnfocused listeners: %{public}zu", focusChangeListeners.size());
151 for (auto& listener : focusChangeListeners) {
152 if (listener == nullptr) {
153 continue;
154 }
155 listener->OnUnfocused(focusChangeInfo);
156 }
157 }
158
NotifyWindowVisibilityInfoChanged(const std::vector<sptr<WindowVisibilityInfo>> & windowVisibilityInfos)159 void WindowManagerLite::Impl::NotifyWindowVisibilityInfoChanged(
160 const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfos)
161 {
162 std::vector<sptr<IVisibilityChangedListener>> visibilityChangeListeners;
163 {
164 std::lock_guard<std::recursive_mutex> lock(mutex_);
165 visibilityChangeListeners = windowVisibilityListeners_;
166 }
167 for (auto& listener : visibilityChangeListeners) {
168 if (listener == nullptr) {
169 continue;
170 }
171 listener->OnWindowVisibilityChanged(windowVisibilityInfos);
172 }
173 }
174
NotifyWindowVisibilityStateChanged(const std::vector<sptr<WindowVisibilityInfo>> & windowVisibilityInfos)175 void WindowManagerLite::Impl::NotifyWindowVisibilityStateChanged(
176 const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfos)
177 {
178 TLOGD(WmsLogTag::WMS_ATTRIBUTE, "in");
179 std::vector<sptr<IWindowInfoChangedListener>> windowVisibilityStateListeners;
180 {
181 std::lock_guard<std::recursive_mutex> lock(mutex_);
182 windowVisibilityStateListeners = windowVisibilityStateListeners_;
183 }
184 for (auto& listener : windowVisibilityStateListeners) {
185 if (listener == nullptr) {
186 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "listener is null");
187 continue;
188 }
189 std::vector<std::unordered_map<WindowInfoKey, WindowChangeInfoType>> windowChangeInfos;
190 PackWindowChangeInfo(listener->GetInterestInfo(), windowVisibilityInfos, windowChangeInfos);
191 TLOGD(WmsLogTag::WMS_ATTRIBUTE, "Notify WindowVisibilityState to caller, info size: %{public}zu",
192 windowChangeInfos.size());
193 listener->OnWindowInfoChanged(windowChangeInfos);
194 }
195 }
196
PackWindowChangeInfo(const std::unordered_set<WindowInfoKey> & interestInfo,const std::vector<sptr<WindowVisibilityInfo>> & windowVisibilityInfos,std::vector<std::unordered_map<WindowInfoKey,WindowChangeInfoType>> & windowChangeInfos)197 void WindowManagerLite::Impl::PackWindowChangeInfo(const std::unordered_set<WindowInfoKey>& interestInfo,
198 const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfos,
199 std::vector<std::unordered_map<WindowInfoKey, WindowChangeInfoType>>& windowChangeInfos)
200 {
201 for (const auto& info : windowVisibilityInfos) {
202 std::unordered_map<WindowInfoKey, WindowChangeInfoType> windowChangeInfo;
203 if (interestInfo.find(WindowInfoKey::WINDOW_ID) != interestInfo.end()) {
204 windowChangeInfo.emplace(WindowInfoKey::WINDOW_ID, info->windowId_);
205 }
206 if (interestInfo.find(WindowInfoKey::BUNDLE_NAME) != interestInfo.end()) {
207 windowChangeInfo.emplace(WindowInfoKey::BUNDLE_NAME, info->bundleName_);
208 }
209 if (interestInfo.find(WindowInfoKey::ABILITY_NAME) != interestInfo.end()) {
210 windowChangeInfo.emplace(WindowInfoKey::ABILITY_NAME, info->abilityName_);
211 }
212 if (interestInfo.find(WindowInfoKey::APP_INDEX) != interestInfo.end()) {
213 windowChangeInfo.emplace(WindowInfoKey::APP_INDEX, info->appIndex_);
214 }
215 if (interestInfo.find(WindowInfoKey::VISIBILITY_STATE) != interestInfo.end()) {
216 windowChangeInfo.emplace(WindowInfoKey::VISIBILITY_STATE, info->visibilityState_);
217 }
218 windowChangeInfos.emplace_back(windowChangeInfo);
219 }
220 }
221
NotifyWindowDrawingContentInfoChanged(const std::vector<sptr<WindowDrawingContentInfo>> & windowDrawingContentInfos)222 void WindowManagerLite::Impl::NotifyWindowDrawingContentInfoChanged(
223 const std::vector<sptr<WindowDrawingContentInfo>>& windowDrawingContentInfos)
224 {
225 std::vector<sptr<IDrawingContentChangedListener>> windowDrawingContentChangeListeners;
226 {
227 std::lock_guard<std::recursive_mutex> lock(mutex_);
228 windowDrawingContentChangeListeners = windowDrawingContentListeners_;
229 }
230 for (auto& listener : windowDrawingContentChangeListeners) {
231 if (listener == nullptr) {
232 continue;
233 }
234 listener->OnWindowDrawingContentChanged(windowDrawingContentInfos);
235 }
236 }
237
NotifyWindowModeChange(WindowModeType type)238 void WindowManagerLite::Impl::NotifyWindowModeChange(WindowModeType type)
239 {
240 TLOGI(WmsLogTag::WMS_MAIN, "WindowManager::Impl UpdateWindowModeTypeInfo type: %{public}d",
241 static_cast<uint8_t>(type));
242 std::vector<sptr<IWindowModeChangedListener>> windowModeListeners;
243 {
244 std::lock_guard<std::recursive_mutex> lock(mutex_);
245 windowModeListeners = windowModeListeners_;
246 }
247 for (auto &listener : windowModeListeners) {
248 listener->OnWindowModeUpdate(type);
249 }
250 }
251
NotifyAccessibilityWindowInfo(const std::vector<sptr<AccessibilityWindowInfo>> & infos,WindowUpdateType type)252 void WindowManagerLite::Impl::NotifyAccessibilityWindowInfo(const std::vector<sptr<AccessibilityWindowInfo>>& infos,
253 WindowUpdateType type)
254 {
255 if (infos.empty()) {
256 WLOGFE("infos is empty");
257 return;
258 }
259 for (auto& info : infos) {
260 if (info == nullptr) {
261 TLOGD(WmsLogTag::WMS_MAIN, "info is nullptr");
262 continue;
263 }
264 TLOGD(WmsLogTag::WMS_MAIN, "wid[%{public}u], innerWid[%{public}u], "
265 "uiNodeId[%{public}u], rect[%{public}d %{public}d %{public}d %{public}d], "
266 "isFocused[%{public}d], isDecorEnable[%{public}d], displayId[%{public}" PRIu64 "], layer[%{public}u], "
267 "mode[%{public}u], type[%{public}u, updateType[%{public}d], bundle[%{public}s]",
268 info->wid_, info->innerWid_, info->uiNodeId_, info->windowRect_.width_, info->windowRect_.height_,
269 info->windowRect_.posX_, info->windowRect_.posY_, info->focused_, info->isDecorEnable_, info->displayId_,
270 info->layer_, info->mode_, info->type_, type, info->bundleName_.c_str());
271 for (const auto& rect : info->touchHotAreas_) {
272 TLOGD(WmsLogTag::WMS_MAIN, "window touch hot areas rect[x=%{public}d, y=%{public}d, "
273 "w=%{public}d, h=%{public}d]", rect.posX_, rect.posY_, rect.width_, rect.height_);
274 }
275 }
276
277 std::vector<sptr<IWindowUpdateListener>> windowUpdateListeners;
278 {
279 std::lock_guard<std::recursive_mutex> lock(mutex_);
280 windowUpdateListeners = windowUpdateListeners_;
281 }
282 for (auto& listener : windowUpdateListeners) {
283 listener->OnWindowUpdate(infos, type);
284 }
285 }
286
UpdateCameraWindowStatus(uint32_t accessTokenId,bool isShowing)287 void WindowManagerLite::Impl::UpdateCameraWindowStatus(uint32_t accessTokenId, bool isShowing)
288 {
289 TLOGI(WmsLogTag::WMS_SYSTEM, "Camera window, accessTokenId=%{public}u, isShowing=%{public}u",
290 accessTokenId, isShowing);
291 std::vector<sptr<ICameraWindowChangedListener>> cameraWindowChangeListeners;
292 {
293 std::lock_guard<std::recursive_mutex> lock(mutex_);
294 cameraWindowChangeListeners = cameraWindowChangedListeners_;
295 }
296 for (auto& listener : cameraWindowChangeListeners) {
297 listener->OnCameraWindowChange(accessTokenId, isShowing);
298 }
299 }
300
NotifyWindowStyleChange(WindowStyleType type)301 void WindowManagerLite::Impl::NotifyWindowStyleChange(WindowStyleType type)
302 {
303 TLOGI(WmsLogTag::WMS_MAIN, "WindowStyleChange: %{public}d",
304 static_cast<uint8_t>(type));
305 std::vector<sptr<IWindowStyleChangedListener>> windowStyleListeners;
306 {
307 std::lock_guard<std::recursive_mutex> lock(mutex_);
308 windowStyleListeners = windowStyleListeners_;
309 }
310 for (auto &listener : windowStyleListeners) {
311 TLOGI(WmsLogTag::WMS_MAIN, "real WindowStyleChange type: %{public}d",
312 static_cast<uint8_t>(type));
313 listener->OnWindowStyleUpdate(type);
314 }
315 }
316
NotifyCallingWindowDisplayChanged(const CallingWindowInfo & callingWindowInfo)317 void WindowManagerLite::Impl::NotifyCallingWindowDisplayChanged(const CallingWindowInfo& callingWindowInfo)
318 {
319 std::vector<sptr<IKeyboardCallingWindowDisplayChangedListener>> displayChangeListeners;
320 {
321 std::lock_guard<std::recursive_mutex> lock(mutex_);
322 displayChangeListeners = callingDisplayChangedListeners_;
323 }
324 TLOGI(WmsLogTag::WMS_KEYBOARD, "notify persistentId: %{public}d, pid: %{public}d, "
325 "displayId: %{public}" PRIu64" , userId: %{public}d, size: %{public}zu",
326 callingWindowInfo.windowId_, callingWindowInfo.callingPid_, callingWindowInfo.displayId_,
327 callingWindowInfo.userId_, displayChangeListeners.size());
328
329 for (const auto& listener : displayChangeListeners) {
330 if (!listener) {
331 TLOGE(WmsLogTag::WMS_KEYBOARD, "listener is nullptr");
332 continue;
333 }
334 listener->OnCallingWindowDisplayChanged(callingWindowInfo);
335 }
336 }
337
UpdatePiPWindowStateChanged(const std::string & bundleName,bool isForeground)338 void WindowManagerLite::Impl::UpdatePiPWindowStateChanged(const std::string& bundleName, bool isForeground)
339 {
340 std::vector<sptr<IPiPStateChangedListener>> pipStateChangedListeners;
341 {
342 std::lock_guard<std::recursive_mutex> lock(mutex_);
343 pipStateChangedListeners = pipStateChangedListeners_;
344 }
345 for (auto& listener : pipStateChangedListeners) {
346 if (listener == nullptr) {
347 continue;
348 }
349 listener->OnPiPStateChanged(bundleName, isForeground);
350 }
351 }
352
WindowManagerLite()353 WindowManagerLite::WindowManagerLite() : pImpl_(std::make_unique<Impl>(mutex_))
354 {
355 }
356
~WindowManagerLite()357 WindowManagerLite::~WindowManagerLite()
358 {
359 std::lock_guard<std::recursive_mutex> lock(mutex_);
360 destroyed_ = true;
361 }
362
RegisterFocusChangedListener(const sptr<IFocusChangedListener> & listener)363 WMError WindowManagerLite::RegisterFocusChangedListener(const sptr<IFocusChangedListener>& listener)
364 {
365 if (listener == nullptr) {
366 WLOGFE("listener could not be null");
367 return WMError::WM_ERROR_NULLPTR;
368 }
369
370 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
371 WMError ret = WMError::WM_OK;
372 if (pImpl_->focusChangedListenerAgent_ == nullptr) {
373 pImpl_->focusChangedListenerAgent_ = new (std::nothrow) WindowManagerAgentLite();
374 ret = SingletonContainer::Get<WindowAdapterLite>().RegisterWindowManagerAgent(
375 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_FOCUS, pImpl_->focusChangedListenerAgent_);
376 }
377 if (ret != WMError::WM_OK) {
378 WLOGFW("RegisterWindowManagerAgent failed !");
379 pImpl_->focusChangedListenerAgent_ = nullptr;
380 } else {
381 auto iter = std::find(pImpl_->focusChangedListeners_.begin(), pImpl_->focusChangedListeners_.end(), listener);
382 if (iter != pImpl_->focusChangedListeners_.end()) {
383 WLOGFW("Listener is already registered.");
384 return WMError::WM_OK;
385 }
386 pImpl_->focusChangedListeners_.push_back(listener);
387 }
388 return ret;
389 }
390
UnregisterFocusChangedListener(const sptr<IFocusChangedListener> & listener)391 WMError WindowManagerLite::UnregisterFocusChangedListener(const sptr<IFocusChangedListener>& listener)
392 {
393 if (listener == nullptr) {
394 WLOGFE("listener could not be null");
395 return WMError::WM_ERROR_NULLPTR;
396 }
397
398 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
399 auto iter = std::find(pImpl_->focusChangedListeners_.begin(), pImpl_->focusChangedListeners_.end(), listener);
400 if (iter == pImpl_->focusChangedListeners_.end()) {
401 WLOGFE("could not find this listener");
402 return WMError::WM_OK;
403 }
404 pImpl_->focusChangedListeners_.erase(iter);
405 WMError ret = WMError::WM_OK;
406 if (pImpl_->focusChangedListeners_.empty() && pImpl_->focusChangedListenerAgent_ != nullptr) {
407 ret = SingletonContainer::Get<WindowAdapterLite>().UnregisterWindowManagerAgent(
408 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_FOCUS, pImpl_->focusChangedListenerAgent_);
409 if (ret == WMError::WM_OK) {
410 pImpl_->focusChangedListenerAgent_ = nullptr;
411 }
412 }
413 return ret;
414 }
415
RegisterVisibilityChangedListener(const sptr<IVisibilityChangedListener> & listener)416 WMError WindowManagerLite::RegisterVisibilityChangedListener(const sptr<IVisibilityChangedListener>& listener)
417 {
418 if (listener == nullptr) {
419 WLOGFE("listener could not be null");
420 return WMError::WM_ERROR_NULLPTR;
421 }
422 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
423 WMError ret = WMError::WM_OK;
424 if (pImpl_->windowVisibilityListenerAgent_ == nullptr) {
425 pImpl_->windowVisibilityListenerAgent_ = new (std::nothrow) WindowManagerAgentLite();
426 ret = SingletonContainer::Get<WindowAdapterLite>().RegisterWindowManagerAgent(
427 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_VISIBILITY,
428 pImpl_->windowVisibilityListenerAgent_);
429 }
430 if (ret != WMError::WM_OK) {
431 WLOGFW("RegisterWindowManagerAgent failed !");
432 pImpl_->windowVisibilityListenerAgent_ = nullptr;
433 } else {
434 auto iter = std::find(pImpl_->windowVisibilityListeners_.begin(), pImpl_->windowVisibilityListeners_.end(),
435 listener);
436 if (iter != pImpl_->windowVisibilityListeners_.end()) {
437 WLOGFW("Listener is already registered.");
438 return WMError::WM_OK;
439 }
440 pImpl_->windowVisibilityListeners_.emplace_back(listener);
441 }
442 return ret;
443 }
444
UnregisterVisibilityChangedListener(const sptr<IVisibilityChangedListener> & listener)445 WMError WindowManagerLite::UnregisterVisibilityChangedListener(const sptr<IVisibilityChangedListener>& listener)
446 {
447 if (listener == nullptr) {
448 WLOGFE("listener could not be null");
449 return WMError::WM_ERROR_NULLPTR;
450 }
451 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
452 pImpl_->windowVisibilityListeners_.erase(std::remove_if(pImpl_->windowVisibilityListeners_.begin(),
453 pImpl_->windowVisibilityListeners_.end(), [listener](sptr<IVisibilityChangedListener> registeredListener) {
454 return registeredListener == listener;
455 }), pImpl_->windowVisibilityListeners_.end());
456
457 WMError ret = WMError::WM_OK;
458 if (pImpl_->windowVisibilityListeners_.empty() && pImpl_->windowVisibilityListenerAgent_ != nullptr) {
459 ret = SingletonContainer::Get<WindowAdapterLite>().UnregisterWindowManagerAgent(
460 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_VISIBILITY,
461 pImpl_->windowVisibilityListenerAgent_);
462 if (ret == WMError::WM_OK) {
463 pImpl_->windowVisibilityListenerAgent_ = nullptr;
464 }
465 }
466 return ret;
467 }
468
RegisterVisibilityStateChangedListener(const sptr<IWindowInfoChangedListener> & listener)469 WMError WindowManagerLite::RegisterVisibilityStateChangedListener(const sptr<IWindowInfoChangedListener>& listener)
470 {
471 if (listener == nullptr) {
472 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "listener is null");
473 return WMError::WM_ERROR_NULLPTR;
474 }
475 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
476 WMError ret = WMError::WM_OK;
477 if (pImpl_->windowVisibilityStateListenerAgent_ == nullptr) {
478 pImpl_->windowVisibilityStateListenerAgent_ = new WindowManagerAgentLite();
479 }
480 ret = SingletonContainer::Get<WindowAdapterLite>().RegisterWindowManagerAgent(
481 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_VISIBILITY,
482 pImpl_->windowVisibilityStateListenerAgent_);
483 if (ret != WMError::WM_OK) {
484 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "RegisterWindowManagerAgent failed!");
485 pImpl_->windowVisibilityStateListenerAgent_ = nullptr;
486 } else {
487 auto iter = std::find(pImpl_->windowVisibilityStateListeners_.begin(),
488 pImpl_->windowVisibilityStateListeners_.end(), listener);
489 if (iter != pImpl_->windowVisibilityStateListeners_.end()) {
490 TLOGW(WmsLogTag::WMS_ATTRIBUTE, "Listener is already registered.");
491 return WMError::WM_OK;
492 }
493 pImpl_->windowVisibilityStateListeners_.emplace_back(listener);
494 }
495 return ret;
496 }
497
UnregisterVisibilityStateChangedListener(const sptr<IWindowInfoChangedListener> & listener)498 WMError WindowManagerLite::UnregisterVisibilityStateChangedListener(const sptr<IWindowInfoChangedListener>& listener)
499 {
500 if (listener == nullptr) {
501 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "listener is null");
502 return WMError::WM_ERROR_NULLPTR;
503 }
504 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
505 pImpl_->windowVisibilityStateListeners_.erase(std::remove_if(pImpl_->windowVisibilityStateListeners_ .begin(),
506 pImpl_->windowVisibilityStateListeners_.end(), [listener](sptr<IWindowInfoChangedListener> registeredListener) {
507 return registeredListener == listener;
508 }), pImpl_->windowVisibilityStateListeners_.end());
509
510 WMError ret = WMError::WM_OK;
511 if (pImpl_->windowVisibilityStateListeners_.empty() && pImpl_->windowVisibilityStateListenerAgent_ != nullptr) {
512 ret = SingletonContainer::Get<WindowAdapterLite>().UnregisterWindowManagerAgent(
513 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_VISIBILITY,
514 pImpl_->windowVisibilityStateListenerAgent_);
515 if (ret == WMError::WM_OK) {
516 pImpl_->windowVisibilityStateListenerAgent_ = nullptr;
517 }
518 }
519 return ret;
520 }
521
GetFocusWindowInfo(FocusChangeInfo & focusInfo,DisplayId displayId)522 void WindowManagerLite::GetFocusWindowInfo(FocusChangeInfo& focusInfo, DisplayId displayId)
523 {
524 WLOGFD("In");
525 SingletonContainer::Get<WindowAdapterLite>().GetFocusWindowInfo(focusInfo, displayId);
526 }
527
UpdateFocusChangeInfo(const sptr<FocusChangeInfo> & focusChangeInfo,bool focused) const528 void WindowManagerLite::UpdateFocusChangeInfo(const sptr<FocusChangeInfo>& focusChangeInfo, bool focused) const
529 {
530 if (focusChangeInfo == nullptr) {
531 WLOGFE("focusChangeInfo is nullptr.");
532 return;
533 }
534 WLOGFD("[WMSFocus]window focus change: %{public}d, id: %{public}u", focused, focusChangeInfo->windowId_);
535 if (focused) {
536 pImpl_->NotifyFocused(focusChangeInfo);
537 } else {
538 pImpl_->NotifyUnfocused(focusChangeInfo);
539 }
540 }
541
UpdateWindowVisibilityInfo(const std::vector<sptr<WindowVisibilityInfo>> & windowVisibilityInfos) const542 void WindowManagerLite::UpdateWindowVisibilityInfo(
543 const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfos) const
544 {
545 pImpl_->NotifyWindowVisibilityInfoChanged(windowVisibilityInfos);
546 pImpl_->NotifyWindowVisibilityStateChanged(windowVisibilityInfos);
547 }
548
GetVisibilityWindowInfo(std::vector<sptr<WindowVisibilityInfo>> & infos) const549 WMError WindowManagerLite::GetVisibilityWindowInfo(std::vector<sptr<WindowVisibilityInfo>>& infos) const
550 {
551 WMError ret = SingletonContainer::Get<WindowAdapterLite>().GetVisibilityWindowInfo(infos);
552 if (ret != WMError::WM_OK) {
553 WLOGFE("get window visibility info failed");
554 }
555 return ret;
556 }
557
UpdateWindowDrawingContentInfo(const std::vector<sptr<WindowDrawingContentInfo>> & windowDrawingContentInfos) const558 void WindowManagerLite::UpdateWindowDrawingContentInfo(
559 const std::vector<sptr<WindowDrawingContentInfo>>& windowDrawingContentInfos) const
560 {
561 pImpl_->NotifyWindowDrawingContentInfoChanged(windowDrawingContentInfos);
562 }
563
UpdateCameraWindowStatus(uint32_t accessTokenId,bool isShowing) const564 void WindowManagerLite::UpdateCameraWindowStatus(uint32_t accessTokenId, bool isShowing) const
565 {
566 pImpl_->UpdateCameraWindowStatus(accessTokenId, isShowing);
567 }
568
OnRemoteDied()569 void WindowManagerLite::OnRemoteDied()
570 {
571 WLOGI("wms is died");
572 std::lock_guard<std::recursive_mutex> lock(mutex_);
573 if (destroyed_) {
574 WLOGE("Already destroyed");
575 return;
576 }
577 pImpl_->focusChangedListenerAgent_ = nullptr;
578 pImpl_->windowUpdateListenerAgent_ = nullptr;
579 pImpl_->windowVisibilityListenerAgent_ = nullptr;
580 pImpl_->windowDrawingContentListenerAgent_ = nullptr;
581 }
582
RegisterDrawingContentChangedListener(const sptr<IDrawingContentChangedListener> & listener)583 WMError WindowManagerLite::RegisterDrawingContentChangedListener(const sptr<IDrawingContentChangedListener>& listener)
584 {
585 if (listener == nullptr) {
586 WLOGFE("listener could not be null");
587 return WMError::WM_ERROR_NULLPTR;
588 }
589 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
590 WMError ret = WMError::WM_OK;
591 if (pImpl_->windowDrawingContentListenerAgent_ == nullptr) {
592 pImpl_->windowDrawingContentListenerAgent_ = new (std::nothrow) WindowManagerAgentLite();
593 ret = SingletonContainer::Get<WindowAdapterLite>().RegisterWindowManagerAgent(
594 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_DRAWING_STATE,
595 pImpl_->windowDrawingContentListenerAgent_);
596 }
597 if (ret != WMError::WM_OK) {
598 WLOGFW("RegisterWindowManagerAgent failed !");
599 pImpl_->windowDrawingContentListenerAgent_ = nullptr;
600 } else {
601 auto iter = std::find(pImpl_->windowDrawingContentListeners_.begin(),
602 pImpl_->windowDrawingContentListeners_.end(), listener);
603 if (iter != pImpl_->windowDrawingContentListeners_.end()) {
604 WLOGFW("Listener is already registered.");
605 return WMError::WM_OK;
606 }
607 pImpl_->windowDrawingContentListeners_.emplace_back(listener);
608 }
609 return ret;
610 }
611
UnregisterDrawingContentChangedListener(const sptr<IDrawingContentChangedListener> & listener)612 WMError WindowManagerLite::UnregisterDrawingContentChangedListener(const sptr<IDrawingContentChangedListener>& listener)
613 {
614 if (listener == nullptr) {
615 WLOGFE("listener could not be null");
616 return WMError::WM_ERROR_NULLPTR;
617 }
618 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
619 pImpl_->windowDrawingContentListeners_.erase(std::remove_if(pImpl_->windowDrawingContentListeners_.begin(),
620 pImpl_->windowDrawingContentListeners_.end(),
621 [listener](sptr<IDrawingContentChangedListener> registeredListener) { return registeredListener == listener; }),
622 pImpl_->windowDrawingContentListeners_.end());
623
624 WMError ret = WMError::WM_OK;
625 if (pImpl_->windowDrawingContentListeners_.empty() && pImpl_->windowDrawingContentListenerAgent_ != nullptr) {
626 ret = SingletonContainer::Get<WindowAdapterLite>().UnregisterWindowManagerAgent(
627 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_DRAWING_STATE,
628 pImpl_->windowDrawingContentListenerAgent_);
629 if (ret == WMError::WM_OK) {
630 pImpl_->windowDrawingContentListenerAgent_ = nullptr;
631 }
632 }
633 return ret;
634 }
635
UpdateWindowModeTypeInfo(WindowModeType type) const636 void WindowManagerLite::UpdateWindowModeTypeInfo(WindowModeType type) const
637 {
638 pImpl_->NotifyWindowModeChange(type);
639 }
640
NotifyAccessibilityWindowInfo(const std::vector<sptr<AccessibilityWindowInfo>> & infos,WindowUpdateType type) const641 void WindowManagerLite::NotifyAccessibilityWindowInfo(const std::vector<sptr<AccessibilityWindowInfo>>& infos,
642 WindowUpdateType type) const
643 {
644 pImpl_->NotifyAccessibilityWindowInfo(infos, type);
645 }
646
GetWindowModeType(WindowModeType & windowModeType) const647 WMError WindowManagerLite::GetWindowModeType(WindowModeType& windowModeType) const
648 {
649 WMError ret = SingletonContainer::Get<WindowAdapterLite>().GetWindowModeType(windowModeType);
650 if (ret != WMError::WM_OK) {
651 WLOGFE("get window visibility info failed");
652 }
653 return ret;
654 }
655
RegisterWindowModeChangedListener(const sptr<IWindowModeChangedListener> & listener)656 WMError WindowManagerLite::RegisterWindowModeChangedListener(const sptr<IWindowModeChangedListener>& listener)
657 {
658 if (listener == nullptr) {
659 TLOGE(WmsLogTag::WMS_MAIN, "listener could not be null");
660 return WMError::WM_ERROR_NULLPTR;
661 }
662
663 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
664 if (pImpl_->windowModeListenerAgent_ == nullptr) {
665 pImpl_->windowModeListenerAgent_ = new (std::nothrow) WindowManagerAgentLite();
666 }
667 WMError ret = SingletonContainer::Get<WindowAdapterLite>().RegisterWindowManagerAgent(
668 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_MODE, pImpl_->windowModeListenerAgent_);
669 if (ret != WMError::WM_OK) {
670 TLOGW(WmsLogTag::WMS_MAIN, "RegisterWindowManagerAgent failed!");
671 pImpl_->windowModeListenerAgent_ = nullptr;
672 return ret;
673 }
674 auto iter = std::find(pImpl_->windowModeListeners_.begin(), pImpl_->windowModeListeners_.end(), listener);
675 if (iter != pImpl_->windowModeListeners_.end()) {
676 TLOGW(WmsLogTag::WMS_MAIN, "Listener is already registered.");
677 return WMError::WM_OK;
678 }
679 pImpl_->windowModeListeners_.push_back(listener);
680 return ret;
681 }
682
UnregisterWindowModeChangedListener(const sptr<IWindowModeChangedListener> & listener)683 WMError WindowManagerLite::UnregisterWindowModeChangedListener(const sptr<IWindowModeChangedListener>& listener)
684 {
685 if (listener == nullptr) {
686 TLOGE(WmsLogTag::WMS_MAIN, "listener could not be null");
687 return WMError::WM_ERROR_NULLPTR;
688 }
689
690 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
691 auto iter = std::find(pImpl_->windowModeListeners_.begin(), pImpl_->windowModeListeners_.end(), listener);
692 if (iter == pImpl_->windowModeListeners_.end()) {
693 TLOGE(WmsLogTag::WMS_MAIN, "could not find this listener");
694 return WMError::WM_OK;
695 }
696 pImpl_->windowModeListeners_.erase(iter);
697 WMError ret = WMError::WM_OK;
698 if (pImpl_->windowModeListeners_.empty() && pImpl_->windowModeListenerAgent_ != nullptr) {
699 ret = SingletonContainer::Get<WindowAdapterLite>().UnregisterWindowManagerAgent(
700 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_MODE, pImpl_->windowModeListenerAgent_);
701 if (ret == WMError::WM_OK) {
702 pImpl_->windowModeListenerAgent_ = nullptr;
703 }
704 }
705 return ret;
706 }
707
RegisterCameraWindowChangedListener(const sptr<ICameraWindowChangedListener> & listener)708 WMError WindowManagerLite::RegisterCameraWindowChangedListener(const sptr<ICameraWindowChangedListener>& listener)
709 {
710 if (listener == nullptr) {
711 TLOGE(WmsLogTag::WMS_SYSTEM, "listener could not be null");
712 return WMError::WM_ERROR_NULLPTR;
713 }
714
715 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
716 if (pImpl_->cameraWindowChangedListenerAgent_ == nullptr) {
717 pImpl_->cameraWindowChangedListenerAgent_ = new WindowManagerAgentLite();
718 }
719 WMError ret = SingletonContainer::Get<WindowAdapterLite>().RegisterWindowManagerAgent(
720 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_WINDOW, pImpl_->cameraWindowChangedListenerAgent_);
721 if (ret != WMError::WM_OK) {
722 TLOGW(WmsLogTag::WMS_SYSTEM, "RegisterWindowManagerAgent failed!");
723 pImpl_->cameraWindowChangedListenerAgent_ = nullptr;
724 } else {
725 auto iter = std::find(pImpl_->cameraWindowChangedListeners_.begin(),
726 pImpl_->cameraWindowChangedListeners_.end(), listener);
727 if (iter != pImpl_->cameraWindowChangedListeners_.end()) {
728 TLOGW(WmsLogTag::WMS_SYSTEM, "Listener is already registered.");
729 return WMError::WM_OK;
730 }
731 pImpl_->cameraWindowChangedListeners_.push_back(listener);
732 }
733 return ret;
734 }
735
UnregisterCameraWindowChangedListener(const sptr<ICameraWindowChangedListener> & listener)736 WMError WindowManagerLite::UnregisterCameraWindowChangedListener(const sptr<ICameraWindowChangedListener>& listener)
737 {
738 if (listener == nullptr) {
739 TLOGE(WmsLogTag::WMS_SYSTEM, "listener could not be null");
740 return WMError::WM_ERROR_NULLPTR;
741 }
742
743 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
744 auto iter = std::find(pImpl_->cameraWindowChangedListeners_.begin(),
745 pImpl_->cameraWindowChangedListeners_.end(), listener);
746 if (iter == pImpl_->cameraWindowChangedListeners_.end()) {
747 TLOGE(WmsLogTag::WMS_SYSTEM, "could not find this listener");
748 return WMError::WM_OK;
749 }
750 pImpl_->cameraWindowChangedListeners_.erase(iter);
751 WMError ret = WMError::WM_OK;
752 if (pImpl_->cameraWindowChangedListeners_.empty() &&
753 pImpl_->cameraWindowChangedListenerAgent_ != nullptr) {
754 ret = SingletonContainer::Get<WindowAdapterLite>().UnregisterWindowManagerAgent(
755 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_WINDOW,
756 pImpl_->cameraWindowChangedListenerAgent_);
757 if (ret == WMError::WM_OK) {
758 pImpl_->cameraWindowChangedListenerAgent_ = nullptr;
759 }
760 }
761 return ret;
762 }
763
RaiseWindowToTop(int32_t persistentId)764 WMError WindowManagerLite::RaiseWindowToTop(int32_t persistentId)
765 {
766 WMError ret = SingletonContainer::Get<WindowAdapterLite>().RaiseWindowToTop(persistentId);
767 if (ret != WMError::WM_OK) {
768 TLOGE(WmsLogTag::WMS_SYSTEM, "raise window to top failed.");
769 }
770 return ret;
771 }
772
GetMainWindowInfos(int32_t topNum,std::vector<MainWindowInfo> & topNInfo)773 WMError WindowManagerLite::GetMainWindowInfos(int32_t topNum, std::vector<MainWindowInfo>& topNInfo)
774 {
775 TLOGI(WmsLogTag::WMS_MAIN, "Get main window info lite");
776 return SingletonContainer::Get<WindowAdapterLite>().GetMainWindowInfos(topNum, topNInfo);
777 }
778
GetCallingWindowInfo(CallingWindowInfo & callingWindowInfo)779 WMError WindowManagerLite::GetCallingWindowInfo(CallingWindowInfo& callingWindowInfo)
780 {
781 return SingletonContainer::Get<WindowAdapterLite>().GetCallingWindowInfo(callingWindowInfo);
782 }
783
RegisterWMSConnectionChangedListener(const sptr<IWMSConnectionChangedListener> & listener)784 WMError WindowManagerLite::RegisterWMSConnectionChangedListener(const sptr<IWMSConnectionChangedListener>& listener)
785 {
786 int32_t clientUserId = GetUserIdByUid(getuid());
787 // only system applications or services with a userId of 0 are allowed to communicate
788 // with multiple WMS-Servers and are permitted to listen for WMS connection status.
789 if (clientUserId != SYSTEM_USERID) {
790 TLOGW(WmsLogTag::WMS_MULTI_USER, "Permission denied");
791 return WMError::WM_ERROR_INVALID_PERMISSION;
792 }
793 if (listener == nullptr) {
794 TLOGE(WmsLogTag::WMS_MULTI_USER, "Register failed: listener is null");
795 return WMError::WM_ERROR_NULLPTR;
796 }
797 TLOGI(WmsLogTag::WMS_MULTI_USER, "Start registration");
798 {
799 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
800 if (pImpl_->wmsConnectionChangedListener_) {
801 TLOGI(WmsLogTag::WMS_MULTI_USER, "Listener already registered, skipping");
802 return WMError::WM_OK;
803 }
804 pImpl_->wmsConnectionChangedListener_ = listener;
805 }
806 auto ret = WindowAdapterLite::GetInstance().RegisterWMSConnectionChangedListener(
807 std::bind(&WindowManagerLite::OnWMSConnectionChanged, this,
808 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
809 if (ret != WMError::WM_OK) {
810 TLOGE(WmsLogTag::WMS_MULTI_USER, "Register failed: error = %{public}d", static_cast<int32_t>(ret));
811 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
812 pImpl_->wmsConnectionChangedListener_ = nullptr;
813 }
814 return ret;
815 }
816
UnregisterWMSConnectionChangedListener()817 WMError WindowManagerLite::UnregisterWMSConnectionChangedListener()
818 {
819 TLOGI(WmsLogTag::WMS_MULTI_USER, "Unregister enter");
820 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
821 pImpl_->wmsConnectionChangedListener_ = nullptr;
822 WindowAdapterLite::GetInstance().UnregisterWMSConnectionChangedListener();
823 return WMError::WM_OK;
824 }
825
OnWMSConnectionChanged(int32_t userId,int32_t screenId,bool isConnected) const826 void WindowManagerLite::OnWMSConnectionChanged(int32_t userId, int32_t screenId, bool isConnected) const
827 {
828 if (isConnected) {
829 pImpl_->NotifyWMSConnected(userId, screenId);
830 } else {
831 pImpl_->NotifyWMSDisconnected(userId, screenId);
832 }
833 }
834
GetAllMainWindowInfos(std::vector<MainWindowInfo> & infos) const835 WMError WindowManagerLite::GetAllMainWindowInfos(std::vector<MainWindowInfo>& infos) const
836 {
837 if (!infos.empty()) {
838 TLOGE(WmsLogTag::WMS_MAIN, "infos is not empty.");
839 return WMError::WM_ERROR_INVALID_PARAM;
840 }
841 return SingletonContainer::Get<WindowAdapterLite>().GetAllMainWindowInfos(infos);
842 }
843
ClearMainSessions(const std::vector<int32_t> & persistentIds)844 WMError WindowManagerLite::ClearMainSessions(const std::vector<int32_t>& persistentIds)
845 {
846 if (persistentIds.empty()) {
847 TLOGW(WmsLogTag::WMS_MAIN, "Clear main Session failed, persistentIds is empty.");
848 return WMError::WM_OK;
849 }
850 return SingletonContainer::Get<WindowAdapterLite>().ClearMainSessions(persistentIds);
851 }
852
ClearMainSessions(const std::vector<int32_t> & persistentIds,std::vector<int32_t> & clearFailedIds)853 WMError WindowManagerLite::ClearMainSessions(const std::vector<int32_t>& persistentIds,
854 std::vector<int32_t>& clearFailedIds)
855 {
856 if (persistentIds.empty()) {
857 TLOGW(WmsLogTag::WMS_MAIN, "Clear main Session failed, persistentIds is empty.");
858 return WMError::WM_OK;
859 }
860 return SingletonContainer::Get<WindowAdapterLite>().ClearMainSessions(persistentIds, clearFailedIds);
861 }
862
NotifyWindowStyleChange(WindowStyleType type)863 WMError WindowManagerLite::NotifyWindowStyleChange(WindowStyleType type)
864 {
865 pImpl_->NotifyWindowStyleChange(type);
866 return WMError::WM_OK;
867 }
868
NotifyCallingWindowDisplayChanged(const CallingWindowInfo & callingWindowInfo)869 WMError WindowManagerLite::NotifyCallingWindowDisplayChanged(const CallingWindowInfo& callingWindowInfo)
870 {
871 pImpl_->NotifyCallingWindowDisplayChanged(callingWindowInfo);
872 return WMError::WM_OK;
873 }
874
RegisterWindowStyleChangedListener(const sptr<IWindowStyleChangedListener> & listener)875 WMError WindowManagerLite::RegisterWindowStyleChangedListener(const sptr<IWindowStyleChangedListener>& listener)
876 {
877 TLOGI(WmsLogTag::WMS_MAIN, "start register windowStyleChangedListener");
878 if (listener == nullptr) {
879 TLOGE(WmsLogTag::WMS_MAIN, "listener could not be null");
880 return WMError::WM_ERROR_NULLPTR;
881 }
882 {
883 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
884 if (pImpl_->windowStyleListenerAgent_ == nullptr) {
885 pImpl_->windowStyleListenerAgent_ = new WindowManagerAgentLite();
886 }
887 auto iter = std::find(pImpl_->windowStyleListeners_.begin(), pImpl_->windowStyleListeners_.end(), listener);
888 if (iter != pImpl_->windowStyleListeners_.end()) {
889 TLOGW(WmsLogTag::WMS_MAIN, "Listener is already registered.");
890 return WMError::WM_OK;
891 }
892 pImpl_->windowStyleListeners_.push_back(listener);
893 }
894 WMError ret = WMError::WM_OK;
895 ret = SingletonContainer::Get<WindowAdapterLite>().RegisterWindowManagerAgent(
896 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_STYLE, pImpl_->windowStyleListenerAgent_);
897 if (ret != WMError::WM_OK) {
898 TLOGW(WmsLogTag::WMS_MAIN, "RegisterWindowManagerAgent failed!");
899 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
900 pImpl_->windowStyleListenerAgent_ = nullptr;
901 auto iter = std::find(pImpl_->windowStyleListeners_.begin(), pImpl_->windowStyleListeners_.end(), listener);
902 if (iter != pImpl_->windowStyleListeners_.end()) {
903 pImpl_->windowStyleListeners_.erase(iter);
904 }
905 }
906 return ret;
907 }
908
UnregisterWindowStyleChangedListener(const sptr<IWindowStyleChangedListener> & listener)909 WMError WindowManagerLite::UnregisterWindowStyleChangedListener(const sptr<IWindowStyleChangedListener>& listener)
910 {
911 TLOGI(WmsLogTag::WMS_MAIN, "start unregister windowStyleChangedListener");
912 if (listener == nullptr) {
913 TLOGE(WmsLogTag::WMS_MAIN, "listener could not be null");
914 return WMError::WM_ERROR_NULLPTR;
915 }
916 {
917 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
918 auto iter = std::find(pImpl_->windowStyleListeners_.begin(), pImpl_->windowStyleListeners_.end(), listener);
919 if (iter == pImpl_->windowStyleListeners_.end()) {
920 TLOGE(WmsLogTag::WMS_MAIN, "could not find this listener");
921 return WMError::WM_OK;
922 }
923 pImpl_->windowStyleListeners_.erase(iter);
924 }
925 WMError ret = WMError::WM_OK;
926 if (pImpl_->windowStyleListeners_.empty() && pImpl_->windowStyleListenerAgent_ != nullptr) {
927 ret = SingletonContainer::Get<WindowAdapterLite>().UnregisterWindowManagerAgent(
928 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_STYLE, pImpl_->windowStyleListenerAgent_);
929 if (ret == WMError::WM_OK) {
930 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
931 pImpl_->windowStyleListenerAgent_ = nullptr;
932 }
933 }
934 return ret;
935 }
936
RegisterCallingWindowDisplayChangedListener(const sptr<IKeyboardCallingWindowDisplayChangedListener> & listener)937 WMError WindowManagerLite::RegisterCallingWindowDisplayChangedListener(
938 const sptr<IKeyboardCallingWindowDisplayChangedListener>& listener)
939 {
940 TLOGI(WmsLogTag::WMS_KEYBOARD, "start register callingDisplayChangeListener");
941 if (listener == nullptr) {
942 TLOGE(WmsLogTag::WMS_KEYBOARD, "listener could not be null");
943 return WMError::WM_ERROR_NULLPTR;
944 }
945 {
946 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
947 if (pImpl_->callingDisplayListenerAgent_ == nullptr) {
948 pImpl_->callingDisplayListenerAgent_ = new WindowManagerAgentLite();
949 }
950 auto iter = std::find(pImpl_->callingDisplayChangedListeners_.begin(),
951 pImpl_->callingDisplayChangedListeners_.end(), listener);
952 if (iter != pImpl_->callingDisplayChangedListeners_.end()) {
953 TLOGE(WmsLogTag::WMS_KEYBOARD, "listener is already registered");
954 return WMError::WM_OK;
955 }
956 pImpl_->callingDisplayChangedListeners_.emplace_back(listener);
957 }
958 WMError ret = WMError::WM_OK;
959 ret = SingletonContainer::Get<WindowAdapterLite>().RegisterWindowManagerAgent(
960 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CALLING_DISPLAY, pImpl_->callingDisplayListenerAgent_);
961 if (ret != WMError::WM_OK) {
962 TLOGW(WmsLogTag::WMS_KEYBOARD, "Register agent failed!");
963 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
964 pImpl_->callingDisplayListenerAgent_ = nullptr;
965 auto iter = std::find(pImpl_->callingDisplayChangedListeners_.begin(),
966 pImpl_->callingDisplayChangedListeners_.end(), listener);
967 if (iter != pImpl_->callingDisplayChangedListeners_.end()) {
968 pImpl_->callingDisplayChangedListeners_.erase(iter);
969 }
970 }
971 return ret;
972 }
973
UnregisterCallingWindowDisplayChangedListener(const sptr<IKeyboardCallingWindowDisplayChangedListener> & listener)974 WMError WindowManagerLite::UnregisterCallingWindowDisplayChangedListener(
975 const sptr<IKeyboardCallingWindowDisplayChangedListener>& listener)
976 {
977 TLOGI(WmsLogTag::WMS_KEYBOARD, "start unRegister callingDisplayChangeListener");
978 if (listener == nullptr) {
979 TLOGE(WmsLogTag::WMS_KEYBOARD, "listener could not be null");
980 return WMError::WM_ERROR_NULLPTR;
981 }
982 {
983 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
984 auto iter = std::find(pImpl_->callingDisplayChangedListeners_.begin(),
985 pImpl_->callingDisplayChangedListeners_.end(), listener);
986 if (iter == pImpl_->callingDisplayChangedListeners_.end()) {
987 TLOGE(WmsLogTag::WMS_KEYBOARD, "could not find this listener");
988 return WMError::WM_OK;
989 }
990 pImpl_->callingDisplayChangedListeners_.erase(iter);
991 }
992 WMError ret = WMError::WM_OK;
993 if (pImpl_->callingDisplayChangedListeners_.empty() && pImpl_->callingDisplayListenerAgent_ != nullptr) {
994 ret = SingletonContainer::Get<WindowAdapterLite>().UnregisterWindowManagerAgent(
995 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CALLING_DISPLAY, pImpl_->callingDisplayListenerAgent_);
996 if (ret == WMError::WM_OK) {
997 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
998 pImpl_->callingDisplayListenerAgent_ = nullptr;
999 }
1000 }
1001 return ret;
1002 }
1003
GetWindowStyleType()1004 WindowStyleType WindowManagerLite::GetWindowStyleType()
1005 {
1006 WindowStyleType styleType;
1007 if (SingletonContainer::Get<WindowAdapterLite>().GetWindowStyleType(styleType) == WMError::WM_OK) {
1008 return styleType;
1009 }
1010 return styleType;
1011 }
1012
TerminateSessionByPersistentId(int32_t persistentId)1013 WMError WindowManagerLite::TerminateSessionByPersistentId(int32_t persistentId)
1014 {
1015 if (persistentId == INVALID_SESSION_ID) {
1016 TLOGE(WmsLogTag::WMS_LIFE, "persistentId is invalid.");
1017 return WMError::WM_ERROR_INVALID_PARAM;
1018 }
1019 return SingletonContainer::Get<WindowAdapterLite>().TerminateSessionByPersistentId(persistentId);
1020 }
1021
CloseTargetFloatWindow(const std::string & bundleName)1022 WMError WindowManagerLite::CloseTargetFloatWindow(const std::string& bundleName)
1023 {
1024 if (bundleName.empty()) {
1025 TLOGE(WmsLogTag::WMS_MULTI_WINDOW, "bundleName is empty.");
1026 return WMError::WM_ERROR_INVALID_PARAM;
1027 }
1028 return SingletonContainer::Get<WindowAdapterLite>().CloseTargetFloatWindow(bundleName);
1029 }
1030
RegisterPiPStateChangedListener(const sptr<IPiPStateChangedListener> & listener)1031 WMError WindowManagerLite::RegisterPiPStateChangedListener(const sptr<IPiPStateChangedListener>& listener)
1032 {
1033 TLOGI(WmsLogTag::WMS_PIP, "in");
1034 if (listener == nullptr) {
1035 TLOGE(WmsLogTag::WMS_PIP, "listener could not be null");
1036 return WMError::WM_ERROR_NULLPTR;
1037 }
1038
1039 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
1040 if (pImpl_->pipStateChangedListenerAgent_ == nullptr) {
1041 pImpl_->pipStateChangedListenerAgent_ = new WindowManagerAgentLite();
1042 }
1043 WMError ret = SingletonContainer::Get<WindowAdapterLite>().RegisterWindowManagerAgent(
1044 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_PIP, pImpl_->pipStateChangedListenerAgent_);
1045 if (ret != WMError::WM_OK) {
1046 TLOGW(WmsLogTag::WMS_PIP, "RegisterWindowManagerAgent failed!");
1047 pImpl_->pipStateChangedListenerAgent_ = nullptr;
1048 } else {
1049 auto iter = std::find(pImpl_->pipStateChangedListeners_.begin(),
1050 pImpl_->pipStateChangedListeners_.end(), listener);
1051 if (iter != pImpl_->pipStateChangedListeners_.end()) {
1052 TLOGW(WmsLogTag::WMS_PIP, "Listener is already registered.");
1053 return WMError::WM_OK;
1054 }
1055 pImpl_->pipStateChangedListeners_.push_back(listener);
1056 }
1057 return ret;
1058 }
1059
UnregisterPiPStateChangedListener(const sptr<IPiPStateChangedListener> & listener)1060 WMError WindowManagerLite::UnregisterPiPStateChangedListener(const sptr<IPiPStateChangedListener>& listener)
1061 {
1062 TLOGI(WmsLogTag::WMS_PIP, "in");
1063 if (listener == nullptr) {
1064 TLOGE(WmsLogTag::WMS_PIP, "listener could not be null");
1065 return WMError::WM_ERROR_NULLPTR;
1066 }
1067
1068 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
1069 auto iter = std::find(pImpl_->pipStateChangedListeners_.begin(),
1070 pImpl_->pipStateChangedListeners_.end(), listener);
1071 if (iter == pImpl_->pipStateChangedListeners_.end()) {
1072 TLOGE(WmsLogTag::WMS_PIP, "could not find this listener");
1073 return WMError::WM_OK;
1074 }
1075 pImpl_->pipStateChangedListeners_.erase(iter);
1076 WMError ret = WMError::WM_OK;
1077 if (pImpl_->pipStateChangedListeners_.empty() &&
1078 pImpl_->pipStateChangedListenerAgent_ != nullptr) {
1079 ret = SingletonContainer::Get<WindowAdapterLite>().UnregisterWindowManagerAgent(
1080 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_PIP,
1081 pImpl_->pipStateChangedListenerAgent_);
1082 if (ret == WMError::WM_OK) {
1083 pImpl_->pipStateChangedListenerAgent_ = nullptr;
1084 }
1085 }
1086 return ret;
1087 }
1088
CloseTargetPiPWindow(const std::string & bundleName)1089 WMError WindowManagerLite::CloseTargetPiPWindow(const std::string& bundleName)
1090 {
1091 if (bundleName.empty()) {
1092 TLOGE(WmsLogTag::WMS_PIP, "bundleName is empty.");
1093 return WMError::WM_ERROR_INVALID_PARAM;
1094 }
1095 TLOGD(WmsLogTag::WMS_PIP, "bundleName:%{public}s", bundleName.c_str());
1096 return SingletonContainer::Get<WindowAdapterLite>().CloseTargetPiPWindow(bundleName);
1097 }
1098
GetCurrentPiPWindowInfo(std::string & bundleName)1099 WMError WindowManagerLite::GetCurrentPiPWindowInfo(std::string& bundleName)
1100 {
1101 return SingletonContainer::Get<WindowAdapterLite>().GetCurrentPiPWindowInfo(bundleName);
1102 }
1103
UpdatePiPWindowStateChanged(const std::string & bundleName,bool isForeground) const1104 void WindowManagerLite::UpdatePiPWindowStateChanged(const std::string& bundleName, bool isForeground) const
1105 {
1106 pImpl_->UpdatePiPWindowStateChanged(bundleName, isForeground);
1107 }
1108
GetAccessibilityWindowInfo(std::vector<sptr<AccessibilityWindowInfo>> & infos) const1109 WMError WindowManagerLite::GetAccessibilityWindowInfo(std::vector<sptr<AccessibilityWindowInfo>>& infos) const
1110 {
1111 WMError ret = SingletonContainer::Get<WindowAdapterLite>().GetAccessibilityWindowInfo(infos);
1112 if (ret != WMError::WM_OK) {
1113 WLOGFE("get window info failed");
1114 }
1115 return ret;
1116 }
1117
RegisterWindowUpdateListener(const sptr<IWindowUpdateListener> & listener)1118 WMError WindowManagerLite::RegisterWindowUpdateListener(const sptr<IWindowUpdateListener>& listener)
1119 {
1120 if (listener == nullptr) {
1121 WLOGFE("listener could not be null");
1122 return WMError::WM_ERROR_NULLPTR;
1123 }
1124 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
1125 if (pImpl_->windowUpdateListenerAgent_ == nullptr) {
1126 pImpl_->windowUpdateListenerAgent_ = new WindowManagerAgentLite();
1127 }
1128 WMError ret = SingletonContainer::Get<WindowAdapterLite>().RegisterWindowManagerAgent(
1129 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_UPDATE, pImpl_->windowUpdateListenerAgent_);
1130 if (ret != WMError::WM_OK) {
1131 WLOGFW("RegisterWindowManagerAgent failed!");
1132 pImpl_->windowUpdateListenerAgent_ = nullptr;
1133 } else {
1134 auto iter = std::find(pImpl_->windowUpdateListeners_.begin(), pImpl_->windowUpdateListeners_.end(), listener);
1135 if (iter != pImpl_->windowUpdateListeners_.end()) {
1136 WLOGI("Listener is already registered.");
1137 return WMError::WM_OK;
1138 }
1139 pImpl_->windowUpdateListeners_.emplace_back(listener);
1140 }
1141 return ret;
1142 }
1143
UnregisterWindowUpdateListener(const sptr<IWindowUpdateListener> & listener)1144 WMError WindowManagerLite::UnregisterWindowUpdateListener(const sptr<IWindowUpdateListener>& listener)
1145 {
1146 if (listener == nullptr) {
1147 WLOGFE("listener could not be null");
1148 return WMError::WM_ERROR_NULLPTR;
1149 }
1150 std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
1151 auto iter = std::find(pImpl_->windowUpdateListeners_.begin(), pImpl_->windowUpdateListeners_.end(), listener);
1152 if (iter == pImpl_->windowUpdateListeners_.end()) {
1153 WLOGFE("could not find this listener");
1154 return WMError::WM_OK;
1155 }
1156 pImpl_->windowUpdateListeners_.erase(iter);
1157 WMError ret = WMError::WM_OK;
1158 if (pImpl_->windowUpdateListeners_.empty() && pImpl_->windowUpdateListenerAgent_ != nullptr) {
1159 ret = SingletonContainer::Get<WindowAdapterLite>().UnregisterWindowManagerAgent(
1160 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_UPDATE, pImpl_->windowUpdateListenerAgent_);
1161 if (ret == WMError::WM_OK) {
1162 pImpl_->windowUpdateListenerAgent_ = nullptr;
1163 }
1164 }
1165 return ret;
1166 }
1167
ProcessRegisterWindowInfoChangeCallback(WindowInfoKey observedInfo,const sptr<IWindowInfoChangedListener> & listener)1168 WMError WindowManagerLite::ProcessRegisterWindowInfoChangeCallback(WindowInfoKey observedInfo,
1169 const sptr<IWindowInfoChangedListener>& listener)
1170 {
1171 switch (observedInfo) {
1172 case WindowInfoKey::VISIBILITY_STATE :
1173 return RegisterVisibilityStateChangedListener(listener);
1174 default:
1175 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "Invalid observedInfo: %{public}d", static_cast<uint32_t>(observedInfo));
1176 return WMError::WM_ERROR_INVALID_PARAM;
1177 }
1178 }
1179
ProcessUnregisterWindowInfoChangeCallback(WindowInfoKey observedInfo,const sptr<IWindowInfoChangedListener> & listener)1180 WMError WindowManagerLite::ProcessUnregisterWindowInfoChangeCallback(WindowInfoKey observedInfo,
1181 const sptr<IWindowInfoChangedListener>& listener)
1182 {
1183 switch (observedInfo) {
1184 case WindowInfoKey::VISIBILITY_STATE :
1185 return UnregisterVisibilityStateChangedListener(listener);
1186 default:
1187 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "Invalid observedInfo: %{public}d", static_cast<uint32_t>(observedInfo));
1188 return WMError::WM_ERROR_INVALID_PARAM;
1189 }
1190 }
1191
RegisterWindowInfoChangeCallback(const std::unordered_set<WindowInfoKey> & observedInfo,const sptr<IWindowInfoChangedListener> & listener)1192 WMError WindowManagerLite::RegisterWindowInfoChangeCallback(const std::unordered_set<WindowInfoKey>& observedInfo,
1193 const sptr<IWindowInfoChangedListener>& listener)
1194 {
1195 std::ostringstream observedInfoForLog;
1196 observedInfoForLog << "ObservedInfo: ";
1197 auto ret = WMError::WM_OK;
1198 for (const auto& info : observedInfo) {
1199 observedInfoForLog << static_cast<uint32_t>(info) << ", ";
1200 if (listener->GetInterestInfo().find(info) == listener->GetInterestInfo().end()) {
1201 listener->AddInterestInfo(info);
1202 }
1203 ret = ProcessRegisterWindowInfoChangeCallback(info, listener);
1204 if (ret != WMError::WM_OK) {
1205 observedInfoForLog << "failed";
1206 break;
1207 }
1208 }
1209 TLOGI(WmsLogTag::WMS_ATTRIBUTE, "%{public}s", observedInfoForLog.str().c_str());
1210 return ret;
1211 }
1212
UnregisterWindowInfoChangeCallback(const std::unordered_set<WindowInfoKey> & observedInfo,const sptr<IWindowInfoChangedListener> & listener)1213 WMError WindowManagerLite::UnregisterWindowInfoChangeCallback(const std::unordered_set<WindowInfoKey>& observedInfo,
1214 const sptr<IWindowInfoChangedListener>& listener)
1215 {
1216 std::ostringstream observedInfoForLog;
1217 observedInfoForLog << "ObservedInfo: ";
1218 auto ret = WMError::WM_OK;
1219 for (const auto& info : observedInfo) {
1220 observedInfoForLog << static_cast<uint32_t>(info) << ", ";
1221 if (listener->GetInterestInfo().find(info) == listener->GetInterestInfo().end()) {
1222 listener->AddInterestInfo(info);
1223 }
1224 ret = ProcessUnregisterWindowInfoChangeCallback(info, listener);
1225 if (ret != WMError::WM_OK) {
1226 observedInfoForLog << "failed";
1227 break;
1228 }
1229 }
1230 TLOGI(WmsLogTag::WMS_ATTRIBUTE, "%{public}s", observedInfoForLog.str().c_str());
1231 return ret;
1232 }
1233
ListWindowInfo(const WindowInfoOption & windowInfoOption,std::vector<sptr<WindowInfo>> & infos) const1234 WMError WindowManagerLite::ListWindowInfo(const WindowInfoOption& windowInfoOption,
1235 std::vector<sptr<WindowInfo>>& infos) const
1236 {
1237 TLOGD(WmsLogTag::WMS_ATTRIBUTE, "windowInfoOption: %{public}u %{public}u %{public}" PRIu64 " %{public}d",
1238 static_cast<WindowInfoFilterOptionDataType>(windowInfoOption.windowInfoFilterOption),
1239 static_cast<WindowInfoTypeOptionDataType>(windowInfoOption.windowInfoTypeOption),
1240 windowInfoOption.displayId, windowInfoOption.windowId);
1241 WMError ret = SingletonContainer::Get<WindowAdapterLite>().ListWindowInfo(windowInfoOption, infos);
1242 if (ret != WMError::WM_OK) {
1243 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "failed");
1244 }
1245 return ret;
1246 }
1247
SendPointerEventForHover(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)1248 WMError WindowManagerLite::SendPointerEventForHover(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
1249 {
1250 if (pointerEvent == nullptr) {
1251 return WMError::WM_ERROR_NULLPTR;
1252 }
1253 bool isHoverDown = pointerEvent->GetPointerAction() == MMI::PointerEvent::POINTER_ACTION_HOVER_ENTER &&
1254 pointerEvent->GetSourceType() == MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN;
1255 if (!isHoverDown) {
1256 TLOGE(WmsLogTag::WMS_EVENT, "pointer event is not hover down");
1257 return WMError::WM_ERROR_INVALID_PARAM;
1258 }
1259 WMError ret = SingletonContainer::Get<WindowAdapterLite>().SendPointerEventForHover(pointerEvent);
1260 if (ret != WMError::WM_OK) {
1261 TLOGE(WmsLogTag::WMS_EVENT, "send failed");
1262 }
1263 return ret;
1264 }
1265 } // namespace Rosen
1266 } // namespace OHOS
1267