diff --git a/interfaces/innerkits/wm/window.h b/interfaces/innerkits/wm/window.h index 34cfaf43..620a76b1 100644 --- a/interfaces/innerkits/wm/window.h +++ b/interfaces/innerkits/wm/window.h @@ -455,6 +455,14 @@ public: * @return std::shared_ptr snapshot pixel */ virtual std::shared_ptr Snapshot() = 0; + + /** + * @brief Handle and notify memory level. + * + * @param level memory level + * @return the error code of window + */ + virtual WMError NotifyMemoryLevel(int32_t level) const = 0; }; } } diff --git a/interfaces/innerkits/wm/window_scene.h b/interfaces/innerkits/wm/window_scene.h index cbaa0bf6..a355b500 100644 --- a/interfaces/innerkits/wm/window_scene.h +++ b/interfaces/innerkits/wm/window_scene.h @@ -137,6 +137,14 @@ public: */ std::string GetContentInfo() const; + /** + * @brief Handle and notify memory. + * + * @param level memory level + * @return the error code of window + */ + WMError NotifyMemoryLevel(int32_t level) const; + public: static const DisplayId DEFAULT_DISPLAY_ID = 0; static const std::string MAIN_WINDOW_ID; diff --git a/wm/include/window_impl.h b/wm/include/window_impl.h index 253d73a4..605775d7 100644 --- a/wm/include/window_impl.h +++ b/wm/include/window_impl.h @@ -249,6 +249,7 @@ public: virtual std::shared_ptr Snapshot() override; void PostListenerTask(ListenerTaskCallback &&callback, Priority priority = Priority::LOW, const std::string taskName = ""); + virtual WMError NotifyMemoryLevel(int32_t level) const override; private: inline std::vector> GetLifecycleListeners() diff --git a/wm/src/window_impl.cpp b/wm/src/window_impl.cpp index 9b48b175..75d69724 100755 --- a/wm/src/window_impl.cpp +++ b/wm/src/window_impl.cpp @@ -2803,5 +2803,16 @@ WMError WindowImpl::SetBackdropBlurStyle(WindowBlurStyle blurStyle) } return WMError::WM_OK; } + +WMError WindowImpl::NotifyMemoryLevel(int32_t level) const +{ + if (uiContent_ == nullptr) { + WLOGFE("[Client] Window %{public}s notify memory level failed, because uicontent is null.", name_.c_str()); + return WMError::WM_ERROR_NULLPTR; + } + // notify memory level + uiContent_->NotifyMemoryLevel(level); + return WMError::WM_OK; +} } // namespace Rosen } // namespace OHOS diff --git a/wm/src/window_scene.cpp b/wm/src/window_scene.cpp index c959f9dd..66ddcd0f 100644 --- a/wm/src/window_scene.cpp +++ b/wm/src/window_scene.cpp @@ -92,7 +92,7 @@ const sptr& WindowScene::GetMainWindow() const std::vector> WindowScene::GetSubWindow() { if (mainWindow_ == nullptr) { - WLOGFE("WindowScene mainWindow_ is nullptr"); + WLOGFE("Get sub window failed, because main window is null"); return std::vector>(); } uint32_t parentId = mainWindow_->GetWindowId(); @@ -103,6 +103,7 @@ WMError WindowScene::GoForeground(uint32_t reason) { WLOGFI("GoForeground reason:%{public}u", reason); if (mainWindow_ == nullptr) { + WLOGFE("Go foreground failed, because main window is null"); return WMError::WM_ERROR_NULLPTR; } return mainWindow_->Show(reason); @@ -112,6 +113,7 @@ WMError WindowScene::GoBackground(uint32_t reason) { WLOGFI("GoBackground reason:%{public}u", reason); if (mainWindow_ == nullptr) { + WLOGFE("Go background failed, because main window is null"); return WMError::WM_ERROR_NULLPTR; } return mainWindow_->Hide(reason); @@ -125,7 +127,7 @@ WMError WindowScene::GoDestroy() WMError ret = mainWindow_->Destroy(); if (ret != WMError::WM_OK) { - WLOGFE("WindowScene GoDestroy Failed Name: %{public}s", mainWindow_->GetWindowName().c_str()); + WLOGFE("WindowScene go destroy failed name: %{public}s", mainWindow_->GetWindowName().c_str()); return ret; } mainWindow_ = nullptr; @@ -135,6 +137,7 @@ WMError WindowScene::GoDestroy() WMError WindowScene::OnNewWant(const AAFwk::Want& want) { if (mainWindow_ == nullptr) { + WLOGFE("On new want failed, because main window is null"); return WMError::WM_ERROR_NULLPTR; } mainWindow_->OnNewWant(want); @@ -144,6 +147,7 @@ WMError WindowScene::OnNewWant(const AAFwk::Want& want) WMError WindowScene::SetSystemBarProperty(WindowType type, const SystemBarProperty& property) const { if (mainWindow_ == nullptr) { + WLOGFE("Set systembar property failed, because main window is null"); return WMError::WM_ERROR_NULLPTR; } return mainWindow_->SetSystemBarProperty(type, property); @@ -152,6 +156,7 @@ WMError WindowScene::SetSystemBarProperty(WindowType type, const SystemBarProper WMError WindowScene::RequestFocus() const { if (mainWindow_ == nullptr) { + WLOGFE("Request focus failed, because main window is null"); return WMError::WM_ERROR_NULLPTR; } return mainWindow_->RequestFocus(); @@ -160,7 +165,7 @@ WMError WindowScene::RequestFocus() const void WindowScene::UpdateConfiguration(const std::shared_ptr& configuration) { if (mainWindow_ == nullptr) { - WLOGFE("mainWindow_ is null"); + WLOGFE("Update configuration failed, because main window is null"); return; } WLOGFI("notify mainWindow winId:%{public}u", mainWindow_->GetWindowId()); @@ -170,10 +175,19 @@ void WindowScene::UpdateConfiguration(const std::shared_ptrGetContentInfo(); } + +WMError WindowScene::NotifyMemoryLevel(int32_t level) const +{ + if (mainWindow_ == nullptr) { + WLOGFE("Notify memory level failed, because main window is null"); + return WMError::WM_ERROR_NULLPTR; + } + return mainWindow_->NotifyMemoryLevel(level); +} } // namespace Rosen } // namespace OHOS diff --git a/wm/test/unittest/window_scene_test.cpp b/wm/test/unittest/window_scene_test.cpp index 69cd205d..1d0c0ea1 100644 --- a/wm/test/unittest/window_scene_test.cpp +++ b/wm/test/unittest/window_scene_test.cpp @@ -216,6 +216,35 @@ HWTEST_F(WindowSceneTest, RequestFocus01, Function | SmallTest | Level2) sptr scene = new WindowScene(); ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->RequestFocus()); } + +/** + * @tc.name: NotifyMemoryLevel01 + * @tc.desc: NotifyMemoryLevel without mainWindow + * @tc.type: FUNC + */ +HWTEST_F(WindowSceneTest, NotifyMemoryLevel01, Function | SmallTest | Level2) +{ + sptr scene = new WindowScene(); + int32_t level = 0; + ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->NotifyMemoryLevel(level)); +} + +/** + * @tc.name: NotifyMemoryLevel02 + * @tc.desc: NotifyMemoryLevel with level + * @tc.type: FUNC + */ +HWTEST_F(WindowSceneTest, NotifyMemoryLevel02, Function | SmallTest | Level2) +{ + DisplayId displayId = 0; + std::unique_ptr m = std::make_unique(); + sptr listener = nullptr; + sptr scene = new WindowScene(); + sptr option = new WindowOption(); + EXPECT_CALL(m->Mock(), CreateWindow(_, _, _)).Times(1).WillOnce(Return(new WindowImpl(option))); + ASSERT_EQ(WMError::WM_OK, scene->Init(displayId, abilityContext_, listener)); + ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->NotifyMemoryLevel(0)); // ui content is null +} } } // namespace Rosen } // namespace OHOS \ No newline at end of file