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