From 58afe42df6f43ba15090a202c77a6e0f37dd77f3 Mon Sep 17 00:00:00 2001 From: "baozeyu1@huawei.com" Date: Fri, 4 Jul 2025 18:13:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=8F=E5=90=8C=E5=88=A0=E9=99=A4=E5=A4=9A?= =?UTF-8?q?=E6=89=8B=E6=9C=BA=E5=9C=BA=E6=99=AF=E5=85=9C=E5=BA=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: baozeyu1@huawei.com --- frameworks/ans/IAnsManager.idl | 2 +- frameworks/ans/src/notification_helper.cpp | 4 +- .../core/common/include/ans_const_define.h | 2 + frameworks/core/include/ans_notification.h | 2 +- frameworks/core/src/ans_notification.cpp | 5 +- .../ans_notification_branch_test.cpp | 2 +- .../unittest/mock/mock_ans_manager_proxy.h | 4 +- interfaces/inner_api/notification_constant.h | 1 + interfaces/inner_api/notification_helper.h | 2 +- .../include/advanced_notification_service.h | 4 +- .../advanced_notification_cancel.cpp | 54 ++++++++++++++++++- .../soft_bus/distributed_publish_service.h | 2 +- .../tlv_box/remove_all_distributed_box.h | 2 + .../distributed_publish_service_v2.cpp | 17 ++++-- .../src/soft_bus/distributed_service.cpp | 12 +++-- .../tlv_box/remove_all_distributed_box.cpp | 16 ++++++ tools/test/mock/mock_ans_manager_stub.h | 3 +- 17 files changed, 114 insertions(+), 20 deletions(-) diff --git a/frameworks/ans/IAnsManager.idl b/frameworks/ans/IAnsManager.idl index 9d447d582..982adcc51 100644 --- a/frameworks/ans/IAnsManager.idl +++ b/frameworks/ans/IAnsManager.idl @@ -105,7 +105,7 @@ interface OHOS.Notification.IAnsManager { void RemoveNotifications([in] String[] hashcodes, [in] int removeReason); - void RemoveDistributedNotifications([in] String[] hashcodes, [in] int slotTypeInt, [in] int deleteTypeInt, [in] int removeReason); + void RemoveDistributedNotifications([in] String[] hashcodes, [in] int slotTypeInt, [in] int deleteTypeInt, [in] int removeReason, [in] String deviceId); void Delete([in] String key, [in] int removeReason); diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index 6f548000e..5aaf74c97 100644 --- a/frameworks/ans/src/notification_helper.cpp +++ b/frameworks/ans/src/notification_helper.cpp @@ -311,10 +311,10 @@ ErrCode NotificationHelper::RemoveNotifications() ErrCode NotificationHelper::RemoveDistributedNotifications(const std::vector& hashcodes, const NotificationConstant::SlotType& slotType, const NotificationConstant::DistributedDeleteType& deleteType, - const int32_t removeReason) + const int32_t removeReason, const std::string& deviceId) { return DelayedSingleton::GetInstance()->RemoveDistributedNotifications( - hashcodes, slotType, deleteType, removeReason); + hashcodes, slotType, deleteType, removeReason, deviceId); } ErrCode NotificationHelper::GetNotificationSlotsForBundle( diff --git a/frameworks/core/common/include/ans_const_define.h b/frameworks/core/common/include/ans_const_define.h index c8dcc2afd..7977b9b39 100644 --- a/frameworks/core/common/include/ans_const_define.h +++ b/frameworks/core/common/include/ans_const_define.h @@ -71,6 +71,8 @@ const std::string DOWNLOAD_TEMPLATE_NAME = "downloadTemplate"; const std::string DEVICE_TYPE_WEARABLE = "wearable"; const std::string DEVICE_TYPE_LITE_WEARABLE = "liteWearable"; const std::string DEVICE_TYPE_HEADSET = "headset"; +const std::string ANS_EXTENDINFO_INFO_PRE = "notification_collaboration_"; +const std::string ANS_EXTENDINFO_DEVICE_ID = "deviceId"; #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED constexpr NotificationConstant::DistributedReminderPolicy DEFAULT_DISTRIBUTED_REMINDER_POLICY = diff --git a/frameworks/core/include/ans_notification.h b/frameworks/core/include/ans_notification.h index a57ccb8e4..c07787ad6 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -598,7 +598,7 @@ public: ErrCode RemoveDistributedNotifications(const std::vector& hashcodes, const NotificationConstant::SlotType& slotType, const NotificationConstant::DistributedDeleteType& deleteType, - const int32_t removeReason); + const int32_t removeReason, const std::string& deviceId = ""); /** * @brief Obtains all notification slots belonging to the specified bundle. diff --git a/frameworks/core/src/ans_notification.cpp b/frameworks/core/src/ans_notification.cpp index 7eda75b94..b63225d98 100644 --- a/frameworks/core/src/ans_notification.cpp +++ b/frameworks/core/src/ans_notification.cpp @@ -928,7 +928,7 @@ ErrCode AnsNotification::RemoveNotifications(const std::vector hash ErrCode AnsNotification::RemoveDistributedNotifications(const std::vector& hashcodes, const NotificationConstant::SlotType& slotType, const NotificationConstant::DistributedDeleteType& deleteType, - const int32_t removeReason) + const int32_t removeReason, const std::string& deviceId) { sptr proxy = GetAnsManagerProxy(); if (!proxy) { @@ -936,7 +936,8 @@ ErrCode AnsNotification::RemoveDistributedNotifications(const std::vectorRemoveDistributedNotifications(hashcodes, slotType, deleteType, removeReason); + return proxy->RemoveDistributedNotifications(hashcodes, slotType, deleteType, + removeReason, deviceId); } ErrCode AnsNotification::RemoveNotificationsByBundle(const NotificationBundleOption &bundleOption) diff --git a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp index f0a4de4b7..72952a343 100644 --- a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp +++ b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp @@ -194,7 +194,7 @@ public: ErrCode RemoveDistributedNotifications(const std::vector& hashcodes, const int32_t slotTypeInt, const int32_t deleteTypeInt, - const int32_t removeReason) override + const int32_t removeReason, const std::string& deviceId) override { return ERR_ANS_INVALID_PARAM; } diff --git a/frameworks/core/test/unittest/mock/mock_ans_manager_proxy.h b/frameworks/core/test/unittest/mock/mock_ans_manager_proxy.h index 9dc390c8e..7df27a3b7 100644 --- a/frameworks/core/test/unittest/mock/mock_ans_manager_proxy.h +++ b/frameworks/core/test/unittest/mock/mock_ans_manager_proxy.h @@ -182,8 +182,8 @@ public: ErrCode(const std::string&, const std::string&, int32_t, const std::vector&)); MOCK_METHOD4(SetTargetDeviceSwitch, ErrCode(const std::string&, const std::string&, bool, bool)); MOCK_METHOD1(SetHashCodeRule, ErrCode(uint32_t)); - MOCK_METHOD4(RemoveDistributedNotifications, ErrCode(const std::vector& hashcodes, - const int32_t, const int32_t, const int32_t)); + MOCK_METHOD5(RemoveDistributedNotifications, ErrCode(const std::vector& hashcodes, + const int32_t, const int32_t, const int32_t, const std::string&)); MOCK_METHOD2(SetSilentReminderEnabled, ErrCode(const sptr &bundleOption, const bool enabled)); MOCK_METHOD2(IsSilentReminderEnabled, ErrCode(const sptr &bundleOption, diff --git a/interfaces/inner_api/notification_constant.h b/interfaces/inner_api/notification_constant.h index 5b45966be..9d3d00323 100644 --- a/interfaces/inner_api/notification_constant.h +++ b/interfaces/inner_api/notification_constant.h @@ -83,6 +83,7 @@ public: SLOT, EXCLUDE_ONE_SLOT, HASHCODES, + DEVICE_ID, }; enum class VisiblenessType { diff --git a/interfaces/inner_api/notification_helper.h b/interfaces/inner_api/notification_helper.h index 2034bf837..8de9c6ada 100644 --- a/interfaces/inner_api/notification_helper.h +++ b/interfaces/inner_api/notification_helper.h @@ -666,7 +666,7 @@ public: static ErrCode RemoveDistributedNotifications(const std::vector& hashcodes, const NotificationConstant::SlotType& slotType, const NotificationConstant::DistributedDeleteType& deleteType, - const int32_t removeReason); + const int32_t removeReason, const std::string& deviceId = ""); /** * @brief Obtains all active notifications in the current system. The caller must have system permissions to diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 65e31518c..bc9bc2252 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -398,7 +398,7 @@ public: */ ErrCode RemoveDistributedNotifications(const std::vector& hashcodes, const int32_t slotTypeInt, const int32_t deleteTypeInt, - const int32_t removeReason) override; + const int32_t removeReason, const std::string& deviceId = "") override; ErrCode GetUnifiedGroupInfoFromDb(std::string &enable); @@ -1714,6 +1714,8 @@ private: ErrCode RemoveDistributedNotifications(const NotificationConstant::SlotType& slotType, const int32_t removeReason, const NotificationConstant::DistributedDeleteType& deleteType); + ErrCode RemoveDistributedNotificationsByDeviceId(const std::string& deviceId, + const int32_t removeReason); ErrCode RemoveAllDistributedNotifications(const int32_t removeReason); bool ExecuteDeleteDistributedNotification(std::shared_ptr& record, std::vector>& notifications, const int32_t removeReason); diff --git a/services/ans/src/advanced_notification_manager/advanced_notification_cancel.cpp b/services/ans/src/advanced_notification_manager/advanced_notification_cancel.cpp index a1946d744..f9a8672e4 100644 --- a/services/ans/src/advanced_notification_manager/advanced_notification_cancel.cpp +++ b/services/ans/src/advanced_notification_manager/advanced_notification_cancel.cpp @@ -915,7 +915,7 @@ void AdvancedNotificationService::ExcuteDeleteAll(ErrCode &result, const int32_t ErrCode AdvancedNotificationService::RemoveDistributedNotifications( const std::vector& hashcodes, const int32_t slotTypeInt, - const int32_t deleteTypeInt, const int32_t removeReason) + const int32_t deleteTypeInt, const int32_t removeReason, const std::string& deviceId) { bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { @@ -945,6 +945,8 @@ ErrCode AdvancedNotificationService::RemoveDistributedNotifications( return RemoveDistributedNotifications(slotType, removeReason, deleteType); case NotificationConstant::DistributedDeleteType::HASHCODES: return RemoveDistributedNotifications(hashcodes, removeReason); + case NotificationConstant::DistributedDeleteType::DEVICE_ID: + return RemoveDistributedNotificationsByDeviceId(deviceId, removeReason); default: ANS_LOGW("no deleteType"); break; @@ -952,6 +954,56 @@ ErrCode AdvancedNotificationService::RemoveDistributedNotifications( return ERR_OK; } +ErrCode AdvancedNotificationService::RemoveDistributedNotificationsByDeviceId( + const std::string& deviceId, const int32_t removeReason) +{ + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([=]() { + std::vector> notifications; + std::list> deleteRecords; + for (auto record : notificationList_) { + auto notification = record->notification; + if (notification == nullptr) { + continue; + } + auto request = notification->GetNotificationRequestPoint(); + if (request == nullptr) { + continue; + } + auto extendInfo = request->GetExtendInfo(); + if (extendInfo == nullptr) { + continue; + } + auto key = ANS_EXTENDINFO_INFO_PRE + ANS_EXTENDINFO_DEVICE_ID; + auto id = extendInfo->GetStringParam(key); + if (id.empty()) { + continue; + } + if (id != deviceId) { + continue; + } + + if (ExecuteDeleteDistributedNotification(record, notifications, removeReason)) { + deleteRecords.push_back(record); + } + if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { + std::vector> currNotificationList = notifications; + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( + currNotificationList, nullptr, removeReason); + notifications.clear(); + } + } + + if (!notifications.empty()) { + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( + notifications, nullptr, removeReason); + } + for (auto deleteRecord : deleteRecords) { + notificationList_.remove(deleteRecord); + } + })); + return ERR_OK; +} + ErrCode AdvancedNotificationService::RemoveDistributedNotifications( const std::vector& hashcodes, const int32_t removeReason) { diff --git a/services/distributed/include/soft_bus/distributed_publish_service.h b/services/distributed/include/soft_bus/distributed_publish_service.h index ff779ab5e..7e6e0dac7 100644 --- a/services/distributed/include/soft_bus/distributed_publish_service.h +++ b/services/distributed/include/soft_bus/distributed_publish_service.h @@ -63,7 +63,7 @@ private: #else void PublishNotification(const std::shared_ptr& boxMessage); void PublishSynchronousLiveView(const std::shared_ptr& boxMessage); - void RemoveAllDistributedNotifications(); + void RemoveAllDistributedNotifications(const std::shared_ptr& boxMessage); private: void MakeExtendInfo(const NotificationRequestBox& box, sptr& request); void MakeNotificationButtons(const NotificationRequestBox& box, diff --git a/services/distributed/include/tlv_box/remove_all_distributed_box.h b/services/distributed/include/tlv_box/remove_all_distributed_box.h index 64ceaef0f..7241b8862 100644 --- a/services/distributed/include/tlv_box/remove_all_distributed_box.h +++ b/services/distributed/include/tlv_box/remove_all_distributed_box.h @@ -28,6 +28,8 @@ public: RemoveAllDistributedNotificationsBox(); ~RemoveAllDistributedNotificationsBox(); RemoveAllDistributedNotificationsBox(std::shared_ptr box); + bool SetLocalDeviceId(const std::string &deviceId); + bool GetLocalDeviceId(std::string &deviceId) const; }; } // namespace Notification } // namespace OHOS diff --git a/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp b/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp index 966adec56..099365143 100644 --- a/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp +++ b/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp @@ -222,6 +222,8 @@ void DistributedPublishService::RemoveAllDistributedNotifications(DistributedDev ANS_LOGW("create box error"); return; } + auto local = DistributedDeviceService::GetInstance().GetLocalDevice(); + removeBox->SetLocalDeviceId(local.deviceId_); if (!removeBox->Serialize()) { ANS_LOGW("dans OnCanceled serialize failed"); @@ -561,13 +563,22 @@ void DistributedPublishService::SetNotificationExtendInfo(const sptr& boxMessage) { + RemoveAllDistributedNotificationsBox removeBox = RemoveAllDistributedNotificationsBox(boxMessage); + std::string deviceId; + removeBox.GetLocalDeviceId(deviceId); + DistributedDeviceInfo device; + if (!DistributedDeviceService::GetInstance().GetDeviceInfo(deviceId, device)) { + ANS_LOGW("Dans bundle get device info failed %{public}s.", StringAnonymous(deviceId).c_str()); + return; + } std::vector hashcodes; IN_PROCESS_CALL(NotificationHelper::RemoveDistributedNotifications(hashcodes, NotificationConstant::SlotType::SOCIAL_COMMUNICATION, - NotificationConstant::DistributedDeleteType::ALL, - NotificationConstant::DISTRIBUTED_RELEASE_DELETE)); + NotificationConstant::DistributedDeleteType::DEVICE_ID, + NotificationConstant::DISTRIBUTED_RELEASE_DELETE, + device.udid_)); } void DistributedPublishService::PublishNotification(const std::shared_ptr& boxMessage) diff --git a/services/distributed/src/soft_bus/distributed_service.cpp b/services/distributed/src/soft_bus/distributed_service.cpp index a8c6debbe..c5cd0a90d 100644 --- a/services/distributed/src/soft_bus/distributed_service.cpp +++ b/services/distributed/src/soft_bus/distributed_service.cpp @@ -151,6 +151,11 @@ void DistributedService::ReleaseDevice(const std::string &deviceId, uint16_t dev return; } std::function subscribeTask = std::bind([deviceId, deviceType]() { + DistributedDeviceInfo device; + if (!DistributedDeviceService::GetInstance().GetDeviceInfo(deviceId, device)) { + ANS_LOGW("Dans bundle get device info failed %{public}s.", StringAnonymous(deviceId).c_str()); + return; + } DistributedSubscribeService::GetInstance().UnSubscribeNotification(deviceId, deviceType); auto localDevice = DistributedDeviceService::GetInstance().GetLocalDevice(); if (localDevice.deviceType_ == DistributedHardware::DmDeviceType::DEVICE_TYPE_WATCH) { @@ -161,8 +166,9 @@ void DistributedService::ReleaseDevice(const std::string &deviceId, uint16_t dev std::vector hashcodes; NotificationHelper::RemoveDistributedNotifications(hashcodes, NotificationConstant::SlotType::SOCIAL_COMMUNICATION, - NotificationConstant::DistributedDeleteType::ALL, - NotificationConstant::DISTRIBUTED_RELEASE_DELETE); + NotificationConstant::DistributedDeleteType::DEVICE_ID, + NotificationConstant::DISTRIBUTED_RELEASE_DELETE, + device.udid_); } }); serviceQueue_->submit(subscribeTask); @@ -467,7 +473,7 @@ void DistributedService::OnHandleMsg(std::shared_ptr& box) DistributedPublishService::GetInstance().PublishSynchronousLiveView(box); break; case NotificationEventType::REMOVE_ALL_DISTRIBUTED_NOTIFICATIONS: - DistributedPublishService::GetInstance().RemoveAllDistributedNotifications(); + DistributedPublishService::GetInstance().RemoveAllDistributedNotifications(box); break; #endif default: diff --git a/services/distributed/src/tlv_box/remove_all_distributed_box.cpp b/services/distributed/src/tlv_box/remove_all_distributed_box.cpp index bfb04ca71..361e1ef1c 100644 --- a/services/distributed/src/tlv_box/remove_all_distributed_box.cpp +++ b/services/distributed/src/tlv_box/remove_all_distributed_box.cpp @@ -33,5 +33,21 @@ RemoveAllDistributedNotificationsBox::RemoveAllDistributedNotificationsBox( std::shared_ptr box) : BoxBase(box) { } + +bool RemoveAllDistributedNotificationsBox::SetLocalDeviceId(const std::string &deviceId) +{ + if (box_ == nullptr) { + return false; + } + return box_->PutValue(std::make_shared(LOCAL_DEVICE_ID, deviceId)); +} + +bool RemoveAllDistributedNotificationsBox::GetLocalDeviceId(std::string& deviceId) const +{ + if (box_ == nullptr) { + return false; + } + return box_->GetStringValue(LOCAL_DEVICE_ID, deviceId); +} } } \ No newline at end of file diff --git a/tools/test/mock/mock_ans_manager_stub.h b/tools/test/mock/mock_ans_manager_stub.h index e32f22365..423b61f75 100644 --- a/tools/test/mock/mock_ans_manager_stub.h +++ b/tools/test/mock/mock_ans_manager_stub.h @@ -179,7 +179,8 @@ public: } ErrCode RemoveDistributedNotifications(const std::vector& hashcodes, - const int32_t slotTypeInt, const int32_t deleteTypeInt, const int32_t removeReason) override + const int32_t slotTypeInt, const int32_t deleteTypeInt, const int32_t removeReason, + const std::string& deviceId = "") override { return ERR_ANS_INVALID_PARAM; } -- Gitee