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