diff --git a/services/distributed/BUILD.gn b/services/distributed/BUILD.gn index 185f5f3c7d614926229450360598571372d24c26..fc708009495570b037d99bdc4520a7000239b71e 100644 --- a/services/distributed/BUILD.gn +++ b/services/distributed/BUILD.gn @@ -108,13 +108,18 @@ ohos_shared_library("libdans") { "src/helper/distributed_operation_helper.cpp", "src/helper/distributed_preference.cpp", "src/helper/distributed_rdb_helper.cpp", + "src/soft_bus/distributed_bundle_service.cpp", "src/soft_bus/distributed_cmd_service.cpp", + "src/soft_bus/distributed_device_service.cpp", "src/soft_bus/distributed_extern_intferface.cpp", "src/soft_bus/distributed_observer_service.cpp", + "src/soft_bus/distributed_operation_service.cpp", "src/soft_bus/distributed_publish_service.cpp", + "src/soft_bus/distributed_publish_service_v2.cpp", "src/soft_bus/distributed_service.cpp", "src/soft_bus/distributed_service_uitls.cpp", "src/soft_bus/distributed_subscribe_service.cpp", + "src/soft_bus/distributed_subscribe_service_v2.cpp", "src/soft_bus/distributed_subscriber.cpp", "src/tlv_box/batch_remove_box.cpp", "src/tlv_box/box_base.cpp", diff --git a/services/distributed/include/soft_bus/distributed_bundle_service.h b/services/distributed/include/soft_bus/distributed_bundle_service.h new file mode 100644 index 0000000000000000000000000000000000000000..be709f532eaad8eeafa2d5bd24bce3fede8c9168 --- /dev/null +++ b/services/distributed/include/soft_bus/distributed_bundle_service.h @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2025 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 DISTRIBUTED_INCLUDE_SOFTBUS_DISTRIBUTED_BUNDLE_SERVICE_H +#define DISTRIBUTED_INCLUDE_SOFTBUS_DISTRIBUTED_BUNDLE_SERVICE_H + +#include +#include + +#include "distributed_device_data.h" + +namespace OHOS { +namespace Notification { +class DistributedBundleService { +public: + static DistributedBundleService& GetInstance(); + +#ifdef DISTRIBUTED_FEATURE_MASTER + void RequestBundlesIcon(const DistributedDeviceInfo peerDevice, bool isForce); + void GenerateBundleIconSync(const DistributedDeviceInfo& device); + void HandleBundleRemoved(const std::string& bundleName); + void HandleBundleChanged(const std::string& bundleName, bool updatedExit); +private: + int32_t UpdateBundlesIcon(const std::unordered_map& icons, + const DistributedDeviceInfo peerDevice); + bool GetBundleResourceInfo(const std::string bundleName, std::string& icon); + void GetNeedUpdateDevice(bool updatedExit, const std::string& bundleName, + std::vector& updateDeviceList); +#else + void ReportBundleIconList(const DistributedDeviceInfo peerDevice); +#endif + +private: + std::map> bundleIconCache_; +}; +} +} +#endif // DISTRIBUTED_INCLUDE_SOFTBUS_DISTRIBUTED_BUNDLE_SERVICE_H diff --git a/services/distributed/include/soft_bus/distributed_device_service.h b/services/distributed/include/soft_bus/distributed_device_service.h new file mode 100644 index 0000000000000000000000000000000000000000..6a0bf7d9e6a33d32b8978f20442b679c031be117 --- /dev/null +++ b/services/distributed/include/soft_bus/distributed_device_service.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2025 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 DISTRIBUTED_INCLUDE_SOFTBUS_DISTRIBUTED_DEVICE_SERVICE_H +#define DISTRIBUTED_INCLUDE_SOFTBUS_DISTRIBUTED_DEVICE_SERVICE_H + +#include "distributed_device_data.h" + +#include "match_box.h" + +namespace OHOS { +namespace Notification { +class DistributedDeviceService { +public: + static DistributedDeviceService& GetInstance(); + static std::string DeviceTypeToTypeString(uint16_t deviceType); + void InitLocalDevice(const std::string &deviceId, uint16_t deviceType); + DistributedDeviceInfo GetLocalDevice(); + bool IsDeviceSyncData(const std::string& deviceId); + void SetDeviceState(const std::string& deviceId, int32_t state); + void SetDeviceSyncData(const std::string& deviceId, bool syncData); + bool CheckDeviceExist(const std::string& deviceId); + bool CheckDeviceNeedSync(const std::string& deviceId); + void IncreaseDeviceSyncCount(const std::string& deviceId); + void AddDeviceInfo(DistributedDeviceInfo deviceItem); + void DeleteDeviceInfo(const std::string& deviceId); + std::map& GetDeviceList(); + int32_t SyncDeviceMatch(const DistributedDeviceInfo peerDevice, MatchType type); +#ifdef DISTRIBUTED_FEATURE_MASTER + void SetDeviceStatus(const std::shared_ptr& boxMessage); +#else + void SyncDeviceStatus(int32_t status); +#endif + +private: + DistributedDeviceInfo localDevice_; + std::map peerDevice_; +}; +} +} +#endif // DISTRIBUTED_INCLUDE_SOFTBUS_DISTRIBUTED_DEVICE_SERVICE_H diff --git a/services/distributed/include/soft_bus/distributed_operation_service.h b/services/distributed/include/soft_bus/distributed_operation_service.h new file mode 100644 index 0000000000000000000000000000000000000000..446407b03fb71f7cb618893f394fba16e684c0d6 --- /dev/null +++ b/services/distributed/include/soft_bus/distributed_operation_service.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2025 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 DISTRIBUTED_INCLUDE_SOFTBUS_DISTRIBUTED_OPERATION_SERVICE_H +#define DISTRIBUTED_INCLUDE_SOFTBUS_DISTRIBUTED_OPERATION_SERVICE_H + +#include +#include + +#include "response_box.h" +#include "distributed_device_data.h" +#include "notification_operation_info.h" + +namespace OHOS { +namespace Notification { +class DistributedOperationService { +public: + static DistributedOperationService& GetInstance(); + +#ifdef DISTRIBUTED_FEATURE_MASTER + void ReplyOperationResponse(const std::string& hashCode, const NotificationResponseBox& responseBox, + OperationType operationType, uint32_t result); + int32_t TriggerReplyApplication(const std::string& hashCode, const NotificationResponseBox& responseBox); + void TriggerJumpApplication(const std::string& hashCode); +#else + int32_t OnOperationResponse(const std::shared_ptr& operationInfo, + const DistributedDeviceInfo& device); + void ResponseOperationResult(const std::string& hashCode, const NotificationResponseBox& responseBox); +#endif +}; +} +} +#endif // DISTRIBUTED_INCLUDE_SOFTBUS_DISTRIBUTED_OPERATION_SERVICE_H diff --git a/services/distributed/include/soft_bus/distributed_publish_service.h b/services/distributed/include/soft_bus/distributed_publish_service.h new file mode 100644 index 0000000000000000000000000000000000000000..56525f573b9554dfba8b76b7237cf36fe120bc87 --- /dev/null +++ b/services/distributed/include/soft_bus/distributed_publish_service.h @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2025 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 DISTRIBUTED_INCLUDE_SOFTBUS_DISTRIBUTED_PUBLISH_SERVICE_H +#define DISTRIBUTED_INCLUDE_SOFTBUS_DISTRIBUTED_PUBLISH_SERVICE_H + +#include "tlv_box.h" +#include "notification.h" +#include "request_box.h" +#include "notification_request.h" +#include "distributed_device_data.h" + +namespace OHOS { +namespace Notification { +class DistributedPublishService { +public: + static DistributedPublishService& GetInstance(); + void RemoveNotification(const std::shared_ptr& boxMessage); + void RemoveNotifications(const std::shared_ptr& boxMessage); + void OnRemoveNotification(const DistributedDeviceInfo& peerDevice, + std::string hashCode, int32_t slotTypes); + void OnRemoveNotifications(const DistributedDeviceInfo& peerDevice, + std::string hashCodes, std::string slotTypes); + +#ifdef DISTRIBUTED_FEATURE_MASTER + void SyncLiveViewNotification(const DistributedDeviceInfo peerDevice, bool isForce); +private: + void SyncNotifictionList(const DistributedDeviceInfo& peerDevice, + const std::vector& notificationList); + void SendNotifictionRequest(const std::shared_ptr request, + const DistributedDeviceInfo& peerDevice, bool isSyncNotification); + void SetNotificationButtons(const sptr notificationRequest, + NotificationConstant::SlotType slotType, std::shared_ptr& requestBox); + void SetNotificationContent(const std::shared_ptr &content, + NotificationContent::Type type, std::shared_ptr& requestBox); +#else + void PublishNotification(const std::shared_ptr& boxMessage); + void PublishSynchronousLiveView(const std::shared_ptr& boxMessage); +private: + void MakeNotificationButtons(const NotifticationRequestBox& box, + NotificationConstant::SlotType slotType, sptr& request); + void MakeNotifictaionContent(const NotifticationRequestBox& box, sptr& request, + bool isCommonLiveView, int32_t contentType); + void MakeNotifictaionIcon(const NotifticationRequestBox& box, sptr& request, + bool isCommonLiveView); + void MakeNotifictaionReminderFlag(const NotifticationRequestBox& box, + sptr& request); + void MakeNotifictaionBasicContent(const NotifticationRequestBox& box, sptr& request, + int32_t contentType); +#endif +}; +} +} +#endif diff --git a/services/distributed/include/soft_bus/distributed_subscribe_service.h b/services/distributed/include/soft_bus/distributed_subscribe_service.h new file mode 100644 index 0000000000000000000000000000000000000000..1fd77efd36fa0d0292f258148ad8ce56be5ee168 --- /dev/null +++ b/services/distributed/include/soft_bus/distributed_subscribe_service.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2025 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 DISTRIBUTED_INCLUDE_SOFTBUS_DISTRIBUTED_SUBSCRIBE_SERVICE_H +#define DISTRIBUTED_INCLUDE_SOFTBUS_DISTRIBUTED_SUBSCRIBE_SERVICE_H + +#include "distributed_subscriber.h" +#include "distributed_device_data.h" + +namespace OHOS { +namespace Notification { +class DistributedSubscribeService { +public: + static DistributedSubscribeService& GetInstance(); + void SubscribeNotifictaion(const DistributedDeviceInfo peerDevice); + void UnSubscribeNotifictaion(const std::string &deviceId); +private: + std::map> subscriberMap_; +}; +} +} +#endif // DISTRIBUTED_INCLUDE_SOFTBUS_DISTRIBUTED_SUBSCRIBE_SERVICE_H diff --git a/services/distributed/src/base/distributed_client.cpp b/services/distributed/src/base/distributed_client.cpp index 898da759871fa405249a0e27e18689c5069d6e63..a673e917dc3e4f55d3b321a2fb9a71de57498417 100644 --- a/services/distributed/src/base/distributed_client.cpp +++ b/services/distributed/src/base/distributed_client.cpp @@ -144,7 +144,8 @@ int32_t DistributedClient::SendMessage(const std::shared_ptr& boxPtr, T DistributedServer::GetInstance().CheckServer(); int32_t result = GetSocketId(deviceId, dataType, socketId); if (boxPtr == nullptr || boxPtr->box_ == nullptr) { - return ERR_OK; + ANS_LOGW("Dans send message failed %{public}s", StringAnonymous(deviceId).c_str()); + return -1; } boxPtr->box_->GetMessageType(type); if (result != ERR_OK) { diff --git a/services/distributed/src/soft_bus/distributed_bundle_service.cpp b/services/distributed/src/soft_bus/distributed_bundle_service.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd484330a7dba637f20baf4329a4030f1b0350d6 --- /dev/null +++ b/services/distributed/src/soft_bus/distributed_bundle_service.cpp @@ -0,0 +1,256 @@ +/* + * Copyright (C) 2025 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 "distributed_bundle_service.h" + +#include "bundle_icon_box.h" +#include "analytics_util.h" +#include "ans_image_util.h" +#include "distributed_client.h" +#include "distributed_data_define.h" +#include "notification_helper.h" +#include "bundle_resource_helper.h" +#include "distributed_device_service.h" +#include "distributed_preference.h" + +namespace OHOS { +namespace Notification { + +DistributedBundleService& DistributedBundleService::GetInstance() +{ + static DistributedBundleService distributedBundleService; + return distributedBundleService; +} + +#ifdef DISTRIBUTED_FEATURE_MASTER +void DistributedBundleService::RequestBundlesIcon(const DistributedDeviceInfo peerDevice, bool isForce) +{ + if (!DistributedDeviceService::GetInstance().CheckDeviceExist(peerDevice.deviceId_)) { + return; + } + bool sync = DistributedDeviceService::GetInstance().IsDeviceSyncData(peerDevice.deviceId_); + if (!isForce && sync) { + ANS_LOGI("Dans %{public}d %{public}d.", isForce, sync); + return; + } + + auto localDevice = DistributedDeviceService::GetInstance().GetLocalDevice(); + 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, TransDataType::DATA_TYPE_MESSAGE, + peerDevice.deviceId_, MODIFY_ERROR_EVENT_CODE); + ANS_LOGI("Dans RequestBundlesIcon %{public}s %{public}d.", + StringAnonymous(peerDevice.deviceId_).c_str(), peerDevice.deviceType_); +} + +void DistributedBundleService::GenerateBundleIconSync(const DistributedDeviceInfo& device) +{ + std::vector bundleOption; + if (NotificationHelper::GetAllLiveViewEnabledBundles(bundleOption) != 0) { + ANS_LOGW("Dans get all live view enable bundle failed."); + return; + } + + std::vector enableBundleOption; + if (NotificationHelper::GetAllDistribuedEnabledBundles("liteWearable", enableBundleOption) != 0) { + ANS_LOGW("Dans get all live view enable bundle failed."); + } + + std::set enabledBundles; + for (auto item : enableBundleOption) { + enabledBundles.insert(item.GetBundleName()); + } + std::set cachedIcons; + std::vector unCachedBundleList; + if (bundleIconCache_.find(device.deviceId_) != bundleIconCache_.end()) { + cachedIcons = bundleIconCache_[device.deviceId_]; + } + for (auto item : bundleOption) { + if (enabledBundles.find(item.GetBundleName()) != enabledBundles.end() || + cachedIcons.find(item.GetBundleName()) != cachedIcons.end()) { + continue; + } + unCachedBundleList.push_back(item.GetBundleName()); + } + + ANS_LOGI("Dans Generate bundle %{public}zu %{public}zu %{public}zu.", bundleOption.size(), + enableBundleOption.size(), 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) { + if (UpdateBundlesIcon(icons, device) == ERR_OK) { + cachedIcons.insert(sendIcon.begin(), sendIcon.end()); + } + icons.clear(); + sendIcon.clear(); + } + } + if (!icons.empty() && UpdateBundlesIcon(icons, device) == ERR_OK) { + cachedIcons.insert(sendIcon.begin(), sendIcon.end()); + } + bundleIconCache_[device.deviceId_] = cachedIcons; +} + +void DistributedBundleService::HandleBundleRemoved(const std::string& bundleName) +{ + auto localDevice = DistributedDeviceService::GetInstance().GetLocalDevice(); + auto peerDevices = DistributedDeviceService::GetInstance().GetDeviceList(); + for (auto& device : peerDevices) { + auto iter = bundleIconCache_.find(device.first); + if (iter == bundleIconCache_.end() || + iter->second.find(bundleName) == iter->second.end()) { + continue; + } + iter->second.erase(bundleName); + if (device.second.deviceType_ == DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) { + continue; + } + + 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, TransDataType::DATA_TYPE_MESSAGE, + device.second.deviceId_, MODIFY_ERROR_EVENT_CODE); + ANS_LOGI("Dans ReportBundleIconList %{public}s %{public}d.", + StringAnonymous(device.second.deviceId_).c_str(), device.second.deviceType_); + } +} + +void DistributedBundleService::HandleBundleChanged(const std::string& bundleName, bool updatedExit) +{ + std::vector updateDeviceList; + GetNeedUpdateDevice(updatedExit, bundleName, updateDeviceList); + if (updateDeviceList.empty()) { + ANS_LOGI("No need update %{public}s.", bundleName.c_str()); + return; + } + std::string icon; + if (!GetBundleResourceInfo(bundleName, icon)) { + return; + } + std::unordered_map icons; + icons.insert(std::make_pair(bundleName, icon)); + for (auto& device : updateDeviceList) { + UpdateBundlesIcon(icons, device); + } +} + +int32_t DistributedBundleService::UpdateBundlesIcon(const std::unordered_map& icons, + const DistributedDeviceInfo peerDevice) +{ + 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 -1; + } + + int32_t result = DistributedClient::GetInstance().SendMessage(iconBox, TransDataType::DATA_TYPE_BYTES, + peerDevice.deviceId_, MODIFY_ERROR_EVENT_CODE); + ANS_LOGI("Dans UpdateBundlesIcon %{public}s %{public}d %{public}d.", + StringAnonymous(peerDevice.deviceId_).c_str(), peerDevice.deviceType_, result); + return result; +} + +bool DistributedBundleService::GetBundleResourceInfo(const std::string bundleName, std::string& icon) +{ + AppExecFwk::BundleResourceInfo resourceInfo; + if (DelayedSingleton::GetInstance()->GetBundleInfo(bundleName, resourceInfo) != 0) { + ANS_LOGW("Dans get bundle icon failed %{public}s.", bundleName.c_str()); + return false; + } + std::shared_ptr iconPixelmap = AnsImageUtil::CreatePixelMapByString(resourceInfo.icon); + if (!AnsImageUtil::ImageScale(iconPixelmap, DEFAULT_ICON_WITHE, DEFAULT_ICON_HEIGHT)) { + return false; + } + icon = AnsImageUtil::PackImage(iconPixelmap); + ANS_LOGI("Dans get bundle icon bundle %{public}s %{public}zu.", bundleName.c_str(), resourceInfo.icon.size()); + return true; +} + +void DistributedBundleService::GetNeedUpdateDevice(bool updatedExit, const std::string& bundleName, + std::vector& updateDeviceList) +{ + auto peerDevices = DistributedDeviceService::GetInstance().GetDeviceList(); + for (auto& device : peerDevices) { + if (device.second.deviceType_ == DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) { + continue; + } + auto iter = bundleIconCache_.find(device.first); + if (updatedExit) { + if (iter == bundleIconCache_.end() || + iter->second.find(bundleName) == iter->second.end()) { + continue; + } + updateDeviceList.push_back(device.second); + } else { + if (iter != bundleIconCache_.end() && + iter->second.find(bundleName) != iter->second.end()) { + continue; + } + if (iter == bundleIconCache_.end()) { + std::set cachedIcons = { bundleName }; + bundleIconCache_.insert(std::make_pair(device.first, cachedIcons)); + } else { + iter->second.insert(bundleName); + } + updateDeviceList.push_back(device.second); + } + } +} +#else +void DistributedBundleService::ReportBundleIconList(const DistributedDeviceInfo peerDevice) +{ + std::vector bundlesName; + DistributedPreferences::GetInstance().GetSavedBundlesIcon(bundlesName); + std::shared_ptr iconBox = std::make_shared(); + auto localDevice = DistributedDeviceService::GetInstance().GetLocalDevice(); + 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, TransDataType::DATA_TYPE_MESSAGE, + peerDevice.deviceId_, MODIFY_ERROR_EVENT_CODE); + ANS_LOGI("Dans ReportBundleIconList %{public}s %{public}d.", + StringAnonymous(peerDevice.deviceId_).c_str(), peerDevice.deviceType_); +} +#endif + +} +} diff --git a/services/distributed/src/soft_bus/distributed_device_service.cpp b/services/distributed/src/soft_bus/distributed_device_service.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a5636492674e42150547ae1b230c4d89ec99ed8 --- /dev/null +++ b/services/distributed/src/soft_bus/distributed_device_service.cpp @@ -0,0 +1,209 @@ +/* + * Copyright (C) 2025 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 "distributed_device_service.h" + +#include "state_box.h" +#include "analytics_util.h" +#include "distributed_client.h" +#include "distributed_data_define.h" +#include "distributed_device_data.h" +#include "notification_helper.h" + +namespace OHOS { +namespace Notification { + +static const int32_t MAX_CONNECTED_TYR = 5; +static const uint32_t DEFAULT_LOCK_SCREEN_FLAG = 2; + +DistributedDeviceService& DistributedDeviceService::GetInstance() +{ + static DistributedDeviceService distributedDeviceService; + return distributedDeviceService; +} + +std::string DeviceTypeToTypeString(uint16_t deviceType) +{ + switch (deviceType) { + case DistributedHardware::DmDeviceType::DEVICE_TYPE_WATCH: { + return "wearable"; + } + case DistributedHardware::DmDeviceType::DEVICE_TYPE_PAD: { + return "Pad"; + } + case DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE: { + return "Phone"; + } + default: + return std::string(); + } +} + +void DistributedDeviceService::InitLocalDevice(const std::string &deviceId, uint16_t deviceType) +{ + localDevice_.deviceId_ = deviceId; + localDevice_.deviceType_ = deviceType; +} + +DistributedDeviceInfo DistributedDeviceService::GetLocalDevice() +{ + return localDevice_; +} + +bool DistributedDeviceService::IsDeviceSyncData(const std::string& deviceId) +{ + auto iter = peerDevice_.find(deviceId); + if (iter == peerDevice_.end()) { + ANS_LOGE("Dans unknown device is data %{public}s.", StringAnonymous(deviceId).c_str()); + return true; + } + return iter->second.isSync; +} + +void DistributedDeviceService::SetDeviceSyncData(const std::string& deviceId, bool syncData) +{ + auto iter = peerDevice_.find(deviceId); + if (iter == peerDevice_.end()) { + ANS_LOGE("Dans unknown device set data %{public}s.", StringAnonymous(deviceId).c_str()); + return; + } + iter->second.isSync = true; +} + +void DistributedDeviceService::SetDeviceState(const std::string& deviceId, int32_t state) +{ + auto iter = peerDevice_.find(deviceId); + if (iter == peerDevice_.end()) { + ANS_LOGE("Dans unknown device set status %{public}s.", StringAnonymous(deviceId).c_str()); + return; + } + iter->second.peerState_ = state; +} + +bool DistributedDeviceService::CheckDeviceExist(const std::string& deviceId) +{ + if (peerDevice_.find(deviceId) == peerDevice_.end()) { + ANS_LOGI("Dans unknown device %{public}s.", StringAnonymous(deviceId).c_str()); + return false; + } + return true; +} + +bool DistributedDeviceService::CheckDeviceNeedSync(const std::string& deviceId) +{ + auto iter = peerDevice_.find(deviceId); + if (iter == peerDevice_.end()) { + ANS_LOGE("Dans unknown device connected %{public}s.", StringAnonymous(deviceId).c_str()); + return false; + } + if (iter->second.connectedTry_ >= MAX_CONNECTED_TYR || iter->second.peerState_ != DeviceState::STATE_SYNC) { + ANS_LOGE("Dans no need try %{public}d.", iter->second.connectedTry_); + iter->second.connectedTry_ = 0; + return false; + } + return true; +} + +void DistributedDeviceService::IncreaseDeviceSyncCount(const std::string& deviceId) +{ + auto iter = peerDevice_.find(deviceId); + if (iter == peerDevice_.end()) { + ANS_LOGE("Dans unknown device count %{public}s.", StringAnonymous(deviceId).c_str()); + return; + } + iter->second.connectedTry_ = iter->second.connectedTry_ + 1; +} + +void DistributedDeviceService::AddDeviceInfo(DistributedDeviceInfo deviceItem) +{ + peerDevice_[deviceItem.deviceId_] = deviceItem; +} + +void DistributedDeviceService::DeleteDeviceInfo(const std::string& deviceId) +{ + auto deviceIter = peerDevice_.find(deviceId); + if (deviceIter != peerDevice_.end()) { + ANS_LOGI("Delete device %{public}s.", StringAnonymous(deviceId).c_str()); + peerDevice_.erase(deviceId); + } +} + +std::map& DistributedDeviceService::GetDeviceList() +{ + return peerDevice_; +} + +int32_t DistributedDeviceService::SyncDeviceMatch(const DistributedDeviceInfo peerDevice, MatchType type) +{ + 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_); + } + if (!matchBox->Serialize()) { + ANS_LOGW("Dans SyncDeviceMatch serialize failed."); + return -1; + } + int32_t result = DistributedClient::GetInstance().SendMessage(matchBox, TransDataType::DATA_TYPE_MESSAGE, + peerDevice.deviceId_, MODIFY_ERROR_EVENT_CODE); + ANS_LOGI("Dans SyncDeviceMatch %{public}s %{public}d %{public}s %{public}d %{public}d.", + StringAnonymous(peerDevice.deviceId_).c_str(), peerDevice.deviceType_, + StringAnonymous(localDevice_.deviceId_).c_str(), localDevice_.deviceType_, type); + return result; +} + +#ifdef DISTRIBUTED_FEATURE_MASTER +void DistributedDeviceService::SetDeviceStatus(const std::shared_ptr& boxMessage) +{ + int32_t status; + std::string deviceName; + NotifticationStateBox stateBox = NotifticationStateBox(boxMessage); + if (!stateBox.GetDeviceType(deviceName) || !stateBox.GetState(status)) { + ANS_LOGW("Dans unbox state failed."); + return; + } + std::string deviceId; + if (!stateBox.GetDeviceId(deviceId)) { + ANS_LOGW("Dans unbox deviceId failed."); + } + int32_t result = NotificationHelper::SetTargetDeviceStatus(deviceName, status, + DEFAULT_LOCK_SCREEN_FLAG, deviceId); + ANS_LOGI("Dans set state %{public}s %{public}d.", deviceName.c_str(), status); +} +#else +void DistributedDeviceService::SyncDeviceStatus(int32_t status) +{ + std::shared_ptr stateBox = std::make_shared(); + stateBox->SetState(status); + stateBox->SetDeviceType(DeviceTypeToTypeString(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, TransDataType::DATA_TYPE_MESSAGE, + peer.second.deviceId_, MODIFY_ERROR_EVENT_CODE); + ANS_LOGI("Dans SyncDeviceState %{public}d %{public}d %{public}d %{public}d.", + peer.second.deviceType_, localDevice_.deviceType_, status, (int32_t)(peerDevice_.size())); + } +} +#endif +} +} diff --git a/services/distributed/src/soft_bus/distributed_operation_service.cpp b/services/distributed/src/soft_bus/distributed_operation_service.cpp new file mode 100644 index 0000000000000000000000000000000000000000..15762ce0a0ca4aa7046ec2cfcf1da3beaab7d61f --- /dev/null +++ b/services/distributed/src/soft_bus/distributed_operation_service.cpp @@ -0,0 +1,321 @@ +/* + * Copyright (C) 2025 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 "distributed_operation_service.h" + +#include "response_box.h" +#include "match_box.h" +#include "ans_inner_errors.h" +#include "notification_helper.h" +#include "distributed_data_define.h" +#include "distributed_device_service.h" +#include "int_wrapper.h" +#include "string_wrapper.h" +#include "analytics_util.h" +#include "distributed_client.h" +#include "power_mgr_client.h" +#include "ans_inner_errors.h" +#include "distributed_operation_helper.h" +#include "ability_manager_helper.h" +#include "distributed_liveview_all_scenarios_extension_wrapper.h" +#include "screenlock_manager.h" + +namespace OHOS { +namespace Notification { + +static const std::string DISTRIBUTED_LABEL = "ans_distributed"; + +DistributedOperationService& DistributedOperationService::GetInstance() +{ + static DistributedOperationService distributedOperationService; + return distributedOperationService; +} + +#ifdef DISTRIBUTED_FEATURE_MASTER +static int64_t GetCurrentTime() +{ + auto now = std::chrono::system_clock::now(); + auto duration = std::chrono::duration_cast(now.time_since_epoch()); + return duration.count(); +} + +static void TriggerReplyWantAgent(const sptr request, + std::string actionName, int32_t errorCode, std::string desc) +{ + AAFwk::WantParams extraInfo; + extraInfo.SetParam("desc", AAFwk::String::Box(desc)); + extraInfo.SetParam("errorCode", AAFwk::Integer::Box(errorCode)); + extraInfo.SetParam("actionName", AAFwk::String::Box(actionName)); + DISTRIBUTED_LIVEVIEW_ALL_SCENARIOS_EXTENTION_WRAPPER->TriggerPushWantAgent(request, + OperationType::DISTRIBUTE_OPERATION_REPLY, extraInfo); +} + +static ErrCode GetNotificationButtonWantPtr(const std::string& hashCode, const std::string& actionName, + std::shared_ptr& wantPtr, sptr& request, std::string& userInputKey) +{ + sptr notificationRequest = nullptr; + auto result = NotificationHelper::GetNotificationRequestByHashCode(hashCode, notificationRequest); + if (result != ERR_OK || notificationRequest == nullptr) { + ANS_LOGE("Check notificationRequest is null."); + return ERR_ANS_NOTIFICATION_NOT_EXISTS; + } + + request = notificationRequest; + auto actionButtons = notificationRequest->GetActionButtons(); + if (actionButtons.empty()) { + ANS_LOGE("Check actionButtons is null."); + return ERR_ANS_INVALID_PARAM; + } + + std::shared_ptr button = nullptr; + for (std::shared_ptr buttonItem : actionButtons) { + if (buttonItem != nullptr && buttonItem->GetUserInput() != nullptr && + buttonItem->GetTitle() == actionName) { + button = buttonItem; + break; + } + } + + if (button == nullptr) { + ANS_LOGE("Check user input is null %{public}s.", actionName.c_str()); + return ERR_ANS_INVALID_PARAM; + } + if (button->GetUserInput() != nullptr) { + userInputKey = button->GetUserInput()->GetInputKey(); + } + if (userInputKey.empty()) { + ANS_LOGE("Check userInputKey is null."); + return ERR_ANS_INVALID_PARAM; + } + std::shared_ptr wantAgentPtr = button->GetWantAgent(); + if (wantAgentPtr == nullptr) { + ANS_LOGE("Check wantAgentPtr is null."); + return ERR_ANS_INVALID_PARAM; + } + + std::shared_ptr pendingWantPtr = wantAgentPtr->GetPendingWant(); + if (pendingWantPtr == nullptr) { + ANS_LOGE("Check pendingWantPtr is null."); + return ERR_ANS_INVALID_PARAM; + } + + wantPtr = pendingWantPtr->GetWant(pendingWantPtr->GetTarget()); + if (wantPtr == nullptr) { + ANS_LOGE("Check wantPtr is null."); + return ERR_ANS_INVALID_PARAM; + } + return ERR_OK; +} + +static std::shared_ptr GetNotificationWantPtr(const std::string& hashCode) +{ + sptr notificationRequest = nullptr; + auto result = NotificationHelper::GetNotificationRequestByHashCode(hashCode, notificationRequest); + if (result != ERR_OK || notificationRequest == nullptr) { + ANS_LOGE("Check notificationRequest is null."); + return nullptr; + } + + std::shared_ptr wantAgentPtr = notificationRequest->GetWantAgent(); + if (wantAgentPtr == nullptr) { + ANS_LOGE("Check wantAgentPtr is null."); + return nullptr; + } + + std::shared_ptr pendingWantPtr = wantAgentPtr->GetPendingWant(); + if (pendingWantPtr == nullptr) { + ANS_LOGE("Check pendingWantPtr is null."); + return nullptr; + } + + return pendingWantPtr->GetWant(pendingWantPtr->GetTarget()); +} + +void DistributedOperationService::ReplyOperationResponse(const std::string& hashCode, + const NotificationResponseBox& responseBox, OperationType operationType, uint32_t result) +{ + std::string eventId; + std::string deviceId; + responseBox.GetOperationEventId(eventId); + responseBox.GetLocalDeviceId(deviceId); + + if (!DistributedDeviceService::GetInstance().CheckDeviceExist(deviceId)) { + ANS_LOGI("Dans get deviceId unknonw %{public}s.", StringAnonymous(deviceId).c_str()); + return; + } + + 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, TransDataType::DATA_TYPE_MESSAGE, + deviceId, MODIFY_ERROR_EVENT_CODE); + if (ret != ERR_OK) { + ANS_LOGE("dans OnResponse send message failed result: %{public}d", ret); + return; + } + ANS_LOGI("Dans reply operation %{public}s %{public}d.", StringAnonymous(deviceId).c_str(), result); + return; +} + +int32_t DistributedOperationService::TriggerReplyApplication(const std::string& hashCode, + const NotificationResponseBox& responseBox) +{ + std::string actionName; + std::string userInput; + std::string userInputKey; + responseBox.GetActionName(actionName); + responseBox.GetUserInput(userInput); + + std::shared_ptr wantPtr = nullptr; + sptr request = nullptr; + auto result = GetNotificationButtonWantPtr(hashCode, actionName, wantPtr, request, userInputKey); + if (result != ERR_OK || wantPtr == nullptr) { + AnalyticsUtil::GetInstance().AbnormalReporting(MODIFY_ERROR_EVENT_CODE, result, + BRANCH4_ID, "reply get button failed"); + TriggerReplyWantAgent(request, actionName, result, "reply get button failed"); + return result; + } + + if (wantPtr->GetBoolParam(AAFwk::Want::PARAM_RESV_CALL_TO_FOREGROUND, false)) { + ANS_LOGE("Not support foreground."); + AnalyticsUtil::GetInstance().AbnormalReporting(MODIFY_ERROR_EVENT_CODE, ERR_ANS_DISTRIBUTED_OPERATION_FAILED, + BRANCH4_ID, "reply foreground failed"); + TriggerReplyWantAgent(request, actionName, ERR_ANS_DISTRIBUTED_OPERATION_FAILED, "reply foreground failed"); + return ERR_ANS_DISTRIBUTED_OPERATION_FAILED; + } + + auto ret = AbilityManagerHelper::GetInstance().ConnectAbility(hashCode, *wantPtr, userInputKey, userInput); + ANS_LOGI("StartAbility result:%{public}d", ret); + if (ret == ERR_OK) { + TriggerReplyWantAgent(request, actionName, ERR_OK, ""); + AnalyticsUtil::GetInstance().OperationalReporting(BRANCH4_ID, + NotificationConstant::SlotType::SOCIAL_COMMUNICATION); + } else { + TriggerReplyWantAgent(request, actionName, ret, "ability reply failed"); + AnalyticsUtil::GetInstance().AbnormalReporting(MODIFY_ERROR_EVENT_CODE, ret, + BRANCH4_ID, "ability reply failed"); + return ERR_ANS_DISTRIBUTED_OPERATION_FAILED; + } + return ERR_OK; +} + +void DistributedOperationService::TriggerJumpApplication(const std::string& hashCode) +{ + auto wantPtr = GetNotificationWantPtr(hashCode); + if (wantPtr == nullptr) { + ANS_LOGE("Get pendingWantPtr is null."); + return; + } + + if (!PowerMgr::PowerMgrClient::GetInstance().IsScreenOn()) { + auto ret = PowerMgr::PowerMgrClient::GetInstance().WakeupDevice(); + if (ret != PowerMgr::PowerErrors::ERR_OK) { + ANS_LOGW("Wake up device %{public}d", ret); + return; + } + } + + if (ScreenLock::ScreenLockManager::GetInstance()->IsScreenLocked()) { + OperationInfo info; + info.type = OperationType::DISTRIBUTE_OPERATION_JUMP; + info.eventId = std::to_string(GetCurrentTime()); + sptr listener = new (std::nothrow) UnlockScreenCallback(info.eventId); + int32_t unlockResult = + ScreenLock::ScreenLockManager::GetInstance()->Unlock(ScreenLock::Action::UNLOCKSCREEN, listener); + ANS_LOGI("unlock result:%{public}d", unlockResult); + if (unlockResult != ERR_OK) { + AnalyticsUtil::GetInstance().AbnormalReporting(MODIFY_ERROR_EVENT_CODE, unlockResult, + BRANCH6_ID, "unlock failed"); + } + info.want = *wantPtr; + OperationService::GetInstance().AddOperation(info); + } else { + auto ret = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(*wantPtr); + ANS_LOGI("StartAbility result:%{public}d", ret); + if (ret == ERR_OK) { + AnalyticsUtil::GetInstance().OperationalReporting(BRANCH3_ID, NotificationConstant::SlotType::LIVE_VIEW); + } else { + AnalyticsUtil::GetInstance().AbnormalReporting(MODIFY_ERROR_EVENT_CODE, 0, ret, "pull up failed"); + } + AnalyticsUtil::GetInstance().AbnormalReporting(MODIFY_ERROR_EVENT_CODE, ret, BRANCH9_ID, "pull up success"); + } +} +#else + +int32_t DistributedOperationService::OnOperationResponse( + const std::shared_ptr & operationInfo, const DistributedDeviceInfo& device) +{ + 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; + } + auto hashCode = operationInfo->GetHashCode(); + if (hashCode.find(DISTRIBUTED_LABEL) == 0) { + hashCode.erase(0, DISTRIBUTED_LABEL.length()); + } + + OperationType type = operationInfo->GetOperationType(); + if (type == OperationType::DISTRIBUTE_OPERATION_REPLY) { + 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()); + } + + auto localDevice = DistributedDeviceService::GetInstance().GetLocalDevice(); + 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, 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; + } + return result; +} + +void DistributedOperationService::ResponseOperationResult(const std::string& hashCode, + const NotificationResponseBox& responseBox) +{ + int32_t result = 0; + std::string eventId; + responseBox.GetOperationEventId(eventId); + responseBox.GetResponseResult(result); + auto ret = NotificationHelper::ReplyDistributeOperation(DISTRIBUTED_LABEL + hashCode + eventId, result); + ANS_LOGI("HandleOperationResponse hashcode %{public}s, result:%{public}d %{public}d", + hashCode.c_str(), result, ret); +} +#endif +} +} diff --git a/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp b/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..41a845933ed5fb223871478f688d72f2e1a0209b --- /dev/null +++ b/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp @@ -0,0 +1,569 @@ +/* + * Copyright (C) 2025 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 "distributed_publish_service.h" + +#include +#include +#include + +#include "request_box.h" +#include "remove_box.h" +#include "batch_remove_box.h" +#include "notification_sync_box.h" +#include "analytics_util.h" +#include "ans_image_util.h" +#include "distributed_client.h" +#include "notification_helper.h" +#include "in_process_call_wrapper.h" +#include "distributed_device_service.h" +#include "distributed_data_define.h" +#include "distributed_preference.h" +#include "distributed_liveview_all_scenarios_extension_wrapper.h" + +namespace OHOS { +namespace Notification { + +static const std::string DISTRIBUTED_LABEL = "ans_distributed"; + +DistributedPublishService& DistributedPublishService::GetInstance() +{ + static DistributedPublishService distributedPublishService; + return distributedPublishService; +} + +void DistributedPublishService::RemoveNotification(const std::shared_ptr& boxMessage) +{ + std::string hashCode; + int32_t slotType; + if (boxMessage == nullptr) { + ANS_LOGE("boxMessage is nullptr"); + return; + } + boxMessage->GetStringValue(NOTIFICATION_HASHCODE, hashCode); + boxMessage->GetInt32Value(NOTIFICATION_SLOT_TYPE, slotType); + + int result = IN_PROCESS_CALL(NotificationHelper::RemoveNotification( + hashCode, NotificationConstant::DISTRIBUTED_COLLABORATIVE_DELETE)); + std::string errorReason = "delete message failed"; + if (result == 0) { + errorReason = "delete message success"; + AnalyticsUtil::GetInstance().AbnormalReporting(DELETE_ERROR_EVENT_CODE, result, BRANCH4_ID, errorReason); + AnalyticsUtil::GetInstance().OperationalReporting(OPERATION_DELETE_BRANCH, slotType); + } else { + AnalyticsUtil::GetInstance().AbnormalReporting(DELETE_ERROR_EVENT_CODE, result, BRANCH3_ID, errorReason); + } + ANS_LOGI("dans remove message %{public}d.", result); +} + +void DistributedPublishService::RemoveNotifications(const std::shared_ptr& boxMessage) +{ + std::vector hashCodes; + std::string hashCodesString; + if (boxMessage == nullptr) { + ANS_LOGE("boxMessage is nullptr"); + return; + } + if (!boxMessage->GetStringValue(NOTIFICATION_HASHCODE, hashCodesString)) { + ANS_LOGE("failed GetStringValue from boxMessage"); + return; + } + std::istringstream hashCodesStream(hashCodesString); + std::string hashCode; + while (hashCodesStream >> hashCode) { + if (!hashCode.empty()) { + hashCodes.push_back(hashCode); + } + } + + int result = IN_PROCESS_CALL( + NotificationHelper::RemoveNotifications(hashCodes, NotificationConstant::DISTRIBUTED_COLLABORATIVE_DELETE)); + ANS_LOGI("dans batch remove message %{public}d.", result); + if (result == 0) { + AnalyticsUtil::GetInstance().AbnormalReporting(DELETE_ERROR_EVENT_CODE, result, BRANCH4_ID, + "delete message success"); + std::string slotTypesString; + if (!boxMessage->GetStringValue(BATCH_REMOVE_SLOT_TYPE, slotTypesString)) { + ANS_LOGE("failed GetStringValue from boxMessage"); + return; + } + std::istringstream slotTypesStream(slotTypesString); + std::string slotTypeString; + while (slotTypesStream >> slotTypeString) { + if (!slotTypeString.empty()) { + AnalyticsUtil::GetInstance().OperationalReporting(OPERATION_DELETE_BRANCH, + atoi(slotTypeString.c_str())); + } + } + } else { + AnalyticsUtil::GetInstance().AbnormalReporting(DELETE_ERROR_EVENT_CODE, result, BRANCH3_ID, + "delete message failed"); + } +} + +void DistributedPublishService::OnRemoveNotification(const DistributedDeviceInfo& peerDevice, + std::string hashCode, int32_t slotTypes) +{ + std::shared_ptr removeBox = std::make_shared(); + ANS_LOGI("dans OnCanceled %{public}s", hashCode.c_str()); + removeBox->SetNotificationHashCode(hashCode); + removeBox->setNotificationSlotType(slotTypes); + if (!removeBox->Serialize()) { + ANS_LOGW("dans OnCanceled serialize failed"); + return; + } + DistributedClient::GetInstance().SendMessage(removeBox, TransDataType::DATA_TYPE_MESSAGE, + peerDevice.deviceId_, DELETE_ERROR_EVENT_CODE); +} + +void DistributedPublishService::OnRemoveNotifications(const DistributedDeviceInfo& peerDevice, + std::string hashCodes, std::string slotTypes) +{ + std::shared_ptr batchRemoveBox = std::make_shared(); + if (!hashCodes.empty()) { + batchRemoveBox->SetNotificationHashCode(hashCodes); + } + batchRemoveBox->SetNotificationSlotTypes(slotTypes); + + if (!batchRemoveBox->Serialize()) { + ANS_LOGW("dans OnCanceled serialize failed"); + return; + } + DistributedClient::GetInstance().SendMessage(batchRemoveBox, TransDataType::DATA_TYPE_MESSAGE, + peerDevice.deviceId_, DELETE_ERROR_EVENT_CODE); +} + +#ifdef DISTRIBUTED_FEATURE_MASTER +void DistributedPublishService::SyncLiveViewNotification(const DistributedDeviceInfo peerDevice, bool isForce) +{ + if (!DistributedDeviceService::GetInstance().CheckDeviceExist(peerDevice.deviceId_)) { + return; + } + bool sync = DistributedDeviceService::GetInstance().IsDeviceSyncData(peerDevice.deviceId_); + if (!isForce && sync) { + ANS_LOGI("Dans %{public}d %{public}d.", isForce, sync); + return; + } + + std::vector> notifications; + auto result = NotificationHelper::GetAllNotificationsBySlotType(notifications, + NotificationConstant::SlotType::LIVE_VIEW); + if (result != ERR_OK) { + ANS_LOGI("Dans get all active %{public}d.", result); + return; + } + + std::vector notificationList; + for (auto& notification : notifications) { + if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr || + !notification->GetNotificationRequestPoint()->IsCommonLiveView()) { + ANS_LOGI("Dans no need sync remove notification."); + continue; + } + notificationList.push_back(notification->GetKey()); + } + SyncNotifictionList(peerDevice, notificationList); + + for (auto& notification : notifications) { + if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr || + !notification->GetNotificationRequestPoint()->IsCommonLiveView()) { + ANS_LOGI("Dans no need sync notification."); + continue; + } + std::shared_ptr sharedNotification = std::make_shared(*notification); + SendNotifictionRequest(sharedNotification, peerDevice, true); + } +} + +void DistributedPublishService::SyncNotifictionList(const DistributedDeviceInfo& peerDevice, + const std::vector& notificationList) +{ + ANS_LOGI("Dans sync notification %{public}d.", (int32_t)(notificationList.size())); + std::shared_ptr notificationSyncBox = std::make_shared(); + notificationSyncBox->SetLocalDeviceId(peerDevice.deviceId_); + notificationSyncBox->SetNotificationEmpty(notificationList.empty()); + if (!notificationList.empty()) { + notificationSyncBox->SetNotificationList(notificationList); + } + + if (!notificationSyncBox->Serialize()) { + ANS_LOGW("Dans SyncNotifictionList serialize failed."); + return; + } + int32_t result = DistributedClient::GetInstance().SendMessage(notificationSyncBox, + TransDataType::DATA_TYPE_BYTES, peerDevice.deviceId_, PUBLISH_ERROR_EVENT_CODE); + ANS_LOGI("Dans SyncNotifictionList %{public}s %{public}d %{public}d.", + StringAnonymous(peerDevice.deviceId_).c_str(), peerDevice.deviceType_, result); +} + +void DistributedPublishService::SendNotifictionRequest(const std::shared_ptr request, + const DistributedDeviceInfo& peerDevice, bool isSyncNotification) +{ + std::shared_ptr requestBox = std::make_shared(); + if (request == nullptr || request->GetNotificationRequestPoint() == nullptr) { + return; + } + + auto requestPoint = request->GetNotificationRequestPoint(); + ANS_LOGI("Dans OnConsumed Notification key = %{public}s, notificationFlag = %{public}s", request->GetKey().c_str(), + requestPoint->GetFlags() == nullptr ? "null" : requestPoint->GetFlags()->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())); + if (isSyncNotification) { + requestBox->SetReminderFlag(0); + } else { + requestBox->SetReminderFlag(requestPoint->GetFlags()->GetReminderFlags()); + } + if (request->GetBundleName().empty()) { + requestBox->SetCreatorBundleName(request->GetCreateBundle()); + } else { + requestBox->SetCreatorBundleName(request->GetBundleName()); + } + if (requestPoint->GetBigIcon() != nullptr) { + requestBox->SetBigIcon(requestPoint->GetBigIcon()); + } + if (requestPoint->GetOverlayIcon() != nullptr) { + requestBox->SetOverlayIcon(requestPoint->GetOverlayIcon()); + } + if (requestPoint->IsCommonLiveView()) { + std::vector buffer; + DISTRIBUTED_LIVEVIEW_ALL_SCENARIOS_EXTENTION_WRAPPER->UpdateLiveviewEncodeContent(requestPoint, buffer); + requestBox->SetCommonLiveView(buffer); + } + SetNotificationButtons(requestPoint, requestPoint->GetSlotType(), requestBox); + SetNotificationContent(request->GetNotificationRequestPoint()->GetContent(), + requestPoint->GetNotificationType(), requestBox); + if (!requestBox->Serialize()) { + ANS_LOGW("Dans OnConsumed serialize failed."); + AnalyticsUtil::GetInstance().SendHaReport(PUBLISH_ERROR_EVENT_CODE, -1, BRANCH3_ID, + "serialization failed"); + return; + } + DistributedClient::GetInstance().SendMessage(requestBox, TransDataType::DATA_TYPE_BYTES, + peerDevice.deviceId_, PUBLISH_ERROR_EVENT_CODE); +} + +void DistributedPublishService::SetNotificationButtons(const sptr notificationRequest, + NotificationConstant::SlotType slotType, std::shared_ptr& requestBox) +{ + if (slotType == NotificationConstant::SlotType::SOCIAL_COMMUNICATION) { + auto actionButtons = notificationRequest->GetActionButtons(); + if (actionButtons.empty()) { + ANS_LOGE("Check actionButtons is null."); + return; + } + + std::shared_ptr button = nullptr; + for (std::shared_ptr buttonItem : actionButtons) { + if (buttonItem != nullptr && buttonItem->GetUserInput() != nullptr && + !buttonItem->GetUserInput()->GetInputKey().empty()) { + button = buttonItem; + break; + } + } + if (button != nullptr && button->GetUserInput() != nullptr) { + requestBox->SetNotificationActionName(button->GetTitle()); + requestBox->SetNotificationUserInput(button->GetUserInput()->GetInputKey()); + } + } +} +#else +void DistributedPublishService::PublishNotification(const std::shared_ptr& boxMessage) +{ + sptr request = new (std::nothrow) NotificationRequest(); + if (request == nullptr) { + ANS_LOGE("NotificationRequest is nullptr"); + return; + } + int32_t slotType = 0; + int32_t contentType = 0; + NotifticationRequestBox requestBox = NotifticationRequestBox(boxMessage); + bool isCommonLiveView = false; + if (requestBox.GetSlotType(slotType) && requestBox.GetContentType(contentType)) { + isCommonLiveView = + (static_cast(contentType) == NotificationContent::Type::LIVE_VIEW) && + (static_cast(slotType) == NotificationConstant::SlotType::LIVE_VIEW); + } + MakeNotificationButtons(requestBox, static_cast(slotType), request); + MakeNotifictaionContent(requestBox, request, isCommonLiveView, contentType); + MakeNotifictaionIcon(requestBox, request, isCommonLiveView); + MakeNotifictaionReminderFlag(requestBox, request); + int result = IN_PROCESS_CALL(NotificationHelper::PublishNotification(*request)); + ANS_LOGI("Dans publish message %{public}s %{public}d.", request->GetDistributedHashCode().c_str(), result); +} + +void DistributedPublishService::PublishSynchronousLiveView(const std::shared_ptr& boxMessage) +{ + bool empty = true; + NotificationSyncBox notificationSyncBox = NotificationSyncBox(boxMessage); + if (!notificationSyncBox.GetNotificationEmpty(empty)) { + ANS_LOGW("Dans get sync notification empty failed."); + return; + } + + std::unordered_set notificationList; + if (!empty) { + if (!notificationSyncBox.GetNotificationList(notificationList)) { + ANS_LOGW("Dans get sync notification failed."); + return; + } + } + std::vector> notifications; + auto result = NotificationHelper::GetAllNotificationsBySlotType(notifications, + NotificationConstant::SlotType::LIVE_VIEW); + if (result != ERR_OK || notifications.empty()) { + ANS_LOGI("Dans get all active %{public}d %{public}d.", result, notifications.empty()); + return; + } + + ANS_LOGI("Dans handle sync notification %{public}d %{public}d.", (int32_t)(notificationList.size()), + (int32_t)(notifications.size())); + for (auto item : notificationList) { + ANS_LOGI("Dans sync %{public}s.", item.c_str()); + } + std::vector removeList; + for (auto& notification : notifications) { + if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr || + !notification->GetNotificationRequestPoint()->IsCommonLiveView()) { + ANS_LOGI("Dans no need sync remove notification."); + continue; + } + std::string hashCode = notification->GetKey(); + ANS_LOGI("Dans sync remove %{public}s.", hashCode.c_str()); + size_t pos = hashCode.find(DISTRIBUTED_LABEL); + if (pos != std::string::npos) { + hashCode.erase(pos, DISTRIBUTED_LABEL.length()); + } + if (notificationList.find(hashCode) == notificationList.end()) { + removeList.push_back(notification->GetKey()); + ANS_LOGI("Dans sync remove notification %{public}s.", notification->GetKey().c_str()); + } + } + if (!removeList.empty()) { + int result = IN_PROCESS_CALL(NotificationHelper::RemoveNotifications(removeList, + NotificationConstant::DISTRIBUTED_COLLABORATIVE_DELETE)); + ANS_LOGI("Dans sync remove message %{public}d.", result); + } +} + +void DistributedPublishService::MakeNotificationButtons(const NotifticationRequestBox& box, + NotificationConstant::SlotType slotType, sptr& request) +{ + if (request != nullptr && slotType == NotificationConstant::SlotType::SOCIAL_COMMUNICATION) { + std::string actionName; + std::string userInputKey; + box.GetNotificationActionName(actionName); + box.GetNotificationUserInput(userInputKey); + std::shared_ptr userInput = NotificationUserInput::Create(userInputKey); + std::shared_ptr actionButton = + NotificationActionButton::Create(nullptr, actionName, nullptr); + actionButton->AddNotificationUserInput(userInput); + request->AddActionButton(actionButton); + } +} + +void DistributedPublishService::MakeNotifictaionReminderFlag(const NotifticationRequestBox& box, + sptr& request) +{ + int32_t type = 0; + std::string context; + if (box.GetSlotType(type)) { + request->SetSlotType(static_cast(type)); + } + if (box.GetReminderFlag(type)) { + request->SetCollaboratedReminderFlag(static_cast(type)); + } + if (box.GetCreatorBundleName(context)) { + request->SetOwnerBundleName(context); + request->SetCreatorBundleName(context); + } + if (box.GetNotificationHashCode(context)) { + request->SetDistributedHashCode(context); + } + request->SetDistributedCollaborate(true); + request->SetLabel(DISTRIBUTED_LABEL); +} + +void DistributedPublishService::MakeNotifictaionIcon(const NotifticationRequestBox& box, + sptr& request, bool isCommonLiveView) +{ + std::shared_ptr icon; + if (box.GetBigIcon(icon)) { + request->SetBigIcon(icon); + } + if (box.GetOverlayIcon(icon)) { + request->SetOverlayIcon(icon); + } + + if (isCommonLiveView) { + std::string bundleName; + if (!box.GetCreatorBundleName(bundleName)) { + return; + } + std::string icon; + DistributedPreferences::GetInstance().GetIconByBundleName(bundleName, icon); + if (!icon.empty()) { + auto iconPixelMap = AnsImageUtil::UnPackImage(icon); + request->SetLittleIcon(iconPixelMap); + } + } +} + +void DistributedPublishService::MakeNotifictaionContent(const NotifticationRequestBox& box, + sptr& request, bool isCommonLiveView, int32_t contentType) +{ + if (isCommonLiveView) { + std::vector buffer; + if (box.GetCommonLiveView(buffer)) { + int64_t deleteTime; + std::string context; + auto liveviewContent = std::make_shared(); + if (box.GetNotificationText(context)) { + liveviewContent->SetText(context); + } + if (box.GetNotificationTitle(context)) { + liveviewContent->SetTitle(context); + } + if (box.GetAutoDeleteTime(deleteTime)) { + request->SetAutoDeletedTime(deleteTime); + } + if (box.GetFinishTime(deleteTime)) { + request->SetFinishDeadLine(deleteTime); + } + auto content = std::make_shared(liveviewContent); + request->SetContent(content); + std::shared_ptr extraInfo = std::make_shared(); + liveviewContent->SetExtraInfo(extraInfo); + DISTRIBUTED_LIVEVIEW_ALL_SCENARIOS_EXTENTION_WRAPPER->UpdateLiveviewDecodeContent(request, buffer); + } + return; + } + MakeNotifictaionBasicContent(box, request, contentType); +} + +struct TransferNotification { + std::string title; + std::string context; + std::string additionalText; + std::string briefText; + std::string expandedTitle; +}; + +static void ConvertBoxToLongContent(const TransferNotification& notificationItem, const NotifticationRequestBox& box, + sptr& request) +{ + auto pContent = std::make_shared(); + pContent->SetText(notificationItem.context); + pContent->SetTitle(notificationItem.title); + pContent->SetAdditionalText(notificationItem.additionalText); + pContent->SetBriefText(notificationItem.briefText); + pContent->SetExpandedTitle(notificationItem.expandedTitle); + std::string longText; + box.GetNotificationLongText(longText); + pContent->SetLongText(longText); + auto content = std::make_shared(pContent); + request->SetContent(content); +} + +static void ConvertBoxToMultileContent(const TransferNotification& notificationItem, const NotifticationRequestBox& box, + sptr& request) +{ + auto pContent = std::make_shared(); + pContent->SetText(notificationItem.context); + pContent->SetTitle(notificationItem.title); + pContent->SetAdditionalText(notificationItem.additionalText); + pContent->SetBriefText(notificationItem.briefText); + pContent->SetExpandedTitle(notificationItem.expandedTitle); + std::vector allLines; + box.GetNotificationAllLines(allLines); + for (auto& item : allLines) { + pContent->AddSingleLine(item); + } + auto content = std::make_shared(pContent); + request->SetContent(content); +} + +static void ConvertBoxToPictureContent(const TransferNotification& notificationItem, const NotifticationRequestBox& box, + sptr& request) +{ + auto pContent = std::make_shared(); + pContent->SetText(notificationItem.context); + pContent->SetTitle(notificationItem.title); + pContent->SetAdditionalText(notificationItem.additionalText); + pContent->SetBriefText(notificationItem.briefText); + pContent->SetExpandedTitle(notificationItem.expandedTitle); + std::shared_ptr bigPicture; + box.GetNotificationBigPicture(bigPicture); + pContent->SetBigPicture(bigPicture); + auto content = std::make_shared(pContent); + request->SetContent(content); +} + +void DistributedPublishService::MakeNotifictaionBasicContent(const NotifticationRequestBox& box, + sptr& request, int32_t contentType) +{ + TransferNotification notificationItem; + box.GetNotificationText(notificationItem.context); + box.GetNotificationTitle(notificationItem.title); + box.GetNotificationAdditionalText(notificationItem.additionalText); + NotificationContent::Type type = static_cast(contentType); + if (type == NotificationContent::Type::LONG_TEXT || type == NotificationContent::Type::MULTILINE || + type == NotificationContent::Type::PICTURE) { + box.GetNotificationBriefText(notificationItem.briefText); + box.GetNotificationExpandedTitle(notificationItem.expandedTitle); + } + switch (type) { + case NotificationContent::Type::BASIC_TEXT: { + auto pContent = std::make_shared(); + pContent->SetText(notificationItem.context); + pContent->SetTitle(notificationItem.title); + pContent->SetAdditionalText(notificationItem.additionalText); + auto content = std::make_shared(pContent); + request->SetContent(content); + break; + } + case NotificationContent::Type::CONVERSATION: { + auto pContent = std::make_shared(); + pContent->SetText(notificationItem.context); + pContent->SetTitle(notificationItem.title); + pContent->SetAdditionalText(notificationItem.additionalText); + auto content = std::make_shared(pContent); + request->SetContent(content); + break; + } + case NotificationContent::Type::LONG_TEXT: { + ConvertBoxToLongContent(notificationItem, box, request); + break; + } + case NotificationContent::Type::MULTILINE: { + ConvertBoxToMultileContent(notificationItem, box, request); + break; + } + case NotificationContent::Type::PICTURE: { + ConvertBoxToPictureContent(notificationItem, box, request); + break; + } + default: { + ANS_LOGE("Set notifictaion content %{public}d", type); + break; + } + } +} +#endif +} +} diff --git a/services/distributed/src/soft_bus/distributed_subscribe_service_v2.cpp b/services/distributed/src/soft_bus/distributed_subscribe_service_v2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28a7b7b979e7757cb363327b7bc14e56b605166b --- /dev/null +++ b/services/distributed/src/soft_bus/distributed_subscribe_service_v2.cpp @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2025 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 "distributed_subscribe_service.h" + +#include "analytics_util.h" +#include "os_account_manager.h" +#include "distributed_data_define.h" +#include "notification_helper.h" +#include "distributed_device_service.h" +#include "notification_subscribe_info.h" + +namespace OHOS { +namespace Notification { + +const int32_t DEFAULT_FILTER_TYPE = 1; + +static std::string SubscribeTransDeviceType(uint16_t deviceType) +{ + switch (deviceType) { + case DistributedHardware::DmDeviceType::DEVICE_TYPE_WATCH: { + return "wearable"; + } + case DistributedHardware::DmDeviceType::DEVICE_TYPE_PAD: { + return "Pad"; + } + default: + return ""; + } +} + +static int32_t GetCurrentActiveUserId() +{ + int32_t userId = DEFAULT_USER_ID; + int32_t ret = OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId); + if (ret != ERR_OK) { + ANS_LOGW("Dans get Current userId failed %{public}d.", ret); + return DEFAULT_USER_ID; + } + return userId; +} + +DistributedSubscribeService& DistributedSubscribeService::GetInstance() +{ + static DistributedSubscribeService distributedSubscribeService; + return distributedSubscribeService; +} + +void DistributedSubscribeService::SubscribeNotifictaion(const DistributedDeviceInfo peerDevice) +{ + if (DistributedDeviceService::GetInstance().CheckDeviceExist(peerDevice.deviceId_)) { + ANS_LOGI("Local device no %{public}s .", StringAnonymous(peerDevice.deviceId_).c_str()); + return; + } + + int32_t userId = GetCurrentActiveUserId(); + std::shared_ptr subscriber = std::make_shared(); + subscriber->SetLocalDevice(DistributedDeviceService::GetInstance().GetLocalDevice()); + subscriber->SetPeerDevice(peerDevice); + sptr subscribeInfo = new NotificationSubscribeInfo(); + std::vector slotTypes; + slotTypes.push_back(NotificationConstant::SlotType::LIVE_VIEW); + slotTypes.push_back(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); + subscribeInfo->SetSlotTypes(slotTypes); + subscribeInfo->SetFilterType(DEFAULT_FILTER_TYPE); + subscribeInfo->AddDeviceType(SubscribeTransDeviceType(peerDevice.deviceType_)); + subscribeInfo->AddAppUserId(userId); + subscribeInfo->SetNeedNotifyApplication(true); + subscribeInfo->SetNeedNotifyResponse(true); + int result = NotificationHelper::SubscribeNotification(subscriber, subscribeInfo); + if (result == 0) { + auto iter = subscriberMap_.find(peerDevice.deviceId_); + if (iter != subscriberMap_.end()) { + NotificationHelper::UnSubscribeNotification(iter->second); + } + subscriberMap_[peerDevice.deviceId_] = subscriber; + DistributedDeviceService::GetInstance().SetDeviceState(peerDevice.deviceId_, DeviceState::STATE_ONLINE); + std::string reason = "deviceType: " + std::to_string(peerDevice.deviceType_) + + " ; deviceId: " + StringAnonymous(peerDevice.deviceId_); + AnalyticsUtil::GetInstance().SendHaReport(PUBLISH_ERROR_EVENT_CODE, 0, BRANCH3_ID, reason); + } + ANS_LOGI("Subscribe notification %{public}s %{public}d %{public}d %{public}d.", + StringAnonymous(peerDevice.deviceId_).c_str(), peerDevice.deviceType_, userId, result); +} + +void DistributedSubscribeService::UnSubscribeNotifictaion(const std::string &deviceId) +{ + DistributedDeviceService::GetInstance().DeleteDeviceInfo(deviceId); + auto iter = subscriberMap_.find(deviceId); + if (iter == subscriberMap_.end()) { + ANS_LOGI("UnSubscribe invalid %{public}s.", StringAnonymous(deviceId).c_str()); + return; + } + + if (NotificationHelper::UnSubscribeNotification(iter->second) == 0) { + subscriberMap_.erase(deviceId); + } + ANS_LOGI("UnSubscribe notification %{public}s.", StringAnonymous(deviceId).c_str()); +} + +} +} diff --git a/services/distributed/test/unittest/BUILD.gn b/services/distributed/test/unittest/BUILD.gn index 2b76ae7b213d85ebae18f9ff66ac23688b7409b7..a394eb899e5e5cc2464fcb628276dc3a6af0bbe2 100644 --- a/services/distributed/test/unittest/BUILD.gn +++ b/services/distributed/test/unittest/BUILD.gn @@ -446,12 +446,17 @@ ohos_unittest("distributed_service_test") { "/${services_path}/distributed/src/helper/distributed_operation_helper.cpp", "/${services_path}/distributed/src/helper/distributed_preference.cpp", "/${services_path}/distributed/src/helper/distributed_rdb_helper.cpp", + "/${services_path}/distributed/src/soft_bus/distributed_bundle_service.cpp", "/${services_path}/distributed/src/soft_bus/distributed_cmd_service.cpp", + "/${services_path}/distributed/src/soft_bus/distributed_device_service.cpp", "/${services_path}/distributed/src/soft_bus/distributed_observer_service.cpp", + "/${services_path}/distributed/src/soft_bus/distributed_operation_service.cpp", "/${services_path}/distributed/src/soft_bus/distributed_publish_service.cpp", + "/${services_path}/distributed/src/soft_bus/distributed_publish_service_v2.cpp", "/${services_path}/distributed/src/soft_bus/distributed_service.cpp", "/${services_path}/distributed/src/soft_bus/distributed_service_uitls.cpp", "/${services_path}/distributed/src/soft_bus/distributed_subscribe_service.cpp", + "/${services_path}/distributed/src/soft_bus/distributed_subscribe_service_v2.cpp", "/${services_path}/distributed/src/soft_bus/distributed_subscriber.cpp", "/${services_path}/distributed/src/tlv_box/batch_remove_box.cpp", "/${services_path}/distributed/src/tlv_box/box_base.cpp", @@ -539,12 +544,17 @@ ohos_unittest("distributed_subscriber_test") { "/${services_path}/distributed/src/helper/distributed_operation_helper.cpp", "/${services_path}/distributed/src/helper/distributed_preference.cpp", "/${services_path}/distributed/src/helper/distributed_rdb_helper.cpp", + "/${services_path}/distributed/src/soft_bus/distributed_bundle_service.cpp", "/${services_path}/distributed/src/soft_bus/distributed_cmd_service.cpp", + "/${services_path}/distributed/src/soft_bus/distributed_device_service.cpp", "/${services_path}/distributed/src/soft_bus/distributed_observer_service.cpp", + "/${services_path}/distributed/src/soft_bus/distributed_operation_service.cpp", "/${services_path}/distributed/src/soft_bus/distributed_publish_service.cpp", + "/${services_path}/distributed/src/soft_bus/distributed_publish_service_v2.cpp", "/${services_path}/distributed/src/soft_bus/distributed_service.cpp", "/${services_path}/distributed/src/soft_bus/distributed_service_uitls.cpp", "/${services_path}/distributed/src/soft_bus/distributed_subscribe_service.cpp", + "/${services_path}/distributed/src/soft_bus/distributed_subscribe_service_v2.cpp", "/${services_path}/distributed/src/soft_bus/distributed_subscriber.cpp", "/${services_path}/distributed/src/tlv_box/batch_remove_box.cpp", "/${services_path}/distributed/src/tlv_box/box_base.cpp",