diff --git a/frameworks/core/common/include/ans_convert_enum.h b/frameworks/core/common/include/ans_convert_enum.h index 8937117521732c1207c3775c5d9730cafce5a8a4..eb1a5961a038418088d069ea1291fc4895724759 100644 --- a/frameworks/core/common/include/ans_convert_enum.h +++ b/frameworks/core/common/include/ans_convert_enum.h @@ -87,6 +87,7 @@ enum class RemoveReason { RECOVER_LIVE_VIEW_DELETE = 30, DISABLE_NOTIFICATION_FEATURE_REASON_DELETE = 31, DISTRIBUTED_COLLABORATIVE_DELETE = 32, + USER_LOGOUT_REASON_DELETE = 33, APP_CANCEL_REASON_OTHER = 100, }; diff --git a/frameworks/core/common/src/ans_convert_enum.cpp b/frameworks/core/common/src/ans_convert_enum.cpp index d50f08bf94335cea7b0f0e215f7a4a41580bd130..8bde20522ebe7195ae2494e239d8b320dfcc0e66 100644 --- a/frameworks/core/common/src/ans_convert_enum.cpp +++ b/frameworks/core/common/src/ans_convert_enum.cpp @@ -282,6 +282,9 @@ bool AnsEnumUtil::ReasonCToJS(const int &inType, int &outType) case NotificationConstant::FLOW_CONTROL_REASON_DELETE: outType = static_cast(RemoveReason::FLOW_CONTROL_REASON_DELETE); break; + case NotificationConstant::USER_LOGOUT_REASON_DELETE: + outType = static_cast(RemoveReason::USER_LOGOUT_REASON_DELETE); + break; default: ReasonCToJSExt(inType, outType); break; diff --git a/interfaces/inner_api/notification_constant.h b/interfaces/inner_api/notification_constant.h index 8e6bfb2de3e25d9b2f5f0492e58e1661ad74ddfb..909b908e4d521531677c526d36748cac7290f60c 100644 --- a/interfaces/inner_api/notification_constant.h +++ b/interfaces/inner_api/notification_constant.h @@ -307,6 +307,11 @@ public: */ static const int32_t DISTRIBUTED_COLLABORATIVE_DELETE = 32; + /** + * Indicates that a notification is deleted because this user is removed. + */ + static const int32_t USER_LOGOUT_REASON_DELETE = 33; + /** * Indicates that a notification is deleted for other reasons. */ diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 697c470a4477c7f15fb10d4c1de601137aec24b4..895da0565821731717bb17ff0294f2beae498ff4 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -816,6 +816,7 @@ public: void OnScreenOff(); #endif void OnResourceRemove(int32_t userId); + void OnUserStopped(int32_t userId); void OnBundleDataCleared(const sptr &bundleOption); /** @@ -1258,7 +1259,7 @@ private: ErrCode RemoveFromNotificationList(const std::string &key, sptr ¬ification, bool isCancel, int32_t removeReason); ErrCode RemoveFromNotificationListForDeleteAll(const std::string &key, - const int32_t &userId, sptr ¬ification); + const int32_t &userId, sptr ¬ification, bool removeAll = false); bool RemoveFromDelayedNotificationList(const std::string &key); std::shared_ptr GetFromNotificationList(const std::string &key); std::shared_ptr GetFromNotificationList(const int32_t ownerUid, const int32_t notificationId); @@ -1426,7 +1427,7 @@ private: static ErrCode SetLockScreenPictureToDb(const sptr &request); static ErrCode GetLockScreenPictureFromDb(NotificationRequest *request); void RemoveDoNotDisturbProfileTrustList(const sptr &bundleOption); - ErrCode DeleteAllByUserInner(const int32_t &userId, int32_t reason, bool isAsync = false); + ErrCode DeleteAllByUserInner(const int32_t &userId, int32_t reason, bool isAsync = false, bool removeAll = false); ErrCode RemoveAllNotificationsInner(const sptr &bundleOption, int32_t reason); ErrCode AssignValidNotificationSlot(const std::shared_ptr &record, const sptr &bundleOption); diff --git a/services/ans/include/interface_system_event.h b/services/ans/include/interface_system_event.h index 71cf783dae1271b5a32e863e70e4dabe93a19889..063ca7123c9f1472be0ae2bda5466d3ceeb4f397 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 OnUserStopped; std::function &)> onBundleDataCleared; std::function &)> onBundleAdd; std::function &)> onBundleUpdate; diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 958b9bae6d0662034f52c9280fad720cfd31f750..8dfd4ebc5c326b28fe92d57adb038f4c4e41fcc1 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -336,6 +336,7 @@ AdvancedNotificationService::AdvancedNotificationService() std::bind(&AdvancedNotificationService::OnScreenOff, this), #endif std::bind(&AdvancedNotificationService::OnResourceRemove, this, std::placeholders::_1), + std::bind(&AdvancedNotificationService::OnUserStopped, this, std::placeholders::_1), std::bind(&AdvancedNotificationService::OnBundleDataCleared, this, std::placeholders::_1), std::bind(&AdvancedNotificationService::OnBundleDataAdd, this, std::placeholders::_1), std::bind(&AdvancedNotificationService::OnBundleDataUpdate, this, std::placeholders::_1), @@ -1475,15 +1476,15 @@ ErrCode AdvancedNotificationService::RemoveFromNotificationList( } ErrCode AdvancedNotificationService::RemoveFromNotificationListForDeleteAll( - const std::string &key, const int32_t &userId, sptr ¬ification) + const std::string &key, const int32_t &userId, sptr ¬ification, bool removeAll) { for (auto record : notificationList_) { if ((record->notification->GetKey() == key) && (record->notification->GetUserId() == userId)) { - if (!record->notification->IsRemoveAllowed()) { + if (!record->notification->IsRemoveAllowed() && !removeAll) { return ERR_ANS_NOTIFICATION_IS_UNALLOWED_REMOVEALLOWED; } - if (record->request->IsUnremovable()) { + if (record->request->IsUnremovable() && !removeAll) { return ERR_ANS_NOTIFICATION_IS_UNREMOVABLE; } diff --git a/services/ans/src/advanced_notification_utils.cpp b/services/ans/src/advanced_notification_utils.cpp index 74c1c681617c50715d25781197183e239ff951ea..a745f55d4830a1447fe60f5b5001e6deef76ff33 100644 --- a/services/ans/src/advanced_notification_utils.cpp +++ b/services/ans/src/advanced_notification_utils.cpp @@ -1253,6 +1253,11 @@ void AdvancedNotificationService::OnUserRemoved(const int32_t &userId) DeleteAllByUserInner(userId, NotificationConstant::USER_REMOVED_REASON_DELETE, true); } +void AdvancedNotificationService::OnUserStopped(int32_t userId) +{ + DeleteAllByUserInner(userId, NotificationConstant::USER_LOGOUT_REASON_DELETE, true, true); +} + ErrCode AdvancedNotificationService::DeleteAllByUser(const int32_t &userId) { bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); @@ -1272,7 +1277,7 @@ ErrCode AdvancedNotificationService::DeleteAllByUser(const int32_t &userId) } ErrCode AdvancedNotificationService::DeleteAllByUserInner(const int32_t &userId, int32_t deleteReason, - bool isAsync) + bool isAsync, bool removeAll) { ANS_LOGD("%{public}s", __FUNCTION__); @@ -1304,7 +1309,7 @@ ErrCode AdvancedNotificationService::DeleteAllByUserInner(const int32_t &userId, #endif sptr notification = nullptr; - *result = RemoveFromNotificationListForDeleteAll(key, userId, notification); + *result = RemoveFromNotificationListForDeleteAll(key, userId, notification, removeAll); if ((*result != ERR_OK) || (notification == nullptr)) { continue; } diff --git a/services/ans/src/system_event_observer.cpp b/services/ans/src/system_event_observer.cpp index 7c03a469a3f8897e77c6cb3927b79ccbd1318223..db7602425ddbf2650630d93300ca87cfe7bb390a 100644 --- a/services/ans/src/system_event_observer.cpp +++ b/services/ans/src/system_event_observer.cpp @@ -40,6 +40,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_USER_STOPPED); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED); @@ -133,6 +134,15 @@ void SystemEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &data) if (callbacks_.onResourceRemove != nullptr) { callbacks_.onResourceRemove(userId); } + } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPED) { + int32_t userId = data.GetCode(); + if (userId <= SUBSCRIBE_USER_INIT) { + ANS_LOGE("Illegal userId, userId[%{public}d].", userId); + return; + } + if (callbacks_.OnUserStopped != nullptr) { + callbacks_.OnUserStopped(userId); + } } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) { if (callbacks_.onBundleDataCleared != nullptr) { sptr bundleOption = GetBundleOptionDataCleared(want); diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index e75a9d904dbea2a91a405c5a5a9da37c7357dff5..69b4724e5407d4482c83c317c0c084a2eda3d87a 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -1882,6 +1882,31 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_20600, GTEST_LOG_(INFO) << "OnResourceRemove_0100 test end"; } +/** + * @tc.number : OnUserStopped_0100 + * @tc.name : OnUserStopped_0100 + * @tc.desc : Test OnUserStopped function + * @tc.require : #I61RF2 + */ +HWTEST_F(AdvancedNotificationServiceTest, OnUserStopped_0100, Function | SmallTest | Level1) +{ + GTEST_LOG_(INFO) << "OnResourceRemove_0100 test start"; + int32_t userId = 2; + sptr request = new (std::nothrow) NotificationRequest(); + request->SetCreatorUserId(userId); + sptr notification = new (std::nothrow) Notification(request); + auto record = std::make_shared(); + record->request = request; + record->notification = notification; + advancedNotificationService_->PublishInNotificationList(record); + + MockIsSystemApp(true); + advancedNotificationService_->OnUserStopped(userId); + ASSERT_EQ(advancedNotificationService_->notificationList_.size(), 0); + + GTEST_LOG_(INFO) << "OnResourceRemove_0100 test end"; +} + /** * @tc.number : AdvancedNotificationServiceTest_20700 * @tc.name : OnBundleDataCleared_0100