1 /*
2 * Copyright (c) 2021-2025 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 "ans_subscriber_listener.h"
17
18 #include "ans_trace_wrapper.h"
19 #include "notification_constant.h"
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22
23 namespace OHOS {
24 namespace Notification {
SubscriberListener(const std::shared_ptr<NotificationSubscriber> & subscriber)25 SubscriberListener::SubscriberListener(const std::shared_ptr<NotificationSubscriber> &subscriber)
26 : subscriber_(subscriber)
27 {};
28
~SubscriberListener()29 SubscriberListener::~SubscriberListener()
30 {}
31
OnConnected()32 ErrCode SubscriberListener::OnConnected()
33 {
34 NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
35 auto subscriber = subscriber_.lock();
36 if (subscriber == nullptr) {
37 ANS_LOGE("null subscriber");
38 return ERR_INVALID_DATA;
39 }
40 subscriber->OnConnected();
41 return ERR_OK;
42 }
43
OnDisconnected()44 ErrCode SubscriberListener::OnDisconnected()
45 {
46 NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
47 auto subscriber = subscriber_.lock();
48 if (subscriber == nullptr) {
49 ANS_LOGE("null subscriber");
50 return ERR_INVALID_DATA;
51 }
52 subscriber->OnDisconnected();
53 return ERR_OK;
54 }
55
OnConsumed(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap)56 ErrCode SubscriberListener::OnConsumed(
57 const sptr<Notification> ¬ification, const sptr<NotificationSortingMap> ¬ificationMap)
58 {
59 NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
60 auto subscriber = subscriber_.lock();
61 if (subscriber == nullptr) {
62 ANS_LOGE("null subscriber");
63 return ERR_INVALID_DATA;
64 }
65 if (notificationMap == nullptr) {
66 ANS_LOGE("null notificationMap");
67 return ERR_INVALID_DATA;
68 }
69 std::shared_ptr<Notification> sharedNotification = std::make_shared<Notification>(*notification);
70 auto deviceType = subscriber->GetDeviceType();
71 if (subscriber->SyncLiveViewVoip(deviceType, sharedNotification)) {
72 ANS_LOGI("Sync LIVE_VIEW VOIP.");
73 }
74 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
75 else if (!subscriber->ProcessSyncDecision(deviceType, sharedNotification)) {
76 return ERR_INVALID_OPERATION;
77 }
78 #endif
79
80 subscriber->OnConsumed(
81 sharedNotification, std::make_shared<NotificationSortingMap>(*notificationMap));
82 return ERR_OK;
83 }
84
OnConsumed(const sptr<Notification> & notification)85 ErrCode SubscriberListener::OnConsumed(const sptr<Notification> ¬ification)
86 {
87 return OnConsumed(notification, nullptr);
88 }
89
OnConsumedWithMaxCapacity(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap)90 ErrCode SubscriberListener::OnConsumedWithMaxCapacity(
91 const sptr<Notification> ¬ification, const sptr<NotificationSortingMap> ¬ificationMap)
92 {
93 return OnConsumed(notification, notificationMap);
94 }
95
OnConsumedWithMaxCapacity(const sptr<Notification> & notification)96 ErrCode SubscriberListener::OnConsumedWithMaxCapacity(const sptr<Notification> ¬ification)
97 {
98 return OnConsumed(notification, nullptr);
99 }
100
OnConsumedList(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap)101 ErrCode SubscriberListener::OnConsumedList(const std::vector<sptr<Notification>> ¬ifications,
102 const sptr<NotificationSortingMap> ¬ificationMap)
103 {
104 NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
105 for (auto notification : notifications) {
106 OnConsumed(notification, notificationMap);
107 }
108 return ERR_OK;
109 }
110
OnConsumedList(const std::vector<sptr<Notification>> & notifications)111 ErrCode SubscriberListener::OnConsumedList(const std::vector<sptr<Notification>> ¬ifications)
112 {
113 return OnConsumedList(notifications, nullptr);
114 }
115
OnCanceled(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)116 ErrCode SubscriberListener::OnCanceled(
117 const sptr<Notification> ¬ification, const sptr<NotificationSortingMap> ¬ificationMap, int32_t deleteReason)
118 {
119 NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
120 auto subscriber = subscriber_.lock();
121 if (subscriber == nullptr) {
122 ANS_LOGE("null subscriber");
123 return ERR_INVALID_DATA;
124 }
125 if (notificationMap == nullptr) {
126 subscriber->OnCanceled(std::make_shared<Notification>(*notification),
127 std::make_shared<NotificationSortingMap>(), deleteReason);
128 } else {
129 subscriber->OnCanceled(std::make_shared<Notification>(*notification),
130 std::make_shared<NotificationSortingMap>(*notificationMap), deleteReason);
131 }
132 return ERR_OK;
133 }
134
OnCanceled(const sptr<Notification> & notification,int32_t deleteReason)135 ErrCode SubscriberListener::OnCanceled(const sptr<Notification> ¬ification, int32_t deleteReason)
136 {
137 return OnCanceled(notification, nullptr, deleteReason);
138 }
139
OnCanceledWithMaxCapacity(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)140 ErrCode SubscriberListener::OnCanceledWithMaxCapacity(
141 const sptr<Notification> ¬ification, const sptr<NotificationSortingMap> ¬ificationMap, int32_t deleteReason)
142 {
143 return OnCanceled(notification, notificationMap, deleteReason);
144 }
145
OnCanceledWithMaxCapacity(const sptr<Notification> & notification,int32_t deleteReason)146 ErrCode SubscriberListener::OnCanceledWithMaxCapacity(const sptr<Notification> ¬ification, int32_t deleteReason)
147 {
148 return OnCanceled(notification, nullptr, deleteReason);
149 }
150
OnBatchCanceled(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)151 void SubscriberListener::OnBatchCanceled(const std::vector<sptr<Notification>> ¬ifications,
152 const sptr<NotificationSortingMap> ¬ificationMap, int32_t deleteReason)
153 {
154 auto subscriber = subscriber_.lock();
155 if (subscriber == nullptr) {
156 ANS_LOGE("null subscriber");
157 return;
158 }
159 std::vector<std::shared_ptr<Notification>> notificationList;
160 for (auto notification : notifications) {
161 notificationList.emplace_back(std::make_shared<Notification>(*notification));
162 }
163 if (notificationMap == nullptr) {
164 subscriber->OnBatchCanceled(notificationList,
165 std::make_shared<NotificationSortingMap>(), deleteReason);
166 } else {
167 subscriber->OnBatchCanceled(notificationList,
168 std::make_shared<NotificationSortingMap>(*notificationMap), deleteReason);
169 }
170 }
171
OnCanceledList(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)172 ErrCode SubscriberListener::OnCanceledList(const std::vector<sptr<Notification>> ¬ifications,
173 const sptr<NotificationSortingMap> ¬ificationMap, int32_t deleteReason)
174 {
175 NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
176 auto subscriber = subscriber_.lock();
177 if (subscriber == nullptr) {
178 ANS_LOGE("null subscriber");
179 return ERR_INVALID_DATA;
180 }
181 if (subscriber->HasOnBatchCancelCallback()) {
182 OnBatchCanceled(notifications, notificationMap, deleteReason);
183 return ERR_INVALID_DATA;
184 }
185 for (auto notification : notifications) {
186 OnCanceled(notification, notificationMap, deleteReason);
187 }
188 return ERR_OK;
189 }
190
OnCanceledList(const std::vector<sptr<Notification>> & notifications,int32_t deleteReason)191 ErrCode SubscriberListener::OnCanceledList(
192 const std::vector<sptr<Notification>> ¬ifications, int32_t deleteReason)
193 {
194 return OnCanceledList(notifications, nullptr, deleteReason);
195 }
196
OnUpdated(const sptr<NotificationSortingMap> & notificationMap)197 ErrCode SubscriberListener::OnUpdated(const sptr<NotificationSortingMap> ¬ificationMap)
198 {
199 auto subscriber = subscriber_.lock();
200 if (subscriber == nullptr) {
201 ANS_LOGE("null subscriber");
202 return ERR_INVALID_DATA;
203 }
204 subscriber->OnUpdate(std::make_shared<NotificationSortingMap>(*notificationMap));
205 return ERR_OK;
206 }
207
OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> & date)208 ErrCode SubscriberListener::OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date)
209 {
210 auto subscriber = subscriber_.lock();
211 if (subscriber == nullptr) {
212 ANS_LOGE("null subscriber");
213 return ERR_INVALID_DATA;
214 }
215 subscriber->OnDoNotDisturbDateChange(std::make_shared<NotificationDoNotDisturbDate>(*date));
216 return ERR_OK;
217 }
218
OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> & callbackData)219 ErrCode SubscriberListener::OnEnabledNotificationChanged(
220 const sptr<EnabledNotificationCallbackData> &callbackData)
221 {
222 NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
223 auto subscriber = subscriber_.lock();
224 if (subscriber == nullptr) {
225 ANS_LOGE("null subscriber");
226 return ERR_INVALID_DATA;
227 }
228 subscriber->OnEnabledNotificationChanged(std::make_shared<EnabledNotificationCallbackData>(*callbackData));
229 return ERR_OK;
230 }
231
OnBadgeChanged(const sptr<BadgeNumberCallbackData> & badgeData)232 ErrCode SubscriberListener::OnBadgeChanged(const sptr<BadgeNumberCallbackData> &badgeData)
233 {
234 NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
235 auto subscriber = subscriber_.lock();
236 if (subscriber == nullptr) {
237 ANS_LOGE("null subscriber");
238 return ERR_INVALID_DATA;
239 }
240 subscriber->OnBadgeChanged(std::make_shared<BadgeNumberCallbackData>(*badgeData));
241 return ERR_OK;
242 }
243
OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> & callbackData)244 ErrCode SubscriberListener::OnBadgeEnabledChanged(
245 const sptr<EnabledNotificationCallbackData> &callbackData)
246 {
247 NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
248 auto subscriber = subscriber_.lock();
249 if (subscriber == nullptr) {
250 ANS_LOGE("null subscriber");
251 return ERR_INVALID_DATA;
252 }
253 subscriber->OnBadgeEnabledChanged(callbackData);
254 return ERR_OK;
255 }
256
OnApplicationInfoNeedChanged(const std::string & bundleName)257 ErrCode SubscriberListener::OnApplicationInfoNeedChanged(const std::string& bundleName)
258 {
259 NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
260 ANS_LOGD("called");
261 auto subscriber = subscriber_.lock();
262 if (subscriber == nullptr) {
263 ANS_LOGE("null subscriber");
264 return ERR_INVALID_DATA;
265 }
266 subscriber->OnApplicationInfoNeedChanged(bundleName);
267 return ERR_OK;
268 }
269
OnOperationResponse(const sptr<NotificationOperationInfo> & operationInfo,int32_t & funcResult)270 ErrCode SubscriberListener::OnOperationResponse(
271 const sptr<NotificationOperationInfo>& operationInfo, int32_t& funcResult)
272 {
273 NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
274 auto subscriber = subscriber_.lock();
275 if (subscriber == nullptr) {
276 ANS_LOGW("null subscriber");
277 return ERR_OK;
278 }
279 std::shared_ptr<NotificationOperationInfo> sharedNotification =
280 std::make_shared<NotificationOperationInfo>(*operationInfo);
281 funcResult = subscriber->OnOperationResponse(sharedNotification);
282 return funcResult;
283 }
284 } // namespace Notification
285 } // namespace OHOS
286