/* * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "window_manager.h" #include #include #include #include "input_manager.h" #include "marshalling_helper.h" #include "window_adapter.h" #include "window_manager_agent.h" #include "window_manager_hilog.h" #include "window_display_change_adapter.h" #include "wm_common.h" #include "ws_common.h" namespace OHOS { namespace Rosen { namespace { constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowManager"}; struct WindowChecker : public MMI::IWindowChecker { public: WindowChecker() = default; ~WindowChecker() = default; int32_t CheckWindowId(int32_t windowId) const override; }; } WM_IMPLEMENT_SINGLE_INSTANCE(WindowManager) class WindowManager::Impl { public: void NotifyWMSConnected(int32_t userId, int32_t screenId); void NotifyWMSDisconnected(int32_t userId, int32_t screenId); void NotifyFocused(uint32_t windowId, const sptr& abilityToken, WindowType windowType, DisplayId displayId); void NotifyUnfocused(uint32_t windowId, const sptr& abilityToken, WindowType windowType, DisplayId displayId); void NotifyFocused(const sptr& focusChangeInfo); void NotifyWindowModeChange(WindowModeType type); void NotifyUnfocused(const sptr& focusChangeInfo); void NotifySystemBarChanged(DisplayId displayId, const SystemBarRegionTints& tints); void NotifyAccessibilityWindowInfo(const std::vector>& infos, WindowUpdateType type); void NotifyWindowVisibilityInfoChanged(const std::vector>& windowVisibilityInfos); void NotifyWindowVisibilityStateChanged(const std::vector>& windowVisibilityInfos); void PackWindowChangeInfo(const std::unordered_set& interestInfo, const std::vector>& windowVisibilityInfos, std::vector>& windowChangeInfos); void NotifyWindowDrawingContentInfoChanged(const std::vector>& windowDrawingContentInfos); void UpdateCameraFloatWindowStatus(uint32_t accessTokenId, bool isShowing); void NotifyWaterMarkFlagChangedResult(bool showWaterMark); void NotifyVisibleWindowNumChanged(const std::vector& visibleWindowNumInfo); void NotifyGestureNavigationEnabledResult(bool enable); void NotifyDisplayInfoChanged(const sptr& token, DisplayId displayId, float density, DisplayOrientation orientation); void NotifyDisplayIdChange( const std::vector>& windowInfoList); void NotifyWindowModeChangeForPropertyChange( const std::vector>& windowInfoList); void NotifyFloatingScaleChange( const std::vector>& windowInfoList); bool IsNeedToSkipForInterestWindowIds(sptr listener, const std::vector>& windowInfoList); void NotifyWindowStyleChange(WindowStyleType type); void NotifyWindowSystemBarPropertyChange(WindowType type, const SystemBarProperty& systemBarProperty); void NotifyWindowPidVisibilityChanged(const sptr& info); void NotifyWindowRectChange( const std::vector>& windowInfoList); void NotifyWMSWindowDestroyed(const WindowLifeCycleInfo& lifeCycleInfo, void* jsWindowNapiValue); static inline SingletonDelegator delegator_; std::shared_mutex listenerMutex_; sptr wmsConnectionChangedListener_; std::vector> focusChangedListeners_; sptr focusChangedListenerAgent_; std::vector> windowModeListeners_; sptr windowModeListenerAgent_; std::vector> systemBarChangedListeners_; sptr systemBarChangedListenerAgent_; std::vector> windowUpdateListeners_; sptr windowUpdateListenerAgent_; std::vector> windowVisibilityListeners_; sptr windowVisibilityListenerAgent_; std::vector> windowVisibilityStateListeners_; sptr windowVisibilityStateListenerAgent_; std::vector> windowDrawingContentListeners_; sptr windowDrawingContentListenerAgent_; std::vector> cameraFloatWindowChangedListeners_; sptr cameraFloatWindowChangedListenerAgent_; std::vector> waterMarkFlagChangeListeners_; sptr waterMarkFlagChangeAgent_; std::vector> gestureNavigationEnabledListeners_; sptr gestureNavigationEnabledAgent_; std::vector> visibleWindowNumChangedListeners_; sptr visibleWindowNumChangedListenerAgent_; std::vector> windowStyleListeners_; sptr windowStyleListenerAgent_; std::map, std::vector>> displayInfoChangedListeners_; std::vector> windowPidVisibilityListeners_; sptr windowPidVisibilityListenerAgent_; std::shared_mutex visibilityListenerAgentListenerMutex_; sptr WindowDisplayIdChangeListenerAgent_; std::vector> windowDisplayIdChangeListeners_; sptr windowPropertyChangeAgent_; std::vector> windowRectChangeListeners_; std::vector> windowModeChangeListeners_; std::vector> floatingScaleChangeListeners_; sptr windowSystemBarPropertyChangeAgent_; std::vector> windowSystemBarPropertyChangedListeners_; sptr windowLifeCycleListener_; }; void WindowManager::Impl::NotifyWMSConnected(int32_t userId, int32_t screenId) { TLOGD(WmsLogTag::WMS_MULTI_USER, "WMS connected [userId:%{public}d; screenId:%{public}d]", userId, screenId); sptr wmsConnectionChangedListener; { std::shared_lock lock(listenerMutex_); wmsConnectionChangedListener = wmsConnectionChangedListener_; } if (wmsConnectionChangedListener != nullptr) { wmsConnectionChangedListener->OnConnected(userId, screenId); } } void WindowManager::Impl::NotifyWMSDisconnected(int32_t userId, int32_t screenId) { TLOGI(WmsLogTag::WMS_MULTI_USER, "WMS disconnected [userId:%{public}d; screenId:%{public}d]", userId, screenId); sptr wmsConnectionChangedListener; { std::shared_lock lock(listenerMutex_); wmsConnectionChangedListener = wmsConnectionChangedListener_; } if (wmsConnectionChangedListener != nullptr) { wmsConnectionChangedListener->OnDisconnected(userId, screenId); } } void WindowManager::Impl::NotifyFocused(const sptr& focusChangeInfo) { TLOGD(WmsLogTag::WMS_FOCUS, "NotifyFocused [%{public}u; %{public}" PRIu64"; %{public}d; %{public}d; %{public}u]", focusChangeInfo->windowId_, focusChangeInfo->displayId_, focusChangeInfo->pid_, focusChangeInfo->uid_, static_cast(focusChangeInfo->windowType_)); std::vector> focusChangeListeners; { std::shared_lock lock(listenerMutex_); focusChangeListeners = focusChangedListeners_; } for (auto& listener : focusChangeListeners) { listener->OnFocused(focusChangeInfo); } } void WindowManager::Impl::NotifyUnfocused(const sptr& focusChangeInfo) { TLOGD(WmsLogTag::WMS_FOCUS, "NotifyUnfocused [%{public}u; %{public}" PRIu64"; %{public}d; %{public}d; %{public}u]", focusChangeInfo->windowId_, focusChangeInfo->displayId_, focusChangeInfo->pid_, focusChangeInfo->uid_, static_cast(focusChangeInfo->windowType_)); std::vector> focusChangeListeners; { std::shared_lock lock(listenerMutex_); focusChangeListeners = focusChangedListeners_; } for (auto& listener : focusChangeListeners) { listener->OnUnfocused(focusChangeInfo); } } void WindowManager::Impl::NotifyWindowModeChange(WindowModeType type) { TLOGI(WmsLogTag::WMS_MAIN, "WindowManager::Impl UpdateWindowModeTypeInfo type: %{public}d", static_cast(type)); std::vector> windowModeListeners; { std::shared_lock lock(listenerMutex_); windowModeListeners = windowModeListeners_; } for (auto &listener : windowModeListeners) { listener->OnWindowModeUpdate(type); } } void WindowManager::Impl::NotifySystemBarChanged(DisplayId displayId, const SystemBarRegionTints& tints) { for (auto tint : tints) { WLOGFD("type:%{public}d, enable:%{public}d," \ "backgroundColor:%{public}x, contentColor:%{public}x " \ "region:[%{public}d, %{public}d, %{public}d, %{public}d]", tint.type_, tint.prop_.enable_, tint.prop_.backgroundColor_, tint.prop_.contentColor_, tint.region_.posX_, tint.region_.posY_, tint.region_.width_, tint.region_.height_); } std::vector> systemBarChangeListeners; { std::shared_lock lock(listenerMutex_); systemBarChangeListeners = systemBarChangedListeners_; } for (auto& listener : systemBarChangeListeners) { listener->OnSystemBarPropertyChange(displayId, tints); } } void WindowManager::Impl::NotifyAccessibilityWindowInfo(const std::vector>& infos, WindowUpdateType type) { if (infos.empty()) { WLOGFE("infos is empty"); return; } for (auto& info : infos) { if (info == nullptr) { TLOGD(WmsLogTag::WMS_MAIN, "info is nullptr"); continue; } TLOGD(WmsLogTag::WMS_MAIN, "NotifyAccessibilityWindowInfo: wid[%{public}u], innerWid_[%{public}u]," \ "uiNodeId_[%{public}u], rect[%{public}d %{public}d %{public}d %{public}d]," \ "isFocused[%{public}d], isDecorEnable[%{public}d], displayId[%{public}" PRIu64"], layer[%{public}u]," \ "mode[%{public}u], type[%{public}u, updateType[%{public}d], bundle[%{public}s]", info->wid_, info->innerWid_, info->uiNodeId_, info->windowRect_.width_, info->windowRect_.height_, info->windowRect_.posX_, info->windowRect_.posY_, info->focused_, info->isDecorEnable_, info->displayId_, info->layer_, info->mode_, info->type_, type, info->bundleName_.c_str()); for (const auto& rect : info->touchHotAreas_) { TLOGD(WmsLogTag::WMS_MAIN, "window touch hot areas rect[x=%{public}d,y=%{public}d," \ "w=%{public}d,h=%{public}d]", rect.posX_, rect.posY_, rect.width_, rect.height_); } } std::vector> windowUpdateListeners; { std::shared_lock lock(listenerMutex_); windowUpdateListeners = windowUpdateListeners_; } for (auto& listener : windowUpdateListeners) { listener->OnWindowUpdate(infos, type); } } void WindowManager::Impl::NotifyWindowVisibilityInfoChanged( const std::vector>& windowVisibilityInfos) { std::vector> visibilityChangeListeners; { std::shared_lock lock(listenerMutex_); visibilityChangeListeners = windowVisibilityListeners_; } for (auto& listener : visibilityChangeListeners) { WLOGD("Notify WindowVisibilityInfo to caller"); listener->OnWindowVisibilityChanged(windowVisibilityInfos); } } void WindowManager::Impl::NotifyWindowVisibilityStateChanged( const std::vector>& windowVisibilityInfos) { TLOGD(WmsLogTag::WMS_ATTRIBUTE, "in"); std::vector> windowVisibilityStateListeners; { std::shared_lock lock(listenerMutex_); windowVisibilityStateListeners = windowVisibilityStateListeners_; } for (auto& listener : windowVisibilityStateListeners) { if (listener == nullptr) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "listener is null"); continue; } std::vector> windowChangeInfos; PackWindowChangeInfo(listener->GetInterestInfo(), windowVisibilityInfos, windowChangeInfos); TLOGD(WmsLogTag::WMS_ATTRIBUTE, "Notify WindowVisibilityState to caller, info size: %{public}zu", windowChangeInfos.size()); listener->OnWindowInfoChanged(windowChangeInfos); } } void WindowManager::Impl::PackWindowChangeInfo(const std::unordered_set& interestInfo, const std::vector>& windowVisibilityInfos, std::vector>& windowChangeInfos) { for (const auto& info : windowVisibilityInfos) { std::unordered_map windowChangeInfo; if (interestInfo.find(WindowInfoKey::WINDOW_ID) != interestInfo.end()) { windowChangeInfo.emplace(WindowInfoKey::WINDOW_ID, info->windowId_); } if (interestInfo.find(WindowInfoKey::BUNDLE_NAME) != interestInfo.end()) { windowChangeInfo.emplace(WindowInfoKey::BUNDLE_NAME, info->bundleName_); } if (interestInfo.find(WindowInfoKey::ABILITY_NAME) != interestInfo.end()) { windowChangeInfo.emplace(WindowInfoKey::ABILITY_NAME, info->abilityName_); } if (interestInfo.find(WindowInfoKey::APP_INDEX) != interestInfo.end()) { windowChangeInfo.emplace(WindowInfoKey::APP_INDEX, info->appIndex_); } if (interestInfo.find(WindowInfoKey::VISIBILITY_STATE) != interestInfo.end()) { windowChangeInfo.emplace(WindowInfoKey::VISIBILITY_STATE, info->visibilityState_); } windowChangeInfos.emplace_back(windowChangeInfo); } } void WindowManager::Impl::NotifyWindowDrawingContentInfoChanged( const std::vector>& windowDrawingContentInfos) { std::vector> windowDrawingContentChangeListeners; { std::shared_lock lock(listenerMutex_); windowDrawingContentChangeListeners = windowDrawingContentListeners_; } for (auto& listener : windowDrawingContentChangeListeners) { WLOGFD("Notify windowDrawingContentInfo to caller"); listener->OnWindowDrawingContentChanged(windowDrawingContentInfos); } } void WindowManager::Impl::UpdateCameraFloatWindowStatus(uint32_t accessTokenId, bool isShowing) { TLOGD(WmsLogTag::DEFAULT, "Camera float window, accessTokenId=%{private}u, isShowing=%{public}u", accessTokenId, isShowing); std::vector> cameraFloatWindowChangeListeners; { std::shared_lock lock(listenerMutex_); cameraFloatWindowChangeListeners = cameraFloatWindowChangedListeners_; } for (auto& listener : cameraFloatWindowChangeListeners) { listener->OnCameraFloatWindowChange(accessTokenId, isShowing); } } void WindowManager::Impl::NotifyWaterMarkFlagChangedResult(bool showWaterMark) { TLOGI(WmsLogTag::DEFAULT, "%{public}d", showWaterMark); std::vector> waterMarkFlagChangeListeners; { std::shared_lock lock(listenerMutex_); waterMarkFlagChangeListeners = waterMarkFlagChangeListeners_; } for (auto& listener : waterMarkFlagChangeListeners) { listener->OnWaterMarkFlagUpdate(showWaterMark); } } void WindowManager::Impl::NotifyGestureNavigationEnabledResult(bool enable) { WLOGFI("Notify gesture navigation enable result, enable=%{public}d", enable); std::vector> gestureNavigationEnabledListeners; { std::shared_lock lock(listenerMutex_); gestureNavigationEnabledListeners = gestureNavigationEnabledListeners_; } for (auto& listener : gestureNavigationEnabledListeners) { listener->OnGestureNavigationEnabledUpdate(enable); } } void WindowManager::Impl::NotifyVisibleWindowNumChanged( const std::vector& visibleWindowNumInfo) { std::vector> visibleWindowNumChangedListeners; { std::shared_lock lock(listenerMutex_); visibleWindowNumChangedListeners = visibleWindowNumChangedListeners_; } for (auto& listener : visibleWindowNumChangedListeners) { if (listener == nullptr) { continue; } listener->OnVisibleWindowNumChange(visibleWindowNumInfo); } } void WindowManager::Impl::NotifyDisplayInfoChanged(const sptr& token, DisplayId displayId, float density, DisplayOrientation orientation) { auto iter = displayInfoChangedListeners_.end(); std::vector> displayInfoChangedListeners; { std::unique_lock lock(listenerMutex_); iter = displayInfoChangedListeners_.find(token); if (iter == displayInfoChangedListeners_.end()) { TLOGI(WmsLogTag::DMS, "can not find token in listener list"); return; } displayInfoChangedListeners = iter->second; } for (auto& listener : displayInfoChangedListeners) { listener->OnDisplayInfoChange(token, displayId, density, orientation); } } void WindowManager::Impl::NotifyWindowModeChangeForPropertyChange( const std::vector>& windowInfoList) { std::vector> windowModeChangeListeners; { std::unique_lock lock(listenerMutex_); windowModeChangeListeners = windowModeChangeListeners_; } for (auto &listener : windowModeChangeListeners) { if (listener != nullptr && !IsNeedToSkipForInterestWindowIds(listener, windowInfoList)) { listener->OnWindowInfoChanged(windowInfoList); } } } void WindowManager::Impl::NotifyFloatingScaleChange( const std::vector>& windowInfoList) { std::vector> floatingScaleChangeListeners; { std::unique_lock lock(listenerMutex_); floatingScaleChangeListeners = floatingScaleChangeListeners_; } for (auto &listener : floatingScaleChangeListeners) { if (listener != nullptr && !IsNeedToSkipForInterestWindowIds(listener, windowInfoList)) { listener->OnWindowInfoChanged(windowInfoList); } } } void WindowManager::Impl::NotifyDisplayIdChange( const std::vector>& windowInfoList) { std::vector> windowDisplayIdChangeListeners; { std::unique_lock lock(listenerMutex_); windowDisplayIdChangeListeners = windowDisplayIdChangeListeners_; } for (auto &listener : windowDisplayIdChangeListeners) { if (listener != nullptr && !IsNeedToSkipForInterestWindowIds(listener, windowInfoList)) { listener->OnWindowInfoChanged(windowInfoList); } } } bool WindowManager::Impl::IsNeedToSkipForInterestWindowIds(sptr listener, const std::vector>& windowInfoList) { auto interestWindowIds = listener->GetInterestWindowIds(); if (interestWindowIds.empty()) { return false; } for (const auto& item : windowInfoList) { auto windowInfo = item; if (windowInfo.find(WindowInfoKey::WINDOW_ID) != windowInfo.end() && interestWindowIds.find(std::get(windowInfo[WindowInfoKey::WINDOW_ID])) != interestWindowIds.end()) { return false; } } return true; } void WindowManager::Impl::NotifyWindowStyleChange(WindowStyleType type) { TLOGI(WmsLogTag::WMS_MAIN, "WindowStyleChange type: %{public}d", static_cast(type)); std::vector> windowStyleListeners; { std::unique_lock lock(listenerMutex_); windowStyleListeners = windowStyleListeners_; } for (auto &listener : windowStyleListeners) { TLOGI(WmsLogTag::WMS_MAIN, "WindowStyleChange type: %{public}d", static_cast(type)); listener->OnWindowStyleUpdate(type); } } void WindowManager::Impl::NotifyWindowPidVisibilityChanged( const sptr& info) { std::vector> windowPidVisibilityListeners; { std::unique_lock lock(listenerMutex_); windowPidVisibilityListeners = windowPidVisibilityListeners_; } for (auto &listener : windowPidVisibilityListeners) { if (listener != nullptr) { listener->NotifyWindowPidVisibilityChanged(info); } } } void WindowManager::Impl::NotifyWindowRectChange( const std::vector>& windowInfoList) { std::vector> windowRectChangeListeners; { std::unique_lock lock(listenerMutex_); windowRectChangeListeners = windowRectChangeListeners_; } for (auto &listener : windowRectChangeListeners) { if (listener != nullptr) { listener->OnWindowInfoChanged(windowInfoList); } } } void WindowManager::Impl::NotifyWMSWindowDestroyed(const WindowLifeCycleInfo& lifeCycleInfo, void* jsWindowNapiValue) { TLOGD(WmsLogTag::WMS_LIFE, "notify window destroyed"); sptr wmsWindowDestroyedListener; { std::shared_lock lock(listenerMutex_); wmsWindowDestroyedListener = windowLifeCycleListener_; } if (wmsWindowDestroyedListener == nullptr) { TLOGE(WmsLogTag::WMS_LIFE, "window destroyed listener is nullptr"); return; } wmsWindowDestroyedListener->OnWindowDestroyed(lifeCycleInfo, jsWindowNapiValue); } WindowManager::WindowManager() : pImpl_(std::make_unique()) { } int32_t WindowChecker::CheckWindowId(int32_t windowId) const { int32_t pid = INVALID_PID; WMError ret = SingletonContainer::Get().CheckWindowId(windowId, pid); if (ret != WMError::WM_OK) { WLOGFE("Window(%{public}d) do not allow styles to be set", windowId); } return pid; } WindowManager::~WindowManager() { std::lock_guard lock(mutex_); destroyed_ = true; } WMError WindowManager::RegisterWMSConnectionChangedListener(const sptr& listener) { int32_t clientUserId = GetUserIdByUid(getuid()); // only system applications or services with a userId of 0 are allowed to communicate // with multiple WMS-Servers and are permitted to listen for WMS connection status. if (clientUserId != SYSTEM_USERID) { TLOGW(WmsLogTag::WMS_MULTI_USER, "Permission denied"); return WMError::WM_ERROR_INVALID_PERMISSION; } if (listener == nullptr) { TLOGE(WmsLogTag::WMS_MULTI_USER, "Register failed: listener is null"); return WMError::WM_ERROR_NULLPTR; } TLOGI(WmsLogTag::WMS_MULTI_USER, "Start registration"); { std::unique_lock lock(pImpl_->listenerMutex_); if (pImpl_->wmsConnectionChangedListener_) { TLOGI(WmsLogTag::WMS_MULTI_USER, "Listener already registered, skipping"); return WMError::WM_OK; } pImpl_->wmsConnectionChangedListener_ = listener; } auto ret = WindowAdapter::GetInstance().RegisterWMSConnectionChangedListener( [this](int32_t userId, int32_t screenId, bool isConnected) { this->OnWMSConnectionChanged(userId, screenId, isConnected); }); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_MULTI_USER, "Register failed: error = %{public}d", static_cast(ret)); std::unique_lock lock(pImpl_->listenerMutex_); pImpl_->wmsConnectionChangedListener_ = nullptr; } return ret; } WMError WindowManager::UnregisterWMSConnectionChangedListener() { TLOGI(WmsLogTag::WMS_MULTI_USER, "Unregister enter"); std::unique_lock lock(pImpl_->listenerMutex_); pImpl_->wmsConnectionChangedListener_ = nullptr; WindowAdapter::GetInstance().UnregisterWMSConnectionChangedListener(); return WMError::WM_OK; } WMError WindowManager::RegisterFocusChangedListener(const sptr& listener) { if (listener == nullptr) { WLOGFE("listener could not be null"); return WMError::WM_ERROR_NULLPTR; } sptr focusChangedListenerAgentBack = nullptr; WMError ret = WMError::WM_OK; { std::unique_lock lock(pImpl_->listenerMutex_); if (pImpl_->focusChangedListenerAgent_ == nullptr) { pImpl_->focusChangedListenerAgent_ = new WindowManagerAgent(); } focusChangedListenerAgentBack = pImpl_->focusChangedListenerAgent_; } ret = WindowAdapter::GetInstance().RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_FOCUS, focusChangedListenerAgentBack); { std::unique_lock lock(pImpl_->listenerMutex_); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_FOCUS, "RegisterWindowManagerAgent failed!"); pImpl_->focusChangedListenerAgent_ = nullptr; } else { auto iter = std::find( pImpl_->focusChangedListeners_.begin(), pImpl_->focusChangedListeners_.end(), listener); if (iter != pImpl_->focusChangedListeners_.end()) { TLOGW(WmsLogTag::WMS_FOCUS, "Listener is already registered."); return WMError::WM_OK; } pImpl_->focusChangedListeners_.push_back(listener); } } return ret; } WMError WindowManager::UnregisterFocusChangedListener(const sptr& listener) { if (listener == nullptr) { WLOGFE("listener could not be null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); auto iter = std::find(pImpl_->focusChangedListeners_.begin(), pImpl_->focusChangedListeners_.end(), listener); if (iter == pImpl_->focusChangedListeners_.end()) { WLOGFE("could not find this listener"); return WMError::WM_OK; } pImpl_->focusChangedListeners_.erase(iter); WMError ret = WMError::WM_OK; if (pImpl_->focusChangedListeners_.empty() && pImpl_->focusChangedListenerAgent_ != nullptr) { ret = SingletonContainer::Get().UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_FOCUS, pImpl_->focusChangedListenerAgent_); if (ret == WMError::WM_OK) { pImpl_->focusChangedListenerAgent_ = nullptr; } } return ret; } WMError WindowManager::RegisterWindowModeChangedListener(const sptr& listener) { if (listener == nullptr) { TLOGE(WmsLogTag::WMS_MAIN, "listener could not be null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); WMError ret = WMError::WM_OK; if (pImpl_->windowModeListenerAgent_ == nullptr) { pImpl_->windowModeListenerAgent_ = new WindowManagerAgent(); } ret = SingletonContainer::Get().RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_MODE, pImpl_->windowModeListenerAgent_); if (ret != WMError::WM_OK) { TLOGW(WmsLogTag::WMS_MAIN, "RegisterWindowManagerAgent failed!"); pImpl_->windowModeListenerAgent_ = nullptr; return ret; } auto iter = std::find(pImpl_->windowModeListeners_.begin(), pImpl_->windowModeListeners_.end(), listener); if (iter != pImpl_->windowModeListeners_.end()) { TLOGW(WmsLogTag::WMS_MAIN, "Listener is already registered."); return WMError::WM_OK; } pImpl_->windowModeListeners_.push_back(listener); return ret; } WMError WindowManager::UnregisterWindowModeChangedListener(const sptr& listener) { if (listener == nullptr) { TLOGE(WmsLogTag::WMS_MAIN, "listener could not be null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); auto iter = std::find(pImpl_->windowModeListeners_.begin(), pImpl_->windowModeListeners_.end(), listener); if (iter == pImpl_->windowModeListeners_.end()) { TLOGE(WmsLogTag::WMS_MAIN, "could not find this listener"); return WMError::WM_OK; } pImpl_->windowModeListeners_.erase(iter); WMError ret = WMError::WM_OK; if (pImpl_->windowModeListeners_.empty() && pImpl_->windowModeListenerAgent_ != nullptr) { ret = SingletonContainer::Get().UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_MODE, pImpl_->windowModeListenerAgent_); if (ret == WMError::WM_OK) { pImpl_->windowModeListenerAgent_ = nullptr; } } return ret; } WMError WindowManager::RegisterSystemBarChangedListener(const sptr& listener) { if (listener == nullptr) { WLOGFE("listener could not be null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); WMError ret = WMError::WM_OK; if (pImpl_->systemBarChangedListenerAgent_ == nullptr) { pImpl_->systemBarChangedListenerAgent_ = new WindowManagerAgent(); } ret = SingletonContainer::Get().RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_SYSTEM_BAR, pImpl_->systemBarChangedListenerAgent_); if (ret != WMError::WM_OK) { WLOGFW("RegisterWindowManagerAgent failed!"); pImpl_->systemBarChangedListenerAgent_ = nullptr; } else { auto iter = std::find(pImpl_->systemBarChangedListeners_.begin(), pImpl_->systemBarChangedListeners_.end(), listener); if (iter != pImpl_->systemBarChangedListeners_.end()) { WLOGFW("Listener is already registered."); return WMError::WM_OK; } pImpl_->systemBarChangedListeners_.push_back(listener); } return ret; } WMError WindowManager::UnregisterSystemBarChangedListener(const sptr& listener) { if (listener == nullptr) { WLOGFE("listener could not be null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); auto iter = std::find(pImpl_->systemBarChangedListeners_.begin(), pImpl_->systemBarChangedListeners_.end(), listener); if (iter == pImpl_->systemBarChangedListeners_.end()) { WLOGFE("could not find this listener"); return WMError::WM_OK; } pImpl_->systemBarChangedListeners_.erase(iter); WMError ret = WMError::WM_OK; if (pImpl_->systemBarChangedListeners_.empty() && pImpl_->systemBarChangedListenerAgent_ != nullptr) { ret = SingletonContainer::Get().UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_SYSTEM_BAR, pImpl_->systemBarChangedListenerAgent_); if (ret == WMError::WM_OK) { pImpl_->systemBarChangedListenerAgent_ = nullptr; } } return ret; } WMError WindowManager::MinimizeAllAppWindows(DisplayId displayId) { WLOGFD("displayId %{public}" PRIu64"", displayId); return SingletonContainer::Get().MinimizeAllAppWindows(displayId); } WMError WindowManager::ToggleShownStateForAllAppWindows() { WLOGFD("ToggleShownStateForAllAppWindows"); return SingletonContainer::Get().ToggleShownStateForAllAppWindows(); } WMError WindowManager::SetWindowLayoutMode(WindowLayoutMode mode) { WLOGFD("set window layout mode: %{public}u", mode); WMError ret = SingletonContainer::Get().SetWindowLayoutMode(mode); if (ret != WMError::WM_OK) { WLOGFE("set layout mode failed"); } return ret; } WMError WindowManager::RegisterWindowUpdateListener(const sptr& listener) { if (listener == nullptr) { WLOGFE("listener could not be null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); WMError ret = WMError::WM_OK; if (pImpl_->windowUpdateListenerAgent_ == nullptr) { pImpl_->windowUpdateListenerAgent_ = new WindowManagerAgent(); } ret = SingletonContainer::Get().RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_UPDATE, pImpl_->windowUpdateListenerAgent_); if (ret != WMError::WM_OK) { WLOGFW("RegisterWindowManagerAgent failed!"); pImpl_->windowUpdateListenerAgent_ = nullptr; } else { auto iter = std::find(pImpl_->windowUpdateListeners_.begin(), pImpl_->windowUpdateListeners_.end(), listener); if (iter != pImpl_->windowUpdateListeners_.end()) { WLOGI("Listener is already registered."); return WMError::WM_OK; } pImpl_->windowUpdateListeners_.emplace_back(listener); } return ret; } WMError WindowManager::UnregisterWindowUpdateListener(const sptr& listener) { if (listener == nullptr) { WLOGFE("listener could not be null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); auto iter = std::find(pImpl_->windowUpdateListeners_.begin(), pImpl_->windowUpdateListeners_.end(), listener); if (iter == pImpl_->windowUpdateListeners_.end()) { WLOGFE("could not find this listener"); return WMError::WM_OK; } pImpl_->windowUpdateListeners_.erase(iter); WMError ret = WMError::WM_OK; if (pImpl_->windowUpdateListeners_.empty() && pImpl_->windowUpdateListenerAgent_ != nullptr) { ret = SingletonContainer::Get().UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_UPDATE, pImpl_->windowUpdateListenerAgent_); if (ret == WMError::WM_OK) { pImpl_->windowUpdateListenerAgent_ = nullptr; } } return ret; } WMError WindowManager::RegisterVisibilityChangedListener(const sptr& listener) { if (listener == nullptr) { WLOGFE("listener could not be null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); WMError ret = WMError::WM_OK; if (pImpl_->windowVisibilityListenerAgent_ == nullptr) { pImpl_->windowVisibilityListenerAgent_ = new WindowManagerAgent(); } ret = SingletonContainer::Get().RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_VISIBILITY, pImpl_->windowVisibilityListenerAgent_); if (ret != WMError::WM_OK) { WLOGFW("RegisterWindowManagerAgent failed!"); pImpl_->windowVisibilityListenerAgent_ = nullptr; } else { auto iter = std::find(pImpl_->windowVisibilityListeners_.begin(), pImpl_->windowVisibilityListeners_.end(), listener); if (iter != pImpl_->windowVisibilityListeners_.end()) { WLOGFW("Listener is already registered."); return WMError::WM_OK; } pImpl_->windowVisibilityListeners_.emplace_back(listener); } return ret; } WMError WindowManager::UnregisterVisibilityChangedListener(const sptr& listener) { if (listener == nullptr) { WLOGFE("listener could not be null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); pImpl_->windowVisibilityListeners_.erase(std::remove_if(pImpl_->windowVisibilityListeners_.begin(), pImpl_->windowVisibilityListeners_.end(), [listener](sptr registeredListener) { return registeredListener == listener; }), pImpl_->windowVisibilityListeners_.end()); WMError ret = WMError::WM_OK; if (pImpl_->windowVisibilityListeners_.empty() && pImpl_->windowVisibilityListenerAgent_ != nullptr) { ret = SingletonContainer::Get().UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_VISIBILITY, pImpl_->windowVisibilityListenerAgent_); if (ret == WMError::WM_OK) { pImpl_->windowVisibilityListenerAgent_ = nullptr; } } return ret; } WMError WindowManager::RegisterDisplayIdChangedListener(const sptr& listener) { if (listener == nullptr) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "listener is null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); WMError ret = WMError::WM_OK; if (pImpl_->windowPropertyChangeAgent_ == nullptr) { pImpl_->windowPropertyChangeAgent_ = new WindowManagerAgent(); } uint32_t interestInfo = 0; for (auto windowInfoKey : listener->GetInterestInfo()) { if (interestInfoMap_.find(windowInfoKey) == interestInfoMap_.end()) { interestInfoMap_[windowInfoKey] = 1; } else { interestInfoMap_[windowInfoKey]++; } interestInfo |= static_cast(windowInfoKey); } ret = SingletonContainer::Get().RegisterWindowPropertyChangeAgent( WindowInfoKey::DISPLAY_ID, interestInfo, pImpl_->windowPropertyChangeAgent_); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "RegisterWindowPropertyChangeAgent failed!"); pImpl_->windowPropertyChangeAgent_ = nullptr; } else { auto iter = std::find(pImpl_->windowDisplayIdChangeListeners_.begin(), pImpl_->windowDisplayIdChangeListeners_.end(), listener); if (iter != pImpl_->windowDisplayIdChangeListeners_.end()) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "Listener is already registered."); return WMError::WM_OK; } pImpl_->windowDisplayIdChangeListeners_.emplace_back(listener); } return ret; } WMError WindowManager::UnregisterDisplayIdChangedListener(const sptr& listener) { if (listener == nullptr) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "listener is null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); pImpl_->windowDisplayIdChangeListeners_.erase(std::remove_if(pImpl_->windowDisplayIdChangeListeners_.begin(), pImpl_->windowDisplayIdChangeListeners_.end(), [listener](sptr registeredListener) { return registeredListener == listener; }), pImpl_->windowDisplayIdChangeListeners_.end()); uint32_t interestInfo = 0; for (auto windowInfoKey : listener->GetInterestInfo()) { if (interestInfoMap_.find(windowInfoKey) == interestInfoMap_.end()) { continue; } else if (interestInfoMap_[windowInfoKey] == 1) { interestInfoMap_.erase(windowInfoKey); interestInfo |= static_cast(windowInfoKey); } else { interestInfoMap_[windowInfoKey]--; } } WMError ret = WMError::WM_OK; if (pImpl_->windowDisplayIdChangeListeners_.empty() && pImpl_->windowPropertyChangeAgent_ != nullptr) { ret = SingletonContainer::Get().UnregisterWindowPropertyChangeAgent(WindowInfoKey::DISPLAY_ID, interestInfo, pImpl_->windowPropertyChangeAgent_); if (ret == WMError::WM_OK) { pImpl_->windowPropertyChangeAgent_ = nullptr; } } return ret; } WMError WindowManager::RegisterRectChangedListener(const sptr& listener) { if (listener == nullptr) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "listener is null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); WMError ret = WMError::WM_OK; if (pImpl_->windowPropertyChangeAgent_ == nullptr) { pImpl_->windowPropertyChangeAgent_ = new WindowManagerAgent(); } uint32_t interestInfo = 0; for (auto windowInfoKey : listener->GetInterestInfo()) { if (interestInfoMap_.find(windowInfoKey) == interestInfoMap_.end()) { interestInfoMap_[windowInfoKey] = 1; } else { interestInfoMap_[windowInfoKey]++; } interestInfo |= static_cast(windowInfoKey); } ret = SingletonContainer::Get().RegisterWindowPropertyChangeAgent( WindowInfoKey::WINDOW_RECT, interestInfo, pImpl_->windowPropertyChangeAgent_); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "RegisterWindowPropertyChangeAgent failed!"); pImpl_->windowPropertyChangeAgent_ = nullptr; } else { auto iter = std::find(pImpl_->windowRectChangeListeners_.begin(), pImpl_->windowRectChangeListeners_.end(), listener); if (iter != pImpl_->windowRectChangeListeners_.end()) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "Listener is already registered."); return WMError::WM_OK; } pImpl_->windowRectChangeListeners_.emplace_back(listener); } return ret; } WMError WindowManager::UnregisterRectChangedListener(const sptr& listener) { if (listener == nullptr) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "listener is null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); pImpl_->windowRectChangeListeners_.erase(std::remove_if(pImpl_->windowRectChangeListeners_.begin(), pImpl_->windowRectChangeListeners_.end(), [listener](sptr registeredListener) { return registeredListener == listener; }), pImpl_->windowRectChangeListeners_.end()); uint32_t interestInfo = 0; for (auto windowInfoKey : listener->GetInterestInfo()) { if (interestInfoMap_.find(windowInfoKey) == interestInfoMap_.end()) { continue; } else if (interestInfoMap_[windowInfoKey] == 1) { interestInfoMap_.erase(windowInfoKey); interestInfo |= static_cast(windowInfoKey); } else { interestInfoMap_[windowInfoKey]--; } } WMError ret = WMError::WM_OK; if (pImpl_->windowRectChangeListeners_.empty() && pImpl_->windowPropertyChangeAgent_ != nullptr) { ret = SingletonContainer::Get().UnregisterWindowPropertyChangeAgent(WindowInfoKey::WINDOW_RECT, interestInfo, pImpl_->windowPropertyChangeAgent_); if (ret == WMError::WM_OK) { pImpl_->windowPropertyChangeAgent_ = nullptr; } } return ret; } WMError WindowManager::RegisterWindowModeChangedListenerForPropertyChange( const sptr& listener) { if (listener == nullptr) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "listener is null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); WMError ret = WMError::WM_OK; if (pImpl_->windowPropertyChangeAgent_ == nullptr) { pImpl_->windowPropertyChangeAgent_ = new WindowManagerAgent(); } uint32_t interestInfo = 0; for (auto windowInfoKey : listener->GetInterestInfo()) { if (interestInfoMap_.find(windowInfoKey) == interestInfoMap_.end()) { interestInfoMap_[windowInfoKey] = 1; } else { interestInfoMap_[windowInfoKey]++; } interestInfo |= static_cast(windowInfoKey); } ret = SingletonContainer::Get().RegisterWindowPropertyChangeAgent( WindowInfoKey::WINDOW_MODE, interestInfo, pImpl_->windowPropertyChangeAgent_); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "RegisterWindowPropertyChangeAgent failed!"); pImpl_->windowPropertyChangeAgent_ = nullptr; } else { auto iter = std::find(pImpl_->windowModeChangeListeners_.begin(), pImpl_->windowModeChangeListeners_.end(), listener); if (iter != pImpl_->windowModeChangeListeners_.end()) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "Listener is already registered."); return WMError::WM_OK; } pImpl_->windowModeChangeListeners_.emplace_back(listener); } return ret; } WMError WindowManager::UnregisterWindowModeChangedListenerForPropertyChange( const sptr& listener) { if (listener == nullptr) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "listener is null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); pImpl_->windowModeChangeListeners_.erase(std::remove_if(pImpl_->windowModeChangeListeners_.begin(), pImpl_->windowModeChangeListeners_.end(), [listener](sptr registeredListener) { return registeredListener == listener; }), pImpl_->windowModeChangeListeners_.end()); uint32_t interestInfo = 0; for (auto windowInfoKey : listener->GetInterestInfo()) { if (interestInfoMap_.find(windowInfoKey) == interestInfoMap_.end()) { continue; } else if (interestInfoMap_[windowInfoKey] == 1) { interestInfoMap_.erase(windowInfoKey); interestInfo |= static_cast(windowInfoKey); } else { interestInfoMap_[windowInfoKey]--; } } WMError ret = WMError::WM_OK; if (pImpl_->windowModeChangeListeners_.empty() && pImpl_->windowPropertyChangeAgent_ != nullptr) { ret = SingletonContainer::Get().UnregisterWindowPropertyChangeAgent(WindowInfoKey::WINDOW_MODE, interestInfo, pImpl_->windowPropertyChangeAgent_); if (ret == WMError::WM_OK) { pImpl_->windowPropertyChangeAgent_ = nullptr; } } return ret; } WMError WindowManager::RegisterFloatingScaleChangedListener(const sptr& listener) { if (listener == nullptr) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "listener is null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); WMError ret = WMError::WM_OK; if (pImpl_->windowPropertyChangeAgent_ == nullptr) { pImpl_->windowPropertyChangeAgent_ = new WindowManagerAgent(); } uint32_t interestInfo = 0; for (auto windowInfoKey : listener->GetInterestInfo()) { if (interestInfoMap_.find(windowInfoKey) == interestInfoMap_.end()) { interestInfoMap_[windowInfoKey] = 1; } else { interestInfoMap_[windowInfoKey]++; } interestInfo |= static_cast(windowInfoKey); } ret = SingletonContainer::Get().RegisterWindowPropertyChangeAgent( WindowInfoKey::FLOATING_SCALE, interestInfo, pImpl_->windowPropertyChangeAgent_); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "RegisterWindowPropertyChangeAgent failed!"); pImpl_->windowPropertyChangeAgent_ = nullptr; } else { auto iter = std::find(pImpl_->floatingScaleChangeListeners_.begin(), pImpl_->floatingScaleChangeListeners_.end(), listener); if (iter != pImpl_->floatingScaleChangeListeners_.end()) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "Listener is already registered."); return WMError::WM_OK; } pImpl_->floatingScaleChangeListeners_.emplace_back(listener); } return ret; } WMError WindowManager::UnregisterFloatingScaleChangedListener(const sptr& listener) { if (listener == nullptr) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "listener is null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); pImpl_->floatingScaleChangeListeners_.erase(std::remove_if(pImpl_->floatingScaleChangeListeners_.begin(), pImpl_->floatingScaleChangeListeners_.end(), [listener](sptr registeredListener) { return registeredListener == listener; }), pImpl_->floatingScaleChangeListeners_.end()); uint32_t interestInfo = 0; for (auto windowInfoKey : listener->GetInterestInfo()) { if (interestInfoMap_.find(windowInfoKey) == interestInfoMap_.end()) { continue; } else if (interestInfoMap_[windowInfoKey] == 1) { interestInfoMap_.erase(windowInfoKey); interestInfo |= static_cast(windowInfoKey); } else { interestInfoMap_[windowInfoKey]--; } } WMError ret = WMError::WM_OK; if (pImpl_->floatingScaleChangeListeners_.empty() && pImpl_->windowPropertyChangeAgent_ != nullptr) { ret = SingletonContainer::Get().UnregisterWindowPropertyChangeAgent( WindowInfoKey::FLOATING_SCALE, interestInfo, pImpl_->windowPropertyChangeAgent_); if (ret == WMError::WM_OK) { pImpl_->windowPropertyChangeAgent_ = nullptr; } } return ret; } WMError WindowManager::RegisterVisibilityStateChangedListener(const sptr& listener) { if (listener == nullptr) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "listener is null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); WMError ret = WMError::WM_OK; if (pImpl_->windowVisibilityStateListenerAgent_ == nullptr) { pImpl_->windowVisibilityStateListenerAgent_ = new WindowManagerAgent(); } ret = SingletonContainer::Get().RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_VISIBILITY, pImpl_->windowVisibilityStateListenerAgent_); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "RegisterWindowManagerAgent failed!"); pImpl_->windowVisibilityStateListenerAgent_ = nullptr; } else { auto iter = std::find(pImpl_->windowVisibilityStateListeners_.begin(), pImpl_->windowVisibilityStateListeners_.end(), listener); if (iter != pImpl_->windowVisibilityStateListeners_.end()) { TLOGW(WmsLogTag::WMS_ATTRIBUTE, "Listener is already registered."); return WMError::WM_OK; } pImpl_->windowVisibilityStateListeners_.emplace_back(listener); } return ret; } WMError WindowManager::UnregisterVisibilityStateChangedListener(const sptr& listener) { if (listener == nullptr) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "listener is null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); pImpl_->windowVisibilityStateListeners_.erase(std::remove_if(pImpl_->windowVisibilityStateListeners_ .begin(), pImpl_->windowVisibilityStateListeners_.end(), [listener](sptr registeredListener) { return registeredListener == listener; }), pImpl_->windowVisibilityStateListeners_.end()); WMError ret = WMError::WM_OK; if (pImpl_->windowVisibilityStateListeners_.empty() && pImpl_->windowVisibilityStateListenerAgent_ != nullptr) { ret = SingletonContainer::Get().UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_VISIBILITY, pImpl_->windowVisibilityStateListenerAgent_); if (ret == WMError::WM_OK) { pImpl_->windowVisibilityStateListenerAgent_ = nullptr; } } return ret; } WMError WindowManager::RegisterCameraFloatWindowChangedListener(const sptr& listener) { if (listener == nullptr) { WLOGFE("listener could not be null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); WMError ret = WMError::WM_OK; if (pImpl_->cameraFloatWindowChangedListenerAgent_ == nullptr) { pImpl_->cameraFloatWindowChangedListenerAgent_ = new WindowManagerAgent(); } ret = SingletonContainer::Get().RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_FLOAT, pImpl_->cameraFloatWindowChangedListenerAgent_); if (ret != WMError::WM_OK) { WLOGFW("RegisterWindowManagerAgent failed!"); pImpl_->cameraFloatWindowChangedListenerAgent_ = nullptr; } else { auto iter = std::find(pImpl_->cameraFloatWindowChangedListeners_.begin(), pImpl_->cameraFloatWindowChangedListeners_.end(), listener); if (iter != pImpl_->cameraFloatWindowChangedListeners_.end()) { WLOGFW("Listener is already registered."); return WMError::WM_OK; } pImpl_->cameraFloatWindowChangedListeners_.push_back(listener); } return ret; } WMError WindowManager::UnregisterCameraFloatWindowChangedListener( const sptr& listener) { if (listener == nullptr) { WLOGFE("listener could not be null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); auto iter = std::find(pImpl_->cameraFloatWindowChangedListeners_.begin(), pImpl_->cameraFloatWindowChangedListeners_.end(), listener); if (iter == pImpl_->cameraFloatWindowChangedListeners_.end()) { WLOGFE("could not find this listener"); return WMError::WM_OK; } pImpl_->cameraFloatWindowChangedListeners_.erase(iter); WMError ret = WMError::WM_OK; if (pImpl_->cameraFloatWindowChangedListeners_.empty() && pImpl_->cameraFloatWindowChangedListenerAgent_ != nullptr) { ret = SingletonContainer::Get().UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_FLOAT, pImpl_->cameraFloatWindowChangedListenerAgent_); if (ret == WMError::WM_OK) { pImpl_->cameraFloatWindowChangedListenerAgent_ = nullptr; } } return ret; } WMError WindowManager::RegisterWaterMarkFlagChangedListener(const sptr& listener) { if (listener == nullptr) { WLOGFE("listener could not be null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); WMError ret = WMError::WM_OK; if (pImpl_->waterMarkFlagChangeAgent_ == nullptr) { pImpl_->waterMarkFlagChangeAgent_ = new WindowManagerAgent(); } ret = SingletonContainer::Get().RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WATER_MARK_FLAG, pImpl_->waterMarkFlagChangeAgent_); if (ret != WMError::WM_OK) { WLOGFW("RegisterWindowManagerAgent failed!"); pImpl_->waterMarkFlagChangeAgent_ = nullptr; } else { auto iter = std::find(pImpl_->waterMarkFlagChangeListeners_.begin(), pImpl_->waterMarkFlagChangeListeners_.end(), listener); if (iter != pImpl_->waterMarkFlagChangeListeners_.end()) { WLOGFW("Listener is already registered."); return WMError::WM_OK; } pImpl_->waterMarkFlagChangeListeners_.push_back(listener); } WLOGFD("Try to registerWaterMarkFlagChangedListener && result : %{public}u", static_cast(ret)); return ret; } WMError WindowManager::UnregisterWaterMarkFlagChangedListener(const sptr& listener) { if (listener == nullptr) { WLOGFE("listener could not be null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); auto iter = std::find(pImpl_->waterMarkFlagChangeListeners_.begin(), pImpl_->waterMarkFlagChangeListeners_.end(), listener); if (iter == pImpl_->waterMarkFlagChangeListeners_.end()) { WLOGFE("could not find this listener"); return WMError::WM_OK; } pImpl_->waterMarkFlagChangeListeners_.erase(iter); WMError ret = WMError::WM_OK; if (pImpl_->waterMarkFlagChangeListeners_.empty() && pImpl_->waterMarkFlagChangeAgent_ != nullptr) { ret = SingletonContainer::Get().UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WATER_MARK_FLAG, pImpl_->waterMarkFlagChangeAgent_); if (ret == WMError::WM_OK) { pImpl_->waterMarkFlagChangeAgent_ = nullptr; } } WLOGFD("Try to unregisterWaterMarkFlagChangedListener && result : %{public}u", static_cast(ret)); return ret; } WMError WindowManager::RegisterGestureNavigationEnabledChangedListener( const sptr& listener) { if (listener == nullptr) { WLOGFE("listener could not be null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); WMError ret = WMError::WM_OK; if (pImpl_->gestureNavigationEnabledAgent_ == nullptr) { pImpl_->gestureNavigationEnabledAgent_ = new (std::nothrow)WindowManagerAgent(); } if (pImpl_->gestureNavigationEnabledAgent_ != nullptr) { ret = SingletonContainer::Get().RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_GESTURE_NAVIGATION_ENABLED, pImpl_->gestureNavigationEnabledAgent_); } else { WLOGFE("Create windowManagerAgent object failed!"); ret = WMError::WM_ERROR_NULLPTR; } if (ret != WMError::WM_OK) { WLOGFE("RegisterWindowManagerAgent failed!"); pImpl_->gestureNavigationEnabledAgent_ = nullptr; } else { auto iter = std::find(pImpl_->gestureNavigationEnabledListeners_.begin(), pImpl_->gestureNavigationEnabledListeners_.end(), listener); if (iter != pImpl_->gestureNavigationEnabledListeners_.end()) { WLOGFW("Listener is already registered."); return WMError::WM_OK; } pImpl_->gestureNavigationEnabledListeners_.push_back(listener); } WLOGFD("Try to registerGestureNavigationEnabledChangedListener and result is %{public}u", static_cast(ret)); return ret; } WMError WindowManager::UnregisterGestureNavigationEnabledChangedListener( const sptr& listener) { if (listener == nullptr) { WLOGFE("listener could not be null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); auto iter = std::find(pImpl_->gestureNavigationEnabledListeners_.begin(), pImpl_->gestureNavigationEnabledListeners_.end(), listener); if (iter == pImpl_->gestureNavigationEnabledListeners_.end()) { WLOGFE("could not find this listener"); return WMError::WM_ERROR_INVALID_PARAM; } pImpl_->gestureNavigationEnabledListeners_.erase(iter); WMError ret = WMError::WM_OK; if (pImpl_->gestureNavigationEnabledListeners_.empty() && pImpl_->gestureNavigationEnabledAgent_ != nullptr) { ret = SingletonContainer::Get().UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_GESTURE_NAVIGATION_ENABLED, pImpl_->gestureNavigationEnabledAgent_); if (ret == WMError::WM_OK) { pImpl_->gestureNavigationEnabledAgent_ = nullptr; } } WLOGFD("Try to unregisterGestureNavigationEnabledChangedListener and result is %{public}u", static_cast(ret)); return ret; } WMError WindowManager::RegisterDisplayInfoChangedListener(const sptr& token, const sptr& listener) { if (token == nullptr) { TLOGE(WmsLogTag::DMS, "ability token could not be null"); return WMError::WM_ERROR_NULLPTR; } if (listener == nullptr) { TLOGE(WmsLogTag::DMS, "listener could not be null"); return WMError::WM_ERROR_NULLPTR; } sptr listenerAdapter = new (std::nothrow) WindowDisplayChangeAdapter(token, listener); if (listenerAdapter == nullptr) { TLOGE(WmsLogTag::DMS, "create listener adapter failed."); return WMError::WM_ERROR_NO_MEM; } std::unique_lock lock(pImpl_->listenerMutex_); auto iter = pImpl_->displayInfoChangedListeners_.find(token); if (iter == pImpl_->displayInfoChangedListeners_.end()) { pImpl_->displayInfoChangedListeners_.insert({token, {listenerAdapter}}); } else { auto listerneIter = std::find_if(iter->second.begin(), iter->second.end(), [&listener](const sptr& item) { return listener == item->GetListener(); }); if (listerneIter != iter->second.end()) { TLOGW(WmsLogTag::DMS, "listener is already registered."); } else { iter->second.push_back(listenerAdapter); } } TLOGD(WmsLogTag::DMS, "try to registerDisplayInfoChangedListener success"); return WMError::WM_OK; } WMError WindowManager::UnregisterDisplayInfoChangedListener(const sptr& token, const sptr& listener) { if (token == nullptr) { TLOGE(WmsLogTag::DMS, "ability token could not be null"); return WMError::WM_ERROR_NULLPTR; } if (listener == nullptr) { TLOGE(WmsLogTag::DMS, "listener could not be null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); auto iter = pImpl_->displayInfoChangedListeners_.find(token); if (iter == pImpl_->displayInfoChangedListeners_.end()) { TLOGW(WmsLogTag::DMS, "can not find the ability token"); } else { auto listerneIter = std::find_if(iter->second.begin(), iter->second.end(), [&listener](sptr& item) { return listener == item->GetListener(); }); if (listerneIter == iter->second.end()) { TLOGW(WmsLogTag::DMS, "can not find the listener."); } else { iter->second.erase(listerneIter); if (iter->second.empty()) { pImpl_->displayInfoChangedListeners_.erase(iter); } } } TLOGD(WmsLogTag::DMS, "try to unregisterDisplayInfoChangedListener success"); return WMError::WM_OK; } WMError WindowManager::RegisterWindowPidVisibilityChangedListener( const sptr& listener) { if (listener == nullptr) { TLOGE(WmsLogTag::WMS_LIFE, "listener could not be null"); return WMError::WM_ERROR_NULLPTR; } WMError ret = WMError::WM_OK; { std::unique_lock lock(pImpl_->visibilityListenerAgentListenerMutex_); if (pImpl_->windowPidVisibilityListenerAgent_ == nullptr) { pImpl_->windowPidVisibilityListenerAgent_ = sptr::MakeSptr(); } ret = SingletonContainer::Get().RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_PID_VISIBILITY, pImpl_->windowPidVisibilityListenerAgent_); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_LIFE, "RegisterWindowManagerAgent failed!"); pImpl_->windowPidVisibilityListenerAgent_ = nullptr; } } std::unique_lock lock(pImpl_->listenerMutex_); if (ret == WMError::WM_OK) { auto iter = std::find(pImpl_->windowPidVisibilityListeners_.begin(), pImpl_->windowPidVisibilityListeners_.end(), listener); if (iter != pImpl_->windowPidVisibilityListeners_.end()) { WLOGI("Listener is already registered."); return WMError::WM_OK; } pImpl_->windowPidVisibilityListeners_.emplace_back(listener); } return ret; } WMError WindowManager::UnregisterWindowPidVisibilityChangedListener( const sptr& listener) { if (listener == nullptr) { TLOGE(WmsLogTag::WMS_LIFE, "listener could not be null"); return WMError::WM_ERROR_NULLPTR; } bool isListenEmpty = true; { std::unique_lock lock(pImpl_->listenerMutex_); auto iter = std::find(pImpl_->windowPidVisibilityListeners_.begin(), pImpl_->windowPidVisibilityListeners_.end(), listener); if (iter == pImpl_->windowPidVisibilityListeners_.end()) { TLOGE(WmsLogTag::WMS_LIFE, "could not find this listener"); return WMError::WM_OK; } pImpl_->windowPidVisibilityListeners_.erase(iter); isListenEmpty = pImpl_->windowPidVisibilityListeners_.empty(); } WMError ret = WMError::WM_OK; if (!isListenEmpty) { return ret; } std::unique_lock lock(pImpl_->visibilityListenerAgentListenerMutex_); if (isListenEmpty && pImpl_->windowPidVisibilityListenerAgent_ != nullptr) { ret = SingletonContainer::Get().UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_PID_VISIBILITY, pImpl_->windowPidVisibilityListenerAgent_); if (ret == WMError::WM_OK) { pImpl_->windowPidVisibilityListenerAgent_ = nullptr; } } return ret; } WMError WindowManager::NotifyDisplayInfoChange(const sptr& token, DisplayId displayId, float density, DisplayOrientation orientation) { TLOGD(WmsLogTag::DMS, "notify display info change, displayid=%{public}" PRIu64 ", density=%{public}f, " "orientation=%{public}d", displayId, density, orientation); if (token == nullptr) { TLOGE(WmsLogTag::DMS, "notify display info change failed, token is nullptr"); return WMError::WM_ERROR_INVALID_PARAM; } pImpl_->NotifyDisplayInfoChanged(token, displayId, density, orientation); return WMError::WM_OK; } void WindowManager::GetFocusWindowInfo(FocusChangeInfo& focusInfo, DisplayId displayId) { SingletonContainer::Get().GetFocusWindowInfo(focusInfo, displayId); } void WindowManager::OnWMSConnectionChanged(int32_t userId, int32_t screenId, bool isConnected) const { if (isConnected) { pImpl_->NotifyWMSConnected(userId, screenId); } else { pImpl_->NotifyWMSDisconnected(userId, screenId); } } void WindowManager::UpdateFocusChangeInfo(const sptr& focusChangeInfo, bool focused) const { if (focusChangeInfo == nullptr) { WLOGFE("focusChangeInfo is nullptr."); return; } TLOGD(WmsLogTag::WMS_FOCUS, "window focus change: %{public}d, id: %{public}u", focused, focusChangeInfo->windowId_); if (focused) { pImpl_->NotifyFocused(focusChangeInfo); } else { pImpl_->NotifyUnfocused(focusChangeInfo); } } void WindowManager::UpdateWindowModeTypeInfo(WindowModeType type) const { pImpl_->NotifyWindowModeChange(type); } WMError WindowManager::GetWindowModeType(WindowModeType& windowModeType) const { WMError ret = SingletonContainer::Get().GetWindowModeType(windowModeType); if (ret != WMError::WM_OK) { WLOGFE("get window mode type failed"); } return ret; } void WindowManager::UpdateSystemBarRegionTints(DisplayId displayId, const SystemBarRegionTints& tints) const { pImpl_->NotifySystemBarChanged(displayId, tints); } void WindowManager::NotifyAccessibilityWindowInfo(const std::vector>& infos, WindowUpdateType type) const { pImpl_->NotifyAccessibilityWindowInfo(infos, type); } void WindowManager::UpdateWindowVisibilityInfo( const std::vector>& windowVisibilityInfos) const { pImpl_->NotifyWindowVisibilityInfoChanged(windowVisibilityInfos); pImpl_->NotifyWindowVisibilityStateChanged(windowVisibilityInfos); } void WindowManager::UpdateWindowDrawingContentInfo( const std::vector>& windowDrawingContentInfos) const { pImpl_->NotifyWindowDrawingContentInfoChanged(windowDrawingContentInfos); } WMError WindowManager::GetAccessibilityWindowInfo(std::vector>& infos) const { WMError ret = SingletonContainer::Get().GetAccessibilityWindowInfo(infos); if (ret != WMError::WM_OK) { WLOGFE("get window info failed"); } return ret; } WMError WindowManager::GetUnreliableWindowInfo(int32_t windowId, std::vector>& infos) const { WMError ret = SingletonContainer::Get().GetUnreliableWindowInfo(windowId, infos); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::DEFAULT, "get unreliable window info failed"); } return ret; } WMError WindowManager::ListWindowInfo(const WindowInfoOption& windowInfoOption, std::vector>& infos) const { TLOGD(WmsLogTag::WMS_ATTRIBUTE, "windowInfoOption: %{public}u %{public}u %{public}" PRIu64 " %{public}d", static_cast(windowInfoOption.windowInfoFilterOption), static_cast(windowInfoOption.windowInfoTypeOption), windowInfoOption.displayId, windowInfoOption.windowId); WMError ret = SingletonContainer::Get().ListWindowInfo(windowInfoOption, infos); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "failed"); } return ret; } WMError WindowManager::GetAllWindowLayoutInfo(DisplayId displayId, std::vector>& infos) const { WMError ret = SingletonContainer::Get().GetAllWindowLayoutInfo(displayId, infos); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "failed"); } return ret; } WMError WindowManager::GetGlobalWindowMode(DisplayId displayId, GlobalWindowMode& globalWinMode) const { return SingletonContainer::Get().GetGlobalWindowMode(displayId, globalWinMode); } WMError WindowManager::GetTopNavDestinationName(int32_t windowId, std::string& topNavDestName) const { return SingletonContainer::Get().GetTopNavDestinationName(windowId, topNavDestName); } WMError WindowManager::GetVisibilityWindowInfo(std::vector>& infos) const { WMError ret = SingletonContainer::Get().GetVisibilityWindowInfo(infos); if (ret != WMError::WM_OK) { WLOGFE("get window visibility info failed"); } return ret; } WMError WindowManager::DumpSessionAll(std::vector& infos) { WMError ret = SingletonContainer::Get().DumpSessionAll(infos); if (ret != WMError::WM_OK) { WLOGFE("dump session all failed"); } return ret; } WMError WindowManager::DumpSessionWithId(int32_t persistentId, std::vector& infos) { WMError ret = SingletonContainer::Get().DumpSessionWithId(persistentId, infos); if (ret != WMError::WM_OK) { WLOGFE("dump session with id failed"); } return ret; } WMError WindowManager::GetUIContentRemoteObj(int32_t windowId, sptr& uiContentRemoteObj) { WMError ret = SingletonContainer::Get().GetUIContentRemoteObj(windowId, uiContentRemoteObj); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "Failed to get UIContentRemoteObj. PersistentId=%{public}d; ret=%{public}u", windowId, static_cast(ret)); } return ret; } WMError WindowManager::SetGestureNavigationEnabled(bool enable) const { WMError ret = SingletonContainer::Get().SetGestureNavigationEnabled(enable); if (ret != WMError::WM_OK) { WLOGFE("set gesture navigation enabled failed"); } return ret; } WMError WindowManager::NotifyWindowExtensionVisibilityChange(int32_t pid, int32_t uid, bool visible) { WMError ret = SingletonContainer::Get().NotifyWindowExtensionVisibilityChange(pid, uid, visible); if (ret != WMError::WM_OK) { WLOGFE("notify WindowExtension visibility change failed"); } return ret; } void WindowManager::UpdateCameraFloatWindowStatus(uint32_t accessTokenId, bool isShowing) const { pImpl_->UpdateCameraFloatWindowStatus(accessTokenId, isShowing); } void WindowManager::NotifyWaterMarkFlagChangedResult(bool showWaterMark) const { pImpl_->NotifyWaterMarkFlagChangedResult(showWaterMark); } void WindowManager::NotifyGestureNavigationEnabledResult(bool enable) const { pImpl_->NotifyGestureNavigationEnabledResult(enable); } void WindowManager::NotifyWindowPidVisibilityChanged(const sptr& info) const { pImpl_->NotifyWindowPidVisibilityChanged(info); } WMError WindowManager::RaiseWindowToTop(int32_t persistentId) { WMError ret = SingletonContainer::Get().RaiseWindowToTop(persistentId); if (ret != WMError::WM_OK) { WLOGFE("raise window to top failed"); } return ret; } WMError WindowManager::NotifyWindowStyleChange(WindowStyleType type) { pImpl_->NotifyWindowStyleChange(type); return WMError::WM_OK; } WMError WindowManager::RegisterDrawingContentChangedListener(const sptr& listener) { if (listener == nullptr) { WLOGFE("listener could not be null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); WMError ret = WMError::WM_OK; if (pImpl_->windowDrawingContentListenerAgent_ == nullptr) { pImpl_->windowDrawingContentListenerAgent_ = new WindowManagerAgent(); } ret = SingletonContainer::Get().RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_DRAWING_STATE, pImpl_->windowDrawingContentListenerAgent_); if (ret != WMError::WM_OK) { WLOGFW("RegisterWindowManagerAgent failed!"); pImpl_->windowDrawingContentListenerAgent_ = nullptr; } else { auto iter = std::find(pImpl_->windowDrawingContentListeners_.begin(), pImpl_->windowDrawingContentListeners_.end(), listener); if (iter != pImpl_->windowDrawingContentListeners_.end()) { WLOGFW("Listener is already registered."); return WMError::WM_OK; } pImpl_->windowDrawingContentListeners_.emplace_back(listener); } return ret; } WMError WindowManager::UnregisterDrawingContentChangedListener(const sptr& listener) { if (listener == nullptr) { WLOGFE("listener could not be null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); pImpl_->windowDrawingContentListeners_.erase(std::remove_if(pImpl_->windowDrawingContentListeners_.begin(), pImpl_->windowDrawingContentListeners_.end(), [listener](sptr registeredListener) { return registeredListener == listener; }), pImpl_->windowDrawingContentListeners_.end()); WMError ret = WMError::WM_OK; if (pImpl_->windowDrawingContentListeners_.empty() && pImpl_->windowDrawingContentListenerAgent_ != nullptr) { ret = SingletonContainer::Get().UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_DRAWING_STATE, pImpl_->windowDrawingContentListenerAgent_); if (ret == WMError::WM_OK) { pImpl_->windowDrawingContentListenerAgent_ = nullptr; } } return ret; } WMError WindowManager::RegisterWindowSystemBarPropertyChangedListener( const sptr& listener) { if (listener == nullptr) { TLOGE(WmsLogTag::WMS_IMMS, "listener is nullptr."); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); WMError ret = WMError::WM_OK; if (pImpl_->windowSystemBarPropertyChangeAgent_ == nullptr) { pImpl_->windowSystemBarPropertyChangeAgent_ = new WindowManagerAgent(); } ret = SingletonContainer::Get().RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_STATUS_BAR_PROPERTY, pImpl_->windowSystemBarPropertyChangeAgent_); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_IMMS, "RegisterWindowManagerAgent failed."); pImpl_->windowSystemBarPropertyChangeAgent_ = nullptr; } else { auto iter = std::find(pImpl_->windowSystemBarPropertyChangedListeners_.begin(), pImpl_->windowSystemBarPropertyChangedListeners_.end(), listener); if (iter != pImpl_->windowSystemBarPropertyChangedListeners_.end()) { TLOGI(WmsLogTag::WMS_IMMS, "listener is already registered."); return WMError::WM_DO_NOTHING; } pImpl_->windowSystemBarPropertyChangedListeners_.emplace_back(listener); TLOGI(WmsLogTag::WMS_IMMS, "listener registered success."); } return ret; } WMError WindowManager::UnregisterWindowSystemBarPropertyChangedListener( const sptr& listener) { if (listener == nullptr) { TLOGE(WmsLogTag::WMS_IMMS, "listener is nullptr."); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); auto iter = std::find(pImpl_->windowSystemBarPropertyChangedListeners_.begin(), pImpl_->windowSystemBarPropertyChangedListeners_.end(), listener); if (iter == pImpl_->windowSystemBarPropertyChangedListeners_.end()) { TLOGI(WmsLogTag::WMS_IMMS, "could not find this listener."); return WMError::WM_DO_NOTHING; } WMError ret = WMError::WM_OK; if (pImpl_->windowSystemBarPropertyChangeAgent_ != nullptr) { ret = SingletonContainer::Get().UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_STATUS_BAR_PROPERTY, pImpl_->windowSystemBarPropertyChangeAgent_); if (ret == WMError::WM_OK) { pImpl_->windowSystemBarPropertyChangeAgent_ = nullptr; } } return ret; } void WindowManager::NotifyWindowSystemBarPropertyChange(WindowType type, const SystemBarProperty& systemBarProperty) { pImpl_->NotifyWindowSystemBarPropertyChange(type, systemBarProperty); } void WindowManager::Impl::NotifyWindowSystemBarPropertyChange( WindowType type, const SystemBarProperty& systemBarProperty) { std::vector> windowSystemBarPropertyChangedListeners; { std::unique_lock lock(listenerMutex_); windowSystemBarPropertyChangedListeners = windowSystemBarPropertyChangedListeners_; } for (auto &listener : windowSystemBarPropertyChangedListeners) { if (listener != nullptr) { TLOGI(WmsLogTag::WMS_IMMS, "type %{public}d, enable %{public}d.", type, systemBarProperty.enable_); listener->OnWindowSystemBarPropertyChanged(type, systemBarProperty); } } } WMError WindowManager::ShiftAppWindowFocus(int32_t sourcePersistentId, int32_t targetPersistentId) { WMError ret = SingletonContainer::Get().ShiftAppWindowFocus(sourcePersistentId, targetPersistentId); if (ret != WMError::WM_OK) { WLOGFE("shift application window focus failed"); } return ret; } WMError WindowManager::RegisterVisibleWindowNumChangedListener(const sptr& listener) { if (listener == nullptr) { TLOGE(WmsLogTag::WMS_MAIN, "listener could not be null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); WMError ret = WMError::WM_OK; if (pImpl_->visibleWindowNumChangedListenerAgent_ == nullptr) { pImpl_->visibleWindowNumChangedListenerAgent_ = new WindowManagerAgent(); } ret = SingletonContainer::Get().RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_VISIBLE_WINDOW_NUM, pImpl_->visibleWindowNumChangedListenerAgent_); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_MAIN, "RegisterWindowManagerAgent failed!"); pImpl_->visibleWindowNumChangedListenerAgent_ = nullptr; } else { auto iter = std::find(pImpl_->visibleWindowNumChangedListeners_.begin(), pImpl_->visibleWindowNumChangedListeners_.end(), listener); if (iter != pImpl_->visibleWindowNumChangedListeners_.end()) { TLOGE(WmsLogTag::WMS_MAIN, "Listener is already registered."); return WMError::WM_OK; } pImpl_->visibleWindowNumChangedListeners_.emplace_back(listener); } return ret; } WMError WindowManager::GetSnapshotByWindowId(int32_t windowId, std::shared_ptr& pixelMap) { return SingletonContainer::Get().GetSnapshotByWindowId(windowId, pixelMap); } WMError WindowManager::UnregisterVisibleWindowNumChangedListener(const sptr& listener) { if (listener == nullptr) { TLOGE(WmsLogTag::WMS_MAIN, "listener could not be null"); return WMError::WM_ERROR_NULLPTR; } std::unique_lock lock(pImpl_->listenerMutex_); auto iter = std::find(pImpl_->visibleWindowNumChangedListeners_.begin(), pImpl_->visibleWindowNumChangedListeners_.end(), listener); if (iter == pImpl_->visibleWindowNumChangedListeners_.end()) { TLOGE(WmsLogTag::WMS_MAIN, "could not find this listener"); return WMError::WM_OK; } WMError ret = WMError::WM_OK; if (pImpl_->visibleWindowNumChangedListeners_.empty() && pImpl_->visibleWindowNumChangedListenerAgent_ != nullptr) { ret = SingletonContainer::Get().UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_VISIBLE_WINDOW_NUM, pImpl_->visibleWindowNumChangedListenerAgent_); if (ret == WMError::WM_OK) { pImpl_->visibleWindowNumChangedListenerAgent_ = nullptr; } } return ret; } void WindowManager::UpdateVisibleWindowNum(const std::vector& visibleWindowNumInfo) { pImpl_->NotifyVisibleWindowNumChanged(visibleWindowNumInfo); } WMError WindowManager::RegisterWindowStyleChangedListener(const sptr& listener) { TLOGI(WmsLogTag::WMS_MAIN, "start register"); if (listener == nullptr) { TLOGE(WmsLogTag::WMS_MAIN, "listener could not be null"); return WMError::WM_ERROR_NULLPTR; } { std::unique_lock lock(pImpl_->listenerMutex_); if (pImpl_->windowStyleListenerAgent_ == nullptr) { pImpl_->windowStyleListenerAgent_ = new WindowManagerAgent(); } auto iter = std::find(pImpl_->windowStyleListeners_.begin(), pImpl_->windowStyleListeners_.end(), listener); if (iter != pImpl_->windowStyleListeners_.end()) { TLOGW(WmsLogTag::WMS_MAIN, "Listener is already registered."); return WMError::WM_OK; } pImpl_->windowStyleListeners_.push_back(listener); } WMError ret = WMError::WM_OK; ret = SingletonContainer::Get().RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_STYLE, pImpl_->windowStyleListenerAgent_); if (ret != WMError::WM_OK) { TLOGW(WmsLogTag::WMS_MAIN, "RegisterWindowManagerAgent failed!"); std::unique_lock lock(pImpl_->listenerMutex_); pImpl_->windowStyleListenerAgent_ = nullptr; auto iter = std::find(pImpl_->windowStyleListeners_.begin(), pImpl_->windowStyleListeners_.end(), listener); if (iter != pImpl_->windowStyleListeners_.end()) { pImpl_->windowStyleListeners_.erase(iter); } } return ret; } WMError WindowManager::UnregisterWindowStyleChangedListener(const sptr& listener) { TLOGI(WmsLogTag::WMS_MAIN, "start unregister"); if (listener == nullptr) { TLOGE(WmsLogTag::WMS_MAIN, "listener could not be null"); return WMError::WM_ERROR_NULLPTR; } { std::unique_lock lock(pImpl_->listenerMutex_); auto iter = std::find(pImpl_->windowStyleListeners_.begin(), pImpl_->windowStyleListeners_.end(), listener); if (iter == pImpl_->windowStyleListeners_.end()) { TLOGE(WmsLogTag::WMS_MAIN, "could not find this listener"); return WMError::WM_OK; } pImpl_->windowStyleListeners_.erase(iter); } WMError ret = WMError::WM_OK; if (pImpl_->windowStyleListeners_.empty() && pImpl_->windowStyleListenerAgent_ != nullptr) { ret = SingletonContainer::Get().UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_STYLE, pImpl_->windowStyleListenerAgent_); if (ret == WMError::WM_OK) { std::unique_lock lock(pImpl_->listenerMutex_); pImpl_->windowStyleListenerAgent_ = nullptr; } } return ret; } WindowStyleType WindowManager::GetWindowStyleType() { WindowStyleType styleType; if (SingletonContainer::Get().GetWindowStyleType(styleType) == WMError::WM_OK) { return styleType; } return styleType; } WMError WindowManager::SkipSnapshotForAppProcess(int32_t pid, bool skip) { WMError ret = SingletonContainer::Get().SkipSnapshotForAppProcess(pid, skip); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "skip failed"); } return ret; } WMError WindowManager::SetProcessWatermark(int32_t pid, const std::string& watermarkName, bool isEnabled) { WMError ret = SingletonContainer::Get().SetProcessWatermark(pid, watermarkName, isEnabled); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "failed"); } return ret; } WMError WindowManager::GetWindowIdsByCoordinate(DisplayId displayId, int32_t windowNumber, int32_t x, int32_t y, std::vector& windowIds) const { WMError ret = SingletonContainer::Get().GetWindowIdsByCoordinate( displayId, windowNumber, x, y, windowIds); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::DEFAULT, "get windowIds by coordinate failed"); } return ret; } WMError WindowManager::UpdateScreenLockStatusForApp(const std::string& bundleName, bool isRelease) { WMError ret = SingletonContainer::Get().UpdateScreenLockStatusForApp(bundleName, isRelease); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "update screen lock status failed"); } return ret; } WMError WindowManager::GetDisplayIdByWindowId(const std::vector& windowIds, std::unordered_map& windowDisplayIdMap) { WMError ret = SingletonContainer::Get().GetDisplayIdByWindowId(windowIds, windowDisplayIdMap); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "failed"); } return ret; } WMError WindowManager::SetGlobalDragResizeType(DragResizeType dragResizeType) { WMError ret = SingletonContainer::Get().SetGlobalDragResizeType(dragResizeType); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::DEFAULT, "failed"); } return ret; } WMError WindowManager::GetGlobalDragResizeType(DragResizeType& dragResizeType) { WMError ret = SingletonContainer::Get().GetGlobalDragResizeType(dragResizeType); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::DEFAULT, "failed"); } return ret; } WMError WindowManager::SetAppDragResizeType(const std::string& bundleName, DragResizeType dragResizeType) { WMError ret = SingletonContainer::Get().SetAppDragResizeType(bundleName, dragResizeType); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::DEFAULT, "failed"); } return ret; } WMError WindowManager::GetAppDragResizeType(const std::string& bundleName, DragResizeType& dragResizeType) { WMError ret = SingletonContainer::Get().GetAppDragResizeType(bundleName, dragResizeType); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::DEFAULT, "failed"); } return ret; } WMError WindowManager::SetAppKeyFramePolicy(const std::string& bundleName, const KeyFramePolicy& keyFramePolicy) { WMError ret = SingletonContainer::Get().SetAppKeyFramePolicy(bundleName, keyFramePolicy); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::DEFAULT, "failed"); } return ret; } WMError WindowManager::ShiftAppWindowPointerEvent(int32_t sourceWindowId, int32_t targetWindowId, int32_t fingerId) { WMError ret = SingletonContainer::Get().ShiftAppWindowPointerEvent( sourceWindowId, targetWindowId, fingerId); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_PC, "failed"); } return ret; } WMError WindowManager::NotifyScreenshotEvent(ScreenshotEventType type) { WMError ret = SingletonContainer::Get().NotifyScreenshotEvent(type); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "failed"); } return ret; } WMError WindowManager::SetStartWindowBackgroundColor( const std::string& moduleName, const std::string& abilityName, uint32_t color) { int32_t uid = static_cast(getuid()); WMError ret = SingletonContainer::Get().SetStartWindowBackgroundColor( moduleName, abilityName, color, uid); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_PATTERN, "failed"); } return ret; } WMError WindowManager::RequestFocus(int32_t persistentId, bool isFocused, bool byForeground, WindowFocusChangeReason reason) { int32_t curReason = static_cast(reason); int32_t reasonStart = static_cast(FocusChangeReason::DEFAULT); int32_t reasonEnd = static_cast(FocusChangeReason::MAX); if (curReason < reasonStart || curReason > reasonEnd) { TLOGE(WmsLogTag::WMS_FOCUS, "could not find focus reason"); return WMError::WM_ERROR_INVALID_PARAM; } WMError ret = SingletonContainer::Get().RequestFocusStatusBySA(persistentId, isFocused, byForeground, static_cast(curReason)); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_FOCUS, "failed"); } return ret; } WMError WindowManager::MinimizeByWindowId(const std::vector& windowIds) { WMError ret = SingletonContainer::Get().MinimizeByWindowId(windowIds); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_LIFE, "failed"); } return ret; } WMError WindowManager::SetForegroundWindowNum(uint32_t windowNum) { WMError ret = SingletonContainer::Get().SetForegroundWindowNum(windowNum); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_PC, "failed"); } return ret; } WMError WindowManager::ProcessRegisterWindowInfoChangeCallback(WindowInfoKey observedInfo, const sptr& listener) { switch (observedInfo) { case WindowInfoKey::VISIBILITY_STATE : return RegisterVisibilityStateChangedListener(listener); case WindowInfoKey::DISPLAY_ID : return RegisterDisplayIdChangedListener(listener); case WindowInfoKey::WINDOW_RECT : return RegisterRectChangedListener(listener); case WindowInfoKey::WINDOW_MODE : return RegisterWindowModeChangedListenerForPropertyChange(listener); case WindowInfoKey::FLOATING_SCALE : return RegisterFloatingScaleChangedListener(listener); default: TLOGE(WmsLogTag::WMS_ATTRIBUTE, "Invalid observedInfo: %{public}d", static_cast(observedInfo)); return WMError::WM_ERROR_INVALID_PARAM; } } WMError WindowManager::ProcessUnregisterWindowInfoChangeCallback(WindowInfoKey observedInfo, const sptr& listener) { switch (observedInfo) { case WindowInfoKey::VISIBILITY_STATE : return UnregisterVisibilityStateChangedListener(listener); case WindowInfoKey::DISPLAY_ID : return UnregisterDisplayIdChangedListener(listener); case WindowInfoKey::WINDOW_RECT : return UnregisterRectChangedListener(listener); case WindowInfoKey::WINDOW_MODE : return UnregisterWindowModeChangedListenerForPropertyChange(listener); case WindowInfoKey::FLOATING_SCALE : return UnregisterFloatingScaleChangedListener(listener); default: TLOGE(WmsLogTag::WMS_ATTRIBUTE, "Invalid observedInfo: %{public}d", static_cast(observedInfo)); return WMError::WM_ERROR_INVALID_PARAM; } } WMError WindowManager::RegisterWindowInfoChangeCallback(const std::unordered_set& observedInfo, const sptr& listener) { std::ostringstream observedInfoForLog; observedInfoForLog << "ObservedInfo: "; auto ret = WMError::WM_OK; if (!listener) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "listener is null."); return WMError::WM_ERROR_NULLPTR; } for (const auto& info : observedInfo) { observedInfoForLog << static_cast(info) << ", "; if (listener->GetInterestInfo().find(info) == listener->GetInterestInfo().end()) { listener->AddInterestInfo(info); } if (!listener->GetInterestWindowIds().empty()) { listener->AddInterestInfo(WindowInfoKey::WINDOW_ID); } ret = ProcessRegisterWindowInfoChangeCallback(info, listener); if (ret != WMError::WM_OK) { observedInfoForLog << "failed"; break; } } TLOGI(WmsLogTag::WMS_ATTRIBUTE, "%{public}s", observedInfoForLog.str().c_str()); return ret; } WMError WindowManager::UnregisterWindowInfoChangeCallback(const std::unordered_set& observedInfo, const sptr& listener) { std::ostringstream observedInfoForLog; observedInfoForLog << "ObservedInfo: "; auto ret = WMError::WM_OK; if (!listener) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "listener is null."); return WMError::WM_ERROR_NULLPTR; } for (const auto& info : observedInfo) { observedInfoForLog << static_cast(info) << ", "; if (listener->GetInterestInfo().find(info) == listener->GetInterestInfo().end()) { listener->AddInterestInfo(info); } if (!listener->GetInterestWindowIds().empty()) { listener->AddInterestInfo(WindowInfoKey::WINDOW_ID); } ret = ProcessUnregisterWindowInfoChangeCallback(info, listener); if (ret != WMError::WM_OK) { observedInfoForLog << "failed"; break; } } TLOGI(WmsLogTag::WMS_ATTRIBUTE, "%{public}s", observedInfoForLog.str().c_str()); return ret; } void WindowManager::SetIsModuleHookOffToSet(const std::string& moduleName) { isModuleHookOffSet_.insert(moduleName); TLOGI(WmsLogTag::WMS_LIFE, "%{public}s", moduleName.c_str()); } bool WindowManager::GetIsModuleHookOffFromSet(const std::string& moduleName) { auto iter = isModuleHookOffSet_.find(moduleName); if (iter != isModuleHookOffSet_.end()) { return true; } return false; } bool WindowManager::IsModuleHookOff(bool isModuleAbilityHookEnd, const std::string& moduleName) { if (isModuleAbilityHookEnd) { SetIsModuleHookOffToSet(moduleName); } if (GetIsModuleHookOffFromSet(moduleName)) { TLOGI(WmsLogTag::WMS_MAIN, "set IsModuleHookOff true"); return true; } return false; } void WindowManager::NotifyWindowPropertyChange(uint32_t propertyDirtyFlags, const std::vector>& windowInfoList) { if (propertyDirtyFlags & static_cast(WindowInfoKey::WINDOW_RECT)) { pImpl_->NotifyWindowRectChange(windowInfoList); } if (propertyDirtyFlags & static_cast(WindowInfoKey::DISPLAY_ID)) { pImpl_->NotifyDisplayIdChange(windowInfoList); } if (propertyDirtyFlags & static_cast(WindowInfoKey::WINDOW_MODE)) { pImpl_->NotifyWindowModeChangeForPropertyChange(windowInfoList); } if (propertyDirtyFlags & static_cast(WindowInfoKey::FLOATING_SCALE)) { pImpl_->NotifyFloatingScaleChange(windowInfoList); } } WMError WindowManager::AnimateTo(int32_t windowId, WindowAnimationProperty animationProperty, WindowAnimationOption animationOption) { TLOGI(WmsLogTag::WMS_ANIMATION, "In, windowId: %{public}d, targetScale: %{public}f, animationOption: %{public}s", windowId, animationProperty.targetScale, animationOption.ToString().c_str()); WMError ret = SingletonContainer::Get().AnimateTo(windowId, animationProperty, animationOption); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ANIMATION, "Failed, errorCode: %{public}d", static_cast(ret)); } return ret; } WMError WindowManager::RegisterWindowLifeCycleCallback(const sptr& listener) { if (listener == nullptr) { TLOGE(WmsLogTag::WMS_LIFE, "window lifecycle listener registered could not be null"); return WMError::WM_ERROR_NULLPTR; } TLOGI(WmsLogTag::WMS_LIFE, "Register window lifecycle listener"); { std::unique_lock lock(pImpl_->listenerMutex_); if (pImpl_->windowLifeCycleListener_) { TLOGI(WmsLogTag::WMS_LIFE, "WindowLifeCycleListener is already registered, do nothing"); return WMError::WM_OK; } pImpl_->windowLifeCycleListener_ = listener; } return WMError::WM_OK; } WMError WindowManager::UnregisterWindowLifeCycleCallback(const sptr& listener) { if (listener == nullptr) { TLOGE(WmsLogTag::WMS_LIFE, "window lifecycle listener unregistered could not be null"); return WMError::WM_ERROR_NULLPTR; } TLOGI(WmsLogTag::WMS_LIFE, "Unregister window lifecycle listener"); { std::unique_lock lock(pImpl_->listenerMutex_); pImpl_->windowLifeCycleListener_ = nullptr; } return WMError::WM_OK; } void WindowManager::RegisterGetJSWindowCallback(GetJSWindowObjFunc&& getJSWindowFunc) { getJSWindowObjFunc_ = std::move(getJSWindowFunc); } void WindowManager::NotifyWMSWindowDestroyed(const WindowLifeCycleInfo& lifeCycleInfo) { void* jsWindowNapiValue = nullptr; if (getJSWindowObjFunc_ != nullptr) { TLOGI(WmsLogTag::WMS_LIFE, "window name: %{public}s, window id: %{public}d", lifeCycleInfo.windowName.c_str(), lifeCycleInfo.windowId); jsWindowNapiValue = getJSWindowObjFunc_(lifeCycleInfo.windowName); } pImpl_->NotifyWMSWindowDestroyed(lifeCycleInfo, jsWindowNapiValue); } WMError WindowManager::AddSessionBlackList( const std::unordered_set& bundleNames, const std::unordered_set& privacyWindowTags) { TLOGI(WmsLogTag::WMS_ATTRIBUTE, "in"); auto ret = SingletonContainer::Get().AddSessionBlackList(bundleNames, privacyWindowTags); return ret; } WMError WindowManager::RemoveSessionBlackList( const std::unordered_set& bundleNames, const std::unordered_set& privacyWindowTags) { TLOGI(WmsLogTag::WMS_ATTRIBUTE, "in"); auto ret = SingletonContainer::Get(). RemoveSessionBlackList(bundleNames, privacyWindowTags); return ret; } } // namespace Rosen } // namespace OHOS