• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
25 namespace OHOS {
26 namespace Notification {
27 class NotificationSubscriber {
28 public:
29     NotificationSubscriber();
30 
31     virtual ~NotificationSubscriber();
32 
33     /**
34      * @brief Called back when a notification is canceled.
35      *
36      * @param request Indicates the canceled Notification object.
37      * @param sortingMap Indicates the sorting map used by the current subscriber
38      * to obtain notification ranking information.
39      * @param deleteReason Indicates the reason for the deletion. For details, see NotificationConstant.
40      **/
41     virtual void OnCanceled(const std::shared_ptr<Notification> &request,
42         const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) = 0;
43 
44     /**
45      * @brief Called back when the subscriber is connected to the Advanced Notification Service (ANS).
46      **/
47     virtual void OnConnected() = 0;
48 
49     /**
50      * @brief Called back when the subscriber receives a new notification.
51      *
52      * @param request Indicates the received Notification object.
53      * @param sortingMap Indicates the sorting map used by the current subscriber to obtain
54      * notification ranking information.
55      **/
56     virtual void OnConsumed(
57         const std::shared_ptr<Notification> &request, const std::shared_ptr<NotificationSortingMap> &sortingMap) = 0;
58 
59     /**
60      * @brief Called back when the subscriber is disconnected from the ANS.
61      **/
62     virtual void OnDisconnected() = 0;
63 
64     /**
65      * @brief Called back when the ranking information about the current notification changes.
66      *
67      * @param sortingMap Indicates the sorting map used to obtain notification ranking information.
68      **/
69     virtual void OnUpdate(const std::shared_ptr<NotificationSortingMap> &sortingMap) = 0;
70 
71     /**
72      * @brief Called back when connection to the ANS has died.
73      **/
74     virtual void OnDied() = 0;
75 
76     /**
77      * @brief Called when the Do Not Disturb date changes.
78      *
79      * @param date Indicates the current Do Not Disturb date.
80      **/
81     virtual void OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> &date) = 0;
82 
83     /**
84      * @brief Called when the notification permission changes.
85      *
86      * @param callbackData Indicates the properties of the application that notification permission has changed.
87      **/
88     virtual void OnEnabledNotificationChanged(const std::shared_ptr<EnabledNotificationCallbackData> &callbackData) = 0;
89 
90     /**
91      * @brief The callback function on the badge number changed.
92      *
93      * @param badgeData Indicates the BadgeNumberCallbackData object.
94      */
95     virtual void OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> &badgeData) = 0;
96 
97     /**
98      * @brief The callback function on the badge number changed.
99      *
100      * @param badgeData Indicates the BadgeNumberCallbackData object.
101      */
102     virtual void OnBatchCanceled(const std::vector<std::shared_ptr<Notification>> &requestList,
103         const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) = 0;
104 
HasOnBatchCancelCallback()105     virtual bool HasOnBatchCancelCallback()
106     {
107         return false;
108     }
109 
110 private:
111     class SubscriberImpl final : public AnsSubscriberStub {
112     public:
113         class DeathRecipient final : public IRemoteObject::DeathRecipient {
114         public:
115             DeathRecipient(SubscriberImpl &subscriberImpl);
116 
117             ~DeathRecipient();
118 
119             void OnRemoteDied(const wptr<IRemoteObject> &object) override;
120 
121         private:
122             SubscriberImpl &subscriberImpl_;
123         };
124 
125     public:
126         SubscriberImpl(NotificationSubscriber &subscriber);
~SubscriberImpl()127         ~SubscriberImpl() {};
128 
129         void OnConnected() override;
130 
131         void OnDisconnected() override;
132 
133         void OnConsumed(
134             const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap) override;
135 
136         void OnConsumedList(const std::vector<sptr<Notification>> &notifications,
137             const sptr<NotificationSortingMap> &notificationMap) override;
138 
139         void OnCanceled(const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap,
140             int32_t deleteReason) override;
141 
142         void OnCanceledList(const std::vector<sptr<Notification>> &notifications,
143             const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason) override;
144 
145         void OnBatchCanceled(const std::vector<sptr<Notification>> &notifications,
146             const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason);
147 
148         void OnUpdated(const sptr<NotificationSortingMap> &notificationMap) override;
149 
150         void OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date) override;
151 
152         void OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override;
153 
154         void OnBadgeChanged(const sptr<BadgeNumberCallbackData> &badgeData) override;
155 
156         bool GetAnsManagerProxy();
157 
158     public:
159         NotificationSubscriber &subscriber_;
160         sptr<DeathRecipient> recipient_ {nullptr};
161         sptr<AnsManagerInterface> proxy_ {nullptr};
162         std::mutex mutex_ {};
163     };
164 
165 private:
166     const sptr<SubscriberImpl> GetImpl() const;
167 
168 private:
169     sptr<SubscriberImpl> impl_ = nullptr;
170 
171     friend class AnsNotification;
172 };
173 }  // namespace Notification
174 }  // namespace OHOS
175 
176 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_SUBSCRIBER_H
177