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_image_util.h"
19 #include "ans_log_wrapper.h"
20 #include "want_agent_helper.h"
21 #include "want_params_wrapper.h"
22
23 namespace OHOS {
24 namespace Notification {
25 const std::string NotificationRequest::CLASSIFICATION_ALARM {"alarm"};
26 const std::string NotificationRequest::CLASSIFICATION_CALL {"call"};
27 const std::string NotificationRequest::CLASSIFICATION_EMAIL {"email"};
28 const std::string NotificationRequest::CLASSIFICATION_ERROR {"err"};
29 const std::string NotificationRequest::CLASSIFICATION_EVENT {"event"};
30 const std::string NotificationRequest::CLASSIFICATION_MESSAGE {"msg"};
31 const std::string NotificationRequest::CLASSIFICATION_NAVIGATION {"navigation"};
32 const std::string NotificationRequest::CLASSIFICATION_PROGRESS {"progress"};
33 const std::string NotificationRequest::CLASSIFICATION_PROMO {"promo"};
34 const std::string NotificationRequest::CLASSIFICATION_RECOMMENDATION {"recommendation"};
35 const std::string NotificationRequest::CLASSIFICATION_REMINDER {"reminder"};
36 const std::string NotificationRequest::CLASSIFICATION_SERVICE {"service"};
37 const std::string NotificationRequest::CLASSIFICATION_SOCIAL {"social"};
38 const std::string NotificationRequest::CLASSIFICATION_STATUS {"status"};
39 const std::string NotificationRequest::CLASSIFICATION_SYSTEM {"sys"};
40 const std::string NotificationRequest::CLASSIFICATION_TRANSPORT {"transport"};
41
42 const uint32_t NotificationRequest::COLOR_DEFAULT {0};
43
44 const uint32_t NotificationRequest::COLOR_MASK {0xFF000000};
45 const std::size_t NotificationRequest::MAX_USER_INPUT_HISTORY {5};
46 const std::size_t NotificationRequest::MAX_ACTION_BUTTONS {3};
47
NotificationRequest(int32_t notificationId)48 NotificationRequest::NotificationRequest(int32_t notificationId) : notificationId_(notificationId)
49 {
50 createTime_ = GetNowSysTime();
51 deliveryTime_ = GetNowSysTime();
52 }
53
NotificationRequest(const NotificationRequest & other)54 NotificationRequest::NotificationRequest(const NotificationRequest &other)
55 {
56 CopyBase(other);
57 CopyOther(other);
58 }
59
operator =(const NotificationRequest & other)60 NotificationRequest &NotificationRequest::operator=(const NotificationRequest &other)
61 {
62 CopyBase(other);
63 CopyOther(other);
64
65 return *this;
66 }
67
~NotificationRequest()68 NotificationRequest::~NotificationRequest()
69 {}
70
IsInProgress() const71 bool NotificationRequest::IsInProgress() const
72 {
73 return inProgress_;
74 }
75
SetInProgress(bool isOngoing)76 void NotificationRequest::SetInProgress(bool isOngoing)
77 {
78 inProgress_ = isOngoing;
79 }
80
IsUnremovable() const81 bool NotificationRequest::IsUnremovable() const
82 {
83 return unremovable_;
84 }
85
SetUnremovable(bool isUnremovable)86 void NotificationRequest::SetUnremovable(bool isUnremovable)
87 {
88 unremovable_ = isUnremovable;
89 }
90
SetBadgeNumber(uint32_t number)91 void NotificationRequest::SetBadgeNumber(uint32_t number)
92 {
93 badgeNumber_ = number;
94 }
95
GetBadgeNumber() const96 uint32_t NotificationRequest::GetBadgeNumber() const
97 {
98 return badgeNumber_;
99 }
100
SetNotificationId(int32_t notificationId)101 void NotificationRequest::SetNotificationId(int32_t notificationId)
102 {
103 notificationId_ = notificationId;
104 }
105
GetNotificationId() const106 int32_t NotificationRequest::GetNotificationId() const
107 {
108 return notificationId_;
109 }
110
SetWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> & wantAgent)111 void NotificationRequest::SetWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> &wantAgent)
112 {
113 wantAgent_ = wantAgent;
114 }
115
GetWantAgent() const116 const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> NotificationRequest::GetWantAgent() const
117 {
118 return wantAgent_;
119 }
120
SetRemovalWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> & wantAgent)121 void NotificationRequest::SetRemovalWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> &wantAgent)
122 {
123 removalWantAgent_ = wantAgent;
124 }
125
GetRemovalWantAgent() const126 const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> NotificationRequest::GetRemovalWantAgent() const
127 {
128 return removalWantAgent_;
129 }
130
SetMaxScreenWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> & wantAgent)131 void NotificationRequest::SetMaxScreenWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> &wantAgent)
132 {
133 maxScreenWantAgent_ = wantAgent;
134 }
135
GetMaxScreenWantAgent() const136 const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> NotificationRequest::GetMaxScreenWantAgent() const
137 {
138 return maxScreenWantAgent_;
139 }
140
SetAdditionalData(const std::shared_ptr<AAFwk::WantParams> & extras)141 void NotificationRequest::SetAdditionalData(const std::shared_ptr<AAFwk::WantParams> &extras)
142 {
143 additionalParams_ = extras;
144 }
145
GetAdditionalData() const146 const std::shared_ptr<AAFwk::WantParams> NotificationRequest::GetAdditionalData() const
147 {
148 return additionalParams_;
149 }
150
SetDeliveryTime(int64_t deliveryTime)151 void NotificationRequest::SetDeliveryTime(int64_t deliveryTime)
152 {
153 deliveryTime_ = deliveryTime;
154 }
155
GetDeliveryTime() const156 int64_t NotificationRequest::GetDeliveryTime() const
157 {
158 return deliveryTime_;
159 }
160
IsShowDeliveryTime() const161 bool NotificationRequest::IsShowDeliveryTime() const
162 {
163 return (deliveryTime_ != 0) && showDeliveryTime_;
164 }
165
SetShowDeliveryTime(bool showDeliveryTime)166 void NotificationRequest::SetShowDeliveryTime(bool showDeliveryTime)
167 {
168 showDeliveryTime_ = showDeliveryTime;
169 }
170
AddActionButton(const std::shared_ptr<NotificationActionButton> & actionButton)171 void NotificationRequest::AddActionButton(const std::shared_ptr<NotificationActionButton> &actionButton)
172 {
173 if (!actionButton) {
174 ANS_LOGW("actionButton can not be null");
175 return;
176 }
177
178 if (actionButtons_.size() >= NotificationRequest::MAX_ACTION_BUTTONS) {
179 ANS_LOGW("three action buttons have been already added");
180 return;
181 }
182
183 actionButtons_.emplace_back(actionButton);
184 }
185
GetActionButtons() const186 const std::vector<std::shared_ptr<NotificationActionButton>> NotificationRequest::GetActionButtons() const
187 {
188 return actionButtons_;
189 }
190
ClearActionButtons()191 void NotificationRequest::ClearActionButtons()
192 {
193 actionButtons_.clear();
194 }
195
IsPermitSystemGeneratedContextualActionButtons() const196 bool NotificationRequest::IsPermitSystemGeneratedContextualActionButtons() const
197 {
198 return permitted_;
199 }
200
SetPermitSystemGeneratedContextualActionButtons(bool permitted)201 void NotificationRequest::SetPermitSystemGeneratedContextualActionButtons(bool permitted)
202 {
203 permitted_ = permitted;
204 }
205
IsAgentNotification() const206 bool NotificationRequest::IsAgentNotification() const
207 {
208 return isAgent_;
209 }
210
SetIsAgentNotification(bool isAgent)211 void NotificationRequest::SetIsAgentNotification(bool isAgent)
212 {
213 isAgent_ = isAgent;
214 }
215
AddMessageUser(const std::shared_ptr<MessageUser> & messageUser)216 void NotificationRequest::AddMessageUser(const std::shared_ptr<MessageUser> &messageUser)
217 {
218 if (!messageUser) {
219 ANS_LOGI("messageUser can not be null");
220 return;
221 }
222
223 messageUsers_.emplace_back(messageUser);
224 }
225
GetMessageUsers() const226 const std::vector<std::shared_ptr<MessageUser>> NotificationRequest::GetMessageUsers() const
227 {
228 return messageUsers_;
229 }
230
IsAlertOneTime() const231 bool NotificationRequest::IsAlertOneTime() const
232 {
233 return alertOneTime_;
234 }
235
SetAlertOneTime(bool isAlertOnce)236 void NotificationRequest::SetAlertOneTime(bool isAlertOnce)
237 {
238 alertOneTime_ = isAlertOnce;
239 }
240
SetAutoDeletedTime(int64_t deletedTime)241 void NotificationRequest::SetAutoDeletedTime(int64_t deletedTime)
242 {
243 autoDeletedTime_ = deletedTime;
244 }
245
GetAutoDeletedTime() const246 int64_t NotificationRequest::GetAutoDeletedTime() const
247 {
248 return autoDeletedTime_;
249 }
250
SetLittleIcon(const std::shared_ptr<Media::PixelMap> & littleIcon)251 void NotificationRequest::SetLittleIcon(const std::shared_ptr<Media::PixelMap> &littleIcon)
252 {
253 littleIcon_ = littleIcon;
254 }
255
GetLittleIcon() const256 const std::shared_ptr<Media::PixelMap> NotificationRequest::GetLittleIcon() const
257 {
258 return littleIcon_;
259 }
260
SetBigIcon(const std::shared_ptr<Media::PixelMap> & bigIcon)261 void NotificationRequest::SetBigIcon(const std::shared_ptr<Media::PixelMap> &bigIcon)
262 {
263 bigIcon_ = bigIcon;
264 }
265
GetBigIcon() const266 const std::shared_ptr<Media::PixelMap> NotificationRequest::GetBigIcon() const
267 {
268 return bigIcon_;
269 }
270
SetClassification(const std::string & classification)271 void NotificationRequest::SetClassification(const std::string &classification)
272 {
273 classification_ = classification;
274 }
275
GetClassification() const276 std::string NotificationRequest::GetClassification() const
277 {
278 return classification_;
279 }
280
SetColor(uint32_t color)281 void NotificationRequest::SetColor(uint32_t color)
282 {
283 color_ = color;
284 if (NotificationRequest::COLOR_DEFAULT != color_) {
285 color_ = color_ | NotificationRequest::COLOR_MASK;
286 }
287 }
288
GetColor() const289 uint32_t NotificationRequest::GetColor() const
290 {
291 return color_;
292 }
293
IsColorEnabled() const294 bool NotificationRequest::IsColorEnabled() const
295 {
296 if (!colorEnabled_) {
297 return false;
298 }
299
300 // no valid content
301 if (!notificationContent_) {
302 ANS_LOGI("no valid notification content");
303 return false;
304 }
305
306 // not a media content
307 if (NotificationContent::Type::MEDIA != notificationContentType_) {
308 ANS_LOGI("not a media notification content");
309 return false;
310 }
311
312 auto basicContent = notificationContent_->GetNotificationContent();
313 auto mediaContent = std::static_pointer_cast<NotificationMediaContent>(basicContent);
314 if (!mediaContent->GetAVToken()) {
315 ANS_LOGI("AVToken has not been attached");
316 return false;
317 }
318
319 return true;
320 }
321
SetColorEnabled(bool colorEnabled)322 void NotificationRequest::SetColorEnabled(bool colorEnabled)
323 {
324 colorEnabled_ = colorEnabled;
325 }
326
SetContent(const std::shared_ptr<NotificationContent> & content)327 void NotificationRequest::SetContent(const std::shared_ptr<NotificationContent> &content)
328 {
329 notificationContent_ = content;
330
331 if (notificationContent_) {
332 notificationContentType_ = notificationContent_->GetContentType();
333 return;
334 }
335
336 notificationContentType_ = NotificationContent::Type::NONE;
337 }
338
GetContent() const339 const std::shared_ptr<NotificationContent> NotificationRequest::GetContent() const
340 {
341 return notificationContent_;
342 }
343
GetNotificationType() const344 NotificationContent::Type NotificationRequest::GetNotificationType() const
345 {
346 return notificationContentType_;
347 }
348
IsCountdownTimer() const349 bool NotificationRequest::IsCountdownTimer() const
350 {
351 return isCountdown_;
352 }
353
SetCountdownTimer(bool isCountDown)354 void NotificationRequest::SetCountdownTimer(bool isCountDown)
355 {
356 isCountdown_ = isCountDown;
357 }
358
SetGroupAlertType(NotificationRequest::GroupAlertType type)359 void NotificationRequest::SetGroupAlertType(NotificationRequest::GroupAlertType type)
360 {
361 groupAlertType_ = type;
362 }
363
GetGroupAlertType() const364 NotificationRequest::GroupAlertType NotificationRequest::GetGroupAlertType() const
365 {
366 return groupAlertType_;
367 }
368
IsGroupOverview() const369 bool NotificationRequest::IsGroupOverview() const
370 {
371 return groupOverview_;
372 }
373
SetGroupOverview(bool overView)374 void NotificationRequest::SetGroupOverview(bool overView)
375 {
376 groupOverview_ = overView;
377 }
378
SetGroupName(const std::string & groupName)379 void NotificationRequest::SetGroupName(const std::string &groupName)
380 {
381 groupName_ = groupName;
382 }
383
GetGroupName() const384 std::string NotificationRequest::GetGroupName() const
385 {
386 return groupName_;
387 }
388
IsOnlyLocal() const389 bool NotificationRequest::IsOnlyLocal() const
390 {
391 return onlyLocal_;
392 }
393
SetOnlyLocal(bool flag)394 void NotificationRequest::SetOnlyLocal(bool flag)
395 {
396 onlyLocal_ = flag;
397 }
398
SetSettingsText(const std::string & text)399 void NotificationRequest::SetSettingsText(const std::string &text)
400 {
401 if ((NotificationContent::Type::LONG_TEXT == notificationContentType_) ||
402 (NotificationContent::Type::PICTURE == notificationContentType_)) {
403 ANS_LOGW("This method is invalid if the notification content type has been set to LONG_TEXT or PICTURE.");
404 return;
405 }
406
407 settingsText_ = text;
408 }
409
GetSettingsText() const410 std::string NotificationRequest::GetSettingsText() const
411 {
412 return settingsText_;
413 }
414
GetCreateTime() const415 int64_t NotificationRequest::GetCreateTime() const
416 {
417 return createTime_;
418 }
419
IsShowStopwatch() const420 bool NotificationRequest::IsShowStopwatch() const
421 {
422 return showStopwatch_;
423 }
424
SetShowStopwatch(bool isShow)425 void NotificationRequest::SetShowStopwatch(bool isShow)
426 {
427 showStopwatch_ = isShow;
428 }
429
SetSlotType(NotificationConstant::SlotType slotType)430 void NotificationRequest::SetSlotType(NotificationConstant::SlotType slotType)
431 {
432 slotType_ = slotType;
433 }
434
GetSlotType() const435 NotificationConstant::SlotType NotificationRequest::GetSlotType() const
436 {
437 return slotType_;
438 }
439
SetSortingKey(const std::string & key)440 void NotificationRequest::SetSortingKey(const std::string &key)
441 {
442 sortingKey_ = key;
443 }
444
GetSortingKey() const445 std::string NotificationRequest::GetSortingKey() const
446 {
447 return sortingKey_;
448 }
449
SetStatusBarText(const std::string & text)450 void NotificationRequest::SetStatusBarText(const std::string &text)
451 {
452 statusBarText_ = text;
453 }
454
GetStatusBarText() const455 std::string NotificationRequest::GetStatusBarText() const
456 {
457 return statusBarText_;
458 }
459
IsTapDismissed() const460 bool NotificationRequest::IsTapDismissed() const
461 {
462 return tapDismissed_;
463 }
464
SetTapDismissed(bool isDismissed)465 void NotificationRequest::SetTapDismissed(bool isDismissed)
466 {
467 tapDismissed_ = isDismissed;
468 }
469
SetVisibleness(NotificationConstant::VisiblenessType type)470 void NotificationRequest::SetVisibleness(NotificationConstant::VisiblenessType type)
471 {
472 visiblenessType_ = type;
473 }
474
GetVisibleness() const475 NotificationConstant::VisiblenessType NotificationRequest::GetVisibleness() const
476 {
477 return visiblenessType_;
478 }
479
SetBadgeIconStyle(NotificationRequest::BadgeStyle style)480 void NotificationRequest::SetBadgeIconStyle(NotificationRequest::BadgeStyle style)
481 {
482 badgeStyle_ = style;
483 }
484
GetBadgeIconStyle() const485 NotificationRequest::BadgeStyle NotificationRequest::GetBadgeIconStyle() const
486 {
487 return badgeStyle_;
488 }
489
SetShortcutId(const std::string & shortcutId)490 void NotificationRequest::SetShortcutId(const std::string &shortcutId)
491 {
492 shortcutId_ = shortcutId;
493 }
494
GetShortcutId() const495 std::string NotificationRequest::GetShortcutId() const
496 {
497 return shortcutId_;
498 }
499
SetFloatingIcon(bool floatingIcon)500 void NotificationRequest::SetFloatingIcon(bool floatingIcon)
501 {
502 floatingIcon_ = floatingIcon;
503 }
504
IsFloatingIcon() const505 bool NotificationRequest::IsFloatingIcon() const
506 {
507 return floatingIcon_;
508 }
509
SetProgressBar(int32_t progress,int32_t progressMax,bool indeterminate)510 void NotificationRequest::SetProgressBar(int32_t progress, int32_t progressMax, bool indeterminate)
511 {
512 progressValue_ = progress;
513 progressMax_ = progressMax;
514 progressIndeterminate_ = indeterminate;
515 }
516
GetProgressMax() const517 int32_t NotificationRequest::GetProgressMax() const
518 {
519 return progressMax_;
520 }
521
GetProgressValue() const522 int32_t NotificationRequest::GetProgressValue() const
523 {
524 return progressValue_;
525 }
526
IsProgressIndeterminate() const527 bool NotificationRequest::IsProgressIndeterminate() const
528 {
529 return progressIndeterminate_;
530 }
531
SetNotificationUserInputHistory(const std::vector<std::string> & text)532 void NotificationRequest::SetNotificationUserInputHistory(const std::vector<std::string> &text)
533 {
534 if (text.empty()) {
535 userInputHistory_.clear();
536 return;
537 }
538
539 auto vsize = std::min(NotificationRequest::MAX_USER_INPUT_HISTORY, text.size());
540 userInputHistory_.assign(text.begin(), text.begin() + vsize);
541 }
542
GetNotificationUserInputHistory() const543 std::vector<std::string> NotificationRequest::GetNotificationUserInputHistory() const
544 {
545 return userInputHistory_;
546 }
547
GetNotificationHashCode() const548 std::string NotificationRequest::GetNotificationHashCode() const
549 {
550 if (creatorBundleName_.empty() || (creatorUid_ == 0) || ownerBundleName_.empty()) {
551 return "";
552 }
553
554 return std::to_string(notificationId_) + "_" + creatorBundleName_ + "_" + std::to_string(creatorUid_) + "_" +
555 ownerBundleName_;
556 }
557
SetOwnerBundleName(const std::string & ownerName)558 void NotificationRequest::SetOwnerBundleName(const std::string &ownerName)
559 {
560 ownerBundleName_ = ownerName;
561 }
562
GetOwnerBundleName() const563 std::string NotificationRequest::GetOwnerBundleName() const
564 {
565 return ownerBundleName_;
566 }
567
SetCreatorBundleName(const std::string & creatorName)568 void NotificationRequest::SetCreatorBundleName(const std::string &creatorName)
569 {
570 creatorBundleName_ = creatorName;
571 }
572
GetCreatorBundleName() const573 std::string NotificationRequest::GetCreatorBundleName() const
574 {
575 return creatorBundleName_;
576 }
577
SetCreatorPid(pid_t pid)578 void NotificationRequest::SetCreatorPid(pid_t pid)
579 {
580 creatorPid_ = pid;
581 }
582
GetCreatorPid() const583 pid_t NotificationRequest::GetCreatorPid() const
584 {
585 return creatorPid_;
586 }
587
SetCreatorUid(int32_t uid)588 void NotificationRequest::SetCreatorUid(int32_t uid)
589 {
590 creatorUid_ = uid;
591 }
592
GetCreatorUid() const593 int32_t NotificationRequest::GetCreatorUid() const
594 {
595 return creatorUid_;
596 }
597
SetOwnerUid(int32_t uid)598 void NotificationRequest::SetOwnerUid(int32_t uid)
599 {
600 ownerUid_ = uid;
601 }
602
GetOwnerUid() const603 int32_t NotificationRequest::GetOwnerUid() const
604 {
605 return ownerUid_;
606 }
607
SetLabel(const std::string & label)608 void NotificationRequest::SetLabel(const std::string &label)
609 {
610 label_ = label;
611 }
612
GetLabel() const613 std::string NotificationRequest::GetLabel() const
614 {
615 return label_;
616 }
617
SetDistributed(bool distribute)618 void NotificationRequest::SetDistributed(bool distribute)
619 {
620 distributedOptions_.SetDistributed(distribute);
621 }
622
SetDevicesSupportDisplay(const std::vector<std::string> & devices)623 void NotificationRequest::SetDevicesSupportDisplay(const std::vector<std::string> &devices)
624 {
625 distributedOptions_.SetDevicesSupportDisplay(devices);
626 }
627
SetDevicesSupportOperate(const std::vector<std::string> & devices)628 void NotificationRequest::SetDevicesSupportOperate(const std::vector<std::string> &devices)
629 {
630 distributedOptions_.SetDevicesSupportOperate(devices);
631 }
632
GetNotificationDistributedOptions() const633 NotificationDistributedOptions NotificationRequest::GetNotificationDistributedOptions() const
634 {
635 return distributedOptions_;
636 }
637
SetCreatorUserId(int32_t userId)638 void NotificationRequest::SetCreatorUserId(int32_t userId)
639 {
640 creatorUserId_ = userId;
641 }
642
GetCreatorUserId() const643 int32_t NotificationRequest::GetCreatorUserId() const
644 {
645 return creatorUserId_;
646 }
647
SetOwnerUserId(int32_t userId)648 void NotificationRequest::SetOwnerUserId(int32_t userId)
649 {
650 ownerUserId_ = userId;
651 }
652
GetOwnerUserId() const653 int32_t NotificationRequest::GetOwnerUserId() const
654 {
655 return ownerUserId_;
656 }
657
Dump()658 std::string NotificationRequest::Dump()
659 {
660 return "NotificationRequest{ "
661 "notificationId = " + std::to_string(notificationId_) +
662 ", slotType = " + std::to_string(static_cast<int32_t>(slotType_)) +
663 ", createTime = " + std::to_string(createTime_) + ", deliveryTime = " + std::to_string(deliveryTime_) +
664 ", autoDeletedTime = " + std::to_string(autoDeletedTime_) + ", settingsText = " + settingsText_ +
665 ", creatorBundleName = " + creatorBundleName_ +
666 ", creatorPid = " + std::to_string(static_cast<int32_t>(creatorPid_)) +
667 ", creatorUid = " + std::to_string(static_cast<int32_t>(creatorUid_)) +
668 ", ownerBundleName = " + ownerBundleName_ +
669 ", ownerUid = " + std::to_string(static_cast<int32_t>(ownerUid_)) +
670 ", groupName = " + groupName_ +
671 ", statusBarText = " + statusBarText_ + ", label = " + label_ + ", shortcutId = " + shortcutId_ +
672 ", sortingKey = " + sortingKey_ +
673 ", groupAlertType = " + std::to_string(static_cast<int32_t>(groupAlertType_)) +
674 ", color = " + std::to_string(color_) + ", badgeNumber = " + std::to_string(badgeNumber_) +
675 ", visiblenessType = " + std::to_string(static_cast<int32_t>(visiblenessType_)) +
676 ", progressValue = " + std::to_string(progressValue_) + ", progressMax = " + std::to_string(progressMax_) +
677 ", badgeStyle = " + std::to_string(static_cast<int32_t>(badgeStyle_)) +
678 ", classification = " + classification_ +
679 ", notificationContentType = " + std::to_string(static_cast<int32_t>(notificationContentType_)) +
680 ", showDeliveryTime = " + (showDeliveryTime_ ? "true" : "false") +
681 ", tapDismissed = " + (tapDismissed_ ? "true" : "false") +
682 ", colorEnabled = " + (colorEnabled_ ? "true" : "false") +
683 ", alertOneTime = " + (alertOneTime_ ? "true" : "false") +
684 ", showStopwatch = " + (showStopwatch_ ? "true" : "false") +
685 ", isCountdown = " + (isCountdown_ ? "true" : "false") +
686 ", inProgress = " + (inProgress_ ? "true" : "false") +
687 ", groupOverview = " + (groupOverview_ ? "true" : "false") +
688 ", progressIndeterminate = " + (progressIndeterminate_ ? "true" : "false") +
689 ", unremovable = " + (unremovable_ ? "true" : "false") +
690 ", floatingIcon = " + (floatingIcon_ ? "true" : "false") +
691 ", onlyLocal = " + (onlyLocal_ ? "true" : "false") + ", permitted = " + (permitted_ ? "true" : "false") +
692 ", isAgent = " + (isAgent_ ? "true" : "false") +
693 ", removalWantAgent = " + (removalWantAgent_ ? "not null" : "null") +
694 ", maxScreenWantAgent = " + (maxScreenWantAgent_ ? "not null" : "null") +
695 ", additionalParams = " + (additionalParams_ ? "not null" : "null") +
696 ", littleIcon = " + (littleIcon_ ? "not null" : "null") +
697 ", bigIcon = " + (bigIcon_ ? "not null" : "null") +
698 ", notificationContent = " + (notificationContent_ ? notificationContent_->Dump() : "null") +
699 ", notificationTemplate = " + (notificationTemplate_ ? "not null" : "null") +
700 ", actionButtons = " + (!actionButtons_.empty() ? actionButtons_.at(0)->Dump() : "empty") +
701 ", messageUsers = " + (!messageUsers_.empty() ? messageUsers_.at(0)->Dump() : "empty") +
702 ", userInputHistory = " + (!userInputHistory_.empty() ? userInputHistory_.at(0) : "empty") +
703 ", distributedOptions = " + distributedOptions_.Dump() +
704 ", notificationFlags = " + (notificationFlags_ ? "not null" : "null") +
705 ", creatorUserId = " + std::to_string(creatorUserId_) +
706 ", ownerUserId = " + std::to_string(ownerUserId_) +
707 ", receiverUserId = " + std::to_string(receiverUserId_) +
708 " }";
709 }
710
ToJson(nlohmann::json & jsonObject) const711 bool NotificationRequest::ToJson(nlohmann::json &jsonObject) const
712 {
713 jsonObject["version"] = 1;
714
715 jsonObject["id"] = notificationId_;
716 jsonObject["color"] = color_;
717 jsonObject["deliveryTime"] = deliveryTime_;
718 jsonObject["autoDeletedTime"] = autoDeletedTime_;
719
720 jsonObject["creatorBundleName"] = creatorBundleName_;
721 jsonObject["ownerBundleName"] = ownerBundleName_;
722 jsonObject["groupName"] = groupName_;
723 jsonObject["label"] = label_;
724 jsonObject["classification"] = classification_;
725
726 jsonObject["slotType"] = static_cast<int32_t>(slotType_);
727 jsonObject["badgeIconStyle"] = static_cast<int32_t>(badgeStyle_);
728
729 jsonObject["showDeliveryTime"] = showDeliveryTime_;
730 jsonObject["tapDismissed"] = tapDismissed_;
731 jsonObject["colorEnabled"] = colorEnabled_;
732 jsonObject["isOngoing"] = inProgress_;
733 jsonObject["isAlertOnce"] = alertOneTime_;
734 jsonObject["isStopwatch"] = showStopwatch_;
735 jsonObject["isCountdown"] = isCountdown_;
736 jsonObject["isUnremovable"] = unremovable_;
737 jsonObject["isFloatingIcon"] = floatingIcon_;
738
739 jsonObject["creatorBundleName"] = creatorBundleName_;
740 jsonObject["creatorUid"] = creatorUid_;
741 jsonObject["creatorPid"] = creatorPid_;
742 jsonObject["creatorUserId"] = creatorUserId_;
743 jsonObject["receiverUserId"] = receiverUserId_;
744
745 if (!ConvertObjectsToJson(jsonObject)) {
746 ANS_LOGE("Cannot convert objects to JSON");
747 return false;
748 }
749
750 return true;
751 }
752
FromJson(const nlohmann::json & jsonObject)753 NotificationRequest *NotificationRequest::FromJson(const nlohmann::json &jsonObject)
754 {
755 if (jsonObject.is_null() or !jsonObject.is_object()) {
756 ANS_LOGE("Invalid JSON object");
757 return nullptr;
758 }
759
760 auto pRequest = new (std::nothrow) NotificationRequest();
761 if (pRequest == nullptr) {
762 ANS_LOGE("Failed to create request instance");
763 return nullptr;
764 }
765
766 const auto &jsonEnd = jsonObject.cend();
767 if (jsonObject.find("version") != jsonEnd) {
768 jsonObject.at("version").get<int32_t>();
769 }
770
771 ConvertJsonToNum(pRequest, jsonObject);
772
773 ConvertJsonToString(pRequest, jsonObject);
774
775 ConvertJsonToEnum(pRequest, jsonObject);
776
777 ConvertJsonToBool(pRequest, jsonObject);
778
779 if (jsonObject.find("wantAgent") != jsonEnd) {
780 auto wantAgentValue = jsonObject.at("wantAgent").get<std::string>();
781 pRequest->wantAgent_ = AbilityRuntime::WantAgent::WantAgentHelper::FromString(wantAgentValue);
782 }
783
784 if (!ConvertJsonToNotificationContent(pRequest, jsonObject)) {
785 delete pRequest;
786 pRequest = nullptr;
787 return nullptr;
788 }
789
790 if (!ConvertJsonToNotificationActionButton(pRequest, jsonObject)) {
791 delete pRequest;
792 pRequest = nullptr;
793 return nullptr;
794 }
795
796 if (jsonObject.find("extraInfo") != jsonEnd) {
797 auto extraInfoStr = jsonObject.at("extraInfo").get<std::string>();
798 if (!extraInfoStr.empty()) {
799 AAFwk::WantParams params = AAFwk::WantParamWrapper::ParseWantParams(extraInfoStr);
800 pRequest->additionalParams_ = std::make_shared<AAFwk::WantParams>(params);
801 }
802 }
803
804 ConvertJsonToPixelMap(pRequest, jsonObject);
805
806 if (!ConvertJsonToNotificationDistributedOptions(pRequest, jsonObject)) {
807 delete pRequest;
808 pRequest = nullptr;
809 return nullptr;
810 }
811
812 if (!ConvertJsonToNotificationFlags(pRequest, jsonObject)) {
813 delete pRequest;
814 pRequest = nullptr;
815 return nullptr;
816 }
817
818 return pRequest;
819 }
820
Marshalling(Parcel & parcel) const821 bool NotificationRequest::Marshalling(Parcel &parcel) const
822 {
823 // write int
824 if (!parcel.WriteInt32(notificationId_)) {
825 ANS_LOGE("Failed to write notification Id");
826 return false;
827 }
828
829 if (!parcel.WriteUint32(color_)) {
830 ANS_LOGE("Failed to write color");
831 return false;
832 }
833
834 if (!parcel.WriteUint32(badgeNumber_)) {
835 ANS_LOGE("Failed to write badge number");
836 return false;
837 }
838
839 if (!parcel.WriteInt32(progressValue_)) {
840 ANS_LOGE("Failed to write progress value");
841 return false;
842 }
843
844 if (!parcel.WriteInt32(progressMax_)) {
845 ANS_LOGE("Failed to write progress max");
846 return false;
847 }
848
849 if (!parcel.WriteInt64(createTime_)) {
850 ANS_LOGE("Failed to write create time");
851 return false;
852 }
853
854 if (!parcel.WriteInt64(deliveryTime_)) {
855 ANS_LOGE("Failed to write delivery time");
856 return false;
857 }
858
859 if (!parcel.WriteInt64(autoDeletedTime_)) {
860 ANS_LOGE("Failed to write auto deleted time");
861 return false;
862 }
863
864 if (!parcel.WriteInt32(static_cast<int32_t>(creatorPid_))) {
865 ANS_LOGE("Failed to write creator pid");
866 return false;
867 }
868
869 if (!parcel.WriteInt32(static_cast<int32_t>(creatorUid_))) {
870 ANS_LOGE("Failed to write creator uid");
871 return false;
872 }
873
874 if (!parcel.WriteInt32(static_cast<int32_t>(ownerUid_))) {
875 ANS_LOGE("Failed to write owner uid");
876 return false;
877 }
878
879 if (!parcel.WriteInt32(static_cast<int32_t>(creatorUserId_))) {
880 ANS_LOGE("Failed to write creator userId");
881 return false;
882 }
883
884 if (!parcel.WriteInt32(static_cast<int32_t>(ownerUserId_))) {
885 ANS_LOGE("Failed to write owner userId");
886 return false;
887 }
888
889 if (!parcel.WriteInt32(static_cast<int32_t>(receiverUserId_))) {
890 ANS_LOGE("Failed to write receiver userId");
891 return false;
892 }
893
894 // write std::string
895 if (!parcel.WriteString(settingsText_)) {
896 ANS_LOGE("Failed to write settings text");
897 return false;
898 }
899
900 if (!parcel.WriteString(creatorBundleName_)) {
901 ANS_LOGE("Failed to write creator bundle name");
902 return false;
903 }
904
905 if (!parcel.WriteString(ownerBundleName_)) {
906 ANS_LOGE("Failed to write owner bundle name");
907 return false;
908 }
909
910 if (!parcel.WriteString(groupName_)) {
911 ANS_LOGE("Failed to write group name");
912 return false;
913 }
914
915 if (!parcel.WriteString(statusBarText_)) {
916 ANS_LOGE("Failed to write status bar text");
917 return false;
918 }
919
920 if (!parcel.WriteString(label_)) {
921 ANS_LOGE("Failed to write label");
922 return false;
923 }
924
925 if (!parcel.WriteString(shortcutId_)) {
926 ANS_LOGE("Failed to write shortcut Id");
927 return false;
928 }
929
930 if (!parcel.WriteString(sortingKey_)) {
931 ANS_LOGE("Failed to write sorting key");
932 return false;
933 }
934
935 if (!parcel.WriteString(classification_)) {
936 ANS_LOGE("Failed to write classification");
937 return false;
938 }
939
940 // write enum
941 if (!parcel.WriteInt32(static_cast<int32_t>(slotType_))) {
942 ANS_LOGE("Failed to write slot type");
943 return false;
944 }
945
946 if (!parcel.WriteInt32(static_cast<int32_t>(groupAlertType_))) {
947 ANS_LOGE("Failed to write group alert type");
948 return false;
949 }
950
951 if (!parcel.WriteInt32(static_cast<int32_t>(visiblenessType_))) {
952 ANS_LOGE("Failed to write visibleness type");
953 return false;
954 }
955
956 if (!parcel.WriteInt32(static_cast<int32_t>(badgeStyle_))) {
957 ANS_LOGE("Failed to write badge type");
958 return false;
959 }
960
961 if (!parcel.WriteInt32(static_cast<int32_t>(notificationContentType_))) {
962 ANS_LOGE("Failed to write notification content type");
963 return false;
964 }
965
966 // write bool
967 if (!parcel.WriteBool(showDeliveryTime_)) {
968 ANS_LOGE("Failed to write flag indicating whether to show delivery time");
969 return false;
970 }
971
972 if (!parcel.WriteBool(tapDismissed_)) {
973 ANS_LOGE("Failed to write flag tap dismissed");
974 return false;
975 }
976
977 if (!parcel.WriteBool(colorEnabled_)) {
978 ANS_LOGE("Failed to write flag indicating whether to enable background color");
979 return false;
980 }
981
982 if (!parcel.WriteBool(alertOneTime_)) {
983 ANS_LOGE("Failed to write flag indicating whether to have this notification alert only once");
984 return false;
985 }
986
987 if (!parcel.WriteBool(showStopwatch_)) {
988 ANS_LOGE("Failed to write flag show stop watch");
989 return false;
990 }
991
992 if (!parcel.WriteBool(isCountdown_)) {
993 ANS_LOGE("Failed to write flag indicating whether to show the notification creation time as a countdown timer");
994 return false;
995 }
996
997 if (!parcel.WriteBool(inProgress_)) {
998 ANS_LOGE("Failed to write flag indicating whether in progress");
999 return false;
1000 }
1001
1002 if (!parcel.WriteBool(groupOverview_)) {
1003 ANS_LOGE("Failed to write flag indicating whether to use this notification as the overview of its group");
1004 return false;
1005 }
1006
1007 if (!parcel.WriteBool(progressIndeterminate_)) {
1008 ANS_LOGE("Failed to write progress indeterminate");
1009 return false;
1010 }
1011
1012 if (!parcel.WriteBool(unremovable_)) {
1013 ANS_LOGE("Failed to write flag indicating whether unremovable");
1014 return false;
1015 }
1016
1017 if (!parcel.WriteBool(floatingIcon_)) {
1018 ANS_LOGE("Failed to write flag floating icon");
1019 return false;
1020 }
1021
1022 if (!parcel.WriteBool(onlyLocal_)) {
1023 ANS_LOGE("Failed to write flag only local");
1024 return false;
1025 }
1026
1027 if (!parcel.WriteBool(permitted_)) {
1028 ANS_LOGE("Failed to write flag indicating whether to allow the platform to \
1029 generate contextual NotificationActionButton objects");
1030 return false;
1031 }
1032
1033 if (!parcel.WriteBool(isAgent_)) {
1034 ANS_LOGE("Failed to write flag indicating whether an agent notification");
1035 return false;
1036 }
1037
1038 // write objects which managed by std::shared_ptr
1039 bool valid {false};
1040
1041 valid = wantAgent_ ? true : false;
1042 if (!parcel.WriteBool(valid)) {
1043 ANS_LOGE("Failed to write the flag which indicate whether wantAgent is null");
1044 return false;
1045 }
1046
1047 if (valid) {
1048 if (!parcel.WriteParcelable(wantAgent_.get())) {
1049 ANS_LOGE("Failed to write wantAgent");
1050 return false;
1051 }
1052 }
1053
1054 valid = removalWantAgent_ ? true : false;
1055 if (!parcel.WriteBool(valid)) {
1056 ANS_LOGE("Failed to write the flag which indicate whether removalWantAgent is null");
1057 return false;
1058 }
1059
1060 if (valid) {
1061 if (!parcel.WriteParcelable(removalWantAgent_.get())) {
1062 ANS_LOGE("Failed to write removalWantAgent");
1063 return false;
1064 }
1065 }
1066
1067 valid = maxScreenWantAgent_ ? true : false;
1068 if (!parcel.WriteBool(valid)) {
1069 ANS_LOGE("Failed to write the flag which indicate whether maxScreenWantAgent is null");
1070 return false;
1071 }
1072
1073 if (valid) {
1074 if (!parcel.WriteParcelable(maxScreenWantAgent_.get())) {
1075 ANS_LOGE("Failed to write maxScreenWantAgent");
1076 return false;
1077 }
1078 }
1079
1080 valid = additionalParams_ ? true : false;
1081 if (!parcel.WriteBool(valid)) {
1082 ANS_LOGE("Failed to write the flag which indicate whether additionalParams is null");
1083 return false;
1084 }
1085
1086 if (valid) {
1087 if (!parcel.WriteParcelable(additionalParams_.get())) {
1088 ANS_LOGE("Failed to write additionalParams");
1089 return false;
1090 }
1091 }
1092
1093 valid = littleIcon_ ? true : false;
1094 if (!parcel.WriteBool(valid)) {
1095 ANS_LOGE("Failed to write the flag which indicate whether littleIcon is null");
1096 return false;
1097 }
1098
1099 if (valid) {
1100 if (!parcel.WriteParcelable(littleIcon_.get())) {
1101 ANS_LOGE("Failed to write littleIcon");
1102 return false;
1103 }
1104 }
1105
1106 valid = bigIcon_ ? true : false;
1107 if (!parcel.WriteBool(valid)) {
1108 ANS_LOGE("Failed to write the flag which indicate whether bigIcon is null");
1109 return false;
1110 }
1111
1112 if (valid) {
1113 if (!parcel.WriteParcelable(bigIcon_.get())) {
1114 ANS_LOGE("Failed to write bigIcon");
1115 return false;
1116 }
1117 }
1118
1119 valid = notificationContent_ ? true : false;
1120 if (!parcel.WriteBool(valid)) {
1121 ANS_LOGE("Failed to write the flag which indicate whether notificationContent is null");
1122 return false;
1123 }
1124
1125 if (valid) {
1126 if (!parcel.WriteParcelable(notificationContent_.get())) {
1127 ANS_LOGE("Failed to write notificationContent");
1128 return false;
1129 }
1130 }
1131
1132 // write std::vector
1133 if (!parcel.WriteUint64(actionButtons_.size())) {
1134 ANS_LOGE("Failed to write the size of actionButtons");
1135 return false;
1136 }
1137
1138 for (auto it = actionButtons_.begin(); it != actionButtons_.end(); ++it) {
1139 if (!parcel.WriteParcelable(it->get())) {
1140 ANS_LOGE("Failed to write actionButton");
1141 return false;
1142 }
1143 }
1144
1145 if (!parcel.WriteUint64(messageUsers_.size())) {
1146 ANS_LOGE("Failed to write the size of messageUsers");
1147 return false;
1148 }
1149
1150 for (auto it = messageUsers_.begin(); it != messageUsers_.end(); ++it) {
1151 if (!parcel.WriteParcelable(it->get())) {
1152 ANS_LOGE("Failed to write messageUser");
1153 return false;
1154 }
1155 }
1156
1157 if (!parcel.WriteStringVector(userInputHistory_)) {
1158 ANS_LOGE("Failed to write userInputHistory");
1159 return false;
1160 }
1161
1162 if (!parcel.WriteParcelable(&distributedOptions_)) {
1163 ANS_LOGE("Failed to write distributedOptions");
1164 return false;
1165 }
1166
1167 valid = notificationTemplate_ ? true : false;
1168 if (!parcel.WriteBool(valid)) {
1169 ANS_LOGE("Failed to write the flag which indicate whether publicNotification is null");
1170 return false;
1171 }
1172
1173 if (valid) {
1174 if (!parcel.WriteParcelable(notificationTemplate_.get())) {
1175 ANS_LOGE("Failed to write notificationTemplate");
1176 return false;
1177 }
1178 }
1179
1180 valid = notificationFlags_ ? true : false;
1181 if (!parcel.WriteBool(valid)) {
1182 ANS_LOGE("Failed to write flags for the notification");
1183 return false;
1184 }
1185
1186 if (valid) {
1187 if (!parcel.WriteParcelable(notificationFlags_.get())) {
1188 ANS_LOGE("Failed to write notification flags");
1189 return false;
1190 }
1191 }
1192
1193 return true;
1194 }
1195
Unmarshalling(Parcel & parcel)1196 NotificationRequest *NotificationRequest::Unmarshalling(Parcel &parcel)
1197 {
1198 auto objptr = new (std::nothrow) NotificationRequest();
1199 if ((objptr != nullptr) && !objptr->ReadFromParcel(parcel)) {
1200 delete objptr;
1201 objptr = nullptr;
1202 }
1203
1204 return objptr;
1205 }
1206
ReadFromParcel(Parcel & parcel)1207 bool NotificationRequest::ReadFromParcel(Parcel &parcel)
1208 {
1209 notificationId_ = parcel.ReadInt32();
1210 color_ = parcel.ReadUint32();
1211 badgeNumber_ = parcel.ReadUint32();
1212 progressValue_ = parcel.ReadInt32();
1213 progressMax_ = parcel.ReadInt32();
1214 createTime_ = parcel.ReadInt64();
1215 deliveryTime_ = parcel.ReadInt64();
1216 autoDeletedTime_ = parcel.ReadInt64();
1217
1218 creatorPid_ = static_cast<pid_t>(parcel.ReadInt32());
1219 creatorUid_ = parcel.ReadInt32();
1220 ownerUid_ = parcel.ReadInt32();
1221 creatorUserId_ = parcel.ReadInt32();
1222 ownerUserId_ = parcel.ReadInt32();
1223 receiverUserId_ = parcel.ReadInt32();
1224
1225 if (!parcel.ReadString(settingsText_)) {
1226 ANS_LOGE("Failed to read settings text");
1227 return false;
1228 }
1229
1230 if (!parcel.ReadString(creatorBundleName_)) {
1231 ANS_LOGE("Failed to read creator bundle name");
1232 return false;
1233 }
1234
1235 if (!parcel.ReadString(ownerBundleName_)) {
1236 ANS_LOGE("Failed to read owner bundle name");
1237 return false;
1238 }
1239
1240 if (!parcel.ReadString(groupName_)) {
1241 ANS_LOGE("Failed to read group name");
1242 return false;
1243 }
1244
1245 if (!parcel.ReadString(statusBarText_)) {
1246 ANS_LOGE("Failed to read status bar text");
1247 return false;
1248 }
1249
1250 if (!parcel.ReadString(label_)) {
1251 ANS_LOGE("Failed to read label");
1252 return false;
1253 }
1254
1255 if (!parcel.ReadString(shortcutId_)) {
1256 ANS_LOGE("Failed to read shortcut Id");
1257 return false;
1258 }
1259
1260 if (!parcel.ReadString(sortingKey_)) {
1261 ANS_LOGE("Failed to read sorting key");
1262 return false;
1263 }
1264
1265 if (!parcel.ReadString(classification_)) {
1266 ANS_LOGE("Failed to read classification");
1267 return false;
1268 }
1269
1270 slotType_ = static_cast<NotificationConstant::SlotType>(parcel.ReadInt32());
1271 groupAlertType_ = static_cast<NotificationRequest::GroupAlertType>(parcel.ReadInt32());
1272 visiblenessType_ = static_cast<NotificationConstant::VisiblenessType>(parcel.ReadInt32());
1273 badgeStyle_ = static_cast<NotificationRequest::BadgeStyle>(parcel.ReadInt32());
1274 notificationContentType_ = static_cast<NotificationContent::Type>(parcel.ReadInt32());
1275
1276 showDeliveryTime_ = parcel.ReadBool();
1277 tapDismissed_ = parcel.ReadBool();
1278 colorEnabled_ = parcel.ReadBool();
1279 alertOneTime_ = parcel.ReadBool();
1280 showStopwatch_ = parcel.ReadBool();
1281 isCountdown_ = parcel.ReadBool();
1282 inProgress_ = parcel.ReadBool();
1283 groupOverview_ = parcel.ReadBool();
1284 progressIndeterminate_ = parcel.ReadBool();
1285 unremovable_ = parcel.ReadBool();
1286 floatingIcon_ = parcel.ReadBool();
1287 onlyLocal_ = parcel.ReadBool();
1288 permitted_ = parcel.ReadBool();
1289 isAgent_ = parcel.ReadBool();
1290
1291 bool valid {false};
1292
1293 valid = parcel.ReadBool();
1294 if (valid) {
1295 wantAgent_ = std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>(
1296 parcel.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>());
1297 if (!wantAgent_) {
1298 ANS_LOGE("Failed to read wantAgent");
1299 return false;
1300 }
1301 }
1302
1303 valid = parcel.ReadBool();
1304 if (valid) {
1305 removalWantAgent_ = std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>(
1306 parcel.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>());
1307 if (!removalWantAgent_) {
1308 ANS_LOGE("Failed to read removalWantAgent");
1309 return false;
1310 }
1311 }
1312
1313 valid = parcel.ReadBool();
1314 if (valid) {
1315 maxScreenWantAgent_ = std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>(
1316 parcel.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>());
1317 if (!maxScreenWantAgent_) {
1318 ANS_LOGE("Failed to read maxScreenWantAgent");
1319 return false;
1320 }
1321 }
1322
1323 valid = parcel.ReadBool();
1324 if (valid) {
1325 additionalParams_ = std::shared_ptr<AAFwk::WantParams>(parcel.ReadParcelable<AAFwk::WantParams>());
1326 if (!additionalParams_) {
1327 ANS_LOGE("Failed to read additionalParams");
1328 return false;
1329 }
1330 }
1331
1332 valid = parcel.ReadBool();
1333 if (valid) {
1334 littleIcon_ = std::shared_ptr<Media::PixelMap>(parcel.ReadParcelable<Media::PixelMap>());
1335 if (!littleIcon_) {
1336 ANS_LOGE("Failed to read littleIcon");
1337 return false;
1338 }
1339 }
1340
1341 valid = parcel.ReadBool();
1342 if (valid) {
1343 bigIcon_ = std::shared_ptr<Media::PixelMap>(parcel.ReadParcelable<Media::PixelMap>());
1344 if (!bigIcon_) {
1345 ANS_LOGE("Failed to read bigIcon");
1346 return false;
1347 }
1348 }
1349
1350 valid = parcel.ReadBool();
1351 if (valid) {
1352 notificationContent_ = std::shared_ptr<NotificationContent>(parcel.ReadParcelable<NotificationContent>());
1353 if (!notificationContent_) {
1354 ANS_LOGE("Failed to read notificationContent");
1355 return false;
1356 }
1357 }
1358
1359 auto vsize = parcel.ReadUint64();
1360 for (uint64_t it = 0; it < vsize; ++it) {
1361 auto member = parcel.ReadParcelable<NotificationActionButton>();
1362 if (member == nullptr) {
1363 ANS_LOGE("Failed to read actionButton");
1364 return false;
1365 }
1366
1367 actionButtons_.emplace_back(member);
1368 }
1369
1370 vsize = parcel.ReadUint64();
1371 for (uint64_t it = 0; it < vsize; ++it) {
1372 auto member = parcel.ReadParcelable<MessageUser>();
1373 if (member == nullptr) {
1374 ANS_LOGE("Failed to read messageUser");
1375 return false;
1376 }
1377
1378 messageUsers_.emplace_back(member);
1379 }
1380
1381 if (!parcel.ReadStringVector(&userInputHistory_)) {
1382 ANS_LOGE("Failed to read userInputHistory");
1383 return false;
1384 }
1385
1386 auto pOpt = parcel.ReadParcelable<NotificationDistributedOptions>();
1387 if (pOpt == nullptr) {
1388 ANS_LOGE("Failed to read distributedOptions");
1389 return false;
1390 }
1391 distributedOptions_ = *pOpt;
1392
1393 valid = parcel.ReadBool();
1394 if (valid) {
1395 notificationTemplate_ = std::shared_ptr<NotificationTemplate>(parcel.ReadParcelable<NotificationTemplate>());
1396 if (!notificationTemplate_) {
1397 ANS_LOGE("Failed to read notificationTemplate");
1398 return false;
1399 }
1400 }
1401
1402 valid = parcel.ReadBool();
1403 if (valid) {
1404 notificationFlags_ = std::shared_ptr<NotificationFlags>(parcel.ReadParcelable<NotificationFlags>());
1405 if (!notificationFlags_) {
1406 ANS_LOGE("Failed to read notificationFlags");
1407 return false;
1408 }
1409 }
1410
1411 return true;
1412 }
1413
GetNowSysTime()1414 int64_t NotificationRequest::GetNowSysTime()
1415 {
1416 std::chrono::time_point<std::chrono::system_clock> nowSys = std::chrono::system_clock::now();
1417 auto epoch = nowSys.time_since_epoch();
1418 auto value = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
1419 int64_t duration = value.count();
1420 return duration;
1421 }
1422
SetTemplate(const std::shared_ptr<NotificationTemplate> & templ)1423 void NotificationRequest::SetTemplate(const std::shared_ptr<NotificationTemplate> &templ)
1424 {
1425 notificationTemplate_ = templ;
1426 }
1427
GetTemplate() const1428 std::shared_ptr<NotificationTemplate> NotificationRequest::GetTemplate() const
1429 {
1430 return notificationTemplate_;
1431 }
1432
SetFlags(const std::shared_ptr<NotificationFlags> & flags)1433 void NotificationRequest::SetFlags(const std::shared_ptr<NotificationFlags> &flags)
1434 {
1435 notificationFlags_ = flags;
1436 }
1437
GetFlags() const1438 std::shared_ptr<NotificationFlags> NotificationRequest::GetFlags() const
1439 {
1440 return notificationFlags_;
1441 }
1442
SetReceiverUserId(int32_t userId)1443 void NotificationRequest::SetReceiverUserId(int32_t userId)
1444 {
1445 receiverUserId_ = userId;
1446 }
1447
GetReceiverUserId() const1448 int32_t NotificationRequest::GetReceiverUserId() const
1449 {
1450 return receiverUserId_;
1451 }
1452
CopyBase(const NotificationRequest & other)1453 void NotificationRequest::CopyBase(const NotificationRequest &other)
1454 {
1455 this->notificationId_ = other.notificationId_;
1456 this->color_ = other.color_;
1457 this->badgeNumber_ = other.badgeNumber_;
1458 this->progressValue_ = other.progressValue_;
1459 this->progressMax_ = other.progressMax_;
1460 this->createTime_ = other.createTime_;
1461 this->deliveryTime_ = other.deliveryTime_;
1462 this->autoDeletedTime_ = other.autoDeletedTime_;
1463
1464 this->creatorPid_ = other.creatorPid_;
1465 this->creatorUid_ = other.creatorUid_;
1466 this->ownerUid_ = other.ownerUid_;
1467 this->creatorUserId_ = other.creatorUserId_;
1468 this->ownerUserId_ = other.ownerUserId_;
1469 this->receiverUserId_ = other.receiverUserId_;
1470 this->isAgent_ = other.isAgent_;
1471
1472 this->slotType_ = other.slotType_;
1473 this->settingsText_ = other.settingsText_;
1474 this->creatorBundleName_ = other.creatorBundleName_;
1475 this->ownerBundleName_ = other.ownerBundleName_;
1476 this->groupName_ = other.groupName_;
1477 this->statusBarText_ = other.statusBarText_;
1478 this->label_ = other.label_;
1479 this->shortcutId_ = other.shortcutId_;
1480 this->sortingKey_ = other.sortingKey_;
1481 this->classification_ = other.classification_;
1482
1483 this->groupAlertType_ = other.groupAlertType_;
1484 this->visiblenessType_ = other.visiblenessType_;
1485 this->badgeStyle_ = other.badgeStyle_;
1486 this->notificationContentType_ = other.notificationContentType_;
1487 }
1488
CopyOther(const NotificationRequest & other)1489 void NotificationRequest::CopyOther(const NotificationRequest &other)
1490 {
1491 this->showDeliveryTime_ = other.showDeliveryTime_;
1492 this->tapDismissed_ = other.tapDismissed_;
1493 this->colorEnabled_ = other.colorEnabled_;
1494 this->alertOneTime_ = other.alertOneTime_;
1495 this->showStopwatch_ = other.showStopwatch_;
1496 this->isCountdown_ = other.isCountdown_;
1497 this->inProgress_ = other.inProgress_;
1498 this->groupOverview_ = other.groupOverview_;
1499 this->progressIndeterminate_ = other.progressIndeterminate_;
1500 this->unremovable_ = other.unremovable_;
1501 this->floatingIcon_ = other.floatingIcon_;
1502 this->onlyLocal_ = other.onlyLocal_;
1503 this->permitted_ = other.permitted_;
1504
1505 this->wantAgent_ = other.wantAgent_;
1506 this->removalWantAgent_ = other.removalWantAgent_;
1507 this->maxScreenWantAgent_ = other.maxScreenWantAgent_;
1508 this->additionalParams_ = other.additionalParams_;
1509 this->littleIcon_ = other.littleIcon_;
1510 this->bigIcon_ = other.bigIcon_;
1511 this->notificationContent_ = other.notificationContent_;
1512
1513 this->actionButtons_ = other.actionButtons_;
1514 this->messageUsers_ = other.messageUsers_;
1515 this->userInputHistory_ = other.userInputHistory_;
1516
1517 this->distributedOptions_ = other.distributedOptions_;
1518
1519 this->notificationTemplate_ = other.notificationTemplate_;
1520 this->notificationFlags_ = other.notificationFlags_;
1521 }
1522
ConvertObjectsToJson(nlohmann::json & jsonObject) const1523 bool NotificationRequest::ConvertObjectsToJson(nlohmann::json &jsonObject) const
1524 {
1525 jsonObject["wantAgent"] = wantAgent_ ? AbilityRuntime::WantAgent::WantAgentHelper::ToString(wantAgent_) : "";
1526
1527 nlohmann::json contentObj;
1528 if (notificationContent_) {
1529 if (!NotificationJsonConverter::ConvertToJson(notificationContent_.get(), contentObj)) {
1530 ANS_LOGE("Cannot convert notificationContent to JSON");
1531 return false;
1532 }
1533 }
1534 jsonObject["content"] = contentObj;
1535
1536 nlohmann::json buttonsArr = nlohmann::json::array();
1537 for (auto &btn : actionButtons_) {
1538 if (!btn) {
1539 continue;
1540 }
1541
1542 nlohmann::json btnObj;
1543 if (!NotificationJsonConverter::ConvertToJson(btn.get(), btnObj)) {
1544 ANS_LOGE("Cannot convert actionButton to JSON");
1545 return false;
1546 }
1547
1548 buttonsArr.emplace_back(btnObj);
1549 }
1550 jsonObject["actionButtons"] = buttonsArr;
1551
1552 std::string extraInfoStr;
1553 if (additionalParams_) {
1554 AAFwk::WantParamWrapper wWrapper(*additionalParams_);
1555 extraInfoStr = wWrapper.ToString();
1556 }
1557 jsonObject["extraInfo"] = extraInfoStr;
1558
1559 jsonObject["smallIcon"] = AnsImageUtil::PackImage(littleIcon_);
1560 jsonObject["largeIcon"] = AnsImageUtil::PackImage(bigIcon_);
1561
1562 nlohmann::json optObj;
1563 if (!NotificationJsonConverter::ConvertToJson(&distributedOptions_, optObj)) {
1564 ANS_LOGE("Cannot convert distributedOptions to JSON");
1565 return false;
1566 }
1567 jsonObject["distributedOptions"] = optObj;
1568
1569 if (notificationFlags_) {
1570 nlohmann::json flagsObj;
1571 if (!NotificationJsonConverter::ConvertToJson(notificationFlags_.get(), flagsObj)) {
1572 ANS_LOGE("Cannot convert notificationFlags to JSON");
1573 return false;
1574 }
1575 jsonObject["notificationFlags"] = flagsObj;
1576 }
1577
1578 return true;
1579 }
1580
ConvertJsonToNum(NotificationRequest * target,const nlohmann::json & jsonObject)1581 void NotificationRequest::ConvertJsonToNum(NotificationRequest *target, const nlohmann::json &jsonObject)
1582 {
1583 if (target == nullptr) {
1584 ANS_LOGE("Invalid input parameter");
1585 return;
1586 }
1587
1588 const auto &jsonEnd = jsonObject.cend();
1589
1590 if (jsonObject.find("id") != jsonEnd) {
1591 target->notificationId_ = jsonObject.at("id").get<int32_t>();
1592 }
1593
1594 if (jsonObject.find("color") != jsonEnd) {
1595 target->color_ = jsonObject.at("color").get<uint32_t>();
1596 }
1597
1598 if (jsonObject.find("deliveryTime") != jsonEnd) {
1599 target->deliveryTime_ = jsonObject.at("deliveryTime").get<int64_t>();
1600 }
1601
1602 if (jsonObject.find("autoDeletedTime") != jsonEnd) {
1603 target->autoDeletedTime_ = jsonObject.at("autoDeletedTime").get<int64_t>();
1604 }
1605
1606 if (jsonObject.find("creatorUid") != jsonEnd) {
1607 target->creatorUid_ = jsonObject.at("creatorUid").get<int32_t>();
1608 }
1609
1610 if (jsonObject.find("creatorPid") != jsonEnd) {
1611 target->creatorPid_ = jsonObject.at("creatorPid").get<int32_t>();
1612 }
1613
1614 if (jsonObject.find("creatorUserId") != jsonEnd) {
1615 target->creatorUserId_ = jsonObject.at("creatorUserId").get<int32_t>();
1616 }
1617
1618 if (jsonObject.find("receiverUserId") != jsonEnd) {
1619 target->receiverUserId_ = jsonObject.at("receiverUserId").get<int32_t>();
1620 }
1621
1622 if (jsonObject.find("badgeNumber") != jsonEnd) {
1623 target->badgeNumber_ = jsonObject.at("badgeNumber").get<uint32_t>();
1624 }
1625 }
1626
ConvertJsonToString(NotificationRequest * target,const nlohmann::json & jsonObject)1627 void NotificationRequest::ConvertJsonToString(NotificationRequest *target, const nlohmann::json &jsonObject)
1628 {
1629 if (target == nullptr) {
1630 ANS_LOGE("Invalid input parameter");
1631 return;
1632 }
1633
1634 const auto &jsonEnd = jsonObject.cend();
1635
1636 if (jsonObject.find("creatorBundleName") != jsonEnd) {
1637 target->creatorBundleName_ = jsonObject.at("creatorBundleName").get<std::string>();
1638 }
1639
1640 if (jsonObject.find("ownerBundleName") != jsonEnd) {
1641 target->ownerBundleName_ = jsonObject.at("ownerBundleName").get<std::string>();
1642 }
1643
1644 if (jsonObject.find("groupName") != jsonEnd) {
1645 target->groupName_ = jsonObject.at("groupName").get<std::string>();
1646 }
1647
1648 if (jsonObject.find("label") != jsonEnd) {
1649 target->label_ = jsonObject.at("label").get<std::string>();
1650 }
1651
1652 if (jsonObject.find("classification") != jsonEnd) {
1653 target->classification_ = jsonObject.at("classification").get<std::string>();
1654 }
1655
1656 if (jsonObject.find("creatorBundleName") != jsonEnd) {
1657 target->creatorBundleName_ = jsonObject.at("creatorBundleName").get<std::string>();
1658 }
1659 }
1660
ConvertJsonToEnum(NotificationRequest * target,const nlohmann::json & jsonObject)1661 void NotificationRequest::ConvertJsonToEnum(NotificationRequest *target, const nlohmann::json &jsonObject)
1662 {
1663 if (target == nullptr) {
1664 ANS_LOGE("Invalid input parameter");
1665 return;
1666 }
1667
1668 const auto &jsonEnd = jsonObject.cend();
1669
1670 if (jsonObject.find("slotType") != jsonEnd) {
1671 auto slotTypeValue = jsonObject.at("slotType").get<int32_t>();
1672 target->slotType_ = static_cast<NotificationConstant::SlotType>(slotTypeValue);
1673 }
1674
1675 if (jsonObject.find("badgeIconStyle") != jsonEnd) {
1676 auto badgeStyleValue = jsonObject.at("badgeIconStyle").get<int32_t>();
1677 target->badgeStyle_ = static_cast<NotificationRequest::BadgeStyle>(badgeStyleValue);
1678 }
1679 }
1680
ConvertJsonToBool(NotificationRequest * target,const nlohmann::json & jsonObject)1681 void NotificationRequest::ConvertJsonToBool(NotificationRequest *target, const nlohmann::json &jsonObject)
1682 {
1683 if (target == nullptr) {
1684 ANS_LOGE("Invalid input parameter");
1685 return;
1686 }
1687
1688 const auto &jsonEnd = jsonObject.cend();
1689
1690 if (jsonObject.find("showDeliveryTime") != jsonEnd) {
1691 target->showDeliveryTime_ = jsonObject.at("showDeliveryTime").get<bool>();
1692 }
1693
1694 if (jsonObject.find("tapDismissed") != jsonEnd) {
1695 target->tapDismissed_ = jsonObject.at("tapDismissed").get<bool>();
1696 }
1697
1698 if (jsonObject.find("colorEnabled") != jsonEnd) {
1699 target->colorEnabled_ = jsonObject.at("colorEnabled").get<bool>();
1700 }
1701
1702 if (jsonObject.find("isOngoing") != jsonEnd) {
1703 target->inProgress_ = jsonObject.at("isOngoing").get<bool>();
1704 }
1705
1706 if (jsonObject.find("isAlertOnce") != jsonEnd) {
1707 target->alertOneTime_ = jsonObject.at("isAlertOnce").get<bool>();
1708 }
1709
1710 if (jsonObject.find("isStopwatch") != jsonEnd) {
1711 target->showStopwatch_ = jsonObject.at("isStopwatch").get<bool>();
1712 }
1713
1714 if (jsonObject.find("isCountdown") != jsonEnd) {
1715 target->isCountdown_ = jsonObject.at("isCountdown").get<bool>();
1716 }
1717
1718 if (jsonObject.find("isUnremovable") != jsonEnd) {
1719 target->unremovable_ = jsonObject.at("isUnremovable").get<bool>();
1720 }
1721
1722 if (jsonObject.find("isFloatingIcon") != jsonEnd) {
1723 target->floatingIcon_ = jsonObject.at("isFloatingIcon").get<bool>();
1724 }
1725 }
1726
ConvertJsonToPixelMap(NotificationRequest * target,const nlohmann::json & jsonObject)1727 void NotificationRequest::ConvertJsonToPixelMap(NotificationRequest *target, const nlohmann::json &jsonObject)
1728 {
1729 if (target == nullptr) {
1730 ANS_LOGE("Invalid input parameter");
1731 return;
1732 }
1733
1734 const auto &jsonEnd = jsonObject.cend();
1735
1736 if (jsonObject.find("smallIcon") != jsonEnd) {
1737 auto littleIconStr = jsonObject.at("smallIcon").get<std::string>();
1738 target->littleIcon_ = AnsImageUtil::UnPackImage(littleIconStr);
1739 }
1740
1741 if (jsonObject.find("largeIcon") != jsonEnd) {
1742 auto bigIconStr = jsonObject.at("largeIcon").get<std::string>();
1743 target->bigIcon_ = AnsImageUtil::UnPackImage(bigIconStr);
1744 }
1745 }
1746
ConvertJsonToNotificationContent(NotificationRequest * target,const nlohmann::json & jsonObject)1747 bool NotificationRequest::ConvertJsonToNotificationContent(
1748 NotificationRequest *target, const nlohmann::json &jsonObject)
1749 {
1750 if (target == nullptr) {
1751 ANS_LOGE("Invalid input parameter");
1752 return false;
1753 }
1754
1755 const auto &jsonEnd = jsonObject.cend();
1756
1757 if (jsonObject.find("content") != jsonEnd) {
1758 auto contentObj = jsonObject.at("content");
1759 if (!contentObj.is_null()) {
1760 auto pContent = NotificationJsonConverter::ConvertFromJson<NotificationContent>(contentObj);
1761 if (pContent == nullptr) {
1762 ANS_LOGE("Failed to parse notification content!");
1763 return false;
1764 }
1765
1766 target->notificationContent_ = std::shared_ptr<NotificationContent>(pContent);
1767 }
1768 }
1769
1770 return true;
1771 }
1772
ConvertJsonToNotificationActionButton(NotificationRequest * target,const nlohmann::json & jsonObject)1773 bool NotificationRequest::ConvertJsonToNotificationActionButton(
1774 NotificationRequest *target, const nlohmann::json &jsonObject)
1775 {
1776 if (target == nullptr) {
1777 ANS_LOGE("Invalid input parameter");
1778 return false;
1779 }
1780
1781 const auto &jsonEnd = jsonObject.cend();
1782
1783 if (jsonObject.find("actionButtons") != jsonEnd) {
1784 auto buttonArr = jsonObject.at("actionButtons");
1785 for (auto &btnObj : buttonArr) {
1786 auto pBtn = NotificationJsonConverter::ConvertFromJson<NotificationActionButton>(btnObj);
1787 if (pBtn == nullptr) {
1788 ANS_LOGE("Failed to parse actionButton!");
1789 return false;
1790 }
1791
1792 target->actionButtons_.emplace_back(pBtn);
1793 }
1794 }
1795
1796 return true;
1797 }
1798
ConvertJsonToNotificationDistributedOptions(NotificationRequest * target,const nlohmann::json & jsonObject)1799 bool NotificationRequest::ConvertJsonToNotificationDistributedOptions(
1800 NotificationRequest *target, const nlohmann::json &jsonObject)
1801 {
1802 if (target == nullptr) {
1803 ANS_LOGE("Invalid input parameter");
1804 return false;
1805 }
1806
1807 const auto &jsonEnd = jsonObject.cend();
1808
1809 if (jsonObject.find("distributedOptions") != jsonEnd) {
1810 auto optObj = jsonObject.at("distributedOptions");
1811 if (!optObj.is_null()) {
1812 auto pOpt = NotificationJsonConverter::ConvertFromJson<NotificationDistributedOptions>(optObj);
1813 if (pOpt == nullptr) {
1814 ANS_LOGE("Failed to parse distributedOptions!");
1815 return false;
1816 }
1817
1818 target->distributedOptions_ = *pOpt;
1819 }
1820 }
1821
1822 return true;
1823 }
1824
ConvertJsonToNotificationFlags(NotificationRequest * target,const nlohmann::json & jsonObject)1825 bool NotificationRequest::ConvertJsonToNotificationFlags(
1826 NotificationRequest *target, const nlohmann::json &jsonObject)
1827 {
1828 if (target == nullptr) {
1829 ANS_LOGE("Invalid input parameter");
1830 return false;
1831 }
1832
1833 const auto &jsonEnd = jsonObject.cend();
1834
1835 if (jsonObject.find("notificationFlags") != jsonEnd) {
1836 auto flagsObj = jsonObject.at("notificationFlags");
1837 if (!flagsObj.is_null()) {
1838 auto pFlags = NotificationJsonConverter::ConvertFromJson<NotificationFlags>(flagsObj);
1839 if (pFlags == nullptr) {
1840 ANS_LOGE("Failed to parse notificationFlags!");
1841 return false;
1842 }
1843
1844 target->notificationFlags_ = std::shared_ptr<NotificationFlags>(pFlags);
1845 }
1846 }
1847
1848 return true;
1849 }
1850 } // namespace Notification
1851 } // namespace OHOS
1852