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