1 /* 2 * Copyright (C) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_H 17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_H 18 19 #include <string> 20 #include "ffrt.h" 21 #include "socket.h" 22 #include "distributed_subscriber.h" 23 #include "distributed_device_data.h" 24 #include "request_box.h" 25 #include "match_box.h" 26 #include <functional> 27 #include "bundle_icon_box.h" 28 #include <unordered_set> 29 #include "distributed_data_define.h" 30 #include "response_box.h" 31 32 namespace OHOS { 33 namespace Notification { 34 35 class DistributedService { 36 public: 37 DistributedService(); 38 static DistributedService& GetInstance(); 39 void SubscribeNotifictaion(const DistributedDeviceInfo device); 40 void UnSubscribeNotifictaion(const std::string &deviceId, uint16_t deviceType); 41 int32_t InitService(const std::string &deviceId, uint16_t deviceType); 42 void OnReceiveMsg(const void *data, uint32_t dataLen); 43 void OnConsumed(const std::shared_ptr<Notification> &request, 44 const DistributedDeviceInfo& device); 45 void OnCanceled(const std::shared_ptr<Notification>& notification, const DistributedDeviceInfo& peerDevice); 46 void OnBatchCanceled(const std::vector<std::shared_ptr<Notification>>& notifications, 47 const DistributedDeviceInfo& peerDevice); 48 void InitDeviceState(const DistributedDeviceInfo device); 49 void SyncDeviceState(int32_t state); 50 void SyncConnectedDevice(DistributedDeviceInfo device); 51 int32_t SyncDeviceMatch(const DistributedDeviceInfo device, MatchType type); 52 void AddDevice(DistributedDeviceInfo device); 53 void HandleMatchSync(const std::shared_ptr<TlvBox>& boxMessage); 54 void DestoryService(); 55 void ReportBundleIconList(const DistributedDeviceInfo peerDevice); 56 void UpdateBundlesIcon(const std::unordered_map<std::string, std::string>& icons, 57 const DistributedDeviceInfo peerDevice); 58 void RequestBundlesIcon(const DistributedDeviceInfo peerDevice); 59 void HandleBundlesEvent(const std::string& bundleName, const std::string& action); 60 void HandleBundleChanged(const std::string& bundleName, bool updatedExit); 61 std::string GetNotificationKey(const std::shared_ptr<Notification>& notification); 62 void SendEventReport(int32_t messageType, int32_t errCode, const std::string& errorReason); 63 void InitHACallBack(std::function<void(int32_t, int32_t, uint32_t, std::string)> callback); 64 void InitSendReportCallBack(std::function<void(int32_t, int32_t, std::string)> callback); 65 void SendHaReport(int32_t errorCode, uint32_t branchId, const std::string& errorReason, int32_t code = -1); 66 ErrCode OnResponse(const std::shared_ptr<NotificationOperationInfo>& operationInfo, 67 const DistributedDeviceInfo& device); 68 void SendNotifictionRequest(const std::shared_ptr<Notification> request, 69 const DistributedDeviceInfo& peerDevice, bool isSyncNotification = false); 70 void SyncAllLiveViewNotification(const DistributedDeviceInfo peerDevice, bool isForce); 71 void SyncNotifictionList(const DistributedDeviceInfo& peerDevice, const std::vector<std::string>& list); 72 void HandleNotificationSync(const std::shared_ptr<TlvBox>& boxMessage); 73 74 private: 75 void OnHandleMsg(std::shared_ptr<TlvBox>& box); 76 int64_t GetCurrentTime(); 77 void HandleBundleRemoved(const std::string& bundleName); 78 bool GetBundleResourceInfo(const std::string bundleName, std::string& icon); 79 void HandleBundleIconSync(const std::shared_ptr<TlvBox>& boxMessage); 80 void GenerateBundleIconSync(const DistributedDeviceInfo& device); 81 bool CheckPeerDevice(const BundleIconBox& boxMessage, DistributedDeviceInfo& device); 82 void PublishNotifictaion(const std::shared_ptr<TlvBox>& boxMessage); 83 void HandleDeviceState(const std::shared_ptr<TlvBox>& boxMessage); 84 void HandleResponseSync(const std::shared_ptr<TlvBox>& boxMessage); 85 void MakeNotifictaionContent(const NotifticationRequestBox& box, sptr<NotificationRequest>& request, 86 bool isCommonLiveView, int32_t contentType); 87 void MakeNotifictaionIcon(const NotifticationRequestBox& box, sptr<NotificationRequest>& request, 88 bool isCommonLiveView); 89 void SetNotifictaionContent(const NotifticationRequestBox& box, sptr<NotificationRequest>& request, 90 int32_t contentType); 91 void MakeNotifictaionReminderFlag(const NotifticationRequestBox& box, sptr<NotificationRequest>& request); 92 void RemoveNotification(const std::shared_ptr<TlvBox>& boxMessage); 93 void RemoveNotifications(const std::shared_ptr<TlvBox>& boxMessage); 94 void SetNotificationContent(const std::shared_ptr<NotificationContent> &content, 95 NotificationContent::Type type, NotifticationRequestBox &requestBox); 96 void GetNeedUpdateDevice(bool updatedExit, const std::string& bundleName, 97 std::vector<DistributedDeviceInfo>& updateDeviceList); 98 void AbnormalReporting(int result, uint32_t branchId, const std::string& errorReason); 99 void OperationalReporting(int branchId, int32_t slotType); 100 std::string AnonymousProcessing(std::string data); 101 void TriggerJumpApplication(const std::string& hashCode); 102 ErrCode TriggerReplyApplication(const std::string& hashCode, const NotificationResponseBox& responseBox); 103 void HandleOperationResponse(const std::string& hashCode, const NotificationResponseBox& responseBox); 104 void ReplyOperationResponse(const std::string& hashCode, const NotificationResponseBox& responseBox, 105 OperationType operationType, uint32_t result); 106 void SetNotificationButtons(const sptr<NotificationRequest> notificationRequest, 107 NotificationConstant::SlotType slotType, NotifticationRequestBox &requestBox); 108 void MakeNotificationButtons(const NotifticationRequestBox& box, NotificationConstant::SlotType slotType, 109 sptr<NotificationRequest>& request); 110 int32_t GetCurrentActiveUserId(); 111 void TriggerReplyWantAgent(const sptr<NotificationRequest> request, std::string actionName, 112 int32_t errorCode, std::string desc); 113 ErrCode GetNotificationButtonWantPtr(const std::string& hashCode, const std::string& actionName, 114 std::shared_ptr<AAFwk::Want>& wantPtr, sptr<NotificationRequest>& request, std::string& userInputKey); 115 116 private: 117 std::function<void(int32_t, int32_t, uint32_t, std::string)> haCallback_ = nullptr; 118 std::function<void(int32_t, int32_t, std::string)> sendReportCallback_ = nullptr; 119 std::map<std::string, std::set<std::string>> bundleIconCache_; 120 DistributedDeviceInfo localDevice_; 121 std::map<std::string, DistributedDeviceInfo> peerDevice_; 122 std::shared_ptr<ffrt::queue> serviceQueue_ = nullptr; 123 std::map<std::string, std::shared_ptr<DistribuedSubscriber>> subscriberMap_; 124 int32_t code_ = -1; 125 }; 126 } 127 } 128 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_H 129