• 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 private:
98     class SubscriberImpl final : public AnsSubscriberStub {
99     public:
100         class DeathRecipient final : public IRemoteObject::DeathRecipient {
101         public:
102             DeathRecipient(SubscriberImpl &subscriberImpl);
103 
104             ~DeathRecipient();
105 
106             void OnRemoteDied(const wptr<IRemoteObject> &object) override;
107 
108         private:
109             SubscriberImpl &subscriberImpl_;
110         };
111 
112     public:
113         SubscriberImpl(NotificationSubscriber &subscriber);
~SubscriberImpl()114         ~SubscriberImpl() {};
115 
116         void OnConnected() override;
117 
118         void OnDisconnected() override;
119 
120         void OnConsumed(
121             const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap) override;
122 
123         void OnCanceled(const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap,
124             int32_t deleteReason) override;
125 
126         void OnCanceledList(const std::vector<sptr<Notification>> &notifications,
127             const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason) override;
128 
129         void OnUpdated(const sptr<NotificationSortingMap> &notificationMap) override;
130 
131         void OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date) override;
132 
133         void OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override;
134 
135         void OnBadgeChanged(const sptr<BadgeNumberCallbackData> &badgeData) override;
136 
137         bool GetAnsManagerProxy();
138 
139     public:
140         NotificationSubscriber &subscriber_;
141         sptr<DeathRecipient> recipient_ {nullptr};
142         sptr<AnsManagerInterface> proxy_ {nullptr};
143         std::mutex mutex_ {};
144     };
145 
146 private:
147     const sptr<SubscriberImpl> GetImpl() const;
148 
149 private:
150     sptr<SubscriberImpl> impl_ = nullptr;
151 
152     friend class AnsNotification;
153 };
154 }  // namespace Notification
155 }  // namespace OHOS
156 
157 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_SUBSCRIBER_H