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 "iservice_registry.h"
19 #include "system_ability_definition.h"
20
21 namespace OHOS {
22 namespace Notification {
NotificationSubscriber()23 NotificationSubscriber::NotificationSubscriber()
24 {
25 impl_ = new (std::nothrow) SubscriberImpl(*this);
26 };
27
~NotificationSubscriber()28 NotificationSubscriber::~NotificationSubscriber()
29 {}
30
GetImpl() const31 const sptr<NotificationSubscriber::SubscriberImpl> NotificationSubscriber::GetImpl() const
32 {
33 return impl_;
34 }
35
SubscriberImpl(NotificationSubscriber & subscriber)36 NotificationSubscriber::SubscriberImpl::SubscriberImpl(NotificationSubscriber &subscriber) : subscriber_(subscriber)
37 {
38 recipient_ = new (std::nothrow) DeathRecipient(*this);
39 };
40
OnConnected()41 void NotificationSubscriber::SubscriberImpl::OnConnected()
42 {
43 if (GetAnsManagerProxy()) {
44 proxy_->AsObject()->AddDeathRecipient(recipient_);
45 ANS_LOGD("%s, Add death recipient.", __func__);
46 }
47 subscriber_.OnConnected();
48 }
49
OnDisconnected()50 void NotificationSubscriber::SubscriberImpl::OnDisconnected()
51 {
52 if (GetAnsManagerProxy()) {
53 proxy_->AsObject()->RemoveDeathRecipient(recipient_);
54 ANS_LOGD("%s, Remove death recipient.", __func__);
55 }
56 subscriber_.OnDisconnected();
57 }
58
OnConsumed(const sptr<Notification> & notification)59 void NotificationSubscriber::SubscriberImpl::OnConsumed(const sptr<Notification> ¬ification)
60 {
61 subscriber_.OnConsumed(std::make_shared<Notification>(*notification));
62 }
63
OnConsumed(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap)64 void NotificationSubscriber::SubscriberImpl::OnConsumed(
65 const sptr<Notification> ¬ification, const sptr<NotificationSortingMap> ¬ificationMap)
66 {
67 subscriber_.OnConsumed(
68 std::make_shared<Notification>(*notification), std::make_shared<NotificationSortingMap>(*notificationMap));
69 }
70
OnCanceled(const sptr<Notification> & notification)71 void NotificationSubscriber::SubscriberImpl::OnCanceled(const sptr<Notification> ¬ification)
72 {
73 subscriber_.OnCanceled(std::make_shared<Notification>(*notification));
74 }
75
OnCanceled(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap,int deleteReason)76 void NotificationSubscriber::SubscriberImpl::OnCanceled(
77 const sptr<Notification> ¬ification, const sptr<NotificationSortingMap> ¬ificationMap, int deleteReason)
78 {
79 subscriber_.OnCanceled(std::make_shared<Notification>(*notification),
80 std::make_shared<NotificationSortingMap>(*notificationMap),
81 deleteReason);
82 }
83
OnUpdated(const sptr<NotificationSortingMap> & notificationMap)84 void NotificationSubscriber::SubscriberImpl::OnUpdated(const sptr<NotificationSortingMap> ¬ificationMap)
85 {
86 subscriber_.OnUpdate(std::make_shared<NotificationSortingMap>(*notificationMap));
87 }
88
OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> & date)89 void NotificationSubscriber::SubscriberImpl::OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date)
90 {
91 subscriber_.OnDoNotDisturbDateChange(std::make_shared<NotificationDoNotDisturbDate>(*date));
92 }
93
OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> & callbackData)94 void NotificationSubscriber::SubscriberImpl::OnEnabledNotificationChanged(
95 const sptr<EnabledNotificationCallbackData> &callbackData)
96 {
97 subscriber_.OnEnabledNotificationChanged(std::make_shared<EnabledNotificationCallbackData>(*callbackData));
98 }
99
GetAnsManagerProxy()100 bool NotificationSubscriber::SubscriberImpl::GetAnsManagerProxy()
101 {
102 if (proxy_ == nullptr) {
103 std::lock_guard<std::mutex> lock(mutex_);
104 if (proxy_ == nullptr) {
105 sptr<ISystemAbilityManager> systemAbilityManager =
106 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
107 if (!systemAbilityManager) {
108 return false;
109 }
110
111 sptr<IRemoteObject> remoteObject =
112 systemAbilityManager->GetSystemAbility(ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID);
113 if (!remoteObject) {
114 return false;
115 }
116
117 proxy_ = iface_cast<IAnsManager>(remoteObject);
118 if ((proxy_ == nullptr) || (proxy_->AsObject() == nullptr)) {
119 return false;
120 }
121 }
122 }
123
124 return true;
125 }
126
DeathRecipient(SubscriberImpl & subscriberImpl)127 NotificationSubscriber::SubscriberImpl::DeathRecipient::DeathRecipient(SubscriberImpl &subscriberImpl)
128 : subscriberImpl_(subscriberImpl) {};
129
~DeathRecipient()130 NotificationSubscriber::SubscriberImpl::DeathRecipient::~DeathRecipient() {};
131
OnRemoteDied(const wptr<IRemoteObject> & object)132 void NotificationSubscriber::SubscriberImpl::DeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
133 {
134 subscriberImpl_.proxy_ = nullptr;
135 subscriberImpl_.subscriber_.OnDied();
136 }
137 } // namespace Notification
138 } // namespace OHOS