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 **/ 54 virtual void OnConsumed(const std::shared_ptr<Notification> &request) = 0; 55 56 /** 57 * @brief Called back when the subscriber receives a new notification. 58 * 59 * @param request Indicates the received Notification object. 60 * @param sortingMap Indicates the sorting map used by the current subscriber to obtain 61 * notification ranking information. 62 **/ 63 virtual void OnConsumed( 64 const std::shared_ptr<Notification> &request, const std::shared_ptr<NotificationSortingMap> &sortingMap) = 0; 65 66 /** 67 * @brief Called back when the subscriber is disconnected from the ANS. 68 **/ 69 virtual void OnDisconnected() = 0; 70 71 /** 72 * @brief Called back when the ranking information about the current notification changes. 73 * 74 * @param sortingMap Indicates the sorting map used to obtain notification ranking information. 75 **/ 76 virtual void OnUpdate(const std::shared_ptr<NotificationSortingMap> &sortingMap) = 0; 77 78 /** 79 * @brief Called back when connection to the ANS has died. 80 **/ 81 virtual void OnDied() = 0; 82 83 /** 84 * @brief Called when the Do Not Disturb date changes. 85 * 86 * @param date Indicates the current Do Not Disturb date. 87 **/ 88 virtual void OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> &date) = 0; 89 90 /** 91 * @brief Called when the notification permission changes. 92 * 93 * @param callbackData Indicates the properties of the application that notification permission has changed. 94 **/ 95 virtual void OnEnabledNotificationChanged(const std::shared_ptr<EnabledNotificationCallbackData> &callbackData) = 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(const sptr<Notification> ¬ification) override; 121 122 void OnConsumed( 123 const sptr<Notification> ¬ification, const sptr<NotificationSortingMap> ¬ificationMap) override; 124 125 void OnCanceled(const sptr<Notification> ¬ification, const sptr<NotificationSortingMap> ¬ificationMap, 126 int32_t deleteReason) override; 127 128 void OnUpdated(const sptr<NotificationSortingMap> ¬ificationMap) override; 129 130 void OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date) override; 131 132 void OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override; 133 134 bool GetAnsManagerProxy(); 135 136 public: 137 NotificationSubscriber &subscriber_; 138 sptr<DeathRecipient> recipient_ {nullptr}; 139 sptr<AnsManagerInterface> proxy_ {nullptr}; 140 std::mutex mutex_ {}; 141 }; 142 143 private: 144 const sptr<SubscriberImpl> GetImpl() const; 145 146 private: 147 sptr<SubscriberImpl> impl_ = nullptr; 148 149 friend class AnsNotification; 150 }; 151 } // namespace Notification 152 } // namespace OHOS 153 154 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_SUBSCRIBER_H