• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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 "ans_manager_interface.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 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
137     bool ProcessSyncDecision(const std::string &deviceType, std::shared_ptr<Notification> &notification) const;
138 #endif
139 
140 private:
141     class SubscriberImpl final : public AnsSubscriberStub {
142     public:
143         class DeathRecipient final : public IRemoteObject::DeathRecipient {
144         public:
145             DeathRecipient(SubscriberImpl &subscriberImpl);
146 
147             ~DeathRecipient();
148 
149             void OnRemoteDied(const wptr<IRemoteObject> &object) override;
150 
151         private:
152             SubscriberImpl &subscriberImpl_;
153         };
154 
155     public:
156         SubscriberImpl(NotificationSubscriber &subscriber);
~SubscriberImpl()157         ~SubscriberImpl() {};
158 
159         void OnConnected() override;
160 
161         void OnDisconnected() override;
162 
163         void OnConsumed(
164             const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap) override;
165 
166         void OnConsumedList(const std::vector<sptr<Notification>> &notifications,
167             const sptr<NotificationSortingMap> &notificationMap) override;
168 
169         void OnCanceled(const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap,
170             int32_t deleteReason) override;
171 
172         void OnCanceledList(const std::vector<sptr<Notification>> &notifications,
173             const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason) override;
174 
175         void OnBatchCanceled(const std::vector<sptr<Notification>> &notifications,
176             const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason);
177 
178         void OnUpdated(const sptr<NotificationSortingMap> &notificationMap) override;
179 
180         void OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date) override;
181 
182         void OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override;
183 
184         void OnBadgeChanged(const sptr<BadgeNumberCallbackData> &badgeData) override;
185 
186         void OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override;
187 
188         void OnApplicationInfoNeedChanged(const std::string& bundleName) override;
189 
190         ErrCode OnOperationResponse(const sptr<NotificationOperationInfo> &operationInfo) override;
191 
192         sptr<AnsManagerInterface> GetAnsManagerProxy();
193 
194     public:
195         NotificationSubscriber &subscriber_;
196         sptr<DeathRecipient> recipient_ {nullptr};
197     };
198 
199 private:
200     const sptr<SubscriberImpl> GetImpl() const;
201 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
202     NotificationConstant::FlagStatus DowngradeReminder(
203         const NotificationConstant::FlagStatus &oldFlags, const NotificationConstant::FlagStatus &judgeFlags) const;
204 #endif
205 
206 private:
207     sptr<SubscriberImpl> impl_ = nullptr;
208     std::string deviceType_;
209 
210     friend class AnsNotification;
211 };
212 }  // namespace Notification
213 }  // namespace OHOS
214 
215 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_SUBSCRIBER_H
216