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 
25 namespace OHOS {
26 namespace Notification {
27 class NotificationSubscriber : public std::enable_shared_from_this<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 enabled state changed.
99      *
100      * @param callbackData Indicates the properties of the application that badge enabled state has changed.
101      */
102     virtual void OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData) = 0;
103 
104     /**
105      * @brief The callback function on the badge number changed.
106      *
107      * @param badgeData Indicates the BadgeNumberCallbackData object.
108      */
109     virtual void OnBatchCanceled(const std::vector<std::shared_ptr<Notification>> &requestList,
110         const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) = 0;
111 
HasOnBatchCancelCallback()112     virtual bool HasOnBatchCancelCallback()
113     {
114         return false;
115     }
116 
117     void SetDeviceType(const std::string &deviceType);
118 
119     std::string GetDeviceType() const;
120 
121 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
122     bool ProcessSyncDecision(const std::string &deviceType, std::shared_ptr<Notification> &notification) const;
123 #endif
124 
125 private:
126     class SubscriberImpl final : public AnsSubscriberStub {
127     public:
128         class DeathRecipient final : public IRemoteObject::DeathRecipient {
129         public:
130             DeathRecipient(SubscriberImpl &subscriberImpl);
131 
132             ~DeathRecipient();
133 
134             void OnRemoteDied(const wptr<IRemoteObject> &object) override;
135 
136         private:
137             SubscriberImpl &subscriberImpl_;
138         };
139 
140     public:
141         SubscriberImpl(NotificationSubscriber &subscriber);
~SubscriberImpl()142         ~SubscriberImpl() {};
143 
144         void OnConnected() override;
145 
146         void OnDisconnected() override;
147 
148         void OnConsumed(
149             const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap) override;
150 
151         void OnConsumedList(const std::vector<sptr<Notification>> &notifications,
152             const sptr<NotificationSortingMap> &notificationMap) override;
153 
154         void OnCanceled(const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap,
155             int32_t deleteReason) override;
156 
157         void OnCanceledList(const std::vector<sptr<Notification>> &notifications,
158             const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason) override;
159 
160         void OnBatchCanceled(const std::vector<sptr<Notification>> &notifications,
161             const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason);
162 
163         void OnUpdated(const sptr<NotificationSortingMap> &notificationMap) override;
164 
165         void OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date) override;
166 
167         void OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override;
168 
169         void OnBadgeChanged(const sptr<BadgeNumberCallbackData> &badgeData) override;
170 
171         void OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override;
172 
173         sptr<AnsManagerInterface> GetAnsManagerProxy();
174 
175         void OnSubscriberDestory();
176 
177     public:
178         NotificationSubscriber &subscriber_;
179         sptr<DeathRecipient> recipient_ {nullptr};
180         std::atomic_bool isSubscriberDestory_ {false};
181     };
182 
183 private:
184     const sptr<SubscriberImpl> GetImpl() const;
185 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
186     NotificationConstant::FlagStatus DowngradeReminder(
187         const NotificationConstant::FlagStatus &oldFlags, const NotificationConstant::FlagStatus &judgeFlags) const;
188 #endif
189 
190 private:
191     sptr<SubscriberImpl> impl_ = nullptr;
192     std::string deviceType_;
193 
194     friend class AnsNotification;
195 };
196 }  // namespace Notification
197 }  // namespace OHOS
198 
199 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_SUBSCRIBER_H
200