From 9e3bade24ee96b89b288a58e4916f71a66cb5abf Mon Sep 17 00:00:00 2001 From: fangJinliang1 Date: Fri, 19 May 2023 10:44:45 +0800 Subject: [PATCH] auto delete notification Signed-off-by: fangJinliang1 Change-Id: Ie606bcf1d04de631037776d2c0736a8eee8f2fa8 Signed-off-by: fangJinliang1 --- frameworks/test/moduletest/BUILD.gn | 4 ++ services/ans/BUILD.gn | 2 + .../include/advanced_notification_service.h | 9 +++ .../ans/src/advanced_notification_service.cpp | 71 +++++++++++++++++++ services/ans/test/unittest/BUILD.gn | 3 + .../advanced_notification_service_test.cpp | 31 ++++++++ services/test/moduletest/BUILD.gn | 1 + .../notification_services_test/BUILD.gn | 1 + 8 files changed, 122 insertions(+) diff --git a/frameworks/test/moduletest/BUILD.gn b/frameworks/test/moduletest/BUILD.gn index 79e1e6ec7..810e1128a 100644 --- a/frameworks/test/moduletest/BUILD.gn +++ b/frameworks/test/moduletest/BUILD.gn @@ -62,6 +62,7 @@ ohos_moduletest("ans_fw_module_test") { "${frameworks_module_ans_path}:ans_innerkits", "${services_path}/ans:libans", "//third_party/googletest:gtest_main", + "//third_party/libuv:uv", ] defines = [] @@ -125,6 +126,7 @@ ohos_moduletest("ans_innerkits_module_publish_test") { "${frameworks_module_ans_path}:ans_innerkits", "${services_path}/ans:libans", "//third_party/googletest:gtest_main", + "//third_party/libuv:uv", ] defines = [] @@ -190,6 +192,7 @@ ohos_moduletest("ans_innerkits_module_slot_test") { "${frameworks_module_ans_path}:ans_innerkits", "${services_path}/ans:libans", "//third_party/googletest:gtest_main", + "//third_party/libuv:uv", ] defines = [] @@ -254,6 +257,7 @@ ohos_moduletest("ans_innerkits_module_setting_test") { "${frameworks_module_ans_path}:ans_innerkits", "${services_path}/ans:libans", "//third_party/googletest:gtest_main", + "//third_party/libuv:uv", ] defines = [] diff --git a/services/ans/BUILD.gn b/services/ans/BUILD.gn index b480e670a..a8948065d 100644 --- a/services/ans/BUILD.gn +++ b/services/ans/BUILD.gn @@ -56,6 +56,8 @@ ohos_shared_library("libans") { deps = [ "${arkui_ace_engine_path}/interfaces/inner_api/ui_service_manager:ui_service_mgr", "${core_path}:ans_core", + "//foundation/arkui/ace_engine/interfaces/inner_api/ui_service_manager:ui_service_mgr", + "//third_party/libuv:uv", ] if (is_double_framework) { diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 5b3e0fde3..fd75cc4af 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -20,6 +20,7 @@ #include #include #include +#include #include "event_handler.h" #include "event_runner.h" @@ -839,6 +840,8 @@ private: ErrCode PublishNotificationBySa(const sptr &request); bool IsNeedPushCheck(NotificationConstant::SlotType slotType); ErrCode PushCheck(const sptr &request); + void StartAutoDelete(); + void TriggerAutoDelete(); private: static sptr instance_; static std::mutex instanceMutex_; @@ -858,6 +861,12 @@ private: NotificationConstant::DistributedReminderPolicy distributedReminderPolicy_ = DEFAULT_DISTRIBUTED_REMINDER_POLICY; bool localScreenOn_ = true; #endif + + std::shared_ptr autoDelRunner_ = nullptr; + std::shared_ptr autoDelHandler_ = nullptr; + uv_timer_t timerReq_; + uv_loop_t *loop_ = nullptr; + bool uvTimerRunning_ = false; }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 0272ff621..16797fe19 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -81,6 +81,7 @@ constexpr int32_t DIALOG_DEFAULT_HEIGHT = 240; constexpr int32_t WINDOW_DEFAULT_WIDTH = 720; constexpr int32_t WINDOW_DEFAULT_HEIGHT = 1280; constexpr int32_t UI_HALF = 2; +constexpr int32_t AUTO_DELETE_INTERVAL = 1000; constexpr char HIDUMPER_HELP_MSG[] = "Usage:dump [options]\n" @@ -440,6 +441,73 @@ ErrCode AdvancedNotificationService::PrepareNotificationInfo( return ERR_OK; } +void AdvancedNotificationService::TriggerAutoDelete() +{ + bool needStop = true; + for (auto iter = notificationList_.begin(); iter != notificationList_.end();) { + std::shared_ptr record = *iter; + if (!record || !record->request) { + continue; + } + + if (!record->request->IsTapDismissed()) { + iter++; + continue; + } + int64_t autoDeleteTime = record->request->GetAutoDeletedTime() / 1000; // 1000: us to ms + if (autoDeleteTime <= time(nullptr)) { + int32_t reason = NotificationConstant::APP_CANCEL_REASON_DELETE; + UpdateRecentNotification(record->notification, true, reason); + sptr sortingMap = GenerateSortingMap(); + NotificationSubscriberManager::GetInstance()->NotifyCanceled(record->notification, sortingMap, reason); + record->request->SetTapDismissed(false); + iter = notificationList_.erase(iter); + } else { + iter++; + } + needStop = false; + } + + if (needStop) { + ANS_LOGD("No auto delete notification, stop timer."); + uvTimerRunning_ = false; + uv_timer_stop(&timerReq_); + } +} + +void AdvancedNotificationService::StartAutoDelete() +{ + ANS_LOGD("enter"); + if (!autoDelHandler_) { + autoDelRunner_ = OHOS::AppExecFwk::EventRunner::Create("NotificationAutoDel"); + autoDelHandler_ = std::make_shared(autoDelRunner_); + if (!autoDelHandler_) { + ANS_LOGE("Failed to create handler."); + return; + } + AnsWatchdog::AddHandlerThread(autoDelHandler_, autoDelRunner_); + + loop_ = uv_default_loop(); + uv_timer_init(loop_, &timerReq_); + timerReq_.data = this; + } + + autoDelHandler_->PostTask(std::bind([&]() { + if (uvTimerRunning_) { + return; + } + uv_timer_start(&timerReq_, [](uv_timer_t* timer) { + auto notificationService = static_cast(timer->data); + if (notificationService) { + notificationService->TriggerAutoDelete(); + } + }, 0, AUTO_DELETE_INTERVAL); + + uvTimerRunning_ = true; + uv_run(loop_, UV_RUN_DEFAULT); + })); +} + ErrCode AdvancedNotificationService::PublishPreparedNotification( const sptr &request, const sptr &bundleOption) { @@ -474,6 +542,9 @@ ErrCode AdvancedNotificationService::PublishPreparedNotification( return; } UpdateRecentNotification(record->notification, false, 0); + if (record->request->IsTapDismissed()) { + StartAutoDelete(); + } sptr sortingMap = GenerateSortingMap(); ReportInfoToResourceSchedule(request->GetCreatorUserId(), bundleOption->GetBundleName()); NotificationSubscriberManager::GetInstance()->NotifyConsumed(record->notification, sortingMap); diff --git a/services/ans/test/unittest/BUILD.gn b/services/ans/test/unittest/BUILD.gn index d4c688151..0212a61cd 100644 --- a/services/ans/test/unittest/BUILD.gn +++ b/services/ans/test/unittest/BUILD.gn @@ -134,6 +134,7 @@ ohos_unittest("ans_unit_test") { "${frameworks_module_ans_path}:ans_innerkits", "${services_path}/ans:libans", "//third_party/googletest:gtest_main", + "//third_party/libuv:uv", ] if (distributed_notification_supported) { @@ -362,6 +363,7 @@ ohos_unittest("notification_subscriber_manager_branch_test") { "${frameworks_module_ans_path}:ans_innerkits", "${services_path}/ans:libans", "//third_party/googletest:gtest_main", + "//third_party/libuv:uv", ] if (distributed_notification_supported) { @@ -431,6 +433,7 @@ ohos_unittest("advanced_notification_service_branch_test") { "${frameworks_module_ans_path}:ans_innerkits", "${services_path}/ans:libans", "//third_party/googletest:gtest_main", + "//third_party/libuv:uv", ] if (distributed_notification_supported) { diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index 0c44df22a..eb314dcf0 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -3531,5 +3531,36 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_220000 std::string result = advancedNotificationService_->TimeToString(time); EXPECT_EQ(result.substr(0, result.size() - 8), ret); } + +/** + * @tc.number : AdvancedNotificationServiceTest_22100 + * @tc.name : StartAutoDelete + * @tc.desc : Test StartAutoDelete function. + * @tc.require : #I61RF2 + */ +HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_221000, Function | SmallTest | Level1) +{ + GTEST_LOG_(INFO) << "AdvancedNotificationServiceTest_221000 test start"; + + sptr request = new (std::nothrow) NotificationRequest(); + EXPECT_NE(request, nullptr); + request->SetCreatorUid(0); + request->SetCreatorUserId(0); + request->SetLabel("label"); + request->SetTapDismissed(true); + request->SetAutoDeletedTime(time(nullptr) + 5); + sptr notification = new (std::nothrow) Notification(request); + EXPECT_NE(notification, nullptr); + + auto record = std::make_shared(); + record->request = request; + record->notification = notification; + advancedNotificationService_->notificationList_.push_back(record); + advancedNotificationService_->StartAutoDelete(); + + std::this_thread::sleep_for(std::chrono::seconds(5)); + EXPECT_EQ(advancedNotificationService_->notificationList_.size(), 0); + GTEST_LOG_(INFO) << "AdvancedNotificationServiceTest_221000 test end"; +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/test/moduletest/BUILD.gn b/services/test/moduletest/BUILD.gn index 55ed2db7a..f778ee4fb 100644 --- a/services/test/moduletest/BUILD.gn +++ b/services/test/moduletest/BUILD.gn @@ -60,6 +60,7 @@ ohos_moduletest("ans_module_test") { "${core_path}:ans_core", "${frameworks_module_ans_path}:ans_innerkits", "${services_path}/ans:libans", + "//third_party/libuv:uv", ] external_deps = [ diff --git a/test/bechmarktest/notification_services_test/BUILD.gn b/test/bechmarktest/notification_services_test/BUILD.gn index 2d9f0f5ae..db32fa020 100644 --- a/test/bechmarktest/notification_services_test/BUILD.gn +++ b/test/bechmarktest/notification_services_test/BUILD.gn @@ -36,6 +36,7 @@ ohos_benchmarktest("Benchmark_Notification_Servece_Test") { "${services_path}/ans:libans", "//third_party/benchmark:benchmark", "//third_party/googletest:gtest_main", + "//third_party/libuv:uv", ] external_deps = [ -- Gitee