1 /*
2  * Copyright (c) 2021-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 "ans_subscriber_listener.h"
17 #include "notification_constant.h"
18 #include "hitrace_meter_adapter.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21 
22 namespace OHOS {
23 namespace Notification {
SubscriberListener(const std::shared_ptr<NotificationSubscriber> & subscriber)24 SubscriberListener::SubscriberListener(const std::shared_ptr<NotificationSubscriber> &subscriber)
25     : subscriber_(subscriber)
26 {};
27 
~SubscriberListener()28 SubscriberListener::~SubscriberListener()
29 {}
30 
OnConnected()31 void SubscriberListener::OnConnected()
32 {
33     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
34     auto subscriber = subscriber_.lock();
35     if (subscriber == nullptr) {
36         ANS_LOGW("Subscriber is nullptr");
37         return;
38     }
39     subscriber->OnConnected();
40 }
41 
OnDisconnected()42 void SubscriberListener::OnDisconnected()
43 {
44     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
45     auto subscriber = subscriber_.lock();
46     if (subscriber == nullptr) {
47         ANS_LOGW("Subscriber is nullptr");
48         return;
49     }
50     subscriber->OnDisconnected();
51 }
52 
OnConsumed(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap)53 void SubscriberListener::OnConsumed(
54     const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap)
55 {
56     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
57     auto subscriber = subscriber_.lock();
58     if (subscriber == nullptr) {
59         ANS_LOGW("Subscriber is nullptr");
60         return;
61     }
62 
63     std::shared_ptr<Notification> sharedNotification = std::make_shared<Notification>(*notification);
64 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
65     if (!subscriber->ProcessSyncDecision(subscriber->GetDeviceType(), sharedNotification)) {
66         return;
67     }
68 #endif
69 
70     subscriber->OnConsumed(
71         sharedNotification, std::make_shared<NotificationSortingMap>(*notificationMap));
72 }
73 
OnConsumedList(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap)74 void SubscriberListener::OnConsumedList(const std::vector<sptr<Notification>> &notifications,
75     const sptr<NotificationSortingMap> &notificationMap)
76 {
77     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
78     for (auto notification : notifications) {
79         OnConsumed(notification, notificationMap);
80     }
81 }
82 
OnCanceled(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)83 void SubscriberListener::OnCanceled(
84     const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason)
85 {
86     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
87     auto subscriber = subscriber_.lock();
88     if (subscriber == nullptr) {
89         ANS_LOGW("Subscriber is nullptr");
90         return;
91     }
92     if (notificationMap == nullptr) {
93         subscriber->OnCanceled(std::make_shared<Notification>(*notification),
94             std::make_shared<NotificationSortingMap>(), deleteReason);
95     } else {
96         subscriber->OnCanceled(std::make_shared<Notification>(*notification),
97             std::make_shared<NotificationSortingMap>(*notificationMap), deleteReason);
98     }
99 }
100 
OnBatchCanceled(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)101 void SubscriberListener::OnBatchCanceled(const std::vector<sptr<Notification>> &notifications,
102     const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason)
103 {
104     auto subscriber = subscriber_.lock();
105     if (subscriber == nullptr) {
106         ANS_LOGW("Subscriber is nullptr");
107         return;
108     }
109     std::vector<std::shared_ptr<Notification>> notificationList;
110     for (auto notification : notifications) {
111         notificationList.emplace_back(std::make_shared<Notification>(*notification));
112     }
113     if (notificationMap == nullptr) {
114         subscriber->OnBatchCanceled(notificationList,
115             std::make_shared<NotificationSortingMap>(), deleteReason);
116     } else {
117         subscriber->OnBatchCanceled(notificationList,
118             std::make_shared<NotificationSortingMap>(*notificationMap), deleteReason);
119     }
120 }
121 
OnCanceledList(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)122 void SubscriberListener::OnCanceledList(const std::vector<sptr<Notification>> &notifications,
123     const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason)
124 {
125     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
126     auto subscriber = subscriber_.lock();
127     if (subscriber == nullptr) {
128         ANS_LOGW("Subscriber is nullptr");
129         return;
130     }
131     if (subscriber->HasOnBatchCancelCallback()) {
132         OnBatchCanceled(notifications, notificationMap, deleteReason);
133         return;
134     }
135     for (auto notification : notifications) {
136         OnCanceled(notification, notificationMap, deleteReason);
137     }
138 }
139 
OnUpdated(const sptr<NotificationSortingMap> & notificationMap)140 void SubscriberListener::OnUpdated(const sptr<NotificationSortingMap> &notificationMap)
141 {
142     auto subscriber = subscriber_.lock();
143     if (subscriber == nullptr) {
144         ANS_LOGW("Subscriber is nullptr");
145         return;
146     }
147     subscriber->OnUpdate(std::make_shared<NotificationSortingMap>(*notificationMap));
148 }
149 
OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> & date)150 void SubscriberListener::OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date)
151 {
152     auto subscriber = subscriber_.lock();
153     if (subscriber == nullptr) {
154         ANS_LOGW("Subscriber is nullptr");
155         return;
156     }
157     subscriber->OnDoNotDisturbDateChange(std::make_shared<NotificationDoNotDisturbDate>(*date));
158 }
159 
OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> & callbackData)160 void SubscriberListener::OnEnabledNotificationChanged(
161     const sptr<EnabledNotificationCallbackData> &callbackData)
162 {
163     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
164     auto subscriber = subscriber_.lock();
165     if (subscriber == nullptr) {
166         ANS_LOGW("Subscriber is nullptr");
167         return;
168     }
169     subscriber->OnEnabledNotificationChanged(std::make_shared<EnabledNotificationCallbackData>(*callbackData));
170 }
171 
OnBadgeChanged(const sptr<BadgeNumberCallbackData> & badgeData)172 void SubscriberListener::OnBadgeChanged(const sptr<BadgeNumberCallbackData> &badgeData)
173 {
174     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
175     auto subscriber = subscriber_.lock();
176     if (subscriber == nullptr) {
177         ANS_LOGW("Subscriber is nullptr");
178         return;
179     }
180     subscriber->OnBadgeChanged(std::make_shared<BadgeNumberCallbackData>(*badgeData));
181 }
182 
OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> & callbackData)183 void SubscriberListener::OnBadgeEnabledChanged(
184     const sptr<EnabledNotificationCallbackData> &callbackData)
185 {
186     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
187     auto subscriber = subscriber_.lock();
188     if (subscriber == nullptr) {
189         ANS_LOGW("Subscriber is nullptr");
190         return;
191     }
192     subscriber->OnBadgeEnabledChanged(callbackData);
193 }
194 }  // namespace Notification
195 }  // namespace OHOS
196