diff --git a/frameworks/test/moduletest/BUILD.gn b/frameworks/test/moduletest/BUILD.gn index 79e1e6ec78074e0fdb85ef82dfd0209ae9ddfdb6..810e1128a5a55577c726190cbe048843fb37ed3c 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 b480e670a777d923d39c2bbd2b3626b278b4cecc..a8948065dbd954c8bd7a5121087d5ef207fc53e8 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 5b3e0fde33177847d17606a2eaf7bb8eb92dfba7..fd75cc4afe598276251046dd633d9307725e8264 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 0272ff6210e8e5375d71b8b855187a37ed43cc6a..16797fe1927373d09098232ae64bae135bf19c8c 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 d4c688151bd400e24984d5057fe81dab1a6a5bcc..0212a61cdc0e89e193c8a49f8eda32ae2a6e81dc 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 0c44df22a49e542fa9ecdd65c85635556b93fe6a..eb314dcf074d659583f50dc65f655b8eda8f5395 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 55ed2db7a4c87e830f228e0dfd310dd63de98bdb..f778ee4fb5ebdd04a623fd881a4e14eb7cc447a2 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 2d9f0f5ae01541c341de3d0fa1f9cf4c1b49ef84..db32fa0204dcc5c9a1b250ba1bb3b8a3d06f354f 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 = [