diff --git a/frameworks/ans/IAnsManager.idl b/frameworks/ans/IAnsManager.idl index 9d447d582c361eed1b4873874e148ba75bba559c..982adcc5162906048d7aa912d95280fd30286717 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 6f548000e766cd845e30f99ac0c491fce431a31c..5aaf74c97ec0a931ccca81e96b16fee6c96d1777 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 c8dcc2afdd4967b2d8d14553b490f7beb404a4a7..7977b9b393e3578775e9cb9723c5d9c164b382fc 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 a57ccb8e4cd033939d0f89e66bd6c574469f7f0a..c07787ad6a8907f227ec7665c4352f5e3e40f8a5 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 7eda75b949752bcac22c14d879b76054d24e8786..b63225d98fc355e9eb8e2d002b53d68cfb58b095 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 f0a4de4b7f8dfa3bc8bafd110e3cc94538580017..72952a3431a0bc987e282e42ec9ac3f232d3ff00 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 9dc390c8ea62963f5b85ef5c48a79649025c6d48..7df27a3b75ef5a7e9894ac97eba4c895de8bc1f5 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 5b45966be7e5c8d3b21de333be6ed9a408a1e9f3..9d3d00323b505dbf5dd8ca922ee9a5363a944476 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 2034bf837d8ea464dca7e43040392963083df3fc..8de9c6adaa1f9acef561fd5134f3eb072e8fc374 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 65e31518cf495107b55708ed9f787c754da81a67..bc9bc22527a12ad7e36a89291a5716941ab8147c 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 a1946d744beaa201f659513f0800725215508421..f9a8672e4736b6cf299402b2b741469e4fc293b7 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 ff779ab5e23386d96544d91313b9561382af9385..7e6e0dac784ca8de2e8a0e493f17bb908e4a72cd 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 64ceaef0fe1ff6e0b265bc69677fb6ad80646629..7241b88626bba028a64e5743ae220f735f7c6e3e 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 966adec562b6900477c570bd5dc6463c807c1758..0993651430dfb0346993637b7cf1510547f3d805 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 a8c6debbe791b0275967f189d611be18c1b70a8d..c5cd0a90d30693fdb98395644e0836e2d96a6a27 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 bfb04ca7120e991b669ec13ada25a8cbf5277317..361e1ef1cecb4d3fdf327b2e30b7e100a365f9dd 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 e32f22365407e47a162e2f4f03acb7d68b5ede99..423b61f759f1638149a124efb4daf9bdf5edb10b 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; }