From 5f802cd2f3f727cfe2c77e2daf99398516d1dc08 Mon Sep 17 00:00:00 2001 From: wangsen1994 Date: Wed, 9 Apr 2025 11:29:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=94=E7=94=A8=E5=9B=BE=E6=A0=87=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangsen1994 --- .../distributed_extension_service.cpp | 4 + .../include/soft_bus/distributed_client.h | 3 +- .../include/soft_bus/distributed_service.h | 8 +- .../src/soft_bus/distributed_client.cpp | 30 +++- .../src/soft_bus/distributed_cmd_service.cpp | 137 +++++++++-------- .../soft_bus/distributed_publish_service.cpp | 20 +-- .../soft_bus/distributed_service_uitls.cpp | 13 +- .../distributed_subscribe_service.cpp | 145 +++++++++--------- 8 files changed, 198 insertions(+), 162 deletions(-) diff --git a/services/ans/src/notification_extension/distributed_extension_service.cpp b/services/ans/src/notification_extension/distributed_extension_service.cpp index 39c464b41..e3601d5a3 100644 --- a/services/ans/src/notification_extension/distributed_extension_service.cpp +++ b/services/ans/src/notification_extension/distributed_extension_service.cpp @@ -373,6 +373,10 @@ void DistributedExtensionService::OnDeviceChanged(const DmDeviceInfo &deviceInfo return; } handler(deviceInfo.deviceId, deviceInfo.deviceTypeId, deviceInfo.networkId); + std::string reason = "deviceType: " + std::to_string(deviceInfo.deviceTypeId) + + " deviceId: " + StringAnonymous(deviceInfo.deviceId) + " networkId: " + + StringAnonymous(deviceInfo.networkId); + HADotCallback(PUBLISH_ERROR_EVENT_CODE, 0, EventSceneId::SCENE_3, reason); ANS_LOGI("Dans refresh %{public}s %{public}s.", StringAnonymous(deviceInfo.deviceId).c_str(), StringAnonymous(deviceInfo.networkId).c_str()); }); diff --git a/services/distributed/include/soft_bus/distributed_client.h b/services/distributed/include/soft_bus/distributed_client.h index f5f1693b0..951166f95 100644 --- a/services/distributed/include/soft_bus/distributed_client.h +++ b/services/distributed/include/soft_bus/distributed_client.h @@ -20,6 +20,7 @@ #include #include #include "distributed_device_data.h" +#include "box_base.h" namespace OHOS { namespace Notification { @@ -34,7 +35,7 @@ public: const std::string &networkId); void initClient(const std::string &deviceId, uint16_t deviceType); int32_t GetSocketId(const std::string &deviceId, TransDataType dataType, int32_t& socketId); - int32_t SendMessage(const void* data, int32_t length, TransDataType dataType, + int32_t SendMessage(const std::shared_ptr& boxPtr, TransDataType dataType, const std::string &deviceId, int32_t eventType); void ReleaseClient(); private: diff --git a/services/distributed/include/soft_bus/distributed_service.h b/services/distributed/include/soft_bus/distributed_service.h index a3ee118f1..14b48ee78 100644 --- a/services/distributed/include/soft_bus/distributed_service.h +++ b/services/distributed/include/soft_bus/distributed_service.h @@ -53,9 +53,9 @@ public: void HandleMatchSync(const std::shared_ptr& boxMessage); void DestoryService(); void ReportBundleIconList(const DistributedDeviceInfo peerDevice); - void UpdateBundlesIcon(const std::unordered_map& icons, + int32_t UpdateBundlesIcon(const std::unordered_map& icons, const DistributedDeviceInfo peerDevice); - void RequestBundlesIcon(const DistributedDeviceInfo peerDevice); + void RequestBundlesIcon(const DistributedDeviceInfo peerDevice, bool isForce); void HandleBundlesEvent(const std::string& bundleName, const std::string& action); void HandleBundleChanged(const std::string& bundleName, bool updatedExit); std::string GetNotificationKey(const std::shared_ptr& notification); @@ -89,7 +89,7 @@ private: void RemoveNotification(const std::shared_ptr& boxMessage); void RemoveNotifications(const std::shared_ptr& boxMessage); void SetNotificationContent(const std::shared_ptr &content, - NotificationContent::Type type, NotifticationRequestBox &requestBox); + NotificationContent::Type type, std::shared_ptr& requestBox); void GetNeedUpdateDevice(bool updatedExit, const std::string& bundleName, std::vector& updateDeviceList); void TriggerJumpApplication(const std::string& hashCode); @@ -98,7 +98,7 @@ private: void ReplyOperationResponse(const std::string& hashCode, const NotificationResponseBox& responseBox, OperationType operationType, uint32_t result); void SetNotificationButtons(const sptr notificationRequest, - NotificationConstant::SlotType slotType, NotifticationRequestBox &requestBox); + NotificationConstant::SlotType slotType, std::shared_ptr& requestBox); void MakeNotificationButtons(const NotifticationRequestBox& box, NotificationConstant::SlotType slotType, sptr& request); int32_t GetCurrentActiveUserId(); diff --git a/services/distributed/src/soft_bus/distributed_client.cpp b/services/distributed/src/soft_bus/distributed_client.cpp index 3e611d30c..898da7598 100644 --- a/services/distributed/src/soft_bus/distributed_client.cpp +++ b/services/distributed/src/soft_bus/distributed_client.cpp @@ -59,6 +59,10 @@ void DistributedClient::AddDevice(DistributedDeviceInfo peerDevice) ANS_LOGI("Distributed client AddDevice %{public}s %{public}s", StringAnonymous(peerDevice.deviceId_).c_str(), StringAnonymous(peerDevice.networkId_).c_str()); networksId_[peerDevice.deviceId_] = peerDevice.networkId_; + std::string message = "AddnetworkId: " + StringAnonymous(peerDevice.deviceId_) + " id: " + + StringAnonymous(peerDevice.networkId_); + AnalyticsUtil::GetInstance().SendHaReport(MODIFY_ERROR_EVENT_CODE, 0, + BRANCH6_ID, message, PUBLISH_ERROR_EVENT_CODE); } void DistributedClient::ReleaseDevice(const std::string &deviceId, uint16_t deviceType) @@ -77,6 +81,9 @@ void DistributedClient::ReleaseDevice(const std::string &deviceId, uint16_t devi socketsId_.erase(socket); } networksId_.erase(deviceId); + std::string message = "ReleasenetworkId: " + StringAnonymous(deviceId); + AnalyticsUtil::GetInstance().SendHaReport(MODIFY_ERROR_EVENT_CODE, 0, + BRANCH7_ID, message, PUBLISH_ERROR_EVENT_CODE); } void DistributedClient::RefreshDevice(const std::string &deviceId, uint16_t deviceType, @@ -87,6 +94,10 @@ void DistributedClient::RefreshDevice(const std::string &deviceId, uint16_t devi networksId_[deviceId] = networkId; ANS_LOGI("Distributed refresh device %{public}s %{public}s", StringAnonymous(deviceId).c_str(), StringAnonymous(networkId).c_str()); + std::string message = "RefreshnetworkId: " + StringAnonymous(deviceId) + " id: " + + StringAnonymous(networkId); + AnalyticsUtil::GetInstance().SendHaReport(MODIFY_ERROR_EVENT_CODE, 0, + BRANCH8_ID, message, PUBLISH_ERROR_EVENT_CODE); } int32_t DistributedClient::GetSocketId(const std::string &deviceId, TransDataType dataType, int32_t& socketId) @@ -111,6 +122,10 @@ int32_t DistributedClient::GetSocketId(const std::string &deviceId, TransDataTyp if (result != ERR_OK) { ANS_LOGW("Get socketid failed %{public}s %{public}s %{public}d", StringAnonymous(deviceId).c_str(), StringAnonymous(networkId).c_str(), dataType); + std::string message = "Get socketid failed: " + StringAnonymous(deviceId) + " id: " + + StringAnonymous(networkId); + AnalyticsUtil::GetInstance().SendHaReport(OPERATION_DELETE_BRANCH, result, + BRANCH8_ID, message, PUBLISH_ERROR_EVENT_CODE); return result; } { @@ -121,24 +136,27 @@ int32_t DistributedClient::GetSocketId(const std::string &deviceId, TransDataTyp return ERR_OK; } -int32_t DistributedClient::SendMessage(const void* data, int32_t length, TransDataType dataType, +int32_t DistributedClient::SendMessage(const std::shared_ptr& boxPtr, TransDataType dataType, const std::string &deviceId, int32_t eventType) { + int32_t type = -1; int32_t socketId = 0; DistributedServer::GetInstance().CheckServer(); int32_t result = GetSocketId(deviceId, dataType, socketId); + if (boxPtr == nullptr || boxPtr->box_ == nullptr) { + return ERR_OK; + } + boxPtr->box_->GetMessageType(type); if (result != ERR_OK) { ANS_LOGW("Get SocketId failed %{public}s %{public}d", StringAnonymous(deviceId).c_str(), dataType); - std::string errorReason = "Bind server failed,"; - errorReason.append("dataType: " + std::to_string(dataType)); + std::string errorReason = "Bind failed type: " + std::to_string(type) + " , id: " + StringAnonymous(deviceId); AnalyticsUtil::GetInstance().SendEventReport(0, result, errorReason); AnalyticsUtil::GetInstance().SendHaReport(eventType, result, BRANCH1_ID, errorReason); return result; } - result = ClientSendMsg(socketId, data, length, dataType); + result = ClientSendMsg(socketId, boxPtr->GetByteBuffer(), boxPtr->GetByteLength(), dataType); if (result != ERR_OK) { - std::string errorReason = "send message failed,"; - errorReason.append("dataType: " + std::to_string(dataType)); + std::string errorReason = "Send failed type: " + std::to_string(type) + " , id: " + StringAnonymous(deviceId); AnalyticsUtil::GetInstance().SendEventReport(0, result, errorReason); AnalyticsUtil::GetInstance().SendHaReport(eventType, result, BRANCH2_ID, errorReason); } diff --git a/services/distributed/src/soft_bus/distributed_cmd_service.cpp b/services/distributed/src/soft_bus/distributed_cmd_service.cpp index f1b1ac1d8..755f06561 100644 --- a/services/distributed/src/soft_bus/distributed_cmd_service.cpp +++ b/services/distributed/src/soft_bus/distributed_cmd_service.cpp @@ -86,17 +86,16 @@ void DistributedService::SyncDeviceState(int32_t state) return; } std::function task = std::bind([&, state]() { - NotifticationStateBox stateBox; - stateBox.SetState(state); - stateBox.SetDeviceType(TransDeviceTypeIdToName(localDevice_.deviceType_)); - stateBox.SetDeviceId(localDevice_.deviceId_); - if (!stateBox.Serialize()) { + std::shared_ptr stateBox = std::make_shared(); + stateBox->SetState(state); + stateBox->SetDeviceType(TransDeviceTypeIdToName(localDevice_.deviceType_)); + stateBox->SetDeviceId(localDevice_.deviceId_); + if (!stateBox->Serialize()) { ANS_LOGW("Dans SyncDeviceState serialize failed."); return; } for (const auto& peer : peerDevice_) { - DistributedClient::GetInstance().SendMessage(stateBox.GetByteBuffer(), - stateBox.GetByteLength(), TransDataType::DATA_TYPE_MESSAGE, + DistributedClient::GetInstance().SendMessage(stateBox, TransDataType::DATA_TYPE_MESSAGE, peer.second.deviceId_, peer.second.deviceType_); ANS_LOGI("Dans SyncDeviceState %{public}d %{public}d %{public}d %{public}d.", peer.second.deviceType_, localDevice_.deviceType_, state, (int32_t)(peerDevice_.size())); @@ -107,21 +106,20 @@ void DistributedService::SyncDeviceState(int32_t state) int32_t DistributedService::SyncDeviceMatch(const DistributedDeviceInfo peerDevice, MatchType type) { - NotifticationMatchBox matchBox; - matchBox.SetVersion(CURRENT_VERSION); - matchBox.SetMatchType(type); - matchBox.SetLocalDeviceId(localDevice_.deviceId_); - matchBox.SetLocalDeviceType(localDevice_.deviceType_); + std::shared_ptr matchBox = std::make_shared(); + matchBox->SetVersion(CURRENT_VERSION); + matchBox->SetMatchType(type); + matchBox->SetLocalDeviceId(localDevice_.deviceId_); + matchBox->SetLocalDeviceType(localDevice_.deviceType_); if (type == MatchType::MATCH_ACK) { - matchBox.SetPeerDeviceId(peerDevice.deviceId_); - matchBox.SetPeerDeviceType(peerDevice.deviceType_); + matchBox->SetPeerDeviceId(peerDevice.deviceId_); + matchBox->SetPeerDeviceType(peerDevice.deviceType_); } - if (!matchBox.Serialize()) { + if (!matchBox->Serialize()) { ANS_LOGW("Dans SyncDeviceMatch serialize failed."); return -1; } - int32_t result = DistributedClient::GetInstance().SendMessage(matchBox.GetByteBuffer(), - matchBox.GetByteLength(), TransDataType::DATA_TYPE_MESSAGE, + int32_t result = DistributedClient::GetInstance().SendMessage(matchBox, TransDataType::DATA_TYPE_MESSAGE, peerDevice.deviceId_, peerDevice.deviceType_); ANS_LOGI("Dans SyncDeviceMatch %{public}s %{public}d %{public}s %{public}d %{public}d.", StringAnonymous(peerDevice.deviceId_).c_str(), peerDevice.deviceType_, @@ -180,20 +178,19 @@ void DistributedService::SyncNotifictionList(const DistributedDeviceInfo& peerDe const std::vector& notificationList) { ANS_LOGI("Dans sync notification %{public}d.", (int32_t)(notificationList.size())); - NotificationSyncBox notificationSyncBox; - notificationSyncBox.SetLocalDeviceId(peerDevice.deviceId_); - notificationSyncBox.SetNotificationEmpty(notificationList.empty()); + std::shared_ptr notificationSyncBox = std::make_shared(); + notificationSyncBox->SetLocalDeviceId(peerDevice.deviceId_); + notificationSyncBox->SetNotificationEmpty(notificationList.empty()); if (!notificationList.empty()) { - notificationSyncBox.SetNotificationList(notificationList); + notificationSyncBox->SetNotificationList(notificationList); } - if (!notificationSyncBox.Serialize()) { + if (!notificationSyncBox->Serialize()) { ANS_LOGW("Dans SyncNotifictionList serialize failed."); return; } - int32_t result = DistributedClient::GetInstance().SendMessage(notificationSyncBox.GetByteBuffer(), - notificationSyncBox.GetByteLength(), TransDataType::DATA_TYPE_BYTES, - peerDevice.deviceId_, peerDevice.deviceType_); + int32_t result = DistributedClient::GetInstance().SendMessage(notificationSyncBox, + TransDataType::DATA_TYPE_BYTES, peerDevice.deviceId_, peerDevice.deviceType_); ANS_LOGI("Dans SyncNotifictionList %{public}s %{public}d %{public}s %{public}d.", StringAnonymous(peerDevice.deviceId_).c_str(), peerDevice.deviceType_, StringAnonymous(localDevice_.deviceId_).c_str(), localDevice_.deviceType_); @@ -222,11 +219,11 @@ void DistributedService::HandleMatchSync(const std::shared_ptr& boxMessa ANS_LOGI("Dans handle match device type %{public}d.", matchType); if (matchType == MatchType::MATCH_SYN) { SyncDeviceMatch(peerDevice, MatchType::MATCH_ACK); + RequestBundlesIcon(peerDevice, true); SyncAllLiveViewNotification(peerDevice, true); } else if (matchType == MatchType::MATCH_ACK) { InitDeviceState(peerDevice); - RequestBundlesIcon(peerDevice); - ReportBundleIconList(peerDevice); + RequestBundlesIcon(peerDevice, false); SubscribeNotifictaion(peerDevice); SyncAllLiveViewNotification(peerDevice, false); } @@ -262,61 +259,71 @@ void DistributedService::ReportBundleIconList(const DistributedDeviceInfo peerDe } std::vector bundlesName; DistributedPreferences::GetInstance().GetSavedBundlesIcon(bundlesName); - BundleIconBox iconBox; - iconBox.SetIconSyncType(IconSyncType::REPORT_SAVED_ICON); - iconBox.SetBundleList(bundlesName); - iconBox.SetLocalDeviceId(localDevice_.deviceId_); - if (!iconBox.Serialize()) { + std::shared_ptr iconBox = std::make_shared(); + iconBox->SetIconSyncType(IconSyncType::REPORT_SAVED_ICON); + iconBox->SetBundleList(bundlesName); + iconBox->SetLocalDeviceId(localDevice_.deviceId_); + if (!iconBox->Serialize()) { ANS_LOGW("Dans ReportBundleIconList serialize failed."); return; } - DistributedClient::GetInstance().SendMessage(iconBox.GetByteBuffer(), - iconBox.GetByteLength(), TransDataType::DATA_TYPE_MESSAGE, + DistributedClient::GetInstance().SendMessage(iconBox, TransDataType::DATA_TYPE_MESSAGE, peerDevice.deviceId_, peerDevice.deviceType_); ANS_LOGI("Dans ReportBundleIconList %{public}s %{public}d %{public}s %{public}d.", StringAnonymous(peerDevice.deviceId_).c_str(), peerDevice.deviceType_, StringAnonymous(localDevice_.deviceId_).c_str(), localDevice_.deviceType_); } -void DistributedService::RequestBundlesIcon(const DistributedDeviceInfo peerDevice) +void DistributedService::RequestBundlesIcon(const DistributedDeviceInfo peerDevice, bool isForce) { if (localDevice_.deviceType_ != DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) { return; } - BundleIconBox iconBox; - iconBox.SetIconSyncType(IconSyncType::REQUEST_BUNDLE_ICON); - iconBox.SetLocalDeviceId(localDevice_.deviceId_); - if (!iconBox.Serialize()) { + + auto iter = peerDevice_.find(peerDevice.deviceId_); + if (iter == peerDevice_.end()) { + ANS_LOGI("Dans %{public}s.", StringAnonymous(peerDevice.deviceId_).c_str()); + return; + } + + if (!isForce && iter->second.isSync) { + ANS_LOGI("Dans %{public}d %{public}d.", isForce, iter->second.isSync); + return; + } + + std::shared_ptr iconBox = std::make_shared(); + iconBox->SetIconSyncType(IconSyncType::REQUEST_BUNDLE_ICON); + iconBox->SetLocalDeviceId(localDevice_.deviceId_); + if (!iconBox->Serialize()) { ANS_LOGW("Dans RequestBundlesIcon serialize failed."); return; } - DistributedClient::GetInstance().SendMessage(iconBox.GetByteBuffer(), - iconBox.GetByteLength(), TransDataType::DATA_TYPE_MESSAGE, + DistributedClient::GetInstance().SendMessage(iconBox, TransDataType::DATA_TYPE_MESSAGE, peerDevice.deviceId_, peerDevice.deviceType_); ANS_LOGI("Dans RequestBundlesIcon %{public}s %{public}d %{public}s %{public}d.", StringAnonymous(peerDevice.deviceId_).c_str(), peerDevice.deviceType_, StringAnonymous(localDevice_.deviceId_).c_str(), localDevice_.deviceType_); } -void DistributedService::UpdateBundlesIcon(const std::unordered_map& icons, +int32_t DistributedService::UpdateBundlesIcon(const std::unordered_map& icons, const DistributedDeviceInfo peerDevice) { - BundleIconBox iconBox; - iconBox.SetIconSyncType(IconSyncType::UPDATE_BUNDLE_ICON); - iconBox.SetBundlesIcon(icons); - if (!iconBox.Serialize()) { + std::shared_ptr iconBox = std::make_shared(); + iconBox->SetIconSyncType(IconSyncType::UPDATE_BUNDLE_ICON); + iconBox->SetBundlesIcon(icons); + if (!iconBox->Serialize()) { ANS_LOGW("Dans UpdateBundlesIcon serialize failed."); - return; + return -1; } - DistributedClient::GetInstance().SendMessage(iconBox.GetByteBuffer(), - iconBox.GetByteLength(), TransDataType::DATA_TYPE_BYTES, + int32_t result = DistributedClient::GetInstance().SendMessage(iconBox, TransDataType::DATA_TYPE_BYTES, peerDevice.deviceId_, peerDevice.deviceType_); - ANS_LOGI("Dans UpdateBundlesIcon %{public}s %{public}d %{public}s %{public}d.", + ANS_LOGI("Dans UpdateBundlesIcon %{public}s %{public}d %{public}s %{public}d %{public}d.", StringAnonymous(peerDevice.deviceId_).c_str(), peerDevice.deviceType_, - StringAnonymous(localDevice_.deviceId_).c_str(), localDevice_.deviceType_); + StringAnonymous(localDevice_.deviceId_).c_str(), localDevice_.deviceType_, result); + return result; } void DistributedService::GenerateBundleIconSync(const DistributedDeviceInfo& device) @@ -338,38 +345,40 @@ void DistributedService::GenerateBundleIconSync(const DistributedDeviceInfo& dev } std::set cachedIcons; std::vector unCachedBundleList; - auto itemIter = bundleIconCache_.find(device.deviceId_); - if (itemIter != bundleIconCache_.end()) { - cachedIcons = itemIter->second; + if (bundleIconCache_.find(device.deviceId_) != bundleIconCache_.end()) { + cachedIcons = bundleIconCache_[device.deviceId_]; } for (auto item : bundleOption) { - if (enabledBundles.find(item.GetBundleName()) != enabledBundles.end()) { - continue; - } - if (cachedIcons.find(item.GetBundleName()) != cachedIcons.end()) { + if (enabledBundles.find(item.GetBundleName()) != enabledBundles.end() || + cachedIcons.find(item.GetBundleName()) != cachedIcons.end()) { continue; } unCachedBundleList.push_back(item.GetBundleName()); - cachedIcons.insert(item.GetBundleName()); } - bundleIconCache_.insert(std::make_pair(device.deviceId_, cachedIcons)); + ANS_LOGI("Dans Generate bundle %{public}d %{public}d %{public}d.", (int32_t)(bundleOption.size()), (int32_t)(enableBundleOption.size()), (int32_t)(unCachedBundleList.size())); + std::vector sendIcon; std::unordered_map icons; for (auto bundle : unCachedBundleList) { std::string icon; if (!GetBundleResourceInfo(bundle, icon)) { continue; } + sendIcon.push_back(bundle); icons.insert(std::make_pair(bundle, icon)); if (icons.size() == BundleIconBox::MAX_ICON_NUM) { - UpdateBundlesIcon(icons, device); + if (UpdateBundlesIcon(icons, device) == ERR_OK) { + cachedIcons.insert(sendIcon.begin(), sendIcon.end()); + } icons.clear(); + sendIcon.clear(); } } - if (!icons.empty()) { - UpdateBundlesIcon(icons, device); + if (!icons.empty() && UpdateBundlesIcon(icons, device) == ERR_OK) { + cachedIcons.insert(sendIcon.begin(), sendIcon.end()); } + bundleIconCache_[device.deviceId_] = cachedIcons; } void DistributedService::HandleBundleIconSync(const std::shared_ptr& boxMessage) @@ -395,7 +404,7 @@ void DistributedService::HandleBundleIconSync(const std::shared_ptr& box ANS_LOGI("Dans handle receive %{public}s.", bundle.c_str()); bundleSet.insert(bundle); } - bundleIconCache_.insert(std::make_pair(device.deviceId_, bundleSet)); + bundleIconCache_[device.deviceId_] = bundleSet; GenerateBundleIconSync(device); } diff --git a/services/distributed/src/soft_bus/distributed_publish_service.cpp b/services/distributed/src/soft_bus/distributed_publish_service.cpp index 3ae05840f..48ccb03a7 100644 --- a/services/distributed/src/soft_bus/distributed_publish_service.cpp +++ b/services/distributed/src/soft_bus/distributed_publish_service.cpp @@ -607,19 +607,19 @@ void DistributedService::ReplyOperationResponse(const std::string& hashCode, return; } - NotificationResponseBox replyBox; - replyBox.SetResponseResult(result); - replyBox.SetNotificationHashCode(hashCode); - replyBox.SetOperationEventId(eventId); - replyBox.SetMatchType(MatchType::MATCH_ACK); - replyBox.SetOperationType(operationType); - - if (!replyBox.Serialize()) { + std::shared_ptr replyBox = std::make_shared(); + replyBox->SetResponseResult(result); + replyBox->SetNotificationHashCode(hashCode); + replyBox->SetOperationEventId(eventId); + replyBox->SetMatchType(MatchType::MATCH_ACK); + replyBox->SetOperationType(operationType); + + if (!replyBox->Serialize()) { ANS_LOGW("dans OnResponse reply serialize failed"); return; } - auto ret = DistributedClient::GetInstance().SendMessage(replyBox.GetByteBuffer(), replyBox.GetByteLength(), - TransDataType::DATA_TYPE_MESSAGE, iter->second.deviceId_, iter->second.deviceType_); + auto ret = DistributedClient::GetInstance().SendMessage(replyBox, TransDataType::DATA_TYPE_MESSAGE, + iter->second.deviceId_, iter->second.deviceType_); if (ret != ERR_OK) { ANS_LOGE("dans OnResponse send message failed result: %{public}d", ret); return; diff --git a/services/distributed/src/soft_bus/distributed_service_uitls.cpp b/services/distributed/src/soft_bus/distributed_service_uitls.cpp index 3c0e406a9..6208a797e 100644 --- a/services/distributed/src/soft_bus/distributed_service_uitls.cpp +++ b/services/distributed/src/soft_bus/distributed_service_uitls.cpp @@ -134,17 +134,16 @@ void DistributedService::HandleBundleRemoved(const std::string& bundleName) if (device.second.deviceType_ == DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) { continue; } - BundleIconBox iconBox; - iconBox.SetIconSyncType(IconSyncType::REMOVE_BUNDLE_ICON); - iconBox.SetBundleList({bundleName}); - iconBox.SetLocalDeviceId(localDevice_.deviceId_); - if (!iconBox.Serialize()) { + std::shared_ptr iconBox = std::make_shared(); + iconBox->SetIconSyncType(IconSyncType::REMOVE_BUNDLE_ICON); + iconBox->SetBundleList({bundleName}); + iconBox->SetLocalDeviceId(localDevice_.deviceId_); + if (!iconBox->Serialize()) { ANS_LOGW("Dans HandleBundleRemove serialize failed."); continue; } - DistributedClient::GetInstance().SendMessage(iconBox.GetByteBuffer(), - iconBox.GetByteLength(), TransDataType::DATA_TYPE_MESSAGE, + DistributedClient::GetInstance().SendMessage(iconBox, TransDataType::DATA_TYPE_MESSAGE, device.second.deviceId_, device.second.deviceType_); ANS_LOGI("Dans ReportBundleIconList %{public}s %{public}d %{public}s %{public}d.", StringAnonymous(device.second.deviceId_).c_str(), device.second.deviceType_, diff --git a/services/distributed/src/soft_bus/distributed_subscribe_service.cpp b/services/distributed/src/soft_bus/distributed_subscribe_service.cpp index dee72930b..0cec6cae7 100644 --- a/services/distributed/src/soft_bus/distributed_subscribe_service.cpp +++ b/services/distributed/src/soft_bus/distributed_subscribe_service.cpp @@ -30,16 +30,13 @@ #include "ans_inner_errors.h" #include "remove_box.h" #include "response_box.h" +#include "analytics_util.h" namespace OHOS { namespace Notification { const std::string DISTRIBUTED_LABEL = "ans_distributed"; const int32_t DEFAULT_FILTER_TYPE = 1; -constexpr const int32_t PUBLISH_ERROR_EVENT_CODE = 0; -constexpr const int32_t DELETE_ERROR_EVENT_CODE = 5; -constexpr const int32_t MODIFY_ERROR_EVENT_CODE = 6; -constexpr const int32_t BRANCH3_ID = 3; std::string SubscribeTransDeviceType(uint16_t deviceType) { @@ -91,6 +88,10 @@ void DistributedService::SubscribeNotifictaion(const DistributedDeviceInfo peerD haCallback_(PUBLISH_ERROR_EVENT_CODE, 0, BRANCH3_ID, reason); } } + std::string message = "Subscribe: " + StringAnonymous(peerDevice.deviceId_) + " type: " + + std::to_string(peerDevice.deviceType_) + ", userId: " + std::to_string(userId); + AnalyticsUtil::GetInstance().SendHaReport(OPERATION_DELETE_BRANCH, result, BRANCH1_ID, message, + PUBLISH_ERROR_EVENT_CODE); ANS_LOGI("Subscribe notification %{public}s %{public}d %{public}d %{public}d.", StringAnonymous(peerDevice.deviceId_).c_str(), peerDevice.deviceType_, userId, result); } @@ -114,16 +115,20 @@ void DistributedService::UnSubscribeNotifictaion(const std::string &deviceId, ui return; } - if (NotificationHelper::UnSubscribeNotification(iter->second) == 0) { + int32_t result = NotificationHelper::UnSubscribeNotification(iter->second); + if (result == ERR_OK) { subscriberMap_.erase(deviceId); } + std::string message = "UnSubscribe: " + StringAnonymous(deviceId) + " type: " + std::to_string(deviceType); + AnalyticsUtil::GetInstance().SendHaReport(OPERATION_DELETE_BRANCH, result, BRANCH2_ID, message, + PUBLISH_ERROR_EVENT_CODE); ANS_LOGI("UnSubscribe notification %{public}s %{public}d.", StringAnonymous(deviceId).c_str(), deviceType); }); serviceQueue_->submit(subscribeTask); } void DistributedService::SetNotificationContent(const std::shared_ptr &content, - NotificationContent::Type type, NotifticationRequestBox &requestBox) + NotificationContent::Type type, std::shared_ptr& requestBox) { if (content == nullptr || content->GetNotificationContent() == nullptr) { return; @@ -133,33 +138,33 @@ void DistributedService::SetNotificationContent(const std::shared_ptr(content->GetNotificationContent()); - requestBox.SetNotificationTitle(picture->GetTitle()); - requestBox.SetNotificationText(picture->GetText()); - requestBox.SetNotificationAdditionalText(picture->GetAdditionalText()); - requestBox.SetNotificationExpandedTitle(picture->GetExpandedTitle()); - requestBox.SetNotificationBriefText(picture->GetBriefText()); - requestBox.SetNotificationBigPicture(picture->GetBigPicture()); + requestBox->SetNotificationTitle(picture->GetTitle()); + requestBox->SetNotificationText(picture->GetText()); + requestBox->SetNotificationAdditionalText(picture->GetAdditionalText()); + requestBox->SetNotificationExpandedTitle(picture->GetExpandedTitle()); + requestBox->SetNotificationBriefText(picture->GetBriefText()); + requestBox->SetNotificationBigPicture(picture->GetBigPicture()); break; } case NotificationContent::Type::MULTILINE: { auto multiline = std::static_pointer_cast(content->GetNotificationContent()); - requestBox.SetNotificationTitle(multiline->GetTitle()); - requestBox.SetNotificationText(multiline->GetText()); - requestBox.SetNotificationAdditionalText(multiline->GetAdditionalText()); - requestBox.SetNotificationExpandedTitle(multiline->GetExpandedTitle()); - requestBox.SetNotificationBriefText(multiline->GetBriefText()); - requestBox.SetNotificationAllLines(multiline->GetAllLines()); + requestBox->SetNotificationTitle(multiline->GetTitle()); + requestBox->SetNotificationText(multiline->GetText()); + requestBox->SetNotificationAdditionalText(multiline->GetAdditionalText()); + requestBox->SetNotificationExpandedTitle(multiline->GetExpandedTitle()); + requestBox->SetNotificationBriefText(multiline->GetBriefText()); + requestBox->SetNotificationAllLines(multiline->GetAllLines()); break; } case NotificationContent::Type::LONG_TEXT: { std::shared_ptr contentLong = std::static_pointer_cast(content->GetNotificationContent()); - requestBox.SetNotificationTitle(contentLong->GetTitle()); - requestBox.SetNotificationText(contentLong->GetText()); - requestBox.SetNotificationAdditionalText(contentLong->GetAdditionalText()); - requestBox.SetNotificationExpandedTitle(contentLong->GetExpandedTitle()); - requestBox.SetNotificationBriefText(contentLong->GetBriefText()); - requestBox.SetNotificationLongText(contentLong->GetLongText()); + requestBox->SetNotificationTitle(contentLong->GetTitle()); + requestBox->SetNotificationText(contentLong->GetText()); + requestBox->SetNotificationAdditionalText(contentLong->GetAdditionalText()); + requestBox->SetNotificationExpandedTitle(contentLong->GetExpandedTitle()); + requestBox->SetNotificationBriefText(contentLong->GetBriefText()); + requestBox->SetNotificationLongText(contentLong->GetLongText()); break; } case NotificationContent::Type::LIVE_VIEW: @@ -168,16 +173,16 @@ void DistributedService::SetNotificationContent(const std::shared_ptr contentBasic = std::static_pointer_cast(content->GetNotificationContent()); - requestBox.SetNotificationTitle(contentBasic->GetTitle()); - requestBox.SetNotificationText(contentBasic->GetText()); - requestBox.SetNotificationAdditionalText(contentBasic->GetAdditionalText()); + requestBox->SetNotificationTitle(contentBasic->GetTitle()); + requestBox->SetNotificationText(contentBasic->GetText()); + requestBox->SetNotificationAdditionalText(contentBasic->GetAdditionalText()); break; } } } void DistributedService::SetNotificationButtons(const sptr notificationRequest, - NotificationConstant::SlotType slotType, NotifticationRequestBox &requestBox) + NotificationConstant::SlotType slotType, std::shared_ptr& requestBox) { if (notificationRequest == nullptr) { return; @@ -198,8 +203,8 @@ void DistributedService::SetNotificationButtons(const sptr } } if (button != nullptr && button->GetUserInput() != nullptr) { - requestBox.SetNotificationActionName(button->GetTitle()); - requestBox.SetNotificationUserInput(button->GetUserInput()->GetInputKey()); + requestBox->SetNotificationActionName(button->GetTitle()); + requestBox->SetNotificationUserInput(button->GetUserInput()->GetInputKey()); } } } @@ -207,51 +212,51 @@ void DistributedService::SetNotificationButtons(const sptr void DistributedService::SendNotifictionRequest(const std::shared_ptr request, const DistributedDeviceInfo& peerDevice, bool isSyncNotification) { - NotifticationRequestBox requestBox; + std::shared_ptr requestBox = std::make_shared(); if (request == nullptr || request->GetNotificationRequestPoint() == nullptr) { return; } auto requestPoint = request->GetNotificationRequestPoint(); ANS_LOGI("Dans OnConsumed %{public}s", requestPoint->Dump().c_str()); - requestBox.SetAutoDeleteTime(requestPoint->GetAutoDeletedTime()); - requestBox.SetFinishTime(requestPoint->GetFinishDeadLine()); - requestBox.SetNotificationHashCode(request->GetKey()); - requestBox.SetSlotType(static_cast(requestPoint->GetSlotType())); - requestBox.SetContentType(static_cast(requestPoint->GetNotificationType())); + requestBox->SetAutoDeleteTime(requestPoint->GetAutoDeletedTime()); + requestBox->SetFinishTime(requestPoint->GetFinishDeadLine()); + requestBox->SetNotificationHashCode(request->GetKey()); + requestBox->SetSlotType(static_cast(requestPoint->GetSlotType())); + requestBox->SetContentType(static_cast(requestPoint->GetNotificationType())); if (isSyncNotification) { - requestBox.SetReminderFlag(0); + requestBox->SetReminderFlag(0); } else { - requestBox.SetReminderFlag(requestPoint->GetFlags()->GetReminderFlags()); + requestBox->SetReminderFlag(requestPoint->GetFlags()->GetReminderFlags()); } if (request->GetBundleName().empty()) { - requestBox.SetCreatorBundleName(request->GetCreateBundle()); + requestBox->SetCreatorBundleName(request->GetCreateBundle()); } else { - requestBox.SetCreatorBundleName(request->GetBundleName()); + requestBox->SetCreatorBundleName(request->GetBundleName()); } if (requestPoint->GetBigIcon() != nullptr) { - requestBox.SetBigIcon(requestPoint->GetBigIcon()); + requestBox->SetBigIcon(requestPoint->GetBigIcon()); } if (requestPoint->GetOverlayIcon() != nullptr) { - requestBox.SetOverlayIcon(requestPoint->GetOverlayIcon()); + requestBox->SetOverlayIcon(requestPoint->GetOverlayIcon()); } if (requestPoint->IsCommonLiveView()) { std::vector buffer; DISTRIBUTED_LIVEVIEW_ALL_SCENARIOS_EXTENTION_WRAPPER->UpdateLiveviewEncodeContent(requestPoint, buffer); - requestBox.SetCommonLiveView(buffer); + requestBox->SetCommonLiveView(buffer); } SetNotificationButtons(requestPoint, requestPoint->GetSlotType(), requestBox); SetNotificationContent(request->GetNotificationRequestPoint()->GetContent(), requestPoint->GetNotificationType(), requestBox); - if (!requestBox.Serialize()) { + if (!requestBox->Serialize()) { ANS_LOGW("Dans OnConsumed serialize failed."); if (haCallback_ != nullptr) { haCallback_(PUBLISH_ERROR_EVENT_CODE, -1, BRANCH3_ID, "serialization failed"); } return; } - DistributedClient::GetInstance().SendMessage(requestBox.GetByteBuffer(), requestBox.GetByteLength(), - TransDataType::DATA_TYPE_BYTES, peerDevice.deviceId_, PUBLISH_ERROR_EVENT_CODE); + DistributedClient::GetInstance().SendMessage(requestBox, TransDataType::DATA_TYPE_BYTES, + peerDevice.deviceId_, PUBLISH_ERROR_EVENT_CODE); } void DistributedService::OnConsumed(const std::shared_ptr &request, @@ -290,18 +295,18 @@ void DistributedService::OnBatchCanceled(const std::vector task = std::bind([peerDevice, notifications, notificationKeys, slotTypes]() { - BatchRemoveNotificationBox batchRemoveBox; + std::shared_ptr batchRemoveBox = std::make_shared(); if (!notificationKeys.empty()) { - batchRemoveBox.SetNotificationHashCode(notificationKeys); + batchRemoveBox->SetNotificationHashCode(notificationKeys); } - batchRemoveBox.SetNotificationSlotTypes(slotTypes); + batchRemoveBox->SetNotificationSlotTypes(slotTypes); - if (!batchRemoveBox.Serialize()) { + if (!batchRemoveBox->Serialize()) { ANS_LOGW("dans OnCanceled serialize failed"); return; } - DistributedClient::GetInstance().SendMessage(batchRemoveBox.GetByteBuffer(), batchRemoveBox.GetByteLength(), - TransDataType::DATA_TYPE_MESSAGE, peerDevice.deviceId_, DELETE_ERROR_EVENT_CODE); + DistributedClient::GetInstance().SendMessage(batchRemoveBox, TransDataType::DATA_TYPE_MESSAGE, + peerDevice.deviceId_, DELETE_ERROR_EVENT_CODE); }); serviceQueue_->submit(task); } @@ -319,16 +324,16 @@ void DistributedService::OnCanceled(const std::shared_ptr& notific } std::string notificationKey = GetNotificationKey(notification); std::function task = std::bind([peerDevice, notification, notificationKey]() { - NotificationRemoveBox removeBox; + std::shared_ptr removeBox = std::make_shared(); ANS_LOGI("dans OnCanceled %{public}s", notification->Dump().c_str()); - removeBox.SetNotificationHashCode(notificationKey); - removeBox.setNotificationSlotType(notification->GetNotificationRequestPoint()->GetSlotType()); - if (!removeBox.Serialize()) { + removeBox->SetNotificationHashCode(notificationKey); + removeBox->setNotificationSlotType(notification->GetNotificationRequestPoint()->GetSlotType()); + if (!removeBox->Serialize()) { ANS_LOGW("dans OnCanceled serialize failed"); return; } - DistributedClient::GetInstance().SendMessage(removeBox.GetByteBuffer(), removeBox.GetByteLength(), - TransDataType::DATA_TYPE_MESSAGE, peerDevice.deviceId_, DELETE_ERROR_EVENT_CODE); + DistributedClient::GetInstance().SendMessage(removeBox, TransDataType::DATA_TYPE_MESSAGE, + peerDevice.deviceId_, DELETE_ERROR_EVENT_CODE); }); serviceQueue_->submit(task); } @@ -354,7 +359,7 @@ std::string DistributedService::GetNotificationKey(const std::shared_ptr & operationInfo, const DistributedDeviceInfo& device) { - NotificationResponseBox responseBox; + std::shared_ptr responseBox = std::make_shared(); ANS_LOGI("dans OnResponse %{public}s", operationInfo->Dump().c_str()); if (operationInfo == nullptr) { return ERR_ANS_INVALID_PARAM; @@ -366,26 +371,26 @@ ErrCode DistributedService::OnResponse( OperationType type = operationInfo->GetOperationType(); if (type == OperationType::DISTRIBUTE_OPERATION_REPLY) { - if (!responseBox.SetMessageType(NOTIFICATION_RESPONSE_REPLY_SYNC)) { + if (!responseBox->SetMessageType(NOTIFICATION_RESPONSE_REPLY_SYNC)) { ANS_LOGW("dans OnResponse SetMessageType failed"); return ERR_ANS_TASK_ERR; } - responseBox.SetActionName(operationInfo->GetActionName()); - responseBox.SetUserInput(operationInfo->GetUserInput()); + responseBox->SetActionName(operationInfo->GetActionName()); + responseBox->SetUserInput(operationInfo->GetUserInput()); } - responseBox.SetMatchType(MatchType::MATCH_SYN); - responseBox.SetOperationType(static_cast(type)); - responseBox.SetNotificationHashCode(hashCode); - responseBox.SetOperationEventId(operationInfo->GetEventId()); - responseBox.SetLocalDeviceId(localDevice_.deviceId_); - if (!responseBox.Serialize()) { + responseBox->SetMatchType(MatchType::MATCH_SYN); + responseBox->SetOperationType(static_cast(type)); + responseBox->SetNotificationHashCode(hashCode); + responseBox->SetOperationEventId(operationInfo->GetEventId()); + responseBox->SetLocalDeviceId(localDevice_.deviceId_); + if (!responseBox->Serialize()) { ANS_LOGW("dans OnResponse serialize failed"); return ERR_ANS_TASK_ERR; } - auto result = DistributedClient::GetInstance().SendMessage(responseBox.GetByteBuffer(), responseBox.GetByteLength(), - TransDataType::DATA_TYPE_MESSAGE, device.deviceId_, MODIFY_ERROR_EVENT_CODE); + auto result = DistributedClient::GetInstance().SendMessage(responseBox, TransDataType::DATA_TYPE_MESSAGE, + device.deviceId_, MODIFY_ERROR_EVENT_CODE); if (result != ERR_OK) { ANS_LOGE("dans OnResponse send message failed result: %{public}d", result); result = ERR_ANS_DISTRIBUTED_OPERATION_FAILED; -- Gitee