1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "notification_manager_impl.h" 17 #include "inner_errors.h" 18 #include "notification_enable.h" 19 #include "pixel_map_impl.h" 20 21 #include "notification_request.h" 22 #include "notification_constant.h" 23 #include "notification_content.h" 24 #include "notification_helper.h" 25 #include "notification_multiline_content.h" 26 #include "notification_normal_content.h" 27 #include "notification_picture_content.h" 28 #include "notification_long_text_content.h" 29 #include "notification_manager_log.h" 30 31 #include "ans_notification.h" 32 #include "singleton.h" 33 #include "securec.h" 34 35 namespace OHOS { 36 namespace CJSystemapi { 37 using namespace OHOS::Notification; 38 using namespace OHOS::CJSystemapi::Notification; 39 GetNotificationBasicContentDetailed(CNotificationBasicContent * contentResult,std::shared_ptr<NotificationBasicContent> basicContent)40 static bool GetNotificationBasicContentDetailed( 41 CNotificationBasicContent* contentResult, 42 std::shared_ptr<NotificationBasicContent> basicContent) 43 { 44 char str[STR_MAX_SIZE] = {0}; 45 // title: String 46 if (strcpy_s(str, STR_MAX_SIZE, contentResult->title) != EOK) { 47 return false; 48 } 49 if (strlen(str) == 0) { 50 LOGE("Property title is empty"); 51 return false; 52 } 53 basicContent->SetTitle(std::string(str)); 54 LOGI("normal::title = %{public}s", str); 55 56 // text: String 57 if (strcpy_s(str, STR_MAX_SIZE, contentResult->text) != EOK) { 58 return false; 59 } 60 if (strlen(str) == 0) { 61 LOGE("Property text is empty"); 62 return false; 63 } 64 basicContent->SetText(std::string(str)); 65 LOGI("normal::text = %{public}s", str); 66 67 // additionalText: string 68 if (strcpy_s(str, STR_MAX_SIZE, contentResult->additionalText) != EOK) { 69 return false; 70 } 71 basicContent->SetAdditionalText(std::string(str)); 72 LOGI("normal::additionalText = %{public}s", str); 73 74 return true; 75 } 76 GetNotificationLongTextContentDetailed(CNotificationLongTextContent * contentResult,std::shared_ptr<NotificationLongTextContent> & longContent)77 static bool GetNotificationLongTextContentDetailed( 78 CNotificationLongTextContent* contentResult, 79 std::shared_ptr<NotificationLongTextContent> &longContent) 80 { 81 char str[STR_MAX_SIZE] = {0}; 82 char long_str[LONG_STR_MAX_SIZE + 1] = {0}; 83 84 std::shared_ptr<CNotificationBasicContent> tempContent = std::make_shared<CNotificationBasicContent>(); 85 tempContent->title = contentResult->title; 86 tempContent->text = contentResult->text; 87 tempContent->additionalText = contentResult->additionalText; 88 if (!GetNotificationBasicContentDetailed(tempContent.get(), longContent)) { 89 return false; 90 } 91 92 // longText: String 93 if (strcpy_s(long_str, LONG_STR_MAX_SIZE + 1, contentResult->longText) != EOK) { 94 return false; 95 } 96 if (strlen(long_str) == 0) { 97 LOGE("Property longText is empty"); 98 return false; 99 } 100 longContent->SetLongText(std::string(long_str)); 101 LOGI("longText::longText = %{public}s", long_str); 102 103 // briefText: String 104 if (strcpy_s(str, STR_MAX_SIZE, contentResult->briefText) != EOK) { 105 return false; 106 } 107 if (strlen(str) == 0) { 108 LOGE("Property briefText is empty"); 109 return false; 110 } 111 longContent->SetBriefText(std::string(str)); 112 LOGI("longText::briefText = %{public}s", str); 113 114 // expandedTitle: String 115 if (strcpy_s(str, STR_MAX_SIZE, contentResult->expandedTitle) != EOK) { 116 return false; 117 } 118 if (strlen(str) == 0) { 119 LOGE("Property expandedTitle is empty"); 120 return false; 121 } 122 longContent->SetExpandedTitle(std::string(str)); 123 LOGI("longText::expandedTitle = %{public}s", str); 124 125 return true; 126 } 127 GetNotificationPictureContentDetailed(CNotificationPictureContent * contentResult,std::shared_ptr<OHOS::Notification::NotificationPictureContent> & pictureContent)128 static bool GetNotificationPictureContentDetailed( 129 CNotificationPictureContent* contentResult, 130 std::shared_ptr<OHOS::Notification::NotificationPictureContent> &pictureContent) 131 { 132 char str[STR_MAX_SIZE] = {0}; 133 134 std::shared_ptr<CNotificationBasicContent> tempContent = std::make_shared<CNotificationBasicContent>(); 135 tempContent->title = contentResult->title; 136 tempContent->text = contentResult->text; 137 tempContent->additionalText = contentResult->additionalText; 138 if (!GetNotificationBasicContentDetailed(tempContent.get(), pictureContent)) { 139 return false; 140 } 141 142 // briefText: String 143 if (strcpy_s(str, STR_MAX_SIZE, contentResult->briefText) != EOK) { 144 return false; 145 } 146 if (std::strlen(str) == 0) { 147 LOGE("Property briefText is empty"); 148 return false; 149 } 150 pictureContent->SetBriefText(std::string(str)); 151 LOGI("longText::briefText = %{public}s", str); 152 153 // expandedTitle: String 154 if (strcpy_s(str, STR_MAX_SIZE, contentResult->expandedTitle) != EOK) { 155 return false; 156 } 157 if (std::strlen(str) == 0) { 158 LOGE("Property expandedTitle is empty"); 159 return false; 160 } 161 pictureContent->SetExpandedTitle(std::string(str)); 162 LOGI("picture::expandedTitle = %{public}s", str); 163 164 // picture: image.PixelMap 165 auto pixelMap = FFI::FFIData::GetData<Media::PixelMapImpl>(contentResult->picture); 166 if (pixelMap == nullptr) { 167 LOGE("Invalid object pixelMap"); 168 return false; 169 } 170 pictureContent->SetBigPicture(pixelMap->GetRealPixelMap()); 171 172 return true; 173 } 174 GetNotificationBasicContent(CNotificationBasicContent * contentResult,NotificationRequest & request)175 static bool GetNotificationBasicContent( 176 CNotificationBasicContent* contentResult, 177 NotificationRequest &request) 178 { 179 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>(); 180 if (normalContent == nullptr) { 181 LOGE("normalContent is null"); 182 return false; 183 } 184 185 if (!GetNotificationBasicContentDetailed(contentResult, normalContent)) { 186 return false; 187 } 188 request.SetContent(std::make_shared<NotificationContent>(normalContent)); 189 return true; 190 } 191 GetNotificationLongTextContent(CNotificationLongTextContent * contentResult,NotificationRequest & request)192 static bool GetNotificationLongTextContent( 193 CNotificationLongTextContent* contentResult, 194 NotificationRequest &request) 195 { 196 std::shared_ptr<OHOS::Notification::NotificationLongTextContent> longContent = 197 std::make_shared<OHOS::Notification::NotificationLongTextContent>(); 198 if (longContent == nullptr) { 199 LOGE("longContent is null"); 200 return false; 201 } 202 if (!GetNotificationLongTextContentDetailed(contentResult, longContent)) { 203 return false; 204 } 205 206 request.SetContent(std::make_shared<NotificationContent>(longContent)); 207 return true; 208 } 209 GetNotificationPictureContent(CNotificationPictureContent * contentResult,NotificationRequest & request)210 static bool GetNotificationPictureContent( 211 CNotificationPictureContent* contentResult, 212 NotificationRequest &request) 213 { 214 std::shared_ptr<OHOS::Notification::NotificationPictureContent> pictureContent = 215 std::make_shared<OHOS::Notification::NotificationPictureContent>(); 216 if (pictureContent == nullptr) { 217 LOGE("pictureContent is null"); 218 return false; 219 } 220 221 if (!GetNotificationPictureContentDetailed(contentResult, pictureContent)) { 222 return false; 223 } 224 225 request.SetContent(std::make_shared<NotificationContent>(pictureContent)); 226 return true; 227 } 228 GetNotificationMultiLineContentLines(CNotificationMultiLineContent * result,std::shared_ptr<OHOS::Notification::NotificationMultiLineContent> & multiLineContent)229 static bool GetNotificationMultiLineContentLines( 230 CNotificationMultiLineContent* result, 231 std::shared_ptr<OHOS::Notification::NotificationMultiLineContent> &multiLineContent) 232 { 233 char str[STR_MAX_SIZE] = {0}; 234 int64_t length = result->lines.size; 235 if (length == 0) { 236 LOGE("The array is empty."); 237 return false; 238 } 239 for (int64_t i = 0; i < length; i++) { 240 if (strcpy_s(str, STR_MAX_SIZE, result->lines.head[i]) != EOK) { 241 return false; 242 } 243 multiLineContent->AddSingleLine(std::string(str)); 244 LOGI("multiLine: lines : addSingleLine = %{public}s", str); 245 } 246 return true; 247 } 248 GetNotificationMultiLineContent(CNotificationMultiLineContent * contentResult,NotificationRequest & request)249 static bool GetNotificationMultiLineContent( 250 CNotificationMultiLineContent* contentResult, 251 NotificationRequest &request) 252 { 253 char str[STR_MAX_SIZE] = {0}; 254 255 std::shared_ptr<OHOS::Notification::NotificationMultiLineContent> multiLineContent = 256 std::make_shared<OHOS::Notification::NotificationMultiLineContent>(); 257 if (multiLineContent == nullptr) { 258 LOGE("multiLineContent is null"); 259 return false; 260 } 261 262 std::shared_ptr<CNotificationBasicContent> tempContent = std::make_shared<CNotificationBasicContent>(); 263 tempContent->title = contentResult->title; 264 tempContent->text = contentResult->text; 265 tempContent->additionalText = contentResult->additionalText; 266 if (!GetNotificationBasicContentDetailed(tempContent.get(), multiLineContent)) { 267 return false; 268 } 269 270 // briefText: String 271 if (strcpy_s(str, STR_MAX_SIZE, contentResult->briefText) != EOK) { 272 return false; 273 } 274 if (std::strlen(str) == 0) { 275 LOGE("Property briefText is empty"); 276 return false; 277 } 278 multiLineContent->SetBriefText(std::string(str)); 279 LOGI("multiLine: briefText = %{public}s", str); 280 281 // longTitle: String 282 if (strcpy_s(str, STR_MAX_SIZE, contentResult->longTitle)) { 283 return false; 284 } 285 if (std::strlen(str) == 0) { 286 LOGE("Property longTitle is empty"); 287 return false; 288 } 289 multiLineContent->SetExpandedTitle(std::string(str)); 290 LOGI("multiLine: longTitle = %{public}s", str); 291 292 // lines: Array<String> 293 if (!GetNotificationMultiLineContentLines(contentResult, multiLineContent)) { 294 return false; 295 } 296 297 request.SetContent(std::make_shared<NotificationContent>(multiLineContent)); 298 return true; 299 } 300 ContentTypeCJToC(const ContentType & inType,NotificationContent::Type & outType)301 static bool ContentTypeCJToC(const ContentType &inType, NotificationContent::Type &outType) 302 { 303 switch (inType) { 304 case ContentType::NOTIFICATION_CONTENT_BASIC_TEXT: 305 outType = NotificationContent::Type::BASIC_TEXT; 306 break; 307 case ContentType::NOTIFICATION_CONTENT_LONG_TEXT: 308 outType = NotificationContent::Type::LONG_TEXT; 309 break; 310 case ContentType::NOTIFICATION_CONTENT_MULTILINE: 311 outType = NotificationContent::Type::MULTILINE; 312 break; 313 case ContentType::NOTIFICATION_CONTENT_PICTURE: 314 outType = NotificationContent::Type::PICTURE; 315 break; 316 case ContentType::NOTIFICATION_CONTENT_CONVERSATION: 317 outType = NotificationContent::Type::CONVERSATION; 318 break; 319 case ContentType::NOTIFICATION_CONTENT_LOCAL_LIVE_VIEW: 320 outType = NotificationContent::Type::LOCAL_LIVE_VIEW; 321 break; 322 case ContentType::NOTIFICATION_CONTENT_LIVE_VIEW: 323 outType = NotificationContent::Type::LIVE_VIEW; 324 break; 325 default: 326 LOGE("ContentType %{public}d is an invalid value", inType); 327 return false; 328 } 329 return true; 330 } 331 SlotTypeCJToC(const SlotType & inType,NotificationConstant::SlotType & outType)332 static bool SlotTypeCJToC(const SlotType &inType, NotificationConstant::SlotType &outType) 333 { 334 switch (inType) { 335 case SlotType::SOCIAL_COMMUNICATION: 336 outType = NotificationConstant::SlotType::SOCIAL_COMMUNICATION; 337 break; 338 case SlotType::SERVICE_INFORMATION: 339 outType = NotificationConstant::SlotType::SERVICE_REMINDER; 340 break; 341 case SlotType::CONTENT_INFORMATION: 342 outType = NotificationConstant::SlotType::CONTENT_INFORMATION; 343 break; 344 case SlotType::LIVE_VIEW: 345 outType = NotificationConstant::SlotType::LIVE_VIEW; 346 break; 347 case SlotType::CUSTOMER_SERVICE: 348 outType = NotificationConstant::SlotType::CUSTOMER_SERVICE; 349 break; 350 case SlotType::UNKNOWN_TYPE: 351 case SlotType::OTHER_TYPES: 352 outType = NotificationConstant::SlotType::OTHER; 353 break; 354 default: 355 LOGE("SlotType %{public}d is an invalid value", inType); 356 return false; 357 } 358 return true; 359 } 360 GetNotificationContent(CNotificationContent content,NotificationRequest & request)361 static bool GetNotificationContent(CNotificationContent content, NotificationRequest &request) 362 { 363 NotificationContent::Type outType = NotificationContent::Type::NONE; 364 if (!ContentTypeCJToC(ContentType(content.notificationContentType), outType)) { 365 return false; 366 } 367 switch (outType) { 368 case NotificationContent::Type::BASIC_TEXT: 369 if (content.normal == nullptr || !GetNotificationBasicContent(content.normal, request)) { 370 return false; 371 } 372 break; 373 case NotificationContent::Type::LONG_TEXT: 374 if (content.longText == nullptr || !GetNotificationLongTextContent(content.longText, request)) { 375 return false; 376 } 377 break; 378 case NotificationContent::Type::PICTURE: 379 if (content.picture == nullptr || !GetNotificationPictureContent(content.picture, request)) { 380 return false; 381 } 382 break; 383 case NotificationContent::Type::CONVERSATION: 384 break; 385 case NotificationContent::Type::MULTILINE: 386 if (content.multiLine == nullptr || !GetNotificationMultiLineContent(content.multiLine, request)) { 387 return false; 388 } 389 break; 390 case NotificationContent::Type::LOCAL_LIVE_VIEW: 391 break; 392 case NotificationContent::Type::LIVE_VIEW: 393 break; 394 default: 395 return false; 396 } 397 return true; 398 } 399 GetNotificationSlotType(int32_t slotType,NotificationRequest & request)400 static bool GetNotificationSlotType(int32_t slotType, NotificationRequest &request) 401 { 402 NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER; 403 if (!SlotTypeCJToC(SlotType(slotType), outType)) { 404 return false; 405 } 406 request.SetSlotType(outType); 407 return true; 408 } 409 GetNotificationSmallIcon(int64_t smallIcon,NotificationRequest request)410 static bool GetNotificationSmallIcon(int64_t smallIcon, NotificationRequest request) 411 { 412 if (smallIcon != -1) { 413 auto pixelMap = FFI::FFIData::GetData<Media::PixelMapImpl>(smallIcon); 414 if (pixelMap == nullptr) { 415 LOGE("Invalid object pixelMap"); 416 return false; 417 } 418 request.SetLittleIcon(pixelMap->GetRealPixelMap()); 419 } 420 return true; 421 } 422 GetNotificationLargeIcon(int64_t largeIcon,NotificationRequest request)423 static bool GetNotificationLargeIcon(int64_t largeIcon, NotificationRequest request) 424 { 425 if (largeIcon != -1) { 426 auto pixelMap = FFI::FFIData::GetData<Media::PixelMapImpl>(largeIcon); 427 if (pixelMap == nullptr) { 428 LOGE("Invalid object pixelMap"); 429 return false; 430 } 431 request.SetBigIcon(pixelMap->GetRealPixelMap()); 432 } 433 return true; 434 } 435 GetNotificationSupportDisplayDevices(CDistributedOptions * distributedOption,NotificationRequest request)436 static bool GetNotificationSupportDisplayDevices( 437 CDistributedOptions* distributedOption, 438 NotificationRequest request) 439 { 440 int64_t length = distributedOption->supportDisplayDevices.size; 441 if (length == 0) { 442 LOGE("The array is empty."); 443 return false; 444 } 445 std::vector<std::string> devices; 446 for (int64_t i = 0; i < length; i++) { 447 char str[STR_MAX_SIZE] = {0}; 448 auto displayDevice = distributedOption->supportDisplayDevices.head[i]; 449 if (strcpy_s(str, STR_MAX_SIZE, displayDevice) != EOK) { 450 return false; 451 } 452 devices.emplace_back(str); 453 LOGI("supportDisplayDevices = %{public}s", str); 454 } 455 request.SetDevicesSupportDisplay(devices); 456 return true; 457 } 458 GetNotificationSupportOperateDevices(CDistributedOptions * distributedOption,NotificationRequest request)459 static bool GetNotificationSupportOperateDevices( 460 CDistributedOptions* distributedOption, 461 NotificationRequest request) 462 { 463 int64_t length = distributedOption->supportOperateDevices.size; 464 if (length == 0) { 465 LOGE("The array is empty."); 466 return false; 467 } 468 std::vector<std::string> devices; 469 for (int64_t i = 0; i < length; i++) { 470 char str[STR_MAX_SIZE] = {0}; 471 auto operateDevice = distributedOption->supportOperateDevices.head[i]; 472 if (strcpy_s(str, STR_MAX_SIZE, operateDevice) != EOK) { 473 return false; 474 } 475 devices.emplace_back(str); 476 LOGI("supportOperateDevices = %{public}s", str); 477 } 478 request.SetDevicesSupportOperate(devices); 479 return true; 480 } 481 GetNotificationRequestDistributedOptions(CDistributedOptions * distributedOption,NotificationRequest request)482 static bool GetNotificationRequestDistributedOptions( 483 CDistributedOptions* distributedOption, 484 NotificationRequest request) 485 { 486 if (distributedOption != nullptr) { 487 // isDistributed?: boolean 488 request.SetDistributed(distributedOption->isDistributed); 489 490 // supportDisplayDevices?: Array<string> 491 if (!GetNotificationSupportDisplayDevices(distributedOption, request)) { 492 return false; 493 } 494 495 // supportOperateDevices?: Array<string> 496 if (!GetNotificationSupportOperateDevices(distributedOption, request)) { 497 return false; 498 } 499 } 500 return true; 501 } 502 GetNotificationRequestByNumber(CNotificationRequest cjRequest,NotificationRequest & request)503 static bool GetNotificationRequestByNumber(CNotificationRequest cjRequest, NotificationRequest &request) 504 { 505 // id?: number 506 int32_t id = cjRequest.id; 507 request.SetNotificationId(id); 508 509 // deliveryTime?: number 510 int64_t deliveryTime = cjRequest.deliveryTime; 511 request.SetDeliveryTime(deliveryTime); 512 513 // autoDeletedTime?: number 514 int64_t autoDeletedTime = cjRequest.autoDeletedTime; 515 request.SetAutoDeletedTime(autoDeletedTime); 516 517 // color?: number 518 request.SetColor(cjRequest.color); 519 520 // badgeIconStyle?: number 521 int32_t badgeIconStyle = cjRequest.badgeIconStyle; 522 request.SetBadgeIconStyle(static_cast<NotificationRequest::BadgeStyle>(badgeIconStyle)); 523 524 // badgeNumber?: number 525 int32_t badgeNumber = cjRequest.badgeNumber; 526 if (badgeNumber < 0) { 527 LOGE("Wrong badge number."); 528 return false; 529 } 530 request.SetBadgeNumber(badgeNumber); 531 532 return true; 533 } 534 GetNotificationRequestByString(CNotificationRequest cjRequest,NotificationRequest & request)535 static bool GetNotificationRequestByString(CNotificationRequest cjRequest, NotificationRequest &request) 536 { 537 // label?: string 538 char label[STR_MAX_SIZE] = {0}; 539 if (strcpy_s(label, STR_MAX_SIZE, cjRequest.label) != EOK) { 540 return false; 541 } 542 request.SetLabel(std::string(label)); 543 544 // groupName?: string 545 char groupName[STR_MAX_SIZE] = {0}; 546 if (strcpy_s(groupName, STR_MAX_SIZE, cjRequest.groupName) != EOK) { 547 return false; 548 } 549 request.SetGroupName(std::string(groupName)); 550 551 return true; 552 } 553 GetNotificationRequestByBool(CNotificationRequest cjRequest,NotificationRequest & request)554 static bool GetNotificationRequestByBool(CNotificationRequest cjRequest, NotificationRequest &request) 555 { 556 // isOngoing?: boolean 557 bool isOngoing = cjRequest.isOngoing; 558 request.SetInProgress(isOngoing); 559 560 // isUnremovable?: boolean 561 bool isUnremovable = cjRequest.isUnremovable; 562 request.SetUnremovable(isUnremovable); 563 564 // tapDismissed?: boolean 565 bool tapDismissed = cjRequest.tapDismissed; 566 request.SetTapDismissed(tapDismissed); 567 568 // colorEnabled?: boolean 569 bool colorEnabled = cjRequest.colorEnabled; 570 request.SetColorEnabled(colorEnabled); 571 572 // isAlertOnce?: boolean 573 bool isAlertOnce = cjRequest.isAlertOnce; 574 request.SetAlertOneTime(isAlertOnce); 575 576 // isStopwatch?: boole 577 bool isStopwatch = cjRequest.isStopwatch; 578 request.SetShowStopwatch(isStopwatch); 579 580 // isCountDown?: boolean 581 bool isCountDown = cjRequest.isCountDown; 582 request.SetCountdownTimer(isCountDown); 583 584 // showDeliveryTime?: boolean 585 bool showDeliveryTime = cjRequest.showDeliveryTime; 586 request.SetShowDeliveryTime(showDeliveryTime); 587 588 return true; 589 } 590 GetNotificationRequestByCustom(CNotificationRequest cjRequest,NotificationRequest & request)591 static bool GetNotificationRequestByCustom(CNotificationRequest cjRequest, NotificationRequest &request) 592 { 593 // content: NotificationContent 594 if (!GetNotificationContent(cjRequest.notificationContent, request)) { 595 return false; 596 } 597 // slotType?: notification.SlotType 598 if (!GetNotificationSlotType(cjRequest.notificationSlotType, request)) { 599 return false; 600 } 601 // smallIcon?: image.PixelMap 602 if (!GetNotificationSmallIcon(cjRequest.smallIcon, request)) { 603 return false; 604 } 605 // largeIcon?: image.PixelMap 606 if (!GetNotificationLargeIcon(cjRequest.largeIcon, request)) { 607 return false; 608 } 609 // distributedOption?:DistributedOptions 610 if (!GetNotificationRequestDistributedOptions(cjRequest.distributedOption, request)) { 611 return false; 612 } 613 614 return true; 615 } 616 ParseParameters(CNotificationRequest params,NotificationRequest & request)617 static bool ParseParameters(CNotificationRequest params, NotificationRequest &request) 618 { 619 if (!GetNotificationRequestByNumber(params, request)) { 620 return false; 621 } 622 623 if (!GetNotificationRequestByString(params, request)) { 624 return false; 625 } 626 627 if (!GetNotificationRequestByBool(params, request)) { 628 return false; 629 } 630 631 if (!GetNotificationRequestByCustom(params, request)) { 632 return false; 633 } 634 return true; 635 } 636 Publish(CNotificationRequest cjRequest)637 int NotificationManagerImpl::Publish(CNotificationRequest cjRequest) 638 { 639 LOGI("start make a NotificationRequest"); 640 NotificationRequest request; 641 LOGI("start parse the parameters of NotificationRequest"); 642 if (!ParseParameters(cjRequest, request)) { 643 return ERROR_PARAM_INVALID; 644 } 645 int code = NotificationHelper::PublishNotification(request); 646 return ErrorToExternal(code); 647 } 648 Cancel(int32_t id,const char * label)649 int NotificationManagerImpl::Cancel(int32_t id, const char* label) 650 { 651 int code = NotificationHelper::CancelNotification(label, id); 652 return ErrorToExternal(code); 653 } 654 CancelAll()655 int NotificationManagerImpl::CancelAll() 656 { 657 int code = NotificationHelper::CancelAllNotifications(); 658 return ErrorToExternal(code); 659 } 660 AddSlot(int32_t type)661 int NotificationManagerImpl::AddSlot(int32_t type) 662 { 663 NotificationConstant::SlotType slot = NotificationConstant::SlotType::OTHER; 664 if (!SlotTypeCJToC(SlotType(type), slot)) { 665 return false; 666 } 667 int code = NotificationHelper::AddNotificationSlot(slot); 668 return ErrorToExternal(code); 669 } 670 IsNotificationEnabled()671 RetDataBool NotificationManagerImpl::IsNotificationEnabled() 672 { 673 RetDataBool ret = { .code = EINVAL, .data = false }; 674 IsEnableParams params {}; 675 bool allowed = false; 676 int errorCode; 677 if (params.hasBundleOption) { 678 LOGI("option.bundle : %{public}s option.uid : %{public}d", 679 params.option.GetBundleName().c_str(), 680 params.option.GetUid()); 681 errorCode = NotificationHelper::IsAllowedNotify(params.option, allowed); 682 } else if (params.hasUserId) { 683 LOGI("userId : %{public}d", params.userId); 684 errorCode = NotificationHelper::IsAllowedNotify(params.userId, allowed); 685 } else { 686 errorCode = NotificationHelper::IsAllowedNotifySelf(allowed); 687 } 688 ret.code = ErrorToExternal(errorCode); 689 ret.data = allowed; 690 LOGI("errorCode : %{public}d, allowed : %{public}d", 691 errorCode, allowed); 692 return ret; 693 } 694 SetBadgeNumber(int32_t badgeNumber)695 int NotificationManagerImpl::SetBadgeNumber(int32_t badgeNumber) 696 { 697 int code = NotificationHelper::SetBadgeNumber(badgeNumber); 698 return ErrorToExternal(code); 699 } 700 RequestEnableNotification()701 int NotificationManagerImpl::RequestEnableNotification() 702 { 703 IsEnableParams params {}; 704 std::string deviceId {""}; 705 sptr<AnsDialogHostClient> client = nullptr; 706 if (!AnsDialogHostClient::CreateIfNullptr(client)) { 707 LOGI("dialog is popping %{public}d.", ERR_ANS_DIALOG_IS_POPPING) 708 return ErrorToExternal(ERR_ANS_DIALOG_IS_POPPING); 709 } 710 int code = NotificationHelper::RequestEnableNotification(deviceId, client, params.callerToken); 711 LOGI("done, code is %{public}d.", code) 712 return ErrorToExternal(code); 713 } 714 RequestEnableNotificationWithContext(sptr<AbilityRuntime::CJAbilityContext> context)715 int NotificationManagerImpl::RequestEnableNotificationWithContext(sptr<AbilityRuntime::CJAbilityContext> context) 716 { 717 IsEnableParams params {}; 718 sptr<IRemoteObject> callerToken = context->GetToken(); 719 params.callerToken = callerToken; 720 sptr<AnsDialogHostClient> client = nullptr; 721 params.hasCallerToken = true; 722 std::string deviceId {""}; 723 if (!AnsDialogHostClient::CreateIfNullptr(client)) { 724 LOGI("dialog is popping %{public}d.", ERR_ANS_DIALOG_IS_POPPING) 725 return ErrorToExternal(ERR_ANS_DIALOG_IS_POPPING); 726 } 727 int code = NotificationHelper::RequestEnableNotification(deviceId, client, params.callerToken); 728 LOGI("done, code is %{public}d.", code) 729 return ErrorToExternal(code); 730 } 731 IsDistributedEnabled()732 RetDataBool NotificationManagerImpl::IsDistributedEnabled() 733 { 734 RetDataBool ret = { .code = EINVAL, .data = false }; 735 bool enable = false; 736 int code = NotificationHelper::IsDistributedEnabled(enable); 737 LOGI("IsDistributedEnabled enable = %{public}d", enable); 738 ret.code = code; 739 ret.data = enable; 740 return ret; 741 } 742 } // CJSystemapi 743 } // namespace OHOS