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