From bb76ff34e9fa167e31f60d99999e25316ce05800 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 8 Mar 2022 22:12:10 +0800 Subject: [PATCH] remove notification when app clean data Signed-off-by: root Change-Id: I05ef3ed33ae12b9973018b6065730f64e08448b7 --- .../include/advanced_notification_service.h | 1 + services/ans/include/interface_system_event.h | 1 + services/ans/include/system_event_observer.h | 1 + .../ans/src/advanced_notification_service.cpp | 29 ++++++++++++++++ services/ans/src/system_event_observer.cpp | 33 +++++++++++++++---- 5 files changed, 59 insertions(+), 6 deletions(-) diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 69fc2cece..ab52e4364 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 a68618e08..92f58dec7 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 382959c4f..f01c53893 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 ddb0d68ed..3830fc129 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 3a1874b82..fd3656ba0 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 -- Gitee