From 0966d60f7d631bf5dc7d7acafc0cc7824ac29d38 Mon Sep 17 00:00:00 2001 From: xuedong Date: Thu, 21 Nov 2024 11:41:29 +0800 Subject: [PATCH] fix: half display Signed-off-by: xuedong --- dm_lite/src/display_manager_lite.cpp | 90 +++++++++++++++++++ .../unittest/display_manager_lite_test.cpp | 65 ++++++++++++++ .../innerkits/dm_lite/display_manager_lite.h | 28 +++++- 3 files changed, 182 insertions(+), 1 deletion(-) diff --git a/dm_lite/src/display_manager_lite.cpp b/dm_lite/src/display_manager_lite.cpp index 83a264823d..3761f9f16c 100644 --- a/dm_lite/src/display_manager_lite.cpp +++ b/dm_lite/src/display_manager_lite.cpp @@ -47,6 +47,8 @@ public: DMError UnregisterFoldStatusListener(sptr listener); DMError RegisterDisplayModeListener(sptr listener); DMError UnregisterDisplayModeListener(sptr listener); + DMError RegisterScreenMagneticStateListener(sptr listener); + DMError UnregisterScreenMagneticStateListener(sptr listener); void OnRemoteDied(); sptr GetDisplayById(DisplayId displayId); /* @@ -84,6 +86,10 @@ private: */ class DisplayManagerAgent; sptr displayStateAgent_; + void NotifyScreenMagneticStateChanged(bool isMagneticState); + std::set> screenMagneticStateListeners_; + class DisplayManagerScreenMagneticStateAgent; + sptr screenMagneticStateListenerAgent_; }; class DisplayManagerLite::Impl::DisplayManagerListener : public DisplayManagerAgentDefault { @@ -185,6 +191,21 @@ private: sptr pImpl_; }; +class DisplayManagerLite::Impl::DisplayManagerScreenMagneticStateAgent : public DisplayManagerAgentDefault { +public: + explicit DisplayManagerScreenMagneticStateAgent(sptr impl) : pImpl_(impl) + { + } + ~DisplayManagerScreenMagneticStateAgent() = default; + + virtual void NotifyScreenMagneticStateChanged(bool isMagneticState) override + { + pImpl_->NotifyScreenMagneticStateChanged(isMagneticState); + } +private: + sptr pImpl_; +}; + /* * used by powermgr */ @@ -672,6 +693,75 @@ void DisplayManagerLite::Impl::ClearDisplayStateCallback() } } +DMError DisplayManagerLite::RegisterScreenMagneticStateListener(sptr listener) +{ + if (listener == nullptr) { + WLOGFE("IScreenMagneticStateListener listener is nullptr."); + return DMError::DM_ERROR_NULLPTR; + } + return pImpl_->RegisterScreenMagneticStateListener(listener); +} + +DMError DisplayManagerLite::Impl::RegisterScreenMagneticStateListener(sptr listener) +{ + std::lock_guard lock(mutex_); + DMError ret = DMError::DM_OK; + if (screenMagneticStateListenerAgent_ == nullptr) { + screenMagneticStateListenerAgent_ = new DisplayManagerScreenMagneticStateAgent(this); + ret = SingletonContainer::Get().RegisterDisplayManagerAgent( + screenMagneticStateListenerAgent_, + DisplayManagerAgentType::SCREEN_MAGNETIC_STATE_CHANGED_LISTENER); + } + if (ret != DMError::DM_OK) { + WLOGFW("RegisterScreenMagneticStateListener failed !"); + screenMagneticStateListenerAgent_ = nullptr; + } else { + WLOGD("IScreenMagneticStateListener register success"); + screenMagneticStateListeners_.insert(listener); + } + return ret; +} + +DMError DisplayManagerLite::UnregisterScreenMagneticStateListener(sptr listener) +{ + if (listener == nullptr) { + WLOGFE("UnregisterScreenMagneticStateListener listener is nullptr."); + return DMError::DM_ERROR_NULLPTR; + } + return pImpl_->UnregisterScreenMagneticStateListener(listener); +} + +DMError DisplayManagerLite::Impl::UnregisterScreenMagneticStateListener(sptr listener) +{ + std::lock_guard lock(mutex_); + auto iter = std::find(screenMagneticStateListeners_.begin(), screenMagneticStateListeners_.end(), listener); + if (iter == screenMagneticStateListeners_.end()) { + WLOGFE("could not find this listener"); + return DMError::DM_ERROR_NULLPTR; + } + screenMagneticStateListeners_.erase(iter); + DMError ret = DMError::DM_OK; + if (screenMagneticStateListeners_.empty() && screenMagneticStateListenerAgent_ != nullptr) { + ret = SingletonContainer::Get().UnregisterDisplayManagerAgent( + screenMagneticStateListenerAgent_, + DisplayManagerAgentType::SCREEN_MAGNETIC_STATE_CHANGED_LISTENER); + screenMagneticStateListenerAgent_ = nullptr; + } + return ret; +} + +void DisplayManagerLite::Impl::NotifyScreenMagneticStateChanged(bool isMagneticState) +{ + std::set> screenMagneticStateListeners; + { + std::lock_guard lock(mutex_); + screenMagneticStateListeners = screenMagneticStateListeners_; + } + for (auto& listener : screenMagneticStateListeners) { + listener->OnScreenMagneticStateChanged(isMagneticState); + } +} + bool DisplayManagerLite::TryToCancelScreenOff() { WLOGFD("[UL_POWER]TryToCancelScreenOff start"); diff --git a/dm_lite/test/unittest/display_manager_lite_test.cpp b/dm_lite/test/unittest/display_manager_lite_test.cpp index 70e723a86f..a0d50f4f62 100644 --- a/dm_lite/test/unittest/display_manager_lite_test.cpp +++ b/dm_lite/test/unittest/display_manager_lite_test.cpp @@ -649,6 +649,71 @@ HWTEST_F(DisplayManagerTest, GetScreenBrightness, Function | SmallTest | Level1) ASSERT_FALSE(ret == 1); } +/** + * @tc.name: RegisterScreenMagneticStateListener + * @tc.desc: RegisterScreenMagneticStateListener fun + * @tc.type: FUNC + */ +HWTEST_F(DisplayManagerTest, RegisterScreenMagneticStateListener, Function | SmallTest | Level1) +{ + sptr listener; + auto ret = DisplayManagerLite::GetInstance().RegisterScreenMagneticStateListener(listener); + ASSERT_EQ(ret, DMError::DM_ERROR_NULLPTR); + listener = new DisplayManagerLite::IScreenMagneticStateListener(); + ret = DisplayManagerLite::GetInstance().RegisterScreenMagneticStateListener(listener); + ASSERT_EQ(ret, DisplayManagerLite::GetInstance().pImpl_->RegisterScreenMagneticStateListener(listener)); + listener.clear(); +} + +/** + * @tc.name: ImplRegisterScreenMagneticStateListener + * @tc.desc: ImplRegisterScreenMagneticStateListener fun + * @tc.type: FUNC + */ +HWTEST_F(DisplayManagerTest, ImplRegisterScreenMagneticStateListener, Function | SmallTest | Level1) +{ + sptr listener; + DisplayManagerLite::GetInstance().pImpl_->screenMagneticStateListenerAgent_ = nullptr; + sptr impl_; + sptr screenMagneticStateListenerAgent = + new DisplayManagerLite::Impl::DisplayManagerScreenMagneticStateAgent(impl_); + auto ret = DisplayManagerLite::GetInstance().pImpl_->RegisterScreenMagneticStateListener(listener); + ASSERT_EQ(ret, SingletonContainer::Get().RegisterDisplayManagerAgent( + screenMagneticStateListenerAgent, + DisplayManagerAgentType::SCREEN_MAGNETIC_STATE_CHANGED_LISTENER)); + listener.clear(); + screenMagneticStateListenerAgent.clear(); +} + +/** + * @tc.name: UnregisterScreenMagneticStateListener + * @tc.desc: UnregisterScreenMagneticStateListener fun + * @tc.type: FUNC + */ +HWTEST_F(DisplayManagerTest, UnregisterScreenMagneticStateListener, Function | SmallTest | Level1) +{ + sptr listener; + auto ret = DisplayManagerLite::GetInstance().UnregisterScreenMagneticStateListener(listener); + ASSERT_EQ(ret, DMError::DM_ERROR_NULLPTR); + listener = new DisplayManagerLite::IScreenMagneticStateListener(); + ret = DisplayManagerLite::GetInstance().UnregisterScreenMagneticStateListener(listener); + ASSERT_EQ(ret, DisplayManagerLite::GetInstance().pImpl_->UnregisterScreenMagneticStateListener(listener)); + listener.clear(); +} + +/** + * @tc.name: ImplUnregisterScreenMagneticStateListener + * @tc.desc: ImplUnregisterScreenMagneticStateListener fun + * @tc.type: FUNC + */ +HWTEST_F(DisplayManagerTest, ImplUnregisterScreenMagneticStateListener, Function | SmallTest | Level1) +{ + sptr listener; + auto ret = DisplayManagerLite::GetInstance().pImpl_->UnregisterScreenMagneticStateListener(listener); + ASSERT_EQ(ret, DMError::DM_OK); + listener.clear(); +} + } } // namespace Rosen } // namespace OHOS \ No newline at end of file diff --git a/interfaces/innerkits/dm_lite/display_manager_lite.h b/interfaces/innerkits/dm_lite/display_manager_lite.h index 30f74db375..4b051920df 100644 --- a/interfaces/innerkits/dm_lite/display_manager_lite.h +++ b/interfaces/innerkits/dm_lite/display_manager_lite.h @@ -67,6 +67,16 @@ public: virtual void OnDisplayModeChanged([[maybe_unused]]FoldDisplayMode displayMode) {} }; + class IScreenMagneticStateListener : public virtual RefBase { + public: + /** + * @brief Notify listeners when screen magnetic state changed. + * + * @param screenMagneticState ScreenMagneticState. + */ + virtual void OnScreenMagneticStateChanged([[maybe_unused]]bool isMagneticState) {} + }; + /** * @brief Register a display listener. * @@ -115,6 +125,22 @@ public: */ DMError UnregisterDisplayModeListener(sptr listener); + /** + * @brief Register a listener for the event of screen magnetic state changed. + * + * @param listener IScreenMagneticStateListener. + * @return DM_OK means register success, others means register failed. + */ + DMError RegisterScreenMagneticStateListener(sptr listener); + + /** + * @brief Unregister an existed listener for the event of screen magnetic state changed. + * + * @param listener IScreenMagneticStateListener. + * @return DM_OK means unregister success, others means unregister failed. + */ + DMError UnregisterScreenMagneticStateListener(sptr listener); + /** * @brief Get the default display object. * @@ -225,7 +251,7 @@ public: * @return State of display. */ DisplayState GetDisplayState(DisplayId displayId); - + /** * @brief Try to cancel screenoff action before display power off. *