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 #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_ADVANCED_NOTIFICATION_SERVICE_H 17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_ADVANCED_NOTIFICATION_SERVICE_H 18 19 #include <ctime> 20 #include <list> 21 #include <memory> 22 #include <mutex> 23 24 #include "event_handler.h" 25 #include "event_runner.h" 26 #include "refbase.h" 27 28 #include "ans_const_define.h" 29 #include "ans_manager_stub.h" 30 #include "distributed_kv_data_manager.h" 31 #include "distributed_kvstore_death_recipient.h" 32 #include "notification.h" 33 #include "notification_bundle_option.h" 34 #include "notification_record.h" 35 #include "notification_sorting_map.h" 36 #include "system_event_observer.h" 37 38 namespace OHOS { 39 namespace Notification { 40 class AdvancedNotificationService final : public AnsManagerStub { 41 public: 42 ~AdvancedNotificationService() override; 43 44 DISALLOW_COPY_AND_MOVE(AdvancedNotificationService); 45 46 /** 47 * @brief Get the instance of service. 48 * 49 * @return Returns the instance. 50 */ 51 static sptr<AdvancedNotificationService> GetInstance(); 52 53 // AnsManagerStub 54 55 /** 56 * @brief Publishes a notification with a specified label. 57 * @note If a notification with the same ID has been published by the current application and has not been deleted, 58 * this method will update the notification. 59 * 60 * @param label Indicates the label of the notification to publish. 61 * @param notification Indicates the NotificationRequest object for setting the notification content. 62 * This parameter must be specified. 63 * @return Returns ERR_OK on success, others on failure. 64 */ 65 ErrCode Publish(const std::string &label, const sptr<NotificationRequest> &request) override; 66 67 /** 68 * @brief Cancels a published notification matching the specified label and notificationId. 69 * 70 * @param notificationId Indicates the ID of the notification to cancel. 71 * @param label Indicates the label of the notification to cancel. 72 * @return Returns cancel notification result. 73 */ 74 ErrCode Cancel(int32_t notificationId, const std::string &label) override; 75 76 /** 77 * @brief Cancels all the published notifications. 78 * 79 * @return Returns ERR_OK on success, others on failure. 80 */ 81 ErrCode CancelAll() override; 82 83 /** 84 * @brief Cancels a published agent notification. 85 * 86 * @param notificationId Indicates the unique notification ID in the application. 87 * The value must be the ID of a published notification. 88 * Otherwise, this method does not take effect. 89 * @param representativeBundle Indicates the name of application bundle your application is representing. 90 * @param userId Indicates the specific user. 91 * @return Returns cancel notification result. 92 */ 93 ErrCode CancelAsBundle( 94 int32_t notificationId, const std::string &representativeBundle, int32_t userId) override; 95 96 /** 97 * @brief Adds a notification slot by type. 98 * 99 * @param slotType Indicates the notification slot type to be added. 100 * @return Returns ERR_OK on success, others on failure. 101 */ 102 ErrCode AddSlotByType(NotificationConstant::SlotType slotType) override; 103 104 /** 105 * @brief Creates multiple notification slots. 106 * 107 * @param slots Indicates the notification slots to create. 108 * @return Returns ERR_OK on success, others on failure. 109 */ 110 ErrCode AddSlots(const std::vector<sptr<NotificationSlot>> &slots) override; 111 112 /** 113 * @brief Deletes a created notification slot based on the slot ID. 114 * 115 * @param slotType Indicates the type of the slot, which is created by AddNotificationSlot 116 * This parameter must be specified. 117 * @return Returns ERR_OK on success, others on failure. 118 */ 119 ErrCode RemoveSlotByType(const NotificationConstant::SlotType &slotType) override; 120 121 /** 122 * @brief Deletes all notification slots. 123 * 124 * @return Returns ERR_OK on success, others on failure. 125 */ 126 ErrCode RemoveAllSlots() override; 127 128 /** 129 * @brief Queries a created notification slot. 130 * 131 * @param slotType Indicates the ID of the slot, which is created by AddNotificationSlot(NotificationSlot). This 132 * parameter must be specified. 133 * @param slot Indicates the created NotificationSlot. 134 * @return Returns ERR_OK on success, others on failure. 135 */ 136 ErrCode GetSlotByType(const NotificationConstant::SlotType &slotType, sptr<NotificationSlot> &slot) override; 137 138 /** 139 * @brief Obtains all notification slots of this application. 140 * 141 * @param slots Indicates the created NotificationSlot. 142 * @return Returns ERR_OK on success, others on failure. 143 */ 144 ErrCode GetSlots(std::vector<sptr<NotificationSlot>> &slots) override; 145 146 /** 147 * @brief Obtains the number of slot. 148 * 149 * @param bundleOption Indicates the bundle name and uid of the application. 150 * @param num Indicates the number of slot. 151 * @return Returns ERR_OK on success, others on failure. 152 */ 153 ErrCode GetSlotNumAsBundle(const sptr<NotificationBundleOption> &bundleOption, uint64_t &num) override; 154 155 /** 156 * @brief Obtains active notifications of the current application in the system. 157 * 158 * @param notifications Indicates active NotificationRequest objects of the current application. 159 * @return Returns ERR_OK on success, others on failure. 160 */ 161 ErrCode GetActiveNotifications(std::vector<sptr<NotificationRequest>> ¬ifications) override; 162 163 /** 164 * @brief Obtains the number of active notifications of the current application in the system. 165 * 166 * @param num Indicates the number of active notifications of the current application. 167 * @return Returns ERR_OK on success, others on failure. 168 */ 169 ErrCode GetActiveNotificationNums(uint64_t &num) override; 170 171 /** 172 * @brief Obtains all active notifications in the current system. The caller must have system permissions to 173 * call this method. 174 * 175 * @param notifications Indicates all active notifications of this application. 176 * @return Returns ERR_OK on success, others on failure. 177 */ 178 ErrCode GetAllActiveNotifications(std::vector<sptr<Notification>> ¬ifications) override; 179 180 /** 181 * @brief Obtains the active notifications corresponding to the specified key in the system. To call this method 182 * to obtain particular active notifications, you must have received the notifications and obtained the key 183 * via {Notification::GetKey()}. 184 * 185 * @param key Indicates the key array for querying corresponding active notifications. 186 * If this parameter is null, this method returns all active notifications in the system. 187 * @param notification Indicates the set of active notifications corresponding to the specified key. 188 * @return Returns ERR_OK on success, others on failure. 189 */ 190 ErrCode GetSpecialActiveNotifications( 191 const std::vector<std::string> &key, std::vector<sptr<Notification>> ¬ifications) override; 192 193 /** 194 * @brief Allows another application to act as an agent to publish notifications in the name of your application 195 * bundle. 196 * 197 * @param agent Indicates the name of the application bundle that can publish notifications for your application. 198 * @return Returns ERR_OK on success, others on failure. 199 */ 200 ErrCode SetNotificationAgent(const std::string &agent) override; 201 202 /** 203 * @brief Obtains the name of the application bundle that can publish notifications in the name of your application. 204 * 205 * @param agent Indicates the name of the application bundle that can publish notifications for your application if 206 * any; returns null otherwise. 207 * @return Returns ERR_OK on success, others on failure. 208 */ 209 ErrCode GetNotificationAgent(std::string &agent) override; 210 211 /** 212 * @brief Checks whether your application has permission to publish notifications by calling 213 * PublishNotificationAsBundle(string, NotificationRequest) in the name of another application indicated by the 214 * given representativeBundle. 215 * 216 * @param representativeBundle Indicates the name of application bundle your application is representing. 217 * @param canPublish Indicates whether your application has permission to publish notifications. 218 * @return Returns ERR_OK on success, others on failure. 219 */ 220 ErrCode CanPublishAsBundle(const std::string &representativeBundle, bool &canPublish) override; 221 222 /** 223 * @brief Publishes a notification in the name of a specified application bundle. 224 * @note If the notification to be published has the same ID as a published notification that has not been canceled, 225 * the existing notification will be replaced by the new one. 226 * 227 * @param notification Indicates the NotificationRequest object for setting the notification content. 228 * This parameter must be specified. 229 * @param representativeBundle Indicates the name of the application bundle that allows your application to publish 230 * notifications for it by calling setNotificationAgent. 231 * @return Returns ERR_OK on success, others on failure. 232 */ 233 ErrCode PublishAsBundle( 234 const sptr<NotificationRequest> notification, const std::string &representativeBundle) override; 235 236 /** 237 * @brief Sets the number of active notifications of the current application as the number to be displayed on the 238 * notification badge. 239 * 240 * @param num Indicates the badge number. 241 * @return Returns ERR_OK on success, others on failure. 242 */ 243 ErrCode SetNotificationBadgeNum(int32_t num) override; 244 245 /** 246 * @brief Obtains the importance level of this application. 247 * 248 * @param importance Indicates the importance level of this application, which can be LEVEL_NONE, 249 LEVEL_MIN, LEVEL_LOW, LEVEL_DEFAULT, LEVEL_HIGH, or LEVEL_UNDEFINED. 250 * @return Returns ERR_OK on success, others on failure. 251 */ 252 ErrCode GetBundleImportance(int32_t &importance) override; 253 254 /** 255 * @brief Checks whether this application has permission to modify the Do Not Disturb (DND) notification policy. 256 * 257 * @param granted True if the application has permission; false for otherwise. 258 * @return Returns ERR_OK on success, others on failure. 259 */ 260 ErrCode HasNotificationPolicyAccessPermission(bool &granted) override; 261 262 /** 263 * @brief Set whether to allow private notifications. 264 * 265 * @param allow Indicates the flag that allows private notification. 266 * @return Returns ERR_OK on success, others on failure. 267 */ 268 ErrCode SetPrivateNotificationsAllowed(bool allow) override; 269 270 /** 271 * @brief Get whether to allow private notifications. 272 * 273 * @param allow Indicates the flag that allows private notification. 274 * @return Returns ERR_OK on success, others on failure. 275 */ 276 ErrCode GetPrivateNotificationsAllowed(bool &allow) override; 277 278 /** 279 * @brief Delete notification. 280 * 281 * @param bundleOption Indicates the NotificationBundleOption of the notification. 282 * @param notificationId Indicates the id of the notification. 283 * @param label Indicates the label of the notification. 284 * @param removeReason Indicates the reason of remove notification. 285 * @return Returns ERR_OK on success, others on failure. 286 */ 287 ErrCode RemoveNotification(const sptr<NotificationBundleOption> &bundleOption, int32_t notificationId, 288 const std::string &label, int32_t removeReason) override; 289 290 /** 291 * @brief Delete all notifications. 292 * 293 * @param bundleOption Indicates the NotificationBundleOption of notifications. 294 * @return Returns ERR_OK on success, others on failure. 295 */ 296 ErrCode RemoveAllNotifications(const sptr<NotificationBundleOption> &bundleOption) override; 297 298 /** 299 * @brief Delete notification based on key. 300 * 301 * @param key Indicates the key to delete notification. 302 * @param removeReason Indicates the reason of remove notification. 303 * @return Returns ERR_OK on success, others on failure. 304 */ 305 ErrCode Delete(const std::string &key, int32_t removeReason) override; 306 307 /** 308 * @brief Remove notifications based on bundle. 309 * 310 * @param bundleOption Indicates the NotificationBundleOption of notifications. 311 * @return Returns ERR_OK on success, others on failure. 312 */ 313 ErrCode DeleteByBundle(const sptr<NotificationBundleOption> &bundleOption) override; 314 315 /** 316 * @brief Remove all notifications. 317 * 318 * @return Returns ERR_OK on success, others on failure. 319 */ 320 ErrCode DeleteAll() override; 321 322 /** 323 * @brief Get all the slots corresponding to the bundle. 324 * 325 * @param bundleOption Indicates the NotificationBundleOption object. 326 * @param slots Indicates the notification slots. 327 * @return Returns ERR_OK on success, others on failure. 328 */ 329 ErrCode GetSlotsByBundle( 330 const sptr<NotificationBundleOption> &bundleOption, std::vector<sptr<NotificationSlot>> &slots) override; 331 332 /** 333 * @brief Update slots according to bundle. 334 * 335 * @param bundleOption Indicates the NotificationBundleOption object. 336 * @param slots Indicates the notification slots to be updated. 337 * @return Returns ERR_OK on success, others on failure. 338 */ 339 ErrCode UpdateSlots( 340 const sptr<NotificationBundleOption> &bundleOption, const std::vector<sptr<NotificationSlot>> &slots) override; 341 342 /** 343 * @brief Allow notifications to be sent based on the deviceId. 344 * 345 * @param deviceId Indicates the device Id. 346 * @return Returns ERR_OK on success, others on failure. 347 */ 348 ErrCode RequestEnableNotification(const std::string &deviceId) override; 349 350 /** 351 * @brief Set whether to allow the specified deviceId to send notifications for current bundle. 352 * 353 * @param deviceId Indicates the device Id. 354 * @param enabled Indicates the flag that allows notification to be pulished. 355 * @return Returns ERR_OK on success, others on failure. 356 */ 357 ErrCode SetNotificationsEnabledForBundle(const std::string &deviceId, bool enabled) override; 358 359 /** 360 * @brief Set whether to allow the specified deviceId to send notifications for all bundles. 361 * 362 * @param deviceId Indicates the device Id. 363 * @param enabled Indicates the flag that allows notification to be pulished. 364 * @return Returns ERR_OK on success, others on failure. 365 */ 366 ErrCode SetNotificationsEnabledForAllBundles(const std::string &deviceId, bool enabled) override; 367 368 /** 369 * @brief Set whether to allow the specified bundle to send notifications. 370 * 371 * @param bundleOption Indicates the NotificationBundleOption object. 372 * @param enabled Indicates the flag that allows notification to be pulished. 373 * @return Returns ERR_OK on success, others on failure. 374 */ 375 ErrCode SetNotificationsEnabledForSpecialBundle( 376 const std::string &deviceId, const sptr<NotificationBundleOption> &bundleOption, bool enabled) override; 377 378 /** 379 * @brief Sets whether the bundle allows the banner to display notification. 380 * 381 * @param bundleOption Indicates the NotificationBundleOption object. 382 * @param enabled Indicates the flag that allows badge to be shown. 383 * @return Returns ERR_OK on success, others on failure. 384 */ 385 ErrCode SetShowBadgeEnabledForBundle(const sptr<NotificationBundleOption> &bundleOption, bool enabled) override; 386 387 /** 388 * @brief Gets whether the bundle allows the badge to display the status of notifications. 389 * 390 * @param bundleOption Indicates the NotificationBundleOption object. 391 * @param enabled Indicates the flag that allows badge to be shown. 392 * @return Returns ERR_OK on success, others on failure. 393 */ 394 ErrCode GetShowBadgeEnabledForBundle(const sptr<NotificationBundleOption> &bundleOption, bool &enabled) override; 395 396 /** 397 * @brief Gets whether allows the badge to display the status of notifications. 398 * 399 * @param enabled Indicates the flag that allows badge to be shown. 400 * @return Returns ERR_OK on success, others on failure. 401 */ 402 ErrCode GetShowBadgeEnabled(bool &enabled) override; 403 404 /** 405 * @brief Subscribes notifications. 406 * 407 * @param subscriber Indicates the subscriber. 408 * @param info Indicates the NotificationSubscribeInfo object. 409 * @return Returns ERR_OK on success, others on failure. 410 */ 411 ErrCode Subscribe(const sptr<AnsSubscriberInterface> &subscriber, 412 const sptr<NotificationSubscribeInfo> &info) override; 413 414 /** 415 * @brief Unsubscribes notifications. 416 * 417 * @param subscriber Indicates the subscriber. 418 * @param info Indicates the NotificationSubscribeInfo object. 419 * @return Returns ERR_OK on success, others on failure. 420 */ 421 ErrCode Unsubscribe(const sptr<AnsSubscriberInterface> &subscriber, 422 const sptr<NotificationSubscribeInfo> &info) override; 423 424 /** 425 * @brief Checks whether this device is allowed to publish notifications. 426 * 427 * @param allowed Indicates the flag that allows notification. 428 * @return Returns ERR_OK on success, others on failure. 429 */ 430 ErrCode IsAllowedNotify(bool &allowed) override; 431 432 /** 433 * @brief Checks whether this application is allowed to publish notifications. 434 * 435 * @param allowed Indicates the flag that allows notification. 436 * @return Returns ERR_OK on success, others on failure. 437 */ 438 ErrCode IsAllowedNotifySelf(bool &allowed) override; 439 440 /** 441 * @brief Checks whether notifications are allowed for a specific bundle. 442 * 443 * @param bundleOption Indicates the NotificationBundleOption object. 444 * @param allowed Indicates the flag that allows notification. 445 * @return Returns ERR_OK on success, others on failure. 446 */ 447 ErrCode IsSpecialBundleAllowedNotify(const sptr<NotificationBundleOption> &bundleOption, bool &allowed) override; 448 449 /** 450 * @brief Set do not disturb date. 451 * 452 * @param date Indicates the NotificationDoNotDisturbDate object. 453 * @return Returns ERR_OK on success, others on failure. 454 */ 455 ErrCode SetDoNotDisturbDate(const sptr<NotificationDoNotDisturbDate> &date) override; 456 457 /** 458 * @brief Get do not disturb date. 459 * 460 * @param date Indicates the NotificationDoNotDisturbDate object. 461 * @return Returns ERR_OK on success, others on failure. 462 */ 463 ErrCode GetDoNotDisturbDate(sptr<NotificationDoNotDisturbDate> &date) override; 464 465 /** 466 * @brief Get whether Do Not Disturb mode is supported. 467 * 468 * @param doesSupport Indicates the flag that supports DND mode. 469 * @return Returns ERR_OK on success, others on failure. 470 */ 471 ErrCode DoesSupportDoNotDisturbMode(bool &doesSupport) override; 472 473 /** 474 * @brief Cancel notifications according to group. 475 * 476 * @param groupName Indicates the group name. 477 * @return Returns ERR_OK on success, others on failure. 478 */ 479 ErrCode CancelGroup(const std::string &groupName) override; 480 481 /** 482 * @brief Delete notifications according to bundle and group. 483 * 484 * @param bundleOption Indicates the NotificationBundleOption object. 485 * @param groupName Indicates the group name. 486 * @return Returns ERR_OK on success, others on failure. 487 */ 488 ErrCode RemoveGroupByBundle( 489 const sptr<NotificationBundleOption> &bundleOption, const std::string &groupName) override; 490 491 /** 492 * @brief Gets whether distributed notification is enabled. 493 * 494 * @param enabled Indicates the enabled flag. 495 * @return Returns ERR_OK on success, others on failure. 496 */ 497 ErrCode IsDistributedEnabled(bool &enabled) override; 498 499 /** 500 * @brief Sets distributed notification enabled or disabled. 501 * 502 * @param enabled Indicates the enabled flag. 503 * @return Returns ERR_OK on success, others on failure. 504 */ 505 ErrCode EnableDistributed(bool enabled) override; 506 507 /** 508 * @brief Sets distributed notification enabled or disabled for specific bundle. 509 * 510 * @param bundleOption Indicates the NotificationBundleOption object. 511 * @param enabled Indicates the enabled flag. 512 * @return Returns ERR_OK on success, others on failure. 513 */ 514 ErrCode EnableDistributedByBundle(const sptr<NotificationBundleOption> &bundleOption, bool enabled) override; 515 516 /** 517 * @brief Sets distributed notification enabled or disabled for current bundle. 518 * 519 * @param enabled Indicates the enabled flag. 520 * @return Returns ERR_OK on success, others on failure. 521 */ 522 ErrCode EnableDistributedSelf(bool enabled) override; 523 524 /** 525 * @brief Gets whether distributed notification is enabled for specific bundle. 526 * 527 * @param bundleOption Indicates the NotificationBundleOption object. 528 * @param enabled Indicates the enabled flag. 529 * @return Returns ERR_OK on success, others on failure. 530 */ 531 ErrCode IsDistributedEnableByBundle(const sptr<NotificationBundleOption> &bundleOption, bool &enabled) override; 532 533 /** 534 * @brief Get the reminder type of the current device. 535 * 536 * @param remindType Reminder type for the device. 537 * @return Returns ERR_OK on success, others on failure. 538 */ 539 ErrCode GetDeviceRemindType(NotificationConstant::RemindType &remindType) override; 540 541 /** 542 * @brief Publishes a continuous notification. 543 * 544 * @param request Notification requests that need to be posted. 545 * @return Returns ERR_OK on success, others on failure. 546 */ 547 ErrCode PublishContinuousTaskNotification(const sptr<NotificationRequest> &request) override; 548 549 /** 550 * @brief Cancels a continuous notification. 551 * 552 * @param label Identifies the label of the specified notification. 553 * @param notificationId Identifies the id of the specified notification. 554 * @return Returns ERR_OK on success, others on failure. 555 */ 556 ErrCode CancelContinuousTaskNotification(const std::string &label, int32_t notificationId) override; 557 558 /** 559 * @brief Publishes a reminder notification. 560 * 561 * @param reminder Identifies the reminder notification request that needs to be published. 562 * @return Returns ERR_OK on success, others on failure. 563 */ 564 ErrCode PublishReminder(sptr<ReminderRequest> &reminder) override; 565 566 /** 567 * @brief Cancel a reminder notifications. 568 * 569 * @param reminderId Identifies the reminders id that needs to be canceled. 570 * @return Returns ERR_OK on success, others on failure. 571 */ 572 ErrCode CancelReminder(const int32_t reminderId) override; 573 574 /** 575 * @brief Get all valid reminder notifications. 576 * 577 * @param reminders Identifies the list of all valid notifications. 578 * @return Returns ERR_OK on success, others on failure. 579 */ 580 ErrCode GetValidReminders(std::vector<sptr<ReminderRequest>> &reminders) override; 581 582 /** 583 * @brief Cancel all reminder notifications. 584 * 585 * @return Returns ERR_OK on success, others on failure. 586 */ 587 ErrCode CancelAllReminders() override; 588 589 /** 590 * @brief Checks whether this device is support template. 591 * 592 * @param templateName Identifies the template name for searching as a condition. 593 * @param support Identifies the support flag. 594 * @return Returns ERR_OK on success, others on failure. 595 */ 596 ErrCode IsSupportTemplate(const std::string &templateName, bool &support) override; 597 598 /** 599 * @brief Checks Whether the specified users is allowed to publish notifications. 600 * 601 * @param userId Identifies the user's id. 602 * @param allowed Identifies the allowed flag. 603 * @return Returns ERR_OK on success, others on failure. 604 */ 605 ErrCode IsSpecialUserAllowedNotify(const int32_t &userId, bool &allowed) override; 606 607 /** 608 * @brief Sets whether to allow all applications to publish notifications on a specified device. The caller must 609 * have system permissions to call this method. 610 * 611 * @param deviceId Indicates the ID of the device running the application. At present, this parameter can only 612 * be null or an empty string, indicating the current device. 613 * @param enabled Specifies whether to allow all applications to publish notifications. The value true 614 * indicates that notifications are allowed, and the value false indicates that notifications 615 * are not allowed. 616 * @return Returns ERR_OK on success, others on failure. 617 */ 618 ErrCode SetNotificationsEnabledByUser(const int32_t &deviceId, bool enabled) override; 619 620 /** 621 * @brief Delete all notifications by user. 622 * 623 * @param userId Indicates the user id. 624 * @return Returns ERR_OK on success, others on failure. 625 */ 626 ErrCode DeleteAllByUser(const int32_t &userId) override; 627 628 /** 629 * @brief Set do not disturb date by user. 630 * 631 * @param userId Indicates the user id. 632 * @param date Indicates NotificationDoNotDisturbDate object. 633 * @return Returns ERR_OK on success, others on failure. 634 */ 635 ErrCode SetDoNotDisturbDate(const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date) override; 636 637 /** 638 * @brief Get the do not disturb date by user. 639 * 640 * @param userId Indicates the user id. 641 * @param date Indicates the NotificationDoNotDisturbDate object. 642 * @return Returns ERR_OK on success, others on failure. 643 */ 644 ErrCode GetDoNotDisturbDate(const int32_t &userId, sptr<NotificationDoNotDisturbDate> &date) override; 645 ErrCode SetEnabledForBundleSlot(const sptr<NotificationBundleOption> &bundleOption, 646 const NotificationConstant::SlotType &slotType, bool enabled) override; 647 ErrCode GetEnabledForBundleSlot(const sptr<NotificationBundleOption> &bundleOption, 648 const NotificationConstant::SlotType &slotType, bool &enabled) override; 649 650 // SystemEvent 651 652 /** 653 * @brief Obtains the event of bundle removed. 654 * 655 * @param bundleOption Indicates the bundle info. 656 */ 657 void OnBundleRemoved(const sptr<NotificationBundleOption> &bundleOption); 658 659 /** 660 * @brief Set whether to sync notifications to devices that do not have the app installed. 661 * 662 * @param userId Indicates the specific user. 663 * @param enabled Allow or disallow sync notifications. 664 * @return Returns set enabled result. 665 */ 666 ErrCode SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled) override; 667 668 /** 669 * @brief Obtains whether to sync notifications to devices that do not have the app installed. 670 * 671 * @param userId Indicates the specific user. 672 * @param enabled Allow or disallow sync notifications. 673 * @return Returns get enabled result. 674 */ 675 ErrCode GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled) override; 676 677 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED 678 /** 679 * @brief Obtains the event of turn on screen. 680 */ 681 void OnScreenOn(); 682 683 /** 684 * @brief Obtains the event of turn off screen. 685 */ 686 void OnScreenOff(); 687 #endif 688 void OnResourceRemove(int32_t userId); 689 void OnBundleDataCleared(const sptr<NotificationBundleOption> &bundleOption); 690 691 // Distributed KvStore 692 693 /** 694 * @brief Obtains the death event of the Distributed KvStore service. 695 */ 696 void OnDistributedKvStoreDeathRecipient(); 697 698 ErrCode CancelPreparedNotification( 699 int32_t notificationId, const std::string &label, const sptr<NotificationBundleOption> &bundleOption); 700 ErrCode PrepareNotificationInfo( 701 const sptr<NotificationRequest> &request, sptr<NotificationBundleOption> &bundleOption); 702 ErrCode PublishPreparedNotification( 703 const sptr<NotificationRequest> &request, const sptr<NotificationBundleOption> &bundleOption); 704 705 /** 706 * @brief Dump current running status for debuging. 707 * 708 * @param cmd Indicates the specified dump command. 709 * @param bundle Indicates the specified bundle name. 710 * @param userId Indicates the specified userId. 711 * @param dumpInfo Indicates the container containing datas. 712 * @return Returns ERR_OK on success, others on failure. 713 */ 714 ErrCode ShellDump(const std::string &cmd, const std::string &bundle, int32_t userId, 715 std::vector<std::string> &dumpInfo) override; 716 717 private: 718 struct RecentInfo; 719 AdvancedNotificationService(); 720 721 void StartFilters(); 722 void StopFilters(); 723 ErrCode Filter(const std::shared_ptr<NotificationRecord> &record); 724 725 void AddToNotificationList(const std::shared_ptr<NotificationRecord> &record); 726 void UpdateInNotificationList(const std::shared_ptr<NotificationRecord> &record); 727 ErrCode AssignToNotificationList(const std::shared_ptr<NotificationRecord> &record); 728 ErrCode RemoveFromNotificationList(const sptr<NotificationBundleOption> &bundleOption, const std::string &label, 729 int32_t notificationId, sptr<Notification> ¬ification, bool isCancel = false); 730 ErrCode RemoveFromNotificationList(const std::string &key, sptr<Notification> ¬ification, 731 bool isCancel, int32_t removeReason); 732 ErrCode RemoveFromNotificationListForDeleteAll(const std::string &key, 733 const int32_t &userId, sptr<Notification> ¬ification); 734 std::vector<std::string> GetNotificationKeys(const sptr<NotificationBundleOption> &bundleOption); 735 bool IsNotificationExists(const std::string &key); 736 void SortNotificationList(); 737 static bool NotificationCompare( 738 const std::shared_ptr<NotificationRecord> &first, const std::shared_ptr<NotificationRecord> &second); 739 ErrCode FlowControl(const std::shared_ptr<NotificationRecord> &record); 740 741 sptr<NotificationSortingMap> GenerateSortingMap(); 742 sptr<NotificationBundleOption> GenerateBundleOption(); 743 sptr<NotificationBundleOption> GenerateValidBundleOption(const sptr<NotificationBundleOption> &bundleOption); 744 745 std::string TimeToString(int64_t time); 746 int64_t GetNowSysTime(); 747 ErrCode ActiveNotificationDump(const std::string& bundle, int32_t userId, std::vector<std::string> &dumpInfo); 748 ErrCode RecentNotificationDump(const std::string& bundle, int32_t userId, std::vector<std::string> &dumpInfo); 749 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED 750 ErrCode DistributedNotificationDump(const std::string& bundle, int32_t userId, 751 std::vector<std::string> &dumpInfo); 752 #endif 753 ErrCode SetRecentNotificationCount(const std::string arg); 754 void UpdateRecentNotification(sptr<Notification> ¬ification, bool isDelete, int32_t reason); 755 756 void AdjustDateForDndTypeOnce(int64_t &beginDate, int64_t &endDate); 757 bool CheckPermission(const std::string &permission); 758 ErrCode PrepareNotificationRequest(const sptr<NotificationRequest> &request); 759 ErrCode PrepareContinuousTaskNotificationRequest(const sptr<NotificationRequest> &request, const int32_t &uid); 760 bool GetActiveUserId(int& userId); 761 void TriggerRemoveWantAgent(const sptr<NotificationRequest> &request); 762 bool CheckApiCompatibility(const sptr<NotificationBundleOption> &bundleOption); 763 ErrCode IsAllowedNotifySelf(const sptr<NotificationBundleOption> &bundleOption, bool &allowed); 764 void GetDisplayPosition(int& offsetX, int& offsetY, int& width, int& height, bool& wideScreen); 765 766 ErrCode SetNotificationRemindType(sptr<Notification> notification, bool isLocal); 767 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED 768 std::vector<std::string> GetLocalNotificationKeys(const sptr<NotificationBundleOption> &bundleOption); 769 NotificationConstant::RemindType GetRemindType(); 770 ErrCode DoDistributedPublish( 771 const sptr<NotificationBundleOption> bundleOption, const std::shared_ptr<NotificationRecord> record); 772 ErrCode DoDistributedDelete( 773 const std::string deviceId, const std::string bundleName, const sptr<Notification> notification); 774 void GetDistributedInfo(const std::string &key, std::string &deviceId, std::string &bundleName); 775 bool CheckDistributedNotificationType(const sptr<NotificationRequest> &request); 776 void OnDistributedPublish( 777 const std::string &deviceId, const std::string &bundleName, sptr<NotificationRequest> &request); 778 void OnDistributedUpdate( 779 const std::string &deviceId, const std::string &bundleName, sptr<NotificationRequest> &request); 780 void OnDistributedDelete( 781 const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id); 782 ErrCode GetDistributedEnableInApplicationInfo(const sptr<NotificationBundleOption> bundleOption, bool &enable); 783 bool CheckPublishWithoutApp(const int32_t userId, const sptr<NotificationRequest> &request); 784 #endif 785 786 ErrCode SetDoNotDisturbDateByUser(const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date); 787 ErrCode GetDoNotDisturbDateByUser(const int32_t &userId, sptr<NotificationDoNotDisturbDate> &date); 788 ErrCode SetHasPoppedDialog(const sptr<NotificationBundleOption> bundleOption, bool hasPopped); 789 ErrCode GetHasPoppedDialog(const sptr<NotificationBundleOption> bundleOption, bool &hasPopped); 790 ErrCode GetAppTargetBundle(const sptr<NotificationBundleOption> &bundleOption, 791 sptr<NotificationBundleOption> &targetBundle); 792 bool PublishSlotChangeCommonEvent(const sptr<NotificationBundleOption> &bundleOption); 793 void ReportInfoToResourceSchedule(const int32_t userId, const std::string &bundleName); 794 int Dump(int fd, const std::vector<std::u16string> &args) override; 795 void GetDumpInfo(const std::vector<std::u16string> &args, std::string &result); 796 797 void SendSubscribeHiSysEvent(int32_t pid, int32_t uid, const sptr<NotificationSubscribeInfo> &info, 798 ErrCode errCode); 799 void SendUnSubscribeHiSysEvent(int32_t pid, int32_t uid, const sptr<NotificationSubscribeInfo> &info); 800 void SendPublishHiSysEvent(const sptr<NotificationRequest> &request, ErrCode errCode); 801 void SendCancelHiSysEvent(int32_t notificationId, const std::string &label, 802 const sptr<NotificationBundleOption> &bundleOption, ErrCode errCode); 803 void SendRemoveHiSysEvent(int32_t notificationId, const std::string &label, 804 const sptr<NotificationBundleOption> &bundleOption, ErrCode errCode); 805 void SendEnableNotificationHiSysEvent(const sptr<NotificationBundleOption> &bundleOption, bool enabled, 806 ErrCode errCode); 807 void SendEnableNotificationSlotHiSysEvent(const sptr<NotificationBundleOption> &bundleOption, 808 const NotificationConstant::SlotType &slotType, bool enabled, ErrCode errCode); 809 void SendFlowControlOccurHiSysEvent(const std::shared_ptr<NotificationRecord> &record); 810 ErrCode PublishNotificationBySa(const sptr<NotificationRequest> &request); 811 812 private: 813 static sptr<AdvancedNotificationService> instance_; 814 static std::mutex instanceMutex_; 815 816 std::shared_ptr<OHOS::AppExecFwk::EventRunner> runner_ = nullptr; 817 std::shared_ptr<OHOS::AppExecFwk::EventHandler> handler_ = nullptr; 818 std::list<std::shared_ptr<NotificationRecord>> notificationList_; 819 std::list<std::chrono::system_clock::time_point> flowControlTimestampList_; 820 std::shared_ptr<RecentInfo> recentInfo_ = nullptr; 821 std::shared_ptr<DistributedKvStoreDeathRecipient> distributedKvStoreDeathRecipient_ = nullptr; 822 std::shared_ptr<SystemEventObserver> systemEventObserver_ = nullptr; 823 DistributedKv::DistributedKvDataManager dataManager_; 824 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED 825 NotificationConstant::DistributedReminderPolicy distributedReminderPolicy_ = DEFAULT_DISTRIBUTED_REMINDER_POLICY; 826 bool localScreenOn_ = true; 827 #endif 828 }; 829 } // namespace Notification 830 } // namespace OHOS 831 832 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_ADVANCED_NOTIFICATION_SERVICE_H 833