From 5464f069663afa1995a381fc29e04ae12b32b000 Mon Sep 17 00:00:00 2001 From: sunjiakun Date: Fri, 27 Dec 2024 09:46:44 +0800 Subject: [PATCH] distributed Signed-off-by: sunjiakun --- frameworks/ans/src/notification.cpp | 2 +- .../core/common/include/ans_convert_enum.h | 1 + .../core/common/src/ans_convert_enum.cpp | 3 + interfaces/inner_api/notification.h | 2 +- interfaces/inner_api/notification_constant.h | 5 ++ .../ans/include/notification_config_parse.h | 5 +- .../src/common/notification_config_parse.cpp | 31 +++++++ services/distributed/BUILD.gn | 14 +++- .../include/distributed_preferences.h | 18 ++++- .../include/distributed_preferences_info.h | 20 ++++- .../include/soft_bus/distributed_service.h | 6 ++ .../include/soft_bus/distributed_subscriber.h | 2 + .../include/tlv_box/batch_remove_box.h | 36 +++++++++ .../distributed/include/tlv_box/remove_box.h | 35 ++++++++ .../distributed/include/tlv_box/tlv_box.h | 5 ++ .../src/distributed_preferences.cpp | 14 +++- .../src/distributed_preferences_info.cpp | 19 ++++- .../soft_bus/distributed_publish_service.cpp | 25 ++++++ .../src/soft_bus/distributed_service.cpp | 6 ++ .../distributed_subscribe_service.cpp | 76 ++++++++++++++++++ .../src/soft_bus/distributed_subscriber.cpp | 80 +++++++++++++++++++ .../src/tlv_box/batch_remove_box.cpp | 51 ++++++++++++ .../distributed/src/tlv_box/remove_box.cpp | 52 ++++++++++++ services/distributed/src/tlv_box/tlv_box.cpp | 15 ++++ 24 files changed, 515 insertions(+), 8 deletions(-) create mode 100644 services/distributed/include/tlv_box/batch_remove_box.h create mode 100644 services/distributed/include/tlv_box/remove_box.h create mode 100644 services/distributed/src/tlv_box/batch_remove_box.cpp create mode 100644 services/distributed/src/tlv_box/remove_box.cpp diff --git a/frameworks/ans/src/notification.cpp b/frameworks/ans/src/notification.cpp index 91224783a..de8e4950f 100644 --- a/frameworks/ans/src/notification.cpp +++ b/frameworks/ans/src/notification.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/core/common/include/ans_convert_enum.h b/frameworks/core/common/include/ans_convert_enum.h index 85a9bb14b..f15af73a3 100644 --- a/frameworks/core/common/include/ans_convert_enum.h +++ b/frameworks/core/common/include/ans_convert_enum.h @@ -86,6 +86,7 @@ enum class RemoveReason { SLOT_ENABLED_REASON_DELETE = 29, RECOVER_LIVE_VIEW_DELETE = 30, DISABLE_NOTIFICATION_FEATURE_REASON_DELETE = 31, + REMOVE_REASON_CROSS_DEVICE = 32, 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 4026f5335..6ba6581dd 100644 --- a/frameworks/core/common/src/ans_convert_enum.cpp +++ b/frameworks/core/common/src/ans_convert_enum.cpp @@ -340,6 +340,9 @@ void AnsEnumUtil::ReasonCToJSSecondExt(const int &inType, int &outType) case NotificationConstant::DISABLE_NOTIFICATION_FEATURE_REASON_DELETE: outType = static_cast(RemoveReason::DISABLE_NOTIFICATION_FEATURE_REASON_DELETE); break; + case NotificationConstant::REMOVE_REASON_CROSS_DEVICE: + outType = static_cast(RemoveReason::REMOVE_REASON_CROSS_DEVICE); + break; default: outType = static_cast(RemoveReason::APP_CANCEL_REASON_OTHER); ANS_LOGW("Reason %{public}d is an invalid value", inType); diff --git a/interfaces/inner_api/notification.h b/interfaces/inner_api/notification.h index f3c450829..2813b2607 100644 --- a/interfaces/inner_api/notification.h +++ b/interfaces/inner_api/notification.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/interfaces/inner_api/notification_constant.h b/interfaces/inner_api/notification_constant.h index de66e8898..91e785bb1 100644 --- a/interfaces/inner_api/notification_constant.h +++ b/interfaces/inner_api/notification_constant.h @@ -302,6 +302,11 @@ public: */ static const int32_t DISABLE_NOTIFICATION_FEATURE_REASON_DELETE = 31; + /** + * Indicates that a notification is deleted because collaborative delete. + */ + static const int32_t REMOVE_REASON_CROSS_DEVICE = 32; + /** * Indicates that a notification is deleted for other reasons. */ diff --git a/services/ans/include/notification_config_parse.h b/services/ans/include/notification_config_parse.h index 8408fdced..e6f210bae 100644 --- a/services/ans/include/notification_config_parse.h +++ b/services/ans/include/notification_config_parse.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2021-2023 Huawei Device Co., Ltd. +* Copyright (c) 2021-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -20,6 +20,7 @@ #include #include #include +#include #ifdef CONFIG_POLICY_ENABLE #include "config_policy_utils.h" @@ -47,6 +48,7 @@ public: bool IsBannerEnabled(const std::string bundleName) const; bool IsReminderEnabled(const std::string& bundleName) const; void GetFlowCtrlConfigFromCCM(FlowControlThreshold &threshold); + std::unordered_set GetCollaborativeDeleteType() const; private: std::map defaultCurrentSlotReminder_; @@ -67,6 +69,7 @@ public: # else constexpr static const char* NOTIFICAITON_CONFIG_FILE = "system/etc/notification/notification_config.json"; #endif + constexpr static const char* CFG_KEY_COLLABORATIVE_DELETE_TYPES = "collaborativeDeleteTypes"; }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/common/notification_config_parse.cpp b/services/ans/src/common/notification_config_parse.cpp index 3657b91f9..eedc6ea63 100644 --- a/services/ans/src/common/notification_config_parse.cpp +++ b/services/ans/src/common/notification_config_parse.cpp @@ -232,5 +232,36 @@ void NotificationConfigParse::GetFlowCtrlConfigFromCCM(FlowControlThreshold &thr ANS_LOGI("GetFlowCtrlConfigFromCCM success"); } + +std::unordered_set NotificationConfigParse::GetCollaborativeDeleteType() const +{ + nlohmann::json root; + std::string JsonPoint = "/"; + JsonPoint.append(CFG_KEY_NOTIFICATION_SERVICE); + JsonPoint.append("/"); + JsonPoint.append(CFG_KEY_COLLABORATIVE_DELETE_TYPES); + if (!GetConfigJson(JsonPoint, root)) { + ANS_LOGE("GetConfigJson faild"); + return std::unordered_set(); + } + if (root.find(CFG_KEY_NOTIFICATION_SERVICE) == root.end()) { + ANS_LOGE("appPrivileges null"); + return std::unordered_set(); + } + + nlohmann::json collaborativeDeleteTypes = root[CFG_KEY_NOTIFICATION_SERVICE][CFG_KEY_COLLABORATIVE_DELETE_TYPES]; + if (collaborativeDeleteTypes.empty() && !collaborativeDeleteTypes.is_array()) { + ANS_LOGE("collaborativeDeleteTypes null or no array"); + return std::unordered_set(); + } + std::unordered_set collaborativeDeleteTypeSet; + for (const auto &item : collaborativeDeleteTypes) { + if (item.is_string()) { + collaborativeDeleteTypeSet.insert(item.get()); + } + } + + return collaborativeDeleteTypeSet; +} } // namespace Notification } // namespace OHOS diff --git a/services/distributed/BUILD.gn b/services/distributed/BUILD.gn index cb77448ed..194d7a0ee 100644 --- a/services/distributed/BUILD.gn +++ b/services/distributed/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2023 Huawei Device Co., Ltd. +# Copyright (c) 2021-2024 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -84,6 +84,13 @@ ohos_shared_library("libans_softbus_distributed") { branch_protector_ret = "pac_ret" sources = [ + "${services_path}/ans/src/common/file_utils.cpp", + "${services_path}/ans/src/common/notification_app_privileges.cpp", + "${services_path}/ans/src/common/notification_config_parse.cpp", + "${services_path}/distributed/src/distributed_flow_control.cpp", + "${services_path}/distributed/src/distributed_preferences.cpp", + "${services_path}/distributed/src/distributed_preferences_database.cpp", + "${services_path}/distributed/src/distributed_preferences_info.cpp", "src/soft_bus/distributed_client.cpp", "src/soft_bus/distributed_cmd_service.cpp", "src/soft_bus/distributed_extern_intferface.cpp", @@ -98,8 +105,10 @@ ohos_shared_library("libans_softbus_distributed") { "src/soft_bus/distributed_subscribe_service.cpp", "src/soft_bus/distributed_subscriber.cpp", "src/soft_bus/distributed_timer_service.cpp", + "src/tlv_box/batch_remove_box.cpp", "src/tlv_box/box_base.cpp", "src/tlv_box/match_box.cpp", + "src/tlv_box/remove_box.cpp", "src/tlv_box/request_box.cpp", "src/tlv_box/state_box.cpp", "src/tlv_box/tlv_box.cpp", @@ -108,6 +117,8 @@ ohos_shared_library("libans_softbus_distributed") { include_dirs = [ "${services_path}/distributed/include/soft_bus", "${services_path}/distributed/include/tlv_box", + "${services_path}/ans/include", + "${services_path}/distributed/include", ] deps = [ "${frameworks_module_ans_path}:ans_innerkits" ] @@ -124,6 +135,7 @@ ohos_shared_library("libans_softbus_distributed") { "ffrt:libffrt", "hilog:libhilog", "ipc:ipc_core", + "kv_store:distributeddata_inner", "os_account:os_account_innerkits", "screenlock_mgr:screenlock_client", "time_service:time_client", diff --git a/services/distributed/include/distributed_preferences.h b/services/distributed/include/distributed_preferences.h index 4f6853a14..5aa5c3420 100644 --- a/services/distributed/include/distributed_preferences.h +++ b/services/distributed/include/distributed_preferences.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -90,6 +90,22 @@ public: ErrCode SetSyncEnabledWithoutApp(const int32_t userId, const bool enabled); ErrCode GetSyncEnabledWithoutApp(const int32_t userId, bool &enabled); + /** + * @brief Add notification for participating in collaboration. + * + * @param notificationKey Notification key value. + * @return Returns add notification result. + */ + ErrCode AddCollaborativeNotification(const std::string ¬ificationKey); + + /** + * @brief Check if the notification has participated in the collaboration and delete it. + * + * @param notificationKey Notification key value. + * @return Return whether if the notification has participated in collaboration result. + */ + bool CheckCollaborativeNotification(const std::string ¬ificationKey); + private: bool InitDistributedAllInfo(); void GetDistributedMainKey(std::string &key); diff --git a/services/distributed/include/distributed_preferences_info.h b/services/distributed/include/distributed_preferences_info.h index caafab0b0..3bc2757ab 100644 --- a/services/distributed/include/distributed_preferences_info.h +++ b/services/distributed/include/distributed_preferences_info.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -18,6 +18,7 @@ #include #include +#include #include "ans_inner_errors.h" @@ -77,10 +78,27 @@ public: void SetSyncEnabledWithoutApp(const int32_t userId, const bool enabled); ErrCode GetSyncEnabledWithoutApp(const int32_t userId, bool &enabled); + /** + * @brief Add notification for participating in collaboration. + * + * @param notificationKey Notification key value. + * @return Returns add notification result. + */ + ErrCode AddCollaborativeNotification(const std::string ¬ificationkey); + + /** + * @brief Check if the notification has participated in the collaboration and delete it. + * + * @param notificationKey Notification key value. + * @return Return whether if the notification has participated in collaboration result. + */ + bool CheckCollaborativeNotification(const std::string ¬ificationkey); + private: bool distributedEnable_ = false; std::map, bool> bundleEnable_; std::map enabledWithoutApp_; + std::unordered_set collaborativeNotificationList_; }; } // namespace Notification } // namespace OHOS diff --git a/services/distributed/include/soft_bus/distributed_service.h b/services/distributed/include/soft_bus/distributed_service.h index c1130bb4c..b95cdf777 100644 --- a/services/distributed/include/soft_bus/distributed_service.h +++ b/services/distributed/include/soft_bus/distributed_service.h @@ -39,6 +39,9 @@ public: void OnReceiveMsg(const void *data, uint32_t dataLen); void OnConsumed(const std::shared_ptr &request, const DistributedDeviceInfo& device); + void OnCanceled(const std::shared_ptr& notification, const DistributedDeviceInfo& peerDevice); + void OnBatchCanceled(const std::vector>& notifications, + const DistributedDeviceInfo& peerDevice); void InitDeviceState(const DistributedDeviceInfo device); void SyncDeviceState(int32_t state); void SetCurrentUserId(int32_t userId); @@ -47,6 +50,7 @@ public: void HandleMatchSync(const std::shared_ptr& boxMessage); void DestoryService(); void ReportDeviceStatus(std::string deviceId); + std::string GetNotificationKey(const std::shared_ptr& notification); private: int64_t GetCurrentTime(); @@ -55,6 +59,8 @@ private: void MakeNotifictaionContent(const NotifticationRequestBox& box, sptr& request); void MakeNotifictaionIcon(const NotifticationRequestBox& box, sptr& request); void MakeNotifictaionReminderFlag(const NotifticationRequestBox& box, sptr& request); + void RemoveNotifictaion(const std::shared_ptr& boxMessage); + void RemoveNotifictaions(const std::shared_ptr& boxMessage); std::function callBack_ = nullptr; int32_t userId_ = DEFAULT_USER_ID; DistributedDeviceInfo localDevice_; diff --git a/services/distributed/include/soft_bus/distributed_subscriber.h b/services/distributed/include/soft_bus/distributed_subscriber.h index 69d1420ec..cb8256131 100644 --- a/services/distributed/include/soft_bus/distributed_subscriber.h +++ b/services/distributed/include/soft_bus/distributed_subscriber.h @@ -42,6 +42,8 @@ public: const std::shared_ptr &sortingMap, int32_t deleteReason) override; void SetLocalDevice(DistributedDeviceInfo localDevice); void SetPeerDevice(DistributedDeviceInfo localDevice); + bool CheckCollaborativeRemoveType(const NotificationConstant::SlotType &slotType); + bool CheckNeedCollaboration(const std::shared_ptr ¬ification); private: DistributedDeviceInfo localDevice_; DistributedDeviceInfo peerDevice_; diff --git a/services/distributed/include/tlv_box/batch_remove_box.h b/services/distributed/include/tlv_box/batch_remove_box.h new file mode 100644 index 000000000..31363ddd8 --- /dev/null +++ b/services/distributed/include/tlv_box/batch_remove_box.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_NOTIFICATION_DISTRIBUTED_BATCH_REMOVE_NOTIFICATION_BOX_H +#define BASE_NOTIFICATION_DISTRIBUTED_BATCH_REMOVE_NOTIFICATION_BOX_H + +#include + +#include "box_base.h" +#include "tlv_box.h" + +namespace OHOS { +namespace Notification { +class BatchRemoveNotifticationBox : public BoxBase { +public: + BatchRemoveNotifticationBox(); + ~BatchRemoveNotifticationBox(); + BatchRemoveNotifticationBox(std::shared_ptr box); + bool SetNotificationHashCode(const std::string& hasdCode); + bool SetNotificationKeys(const std::vector& keys); +}; +} // namespace Notification +} // namespace OHOS +#endif // BASE_NOTIFICATION_DISTRIBUTED_BATCH_REMOVE_NOTIFICATION_BOX_H diff --git a/services/distributed/include/tlv_box/remove_box.h b/services/distributed/include/tlv_box/remove_box.h new file mode 100644 index 000000000..27628513c --- /dev/null +++ b/services/distributed/include/tlv_box/remove_box.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_REMOVE_BOX_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_REMOVE_BOX_H + +#include +#include "tlv_box.h" +#include "box_base.h" + +namespace OHOS { +namespace Notification { +class NotificationRemoveBox : public BoxBase { +public: + NotificationRemoveBox(); + ~NotificationRemoveBox(); + NotificationRemoveBox(std::shared_ptr box); + bool SetNotificationHashCode(const std::string& hasdCode); + bool SetDistributedCollaborate(bool distributedCollaborate); +}; +} // namespace Notification +} // namespace OHOS +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_REMOVE_BOX_H diff --git a/services/distributed/include/tlv_box/tlv_box.h b/services/distributed/include/tlv_box/tlv_box.h index 9cf98fb71..d759ecc1c 100644 --- a/services/distributed/include/tlv_box/tlv_box.h +++ b/services/distributed/include/tlv_box/tlv_box.h @@ -31,6 +31,7 @@ enum NotificationEventType : int32_t { NOTIFICATION_QUICK_REPLY = 4, NOTIFICATION_STATE_SYNC = 5, NOTIFICATION_MATCH_SYNC = 6, + REMOVE_ALL_NOTIFICATIONS = 7, }; enum TlvType : int32_t { @@ -46,6 +47,8 @@ enum TlvType : int32_t { NOTIFICATION_OVERLAY_ICON = 9, NOTIFICATION_CONTENT_TYPE = 10, NOTIFICATION_COMMON_LIVEVIEW = 11, + NOTIFICATION_KEYS = 12, + COLLABORATIVE_NOTIFICATION = 13, MATCH_TYPE = 993, PEER_DEVICE_ID = 994, PEER_DEVICE_TYPE = 995, @@ -63,6 +66,7 @@ public: TlvItem(int32_t type, std::string value); TlvItem(int32_t type, const unsigned char* value, int32_t length); TlvItem(int32_t type, const TlvItem& value); + TlvItem(int32_t type, std::vector value); ~TlvItem(); int32_t GetType() const; @@ -93,6 +97,7 @@ public: bool GetStringValue(int32_t type, std::string& value); bool GetInt32Value(int32_t type, int32_t& value); bool GetObjectValue(int32_t type, TlvBox& value); + bool GetVectorValue(int32_t type, std::vector& value); int32_t bytesLength_ = 0; unsigned char* byteBuffer_ = nullptr; diff --git a/services/distributed/src/distributed_preferences.cpp b/services/distributed/src/distributed_preferences.cpp index a075a59b3..885efe431 100644 --- a/services/distributed/src/distributed_preferences.cpp +++ b/services/distributed/src/distributed_preferences.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -271,5 +271,17 @@ ErrCode DistributedPreferences::GetSyncEnabledWithoutApp(const int32_t userId, b return preferencesInfo_ == nullptr ? ERR_ANS_DISTRIBUTED_OPERATION_FAILED : preferencesInfo_->GetSyncEnabledWithoutApp(userId, enabled); } + +ErrCode DistributedPreferences::AddCollaborativeNotification(const std::string ¬ificationkey) +{ + return preferencesInfo_ == nullptr ? + ERR_ANS_DISTRIBUTED_OPERATION_FAILED : preferencesInfo_->AddCollaborativeNotification(notificationkey); +} + +bool DistributedPreferences::CheckCollaborativeNotification(const std::string ¬ificationkey) +{ + return preferencesInfo_ == nullptr ? + false : preferencesInfo_->CheckCollaborativeNotification(notificationkey); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/distributed/src/distributed_preferences_info.cpp b/services/distributed/src/distributed_preferences_info.cpp index b9ef2836a..39a474f8a 100644 --- a/services/distributed/src/distributed_preferences_info.cpp +++ b/services/distributed/src/distributed_preferences_info.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -73,5 +73,22 @@ ErrCode DistributedPreferencesInfo::GetSyncEnabledWithoutApp(const int32_t userI } return ERR_OK; } + +ErrCode DistributedPreferencesInfo::AddCollaborativeNotification(const std::string ¬ificationkey) +{ + collaborativeNotificationList_.insert(notificationkey); + return ERR_OK; +} + +bool DistributedPreferencesInfo::CheckCollaborativeNotification(const std::string ¬ificationkey) +{ + auto it = collaborativeNotificationList_.find(notificationkey); + if (it != collaborativeNotificationList_.end()) { + collaborativeNotificationList_.erase(it); + return true; + } + ANS_LOGE("CheckCollaborativeNotification failed"); + return false; +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/distributed/src/soft_bus/distributed_publish_service.cpp b/services/distributed/src/soft_bus/distributed_publish_service.cpp index 9a9d4d297..88a8954d0 100644 --- a/services/distributed/src/soft_bus/distributed_publish_service.cpp +++ b/services/distributed/src/soft_bus/distributed_publish_service.cpp @@ -116,5 +116,30 @@ void DistributedService::PublishNotifictaion(const std::shared_ptr& boxM ANS_LOGI("Dans publish message %{public}s %{public}d.", request->Dump().c_str(), result); } +void DistributedService::RemoveNotifictaion(const std::shared_ptr& boxMessage) +{ + std::string hasdCode; + if (boxMessage == nullptr) { + ANS_LOGE("boxMessage is nullptr"); + return; + } + boxMessage->GetStringValue(NOTIFICATION_HASHCODE, hasdCode); + int result = IN_PROCESS_CALL(NotificationHelper::RemoveNotification( + hasdCode, NotificationConstant::REMOVE_REASON_CROSS_DEVICE)); + ANS_LOGI("dans remove message %{public}d.", result); +} + +void DistributedService::RemoveNotifictaions(const std::shared_ptr& boxMessage) +{ + std::vector hasdCodes; + if (boxMessage == nullptr) { + ANS_LOGE("boxMessage is nullptr"); + return; + } + boxMessage->GetVectorValue(NOTIFICATION_KEYS, hasdCodes); + int result = IN_PROCESS_CALL( + NotificationHelper::RemoveNotifications(hasdCodes, NotificationConstant::REMOVE_REASON_CROSS_DEVICE)); + ANS_LOGI("dans batch remove message %{public}d.", result); +} } } diff --git a/services/distributed/src/soft_bus/distributed_service.cpp b/services/distributed/src/soft_bus/distributed_service.cpp index f3895cc35..ba84c4276 100644 --- a/services/distributed/src/soft_bus/distributed_service.cpp +++ b/services/distributed/src/soft_bus/distributed_service.cpp @@ -162,6 +162,12 @@ void DistributedService::OnReceiveMsg(const void *data, uint32_t dataLen) case NotificationEventType::NOTIFICATION_MATCH_SYNC: HandleMatchSync(box); break; + case NotificationEventType::REMOVE_NOTIFICATION: + RemoveNotifictaion(box); + break; + case NotificationEventType::REMOVE_ALL_NOTIFICATIONS: + RemoveNotifictaions(box); + break; default: ANS_LOGW("Dans receive msg %{public}d %{public}d.", type, box->bytesLength_); break; diff --git a/services/distributed/src/soft_bus/distributed_subscribe_service.cpp b/services/distributed/src/soft_bus/distributed_subscribe_service.cpp index 7bcf08dcb..947cb9b12 100644 --- a/services/distributed/src/soft_bus/distributed_subscribe_service.cpp +++ b/services/distributed/src/soft_bus/distributed_subscribe_service.cpp @@ -23,9 +23,14 @@ #include "notification_subscribe_info.h" #include "distributed_timer_service.h" #include "distributed_liveview_all_scenarios_extension_wrapper.h" +#include "distributed_preferences.h" +#include "batch_remove_box.h" +#include "remove_box.h" namespace OHOS { namespace Notification { +const std::string DISTRIBUTED_LABEL = "ans_distributed"; + std::string SubscribeTransDeviceType(uint16_t deviceType_) { switch (deviceType_) { @@ -139,11 +144,82 @@ void DistributedService::OnConsumed(const std::shared_ptr &request ANS_LOGW("Dans OnConsumed serialize failed."); return; } + DistributedPreferences::GetInstance()->AddCollaborativeNotification(request->GetKey()); DistributedClient::GetInstance().SendMessage(requestBox.GetByteBuffer(), requestBox.GetByteLength(), TransDataType::DATA_TYPE_BYTES, peerDevice.deviceId_, peerDevice.deviceType_); }); serviceQueue_->submit(task); } +void DistributedService::OnBatchCanceled(const std::vector>& notifications, + const DistributedDeviceInfo& peerDevice) +{ + if (serviceQueue_ == nullptr) { + ANS_LOGE("check handler is null."); + return; + } + std::function task = std::bind([peerDevice, notifications, this]() { + BatchRemoveNotifticationBox batchRemoveBox; + std::vector hasdCodes; + std::string hasdCode; + for (auto notification : notifications) { + if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr) { + continue; + } + ANS_LOGI("dans OnBatchCanceled %{public}s", notification->Dump().c_str()); + hasdCodes.push_back(GetNotificationKey(notification)); + } + if (!hasdCodes.empty()) { + batchRemoveBox.SetNotificationHashCode(hasdCodes[0]); + } + batchRemoveBox.SetNotificationKeys(hasdCodes); + + if (!batchRemoveBox.Serialize()) { + ANS_LOGW("dans OnCanceled serialize failed"); + return; + } + DistributedClient::GetInstance().SendMessage(batchRemoveBox.GetByteBuffer(), batchRemoveBox.GetByteLength(), + TransDataType::DATA_TYPE_MESSAGE, peerDevice.deviceId_, peerDevice.deviceType_); + }); + serviceQueue_->submit(task); +} + +void DistributedService::OnCanceled(const std::shared_ptr& notification, + const DistributedDeviceInfo& peerDevice) +{ + if (serviceQueue_ == nullptr) { + ANS_LOGE("check handler is null"); + return; + } + std::function task = std::bind([peerDevice, notification, this]() { + NotificationRemoveBox removeBox; + if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr) { + return; + } + ANS_LOGI("dans OnCanceled %{public}s", notification->Dump().c_str()); + removeBox.SetNotificationHashCode(GetNotificationKey(notification)); + if (!removeBox.Serialize()) { + ANS_LOGW("dans OnCanceled serialize failed"); + return; + } + DistributedClient::GetInstance().SendMessage(removeBox.GetByteBuffer(), removeBox.GetByteLength(), + TransDataType::DATA_TYPE_MESSAGE, peerDevice.deviceId_, peerDevice.deviceType_); + }); + serviceQueue_->submit(task); +} + +std::string DistributedService::GetNotificationKey(const std::shared_ptr& notification) +{ + std::string notificationKey = notification->GetKey(); + if (notification->GetNotificationRequestPoint()->GetDistributedCollaborate()) { + size_t pos = notificationKey.find(DISTRIBUTED_LABEL); + if (pos != std::string::npos) { + notificationKey.erase(pos, DISTRIBUTED_LABEL.length()); + } + } else { + notificationKey = DISTRIBUTED_LABEL + notificationKey; + } + return notificationKey; +} } } diff --git a/services/distributed/src/soft_bus/distributed_subscriber.cpp b/services/distributed/src/soft_bus/distributed_subscriber.cpp index 86f6450c1..b48737815 100644 --- a/services/distributed/src/soft_bus/distributed_subscriber.cpp +++ b/services/distributed/src/soft_bus/distributed_subscriber.cpp @@ -17,6 +17,8 @@ #include "ans_log_wrapper.h" #include "distributed_service.h" +#include "notification_config_parse.h" +#include "distributed_preferences.h" namespace OHOS { namespace Notification { @@ -52,6 +54,13 @@ void DistribuedSubscriber::OnCanceled(const std::shared_ptr &reque ANS_LOGI("Subscriber on canceled %{public}d %{public}s %{public}d %{public}s.", peerDevice_.deviceType_, peerDevice_.deviceId_.c_str(), localDevice_.deviceType_, localDevice_.deviceId_.c_str()); + if (deleteReason == NotificationConstant::REMOVE_REASON_CROSS_DEVICE) { + ANS_LOGD("is cross device deletion"); + return; + } + if (CheckNeedCollaboration(request)) { + DistributedService::GetInstance().OnCanceled(request, peerDevice_); + } } void DistribuedSubscriber::OnConsumed(const std::shared_ptr &request, @@ -93,6 +102,22 @@ void DistribuedSubscriber::OnBadgeEnabledChanged(const sptr> &requestList, const std::shared_ptr &sortingMap, int32_t deleteReason) { + ANS_LOGI("Subscriber on batch canceled %{public}d %{public}s %{public}d %{public}s.", + peerDevice_.deviceType_, peerDevice_.deviceId_.c_str(), localDevice_.deviceType_, + localDevice_.deviceId_.c_str()); + if (deleteReason == NotificationConstant::REMOVE_REASON_CROSS_DEVICE) { + ANS_LOGD("is cross device deletion"); + return; + } + std::vector> notifications; + for (auto notification : requestList) { + if (CheckNeedCollaboration(notification)) { + notifications.push_back(notification); + } + } + if (!notifications.empty()) { + DistributedService::GetInstance().OnBatchCanceled(notifications, peerDevice_); + } } void DistribuedSubscriber::SetLocalDevice(DistributedDeviceInfo localDevice) @@ -105,5 +130,60 @@ void DistribuedSubscriber::SetPeerDevice(DistributedDeviceInfo peerDevice) peerDevice_ = peerDevice; } +bool DistribuedSubscriber::CheckCollaborativeRemoveType(const NotificationConstant::SlotType& slotType) +{ + std::string type; + switch (slotType) { + case NotificationConstant::SlotType::SOCIAL_COMMUNICATION: + type = "SOCIAL_COMMUNICATION"; + break; + case NotificationConstant::SlotType::SERVICE_REMINDER: + type = "SERVICE_REMINDER"; + break; + case NotificationConstant::SlotType::CONTENT_INFORMATION: + type = "CONTENT_INFORMATION"; + break; + case NotificationConstant::SlotType::OTHER: + type = "OTHER"; + break; + case NotificationConstant::SlotType::CUSTOM: + type = "CUSTOM"; + break; + case NotificationConstant::SlotType::LIVE_VIEW: + type = "LIVE_VIEW"; + break; + case NotificationConstant::SlotType::CUSTOMER_SERVICE: + type = "CUSTOMER_SERVICE"; + break; + case NotificationConstant::SlotType::EMERGENCY_INFORMATION: + type = "EMERGENCY_INFORMATION"; + break; + case NotificationConstant::SlotType::ILLEGAL_TYPE: + type = "ILLEGAL_TYPE"; + break; + default: + return false; + } + auto collaboratives = NotificationConfigParse::GetInstance()->GetCollaborativeDeleteType(); + return collaboratives.find(type) != collaboratives.end(); +} + +bool DistribuedSubscriber::CheckNeedCollaboration(const std::shared_ptr& notification) +{ + if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr) { + ANS_LOGE("notification or request is nullptr"); + return false; + } + if (!CheckCollaborativeRemoveType(notification->GetNotificationRequestPoint()->GetSlotType())) { + ANS_LOGE("CheckCollaborativeRemoveType failed"); + return false; + } + if (!notification->GetNotificationRequestPoint()->GetDistributedCollaborate() && + !DistributedPreferences::GetInstance()->CheckCollaborativeNotification(notification->GetKey())) { + ANS_LOGE("notification not collaborative"); + return false; + } + return true; } } +} \ No newline at end of file diff --git a/services/distributed/src/tlv_box/batch_remove_box.cpp b/services/distributed/src/tlv_box/batch_remove_box.cpp new file mode 100644 index 000000000..910bc3121 --- /dev/null +++ b/services/distributed/src/tlv_box/batch_remove_box.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "batch_remove_box.h" + +namespace OHOS { +namespace Notification { + +BatchRemoveNotifticationBox::BatchRemoveNotifticationBox() +{ + if (box_ == nullptr) { + return; + } + box_->SetMessageType(REMOVE_ALL_NOTIFICATIONS); +} + +BatchRemoveNotifticationBox::~BatchRemoveNotifticationBox() +{} + +BatchRemoveNotifticationBox::BatchRemoveNotifticationBox(std::shared_ptr box) : BoxBase(box) +{} + +bool BatchRemoveNotifticationBox::SetNotificationKeys(const std::vector& keys) +{ + if (box_ == nullptr) { + return false; + } + return box_->PutValue(std::make_shared(NOTIFICATION_KEYS, keys)); +} + +bool BatchRemoveNotifticationBox::SetNotificationHashCode(const std::string& hasdCode) +{ + if (box_ == nullptr) { + return false; + } + return box_->PutValue(std::make_shared(NOTIFICATION_HASHCODE, hasdCode)); +} +} +} diff --git a/services/distributed/src/tlv_box/remove_box.cpp b/services/distributed/src/tlv_box/remove_box.cpp new file mode 100644 index 000000000..5603b555b --- /dev/null +++ b/services/distributed/src/tlv_box/remove_box.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "remove_box.h" + +namespace OHOS { +namespace Notification { + +NotificationRemoveBox::NotificationRemoveBox() +{ + if (box_ == nullptr) { + return; + } + box_->SetMessageType(REMOVE_NOTIFICATION); +} + +NotificationRemoveBox::~NotificationRemoveBox() +{} + +NotificationRemoveBox::NotificationRemoveBox(std::shared_ptr box) : BoxBase(box) +{ +} + +bool NotificationRemoveBox::SetNotificationHashCode(const std::string& hasdCode) +{ + if (box_ == nullptr) { + return false; + } + return box_->PutValue(std::make_shared(NOTIFICATION_HASHCODE, hasdCode)); +} + +bool NotificationRemoveBox::SetDistributedCollaborate(bool distributedCollaborate) +{ + if (box_ == nullptr) { + return false; + } + return box_->PutValue(std::make_shared(COLLABORATIVE_NOTIFICATION, distributedCollaborate)); +} +} +} diff --git a/services/distributed/src/tlv_box/tlv_box.cpp b/services/distributed/src/tlv_box/tlv_box.cpp index 219657978..770c203bb 100644 --- a/services/distributed/src/tlv_box/tlv_box.cpp +++ b/services/distributed/src/tlv_box/tlv_box.cpp @@ -76,6 +76,11 @@ TlvItem::TlvItem(int32_t type, std::string value) : type_(type) Initialize(value.c_str(), value.size() + 1); } +TlvItem::TlvItem(int32_t type, std::vector value) : type_(type) +{ + Initialize(&value, value.size()); +} + TlvItem::TlvItem(int32_t type, const TlvItem& value) : type_(type) { Initialize(value.GetValue(), value.GetLength()); @@ -145,6 +150,16 @@ bool TlvBox::GetStringValue(int32_t type, std::string& value) return false; } +bool TlvBox::GetVectorValue(int32_t type, std::vector& value) +{ + auto iter = TlvMap_.find(type); + if (iter != TlvMap_.end()) { + value = *reinterpret_cast *>(iter->second->GetValue()); + return true; + } + return false; +} + bool TlvBox::GetInt32Value(int32_t type, int32_t& value) { auto iter = TlvMap_.find(type); -- Gitee