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 constexpr int32_t PKG_INSTALL_STATUS_UNKMOWN = -1;
58 constexpr int32_t PKG_INSTALL_STATUS_UNINSTALL = 0;
59
NotificationRequest(int32_t notificationId)60 NotificationRequest::NotificationRequest(int32_t notificationId) : notificationId_(notificationId)
61 {
62 createTime_ = GetNowSysTime();
63 deliveryTime_ = GetNowSysTime();
64 }
65
NotificationRequest(const NotificationRequest & other)66 NotificationRequest::NotificationRequest(const NotificationRequest &other)
67 {
68 CopyBase(other);
69 CopyOther(other);
70 }
71
operator =(const NotificationRequest & other)72 NotificationRequest &NotificationRequest::operator=(const NotificationRequest &other)
73 {
74 CopyBase(other);
75 CopyOther(other);
76
77 return *this;
78 }
79
~NotificationRequest()80 NotificationRequest::~NotificationRequest()
81 {}
82
IsInProgress() const83 bool NotificationRequest::IsInProgress() const
84 {
85 return inProgress_;
86 }
87
SetInProgress(bool isOngoing)88 void NotificationRequest::SetInProgress(bool isOngoing)
89 {
90 inProgress_ = isOngoing;
91 }
92
IsUnremovable() const93 bool NotificationRequest::IsUnremovable() const
94 {
95 return unremovable_;
96 }
97
SetUnremovable(bool isUnremovable)98 void NotificationRequest::SetUnremovable(bool isUnremovable)
99 {
100 unremovable_ = isUnremovable;
101 }
102
SetBadgeNumber(uint32_t number)103 void NotificationRequest::SetBadgeNumber(uint32_t number)
104 {
105 badgeNumber_ = number;
106 }
107
GetBadgeNumber() const108 uint32_t NotificationRequest::GetBadgeNumber() const
109 {
110 return badgeNumber_;
111 }
112
SetNotificationControlFlags(uint32_t notificationControlFlags)113 void NotificationRequest::SetNotificationControlFlags(uint32_t notificationControlFlags)
114 {
115 notificationControlFlags_ = notificationControlFlags;
116 }
117
GetNotificationControlFlags() const118 uint32_t NotificationRequest::GetNotificationControlFlags() const
119 {
120 return notificationControlFlags_;
121 }
122
SetNotificationId(int32_t notificationId)123 void NotificationRequest::SetNotificationId(int32_t notificationId)
124 {
125 notificationId_ = notificationId;
126 }
127
GetNotificationId() const128 int32_t NotificationRequest::GetNotificationId() const
129 {
130 return notificationId_;
131 }
132
SetWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> & wantAgent)133 void NotificationRequest::SetWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> &wantAgent)
134 {
135 wantAgent_ = wantAgent;
136 }
137
GetWantAgent() const138 const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> NotificationRequest::GetWantAgent() const
139 {
140 return wantAgent_;
141 }
142
SetRemovalWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> & wantAgent)143 void NotificationRequest::SetRemovalWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> &wantAgent)
144 {
145 removalWantAgent_ = wantAgent;
146 }
147
GetRemovalWantAgent() const148 const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> NotificationRequest::GetRemovalWantAgent() const
149 {
150 return removalWantAgent_;
151 }
152
SetMaxScreenWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> & wantAgent)153 void NotificationRequest::SetMaxScreenWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> &wantAgent)
154 {
155 maxScreenWantAgent_ = wantAgent;
156 }
157
GetMaxScreenWantAgent() const158 const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> NotificationRequest::GetMaxScreenWantAgent() const
159 {
160 return maxScreenWantAgent_;
161 }
162
SetAdditionalData(const std::shared_ptr<AAFwk::WantParams> & extras)163 void NotificationRequest::SetAdditionalData(const std::shared_ptr<AAFwk::WantParams> &extras)
164 {
165 additionalParams_ = extras;
166 }
167
GetAdditionalData() const168 const std::shared_ptr<AAFwk::WantParams> NotificationRequest::GetAdditionalData() const
169 {
170 return additionalParams_;
171 }
172
SetExtendInfo(const std::shared_ptr<AAFwk::WantParams> & extendInfo)173 void NotificationRequest::SetExtendInfo(const std::shared_ptr<AAFwk::WantParams> &extendInfo)
174 {
175 extendInfo_ = extendInfo;
176 }
177
GetExtendInfo() const178 const std::shared_ptr<AAFwk::WantParams> NotificationRequest::GetExtendInfo() const
179 {
180 return extendInfo_;
181 }
182
SetDeliveryTime(int64_t deliveryTime)183 void NotificationRequest::SetDeliveryTime(int64_t deliveryTime)
184 {
185 deliveryTime_ = deliveryTime;
186 }
187
GetDeliveryTime() const188 int64_t NotificationRequest::GetDeliveryTime() const
189 {
190 return deliveryTime_;
191 }
192
IsShowDeliveryTime() const193 bool NotificationRequest::IsShowDeliveryTime() const
194 {
195 return (deliveryTime_ != 0) && showDeliveryTime_;
196 }
197
SetShowDeliveryTime(bool showDeliveryTime)198 void NotificationRequest::SetShowDeliveryTime(bool showDeliveryTime)
199 {
200 showDeliveryTime_ = showDeliveryTime;
201 }
202
AddActionButton(const std::shared_ptr<NotificationActionButton> & actionButton)203 void NotificationRequest::AddActionButton(const std::shared_ptr<NotificationActionButton> &actionButton)
204 {
205 if (!actionButton) {
206 ANS_LOGW("null actionButton");
207 return;
208 }
209
210 if (actionButtons_.size() >= NotificationRequest::MAX_ACTION_BUTTONS) {
211 ANS_LOGW("three action buttons have been already added");
212 return;
213 }
214
215 actionButtons_.emplace_back(actionButton);
216 }
217
GetActionButtons() const218 const std::vector<std::shared_ptr<NotificationActionButton>> NotificationRequest::GetActionButtons() const
219 {
220 return actionButtons_;
221 }
222
ClearActionButtons()223 void NotificationRequest::ClearActionButtons()
224 {
225 actionButtons_.clear();
226 }
227
IsPermitSystemGeneratedContextualActionButtons() const228 bool NotificationRequest::IsPermitSystemGeneratedContextualActionButtons() const
229 {
230 return permitted_;
231 }
232
SetPermitSystemGeneratedContextualActionButtons(bool permitted)233 void NotificationRequest::SetPermitSystemGeneratedContextualActionButtons(bool permitted)
234 {
235 permitted_ = permitted;
236 }
237
IsAgentNotification() const238 bool NotificationRequest::IsAgentNotification() const
239 {
240 return isAgent_;
241 }
242
SetIsAgentNotification(bool isAgent)243 void NotificationRequest::SetIsAgentNotification(bool isAgent)
244 {
245 isAgent_ = isAgent;
246 }
247
AddMessageUser(const std::shared_ptr<MessageUser> & messageUser)248 void NotificationRequest::AddMessageUser(const std::shared_ptr<MessageUser> &messageUser)
249 {
250 if (!messageUser) {
251 ANS_LOGE("null messageUser");
252 return;
253 }
254
255 messageUsers_.emplace_back(messageUser);
256 }
257
GetMessageUsers() const258 const std::vector<std::shared_ptr<MessageUser>> NotificationRequest::GetMessageUsers() const
259 {
260 return messageUsers_;
261 }
262
IsAlertOneTime() const263 bool NotificationRequest::IsAlertOneTime() const
264 {
265 return alertOneTime_;
266 }
267
SetAlertOneTime(bool isAlertOnce)268 void NotificationRequest::SetAlertOneTime(bool isAlertOnce)
269 {
270 alertOneTime_ = isAlertOnce;
271 }
272
SetAutoDeletedTime(int64_t deletedTime)273 void NotificationRequest::SetAutoDeletedTime(int64_t deletedTime)
274 {
275 autoDeletedTime_ = deletedTime;
276 }
277
GetAutoDeletedTime() const278 int64_t NotificationRequest::GetAutoDeletedTime() const
279 {
280 return autoDeletedTime_;
281 }
282
SetUpdateDeadLine(int64_t updateDeadLine)283 void NotificationRequest::SetUpdateDeadLine(int64_t updateDeadLine)
284 {
285 updateDeadLine_ = updateDeadLine;
286 }
287
GetUpdateDeadLine() const288 int64_t NotificationRequest::GetUpdateDeadLine() const
289 {
290 return updateDeadLine_;
291 }
292
SetFinishDeadLine(int64_t finishDeadLine)293 void NotificationRequest::SetFinishDeadLine(int64_t finishDeadLine)
294 {
295 finishDeadLine_ = finishDeadLine;
296 }
297
GetFinishDeadLine() const298 int64_t NotificationRequest::GetFinishDeadLine() const
299 {
300 return finishDeadLine_;
301 }
302
SetArchiveDeadLine(int64_t archiveDeadLine)303 void NotificationRequest::SetArchiveDeadLine(int64_t archiveDeadLine)
304 {
305 archiveDeadLine_ = archiveDeadLine;
306 }
307
GetArchiveDeadLine() const308 int64_t NotificationRequest::GetArchiveDeadLine() const
309 {
310 return archiveDeadLine_;
311 }
312
SetLittleIcon(const std::shared_ptr<Media::PixelMap> & littleIcon)313 void NotificationRequest::SetLittleIcon(const std::shared_ptr<Media::PixelMap> &littleIcon)
314 {
315 littleIcon_ = littleIcon;
316 }
317
GetLittleIcon() const318 const std::shared_ptr<Media::PixelMap> NotificationRequest::GetLittleIcon() const
319 {
320 return littleIcon_;
321 }
322
SetBigIcon(const std::shared_ptr<Media::PixelMap> & bigIcon)323 void NotificationRequest::SetBigIcon(const std::shared_ptr<Media::PixelMap> &bigIcon)
324 {
325 bigIcon_ = bigIcon;
326 }
327
ResetBigIcon() const328 void NotificationRequest::ResetBigIcon() const
329 {
330 bigIcon_ = nullptr;
331 }
332
GetBigIcon() const333 const std::shared_ptr<Media::PixelMap> NotificationRequest::GetBigIcon() const
334 {
335 return bigIcon_;
336 }
337
SetOverlayIcon(const std::shared_ptr<Media::PixelMap> & overlayIcon)338 void NotificationRequest::SetOverlayIcon(const std::shared_ptr<Media::PixelMap> &overlayIcon)
339 {
340 overlayIcon_ = overlayIcon;
341 }
342
GetOverlayIcon() const343 const std::shared_ptr<Media::PixelMap> NotificationRequest::GetOverlayIcon() const
344 {
345 return overlayIcon_;
346 }
347
SetClassification(const std::string & classification)348 void NotificationRequest::SetClassification(const std::string &classification)
349 {
350 classification_ = classification;
351 }
352
GetClassification() const353 std::string NotificationRequest::GetClassification() const
354 {
355 return classification_;
356 }
357
SetColor(uint32_t color)358 void NotificationRequest::SetColor(uint32_t color)
359 {
360 color_ = color;
361 if (NotificationRequest::COLOR_DEFAULT != color_) {
362 color_ = color_ | NotificationRequest::COLOR_MASK;
363 }
364 }
365
GetColor() const366 uint32_t NotificationRequest::GetColor() const
367 {
368 return color_;
369 }
370
IsColorEnabled() const371 bool NotificationRequest::IsColorEnabled() const
372 {
373 if (!colorEnabled_) {
374 return false;
375 }
376
377 // no valid content
378 if (!notificationContent_) {
379 ANS_LOGE("no valid notification content");
380 return false;
381 }
382
383 // not a media content
384 if (NotificationContent::Type::MEDIA != notificationContentType_) {
385 ANS_LOGE("not a media notification content");
386 return false;
387 }
388
389 auto basicContent = notificationContent_->GetNotificationContent();
390 auto mediaContent = std::static_pointer_cast<NotificationMediaContent>(basicContent);
391 if (!mediaContent->GetAVToken()) {
392 ANS_LOGE("AVToken has not been attached");
393 return false;
394 }
395
396 return true;
397 }
398
SetColorEnabled(bool colorEnabled)399 void NotificationRequest::SetColorEnabled(bool colorEnabled)
400 {
401 colorEnabled_ = colorEnabled;
402 }
403
SetContent(const std::shared_ptr<NotificationContent> & content)404 void NotificationRequest::SetContent(const std::shared_ptr<NotificationContent> &content)
405 {
406 notificationContent_ = content;
407
408 if (notificationContent_) {
409 notificationContentType_ = notificationContent_->GetContentType();
410 return;
411 }
412
413 notificationContentType_ = NotificationContent::Type::NONE;
414 }
415
GetContent() const416 const std::shared_ptr<NotificationContent> NotificationRequest::GetContent() const
417 {
418 return notificationContent_;
419 }
420
GetNotificationType() const421 NotificationContent::Type NotificationRequest::GetNotificationType() const
422 {
423 return notificationContentType_;
424 }
425
IsCountdownTimer() const426 bool NotificationRequest::IsCountdownTimer() const
427 {
428 return isCountdown_;
429 }
430
SetCountdownTimer(bool isCountDown)431 void NotificationRequest::SetCountdownTimer(bool isCountDown)
432 {
433 isCountdown_ = isCountDown;
434 }
435
SetGroupAlertType(NotificationRequest::GroupAlertType type)436 void NotificationRequest::SetGroupAlertType(NotificationRequest::GroupAlertType type)
437 {
438 groupAlertType_ = type;
439 }
440
GetGroupAlertType() const441 NotificationRequest::GroupAlertType NotificationRequest::GetGroupAlertType() const
442 {
443 return groupAlertType_;
444 }
445
IsGroupOverview() const446 bool NotificationRequest::IsGroupOverview() const
447 {
448 return groupOverview_;
449 }
450
SetGroupOverview(bool overView)451 void NotificationRequest::SetGroupOverview(bool overView)
452 {
453 groupOverview_ = overView;
454 }
455
SetGroupName(const std::string & groupName)456 void NotificationRequest::SetGroupName(const std::string &groupName)
457 {
458 groupName_ = groupName;
459 }
460
GetGroupName() const461 std::string NotificationRequest::GetGroupName() const
462 {
463 return groupName_;
464 }
465
IsOnlyLocal() const466 bool NotificationRequest::IsOnlyLocal() const
467 {
468 return onlyLocal_;
469 }
470
SetOnlyLocal(bool flag)471 void NotificationRequest::SetOnlyLocal(bool flag)
472 {
473 onlyLocal_ = flag;
474 }
475
SetSettingsText(const std::string & text)476 void NotificationRequest::SetSettingsText(const std::string &text)
477 {
478 if ((NotificationContent::Type::LONG_TEXT == notificationContentType_) ||
479 (NotificationContent::Type::PICTURE == notificationContentType_)) {
480 ANS_LOGW("This method is invalid if the notification content type has been set to LONG_TEXT or PICTURE.");
481 return;
482 }
483
484 settingsText_ = text;
485 }
486
GetSettingsText() const487 std::string NotificationRequest::GetSettingsText() const
488 {
489 return settingsText_;
490 }
491
GetCreateTime() const492 int64_t NotificationRequest::GetCreateTime() const
493 {
494 return createTime_;
495 }
496
SetCreateTime(int64_t createTime)497 void NotificationRequest::SetCreateTime(int64_t createTime)
498 {
499 createTime_ = createTime;
500 }
501
IsShowStopwatch() const502 bool NotificationRequest::IsShowStopwatch() const
503 {
504 return showStopwatch_;
505 }
506
SetShowStopwatch(bool isShow)507 void NotificationRequest::SetShowStopwatch(bool isShow)
508 {
509 showStopwatch_ = isShow;
510 }
511
SetSlotType(NotificationConstant::SlotType slotType)512 void NotificationRequest::SetSlotType(NotificationConstant::SlotType slotType)
513 {
514 slotType_ = slotType;
515 }
516
GetSlotType() const517 NotificationConstant::SlotType NotificationRequest::GetSlotType() const
518 {
519 return slotType_;
520 }
521
SetSortingKey(const std::string & key)522 void NotificationRequest::SetSortingKey(const std::string &key)
523 {
524 sortingKey_ = key;
525 }
526
GetSortingKey() const527 std::string NotificationRequest::GetSortingKey() const
528 {
529 return sortingKey_;
530 }
531
SetStatusBarText(const std::string & text)532 void NotificationRequest::SetStatusBarText(const std::string &text)
533 {
534 statusBarText_ = text;
535 }
536
GetStatusBarText() const537 std::string NotificationRequest::GetStatusBarText() const
538 {
539 return statusBarText_;
540 }
541
IsTapDismissed() const542 bool NotificationRequest::IsTapDismissed() const
543 {
544 return tapDismissed_;
545 }
546
SetTapDismissed(bool isDismissed)547 void NotificationRequest::SetTapDismissed(bool isDismissed)
548 {
549 tapDismissed_ = isDismissed;
550 }
551
SetVisibleness(NotificationConstant::VisiblenessType type)552 void NotificationRequest::SetVisibleness(NotificationConstant::VisiblenessType type)
553 {
554 visiblenessType_ = type;
555 }
556
GetVisibleness() const557 NotificationConstant::VisiblenessType NotificationRequest::GetVisibleness() const
558 {
559 return visiblenessType_;
560 }
561
SetBadgeIconStyle(NotificationRequest::BadgeStyle style)562 void NotificationRequest::SetBadgeIconStyle(NotificationRequest::BadgeStyle style)
563 {
564 badgeStyle_ = style;
565 }
566
GetBadgeIconStyle() const567 NotificationRequest::BadgeStyle NotificationRequest::GetBadgeIconStyle() const
568 {
569 return badgeStyle_;
570 }
571
SetShortcutId(const std::string & shortcutId)572 void NotificationRequest::SetShortcutId(const std::string &shortcutId)
573 {
574 shortcutId_ = shortcutId;
575 }
576
GetShortcutId() const577 std::string NotificationRequest::GetShortcutId() const
578 {
579 return shortcutId_;
580 }
581
SetFloatingIcon(bool floatingIcon)582 void NotificationRequest::SetFloatingIcon(bool floatingIcon)
583 {
584 floatingIcon_ = floatingIcon;
585 }
586
IsFloatingIcon() const587 bool NotificationRequest::IsFloatingIcon() const
588 {
589 return floatingIcon_;
590 }
591
SetProgressBar(int32_t progress,int32_t progressMax,bool indeterminate)592 void NotificationRequest::SetProgressBar(int32_t progress, int32_t progressMax, bool indeterminate)
593 {
594 progressValue_ = progress;
595 progressMax_ = progressMax;
596 progressIndeterminate_ = indeterminate;
597 }
598
GetProgressMax() const599 int32_t NotificationRequest::GetProgressMax() const
600 {
601 return progressMax_;
602 }
603
GetProgressValue() const604 int32_t NotificationRequest::GetProgressValue() const
605 {
606 return progressValue_;
607 }
608
IsProgressIndeterminate() const609 bool NotificationRequest::IsProgressIndeterminate() const
610 {
611 return progressIndeterminate_;
612 }
613
SetNotificationUserInputHistory(const std::vector<std::string> & text)614 void NotificationRequest::SetNotificationUserInputHistory(const std::vector<std::string> &text)
615 {
616 if (text.empty()) {
617 userInputHistory_.clear();
618 return;
619 }
620
621 auto vsize = std::min(NotificationRequest::MAX_USER_INPUT_HISTORY, text.size());
622 userInputHistory_.assign(text.begin(), text.begin() + vsize);
623 }
624
GetNotificationUserInputHistory() const625 std::vector<std::string> NotificationRequest::GetNotificationUserInputHistory() const
626 {
627 return userInputHistory_;
628 }
629
GetNotificationHashCode() const630 std::string NotificationRequest::GetNotificationHashCode() const
631 {
632 if (creatorBundleName_.empty() || (creatorUid_ == 0) || ownerBundleName_.empty()) {
633 return "";
634 }
635
636 return std::to_string(notificationId_) + "_" + creatorBundleName_ + "_" + std::to_string(creatorUid_) + "_" +
637 ownerBundleName_;
638 }
639
SetOwnerBundleName(const std::string & ownerName)640 void NotificationRequest::SetOwnerBundleName(const std::string &ownerName)
641 {
642 ownerBundleName_ = ownerName;
643 }
644
GetOwnerBundleName() const645 std::string NotificationRequest::GetOwnerBundleName() const
646 {
647 return ownerBundleName_;
648 }
649
SetCreatorBundleName(const std::string & creatorName)650 void NotificationRequest::SetCreatorBundleName(const std::string &creatorName)
651 {
652 creatorBundleName_ = creatorName;
653 }
654
GetCreatorBundleName() const655 std::string NotificationRequest::GetCreatorBundleName() const
656 {
657 return creatorBundleName_;
658 }
659
SetCreatorPid(pid_t pid)660 void NotificationRequest::SetCreatorPid(pid_t pid)
661 {
662 creatorPid_ = pid;
663 }
664
GetCreatorPid() const665 pid_t NotificationRequest::GetCreatorPid() const
666 {
667 return creatorPid_;
668 }
669
SetCreatorUid(int32_t uid)670 void NotificationRequest::SetCreatorUid(int32_t uid)
671 {
672 creatorUid_ = uid;
673 }
674
GetCreatorUid() const675 int32_t NotificationRequest::GetCreatorUid() const
676 {
677 return creatorUid_;
678 }
679
SetOwnerUid(int32_t uid)680 void NotificationRequest::SetOwnerUid(int32_t uid)
681 {
682 ownerUid_ = uid;
683 }
684
GetOwnerUid() const685 int32_t NotificationRequest::GetOwnerUid() const
686 {
687 return ownerUid_;
688 }
689
SetLabel(const std::string & label)690 void NotificationRequest::SetLabel(const std::string &label)
691 {
692 label_ = label;
693 }
694
GetLabel() const695 std::string NotificationRequest::GetLabel() const
696 {
697 return label_;
698 }
699
SetDistributed(bool distribute)700 void NotificationRequest::SetDistributed(bool distribute)
701 {
702 distributedOptions_.SetDistributed(distribute);
703 }
704
SetDevicesSupportDisplay(const std::vector<std::string> & devices)705 void NotificationRequest::SetDevicesSupportDisplay(const std::vector<std::string> &devices)
706 {
707 distributedOptions_.SetDevicesSupportDisplay(devices);
708 }
709
SetDevicesSupportOperate(const std::vector<std::string> & devices)710 void NotificationRequest::SetDevicesSupportOperate(const std::vector<std::string> &devices)
711 {
712 distributedOptions_.SetDevicesSupportOperate(devices);
713 }
714
GetNotificationDistributedOptions() const715 NotificationDistributedOptions NotificationRequest::GetNotificationDistributedOptions() const
716 {
717 return distributedOptions_;
718 }
719
SetCreatorUserId(int32_t userId)720 void NotificationRequest::SetCreatorUserId(int32_t userId)
721 {
722 creatorUserId_ = userId;
723 }
724
GetCreatorUserId() const725 int32_t NotificationRequest::GetCreatorUserId() const
726 {
727 return creatorUserId_;
728 }
729
SetCreatorInstanceKey(int32_t key)730 void NotificationRequest::SetCreatorInstanceKey(int32_t key)
731 {
732 creatorInstanceKey_ = key;
733 }
734
GetCreatorInstanceKey() const735 int32_t NotificationRequest::GetCreatorInstanceKey() const
736 {
737 return creatorInstanceKey_;
738 }
739
SetAppInstanceKey(const std::string & key)740 void NotificationRequest::SetAppInstanceKey(const std::string &key)
741 {
742 appInstanceKey_ = key;
743 }
744
GetAppInstanceKey() const745 std::string NotificationRequest::GetAppInstanceKey() const
746 {
747 return appInstanceKey_;
748 }
749
SetOwnerUserId(int32_t userId)750 void NotificationRequest::SetOwnerUserId(int32_t userId)
751 {
752 ownerUserId_ = userId;
753 }
754
GetOwnerUserId() const755 int32_t NotificationRequest::GetOwnerUserId() const
756 {
757 return ownerUserId_;
758 }
759
SetHashCodeGenerateType(uint32_t type)760 void NotificationRequest::SetHashCodeGenerateType(uint32_t type)
761 {
762 hashCodeGenerateType_ = type;
763 }
764
GetHashCodeGenerateType() const765 uint32_t NotificationRequest::GetHashCodeGenerateType() const
766 {
767 return hashCodeGenerateType_;
768 }
769
SetCollaboratedReminderFlag(uint32_t reminderFlag)770 void NotificationRequest::SetCollaboratedReminderFlag(uint32_t reminderFlag)
771 {
772 collaboratedReminderFlag_ = reminderFlag;
773 }
774
GetCollaboratedReminderFlag() const775 uint32_t NotificationRequest::GetCollaboratedReminderFlag() const
776 {
777 return collaboratedReminderFlag_;
778 }
779
Dump()780 std::string NotificationRequest::Dump()
781 {
782 return "NotificationRequest{ "
783 "notificationId = " + std::to_string(notificationId_) +
784 ", slotType = " + std::to_string(static_cast<int32_t>(slotType_)) +
785 ", createTime = " + std::to_string(createTime_) + ", deliveryTime = " + std::to_string(deliveryTime_) +
786 ", autoDeletedTime = " + std::to_string(autoDeletedTime_) + ", settingsText = " + settingsText_ +
787 ", creatorBundleName = " + creatorBundleName_ +
788 ", creatorPid = " + std::to_string(static_cast<int32_t>(creatorPid_)) +
789 ", creatorUid = " + std::to_string(static_cast<int32_t>(creatorUid_)) +
790 ", ownerBundleName = " + ownerBundleName_ +
791 ", ownerUid = " + std::to_string(static_cast<int32_t>(ownerUid_)) +
792 ", groupName = " + groupName_ + ", statusBarText = " + statusBarText_ + ", label = " + label_ +
793 ", shortcutId = " + shortcutId_ + ", sortingKey = " + sortingKey_ +
794 ", groupAlertType = " + std::to_string(static_cast<int32_t>(groupAlertType_)) +
795 ", color = " + std::to_string(color_) + ", badgeNumber = " + std::to_string(badgeNumber_) +
796 ", visiblenessType = " + std::to_string(static_cast<int32_t>(visiblenessType_)) +
797 ", progressValue = " + std::to_string(progressValue_) + ", progressMax = " + std::to_string(progressMax_) +
798 ", badgeStyle = " + std::to_string(static_cast<int32_t>(badgeStyle_)) +
799 ", classification = " + classification_ +
800 ", notificationContentType = " + std::to_string(static_cast<int32_t>(notificationContentType_)) +
801 ", notificationControlFlags = " + std::to_string(notificationControlFlags_) +
802 ", showDeliveryTime = " + (showDeliveryTime_ ? "true" : "false") +
803 ", tapDismissed = " + (tapDismissed_ ? "true" : "false") +
804 ", colorEnabled = " + (colorEnabled_ ? "true" : "false") +
805 ", alertOneTime = " + (alertOneTime_ ? "true" : "false") +
806 ", showStopwatch = " + (showStopwatch_ ? "true" : "false") +
807 ", isCountdown = " + (isCountdown_ ? "true" : "false") +
808 ", inProgress = " + (inProgress_ ? "true" : "false") +
809 ", groupOverview = " + (groupOverview_ ? "true" : "false") +
810 ", isRemoveAllowed = " + (isRemoveAllowed_ ? "true" : "false") +
811 ", progressIndeterminate = " + (progressIndeterminate_ ? "true" : "false") +
812 ", unremovable = " + (unremovable_ ? "true" : "false") +
813 ", floatingIcon = " + (floatingIcon_ ? "true" : "false") +
814 ", onlyLocal = " + (onlyLocal_ ? "true" : "false") + ", permitted = " + (permitted_ ? "true" : "false") +
815 ", isAgent = " + (isAgent_ ? "true" : "false") +
816 ", updateOnly = " + (updateOnly_ ? "true" : "false") +
817 ", isForceDistributed = " + (forceDistributed_ ? "true" : "false") +
818 ", isNotDistributed = " + (notDistributed_ ? "true" : "false") +
819 ", isDoNotDisturbByPassed = " + (isDoNotDisturbByPassed_ ? "true" : "false") +
820 ", removalWantAgent = " + (removalWantAgent_ ? "not null" : "null") +
821 ", maxScreenWantAgent = " + (maxScreenWantAgent_ ? "not null" : "null") +
822 ", additionalParams = " + (additionalParams_ ? "not null" : "null") +
823 ", extendInfo = " + (extendInfo_ ? "not null" : "null") +
824 ", littleIcon = " + (littleIcon_ ? "not null" : "null") +
825 ", bigIcon = " + (bigIcon_ ? "not null" : "null") +
826 ", overlayIcon = " + (overlayIcon_ ? "not null" : "null") +
827 ", notificationContent = " + (notificationContent_ ? notificationContent_->Dump() : "null") +
828 ", notificationTemplate = " + (notificationTemplate_ ? "not null" : "null") +
829 ", actionButtons = " + (!actionButtons_.empty() ? actionButtons_.at(0)->Dump() : "empty") +
830 ", messageUsers = " + (!messageUsers_.empty() ? messageUsers_.at(0)->Dump() : "empty") +
831 ", userInputHistory = " + (!userInputHistory_.empty() ? userInputHistory_.at(0) : "empty") +
832 ", distributedOptions = " + distributedOptions_.Dump() +
833 ", notificationFlags = " + (notificationFlags_ ? "not null" : "null") +
834 ", notificationFlagsOfDevices = " + (notificationFlagsOfDevices_ ? "not null" : "null") +
835 ", notificationBundleOption = " + (notificationBundleOption_ != nullptr ? "not null" : "null") +
836 ", agentBundle = " + (agentBundle_ != nullptr ? "not null" : "null") +
837 ", creatorUserId = " + std::to_string(creatorUserId_) + ", ownerUserId = " + std::to_string(ownerUserId_) +
838 ", receiverUserId = " + std::to_string(receiverUserId_) + ", updateDeadLine = " +
839 std::to_string(updateDeadLine_) + ", finishDeadLine = " + std::to_string(finishDeadLine_) +
840 ", sound = " + sound_ + ", distributed = " + std::to_string(distributedCollaborate_) + ":" +
841 distributedHashCode_ + " flag: " + std::to_string(collaboratedReminderFlag_) + ", unifiedGroupInfo_ = " +
842 (unifiedGroupInfo_ ? unifiedGroupInfo_->Dump() : "null")+ " }";
843 }
844
CollaborationToJson(std::string & data) const845 bool NotificationRequest::CollaborationToJson(std::string& data) const
846 {
847 nlohmann::json jsonObject;
848 jsonObject["id"] = notificationId_;
849 jsonObject["autoDeletedTime"] = autoDeletedTime_;
850
851 jsonObject["groupName"] = groupName_;
852 jsonObject["label"] = label_;
853 jsonObject["classification"] = classification_;
854 jsonObject["isRemoveAllowed"] = isRemoveAllowed_;
855
856 jsonObject["tapDismissed"] = tapDismissed_;
857 jsonObject["isOngoing"] = inProgress_;
858 jsonObject["isAlertOnce"] = alertOneTime_;
859 jsonObject["isUnremovable"] = unremovable_;
860
861 jsonObject["creatorBundleName"] = GetOwnerBundleName();
862 jsonObject["creatorUid"] = GetOwnerUid();
863 jsonObject["creatorPid"] = GetCreatorPid();
864 jsonObject["creatorInstanceKey"] = creatorInstanceKey_;
865 jsonObject["appInstanceKey"] = appInstanceKey_;
866 jsonObject["notificationControlFlags"] = notificationControlFlags_;
867
868 if (agentBundle_ != nullptr) {
869 nlohmann::json bundleOptionObj;
870 if (!NotificationJsonConverter::ConvertToJson(agentBundle_.get(), bundleOptionObj)) {
871 ANS_LOGE("Cannot convert agentBundle to JSON.");
872 return false;
873 }
874 jsonObject["agentBundle"] = bundleOptionObj;
875 }
876 data = jsonObject.dump();
877 return true;
878 }
879
CollaborationFromJson(const std::string & basicInfo)880 NotificationRequest *NotificationRequest::CollaborationFromJson(const std::string& basicInfo)
881 {
882 if (basicInfo.empty() || !nlohmann::json::accept(basicInfo)) {
883 ANS_LOGE("Invalid JSON string");
884 return nullptr;
885 }
886
887 nlohmann::json jsonObject = nlohmann::json::parse(basicInfo, nullptr, false);
888 if (jsonObject.is_null() or !jsonObject.is_object()) {
889 ANS_LOGE("Invalid JSON object");
890 return nullptr;
891 }
892
893 auto pRequest = new (std::nothrow) NotificationRequest();
894 if (pRequest == nullptr) {
895 ANS_LOGE("null pRequest");
896 return nullptr;
897 }
898
899 ConvertJsonToNum(pRequest, jsonObject);
900 ConvertJsonToString(pRequest, jsonObject);
901 ConvertJsonToBool(pRequest, jsonObject);
902 ConvertJsonToAgentBundle(pRequest, jsonObject);
903 return pRequest;
904 }
905
ToJson(nlohmann::json & jsonObject) const906 bool NotificationRequest::ToJson(nlohmann::json &jsonObject) const
907 {
908 jsonObject["version"] = 1;
909
910 jsonObject["id"] = notificationId_;
911 jsonObject["color"] = color_;
912 jsonObject["deliveryTime"] = deliveryTime_;
913 jsonObject["autoDeletedTime"] = autoDeletedTime_;
914
915 jsonObject["creatorBundleName"] = creatorBundleName_;
916 jsonObject["ownerBundleName"] = ownerBundleName_;
917 jsonObject["groupName"] = groupName_;
918 jsonObject["label"] = label_;
919 jsonObject["classification"] = classification_;
920
921 jsonObject["slotType"] = static_cast<int32_t>(slotType_);
922 jsonObject["notificationSlotType"] = static_cast<int32_t>(slotType_);
923 jsonObject["badgeIconStyle"] = static_cast<int32_t>(badgeStyle_);
924 jsonObject["notificationContentType"] = static_cast<int32_t>(notificationContentType_);
925
926 jsonObject["showDeliveryTime"] = showDeliveryTime_;
927 jsonObject["tapDismissed"] = tapDismissed_;
928 jsonObject["colorEnabled"] = colorEnabled_;
929 jsonObject["isOngoing"] = inProgress_;
930 jsonObject["isAlertOnce"] = alertOneTime_;
931 jsonObject["isStopwatch"] = showStopwatch_;
932 jsonObject["isCountdown"] = isCountdown_;
933 jsonObject["isUnremovable"] = unremovable_;
934 jsonObject["isAgent"] = isAgent_;
935 jsonObject["isFloatingIcon"] = floatingIcon_;
936 jsonObject["updateOnly"] = updateOnly_;
937 jsonObject["distributedCollaborate"] = distributedCollaborate_;
938
939 jsonObject["creatorBundleName"] = creatorBundleName_;
940 jsonObject["creatorUid"] = creatorUid_;
941 jsonObject["creatorPid"] = creatorPid_;
942 jsonObject["creatorUserId"] = creatorUserId_;
943 jsonObject["ownerUserId"] = ownerUserId_;
944 jsonObject["ownerUid"] = ownerUid_;
945 jsonObject["receiverUserId"] = receiverUserId_;
946 jsonObject["creatorInstanceKey"] = creatorInstanceKey_;
947 jsonObject["appInstanceKey"] = appInstanceKey_;
948 jsonObject["notificationControlFlags"] = notificationControlFlags_;
949 jsonObject["updateDeadLine"] = updateDeadLine_;
950 jsonObject["finishDeadLine"] = finishDeadLine_;
951 jsonObject["hashCodeGenerateType"] = hashCodeGenerateType_;
952 jsonObject["collaboratedReminderFlag"] = collaboratedReminderFlag_;
953 jsonObject["distributedHashCode"] = distributedHashCode_;
954
955 if (!ConvertObjectsToJson(jsonObject)) {
956 ANS_LOGE("Cannot convert objects to JSON");
957 return false;
958 }
959
960 return true;
961 }
962
FromJson(const nlohmann::json & jsonObject)963 NotificationRequest *NotificationRequest::FromJson(const nlohmann::json &jsonObject)
964 {
965 if (jsonObject.is_null() or !jsonObject.is_object()) {
966 ANS_LOGE("Invalid JSON object");
967 return nullptr;
968 }
969
970 auto pRequest = new (std::nothrow) NotificationRequest();
971 if (pRequest == nullptr) {
972 ANS_LOGE("null pRequest");
973 return nullptr;
974 }
975
976 const auto &jsonEnd = jsonObject.cend();
977 if (jsonObject.find("version") != jsonEnd && jsonObject.at("version").is_number_integer()) {
978 jsonObject.at("version").get<int32_t>();
979 }
980
981 ConvertJsonToNum(pRequest, jsonObject);
982
983 ConvertJsonToString(pRequest, jsonObject);
984
985 ConvertJsonToEnum(pRequest, jsonObject);
986
987 ConvertJsonToBool(pRequest, jsonObject);
988
989 if (jsonObject.find("wantAgent") != jsonEnd && jsonObject.at("wantAgent").is_string()) {
990 auto wantAgentValue = jsonObject.at("wantAgent").get<std::string>();
991 int32_t targetUid = -1;
992 if (pRequest->GetOwnerUid() != DEFAULT_UID) {
993 targetUid = pRequest->GetOwnerUid();
994 }
995 ANS_LOGI("wantAgent Fromjson, uid = %{public}d ", targetUid);
996 pRequest->wantAgent_ = AbilityRuntime::WantAgent::WantAgentHelper::FromString(wantAgentValue, targetUid);
997 }
998
999 if (!ConvertJsonToNotificationContent(pRequest, jsonObject)) {
1000 delete pRequest;
1001 pRequest = nullptr;
1002 return nullptr;
1003 }
1004
1005 if (!ConvertJsonToNotificationActionButton(pRequest, jsonObject)) {
1006 delete pRequest;
1007 pRequest = nullptr;
1008 return nullptr;
1009 }
1010
1011 if (jsonObject.find("extraInfo") != jsonEnd && jsonObject.at("extraInfo").is_string()) {
1012 auto extraInfoStr = jsonObject.at("extraInfo").get<std::string>();
1013 if (!extraInfoStr.empty()) {
1014 AAFwk::WantParams params = AAFwk::WantParamWrapper::ParseWantParams(extraInfoStr);
1015 pRequest->additionalParams_ = std::make_shared<AAFwk::WantParams>(params);
1016 }
1017 }
1018
1019 if (jsonObject.find("extendInfo") != jsonEnd && jsonObject.at("extendInfo").is_string()) {
1020 auto extendInfoStr = jsonObject.at("extendInfo").get<std::string>();
1021 if (!extendInfoStr.empty()) {
1022 AAFwk::WantParams extendInfoParams = AAFwk::WantParamWrapper::ParseWantParams(extendInfoStr);
1023 pRequest->extendInfo_ = std::make_shared<AAFwk::WantParams>(extendInfoParams);
1024 }
1025 }
1026
1027 ConvertJsonToPixelMap(pRequest, jsonObject);
1028
1029 if (!ConvertJsonToNotificationDistributedOptions(pRequest, jsonObject)) {
1030 delete pRequest;
1031 pRequest = nullptr;
1032 return nullptr;
1033 }
1034
1035 if (!ConvertJsonToNotificationFlags(pRequest, jsonObject)) {
1036 delete pRequest;
1037 pRequest = nullptr;
1038 return nullptr;
1039 }
1040
1041 if (!ConvertJsonToNotificationBundleOption(pRequest, jsonObject)) {
1042 delete pRequest;
1043 pRequest = nullptr;
1044 return nullptr;
1045 }
1046
1047 ConvertJsonToAgentBundle(pRequest, jsonObject);
1048
1049 return pRequest;
1050 }
1051
Marshalling(Parcel & parcel) const1052 bool NotificationRequest::Marshalling(Parcel &parcel) const
1053 {
1054 // write int
1055 if (!parcel.WriteInt32(notificationId_)) {
1056 ANS_LOGE("Failed to write notification Id");
1057 return false;
1058 }
1059
1060 if (!parcel.WriteUint32(color_)) {
1061 ANS_LOGE("Failed to write color");
1062 return false;
1063 }
1064
1065 if (!parcel.WriteUint32(badgeNumber_)) {
1066 ANS_LOGE("Failed to write badge number");
1067 return false;
1068 }
1069
1070 if (!parcel.WriteInt32(progressValue_)) {
1071 ANS_LOGE("Failed to write progress value");
1072 return false;
1073 }
1074
1075 if (!parcel.WriteInt32(progressMax_)) {
1076 ANS_LOGE("Failed to write progress max");
1077 return false;
1078 }
1079
1080 if (!parcel.WriteInt64(createTime_)) {
1081 ANS_LOGE("Failed to write create time");
1082 return false;
1083 }
1084
1085 if (!parcel.WriteInt64(deliveryTime_)) {
1086 ANS_LOGE("Failed to write delivery time");
1087 return false;
1088 }
1089
1090 if (!parcel.WriteInt64(autoDeletedTime_)) {
1091 ANS_LOGE("Failed to write auto deleted time");
1092 return false;
1093 }
1094
1095 if (!parcel.WriteInt32(static_cast<int32_t>(creatorPid_))) {
1096 ANS_LOGE("Failed to write creator pid");
1097 return false;
1098 }
1099
1100 if (!parcel.WriteInt32(static_cast<int32_t>(creatorUid_))) {
1101 ANS_LOGE("Failed to write creator uid");
1102 return false;
1103 }
1104
1105 if (!parcel.WriteInt32(static_cast<int32_t>(ownerUid_))) {
1106 ANS_LOGE("Failed to write owner uid");
1107 return false;
1108 }
1109
1110 if (!parcel.WriteInt32(static_cast<int32_t>(creatorUserId_))) {
1111 ANS_LOGE("Failed to write creator userId");
1112 return false;
1113 }
1114
1115 if (!parcel.WriteInt32(static_cast<int32_t>(ownerUserId_))) {
1116 ANS_LOGE("Failed to write owner userId");
1117 return false;
1118 }
1119
1120 if (!parcel.WriteInt32(static_cast<int32_t>(receiverUserId_))) {
1121 ANS_LOGE("Failed to write receiver userId");
1122 return false;
1123 }
1124
1125 if (!parcel.WriteInt32(static_cast<int32_t>(creatorInstanceKey_))) {
1126 ANS_LOGE("Failed to write creator instance key");
1127 return false;
1128 }
1129
1130 if (!parcel.WriteUint32(notificationControlFlags_)) {
1131 ANS_LOGE("Failed to write notification control flags.");
1132 return false;
1133 }
1134
1135 if (!parcel.WriteUint32(publishDelayTime_)) {
1136 ANS_LOGE("Failed to write publish delay time");
1137 return false;
1138 }
1139
1140 if (!parcel.WriteUint32(hashCodeGenerateType_)) {
1141 ANS_LOGE("Failed to write hash code generatetype");
1142 return false;
1143 }
1144
1145 if (!parcel.WriteUint32(collaboratedReminderFlag_)) {
1146 ANS_LOGE("Failed to write collaborated reminderFlag");
1147 return false;
1148 }
1149
1150 // write std::string
1151 if (!parcel.WriteString(appInstanceKey_)) {
1152 ANS_LOGE("Failed to write instance key");
1153 return false;
1154 }
1155
1156 if (!parcel.WriteString(settingsText_)) {
1157 ANS_LOGE("Failed to write settings text");
1158 return false;
1159 }
1160
1161 if (!parcel.WriteString(creatorBundleName_)) {
1162 ANS_LOGE("Failed to write creator bundle name");
1163 return false;
1164 }
1165
1166 if (!parcel.WriteString(ownerBundleName_)) {
1167 ANS_LOGE("Failed to write owner bundle name");
1168 return false;
1169 }
1170
1171 if (!parcel.WriteString(groupName_)) {
1172 ANS_LOGE("Failed to write group name");
1173 return false;
1174 }
1175
1176 if (!parcel.WriteString(statusBarText_)) {
1177 ANS_LOGE("Failed to write status bar text");
1178 return false;
1179 }
1180
1181 if (!parcel.WriteString(label_)) {
1182 ANS_LOGE("Failed to write label");
1183 return false;
1184 }
1185
1186 if (!parcel.WriteString(shortcutId_)) {
1187 ANS_LOGE("Failed to write shortcut Id");
1188 return false;
1189 }
1190
1191 if (!parcel.WriteString(sortingKey_)) {
1192 ANS_LOGE("Failed to write sorting key");
1193 return false;
1194 }
1195
1196 if (!parcel.WriteString(classification_)) {
1197 ANS_LOGE("Failed to write classification");
1198 return false;
1199 }
1200
1201 if (!parcel.WriteString(appMessageId_)) {
1202 ANS_LOGE("Failed to write appMessageId");
1203 return false;
1204 }
1205
1206 if (!parcel.WriteString(sound_)) {
1207 ANS_LOGE("Failed to write sound");
1208 return false;
1209 }
1210
1211 if (!parcel.WriteString(distributedHashCode_)) {
1212 ANS_LOGE("Failed to write distributedHashCode");
1213 return false;
1214 }
1215
1216 // write enum
1217 if (!parcel.WriteInt32(static_cast<int32_t>(slotType_))) {
1218 ANS_LOGE("Failed to write slot type");
1219 return false;
1220 }
1221
1222 if (!parcel.WriteInt32(static_cast<int32_t>(groupAlertType_))) {
1223 ANS_LOGE("Failed to write group alert type");
1224 return false;
1225 }
1226
1227 if (!parcel.WriteInt32(static_cast<int32_t>(visiblenessType_))) {
1228 ANS_LOGE("Failed to write visibleness type");
1229 return false;
1230 }
1231
1232 if (!parcel.WriteInt32(static_cast<int32_t>(badgeStyle_))) {
1233 ANS_LOGE("Failed to write badge type");
1234 return false;
1235 }
1236
1237 if (!parcel.WriteInt32(static_cast<int32_t>(notificationContentType_))) {
1238 ANS_LOGE("Failed to write notification content type");
1239 return false;
1240 }
1241
1242 // write bool
1243 if (!parcel.WriteBool(showDeliveryTime_)) {
1244 ANS_LOGE("Failed to write flag indicating whether to show delivery time");
1245 return false;
1246 }
1247
1248 if (!parcel.WriteBool(tapDismissed_)) {
1249 ANS_LOGE("Failed to write flag tap dismissed");
1250 return false;
1251 }
1252
1253 if (!parcel.WriteBool(colorEnabled_)) {
1254 ANS_LOGE("Failed to write flag indicating whether to enable background color");
1255 return false;
1256 }
1257
1258 if (!parcel.WriteBool(alertOneTime_)) {
1259 ANS_LOGE("Failed to write flag indicating whether to have this notification alert only once");
1260 return false;
1261 }
1262
1263 if (!parcel.WriteBool(showStopwatch_)) {
1264 ANS_LOGE("Failed to write flag show stop watch");
1265 return false;
1266 }
1267
1268 if (!parcel.WriteBool(isCountdown_)) {
1269 ANS_LOGE("Failed to write flag indicating whether to show the notification creation time as a countdown timer");
1270 return false;
1271 }
1272
1273 if (!parcel.WriteBool(inProgress_)) {
1274 ANS_LOGE("Failed to write flag indicating whether in progress");
1275 return false;
1276 }
1277
1278 if (!parcel.WriteBool(groupOverview_)) {
1279 ANS_LOGE("Failed to write flag indicating whether to use this notification as the overview of its group");
1280 return false;
1281 }
1282
1283 if (!parcel.WriteBool(progressIndeterminate_)) {
1284 ANS_LOGE("Failed to write progress indeterminate");
1285 return false;
1286 }
1287
1288 if (!parcel.WriteBool(unremovable_)) {
1289 ANS_LOGE("Failed to write flag indicating whether unremovable");
1290 return false;
1291 }
1292
1293 if (!parcel.WriteBool(floatingIcon_)) {
1294 ANS_LOGE("Failed to write flag floating icon");
1295 return false;
1296 }
1297
1298 if (!parcel.WriteBool(onlyLocal_)) {
1299 ANS_LOGE("Failed to write flag only local");
1300 return false;
1301 }
1302
1303 if (!parcel.WriteBool(permitted_)) {
1304 ANS_LOGE("Failed to write flag indicating whether to allow the platform to \
1305 generate contextual NotificationActionButton objects");
1306 return false;
1307 }
1308
1309 if (!parcel.WriteBool(isAgent_)) {
1310 ANS_LOGE("Failed to write flag indicating whether an agent notification");
1311 return false;
1312 }
1313
1314 if (!parcel.WriteBool(isRemoveAllowed_)) {
1315 ANS_LOGE("Failed to write flag isRemoveAllowed");
1316 return false;
1317 }
1318
1319 if (!parcel.WriteBool(forceDistributed_)) {
1320 ANS_LOGE("Failed to write flag forceDistributed");
1321 return false;
1322 }
1323
1324 if (!parcel.WriteBool(notDistributed_)) {
1325 ANS_LOGE("Failed to write flag notDistributed");
1326 return false;
1327 }
1328
1329 if (!parcel.WriteBool(isDoNotDisturbByPassed_)) {
1330 ANS_LOGE("Failed to write flag notDistributed");
1331 return false;
1332 }
1333
1334 // write objects which managed by std::shared_ptr
1335 bool valid {false};
1336
1337 valid = wantAgent_ ? true : false;
1338 if (!parcel.WriteBool(valid)) {
1339 ANS_LOGE("Failed to write the flag which indicate whether wantAgent is null");
1340 return false;
1341 }
1342
1343 if (valid) {
1344 if (!parcel.WriteParcelable(wantAgent_.get())) {
1345 ANS_LOGE("Failed to write wantAgent");
1346 return false;
1347 }
1348 }
1349
1350 valid = removalWantAgent_ ? true : false;
1351 if (!parcel.WriteBool(valid)) {
1352 ANS_LOGE("Failed to write the flag which indicate whether removalWantAgent is null");
1353 return false;
1354 }
1355
1356 if (valid) {
1357 if (!parcel.WriteParcelable(removalWantAgent_.get())) {
1358 ANS_LOGE("Failed to write removalWantAgent");
1359 return false;
1360 }
1361 }
1362
1363 valid = maxScreenWantAgent_ ? true : false;
1364 if (!parcel.WriteBool(valid)) {
1365 ANS_LOGE("Failed to write the flag which indicate whether maxScreenWantAgent is null");
1366 return false;
1367 }
1368
1369 if (valid) {
1370 if (!parcel.WriteParcelable(maxScreenWantAgent_.get())) {
1371 ANS_LOGE("Failed to write maxScreenWantAgent");
1372 return false;
1373 }
1374 }
1375
1376 valid = additionalParams_ ? true : false;
1377 if (!parcel.WriteBool(valid)) {
1378 ANS_LOGE("Failed to write the flag which indicate whether additionalParams is null");
1379 return false;
1380 }
1381
1382 if (valid) {
1383 if (!parcel.WriteParcelable(additionalParams_.get())) {
1384 ANS_LOGE("Failed to write additionalParams");
1385 return false;
1386 }
1387 }
1388
1389 valid = extendInfo_ ? true : false;
1390 if (!parcel.WriteBool(valid)) {
1391 ANS_LOGE("Failed to write the flag which indicate whether extendInfo is null");
1392 return false;
1393 }
1394
1395 if (valid) {
1396 if (!parcel.WriteParcelable(extendInfo_.get())) {
1397 ANS_LOGE("Failed to write extendInfo");
1398 return false;
1399 }
1400 }
1401
1402 valid = littleIcon_ ? true : false;
1403 if (!parcel.WriteBool(valid)) {
1404 ANS_LOGE("Failed to write the flag which indicate whether littleIcon is null");
1405 return false;
1406 }
1407
1408 if (valid) {
1409 if (!parcel.WriteParcelable(littleIcon_.get())) {
1410 ANS_LOGE("Failed to write littleIcon");
1411 return false;
1412 }
1413 }
1414
1415 valid = bigIcon_ ? true : false;
1416 if (!parcel.WriteBool(valid)) {
1417 ANS_LOGE("Failed to write the flag which indicate whether bigIcon is null");
1418 return false;
1419 }
1420
1421 if (valid) {
1422 if (!parcel.WriteParcelable(bigIcon_.get())) {
1423 ANS_LOGE("Failed to write bigIcon");
1424 return false;
1425 }
1426 }
1427
1428 valid = overlayIcon_ ? true : false;
1429 if (!parcel.WriteBool(valid)) {
1430 ANS_LOGE("Failed to write the flag which indicate whether overlayIcon is null");
1431 return false;
1432 }
1433
1434 if (valid) {
1435 if (!parcel.WriteParcelable(overlayIcon_.get())) {
1436 ANS_LOGE("Failed to write overlayIcon");
1437 return false;
1438 }
1439 }
1440
1441 valid = notificationContent_ ? true : false;
1442 if (!parcel.WriteBool(valid)) {
1443 ANS_LOGE("Failed to write the flag which indicate whether notificationContent is null");
1444 return false;
1445 }
1446
1447 if (valid) {
1448 if (!parcel.WriteParcelable(notificationContent_.get())) {
1449 ANS_LOGE("Failed to write notificationContent");
1450 return false;
1451 }
1452 }
1453
1454 // write std::vector
1455 if (!parcel.WriteUint64(actionButtons_.size())) {
1456 ANS_LOGE("Failed to write the size of actionButtons");
1457 return false;
1458 }
1459
1460 for (auto it = actionButtons_.begin(); it != actionButtons_.end(); ++it) {
1461 if (!parcel.WriteParcelable(it->get())) {
1462 ANS_LOGE("Failed to write actionButton");
1463 return false;
1464 }
1465 }
1466
1467 if (!parcel.WriteBool(isCoverActionButtons_)) {
1468 ANS_LOGE("Failed to write isCoverActionButtons_");
1469 return false;
1470 }
1471
1472 if (!parcel.WriteBool(isUpdateByOwnerAllowed_)) {
1473 ANS_LOGE("Failed to write isUpdateByOwnerAllowed_");
1474 return false;
1475 }
1476
1477 if (!parcel.WriteBool(distributedCollaborate_)) {
1478 ANS_LOGE("Failed to write distributedCollaborate_");
1479 return false;
1480 }
1481
1482 if (!parcel.WriteBool(updateOnly_)) {
1483 ANS_LOGE("Failed to write updateOnly_");
1484 return false;
1485 }
1486
1487 if (!parcel.WriteUint64(messageUsers_.size())) {
1488 ANS_LOGE("Failed to write the size of messageUsers");
1489 return false;
1490 }
1491
1492 for (auto it = messageUsers_.begin(); it != messageUsers_.end(); ++it) {
1493 if (!parcel.WriteParcelable(it->get())) {
1494 ANS_LOGE("Failed to write messageUser");
1495 return false;
1496 }
1497 }
1498
1499 if (!parcel.WriteStringVector(userInputHistory_)) {
1500 ANS_LOGE("Failed to write userInputHistory");
1501 return false;
1502 }
1503
1504 if (!parcel.WriteParcelable(&distributedOptions_)) {
1505 ANS_LOGE("Failed to write distributedOptions");
1506 return false;
1507 }
1508
1509 valid = notificationTemplate_ ? true : false;
1510 if (!parcel.WriteBool(valid)) {
1511 ANS_LOGE("Failed to write the flag which indicate whether publicNotification is null");
1512 return false;
1513 }
1514
1515 if (valid) {
1516 if (!parcel.WriteParcelable(notificationTemplate_.get())) {
1517 ANS_LOGE("Failed to write notificationTemplate");
1518 return false;
1519 }
1520 }
1521
1522 valid = notificationFlags_ ? true : false;
1523 if (!parcel.WriteBool(valid)) {
1524 ANS_LOGE("Failed to write flags for the notification");
1525 return false;
1526 }
1527
1528 if (valid) {
1529 if (!parcel.WriteParcelable(notificationFlags_.get())) {
1530 ANS_LOGE("Failed to write notification flags");
1531 return false;
1532 }
1533 }
1534
1535 valid = notificationFlagsOfDevices_ ? true : false;
1536 if (!parcel.WriteBool(valid)) {
1537 ANS_LOGE("Failed to write notification device flags cause invalid sptr");
1538 return false;
1539 }
1540
1541 if (valid) {
1542 if (!parcel.WriteInt32(static_cast<int32_t>(notificationFlagsOfDevices_->size()))) {
1543 ANS_LOGE("Failed to write notification devices flags size");
1544 return false;
1545 }
1546 for (auto deviceFlag : *notificationFlagsOfDevices_) {
1547 if (!parcel.WriteString(deviceFlag.first)) {
1548 ANS_LOGE("Failed to write notification devices flags key");
1549 return false;
1550 }
1551 if (!parcel.WriteParcelable(deviceFlag.second.get())) {
1552 ANS_LOGE("Failed to write notification devices flags value");
1553 return false;
1554 }
1555 }
1556 }
1557
1558 valid = unifiedGroupInfo_ ? true : false;
1559 if (!parcel.WriteBool(valid)) {
1560 ANS_LOGE("Failed to write unifiedGroupInfo for the notification");
1561 return false;
1562 }
1563
1564 if (valid) {
1565 if (!parcel.WriteParcelable(unifiedGroupInfo_.get())) {
1566 ANS_LOGE("Failed to write notification unifiedGroupInfo");
1567 return false;
1568 }
1569 }
1570
1571 valid = notificationBundleOption_ != nullptr ? true : false;
1572 if (!parcel.WriteBool(valid)) {
1573 ANS_LOGE("Failed to write bundleOption for the notification");
1574 return false;
1575 }
1576
1577 if (valid) {
1578 if (!parcel.WriteParcelable(notificationBundleOption_.get())) {
1579 ANS_LOGE("Failed to write notification bundleOption");
1580 return false;
1581 }
1582 }
1583
1584 valid = agentBundle_ != nullptr ? true : false;
1585 if (!parcel.WriteBool(valid)) {
1586 ANS_LOGE("Failed to write agentBundle for the notification");
1587 return false;
1588 }
1589
1590 if (valid) {
1591 if (!parcel.WriteParcelable(agentBundle_.get())) {
1592 ANS_LOGE("Failed to write notification agentBundle");
1593 return false;
1594 }
1595 }
1596
1597 if (!parcel.WriteInt64(updateDeadLine_)) {
1598 ANS_LOGE("Failed to write max update time");
1599 return false;
1600 }
1601
1602 if (!parcel.WriteInt64(finishDeadLine_)) {
1603 ANS_LOGE("Failed to write max finish time");
1604 return false;
1605 }
1606
1607 return true;
1608 }
1609
Unmarshalling(Parcel & parcel)1610 NotificationRequest *NotificationRequest::Unmarshalling(Parcel &parcel)
1611 {
1612 auto objptr = new (std::nothrow) NotificationRequest();
1613 if ((objptr != nullptr) && !objptr->ReadFromParcel(parcel)) {
1614 delete objptr;
1615 objptr = nullptr;
1616 }
1617
1618 return objptr;
1619 }
1620
ReadFromParcel(Parcel & parcel)1621 bool NotificationRequest::ReadFromParcel(Parcel &parcel)
1622 {
1623 notificationId_ = parcel.ReadInt32();
1624 color_ = parcel.ReadUint32();
1625 badgeNumber_ = parcel.ReadUint32();
1626 progressValue_ = parcel.ReadInt32();
1627 progressMax_ = parcel.ReadInt32();
1628 createTime_ = parcel.ReadInt64();
1629 deliveryTime_ = parcel.ReadInt64();
1630 autoDeletedTime_ = parcel.ReadInt64();
1631
1632 creatorPid_ = static_cast<pid_t>(parcel.ReadInt32());
1633 creatorUid_ = parcel.ReadInt32();
1634 ownerUid_ = parcel.ReadInt32();
1635 creatorUserId_ = parcel.ReadInt32();
1636 ownerUserId_ = parcel.ReadInt32();
1637 receiverUserId_ = parcel.ReadInt32();
1638 creatorInstanceKey_ = parcel.ReadInt32();
1639 notificationControlFlags_ = parcel.ReadUint32();
1640 publishDelayTime_ = parcel.ReadUint32();
1641 hashCodeGenerateType_ = parcel.ReadUint32();
1642 collaboratedReminderFlag_ = parcel.ReadUint32();
1643
1644 if (!parcel.ReadString(appInstanceKey_)) {
1645 ANS_LOGE("Failed to read Instance key");
1646 return false;
1647 }
1648
1649 if (!parcel.ReadString(settingsText_)) {
1650 ANS_LOGE("Failed to read settings text");
1651 return false;
1652 }
1653
1654 if (!parcel.ReadString(creatorBundleName_)) {
1655 ANS_LOGE("Failed to read creator bundle name");
1656 return false;
1657 }
1658
1659 if (!parcel.ReadString(ownerBundleName_)) {
1660 ANS_LOGE("Failed to read owner bundle name");
1661 return false;
1662 }
1663
1664 if (!parcel.ReadString(groupName_)) {
1665 ANS_LOGE("Failed to read group name");
1666 return false;
1667 }
1668
1669 if (!parcel.ReadString(statusBarText_)) {
1670 ANS_LOGE("Failed to read status bar text");
1671 return false;
1672 }
1673
1674 if (!parcel.ReadString(label_)) {
1675 ANS_LOGE("Failed to read label");
1676 return false;
1677 }
1678
1679 if (!parcel.ReadString(shortcutId_)) {
1680 ANS_LOGE("Failed to read shortcut Id");
1681 return false;
1682 }
1683
1684 if (!parcel.ReadString(sortingKey_)) {
1685 ANS_LOGE("Failed to read sorting key");
1686 return false;
1687 }
1688
1689 if (!parcel.ReadString(classification_)) {
1690 ANS_LOGE("Failed to read classification");
1691 return false;
1692 }
1693
1694 if (!parcel.ReadString(appMessageId_)) {
1695 ANS_LOGE("Failed to read appMessageId");
1696 return false;
1697 }
1698
1699 if (!parcel.ReadString(sound_)) {
1700 ANS_LOGE("Failed to read sound");
1701 return false;
1702 }
1703
1704 if (!parcel.ReadString(distributedHashCode_)) {
1705 ANS_LOGE("Failed to read distributedHashCode");
1706 return false;
1707 }
1708
1709 int32_t slotTypeValue = parcel.ReadInt32();
1710 if (slotTypeValue < 0 ||
1711 slotTypeValue >= static_cast<int>(NotificationConstant::SlotType::ILLEGAL_TYPE)) {
1712 ANS_LOGE("Invalid SlotType:%{public}d. It should be in [0 , %{public}d).",
1713 slotTypeValue, static_cast<int>(NotificationConstant::SlotType::ILLEGAL_TYPE));
1714 return false;
1715 }
1716 slotType_ = static_cast<NotificationConstant::SlotType>(slotTypeValue);
1717 int32_t groupAlertTypeValue = parcel.ReadInt32();
1718 if (groupAlertTypeValue < 0 ||
1719 groupAlertTypeValue >= static_cast<int>(NotificationRequest::GroupAlertType::ILLEGAL_TYPE)) {
1720 ANS_LOGE("Invalid GroupAlertType:%{public}d. It should be in [0 , %{public}d).",
1721 groupAlertTypeValue, static_cast<int>(NotificationRequest::GroupAlertType::ILLEGAL_TYPE));
1722 return false;
1723 }
1724 groupAlertType_ = static_cast<NotificationRequest::GroupAlertType>(groupAlertTypeValue);
1725 int32_t visiblenessTypeValue = parcel.ReadInt32();
1726 if (visiblenessTypeValue < 0 ||
1727 visiblenessTypeValue >= static_cast<int>(NotificationConstant::VisiblenessType::ILLEGAL_TYPE)) {
1728 ANS_LOGE("Invalid VisiblenessType:%{public}d. It should be in [0 , %{public}d).",
1729 visiblenessTypeValue, static_cast<int>(NotificationConstant::VisiblenessType::ILLEGAL_TYPE));
1730 return false;
1731 }
1732 visiblenessType_ = static_cast<NotificationConstant::VisiblenessType>(visiblenessTypeValue);
1733 int32_t badgeStyleValue = parcel.ReadInt32();
1734 if (badgeStyleValue < 0) {
1735 ANS_LOGE("Invalid badgeStyle:%{public}d. It should be greater than 0.", badgeStyleValue);
1736 return false;
1737 }
1738 if (badgeStyleValue >= static_cast<int>(NotificationRequest::BadgeStyle::ILLEGAL_TYPE)) {
1739 badgeStyleValue = static_cast<int>(NotificationRequest::BadgeStyle::NONE);
1740 ANS_LOGE("The badge style value is too large, set it to the default enumeration value: %{public}d.",
1741 static_cast<int>(NotificationRequest::BadgeStyle::NONE));
1742 }
1743 badgeStyle_ = static_cast<NotificationRequest::BadgeStyle>(badgeStyleValue);
1744 int32_t notificationContentTypeValue = parcel.ReadInt32();
1745 if (notificationContentTypeValue <= static_cast<int>(NotificationContent::Type::NONE) ||
1746 notificationContentTypeValue >= static_cast<int>(NotificationContent::Type::ILLEGAL_TYPE)) {
1747 ANS_LOGE("Invalid notificationContent:%{public}d. It should be in (%{public}d , %{public}d)",
1748 notificationContentTypeValue, static_cast<int>(NotificationContent::Type::NONE),
1749 static_cast<int>(NotificationContent::Type::ILLEGAL_TYPE));
1750 return false;
1751 }
1752 notificationContentType_ = static_cast<NotificationContent::Type>(notificationContentTypeValue);
1753
1754 showDeliveryTime_ = parcel.ReadBool();
1755 tapDismissed_ = parcel.ReadBool();
1756 colorEnabled_ = parcel.ReadBool();
1757 alertOneTime_ = parcel.ReadBool();
1758 showStopwatch_ = parcel.ReadBool();
1759 isCountdown_ = parcel.ReadBool();
1760 inProgress_ = parcel.ReadBool();
1761 groupOverview_ = parcel.ReadBool();
1762 progressIndeterminate_ = parcel.ReadBool();
1763 unremovable_ = parcel.ReadBool();
1764 floatingIcon_ = parcel.ReadBool();
1765 onlyLocal_ = parcel.ReadBool();
1766 permitted_ = parcel.ReadBool();
1767 isAgent_ = parcel.ReadBool();
1768 isRemoveAllowed_ = parcel.ReadBool();
1769 forceDistributed_ = parcel.ReadBool();
1770 notDistributed_ = parcel.ReadBool();
1771 isDoNotDisturbByPassed_ = parcel.ReadBool();
1772
1773 bool valid {false};
1774
1775 valid = parcel.ReadBool();
1776 if (valid) {
1777 wantAgent_ = std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>(
1778 parcel.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>());
1779 if (!wantAgent_) {
1780 ANS_LOGE("null wantAgent");
1781 return false;
1782 }
1783 }
1784
1785 valid = parcel.ReadBool();
1786 if (valid) {
1787 removalWantAgent_ = std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>(
1788 parcel.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>());
1789 if (!removalWantAgent_) {
1790 ANS_LOGE("null removalWantAgent");
1791 return false;
1792 }
1793 }
1794
1795 valid = parcel.ReadBool();
1796 if (valid) {
1797 maxScreenWantAgent_ = std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>(
1798 parcel.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>());
1799 if (!maxScreenWantAgent_) {
1800 ANS_LOGE("null maxScreenWantAgent");
1801 return false;
1802 }
1803 }
1804
1805 valid = parcel.ReadBool();
1806 if (valid) {
1807 additionalParams_ = std::shared_ptr<AAFwk::WantParams>(parcel.ReadParcelable<AAFwk::WantParams>());
1808 if (!additionalParams_) {
1809 ANS_LOGE("null additionalParams");
1810 return false;
1811 }
1812 }
1813
1814 valid = parcel.ReadBool();
1815 if (valid) {
1816 extendInfo_ = std::shared_ptr<AAFwk::WantParams>(parcel.ReadParcelable<AAFwk::WantParams>());
1817 if (!extendInfo_) {
1818 ANS_LOGE("null extendInfo");
1819 return false;
1820 }
1821 }
1822
1823 valid = parcel.ReadBool();
1824 if (valid) {
1825 littleIcon_ = std::shared_ptr<Media::PixelMap>(parcel.ReadParcelable<Media::PixelMap>());
1826 }
1827
1828 valid = parcel.ReadBool();
1829 if (valid) {
1830 bigIcon_ = std::shared_ptr<Media::PixelMap>(parcel.ReadParcelable<Media::PixelMap>());
1831 if (!bigIcon_) {
1832 ANS_LOGE("null bigIcon");
1833 return false;
1834 }
1835 }
1836
1837 valid = parcel.ReadBool();
1838 if (valid) {
1839 overlayIcon_ = std::shared_ptr<Media::PixelMap>(parcel.ReadParcelable<Media::PixelMap>());
1840 if (!overlayIcon_) {
1841 ANS_LOGE("null overlayIcon");
1842 return false;
1843 }
1844 }
1845
1846 valid = parcel.ReadBool();
1847 if (valid) {
1848 notificationContent_ = std::shared_ptr<NotificationContent>(parcel.ReadParcelable<NotificationContent>());
1849 if (!notificationContent_) {
1850 ANS_LOGE("null notificationContent");
1851 return false;
1852 }
1853 }
1854
1855 auto vsize = parcel.ReadUint64();
1856 vsize = (vsize < NotificationRequest::MAX_ACTION_BUTTONS) ? vsize : NotificationRequest::MAX_ACTION_BUTTONS;
1857 for (uint64_t it = 0; it < vsize; ++it) {
1858 auto member = std::shared_ptr<NotificationActionButton>(parcel.ReadParcelable<NotificationActionButton>());
1859 if (member == nullptr) {
1860 actionButtons_.clear();
1861 ANS_LOGE("null member");
1862 return false;
1863 }
1864
1865 actionButtons_.emplace_back(member);
1866 }
1867
1868 isCoverActionButtons_ = parcel.ReadBool();
1869 isUpdateByOwnerAllowed_ = parcel.ReadBool();
1870 distributedCollaborate_ = parcel.ReadBool();
1871 updateOnly_ = parcel.ReadBool();
1872
1873 vsize = parcel.ReadUint64();
1874 vsize = (vsize < NotificationRequest::MAX_MESSAGE_USERS) ? vsize : NotificationRequest::MAX_MESSAGE_USERS;
1875 for (uint64_t it = 0; it < vsize; ++it) {
1876 auto member = std::shared_ptr<MessageUser>(parcel.ReadParcelable<MessageUser>());
1877 if (member == nullptr) {
1878 ANS_LOGE("null member");
1879 messageUsers_.clear();
1880 return false;
1881 }
1882
1883 messageUsers_.emplace_back(member);
1884 }
1885
1886 if (!parcel.ReadStringVector(&userInputHistory_)) {
1887 ANS_LOGE("Failed to read userInputHistory");
1888 return false;
1889 }
1890
1891 auto pOpt = parcel.ReadParcelable<NotificationDistributedOptions>();
1892 if (pOpt == nullptr) {
1893 ANS_LOGE("null pOpt");
1894 return false;
1895 }
1896 distributedOptions_ = *pOpt;
1897 delete pOpt;
1898 pOpt = nullptr;
1899
1900 valid = parcel.ReadBool();
1901 if (valid) {
1902 notificationTemplate_ = std::shared_ptr<NotificationTemplate>(parcel.ReadParcelable<NotificationTemplate>());
1903 if (!notificationTemplate_) {
1904 ANS_LOGE("null notificationTemplate");
1905 return false;
1906 }
1907 }
1908
1909 valid = parcel.ReadBool();
1910 if (valid) {
1911 notificationFlags_ = std::shared_ptr<NotificationFlags>(parcel.ReadParcelable<NotificationFlags>());
1912 if (!notificationFlags_) {
1913 ANS_LOGE("null notificationFlags");
1914 return false;
1915 }
1916 }
1917
1918 valid = parcel.ReadBool();
1919 if (valid) {
1920 notificationFlagsOfDevices_ = std::make_shared<std::map<std::string, std::shared_ptr<NotificationFlags>>>();
1921 int32_t mapSize = parcel.ReadInt32();
1922 mapSize = (mapSize < MAX_MAP_SIZE) ? mapSize : MAX_MAP_SIZE;
1923 for (int32_t seq = 0; seq < mapSize; seq++) {
1924 std::string deviceType = parcel.ReadString();
1925 std::shared_ptr<NotificationFlags> notificationFlags =
1926 std::shared_ptr<NotificationFlags>(parcel.ReadParcelable<NotificationFlags>());
1927 (*notificationFlagsOfDevices_)[deviceType] = notificationFlags;
1928 }
1929 }
1930
1931 valid = parcel.ReadBool();
1932 if (valid) {
1933 unifiedGroupInfo_ =
1934 std::shared_ptr<NotificationUnifiedGroupInfo>(parcel.ReadParcelable<NotificationUnifiedGroupInfo>());
1935 if (!unifiedGroupInfo_) {
1936 ANS_LOGE("null unifiedGroupInfo");
1937 return false;
1938 }
1939 }
1940
1941 valid = parcel.ReadBool();
1942 if (valid) {
1943 notificationBundleOption_ =
1944 std::shared_ptr<NotificationBundleOption>(parcel.ReadParcelable<NotificationBundleOption>());
1945 if (!notificationBundleOption_) {
1946 ANS_LOGE("null notificationBundleOption");
1947 return false;
1948 }
1949 }
1950
1951 valid = parcel.ReadBool();
1952 if (valid) {
1953 agentBundle_ =
1954 std::shared_ptr<NotificationBundleOption>(parcel.ReadParcelable<NotificationBundleOption>());
1955 if (!agentBundle_) {
1956 ANS_LOGE("null agentBundle");
1957 return false;
1958 }
1959 }
1960
1961 updateDeadLine_ = parcel.ReadInt64();
1962 finishDeadLine_ = parcel.ReadInt64();
1963
1964 return true;
1965 }
1966
GetNowSysTime()1967 int64_t NotificationRequest::GetNowSysTime()
1968 {
1969 std::chrono::time_point<std::chrono::system_clock> nowSys = std::chrono::system_clock::now();
1970 auto epoch = nowSys.time_since_epoch();
1971 auto value = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
1972 int64_t duration = value.count();
1973 return duration;
1974 }
1975
SetTemplate(const std::shared_ptr<NotificationTemplate> & templ)1976 void NotificationRequest::SetTemplate(const std::shared_ptr<NotificationTemplate> &templ)
1977 {
1978 notificationTemplate_ = templ;
1979 }
1980
GetTemplate() const1981 std::shared_ptr<NotificationTemplate> NotificationRequest::GetTemplate() const
1982 {
1983 return notificationTemplate_;
1984 }
1985
SetFlags(const std::shared_ptr<NotificationFlags> & flags)1986 void NotificationRequest::SetFlags(const std::shared_ptr<NotificationFlags> &flags)
1987 {
1988 notificationFlags_ = flags;
1989 }
1990
GetFlags() const1991 std::shared_ptr<NotificationFlags> NotificationRequest::GetFlags() const
1992 {
1993 return notificationFlags_;
1994 }
1995
SetDeviceFlags(const std::shared_ptr<std::map<std::string,std::shared_ptr<NotificationFlags>>> & mapFlags)1996 void NotificationRequest::SetDeviceFlags(
1997 const std::shared_ptr<std::map<std::string, std::shared_ptr<NotificationFlags>>> &mapFlags)
1998 {
1999 notificationFlagsOfDevices_ = mapFlags;
2000 }
2001
GetDeviceFlags() const2002 std::shared_ptr<std::map<std::string, std::shared_ptr<NotificationFlags>>> NotificationRequest::GetDeviceFlags() const
2003 {
2004 return notificationFlagsOfDevices_;
2005 }
2006
2007
SetBundleOption(const std::shared_ptr<NotificationBundleOption> & bundleOption)2008 void NotificationRequest::SetBundleOption(const std::shared_ptr<NotificationBundleOption> &bundleOption)
2009 {
2010 notificationBundleOption_ = bundleOption;
2011 }
2012
GetBundleOption() const2013 std::shared_ptr<NotificationBundleOption> NotificationRequest::GetBundleOption() const
2014 {
2015 return notificationBundleOption_;
2016 }
2017
SetAgentBundle(const std::shared_ptr<NotificationBundleOption> & agentBundle)2018 void NotificationRequest::SetAgentBundle(const std::shared_ptr<NotificationBundleOption> &agentBundle)
2019 {
2020 agentBundle_ = agentBundle;
2021 }
2022
GetAgentBundle() const2023 std::shared_ptr<NotificationBundleOption> NotificationRequest::GetAgentBundle() const
2024 {
2025 return agentBundle_;
2026 }
2027
SetReceiverUserId(int32_t userId)2028 void NotificationRequest::SetReceiverUserId(int32_t userId)
2029 {
2030 receiverUserId_ = userId;
2031 }
2032
GetReceiverUserId() const2033 int32_t NotificationRequest::GetReceiverUserId() const
2034 {
2035 if (receiverUserId_ == SUBSCRIBE_USER_INIT) {
2036 if (ownerUserId_ == SUBSCRIBE_USER_INIT) {
2037 return creatorUserId_;
2038 }
2039 return ownerUserId_;
2040 }
2041 return receiverUserId_;
2042 }
2043
IsRemoveAllowed() const2044 bool NotificationRequest::IsRemoveAllowed() const
2045 {
2046 return isRemoveAllowed_;
2047 }
2048
SetRemoveAllowed(bool isRemoveAllowed)2049 void NotificationRequest::SetRemoveAllowed(bool isRemoveAllowed)
2050 {
2051 isRemoveAllowed_ = isRemoveAllowed;
2052 }
2053
IsForceDistributed() const2054 bool NotificationRequest::IsForceDistributed() const
2055 {
2056 return forceDistributed_;
2057 }
2058
SetForceDistributed(bool forceDistributed)2059 void NotificationRequest::SetForceDistributed(bool forceDistributed)
2060 {
2061 forceDistributed_ = forceDistributed;
2062 }
2063
IsNotDistributed() const2064 bool NotificationRequest::IsNotDistributed() const
2065 {
2066 return notDistributed_;
2067 }
2068
SetNotDistributed(bool notDistributed)2069 void NotificationRequest::SetNotDistributed(bool notDistributed)
2070 {
2071 notDistributed_ = notDistributed;
2072 }
2073
IsSystemApp() const2074 bool NotificationRequest::IsSystemApp() const
2075 {
2076 return isSystemApp_;
2077 }
2078
SetIsSystemApp(bool isSystemApp)2079 void NotificationRequest::SetIsSystemApp(bool isSystemApp)
2080 {
2081 isSystemApp_ = isSystemApp;
2082 }
2083
IsDoNotDisturbByPassed() const2084 bool NotificationRequest::IsDoNotDisturbByPassed() const
2085 {
2086 return isDoNotDisturbByPassed_;
2087 }
2088
SetIsDoNotDisturbByPassed(bool isDoNotDisturbByPassed)2089 void NotificationRequest::SetIsDoNotDisturbByPassed(bool isDoNotDisturbByPassed)
2090 {
2091 isDoNotDisturbByPassed_ = isDoNotDisturbByPassed;
2092 }
2093
CopyBase(const NotificationRequest & other)2094 void NotificationRequest::CopyBase(const NotificationRequest &other)
2095 {
2096 this->notificationId_ = other.notificationId_;
2097 this->color_ = other.color_;
2098 this->badgeNumber_ = other.badgeNumber_;
2099 this->notificationControlFlags_ = other.notificationControlFlags_;
2100 this->progressValue_ = other.progressValue_;
2101 this->progressMax_ = other.progressMax_;
2102 this->createTime_ = other.createTime_;
2103 this->deliveryTime_ = other.deliveryTime_;
2104 this->autoDeletedTime_ = other.autoDeletedTime_;
2105 this->updateDeadLine_ = other.updateDeadLine_;
2106 this->finishDeadLine_ = other.finishDeadLine_;
2107
2108 this->creatorPid_ = other.creatorPid_;
2109 this->creatorUid_ = other.creatorUid_;
2110 this->ownerUid_ = other.ownerUid_;
2111 this->creatorUserId_ = other.creatorUserId_;
2112 this->ownerUserId_ = other.ownerUserId_;
2113 this->receiverUserId_ = other.receiverUserId_;
2114 this->creatorInstanceKey_ = other.creatorInstanceKey_;
2115 this->appInstanceKey_ = other.appInstanceKey_;
2116 this->isAgent_ = other.isAgent_;
2117 this->isRemoveAllowed_ = other.isRemoveAllowed_;
2118 this->forceDistributed_ = other.forceDistributed_;
2119 this->notDistributed_ = other.notDistributed_;
2120 this->isSystemApp_ = other.isSystemApp_;
2121 this->isDoNotDisturbByPassed_ = other.isDoNotDisturbByPassed_;
2122 this->isCoverActionButtons_ = other.isCoverActionButtons_;
2123 this->isUpdateByOwnerAllowed_ = other.isUpdateByOwnerAllowed_;
2124 this->distributedCollaborate_ = other.distributedCollaborate_;
2125 this->updateOnly_ = other.updateOnly_;
2126
2127 this->slotType_ = other.slotType_;
2128 this->settingsText_ = other.settingsText_;
2129 this->creatorBundleName_ = other.creatorBundleName_;
2130 this->ownerBundleName_ = other.ownerBundleName_;
2131 this->groupName_ = other.groupName_;
2132 this->statusBarText_ = other.statusBarText_;
2133 this->label_ = other.label_;
2134 this->shortcutId_ = other.shortcutId_;
2135 this->sortingKey_ = other.sortingKey_;
2136 this->classification_ = other.classification_;
2137 this->appMessageId_ = other.appMessageId_;
2138 this->sound_ = other.sound_;
2139 this->distributedHashCode_ = other.distributedHashCode_;
2140
2141 this->groupAlertType_ = other.groupAlertType_;
2142 this->visiblenessType_ = other.visiblenessType_;
2143 this->badgeStyle_ = other.badgeStyle_;
2144 this->notificationContentType_ = other.notificationContentType_;
2145 }
2146
CopyOther(const NotificationRequest & other)2147 void NotificationRequest::CopyOther(const NotificationRequest &other)
2148 {
2149 this->showDeliveryTime_ = other.showDeliveryTime_;
2150 this->tapDismissed_ = other.tapDismissed_;
2151 this->colorEnabled_ = other.colorEnabled_;
2152 this->alertOneTime_ = other.alertOneTime_;
2153 this->showStopwatch_ = other.showStopwatch_;
2154 this->isCountdown_ = other.isCountdown_;
2155 this->inProgress_ = other.inProgress_;
2156 this->groupOverview_ = other.groupOverview_;
2157 this->progressIndeterminate_ = other.progressIndeterminate_;
2158 this->unremovable_ = other.unremovable_;
2159 this->floatingIcon_ = other.floatingIcon_;
2160 this->onlyLocal_ = other.onlyLocal_;
2161 this->permitted_ = other.permitted_;
2162
2163 this->wantAgent_ = other.wantAgent_;
2164 this->removalWantAgent_ = other.removalWantAgent_;
2165 this->maxScreenWantAgent_ = other.maxScreenWantAgent_;
2166 this->additionalParams_ = other.additionalParams_;
2167 this->extendInfo_ = other.extendInfo_;
2168 this->littleIcon_ = other.littleIcon_;
2169 this->bigIcon_ = other.bigIcon_;
2170 this->overlayIcon_ = other.overlayIcon_;
2171 this->notificationContent_ = other.notificationContent_;
2172
2173 this->actionButtons_ = other.actionButtons_;
2174 this->messageUsers_ = other.messageUsers_;
2175 this->userInputHistory_ = other.userInputHistory_;
2176
2177 this->distributedOptions_ = other.distributedOptions_;
2178
2179 this->notificationTemplate_ = other.notificationTemplate_;
2180 this->notificationFlags_ = other.notificationFlags_;
2181 this->agentBundle_ = other.agentBundle_;
2182 this->unifiedGroupInfo_ = other.unifiedGroupInfo_;
2183 this->notificationBundleOption_ = other.notificationBundleOption_;
2184 this->notificationFlagsOfDevices_ = other.notificationFlagsOfDevices_;
2185 this->publishDelayTime_ = other.publishDelayTime_;
2186 this->hashCodeGenerateType_ = other.hashCodeGenerateType_;
2187 this->collaboratedReminderFlag_ = other.collaboratedReminderFlag_;
2188 }
2189
ConvertObjectsToJson(nlohmann::json & jsonObject) const2190 bool NotificationRequest::ConvertObjectsToJson(nlohmann::json &jsonObject) const
2191 {
2192 jsonObject["wantAgent"] = wantAgent_ ? AbilityRuntime::WantAgent::WantAgentHelper::ToString(wantAgent_) : "";
2193
2194 nlohmann::json contentObj;
2195 if (notificationContent_) {
2196 if (!NotificationJsonConverter::ConvertToJson(notificationContent_.get(), contentObj)) {
2197 ANS_LOGE("Cannot convert notificationContent to JSON");
2198 return false;
2199 }
2200 }
2201 jsonObject["content"] = contentObj;
2202
2203 nlohmann::json buttonsArr = nlohmann::json::array();
2204 for (auto &btn : actionButtons_) {
2205 if (!btn) {
2206 continue;
2207 }
2208
2209 nlohmann::json btnObj;
2210 if (!NotificationJsonConverter::ConvertToJson(btn.get(), btnObj)) {
2211 ANS_LOGE("Cannot convert actionButton to JSON");
2212 return false;
2213 }
2214
2215 buttonsArr.emplace_back(btnObj);
2216 }
2217 jsonObject["actionButtons"] = buttonsArr;
2218
2219 std::string extraInfoStr;
2220 if (additionalParams_) {
2221 AAFwk::WantParamWrapper wWrapper(*additionalParams_);
2222 extraInfoStr = wWrapper.ToString();
2223 }
2224 jsonObject["extraInfo"] = extraInfoStr;
2225
2226 std::string extendInfoStr;
2227 if (extendInfo_) {
2228 AAFwk::WantParamWrapper wWrapper(*extendInfo_);
2229 extendInfoStr = wWrapper.ToString();
2230 }
2231 jsonObject["extendInfo"] = extendInfoStr;
2232
2233 jsonObject["smallIcon"] = AnsImageUtil::PackImage(littleIcon_);
2234 jsonObject["largeIcon"] = AnsImageUtil::PackImage(bigIcon_);
2235 jsonObject["overlayIcon"] = overlayIcon_ ? AnsImageUtil::PackImage(overlayIcon_) : "";
2236
2237 nlohmann::json optObj;
2238 if (!NotificationJsonConverter::ConvertToJson(&distributedOptions_, optObj)) {
2239 ANS_LOGE("Cannot convert distributedOptions to JSON");
2240 return false;
2241 }
2242 jsonObject["distributedOptions"] = optObj;
2243
2244 if (notificationFlags_) {
2245 nlohmann::json flagsObj;
2246 if (!NotificationJsonConverter::ConvertToJson(notificationFlags_.get(), flagsObj)) {
2247 ANS_LOGE("Cannot convert notificationFlags to JSON");
2248 return false;
2249 }
2250 jsonObject["notificationFlags"] = flagsObj;
2251 }
2252
2253 if (notificationBundleOption_ != nullptr) {
2254 nlohmann::json bundleOptionObj;
2255 if (!NotificationJsonConverter::ConvertToJson(notificationBundleOption_.get(), bundleOptionObj)) {
2256 ANS_LOGE("Cannot convert notificationBundleOption to JSON.");
2257 return false;
2258 }
2259 jsonObject["notificationBundleOption"] = bundleOptionObj;
2260 }
2261
2262 if (agentBundle_ != nullptr) {
2263 nlohmann::json bundleOptionObj;
2264 if (!NotificationJsonConverter::ConvertToJson(agentBundle_.get(), bundleOptionObj)) {
2265 ANS_LOGE("Cannot convert agentBundle to JSON.");
2266 return false;
2267 }
2268 jsonObject["agentBundle"] = bundleOptionObj;
2269 }
2270
2271 return true;
2272 }
2273
ConvertJsonToNumExt(NotificationRequest * target,const nlohmann::json & jsonObject)2274 void NotificationRequest::ConvertJsonToNumExt(
2275 NotificationRequest *target, const nlohmann::json &jsonObject)
2276 {
2277 const auto &jsonEnd = jsonObject.cend();
2278
2279 if (jsonObject.find("updateDeadLine") != jsonEnd && jsonObject.at("updateDeadLine").is_number_integer()) {
2280 target->updateDeadLine_ = jsonObject.at("updateDeadLine").get<int64_t>();
2281 }
2282
2283 if (jsonObject.find("finishDeadLine") != jsonEnd && jsonObject.at("finishDeadLine").is_number_integer()) {
2284 target->finishDeadLine_ = jsonObject.at("finishDeadLine").get<int64_t>();
2285 }
2286
2287 if (jsonObject.find("ownerUserId") != jsonEnd && jsonObject.at("ownerUserId").is_number_integer()) {
2288 target->ownerUserId_ = jsonObject.at("ownerUserId").get<int32_t>();
2289 }
2290
2291 if (jsonObject.find("ownerUid") != jsonEnd && jsonObject.at("ownerUid").is_number_integer()) {
2292 target->ownerUid_ = jsonObject.at("ownerUid").get<int32_t>();
2293 }
2294
2295 if (jsonObject.find("notificationControlFlags") != jsonEnd &&
2296 jsonObject.at("notificationControlFlags").is_number_integer()) {
2297 target->notificationControlFlags_ = jsonObject.at("notificationControlFlags").get<uint32_t>();
2298 }
2299 }
2300
ConvertJsonToNum(NotificationRequest * target,const nlohmann::json & jsonObject)2301 void NotificationRequest::ConvertJsonToNum(NotificationRequest *target, const nlohmann::json &jsonObject)
2302 {
2303 if (target == nullptr) {
2304 ANS_LOGE("null target");
2305 return;
2306 }
2307
2308 const auto &jsonEnd = jsonObject.cend();
2309
2310 if (jsonObject.find("id") != jsonEnd && jsonObject.at("id").is_number_integer()) {
2311 target->notificationId_ = jsonObject.at("id").get<int32_t>();
2312 }
2313
2314 if (jsonObject.find("color") != jsonEnd && jsonObject.at("color").is_number_integer()) {
2315 target->color_ = jsonObject.at("color").get<uint32_t>();
2316 }
2317
2318 if (jsonObject.find("deliveryTime") != jsonEnd && jsonObject.at("deliveryTime").is_number_integer()) {
2319 target->deliveryTime_ = jsonObject.at("deliveryTime").get<int64_t>();
2320 }
2321
2322 if (jsonObject.find("autoDeletedTime") != jsonEnd && jsonObject.at("autoDeletedTime").is_number_integer()) {
2323 target->autoDeletedTime_ = jsonObject.at("autoDeletedTime").get<int64_t>();
2324 }
2325
2326 if (jsonObject.find("creatorUid") != jsonEnd && jsonObject.at("creatorUid").is_number_integer()) {
2327 target->creatorUid_ = jsonObject.at("creatorUid").get<int32_t>();
2328 }
2329
2330 if (jsonObject.find("creatorPid") != jsonEnd && jsonObject.at("creatorPid").is_number_integer()) {
2331 target->creatorPid_ = jsonObject.at("creatorPid").get<int32_t>();
2332 }
2333
2334 if (jsonObject.find("creatorUserId") != jsonEnd && jsonObject.at("creatorUserId").is_number_integer()) {
2335 target->creatorUserId_ = jsonObject.at("creatorUserId").get<int32_t>();
2336 }
2337
2338 if (jsonObject.find("receiverUserId") != jsonEnd && jsonObject.at("receiverUserId").is_number_integer()) {
2339 target->receiverUserId_ = jsonObject.at("receiverUserId").get<int32_t>();
2340 }
2341
2342 if (jsonObject.find("creatorInstanceKey") != jsonEnd && jsonObject.at("creatorInstanceKey").is_number_integer()) {
2343 target->creatorInstanceKey_ = jsonObject.at("creatorInstanceKey").get<int32_t>();
2344 }
2345
2346 if (jsonObject.find("badgeNumber") != jsonEnd && jsonObject.at("badgeNumber").is_number_integer()) {
2347 target->badgeNumber_ = jsonObject.at("badgeNumber").get<uint32_t>();
2348 }
2349 if (jsonObject.find("hashCodeGenerateType") != jsonEnd &&
2350 jsonObject.at("hashCodeGenerateType").is_number_integer()) {
2351 target->hashCodeGenerateType_ = jsonObject.at("hashCodeGenerateType").get<uint32_t>();
2352 }
2353 if (jsonObject.find("collaboratedReminderFlag") != jsonEnd &&
2354 jsonObject.at("collaboratedReminderFlag").is_number_integer()) {
2355 target->collaboratedReminderFlag_ = jsonObject.at("collaboratedReminderFlag").get<uint32_t>();
2356 }
2357
2358 ConvertJsonToNumExt(target, jsonObject);
2359 }
2360
ConvertJsonToString(NotificationRequest * target,const nlohmann::json & jsonObject)2361 void NotificationRequest::ConvertJsonToString(NotificationRequest *target, const nlohmann::json &jsonObject)
2362 {
2363 if (target == nullptr) {
2364 ANS_LOGE("null target");
2365 return;
2366 }
2367
2368 const auto &jsonEnd = jsonObject.cend();
2369
2370 if (jsonObject.find("appInstanceKey") != jsonEnd && jsonObject.at("appInstanceKey").is_string()) {
2371 target->appInstanceKey_ = jsonObject.at("appInstanceKey").get<std::string>();
2372 }
2373
2374 if (jsonObject.find("creatorBundleName") != jsonEnd && jsonObject.at("creatorBundleName").is_string()) {
2375 target->creatorBundleName_ = jsonObject.at("creatorBundleName").get<std::string>();
2376 }
2377
2378 if (jsonObject.find("ownerBundleName") != jsonEnd && jsonObject.at("ownerBundleName").is_string()) {
2379 target->ownerBundleName_ = jsonObject.at("ownerBundleName").get<std::string>();
2380 }
2381
2382 if (jsonObject.find("groupName") != jsonEnd && jsonObject.at("groupName").is_string()) {
2383 target->groupName_ = jsonObject.at("groupName").get<std::string>();
2384 }
2385
2386 if (jsonObject.find("label") != jsonEnd && jsonObject.at("label").is_string()) {
2387 target->label_ = jsonObject.at("label").get<std::string>();
2388 }
2389
2390 if (jsonObject.find("classification") != jsonEnd && jsonObject.at("classification").is_string()) {
2391 target->classification_ = jsonObject.at("classification").get<std::string>();
2392 }
2393
2394 if (jsonObject.find("creatorBundleName") != jsonEnd && jsonObject.at("creatorBundleName").is_string()) {
2395 target->creatorBundleName_ = jsonObject.at("creatorBundleName").get<std::string>();
2396 }
2397
2398 if (jsonObject.find("distributedHashCode") != jsonEnd && jsonObject.at("distributedHashCode").is_string()) {
2399 target->distributedHashCode_ = jsonObject.at("distributedHashCode").get<std::string>();
2400 }
2401 }
2402
ConvertJsonToEnum(NotificationRequest * target,const nlohmann::json & jsonObject)2403 void NotificationRequest::ConvertJsonToEnum(NotificationRequest *target, const nlohmann::json &jsonObject)
2404 {
2405 if (target == nullptr) {
2406 ANS_LOGE("null target");
2407 return;
2408 }
2409
2410 const auto &jsonEnd = jsonObject.cend();
2411
2412 if (jsonObject.find("slotType") != jsonEnd && jsonObject.at("slotType").is_number_integer()) {
2413 auto slotTypeValue = jsonObject.at("slotType").get<int32_t>();
2414 target->slotType_ = static_cast<NotificationConstant::SlotType>(slotTypeValue);
2415 }
2416
2417 if (jsonObject.find("badgeIconStyle") != jsonEnd && jsonObject.at("badgeIconStyle").is_number_integer()) {
2418 auto badgeStyleValue = jsonObject.at("badgeIconStyle").get<int32_t>();
2419 target->badgeStyle_ = static_cast<NotificationRequest::BadgeStyle>(badgeStyleValue);
2420 }
2421
2422 if (jsonObject.find("notificationContentType") != jsonEnd &&
2423 jsonObject.at("notificationContentType").is_number_integer()) {
2424 auto notificationContentType = jsonObject.at("notificationContentType").get<int32_t>();
2425 target->notificationContentType_ = static_cast<NotificationContent::Type>(notificationContentType);
2426 }
2427 }
2428
ConvertJsonToBool(NotificationRequest * target,const nlohmann::json & jsonObject)2429 void NotificationRequest::ConvertJsonToBool(NotificationRequest *target, const nlohmann::json &jsonObject)
2430 {
2431 if (target == nullptr) {
2432 ANS_LOGE("null target");
2433 return;
2434 }
2435
2436 const auto &jsonEnd = jsonObject.cend();
2437
2438 if (jsonObject.find("showDeliveryTime") != jsonEnd && jsonObject.at("showDeliveryTime").is_boolean()) {
2439 target->showDeliveryTime_ = jsonObject.at("showDeliveryTime").get<bool>();
2440 }
2441
2442 if (jsonObject.find("tapDismissed") != jsonEnd && jsonObject.at("tapDismissed").is_boolean()) {
2443 target->tapDismissed_ = jsonObject.at("tapDismissed").get<bool>();
2444 }
2445
2446 if (jsonObject.find("colorEnabled") != jsonEnd && jsonObject.at("colorEnabled").is_boolean()) {
2447 target->colorEnabled_ = jsonObject.at("colorEnabled").get<bool>();
2448 }
2449
2450 if (jsonObject.find("isOngoing") != jsonEnd && jsonObject.at("isOngoing").is_boolean()) {
2451 target->inProgress_ = jsonObject.at("isOngoing").get<bool>();
2452 }
2453
2454 if (jsonObject.find("isAlertOnce") != jsonEnd && jsonObject.at("isAlertOnce").is_boolean()) {
2455 target->alertOneTime_ = jsonObject.at("isAlertOnce").get<bool>();
2456 }
2457
2458 if (jsonObject.find("isStopwatch") != jsonEnd && jsonObject.at("isStopwatch").is_boolean()) {
2459 target->showStopwatch_ = jsonObject.at("isStopwatch").get<bool>();
2460 }
2461
2462 if (jsonObject.find("isCountdown") != jsonEnd && jsonObject.at("isCountdown").is_boolean()) {
2463 target->isCountdown_ = jsonObject.at("isCountdown").get<bool>();
2464 }
2465
2466 if (jsonObject.find("isUnremovable") != jsonEnd && jsonObject.at("isUnremovable").is_boolean()) {
2467 target->unremovable_ = jsonObject.at("isUnremovable").get<bool>();
2468 }
2469
2470 if (jsonObject.find("isFloatingIcon") != jsonEnd && jsonObject.at("isFloatingIcon").is_boolean()) {
2471 target->floatingIcon_ = jsonObject.at("isFloatingIcon").get<bool>();
2472 }
2473
2474 if (jsonObject.find("updateOnly") != jsonEnd && jsonObject.at("updateOnly").is_boolean()) {
2475 target->updateOnly_ = jsonObject.at("updateOnly").get<bool>();
2476 }
2477
2478 if (jsonObject.find("distributedCollaborate") != jsonEnd && jsonObject.at("distributedCollaborate").is_boolean()) {
2479 target->distributedCollaborate_ = jsonObject.at("distributedCollaborate").get<bool>();
2480 }
2481
2482 if (jsonObject.find("isRemoveAllowed") != jsonEnd && jsonObject.at("isRemoveAllowed").is_boolean()) {
2483 target->isRemoveAllowed_ = jsonObject.at("isRemoveAllowed").get<bool>();
2484 }
2485
2486 ConvertJsonToBoolExt(target, jsonObject);
2487 }
2488
ConvertJsonToBoolExt(NotificationRequest * target,const nlohmann::json & jsonObject)2489 void NotificationRequest::ConvertJsonToBoolExt(NotificationRequest *target, const nlohmann::json &jsonObject)
2490 {
2491 const auto &jsonEnd = jsonObject.cend();
2492
2493 if (jsonObject.find("isAgent") != jsonEnd && jsonObject.at("isAgent").is_boolean()) {
2494 target->isAgent_ = jsonObject.at("isAgent").get<bool>();
2495 }
2496 }
2497
ConvertJsonToPixelMap(NotificationRequest * target,const nlohmann::json & jsonObject)2498 void NotificationRequest::ConvertJsonToPixelMap(NotificationRequest *target, const nlohmann::json &jsonObject)
2499 {
2500 if (target == nullptr) {
2501 ANS_LOGE("null target");
2502 return;
2503 }
2504
2505 const auto &jsonEnd = jsonObject.cend();
2506
2507 if (jsonObject.find("smallIcon") != jsonEnd && jsonObject.at("smallIcon").is_string()) {
2508 auto littleIconStr = jsonObject.at("smallIcon").get<std::string>();
2509 target->littleIcon_ = AnsImageUtil::UnPackImage(littleIconStr);
2510 }
2511
2512 if (jsonObject.find("largeIcon") != jsonEnd && jsonObject.at("largeIcon").is_string()) {
2513 auto bigIconStr = jsonObject.at("largeIcon").get<std::string>();
2514 target->bigIcon_ = AnsImageUtil::UnPackImage(bigIconStr);
2515 }
2516
2517 if (jsonObject.find("overlayIcon") != jsonEnd && jsonObject.at("overlayIcon").is_string()) {
2518 auto overlayIconStr = jsonObject.at("overlayIcon").get<std::string>();
2519 target->overlayIcon_ = AnsImageUtil::UnPackImage(overlayIconStr);
2520 }
2521 }
2522
ConvertJsonToNotificationContent(NotificationRequest * target,const nlohmann::json & jsonObject)2523 bool NotificationRequest::ConvertJsonToNotificationContent(
2524 NotificationRequest *target, const nlohmann::json &jsonObject)
2525 {
2526 if (target == nullptr) {
2527 ANS_LOGE("null target");
2528 return false;
2529 }
2530
2531 const auto &jsonEnd = jsonObject.cend();
2532
2533 if (jsonObject.find("content") != jsonEnd) {
2534 auto contentObj = jsonObject.at("content");
2535 if (!contentObj.is_null()) {
2536 auto pContent = NotificationJsonConverter::ConvertFromJson<NotificationContent>(contentObj);
2537 if (pContent == nullptr) {
2538 ANS_LOGE("null pContent");
2539 return false;
2540 }
2541
2542 target->notificationContent_ = std::shared_ptr<NotificationContent>(pContent);
2543 }
2544 }
2545
2546 return true;
2547 }
2548
ConvertJsonToNotificationActionButton(NotificationRequest * target,const nlohmann::json & jsonObject)2549 bool NotificationRequest::ConvertJsonToNotificationActionButton(
2550 NotificationRequest *target, const nlohmann::json &jsonObject)
2551 {
2552 if (target == nullptr) {
2553 ANS_LOGE("null target");
2554 return false;
2555 }
2556 int32_t targetUid = -1;
2557 if (target->GetOwnerUid() != DEFAULT_UID) {
2558 targetUid = target->GetOwnerUid();
2559 }
2560 ANS_LOGI("wantAgent Fromjson, uid = %{public}d ", targetUid);
2561
2562 const auto &jsonEnd = jsonObject.cend();
2563
2564 if (jsonObject.find("actionButtons") != jsonEnd) {
2565 auto buttonArr = jsonObject.at("actionButtons");
2566 for (auto &btnObj : buttonArr) {
2567 auto pBtn = NotificationActionButton::ConvertNotificationActionButton(targetUid, btnObj);
2568 if (pBtn == nullptr) {
2569 ANS_LOGE("null pBtn");
2570 return false;
2571 }
2572
2573 target->actionButtons_.emplace_back(pBtn);
2574 }
2575 }
2576
2577 return true;
2578 }
2579
ConvertJsonToNotificationDistributedOptions(NotificationRequest * target,const nlohmann::json & jsonObject)2580 bool NotificationRequest::ConvertJsonToNotificationDistributedOptions(
2581 NotificationRequest *target, const nlohmann::json &jsonObject)
2582 {
2583 if (target == nullptr) {
2584 ANS_LOGE("null target");
2585 return false;
2586 }
2587
2588 const auto &jsonEnd = jsonObject.cend();
2589
2590 if (jsonObject.find("distributedOptions") != jsonEnd) {
2591 auto optObj = jsonObject.at("distributedOptions");
2592 if (!optObj.is_null()) {
2593 auto *pOpt = NotificationJsonConverter::ConvertFromJson<NotificationDistributedOptions>(optObj);
2594 if (pOpt == nullptr) {
2595 ANS_LOGE("null pOpt");
2596 return false;
2597 }
2598
2599 target->distributedOptions_ = *pOpt;
2600 delete pOpt;
2601 }
2602 }
2603
2604 return true;
2605 }
2606
ConvertJsonToNotificationFlags(NotificationRequest * target,const nlohmann::json & jsonObject)2607 bool NotificationRequest::ConvertJsonToNotificationFlags(
2608 NotificationRequest *target, const nlohmann::json &jsonObject)
2609 {
2610 if (target == nullptr) {
2611 ANS_LOGE("null target");
2612 return false;
2613 }
2614
2615 const auto &jsonEnd = jsonObject.cend();
2616
2617 if (jsonObject.find("notificationFlags") != jsonEnd) {
2618 auto flagsObj = jsonObject.at("notificationFlags");
2619 if (!flagsObj.is_null()) {
2620 auto *pFlags = NotificationJsonConverter::ConvertFromJson<NotificationFlags>(flagsObj);
2621 if (pFlags == nullptr) {
2622 ANS_LOGE("null pFlags");
2623 return false;
2624 }
2625
2626 target->notificationFlags_ = std::shared_ptr<NotificationFlags>(pFlags);
2627 }
2628 }
2629
2630 return true;
2631 }
2632
ConvertJsonToNotificationBundleOption(NotificationRequest * target,const nlohmann::json & jsonObject)2633 bool NotificationRequest::ConvertJsonToNotificationBundleOption(
2634 NotificationRequest *target, const nlohmann::json &jsonObject)
2635 {
2636 if (target == nullptr) {
2637 ANS_LOGE("null target");
2638 return false;
2639 }
2640
2641 const auto &jsonEnd = jsonObject.cend();
2642
2643 if (jsonObject.find("notificationBundleOption") != jsonEnd) {
2644 auto bundleOptionObj = jsonObject.at("notificationBundleOption");
2645 if (!bundleOptionObj.is_null()) {
2646 auto *pBundleOption = NotificationJsonConverter::ConvertFromJson<NotificationBundleOption>(bundleOptionObj);
2647 if (pBundleOption == nullptr) {
2648 ANS_LOGE("null pBundleOption");
2649 return false;
2650 }
2651
2652 target->notificationBundleOption_ = std::shared_ptr<NotificationBundleOption>(pBundleOption);
2653 }
2654 }
2655
2656 return true;
2657 }
2658
ConvertJsonToAgentBundle(NotificationRequest * target,const nlohmann::json & jsonObject)2659 bool NotificationRequest::ConvertJsonToAgentBundle(
2660 NotificationRequest *target, const nlohmann::json &jsonObject)
2661 {
2662 if (target == nullptr) {
2663 ANS_LOGE("null target");
2664 return false;
2665 }
2666
2667 const auto &jsonEnd = jsonObject.cend();
2668
2669 if (jsonObject.find("agentBundle") != jsonEnd) {
2670 auto bundleOptionObj = jsonObject.at("agentBundle");
2671 if (!bundleOptionObj.is_null()) {
2672 auto *pBundleOption = NotificationJsonConverter::ConvertFromJson<NotificationBundleOption>(bundleOptionObj);
2673 if (pBundleOption == nullptr) {
2674 ANS_LOGE("null pBundleOption");
2675 return false;
2676 }
2677
2678 target->agentBundle_ = std::shared_ptr<NotificationBundleOption>(pBundleOption);
2679 }
2680 }
2681
2682 return true;
2683 }
2684
IsCommonLiveView() const2685 bool NotificationRequest::IsCommonLiveView() const
2686 {
2687 return (slotType_ == NotificationConstant::SlotType::LIVE_VIEW) &&
2688 (notificationContentType_ == NotificationContent::Type::LIVE_VIEW);
2689 }
2690
IsSystemLiveView() const2691 bool NotificationRequest::IsSystemLiveView() const
2692 {
2693 return (slotType_ == NotificationConstant::SlotType::LIVE_VIEW) &&
2694 (notificationContentType_ == NotificationContent::Type::LOCAL_LIVE_VIEW);
2695 }
2696
CheckVersion(const sptr<NotificationRequest> & oldRequest) const2697 ErrCode NotificationRequest::CheckVersion(const sptr<NotificationRequest> &oldRequest) const
2698 {
2699 auto content = notificationContent_->GetNotificationContent();
2700 auto liveView = std::static_pointer_cast<NotificationLiveViewContent>(content);
2701 auto oldContent = oldRequest->GetContent()->GetNotificationContent();
2702 auto oldLiveView = std::static_pointer_cast<NotificationLiveViewContent>(oldContent);
2703
2704 if (oldLiveView->GetVersion() == NotificationLiveViewContent::MAX_VERSION) {
2705 return ERR_OK;
2706 }
2707 if (liveView->GetVersion() == NotificationLiveViewContent::MAX_VERSION) {
2708 ANS_LOGE("Invalid version, creator bundle name %{public}s, id %{public}d, "
2709 "old version %{public}u, new version %{public}u.", GetCreatorBundleName().c_str(),
2710 GetNotificationId(), oldLiveView->GetVersion(), liveView->GetVersion());
2711 return ERR_ANS_EXPIRED_NOTIFICATION;
2712 }
2713 if (oldLiveView->GetVersion() >= liveView->GetVersion()) {
2714 ANS_LOGE("Live view has finished, creator bundle name %{public}s, id %{public}d, "
2715 "old version %{public}u, new version %{public}u.", GetCreatorBundleName().c_str(),
2716 GetNotificationId(), oldLiveView->GetVersion(), liveView->GetVersion());
2717 return ERR_ANS_EXPIRED_NOTIFICATION;
2718 }
2719 return ERR_OK;
2720 }
2721
CheckNotificationRequest(const sptr<NotificationRequest> & oldRequest) const2722 ErrCode NotificationRequest::CheckNotificationRequest(const sptr<NotificationRequest> &oldRequest) const
2723 {
2724 if (!IsCommonLiveView()) {
2725 if ((oldRequest != nullptr) && oldRequest->IsCommonLiveView()) {
2726 ANS_LOGE("Invalid new request param, slot type %{public}d, content type %{public}d.",
2727 GetSlotType(), GetNotificationType());
2728 return ERR_ANS_INVALID_PARAM;
2729 }
2730 return ERR_OK;
2731 }
2732
2733 using StatusType = NotificationLiveViewContent::LiveViewStatus;
2734 auto content = notificationContent_->GetNotificationContent();
2735 auto liveView = std::static_pointer_cast<NotificationLiveViewContent>(content);
2736 auto status = liveView->GetLiveViewStatus();
2737 if (oldRequest == nullptr) {
2738 if (status != StatusType::LIVE_VIEW_CREATE) {
2739 ANS_LOGE("Doesn't exist live view, bundle name %{public}s, id %{public}d.",
2740 GetCreatorBundleName().c_str(), GetNotificationId());
2741 return ERR_ANS_NOTIFICATION_NOT_EXISTS;
2742 }
2743 return ERR_OK;
2744 }
2745
2746 if (!oldRequest->IsCommonLiveView()) {
2747 ANS_LOGE("Invalid old request param, slot type %{public}d, content type %{public}d.",
2748 oldRequest->GetSlotType(), oldRequest->GetNotificationType());
2749 return ERR_ANS_INVALID_PARAM;
2750 }
2751
2752 if (status == StatusType::LIVE_VIEW_CREATE) {
2753 ANS_LOGW("Repeat create live view, bundle name %{public}s, id %{public}d.",
2754 GetCreatorBundleName().c_str(), GetNotificationId());
2755 return ERR_ANS_REPEAT_CREATE;
2756 }
2757
2758 auto oldContent = oldRequest->GetContent()->GetNotificationContent();
2759 auto oldLiveView = std::static_pointer_cast<NotificationLiveViewContent>(oldContent);
2760 auto oldStatus = oldLiveView->GetLiveViewStatus();
2761 if (oldStatus == StatusType::LIVE_VIEW_END) {
2762 ANS_LOGW("Live view has finished, bundle name %{public}s, id %{public}d.",
2763 GetCreatorBundleName().c_str(), GetNotificationId());
2764 return ERR_ANS_END_NOTIFICATION;
2765 }
2766
2767 return CheckVersion(oldRequest);
2768 }
2769
FillMissingParameters(const sptr<NotificationRequest> & oldRequest)2770 void NotificationRequest::FillMissingParameters(const sptr<NotificationRequest> &oldRequest)
2771 {
2772 if (!IsCommonLiveView() || (oldRequest == nullptr)) {
2773 return;
2774 }
2775
2776 updateDeadLine_ = oldRequest->updateDeadLine_;
2777 finishDeadLine_ = oldRequest->finishDeadLine_;
2778 if (autoDeletedTime_ == NotificationConstant::INVALID_AUTO_DELETE_TIME) {
2779 autoDeletedTime_ = oldRequest->autoDeletedTime_;
2780 }
2781 if (wantAgent_ == nullptr) {
2782 wantAgent_ = oldRequest->wantAgent_;
2783 }
2784
2785 auto content = notificationContent_->GetNotificationContent();
2786 auto newLiveViewContent = std::static_pointer_cast<NotificationLiveViewContent>(content);
2787 if (newLiveViewContent->GetLiveViewStatus() ==
2788 NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_FULL_UPDATE) {
2789 return;
2790 }
2791 IncrementalUpdateLiveview(oldRequest);
2792 }
2793
IncrementalUpdateLiveview(const sptr<NotificationRequest> & oldRequest)2794 void NotificationRequest::IncrementalUpdateLiveview(const sptr<NotificationRequest> &oldRequest)
2795 {
2796 auto content = notificationContent_->GetNotificationContent();
2797 auto newLiveViewContent = std::static_pointer_cast<NotificationLiveViewContent>(content);
2798 auto newExtraInfo = newLiveViewContent->GetExtraInfo();
2799 auto oldContent = oldRequest->GetContent()->GetNotificationContent();
2800 auto oldLiveViewContent = std::static_pointer_cast<NotificationLiveViewContent>(oldContent);
2801 auto oldExtraInfo = oldLiveViewContent->GetExtraInfo();
2802 if (newExtraInfo == nullptr) {
2803 newLiveViewContent->SetExtraInfo(oldExtraInfo);
2804 } else if (oldExtraInfo != nullptr) {
2805 newExtraInfo->Remove("eventControl");
2806 auto oldKeySet = oldExtraInfo->KeySet();
2807 for (const auto &key : oldKeySet) {
2808 if (!newExtraInfo->HasParam(key)) {
2809 newExtraInfo->SetParam(key, oldExtraInfo->GetParam(key));
2810 }
2811 }
2812 }
2813
2814 auto oldIsOnlyLocalUpdate = oldLiveViewContent->GetIsOnlyLocalUpdate();
2815 if (oldIsOnlyLocalUpdate!= newLiveViewContent->GetIsOnlyLocalUpdate()) {
2816 newLiveViewContent->SetIsOnlyLocalUpdate(oldIsOnlyLocalUpdate);
2817 }
2818
2819 auto newPicture = newLiveViewContent->GetPicture();
2820 bool isSet = false;
2821 for (const auto &pictureRecord : oldLiveViewContent->GetPicture()) {
2822 if (newPicture.find(pictureRecord.first) != newPicture.end()) {
2823 continue;
2824 }
2825 newPicture[pictureRecord.first] = pictureRecord.second;
2826 isSet = true;
2827 }
2828 if (isSet) {
2829 newLiveViewContent->SetPicture(newPicture);
2830 }
2831
2832 auto oldExtensionWantAgent = oldLiveViewContent->GetExtensionWantAgent();
2833 if (newLiveViewContent->GetExtensionWantAgent() == nullptr) {
2834 newLiveViewContent->SetExtensionWantAgent(oldExtensionWantAgent);
2835 }
2836 }
2837
GetBaseKey(const std::string & deviceId)2838 std::string NotificationRequest::GetBaseKey(const std::string &deviceId)
2839 {
2840 const char *keySpliter = "_";
2841
2842 if (distributedCollaborate_) {
2843 ANS_LOGI("NotificationRequest use collaborate!");
2844 return "ans_distributed" + distributedHashCode_;
2845 }
2846
2847 std::stringstream stream;
2848 uint32_t hashCodeGeneratetype = GetHashCodeGenerateType();
2849 if (IsAgentNotification()) {
2850 stream << appInstanceKey_ << keySpliter << deviceId << keySpliter <<
2851 ownerUserId_ << keySpliter << ownerUid_ << keySpliter <<
2852 ownerBundleName_ << keySpliter << label_ << keySpliter << notificationId_;
2853 } else {
2854 if (hashCodeGeneratetype == 1) {
2855 stream << appInstanceKey_ << keySpliter << deviceId << keySpliter <<
2856 creatorUserId_ << keySpliter << creatorUid_ << keySpliter <<
2857 ownerUserId_ << keySpliter << label_ << keySpliter << notificationId_;
2858 } else {
2859 stream << appInstanceKey_ << keySpliter << deviceId << keySpliter <<
2860 creatorUserId_ << keySpliter << creatorUid_ << keySpliter <<
2861 creatorBundleName_ << keySpliter << label_ << keySpliter << notificationId_;
2862 }
2863 }
2864 return stream.str();
2865 }
2866
GetKey()2867 std::string NotificationRequest::GetKey()
2868 {
2869 std::stringstream stream;
2870 const char *keySpliter = "_";
2871 stream << REQUEST_STORAGE_KEY_PREFIX << keySpliter << GetBaseKey("");
2872 return stream.str();
2873 }
2874
GetSecureKey()2875 std::string NotificationRequest::GetSecureKey()
2876 {
2877 std::stringstream stream;
2878 const char *keySpliter = "_";
2879 stream << REQUEST_STORAGE_SECURE_KEY_PREFIX << keySpliter << GetBaseKey("");
2880 return stream.str();
2881 }
2882
CheckImageOverSizeForPixelMap(const std::shared_ptr<Media::PixelMap> & pixelMap,uint32_t maxSize)2883 bool NotificationRequest::CheckImageOverSizeForPixelMap(
2884 const std::shared_ptr<Media::PixelMap> &pixelMap, uint32_t maxSize)
2885 {
2886 if (pixelMap == nullptr) {
2887 return false;
2888 }
2889
2890 auto size = static_cast<uint32_t>(pixelMap->GetByteCount());
2891 return size > maxSize;
2892 }
2893
CheckImageSizeForConverSation(std::shared_ptr<NotificationBasicContent> & content)2894 ErrCode NotificationRequest::CheckImageSizeForConverSation(std::shared_ptr<NotificationBasicContent> &content)
2895 {
2896 auto conversationalContent = std::static_pointer_cast<NotificationConversationalContent>(content);
2897 auto picture = conversationalContent->GetMessageUser().GetPixelMap();
2898 if (CheckImageOverSizeForPixelMap(picture, MAX_ICON_SIZE)) {
2899 ANS_LOGE("The size of picture in ConversationalContent's message user exceeds limit");
2900 return ERR_ANS_ICON_OVER_SIZE;
2901 }
2902
2903 auto messages = conversationalContent->GetAllConversationalMessages();
2904 for (auto &msg : messages) {
2905 if (!msg) {
2906 continue;
2907 }
2908 auto img = msg->GetSender().GetPixelMap();
2909 if (CheckImageOverSizeForPixelMap(img, MAX_ICON_SIZE)) {
2910 ANS_LOGE("The size of picture in ConversationalContent's message exceeds limit");
2911 return ERR_ANS_ICON_OVER_SIZE;
2912 }
2913 }
2914 return ERR_OK;
2915 }
2916
CheckImageSizeForPicture(std::shared_ptr<NotificationBasicContent> & content)2917 ErrCode NotificationRequest::CheckImageSizeForPicture(std::shared_ptr<NotificationBasicContent> &content)
2918 {
2919 auto pictureContent = std::static_pointer_cast<NotificationPictureContent>(content);
2920 auto bigPicture = pictureContent->GetBigPicture();
2921 if (CheckImageOverSizeForPixelMap(bigPicture, MAX_PICTURE_SIZE)) {
2922 ANS_LOGE("The size of big picture in PictureContent exceeds limit");
2923 return ERR_ANS_PICTURE_OVER_SIZE;
2924 }
2925 return ERR_OK;
2926 }
2927
CheckImageSizeForLiveView(std::shared_ptr<NotificationBasicContent> & content,bool collaborateFlag)2928 ErrCode NotificationRequest::CheckImageSizeForLiveView(std::shared_ptr<NotificationBasicContent> &content,
2929 bool collaborateFlag)
2930 {
2931 auto liveViewContent = std::static_pointer_cast<NotificationLiveViewContent>(content);
2932 auto pictureMap = liveViewContent->GetPicture();
2933 for (const auto &pixelMapRecord : pictureMap) {
2934 if (pixelMapRecord.second.empty()) {
2935 ANS_LOGE("Picture key exist, but picture content is empty.");
2936 return ERR_ANS_INVALID_PARAM;
2937 }
2938 if (pixelMapRecord.second.size() > MAX_LIVE_VIEW_ICON_NUM) {
2939 ANS_LOGE("Picture key exist, but picture content count exceeds limit.");
2940 return ERR_ANS_INVALID_PARAM;
2941 }
2942 for (const auto &pixelMap : pixelMapRecord.second) {
2943 if (!collaborateFlag && CheckImageOverSizeForPixelMap(pixelMap, MAX_ICON_SIZE)) {
2944 ANS_LOGE("The size of big picture in PictureContent exceeds limit.");
2945 return ERR_ANS_ICON_OVER_SIZE;
2946 }
2947 }
2948 }
2949 return ERR_OK;
2950 }
2951
CheckImageSizeForContent(bool collaborateFlag) const2952 ErrCode NotificationRequest::CheckImageSizeForContent(bool collaborateFlag) const
2953 {
2954 auto content = GetContent();
2955 if (content == nullptr) {
2956 ANS_LOGE("null content");
2957 return ERR_OK;
2958 }
2959
2960 auto basicContent = GetContent()->GetNotificationContent();
2961 if (basicContent == nullptr) {
2962 ANS_LOGE("null basicContent");
2963 return ERR_OK;
2964 }
2965
2966 if (GetSlotType() == NotificationConstant::SlotType::LIVE_VIEW) {
2967 auto result = CheckLockScreenPictureSizeForLiveView(basicContent);
2968 if (result != ERR_OK) {
2969 return result;
2970 }
2971 }
2972
2973 auto contentType = GetNotificationType();
2974 switch (contentType) {
2975 case NotificationContent::Type::CONVERSATION:
2976 return CheckImageSizeForConverSation(basicContent);
2977 case NotificationContent::Type::PICTURE:
2978 return CheckImageSizeForPicture(basicContent);
2979 case NotificationContent::Type::LIVE_VIEW:
2980 return CheckImageSizeForLiveView(basicContent, collaborateFlag);
2981 default:
2982 return ERR_OK;
2983 }
2984 }
2985
HasUserInputButton()2986 bool NotificationRequest::HasUserInputButton()
2987 {
2988 for (std::shared_ptr<NotificationActionButton> button : actionButtons_) {
2989 if (button->GetUserInput() != nullptr) {
2990 std::string inputKey = button->GetUserInput()->GetInputKey();
2991 if (!inputKey.empty()) {
2992 return true;
2993 }
2994 }
2995 }
2996 return false;
2997 }
2998
SetIsCoverActionButtons(bool isCoverActionButtons)2999 void NotificationRequest::SetIsCoverActionButtons(bool isCoverActionButtons)
3000 {
3001 isCoverActionButtons_ = isCoverActionButtons;
3002 }
3003
IsCoverActionButtons() const3004 bool NotificationRequest::IsCoverActionButtons() const
3005 {
3006 return isCoverActionButtons_;
3007 }
3008
SetAppMessageId(const std::string & appMessageId)3009 void NotificationRequest::SetAppMessageId(const std::string &appMessageId)
3010 {
3011 appMessageId_ = appMessageId;
3012 }
3013
GetAppMessageId() const3014 std::string NotificationRequest::GetAppMessageId() const
3015 {
3016 return appMessageId_;
3017 }
3018
SetSound(const std::string & sound)3019 void NotificationRequest::SetSound(const std::string &sound)
3020 {
3021 sound_ = sound;
3022 }
3023
GetSound() const3024 std::string NotificationRequest::GetSound() const
3025 {
3026 return sound_;
3027 }
3028
GenerateUniqueKey()3029 std::string NotificationRequest::GenerateUniqueKey()
3030 {
3031 const char *keySpliter = "_";
3032 int typeFlag = 0;
3033 if (GetSlotType() == NotificationConstant::SlotType::LIVE_VIEW) {
3034 typeFlag = 1;
3035 }
3036
3037 std::stringstream stream;
3038 if (IsAgentNotification()) {
3039 stream << ownerUid_ << keySpliter << ownerBundleName_ << keySpliter << ownerUserId_ << keySpliter <<
3040 typeFlag << keySpliter << appMessageId_;
3041 } else {
3042 stream << creatorUid_ << keySpliter << creatorBundleName_ << keySpliter << creatorUserId_ << keySpliter <<
3043 typeFlag << keySpliter << appMessageId_;
3044 }
3045 return stream.str();
3046 }
3047
GenerateDistributedUniqueKey()3048 std::string NotificationRequest::GenerateDistributedUniqueKey()
3049 {
3050 const char *keySpliter = "_";
3051 int typeFlag = 0;
3052 if (GetSlotType() == NotificationConstant::SlotType::LIVE_VIEW) {
3053 typeFlag = 1;
3054 }
3055
3056 std::stringstream stream;
3057 if (IsAgentNotification()) {
3058 stream << ownerBundleName_ << keySpliter << ownerUserId_ << keySpliter << typeFlag << keySpliter
3059 << appMessageId_;
3060 } else {
3061 stream << creatorBundleName_ << keySpliter << creatorUserId_ << keySpliter << typeFlag << keySpliter
3062 << appMessageId_;
3063 }
3064 return stream.str();
3065 }
3066
SetUnifiedGroupInfo(const std::shared_ptr<NotificationUnifiedGroupInfo> & unifiedGroupInfo)3067 void NotificationRequest::SetUnifiedGroupInfo(const std::shared_ptr<NotificationUnifiedGroupInfo> &unifiedGroupInfo)
3068 {
3069 unifiedGroupInfo_ = unifiedGroupInfo;
3070 }
3071
GetUnifiedGroupInfo() const3072 std::shared_ptr<NotificationUnifiedGroupInfo> NotificationRequest::GetUnifiedGroupInfo() const
3073 {
3074 return unifiedGroupInfo_;
3075 }
3076
CheckLockScreenPictureSizeForLiveView(std::shared_ptr<NotificationBasicContent> & content)3077 ErrCode NotificationRequest::CheckLockScreenPictureSizeForLiveView(std::shared_ptr<NotificationBasicContent> &content)
3078 {
3079 auto lockScreenPicture = content->GetLockScreenPicture();
3080 if (CheckImageOverSizeForPixelMap(lockScreenPicture, MAX_PICTURE_SIZE)) {
3081 ANS_LOGE("The size of lockScreen picture in live view exceeds limit");
3082 return ERR_ANS_PICTURE_OVER_SIZE;
3083 }
3084 return ERR_OK;
3085 }
3086
SetPublishDelayTime(uint32_t delayTime)3087 void NotificationRequest::SetPublishDelayTime(uint32_t delayTime)
3088 {
3089 publishDelayTime_ = delayTime;
3090 }
3091
GetPublishDelayTime() const3092 uint32_t NotificationRequest::GetPublishDelayTime() const
3093 {
3094 return publishDelayTime_;
3095 }
3096
SetUpdateByOwnerAllowed(bool isUpdateByOwnerAllowed)3097 void NotificationRequest::SetUpdateByOwnerAllowed(bool isUpdateByOwnerAllowed)
3098 {
3099 isUpdateByOwnerAllowed_ = isUpdateByOwnerAllowed;
3100 }
3101
IsUpdateByOwnerAllowed() const3102 bool NotificationRequest::IsUpdateByOwnerAllowed() const
3103 {
3104 return isUpdateByOwnerAllowed_;
3105 }
3106
SetUpdateOnly(bool updateOnly)3107 void NotificationRequest::SetUpdateOnly(bool updateOnly)
3108 {
3109 updateOnly_ = updateOnly;
3110 }
3111
IsUpdateOnly() const3112 bool NotificationRequest::IsUpdateOnly() const
3113 {
3114 return updateOnly_;
3115 }
3116
GetLittleIconType() const3117 const std::string NotificationRequest::GetLittleIconType() const
3118 {
3119 return littleIconType_;
3120 }
3121
GetDistributedCollaborate() const3122 bool NotificationRequest::GetDistributedCollaborate() const
3123 {
3124 return distributedCollaborate_;
3125 }
3126
SetDistributedCollaborate(bool distributedCollaborate)3127 void NotificationRequest::SetDistributedCollaborate(bool distributedCollaborate)
3128 {
3129 distributedCollaborate_ = distributedCollaborate;
3130 }
3131
GetDistributedHashCode() const3132 const std::string NotificationRequest::GetDistributedHashCode() const
3133 {
3134 return distributedHashCode_;
3135 }
3136
SetDistributedHashCode(const std::string hashCode)3137 void NotificationRequest::SetDistributedHashCode(const std::string hashCode)
3138 {
3139 distributedHashCode_ = hashCode;
3140 }
3141
AdddeviceStatu(const std::string & deviceType,const std::string deviceStatu)3142 void NotificationRequest::AdddeviceStatu(const std::string &deviceType,
3143 const std::string deviceStatu)
3144 {
3145 deviceStatus_[deviceType] = deviceStatu;
3146 }
3147
GetdeviceStatus() const3148 const std::map<std::string, std::string> NotificationRequest::GetdeviceStatus() const
3149 {
3150 return deviceStatus_;
3151 }
3152
IsAtomicServiceNotification()3153 bool NotificationRequest::IsAtomicServiceNotification()
3154 {
3155 if (!IsCommonLiveView() || !IsAgentNotification()) {
3156 ANS_LOGD("not commonLiveView or not agent");
3157 return false;
3158 }
3159
3160 if (extendInfo_ == nullptr) {
3161 ANS_LOGD("extend info is null.");
3162 return false;
3163 }
3164 int32_t installedStatus = extendInfo_->GetIntParam("autoServiceInstallStatus", PKG_INSTALL_STATUS_UNKMOWN);
3165 if (installedStatus == PKG_INSTALL_STATUS_UNINSTALL) {
3166 ANS_LOGD("AtomicServiceNotification.");
3167 return true;
3168 }
3169 return false;
3170 }
3171
SetDistributedFlagBit(const NotificationConstant::ReminderFlag & bit,const bool status,const std::set<std::string> & unaffectDevice)3172 void NotificationRequest::SetDistributedFlagBit(
3173 const NotificationConstant::ReminderFlag &bit,
3174 const bool status,
3175 const std::set<std::string> &unaffectDevice)
3176 {
3177 if (!notificationFlags_) {
3178 ANS_LOGE("notificationFlags not init");
3179 return;
3180 }
3181 SetFlagBit(bit, status, notificationFlags_);
3182
3183 if (!notificationFlagsOfDevices_ || notificationFlagsOfDevices_->size() <= 0 || status) {
3184 return;
3185 }
3186 for (auto it = notificationFlagsOfDevices_->begin(); it != notificationFlagsOfDevices_->end(); ++it) {
3187 std::string deviceType = it->first;
3188 std::shared_ptr<NotificationFlags> flag = it->second;
3189 if (deviceType == NotificationConstant::HEADSET_DEVICE_TYPE) {
3190 continue;
3191 }
3192
3193 if (unaffectDevice.find(deviceType) == unaffectDevice.end()) {
3194 SetFlagBit(bit, status, flag);
3195 }
3196 }
3197 }
3198
SetFlagBit(const NotificationConstant::ReminderFlag & bit,const bool status,std::shared_ptr<NotificationFlags> & flag)3199 void NotificationRequest::SetFlagBit(
3200 const NotificationConstant::ReminderFlag &bit,
3201 const bool status,
3202 std::shared_ptr<NotificationFlags> &flag)
3203 {
3204 if (!flag) {
3205 return;
3206 }
3207 NotificationConstant::FlagStatus enumStatus = status? NotificationConstant::FlagStatus::OPEN :
3208 NotificationConstant::FlagStatus::CLOSE;
3209 switch (bit) {
3210 case NotificationConstant::ReminderFlag::SOUND_FLAG :
3211 flag->SetSoundEnabled(enumStatus);
3212 break;
3213 case NotificationConstant::ReminderFlag::LOCKSCREEN_FLAG :
3214 flag->SetLockScreenVisblenessEnabled(status);
3215 break;
3216 case NotificationConstant::ReminderFlag::BANNER_FLAG :
3217 flag->SetBannerEnabled(status);
3218 break;
3219 case NotificationConstant::ReminderFlag::LIGHTSCREEN_FLAG :
3220 flag->SetLightScreenEnabled(status);
3221 break;
3222 case NotificationConstant::ReminderFlag::VIBRATION_FLAG :
3223 flag->SetVibrationEnabled(enumStatus);
3224 break;
3225 case NotificationConstant::ReminderFlag::STATUSBAR_ICON_FLAG :
3226 flag->SetStatusIconEnabled(status);
3227 break;
3228 default :
3229 ANS_LOGE("unknow bit");
3230 break;
3231 }
3232 }
3233 } // namespace Notification
3234 } // namespace OHOS
3235