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_request.h"
17
18 #include "ans_const_define.h"
19 #include "ans_inner_errors.h"
20 #include "ans_image_util.h"
21 #include "ans_log_wrapper.h"
22 #include "errors.h"
23 #include "notification_live_view_content.h"
24 #include "refbase.h"
25 #include "want_agent_helper.h"
26 #include "want_params_wrapper.h"
27 #include <memory>
28
29 namespace OHOS {
30 namespace Notification {
31 const std::string NotificationRequest::CLASSIFICATION_ALARM {"alarm"};
32 const std::string NotificationRequest::CLASSIFICATION_CALL {"call"};
33 const std::string NotificationRequest::CLASSIFICATION_EMAIL {"email"};
34 const std::string NotificationRequest::CLASSIFICATION_ERROR {"err"};
35 const std::string NotificationRequest::CLASSIFICATION_EVENT {"event"};
36 const std::string NotificationRequest::CLASSIFICATION_MESSAGE {"msg"};
37 const std::string NotificationRequest::CLASSIFICATION_NAVIGATION {"navigation"};
38 const std::string NotificationRequest::CLASSIFICATION_PROGRESS {"progress"};
39 const std::string NotificationRequest::CLASSIFICATION_PROMO {"promo"};
40 const std::string NotificationRequest::CLASSIFICATION_RECOMMENDATION {"recommendation"};
41 const std::string NotificationRequest::CLASSIFICATION_REMINDER {"reminder"};
42 const std::string NotificationRequest::CLASSIFICATION_SERVICE {"service"};
43 const std::string NotificationRequest::CLASSIFICATION_SOCIAL {"social"};
44 const std::string NotificationRequest::CLASSIFICATION_STATUS {"status"};
45 const std::string NotificationRequest::CLASSIFICATION_SYSTEM {"sys"};
46 const std::string NotificationRequest::CLASSIFICATION_TRANSPORT {"transport"};
47
48 const uint32_t NotificationRequest::COLOR_DEFAULT {0};
49
50 const uint32_t NotificationRequest::COLOR_MASK {0xFF000000};
51 const std::size_t NotificationRequest::MAX_USER_INPUT_HISTORY {5};
52 const std::size_t NotificationRequest::MAX_ACTION_BUTTONS {3};
53 const std::size_t NotificationRequest::MAX_MESSAGE_USERS {1000};
54
55 constexpr int32_t MAX_MAP_SIZE = 1000;
56
NotificationRequest(int32_t notificationId)57 NotificationRequest::NotificationRequest(int32_t notificationId) : notificationId_(notificationId)
58 {
59 createTime_ = GetNowSysTime();
60 deliveryTime_ = GetNowSysTime();
61 }
62
NotificationRequest(const NotificationRequest & other)63 NotificationRequest::NotificationRequest(const NotificationRequest &other)
64 {
65 CopyBase(other);
66 CopyOther(other);
67 }
68
operator =(const NotificationRequest & other)69 NotificationRequest &NotificationRequest::operator=(const NotificationRequest &other)
70 {
71 CopyBase(other);
72 CopyOther(other);
73
74 return *this;
75 }
76
~NotificationRequest()77 NotificationRequest::~NotificationRequest()
78 {}
79
IsInProgress() const80 bool NotificationRequest::IsInProgress() const
81 {
82 return inProgress_;
83 }
84
SetInProgress(bool isOngoing)85 void NotificationRequest::SetInProgress(bool isOngoing)
86 {
87 inProgress_ = isOngoing;
88 }
89
IsUnremovable() const90 bool NotificationRequest::IsUnremovable() const
91 {
92 return unremovable_;
93 }
94
SetUnremovable(bool isUnremovable)95 void NotificationRequest::SetUnremovable(bool isUnremovable)
96 {
97 unremovable_ = isUnremovable;
98 }
99
SetBadgeNumber(uint32_t number)100 void NotificationRequest::SetBadgeNumber(uint32_t number)
101 {
102 badgeNumber_ = number;
103 }
104
GetBadgeNumber() const105 uint32_t NotificationRequest::GetBadgeNumber() const
106 {
107 return badgeNumber_;
108 }
109
SetNotificationControlFlags(uint32_t notificationControlFlags)110 void NotificationRequest::SetNotificationControlFlags(uint32_t notificationControlFlags)
111 {
112 notificationControlFlags_ = notificationControlFlags;
113 }
114
GetNotificationControlFlags() const115 uint32_t NotificationRequest::GetNotificationControlFlags() const
116 {
117 return notificationControlFlags_;
118 }
119
SetNotificationId(int32_t notificationId)120 void NotificationRequest::SetNotificationId(int32_t notificationId)
121 {
122 notificationId_ = notificationId;
123 }
124
GetNotificationId() const125 int32_t NotificationRequest::GetNotificationId() const
126 {
127 return notificationId_;
128 }
129
SetWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> & wantAgent)130 void NotificationRequest::SetWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> &wantAgent)
131 {
132 wantAgent_ = wantAgent;
133 }
134
GetWantAgent() const135 const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> NotificationRequest::GetWantAgent() const
136 {
137 return wantAgent_;
138 }
139
SetRemovalWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> & wantAgent)140 void NotificationRequest::SetRemovalWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> &wantAgent)
141 {
142 removalWantAgent_ = wantAgent;
143 }
144
GetRemovalWantAgent() const145 const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> NotificationRequest::GetRemovalWantAgent() const
146 {
147 return removalWantAgent_;
148 }
149
SetMaxScreenWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> & wantAgent)150 void NotificationRequest::SetMaxScreenWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> &wantAgent)
151 {
152 maxScreenWantAgent_ = wantAgent;
153 }
154
GetMaxScreenWantAgent() const155 const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> NotificationRequest::GetMaxScreenWantAgent() const
156 {
157 return maxScreenWantAgent_;
158 }
159
SetAdditionalData(const std::shared_ptr<AAFwk::WantParams> & extras)160 void NotificationRequest::SetAdditionalData(const std::shared_ptr<AAFwk::WantParams> &extras)
161 {
162 additionalParams_ = extras;
163 }
164
GetAdditionalData() const165 const std::shared_ptr<AAFwk::WantParams> NotificationRequest::GetAdditionalData() const
166 {
167 return additionalParams_;
168 }
169
SetDeliveryTime(int64_t deliveryTime)170 void NotificationRequest::SetDeliveryTime(int64_t deliveryTime)
171 {
172 deliveryTime_ = deliveryTime;
173 }
174
GetDeliveryTime() const175 int64_t NotificationRequest::GetDeliveryTime() const
176 {
177 return deliveryTime_;
178 }
179
IsShowDeliveryTime() const180 bool NotificationRequest::IsShowDeliveryTime() const
181 {
182 return (deliveryTime_ != 0) && showDeliveryTime_;
183 }
184
SetShowDeliveryTime(bool showDeliveryTime)185 void NotificationRequest::SetShowDeliveryTime(bool showDeliveryTime)
186 {
187 showDeliveryTime_ = showDeliveryTime;
188 }
189
AddActionButton(const std::shared_ptr<NotificationActionButton> & actionButton)190 void NotificationRequest::AddActionButton(const std::shared_ptr<NotificationActionButton> &actionButton)
191 {
192 if (!actionButton) {
193 ANS_LOGW("actionButton can not be null");
194 return;
195 }
196
197 if (actionButtons_.size() >= NotificationRequest::MAX_ACTION_BUTTONS) {
198 ANS_LOGW("three action buttons have been already added");
199 return;
200 }
201
202 actionButtons_.emplace_back(actionButton);
203 }
204
GetActionButtons() const205 const std::vector<std::shared_ptr<NotificationActionButton>> NotificationRequest::GetActionButtons() const
206 {
207 return actionButtons_;
208 }
209
ClearActionButtons()210 void NotificationRequest::ClearActionButtons()
211 {
212 actionButtons_.clear();
213 }
214
IsPermitSystemGeneratedContextualActionButtons() const215 bool NotificationRequest::IsPermitSystemGeneratedContextualActionButtons() const
216 {
217 return permitted_;
218 }
219
SetPermitSystemGeneratedContextualActionButtons(bool permitted)220 void NotificationRequest::SetPermitSystemGeneratedContextualActionButtons(bool permitted)
221 {
222 permitted_ = permitted;
223 }
224
IsAgentNotification() const225 bool NotificationRequest::IsAgentNotification() const
226 {
227 return isAgent_;
228 }
229
SetIsAgentNotification(bool isAgent)230 void NotificationRequest::SetIsAgentNotification(bool isAgent)
231 {
232 isAgent_ = isAgent;
233 }
234
AddMessageUser(const std::shared_ptr<MessageUser> & messageUser)235 void NotificationRequest::AddMessageUser(const std::shared_ptr<MessageUser> &messageUser)
236 {
237 if (!messageUser) {
238 ANS_LOGI("messageUser can not be null");
239 return;
240 }
241
242 messageUsers_.emplace_back(messageUser);
243 }
244
GetMessageUsers() const245 const std::vector<std::shared_ptr<MessageUser>> NotificationRequest::GetMessageUsers() const
246 {
247 return messageUsers_;
248 }
249
IsAlertOneTime() const250 bool NotificationRequest::IsAlertOneTime() const
251 {
252 return alertOneTime_;
253 }
254
SetAlertOneTime(bool isAlertOnce)255 void NotificationRequest::SetAlertOneTime(bool isAlertOnce)
256 {
257 alertOneTime_ = isAlertOnce;
258 }
259
SetAutoDeletedTime(int64_t deletedTime)260 void NotificationRequest::SetAutoDeletedTime(int64_t deletedTime)
261 {
262 autoDeletedTime_ = deletedTime;
263 }
264
GetAutoDeletedTime() const265 int64_t NotificationRequest::GetAutoDeletedTime() const
266 {
267 return autoDeletedTime_;
268 }
269
SetUpdateDeadLine(int64_t updateDeadLine)270 void NotificationRequest::SetUpdateDeadLine(int64_t updateDeadLine)
271 {
272 updateDeadLine_ = updateDeadLine;
273 }
274
GetUpdateDeadLine() const275 int64_t NotificationRequest::GetUpdateDeadLine() const
276 {
277 return updateDeadLine_;
278 }
279
SetFinishDeadLine(int64_t finishDeadLine)280 void NotificationRequest::SetFinishDeadLine(int64_t finishDeadLine)
281 {
282 finishDeadLine_ = finishDeadLine;
283 }
284
GetFinishDeadLine() const285 int64_t NotificationRequest::GetFinishDeadLine() const
286 {
287 return finishDeadLine_;
288 }
289
SetArchiveDeadLine(int64_t archiveDeadLine)290 void NotificationRequest::SetArchiveDeadLine(int64_t archiveDeadLine)
291 {
292 archiveDeadLine_ = archiveDeadLine;
293 }
294
GetArchiveDeadLine() const295 int64_t NotificationRequest::GetArchiveDeadLine() const
296 {
297 return archiveDeadLine_;
298 }
299
SetLittleIcon(const std::shared_ptr<Media::PixelMap> & littleIcon)300 void NotificationRequest::SetLittleIcon(const std::shared_ptr<Media::PixelMap> &littleIcon)
301 {
302 littleIcon_ = littleIcon;
303 }
304
GetLittleIcon() const305 const std::shared_ptr<Media::PixelMap> NotificationRequest::GetLittleIcon() const
306 {
307 return littleIcon_;
308 }
309
SetBigIcon(const std::shared_ptr<Media::PixelMap> & bigIcon)310 void NotificationRequest::SetBigIcon(const std::shared_ptr<Media::PixelMap> &bigIcon)
311 {
312 bigIcon_ = bigIcon;
313 }
314
ResetBigIcon() const315 void NotificationRequest::ResetBigIcon() const
316 {
317 bigIcon_ = nullptr;
318 }
319
GetBigIcon() const320 const std::shared_ptr<Media::PixelMap> NotificationRequest::GetBigIcon() const
321 {
322 return bigIcon_;
323 }
324
SetOverlayIcon(const std::shared_ptr<Media::PixelMap> & overlayIcon)325 void NotificationRequest::SetOverlayIcon(const std::shared_ptr<Media::PixelMap> &overlayIcon)
326 {
327 overlayIcon_ = overlayIcon;
328 }
329
GetOverlayIcon() const330 const std::shared_ptr<Media::PixelMap> NotificationRequest::GetOverlayIcon() const
331 {
332 return overlayIcon_;
333 }
334
SetClassification(const std::string & classification)335 void NotificationRequest::SetClassification(const std::string &classification)
336 {
337 classification_ = classification;
338 }
339
GetClassification() const340 std::string NotificationRequest::GetClassification() const
341 {
342 return classification_;
343 }
344
SetColor(uint32_t color)345 void NotificationRequest::SetColor(uint32_t color)
346 {
347 color_ = color;
348 if (NotificationRequest::COLOR_DEFAULT != color_) {
349 color_ = color_ | NotificationRequest::COLOR_MASK;
350 }
351 }
352
GetColor() const353 uint32_t NotificationRequest::GetColor() const
354 {
355 return color_;
356 }
357
IsColorEnabled() const358 bool NotificationRequest::IsColorEnabled() const
359 {
360 if (!colorEnabled_) {
361 return false;
362 }
363
364 // no valid content
365 if (!notificationContent_) {
366 ANS_LOGI("no valid notification content");
367 return false;
368 }
369
370 // not a media content
371 if (NotificationContent::Type::MEDIA != notificationContentType_) {
372 ANS_LOGI("not a media notification content");
373 return false;
374 }
375
376 auto basicContent = notificationContent_->GetNotificationContent();
377 auto mediaContent = std::static_pointer_cast<NotificationMediaContent>(basicContent);
378 if (!mediaContent->GetAVToken()) {
379 ANS_LOGI("AVToken has not been attached");
380 return false;
381 }
382
383 return true;
384 }
385
SetColorEnabled(bool colorEnabled)386 void NotificationRequest::SetColorEnabled(bool colorEnabled)
387 {
388 colorEnabled_ = colorEnabled;
389 }
390
SetContent(const std::shared_ptr<NotificationContent> & content)391 void NotificationRequest::SetContent(const std::shared_ptr<NotificationContent> &content)
392 {
393 notificationContent_ = content;
394
395 if (notificationContent_) {
396 notificationContentType_ = notificationContent_->GetContentType();
397 return;
398 }
399
400 notificationContentType_ = NotificationContent::Type::NONE;
401 }
402
GetContent() const403 const std::shared_ptr<NotificationContent> NotificationRequest::GetContent() const
404 {
405 return notificationContent_;
406 }
407
GetNotificationType() const408 NotificationContent::Type NotificationRequest::GetNotificationType() const
409 {
410 return notificationContentType_;
411 }
412
IsCountdownTimer() const413 bool NotificationRequest::IsCountdownTimer() const
414 {
415 return isCountdown_;
416 }
417
SetCountdownTimer(bool isCountDown)418 void NotificationRequest::SetCountdownTimer(bool isCountDown)
419 {
420 isCountdown_ = isCountDown;
421 }
422
SetGroupAlertType(NotificationRequest::GroupAlertType type)423 void NotificationRequest::SetGroupAlertType(NotificationRequest::GroupAlertType type)
424 {
425 groupAlertType_ = type;
426 }
427
GetGroupAlertType() const428 NotificationRequest::GroupAlertType NotificationRequest::GetGroupAlertType() const
429 {
430 return groupAlertType_;
431 }
432
IsGroupOverview() const433 bool NotificationRequest::IsGroupOverview() const
434 {
435 return groupOverview_;
436 }
437
SetGroupOverview(bool overView)438 void NotificationRequest::SetGroupOverview(bool overView)
439 {
440 groupOverview_ = overView;
441 }
442
SetGroupName(const std::string & groupName)443 void NotificationRequest::SetGroupName(const std::string &groupName)
444 {
445 groupName_ = groupName;
446 }
447
GetGroupName() const448 std::string NotificationRequest::GetGroupName() const
449 {
450 return groupName_;
451 }
452
IsOnlyLocal() const453 bool NotificationRequest::IsOnlyLocal() const
454 {
455 return onlyLocal_;
456 }
457
SetOnlyLocal(bool flag)458 void NotificationRequest::SetOnlyLocal(bool flag)
459 {
460 onlyLocal_ = flag;
461 }
462
SetSettingsText(const std::string & text)463 void NotificationRequest::SetSettingsText(const std::string &text)
464 {
465 if ((NotificationContent::Type::LONG_TEXT == notificationContentType_) ||
466 (NotificationContent::Type::PICTURE == notificationContentType_)) {
467 ANS_LOGW("This method is invalid if the notification content type has been set to LONG_TEXT or PICTURE.");
468 return;
469 }
470
471 settingsText_ = text;
472 }
473
GetSettingsText() const474 std::string NotificationRequest::GetSettingsText() const
475 {
476 return settingsText_;
477 }
478
GetCreateTime() const479 int64_t NotificationRequest::GetCreateTime() const
480 {
481 return createTime_;
482 }
483
SetCreateTime(int64_t createTime)484 void NotificationRequest::SetCreateTime(int64_t createTime)
485 {
486 createTime_ = createTime;
487 }
488
IsShowStopwatch() const489 bool NotificationRequest::IsShowStopwatch() const
490 {
491 return showStopwatch_;
492 }
493
SetShowStopwatch(bool isShow)494 void NotificationRequest::SetShowStopwatch(bool isShow)
495 {
496 showStopwatch_ = isShow;
497 }
498
SetSlotType(NotificationConstant::SlotType slotType)499 void NotificationRequest::SetSlotType(NotificationConstant::SlotType slotType)
500 {
501 slotType_ = slotType;
502 }
503
GetSlotType() const504 NotificationConstant::SlotType NotificationRequest::GetSlotType() const
505 {
506 return slotType_;
507 }
508
SetSortingKey(const std::string & key)509 void NotificationRequest::SetSortingKey(const std::string &key)
510 {
511 sortingKey_ = key;
512 }
513
GetSortingKey() const514 std::string NotificationRequest::GetSortingKey() const
515 {
516 return sortingKey_;
517 }
518
SetStatusBarText(const std::string & text)519 void NotificationRequest::SetStatusBarText(const std::string &text)
520 {
521 statusBarText_ = text;
522 }
523
GetStatusBarText() const524 std::string NotificationRequest::GetStatusBarText() const
525 {
526 return statusBarText_;
527 }
528
IsTapDismissed() const529 bool NotificationRequest::IsTapDismissed() const
530 {
531 return tapDismissed_;
532 }
533
SetTapDismissed(bool isDismissed)534 void NotificationRequest::SetTapDismissed(bool isDismissed)
535 {
536 tapDismissed_ = isDismissed;
537 }
538
SetVisibleness(NotificationConstant::VisiblenessType type)539 void NotificationRequest::SetVisibleness(NotificationConstant::VisiblenessType type)
540 {
541 visiblenessType_ = type;
542 }
543
GetVisibleness() const544 NotificationConstant::VisiblenessType NotificationRequest::GetVisibleness() const
545 {
546 return visiblenessType_;
547 }
548
SetBadgeIconStyle(NotificationRequest::BadgeStyle style)549 void NotificationRequest::SetBadgeIconStyle(NotificationRequest::BadgeStyle style)
550 {
551 badgeStyle_ = style;
552 }
553
GetBadgeIconStyle() const554 NotificationRequest::BadgeStyle NotificationRequest::GetBadgeIconStyle() const
555 {
556 return badgeStyle_;
557 }
558
SetShortcutId(const std::string & shortcutId)559 void NotificationRequest::SetShortcutId(const std::string &shortcutId)
560 {
561 shortcutId_ = shortcutId;
562 }
563
GetShortcutId() const564 std::string NotificationRequest::GetShortcutId() const
565 {
566 return shortcutId_;
567 }
568
SetFloatingIcon(bool floatingIcon)569 void NotificationRequest::SetFloatingIcon(bool floatingIcon)
570 {
571 floatingIcon_ = floatingIcon;
572 }
573
IsFloatingIcon() const574 bool NotificationRequest::IsFloatingIcon() const
575 {
576 return floatingIcon_;
577 }
578
SetProgressBar(int32_t progress,int32_t progressMax,bool indeterminate)579 void NotificationRequest::SetProgressBar(int32_t progress, int32_t progressMax, bool indeterminate)
580 {
581 progressValue_ = progress;
582 progressMax_ = progressMax;
583 progressIndeterminate_ = indeterminate;
584 }
585
GetProgressMax() const586 int32_t NotificationRequest::GetProgressMax() const
587 {
588 return progressMax_;
589 }
590
GetProgressValue() const591 int32_t NotificationRequest::GetProgressValue() const
592 {
593 return progressValue_;
594 }
595
IsProgressIndeterminate() const596 bool NotificationRequest::IsProgressIndeterminate() const
597 {
598 return progressIndeterminate_;
599 }
600
SetNotificationUserInputHistory(const std::vector<std::string> & text)601 void NotificationRequest::SetNotificationUserInputHistory(const std::vector<std::string> &text)
602 {
603 if (text.empty()) {
604 userInputHistory_.clear();
605 return;
606 }
607
608 auto vsize = std::min(NotificationRequest::MAX_USER_INPUT_HISTORY, text.size());
609 userInputHistory_.assign(text.begin(), text.begin() + vsize);
610 }
611
GetNotificationUserInputHistory() const612 std::vector<std::string> NotificationRequest::GetNotificationUserInputHistory() const
613 {
614 return userInputHistory_;
615 }
616
GetNotificationHashCode() const617 std::string NotificationRequest::GetNotificationHashCode() const
618 {
619 if (creatorBundleName_.empty() || (creatorUid_ == 0) || ownerBundleName_.empty()) {
620 return "";
621 }
622
623 return std::to_string(notificationId_) + "_" + creatorBundleName_ + "_" + std::to_string(creatorUid_) + "_" +
624 ownerBundleName_;
625 }
626
SetOwnerBundleName(const std::string & ownerName)627 void NotificationRequest::SetOwnerBundleName(const std::string &ownerName)
628 {
629 ownerBundleName_ = ownerName;
630 }
631
GetOwnerBundleName() const632 std::string NotificationRequest::GetOwnerBundleName() const
633 {
634 return ownerBundleName_;
635 }
636
SetCreatorBundleName(const std::string & creatorName)637 void NotificationRequest::SetCreatorBundleName(const std::string &creatorName)
638 {
639 creatorBundleName_ = creatorName;
640 }
641
GetCreatorBundleName() const642 std::string NotificationRequest::GetCreatorBundleName() const
643 {
644 return creatorBundleName_;
645 }
646
SetCreatorPid(pid_t pid)647 void NotificationRequest::SetCreatorPid(pid_t pid)
648 {
649 creatorPid_ = pid;
650 }
651
GetCreatorPid() const652 pid_t NotificationRequest::GetCreatorPid() const
653 {
654 return creatorPid_;
655 }
656
SetCreatorUid(int32_t uid)657 void NotificationRequest::SetCreatorUid(int32_t uid)
658 {
659 creatorUid_ = uid;
660 }
661
GetCreatorUid() const662 int32_t NotificationRequest::GetCreatorUid() const
663 {
664 return creatorUid_;
665 }
666
SetOwnerUid(int32_t uid)667 void NotificationRequest::SetOwnerUid(int32_t uid)
668 {
669 ownerUid_ = uid;
670 }
671
GetOwnerUid() const672 int32_t NotificationRequest::GetOwnerUid() const
673 {
674 return ownerUid_;
675 }
676
SetLabel(const std::string & label)677 void NotificationRequest::SetLabel(const std::string &label)
678 {
679 label_ = label;
680 }
681
GetLabel() const682 std::string NotificationRequest::GetLabel() const
683 {
684 return label_;
685 }
686
SetDistributed(bool distribute)687 void NotificationRequest::SetDistributed(bool distribute)
688 {
689 distributedOptions_.SetDistributed(distribute);
690 }
691
SetDevicesSupportDisplay(const std::vector<std::string> & devices)692 void NotificationRequest::SetDevicesSupportDisplay(const std::vector<std::string> &devices)
693 {
694 distributedOptions_.SetDevicesSupportDisplay(devices);
695 }
696
SetDevicesSupportOperate(const std::vector<std::string> & devices)697 void NotificationRequest::SetDevicesSupportOperate(const std::vector<std::string> &devices)
698 {
699 distributedOptions_.SetDevicesSupportOperate(devices);
700 }
701
GetNotificationDistributedOptions() const702 NotificationDistributedOptions NotificationRequest::GetNotificationDistributedOptions() const
703 {
704 return distributedOptions_;
705 }
706
SetCreatorUserId(int32_t userId)707 void NotificationRequest::SetCreatorUserId(int32_t userId)
708 {
709 creatorUserId_ = userId;
710 }
711
GetCreatorUserId() const712 int32_t NotificationRequest::GetCreatorUserId() const
713 {
714 return creatorUserId_;
715 }
716
SetCreatorInstanceKey(int32_t key)717 void NotificationRequest::SetCreatorInstanceKey(int32_t key)
718 {
719 creatorInstanceKey_ = key;
720 }
721
GetCreatorInstanceKey() const722 int32_t NotificationRequest::GetCreatorInstanceKey() const
723 {
724 return creatorInstanceKey_;
725 }
726
SetOwnerUserId(int32_t userId)727 void NotificationRequest::SetOwnerUserId(int32_t userId)
728 {
729 ownerUserId_ = userId;
730 }
731
GetOwnerUserId() const732 int32_t NotificationRequest::GetOwnerUserId() const
733 {
734 return ownerUserId_;
735 }
736
Dump()737 std::string NotificationRequest::Dump()
738 {
739 return "NotificationRequest{ "
740 "notificationId = " + std::to_string(notificationId_) +
741 ", slotType = " + std::to_string(static_cast<int32_t>(slotType_)) +
742 ", createTime = " + std::to_string(createTime_) + ", deliveryTime = " + std::to_string(deliveryTime_) +
743 ", autoDeletedTime = " + std::to_string(autoDeletedTime_) + ", settingsText = " + settingsText_ +
744 ", creatorBundleName = " + creatorBundleName_ +
745 ", creatorPid = " + std::to_string(static_cast<int32_t>(creatorPid_)) +
746 ", creatorUid = " + std::to_string(static_cast<int32_t>(creatorUid_)) +
747 ", ownerBundleName = " + ownerBundleName_ +
748 ", ownerUid = " + std::to_string(static_cast<int32_t>(ownerUid_)) +
749 ", groupName = " + groupName_ + ", statusBarText = " + statusBarText_ + ", label = " + label_ +
750 ", shortcutId = " + shortcutId_ + ", sortingKey = " + sortingKey_ +
751 ", groupAlertType = " + std::to_string(static_cast<int32_t>(groupAlertType_)) +
752 ", color = " + std::to_string(color_) + ", badgeNumber = " + std::to_string(badgeNumber_) +
753 ", visiblenessType = " + std::to_string(static_cast<int32_t>(visiblenessType_)) +
754 ", progressValue = " + std::to_string(progressValue_) + ", progressMax = " + std::to_string(progressMax_) +
755 ", badgeStyle = " + std::to_string(static_cast<int32_t>(badgeStyle_)) +
756 ", classification = " + classification_ +
757 ", notificationContentType = " + std::to_string(static_cast<int32_t>(notificationContentType_)) +
758 ", notificationControlFlags = " + std::to_string(notificationControlFlags_) +
759 ", showDeliveryTime = " + (showDeliveryTime_ ? "true" : "false") +
760 ", tapDismissed = " + (tapDismissed_ ? "true" : "false") +
761 ", colorEnabled = " + (colorEnabled_ ? "true" : "false") +
762 ", alertOneTime = " + (alertOneTime_ ? "true" : "false") +
763 ", showStopwatch = " + (showStopwatch_ ? "true" : "false") +
764 ", isCountdown = " + (isCountdown_ ? "true" : "false") +
765 ", inProgress = " + (inProgress_ ? "true" : "false") +
766 ", groupOverview = " + (groupOverview_ ? "true" : "false") +
767 ", isRemoveAllowed = " + (isRemoveAllowed_ ? "true" : "false") +
768 ", progressIndeterminate = " + (progressIndeterminate_ ? "true" : "false") +
769 ", unremovable = " + (unremovable_ ? "true" : "false") +
770 ", floatingIcon = " + (floatingIcon_ ? "true" : "false") +
771 ", onlyLocal = " + (onlyLocal_ ? "true" : "false") + ", permitted = " + (permitted_ ? "true" : "false") +
772 ", isAgent = " + (isAgent_ ? "true" : "false") +
773 ", removalWantAgent = " + (removalWantAgent_ ? "not null" : "null") +
774 ", maxScreenWantAgent = " + (maxScreenWantAgent_ ? "not null" : "null") +
775 ", additionalParams = " + (additionalParams_ ? "not null" : "null") +
776 ", littleIcon = " + (littleIcon_ ? "not null" : "null") +
777 ", bigIcon = " + (bigIcon_ ? "not null" : "null") +
778 ", overlayIcon = " + (overlayIcon_ ? "not null" : "null") +
779 ", notificationContent = " + (notificationContent_ ? notificationContent_->Dump() : "null") +
780 ", notificationTemplate = " + (notificationTemplate_ ? "not null" : "null") +
781 ", actionButtons = " + (!actionButtons_.empty() ? actionButtons_.at(0)->Dump() : "empty") +
782 ", messageUsers = " + (!messageUsers_.empty() ? messageUsers_.at(0)->Dump() : "empty") +
783 ", userInputHistory = " + (!userInputHistory_.empty() ? userInputHistory_.at(0) : "empty") +
784 ", distributedOptions = " + distributedOptions_.Dump() +
785 ", notificationFlags = " + (notificationFlags_ ? "not null" : "null") +
786 ", notificationFlagsOfDevices = " + (notificationFlagsOfDevices_ ? "not null" : "null") +
787 ", notificationBundleOption = " + (notificationBundleOption_ != nullptr ? "not null" : "null") +
788 ", agentBundle = " + (agentBundle_ != nullptr ? "not null" : "null") +
789 ", creatorUserId = " + std::to_string(creatorUserId_) + ", ownerUserId = " + std::to_string(ownerUserId_) +
790 ", receiverUserId = " + std::to_string(receiverUserId_) + ", updateDeadLine = " +
791 std::to_string(updateDeadLine_) + ", finishDeadLine = " + std::to_string(finishDeadLine_) +
792 ", sound = " + sound_ + ", unifiedGroupInfo_ = " +
793 (unifiedGroupInfo_ ? unifiedGroupInfo_->Dump() : "null")+ " }";
794 }
795
ToJson(nlohmann::json & jsonObject) const796 bool NotificationRequest::ToJson(nlohmann::json &jsonObject) const
797 {
798 jsonObject["version"] = 1;
799
800 jsonObject["id"] = notificationId_;
801 jsonObject["color"] = color_;
802 jsonObject["deliveryTime"] = deliveryTime_;
803 jsonObject["autoDeletedTime"] = autoDeletedTime_;
804
805 jsonObject["creatorBundleName"] = creatorBundleName_;
806 jsonObject["ownerBundleName"] = ownerBundleName_;
807 jsonObject["groupName"] = groupName_;
808 jsonObject["label"] = label_;
809 jsonObject["classification"] = classification_;
810
811 jsonObject["slotType"] = static_cast<int32_t>(slotType_);
812 jsonObject["notificationSlotType"] = static_cast<int32_t>(slotType_);
813 jsonObject["badgeIconStyle"] = static_cast<int32_t>(badgeStyle_);
814 jsonObject["notificationContentType"] = static_cast<int32_t>(notificationContentType_);
815
816 jsonObject["showDeliveryTime"] = showDeliveryTime_;
817 jsonObject["tapDismissed"] = tapDismissed_;
818 jsonObject["colorEnabled"] = colorEnabled_;
819 jsonObject["isOngoing"] = inProgress_;
820 jsonObject["isAlertOnce"] = alertOneTime_;
821 jsonObject["isStopwatch"] = showStopwatch_;
822 jsonObject["isCountdown"] = isCountdown_;
823 jsonObject["isUnremovable"] = unremovable_;
824 jsonObject["isAgent"] = isAgent_;
825 jsonObject["isFloatingIcon"] = floatingIcon_;
826
827 jsonObject["creatorBundleName"] = creatorBundleName_;
828 jsonObject["creatorUid"] = creatorUid_;
829 jsonObject["creatorPid"] = creatorPid_;
830 jsonObject["creatorUserId"] = creatorUserId_;
831 jsonObject["ownerUserId"] = ownerUserId_;
832 jsonObject["ownerUid"] = ownerUid_;
833 jsonObject["receiverUserId"] = receiverUserId_;
834 jsonObject["creatorInstanceKey"] = creatorInstanceKey_;
835 jsonObject["notificationControlFlags"] = notificationControlFlags_;
836 jsonObject["updateDeadLine"] = updateDeadLine_;
837 jsonObject["finishDeadLine"] = finishDeadLine_;
838
839 if (!ConvertObjectsToJson(jsonObject)) {
840 ANS_LOGE("Cannot convert objects to JSON");
841 return false;
842 }
843
844 return true;
845 }
846
FromJson(const nlohmann::json & jsonObject)847 NotificationRequest *NotificationRequest::FromJson(const nlohmann::json &jsonObject)
848 {
849 if (jsonObject.is_null() or !jsonObject.is_object()) {
850 ANS_LOGE("Invalid JSON object");
851 return nullptr;
852 }
853
854 auto pRequest = new (std::nothrow) NotificationRequest();
855 if (pRequest == nullptr) {
856 ANS_LOGE("Failed to create request instance");
857 return nullptr;
858 }
859
860 const auto &jsonEnd = jsonObject.cend();
861 if (jsonObject.find("version") != jsonEnd && jsonObject.at("version").is_number_integer()) {
862 jsonObject.at("version").get<int32_t>();
863 }
864
865 ConvertJsonToNum(pRequest, jsonObject);
866
867 ConvertJsonToString(pRequest, jsonObject);
868
869 ConvertJsonToEnum(pRequest, jsonObject);
870
871 ConvertJsonToBool(pRequest, jsonObject);
872
873 if (jsonObject.find("wantAgent") != jsonEnd && jsonObject.at("wantAgent").is_string()) {
874 auto wantAgentValue = jsonObject.at("wantAgent").get<std::string>();
875 int32_t targetUid = -1;
876 if (pRequest->GetOwnerUid() != DEFAULT_UID) {
877 targetUid = pRequest->GetOwnerUid();
878 }
879 ANS_LOGI("wantAgent Fromjson, uid = %{public}d ", targetUid);
880 pRequest->wantAgent_ = AbilityRuntime::WantAgent::WantAgentHelper::FromString(wantAgentValue, targetUid);
881 }
882
883 if (!ConvertJsonToNotificationContent(pRequest, jsonObject)) {
884 delete pRequest;
885 pRequest = nullptr;
886 return nullptr;
887 }
888
889 if (!ConvertJsonToNotificationActionButton(pRequest, jsonObject)) {
890 delete pRequest;
891 pRequest = nullptr;
892 return nullptr;
893 }
894
895 if (jsonObject.find("extraInfo") != jsonEnd && jsonObject.at("extraInfo").is_string()) {
896 auto extraInfoStr = jsonObject.at("extraInfo").get<std::string>();
897 if (!extraInfoStr.empty()) {
898 AAFwk::WantParams params = AAFwk::WantParamWrapper::ParseWantParams(extraInfoStr);
899 pRequest->additionalParams_ = std::make_shared<AAFwk::WantParams>(params);
900 }
901 }
902
903 ConvertJsonToPixelMap(pRequest, jsonObject);
904
905 if (!ConvertJsonToNotificationDistributedOptions(pRequest, jsonObject)) {
906 delete pRequest;
907 pRequest = nullptr;
908 return nullptr;
909 }
910
911 if (!ConvertJsonToNotificationFlags(pRequest, jsonObject)) {
912 delete pRequest;
913 pRequest = nullptr;
914 return nullptr;
915 }
916
917 if (!ConvertJsonToNotificationBundleOption(pRequest, jsonObject)) {
918 delete pRequest;
919 pRequest = nullptr;
920 return nullptr;
921 }
922
923 ConvertJsonToAgentBundle(pRequest, jsonObject);
924
925 return pRequest;
926 }
927
Marshalling(Parcel & parcel) const928 bool NotificationRequest::Marshalling(Parcel &parcel) const
929 {
930 // write int
931 if (!parcel.WriteInt32(notificationId_)) {
932 ANS_LOGE("Failed to write notification Id");
933 return false;
934 }
935
936 if (!parcel.WriteUint32(color_)) {
937 ANS_LOGE("Failed to write color");
938 return false;
939 }
940
941 if (!parcel.WriteUint32(badgeNumber_)) {
942 ANS_LOGE("Failed to write badge number");
943 return false;
944 }
945
946 if (!parcel.WriteInt32(progressValue_)) {
947 ANS_LOGE("Failed to write progress value");
948 return false;
949 }
950
951 if (!parcel.WriteInt32(progressMax_)) {
952 ANS_LOGE("Failed to write progress max");
953 return false;
954 }
955
956 if (!parcel.WriteInt64(createTime_)) {
957 ANS_LOGE("Failed to write create time");
958 return false;
959 }
960
961 if (!parcel.WriteInt64(deliveryTime_)) {
962 ANS_LOGE("Failed to write delivery time");
963 return false;
964 }
965
966 if (!parcel.WriteInt64(autoDeletedTime_)) {
967 ANS_LOGE("Failed to write auto deleted time");
968 return false;
969 }
970
971 if (!parcel.WriteInt32(static_cast<int32_t>(creatorPid_))) {
972 ANS_LOGE("Failed to write creator pid");
973 return false;
974 }
975
976 if (!parcel.WriteInt32(static_cast<int32_t>(creatorUid_))) {
977 ANS_LOGE("Failed to write creator uid");
978 return false;
979 }
980
981 if (!parcel.WriteInt32(static_cast<int32_t>(ownerUid_))) {
982 ANS_LOGE("Failed to write owner uid");
983 return false;
984 }
985
986 if (!parcel.WriteInt32(static_cast<int32_t>(creatorUserId_))) {
987 ANS_LOGE("Failed to write creator userId");
988 return false;
989 }
990
991 if (!parcel.WriteInt32(static_cast<int32_t>(ownerUserId_))) {
992 ANS_LOGE("Failed to write owner userId");
993 return false;
994 }
995
996 if (!parcel.WriteInt32(static_cast<int32_t>(receiverUserId_))) {
997 ANS_LOGE("Failed to write receiver userId");
998 return false;
999 }
1000
1001 if (!parcel.WriteInt32(static_cast<int32_t>(creatorInstanceKey_))) {
1002 ANS_LOGE("Failed to write creator instance key");
1003 return false;
1004 }
1005
1006 if (!parcel.WriteUint32(notificationControlFlags_)) {
1007 ANS_LOGE("Failed to write notification control flags.");
1008 return false;
1009 }
1010
1011 if (!parcel.WriteUint32(publishDelayTime_)) {
1012 ANS_LOGE("Failed to write publish delay time");
1013 return false;
1014 }
1015
1016 // write std::string
1017 if (!parcel.WriteString(settingsText_)) {
1018 ANS_LOGE("Failed to write settings text");
1019 return false;
1020 }
1021
1022 if (!parcel.WriteString(creatorBundleName_)) {
1023 ANS_LOGE("Failed to write creator bundle name");
1024 return false;
1025 }
1026
1027 if (!parcel.WriteString(ownerBundleName_)) {
1028 ANS_LOGE("Failed to write owner bundle name");
1029 return false;
1030 }
1031
1032 if (!parcel.WriteString(groupName_)) {
1033 ANS_LOGE("Failed to write group name");
1034 return false;
1035 }
1036
1037 if (!parcel.WriteString(statusBarText_)) {
1038 ANS_LOGE("Failed to write status bar text");
1039 return false;
1040 }
1041
1042 if (!parcel.WriteString(label_)) {
1043 ANS_LOGE("Failed to write label");
1044 return false;
1045 }
1046
1047 if (!parcel.WriteString(shortcutId_)) {
1048 ANS_LOGE("Failed to write shortcut Id");
1049 return false;
1050 }
1051
1052 if (!parcel.WriteString(sortingKey_)) {
1053 ANS_LOGE("Failed to write sorting key");
1054 return false;
1055 }
1056
1057 if (!parcel.WriteString(classification_)) {
1058 ANS_LOGE("Failed to write classification");
1059 return false;
1060 }
1061
1062 if (!parcel.WriteString(appMessageId_)) {
1063 ANS_LOGE("Failed to write appMessageId");
1064 return false;
1065 }
1066
1067 if (!parcel.WriteString(sound_)) {
1068 ANS_LOGE("Failed to write sound");
1069 return false;
1070 }
1071
1072 // write enum
1073 if (!parcel.WriteInt32(static_cast<int32_t>(slotType_))) {
1074 ANS_LOGE("Failed to write slot type");
1075 return false;
1076 }
1077
1078 if (!parcel.WriteInt32(static_cast<int32_t>(groupAlertType_))) {
1079 ANS_LOGE("Failed to write group alert type");
1080 return false;
1081 }
1082
1083 if (!parcel.WriteInt32(static_cast<int32_t>(visiblenessType_))) {
1084 ANS_LOGE("Failed to write visibleness type");
1085 return false;
1086 }
1087
1088 if (!parcel.WriteInt32(static_cast<int32_t>(badgeStyle_))) {
1089 ANS_LOGE("Failed to write badge type");
1090 return false;
1091 }
1092
1093 if (!parcel.WriteInt32(static_cast<int32_t>(notificationContentType_))) {
1094 ANS_LOGE("Failed to write notification content type");
1095 return false;
1096 }
1097
1098 // write bool
1099 if (!parcel.WriteBool(showDeliveryTime_)) {
1100 ANS_LOGE("Failed to write flag indicating whether to show delivery time");
1101 return false;
1102 }
1103
1104 if (!parcel.WriteBool(tapDismissed_)) {
1105 ANS_LOGE("Failed to write flag tap dismissed");
1106 return false;
1107 }
1108
1109 if (!parcel.WriteBool(colorEnabled_)) {
1110 ANS_LOGE("Failed to write flag indicating whether to enable background color");
1111 return false;
1112 }
1113
1114 if (!parcel.WriteBool(alertOneTime_)) {
1115 ANS_LOGE("Failed to write flag indicating whether to have this notification alert only once");
1116 return false;
1117 }
1118
1119 if (!parcel.WriteBool(showStopwatch_)) {
1120 ANS_LOGE("Failed to write flag show stop watch");
1121 return false;
1122 }
1123
1124 if (!parcel.WriteBool(isCountdown_)) {
1125 ANS_LOGE("Failed to write flag indicating whether to show the notification creation time as a countdown timer");
1126 return false;
1127 }
1128
1129 if (!parcel.WriteBool(inProgress_)) {
1130 ANS_LOGE("Failed to write flag indicating whether in progress");
1131 return false;
1132 }
1133
1134 if (!parcel.WriteBool(groupOverview_)) {
1135 ANS_LOGE("Failed to write flag indicating whether to use this notification as the overview of its group");
1136 return false;
1137 }
1138
1139 if (!parcel.WriteBool(progressIndeterminate_)) {
1140 ANS_LOGE("Failed to write progress indeterminate");
1141 return false;
1142 }
1143
1144 if (!parcel.WriteBool(unremovable_)) {
1145 ANS_LOGE("Failed to write flag indicating whether unremovable");
1146 return false;
1147 }
1148
1149 if (!parcel.WriteBool(floatingIcon_)) {
1150 ANS_LOGE("Failed to write flag floating icon");
1151 return false;
1152 }
1153
1154 if (!parcel.WriteBool(onlyLocal_)) {
1155 ANS_LOGE("Failed to write flag only local");
1156 return false;
1157 }
1158
1159 if (!parcel.WriteBool(permitted_)) {
1160 ANS_LOGE("Failed to write flag indicating whether to allow the platform to \
1161 generate contextual NotificationActionButton objects");
1162 return false;
1163 }
1164
1165 if (!parcel.WriteBool(isAgent_)) {
1166 ANS_LOGE("Failed to write flag indicating whether an agent notification");
1167 return false;
1168 }
1169
1170 if (!parcel.WriteBool(isRemoveAllowed_)) {
1171 ANS_LOGE("Failed to write flag isRemoveAllowed");
1172 return false;
1173 }
1174
1175 // write objects which managed by std::shared_ptr
1176 bool valid {false};
1177
1178 valid = wantAgent_ ? true : false;
1179 if (!parcel.WriteBool(valid)) {
1180 ANS_LOGE("Failed to write the flag which indicate whether wantAgent is null");
1181 return false;
1182 }
1183
1184 if (valid) {
1185 if (!parcel.WriteParcelable(wantAgent_.get())) {
1186 ANS_LOGE("Failed to write wantAgent");
1187 return false;
1188 }
1189 }
1190
1191 valid = removalWantAgent_ ? true : false;
1192 if (!parcel.WriteBool(valid)) {
1193 ANS_LOGE("Failed to write the flag which indicate whether removalWantAgent is null");
1194 return false;
1195 }
1196
1197 if (valid) {
1198 if (!parcel.WriteParcelable(removalWantAgent_.get())) {
1199 ANS_LOGE("Failed to write removalWantAgent");
1200 return false;
1201 }
1202 }
1203
1204 valid = maxScreenWantAgent_ ? true : false;
1205 if (!parcel.WriteBool(valid)) {
1206 ANS_LOGE("Failed to write the flag which indicate whether maxScreenWantAgent is null");
1207 return false;
1208 }
1209
1210 if (valid) {
1211 if (!parcel.WriteParcelable(maxScreenWantAgent_.get())) {
1212 ANS_LOGE("Failed to write maxScreenWantAgent");
1213 return false;
1214 }
1215 }
1216
1217 valid = additionalParams_ ? true : false;
1218 if (!parcel.WriteBool(valid)) {
1219 ANS_LOGE("Failed to write the flag which indicate whether additionalParams is null");
1220 return false;
1221 }
1222
1223 if (valid) {
1224 if (!parcel.WriteParcelable(additionalParams_.get())) {
1225 ANS_LOGE("Failed to write additionalParams");
1226 return false;
1227 }
1228 }
1229
1230 valid = littleIcon_ ? true : false;
1231 if (!parcel.WriteBool(valid)) {
1232 ANS_LOGE("Failed to write the flag which indicate whether littleIcon is null");
1233 return false;
1234 }
1235
1236 if (valid) {
1237 if (!parcel.WriteParcelable(littleIcon_.get())) {
1238 ANS_LOGE("Failed to write littleIcon");
1239 return false;
1240 }
1241 }
1242
1243 valid = bigIcon_ ? true : false;
1244 if (!parcel.WriteBool(valid)) {
1245 ANS_LOGE("Failed to write the flag which indicate whether bigIcon is null");
1246 return false;
1247 }
1248
1249 if (valid) {
1250 if (!parcel.WriteParcelable(bigIcon_.get())) {
1251 ANS_LOGE("Failed to write bigIcon");
1252 return false;
1253 }
1254 }
1255
1256 valid = overlayIcon_ ? true : false;
1257 if (!parcel.WriteBool(valid)) {
1258 ANS_LOGE("Failed to write the flag which indicate whether overlayIcon is null");
1259 return false;
1260 }
1261
1262 if (valid) {
1263 if (!parcel.WriteParcelable(overlayIcon_.get())) {
1264 ANS_LOGE("Failed to write overlayIcon");
1265 return false;
1266 }
1267 }
1268
1269 valid = notificationContent_ ? true : false;
1270 if (!parcel.WriteBool(valid)) {
1271 ANS_LOGE("Failed to write the flag which indicate whether notificationContent is null");
1272 return false;
1273 }
1274
1275 if (valid) {
1276 if (!parcel.WriteParcelable(notificationContent_.get())) {
1277 ANS_LOGE("Failed to write notificationContent");
1278 return false;
1279 }
1280 }
1281
1282 // write std::vector
1283 if (!parcel.WriteUint64(actionButtons_.size())) {
1284 ANS_LOGE("Failed to write the size of actionButtons");
1285 return false;
1286 }
1287
1288 for (auto it = actionButtons_.begin(); it != actionButtons_.end(); ++it) {
1289 if (!parcel.WriteParcelable(it->get())) {
1290 ANS_LOGE("Failed to write actionButton");
1291 return false;
1292 }
1293 }
1294
1295 if (!parcel.WriteBool(isCoverActionButtons_)) {
1296 ANS_LOGE("Failed to write isCoverActionButtons_");
1297 return false;
1298 }
1299
1300 if (!parcel.WriteBool(isUpdateByOwnerAllowed_)) {
1301 ANS_LOGE("Failed to write isUpdateByOwnerAllowed_");
1302 return false;
1303 }
1304
1305 if (!parcel.WriteUint64(messageUsers_.size())) {
1306 ANS_LOGE("Failed to write the size of messageUsers");
1307 return false;
1308 }
1309
1310 for (auto it = messageUsers_.begin(); it != messageUsers_.end(); ++it) {
1311 if (!parcel.WriteParcelable(it->get())) {
1312 ANS_LOGE("Failed to write messageUser");
1313 return false;
1314 }
1315 }
1316
1317 if (!parcel.WriteStringVector(userInputHistory_)) {
1318 ANS_LOGE("Failed to write userInputHistory");
1319 return false;
1320 }
1321
1322 if (!parcel.WriteParcelable(&distributedOptions_)) {
1323 ANS_LOGE("Failed to write distributedOptions");
1324 return false;
1325 }
1326
1327 valid = notificationTemplate_ ? true : false;
1328 if (!parcel.WriteBool(valid)) {
1329 ANS_LOGE("Failed to write the flag which indicate whether publicNotification is null");
1330 return false;
1331 }
1332
1333 if (valid) {
1334 if (!parcel.WriteParcelable(notificationTemplate_.get())) {
1335 ANS_LOGE("Failed to write notificationTemplate");
1336 return false;
1337 }
1338 }
1339
1340 valid = notificationFlags_ ? true : false;
1341 if (!parcel.WriteBool(valid)) {
1342 ANS_LOGE("Failed to write flags for the notification");
1343 return false;
1344 }
1345
1346 if (valid) {
1347 if (!parcel.WriteParcelable(notificationFlags_.get())) {
1348 ANS_LOGE("Failed to write notification flags");
1349 return false;
1350 }
1351 }
1352
1353 valid = notificationFlagsOfDevices_ ? true : false;
1354 if (!parcel.WriteBool(valid)) {
1355 ANS_LOGE("Failed to write notification device flags cause invalid sptr");
1356 return false;
1357 }
1358
1359 if (valid) {
1360 if (!parcel.WriteInt32(static_cast<int32_t>(notificationFlagsOfDevices_->size()))) {
1361 ANS_LOGE("Failed to write notification devices flags size");
1362 return false;
1363 }
1364 for (auto deviceFlag : *notificationFlagsOfDevices_) {
1365 if (!parcel.WriteString(deviceFlag.first)) {
1366 ANS_LOGE("Failed to write notification devices flags key");
1367 return false;
1368 }
1369 if (!parcel.WriteParcelable(deviceFlag.second.get())) {
1370 ANS_LOGE("Failed to write notification devices flags value");
1371 return false;
1372 }
1373 }
1374 }
1375
1376 valid = unifiedGroupInfo_ ? true : false;
1377 if (!parcel.WriteBool(valid)) {
1378 ANS_LOGE("Failed to write unifiedGroupInfo for the notification");
1379 return false;
1380 }
1381
1382 if (valid) {
1383 if (!parcel.WriteParcelable(unifiedGroupInfo_.get())) {
1384 ANS_LOGE("Failed to write notification unifiedGroupInfo");
1385 return false;
1386 }
1387 }
1388
1389 valid = notificationBundleOption_ != nullptr ? true : false;
1390 if (!parcel.WriteBool(valid)) {
1391 ANS_LOGE("Failed to write bundleOption for the notification");
1392 return false;
1393 }
1394
1395 if (valid) {
1396 if (!parcel.WriteParcelable(notificationBundleOption_.get())) {
1397 ANS_LOGE("Failed to write notification bundleOption");
1398 return false;
1399 }
1400 }
1401
1402 valid = agentBundle_ != nullptr ? true : false;
1403 if (!parcel.WriteBool(valid)) {
1404 ANS_LOGE("Failed to write agentBundle for the notification");
1405 return false;
1406 }
1407
1408 if (valid) {
1409 if (!parcel.WriteParcelable(agentBundle_.get())) {
1410 ANS_LOGE("Failed to write notification agentBundle");
1411 return false;
1412 }
1413 }
1414
1415 if (!parcel.WriteInt64(updateDeadLine_)) {
1416 ANS_LOGE("Failed to write max update time");
1417 return false;
1418 }
1419
1420 if (!parcel.WriteInt64(finishDeadLine_)) {
1421 ANS_LOGE("Failed to write max finish time");
1422 return false;
1423 }
1424
1425 return true;
1426 }
1427
Unmarshalling(Parcel & parcel)1428 NotificationRequest *NotificationRequest::Unmarshalling(Parcel &parcel)
1429 {
1430 auto objptr = new (std::nothrow) NotificationRequest();
1431 if ((objptr != nullptr) && !objptr->ReadFromParcel(parcel)) {
1432 delete objptr;
1433 objptr = nullptr;
1434 }
1435
1436 return objptr;
1437 }
1438
ReadFromParcel(Parcel & parcel)1439 bool NotificationRequest::ReadFromParcel(Parcel &parcel)
1440 {
1441 notificationId_ = parcel.ReadInt32();
1442 color_ = parcel.ReadUint32();
1443 badgeNumber_ = parcel.ReadUint32();
1444 progressValue_ = parcel.ReadInt32();
1445 progressMax_ = parcel.ReadInt32();
1446 createTime_ = parcel.ReadInt64();
1447 deliveryTime_ = parcel.ReadInt64();
1448 autoDeletedTime_ = parcel.ReadInt64();
1449
1450 creatorPid_ = static_cast<pid_t>(parcel.ReadInt32());
1451 creatorUid_ = parcel.ReadInt32();
1452 ownerUid_ = parcel.ReadInt32();
1453 creatorUserId_ = parcel.ReadInt32();
1454 ownerUserId_ = parcel.ReadInt32();
1455 receiverUserId_ = parcel.ReadInt32();
1456 creatorInstanceKey_ = parcel.ReadInt32();
1457 notificationControlFlags_ = parcel.ReadUint32();
1458 publishDelayTime_ = parcel.ReadUint32();
1459
1460 if (!parcel.ReadString(settingsText_)) {
1461 ANS_LOGE("Failed to read settings text");
1462 return false;
1463 }
1464
1465 if (!parcel.ReadString(creatorBundleName_)) {
1466 ANS_LOGE("Failed to read creator bundle name");
1467 return false;
1468 }
1469
1470 if (!parcel.ReadString(ownerBundleName_)) {
1471 ANS_LOGE("Failed to read owner bundle name");
1472 return false;
1473 }
1474
1475 if (!parcel.ReadString(groupName_)) {
1476 ANS_LOGE("Failed to read group name");
1477 return false;
1478 }
1479
1480 if (!parcel.ReadString(statusBarText_)) {
1481 ANS_LOGE("Failed to read status bar text");
1482 return false;
1483 }
1484
1485 if (!parcel.ReadString(label_)) {
1486 ANS_LOGE("Failed to read label");
1487 return false;
1488 }
1489
1490 if (!parcel.ReadString(shortcutId_)) {
1491 ANS_LOGE("Failed to read shortcut Id");
1492 return false;
1493 }
1494
1495 if (!parcel.ReadString(sortingKey_)) {
1496 ANS_LOGE("Failed to read sorting key");
1497 return false;
1498 }
1499
1500 if (!parcel.ReadString(classification_)) {
1501 ANS_LOGE("Failed to read classification");
1502 return false;
1503 }
1504
1505 if (!parcel.ReadString(appMessageId_)) {
1506 ANS_LOGE("Failed to read appMessageId");
1507 return false;
1508 }
1509
1510 if (!parcel.ReadString(sound_)) {
1511 ANS_LOGE("Failed to read sound");
1512 return false;
1513 }
1514
1515 int32_t slotTypeValue = parcel.ReadInt32();
1516 if (slotTypeValue < 0 ||
1517 slotTypeValue >= static_cast<int>(NotificationConstant::SlotType::ILLEGAL_TYPE)) {
1518 ANS_LOGE("Invalid slot type value :%{public}d. It should be in [0 , %{public}d).",
1519 slotTypeValue, static_cast<int>(NotificationConstant::SlotType::ILLEGAL_TYPE));
1520 return false;
1521 }
1522 slotType_ = static_cast<NotificationConstant::SlotType>(slotTypeValue);
1523 int32_t groupAlertTypeValue = parcel.ReadInt32();
1524 if (groupAlertTypeValue < 0 ||
1525 groupAlertTypeValue >= static_cast<int>(NotificationRequest::GroupAlertType::ILLEGAL_TYPE)) {
1526 ANS_LOGE("Invalid groupAlert type value :%{public}d. It should be in [0 , %{public}d).",
1527 groupAlertTypeValue, static_cast<int>(NotificationRequest::GroupAlertType::ILLEGAL_TYPE));
1528 return false;
1529 }
1530 groupAlertType_ = static_cast<NotificationRequest::GroupAlertType>(groupAlertTypeValue);
1531 int32_t visiblenessTypeValue = parcel.ReadInt32();
1532 if (visiblenessTypeValue < 0 ||
1533 visiblenessTypeValue >= static_cast<int>(NotificationConstant::VisiblenessType::ILLEGAL_TYPE)) {
1534 ANS_LOGE("Invalid visibleness type value :%{public}d. It should be in [0 , %{public}d).",
1535 visiblenessTypeValue, static_cast<int>(NotificationConstant::VisiblenessType::ILLEGAL_TYPE));
1536 return false;
1537 }
1538 visiblenessType_ = static_cast<NotificationConstant::VisiblenessType>(visiblenessTypeValue);
1539 int32_t badgeStyleValue = parcel.ReadInt32();
1540 if (badgeStyleValue < 0) {
1541 ANS_LOGE("Invalid badge style value :%{public}d. It should be greater than 0.", badgeStyleValue);
1542 return false;
1543 }
1544 if (badgeStyleValue >= static_cast<int>(NotificationRequest::BadgeStyle::ILLEGAL_TYPE)) {
1545 badgeStyleValue = static_cast<int>(NotificationRequest::BadgeStyle::NONE);
1546 ANS_LOGE("The badge style value is too large, set it to the default enumeration value: %{public}d.",
1547 static_cast<int>(NotificationRequest::BadgeStyle::NONE));
1548 }
1549 badgeStyle_ = static_cast<NotificationRequest::BadgeStyle>(badgeStyleValue);
1550 int32_t notificationContentTypeValue = parcel.ReadInt32();
1551 if (notificationContentTypeValue <= static_cast<int>(NotificationContent::Type::NONE) ||
1552 notificationContentTypeValue >= static_cast<int>(NotificationContent::Type::ILLEGAL_TYPE)) {
1553 ANS_LOGE("Invalid notification content type value :%{public}d. It should be in (%{public}d , %{public}d)",
1554 notificationContentTypeValue, static_cast<int>(NotificationContent::Type::NONE),
1555 static_cast<int>(NotificationContent::Type::ILLEGAL_TYPE));
1556 return false;
1557 }
1558 notificationContentType_ = static_cast<NotificationContent::Type>(notificationContentTypeValue);
1559
1560 showDeliveryTime_ = parcel.ReadBool();
1561 tapDismissed_ = parcel.ReadBool();
1562 colorEnabled_ = parcel.ReadBool();
1563 alertOneTime_ = parcel.ReadBool();
1564 showStopwatch_ = parcel.ReadBool();
1565 isCountdown_ = parcel.ReadBool();
1566 inProgress_ = parcel.ReadBool();
1567 groupOverview_ = parcel.ReadBool();
1568 progressIndeterminate_ = parcel.ReadBool();
1569 unremovable_ = parcel.ReadBool();
1570 floatingIcon_ = parcel.ReadBool();
1571 onlyLocal_ = parcel.ReadBool();
1572 permitted_ = parcel.ReadBool();
1573 isAgent_ = parcel.ReadBool();
1574 isRemoveAllowed_ = parcel.ReadBool();
1575
1576 bool valid {false};
1577
1578 valid = parcel.ReadBool();
1579 if (valid) {
1580 wantAgent_ = std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>(
1581 parcel.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>());
1582 if (!wantAgent_) {
1583 ANS_LOGE("Failed to read wantAgent");
1584 return false;
1585 }
1586 }
1587
1588 valid = parcel.ReadBool();
1589 if (valid) {
1590 removalWantAgent_ = std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>(
1591 parcel.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>());
1592 if (!removalWantAgent_) {
1593 ANS_LOGE("Failed to read removalWantAgent");
1594 return false;
1595 }
1596 }
1597
1598 valid = parcel.ReadBool();
1599 if (valid) {
1600 maxScreenWantAgent_ = std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>(
1601 parcel.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>());
1602 if (!maxScreenWantAgent_) {
1603 ANS_LOGE("Failed to read maxScreenWantAgent");
1604 return false;
1605 }
1606 }
1607
1608 valid = parcel.ReadBool();
1609 if (valid) {
1610 additionalParams_ = std::shared_ptr<AAFwk::WantParams>(parcel.ReadParcelable<AAFwk::WantParams>());
1611 if (!additionalParams_) {
1612 ANS_LOGE("Failed to read additionalParams");
1613 return false;
1614 }
1615 }
1616
1617 valid = parcel.ReadBool();
1618 if (valid) {
1619 littleIcon_ = std::shared_ptr<Media::PixelMap>(parcel.ReadParcelable<Media::PixelMap>());
1620 }
1621
1622 valid = parcel.ReadBool();
1623 if (valid) {
1624 bigIcon_ = std::shared_ptr<Media::PixelMap>(parcel.ReadParcelable<Media::PixelMap>());
1625 if (!bigIcon_) {
1626 ANS_LOGE("Failed to read bigIcon");
1627 return false;
1628 }
1629 }
1630
1631 valid = parcel.ReadBool();
1632 if (valid) {
1633 overlayIcon_ = std::shared_ptr<Media::PixelMap>(parcel.ReadParcelable<Media::PixelMap>());
1634 if (!overlayIcon_) {
1635 ANS_LOGE("Failed to read overlayIcon");
1636 return false;
1637 }
1638 }
1639
1640 valid = parcel.ReadBool();
1641 if (valid) {
1642 notificationContent_ = std::shared_ptr<NotificationContent>(parcel.ReadParcelable<NotificationContent>());
1643 if (!notificationContent_) {
1644 ANS_LOGE("Failed to read notificationContent");
1645 return false;
1646 }
1647 }
1648
1649 auto vsize = parcel.ReadUint64();
1650 vsize = (vsize < NotificationRequest::MAX_ACTION_BUTTONS) ? vsize : NotificationRequest::MAX_ACTION_BUTTONS;
1651 for (uint64_t it = 0; it < vsize; ++it) {
1652 auto member = std::shared_ptr<NotificationActionButton>(parcel.ReadParcelable<NotificationActionButton>());
1653 if (member == nullptr) {
1654 actionButtons_.clear();
1655 ANS_LOGE("Failed to read actionButton");
1656 return false;
1657 }
1658
1659 actionButtons_.emplace_back(member);
1660 }
1661
1662 isCoverActionButtons_ = parcel.ReadBool();
1663 isUpdateByOwnerAllowed_ = parcel.ReadBool();
1664
1665 vsize = parcel.ReadUint64();
1666 vsize = (vsize < NotificationRequest::MAX_MESSAGE_USERS) ? vsize : NotificationRequest::MAX_MESSAGE_USERS;
1667 for (uint64_t it = 0; it < vsize; ++it) {
1668 auto member = std::shared_ptr<MessageUser>(parcel.ReadParcelable<MessageUser>());
1669 if (member == nullptr) {
1670 ANS_LOGE("Failed to read messageUser");
1671 messageUsers_.clear();
1672 return false;
1673 }
1674
1675 messageUsers_.emplace_back(member);
1676 }
1677
1678 if (!parcel.ReadStringVector(&userInputHistory_)) {
1679 ANS_LOGE("Failed to read userInputHistory");
1680 return false;
1681 }
1682
1683 auto pOpt = parcel.ReadParcelable<NotificationDistributedOptions>();
1684 if (pOpt == nullptr) {
1685 ANS_LOGE("Failed to read distributedOptions");
1686 return false;
1687 }
1688 distributedOptions_ = *pOpt;
1689 delete pOpt;
1690 pOpt = nullptr;
1691
1692 valid = parcel.ReadBool();
1693 if (valid) {
1694 notificationTemplate_ = std::shared_ptr<NotificationTemplate>(parcel.ReadParcelable<NotificationTemplate>());
1695 if (!notificationTemplate_) {
1696 ANS_LOGE("Failed to read notificationTemplate");
1697 return false;
1698 }
1699 }
1700
1701 valid = parcel.ReadBool();
1702 if (valid) {
1703 notificationFlags_ = std::shared_ptr<NotificationFlags>(parcel.ReadParcelable<NotificationFlags>());
1704 if (!notificationFlags_) {
1705 ANS_LOGE("Failed to read notificationFlags");
1706 return false;
1707 }
1708 }
1709
1710 valid = parcel.ReadBool();
1711 if (valid) {
1712 notificationFlagsOfDevices_ = std::make_shared<std::map<std::string, std::shared_ptr<NotificationFlags>>>();
1713 int32_t mapSize = parcel.ReadInt32();
1714 mapSize = (mapSize < MAX_MAP_SIZE) ? mapSize : MAX_MAP_SIZE;
1715 for (int32_t seq = 0; seq < mapSize; seq++) {
1716 std::string deviceType = parcel.ReadString();
1717 std::shared_ptr<NotificationFlags> notificationFlags =
1718 std::shared_ptr<NotificationFlags>(parcel.ReadParcelable<NotificationFlags>());
1719 (*notificationFlagsOfDevices_)[deviceType] = notificationFlags;
1720 }
1721 }
1722
1723 valid = parcel.ReadBool();
1724 if (valid) {
1725 unifiedGroupInfo_ =
1726 std::shared_ptr<NotificationUnifiedGroupInfo>(parcel.ReadParcelable<NotificationUnifiedGroupInfo>());
1727 if (!unifiedGroupInfo_) {
1728 ANS_LOGE("Failed to read unifiedGroupInfo+");
1729 return false;
1730 }
1731 }
1732
1733 valid = parcel.ReadBool();
1734 if (valid) {
1735 notificationBundleOption_ =
1736 std::shared_ptr<NotificationBundleOption>(parcel.ReadParcelable<NotificationBundleOption>());
1737 if (!notificationBundleOption_) {
1738 ANS_LOGE("Failed to read notificationBundleOption");
1739 return false;
1740 }
1741 }
1742
1743 valid = parcel.ReadBool();
1744 if (valid) {
1745 agentBundle_ =
1746 std::shared_ptr<NotificationBundleOption>(parcel.ReadParcelable<NotificationBundleOption>());
1747 if (!agentBundle_) {
1748 ANS_LOGE("Failed to read agentBundle");
1749 return false;
1750 }
1751 }
1752
1753 updateDeadLine_ = parcel.ReadInt64();
1754 finishDeadLine_ = parcel.ReadInt64();
1755
1756 return true;
1757 }
1758
GetNowSysTime()1759 int64_t NotificationRequest::GetNowSysTime()
1760 {
1761 std::chrono::time_point<std::chrono::system_clock> nowSys = std::chrono::system_clock::now();
1762 auto epoch = nowSys.time_since_epoch();
1763 auto value = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
1764 int64_t duration = value.count();
1765 return duration;
1766 }
1767
SetTemplate(const std::shared_ptr<NotificationTemplate> & templ)1768 void NotificationRequest::SetTemplate(const std::shared_ptr<NotificationTemplate> &templ)
1769 {
1770 notificationTemplate_ = templ;
1771 }
1772
GetTemplate() const1773 std::shared_ptr<NotificationTemplate> NotificationRequest::GetTemplate() const
1774 {
1775 return notificationTemplate_;
1776 }
1777
SetFlags(const std::shared_ptr<NotificationFlags> & flags)1778 void NotificationRequest::SetFlags(const std::shared_ptr<NotificationFlags> &flags)
1779 {
1780 notificationFlags_ = flags;
1781 }
1782
GetFlags() const1783 std::shared_ptr<NotificationFlags> NotificationRequest::GetFlags() const
1784 {
1785 return notificationFlags_;
1786 }
1787
SetDeviceFlags(const std::shared_ptr<std::map<std::string,std::shared_ptr<NotificationFlags>>> & mapFlags)1788 void NotificationRequest::SetDeviceFlags(
1789 const std::shared_ptr<std::map<std::string, std::shared_ptr<NotificationFlags>>> &mapFlags)
1790 {
1791 notificationFlagsOfDevices_ = mapFlags;
1792 }
1793
GetDeviceFlags() const1794 std::shared_ptr<std::map<std::string, std::shared_ptr<NotificationFlags>>> NotificationRequest::GetDeviceFlags() const
1795 {
1796 return notificationFlagsOfDevices_;
1797 }
1798
1799
SetBundleOption(const std::shared_ptr<NotificationBundleOption> & bundleOption)1800 void NotificationRequest::SetBundleOption(const std::shared_ptr<NotificationBundleOption> &bundleOption)
1801 {
1802 notificationBundleOption_ = bundleOption;
1803 }
1804
GetBundleOption() const1805 std::shared_ptr<NotificationBundleOption> NotificationRequest::GetBundleOption() const
1806 {
1807 return notificationBundleOption_;
1808 }
1809
SetAgentBundle(const std::shared_ptr<NotificationBundleOption> & agentBundle)1810 void NotificationRequest::SetAgentBundle(const std::shared_ptr<NotificationBundleOption> &agentBundle)
1811 {
1812 agentBundle_ = agentBundle;
1813 }
1814
GetAgentBundle() const1815 std::shared_ptr<NotificationBundleOption> NotificationRequest::GetAgentBundle() const
1816 {
1817 return agentBundle_;
1818 }
1819
SetReceiverUserId(int32_t userId)1820 void NotificationRequest::SetReceiverUserId(int32_t userId)
1821 {
1822 receiverUserId_ = userId;
1823 }
1824
GetReceiverUserId() const1825 int32_t NotificationRequest::GetReceiverUserId() const
1826 {
1827 if (receiverUserId_ == SUBSCRIBE_USER_INIT) {
1828 if (ownerUserId_ == SUBSCRIBE_USER_INIT) {
1829 return creatorUserId_;
1830 }
1831 return ownerUserId_;
1832 }
1833 return receiverUserId_;
1834 }
1835
IsRemoveAllowed() const1836 bool NotificationRequest::IsRemoveAllowed() const
1837 {
1838 return isRemoveAllowed_;
1839 }
1840
SetRemoveAllowed(bool isRemoveAllowed)1841 void NotificationRequest::SetRemoveAllowed(bool isRemoveAllowed)
1842 {
1843 isRemoveAllowed_ = isRemoveAllowed;
1844 }
1845
CopyBase(const NotificationRequest & other)1846 void NotificationRequest::CopyBase(const NotificationRequest &other)
1847 {
1848 this->notificationId_ = other.notificationId_;
1849 this->color_ = other.color_;
1850 this->badgeNumber_ = other.badgeNumber_;
1851 this->notificationControlFlags_ = other.notificationControlFlags_;
1852 this->progressValue_ = other.progressValue_;
1853 this->progressMax_ = other.progressMax_;
1854 this->createTime_ = other.createTime_;
1855 this->deliveryTime_ = other.deliveryTime_;
1856 this->autoDeletedTime_ = other.autoDeletedTime_;
1857 this->updateDeadLine_ = other.updateDeadLine_;
1858 this->finishDeadLine_ = other.finishDeadLine_;
1859
1860 this->creatorPid_ = other.creatorPid_;
1861 this->creatorUid_ = other.creatorUid_;
1862 this->ownerUid_ = other.ownerUid_;
1863 this->creatorUserId_ = other.creatorUserId_;
1864 this->ownerUserId_ = other.ownerUserId_;
1865 this->receiverUserId_ = other.receiverUserId_;
1866 this->creatorInstanceKey_ = other.creatorInstanceKey_;
1867 this->isAgent_ = other.isAgent_;
1868 this->isRemoveAllowed_ = other.isRemoveAllowed_;
1869 this->isCoverActionButtons_ = other.isCoverActionButtons_;
1870 this->isUpdateByOwnerAllowed_ = other.isUpdateByOwnerAllowed_;
1871
1872 this->slotType_ = other.slotType_;
1873 this->settingsText_ = other.settingsText_;
1874 this->creatorBundleName_ = other.creatorBundleName_;
1875 this->ownerBundleName_ = other.ownerBundleName_;
1876 this->groupName_ = other.groupName_;
1877 this->statusBarText_ = other.statusBarText_;
1878 this->label_ = other.label_;
1879 this->shortcutId_ = other.shortcutId_;
1880 this->sortingKey_ = other.sortingKey_;
1881 this->classification_ = other.classification_;
1882 this->appMessageId_ = other.appMessageId_;
1883 this->sound_ = other.sound_;
1884
1885 this->groupAlertType_ = other.groupAlertType_;
1886 this->visiblenessType_ = other.visiblenessType_;
1887 this->badgeStyle_ = other.badgeStyle_;
1888 this->notificationContentType_ = other.notificationContentType_;
1889 }
1890
CopyOther(const NotificationRequest & other)1891 void NotificationRequest::CopyOther(const NotificationRequest &other)
1892 {
1893 this->showDeliveryTime_ = other.showDeliveryTime_;
1894 this->tapDismissed_ = other.tapDismissed_;
1895 this->colorEnabled_ = other.colorEnabled_;
1896 this->alertOneTime_ = other.alertOneTime_;
1897 this->showStopwatch_ = other.showStopwatch_;
1898 this->isCountdown_ = other.isCountdown_;
1899 this->inProgress_ = other.inProgress_;
1900 this->groupOverview_ = other.groupOverview_;
1901 this->progressIndeterminate_ = other.progressIndeterminate_;
1902 this->unremovable_ = other.unremovable_;
1903 this->floatingIcon_ = other.floatingIcon_;
1904 this->onlyLocal_ = other.onlyLocal_;
1905 this->permitted_ = other.permitted_;
1906
1907 this->wantAgent_ = other.wantAgent_;
1908 this->removalWantAgent_ = other.removalWantAgent_;
1909 this->maxScreenWantAgent_ = other.maxScreenWantAgent_;
1910 this->additionalParams_ = other.additionalParams_;
1911 this->littleIcon_ = other.littleIcon_;
1912 this->bigIcon_ = other.bigIcon_;
1913 this->overlayIcon_ = other.overlayIcon_;
1914 this->notificationContent_ = other.notificationContent_;
1915
1916 this->actionButtons_ = other.actionButtons_;
1917 this->messageUsers_ = other.messageUsers_;
1918 this->userInputHistory_ = other.userInputHistory_;
1919
1920 this->distributedOptions_ = other.distributedOptions_;
1921
1922 this->notificationTemplate_ = other.notificationTemplate_;
1923 this->notificationFlags_ = other.notificationFlags_;
1924 this->agentBundle_ = other.agentBundle_;
1925 this->unifiedGroupInfo_ = other.unifiedGroupInfo_;
1926 this->notificationBundleOption_ = other.notificationBundleOption_;
1927 this->notificationFlagsOfDevices_ = other.notificationFlagsOfDevices_;
1928 this->publishDelayTime_ = other.publishDelayTime_;
1929 }
1930
ConvertObjectsToJson(nlohmann::json & jsonObject) const1931 bool NotificationRequest::ConvertObjectsToJson(nlohmann::json &jsonObject) const
1932 {
1933 jsonObject["wantAgent"] = wantAgent_ ? AbilityRuntime::WantAgent::WantAgentHelper::ToString(wantAgent_) : "";
1934
1935 nlohmann::json contentObj;
1936 if (notificationContent_) {
1937 if (!NotificationJsonConverter::ConvertToJson(notificationContent_.get(), contentObj)) {
1938 ANS_LOGE("Cannot convert notificationContent to JSON");
1939 return false;
1940 }
1941 }
1942 jsonObject["content"] = contentObj;
1943
1944 nlohmann::json buttonsArr = nlohmann::json::array();
1945 for (auto &btn : actionButtons_) {
1946 if (!btn) {
1947 continue;
1948 }
1949
1950 nlohmann::json btnObj;
1951 if (!NotificationJsonConverter::ConvertToJson(btn.get(), btnObj)) {
1952 ANS_LOGE("Cannot convert actionButton to JSON");
1953 return false;
1954 }
1955
1956 buttonsArr.emplace_back(btnObj);
1957 }
1958 jsonObject["actionButtons"] = buttonsArr;
1959
1960 std::string extraInfoStr;
1961 if (additionalParams_) {
1962 AAFwk::WantParamWrapper wWrapper(*additionalParams_);
1963 extraInfoStr = wWrapper.ToString();
1964 }
1965 jsonObject["extraInfo"] = extraInfoStr;
1966 jsonObject["smallIcon"] = AnsImageUtil::PackImage(littleIcon_);
1967 jsonObject["largeIcon"] = AnsImageUtil::PackImage(bigIcon_);
1968 jsonObject["overlayIcon"] = overlayIcon_ ? AnsImageUtil::PackImage(overlayIcon_) : "";
1969
1970 nlohmann::json optObj;
1971 if (!NotificationJsonConverter::ConvertToJson(&distributedOptions_, optObj)) {
1972 ANS_LOGE("Cannot convert distributedOptions to JSON");
1973 return false;
1974 }
1975 jsonObject["distributedOptions"] = optObj;
1976
1977 if (notificationFlags_) {
1978 nlohmann::json flagsObj;
1979 if (!NotificationJsonConverter::ConvertToJson(notificationFlags_.get(), flagsObj)) {
1980 ANS_LOGE("Cannot convert notificationFlags to JSON");
1981 return false;
1982 }
1983 jsonObject["notificationFlags"] = flagsObj;
1984 }
1985
1986 if (notificationBundleOption_ != nullptr) {
1987 nlohmann::json bundleOptionObj;
1988 if (!NotificationJsonConverter::ConvertToJson(notificationBundleOption_.get(), bundleOptionObj)) {
1989 ANS_LOGE("Cannot convert notificationBundleOption to JSON.");
1990 return false;
1991 }
1992 jsonObject["notificationBundleOption"] = bundleOptionObj;
1993 }
1994
1995 if (agentBundle_ != nullptr) {
1996 nlohmann::json bundleOptionObj;
1997 if (!NotificationJsonConverter::ConvertToJson(agentBundle_.get(), bundleOptionObj)) {
1998 ANS_LOGE("Cannot convert agentBundle to JSON.");
1999 return false;
2000 }
2001 jsonObject["agentBundle"] = bundleOptionObj;
2002 }
2003
2004 return true;
2005 }
2006
ConvertJsonToNumExt(NotificationRequest * target,const nlohmann::json & jsonObject)2007 void NotificationRequest::ConvertJsonToNumExt(
2008 NotificationRequest *target, const nlohmann::json &jsonObject)
2009 {
2010 const auto &jsonEnd = jsonObject.cend();
2011
2012 if (jsonObject.find("updateDeadLine") != jsonEnd && jsonObject.at("updateDeadLine").is_number_integer()) {
2013 target->updateDeadLine_ = jsonObject.at("updateDeadLine").get<int64_t>();
2014 }
2015
2016 if (jsonObject.find("finishDeadLine") != jsonEnd && jsonObject.at("finishDeadLine").is_number_integer()) {
2017 target->finishDeadLine_ = jsonObject.at("finishDeadLine").get<int64_t>();
2018 }
2019
2020 if (jsonObject.find("ownerUserId") != jsonEnd && jsonObject.at("ownerUserId").is_number_integer()) {
2021 target->ownerUserId_ = jsonObject.at("ownerUserId").get<int32_t>();
2022 }
2023
2024 if (jsonObject.find("ownerUid") != jsonEnd && jsonObject.at("ownerUid").is_number_integer()) {
2025 target->ownerUid_ = jsonObject.at("ownerUid").get<int32_t>();
2026 }
2027
2028 if (jsonObject.find("notificationControlFlags") != jsonEnd &&
2029 jsonObject.at("notificationControlFlags").is_number_integer()) {
2030 target->notificationControlFlags_ = jsonObject.at("notificationControlFlags").get<uint32_t>();
2031 }
2032 }
2033
ConvertJsonToNum(NotificationRequest * target,const nlohmann::json & jsonObject)2034 void NotificationRequest::ConvertJsonToNum(NotificationRequest *target, const nlohmann::json &jsonObject)
2035 {
2036 if (target == nullptr) {
2037 ANS_LOGE("Invalid input parameter");
2038 return;
2039 }
2040
2041 const auto &jsonEnd = jsonObject.cend();
2042
2043 if (jsonObject.find("id") != jsonEnd && jsonObject.at("id").is_number_integer()) {
2044 target->notificationId_ = jsonObject.at("id").get<int32_t>();
2045 }
2046
2047 if (jsonObject.find("color") != jsonEnd && jsonObject.at("color").is_number_integer()) {
2048 target->color_ = jsonObject.at("color").get<uint32_t>();
2049 }
2050
2051 if (jsonObject.find("deliveryTime") != jsonEnd && jsonObject.at("deliveryTime").is_number_integer()) {
2052 target->deliveryTime_ = jsonObject.at("deliveryTime").get<int64_t>();
2053 }
2054
2055 if (jsonObject.find("autoDeletedTime") != jsonEnd && jsonObject.at("autoDeletedTime").is_number_integer()) {
2056 target->autoDeletedTime_ = jsonObject.at("autoDeletedTime").get<int64_t>();
2057 }
2058
2059 if (jsonObject.find("creatorUid") != jsonEnd && jsonObject.at("creatorUid").is_number_integer()) {
2060 target->creatorUid_ = jsonObject.at("creatorUid").get<int32_t>();
2061 }
2062
2063 if (jsonObject.find("creatorPid") != jsonEnd && jsonObject.at("creatorPid").is_number_integer()) {
2064 target->creatorPid_ = jsonObject.at("creatorPid").get<int32_t>();
2065 }
2066
2067 if (jsonObject.find("creatorUserId") != jsonEnd && jsonObject.at("creatorUserId").is_number_integer()) {
2068 target->creatorUserId_ = jsonObject.at("creatorUserId").get<int32_t>();
2069 }
2070
2071 if (jsonObject.find("receiverUserId") != jsonEnd && jsonObject.at("receiverUserId").is_number_integer()) {
2072 target->receiverUserId_ = jsonObject.at("receiverUserId").get<int32_t>();
2073 }
2074
2075 if (jsonObject.find("creatorInstanceKey") != jsonEnd && jsonObject.at("creatorInstanceKey").is_number_integer()) {
2076 target->creatorInstanceKey_ = jsonObject.at("creatorInstanceKey").get<int32_t>();
2077 }
2078
2079 if (jsonObject.find("badgeNumber") != jsonEnd && jsonObject.at("badgeNumber").is_number_integer()) {
2080 target->badgeNumber_ = jsonObject.at("badgeNumber").get<uint32_t>();
2081 }
2082
2083 ConvertJsonToNumExt(target, jsonObject);
2084 }
2085
ConvertJsonToString(NotificationRequest * target,const nlohmann::json & jsonObject)2086 void NotificationRequest::ConvertJsonToString(NotificationRequest *target, const nlohmann::json &jsonObject)
2087 {
2088 if (target == nullptr) {
2089 ANS_LOGE("Invalid input parameter");
2090 return;
2091 }
2092
2093 const auto &jsonEnd = jsonObject.cend();
2094
2095 if (jsonObject.find("creatorBundleName") != jsonEnd && jsonObject.at("creatorBundleName").is_string()) {
2096 target->creatorBundleName_ = jsonObject.at("creatorBundleName").get<std::string>();
2097 }
2098
2099 if (jsonObject.find("ownerBundleName") != jsonEnd && jsonObject.at("ownerBundleName").is_string()) {
2100 target->ownerBundleName_ = jsonObject.at("ownerBundleName").get<std::string>();
2101 }
2102
2103 if (jsonObject.find("groupName") != jsonEnd && jsonObject.at("groupName").is_string()) {
2104 target->groupName_ = jsonObject.at("groupName").get<std::string>();
2105 }
2106
2107 if (jsonObject.find("label") != jsonEnd && jsonObject.at("label").is_string()) {
2108 target->label_ = jsonObject.at("label").get<std::string>();
2109 }
2110
2111 if (jsonObject.find("classification") != jsonEnd && jsonObject.at("classification").is_string()) {
2112 target->classification_ = jsonObject.at("classification").get<std::string>();
2113 }
2114
2115 if (jsonObject.find("creatorBundleName") != jsonEnd && jsonObject.at("creatorBundleName").is_string()) {
2116 target->creatorBundleName_ = jsonObject.at("creatorBundleName").get<std::string>();
2117 }
2118 }
2119
ConvertJsonToEnum(NotificationRequest * target,const nlohmann::json & jsonObject)2120 void NotificationRequest::ConvertJsonToEnum(NotificationRequest *target, const nlohmann::json &jsonObject)
2121 {
2122 if (target == nullptr) {
2123 ANS_LOGE("Invalid input parameter");
2124 return;
2125 }
2126
2127 const auto &jsonEnd = jsonObject.cend();
2128
2129 if (jsonObject.find("slotType") != jsonEnd && jsonObject.at("slotType").is_number_integer()) {
2130 auto slotTypeValue = jsonObject.at("slotType").get<int32_t>();
2131 target->slotType_ = static_cast<NotificationConstant::SlotType>(slotTypeValue);
2132 }
2133
2134 if (jsonObject.find("badgeIconStyle") != jsonEnd && jsonObject.at("badgeIconStyle").is_number_integer()) {
2135 auto badgeStyleValue = jsonObject.at("badgeIconStyle").get<int32_t>();
2136 target->badgeStyle_ = static_cast<NotificationRequest::BadgeStyle>(badgeStyleValue);
2137 }
2138
2139 if (jsonObject.find("notificationContentType") != jsonEnd &&
2140 jsonObject.at("notificationContentType").is_number_integer()) {
2141 auto notificationContentType = jsonObject.at("notificationContentType").get<int32_t>();
2142 target->notificationContentType_ = static_cast<NotificationContent::Type>(notificationContentType);
2143 }
2144 }
2145
ConvertJsonToBool(NotificationRequest * target,const nlohmann::json & jsonObject)2146 void NotificationRequest::ConvertJsonToBool(NotificationRequest *target, const nlohmann::json &jsonObject)
2147 {
2148 if (target == nullptr) {
2149 ANS_LOGE("Invalid input parameter");
2150 return;
2151 }
2152
2153 const auto &jsonEnd = jsonObject.cend();
2154
2155 if (jsonObject.find("showDeliveryTime") != jsonEnd && jsonObject.at("showDeliveryTime").is_boolean()) {
2156 target->showDeliveryTime_ = jsonObject.at("showDeliveryTime").get<bool>();
2157 }
2158
2159 if (jsonObject.find("tapDismissed") != jsonEnd && jsonObject.at("tapDismissed").is_boolean()) {
2160 target->tapDismissed_ = jsonObject.at("tapDismissed").get<bool>();
2161 }
2162
2163 if (jsonObject.find("colorEnabled") != jsonEnd && jsonObject.at("colorEnabled").is_boolean()) {
2164 target->colorEnabled_ = jsonObject.at("colorEnabled").get<bool>();
2165 }
2166
2167 if (jsonObject.find("isOngoing") != jsonEnd && jsonObject.at("isOngoing").is_boolean()) {
2168 target->inProgress_ = jsonObject.at("isOngoing").get<bool>();
2169 }
2170
2171 if (jsonObject.find("isAlertOnce") != jsonEnd && jsonObject.at("isAlertOnce").is_boolean()) {
2172 target->alertOneTime_ = jsonObject.at("isAlertOnce").get<bool>();
2173 }
2174
2175 if (jsonObject.find("isStopwatch") != jsonEnd && jsonObject.at("isStopwatch").is_boolean()) {
2176 target->showStopwatch_ = jsonObject.at("isStopwatch").get<bool>();
2177 }
2178
2179 if (jsonObject.find("isCountdown") != jsonEnd && jsonObject.at("isCountdown").is_boolean()) {
2180 target->isCountdown_ = jsonObject.at("isCountdown").get<bool>();
2181 }
2182
2183 if (jsonObject.find("isUnremovable") != jsonEnd && jsonObject.at("isUnremovable").is_boolean()) {
2184 target->unremovable_ = jsonObject.at("isUnremovable").get<bool>();
2185 }
2186
2187 if (jsonObject.find("isFloatingIcon") != jsonEnd && jsonObject.at("isFloatingIcon").is_boolean()) {
2188 target->floatingIcon_ = jsonObject.at("isFloatingIcon").get<bool>();
2189 }
2190
2191 ConvertJsonToBoolExt(target, jsonObject);
2192 }
2193
ConvertJsonToBoolExt(NotificationRequest * target,const nlohmann::json & jsonObject)2194 void NotificationRequest::ConvertJsonToBoolExt(NotificationRequest *target, const nlohmann::json &jsonObject)
2195 {
2196 const auto &jsonEnd = jsonObject.cend();
2197
2198 if (jsonObject.find("isAgent") != jsonEnd && jsonObject.at("isAgent").is_boolean()) {
2199 target->isAgent_ = jsonObject.at("isAgent").get<bool>();
2200 }
2201 }
2202
ConvertJsonToPixelMap(NotificationRequest * target,const nlohmann::json & jsonObject)2203 void NotificationRequest::ConvertJsonToPixelMap(NotificationRequest *target, const nlohmann::json &jsonObject)
2204 {
2205 if (target == nullptr) {
2206 ANS_LOGE("Invalid input parameter");
2207 return;
2208 }
2209
2210 const auto &jsonEnd = jsonObject.cend();
2211
2212 if (jsonObject.find("smallIcon") != jsonEnd && jsonObject.at("smallIcon").is_string()) {
2213 auto littleIconStr = jsonObject.at("smallIcon").get<std::string>();
2214 target->littleIcon_ = AnsImageUtil::UnPackImage(littleIconStr);
2215 }
2216
2217 if (jsonObject.find("largeIcon") != jsonEnd && jsonObject.at("largeIcon").is_string()) {
2218 auto bigIconStr = jsonObject.at("largeIcon").get<std::string>();
2219 target->bigIcon_ = AnsImageUtil::UnPackImage(bigIconStr);
2220 }
2221
2222 if (jsonObject.find("overlayIcon") != jsonEnd && jsonObject.at("overlayIcon").is_string()) {
2223 auto overlayIconStr = jsonObject.at("overlayIcon").get<std::string>();
2224 target->overlayIcon_ = AnsImageUtil::UnPackImage(overlayIconStr);
2225 }
2226 }
2227
ConvertJsonToNotificationContent(NotificationRequest * target,const nlohmann::json & jsonObject)2228 bool NotificationRequest::ConvertJsonToNotificationContent(
2229 NotificationRequest *target, const nlohmann::json &jsonObject)
2230 {
2231 if (target == nullptr) {
2232 ANS_LOGE("Invalid input parameter");
2233 return false;
2234 }
2235
2236 const auto &jsonEnd = jsonObject.cend();
2237
2238 if (jsonObject.find("content") != jsonEnd) {
2239 auto contentObj = jsonObject.at("content");
2240 if (!contentObj.is_null()) {
2241 auto pContent = NotificationJsonConverter::ConvertFromJson<NotificationContent>(contentObj);
2242 if (pContent == nullptr) {
2243 ANS_LOGE("Failed to parse notification content!");
2244 return false;
2245 }
2246
2247 target->notificationContent_ = std::shared_ptr<NotificationContent>(pContent);
2248 }
2249 }
2250
2251 return true;
2252 }
2253
ConvertJsonToNotificationActionButton(NotificationRequest * target,const nlohmann::json & jsonObject)2254 bool NotificationRequest::ConvertJsonToNotificationActionButton(
2255 NotificationRequest *target, const nlohmann::json &jsonObject)
2256 {
2257 if (target == nullptr) {
2258 ANS_LOGE("Invalid input parameter");
2259 return false;
2260 }
2261
2262 const auto &jsonEnd = jsonObject.cend();
2263
2264 if (jsonObject.find("actionButtons") != jsonEnd) {
2265 auto buttonArr = jsonObject.at("actionButtons");
2266 for (auto &btnObj : buttonArr) {
2267 auto pBtn = NotificationJsonConverter::ConvertFromJson<NotificationActionButton>(btnObj);
2268 if (pBtn == nullptr) {
2269 ANS_LOGE("Failed to parse actionButton!");
2270 return false;
2271 }
2272
2273 target->actionButtons_.emplace_back(pBtn);
2274 }
2275 }
2276
2277 return true;
2278 }
2279
ConvertJsonToNotificationDistributedOptions(NotificationRequest * target,const nlohmann::json & jsonObject)2280 bool NotificationRequest::ConvertJsonToNotificationDistributedOptions(
2281 NotificationRequest *target, const nlohmann::json &jsonObject)
2282 {
2283 if (target == nullptr) {
2284 ANS_LOGE("Invalid input parameter");
2285 return false;
2286 }
2287
2288 const auto &jsonEnd = jsonObject.cend();
2289
2290 if (jsonObject.find("distributedOptions") != jsonEnd) {
2291 auto optObj = jsonObject.at("distributedOptions");
2292 if (!optObj.is_null()) {
2293 auto *pOpt = NotificationJsonConverter::ConvertFromJson<NotificationDistributedOptions>(optObj);
2294 if (pOpt == nullptr) {
2295 ANS_LOGE("Failed to parse distributedOptions!");
2296 return false;
2297 }
2298
2299 target->distributedOptions_ = *pOpt;
2300 delete pOpt;
2301 }
2302 }
2303
2304 return true;
2305 }
2306
ConvertJsonToNotificationFlags(NotificationRequest * target,const nlohmann::json & jsonObject)2307 bool NotificationRequest::ConvertJsonToNotificationFlags(
2308 NotificationRequest *target, const nlohmann::json &jsonObject)
2309 {
2310 if (target == nullptr) {
2311 ANS_LOGE("Invalid input parameter");
2312 return false;
2313 }
2314
2315 const auto &jsonEnd = jsonObject.cend();
2316
2317 if (jsonObject.find("notificationFlags") != jsonEnd) {
2318 auto flagsObj = jsonObject.at("notificationFlags");
2319 if (!flagsObj.is_null()) {
2320 auto *pFlags = NotificationJsonConverter::ConvertFromJson<NotificationFlags>(flagsObj);
2321 if (pFlags == nullptr) {
2322 ANS_LOGE("Failed to parse notificationFlags!");
2323 return false;
2324 }
2325
2326 target->notificationFlags_ = std::shared_ptr<NotificationFlags>(pFlags);
2327 }
2328 }
2329
2330 return true;
2331 }
2332
ConvertJsonToNotificationBundleOption(NotificationRequest * target,const nlohmann::json & jsonObject)2333 bool NotificationRequest::ConvertJsonToNotificationBundleOption(
2334 NotificationRequest *target, const nlohmann::json &jsonObject)
2335 {
2336 if (target == nullptr) {
2337 ANS_LOGE("Invalid input parameter.");
2338 return false;
2339 }
2340
2341 const auto &jsonEnd = jsonObject.cend();
2342
2343 if (jsonObject.find("notificationBundleOption") != jsonEnd) {
2344 auto bundleOptionObj = jsonObject.at("notificationBundleOption");
2345 if (!bundleOptionObj.is_null()) {
2346 auto *pBundleOption = NotificationJsonConverter::ConvertFromJson<NotificationBundleOption>(bundleOptionObj);
2347 if (pBundleOption == nullptr) {
2348 ANS_LOGE("Failed to parse notificationBundleOption!");
2349 return false;
2350 }
2351
2352 target->notificationBundleOption_ = std::shared_ptr<NotificationBundleOption>(pBundleOption);
2353 }
2354 }
2355
2356 return true;
2357 }
2358
ConvertJsonToAgentBundle(NotificationRequest * target,const nlohmann::json & jsonObject)2359 bool NotificationRequest::ConvertJsonToAgentBundle(
2360 NotificationRequest *target, const nlohmann::json &jsonObject)
2361 {
2362 if (target == nullptr) {
2363 ANS_LOGE("Invalid input parameter.");
2364 return false;
2365 }
2366
2367 const auto &jsonEnd = jsonObject.cend();
2368
2369 if (jsonObject.find("agentBundle") != jsonEnd) {
2370 auto bundleOptionObj = jsonObject.at("agentBundle");
2371 if (!bundleOptionObj.is_null()) {
2372 auto *pBundleOption = NotificationJsonConverter::ConvertFromJson<NotificationBundleOption>(bundleOptionObj);
2373 if (pBundleOption == nullptr) {
2374 ANS_LOGE("Failed to parse agentBundle!");
2375 return false;
2376 }
2377
2378 target->agentBundle_ = std::shared_ptr<NotificationBundleOption>(pBundleOption);
2379 }
2380 }
2381
2382 return true;
2383 }
2384
IsCommonLiveView() const2385 bool NotificationRequest::IsCommonLiveView() const
2386 {
2387 return (slotType_ == NotificationConstant::SlotType::LIVE_VIEW) &&
2388 (notificationContentType_ == NotificationContent::Type::LIVE_VIEW);
2389 }
2390
IsSystemLiveView() const2391 bool NotificationRequest::IsSystemLiveView() const
2392 {
2393 return (slotType_ == NotificationConstant::SlotType::LIVE_VIEW) &&
2394 (notificationContentType_ == NotificationContent::Type::LOCAL_LIVE_VIEW);
2395 }
2396
CheckVersion(const sptr<NotificationRequest> & oldRequest) const2397 ErrCode NotificationRequest::CheckVersion(const sptr<NotificationRequest> &oldRequest) const
2398 {
2399 auto content = notificationContent_->GetNotificationContent();
2400 auto liveView = std::static_pointer_cast<NotificationLiveViewContent>(content);
2401 auto oldContent = oldRequest->GetContent()->GetNotificationContent();
2402 auto oldLiveView = std::static_pointer_cast<NotificationLiveViewContent>(oldContent);
2403
2404 if (oldLiveView->GetVersion() == NotificationLiveViewContent::MAX_VERSION) {
2405 return ERR_OK;
2406 }
2407 if (liveView->GetVersion() == NotificationLiveViewContent::MAX_VERSION) {
2408 ANS_LOGE("Invalid version, creator bundle name %{public}s, id %{public}d, "
2409 "old version %{public}u, new version %{public}u.", GetCreatorBundleName().c_str(),
2410 GetNotificationId(), oldLiveView->GetVersion(), liveView->GetVersion());
2411 return ERR_ANS_EXPIRED_NOTIFICATION;
2412 }
2413 if (oldLiveView->GetVersion() >= liveView->GetVersion()) {
2414 ANS_LOGE("Live view has finished, creator bundle name %{public}s, id %{public}d, "
2415 "old version %{public}u, new version %{public}u.", GetCreatorBundleName().c_str(),
2416 GetNotificationId(), oldLiveView->GetVersion(), liveView->GetVersion());
2417 return ERR_ANS_EXPIRED_NOTIFICATION;
2418 }
2419 return ERR_OK;
2420 }
2421
CheckNotificationRequest(const sptr<NotificationRequest> & oldRequest) const2422 ErrCode NotificationRequest::CheckNotificationRequest(const sptr<NotificationRequest> &oldRequest) const
2423 {
2424 if (!IsCommonLiveView()) {
2425 if ((oldRequest != nullptr) && oldRequest->IsCommonLiveView()) {
2426 ANS_LOGE("Invalid new request param, slot type %{public}d, content type %{public}d.",
2427 GetSlotType(), GetNotificationType());
2428 return ERR_ANS_INVALID_PARAM;
2429 }
2430 return ERR_OK;
2431 }
2432
2433 using StatusType = NotificationLiveViewContent::LiveViewStatus;
2434 auto content = notificationContent_->GetNotificationContent();
2435 auto liveView = std::static_pointer_cast<NotificationLiveViewContent>(content);
2436 auto status = liveView->GetLiveViewStatus();
2437 if (oldRequest == nullptr) {
2438 if (status != StatusType::LIVE_VIEW_CREATE) {
2439 ANS_LOGE("Doesn't exist live view, bundle name %{public}s, id %{public}d.",
2440 GetCreatorBundleName().c_str(), GetNotificationId());
2441 return ERR_ANS_NOTIFICATION_NOT_EXISTS;
2442 }
2443 return ERR_OK;
2444 }
2445
2446 if (!oldRequest->IsCommonLiveView()) {
2447 ANS_LOGE("Invalid old request param, slot type %{public}d, content type %{public}d.",
2448 oldRequest->GetSlotType(), oldRequest->GetNotificationType());
2449 return ERR_ANS_INVALID_PARAM;
2450 }
2451
2452 if (status == StatusType::LIVE_VIEW_CREATE) {
2453 ANS_LOGW("Repeat create live view, bundle name %{public}s, id %{public}d.",
2454 GetCreatorBundleName().c_str(), GetNotificationId());
2455 return ERR_ANS_REPEAT_CREATE;
2456 }
2457
2458 auto oldContent = oldRequest->GetContent()->GetNotificationContent();
2459 auto oldLiveView = std::static_pointer_cast<NotificationLiveViewContent>(oldContent);
2460 auto oldStatus = oldLiveView->GetLiveViewStatus();
2461 if (oldStatus == StatusType::LIVE_VIEW_END) {
2462 ANS_LOGW("Live view has finished, bundle name %{public}s, id %{public}d.",
2463 GetCreatorBundleName().c_str(), GetNotificationId());
2464 return ERR_ANS_END_NOTIFICATION;
2465 }
2466
2467 return CheckVersion(oldRequest);
2468 }
2469
FillMissingParameters(const sptr<NotificationRequest> & oldRequest)2470 void NotificationRequest::FillMissingParameters(const sptr<NotificationRequest> &oldRequest)
2471 {
2472 if (!IsCommonLiveView() || (oldRequest == nullptr)) {
2473 return;
2474 }
2475
2476 updateDeadLine_ = oldRequest->updateDeadLine_;
2477 finishDeadLine_ = oldRequest->finishDeadLine_;
2478 if (autoDeletedTime_ == NotificationConstant::INVALID_AUTO_DELETE_TIME) {
2479 autoDeletedTime_ = oldRequest->autoDeletedTime_;
2480 }
2481 if (wantAgent_ == nullptr) {
2482 wantAgent_ = oldRequest->wantAgent_;
2483 }
2484
2485 auto content = notificationContent_->GetNotificationContent();
2486 auto newLiveViewContent = std::static_pointer_cast<NotificationLiveViewContent>(content);
2487 if (newLiveViewContent->GetLiveViewStatus() ==
2488 NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_FULL_UPDATE) {
2489 return;
2490 }
2491 auto newExtraInfo = newLiveViewContent->GetExtraInfo();
2492 auto oldContent = oldRequest->GetContent()->GetNotificationContent();
2493 auto oldLiveViewContent = std::static_pointer_cast<NotificationLiveViewContent>(oldContent);
2494 auto oldExtraInfo = oldLiveViewContent->GetExtraInfo();
2495 if (newExtraInfo == nullptr) {
2496 newLiveViewContent->SetExtraInfo(oldExtraInfo);
2497 } else if (oldExtraInfo != nullptr) {
2498 auto oldKeySet = oldExtraInfo->KeySet();
2499 for (const auto &key : oldKeySet) {
2500 if (!newExtraInfo->HasParam(key)) {
2501 newExtraInfo->SetParam(key, oldExtraInfo->GetParam(key));
2502 }
2503 }
2504 }
2505
2506 auto oldIsOnlyLocalUpdate = oldLiveViewContent->GetIsOnlyLocalUpdate();
2507 if (oldIsOnlyLocalUpdate!= newLiveViewContent->GetIsOnlyLocalUpdate()) {
2508 newLiveViewContent->SetIsOnlyLocalUpdate(oldIsOnlyLocalUpdate);
2509 }
2510
2511 auto newPicture = newLiveViewContent->GetPicture();
2512 auto oldPicture = oldLiveViewContent->GetPicture();
2513 bool isSet = false;
2514 for (const auto &pictureRecord : oldPicture) {
2515 if (newPicture.find(pictureRecord.first) != newPicture.end()) {
2516 continue;
2517 }
2518 newPicture[pictureRecord.first] = pictureRecord.second;
2519 isSet = true;
2520 }
2521 if (isSet) {
2522 newLiveViewContent->SetPicture(newPicture);
2523 }
2524 }
2525
GetBaseKey(const std::string & deviceId)2526 std::string NotificationRequest::GetBaseKey(const std::string &deviceId)
2527 {
2528 const char *keySpliter = "_";
2529
2530 std::stringstream stream;
2531 if (IsAgentNotification()) {
2532 stream << deviceId << keySpliter << ownerUserId_ << keySpliter <<
2533 ownerUid_ << keySpliter << ownerBundleName_ << keySpliter <<
2534 label_ << keySpliter << notificationId_;
2535 } else {
2536 stream << deviceId << keySpliter << creatorUserId_ << keySpliter <<
2537 creatorUid_ << keySpliter << creatorBundleName_ << keySpliter <<
2538 label_ << keySpliter << notificationId_;
2539 }
2540 return stream.str();
2541 }
2542
GetKey()2543 std::string NotificationRequest::GetKey()
2544 {
2545 std::stringstream stream;
2546 const char *keySpliter = "_";
2547 stream << REQUEST_STORAGE_KEY_PREFIX << keySpliter << GetBaseKey("");
2548 return stream.str();
2549 }
2550
GetSecureKey()2551 std::string NotificationRequest::GetSecureKey()
2552 {
2553 std::stringstream stream;
2554 const char *keySpliter = "_";
2555 stream << REQUEST_STORAGE_SECURE_KEY_PREFIX << keySpliter << GetBaseKey("");
2556 return stream.str();
2557 }
2558
CheckImageOverSizeForPixelMap(const std::shared_ptr<Media::PixelMap> & pixelMap,uint32_t maxSize)2559 bool NotificationRequest::CheckImageOverSizeForPixelMap(
2560 const std::shared_ptr<Media::PixelMap> &pixelMap, uint32_t maxSize)
2561 {
2562 if (pixelMap == nullptr) {
2563 return false;
2564 }
2565
2566 auto size = static_cast<uint32_t>(pixelMap->GetByteCount());
2567 return size > maxSize;
2568 }
2569
CheckImageSizeForConverSation(std::shared_ptr<NotificationBasicContent> & content)2570 ErrCode NotificationRequest::CheckImageSizeForConverSation(std::shared_ptr<NotificationBasicContent> &content)
2571 {
2572 auto conversationalContent = std::static_pointer_cast<NotificationConversationalContent>(content);
2573 auto picture = conversationalContent->GetMessageUser().GetPixelMap();
2574 if (CheckImageOverSizeForPixelMap(picture, MAX_ICON_SIZE)) {
2575 ANS_LOGE("The size of picture in ConversationalContent's message user exceeds limit");
2576 return ERR_ANS_ICON_OVER_SIZE;
2577 }
2578
2579 auto messages = conversationalContent->GetAllConversationalMessages();
2580 for (auto &msg : messages) {
2581 if (!msg) {
2582 continue;
2583 }
2584 auto img = msg->GetSender().GetPixelMap();
2585 if (CheckImageOverSizeForPixelMap(img, MAX_ICON_SIZE)) {
2586 ANS_LOGE("The size of picture in ConversationalContent's message exceeds limit");
2587 return ERR_ANS_ICON_OVER_SIZE;
2588 }
2589 }
2590 return ERR_OK;
2591 }
2592
CheckImageSizeForPicture(std::shared_ptr<NotificationBasicContent> & content)2593 ErrCode NotificationRequest::CheckImageSizeForPicture(std::shared_ptr<NotificationBasicContent> &content)
2594 {
2595 auto pictureContent = std::static_pointer_cast<NotificationPictureContent>(content);
2596 auto bigPicture = pictureContent->GetBigPicture();
2597 if (CheckImageOverSizeForPixelMap(bigPicture, MAX_PICTURE_SIZE)) {
2598 ANS_LOGE("The size of big picture in PictureContent exceeds limit");
2599 return ERR_ANS_PICTURE_OVER_SIZE;
2600 }
2601 return ERR_OK;
2602 }
2603
CheckImageSizeForLiveView(std::shared_ptr<NotificationBasicContent> & content)2604 ErrCode NotificationRequest::CheckImageSizeForLiveView(std::shared_ptr<NotificationBasicContent> &content)
2605 {
2606 auto liveViewContent = std::static_pointer_cast<NotificationLiveViewContent>(content);
2607 auto pictureMap = liveViewContent->GetPicture();
2608 for (const auto &pixelMapRecord : pictureMap) {
2609 if (pixelMapRecord.second.empty()) {
2610 ANS_LOGE("Picture key exist, but picture content is empty.");
2611 return ERR_ANS_INVALID_PARAM;
2612 }
2613 if (pixelMapRecord.second.size() > MAX_LIVE_VIEW_ICON_NUM) {
2614 ANS_LOGE("Picture key exist, but picture content count exceeds limit.");
2615 return ERR_ANS_INVALID_PARAM;
2616 }
2617 for (const auto &pixelMap : pixelMapRecord.second) {
2618 if (CheckImageOverSizeForPixelMap(pixelMap, MAX_ICON_SIZE)) {
2619 ANS_LOGE("The size of big picture in PictureContent exceeds limit.");
2620 return ERR_ANS_ICON_OVER_SIZE;
2621 }
2622 }
2623 }
2624 return ERR_OK;
2625 }
2626
CheckImageSizeForContent() const2627 ErrCode NotificationRequest::CheckImageSizeForContent() const
2628 {
2629 auto content = GetContent();
2630 if (content == nullptr) {
2631 ANS_LOGE("Invalid content in NotificationRequest");
2632 return ERR_OK;
2633 }
2634
2635 auto basicContent = GetContent()->GetNotificationContent();
2636 if (basicContent == nullptr) {
2637 ANS_LOGE("Invalid content in NotificationRequest");
2638 return ERR_OK;
2639 }
2640
2641 if (GetSlotType() == NotificationConstant::SlotType::LIVE_VIEW) {
2642 auto result = CheckLockScreenPictureSizeForLiveView(basicContent);
2643 if (result != ERR_OK) {
2644 return result;
2645 }
2646 }
2647
2648 auto contentType = GetNotificationType();
2649 switch (contentType) {
2650 case NotificationContent::Type::CONVERSATION:
2651 return CheckImageSizeForConverSation(basicContent);
2652 case NotificationContent::Type::PICTURE:
2653 return CheckImageSizeForPicture(basicContent);
2654 case NotificationContent::Type::LIVE_VIEW:
2655 return CheckImageSizeForLiveView(basicContent);
2656 default:
2657 return ERR_OK;
2658 }
2659 }
2660
SetIsCoverActionButtons(bool isCoverActionButtons)2661 void NotificationRequest::SetIsCoverActionButtons(bool isCoverActionButtons)
2662 {
2663 isCoverActionButtons_ = isCoverActionButtons;
2664 }
2665
IsCoverActionButtons() const2666 bool NotificationRequest::IsCoverActionButtons() const
2667 {
2668 return isCoverActionButtons_;
2669 }
2670
SetAppMessageId(const std::string & appMessageId)2671 void NotificationRequest::SetAppMessageId(const std::string &appMessageId)
2672 {
2673 appMessageId_ = appMessageId;
2674 }
2675
GetAppMessageId() const2676 std::string NotificationRequest::GetAppMessageId() const
2677 {
2678 return appMessageId_;
2679 }
2680
SetSound(const std::string & sound)2681 void NotificationRequest::SetSound(const std::string &sound)
2682 {
2683 sound_ = sound;
2684 }
2685
GetSound() const2686 std::string NotificationRequest::GetSound() const
2687 {
2688 return sound_;
2689 }
2690
GenerateUniqueKey()2691 std::string NotificationRequest::GenerateUniqueKey()
2692 {
2693 const char *keySpliter = "_";
2694 int typeFlag = 0;
2695 if (GetSlotType() == NotificationConstant::SlotType::LIVE_VIEW) {
2696 typeFlag = 1;
2697 }
2698
2699 std::stringstream stream;
2700 if (IsAgentNotification()) {
2701 stream << ownerUid_ << keySpliter << ownerBundleName_ << keySpliter << ownerUserId_ << keySpliter <<
2702 typeFlag << keySpliter << appMessageId_;
2703 } else {
2704 stream << creatorUid_ << keySpliter << creatorBundleName_ << keySpliter << creatorUserId_ << keySpliter <<
2705 typeFlag << keySpliter << appMessageId_;
2706 }
2707 return stream.str();
2708 }
2709
SetUnifiedGroupInfo(const std::shared_ptr<NotificationUnifiedGroupInfo> & unifiedGroupInfo)2710 void NotificationRequest::SetUnifiedGroupInfo(const std::shared_ptr<NotificationUnifiedGroupInfo> &unifiedGroupInfo)
2711 {
2712 unifiedGroupInfo_ = unifiedGroupInfo;
2713 }
2714
GetUnifiedGroupInfo() const2715 std::shared_ptr<NotificationUnifiedGroupInfo> NotificationRequest::GetUnifiedGroupInfo() const
2716 {
2717 return unifiedGroupInfo_;
2718 }
2719
CheckLockScreenPictureSizeForLiveView(std::shared_ptr<NotificationBasicContent> & content)2720 ErrCode NotificationRequest::CheckLockScreenPictureSizeForLiveView(std::shared_ptr<NotificationBasicContent> &content)
2721 {
2722 auto lockScreenPicture = content->GetLockScreenPicture();
2723 if (CheckImageOverSizeForPixelMap(lockScreenPicture, MAX_PICTURE_SIZE)) {
2724 ANS_LOGE("The size of lockScreen picture in live view exceeds limit");
2725 return ERR_ANS_PICTURE_OVER_SIZE;
2726 }
2727 return ERR_OK;
2728 }
2729
SetPublishDelayTime(uint32_t delayTime)2730 void NotificationRequest::SetPublishDelayTime(uint32_t delayTime)
2731 {
2732 publishDelayTime_ = delayTime;
2733 }
2734
GetPublishDelayTime() const2735 uint32_t NotificationRequest::GetPublishDelayTime() const
2736 {
2737 return publishDelayTime_;
2738 }
2739
SetUpdateByOwnerAllowed(bool isUpdateByOwnerAllowed)2740 void NotificationRequest::SetUpdateByOwnerAllowed(bool isUpdateByOwnerAllowed)
2741 {
2742 isUpdateByOwnerAllowed_ = isUpdateByOwnerAllowed;
2743 }
2744
IsUpdateByOwnerAllowed() const2745 bool NotificationRequest::IsUpdateByOwnerAllowed() const
2746 {
2747 return isUpdateByOwnerAllowed_;
2748 }
2749
GetLittleIconType() const2750 const std::string NotificationRequest::GetLittleIconType() const
2751 {
2752 return littleIconType_;
2753 }
2754 } // namespace Notification
2755 } // namespace OHOS
2756