From 31239d0a05ef41059d9d4df6e3a46a526350588f Mon Sep 17 00:00:00 2001 From: wangsen1994 Date: Fri, 23 May 2025 16:59:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E5=9C=BA=E6=99=AF=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangsen1994 --- .../distributed_extension_service.cpp | 2 - services/distributed/BUILD.gn | 4 - .../include/helper/distributed_preference.h | 4 +- .../soft_bus/distributed_bundle_service.h | 2 + .../soft_bus/distributed_device_service.h | 4 +- .../soft_bus/distributed_operation_service.h | 1 + .../soft_bus/distributed_publish_service.h | 4 +- .../include/soft_bus/distributed_service.h | 79 +-- .../soft_bus/distributed_subscribe_service.h | 4 +- .../include/tlv_box/notification_sync_box.h | 3 + .../distributed/include/tlv_box/request_box.h | 3 + .../distributed/include/tlv_box/state_box.h | 10 +- .../distributed/include/tlv_box/tlv_box.h | 2 +- .../distributed/src/helper/analytics_util.cpp | 10 +- .../src/helper/distributed_preference.cpp | 4 +- .../soft_bus/distributed_bundle_service.cpp | 75 ++ .../src/soft_bus/distributed_cmd_service.cpp | 440 ------------ .../soft_bus/distributed_device_service.cpp | 27 +- .../distributed_extern_intferface.cpp | 2 +- .../soft_bus/distributed_observer_service.cpp | 36 +- .../distributed_operation_service.cpp | 27 + .../soft_bus/distributed_publish_service.cpp | 655 ------------------ .../distributed_publish_service_v2.cpp | 54 ++ .../src/soft_bus/distributed_service.cpp | 303 ++++++-- .../soft_bus/distributed_service_uitls.cpp | 167 ----- .../distributed_subscribe_service.cpp | 406 ----------- .../distributed_subscribe_service_v2.cpp | 19 +- .../src/soft_bus/distributed_subscriber.cpp | 4 +- .../src/tlv_box/notification_sync_box.cpp | 3 + .../distributed/src/tlv_box/request_box.cpp | 3 + .../distributed/src/tlv_box/state_box.cpp | 28 +- services/distributed/test/unittest/BUILD.gn | 8 - .../unittest/distributed_service_test.cpp | 11 +- 33 files changed, 537 insertions(+), 1867 deletions(-) delete mode 100644 services/distributed/src/soft_bus/distributed_cmd_service.cpp delete mode 100644 services/distributed/src/soft_bus/distributed_publish_service.cpp delete mode 100644 services/distributed/src/soft_bus/distributed_service_uitls.cpp delete mode 100644 services/distributed/src/soft_bus/distributed_subscribe_service.cpp diff --git a/services/ans/src/notification_extension/distributed_extension_service.cpp b/services/ans/src/notification_extension/distributed_extension_service.cpp index e3601d5a3..4e5c957d5 100644 --- a/services/ans/src/notification_extension/distributed_extension_service.cpp +++ b/services/ans/src/notification_extension/distributed_extension_service.cpp @@ -56,8 +56,6 @@ constexpr const int32_t DELETE_ERROR_EVENT_CODE = 5; constexpr const int32_t MODIFY_ERROR_EVENT_CODE = 6; constexpr const int32_t ANS_CUSTOMIZE_CODE = 7; -static const int32_t MAX_DATA_LENGTH = 7; -static const int32_t START_ANONYMOUS_INDEX = 5; constexpr int64_t DURATION_ONE_SECOND = 1000; // 1s, millisecond } diff --git a/services/distributed/BUILD.gn b/services/distributed/BUILD.gn index 48855b613..87ab3b7c8 100644 --- a/services/distributed/BUILD.gn +++ b/services/distributed/BUILD.gn @@ -109,16 +109,12 @@ ohos_shared_library("libdans") { "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", diff --git a/services/distributed/include/helper/distributed_preference.h b/services/distributed/include/helper/distributed_preference.h index 688a74c61..bf139fb9e 100644 --- a/services/distributed/include/helper/distributed_preference.h +++ b/services/distributed/include/helper/distributed_preference.h @@ -33,8 +33,8 @@ public: static DistributedPreferences& GetInstance(); int32_t DeleteBundleIcon(const std::string &bundleName); - int32_t InertBundleIcon(const std::string &bundleName, const std::string &icon); - int32_t InertBatchBundleIcons(std::unordered_map &values); + int32_t InsertBundleIcon(const std::string &bundleName, const std::string &icon); + int32_t InsertBatchBundleIcons(std::unordered_map &values); int32_t GetIconByBundleName(const std::string& bundleName, std::string &icon); int32_t GetSavedBundlesIcon(std::vector& bundleNames); private: diff --git a/services/distributed/include/soft_bus/distributed_bundle_service.h b/services/distributed/include/soft_bus/distributed_bundle_service.h index be709f532..867cb2687 100644 --- a/services/distributed/include/soft_bus/distributed_bundle_service.h +++ b/services/distributed/include/soft_bus/distributed_bundle_service.h @@ -19,6 +19,7 @@ #include #include +#include "tlv_box.h" #include "distributed_device_data.h" namespace OHOS { @@ -27,6 +28,7 @@ class DistributedBundleService { public: static DistributedBundleService& GetInstance(); + void HandleBundleIconSync(const std::shared_ptr& boxMessage); #ifdef DISTRIBUTED_FEATURE_MASTER void RequestBundlesIcon(const DistributedDeviceInfo peerDevice, bool isForce); void GenerateBundleIconSync(const DistributedDeviceInfo& device); diff --git a/services/distributed/include/soft_bus/distributed_device_service.h b/services/distributed/include/soft_bus/distributed_device_service.h index 6a0bf7d9e..dca01cbd7 100644 --- a/services/distributed/include/soft_bus/distributed_device_service.h +++ b/services/distributed/include/soft_bus/distributed_device_service.h @@ -25,10 +25,11 @@ namespace Notification { class DistributedDeviceService { public: static DistributedDeviceService& GetInstance(); - static std::string DeviceTypeToTypeString(uint16_t deviceType); + bool IsReportDataByHa(); void InitLocalDevice(const std::string &deviceId, uint16_t deviceType); DistributedDeviceInfo GetLocalDevice(); bool IsDeviceSyncData(const std::string& deviceId); + bool GetDeviceInfo(const std::string& deviceId, DistributedDeviceInfo& device); void SetDeviceState(const std::string& deviceId, int32_t state); void SetDeviceSyncData(const std::string& deviceId, bool syncData); bool CheckDeviceExist(const std::string& deviceId); @@ -41,6 +42,7 @@ public: #ifdef DISTRIBUTED_FEATURE_MASTER void SetDeviceStatus(const std::shared_ptr& boxMessage); #else + void InitCurrentDeviceStatus(); void SyncDeviceStatus(int32_t status); #endif diff --git a/services/distributed/include/soft_bus/distributed_operation_service.h b/services/distributed/include/soft_bus/distributed_operation_service.h index 446407b03..f8861e643 100644 --- a/services/distributed/include/soft_bus/distributed_operation_service.h +++ b/services/distributed/include/soft_bus/distributed_operation_service.h @@ -29,6 +29,7 @@ class DistributedOperationService { public: static DistributedOperationService& GetInstance(); + void HandleNotificationOperation(const std::shared_ptr& boxMessage); #ifdef DISTRIBUTED_FEATURE_MASTER void ReplyOperationResponse(const std::string& hashCode, const NotificationResponseBox& responseBox, OperationType operationType, uint32_t result); diff --git a/services/distributed/include/soft_bus/distributed_publish_service.h b/services/distributed/include/soft_bus/distributed_publish_service.h index 27a116cde..635eee5b4 100644 --- a/services/distributed/include/soft_bus/distributed_publish_service.h +++ b/services/distributed/include/soft_bus/distributed_publish_service.h @@ -36,11 +36,11 @@ public: #ifdef DISTRIBUTED_FEATURE_MASTER void SyncLiveViewNotification(const DistributedDeviceInfo peerDevice, bool isForce); + void SendNotifictionRequest(const std::shared_ptr request, + const DistributedDeviceInfo& peerDevice, bool isSyncNotification = false); 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, diff --git a/services/distributed/include/soft_bus/distributed_service.h b/services/distributed/include/soft_bus/distributed_service.h index 87c43b3bb..a14e533da 100644 --- a/services/distributed/include/soft_bus/distributed_service.h +++ b/services/distributed/include/soft_bus/distributed_service.h @@ -36,85 +36,32 @@ class DistributedService { public: DistributedService(); static DistributedService& GetInstance(); - void SubscribeNotifictaion(const DistributedDeviceInfo device); - void UnSubscribeNotifictaion(const std::string &deviceId, uint16_t deviceType); + void DestoryService(); int32_t InitService(const std::string &deviceId, uint16_t deviceType); - void OnReceiveMsg(const void *data, uint32_t dataLen); + void AddDevice(DistributedDeviceInfo device); + void ReleaseDevice(const std::string &deviceId, uint16_t deviceType); void OnConsumed(const std::shared_ptr &request, const DistributedDeviceInfo& device); + void OnApplicationInfnChanged(const std::string& bundleName); + int32_t OnOperationResponse(const std::shared_ptr& operationInfo, + const DistributedDeviceInfo& device); void OnCanceled(const std::shared_ptr& notification, const DistributedDeviceInfo& peerDevice); void OnBatchCanceled(const std::vector>& notifications, const DistributedDeviceInfo& peerDevice); - void InitDeviceState(const DistributedDeviceInfo device); - void SyncDeviceState(int32_t state); - void SyncConnectedDevice(DistributedDeviceInfo device); - int32_t SyncDeviceMatch(const DistributedDeviceInfo device, MatchType type); - void AddDevice(DistributedDeviceInfo device); - void HandleMatchSync(const std::shared_ptr& boxMessage); - void DestoryService(); - void ReportBundleIconList(const DistributedDeviceInfo peerDevice); - int32_t UpdateBundlesIcon(const std::unordered_map& icons, - const DistributedDeviceInfo peerDevice); - void RequestBundlesIcon(const DistributedDeviceInfo peerDevice, bool isForce); + void OnReceiveMsg(const void *data, uint32_t dataLen); +#ifdef DISTRIBUTED_FEATURE_MASTER void HandleBundlesEvent(const std::string& bundleName, const std::string& action); - void HandleBundleChanged(const std::string& bundleName, bool updatedExit); +#else + void SyncDeviceStatus(int32_t status); +#endif std::string GetNotificationKey(const std::shared_ptr& notification); - ErrCode OnResponse(const std::shared_ptr& operationInfo, - const DistributedDeviceInfo& device); - void SendNotifictionRequest(const std::shared_ptr request, - const DistributedDeviceInfo& peerDevice, bool isSyncNotification = false); - void SyncAllLiveViewNotification(const DistributedDeviceInfo peerDevice, bool isForce); - void SyncNotifictionList(const DistributedDeviceInfo& peerDevice, const std::vector& list); - void HandleNotificationSync(const std::shared_ptr& boxMessage); - bool IsReportHa(); - private: void OnHandleMsg(std::shared_ptr& box); - int64_t GetCurrentTime(); - void HandleBundleRemoved(const std::string& bundleName); - bool GetBundleResourceInfo(const std::string bundleName, std::string& icon); - void HandleBundleIconSync(const std::shared_ptr& boxMessage); - void GenerateBundleIconSync(const DistributedDeviceInfo& device); - bool CheckPeerDevice(const BundleIconBox& boxMessage, DistributedDeviceInfo& device); - void PublishNotifictaion(const std::shared_ptr& boxMessage); - void HandleDeviceState(const std::shared_ptr& boxMessage); - void HandleResponseSync(const std::shared_ptr& boxMessage); - void MakeNotificationContent(const NotificationRequestBox& box, sptr& request, - bool isCommonLiveView, int32_t contentType); - void MakeNotificationIcon(const NotificationRequestBox& box, sptr& request, - bool isCommonLiveView); - void SetNotifictaionContent(const NotificationRequestBox& box, sptr& request, - int32_t contentType); - void MakeNotificationReminderFlag(const NotificationRequestBox& box, sptr& request); - void RemoveNotification(const std::shared_ptr& boxMessage); - void RemoveNotifications(const std::shared_ptr& boxMessage); - void SetNotificationContent(const std::shared_ptr &content, - NotificationContent::Type type, std::shared_ptr& requestBox); - void GetNeedUpdateDevice(bool updatedExit, const std::string& bundleName, - std::vector& updateDeviceList); - void TriggerJumpApplication(const std::string& hashCode); - ErrCode TriggerReplyApplication(const std::string& hashCode, const NotificationResponseBox& responseBox); - void HandleOperationResponse(const std::string& hashCode, const NotificationResponseBox& responseBox); - void ReplyOperationResponse(const std::string& hashCode, const NotificationResponseBox& responseBox, - OperationType operationType, uint32_t result); - void SetNotificationButtons(const sptr notificationRequest, - NotificationConstant::SlotType slotType, std::shared_ptr& requestBox); - void MakeNotificationButtons(const NotificationRequestBox& box, NotificationConstant::SlotType slotType, - sptr& request); - int32_t GetCurrentActiveUserId(); - void TriggerReplyWantAgent(const sptr request, std::string actionName, - int32_t errorCode, std::string desc); - ErrCode GetNotificationButtonWantPtr(const std::string& hashCode, const std::string& actionName, - std::shared_ptr& wantPtr, sptr& request, std::string& userInputKey); + void ConnectPeerDevice(DistributedDeviceInfo device); + void HandleMatchSync(const std::shared_ptr& boxMessage); private: - std::function haCallback_ = nullptr; - std::function sendReportCallback_ = nullptr; - std::map> bundleIconCache_; - DistributedDeviceInfo localDevice_; - std::map peerDevice_; std::shared_ptr serviceQueue_ = nullptr; - std::map> subscriberMap_; }; } } diff --git a/services/distributed/include/soft_bus/distributed_subscribe_service.h b/services/distributed/include/soft_bus/distributed_subscribe_service.h index 50bb62aa4..058111c7d 100644 --- a/services/distributed/include/soft_bus/distributed_subscribe_service.h +++ b/services/distributed/include/soft_bus/distributed_subscribe_service.h @@ -24,11 +24,13 @@ namespace Notification { class DistributedSubscribeService { public: static DistributedSubscribeService& GetInstance(); + void UnSubscribeAllNotification(); void SubscribeNotification(const DistributedDeviceInfo peerDevice); - void UnSubscribeNotification(const std::string &deviceId); + void UnSubscribeNotification(const std::string &deviceId, uint16_t deviceType); private: std::map> subscriberMap_; }; } } #endif // DISTRIBUTED_INCLUDE_SOFTBUS_DISTRIBUTED_SUBSCRIBE_SERVICE_H + diff --git a/services/distributed/include/tlv_box/notification_sync_box.h b/services/distributed/include/tlv_box/notification_sync_box.h index 811e9f6c7..e8a96b3f6 100644 --- a/services/distributed/include/tlv_box/notification_sync_box.h +++ b/services/distributed/include/tlv_box/notification_sync_box.h @@ -28,12 +28,15 @@ public: NotificationSyncBox(); ~NotificationSyncBox(); NotificationSyncBox(std::shared_ptr box); +#ifdef DISTRIBUTED_FEATURE_MASTER bool SetLocalDeviceId(const std::string& deviceId); bool SetNotificationEmpty(const bool empty); bool SetNotificationList(const std::vector& notificationList); +#else bool GetLocalDeviceId(std::string& deviceId) const; bool GetNotificationEmpty(bool& empty) const; bool GetNotificationList(std::unordered_set& notificationList) const; +#endif }; } // namespace Notification } // namespace OHOS diff --git a/services/distributed/include/tlv_box/request_box.h b/services/distributed/include/tlv_box/request_box.h index c122e2754..388eb6e88 100644 --- a/services/distributed/include/tlv_box/request_box.h +++ b/services/distributed/include/tlv_box/request_box.h @@ -29,6 +29,7 @@ class NotificationRequestBox : public BoxBase { public: NotificationRequestBox(); NotificationRequestBox(std::shared_ptr box); +#ifdef DISTRIBUTED_FEATURE_MASTER bool SetNotificationHashCode(const std::string& hasdCode); bool SetSlotType(int32_t type); bool SetContentType(int32_t type); @@ -50,6 +51,7 @@ public: bool SetFinishTime(int64_t time); bool SetAutoDeleteTime(int64_t time); +#else bool GetNotificationHashCode(std::string& hasdCode) const; bool GetSlotType(int32_t& type) const; bool GetContentType(int32_t& type) const; @@ -70,6 +72,7 @@ public: bool GetCommonLiveView(std::vector& byteSequence) const; bool GetFinishTime(int64_t& time) const; bool GetAutoDeleteTime(int64_t& time) const; +#endif }; } // namespace Notification } // namespace OHOS diff --git a/services/distributed/include/tlv_box/state_box.h b/services/distributed/include/tlv_box/state_box.h index 2718c57d4..19cdb9ed1 100644 --- a/services/distributed/include/tlv_box/state_box.h +++ b/services/distributed/include/tlv_box/state_box.h @@ -26,13 +26,15 @@ class NotifticationStateBox : public BoxBase { public: NotifticationStateBox(); NotifticationStateBox(std::shared_ptr box); - bool SetDeviceType(const std::string& deviceType); - bool SetDeviceId(const std::string& deviceId); - bool SetState(int32_t state); - +#ifdef DISTRIBUTED_FEATURE_MASTER bool GetDeviceType(std::string& deviceType); bool GetDeviceId(std::string& deviceId); bool GetState(int32_t& state); +#else + bool SetDeviceType(const std::string& deviceType); + bool SetDeviceId(const std::string& deviceId); + bool SetState(int32_t state); +#endif }; } // namespace Notification } // namespace OHOS diff --git a/services/distributed/include/tlv_box/tlv_box.h b/services/distributed/include/tlv_box/tlv_box.h index 6a5bff369..8c6bd25aa 100644 --- a/services/distributed/include/tlv_box/tlv_box.h +++ b/services/distributed/include/tlv_box/tlv_box.h @@ -67,7 +67,7 @@ enum TlvType : int32_t { MATCH_TYPE = 993, PEER_DEVICE_ID = 994, PEER_DEVICE_TYPE = 995, - LOCAL_DEVICE_STATUE = 996, + LOCAL_DEVICE_STATUS = 996, LOCAL_DEVICE_ID = 997, LOCAL_DEVICE_TYPE = 998, LOCAL_VERSION = 999, diff --git a/services/distributed/src/helper/analytics_util.cpp b/services/distributed/src/helper/analytics_util.cpp index 74ae5aa52..88c5f50e0 100644 --- a/services/distributed/src/helper/analytics_util.cpp +++ b/services/distributed/src/helper/analytics_util.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ #include "analytics_util.h" -#include "distributed_service.h" +#include "distributed_device_service.h" namespace OHOS { namespace Notification { @@ -37,7 +37,7 @@ void AnalyticsUtil::InitSendReportCallBack(std::function lock(preferenceMutex_); if (preferncesDB_ == nullptr) { @@ -65,7 +65,7 @@ int32_t DistributedPreferences::DeleteBundleIcon(const std::string &bundleName) return 0; } -int32_t DistributedPreferences::InertBatchBundleIcons(std::unordered_map &values) +int32_t DistributedPreferences::InsertBatchBundleIcons(std::unordered_map &values) { std::lock_guard lock(preferenceMutex_); if (preferncesDB_ == nullptr) { diff --git a/services/distributed/src/soft_bus/distributed_bundle_service.cpp b/services/distributed/src/soft_bus/distributed_bundle_service.cpp index dd484330a..7c47be063 100644 --- a/services/distributed/src/soft_bus/distributed_bundle_service.cpp +++ b/services/distributed/src/soft_bus/distributed_bundle_service.cpp @@ -35,6 +35,39 @@ DistributedBundleService& DistributedBundleService::GetInstance() } #ifdef DISTRIBUTED_FEATURE_MASTER +void DistributedBundleService::HandleBundleIconSync(const std::shared_ptr& boxMessage) +{ + int32_t type = 0; + BundleIconBox iconBox = BundleIconBox(boxMessage); + if (!iconBox.GetIconSyncType(type)) { + ANS_LOGI("Dans handle bundle icon sync failed."); + return; + } + + ANS_LOGI("Dans handle bundl icon type %{public}d.", type); + if (type == IconSyncType::REPORT_SAVED_ICON) { + std::string deviceId; + if (!iconBox.GetLocalDeviceId(deviceId)) { + ANS_LOGI("Dans get deviceId failed."); + return; + } + + DistributedDeviceInfo device; + if (!DistributedDeviceService::GetInstance().GetDeviceInfo(deviceId, device)) { + return; + } + std::set bundleSet; + std::vector bundleList; + iconBox.GetBundleList(bundleList); + for (auto bundle : bundleList) { + ANS_LOGI("Dans handle receive %{public}s.", bundle.c_str()); + bundleSet.insert(bundle); + } + bundleIconCache_[device.deviceId_] = bundleSet; + GenerateBundleIconSync(device); + } +} + void DistributedBundleService::RequestBundlesIcon(const DistributedDeviceInfo peerDevice, bool isForce) { if (!DistributedDeviceService::GetInstance().CheckDeviceExist(peerDevice.deviceId_)) { @@ -231,6 +264,48 @@ void DistributedBundleService::GetNeedUpdateDevice(bool updatedExit, const std:: } } #else +void DistributedBundleService::HandleBundleIconSync(const std::shared_ptr& boxMessage) +{ + int32_t type = 0; + BundleIconBox iconBox = BundleIconBox(boxMessage); + if (!iconBox.GetIconSyncType(type)) { + ANS_LOGI("Dans handle bundle icon sync failed."); + return; + } + + ANS_LOGI("Dans handle bundl icon type %{public}d.", type); + if (type == IconSyncType::UPDATE_BUNDLE_ICON) { + std::unordered_map bundlesIcon; + if (!iconBox.GetBundlesIcon(bundlesIcon)) { + ANS_LOGI("Dans handle bundle icon get icon failed."); + return; + } + DistributedPreferences::GetInstance().InsertBatchBundleIcons(bundlesIcon); + } + + if (type == IconSyncType::REQUEST_BUNDLE_ICON) { + std::string deviceId; + if (!iconBox.GetLocalDeviceId(deviceId)) { + ANS_LOGI("Dans get deviceId failed."); + return; + } + + DistributedDeviceInfo device; + if (!DistributedDeviceService::GetInstance().GetDeviceInfo(deviceId, device)) { + return; + } + ReportBundleIconList(device); + } + + if (type == IconSyncType::REMOVE_BUNDLE_ICON) { + std::vector bundleList; + iconBox.GetBundleList(bundleList); + for (auto& bundle : bundleList) { + DistributedPreferences::GetInstance().DeleteBundleIcon(bundle); + } + } +} + void DistributedBundleService::ReportBundleIconList(const DistributedDeviceInfo peerDevice) { std::vector bundlesName; diff --git a/services/distributed/src/soft_bus/distributed_cmd_service.cpp b/services/distributed/src/soft_bus/distributed_cmd_service.cpp deleted file mode 100644 index 755f06561..000000000 --- a/services/distributed/src/soft_bus/distributed_cmd_service.cpp +++ /dev/null @@ -1,440 +0,0 @@ -/* - * Copyright (C) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "distributed_service.h" - -#include "notification_helper.h" -#include "distributed_client.h" -#include "state_box.h" -#include "in_process_call_wrapper.h" -#include "distributed_observer_service.h" -#include "distributed_device_data.h" -#include "dm_device_info.h" -#include "match_box.h" -#include "bundle_icon_box.h" -#include "distributed_preference.h" -#include "bundle_resource_helper.h" -#include "notification_sync_box.h" - -namespace OHOS { -namespace Notification { - -std::string TransDeviceTypeIdToName(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 ""; - } -} - -namespace { -constexpr uint32_t DEFAULT_LOCK_SCREEN_FLAG = 2; -} - -void DistributedService::InitDeviceState(const DistributedDeviceInfo device) -{ - if (device.deviceType_ == DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE && - localDevice_.deviceType_ != DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) { - int32_t state = OberverService::GetInstance().IsScreenLocked(); - SyncDeviceState(state); - } -} - -void DistributedService::HandleDeviceState(const std::shared_ptr& boxMessage) -{ - int32_t state; - std::string deviceName; - NotifticationStateBox stateBox = NotifticationStateBox(boxMessage); - if (!stateBox.GetDeviceType(deviceName) || !stateBox.GetState(state)) { - ANS_LOGW("Dans unbox state failed."); - return; - } - std::string deviceId; - if (!stateBox.GetDeviceId(deviceId)) { - ANS_LOGW("Dans unbox deviceId failed."); - } - uint32_t status = (static_cast(state) << 1); - int32_t result = NotificationHelper::SetTargetDeviceStatus(deviceName, status, - DEFAULT_LOCK_SCREEN_FLAG, deviceId); - ANS_LOGI("Dans set state %{public}s %{public}d.", deviceName.c_str(), state); -} - -void DistributedService::SyncDeviceState(int32_t state) -{ - if (serviceQueue_ == nullptr) { - ANS_LOGE("Check handler is null."); - return; - } - std::function task = std::bind([&, state]() { - 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, 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())); - } - }); - serviceQueue_->submit(task); -} - -int32_t DistributedService::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_, peerDevice.deviceType_); - 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; -} - -void DistributedService::SyncAllLiveViewNotification(const DistributedDeviceInfo peerDevice, bool isForce) -{ - if (localDevice_.deviceType_ != DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) { - return; - } - - 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::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 DistributedService::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_, 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_); -} - -void DistributedService::HandleMatchSync(const std::shared_ptr& boxMessage) -{ - int32_t type = 0; - DistributedDeviceInfo peerDevice; - NotifticationMatchBox matchBox = NotifticationMatchBox(boxMessage); - if (!matchBox.GetLocalDeviceType(type)) { - ANS_LOGI("Dans handle match device type failed."); - return; - } else { - peerDevice.deviceType_ = static_cast(type); - } - if (!matchBox.GetLocalDeviceId(peerDevice.deviceId_)) { - ANS_LOGI("Dans handle match device id failed."); - return; - } - int32_t matchType = 0; - if (!matchBox.GetMatchType(matchType)) { - ANS_LOGI("Dans handle match sync failed."); - return; - } - 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, false); - SubscribeNotifictaion(peerDevice); - SyncAllLiveViewNotification(peerDevice, false); - } - - auto iter = peerDevice_.find(peerDevice.deviceId_); - if (iter == peerDevice_.end()) { - ANS_LOGI("Dans get deviceId unknonw %{public}s.", peerDevice.deviceId_.c_str()); - return; - } - iter->second.isSync = true; -} - -bool DistributedService::CheckPeerDevice(const BundleIconBox& boxMessage, DistributedDeviceInfo& device) -{ - std::string deviceId; - if (!boxMessage.GetLocalDeviceId(deviceId)) { - ANS_LOGI("Dans get deviceId failed."); - return false; - } - auto iter = peerDevice_.find(deviceId); - if (iter == peerDevice_.end()) { - ANS_LOGI("Dans get deviceId unknonw %{public}s.", StringAnonymous(deviceId).c_str()); - return false; - } - device = iter->second; - return true; -} - -void DistributedService::ReportBundleIconList(const DistributedDeviceInfo peerDevice) -{ - if (localDevice_.deviceType_ == DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) { - return; - } - std::vector bundlesName; - DistributedPreferences::GetInstance().GetSavedBundlesIcon(bundlesName); - 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, 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, bool isForce) -{ - if (localDevice_.deviceType_ != DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) { - return; - } - - 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, 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_); -} - -int32_t DistributedService::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_, peerDevice.deviceType_); - 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_, result); - return result; -} - -void DistributedService::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}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) { - 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 DistributedService::HandleBundleIconSync(const std::shared_ptr& boxMessage) -{ - int32_t type = 0; - BundleIconBox iconBox = BundleIconBox(boxMessage); - if (!iconBox.GetIconSyncType(type)) { - ANS_LOGI("Dans handle bundle icon sync failed."); - return; - } - - ANS_LOGI("Dans handle bundl icon type %{public}d %{public}d.", type, localDevice_.deviceType_); - if (type == IconSyncType::REPORT_SAVED_ICON && - localDevice_.deviceType_ == DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) { - DistributedDeviceInfo device; - if (!CheckPeerDevice(iconBox, device)) { - return; - } - std::set bundleSet; - std::vector bundleList; - iconBox.GetBundleList(bundleList); - for (auto bundle : bundleList) { - ANS_LOGI("Dans handle receive %{public}s.", bundle.c_str()); - bundleSet.insert(bundle); - } - bundleIconCache_[device.deviceId_] = bundleSet; - GenerateBundleIconSync(device); - } - - if (type == IconSyncType::UPDATE_BUNDLE_ICON && - localDevice_.deviceType_ != DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) { - std::unordered_map bundlesIcon; - if (!iconBox.GetBundlesIcon(bundlesIcon)) { - ANS_LOGI("Dans handle bundle icon get icon failed."); - return; - } - DistributedPreferences::GetInstance().InertBatchBundleIcons(bundlesIcon); - } - - if (type == IconSyncType::REQUEST_BUNDLE_ICON && - localDevice_.deviceType_ != DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) { - DistributedDeviceInfo device; - if (!CheckPeerDevice(iconBox, device)) { - return; - } - ReportBundleIconList(device); - } - - if (type == IconSyncType::REMOVE_BUNDLE_ICON && - localDevice_.deviceType_ != DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) { - std::vector bundleList; - iconBox.GetBundleList(bundleList); - for (auto& bundle : bundleList) { - DistributedPreferences::GetInstance().DeleteBundleIcon(bundle); - } - } -} -} -} diff --git a/services/distributed/src/soft_bus/distributed_device_service.cpp b/services/distributed/src/soft_bus/distributed_device_service.cpp index 6a5636492..47ac0ecbb 100644 --- a/services/distributed/src/soft_bus/distributed_device_service.cpp +++ b/services/distributed/src/soft_bus/distributed_device_service.cpp @@ -21,6 +21,7 @@ #include "distributed_data_define.h" #include "distributed_device_data.h" #include "notification_helper.h" +#include "distributed_observer_service.h" namespace OHOS { namespace Notification { @@ -62,6 +63,11 @@ DistributedDeviceInfo DistributedDeviceService::GetLocalDevice() return localDevice_; } +bool DistributedDeviceService::IsReportDataByHa() +{ + return localDevice_.deviceType_ != DistributedHardware::DmDeviceType::DEVICE_TYPE_WATCH; +} + bool DistributedDeviceService::IsDeviceSyncData(const std::string& deviceId) { auto iter = peerDevice_.find(deviceId); @@ -79,7 +85,7 @@ void DistributedDeviceService::SetDeviceSyncData(const std::string& deviceId, bo ANS_LOGE("Dans unknown device set data %{public}s.", StringAnonymous(deviceId).c_str()); return; } - iter->second.isSync = true; + iter->second.isSync = syncData; } void DistributedDeviceService::SetDeviceState(const std::string& deviceId, int32_t state) @@ -101,6 +107,17 @@ bool DistributedDeviceService::CheckDeviceExist(const std::string& deviceId) return true; } +bool DistributedDeviceService::GetDeviceInfo(const std::string& deviceId, DistributedDeviceInfo& device) +{ + auto iter = peerDevice_.find(deviceId); + if (iter == peerDevice_.end()) { + ANS_LOGI("Dans get deviceId unknonw %{public}s.", StringAnonymous(deviceId).c_str()); + return false; + } + device = iter->second; + return true; +} + bool DistributedDeviceService::CheckDeviceNeedSync(const std::string& deviceId) { auto iter = peerDevice_.find(deviceId); @@ -124,6 +141,8 @@ void DistributedDeviceService::IncreaseDeviceSyncCount(const std::string& device return; } iter->second.connectedTry_ = iter->second.connectedTry_ + 1; + ANS_LOGI("Dans sync device count %{public}s %{public}d.", StringAnonymous(deviceId).c_str(), + iter->second.connectedTry_); } void DistributedDeviceService::AddDeviceInfo(DistributedDeviceInfo deviceItem) @@ -187,6 +206,12 @@ void DistributedDeviceService::SetDeviceStatus(const std::shared_ptr& bo ANS_LOGI("Dans set state %{public}s %{public}d.", deviceName.c_str(), status); } #else +void DistributedDeviceService::InitCurrentDeviceStatus() +{ + int32_t status = OberverService::GetInstance().IsScreenLocked(); + SyncDeviceStatus(status); +} + void DistributedDeviceService::SyncDeviceStatus(int32_t status) { std::shared_ptr stateBox = std::make_shared(); diff --git a/services/distributed/src/soft_bus/distributed_extern_intferface.cpp b/services/distributed/src/soft_bus/distributed_extern_intferface.cpp index a942d4573..85471cb3a 100644 --- a/services/distributed/src/soft_bus/distributed_extern_intferface.cpp +++ b/services/distributed/src/soft_bus/distributed_extern_intferface.cpp @@ -52,7 +52,7 @@ SYMBOL_EXPORT void ReleaseDevice(const std::string &deviceId, uint16_t deviceTyp { ANS_LOGI("ReleaseDevice %{public}s %{public}d.", StringAnonymous(deviceId).c_str(), (int32_t)(deviceType)); DistributedClient::GetInstance().ReleaseDevice(deviceId, deviceType); - DistributedService::GetInstance().UnSubscribeNotifictaion(deviceId, deviceType); + DistributedService::GetInstance().ReleaseDevice(deviceId, deviceType); } SYMBOL_EXPORT void RefreshDevice(const std::string &deviceId, uint16_t deviceType, diff --git a/services/distributed/src/soft_bus/distributed_observer_service.cpp b/services/distributed/src/soft_bus/distributed_observer_service.cpp index e31cdc937..2976ded77 100644 --- a/services/distributed/src/soft_bus/distributed_observer_service.cpp +++ b/services/distributed/src/soft_bus/distributed_observer_service.cpp @@ -32,14 +32,7 @@ void DistributedEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData auto const &want = data.GetWant(); std::string action = want.GetAction(); ANS_LOGI("DistributedEventSubscriber receiver event %{public}s", action.c_str()); - if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED) { - DistributedService::GetInstance().SyncDeviceState(SCREEN_OFF); - return; - } - if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) { - DistributedService::GetInstance().SyncDeviceState(SCREEN_ON); - return; - } +#ifdef DISTRIBUTED_FEATURE_MASTER if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) { OperationService::GetInstance().HandleScreenEvent(); return; @@ -55,6 +48,16 @@ void DistributedEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData DistributedService::GetInstance().HandleBundlesEvent(bundleName, action); return; } +#else + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED) { + DistributedService::GetInstance().SyncDeviceStatus(SCREEN_OFF); + return; + } + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) { + DistributedService::GetInstance().SyncDeviceStatus(SCREEN_ON); + return; + } +#endif } OberverService& OberverService::GetInstance() @@ -66,15 +69,14 @@ OberverService& OberverService::GetInstance() void OberverService::Init(uint16_t deviceType) { EventFwk::MatchingSkills matchingSkills; - if (deviceType != DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) { - matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED); - matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED); - } - if (deviceType == DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) { - matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); - matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED); - matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF); - } +#ifdef DISTRIBUTED_FEATURE_MASTER + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF); +#else + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED); +#endif EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills); subscriber_ = std::make_shared(subscribeInfo); if (subscriber_ == nullptr) { diff --git a/services/distributed/src/soft_bus/distributed_operation_service.cpp b/services/distributed/src/soft_bus/distributed_operation_service.cpp index aff2813e1..336e31600 100644 --- a/services/distributed/src/soft_bus/distributed_operation_service.cpp +++ b/services/distributed/src/soft_bus/distributed_operation_service.cpp @@ -43,6 +43,33 @@ DistributedOperationService& DistributedOperationService::GetInstance() return distributedOperationService; } +void DistributedOperationService::HandleNotificationOperation(const std::shared_ptr& boxMessage) +{ + int32_t operationType = 0; + int32_t matchType = 0; + std::string hashCode; + NotificationResponseBox responseBox = NotificationResponseBox(boxMessage); + responseBox.GetOperationType(operationType); + responseBox.GetMatchType(matchType); + responseBox.GetNotificationHashCode(hashCode); + ANS_LOGI("handle response, hashCode: %{public}s type: %{public}d %{public}d.", + hashCode.c_str(), operationType, matchType); +#ifdef DISTRIBUTED_FEATURE_MASTER + if (matchType == MatchType::MATCH_SYN) { + if (static_cast(operationType) == OperationType::DISTRIBUTE_OPERATION_JUMP) { + TriggerJumpApplication(hashCode); + } else if (static_cast(operationType) == OperationType::DISTRIBUTE_OPERATION_REPLY) { + ErrCode result = TriggerReplyApplication(hashCode, responseBox); + ReplyOperationResponse(hashCode, responseBox, OperationType::DISTRIBUTE_OPERATION_REPLY, result); + } + } +#else + if (matchType == MatchType::MATCH_ACK) { + ResponseOperationResult(hashCode, responseBox); + } +#endif +} + #ifdef DISTRIBUTED_FEATURE_MASTER static int64_t GetCurrentTime() { diff --git a/services/distributed/src/soft_bus/distributed_publish_service.cpp b/services/distributed/src/soft_bus/distributed_publish_service.cpp deleted file mode 100644 index 421e96ad7..000000000 --- a/services/distributed/src/soft_bus/distributed_publish_service.cpp +++ /dev/null @@ -1,655 +0,0 @@ -/* - * Copyright (C) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -#include "distributed_service.h" - -#include "ability_manager_client.h" -#include "notification_helper.h" -#include "distributed_client.h" -#include "request_box.h" -#include "state_box.h" -#include "ans_image_util.h" -#include "in_process_call_wrapper.h" -#include "distributed_observer_service.h" -#include "distributed_preference.h" -#include "distributed_liveview_all_scenarios_extension_wrapper.h" -#include "response_box.h" -#include "power_mgr_client.h" -#include "distributed_local_config.h" -#include "distributed_operation_helper.h" -#include "notification_sync_box.h" -#include "ans_inner_errors.h" -#include "ability_manager_helper.h" -#include "int_wrapper.h" -#include "string_wrapper.h" -#include "analytics_util.h" - -namespace OHOS { -namespace Notification { - -namespace { -const std::string DISTRIBUTED_LABEL = "ans_distributed"; -} - -int64_t GetCurrentTime() -{ - auto now = std::chrono::system_clock::now(); - auto duration = std::chrono::duration_cast(now.time_since_epoch()); - return duration.count(); -} - -struct TransferNotification { - std::string title; - std::string context; - std::string additionalText; - std::string briefText; - std::string expandedTitle; -}; - -void ConvertBoxToLongContent(const TransferNotification& notificationItem, const NotificationRequestBox& 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); -} - -void ConvertBoxToMultileContent(const TransferNotification& notificationItem, const NotificationRequestBox& 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); -} - -void ConvertBoxToPictureContent(const TransferNotification& notificationItem, const NotificationRequestBox& 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 DistributedService::SetNotifictaionContent(const NotificationRequestBox& 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; - } - } -} - -void DistributedService::MakeNotificationContent(const NotificationRequestBox& 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; - } - SetNotifictaionContent(box, request, contentType); -} - -void DistributedService::MakeNotificationIcon(const NotificationRequestBox& 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 DistributedService::MakeNotificationReminderFlag(const NotificationRequestBox& 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 DistributedService::MakeNotificationButtons(const NotificationRequestBox& 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 DistributedService::PublishNotifictaion(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; - NotificationRequestBox requestBox = NotificationRequestBox(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); - MakeNotificationContent(requestBox, request, isCommonLiveView, contentType); - MakeNotificationIcon(requestBox, request, isCommonLiveView); - MakeNotificationReminderFlag(requestBox, request); - int result = IN_PROCESS_CALL(NotificationHelper::PublishNotification(*request)); - ANS_LOGD("Dans publish result = %{public}d.", result); -} - -void DistributedService::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 DistributedService::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 DistributedService::HandleNotificationSync(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); - } -} - -std::shared_ptr GetNotificationWantPtr(const std::string& hashCode) -{ - sptr notificationRequest = new (std::nothrow) NotificationRequest(); - 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 DistributedService::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"); - } -} - -ErrCode DistributedService::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; -} - -void DistributedService::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); -} - -ErrCode DistributedService::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 DistributedService::HandleOperationResponse(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); -} - -void DistributedService::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); - - auto iter = peerDevice_.find(deviceId); - if (iter == peerDevice_.end()) { - 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, - iter->second.deviceId_, iter->second.deviceType_); - 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(iter->second.deviceId_).c_str(), result); - return; -} - -void DistributedService::HandleResponseSync(const std::shared_ptr& boxMessage) -{ - int32_t operationType = 0; - int32_t matchType = 0; - std::string hashCode; - NotificationResponseBox responseBox = NotificationResponseBox(boxMessage); - responseBox.GetOperationType(operationType); - responseBox.GetMatchType(matchType); - responseBox.GetNotificationHashCode(hashCode); - ANS_LOGI("handle response, hashCode: %{public}s type: %{public}d %{public}d.", - hashCode.c_str(), operationType, matchType); - - if (matchType == MatchType::MATCH_SYN) { - if (static_cast(operationType) == OperationType::DISTRIBUTE_OPERATION_JUMP) { - TriggerJumpApplication(hashCode); - } else if (static_cast(operationType) == OperationType::DISTRIBUTE_OPERATION_REPLY) { - ErrCode result = TriggerReplyApplication(hashCode, responseBox); - ReplyOperationResponse(hashCode, responseBox, OperationType::DISTRIBUTE_OPERATION_REPLY, result); - } - } else if (matchType == MatchType::MATCH_ACK) { - HandleOperationResponse(hashCode, responseBox); - } -} -} -} diff --git a/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp b/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp index 833a12bfc..14295b315 100644 --- a/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp +++ b/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp @@ -258,6 +258,60 @@ void DistributedPublishService::SendNotifictionRequest(const std::shared_ptr &content, + NotificationContent::Type type, std::shared_ptr& requestBox) +{ + if (content == nullptr || content->GetNotificationContent() == nullptr) { + return; + } + + ANS_LOGI("Set Notification notification content %{public}d.", type); + switch (type) { + case NotificationContent::Type::PICTURE: { + auto picture = std::static_pointer_cast(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()); + 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()); + 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()); + break; + } + case NotificationContent::Type::LIVE_VIEW: + case NotificationContent::Type::LOCAL_LIVE_VIEW: + case NotificationContent::Type::BASIC_TEXT: + default: { + std::shared_ptr contentBasic = + std::static_pointer_cast(content->GetNotificationContent()); + requestBox->SetNotificationTitle(contentBasic->GetTitle()); + requestBox->SetNotificationText(contentBasic->GetText()); + requestBox->SetNotificationAdditionalText(contentBasic->GetAdditionalText()); + break; + } + } +} + void DistributedPublishService::SetNotificationButtons(const sptr notificationRequest, NotificationConstant::SlotType slotType, std::shared_ptr& requestBox) { diff --git a/services/distributed/src/soft_bus/distributed_service.cpp b/services/distributed/src/soft_bus/distributed_service.cpp index d9ce7efc1..dbab00a71 100644 --- a/services/distributed/src/soft_bus/distributed_service.cpp +++ b/services/distributed/src/soft_bus/distributed_service.cpp @@ -23,17 +23,22 @@ #include "distributed_observer_service.h" #include "os_account_manager.h" #include "distributed_server.h" +#include "common_event_support.h" #include "distributed_device_data.h" +#include "distributed_bundle_service.h" +#include "distributed_device_service.h" +#include "distributed_operation_service.h" +#include "distributed_publish_service.h" +#include "distributed_subscribe_service.h" namespace OHOS { namespace Notification { +static const std::string DISTRIBUTED_LABEL = "ans_distributed"; + namespace { -static const int32_t MAX_CONNECTED_TYR = 5; static const int32_t ADD_DEVICE_SLEEP_TIMES_MS = 1000; // 1s static const uint64_t SYNC_TASK_DELAY = 7 * 1000 * 1000; -static const int32_t MAX_DATA_LENGTH = 7; -static const int32_t START_ANONYMOUS_INDEX = 5; } DistributedService& DistributedService::GetInstance() @@ -52,11 +57,27 @@ DistributedService::DistributedService() ANS_LOGI("Distributed service init successfully."); } +std::string DistributedService::GetNotificationKey(const std::shared_ptr& notification) +{ + if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr) { + ANS_LOGE("notification or GetNotificationRequestPoint is nullptr"); + return ""; + } + std::string notificationKey = notification->GetKey(); + if (notification->GetNotificationRequestPoint()->GetDistributedCollaborate()) { + size_t pos = notificationKey.find(DISTRIBUTED_LABEL); + if (pos != std::string::npos) { + notificationKey.erase(pos, DISTRIBUTED_LABEL.length()); + } + } else { + notificationKey = DISTRIBUTED_LABEL + notificationKey; + } + return notificationKey; +} + int32_t DistributedService::InitService(const std::string &deviceId, uint16_t deviceType) { - int32_t userId; - localDevice_.deviceId_ = deviceId; - localDevice_.deviceType_ = deviceType; + DistributedDeviceService::GetInstance().InitLocalDevice(deviceId, deviceType); if (DistributedServer::GetInstance().InitServer(deviceId, deviceType) != 0) { ANS_LOGI("Distributed service init server failed."); return -1; @@ -76,38 +97,27 @@ void DistributedService::DestoryService() DistributedClient::GetInstance().ReleaseClient(); DistributedServer::GetInstance().ReleaseServer(); OberverService::GetInstance().Destory(); - for (auto& subscriberInfo : subscriberMap_) { - int32_t result = NotificationHelper::UnSubscribeNotification(subscriberInfo.second); - ANS_LOGI("UnSubscribe %{public}s %{public}d.", subscriberInfo.first.c_str(), result); - } + DistributedSubscribeService::GetInstance().UnSubscribeAllNotification(); }); serviceQueue_->wait(handler); } -void DistributedService::SyncConnectedDevice(DistributedDeviceInfo device) +void DistributedService::ConnectPeerDevice(DistributedDeviceInfo device) { - auto iter = peerDevice_.find(device.deviceId_); - if (iter == peerDevice_.end()) { - ANS_LOGE("SyncConnectedDevice device is valid."); + if (!DistributedDeviceService::GetInstance().CheckDeviceNeedSync(device.deviceId_)) { + ANS_LOGE("ConnectPeerDevice device is failed."); return; } - if (iter->second.connectedTry_ >= MAX_CONNECTED_TYR || iter->second.peerState_ != DeviceState::STATE_SYNC) { - ANS_LOGE("SyncConnectedDevice no need try %{public}d.", iter->second.connectedTry_); + + int32_t result = DistributedDeviceService::GetInstance().SyncDeviceMatch(device, MatchType::MATCH_SYN); + ANS_LOGI("ConnectPeerDevice try %{public}d.", result); + DistributedDeviceService::GetInstance().IncreaseDeviceSyncCount(device.deviceId_); + if (serviceQueue_ == nullptr) { + ANS_LOGE("Check handler is null."); return; } - int32_t result = SyncDeviceMatch(device, MatchType::MATCH_SYN); - ANS_LOGI("SyncConnectedDevice try %{public}d %{public}d.", iter->second.connectedTry_, result); - iter->second.connectedTry_ = iter->second.connectedTry_ + 1; - if (result != 0) { - if (serviceQueue_ == nullptr) { - ANS_LOGE("Check handler is null."); - return; - } - serviceQueue_->submit_h([&, device]() { SyncConnectedDevice(device); }, - ffrt::task_attr().name("sync").delay(SYNC_TASK_DELAY)); - } else { - iter->second.connectedTry_ = 0; - } + serviceQueue_->submit_h([&, device]() { ConnectPeerDevice(device); }, + ffrt::task_attr().name("sync").delay(SYNC_TASK_DELAY)); } void DistributedService::AddDevice(DistributedDeviceInfo device) @@ -117,17 +127,201 @@ void DistributedService::AddDevice(DistributedDeviceInfo device) return; } serviceQueue_->submit_h([&, device]() { - ANS_LOGI("Dans AddDevice %{public}s %{public}d %{public}s %{public}d.", - StringAnonymous(device.deviceId_).c_str(), device.deviceType_, - StringAnonymous(localDevice_.deviceId_).c_str(), localDevice_.deviceType_); + ANS_LOGI("Dans AddDevice %{public}s %{public}d", StringAnonymous(device.deviceId_).c_str(), + device.deviceType_); DistributedDeviceInfo deviceItem = device; deviceItem.peerState_ = DeviceState::STATE_SYNC; - peerDevice_[deviceItem.deviceId_] = deviceItem; + DistributedDeviceService::GetInstance().AddDeviceInfo(deviceItem); // Delay linking to avoid bind failure, There is a delay in reporting the device online auto sleepTime = std::chrono::milliseconds(ADD_DEVICE_SLEEP_TIMES_MS); std::this_thread::sleep_for(sleepTime); - SyncConnectedDevice(device); + ConnectPeerDevice(device); + }); +} + +void DistributedService::ReleaseDevice(const std::string &deviceId, uint16_t deviceType) +{ + if (serviceQueue_ == nullptr) { + ANS_LOGE("Check handler is null."); + return; + } + std::function subscribeTask = std::bind([deviceId, deviceType]() { + DistributedSubscribeService::GetInstance().UnSubscribeNotification(deviceId, deviceType); + }); + serviceQueue_->submit(subscribeTask); +} + +void DistributedService::OnCanceled(const std::shared_ptr& notification, + const DistributedDeviceInfo& peerDevice) +{ + if (serviceQueue_ == nullptr) { + ANS_LOGE("check handler is null"); + return; + } + if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr) { + ANS_LOGE("notification or GetNotificationRequestPoint is nullptr"); + return; + } + std::string notificationKey = GetNotificationKey(notification); + auto slotType = notification->GetNotificationRequestPoint()->GetSlotType(); + std::function task = std::bind([peerDevice, notificationKey, slotType]() { + DistributedPublishService::GetInstance().OnRemoveNotification(peerDevice, + notificationKey, slotType); + }); + serviceQueue_->submit(task); +} + +void DistributedService::OnBatchCanceled(const std::vector>& notifications, + const DistributedDeviceInfo& peerDevice) +{ + if (serviceQueue_ == nullptr) { + ANS_LOGE("check handler is null."); + return; + } + + std::ostringstream keysStream; + std::ostringstream slotTypesStream; + for (auto notification : notifications) { + if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr) { + ANS_LOGE("notification or GetNotificationRequestPoint is nullptr"); + continue; + } + ANS_LOGI("dans OnBatchCanceled %{public}s", notification->Dump().c_str()); + keysStream << GetNotificationKey(notification) << ' '; + slotTypesStream << std::to_string(notification->GetNotificationRequestPoint()->GetSlotType()) << ' '; + } + std::string notificationKeys = keysStream.str(); + std::string slotTypes = slotTypesStream.str(); + std::function task = std::bind([peerDevice, notificationKeys, slotTypes]() { + DistributedPublishService::GetInstance().OnRemoveNotifications(peerDevice, + notificationKeys, slotTypes); + }); + serviceQueue_->submit(task); +} + +#ifdef DISTRIBUTED_FEATURE_MASTER +int32_t DistributedService::OnOperationResponse(const std::shared_ptr & operationInfo, + const DistributedDeviceInfo& device) +{ + return ERR_OK; +} + +void DistributedService::OnConsumed(const std::shared_ptr &request, + const DistributedDeviceInfo& peerDevice) +{ + if (serviceQueue_ == nullptr) { + ANS_LOGE("Check handler is null."); + return; + } + std::function task = std::bind([request, peerDevice]() { + DistributedPublishService::GetInstance().SendNotifictionRequest(request, peerDevice); + }); + serviceQueue_->submit(task); +} + +void DistributedService::OnApplicationInfnChanged(const std::string& bundleName) +{ + if (serviceQueue_ == nullptr) { + ANS_LOGE("Check handler is null."); + return; + } + + std::function task = std::bind([&, bundleName]() { + DistributedBundleService::GetInstance().HandleBundleChanged(bundleName, false); + }); + serviceQueue_->submit(task); +} + +void DistributedService::HandleBundlesEvent(const std::string& bundleName, const std::string& action) +{ + if (serviceQueue_ == nullptr) { + ANS_LOGE("Check handler is null."); + return; + } + + std::function task = std::bind([&, bundleName, action]() { + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) { + DistributedBundleService::GetInstance().HandleBundleChanged(bundleName, true); + } + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) { + DistributedBundleService::GetInstance().HandleBundleRemoved(bundleName); + } + ANS_LOGI("Handle bundle event %{public}s, %{public}s.", bundleName.c_str(), action.c_str()); + }); + serviceQueue_->submit(task); +} +#else +void DistributedService::OnConsumed(const std::shared_ptr &request, + const DistributedDeviceInfo& peerDevice) +{ + return; +} + +int32_t DistributedService::OnOperationResponse(const std::shared_ptr & operationInfo, + const DistributedDeviceInfo& device) +{ + return DistributedOperationService::GetInstance().OnOperationResponse(operationInfo, device); +} + +void DistributedService::SyncDeviceStatus(int32_t status) +{ + if (serviceQueue_ == nullptr) { + ANS_LOGE("Check handler is null."); + return; + } + status = (static_cast(status) << 1); + std::function task = std::bind([&, status]() { + DistributedDeviceService::GetInstance().SyncDeviceStatus(status); }); + serviceQueue_->submit(task); +} + +void DistributedService::OnApplicationInfnChanged(const std::string& bundleName) +{ + return; +} +#endif + +void DistributedService::HandleMatchSync(const std::shared_ptr& boxMessage) +{ + int32_t type = 0; + DistributedDeviceInfo peerDevice; + NotifticationMatchBox matchBox = NotifticationMatchBox(boxMessage); + if (!matchBox.GetLocalDeviceType(type)) { + ANS_LOGI("Dans handle match device type failed."); + return; + } else { + peerDevice.deviceType_ = static_cast(type); + } + if (!matchBox.GetLocalDeviceId(peerDevice.deviceId_)) { + ANS_LOGI("Dans handle match device id failed."); + return; + } + int32_t matchType = 0; + if (!matchBox.GetMatchType(matchType)) { + ANS_LOGI("Dans handle match sync failed."); + return; + } + ANS_LOGI("Dans handle match device type %{public}d.", matchType); +#ifdef DISTRIBUTED_FEATURE_MASTER + if (matchType == MatchType::MATCH_SYN) { + DistributedDeviceService::GetInstance().SyncDeviceMatch(peerDevice, MatchType::MATCH_ACK); + DistributedBundleService::GetInstance().RequestBundlesIcon(peerDevice, true); + DistributedPublishService::GetInstance().SyncLiveViewNotification(peerDevice, true); + } else if (matchType == MatchType::MATCH_ACK) { + DistributedBundleService::GetInstance().RequestBundlesIcon(peerDevice, false); + DistributedSubscribeService::GetInstance().SubscribeNotification(peerDevice); + DistributedPublishService::GetInstance().SyncLiveViewNotification(peerDevice, false); + } + DistributedDeviceService::GetInstance().SetDeviceSyncData(peerDevice.deviceId_, true); +#else + if (matchType == MatchType::MATCH_SYN) { + DistributedDeviceService::GetInstance().SyncDeviceMatch(peerDevice, MatchType::MATCH_ACK); + } else if (matchType == MatchType::MATCH_ACK) { + DistributedDeviceService::GetInstance().InitCurrentDeviceStatus(); + DistributedSubscribeService::GetInstance().SubscribeNotification(peerDevice); + } +#endif } void DistributedService::OnHandleMsg(std::shared_ptr& box) @@ -144,31 +338,34 @@ void DistributedService::OnHandleMsg(std::shared_ptr& box) } ANS_LOGI("Dans handle message type %{public}d.", type); switch (type) { - case NotificationEventType::PUBLISH_NOTIFICATION: - PublishNotifictaion(box); - break; - case NotificationEventType::NOTIFICATION_STATE_SYNC: - HandleDeviceState(box); - break; case NotificationEventType::NOTIFICATION_MATCH_SYNC: HandleMatchSync(box); break; case NotificationEventType::REMOVE_NOTIFICATION: - RemoveNotification(box); + DistributedPublishService::GetInstance().RemoveNotification(box); break; case NotificationEventType::REMOVE_ALL_NOTIFICATIONS: - RemoveNotifications(box); + DistributedPublishService::GetInstance().RemoveNotifications(box); break; case NotificationEventType::BUNDLE_ICON_SYNC: - HandleBundleIconSync(box); - break; - case NotificationEventType::SYNC_NOTIFICATION: - HandleNotificationSync(box); + DistributedBundleService::GetInstance().HandleBundleIconSync(box); break; case NotificationEventType::NOTIFICATION_RESPONSE_SYNC: case NotificationEventType::NOTIFICATION_RESPONSE_REPLY_SYNC: - HandleResponseSync(box); + DistributedOperationService::GetInstance().HandleNotificationOperation(box); + break; +#ifdef DISTRIBUTED_FEATURE_MASTER + case NotificationEventType::NOTIFICATION_STATE_SYNC: + DistributedDeviceService::GetInstance().SetDeviceStatus(box); + break; +#else + case NotificationEventType::PUBLISH_NOTIFICATION: + DistributedPublishService::GetInstance().PublishNotification(box); break; + case NotificationEventType::SYNC_NOTIFICATION: + DistributedPublishService::GetInstance().PublishSynchronousLiveView(box); + break; +#endif default: ANS_LOGW("Dans receive msg %{public}d %{public}d.", type, box->bytesLength_); break; @@ -190,17 +387,5 @@ void DistributedService::OnReceiveMsg(const void *data, uint32_t dataLen) } OnHandleMsg(box); } - -int64_t DistributedService::GetCurrentTime() -{ - auto now = std::chrono::system_clock::now(); - auto duration = std::chrono::duration_cast(now.time_since_epoch()); - return duration.count(); -} - -bool DistributedService::IsReportHa() -{ - return localDevice_.deviceType_ == DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE; -} } } diff --git a/services/distributed/src/soft_bus/distributed_service_uitls.cpp b/services/distributed/src/soft_bus/distributed_service_uitls.cpp deleted file mode 100644 index 6208a797e..000000000 --- a/services/distributed/src/soft_bus/distributed_service_uitls.cpp +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright (C) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "distributed_service.h" - -#include "socket.h" -#include "session.h" -#include "distributed_device_data.h" -#include "dm_device_info.h" -#include "ans_log_wrapper.h" -#include "distributed_socket.h" -#include "distributed_preference.h" -#include "bundle_resource_helper.h" -#include "common_event_manager.h" -#include "common_event_support.h" -#include "distributed_client.h" -#include "ans_image_util.h" -#include "os_account_manager.h" - -namespace OHOS { -namespace Notification { - -int32_t DistributedService::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; -} - -bool DistributedService::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}d.", bundleName.c_str(), - (int32_t)(resourceInfo.icon.size())); - return true; -} - -void DistributedService::GetNeedUpdateDevice(bool updatedExit, const std::string& bundleName, - std::vector& updateDeviceList) -{ - for (auto& device : peerDevice_) { - 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); - } - } -} - -void DistributedService::HandleBundleChanged(const std::string& bundleName, bool updatedExit) -{ - if (serviceQueue_ == nullptr) { - ANS_LOGE("Check handler is null."); - return; - } - - std::function task = std::bind([&, bundleName, 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); - } - }); - serviceQueue_->submit(task); -} - -void DistributedService::HandleBundleRemoved(const std::string& bundleName) -{ - if (serviceQueue_ == nullptr) { - ANS_LOGE("Check handler is null."); - return; - } - - std::function task = std::bind([&, bundleName]() { - for (auto& device : peerDevice_) { - 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_, device.second.deviceType_); - ANS_LOGI("Dans ReportBundleIconList %{public}s %{public}d %{public}s %{public}d.", - StringAnonymous(device.second.deviceId_).c_str(), device.second.deviceType_, - StringAnonymous(localDevice_.deviceId_).c_str(), localDevice_.deviceType_); - } - }); - serviceQueue_->submit(task); -} - -void DistributedService::HandleBundlesEvent(const std::string& bundleName, const std::string& action) -{ - if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) { - HandleBundleChanged(bundleName, true); - } - if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) { - HandleBundleRemoved(bundleName); - } -} - -} -} diff --git a/services/distributed/src/soft_bus/distributed_subscribe_service.cpp b/services/distributed/src/soft_bus/distributed_subscribe_service.cpp deleted file mode 100644 index ef428bfe1..000000000 --- a/services/distributed/src/soft_bus/distributed_subscribe_service.cpp +++ /dev/null @@ -1,406 +0,0 @@ -/* - * Copyright (C) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -#include "distributed_service.h" - -#include "notification_helper.h" -#include "distributed_client.h" -#include "request_box.h" -#include "state_box.h" -#include "in_process_call_wrapper.h" -#include "distributed_observer_service.h" -#include "notification_subscribe_info.h" -#include "distributed_liveview_all_scenarios_extension_wrapper.h" -#include "distributed_preference.h" -#include "batch_remove_box.h" -#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; - -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 ""; - } -} - -void DistributedService::SubscribeNotifictaion(const DistributedDeviceInfo peerDevice) -{ - if (peerDevice_.find(peerDevice.deviceId_) == peerDevice_.end()) { - ANS_LOGI("Local device no %{public}s %{public}d.", StringAnonymous(peerDevice.deviceId_).c_str(), - peerDevice.deviceType_); - return; - } - - int32_t userId = GetCurrentActiveUserId(); - std::shared_ptr subscriber = std::make_shared(); - subscriber->SetLocalDevice(localDevice_); - 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; - peerDevice_[peerDevice.deviceId_].peerState_ = DeviceState::STATE_ONLINE; - if (haCallback_ != nullptr) { - std::string reason = "deviceType: " + std::to_string(localDevice_.deviceType_) + - " ; deviceId: " + StringAnonymous(localDevice_.deviceId_); - 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); -} - -void DistributedService::UnSubscribeNotifictaion(const std::string &deviceId, uint16_t deviceType) -{ - if (serviceQueue_ == nullptr) { - ANS_LOGE("Check handler is null."); - return; - } - std::function subscribeTask = std::bind([&, deviceId, deviceType]() { - auto deviceIter = peerDevice_.find(deviceId); - if (deviceIter != peerDevice_.end()) { - ANS_LOGI("UnSubscribe device %{public}s %{public}d.", StringAnonymous(deviceId).c_str(), deviceType); - peerDevice_.erase(deviceId); - } - - auto iter = subscriberMap_.find(deviceId); - if (iter == subscriberMap_.end()) { - ANS_LOGI("UnSubscribe invalid %{public}s %{public}d.", StringAnonymous(deviceId).c_str(), deviceType); - return; - } - - 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, std::shared_ptr& requestBox) -{ - if (content == nullptr || content->GetNotificationContent() == nullptr) { - return; - } - - ANS_LOGI("Set Notification notification content %{public}d.", type); - switch (type) { - case NotificationContent::Type::PICTURE: { - auto picture = std::static_pointer_cast(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()); - 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()); - 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()); - break; - } - case NotificationContent::Type::LIVE_VIEW: - case NotificationContent::Type::LOCAL_LIVE_VIEW: - case NotificationContent::Type::BASIC_TEXT: - default: { - std::shared_ptr contentBasic = - std::static_pointer_cast(content->GetNotificationContent()); - requestBox->SetNotificationTitle(contentBasic->GetTitle()); - requestBox->SetNotificationText(contentBasic->GetText()); - requestBox->SetNotificationAdditionalText(contentBasic->GetAdditionalText()); - break; - } - } -} - -void DistributedService::SetNotificationButtons(const sptr notificationRequest, - NotificationConstant::SlotType slotType, std::shared_ptr& requestBox) -{ - if (notificationRequest == nullptr) { - return; - } - 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()); - } - } -} - -void DistributedService::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."); - if (haCallback_ != nullptr) { - haCallback_(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 DistributedService::OnConsumed(const std::shared_ptr &request, - const DistributedDeviceInfo& peerDevice) -{ - if (serviceQueue_ == nullptr) { - ANS_LOGE("Check handler is null."); - return; - } - std::function task = std::bind([&, peerDevice, request]() { - SendNotifictionRequest(request, peerDevice); - }); - serviceQueue_->submit(task); -} - -void DistributedService::OnBatchCanceled(const std::vector>& notifications, - const DistributedDeviceInfo& peerDevice) -{ - if (serviceQueue_ == nullptr) { - ANS_LOGE("check handler is null."); - return; - } - - std::ostringstream keysStream; - std::ostringstream slotTypesStream; - for (auto notification : notifications) { - if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr) { - ANS_LOGE("notification or GetNotificationRequestPoint is nullptr"); - continue; - } - auto notificationFlags = notification->GetNotificationRequestPoint()->GetFlags(); - ANS_LOGI("Dans OnConsumed Notification key = %{public}s, notificationFlag = %{public}s", notification->GetKey().c_str(), - notificationFlags == nullptr ? "null" : notificationFlags->Dump().c_str()); - keysStream << GetNotificationKey(notification) << ' '; - slotTypesStream << std::to_string(notification->GetNotificationRequestPoint()->GetSlotType()) << ' '; - } - std::string notificationKeys = keysStream.str(); - std::string slotTypes = slotTypesStream.str(); - - std::function task = std::bind([peerDevice, notifications, notificationKeys, slotTypes]() { - std::shared_ptr batchRemoveBox = std::make_shared(); - if (!notificationKeys.empty()) { - batchRemoveBox->SetNotificationHashCode(notificationKeys); - } - 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); - }); - serviceQueue_->submit(task); -} - -void DistributedService::OnCanceled(const std::shared_ptr& notification, - const DistributedDeviceInfo& peerDevice) -{ - if (serviceQueue_ == nullptr) { - ANS_LOGE("check handler is null"); - return; - } - if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr) { - ANS_LOGE("notification or GetNotificationRequestPoint is nullptr"); - return; - } - std::string notificationKey = GetNotificationKey(notification); - std::function task = std::bind([peerDevice, notification, notificationKey]() { - std::shared_ptr removeBox = std::make_shared(); - auto notificationFlags = notification->GetNotificationRequestPoint()->GetFlags(); - ANS_LOGI("Dans OnCanceled Notification key = %{public}s, notificationFlag = %{public}s", notification->GetKey().c_str(), - notificationFlags == nullptr ? "null" : notificationFlags->Dump().c_str()); - removeBox->SetNotificationHashCode(notificationKey); - removeBox->setNotificationSlotType(notification->GetNotificationRequestPoint()->GetSlotType()); - if (!removeBox->Serialize()) { - ANS_LOGW("Dans OnCanceled serialize failed"); - return; - } - DistributedClient::GetInstance().SendMessage(removeBox, TransDataType::DATA_TYPE_MESSAGE, - peerDevice.deviceId_, DELETE_ERROR_EVENT_CODE); - }); - serviceQueue_->submit(task); -} - -std::string DistributedService::GetNotificationKey(const std::shared_ptr& notification) -{ - if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr) { - ANS_LOGE("notification or GetNotificationRequestPoint is nullptr"); - return ""; - } - std::string notificationKey = notification->GetKey(); - if (notification->GetNotificationRequestPoint()->GetDistributedCollaborate()) { - size_t pos = notificationKey.find(DISTRIBUTED_LABEL); - if (pos != std::string::npos) { - notificationKey.erase(pos, DISTRIBUTED_LABEL.length()); - } - } else { - notificationKey = DISTRIBUTED_LABEL + notificationKey; - } - return notificationKey; -} - -ErrCode DistributedService::OnResponse( - 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()); - } - - 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; -} -} -} diff --git a/services/distributed/src/soft_bus/distributed_subscribe_service_v2.cpp b/services/distributed/src/soft_bus/distributed_subscribe_service_v2.cpp index 27d937d95..0f2e9f2fc 100644 --- a/services/distributed/src/soft_bus/distributed_subscribe_service_v2.cpp +++ b/services/distributed/src/soft_bus/distributed_subscribe_service_v2.cpp @@ -95,7 +95,7 @@ void DistributedSubscribeService::SubscribeNotification(const DistributedDeviceI StringAnonymous(peerDevice.deviceId_).c_str(), peerDevice.deviceType_, userId, result); } -void DistributedSubscribeService::UnSubscribeNotification(const std::string &deviceId) +void DistributedSubscribeService::UnSubscribeNotification(const std::string &deviceId, uint16_t deviceType) { DistributedDeviceService::GetInstance().DeleteDeviceInfo(deviceId); auto iter = subscriberMap_.find(deviceId); @@ -104,11 +104,24 @@ void DistributedSubscribeService::UnSubscribeNotification(const std::string &dev return; } - if (NotificationHelper::UnSubscribeNotification(iter->second) == 0) { + int32_t result = NotificationHelper::UnSubscribeNotification(iter->second); + if (result == ERR_OK) { subscriberMap_.erase(deviceId); } - ANS_LOGI("UnSubscribe notification %{public}s.", StringAnonymous(deviceId).c_str()); + 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); } +void DistributedSubscribeService::UnSubscribeAllNotification() +{ + for (auto& subscriberInfo : subscriberMap_) { + int32_t result = NotificationHelper::UnSubscribeNotification(subscriberInfo.second); + ANS_LOGI("UnSubscribe %{public}s %{public}d.", subscriberInfo.first.c_str(), result); + } } + } +} + diff --git a/services/distributed/src/soft_bus/distributed_subscriber.cpp b/services/distributed/src/soft_bus/distributed_subscriber.cpp index 251533fca..3b74ca545 100644 --- a/services/distributed/src/soft_bus/distributed_subscriber.cpp +++ b/services/distributed/src/soft_bus/distributed_subscriber.cpp @@ -127,7 +127,7 @@ ErrCode DistribuedSubscriber::OnOperationResponse(const std::shared_ptr box) : BoxBase( { } +#ifdef DISTRIBUTED_FEATURE_MASTER bool NotificationSyncBox::SetLocalDeviceId(const std::string& deviceId) { if (box_ == nullptr) { @@ -66,6 +67,7 @@ bool NotificationSyncBox::SetNotificationEmpty(const bool empty) return box_->PutValue(std::make_shared(NOTIFICATION_HASHCODE + 1, empty)); } +#else bool NotificationSyncBox::GetLocalDeviceId(std::string& deviceId) const { if (box_ == nullptr) { @@ -102,5 +104,6 @@ bool NotificationSyncBox::GetNotificationEmpty(bool& empty) const } return box_->GetBoolValue(NOTIFICATION_HASHCODE + 1, empty); } +#endif } } diff --git a/services/distributed/src/tlv_box/request_box.cpp b/services/distributed/src/tlv_box/request_box.cpp index a8b1d6d7b..13bd8c8c2 100644 --- a/services/distributed/src/tlv_box/request_box.cpp +++ b/services/distributed/src/tlv_box/request_box.cpp @@ -34,6 +34,7 @@ NotificationRequestBox::NotificationRequestBox(std::shared_ptr box) : Bo { } +#ifdef DISTRIBUTED_FEATURE_MASTER bool NotificationRequestBox::SetNotificationHashCode(const std::string& hasdCode) { if (box_ == nullptr) { @@ -219,6 +220,7 @@ bool NotificationRequestBox::SetAutoDeleteTime(int64_t time) return box_->PutValue(std::make_shared(AUTO_DELETE_TIME, time)); } +#else bool NotificationRequestBox::GetNotificationHashCode(std::string& hasdCode) const { if (box_ == nullptr) { @@ -384,5 +386,6 @@ bool NotificationRequestBox::GetAutoDeleteTime(int64_t& time) const } return box_->GetInt64Value(AUTO_DELETE_TIME, time); } +#endif } } diff --git a/services/distributed/src/tlv_box/state_box.cpp b/services/distributed/src/tlv_box/state_box.cpp index 748f9ae70..b6a0690ec 100644 --- a/services/distributed/src/tlv_box/state_box.cpp +++ b/services/distributed/src/tlv_box/state_box.cpp @@ -30,52 +30,54 @@ NotifticationStateBox::NotifticationStateBox(std::shared_ptr box) : BoxB { } -bool NotifticationStateBox::SetDeviceType(const std::string& deviceType) +#ifdef DISTRIBUTED_FEATURE_MASTER +bool NotifticationStateBox::GetDeviceType(std::string& deviceType) { if (box_ == nullptr) { return false; } - return box_->PutValue(std::make_shared(LOCAL_DEVICE_TYPE, deviceType)); + return box_->GetStringValue(LOCAL_DEVICE_TYPE, deviceType); } -bool NotifticationStateBox::SetDeviceId(const std::string& deviceId) +bool NotifticationStateBox::GetDeviceId(std::string& deviceId) { if (box_ == nullptr) { return false; } - return box_->PutValue(std::make_shared(LOCAL_DEVICE_ID, deviceId)); + return box_->GetStringValue(LOCAL_DEVICE_ID, deviceId); } -bool NotifticationStateBox::SetState(int32_t state) +bool NotifticationStateBox::GetState(int32_t& state) { if (box_ == nullptr) { return false; } - return box_->PutValue(std::make_shared(LOCAL_DEVICE_STATUE, state)); + return box_->GetInt32Value(LOCAL_DEVICE_STATUS, state); } - -bool NotifticationStateBox::GetDeviceType(std::string& deviceType) +#else +bool NotifticationStateBox::SetDeviceType(const std::string& deviceType) { if (box_ == nullptr) { return false; } - return box_->GetStringValue(LOCAL_DEVICE_TYPE, deviceType); + return box_->PutValue(std::make_shared(LOCAL_DEVICE_TYPE, deviceType)); } -bool NotifticationStateBox::GetDeviceId(std::string& deviceId) +bool NotifticationStateBox::SetDeviceId(const std::string& deviceId) { if (box_ == nullptr) { return false; } - return box_->GetStringValue(LOCAL_DEVICE_ID, deviceId); + return box_->PutValue(std::make_shared(LOCAL_DEVICE_ID, deviceId)); } -bool NotifticationStateBox::GetState(int32_t& state) +bool NotifticationStateBox::SetState(int32_t state) { if (box_ == nullptr) { return false; } - return box_->GetInt32Value(LOCAL_DEVICE_STATUE, state); + return box_->PutValue(std::make_shared(LOCAL_DEVICE_STATUS, state)); } +#endif } } diff --git a/services/distributed/test/unittest/BUILD.gn b/services/distributed/test/unittest/BUILD.gn index a394eb899..8a09f6caa 100644 --- a/services/distributed/test/unittest/BUILD.gn +++ b/services/distributed/test/unittest/BUILD.gn @@ -447,15 +447,11 @@ ohos_unittest("distributed_service_test") { "/${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", @@ -545,15 +541,11 @@ ohos_unittest("distributed_subscriber_test") { "/${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", diff --git a/services/distributed/test/unittest/distributed_service_test.cpp b/services/distributed/test/unittest/distributed_service_test.cpp index 77176da3a..897ad1e71 100644 --- a/services/distributed/test/unittest/distributed_service_test.cpp +++ b/services/distributed/test/unittest/distributed_service_test.cpp @@ -19,6 +19,7 @@ #define private public #include "batch_remove_box.h" #include "distributed_service.h" +#include "distributed_publish_service.h" #undef private #include "remove_box.h" @@ -45,7 +46,7 @@ void DistributedServiceTest::TearDown() {} HWTEST_F(DistributedServiceTest, DistributedServiceTest_00100, Function | SmallTest | Level1) { std::shared_ptr boxMessage = nullptr; - DistributedService::GetInstance().RemoveNotification(boxMessage); + DistributedPublishService::GetInstance().RemoveNotification(boxMessage); ASSERT_EQ(boxMessage, nullptr); } @@ -59,7 +60,7 @@ HWTEST_F(DistributedServiceTest, DistributedServiceTest_00200, Function | SmallT NotificationRemoveBox removeBox; std::string notificationKey = "notificationId"; removeBox.SetNotificationHashCode(notificationKey); - DistributedService::GetInstance().RemoveNotification(removeBox.box_); + DistributedPublishService::GetInstance().RemoveNotification(removeBox.box_); ASSERT_NE(removeBox.box_, nullptr); } @@ -71,7 +72,7 @@ HWTEST_F(DistributedServiceTest, DistributedServiceTest_00200, Function | SmallT HWTEST_F(DistributedServiceTest, DistributedServiceTest_00300, Function | SmallTest | Level1) { std::shared_ptr boxMessage = nullptr; - DistributedService::GetInstance().RemoveNotifications(boxMessage); + DistributedPublishService::GetInstance().RemoveNotifications(boxMessage); ASSERT_EQ(boxMessage, nullptr); } @@ -85,7 +86,7 @@ HWTEST_F(DistributedServiceTest, DistributedServiceTest_00400, Function | SmallT BatchRemoveNotificationBox batchRemoveBox; std::string hashCodes = "notificationId1 notificationId2"; batchRemoveBox.SetNotificationHashCode(hashCodes); - DistributedService::GetInstance().RemoveNotifications(batchRemoveBox.box_); + DistributedPublishService::GetInstance().RemoveNotifications(batchRemoveBox.box_); ASSERT_NE(batchRemoveBox.box_, nullptr); } @@ -207,4 +208,4 @@ HWTEST_F(DistributedServiceTest, DistributedServiceTest_01100, Function | SmallT ASSERT_EQ(result, "ans_distributed_notificationKey"); } } // namespace Notification -} // namespace OHOS \ No newline at end of file +} // namespace OHOS -- Gitee