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 #include "distributed_subscriber.h"
17
18 #include "ans_log_wrapper.h"
19 #include "distributed_service.h"
20 #include "notification_config_parse.h"
21 #include "distributed_preferences.h"
22 #include "distributed_local_config.h"
23
24 namespace OHOS {
25 namespace Notification {
26
~DistribuedSubscriber()27 DistribuedSubscriber::~DistribuedSubscriber()
28 {
29 }
30
OnDied()31 void DistribuedSubscriber::OnDied()
32 {
33 ANS_LOGW("Subscriber on died %{public}d %{public}s %{public}d %{public}s.",
34 peerDevice_.deviceType_, StringAnonymous(peerDevice_.deviceId_).c_str(), localDevice_.deviceType_,
35 StringAnonymous(localDevice_.deviceId_).c_str());
36 }
37
OnConnected()38 void DistribuedSubscriber::OnConnected()
39 {
40 ANS_LOGI("Subscriber on connected %{public}d %{public}s %{public}d %{public}s.",
41 peerDevice_.deviceType_, StringAnonymous(peerDevice_.deviceId_).c_str(), localDevice_.deviceType_,
42 StringAnonymous(localDevice_.deviceId_).c_str());
43 }
44
OnDisconnected()45 void DistribuedSubscriber::OnDisconnected()
46 {
47 ANS_LOGI("Subscriber on disconnected %{public}d %{public}s %{public}d %{public}s.",
48 peerDevice_.deviceType_, StringAnonymous(peerDevice_.deviceId_).c_str(), localDevice_.deviceType_,
49 StringAnonymous(localDevice_.deviceId_).c_str());
50 }
51
OnCanceled(const std::shared_ptr<Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap,int32_t deleteReason)52 void DistribuedSubscriber::OnCanceled(const std::shared_ptr<Notification> &request,
53 const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason)
54 {
55 ANS_LOGI("Subscriber on canceled %{public}d %{public}s %{public}d %{public}s.",
56 peerDevice_.deviceType_, StringAnonymous(peerDevice_.deviceId_).c_str(), localDevice_.deviceType_,
57 StringAnonymous(localDevice_.deviceId_).c_str());
58 if (deleteReason == NotificationConstant::DISTRIBUTED_COLLABORATIVE_DELETE) {
59 ANS_LOGD("is cross device deletion");
60 return;
61 }
62
63 if (CheckNeedCollaboration(request)) {
64 DistributedService::GetInstance().OnCanceled(request, peerDevice_);
65 }
66 }
67
OnConsumed(const std::shared_ptr<Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap)68 void DistribuedSubscriber::OnConsumed(const std::shared_ptr<Notification> &request,
69 const std::shared_ptr<NotificationSortingMap> &sortingMap)
70 {
71 ANS_LOGI("Subscriber on consumed %{public}d %{public}s %{public}d %{public}s.",
72 peerDevice_.deviceType_, StringAnonymous(peerDevice_.deviceId_).c_str(), localDevice_.deviceType_,
73 StringAnonymous(localDevice_.deviceId_).c_str());
74 if (localDevice_.deviceType_ != DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) {
75 ANS_LOGI("No need consumed notification %{public}d %{public}s.",
76 localDevice_.deviceType_, StringAnonymous(localDevice_.deviceId_).c_str());
77 return;
78 }
79 DistributedService::GetInstance().OnConsumed(request, peerDevice_);
80 }
81
OnUpdate(const std::shared_ptr<NotificationSortingMap> & sortingMap)82 void DistribuedSubscriber::OnUpdate(const std::shared_ptr<NotificationSortingMap> &sortingMap)
83 {
84 ANS_LOGI("Subscriber on update.");
85 }
86
OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> & date)87 void DistribuedSubscriber::OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> &date)
88 {
89 }
90
OnEnabledNotificationChanged(const std::shared_ptr<EnabledNotificationCallbackData> & callbackData)91 void DistribuedSubscriber::OnEnabledNotificationChanged(
92 const std::shared_ptr<EnabledNotificationCallbackData> &callbackData)
93 {
94 }
95
OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> & badgeData)96 void DistribuedSubscriber::OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> &badgeData)
97 {
98 }
99
OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> & callbackData)100 void DistribuedSubscriber::OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData)
101 {
102 }
103
OnBatchCanceled(const std::vector<std::shared_ptr<Notification>> & requestList,const std::shared_ptr<NotificationSortingMap> & sortingMap,int32_t deleteReason)104 void DistribuedSubscriber::OnBatchCanceled(const std::vector<std::shared_ptr<Notification>> &requestList,
105 const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason)
106 {
107 ANS_LOGI("Subscriber on batch canceled %{public}d %{public}s %{public}d %{public}s.",
108 peerDevice_.deviceType_, StringAnonymous(peerDevice_.deviceId_).c_str(), localDevice_.deviceType_,
109 StringAnonymous(localDevice_.deviceId_).c_str());
110 if (deleteReason == NotificationConstant::DISTRIBUTED_COLLABORATIVE_DELETE) {
111 ANS_LOGD("is cross device deletion");
112 return;
113 }
114 std::vector<std::shared_ptr<Notification>> notifications;
115 for (auto notification : requestList) {
116 if (CheckNeedCollaboration(notification)) {
117 notifications.push_back(notification);
118 }
119 }
120 if (!notifications.empty()) {
121 DistributedService::GetInstance().OnBatchCanceled(notifications, peerDevice_);
122 }
123 }
124
OnOperationResponse(const std::shared_ptr<NotificationOperationInfo> & operationInfo)125 ErrCode DistribuedSubscriber::OnOperationResponse(const std::shared_ptr<NotificationOperationInfo> &operationInfo)
126 {
127 ANS_LOGI("Subscriber on response %{public}d %{public}s %{public}d %{public}s.",
128 peerDevice_.deviceType_, StringAnonymous(peerDevice_.deviceId_).c_str(), localDevice_.deviceType_,
129 StringAnonymous(localDevice_.deviceId_).c_str());
130 return DistributedService::GetInstance().OnResponse(operationInfo, peerDevice_);
131 }
132
OnApplicationInfoNeedChanged(const std::string & bundleName)133 void DistribuedSubscriber::OnApplicationInfoNeedChanged(const std::string& bundleName)
134 {
135 ANS_LOGI("Notify changed %{public}s %{public}d.", bundleName.c_str(), localDevice_.deviceType_);
136 if (localDevice_.deviceType_ != DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) {
137 return;
138 }
139 DistributedService::GetInstance().HandleBundleChanged(bundleName, false);
140 }
141
SetLocalDevice(DistributedDeviceInfo localDevice)142 void DistribuedSubscriber::SetLocalDevice(DistributedDeviceInfo localDevice)
143 {
144 localDevice_ = localDevice;
145 }
146
SetPeerDevice(DistributedDeviceInfo peerDevice)147 void DistribuedSubscriber::SetPeerDevice(DistributedDeviceInfo peerDevice)
148 {
149 peerDevice_ = peerDevice;
150 }
151
CheckNeedCollaboration(const std::shared_ptr<Notification> & notification)152 bool DistribuedSubscriber::CheckNeedCollaboration(const std::shared_ptr<Notification>& notification)
153 {
154 if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr) {
155 ANS_LOGE("notification or request is nullptr");
156 return false;
157 }
158 if (!CheckCollaborativeRemoveType(notification->GetNotificationRequestPoint()->GetSlotType())) {
159 ANS_LOGE("CheckCollaborativeRemoveType failed");
160 return false;
161 }
162 return true;
163 }
164
CheckCollaborativeRemoveType(const NotificationConstant::SlotType & slotType)165 bool DistribuedSubscriber::CheckCollaborativeRemoveType(const NotificationConstant::SlotType& slotType)
166 {
167 std::string type;
168 switch (slotType) {
169 case NotificationConstant::SlotType::SOCIAL_COMMUNICATION:
170 type = "SOCIAL_COMMUNICATION";
171 break;
172 case NotificationConstant::SlotType::SERVICE_REMINDER:
173 type = "SERVICE_REMINDER";
174 break;
175 case NotificationConstant::SlotType::CONTENT_INFORMATION:
176 type = "CONTENT_INFORMATION";
177 break;
178 case NotificationConstant::SlotType::OTHER:
179 type = "OTHER";
180 break;
181 case NotificationConstant::SlotType::CUSTOM:
182 type = "CUSTOM";
183 break;
184 case NotificationConstant::SlotType::LIVE_VIEW:
185 type = "LIVE_VIEW";
186 break;
187 case NotificationConstant::SlotType::CUSTOMER_SERVICE:
188 type = "CUSTOMER_SERVICE";
189 break;
190 case NotificationConstant::SlotType::EMERGENCY_INFORMATION:
191 type = "EMERGENCY_INFORMATION";
192 break;
193 case NotificationConstant::SlotType::ILLEGAL_TYPE:
194 type = "ILLEGAL_TYPE";
195 break;
196 default:
197 return false;
198 }
199 auto collaborativeDeleteTypes = DistributedLocalConfig::GetInstance().GetCollaborativeDeleteTypes();
200 return collaborativeDeleteTypes.find(type) != collaborativeDeleteTypes.end();
201 }
202
203 }
204 }
205