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_ANS_STANDARD_KITS_NATIVE_INCLUDE_NOTIFICATION_SUBSCRIBER_H 17 #define BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_INCLUDE_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 /** 30 * @brief Default constructor used to create a instance. 31 */ 32 NotificationSubscriber(); 33 34 /** 35 * @brief Default destructor. 36 */ 37 virtual ~NotificationSubscriber(); 38 39 /** 40 * @brief Called back when a notification is canceled. 41 * 42 * @param request Indicates the canceled Notification object. 43 **/ 44 virtual void OnCanceled(const std::shared_ptr<Notification> &request) = 0; 45 46 /** 47 * @brief Called back when a notification is canceled. 48 * 49 * @param request Indicates the canceled Notification object. 50 * @param sortingMap Indicates the sorting map used by the current subscriber 51 * to obtain notification ranking information. 52 * @param deleteReason Indicates the reason for the deletion. For details, see NotificationConstant. 53 **/ 54 virtual void OnCanceled(const std::shared_ptr<Notification> &request, 55 const std::shared_ptr<NotificationSortingMap> &sortingMap, int deleteReason) = 0; 56 57 /** 58 * Called back when the subscriber is connected to the Advanced Notification Service (ANS). 59 **/ 60 virtual void OnConnected() = 0; 61 62 /** 63 * @brief Called back when the subscriber receives a new notification. 64 * 65 * @param request Indicates the received Notification object. 66 **/ 67 virtual void OnConsumed(const std::shared_ptr<Notification> &request) = 0; 68 69 /** 70 * @brief Called back when the subscriber receives a new notification. 71 * 72 * @param request Indicates the received Notification object. 73 * @param sortingMap Indicates the sorting map used by the current subscriber to obtain 74 * notification ranking information. 75 **/ 76 virtual void OnConsumed( 77 const std::shared_ptr<Notification> &request, const std::shared_ptr<NotificationSortingMap> &sortingMap) = 0; 78 79 /** 80 * Called back when the subscriber is disconnected from the ANS. 81 **/ 82 virtual void OnDisconnected() = 0; 83 84 /** 85 * @brief Called back when the ranking information about the current notification changes. 86 * 87 * @param sortingMap Indicates the sorting map used to obtain notification ranking information. 88 **/ 89 virtual void OnUpdate(const std::shared_ptr<NotificationSortingMap> &sortingMap) = 0; 90 91 /** 92 * @brief Called back when connection to the ANS has died. 93 **/ 94 virtual void OnDied() = 0; 95 96 /** 97 * @brief Called when the Do Not Disturb date changes. 98 * 99 * @param date Indicates the current Do Not Disturb date. 100 **/ 101 virtual void OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> &date) = 0; 102 103 /** 104 * @brief Called when the notification permission changes. 105 * 106 * @param callbackData Indicates the properties of the application that notification permission has changed. 107 **/ 108 virtual void OnEnabledNotificationChanged(const std::shared_ptr<EnabledNotificationCallbackData> &callbackData) = 0; 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(const sptr<Notification> ¬ification) override; 134 135 void OnConsumed( 136 const sptr<Notification> ¬ification, const sptr<NotificationSortingMap> ¬ificationMap) override; 137 138 void OnCanceled(const sptr<Notification> ¬ification) override; 139 140 void OnCanceled(const sptr<Notification> ¬ification, const sptr<NotificationSortingMap> ¬ificationMap, 141 int deleteReason) override; 142 143 void OnUpdated(const sptr<NotificationSortingMap> ¬ificationMap) override; 144 145 void OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date) override; 146 147 void OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override; 148 149 bool GetAnsManagerProxy(); 150 151 public: 152 NotificationSubscriber &subscriber_; 153 sptr<DeathRecipient> recipient_ {nullptr}; 154 sptr<IAnsManager> proxy_ {nullptr}; 155 std::mutex mutex_ {}; 156 }; 157 158 private: 159 const sptr<SubscriberImpl> GetImpl() const; 160 161 private: 162 sptr<SubscriberImpl> impl_ = nullptr; 163 164 friend class AnsNotification; 165 }; 166 } // namespace Notification 167 } // namespace OHOS 168 169 #endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_INCLUDE_NOTIFICATION_SUBSCRIBER_H