• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #include "notification_subscriber.h"
17 
18 #include "hitrace_meter_adapter.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21 
22 namespace OHOS {
23 namespace Notification {
NotificationSubscriber()24 NotificationSubscriber::NotificationSubscriber()
25 {
26     impl_ = new (std::nothrow) SubscriberImpl(*this);
27 };
28 
~NotificationSubscriber()29 NotificationSubscriber::~NotificationSubscriber()
30 {}
31 
GetImpl() const32 const sptr<NotificationSubscriber::SubscriberImpl> NotificationSubscriber::GetImpl() const
33 {
34     return impl_;
35 }
36 
SubscriberImpl(NotificationSubscriber & subscriber)37 NotificationSubscriber::SubscriberImpl::SubscriberImpl(NotificationSubscriber &subscriber) : subscriber_(subscriber)
38 {
39     recipient_ = new (std::nothrow) DeathRecipient(*this);
40 };
41 
OnConnected()42 void NotificationSubscriber::SubscriberImpl::OnConnected()
43 {
44     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
45     if (GetAnsManagerProxy()) {
46         proxy_->AsObject()->AddDeathRecipient(recipient_);
47         ANS_LOGD("%s, Add death recipient.", __func__);
48     }
49     subscriber_.OnConnected();
50 }
51 
OnDisconnected()52 void NotificationSubscriber::SubscriberImpl::OnDisconnected()
53 {
54     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
55     if (GetAnsManagerProxy()) {
56         proxy_->AsObject()->RemoveDeathRecipient(recipient_);
57         ANS_LOGD("%s, Remove death recipient.", __func__);
58     }
59     subscriber_.OnDisconnected();
60 }
61 
OnConsumed(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap)62 void NotificationSubscriber::SubscriberImpl::OnConsumed(
63     const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap)
64 {
65     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
66     subscriber_.OnConsumed(
67         std::make_shared<Notification>(*notification), std::make_shared<NotificationSortingMap>(*notificationMap));
68 }
69 
OnConsumedList(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap)70 void NotificationSubscriber::SubscriberImpl::OnConsumedList(const std::vector<sptr<Notification>> &notifications,
71     const sptr<NotificationSortingMap> &notificationMap)
72 {
73     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
74     for (auto notification : notifications) {
75         OnConsumed(notification, notificationMap);
76     }
77 }
78 
OnCanceled(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)79 void NotificationSubscriber::SubscriberImpl::OnCanceled(
80     const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason)
81 {
82     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
83     if (notificationMap == nullptr) {
84         subscriber_.OnCanceled(std::make_shared<Notification>(*notification),
85             std::make_shared<NotificationSortingMap>(), deleteReason);
86     } else {
87         subscriber_.OnCanceled(std::make_shared<Notification>(*notification),
88             std::make_shared<NotificationSortingMap>(*notificationMap), deleteReason);
89     }
90 }
91 
OnBatchCanceled(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)92 void NotificationSubscriber::SubscriberImpl::OnBatchCanceled(const std::vector<sptr<Notification>> &notifications,
93     const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason)
94 {
95     std::vector<std::shared_ptr<Notification>> notificationList;
96     for (auto notification : notifications) {
97         notificationList.emplace_back(std::make_shared<Notification>(*notification));
98     }
99     if (notificationMap == nullptr) {
100         subscriber_.OnBatchCanceled(notificationList,
101             std::make_shared<NotificationSortingMap>(), deleteReason);
102     } else if (notificationMap != nullptr) {
103         subscriber_.OnBatchCanceled(notificationList,
104             std::make_shared<NotificationSortingMap>(*notificationMap), deleteReason);
105     }
106 }
107 
108 
OnCanceledList(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)109 void NotificationSubscriber::SubscriberImpl::OnCanceledList(const std::vector<sptr<Notification>> &notifications,
110     const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason)
111 {
112     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
113     if (subscriber_.HasOnBatchCancelCallback()) {
114         OnBatchCanceled(notifications, notificationMap, deleteReason);
115         return;
116     }
117     for (auto notification : notifications) {
118         OnCanceled(notification, notificationMap, deleteReason);
119     }
120 }
121 
OnUpdated(const sptr<NotificationSortingMap> & notificationMap)122 void NotificationSubscriber::SubscriberImpl::OnUpdated(const sptr<NotificationSortingMap> &notificationMap)
123 {
124     subscriber_.OnUpdate(std::make_shared<NotificationSortingMap>(*notificationMap));
125 }
126 
OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> & date)127 void NotificationSubscriber::SubscriberImpl::OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date)
128 {
129     subscriber_.OnDoNotDisturbDateChange(std::make_shared<NotificationDoNotDisturbDate>(*date));
130 }
131 
OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> & callbackData)132 void NotificationSubscriber::SubscriberImpl::OnEnabledNotificationChanged(
133     const sptr<EnabledNotificationCallbackData> &callbackData)
134 {
135     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
136     subscriber_.OnEnabledNotificationChanged(std::make_shared<EnabledNotificationCallbackData>(*callbackData));
137 }
138 
OnBadgeChanged(const sptr<BadgeNumberCallbackData> & badgeData)139 void NotificationSubscriber::SubscriberImpl::OnBadgeChanged(const sptr<BadgeNumberCallbackData> &badgeData)
140 {
141     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
142     subscriber_.OnBadgeChanged(std::make_shared<BadgeNumberCallbackData>(*badgeData));
143 }
144 
GetAnsManagerProxy()145 bool NotificationSubscriber::SubscriberImpl::GetAnsManagerProxy()
146 {
147     if (proxy_ == nullptr) {
148         std::lock_guard<std::mutex> lock(mutex_);
149         if (proxy_ == nullptr) {
150             sptr<ISystemAbilityManager> systemAbilityManager =
151                 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
152             if (!systemAbilityManager) {
153                 return false;
154             }
155 
156             sptr<IRemoteObject> remoteObject =
157                 systemAbilityManager->GetSystemAbility(ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID);
158             if (!remoteObject) {
159                 return false;
160             }
161 
162             proxy_ = iface_cast<AnsManagerInterface>(remoteObject);
163             if ((proxy_ == nullptr) || (proxy_->AsObject() == nullptr)) {
164                 return false;
165             }
166         }
167     }
168 
169     return true;
170 }
171 
DeathRecipient(SubscriberImpl & subscriberImpl)172 NotificationSubscriber::SubscriberImpl::DeathRecipient::DeathRecipient(SubscriberImpl &subscriberImpl)
173     : subscriberImpl_(subscriberImpl) {};
174 
~DeathRecipient()175 NotificationSubscriber::SubscriberImpl::DeathRecipient::~DeathRecipient() {};
176 
OnRemoteDied(const wptr<IRemoteObject> & object)177 void NotificationSubscriber::SubscriberImpl::DeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
178 {
179     subscriberImpl_.proxy_ = nullptr;
180     subscriberImpl_.subscriber_.OnDied();
181     subscriberImpl_.subscriber_.OnDisconnected();
182 }
183 }  // namespace Notification
184 }  // namespace OHOS
185