• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_inner_errors.h"
19 #include "ans_image_util.h"
20 #include "ans_log_wrapper.h"
21 #include "errors.h"
22 #include "notification_live_view_content.h"
23 #include "refbase.h"
24 #include "want_agent_helper.h"
25 #include "want_params_wrapper.h"
26 #include <memory>
27 
28 namespace OHOS {
29 namespace Notification {
30 const std::string NotificationRequest::CLASSIFICATION_ALARM {"alarm"};
31 const std::string NotificationRequest::CLASSIFICATION_CALL {"call"};
32 const std::string NotificationRequest::CLASSIFICATION_EMAIL {"email"};
33 const std::string NotificationRequest::CLASSIFICATION_ERROR {"err"};
34 const std::string NotificationRequest::CLASSIFICATION_EVENT {"event"};
35 const std::string NotificationRequest::CLASSIFICATION_MESSAGE {"msg"};
36 const std::string NotificationRequest::CLASSIFICATION_NAVIGATION {"navigation"};
37 const std::string NotificationRequest::CLASSIFICATION_PROGRESS {"progress"};
38 const std::string NotificationRequest::CLASSIFICATION_PROMO {"promo"};
39 const std::string NotificationRequest::CLASSIFICATION_RECOMMENDATION {"recommendation"};
40 const std::string NotificationRequest::CLASSIFICATION_REMINDER {"reminder"};
41 const std::string NotificationRequest::CLASSIFICATION_SERVICE {"service"};
42 const std::string NotificationRequest::CLASSIFICATION_SOCIAL {"social"};
43 const std::string NotificationRequest::CLASSIFICATION_STATUS {"status"};
44 const std::string NotificationRequest::CLASSIFICATION_SYSTEM {"sys"};
45 const std::string NotificationRequest::CLASSIFICATION_TRANSPORT {"transport"};
46 
47 const uint32_t NotificationRequest::COLOR_DEFAULT {0};
48 
49 const uint32_t NotificationRequest::COLOR_MASK {0xFF000000};
50 const std::size_t NotificationRequest::MAX_USER_INPUT_HISTORY {5};
51 const std::size_t NotificationRequest::MAX_ACTION_BUTTONS {3};
52 const std::size_t NotificationRequest::MAX_MESSAGE_USERS {1000};
53 
NotificationRequest(int32_t notificationId)54 NotificationRequest::NotificationRequest(int32_t notificationId) : notificationId_(notificationId)
55 {
56     createTime_ = GetNowSysTime();
57     deliveryTime_ = GetNowSysTime();
58 }
59 
NotificationRequest(const NotificationRequest & other)60 NotificationRequest::NotificationRequest(const NotificationRequest &other)
61 {
62     CopyBase(other);
63     CopyOther(other);
64 }
65 
operator =(const NotificationRequest & other)66 NotificationRequest &NotificationRequest::operator=(const NotificationRequest &other)
67 {
68     CopyBase(other);
69     CopyOther(other);
70 
71     return *this;
72 }
73 
~NotificationRequest()74 NotificationRequest::~NotificationRequest()
75 {}
76 
IsInProgress() const77 bool NotificationRequest::IsInProgress() const
78 {
79     return inProgress_;
80 }
81 
SetInProgress(bool isOngoing)82 void NotificationRequest::SetInProgress(bool isOngoing)
83 {
84     inProgress_ = isOngoing;
85 }
86 
IsUnremovable() const87 bool NotificationRequest::IsUnremovable() const
88 {
89     return unremovable_;
90 }
91 
SetUnremovable(bool isUnremovable)92 void NotificationRequest::SetUnremovable(bool isUnremovable)
93 {
94     unremovable_ = isUnremovable;
95 }
96 
SetBadgeNumber(uint32_t number)97 void NotificationRequest::SetBadgeNumber(uint32_t number)
98 {
99     badgeNumber_ = number;
100 }
101 
GetBadgeNumber() const102 uint32_t NotificationRequest::GetBadgeNumber() const
103 {
104     return badgeNumber_;
105 }
106 
SetNotificationId(int32_t notificationId)107 void NotificationRequest::SetNotificationId(int32_t notificationId)
108 {
109     notificationId_ = notificationId;
110 }
111 
GetNotificationId() const112 int32_t NotificationRequest::GetNotificationId() const
113 {
114     return notificationId_;
115 }
116 
SetWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> & wantAgent)117 void NotificationRequest::SetWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> &wantAgent)
118 {
119     wantAgent_ = wantAgent;
120 }
121 
GetWantAgent() const122 const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> NotificationRequest::GetWantAgent() const
123 {
124     return wantAgent_;
125 }
126 
SetRemovalWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> & wantAgent)127 void NotificationRequest::SetRemovalWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> &wantAgent)
128 {
129     removalWantAgent_ = wantAgent;
130 }
131 
GetRemovalWantAgent() const132 const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> NotificationRequest::GetRemovalWantAgent() const
133 {
134     return removalWantAgent_;
135 }
136 
SetMaxScreenWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> & wantAgent)137 void NotificationRequest::SetMaxScreenWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> &wantAgent)
138 {
139     maxScreenWantAgent_ = wantAgent;
140 }
141 
GetMaxScreenWantAgent() const142 const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> NotificationRequest::GetMaxScreenWantAgent() const
143 {
144     return maxScreenWantAgent_;
145 }
146 
SetAdditionalData(const std::shared_ptr<AAFwk::WantParams> & extras)147 void NotificationRequest::SetAdditionalData(const std::shared_ptr<AAFwk::WantParams> &extras)
148 {
149     additionalParams_ = extras;
150 }
151 
GetAdditionalData() const152 const std::shared_ptr<AAFwk::WantParams> NotificationRequest::GetAdditionalData() const
153 {
154     return additionalParams_;
155 }
156 
SetDeliveryTime(int64_t deliveryTime)157 void NotificationRequest::SetDeliveryTime(int64_t deliveryTime)
158 {
159     deliveryTime_ = deliveryTime;
160 }
161 
GetDeliveryTime() const162 int64_t NotificationRequest::GetDeliveryTime() const
163 {
164     return deliveryTime_;
165 }
166 
IsShowDeliveryTime() const167 bool NotificationRequest::IsShowDeliveryTime() const
168 {
169     return (deliveryTime_ != 0) && showDeliveryTime_;
170 }
171 
SetShowDeliveryTime(bool showDeliveryTime)172 void NotificationRequest::SetShowDeliveryTime(bool showDeliveryTime)
173 {
174     showDeliveryTime_ = showDeliveryTime;
175 }
176 
AddActionButton(const std::shared_ptr<NotificationActionButton> & actionButton)177 void NotificationRequest::AddActionButton(const std::shared_ptr<NotificationActionButton> &actionButton)
178 {
179     if (!actionButton) {
180         ANS_LOGW("actionButton can not be null");
181         return;
182     }
183 
184     if (actionButtons_.size() >= NotificationRequest::MAX_ACTION_BUTTONS) {
185         ANS_LOGW("three action buttons have been already added");
186         return;
187     }
188 
189     actionButtons_.emplace_back(actionButton);
190 }
191 
GetActionButtons() const192 const std::vector<std::shared_ptr<NotificationActionButton>> NotificationRequest::GetActionButtons() const
193 {
194     return actionButtons_;
195 }
196 
ClearActionButtons()197 void NotificationRequest::ClearActionButtons()
198 {
199     actionButtons_.clear();
200 }
201 
IsPermitSystemGeneratedContextualActionButtons() const202 bool NotificationRequest::IsPermitSystemGeneratedContextualActionButtons() const
203 {
204     return permitted_;
205 }
206 
SetPermitSystemGeneratedContextualActionButtons(bool permitted)207 void NotificationRequest::SetPermitSystemGeneratedContextualActionButtons(bool permitted)
208 {
209     permitted_ = permitted;
210 }
211 
IsAgentNotification() const212 bool NotificationRequest::IsAgentNotification() const
213 {
214     return isAgent_;
215 }
216 
SetIsAgentNotification(bool isAgent)217 void NotificationRequest::SetIsAgentNotification(bool isAgent)
218 {
219     isAgent_ = isAgent;
220 }
221 
AddMessageUser(const std::shared_ptr<MessageUser> & messageUser)222 void NotificationRequest::AddMessageUser(const std::shared_ptr<MessageUser> &messageUser)
223 {
224     if (!messageUser) {
225         ANS_LOGI("messageUser can not be null");
226         return;
227     }
228 
229     messageUsers_.emplace_back(messageUser);
230 }
231 
GetMessageUsers() const232 const std::vector<std::shared_ptr<MessageUser>> NotificationRequest::GetMessageUsers() const
233 {
234     return messageUsers_;
235 }
236 
IsAlertOneTime() const237 bool NotificationRequest::IsAlertOneTime() const
238 {
239     return alertOneTime_;
240 }
241 
SetAlertOneTime(bool isAlertOnce)242 void NotificationRequest::SetAlertOneTime(bool isAlertOnce)
243 {
244     alertOneTime_ = isAlertOnce;
245 }
246 
SetAutoDeletedTime(int64_t deletedTime)247 void NotificationRequest::SetAutoDeletedTime(int64_t deletedTime)
248 {
249     autoDeletedTime_ = deletedTime;
250 }
251 
GetAutoDeletedTime() const252 int64_t NotificationRequest::GetAutoDeletedTime() const
253 {
254     return autoDeletedTime_;
255 }
256 
SetUpdateDeadLine(int64_t updateDeadLine)257 void NotificationRequest::SetUpdateDeadLine(int64_t updateDeadLine)
258 {
259     updateDeadLine_ = updateDeadLine;
260 }
261 
GetUpdateDeadLine() const262 int64_t NotificationRequest::GetUpdateDeadLine() const
263 {
264     return updateDeadLine_;
265 }
266 
SetFinishDeadLine(int64_t finishDeadLine)267 void NotificationRequest::SetFinishDeadLine(int64_t finishDeadLine)
268 {
269     finishDeadLine_ = finishDeadLine;
270 }
271 
GetFinishDeadLine() const272 int64_t NotificationRequest::GetFinishDeadLine() const
273 {
274     return finishDeadLine_;
275 }
276 
SetArchiveDeadLine(int64_t archiveDeadLine)277 void NotificationRequest::SetArchiveDeadLine(int64_t archiveDeadLine)
278 {
279     archiveDeadLine_ = archiveDeadLine;
280 }
281 
GetArchiveDeadLine() const282 int64_t NotificationRequest::GetArchiveDeadLine() const
283 {
284     return archiveDeadLine_;
285 }
286 
SetLittleIcon(const std::shared_ptr<Media::PixelMap> & littleIcon)287 void NotificationRequest::SetLittleIcon(const std::shared_ptr<Media::PixelMap> &littleIcon)
288 {
289     littleIcon_ = littleIcon;
290 }
291 
GetLittleIcon() const292 const std::shared_ptr<Media::PixelMap> NotificationRequest::GetLittleIcon() const
293 {
294     return littleIcon_;
295 }
296 
SetBigIcon(const std::shared_ptr<Media::PixelMap> & bigIcon)297 void NotificationRequest::SetBigIcon(const std::shared_ptr<Media::PixelMap> &bigIcon)
298 {
299     bigIcon_ = bigIcon;
300 }
301 
GetBigIcon() const302 const std::shared_ptr<Media::PixelMap> NotificationRequest::GetBigIcon() const
303 {
304     return bigIcon_;
305 }
306 
SetOverlayIcon(const std::shared_ptr<Media::PixelMap> & overlayIcon)307 void NotificationRequest::SetOverlayIcon(const std::shared_ptr<Media::PixelMap> &overlayIcon)
308 {
309     overlayIcon_ = overlayIcon;
310 }
311 
GetOverlayIcon() const312 const std::shared_ptr<Media::PixelMap> NotificationRequest::GetOverlayIcon() const
313 {
314     return overlayIcon_;
315 }
316 
SetClassification(const std::string & classification)317 void NotificationRequest::SetClassification(const std::string &classification)
318 {
319     classification_ = classification;
320 }
321 
GetClassification() const322 std::string NotificationRequest::GetClassification() const
323 {
324     return classification_;
325 }
326 
SetColor(uint32_t color)327 void NotificationRequest::SetColor(uint32_t color)
328 {
329     color_ = color;
330     if (NotificationRequest::COLOR_DEFAULT != color_) {
331         color_ = color_ | NotificationRequest::COLOR_MASK;
332     }
333 }
334 
GetColor() const335 uint32_t NotificationRequest::GetColor() const
336 {
337     return color_;
338 }
339 
IsColorEnabled() const340 bool NotificationRequest::IsColorEnabled() const
341 {
342     if (!colorEnabled_) {
343         return false;
344     }
345 
346     // no valid content
347     if (!notificationContent_) {
348         ANS_LOGI("no valid notification content");
349         return false;
350     }
351 
352     // not a media content
353     if (NotificationContent::Type::MEDIA != notificationContentType_) {
354         ANS_LOGI("not a media notification content");
355         return false;
356     }
357 
358     auto basicContent = notificationContent_->GetNotificationContent();
359     auto mediaContent = std::static_pointer_cast<NotificationMediaContent>(basicContent);
360     if (!mediaContent->GetAVToken()) {
361         ANS_LOGI("AVToken has not been attached");
362         return false;
363     }
364 
365     return true;
366 }
367 
SetColorEnabled(bool colorEnabled)368 void NotificationRequest::SetColorEnabled(bool colorEnabled)
369 {
370     colorEnabled_ = colorEnabled;
371 }
372 
SetContent(const std::shared_ptr<NotificationContent> & content)373 void NotificationRequest::SetContent(const std::shared_ptr<NotificationContent> &content)
374 {
375     notificationContent_ = content;
376 
377     if (notificationContent_) {
378         notificationContentType_ = notificationContent_->GetContentType();
379         return;
380     }
381 
382     notificationContentType_ = NotificationContent::Type::NONE;
383 }
384 
GetContent() const385 const std::shared_ptr<NotificationContent> NotificationRequest::GetContent() const
386 {
387     return notificationContent_;
388 }
389 
GetNotificationType() const390 NotificationContent::Type NotificationRequest::GetNotificationType() const
391 {
392     return notificationContentType_;
393 }
394 
IsCountdownTimer() const395 bool NotificationRequest::IsCountdownTimer() const
396 {
397     return isCountdown_;
398 }
399 
SetCountdownTimer(bool isCountDown)400 void NotificationRequest::SetCountdownTimer(bool isCountDown)
401 {
402     isCountdown_ = isCountDown;
403 }
404 
SetGroupAlertType(NotificationRequest::GroupAlertType type)405 void NotificationRequest::SetGroupAlertType(NotificationRequest::GroupAlertType type)
406 {
407     groupAlertType_ = type;
408 }
409 
GetGroupAlertType() const410 NotificationRequest::GroupAlertType NotificationRequest::GetGroupAlertType() const
411 {
412     return groupAlertType_;
413 }
414 
IsGroupOverview() const415 bool NotificationRequest::IsGroupOverview() const
416 {
417     return groupOverview_;
418 }
419 
SetGroupOverview(bool overView)420 void NotificationRequest::SetGroupOverview(bool overView)
421 {
422     groupOverview_ = overView;
423 }
424 
SetGroupName(const std::string & groupName)425 void NotificationRequest::SetGroupName(const std::string &groupName)
426 {
427     groupName_ = groupName;
428 }
429 
GetGroupName() const430 std::string NotificationRequest::GetGroupName() const
431 {
432     return groupName_;
433 }
434 
IsOnlyLocal() const435 bool NotificationRequest::IsOnlyLocal() const
436 {
437     return onlyLocal_;
438 }
439 
SetOnlyLocal(bool flag)440 void NotificationRequest::SetOnlyLocal(bool flag)
441 {
442     onlyLocal_ = flag;
443 }
444 
SetSettingsText(const std::string & text)445 void NotificationRequest::SetSettingsText(const std::string &text)
446 {
447     if ((NotificationContent::Type::LONG_TEXT == notificationContentType_) ||
448         (NotificationContent::Type::PICTURE == notificationContentType_)) {
449         ANS_LOGW("This method is invalid if the notification content type has been set to LONG_TEXT or PICTURE.");
450         return;
451     }
452 
453     settingsText_ = text;
454 }
455 
GetSettingsText() const456 std::string NotificationRequest::GetSettingsText() const
457 {
458     return settingsText_;
459 }
460 
GetCreateTime() const461 int64_t NotificationRequest::GetCreateTime() const
462 {
463     return createTime_;
464 }
465 
IsShowStopwatch() const466 bool NotificationRequest::IsShowStopwatch() const
467 {
468     return showStopwatch_;
469 }
470 
SetShowStopwatch(bool isShow)471 void NotificationRequest::SetShowStopwatch(bool isShow)
472 {
473     showStopwatch_ = isShow;
474 }
475 
SetSlotType(NotificationConstant::SlotType slotType)476 void NotificationRequest::SetSlotType(NotificationConstant::SlotType slotType)
477 {
478     slotType_ = slotType;
479 }
480 
GetSlotType() const481 NotificationConstant::SlotType NotificationRequest::GetSlotType() const
482 {
483     return slotType_;
484 }
485 
SetSortingKey(const std::string & key)486 void NotificationRequest::SetSortingKey(const std::string &key)
487 {
488     sortingKey_ = key;
489 }
490 
GetSortingKey() const491 std::string NotificationRequest::GetSortingKey() const
492 {
493     return sortingKey_;
494 }
495 
SetStatusBarText(const std::string & text)496 void NotificationRequest::SetStatusBarText(const std::string &text)
497 {
498     statusBarText_ = text;
499 }
500 
GetStatusBarText() const501 std::string NotificationRequest::GetStatusBarText() const
502 {
503     return statusBarText_;
504 }
505 
IsTapDismissed() const506 bool NotificationRequest::IsTapDismissed() const
507 {
508     return tapDismissed_;
509 }
510 
SetTapDismissed(bool isDismissed)511 void NotificationRequest::SetTapDismissed(bool isDismissed)
512 {
513     tapDismissed_ = isDismissed;
514 }
515 
SetVisibleness(NotificationConstant::VisiblenessType type)516 void NotificationRequest::SetVisibleness(NotificationConstant::VisiblenessType type)
517 {
518     visiblenessType_ = type;
519 }
520 
GetVisibleness() const521 NotificationConstant::VisiblenessType NotificationRequest::GetVisibleness() const
522 {
523     return visiblenessType_;
524 }
525 
SetBadgeIconStyle(NotificationRequest::BadgeStyle style)526 void NotificationRequest::SetBadgeIconStyle(NotificationRequest::BadgeStyle style)
527 {
528     badgeStyle_ = style;
529 }
530 
GetBadgeIconStyle() const531 NotificationRequest::BadgeStyle NotificationRequest::GetBadgeIconStyle() const
532 {
533     return badgeStyle_;
534 }
535 
SetShortcutId(const std::string & shortcutId)536 void NotificationRequest::SetShortcutId(const std::string &shortcutId)
537 {
538     shortcutId_ = shortcutId;
539 }
540 
GetShortcutId() const541 std::string NotificationRequest::GetShortcutId() const
542 {
543     return shortcutId_;
544 }
545 
SetFloatingIcon(bool floatingIcon)546 void NotificationRequest::SetFloatingIcon(bool floatingIcon)
547 {
548     floatingIcon_ = floatingIcon;
549 }
550 
IsFloatingIcon() const551 bool NotificationRequest::IsFloatingIcon() const
552 {
553     return floatingIcon_;
554 }
555 
SetProgressBar(int32_t progress,int32_t progressMax,bool indeterminate)556 void NotificationRequest::SetProgressBar(int32_t progress, int32_t progressMax, bool indeterminate)
557 {
558     progressValue_ = progress;
559     progressMax_ = progressMax;
560     progressIndeterminate_ = indeterminate;
561 }
562 
GetProgressMax() const563 int32_t NotificationRequest::GetProgressMax() const
564 {
565     return progressMax_;
566 }
567 
GetProgressValue() const568 int32_t NotificationRequest::GetProgressValue() const
569 {
570     return progressValue_;
571 }
572 
IsProgressIndeterminate() const573 bool NotificationRequest::IsProgressIndeterminate() const
574 {
575     return progressIndeterminate_;
576 }
577 
SetNotificationUserInputHistory(const std::vector<std::string> & text)578 void NotificationRequest::SetNotificationUserInputHistory(const std::vector<std::string> &text)
579 {
580     if (text.empty()) {
581         userInputHistory_.clear();
582         return;
583     }
584 
585     auto vsize = std::min(NotificationRequest::MAX_USER_INPUT_HISTORY, text.size());
586     userInputHistory_.assign(text.begin(), text.begin() + vsize);
587 }
588 
GetNotificationUserInputHistory() const589 std::vector<std::string> NotificationRequest::GetNotificationUserInputHistory() const
590 {
591     return userInputHistory_;
592 }
593 
GetNotificationHashCode() const594 std::string NotificationRequest::GetNotificationHashCode() const
595 {
596     if (creatorBundleName_.empty() || (creatorUid_ == 0) || ownerBundleName_.empty()) {
597         return "";
598     }
599 
600     return std::to_string(notificationId_) + "_" + creatorBundleName_ + "_" + std::to_string(creatorUid_) + "_" +
601            ownerBundleName_;
602 }
603 
SetOwnerBundleName(const std::string & ownerName)604 void NotificationRequest::SetOwnerBundleName(const std::string &ownerName)
605 {
606     ownerBundleName_ = ownerName;
607 }
608 
GetOwnerBundleName() const609 std::string NotificationRequest::GetOwnerBundleName() const
610 {
611     return ownerBundleName_;
612 }
613 
SetCreatorBundleName(const std::string & creatorName)614 void NotificationRequest::SetCreatorBundleName(const std::string &creatorName)
615 {
616     creatorBundleName_ = creatorName;
617 }
618 
GetCreatorBundleName() const619 std::string NotificationRequest::GetCreatorBundleName() const
620 {
621     return creatorBundleName_;
622 }
623 
SetCreatorPid(pid_t pid)624 void NotificationRequest::SetCreatorPid(pid_t pid)
625 {
626     creatorPid_ = pid;
627 }
628 
GetCreatorPid() const629 pid_t NotificationRequest::GetCreatorPid() const
630 {
631     return creatorPid_;
632 }
633 
SetCreatorUid(int32_t uid)634 void NotificationRequest::SetCreatorUid(int32_t uid)
635 {
636     creatorUid_ = uid;
637 }
638 
GetCreatorUid() const639 int32_t NotificationRequest::GetCreatorUid() const
640 {
641     return creatorUid_;
642 }
643 
SetOwnerUid(int32_t uid)644 void NotificationRequest::SetOwnerUid(int32_t uid)
645 {
646     ownerUid_ = uid;
647 }
648 
GetOwnerUid() const649 int32_t NotificationRequest::GetOwnerUid() const
650 {
651     return ownerUid_;
652 }
653 
SetLabel(const std::string & label)654 void NotificationRequest::SetLabel(const std::string &label)
655 {
656     label_ = label;
657 }
658 
GetLabel() const659 std::string NotificationRequest::GetLabel() const
660 {
661     return label_;
662 }
663 
SetDistributed(bool distribute)664 void NotificationRequest::SetDistributed(bool distribute)
665 {
666     distributedOptions_.SetDistributed(distribute);
667 }
668 
SetDevicesSupportDisplay(const std::vector<std::string> & devices)669 void NotificationRequest::SetDevicesSupportDisplay(const std::vector<std::string> &devices)
670 {
671     distributedOptions_.SetDevicesSupportDisplay(devices);
672 }
673 
SetDevicesSupportOperate(const std::vector<std::string> & devices)674 void NotificationRequest::SetDevicesSupportOperate(const std::vector<std::string> &devices)
675 {
676     distributedOptions_.SetDevicesSupportOperate(devices);
677 }
678 
GetNotificationDistributedOptions() const679 NotificationDistributedOptions NotificationRequest::GetNotificationDistributedOptions() const
680 {
681     return distributedOptions_;
682 }
683 
SetCreatorUserId(int32_t userId)684 void NotificationRequest::SetCreatorUserId(int32_t userId)
685 {
686     creatorUserId_ = userId;
687 }
688 
GetCreatorUserId() const689 int32_t NotificationRequest::GetCreatorUserId() const
690 {
691     return creatorUserId_;
692 }
693 
SetOwnerUserId(int32_t userId)694 void NotificationRequest::SetOwnerUserId(int32_t userId)
695 {
696     ownerUserId_ = userId;
697 }
698 
GetOwnerUserId() const699 int32_t NotificationRequest::GetOwnerUserId() const
700 {
701     return ownerUserId_;
702 }
703 
Dump()704 std::string NotificationRequest::Dump()
705 {
706     return "NotificationRequest{ "
707             "notificationId = " + std::to_string(notificationId_) +
708             ", slotType = " + std::to_string(static_cast<int32_t>(slotType_)) +
709             ", createTime = " + std::to_string(createTime_) + ", deliveryTime = " + std::to_string(deliveryTime_) +
710             ", autoDeletedTime = " + std::to_string(autoDeletedTime_) + ", settingsText = " + settingsText_ +
711             ", creatorBundleName = " + creatorBundleName_ +
712             ", creatorPid = " + std::to_string(static_cast<int32_t>(creatorPid_)) +
713             ", creatorUid = " + std::to_string(static_cast<int32_t>(creatorUid_)) +
714             ", ownerBundleName = " + ownerBundleName_ +
715             ", ownerUid = " + std::to_string(static_cast<int32_t>(ownerUid_)) +
716             ", groupName = " + groupName_ + ", statusBarText = " + statusBarText_ + ", label = " + label_ +
717             ", shortcutId = " + shortcutId_ + ", sortingKey = " + sortingKey_ +
718             ", groupAlertType = " + std::to_string(static_cast<int32_t>(groupAlertType_)) +
719             ", color = " + std::to_string(color_) + ", badgeNumber = " + std::to_string(badgeNumber_) +
720             ", visiblenessType = " + std::to_string(static_cast<int32_t>(visiblenessType_)) +
721             ", progressValue = " + std::to_string(progressValue_) + ", progressMax = " + std::to_string(progressMax_) +
722             ", badgeStyle = " + std::to_string(static_cast<int32_t>(badgeStyle_)) +
723             ", classification = " + classification_ +
724             ", notificationContentType = " + std::to_string(static_cast<int32_t>(notificationContentType_)) +
725             ", showDeliveryTime = " + (showDeliveryTime_ ? "true" : "false") +
726             ", tapDismissed = " + (tapDismissed_ ? "true" : "false") +
727             ", colorEnabled = " + (colorEnabled_ ? "true" : "false") +
728             ", alertOneTime = " + (alertOneTime_ ? "true" : "false") +
729             ", showStopwatch = " + (showStopwatch_ ? "true" : "false") +
730             ", isCountdown = " + (isCountdown_ ? "true" : "false") +
731             ", inProgress = " + (inProgress_ ? "true" : "false") +
732             ", groupOverview = " + (groupOverview_ ? "true" : "false") +
733             ", isRemoveAllowed = " + (isRemoveAllowed_ ? "true" : "false") +
734             ", progressIndeterminate = " + (progressIndeterminate_ ? "true" : "false") +
735             ", unremovable = " + (unremovable_ ? "true" : "false") +
736             ", floatingIcon = " + (floatingIcon_ ? "true" : "false") +
737             ", onlyLocal = " + (onlyLocal_ ? "true" : "false") + ", permitted = " + (permitted_ ? "true" : "false") +
738             ", isAgent = " + (isAgent_ ? "true" : "false") +
739             ", removalWantAgent = " + (removalWantAgent_ ? "not null" : "null") +
740             ", maxScreenWantAgent = " + (maxScreenWantAgent_ ? "not null" : "null") +
741             ", additionalParams = " + (additionalParams_ ? "not null" : "null") +
742             ", littleIcon = " + (littleIcon_ ? "not null" : "null") +
743             ", bigIcon = " + (bigIcon_ ? "not null" : "null") +
744             ", overlayIcon = " + (overlayIcon_ ? "not null" : "null") +
745             ", notificationContent = " + (notificationContent_ ? notificationContent_->Dump() : "null") +
746             ", notificationTemplate = " + (notificationTemplate_ ? "not null" : "null") +
747             ", actionButtons = " + (!actionButtons_.empty() ? actionButtons_.at(0)->Dump() : "empty") +
748             ", messageUsers = " + (!messageUsers_.empty() ? messageUsers_.at(0)->Dump() : "empty") +
749             ", userInputHistory = " + (!userInputHistory_.empty() ? userInputHistory_.at(0) : "empty") +
750             ", distributedOptions = " + distributedOptions_.Dump() +
751             ", notificationFlags = " + (notificationFlags_ ? "not null" : "null") +
752             ", creatorUserId = " + std::to_string(creatorUserId_) + ", ownerUserId = " + std::to_string(ownerUserId_) +
753             ", receiverUserId = " + std::to_string(receiverUserId_) + ", updateDeadLine = " +
754             std::to_string(updateDeadLine_) + ", finishDeadLine = " + std::to_string(finishDeadLine_) + " }";
755 }
756 
ToJson(nlohmann::json & jsonObject) const757 bool NotificationRequest::ToJson(nlohmann::json &jsonObject) const
758 {
759     jsonObject["version"]         = 1;
760 
761     jsonObject["id"]              = notificationId_;
762     jsonObject["color"]           = color_;
763     jsonObject["deliveryTime"]    = deliveryTime_;
764     jsonObject["autoDeletedTime"] = autoDeletedTime_;
765 
766     jsonObject["creatorBundleName"] = creatorBundleName_;
767     jsonObject["ownerBundleName"]   = ownerBundleName_;
768     jsonObject["groupName"]         = groupName_;
769     jsonObject["label"]             = label_;
770     jsonObject["classification"]    = classification_;
771 
772     jsonObject["slotType"]       = static_cast<int32_t>(slotType_);
773     jsonObject["notificationSlotType"] = static_cast<int32_t>(slotType_);
774     jsonObject["badgeIconStyle"] = static_cast<int32_t>(badgeStyle_);
775     jsonObject["notificationContentType"] = static_cast<int32_t>(notificationContentType_);
776 
777     jsonObject["showDeliveryTime"] = showDeliveryTime_;
778     jsonObject["tapDismissed"]     = tapDismissed_;
779     jsonObject["colorEnabled"]     = colorEnabled_;
780     jsonObject["isOngoing"]        = inProgress_;
781     jsonObject["isAlertOnce"]      = alertOneTime_;
782     jsonObject["isStopwatch"]      = showStopwatch_;
783     jsonObject["isCountdown"]      = isCountdown_;
784     jsonObject["isUnremovable"]    = unremovable_;
785     jsonObject["isAgent"]          = isAgent_;
786     jsonObject["isFloatingIcon"]   = floatingIcon_;
787 
788     jsonObject["creatorBundleName"] = creatorBundleName_;
789     jsonObject["creatorUid"]        = creatorUid_;
790     jsonObject["creatorPid"]        = creatorPid_;
791     jsonObject["creatorUserId"]     = creatorUserId_;
792     jsonObject["ownerUserId"]       = ownerUserId_;
793     jsonObject["ownerUid"]          = ownerUid_;
794     jsonObject["receiverUserId"]    = receiverUserId_;
795     jsonObject["updateDeadLine"]     = updateDeadLine_;
796     jsonObject["finishDeadLine"]     = finishDeadLine_;
797 
798     if (!ConvertObjectsToJson(jsonObject)) {
799         ANS_LOGE("Cannot convert objects to JSON");
800         return false;
801     }
802 
803     return true;
804 }
805 
FromJson(const nlohmann::json & jsonObject)806 NotificationRequest *NotificationRequest::FromJson(const nlohmann::json &jsonObject)
807 {
808     if (jsonObject.is_null() or !jsonObject.is_object()) {
809         ANS_LOGE("Invalid JSON object");
810         return nullptr;
811     }
812 
813     auto pRequest = new (std::nothrow) NotificationRequest();
814     if (pRequest == nullptr) {
815         ANS_LOGE("Failed to create request instance");
816         return nullptr;
817     }
818 
819     const auto &jsonEnd = jsonObject.cend();
820     if (jsonObject.find("version") != jsonEnd && jsonObject.at("version").is_number_integer()) {
821         jsonObject.at("version").get<int32_t>();
822     }
823 
824     ConvertJsonToNum(pRequest, jsonObject);
825 
826     ConvertJsonToString(pRequest, jsonObject);
827 
828     ConvertJsonToEnum(pRequest, jsonObject);
829 
830     ConvertJsonToBool(pRequest, jsonObject);
831 
832     if (jsonObject.find("wantAgent") != jsonEnd && jsonObject.at("wantAgent").is_string()) {
833         auto wantAgentValue  = jsonObject.at("wantAgent").get<std::string>();
834         pRequest->wantAgent_ = AbilityRuntime::WantAgent::WantAgentHelper::FromString(wantAgentValue);
835     }
836 
837     if (!ConvertJsonToNotificationContent(pRequest, jsonObject)) {
838         delete pRequest;
839         pRequest = nullptr;
840         return nullptr;
841     }
842 
843     if (!ConvertJsonToNotificationActionButton(pRequest, jsonObject)) {
844         delete pRequest;
845         pRequest = nullptr;
846         return nullptr;
847     }
848 
849     if (jsonObject.find("extraInfo") != jsonEnd && jsonObject.at("extraInfo").is_string()) {
850         auto extraInfoStr = jsonObject.at("extraInfo").get<std::string>();
851         if (!extraInfoStr.empty()) {
852             AAFwk::WantParams params    = AAFwk::WantParamWrapper::ParseWantParams(extraInfoStr);
853             pRequest->additionalParams_ = std::make_shared<AAFwk::WantParams>(params);
854         }
855     }
856 
857     ConvertJsonToPixelMap(pRequest, jsonObject);
858 
859     if (!ConvertJsonToNotificationDistributedOptions(pRequest, jsonObject)) {
860         delete pRequest;
861         pRequest = nullptr;
862         return nullptr;
863     }
864 
865     if (!ConvertJsonToNotificationFlags(pRequest, jsonObject)) {
866         delete pRequest;
867         pRequest = nullptr;
868         return nullptr;
869     }
870 
871     return pRequest;
872 }
873 
Marshalling(Parcel & parcel) const874 bool NotificationRequest::Marshalling(Parcel &parcel) const
875 {
876     // write int
877     if (!parcel.WriteInt32(notificationId_)) {
878         ANS_LOGE("Failed to write notification Id");
879         return false;
880     }
881 
882     if (!parcel.WriteUint32(color_)) {
883         ANS_LOGE("Failed to write color");
884         return false;
885     }
886 
887     if (!parcel.WriteUint32(badgeNumber_)) {
888         ANS_LOGE("Failed to write badge number");
889         return false;
890     }
891 
892     if (!parcel.WriteInt32(progressValue_)) {
893         ANS_LOGE("Failed to write progress value");
894         return false;
895     }
896 
897     if (!parcel.WriteInt32(progressMax_)) {
898         ANS_LOGE("Failed to write progress max");
899         return false;
900     }
901 
902     if (!parcel.WriteInt64(createTime_)) {
903         ANS_LOGE("Failed to write create time");
904         return false;
905     }
906 
907     if (!parcel.WriteInt64(deliveryTime_)) {
908         ANS_LOGE("Failed to write delivery time");
909         return false;
910     }
911 
912     if (!parcel.WriteInt64(autoDeletedTime_)) {
913         ANS_LOGE("Failed to write auto deleted time");
914         return false;
915     }
916 
917     if (!parcel.WriteInt32(static_cast<int32_t>(creatorPid_))) {
918         ANS_LOGE("Failed to write creator pid");
919         return false;
920     }
921 
922     if (!parcel.WriteInt32(static_cast<int32_t>(creatorUid_))) {
923         ANS_LOGE("Failed to write creator uid");
924         return false;
925     }
926 
927     if (!parcel.WriteInt32(static_cast<int32_t>(ownerUid_))) {
928         ANS_LOGE("Failed to write owner uid");
929         return false;
930     }
931 
932     if (!parcel.WriteInt32(static_cast<int32_t>(creatorUserId_))) {
933         ANS_LOGE("Failed to write creator userId");
934         return false;
935     }
936 
937     if (!parcel.WriteInt32(static_cast<int32_t>(ownerUserId_))) {
938         ANS_LOGE("Failed to write owner userId");
939         return false;
940     }
941 
942     if (!parcel.WriteInt32(static_cast<int32_t>(receiverUserId_))) {
943         ANS_LOGE("Failed to write receiver userId");
944         return false;
945     }
946 
947     // write std::string
948     if (!parcel.WriteString(settingsText_)) {
949         ANS_LOGE("Failed to write settings text");
950         return false;
951     }
952 
953     if (!parcel.WriteString(creatorBundleName_)) {
954         ANS_LOGE("Failed to write creator bundle name");
955         return false;
956     }
957 
958     if (!parcel.WriteString(ownerBundleName_)) {
959         ANS_LOGE("Failed to write owner bundle name");
960         return false;
961     }
962 
963     if (!parcel.WriteString(groupName_)) {
964         ANS_LOGE("Failed to write group name");
965         return false;
966     }
967 
968     if (!parcel.WriteString(statusBarText_)) {
969         ANS_LOGE("Failed to write status bar text");
970         return false;
971     }
972 
973     if (!parcel.WriteString(label_)) {
974         ANS_LOGE("Failed to write label");
975         return false;
976     }
977 
978     if (!parcel.WriteString(shortcutId_)) {
979         ANS_LOGE("Failed to write shortcut Id");
980         return false;
981     }
982 
983     if (!parcel.WriteString(sortingKey_)) {
984         ANS_LOGE("Failed to write sorting key");
985         return false;
986     }
987 
988     if (!parcel.WriteString(classification_)) {
989         ANS_LOGE("Failed to write classification");
990         return false;
991     }
992 
993     // write enum
994     if (!parcel.WriteInt32(static_cast<int32_t>(slotType_))) {
995         ANS_LOGE("Failed to write slot type");
996         return false;
997     }
998 
999     if (!parcel.WriteInt32(static_cast<int32_t>(groupAlertType_))) {
1000         ANS_LOGE("Failed to write group alert type");
1001         return false;
1002     }
1003 
1004     if (!parcel.WriteInt32(static_cast<int32_t>(visiblenessType_))) {
1005         ANS_LOGE("Failed to write visibleness type");
1006         return false;
1007     }
1008 
1009     if (!parcel.WriteInt32(static_cast<int32_t>(badgeStyle_))) {
1010         ANS_LOGE("Failed to write badge type");
1011         return false;
1012     }
1013 
1014     if (!parcel.WriteInt32(static_cast<int32_t>(notificationContentType_))) {
1015         ANS_LOGE("Failed to write notification content type");
1016         return false;
1017     }
1018 
1019     // write bool
1020     if (!parcel.WriteBool(showDeliveryTime_)) {
1021         ANS_LOGE("Failed to write flag indicating whether to show delivery time");
1022         return false;
1023     }
1024 
1025     if (!parcel.WriteBool(tapDismissed_)) {
1026         ANS_LOGE("Failed to write flag tap dismissed");
1027         return false;
1028     }
1029 
1030     if (!parcel.WriteBool(colorEnabled_)) {
1031         ANS_LOGE("Failed to write flag indicating whether to enable background color");
1032         return false;
1033     }
1034 
1035     if (!parcel.WriteBool(alertOneTime_)) {
1036         ANS_LOGE("Failed to write flag indicating whether to have this notification alert only once");
1037         return false;
1038     }
1039 
1040     if (!parcel.WriteBool(showStopwatch_)) {
1041         ANS_LOGE("Failed to write flag show stop watch");
1042         return false;
1043     }
1044 
1045     if (!parcel.WriteBool(isCountdown_)) {
1046         ANS_LOGE("Failed to write flag indicating whether to show the notification creation time as a countdown timer");
1047         return false;
1048     }
1049 
1050     if (!parcel.WriteBool(inProgress_)) {
1051         ANS_LOGE("Failed to write flag indicating whether in progress");
1052         return false;
1053     }
1054 
1055     if (!parcel.WriteBool(groupOverview_)) {
1056         ANS_LOGE("Failed to write flag indicating whether to use this notification as the overview of its group");
1057         return false;
1058     }
1059 
1060     if (!parcel.WriteBool(progressIndeterminate_)) {
1061         ANS_LOGE("Failed to write progress indeterminate");
1062         return false;
1063     }
1064 
1065     if (!parcel.WriteBool(unremovable_)) {
1066         ANS_LOGE("Failed to write flag indicating whether unremovable");
1067         return false;
1068     }
1069 
1070     if (!parcel.WriteBool(floatingIcon_)) {
1071         ANS_LOGE("Failed to write flag floating icon");
1072         return false;
1073     }
1074 
1075     if (!parcel.WriteBool(onlyLocal_)) {
1076         ANS_LOGE("Failed to write flag only local");
1077         return false;
1078     }
1079 
1080     if (!parcel.WriteBool(permitted_)) {
1081         ANS_LOGE("Failed to write flag indicating whether to allow the platform to \
1082             generate contextual NotificationActionButton objects");
1083         return false;
1084     }
1085 
1086     if (!parcel.WriteBool(isAgent_)) {
1087         ANS_LOGE("Failed to write flag indicating whether an agent notification");
1088         return false;
1089     }
1090 
1091     if (!parcel.WriteBool(isRemoveAllowed_)) {
1092         ANS_LOGE("Failed to write flag isRemoveAllowed");
1093         return false;
1094     }
1095 
1096     // write objects which managed by std::shared_ptr
1097     bool valid {false};
1098 
1099     valid = wantAgent_ ? true : false;
1100     if (!parcel.WriteBool(valid)) {
1101         ANS_LOGE("Failed to write the flag which indicate whether wantAgent is null");
1102         return false;
1103     }
1104 
1105     if (valid) {
1106         if (!parcel.WriteParcelable(wantAgent_.get())) {
1107             ANS_LOGE("Failed to write wantAgent");
1108             return false;
1109         }
1110     }
1111 
1112     valid = removalWantAgent_ ? true : false;
1113     if (!parcel.WriteBool(valid)) {
1114         ANS_LOGE("Failed to write the flag which indicate whether removalWantAgent is null");
1115         return false;
1116     }
1117 
1118     if (valid) {
1119         if (!parcel.WriteParcelable(removalWantAgent_.get())) {
1120             ANS_LOGE("Failed to write removalWantAgent");
1121             return false;
1122         }
1123     }
1124 
1125     valid = maxScreenWantAgent_ ? true : false;
1126     if (!parcel.WriteBool(valid)) {
1127         ANS_LOGE("Failed to write the flag which indicate whether maxScreenWantAgent is null");
1128         return false;
1129     }
1130 
1131     if (valid) {
1132         if (!parcel.WriteParcelable(maxScreenWantAgent_.get())) {
1133             ANS_LOGE("Failed to write maxScreenWantAgent");
1134             return false;
1135         }
1136     }
1137 
1138     valid = additionalParams_ ? true : false;
1139     if (!parcel.WriteBool(valid)) {
1140         ANS_LOGE("Failed to write the flag which indicate whether additionalParams is null");
1141         return false;
1142     }
1143 
1144     if (valid) {
1145         if (!parcel.WriteParcelable(additionalParams_.get())) {
1146             ANS_LOGE("Failed to write additionalParams");
1147             return false;
1148         }
1149     }
1150 
1151     valid = littleIcon_ ? true : false;
1152     if (!parcel.WriteBool(valid)) {
1153         ANS_LOGE("Failed to write the flag which indicate whether littleIcon is null");
1154         return false;
1155     }
1156 
1157     if (valid) {
1158         if (!parcel.WriteParcelable(littleIcon_.get())) {
1159             ANS_LOGE("Failed to write littleIcon");
1160             return false;
1161         }
1162     }
1163 
1164     valid = bigIcon_ ? true : false;
1165     if (!parcel.WriteBool(valid)) {
1166         ANS_LOGE("Failed to write the flag which indicate whether bigIcon is null");
1167         return false;
1168     }
1169 
1170     if (valid) {
1171         if (!parcel.WriteParcelable(bigIcon_.get())) {
1172             ANS_LOGE("Failed to write bigIcon");
1173             return false;
1174         }
1175     }
1176 
1177     valid = overlayIcon_ ? true : false;
1178     if (!parcel.WriteBool(valid)) {
1179         ANS_LOGE("Failed to write the flag which indicate whether overlayIcon is null");
1180         return false;
1181     }
1182 
1183     if (valid) {
1184         if (!parcel.WriteParcelable(overlayIcon_.get())) {
1185             ANS_LOGE("Failed to write overlayIcon");
1186             return false;
1187         }
1188     }
1189 
1190     valid = notificationContent_ ? true : false;
1191     if (!parcel.WriteBool(valid)) {
1192         ANS_LOGE("Failed to write the flag which indicate whether notificationContent is null");
1193         return false;
1194     }
1195 
1196     if (valid) {
1197         if (!parcel.WriteParcelable(notificationContent_.get())) {
1198             ANS_LOGE("Failed to write notificationContent");
1199             return false;
1200         }
1201     }
1202 
1203     // write std::vector
1204     if (!parcel.WriteUint64(actionButtons_.size())) {
1205         ANS_LOGE("Failed to write the size of actionButtons");
1206         return false;
1207     }
1208 
1209     for (auto it = actionButtons_.begin(); it != actionButtons_.end(); ++it) {
1210         if (!parcel.WriteParcelable(it->get())) {
1211             ANS_LOGE("Failed to write actionButton");
1212             return false;
1213         }
1214     }
1215 
1216     if (!parcel.WriteBool(isCoverActionButtons_)) {
1217         ANS_LOGE("Failed to write isCoverActionButtons_");
1218         return false;
1219     }
1220 
1221     if (!parcel.WriteUint64(messageUsers_.size())) {
1222         ANS_LOGE("Failed to write the size of messageUsers");
1223         return false;
1224     }
1225 
1226     for (auto it = messageUsers_.begin(); it != messageUsers_.end(); ++it) {
1227         if (!parcel.WriteParcelable(it->get())) {
1228             ANS_LOGE("Failed to write messageUser");
1229             return false;
1230         }
1231     }
1232 
1233     if (!parcel.WriteStringVector(userInputHistory_)) {
1234         ANS_LOGE("Failed to write userInputHistory");
1235         return false;
1236     }
1237 
1238     if (!parcel.WriteParcelable(&distributedOptions_)) {
1239         ANS_LOGE("Failed to write distributedOptions");
1240         return false;
1241     }
1242 
1243     valid = notificationTemplate_ ? true : false;
1244     if (!parcel.WriteBool(valid)) {
1245         ANS_LOGE("Failed to write the flag which indicate whether publicNotification is null");
1246         return false;
1247     }
1248 
1249     if (valid) {
1250         if (!parcel.WriteParcelable(notificationTemplate_.get())) {
1251             ANS_LOGE("Failed to write notificationTemplate");
1252             return false;
1253         }
1254     }
1255 
1256     valid = notificationFlags_ ? true : false;
1257     if (!parcel.WriteBool(valid)) {
1258         ANS_LOGE("Failed to write flags for the notification");
1259         return false;
1260     }
1261 
1262     if (valid) {
1263         if (!parcel.WriteParcelable(notificationFlags_.get())) {
1264             ANS_LOGE("Failed to write notification flags");
1265             return false;
1266         }
1267     }
1268 
1269     if (!parcel.WriteInt64(updateDeadLine_)) {
1270         ANS_LOGE("Failed to write max update time");
1271         return false;
1272     }
1273 
1274     if (!parcel.WriteInt64(finishDeadLine_)) {
1275         ANS_LOGE("Failed to write max finish time");
1276         return false;
1277     }
1278 
1279     return true;
1280 }
1281 
Unmarshalling(Parcel & parcel)1282 NotificationRequest *NotificationRequest::Unmarshalling(Parcel &parcel)
1283 {
1284     auto objptr = new (std::nothrow) NotificationRequest();
1285     if ((objptr != nullptr) && !objptr->ReadFromParcel(parcel)) {
1286         delete objptr;
1287         objptr = nullptr;
1288     }
1289 
1290     return objptr;
1291 }
1292 
ReadFromParcel(Parcel & parcel)1293 bool NotificationRequest::ReadFromParcel(Parcel &parcel)
1294 {
1295     notificationId_ = parcel.ReadInt32();
1296     color_ = parcel.ReadUint32();
1297     badgeNumber_ = parcel.ReadUint32();
1298     progressValue_ = parcel.ReadInt32();
1299     progressMax_ = parcel.ReadInt32();
1300     createTime_ = parcel.ReadInt64();
1301     deliveryTime_ = parcel.ReadInt64();
1302     autoDeletedTime_ = parcel.ReadInt64();
1303 
1304     creatorPid_ = static_cast<pid_t>(parcel.ReadInt32());
1305     creatorUid_ = parcel.ReadInt32();
1306     ownerUid_ = parcel.ReadInt32();
1307     creatorUserId_ = parcel.ReadInt32();
1308     ownerUserId_ = parcel.ReadInt32();
1309     receiverUserId_ = parcel.ReadInt32();
1310 
1311     if (!parcel.ReadString(settingsText_)) {
1312         ANS_LOGE("Failed to read settings text");
1313         return false;
1314     }
1315 
1316     if (!parcel.ReadString(creatorBundleName_)) {
1317         ANS_LOGE("Failed to read creator bundle name");
1318         return false;
1319     }
1320 
1321     if (!parcel.ReadString(ownerBundleName_)) {
1322         ANS_LOGE("Failed to read owner bundle name");
1323         return false;
1324     }
1325 
1326     if (!parcel.ReadString(groupName_)) {
1327         ANS_LOGE("Failed to read group name");
1328         return false;
1329     }
1330 
1331     if (!parcel.ReadString(statusBarText_)) {
1332         ANS_LOGE("Failed to read status bar text");
1333         return false;
1334     }
1335 
1336     if (!parcel.ReadString(label_)) {
1337         ANS_LOGE("Failed to read label");
1338         return false;
1339     }
1340 
1341     if (!parcel.ReadString(shortcutId_)) {
1342         ANS_LOGE("Failed to read shortcut Id");
1343         return false;
1344     }
1345 
1346     if (!parcel.ReadString(sortingKey_)) {
1347         ANS_LOGE("Failed to read sorting key");
1348         return false;
1349     }
1350 
1351     if (!parcel.ReadString(classification_)) {
1352         ANS_LOGE("Failed to read classification");
1353         return false;
1354     }
1355 
1356     slotType_ = static_cast<NotificationConstant::SlotType>(parcel.ReadInt32());
1357     groupAlertType_ = static_cast<NotificationRequest::GroupAlertType>(parcel.ReadInt32());
1358     visiblenessType_ = static_cast<NotificationConstant::VisiblenessType>(parcel.ReadInt32());
1359     badgeStyle_ = static_cast<NotificationRequest::BadgeStyle>(parcel.ReadInt32());
1360     notificationContentType_ = static_cast<NotificationContent::Type>(parcel.ReadInt32());
1361 
1362     showDeliveryTime_ = parcel.ReadBool();
1363     tapDismissed_ = parcel.ReadBool();
1364     colorEnabled_ = parcel.ReadBool();
1365     alertOneTime_ = parcel.ReadBool();
1366     showStopwatch_ = parcel.ReadBool();
1367     isCountdown_ = parcel.ReadBool();
1368     inProgress_ = parcel.ReadBool();
1369     groupOverview_ = parcel.ReadBool();
1370     progressIndeterminate_ = parcel.ReadBool();
1371     unremovable_ = parcel.ReadBool();
1372     floatingIcon_ = parcel.ReadBool();
1373     onlyLocal_ = parcel.ReadBool();
1374     permitted_ = parcel.ReadBool();
1375     isAgent_ = parcel.ReadBool();
1376     isRemoveAllowed_ = parcel.ReadBool();
1377 
1378     bool valid {false};
1379 
1380     valid = parcel.ReadBool();
1381     if (valid) {
1382         wantAgent_ = std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>(
1383             parcel.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>());
1384         if (!wantAgent_) {
1385             ANS_LOGE("Failed to read wantAgent");
1386             return false;
1387         }
1388     }
1389 
1390     valid = parcel.ReadBool();
1391     if (valid) {
1392         removalWantAgent_ = std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>(
1393             parcel.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>());
1394         if (!removalWantAgent_) {
1395             ANS_LOGE("Failed to read removalWantAgent");
1396             return false;
1397         }
1398     }
1399 
1400     valid = parcel.ReadBool();
1401     if (valid) {
1402         maxScreenWantAgent_ = std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>(
1403             parcel.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>());
1404         if (!maxScreenWantAgent_) {
1405             ANS_LOGE("Failed to read maxScreenWantAgent");
1406             return false;
1407         }
1408     }
1409 
1410     valid = parcel.ReadBool();
1411     if (valid) {
1412         additionalParams_ = std::shared_ptr<AAFwk::WantParams>(parcel.ReadParcelable<AAFwk::WantParams>());
1413         if (!additionalParams_) {
1414             ANS_LOGE("Failed to read additionalParams");
1415             return false;
1416         }
1417     }
1418 
1419     valid = parcel.ReadBool();
1420     if (valid) {
1421         littleIcon_ = std::shared_ptr<Media::PixelMap>(parcel.ReadParcelable<Media::PixelMap>());
1422         if (!littleIcon_) {
1423             ANS_LOGE("Failed to read littleIcon");
1424             return false;
1425         }
1426     }
1427 
1428     valid = parcel.ReadBool();
1429     if (valid) {
1430         bigIcon_ = std::shared_ptr<Media::PixelMap>(parcel.ReadParcelable<Media::PixelMap>());
1431         if (!bigIcon_) {
1432             ANS_LOGE("Failed to read bigIcon");
1433             return false;
1434         }
1435     }
1436 
1437     valid = parcel.ReadBool();
1438     if (valid) {
1439         overlayIcon_ = std::shared_ptr<Media::PixelMap>(parcel.ReadParcelable<Media::PixelMap>());
1440         if (!overlayIcon_) {
1441             ANS_LOGE("Failed to read overlayIcon");
1442             return false;
1443         }
1444     }
1445 
1446     valid = parcel.ReadBool();
1447     if (valid) {
1448         notificationContent_ = std::shared_ptr<NotificationContent>(parcel.ReadParcelable<NotificationContent>());
1449         if (!notificationContent_) {
1450             ANS_LOGE("Failed to read notificationContent");
1451             return false;
1452         }
1453     }
1454 
1455     auto vsize = parcel.ReadUint64();
1456     vsize = (vsize < NotificationRequest::MAX_ACTION_BUTTONS) ? vsize : NotificationRequest::MAX_ACTION_BUTTONS;
1457     for (uint64_t it = 0; it < vsize; ++it) {
1458         auto member = parcel.ReadParcelable<NotificationActionButton>();
1459         if (member == nullptr) {
1460             actionButtons_.clear();
1461             ANS_LOGE("Failed to read actionButton");
1462             return false;
1463         }
1464 
1465         actionButtons_.emplace_back(member);
1466     }
1467 
1468     isCoverActionButtons_ = parcel.ReadBool();
1469 
1470     vsize = parcel.ReadUint64();
1471     vsize = (vsize < NotificationRequest::MAX_MESSAGE_USERS) ? vsize : NotificationRequest::MAX_MESSAGE_USERS;
1472     for (uint64_t it = 0; it < vsize; ++it) {
1473         auto member = std::shared_ptr<MessageUser>(parcel.ReadParcelable<MessageUser>());
1474         if (member == nullptr) {
1475             ANS_LOGE("Failed to read messageUser");
1476             messageUsers_.clear();
1477             return false;
1478         }
1479 
1480         messageUsers_.emplace_back(member);
1481     }
1482 
1483     if (!parcel.ReadStringVector(&userInputHistory_)) {
1484         ANS_LOGE("Failed to read userInputHistory");
1485         return false;
1486     }
1487 
1488     auto pOpt = parcel.ReadParcelable<NotificationDistributedOptions>();
1489     if (pOpt == nullptr) {
1490         ANS_LOGE("Failed to read distributedOptions");
1491         return false;
1492     }
1493     distributedOptions_ = *pOpt;
1494     delete pOpt;
1495     pOpt = nullptr;
1496 
1497     valid = parcel.ReadBool();
1498     if (valid) {
1499         notificationTemplate_ = std::shared_ptr<NotificationTemplate>(parcel.ReadParcelable<NotificationTemplate>());
1500         if (!notificationTemplate_) {
1501             ANS_LOGE("Failed to read notificationTemplate");
1502             return false;
1503         }
1504     }
1505 
1506     valid = parcel.ReadBool();
1507     if (valid) {
1508         notificationFlags_ = std::shared_ptr<NotificationFlags>(parcel.ReadParcelable<NotificationFlags>());
1509         if (!notificationFlags_) {
1510             ANS_LOGE("Failed to read notificationFlags");
1511             return false;
1512         }
1513     }
1514 
1515     updateDeadLine_ = parcel.ReadInt64();
1516     finishDeadLine_ = parcel.ReadInt64();
1517 
1518     return true;
1519 }
1520 
GetNowSysTime()1521 int64_t NotificationRequest::GetNowSysTime()
1522 {
1523     std::chrono::time_point<std::chrono::system_clock> nowSys = std::chrono::system_clock::now();
1524     auto epoch = nowSys.time_since_epoch();
1525     auto value = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
1526     int64_t duration = value.count();
1527     return duration;
1528 }
1529 
SetTemplate(const std::shared_ptr<NotificationTemplate> & templ)1530 void NotificationRequest::SetTemplate(const std::shared_ptr<NotificationTemplate> &templ)
1531 {
1532     notificationTemplate_ = templ;
1533 }
1534 
GetTemplate() const1535 std::shared_ptr<NotificationTemplate> NotificationRequest::GetTemplate() const
1536 {
1537     return notificationTemplate_;
1538 }
1539 
SetFlags(const std::shared_ptr<NotificationFlags> & flags)1540 void NotificationRequest::SetFlags(const std::shared_ptr<NotificationFlags> &flags)
1541 {
1542     notificationFlags_ = flags;
1543 }
1544 
GetFlags() const1545 std::shared_ptr<NotificationFlags> NotificationRequest::GetFlags() const
1546 {
1547     return notificationFlags_;
1548 }
1549 
SetReceiverUserId(int32_t userId)1550 void NotificationRequest::SetReceiverUserId(int32_t userId)
1551 {
1552     receiverUserId_ = userId;
1553 }
1554 
GetReceiverUserId() const1555 int32_t NotificationRequest::GetReceiverUserId() const
1556 {
1557     return receiverUserId_;
1558 }
1559 
IsRemoveAllowed() const1560 bool NotificationRequest::IsRemoveAllowed() const
1561 {
1562     return isRemoveAllowed_;
1563 }
1564 
SetRemoveAllowed(bool isRemoveAllowed)1565 void NotificationRequest::SetRemoveAllowed(bool isRemoveAllowed)
1566 {
1567     isRemoveAllowed_ = isRemoveAllowed;
1568 }
1569 
CopyBase(const NotificationRequest & other)1570 void NotificationRequest::CopyBase(const NotificationRequest &other)
1571 {
1572     this->notificationId_ = other.notificationId_;
1573     this->color_ = other.color_;
1574     this->badgeNumber_ = other.badgeNumber_;
1575     this->progressValue_ = other.progressValue_;
1576     this->progressMax_ = other.progressMax_;
1577     this->createTime_ = other.createTime_;
1578     this->deliveryTime_ = other.deliveryTime_;
1579     this->autoDeletedTime_ = other.autoDeletedTime_;
1580     this->updateDeadLine_ = other.updateDeadLine_;
1581     this->finishDeadLine_ = other.finishDeadLine_;
1582 
1583     this->creatorPid_ = other.creatorPid_;
1584     this->creatorUid_ = other.creatorUid_;
1585     this->ownerUid_ = other.ownerUid_;
1586     this->creatorUserId_ = other.creatorUserId_;
1587     this->ownerUserId_ = other.ownerUserId_;
1588     this->receiverUserId_ = other.receiverUserId_;
1589     this->isAgent_ = other.isAgent_;
1590     this->isRemoveAllowed_ = other.isRemoveAllowed_;
1591     this->isCoverActionButtons_ = other.isCoverActionButtons_;
1592 
1593     this->slotType_ = other.slotType_;
1594     this->settingsText_ = other.settingsText_;
1595     this->creatorBundleName_ = other.creatorBundleName_;
1596     this->ownerBundleName_ = other.ownerBundleName_;
1597     this->groupName_ = other.groupName_;
1598     this->statusBarText_ = other.statusBarText_;
1599     this->label_ = other.label_;
1600     this->shortcutId_ = other.shortcutId_;
1601     this->sortingKey_ = other.sortingKey_;
1602     this->classification_ = other.classification_;
1603 
1604     this->groupAlertType_ = other.groupAlertType_;
1605     this->visiblenessType_ = other.visiblenessType_;
1606     this->badgeStyle_ = other.badgeStyle_;
1607     this->notificationContentType_ = other.notificationContentType_;
1608 }
1609 
CopyOther(const NotificationRequest & other)1610 void NotificationRequest::CopyOther(const NotificationRequest &other)
1611 {
1612     this->showDeliveryTime_ = other.showDeliveryTime_;
1613     this->tapDismissed_ = other.tapDismissed_;
1614     this->colorEnabled_ = other.colorEnabled_;
1615     this->alertOneTime_ = other.alertOneTime_;
1616     this->showStopwatch_ = other.showStopwatch_;
1617     this->isCountdown_ = other.isCountdown_;
1618     this->inProgress_ = other.inProgress_;
1619     this->groupOverview_ = other.groupOverview_;
1620     this->progressIndeterminate_ = other.progressIndeterminate_;
1621     this->unremovable_ = other.unremovable_;
1622     this->floatingIcon_ = other.floatingIcon_;
1623     this->onlyLocal_ = other.onlyLocal_;
1624     this->permitted_ = other.permitted_;
1625 
1626     this->wantAgent_ = other.wantAgent_;
1627     this->removalWantAgent_ = other.removalWantAgent_;
1628     this->maxScreenWantAgent_ = other.maxScreenWantAgent_;
1629     this->additionalParams_ = other.additionalParams_;
1630     this->littleIcon_ = other.littleIcon_;
1631     this->bigIcon_ = other.bigIcon_;
1632     this->overlayIcon_ = other.overlayIcon_;
1633     this->notificationContent_ = other.notificationContent_;
1634 
1635     this->actionButtons_ = other.actionButtons_;
1636     this->messageUsers_ = other.messageUsers_;
1637     this->userInputHistory_ = other.userInputHistory_;
1638 
1639     this->distributedOptions_ = other.distributedOptions_;
1640 
1641     this->notificationTemplate_ = other.notificationTemplate_;
1642     this->notificationFlags_ = other.notificationFlags_;
1643 }
1644 
ConvertObjectsToJson(nlohmann::json & jsonObject) const1645 bool NotificationRequest::ConvertObjectsToJson(nlohmann::json &jsonObject) const
1646 {
1647     jsonObject["wantAgent"] = wantAgent_ ? AbilityRuntime::WantAgent::WantAgentHelper::ToString(wantAgent_) : "";
1648 
1649     nlohmann::json contentObj;
1650     if (notificationContent_) {
1651         if (!NotificationJsonConverter::ConvertToJson(notificationContent_.get(), contentObj)) {
1652             ANS_LOGE("Cannot convert notificationContent to JSON");
1653             return false;
1654         }
1655     }
1656     jsonObject["content"] = contentObj;
1657 
1658     nlohmann::json buttonsArr = nlohmann::json::array();
1659     for (auto &btn : actionButtons_) {
1660         if (!btn) {
1661             continue;
1662         }
1663 
1664         nlohmann::json btnObj;
1665         if (!NotificationJsonConverter::ConvertToJson(btn.get(), btnObj)) {
1666             ANS_LOGE("Cannot convert actionButton to JSON");
1667             return false;
1668         }
1669 
1670         buttonsArr.emplace_back(btnObj);
1671     }
1672     jsonObject["actionButtons"] = buttonsArr;
1673 
1674     std::string extraInfoStr;
1675     if (additionalParams_) {
1676         AAFwk::WantParamWrapper wWrapper(*additionalParams_);
1677         extraInfoStr = wWrapper.ToString();
1678     }
1679     jsonObject["extraInfo"] = extraInfoStr;
1680 
1681     jsonObject["smallIcon"] = AnsImageUtil::PackImage(littleIcon_);
1682     jsonObject["largeIcon"] = AnsImageUtil::PackImage(bigIcon_);
1683     jsonObject["overlayIcon"] = overlayIcon_ ? AnsImageUtil::PackImage(overlayIcon_) : "";
1684 
1685     nlohmann::json optObj;
1686     if (!NotificationJsonConverter::ConvertToJson(&distributedOptions_, optObj)) {
1687         ANS_LOGE("Cannot convert distributedOptions to JSON");
1688         return false;
1689     }
1690     jsonObject["distributedOptions"] = optObj;
1691 
1692     if (notificationFlags_) {
1693         nlohmann::json flagsObj;
1694         if (!NotificationJsonConverter::ConvertToJson(notificationFlags_.get(), flagsObj)) {
1695             ANS_LOGE("Cannot convert notificationFlags to JSON");
1696             return false;
1697         }
1698         jsonObject["notificationFlags"] = flagsObj;
1699     }
1700 
1701     return true;
1702 }
1703 
ConvertJsonToNumExt(NotificationRequest * target,const nlohmann::json & jsonObject)1704 void NotificationRequest::ConvertJsonToNumExt(
1705     NotificationRequest *target, const nlohmann::json &jsonObject)
1706 {
1707     const auto &jsonEnd = jsonObject.cend();
1708 
1709     if (jsonObject.find("updateDeadLine") != jsonEnd && jsonObject.at("updateDeadLine").is_number_integer()) {
1710         target->updateDeadLine_ = jsonObject.at("updateDeadLine").get<int64_t>();
1711     }
1712 
1713     if (jsonObject.find("finishDeadLine") != jsonEnd && jsonObject.at("finishDeadLine").is_number_integer()) {
1714         target->finishDeadLine_ = jsonObject.at("finishDeadLine").get<int64_t>();
1715     }
1716 
1717     if (jsonObject.find("ownerUserId") != jsonEnd && jsonObject.at("ownerUserId").is_number_integer()) {
1718         target->ownerUserId_ = jsonObject.at("ownerUserId").get<int32_t>();
1719     }
1720 
1721     if (jsonObject.find("ownerUid") != jsonEnd && jsonObject.at("ownerUid").is_number_integer()) {
1722         target->ownerUid_ = jsonObject.at("ownerUid").get<int32_t>();
1723     }
1724 }
1725 
ConvertJsonToNum(NotificationRequest * target,const nlohmann::json & jsonObject)1726 void NotificationRequest::ConvertJsonToNum(NotificationRequest *target, const nlohmann::json &jsonObject)
1727 {
1728     if (target == nullptr) {
1729         ANS_LOGE("Invalid input parameter");
1730         return;
1731     }
1732 
1733     const auto &jsonEnd = jsonObject.cend();
1734 
1735     if (jsonObject.find("id") != jsonEnd && jsonObject.at("id").is_number_integer()) {
1736         target->notificationId_ = jsonObject.at("id").get<int32_t>();
1737     }
1738 
1739     if (jsonObject.find("color") != jsonEnd && jsonObject.at("color").is_number_integer()) {
1740         target->color_ = jsonObject.at("color").get<uint32_t>();
1741     }
1742 
1743     if (jsonObject.find("deliveryTime") != jsonEnd && jsonObject.at("deliveryTime").is_number_integer()) {
1744         target->deliveryTime_ = jsonObject.at("deliveryTime").get<int64_t>();
1745     }
1746 
1747     if (jsonObject.find("autoDeletedTime") != jsonEnd && jsonObject.at("autoDeletedTime").is_number_integer()) {
1748         target->autoDeletedTime_ = jsonObject.at("autoDeletedTime").get<int64_t>();
1749     }
1750 
1751     if (jsonObject.find("creatorUid") != jsonEnd && jsonObject.at("creatorUid").is_number_integer()) {
1752         target->creatorUid_ = jsonObject.at("creatorUid").get<int32_t>();
1753     }
1754 
1755     if (jsonObject.find("creatorPid") != jsonEnd && jsonObject.at("creatorPid").is_number_integer()) {
1756         target->creatorPid_ = jsonObject.at("creatorPid").get<int32_t>();
1757     }
1758 
1759     if (jsonObject.find("creatorUserId") != jsonEnd && jsonObject.at("creatorUserId").is_number_integer()) {
1760         target->creatorUserId_ = jsonObject.at("creatorUserId").get<int32_t>();
1761     }
1762 
1763     if (jsonObject.find("receiverUserId") != jsonEnd && jsonObject.at("receiverUserId").is_number_integer()) {
1764         target->receiverUserId_ = jsonObject.at("receiverUserId").get<int32_t>();
1765     }
1766 
1767     if (jsonObject.find("badgeNumber") != jsonEnd && jsonObject.at("badgeNumber").is_number_integer()) {
1768         target->badgeNumber_ = jsonObject.at("badgeNumber").get<uint32_t>();
1769     }
1770 
1771     ConvertJsonToNumExt(target, jsonObject);
1772 }
1773 
ConvertJsonToString(NotificationRequest * target,const nlohmann::json & jsonObject)1774 void NotificationRequest::ConvertJsonToString(NotificationRequest *target, const nlohmann::json &jsonObject)
1775 {
1776     if (target == nullptr) {
1777         ANS_LOGE("Invalid input parameter");
1778         return;
1779     }
1780 
1781     const auto &jsonEnd = jsonObject.cend();
1782 
1783     if (jsonObject.find("creatorBundleName") != jsonEnd && jsonObject.at("creatorBundleName").is_string()) {
1784         target->creatorBundleName_ = jsonObject.at("creatorBundleName").get<std::string>();
1785     }
1786 
1787     if (jsonObject.find("ownerBundleName") != jsonEnd && jsonObject.at("ownerBundleName").is_string()) {
1788         target->ownerBundleName_ = jsonObject.at("ownerBundleName").get<std::string>();
1789     }
1790 
1791     if (jsonObject.find("groupName") != jsonEnd && jsonObject.at("groupName").is_string()) {
1792         target->groupName_ = jsonObject.at("groupName").get<std::string>();
1793     }
1794 
1795     if (jsonObject.find("label") != jsonEnd && jsonObject.at("label").is_string()) {
1796         target->label_ = jsonObject.at("label").get<std::string>();
1797     }
1798 
1799     if (jsonObject.find("classification") != jsonEnd && jsonObject.at("classification").is_string()) {
1800         target->classification_ = jsonObject.at("classification").get<std::string>();
1801     }
1802 
1803     if (jsonObject.find("creatorBundleName") != jsonEnd && jsonObject.at("creatorBundleName").is_string()) {
1804         target->creatorBundleName_ = jsonObject.at("creatorBundleName").get<std::string>();
1805     }
1806 }
1807 
ConvertJsonToEnum(NotificationRequest * target,const nlohmann::json & jsonObject)1808 void NotificationRequest::ConvertJsonToEnum(NotificationRequest *target, const nlohmann::json &jsonObject)
1809 {
1810     if (target == nullptr) {
1811         ANS_LOGE("Invalid input parameter");
1812         return;
1813     }
1814 
1815     const auto &jsonEnd = jsonObject.cend();
1816 
1817     if (jsonObject.find("slotType") != jsonEnd && jsonObject.at("slotType").is_number_integer()) {
1818         auto slotTypeValue  = jsonObject.at("slotType").get<int32_t>();
1819         target->slotType_ = static_cast<NotificationConstant::SlotType>(slotTypeValue);
1820     }
1821 
1822     if (jsonObject.find("badgeIconStyle") != jsonEnd && jsonObject.at("badgeIconStyle").is_number_integer()) {
1823         auto badgeStyleValue  = jsonObject.at("badgeIconStyle").get<int32_t>();
1824         target->badgeStyle_ = static_cast<NotificationRequest::BadgeStyle>(badgeStyleValue);
1825     }
1826 
1827     if (jsonObject.find("notificationContentType") != jsonEnd &&
1828         jsonObject.at("notificationContentType").is_number_integer()) {
1829         auto notificationContentType = jsonObject.at("notificationContentType").get<int32_t>();
1830         target->notificationContentType_ = static_cast<NotificationContent::Type>(notificationContentType);
1831     }
1832 }
1833 
ConvertJsonToBool(NotificationRequest * target,const nlohmann::json & jsonObject)1834 void NotificationRequest::ConvertJsonToBool(NotificationRequest *target, const nlohmann::json &jsonObject)
1835 {
1836     if (target == nullptr) {
1837         ANS_LOGE("Invalid input parameter");
1838         return;
1839     }
1840 
1841     const auto &jsonEnd = jsonObject.cend();
1842 
1843     if (jsonObject.find("showDeliveryTime") != jsonEnd && jsonObject.at("showDeliveryTime").is_boolean()) {
1844         target->showDeliveryTime_ = jsonObject.at("showDeliveryTime").get<bool>();
1845     }
1846 
1847     if (jsonObject.find("tapDismissed") != jsonEnd && jsonObject.at("tapDismissed").is_boolean()) {
1848         target->tapDismissed_ = jsonObject.at("tapDismissed").get<bool>();
1849     }
1850 
1851     if (jsonObject.find("colorEnabled") != jsonEnd && jsonObject.at("colorEnabled").is_boolean()) {
1852         target->colorEnabled_ = jsonObject.at("colorEnabled").get<bool>();
1853     }
1854 
1855     if (jsonObject.find("isOngoing") != jsonEnd && jsonObject.at("isOngoing").is_boolean()) {
1856         target->inProgress_ = jsonObject.at("isOngoing").get<bool>();
1857     }
1858 
1859     if (jsonObject.find("isAlertOnce") != jsonEnd && jsonObject.at("isAlertOnce").is_boolean()) {
1860         target->alertOneTime_ = jsonObject.at("isAlertOnce").get<bool>();
1861     }
1862 
1863     if (jsonObject.find("isStopwatch") != jsonEnd && jsonObject.at("isStopwatch").is_boolean()) {
1864         target->showStopwatch_ = jsonObject.at("isStopwatch").get<bool>();
1865     }
1866 
1867     if (jsonObject.find("isCountdown") != jsonEnd && jsonObject.at("isCountdown").is_boolean()) {
1868         target->isCountdown_ = jsonObject.at("isCountdown").get<bool>();
1869     }
1870 
1871     if (jsonObject.find("isUnremovable") != jsonEnd && jsonObject.at("isUnremovable").is_boolean()) {
1872         target->unremovable_ = jsonObject.at("isUnremovable").get<bool>();
1873     }
1874 
1875     if (jsonObject.find("isFloatingIcon") != jsonEnd && jsonObject.at("isFloatingIcon").is_boolean()) {
1876         target->floatingIcon_ = jsonObject.at("isFloatingIcon").get<bool>();
1877     }
1878 
1879     ConvertJsonToBoolExt(target, jsonObject);
1880 }
1881 
ConvertJsonToBoolExt(NotificationRequest * target,const nlohmann::json & jsonObject)1882 void NotificationRequest::ConvertJsonToBoolExt(NotificationRequest *target, const nlohmann::json &jsonObject)
1883 {
1884     const auto &jsonEnd = jsonObject.cend();
1885 
1886     if (jsonObject.find("isAgent") != jsonEnd && jsonObject.at("isAgent").is_boolean()) {
1887         target->isAgent_ = jsonObject.at("isAgent").get<bool>();
1888     }
1889 }
1890 
ConvertJsonToPixelMap(NotificationRequest * target,const nlohmann::json & jsonObject)1891 void NotificationRequest::ConvertJsonToPixelMap(NotificationRequest *target, const nlohmann::json &jsonObject)
1892 {
1893     if (target == nullptr) {
1894         ANS_LOGE("Invalid input parameter");
1895         return;
1896     }
1897 
1898     const auto &jsonEnd = jsonObject.cend();
1899 
1900     if (jsonObject.find("smallIcon") != jsonEnd && jsonObject.at("smallIcon").is_string()) {
1901         auto littleIconStr    = jsonObject.at("smallIcon").get<std::string>();
1902         target->littleIcon_ = AnsImageUtil::UnPackImage(littleIconStr);
1903     }
1904 
1905     if (jsonObject.find("largeIcon") != jsonEnd && jsonObject.at("largeIcon").is_string()) {
1906         auto bigIconStr    = jsonObject.at("largeIcon").get<std::string>();
1907         target->bigIcon_ = AnsImageUtil::UnPackImage(bigIconStr);
1908     }
1909 
1910     if (jsonObject.find("overlayIcon") != jsonEnd && jsonObject.at("overlayIcon").is_string()) {
1911         auto overlayIconStr    = jsonObject.at("overlayIcon").get<std::string>();
1912         target->overlayIcon_ = AnsImageUtil::UnPackImage(overlayIconStr);
1913     }
1914 }
1915 
ConvertJsonToNotificationContent(NotificationRequest * target,const nlohmann::json & jsonObject)1916 bool NotificationRequest::ConvertJsonToNotificationContent(
1917     NotificationRequest *target, const nlohmann::json &jsonObject)
1918 {
1919     if (target == nullptr) {
1920         ANS_LOGE("Invalid input parameter");
1921         return false;
1922     }
1923 
1924     const auto &jsonEnd = jsonObject.cend();
1925 
1926     if (jsonObject.find("content") != jsonEnd) {
1927         auto contentObj = jsonObject.at("content");
1928         if (!contentObj.is_null()) {
1929             auto pContent = NotificationJsonConverter::ConvertFromJson<NotificationContent>(contentObj);
1930             if (pContent == nullptr) {
1931                 ANS_LOGE("Failed to parse notification content!");
1932                 return false;
1933             }
1934 
1935             target->notificationContent_ = std::shared_ptr<NotificationContent>(pContent);
1936         }
1937     }
1938 
1939     return true;
1940 }
1941 
ConvertJsonToNotificationActionButton(NotificationRequest * target,const nlohmann::json & jsonObject)1942 bool NotificationRequest::ConvertJsonToNotificationActionButton(
1943     NotificationRequest *target, const nlohmann::json &jsonObject)
1944 {
1945     if (target == nullptr) {
1946         ANS_LOGE("Invalid input parameter");
1947         return false;
1948     }
1949 
1950     const auto &jsonEnd = jsonObject.cend();
1951 
1952     if (jsonObject.find("actionButtons") != jsonEnd) {
1953         auto buttonArr = jsonObject.at("actionButtons");
1954         for (auto &btnObj : buttonArr) {
1955             auto pBtn = NotificationJsonConverter::ConvertFromJson<NotificationActionButton>(btnObj);
1956             if (pBtn == nullptr) {
1957                 ANS_LOGE("Failed to parse actionButton!");
1958                 return false;
1959             }
1960 
1961             target->actionButtons_.emplace_back(pBtn);
1962         }
1963     }
1964 
1965     return true;
1966 }
1967 
ConvertJsonToNotificationDistributedOptions(NotificationRequest * target,const nlohmann::json & jsonObject)1968 bool NotificationRequest::ConvertJsonToNotificationDistributedOptions(
1969     NotificationRequest *target, const nlohmann::json &jsonObject)
1970 {
1971     if (target == nullptr) {
1972         ANS_LOGE("Invalid input parameter");
1973         return false;
1974     }
1975 
1976     const auto &jsonEnd = jsonObject.cend();
1977 
1978     if (jsonObject.find("distributedOptions") != jsonEnd) {
1979         auto optObj = jsonObject.at("distributedOptions");
1980         if (!optObj.is_null()) {
1981             auto *pOpt = NotificationJsonConverter::ConvertFromJson<NotificationDistributedOptions>(optObj);
1982             if (pOpt == nullptr) {
1983                 ANS_LOGE("Failed to parse distributedOptions!");
1984                 return false;
1985             }
1986 
1987             target->distributedOptions_ = *pOpt;
1988         }
1989     }
1990 
1991     return true;
1992 }
1993 
ConvertJsonToNotificationFlags(NotificationRequest * target,const nlohmann::json & jsonObject)1994 bool NotificationRequest::ConvertJsonToNotificationFlags(
1995     NotificationRequest *target, const nlohmann::json &jsonObject)
1996 {
1997     if (target == nullptr) {
1998         ANS_LOGE("Invalid input parameter");
1999         return false;
2000     }
2001 
2002     const auto &jsonEnd = jsonObject.cend();
2003 
2004     if (jsonObject.find("notificationFlags") != jsonEnd) {
2005         auto flagsObj = jsonObject.at("notificationFlags");
2006         if (!flagsObj.is_null()) {
2007             auto *pFlags = NotificationJsonConverter::ConvertFromJson<NotificationFlags>(flagsObj);
2008             if (pFlags == nullptr) {
2009                 ANS_LOGE("Failed to parse notificationFlags!");
2010                 return false;
2011             }
2012 
2013             target->notificationFlags_ = std::shared_ptr<NotificationFlags>(pFlags);
2014         }
2015     }
2016 
2017     return true;
2018 }
2019 
IsCommonLiveView() const2020 bool NotificationRequest::IsCommonLiveView() const
2021 {
2022     return (slotType_ == NotificationConstant::SlotType::LIVE_VIEW) &&
2023         (notificationContentType_ == NotificationContent::Type::LIVE_VIEW);
2024 }
2025 
CheckVersion(const sptr<NotificationRequest> & oldRequest) const2026 ErrCode NotificationRequest::CheckVersion(const sptr<NotificationRequest> &oldRequest) const
2027 {
2028     auto content = notificationContent_->GetNotificationContent();
2029     auto liveView = std::static_pointer_cast<NotificationLiveViewContent>(content);
2030     auto oldContent = oldRequest->GetContent()->GetNotificationContent();
2031     auto oldLiveView = std::static_pointer_cast<NotificationLiveViewContent>(oldContent);
2032 
2033     if (oldLiveView->GetVersion() == NotificationLiveViewContent::MAX_VERSION) {
2034         return ERR_OK;
2035     }
2036     if (liveView->GetVersion() == NotificationLiveViewContent::MAX_VERSION) {
2037         ANS_LOGE("Invalid version, creator bundle name %{public}s, id %{public}d, "
2038             "old version %{public}u, new version %{public}u.", GetCreatorBundleName().c_str(),
2039             GetNotificationId(), oldLiveView->GetVersion(), liveView->GetVersion());
2040         return ERR_ANS_EXPIRED_NOTIFICATION;
2041     }
2042     if (oldLiveView->GetVersion() >= liveView->GetVersion()) {
2043         ANS_LOGE("Live view has finished, creator bundle name %{public}s, id %{public}d, "
2044             "old version %{public}u, new version %{public}u.", GetCreatorBundleName().c_str(),
2045             GetNotificationId(), oldLiveView->GetVersion(), liveView->GetVersion());
2046         return ERR_ANS_EXPIRED_NOTIFICATION;
2047     }
2048     return ERR_OK;
2049 }
2050 
CheckNotificationRequest(const sptr<NotificationRequest> & oldRequest) const2051 ErrCode NotificationRequest::CheckNotificationRequest(const sptr<NotificationRequest> &oldRequest) const
2052 {
2053     if (!IsCommonLiveView()) {
2054         if ((oldRequest != nullptr) && oldRequest->IsCommonLiveView()) {
2055             ANS_LOGE("Invalid new request param, slot type %{public}d, content type %{public}d.",
2056                 GetSlotType(), GetNotificationType());
2057             return ERR_ANS_INVALID_PARAM;
2058         }
2059         return ERR_OK;
2060     }
2061 
2062     using StatusType = NotificationLiveViewContent::LiveViewStatus;
2063     auto content = notificationContent_->GetNotificationContent();
2064     auto liveView = std::static_pointer_cast<NotificationLiveViewContent>(content);
2065     auto status = liveView->GetLiveViewStatus();
2066     if (oldRequest == nullptr) {
2067         if (status != StatusType::LIVE_VIEW_CREATE) {
2068             ANS_LOGE("Doesn't exist live view, bundle name %{public}s, id %{public}d.",
2069                 GetCreatorBundleName().c_str(), GetNotificationId());
2070             return ERR_ANS_NOTIFICATION_NOT_EXISTS;
2071         }
2072 
2073         return ERR_OK;
2074     }
2075 
2076     if (!oldRequest->IsCommonLiveView()) {
2077         ANS_LOGE("Invalid old request param, slot type %{public}d, content type %{public}d.",
2078             oldRequest->GetSlotType(), oldRequest->GetNotificationType());
2079         return ERR_ANS_INVALID_PARAM;
2080     }
2081 
2082     if (status == StatusType::LIVE_VIEW_CREATE) {
2083         ANS_LOGW("Repeat create live view, bundle name %{public}s, id %{public}d.",
2084             GetCreatorBundleName().c_str(), GetNotificationId());
2085         return ERR_ANS_REPEAT_CREATE;
2086     }
2087 
2088     auto oldContent = oldRequest->GetContent()->GetNotificationContent();
2089     auto oldLiveView = std::static_pointer_cast<NotificationLiveViewContent>(oldContent);
2090     auto oldStatus = oldLiveView->GetLiveViewStatus();
2091     if (oldStatus == StatusType::LIVE_VIEW_END) {
2092         ANS_LOGW("Live view has finished, bundle name %{public}s, id %{public}d.",
2093             GetCreatorBundleName().c_str(), GetNotificationId());
2094         return ERR_ANS_END_NOTIFICATION;
2095     }
2096 
2097     return CheckVersion(oldRequest);
2098 }
2099 
FillMissingParameters(const sptr<NotificationRequest> & oldRequest)2100 void NotificationRequest::FillMissingParameters(const sptr<NotificationRequest> &oldRequest)
2101 {
2102     if (!IsCommonLiveView() || (oldRequest == nullptr)) {
2103         return;
2104     }
2105 
2106     updateDeadLine_ = oldRequest->updateDeadLine_;
2107     finishDeadLine_ = oldRequest->finishDeadLine_;
2108     if (autoDeletedTime_ == NotificationConstant::INVALID_AUTO_DELETE_TIME) {
2109         autoDeletedTime_ = oldRequest->autoDeletedTime_;
2110     }
2111     if (wantAgent_ == nullptr) {
2112         wantAgent_ = oldRequest->wantAgent_;
2113     }
2114 
2115     auto content = notificationContent_->GetNotificationContent();
2116     auto newLiveViewContent = std::static_pointer_cast<NotificationLiveViewContent>(content);
2117     if (newLiveViewContent->GetLiveViewStatus() ==
2118         NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_FULL_UPDATE) {
2119         return;
2120     }
2121     auto newExtraInfo = newLiveViewContent->GetExtraInfo();
2122     auto oldContent = oldRequest->GetContent()->GetNotificationContent();
2123     auto oldLiveViewContent = std::static_pointer_cast<NotificationLiveViewContent>(oldContent);
2124     auto oldExtraInfo = oldLiveViewContent->GetExtraInfo();
2125     if (newExtraInfo == nullptr) {
2126         newLiveViewContent->SetExtraInfo(oldExtraInfo);
2127     } else if (oldExtraInfo != nullptr) {
2128         auto oldKeySet = oldExtraInfo->KeySet();
2129         for (const auto &key : oldKeySet) {
2130             if (!newExtraInfo->HasParam(key)) {
2131                 newExtraInfo->SetParam(key, oldExtraInfo->GetParam(key));
2132             }
2133         }
2134     }
2135 
2136     auto newPicture = newLiveViewContent->GetPicture();
2137     auto oldPicture = oldLiveViewContent->GetPicture();
2138     bool isSet = false;
2139     for (const auto &pictureRecord : oldPicture) {
2140         if (newPicture.find(pictureRecord.first) != newPicture.end()) {
2141             continue;
2142         }
2143         newPicture[pictureRecord.first] = pictureRecord.second;
2144         isSet = true;
2145     }
2146     if (isSet) {
2147         newLiveViewContent->SetPicture(newPicture);
2148     }
2149 }
2150 
GetBaseKey(const std::string & deviceId)2151 std::string NotificationRequest::GetBaseKey(const std::string &deviceId)
2152 {
2153     const char *keySpliter = "_";
2154 
2155     std::stringstream stream;
2156     if (IsAgentNotification()) {
2157         stream << deviceId << keySpliter << ownerUserId_ << keySpliter <<
2158             ownerUid_ << keySpliter << ownerBundleName_ << keySpliter <<
2159             label_ << keySpliter << notificationId_;
2160     } else {
2161         stream << deviceId << keySpliter << creatorUserId_ << keySpliter <<
2162             creatorUid_ << keySpliter << creatorBundleName_ << keySpliter <<
2163             label_ << keySpliter << notificationId_;
2164     }
2165     return stream.str();
2166 }
2167 
GetKey()2168 std::string NotificationRequest::GetKey()
2169 {
2170     std::stringstream stream;
2171     const char *keySpliter = "_";
2172     stream << REQUEST_STORAGE_KEY_PREFIX << keySpliter << GetBaseKey("");
2173     return stream.str();
2174 }
2175 
CheckImageOverSizeForPixelMap(const std::shared_ptr<Media::PixelMap> & pixelMap,uint32_t maxSize)2176 bool NotificationRequest::CheckImageOverSizeForPixelMap(
2177     const std::shared_ptr<Media::PixelMap> &pixelMap, uint32_t maxSize)
2178 {
2179     if (pixelMap == nullptr) {
2180         return false;
2181     }
2182 
2183     auto size = static_cast<uint32_t>(pixelMap->GetByteCount());
2184     return size > maxSize;
2185 }
2186 
CheckImageSizeForConverSation(std::shared_ptr<NotificationBasicContent> & content)2187 ErrCode NotificationRequest::CheckImageSizeForConverSation(std::shared_ptr<NotificationBasicContent> &content)
2188 {
2189     auto conversationalContent = std::static_pointer_cast<NotificationConversationalContent>(content);
2190     auto picture = conversationalContent->GetMessageUser().GetPixelMap();
2191     if (CheckImageOverSizeForPixelMap(picture, MAX_ICON_SIZE)) {
2192         ANS_LOGE("The size of picture in ConversationalContent's message user exceeds limit");
2193         return ERR_ANS_ICON_OVER_SIZE;
2194     }
2195 
2196     auto messages = conversationalContent->GetAllConversationalMessages();
2197     for (auto &msg : messages) {
2198         if (!msg) {
2199             continue;
2200         }
2201         auto img = msg->GetSender().GetPixelMap();
2202         if (CheckImageOverSizeForPixelMap(img, MAX_ICON_SIZE)) {
2203             ANS_LOGE("The size of picture in ConversationalContent's message exceeds limit");
2204             return ERR_ANS_ICON_OVER_SIZE;
2205         }
2206     }
2207     return ERR_OK;
2208 }
2209 
CheckImageSizeForPicture(std::shared_ptr<NotificationBasicContent> & content)2210 ErrCode NotificationRequest::CheckImageSizeForPicture(std::shared_ptr<NotificationBasicContent> &content)
2211 {
2212     auto pictureContent = std::static_pointer_cast<NotificationPictureContent>(content);
2213     auto bigPicture = pictureContent->GetBigPicture();
2214     if (CheckImageOverSizeForPixelMap(bigPicture, MAX_PICTURE_SIZE)) {
2215         ANS_LOGE("The size of big picture in PictureContent exceeds limit");
2216         return ERR_ANS_PICTURE_OVER_SIZE;
2217     }
2218     return ERR_OK;
2219 }
2220 
CheckImageSizeForLiveView(std::shared_ptr<NotificationBasicContent> & content)2221 ErrCode NotificationRequest::CheckImageSizeForLiveView(std::shared_ptr<NotificationBasicContent> &content)
2222 {
2223     auto liveViewContent = std::static_pointer_cast<NotificationLiveViewContent>(content);
2224     auto pictureMap = liveViewContent->GetPicture();
2225     for (const auto &pixelMapRecord : pictureMap) {
2226         if (pixelMapRecord.second.empty()) {
2227             ANS_LOGE("Picture key exist, but picture content is empty.");
2228             return ERR_ANS_INVALID_PARAM;
2229         }
2230         if (pixelMapRecord.second.size() > MAX_LIVE_VIEW_ICON_NUM) {
2231             ANS_LOGE("Picture key exist, but picture content is empty.");
2232             return ERR_ANS_INVALID_PARAM;
2233         }
2234         for (const auto &pixelMap : pixelMapRecord.second) {
2235             if (CheckImageOverSizeForPixelMap(pixelMap, MAX_ICON_SIZE)) {
2236                 ANS_LOGE("The size of big picture in PictureContent exceeds limit.");
2237                 return ERR_ANS_ICON_OVER_SIZE;
2238             }
2239         }
2240     }
2241     return ERR_OK;
2242 }
2243 
CheckImageSizeForContent() const2244 ErrCode NotificationRequest::CheckImageSizeForContent() const
2245 {
2246     auto content = GetContent();
2247     if (content == nullptr) {
2248         ANS_LOGE("Invalid content in NotificationRequest");
2249         return ERR_OK;
2250     }
2251 
2252     auto basicContent = GetContent()->GetNotificationContent();
2253     if (basicContent == nullptr) {
2254         ANS_LOGE("Invalid content in NotificationRequest");
2255         return ERR_OK;
2256     }
2257 
2258     auto contentType = GetNotificationType();
2259     switch (contentType) {
2260         case NotificationContent::Type::CONVERSATION:
2261             return CheckImageSizeForConverSation(basicContent);
2262         case NotificationContent::Type::PICTURE:
2263             return CheckImageSizeForPicture(basicContent);
2264         case NotificationContent::Type::LIVE_VIEW:
2265             return CheckImageSizeForLiveView(basicContent);
2266         default:
2267             return ERR_OK;
2268     }
2269 }
2270 
SetIsCoverActionButtons(bool isCoverActionButtons)2271 void NotificationRequest::SetIsCoverActionButtons(bool isCoverActionButtons)
2272 {
2273     isCoverActionButtons_ = isCoverActionButtons;
2274 }
2275 
IsCoverActionButtons() const2276 bool NotificationRequest::IsCoverActionButtons() const
2277 {
2278     return isCoverActionButtons_;
2279 }
2280 
2281 }  // namespace Notification
2282 }  // namespace OHOS
2283