• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_SUBSCRIBER_H
17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_SUBSCRIBER_H
18 
19 #include "ians_manager.h"
20 #include "ans_subscriber_stub.h"
21 #include "notification_request.h"
22 #include "notification_sorting.h"
23 #include "notification_sorting_map.h"
24 #include "notification_operation_info.h"
25 
26 namespace OHOS {
27 namespace Notification {
28 class NotificationSubscriber : public std::enable_shared_from_this<NotificationSubscriber> {
29 public:
30     NotificationSubscriber();
31 
32     virtual ~NotificationSubscriber();
33 
34     /**
35      * @brief Called back when a notification is canceled.
36      *
37      * @param request Indicates the canceled Notification object.
38      * @param sortingMap Indicates the sorting map used by the current subscriber
39      * to obtain notification ranking information.
40      * @param deleteReason Indicates the reason for the deletion. For details, see NotificationConstant.
41      **/
42     virtual void OnCanceled(const std::shared_ptr<Notification> &request,
43         const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) = 0;
44 
45     /**
46      * @brief Called back when the subscriber is connected to the Advanced Notification Service (ANS).
47      **/
48     virtual void OnConnected() = 0;
49 
50     /**
51      * @brief Called back when the subscriber receives a new notification.
52      *
53      * @param request Indicates the received Notification object.
54      * @param sortingMap Indicates the sorting map used by the current subscriber to obtain
55      * notification ranking information.
56      **/
57     virtual void OnConsumed(
58         const std::shared_ptr<Notification> &request, const std::shared_ptr<NotificationSortingMap> &sortingMap) = 0;
59 
60     /**
61      * @brief Called back when the subscriber is disconnected from the ANS.
62      **/
63     virtual void OnDisconnected() = 0;
64 
65     /**
66      * @brief Called back when the ranking information about the current notification changes.
67      *
68      * @param sortingMap Indicates the sorting map used to obtain notification ranking information.
69      **/
70     virtual void OnUpdate(const std::shared_ptr<NotificationSortingMap> &sortingMap) = 0;
71 
72     /**
73      * @brief Called back when connection to the ANS has died.
74      **/
75     virtual void OnDied() = 0;
76 
77     /**
78      * @brief Called when the Do Not Disturb date changes.
79      *
80      * @param date Indicates the current Do Not Disturb date.
81      **/
82     virtual void OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> &date) = 0;
83 
84     /**
85      * @brief Called when the notification permission changes.
86      *
87      * @param callbackData Indicates the properties of the application that notification permission has changed.
88      **/
89     virtual void OnEnabledNotificationChanged(const std::shared_ptr<EnabledNotificationCallbackData> &callbackData) = 0;
90 
91     /**
92      * @brief The callback function on the badge number changed.
93      *
94      * @param badgeData Indicates the BadgeNumberCallbackData object.
95      */
96     virtual void OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> &badgeData) = 0;
97 
98     /**
99      * @brief The callback function on the badge enabled state changed.
100      *
101      * @param callbackData Indicates the properties of the application that badge enabled state has changed.
102      */
103     virtual void OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData) = 0;
104 
105     /**
106      * @brief The callback function on the badge number changed.
107      *
108      * @param badgeData Indicates the BadgeNumberCallbackData object.
109      */
110     virtual void OnBatchCanceled(const std::vector<std::shared_ptr<Notification>> &requestList,
111         const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) = 0;
112 
113     /**
114      * @brief The callback function on the response.
115      *
116      * @param notification Indicates the received Notification object.
117      */
OnOperationResponse(const std::shared_ptr<NotificationOperationInfo> & operationInfo)118     virtual ErrCode OnOperationResponse(const std::shared_ptr<NotificationOperationInfo> &operationInfo)
119     {
120         return 0;
121     }
122 
HasOnBatchCancelCallback()123     virtual bool HasOnBatchCancelCallback()
124     {
125         return false;
126     }
127 
OnApplicationInfoNeedChanged(const std::string & bundleName)128     virtual void OnApplicationInfoNeedChanged(const std::string& bundleName)
129     {
130     }
131 
132     void SetDeviceType(const std::string &deviceType);
133 
134     std::string GetDeviceType() const;
135 
136     bool SyncLiveViewVoip(const std::string &deviceType, std::shared_ptr<Notification> &notification) const;
137 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
138     bool ProcessSyncDecision(const std::string &deviceType, std::shared_ptr<Notification> &notification) const;
139 #endif
140 
141 private:
142     class SubscriberImpl final : public AnsSubscriberStub {
143     public:
144         class DeathRecipient final : public IRemoteObject::DeathRecipient {
145         public:
146             DeathRecipient(SubscriberImpl &subscriberImpl);
147 
148             ~DeathRecipient();
149 
150             void OnRemoteDied(const wptr<IRemoteObject> &object) override;
151 
152         private:
153             SubscriberImpl &subscriberImpl_;
154         };
155 
156     public:
157         SubscriberImpl(NotificationSubscriber &subscriber);
~SubscriberImpl()158         ~SubscriberImpl() {};
159 
160         ErrCode OnConnected() override;
161 
162         ErrCode OnDisconnected() override;
163 
164         ErrCode OnConsumed(
165             const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap) override;
166 
167         ErrCode OnConsumed(const sptr<Notification> &notification) override;
168 
169         ErrCode OnConsumedWithMaxCapacity(
170             const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap) override;
171 
172         ErrCode OnConsumedWithMaxCapacity(const sptr<Notification> &notification) override;
173 
174         ErrCode OnConsumedList(const std::vector<sptr<Notification>> &notifications,
175             const sptr<NotificationSortingMap> &notificationMap) override;
176 
177         ErrCode OnConsumedList(const std::vector<sptr<Notification>> &notifications) override;
178 
179         ErrCode OnCanceled(const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap,
180             int32_t deleteReason) override;
181 
182         ErrCode OnCanceled(const sptr<Notification> &notification, int32_t deleteReason) override;
183 
184         ErrCode OnCanceledWithMaxCapacity(const sptr<Notification> &notification,
185             const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason) override;
186 
187         ErrCode OnCanceledWithMaxCapacity(const sptr<Notification> &notification, int32_t deleteReason) override;
188 
189         ErrCode OnCanceledList(const std::vector<sptr<Notification>> &notifications,
190             const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason) override;
191 
192         ErrCode OnCanceledList(const std::vector<sptr<Notification>> &notifications, int32_t deleteReason) override;
193 
194         void OnBatchCanceled(const std::vector<sptr<Notification>> &notifications,
195             const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason);
196 
197         ErrCode OnUpdated(const sptr<NotificationSortingMap> &notificationMap) override;
198 
199         ErrCode OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date) override;
200 
201         ErrCode OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override;
202 
203         ErrCode OnBadgeChanged(const sptr<BadgeNumberCallbackData> &badgeData) override;
204 
205         ErrCode OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override;
206 
207         ErrCode OnApplicationInfoNeedChanged(const std::string& bundleName) override;
208 
209         ErrCode OnOperationResponse(const sptr<NotificationOperationInfo> &operationInfo, int32_t& funcResult) override;
210 
211         sptr<IAnsManager> GetAnsManagerProxy();
212 
213     public:
214         NotificationSubscriber &subscriber_;
215         sptr<DeathRecipient> recipient_ {nullptr};
216     };
217 
218 private:
219     const sptr<SubscriberImpl> GetImpl() const;
220 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
221     NotificationConstant::FlagStatus DowngradeReminder(
222         const NotificationConstant::FlagStatus &oldFlags, const NotificationConstant::FlagStatus &judgeFlags) const;
223 #endif
224 
225 private:
226     sptr<SubscriberImpl> impl_ = nullptr;
227     std::string deviceType_;
228 
229     friend class AnsNotification;
230 };
231 }  // namespace Notification
232 }  // namespace OHOS
233 
234 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_SUBSCRIBER_H
235