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.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)62 void NotificationSubscriber::SubscriberImpl::OnConsumed(const sptr<Notification> ¬ification)
63 {
64 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
65 subscriber_.OnConsumed(std::make_shared<Notification>(*notification));
66 }
67
OnConsumed(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap)68 void NotificationSubscriber::SubscriberImpl::OnConsumed(
69 const sptr<Notification> ¬ification, const sptr<NotificationSortingMap> ¬ificationMap)
70 {
71 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
72 subscriber_.OnConsumed(
73 std::make_shared<Notification>(*notification), std::make_shared<NotificationSortingMap>(*notificationMap));
74 }
75
OnCanceled(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)76 void NotificationSubscriber::SubscriberImpl::OnCanceled(
77 const sptr<Notification> ¬ification, const sptr<NotificationSortingMap> ¬ificationMap, int32_t deleteReason)
78 {
79 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
80 subscriber_.OnCanceled(std::make_shared<Notification>(*notification),
81 std::make_shared<NotificationSortingMap>(*notificationMap),
82 deleteReason);
83 }
84
OnUpdated(const sptr<NotificationSortingMap> & notificationMap)85 void NotificationSubscriber::SubscriberImpl::OnUpdated(const sptr<NotificationSortingMap> ¬ificationMap)
86 {
87 subscriber_.OnUpdate(std::make_shared<NotificationSortingMap>(*notificationMap));
88 }
89
OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> & date)90 void NotificationSubscriber::SubscriberImpl::OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date)
91 {
92 subscriber_.OnDoNotDisturbDateChange(std::make_shared<NotificationDoNotDisturbDate>(*date));
93 }
94
OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> & callbackData)95 void NotificationSubscriber::SubscriberImpl::OnEnabledNotificationChanged(
96 const sptr<EnabledNotificationCallbackData> &callbackData)
97 {
98 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
99 subscriber_.OnEnabledNotificationChanged(std::make_shared<EnabledNotificationCallbackData>(*callbackData));
100 }
101
GetAnsManagerProxy()102 bool NotificationSubscriber::SubscriberImpl::GetAnsManagerProxy()
103 {
104 if (proxy_ == nullptr) {
105 std::lock_guard<std::mutex> lock(mutex_);
106 if (proxy_ == nullptr) {
107 sptr<ISystemAbilityManager> systemAbilityManager =
108 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
109 if (!systemAbilityManager) {
110 return false;
111 }
112
113 sptr<IRemoteObject> remoteObject =
114 systemAbilityManager->GetSystemAbility(ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID);
115 if (!remoteObject) {
116 return false;
117 }
118
119 proxy_ = iface_cast<AnsManagerInterface>(remoteObject);
120 if ((proxy_ == nullptr) || (proxy_->AsObject() == nullptr)) {
121 return false;
122 }
123 }
124 }
125
126 return true;
127 }
128
DeathRecipient(SubscriberImpl & subscriberImpl)129 NotificationSubscriber::SubscriberImpl::DeathRecipient::DeathRecipient(SubscriberImpl &subscriberImpl)
130 : subscriberImpl_(subscriberImpl) {};
131
~DeathRecipient()132 NotificationSubscriber::SubscriberImpl::DeathRecipient::~DeathRecipient() {};
133
OnRemoteDied(const wptr<IRemoteObject> & object)134 void NotificationSubscriber::SubscriberImpl::DeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
135 {
136 subscriberImpl_.proxy_ = nullptr;
137 subscriberImpl_.subscriber_.OnDied();
138 }
139 } // namespace Notification
140 } // namespace OHOS