diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 69fc2cece3b23e3c290ac1d07c94785273afe749..ab52e436418e5317f1f8709e35d0118bbfdd943b 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -136,6 +136,7 @@ public: void OnScreenOff(); #endif void OnResourceRemove(int32_t userId); + void OnPackageDataCleared(const sptr &bundleOption); // Distributed KvStore void OnDistributedKvStoreDeathRecipient(); diff --git a/services/ans/include/interface_system_event.h b/services/ans/include/interface_system_event.h index a68618e0894a2f0499ba3ddca76451346acbdb1f..92f58dec7ca0e8a89f42871d4a611bf10eaba067 100644 --- a/services/ans/include/interface_system_event.h +++ b/services/ans/include/interface_system_event.h @@ -30,6 +30,7 @@ struct ISystemEvent { std::function onScreenOff; #endif std::function onResourceRemove; + std::function &)> onPackageDataCleared; }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/include/system_event_observer.h b/services/ans/include/system_event_observer.h index 382959c4f802efe02da74dfd433497a4131c8777..f01c53893eddf3589c012b79229b043fafd437ea 100644 --- a/services/ans/include/system_event_observer.h +++ b/services/ans/include/system_event_observer.h @@ -33,6 +33,7 @@ public: private: void OnReceiveEvent(const EventFwk::CommonEventData &data); + sptr GetBundleOption(AAFwk::Want want); private: std::shared_ptr subscriber_ = nullptr; diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index ddb0d68ed653351860875951b0e9ca7d21fca603..3830fc129ab4bd58ff9dda0fd62f595cf8cecb14 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -246,6 +246,7 @@ AdvancedNotificationService::AdvancedNotificationService() std::bind(&AdvancedNotificationService::OnScreenOff, this), #endif std::bind(&AdvancedNotificationService::OnResourceRemove, this, std::placeholders::_1), + std::bind(&AdvancedNotificationService::OnPackageDataCleared, this, std::placeholders::_1), }; systemEventObserver_ = std::make_shared(iSystemEvent); @@ -3250,5 +3251,33 @@ void AdvancedNotificationService::OnResourceRemove(int32_t userId) NotificationPreferences::GetInstance().RemoveSettings(userId); })); } + +void AdvancedNotificationService::OnPackageDataCleared(const sptr &bundleOption) +{ + handler_->PostSyncTask(std::bind([&]() { + std::vector keys = GetNotificationKeys(bundleOption); + for (auto key : keys) { +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + std::string deviceId = GetNotificationDeviceId(key); +#endif + sptr notification = nullptr; + + ErrCode result = RemoveFromNotificationList(key, notification); + if (result != ERR_OK) { + continue; + } + + if (notification != nullptr) { + int reason = NotificationConstant::CANCEL_REASON_DELETE; + UpdateRecentNotification(notification, true, reason); + sptr sortingMap = GenerateSortingMap(); + NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, sortingMap, reason); +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + DoDistributedDelete(deviceId, notification); +#endif + } + } + })); +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/system_event_observer.cpp b/services/ans/src/system_event_observer.cpp index 3a1874b82063389e8afa6f732f75b08cc450cd06..fd3656ba04fa37c90989168cfa4f3f2a4c863ce3 100644 --- a/services/ans/src/system_event_observer.cpp +++ b/services/ans/src/system_event_observer.cpp @@ -33,6 +33,7 @@ SystemEventObserver::SystemEventObserver(const ISystemEvent &callbacks) : callba #endif matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED); EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills); subscriber_ = std::make_shared( @@ -46,6 +47,18 @@ SystemEventObserver::~SystemEventObserver() EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_); } +sptr SystemEventObserver::GetBundleOption(AAFwk::Want want) +{ + auto element = want.GetElement(); + std::string bundleName = element.GetBundleName(); + int uid = want.GetIntParam(AppExecFwk::Constants::UID, -1); + sptr bundleOption = new NotificationBundleOption(bundleName, uid); + if (bundleOption == nullptr) { + ANS_LOGE("Failed to create bundleOption."); + } + return bundleOption; +} + void SystemEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &data) { auto want = data.GetWant(); @@ -53,11 +66,10 @@ void SystemEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &data) ANS_LOGD("OnReceiveEvent action is %{public}s.", action.c_str()); if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) { if (callbacks_.onBundleRemoved != nullptr) { - auto element = want.GetElement(); - std::string bundleName = element.GetBundleName(); - int uid = want.GetIntParam(AppExecFwk::Constants::UID, -1); - sptr bundleOption = new NotificationBundleOption(bundleName, uid); - callbacks_.onBundleRemoved(bundleOption); + sptr bundleOption = GetBundleOption(want); + if (bundleOption != nullptr) { + callbacks_.onBundleRemoved(bundleOption); + } } #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) { @@ -73,7 +85,16 @@ void SystemEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &data) NotificationPreferences::GetInstance().InitSettingFromDisturbDB(); } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) { int32_t userId = data.GetCode(); - callbacks_.onResourceRemove(userId); + if (callbacks_.onResourceRemove != nullptr) { + callbacks_.onResourceRemove(userId); + } + } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) { + if (callbacks_.onPackageDataCleared != nullptr) { + sptr bundleOption = GetBundleOption(want); + if (bundleOption != nullptr) { + callbacks_.onPackageDataCleared(bundleOption); + } + } } } } // namespace Notification