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 "ans_notification.h"
17 #include "ans_const_define.h"
18 #include "ans_inner_errors.h"
19 #include "ans_log_wrapper.h"
20 #include "hitrace_meter.h"
21 #include "iservice_registry.h"
22 #include "reminder_request_alarm.h"
23 #include "reminder_request_calendar.h"
24 #include "reminder_request_timer.h"
25 #include "system_ability_definition.h"
26
27 namespace OHOS {
28 namespace Notification {
AddNotificationSlot(const NotificationSlot & slot)29 ErrCode AnsNotification::AddNotificationSlot(const NotificationSlot &slot)
30 {
31 std::vector<NotificationSlot> slots;
32 slots.push_back(slot);
33 return AddNotificationSlots(slots);
34 }
35
AddSlotByType(const NotificationConstant::SlotType & slotType)36 ErrCode AnsNotification::AddSlotByType(const NotificationConstant::SlotType &slotType)
37 {
38 if (!GetAnsManagerProxy()) {
39 ANS_LOGE("GetAnsManagerProxy fail.");
40 return ERR_ANS_SERVICE_NOT_CONNECTED;
41 }
42 return ansManagerProxy_->AddSlotByType(slotType);
43 }
44
AddNotificationSlots(const std::vector<NotificationSlot> & slots)45 ErrCode AnsNotification::AddNotificationSlots(const std::vector<NotificationSlot> &slots)
46 {
47 if (slots.size() == 0) {
48 ANS_LOGE("Failed to add notification slots because input slots size is 0.");
49 return ERR_ANS_INVALID_PARAM;
50 }
51 if (!GetAnsManagerProxy()) {
52 ANS_LOGE("GetAnsManagerProxy fail.");
53 return ERR_ANS_SERVICE_NOT_CONNECTED;
54 }
55
56 std::vector<sptr<NotificationSlot>> slotsSptr;
57 for (auto it = slots.begin(); it != slots.end(); ++it) {
58 sptr<NotificationSlot> slot = new (std::nothrow) NotificationSlot(*it);
59 if (slot == nullptr) {
60 ANS_LOGE("Failed to create NotificationSlot ptr.");
61 return ERR_ANS_NO_MEMORY;
62 }
63 slotsSptr.emplace_back(slot);
64 }
65
66 return ansManagerProxy_->AddSlots(slotsSptr);
67 }
68
RemoveNotificationSlot(const NotificationConstant::SlotType & slotType)69 ErrCode AnsNotification::RemoveNotificationSlot(const NotificationConstant::SlotType &slotType)
70 {
71 if (!GetAnsManagerProxy()) {
72 ANS_LOGE("GetAnsManagerProxy fail.");
73 return ERR_ANS_SERVICE_NOT_CONNECTED;
74 }
75 return ansManagerProxy_->RemoveSlotByType(slotType);
76 }
77
RemoveAllSlots()78 ErrCode AnsNotification::RemoveAllSlots()
79 {
80 if (!GetAnsManagerProxy()) {
81 ANS_LOGE("GetAnsManagerProxy fail.");
82 return ERR_ANS_SERVICE_NOT_CONNECTED;
83 }
84 return ansManagerProxy_->RemoveAllSlots();
85 }
86
GetNotificationSlot(const NotificationConstant::SlotType & slotType,sptr<NotificationSlot> & slot)87 ErrCode AnsNotification::GetNotificationSlot(
88 const NotificationConstant::SlotType &slotType, sptr<NotificationSlot> &slot)
89 {
90 if (!GetAnsManagerProxy()) {
91 ANS_LOGE("GetAnsManagerProxy fail.");
92 return ERR_ANS_SERVICE_NOT_CONNECTED;
93 }
94 return ansManagerProxy_->GetSlotByType(slotType, slot);
95 }
96
GetNotificationSlots(std::vector<sptr<NotificationSlot>> & slots)97 ErrCode AnsNotification::GetNotificationSlots(std::vector<sptr<NotificationSlot>> &slots)
98 {
99 if (!GetAnsManagerProxy()) {
100 ANS_LOGE("GetAnsManagerProxy fail.");
101 return ERR_ANS_SERVICE_NOT_CONNECTED;
102 }
103 return ansManagerProxy_->GetSlots(slots);
104 }
105
GetNotificationSlotNumAsBundle(const NotificationBundleOption & bundleOption,uint64_t & num)106 ErrCode AnsNotification::GetNotificationSlotNumAsBundle(const NotificationBundleOption &bundleOption, uint64_t &num)
107 {
108 if (bundleOption.GetBundleName().empty()) {
109 ANS_LOGE("Invalid bundle name.");
110 return ERR_ANS_INVALID_PARAM;
111 }
112
113 if (!GetAnsManagerProxy()) {
114 ANS_LOGE("GetAnsManagerProxy fail.");
115 return ERR_ANS_SERVICE_NOT_CONNECTED;
116 }
117
118 sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
119 return ansManagerProxy_->GetSlotNumAsBundle(bo, num);
120 }
121
PublishNotification(const NotificationRequest & request)122 ErrCode AnsNotification::PublishNotification(const NotificationRequest &request)
123 {
124 ANS_LOGI("enter");
125 return PublishNotification(std::string(), request);
126 }
127
PublishNotification(const std::string & label,const NotificationRequest & request)128 ErrCode AnsNotification::PublishNotification(const std::string &label, const NotificationRequest &request)
129 {
130 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
131 ANS_LOGI("enter");
132
133 if (request.GetContent() == nullptr || request.GetNotificationType() == NotificationContent::Type::NONE) {
134 ANS_LOGE("Refuse to publish the notification without valid content");
135 return ERR_ANS_INVALID_PARAM;
136 }
137
138 if (!CanPublishMediaContent(request)) {
139 ANS_LOGE("Refuse to publish the notification because the sequence numbers actions not match those assigned to "
140 "added action buttons.");
141 return ERR_ANS_INVALID_PARAM;
142 }
143
144 ErrCode checkErr = CheckImageSize(request);
145 if (checkErr != ERR_OK) {
146 ANS_LOGE("The size of one picture exceeds the limit");
147 return checkErr;
148 }
149
150 if (!GetAnsManagerProxy()) {
151 ANS_LOGE("GetAnsManagerProxy fail.");
152 return ERR_ANS_SERVICE_NOT_CONNECTED;
153 }
154
155 sptr<NotificationRequest> reqPtr = new (std::nothrow) NotificationRequest(request);
156 if (reqPtr == nullptr) {
157 ANS_LOGE("Failed to create NotificationRequest ptr");
158 return ERR_ANS_NO_MEMORY;
159 }
160 if (IsNonDistributedNotificationType(reqPtr->GetNotificationType())) {
161 reqPtr->SetDistributed(false);
162 }
163 return ansManagerProxy_->Publish(label, reqPtr);
164 }
165
PublishNotification(const NotificationRequest & request,const std::string & deviceId)166 ErrCode AnsNotification::PublishNotification(const NotificationRequest &request, const std::string &deviceId)
167 {
168 if (request.GetContent() == nullptr || request.GetNotificationType() == NotificationContent::Type::NONE) {
169 ANS_LOGE("Refuse to publish the notification without valid content");
170 return ERR_ANS_INVALID_PARAM;
171 }
172
173 if (!deviceId.empty() &&
174 (IsNonDistributedNotificationType(request.GetNotificationType()))) {
175 ANS_LOGE("Refuse to publish the conversational and picture notification to the remote device");
176 return ERR_ANS_INVALID_PARAM;
177 }
178
179 if (!CanPublishMediaContent(request)) {
180 ANS_LOGE("Refuse to publish the notification because the sequence numbers actions not match those assigned to "
181 "added action buttons.");
182 return ERR_ANS_INVALID_PARAM;
183 }
184
185 ErrCode checkErr = CheckImageSize(request);
186 if (checkErr != ERR_OK) {
187 ANS_LOGE("The size of one picture exceeds the limit");
188 return checkErr;
189 }
190
191 if (!GetAnsManagerProxy()) {
192 ANS_LOGE("GetAnsManagerProxy fail.");
193 return ERR_ANS_SERVICE_NOT_CONNECTED;
194 }
195
196 sptr<NotificationRequest> reqPtr = new (std::nothrow) NotificationRequest(request);
197 if (reqPtr == nullptr) {
198 ANS_LOGE("Failed to create NotificationRequest ptr");
199 return ERR_ANS_NO_MEMORY;
200 }
201 return ansManagerProxy_->PublishToDevice(reqPtr, deviceId);
202 }
203
CancelNotification(int32_t notificationId)204 ErrCode AnsNotification::CancelNotification(int32_t notificationId)
205 {
206 return CancelNotification("", notificationId);
207 }
208
CancelNotification(const std::string & label,int32_t notificationId)209 ErrCode AnsNotification::CancelNotification(const std::string &label, int32_t notificationId)
210 {
211 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
212 if (!GetAnsManagerProxy()) {
213 ANS_LOGE("GetAnsManagerProxy fail.");
214 return ERR_ANS_SERVICE_NOT_CONNECTED;
215 }
216 return ansManagerProxy_->Cancel(notificationId, label);
217 }
218
CancelAllNotifications()219 ErrCode AnsNotification::CancelAllNotifications()
220 {
221 if (!GetAnsManagerProxy()) {
222 ANS_LOGE("GetAnsManagerProxy fail.");
223 return ERR_ANS_SERVICE_NOT_CONNECTED;
224 }
225 return ansManagerProxy_->CancelAll();
226 }
227
CancelAsBundle(int32_t notificationId,const std::string & representativeBundle,int32_t userId)228 ErrCode AnsNotification::CancelAsBundle(
229 int32_t notificationId, const std::string &representativeBundle, int32_t userId)
230 {
231 if (!GetAnsManagerProxy()) {
232 ANS_LOGE("GetAnsManagerProxy fail.");
233 return ERR_ANS_SERVICE_NOT_CONNECTED;
234 }
235 return ansManagerProxy_->CancelAsBundle(notificationId, representativeBundle, userId);
236 }
237
GetActiveNotificationNums(uint64_t & num)238 ErrCode AnsNotification::GetActiveNotificationNums(uint64_t &num)
239 {
240 if (!GetAnsManagerProxy()) {
241 ANS_LOGE("GetAnsManagerProxy fail.");
242 return ERR_ANS_SERVICE_NOT_CONNECTED;
243 }
244 return ansManagerProxy_->GetActiveNotificationNums(num);
245 }
246
GetActiveNotifications(std::vector<sptr<NotificationRequest>> & request)247 ErrCode AnsNotification::GetActiveNotifications(std::vector<sptr<NotificationRequest>> &request)
248 {
249 if (!GetAnsManagerProxy()) {
250 ANS_LOGE("GetAnsManagerProxy fail.");
251 return ERR_ANS_SERVICE_NOT_CONNECTED;
252 }
253 return ansManagerProxy_->GetActiveNotifications(request);
254 }
255
GetCurrentAppSorting(sptr<NotificationSortingMap> & sortingMap)256 ErrCode AnsNotification::GetCurrentAppSorting(sptr<NotificationSortingMap> &sortingMap)
257 {
258 if (!GetAnsManagerProxy()) {
259 ANS_LOGE("GetAnsManagerProxy fail.");
260 return ERR_ANS_SERVICE_NOT_CONNECTED;
261 }
262 return ansManagerProxy_->GetCurrentAppSorting(sortingMap);
263 }
264
SetNotificationAgent(const std::string & agent)265 ErrCode AnsNotification::SetNotificationAgent(const std::string &agent)
266 {
267 if (!GetAnsManagerProxy()) {
268 ANS_LOGE("GetAnsManagerProxy fail.");
269 return ERR_ANS_SERVICE_NOT_CONNECTED;
270 }
271 return ansManagerProxy_->SetNotificationAgent(agent);
272 }
273
GetNotificationAgent(std::string & agent)274 ErrCode AnsNotification::GetNotificationAgent(std::string &agent)
275 {
276 if (!GetAnsManagerProxy()) {
277 ANS_LOGE("GetAnsManagerProxy fail.");
278 return ERR_ANS_SERVICE_NOT_CONNECTED;
279 }
280 return ansManagerProxy_->GetNotificationAgent(agent);
281 }
282
CanPublishNotificationAsBundle(const std::string & representativeBundle,bool & canPublish)283 ErrCode AnsNotification::CanPublishNotificationAsBundle(const std::string &representativeBundle, bool &canPublish)
284 {
285 if (representativeBundle.empty()) {
286 ANS_LOGW("Input representativeBundle is empty");
287 return ERR_ANS_INVALID_PARAM;
288 }
289
290 if (!GetAnsManagerProxy()) {
291 ANS_LOGE("GetAnsManagerProxy fail.");
292 return ERR_ANS_SERVICE_NOT_CONNECTED;
293 }
294 return ansManagerProxy_->CanPublishAsBundle(representativeBundle, canPublish);
295 }
296
PublishNotificationAsBundle(const std::string & representativeBundle,const NotificationRequest & request)297 ErrCode AnsNotification::PublishNotificationAsBundle(
298 const std::string &representativeBundle, const NotificationRequest &request)
299 {
300 if (representativeBundle.empty()) {
301 ANS_LOGE("Refuse to publish the notification whit invalid representativeBundle");
302 return ERR_ANS_INVALID_PARAM;
303 }
304
305 if (request.GetContent() == nullptr || request.GetNotificationType() == NotificationContent::Type::NONE) {
306 ANS_LOGE("Refuse to publish the notification without valid content");
307 return ERR_ANS_INVALID_PARAM;
308 }
309
310 if (!CanPublishMediaContent(request)) {
311 ANS_LOGE("Refuse to publish the notification because the sequence numbers actions not match those assigned to "
312 "added action buttons.");
313 return ERR_ANS_INVALID_PARAM;
314 }
315
316 ErrCode checkErr = CheckImageSize(request);
317 if (checkErr != ERR_OK) {
318 ANS_LOGE("The size of one picture exceeds the limit");
319 return checkErr;
320 }
321
322 if (!GetAnsManagerProxy()) {
323 ANS_LOGE("GetAnsManagerProxy fail.");
324 return ERR_ANS_SERVICE_NOT_CONNECTED;
325 }
326
327 sptr<NotificationRequest> reqPtr = new (std::nothrow) NotificationRequest(request);
328 if (reqPtr == nullptr) {
329 ANS_LOGE("Failed to create NotificationRequest ptr");
330 return ERR_ANS_NO_MEMORY;
331 }
332 if (IsNonDistributedNotificationType(reqPtr->GetNotificationType())) {
333 reqPtr->SetDistributed(false);
334 }
335 return ansManagerProxy_->PublishAsBundle(reqPtr, representativeBundle);
336 }
337
SetNotificationBadgeNum()338 ErrCode AnsNotification::SetNotificationBadgeNum()
339 {
340 if (!GetAnsManagerProxy()) {
341 ANS_LOGE("GetAnsManagerProxy fail.");
342 return ERR_ANS_SERVICE_NOT_CONNECTED;
343 }
344 int32_t num = -1;
345 return ansManagerProxy_->SetNotificationBadgeNum(num);
346 }
347
SetNotificationBadgeNum(int32_t num)348 ErrCode AnsNotification::SetNotificationBadgeNum(int32_t num)
349 {
350 if (!GetAnsManagerProxy()) {
351 ANS_LOGE("GetAnsManagerProxy fail.");
352 return ERR_ANS_SERVICE_NOT_CONNECTED;
353 }
354 return ansManagerProxy_->SetNotificationBadgeNum(num);
355 }
356
IsAllowedNotify(bool & allowed)357 ErrCode AnsNotification::IsAllowedNotify(bool &allowed)
358 {
359 if (!GetAnsManagerProxy()) {
360 ANS_LOGE("GetAnsManagerProxy fail.");
361 return ERR_ANS_SERVICE_NOT_CONNECTED;
362 }
363 return ansManagerProxy_->IsAllowedNotify(allowed);
364 }
365
IsAllowedNotifySelf(bool & allowed)366 ErrCode AnsNotification::IsAllowedNotifySelf(bool &allowed)
367 {
368 ANS_LOGD("enter");
369 if (!GetAnsManagerProxy()) {
370 ANS_LOGE("GetAnsManagerProxy fail.");
371 return ERR_ANS_SERVICE_NOT_CONNECTED;
372 }
373 return ansManagerProxy_->IsAllowedNotifySelf(allowed);
374 }
375
RequestEnableNotification(std::string & deviceId)376 ErrCode AnsNotification::RequestEnableNotification(std::string &deviceId)
377 {
378 ANS_LOGD("enter");
379 if (!GetAnsManagerProxy()) {
380 ANS_LOGE("GetAnsManagerProxy fail.");
381 return ERR_ANS_SERVICE_NOT_CONNECTED;
382 }
383 return ansManagerProxy_->RequestEnableNotification(deviceId);
384 }
385
AreNotificationsSuspended(bool & suspended)386 ErrCode AnsNotification::AreNotificationsSuspended(bool &suspended)
387 {
388 if (!GetAnsManagerProxy()) {
389 ANS_LOGE("GetAnsManagerProxy fail.");
390 return ERR_ANS_SERVICE_NOT_CONNECTED;
391 }
392 return ansManagerProxy_->AreNotificationsSuspended(suspended);
393 }
394
HasNotificationPolicyAccessPermission(bool & hasPermission)395 ErrCode AnsNotification::HasNotificationPolicyAccessPermission(bool &hasPermission)
396 {
397 if (!GetAnsManagerProxy()) {
398 ANS_LOGE("GetAnsManagerProxy fail.");
399 return ERR_ANS_SERVICE_NOT_CONNECTED;
400 }
401 return ansManagerProxy_->HasNotificationPolicyAccessPermission(hasPermission);
402 }
403
GetBundleImportance(NotificationSlot::NotificationLevel & importance)404 ErrCode AnsNotification::GetBundleImportance(NotificationSlot::NotificationLevel &importance)
405 {
406 if (!GetAnsManagerProxy()) {
407 ANS_LOGE("GetAnsManagerProxy fail.");
408 return ERR_ANS_SERVICE_NOT_CONNECTED;
409 }
410 int32_t importanceTemp;
411 ErrCode ret = ansManagerProxy_->GetBundleImportance(importanceTemp);
412 if ((NotificationSlot::LEVEL_NONE <= importanceTemp) && (importanceTemp <= NotificationSlot::LEVEL_HIGH)) {
413 importance = static_cast<NotificationSlot::NotificationLevel>(importanceTemp);
414 } else {
415 importance = NotificationSlot::LEVEL_UNDEFINED;
416 }
417 return ret;
418 }
419
SubscribeNotification(const NotificationSubscriber & subscriber)420 ErrCode AnsNotification::SubscribeNotification(const NotificationSubscriber &subscriber)
421 {
422 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
423 if (!GetAnsManagerProxy()) {
424 ANS_LOGE("GetAnsManagerProxy fail.");
425 return ERR_ANS_SERVICE_NOT_CONNECTED;
426 }
427
428 sptr<NotificationSubscriber::SubscriberImpl> subscriberSptr = subscriber.GetImpl();
429 if (subscriberSptr == nullptr) {
430 ANS_LOGE("Failed to subscribe with SubscriberImpl null ptr.");
431 return ERR_ANS_INVALID_PARAM;
432 }
433 return ansManagerProxy_->Subscribe(subscriberSptr, nullptr);
434 }
435
SubscribeNotification(const NotificationSubscriber & subscriber,const NotificationSubscribeInfo & subscribeInfo)436 ErrCode AnsNotification::SubscribeNotification(
437 const NotificationSubscriber &subscriber, const NotificationSubscribeInfo &subscribeInfo)
438 {
439 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
440 if (!GetAnsManagerProxy()) {
441 ANS_LOGE("GetAnsManagerProxy fail.");
442 return ERR_ANS_SERVICE_NOT_CONNECTED;
443 }
444
445 sptr<NotificationSubscribeInfo> sptrInfo = new (std::nothrow) NotificationSubscribeInfo(subscribeInfo);
446 if (sptrInfo == nullptr) {
447 ANS_LOGE("Failed to create NotificationSubscribeInfo ptr.");
448 return ERR_ANS_NO_MEMORY;
449 }
450
451 sptr<NotificationSubscriber::SubscriberImpl> subscriberSptr = subscriber.GetImpl();
452 if (subscriberSptr == nullptr) {
453 ANS_LOGE("Failed to subscribe with SubscriberImpl null ptr.");
454 return ERR_ANS_INVALID_PARAM;
455 }
456 return ansManagerProxy_->Subscribe(subscriberSptr, sptrInfo);
457 }
458
UnSubscribeNotification(NotificationSubscriber & subscriber)459 ErrCode AnsNotification::UnSubscribeNotification(NotificationSubscriber &subscriber)
460 {
461 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
462 if (!GetAnsManagerProxy()) {
463 ANS_LOGE("GetAnsManagerProxy fail.");
464 return ERR_ANS_SERVICE_NOT_CONNECTED;
465 }
466
467 sptr<NotificationSubscriber::SubscriberImpl> subscriberSptr = subscriber.GetImpl();
468 if (subscriberSptr == nullptr) {
469 ANS_LOGE("Failed to unsubscribe with SubscriberImpl null ptr.");
470 return ERR_ANS_INVALID_PARAM;
471 }
472 return ansManagerProxy_->Unsubscribe(subscriberSptr, nullptr);
473 }
474
UnSubscribeNotification(NotificationSubscriber & subscriber,NotificationSubscribeInfo subscribeInfo)475 ErrCode AnsNotification::UnSubscribeNotification(
476 NotificationSubscriber &subscriber, NotificationSubscribeInfo subscribeInfo)
477 {
478 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
479 if (!GetAnsManagerProxy()) {
480 ANS_LOGE("GetAnsManagerProxy fail.");
481 return ERR_ANS_SERVICE_NOT_CONNECTED;
482 }
483
484 sptr<NotificationSubscribeInfo> sptrInfo = new (std::nothrow) NotificationSubscribeInfo(subscribeInfo);
485 if (sptrInfo == nullptr) {
486 ANS_LOGE("Failed to create NotificationSubscribeInfo ptr.");
487 return ERR_ANS_NO_MEMORY;
488 }
489
490 sptr<NotificationSubscriber::SubscriberImpl> subscriberSptr = subscriber.GetImpl();
491 if (subscriberSptr == nullptr) {
492 ANS_LOGE("Failed to unsubscribe with SubscriberImpl null ptr.");
493 return ERR_ANS_INVALID_PARAM;
494 }
495 return ansManagerProxy_->Unsubscribe(subscriberSptr, sptrInfo);
496 }
497
RemoveNotification(const std::string & key,int32_t removeReason)498 ErrCode AnsNotification::RemoveNotification(const std::string &key, int32_t removeReason)
499 {
500 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
501 if (key.empty()) {
502 ANS_LOGW("Input key is empty.");
503 return ERR_ANS_INVALID_PARAM;
504 }
505
506 if (!GetAnsManagerProxy()) {
507 ANS_LOGE("GetAnsManagerProxy fail.");
508 return ERR_ANS_SERVICE_NOT_CONNECTED;
509 }
510 return ansManagerProxy_->Delete(key, removeReason);
511 }
512
RemoveNotification(const NotificationBundleOption & bundleOption,const int32_t notificationId,const std::string & label,int32_t removeReason)513 ErrCode AnsNotification::RemoveNotification(const NotificationBundleOption &bundleOption,
514 const int32_t notificationId, const std::string &label, int32_t removeReason)
515 {
516 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
517 if (bundleOption.GetBundleName().empty()) {
518 ANS_LOGE("Invalid bundle name.");
519 return ERR_ANS_INVALID_PARAM;
520 }
521
522 if (!GetAnsManagerProxy()) {
523 ANS_LOGE("GetAnsManagerProxy fail.");
524 return ERR_ANS_SERVICE_NOT_CONNECTED;
525 }
526
527 sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
528 return ansManagerProxy_->RemoveNotification(bo, notificationId, label, removeReason);
529 }
530
RemoveAllNotifications(const NotificationBundleOption & bundleOption)531 ErrCode AnsNotification::RemoveAllNotifications(const NotificationBundleOption &bundleOption)
532 {
533 if (bundleOption.GetBundleName().empty()) {
534 ANS_LOGE("Invalid bundle name.");
535 return ERR_ANS_INVALID_PARAM;
536 }
537
538 if (!GetAnsManagerProxy()) {
539 ANS_LOGE("GetAnsManagerProxy fail.");
540 return ERR_ANS_SERVICE_NOT_CONNECTED;
541 }
542
543 sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
544 return ansManagerProxy_->RemoveAllNotifications(bo);
545 }
546
RemoveNotificationsByBundle(const NotificationBundleOption & bundleOption)547 ErrCode AnsNotification::RemoveNotificationsByBundle(const NotificationBundleOption &bundleOption)
548 {
549 if (bundleOption.GetBundleName().empty()) {
550 ANS_LOGE("Invalid bundle name.");
551 return ERR_ANS_INVALID_PARAM;
552 }
553
554 if (!GetAnsManagerProxy()) {
555 ANS_LOGE("GetAnsManagerProxy fail.");
556 return ERR_ANS_SERVICE_NOT_CONNECTED;
557 }
558
559 sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
560 return ansManagerProxy_->DeleteByBundle(bo);
561 }
562
RemoveNotifications()563 ErrCode AnsNotification::RemoveNotifications()
564 {
565 if (!GetAnsManagerProxy()) {
566 ANS_LOGE("GetAnsManagerProxy fail.");
567 return ERR_ANS_SERVICE_NOT_CONNECTED;
568 }
569 return ansManagerProxy_->DeleteAll();
570 }
571
GetNotificationSlotsForBundle(const NotificationBundleOption & bundleOption,std::vector<sptr<NotificationSlot>> & slots)572 ErrCode AnsNotification::GetNotificationSlotsForBundle(
573 const NotificationBundleOption &bundleOption, std::vector<sptr<NotificationSlot>> &slots)
574 {
575 if (bundleOption.GetBundleName().empty()) {
576 ANS_LOGE("Input bundleName is empty.");
577 return ERR_ANS_INVALID_PARAM;
578 }
579
580 if (!GetAnsManagerProxy()) {
581 ANS_LOGE("GetAnsManagerProxy fail.");
582 return ERR_ANS_SERVICE_NOT_CONNECTED;
583 }
584
585 sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
586 return ansManagerProxy_->GetSlotsByBundle(bo, slots);
587 }
588
UpdateNotificationSlots(const NotificationBundleOption & bundleOption,const std::vector<sptr<NotificationSlot>> & slots)589 ErrCode AnsNotification::UpdateNotificationSlots(
590 const NotificationBundleOption &bundleOption, const std::vector<sptr<NotificationSlot>> &slots)
591 {
592 if (bundleOption.GetBundleName().empty()) {
593 ANS_LOGE("Invalid bundle name.");
594 return ERR_ANS_INVALID_PARAM;
595 }
596
597 if (!GetAnsManagerProxy()) {
598 ANS_LOGE("GetAnsManagerProxy fail.");
599 return ERR_ANS_SERVICE_NOT_CONNECTED;
600 }
601
602 sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
603 return ansManagerProxy_->UpdateSlots(bo, slots);
604 }
605
GetAllActiveNotifications(std::vector<sptr<Notification>> & notification)606 ErrCode AnsNotification::GetAllActiveNotifications(std::vector<sptr<Notification>> ¬ification)
607 {
608 if (!GetAnsManagerProxy()) {
609 ANS_LOGE("GetAnsManagerProxy fail.");
610 return ERR_ANS_SERVICE_NOT_CONNECTED;
611 }
612 return ansManagerProxy_->GetAllActiveNotifications(notification);
613 }
614
GetAllActiveNotifications(const std::vector<std::string> key,std::vector<sptr<Notification>> & notification)615 ErrCode AnsNotification::GetAllActiveNotifications(
616 const std::vector<std::string> key, std::vector<sptr<Notification>> ¬ification)
617 {
618 if (!GetAnsManagerProxy()) {
619 ANS_LOGE("GetAnsManagerProxy fail.");
620 return ERR_ANS_SERVICE_NOT_CONNECTED;
621 }
622 return ansManagerProxy_->GetSpecialActiveNotifications(key, notification);
623 }
624
IsAllowedNotify(const NotificationBundleOption & bundleOption,bool & allowed)625 ErrCode AnsNotification::IsAllowedNotify(const NotificationBundleOption &bundleOption, bool &allowed)
626 {
627 if (bundleOption.GetBundleName().empty()) {
628 ANS_LOGE("Input bundle is empty.");
629 return ERR_ANS_INVALID_PARAM;
630 }
631
632 if (!GetAnsManagerProxy()) {
633 ANS_LOGE("GetAnsManagerProxy fail.");
634 return ERR_ANS_SERVICE_NOT_CONNECTED;
635 }
636
637 sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
638 return ansManagerProxy_->IsSpecialBundleAllowedNotify(bo, allowed);
639 }
640
SetNotificationsEnabledForAllBundles(const std::string & deviceId,bool enabled)641 ErrCode AnsNotification::SetNotificationsEnabledForAllBundles(const std::string &deviceId, bool enabled)
642 {
643 if (!GetAnsManagerProxy()) {
644 ANS_LOGE("GetAnsManagerProxy fail.");
645 return ERR_ANS_SERVICE_NOT_CONNECTED;
646 }
647 return ansManagerProxy_->SetNotificationsEnabledForAllBundles(deviceId, enabled);
648 }
649
SetNotificationsEnabledForDefaultBundle(const std::string & deviceId,bool enabled)650 ErrCode AnsNotification::SetNotificationsEnabledForDefaultBundle(const std::string &deviceId, bool enabled)
651 {
652 if (!GetAnsManagerProxy()) {
653 ANS_LOGE("GetAnsManagerProxy fail.");
654 return ERR_ANS_SERVICE_NOT_CONNECTED;
655 }
656 return ansManagerProxy_->SetNotificationsEnabledForBundle(deviceId, enabled);
657 }
658
SetNotificationsEnabledForSpecifiedBundle(const NotificationBundleOption & bundleOption,const std::string & deviceId,bool enabled)659 ErrCode AnsNotification::SetNotificationsEnabledForSpecifiedBundle(
660 const NotificationBundleOption &bundleOption, const std::string &deviceId, bool enabled)
661 {
662 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
663 if (bundleOption.GetBundleName().empty()) {
664 ANS_LOGE("Invalid bundle name.");
665 return ERR_ANS_INVALID_PARAM;
666 }
667
668 if (!GetAnsManagerProxy()) {
669 ANS_LOGE("GetAnsManagerProxy fail.");
670 return ERR_ANS_SERVICE_NOT_CONNECTED;
671 }
672
673 sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
674 return ansManagerProxy_->SetNotificationsEnabledForSpecialBundle(deviceId, bo, enabled);
675 }
676
SetShowBadgeEnabledForBundle(const NotificationBundleOption & bundleOption,bool enabled)677 ErrCode AnsNotification::SetShowBadgeEnabledForBundle(const NotificationBundleOption &bundleOption, bool enabled)
678 {
679 if (bundleOption.GetBundleName().empty()) {
680 ANS_LOGE("Invalid bundle name.");
681 return ERR_ANS_INVALID_PARAM;
682 }
683
684 if (!GetAnsManagerProxy()) {
685 ANS_LOGE("GetAnsManagerProxy fail.");
686 return ERR_ANS_SERVICE_NOT_CONNECTED;
687 }
688
689 sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
690 return ansManagerProxy_->SetShowBadgeEnabledForBundle(bo, enabled);
691 }
692
GetShowBadgeEnabledForBundle(const NotificationBundleOption & bundleOption,bool & enabled)693 ErrCode AnsNotification::GetShowBadgeEnabledForBundle(const NotificationBundleOption &bundleOption, bool &enabled)
694 {
695 if (bundleOption.GetBundleName().empty()) {
696 ANS_LOGE("Invalid bundle name.");
697 return ERR_ANS_INVALID_PARAM;
698 }
699
700 if (!GetAnsManagerProxy()) {
701 ANS_LOGE("GetAnsManagerProxy fail.");
702 return ERR_ANS_SERVICE_NOT_CONNECTED;
703 }
704
705 sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
706 return ansManagerProxy_->GetShowBadgeEnabledForBundle(bo, enabled);
707 }
708
GetShowBadgeEnabled(bool & enabled)709 ErrCode AnsNotification::GetShowBadgeEnabled(bool &enabled)
710 {
711 if (!GetAnsManagerProxy()) {
712 ANS_LOGE("GetAnsManagerProxy fail.");
713 return ERR_ANS_SERVICE_NOT_CONNECTED;
714 }
715
716 return ansManagerProxy_->GetShowBadgeEnabled(enabled);
717 }
718
CancelGroup(const std::string & groupName)719 ErrCode AnsNotification::CancelGroup(const std::string &groupName)
720 {
721 if (groupName.empty()) {
722 ANS_LOGE("Invalid group name.");
723 return ERR_ANS_INVALID_PARAM;
724 }
725
726 if (!GetAnsManagerProxy()) {
727 ANS_LOGE("GetAnsManagerProxy fail.");
728 return ERR_ANS_SERVICE_NOT_CONNECTED;
729 }
730
731 return ansManagerProxy_->CancelGroup(groupName);
732 }
733
RemoveGroupByBundle(const NotificationBundleOption & bundleOption,const std::string & groupName)734 ErrCode AnsNotification::RemoveGroupByBundle(
735 const NotificationBundleOption &bundleOption, const std::string &groupName)
736 {
737 if (bundleOption.GetBundleName().empty() || groupName.empty()) {
738 ANS_LOGE("Invalid parameter.");
739 return ERR_ANS_INVALID_PARAM;
740 }
741
742 if (!GetAnsManagerProxy()) {
743 ANS_LOGE("GetAnsManagerProxy fail.");
744 return ERR_ANS_SERVICE_NOT_CONNECTED;
745 }
746
747 sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
748 return ansManagerProxy_->RemoveGroupByBundle(bo, groupName);
749 }
750
SetDoNotDisturbDate(const NotificationDoNotDisturbDate & doNotDisturbDate)751 ErrCode AnsNotification::SetDoNotDisturbDate(const NotificationDoNotDisturbDate &doNotDisturbDate)
752 {
753 if (!GetAnsManagerProxy()) {
754 ANS_LOGE("GetAnsManagerProxy fail.");
755 return ERR_ANS_SERVICE_NOT_CONNECTED;
756 }
757
758 auto dndDatePtr = new (std::nothrow) NotificationDoNotDisturbDate(doNotDisturbDate);
759 if (dndDatePtr == nullptr) {
760 ANS_LOGE("create DoNotDisturbDate failed.");
761 return ERR_ANS_NO_MEMORY;
762 }
763
764 sptr<NotificationDoNotDisturbDate> dndDate(dndDatePtr);
765 return ansManagerProxy_->SetDoNotDisturbDate(dndDate);
766 }
767
GetDoNotDisturbDate(NotificationDoNotDisturbDate & doNotDisturbDate)768 ErrCode AnsNotification::GetDoNotDisturbDate(NotificationDoNotDisturbDate &doNotDisturbDate)
769 {
770 if (!GetAnsManagerProxy()) {
771 ANS_LOGE("GetAnsManagerProxy fail.");
772 return ERR_ANS_SERVICE_NOT_CONNECTED;
773 }
774
775 sptr<NotificationDoNotDisturbDate> dndDate = nullptr;
776 auto ret = ansManagerProxy_->GetDoNotDisturbDate(dndDate);
777 if (ret != ERR_OK) {
778 ANS_LOGE("Get DoNotDisturbDate failed.");
779 return ret;
780 }
781
782 if (!dndDate) {
783 ANS_LOGE("Invalid DoNotDisturbDate.");
784 return ERR_ANS_NO_MEMORY;
785 }
786
787 doNotDisturbDate = *dndDate;
788 return ret;
789 }
790
DoesSupportDoNotDisturbMode(bool & doesSupport)791 ErrCode AnsNotification::DoesSupportDoNotDisturbMode(bool &doesSupport)
792 {
793 if (!GetAnsManagerProxy()) {
794 ANS_LOGE("GetAnsManagerProxy fail.");
795 return ERR_ANS_SERVICE_NOT_CONNECTED;
796 }
797
798 return ansManagerProxy_->DoesSupportDoNotDisturbMode(doesSupport);
799 }
800
PublishContinuousTaskNotification(const NotificationRequest & request)801 ErrCode AnsNotification::PublishContinuousTaskNotification(const NotificationRequest &request)
802 {
803 if (request.GetContent() == nullptr || request.GetNotificationType() == NotificationContent::Type::NONE) {
804 ANS_LOGE("Refuse to publish the notification without valid content");
805 return ERR_ANS_INVALID_PARAM;
806 }
807
808 if (!CanPublishMediaContent(request)) {
809 ANS_LOGE("Refuse to publish the notification because the sequence numbers actions not match those assigned to "
810 "added action buttons.");
811 return ERR_ANS_INVALID_PARAM;
812 }
813
814 ErrCode checkErr = CheckImageSize(request);
815 if (checkErr != ERR_OK) {
816 ANS_LOGE("The size of one picture exceeds the limit");
817 return checkErr;
818 }
819
820 if (!GetAnsManagerProxy()) {
821 ANS_LOGE("GetAnsManagerProxy fail.");
822 return ERR_ANS_SERVICE_NOT_CONNECTED;
823 }
824
825 auto pReq = new (std::nothrow) NotificationRequest(request);
826 if (pReq == nullptr) {
827 ANS_LOGE("Failed to create NotificationRequest ptr.");
828 return ERR_ANS_NO_MEMORY;
829 }
830
831 sptr<NotificationRequest> sptrReq(pReq);
832 if (IsNonDistributedNotificationType(sptrReq->GetNotificationType())) {
833 sptrReq->SetDistributed(false);
834 }
835 return ansManagerProxy_->PublishContinuousTaskNotification(sptrReq);
836 }
837
CancelContinuousTaskNotification(const std::string & label,int32_t notificationId)838 ErrCode AnsNotification::CancelContinuousTaskNotification(const std::string &label, int32_t notificationId)
839 {
840 if (!GetAnsManagerProxy()) {
841 ANS_LOGE("GetAnsManagerProxy fail.");
842 return ERR_ANS_SERVICE_NOT_CONNECTED;
843 }
844
845 return ansManagerProxy_->CancelContinuousTaskNotification(label, notificationId);
846 }
847
IsDistributedEnabled(bool & enabled)848 ErrCode AnsNotification::IsDistributedEnabled(bool &enabled)
849 {
850 if (!GetAnsManagerProxy()) {
851 ANS_LOGE("GetAnsManagerProxy fail.");
852 return ERR_ANS_SERVICE_NOT_CONNECTED;
853 }
854
855 return ansManagerProxy_->IsDistributedEnabled(enabled);
856 }
857
EnableDistributed(const bool enabled)858 ErrCode AnsNotification::EnableDistributed(const bool enabled)
859 {
860 if (!GetAnsManagerProxy()) {
861 ANS_LOGE("GetAnsManagerProxy fail.");
862 return ERR_ANS_SERVICE_NOT_CONNECTED;
863 }
864
865 return ansManagerProxy_->EnableDistributed(enabled);
866 }
867
EnableDistributedByBundle(const NotificationBundleOption & bundleOption,const bool enabled)868 ErrCode AnsNotification::EnableDistributedByBundle(const NotificationBundleOption &bundleOption, const bool enabled)
869 {
870 if (!GetAnsManagerProxy()) {
871 ANS_LOGE("GetAnsManagerProxy fail.");
872 return ERR_ANS_SERVICE_NOT_CONNECTED;
873 }
874
875 sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
876 return ansManagerProxy_->EnableDistributedByBundle(bo, enabled);
877 }
878
EnableDistributedSelf(const bool enabled)879 ErrCode AnsNotification::EnableDistributedSelf(const bool enabled)
880 {
881 if (!GetAnsManagerProxy()) {
882 ANS_LOGE("GetAnsManagerProxy fail.");
883 return ERR_ANS_SERVICE_NOT_CONNECTED;
884 }
885
886 return ansManagerProxy_->EnableDistributedSelf(enabled);
887 }
888
IsDistributedEnableByBundle(const NotificationBundleOption & bundleOption,bool & enabled)889 ErrCode AnsNotification::IsDistributedEnableByBundle(const NotificationBundleOption &bundleOption, bool &enabled)
890 {
891 if (!GetAnsManagerProxy()) {
892 ANS_LOGE("GetAnsManagerProxy fail.");
893 return ERR_ANS_SERVICE_NOT_CONNECTED;
894 }
895
896 sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
897 return ansManagerProxy_->IsDistributedEnableByBundle(bo, enabled);
898 }
899
GetDeviceRemindType(NotificationConstant::RemindType & remindType)900 ErrCode AnsNotification::GetDeviceRemindType(NotificationConstant::RemindType &remindType)
901 {
902 if (!GetAnsManagerProxy()) {
903 ANS_LOGE("GetAnsManagerProxy fail.");
904 return ERR_ANS_SERVICE_NOT_CONNECTED;
905 }
906
907 return ansManagerProxy_->GetDeviceRemindType(remindType);
908 }
909
ResetAnsManagerProxy()910 void AnsNotification::ResetAnsManagerProxy()
911 {
912 ANS_LOGI("enter");
913 std::lock_guard<std::mutex> lock(mutex_);
914 if ((ansManagerProxy_ != nullptr) && (ansManagerProxy_->AsObject() != nullptr)) {
915 ansManagerProxy_->AsObject()->RemoveDeathRecipient(recipient_);
916 }
917 ansManagerProxy_ = nullptr;
918 }
919
PublishReminder(ReminderRequest & reminder)920 ErrCode AnsNotification::PublishReminder(ReminderRequest &reminder)
921 {
922 if (!GetAnsManagerProxy()) {
923 ANS_LOGE("GetAnsManagerProxy fail.");
924 return ERR_ANS_SERVICE_NOT_CONNECTED;
925 }
926 sptr<ReminderRequest> tarReminder = nullptr;
927 switch (reminder.GetReminderType()) {
928 case (ReminderRequest::ReminderType::TIMER): {
929 ANSR_LOGI("Publish timer");
930 ReminderRequestTimer &timer = (ReminderRequestTimer &)reminder;
931 tarReminder = new (std::nothrow) ReminderRequestTimer(timer);
932 break;
933 }
934 case (ReminderRequest::ReminderType::ALARM): {
935 ANSR_LOGI("Publish alarm");
936 ReminderRequestAlarm &alarm = (ReminderRequestAlarm &)reminder;
937 tarReminder = new (std::nothrow) ReminderRequestAlarm(alarm);
938 break;
939 }
940 case (ReminderRequest::ReminderType::CALENDAR): {
941 ANSR_LOGI("Publish calendar");
942 ReminderRequestCalendar &calendar = (ReminderRequestCalendar &)reminder;
943 tarReminder = new (std::nothrow) ReminderRequestCalendar(calendar);
944 break;
945 }
946 default: {
947 ANSR_LOGW("PublishReminder fail.");
948 return ERR_ANS_INVALID_PARAM;
949 }
950 }
951 ErrCode code = ansManagerProxy_->PublishReminder(tarReminder);
952 reminder.SetReminderId(tarReminder->GetReminderId());
953 return code;
954 }
955
CancelReminder(const int32_t reminderId)956 ErrCode AnsNotification::CancelReminder(const int32_t reminderId)
957 {
958 if (!GetAnsManagerProxy()) {
959 ANS_LOGE("GetAnsManagerProxy fail.");
960 return ERR_ANS_SERVICE_NOT_CONNECTED;
961 }
962 return ansManagerProxy_->CancelReminder(reminderId);
963 }
964
CancelAllReminders()965 ErrCode AnsNotification::CancelAllReminders()
966 {
967 if (!GetAnsManagerProxy()) {
968 ANS_LOGE("GetAnsManagerProxy fail.");
969 return ERR_ANS_SERVICE_NOT_CONNECTED;
970 }
971 return ansManagerProxy_->CancelAllReminders();
972 }
973
GetValidReminders(std::vector<sptr<ReminderRequest>> & validReminders)974 ErrCode AnsNotification::GetValidReminders(std::vector<sptr<ReminderRequest>> &validReminders)
975 {
976 if (!GetAnsManagerProxy()) {
977 ANS_LOGE("GetAnsManagerProxy fail.");
978 return ERR_ANS_SERVICE_NOT_CONNECTED;
979 }
980 return ansManagerProxy_->GetValidReminders(validReminders);
981 }
982
GetAnsManagerProxy()983 bool AnsNotification::GetAnsManagerProxy()
984 {
985 if (!ansManagerProxy_) {
986 std::lock_guard<std::mutex> lock(mutex_);
987 if (!ansManagerProxy_) {
988 sptr<ISystemAbilityManager> systemAbilityManager =
989 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
990 if (!systemAbilityManager) {
991 ANS_LOGE("Failed to get system ability mgr.");
992 return false;
993 }
994
995 sptr<IRemoteObject> remoteObject =
996 systemAbilityManager->GetSystemAbility(ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID);
997 if (!remoteObject) {
998 ANS_LOGE("Failed to get notification Manager.");
999 return false;
1000 }
1001
1002 ansManagerProxy_ = iface_cast<AnsManagerInterface>(remoteObject);
1003 if ((!ansManagerProxy_) || (!ansManagerProxy_->AsObject())) {
1004 ANS_LOGE("Failed to get notification Manager's proxy");
1005 return false;
1006 }
1007
1008 recipient_ = new (std::nothrow) AnsManagerDeathRecipient();
1009 if (!recipient_) {
1010 ANS_LOGE("Failed to create death recipient");
1011 return false;
1012 }
1013 ansManagerProxy_->AsObject()->AddDeathRecipient(recipient_);
1014 }
1015 }
1016
1017 return true;
1018 }
1019
CanPublishMediaContent(const NotificationRequest & request) const1020 bool AnsNotification::CanPublishMediaContent(const NotificationRequest &request) const
1021 {
1022 if (NotificationContent::Type::MEDIA != request.GetNotificationType()) {
1023 return true;
1024 }
1025
1026 if (request.GetContent() == nullptr) {
1027 ANS_LOGE("Failed to publish notification with null content.");
1028 return false;
1029 }
1030
1031 auto media = std::static_pointer_cast<NotificationMediaContent>(request.GetContent()->GetNotificationContent());
1032 if (media == nullptr) {
1033 ANS_LOGE("Failed to get media content.");
1034 return false;
1035 }
1036
1037 auto showActions = media->GetShownActions();
1038 size_t size = request.GetActionButtons().size();
1039 for (auto it = showActions.begin(); it != showActions.end(); ++it) {
1040 if (*it > size) {
1041 ANS_LOGE("The sequence numbers actions is: %{public}d, the assigned to added action buttons size is: "
1042 "%{public}zu.", *it, size);
1043 return false;
1044 }
1045 }
1046
1047 return true;
1048 }
1049
CheckImageOverSizeForPixelMap(const std::shared_ptr<Media::PixelMap> & pixelMap,uint32_t maxSize)1050 bool AnsNotification::CheckImageOverSizeForPixelMap(const std::shared_ptr<Media::PixelMap> &pixelMap, uint32_t maxSize)
1051 {
1052 if (!pixelMap) {
1053 return false;
1054 }
1055
1056 uint32_t size = static_cast<uint32_t>(pixelMap->GetByteCount());
1057 if (size > maxSize) {
1058 return true;
1059 }
1060 return false;
1061 }
1062
CheckImageSizeForContent(const NotificationRequest & request)1063 ErrCode AnsNotification::CheckImageSizeForContent(const NotificationRequest &request)
1064 {
1065 auto content = request.GetContent();
1066 if (!content) {
1067 ANS_LOGW("Invalid content in NotificationRequest");
1068 return ERR_OK;
1069 }
1070
1071 auto basicContent = request.GetContent()->GetNotificationContent();
1072 if (!basicContent) {
1073 ANS_LOGW("Invalid content in NotificationRequest");
1074 return ERR_OK;
1075 }
1076
1077 auto contentType = request.GetNotificationType();
1078 switch (contentType) {
1079 case NotificationContent::Type::CONVERSATION: {
1080 auto conversationalContent = std::static_pointer_cast<NotificationConversationalContent>(basicContent);
1081
1082 auto picture = conversationalContent->GetMessageUser().GetPixelMap();
1083 if (CheckImageOverSizeForPixelMap(picture, MAX_ICON_SIZE)) {
1084 ANS_LOGE("The size of picture in ConversationalContent's message user exceeds limit");
1085 return ERR_ANS_ICON_OVER_SIZE;
1086 }
1087
1088 auto messages = conversationalContent->GetAllConversationalMessages();
1089 for (auto &msg : messages) {
1090 if (!msg) {
1091 continue;
1092 }
1093
1094 auto img = msg->GetSender().GetPixelMap();
1095 if (CheckImageOverSizeForPixelMap(img, MAX_ICON_SIZE)) {
1096 ANS_LOGE("The size of picture in ConversationalContent's message exceeds limit");
1097 return ERR_ANS_ICON_OVER_SIZE;
1098 }
1099 }
1100 break;
1101 }
1102 case NotificationContent::Type::PICTURE: {
1103 auto pictureContent = std::static_pointer_cast<NotificationPictureContent>(basicContent);
1104
1105 auto bigPicture = pictureContent->GetBigPicture();
1106 if (CheckImageOverSizeForPixelMap(bigPicture, MAX_PICTURE_SIZE)) {
1107 ANS_LOGE("The size of big picture in PictureContent exceeds limit");
1108 return ERR_ANS_PICTURE_OVER_SIZE;
1109 }
1110 break;
1111 }
1112 default:
1113 break;
1114 }
1115
1116 return ERR_OK;
1117 }
1118
CheckImageSize(const NotificationRequest & request)1119 ErrCode AnsNotification::CheckImageSize(const NotificationRequest &request)
1120 {
1121 auto littleIcon = request.GetLittleIcon();
1122 if (CheckImageOverSizeForPixelMap(littleIcon, MAX_ICON_SIZE)) {
1123 ANS_LOGE("The size of little icon exceeds limit");
1124 return ERR_ANS_ICON_OVER_SIZE;
1125 }
1126
1127 auto bigIcon = request.GetBigIcon();
1128 if (CheckImageOverSizeForPixelMap(bigIcon, MAX_ICON_SIZE)) {
1129 ANS_LOGE("The size of big icon exceeds limit");
1130 return ERR_ANS_ICON_OVER_SIZE;
1131 }
1132
1133 ErrCode err = CheckImageSizeForContent(request);
1134 if (err != ERR_OK) {
1135 return err;
1136 }
1137
1138 auto buttons = request.GetActionButtons();
1139 for (auto &btn : buttons) {
1140 if (!btn) {
1141 continue;
1142 }
1143 auto icon = btn->GetIcon();
1144 if (CheckImageOverSizeForPixelMap(icon, MAX_ICON_SIZE)) {
1145 ANS_LOGE("The size of icon in ActionButton exceeds limit");
1146 return ERR_ANS_ICON_OVER_SIZE;
1147 }
1148 }
1149
1150 auto users = request.GetMessageUsers();
1151 for (auto &user : users) {
1152 if (!user) {
1153 continue;
1154 }
1155 auto icon = user->GetPixelMap();
1156 if (CheckImageOverSizeForPixelMap(icon, MAX_ICON_SIZE)) {
1157 ANS_LOGE("The size of picture in MessageUser exceeds limit");
1158 return ERR_ANS_ICON_OVER_SIZE;
1159 }
1160 }
1161
1162 return ERR_OK;
1163 }
1164
IsSupportTemplate(const std::string & templateName,bool & support)1165 ErrCode AnsNotification::IsSupportTemplate(const std::string &templateName, bool &support)
1166 {
1167 if (!GetAnsManagerProxy()) {
1168 ANS_LOGE("GetAnsManagerProxy fail.");
1169 return ERR_ANS_SERVICE_NOT_CONNECTED;
1170 }
1171
1172 return ansManagerProxy_->IsSupportTemplate(templateName, support);
1173 }
1174
IsNonDistributedNotificationType(const NotificationContent::Type & type)1175 bool AnsNotification::IsNonDistributedNotificationType(const NotificationContent::Type &type)
1176 {
1177 return ((type == NotificationContent::Type::CONVERSATION) || (type == NotificationContent::Type::PICTURE));
1178 }
1179
IsAllowedNotify(const int32_t & userId,bool & allowed)1180 ErrCode AnsNotification::IsAllowedNotify(const int32_t &userId, bool &allowed)
1181 {
1182 if (userId <= SUBSCRIBE_USER_INIT) {
1183 ANS_LOGE("Input userId is invalid.");
1184 return ERR_ANS_INVALID_PARAM;
1185 }
1186
1187 if (!GetAnsManagerProxy()) {
1188 ANS_LOGE("GetAnsManagerProxy fail.");
1189 return ERR_ANS_SERVICE_NOT_CONNECTED;
1190 }
1191
1192 return ansManagerProxy_->IsSpecialUserAllowedNotify(userId, allowed);
1193 }
1194
SetNotificationsEnabledForAllBundles(const int32_t & userId,bool enabled)1195 ErrCode AnsNotification::SetNotificationsEnabledForAllBundles(const int32_t &userId, bool enabled)
1196 {
1197 if (userId <= SUBSCRIBE_USER_INIT) {
1198 ANS_LOGE("Input userId is invalid.");
1199 return ERR_ANS_INVALID_PARAM;
1200 }
1201
1202 if (!GetAnsManagerProxy()) {
1203 ANS_LOGE("GetAnsManagerProxy fail.");
1204 return ERR_ANS_SERVICE_NOT_CONNECTED;
1205 }
1206 return ansManagerProxy_->SetNotificationsEnabledByUser(userId, enabled);
1207 }
1208
RemoveNotifications(const int32_t & userId)1209 ErrCode AnsNotification::RemoveNotifications(const int32_t &userId)
1210 {
1211 if (userId <= SUBSCRIBE_USER_INIT) {
1212 ANS_LOGE("Input userId is invalid.");
1213 return ERR_ANS_INVALID_PARAM;
1214 }
1215
1216 if (!GetAnsManagerProxy()) {
1217 ANS_LOGE("GetAnsManagerProxy fail.");
1218 return ERR_ANS_SERVICE_NOT_CONNECTED;
1219 }
1220
1221 return ansManagerProxy_->DeleteAllByUser(userId);
1222 }
1223
SetDoNotDisturbDate(const int32_t & userId,const NotificationDoNotDisturbDate & doNotDisturbDate)1224 ErrCode AnsNotification::SetDoNotDisturbDate(const int32_t &userId,
1225 const NotificationDoNotDisturbDate &doNotDisturbDate)
1226 {
1227 if (userId <= SUBSCRIBE_USER_INIT) {
1228 ANS_LOGE("Input userId is invalid.");
1229 return ERR_ANS_INVALID_PARAM;
1230 }
1231
1232 if (!GetAnsManagerProxy()) {
1233 ANS_LOGE("GetAnsManagerProxy fail.");
1234 return ERR_ANS_SERVICE_NOT_CONNECTED;
1235 }
1236
1237 auto dndDatePtr = new (std::nothrow) NotificationDoNotDisturbDate(doNotDisturbDate);
1238 if (dndDatePtr == nullptr) {
1239 ANS_LOGE("create DoNotDisturbDate failed.");
1240 return ERR_ANS_NO_MEMORY;
1241 }
1242
1243 sptr<NotificationDoNotDisturbDate> dndDate(dndDatePtr);
1244 return ansManagerProxy_->SetDoNotDisturbDate(dndDate);
1245 }
1246
GetDoNotDisturbDate(const int32_t & userId,NotificationDoNotDisturbDate & doNotDisturbDate)1247 ErrCode AnsNotification::GetDoNotDisturbDate(const int32_t &userId, NotificationDoNotDisturbDate &doNotDisturbDate)
1248 {
1249 if (userId <= SUBSCRIBE_USER_INIT) {
1250 ANS_LOGE("Input userId is invalid.");
1251 return ERR_ANS_INVALID_PARAM;
1252 }
1253
1254 if (!GetAnsManagerProxy()) {
1255 ANS_LOGE("GetAnsManagerProxy fail.");
1256 return ERR_ANS_SERVICE_NOT_CONNECTED;
1257 }
1258
1259 sptr<NotificationDoNotDisturbDate> dndDate = nullptr;
1260 auto ret = ansManagerProxy_->GetDoNotDisturbDate(dndDate);
1261 if (ret != ERR_OK) {
1262 ANS_LOGE("Get DoNotDisturbDate failed.");
1263 return ret;
1264 }
1265
1266 if (!dndDate) {
1267 ANS_LOGE("Invalid DoNotDisturbDate.");
1268 return ERR_ANS_NO_MEMORY;
1269 }
1270
1271 doNotDisturbDate = *dndDate;
1272 return ret;
1273 }
1274
SetEnabledForBundleSlot(const NotificationBundleOption & bundleOption,const NotificationConstant::SlotType & slotType,bool enabled)1275 ErrCode AnsNotification::SetEnabledForBundleSlot(
1276 const NotificationBundleOption &bundleOption, const NotificationConstant::SlotType &slotType, bool enabled)
1277 {
1278 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
1279 if (bundleOption.GetBundleName().empty()) {
1280 ANS_LOGE("Invalid bundle name.");
1281 return ERR_ANS_INVALID_PARAM;
1282 }
1283
1284 if (!GetAnsManagerProxy()) {
1285 ANS_LOGE("SetEnabledForBundleSlot fail.");
1286 return ERR_ANS_SERVICE_NOT_CONNECTED;
1287 }
1288
1289 sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1290 return ansManagerProxy_->SetEnabledForBundleSlot(bo, slotType, enabled);
1291 }
1292
GetEnabledForBundleSlot(const NotificationBundleOption & bundleOption,const NotificationConstant::SlotType & slotType,bool & enabled)1293 ErrCode AnsNotification::GetEnabledForBundleSlot(
1294 const NotificationBundleOption &bundleOption, const NotificationConstant::SlotType &slotType, bool &enabled)
1295 {
1296 if (bundleOption.GetBundleName().empty()) {
1297 ANS_LOGE("Invalid bundle name.");
1298 return ERR_ANS_INVALID_PARAM;
1299 }
1300
1301 if (!GetAnsManagerProxy()) {
1302 ANS_LOGE("GetEnabledForBundleSlot fail.");
1303 return ERR_ANS_SERVICE_NOT_CONNECTED;
1304 }
1305
1306 sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1307 return ansManagerProxy_->GetEnabledForBundleSlot(bo, slotType, enabled);
1308 }
1309
ShellDump(const std::string & cmd,const std::string & bundle,int32_t userId,std::vector<std::string> & dumpInfo)1310 ErrCode AnsNotification::ShellDump(const std::string &cmd, const std::string &bundle, int32_t userId,
1311 std::vector<std::string> &dumpInfo)
1312 {
1313 if (!GetAnsManagerProxy()) {
1314 ANS_LOGE("GetAnsManagerProxy fail.");
1315 return ERR_ANS_SERVICE_NOT_CONNECTED;
1316 }
1317
1318 return ansManagerProxy_->ShellDump(cmd, bundle, userId, dumpInfo);
1319 }
1320
SetSyncNotificationEnabledWithoutApp(const int32_t userId,const bool enabled)1321 ErrCode AnsNotification::SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled)
1322 {
1323 if (userId <= SUBSCRIBE_USER_INIT) {
1324 ANS_LOGE("Input userId is invalid.");
1325 return ERR_ANS_INVALID_PARAM;
1326 }
1327
1328 if (!GetAnsManagerProxy()) {
1329 ANS_LOGE("GetAnsManagerProxy fail.");
1330 return ERR_ANS_SERVICE_NOT_CONNECTED;
1331 }
1332
1333 return ansManagerProxy_->SetSyncNotificationEnabledWithoutApp(userId, enabled);
1334 }
1335
GetSyncNotificationEnabledWithoutApp(const int32_t userId,bool & enabled)1336 ErrCode AnsNotification::GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled)
1337 {
1338 if (userId <= SUBSCRIBE_USER_INIT) {
1339 ANS_LOGE("Input userId is invalid.");
1340 return ERR_ANS_INVALID_PARAM;
1341 }
1342
1343 if (!GetAnsManagerProxy()) {
1344 ANS_LOGE("GetAnsManagerProxy fail.");
1345 return ERR_ANS_SERVICE_NOT_CONNECTED;
1346 }
1347
1348 return ansManagerProxy_->GetSyncNotificationEnabledWithoutApp(userId, enabled);
1349 }
1350 } // namespace Notification
1351 } // namespace OHOS