• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 "notification_constant.h"
19 #include "hitrace_meter_adapter.h"
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22 
23 namespace OHOS {
24 namespace Notification {
NotificationSubscriber()25 NotificationSubscriber::NotificationSubscriber()
26 {
27     impl_ = new (std::nothrow) SubscriberImpl(*this);
28     deviceType_ = NotificationConstant::CURRENT_DEVICE_TYPE;
29 };
30 
~NotificationSubscriber()31 NotificationSubscriber::~NotificationSubscriber()
32 {}
33 
SetDeviceType(const std::string & deviceType)34 void NotificationSubscriber::SetDeviceType(const std::string &deviceType)
35 {
36     deviceType_ = deviceType;
37 }
38 
GetDeviceType() const39 std::string NotificationSubscriber::GetDeviceType() const
40 {
41     return deviceType_;
42 }
43 
44 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
ProcessSyncDecision(const std::string & deviceType,std::shared_ptr<Notification> & notification) const45 bool NotificationSubscriber::ProcessSyncDecision(
46     const std::string &deviceType, std::shared_ptr<Notification> &notification) const
47 {
48     sptr<NotificationRequest> request = notification->GetNotificationRequestPoint();
49     if (request == nullptr) {
50         ANS_LOGE("No need to consume cause invalid reqeuest.");
51         return false;
52     }
53 
54 #ifdef ENABLE_ANS_PRIVILEGED_MESSAGE_EXT_WRAPPER
55     if (notification->GetPrivileged()) {
56         ANS_LOGI("No need to consume cause privileged reqeuest.")
57         return true;
58     }
59 #endif
60 
61     auto flagsMap = request->GetDeviceFlags();
62     if (flagsMap == nullptr || flagsMap->size() <= 0) {
63         return true;
64     }
65     auto flagIter = flagsMap->find(deviceType);
66     if (flagIter != flagsMap->end() && flagIter->second != nullptr) {
67         ANS_LOGI("SetFlags-before filte, notificationKey = %{public}s flagIter \
68             flags = %{public}d, deviceType:%{public}s",
69             request->GetKey().c_str(), flagIter->second->GetReminderFlags(), deviceType.c_str());
70         std::shared_ptr<NotificationFlags> tempFlags = request->GetFlags();
71         tempFlags->SetSoundEnabled(DowngradeReminder(tempFlags->IsSoundEnabled(), flagIter->second->IsSoundEnabled()));
72         tempFlags->SetVibrationEnabled(
73             DowngradeReminder(tempFlags->IsVibrationEnabled(), flagIter->second->IsVibrationEnabled()));
74         tempFlags->SetLockScreenVisblenessEnabled(
75             tempFlags->IsLockScreenVisblenessEnabled() && flagIter->second->IsLockScreenVisblenessEnabled());
76         tempFlags->SetBannerEnabled(
77             tempFlags->IsBannerEnabled() && flagIter->second->IsBannerEnabled());
78         tempFlags->SetLightScreenEnabled(
79             tempFlags->IsLightScreenEnabled() && flagIter->second->IsLightScreenEnabled());
80         request->SetFlags(tempFlags);
81         ANS_LOGI("SetFlags-after filte, notificationKey = %{public}s flags = %{public}d",
82             request->GetKey().c_str(), tempFlags->GetReminderFlags());
83         return true;
84     }
85     if (deviceType.size() <= 0 || deviceType.compare(NotificationConstant::CURRENT_DEVICE_TYPE) == 0) {
86         return true;
87     }
88     ANS_LOGI("Cannot find deviceFlags,notificationKey = %{public}s, deviceType: %{public}s.",
89         request->GetKey().c_str(), deviceType.c_str());
90     return false;
91 }
92 
DowngradeReminder(const NotificationConstant::FlagStatus & oldFlags,const NotificationConstant::FlagStatus & judgeFlags) const93 NotificationConstant::FlagStatus NotificationSubscriber::DowngradeReminder(
94     const NotificationConstant::FlagStatus &oldFlags, const NotificationConstant::FlagStatus &judgeFlags) const
95 {
96     if (judgeFlags == NotificationConstant::FlagStatus::NONE || oldFlags == NotificationConstant::FlagStatus::NONE) {
97         return NotificationConstant::FlagStatus::NONE;
98     }
99     if (judgeFlags > oldFlags) {
100         return judgeFlags;
101     } else {
102         return oldFlags;
103     }
104 }
105 #endif
106 
GetImpl() const107 const sptr<NotificationSubscriber::SubscriberImpl> NotificationSubscriber::GetImpl() const
108 {
109     return impl_;
110 }
111 
SubscriberImpl(NotificationSubscriber & subscriber)112 NotificationSubscriber::SubscriberImpl::SubscriberImpl(NotificationSubscriber &subscriber) : subscriber_(subscriber)
113 {
114     recipient_ = new (std::nothrow) DeathRecipient(*this);
115 };
116 
OnConnected()117 void NotificationSubscriber::SubscriberImpl::OnConnected()
118 {
119     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
120     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
121     if (proxy != nullptr) {
122         proxy->AsObject()->AddDeathRecipient(recipient_);
123         ANS_LOGD("%s, Add death recipient.", __func__);
124     }
125     subscriber_.OnConnected();
126 }
127 
OnDisconnected()128 void NotificationSubscriber::SubscriberImpl::OnDisconnected()
129 {
130     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
131     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
132     if (proxy != nullptr) {
133         proxy->AsObject()->RemoveDeathRecipient(recipient_);
134         ANS_LOGD("%s, Remove death recipient.", __func__);
135     }
136     subscriber_.OnDisconnected();
137 }
138 
OnConsumed(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap)139 void NotificationSubscriber::SubscriberImpl::OnConsumed(
140     const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap)
141 {
142     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
143     std::shared_ptr<Notification> sharedNotification = std::make_shared<Notification>(*notification);
144 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
145     if (!subscriber_.ProcessSyncDecision(subscriber_.GetDeviceType(), sharedNotification)) {
146         return;
147     }
148 #endif
149     subscriber_.OnConsumed(
150         sharedNotification, std::make_shared<NotificationSortingMap>(*notificationMap));
151 }
152 
OnConsumedList(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap)153 void NotificationSubscriber::SubscriberImpl::OnConsumedList(const std::vector<sptr<Notification>> &notifications,
154     const sptr<NotificationSortingMap> &notificationMap)
155 {
156     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
157     for (auto notification : notifications) {
158         OnConsumed(notification, notificationMap);
159     }
160 }
161 
OnCanceled(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)162 void NotificationSubscriber::SubscriberImpl::OnCanceled(
163     const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason)
164 {
165     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
166     if (notificationMap == nullptr) {
167         subscriber_.OnCanceled(std::make_shared<Notification>(*notification),
168             std::make_shared<NotificationSortingMap>(), deleteReason);
169     } else {
170         subscriber_.OnCanceled(std::make_shared<Notification>(*notification),
171             std::make_shared<NotificationSortingMap>(*notificationMap), deleteReason);
172     }
173 }
174 
OnBatchCanceled(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)175 void NotificationSubscriber::SubscriberImpl::OnBatchCanceled(const std::vector<sptr<Notification>> &notifications,
176     const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason)
177 {
178     std::vector<std::shared_ptr<Notification>> notificationList;
179     for (auto notification : notifications) {
180         notificationList.emplace_back(std::make_shared<Notification>(*notification));
181     }
182     if (notificationMap == nullptr) {
183         subscriber_.OnBatchCanceled(notificationList,
184             std::make_shared<NotificationSortingMap>(), deleteReason);
185     } else {
186         subscriber_.OnBatchCanceled(notificationList,
187             std::make_shared<NotificationSortingMap>(*notificationMap), deleteReason);
188     }
189 }
190 
191 
OnCanceledList(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)192 void NotificationSubscriber::SubscriberImpl::OnCanceledList(const std::vector<sptr<Notification>> &notifications,
193     const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason)
194 {
195     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
196     if (subscriber_.HasOnBatchCancelCallback()) {
197         OnBatchCanceled(notifications, notificationMap, deleteReason);
198         return;
199     }
200     for (auto notification : notifications) {
201         OnCanceled(notification, notificationMap, deleteReason);
202     }
203 }
204 
OnUpdated(const sptr<NotificationSortingMap> & notificationMap)205 void NotificationSubscriber::SubscriberImpl::OnUpdated(const sptr<NotificationSortingMap> &notificationMap)
206 {
207     subscriber_.OnUpdate(std::make_shared<NotificationSortingMap>(*notificationMap));
208 }
209 
OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> & date)210 void NotificationSubscriber::SubscriberImpl::OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date)
211 {
212     subscriber_.OnDoNotDisturbDateChange(std::make_shared<NotificationDoNotDisturbDate>(*date));
213 }
214 
OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> & callbackData)215 void NotificationSubscriber::SubscriberImpl::OnEnabledNotificationChanged(
216     const sptr<EnabledNotificationCallbackData> &callbackData)
217 {
218     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
219     subscriber_.OnEnabledNotificationChanged(std::make_shared<EnabledNotificationCallbackData>(*callbackData));
220 }
221 
OnBadgeChanged(const sptr<BadgeNumberCallbackData> & badgeData)222 void NotificationSubscriber::SubscriberImpl::OnBadgeChanged(const sptr<BadgeNumberCallbackData> &badgeData)
223 {
224     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
225     subscriber_.OnBadgeChanged(std::make_shared<BadgeNumberCallbackData>(*badgeData));
226 }
227 
OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> & callbackData)228 void NotificationSubscriber::SubscriberImpl::OnBadgeEnabledChanged(
229     const sptr<EnabledNotificationCallbackData> &callbackData)
230 {
231     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
232     subscriber_.OnBadgeEnabledChanged(callbackData);
233 }
234 
OnApplicationInfoNeedChanged(const std::string & bundleName)235 void NotificationSubscriber::SubscriberImpl::OnApplicationInfoNeedChanged(
236     const std::string& bundleName)
237 {
238     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
239     subscriber_.OnApplicationInfoNeedChanged(bundleName);
240 }
241 
OnOperationResponse(const sptr<NotificationOperationInfo> & operationInfo)242 ErrCode NotificationSubscriber::SubscriberImpl::OnOperationResponse(
243     const sptr<NotificationOperationInfo> &operationInfo)
244 {
245     return subscriber_.OnOperationResponse(std::make_shared<NotificationOperationInfo>(*operationInfo));
246 }
247 
GetAnsManagerProxy()248 sptr<AnsManagerInterface> NotificationSubscriber::SubscriberImpl::GetAnsManagerProxy()
249 {
250     sptr<ISystemAbilityManager> systemAbilityManager =
251         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
252     if (!systemAbilityManager) {
253         return nullptr;
254     }
255 
256     sptr<IRemoteObject> remoteObject =
257         systemAbilityManager->GetSystemAbility(ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID);
258     if (!remoteObject) {
259         return nullptr;
260     }
261 
262     sptr<AnsManagerInterface> proxy = iface_cast<AnsManagerInterface>(remoteObject);
263     if ((proxy == nullptr) || (proxy->AsObject() == nullptr)) {
264         return nullptr;
265     }
266 
267     return proxy;
268 }
269 
DeathRecipient(SubscriberImpl & subscriberImpl)270 NotificationSubscriber::SubscriberImpl::DeathRecipient::DeathRecipient(SubscriberImpl &subscriberImpl)
271     : subscriberImpl_(subscriberImpl) {};
272 
~DeathRecipient()273 NotificationSubscriber::SubscriberImpl::DeathRecipient::~DeathRecipient() {};
274 
OnRemoteDied(const wptr<IRemoteObject> & object)275 void NotificationSubscriber::SubscriberImpl::DeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
276 {
277     subscriberImpl_.subscriber_.OnDied();
278 }
279 }  // namespace Notification
280 }  // namespace OHOS
281