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_dialog_manager.h" 36 #include "notification_record.h" 37 #include "notification_slot_filter.h" 38 #include "notification_sorting_map.h" 39 #include "permission_filter.h" 40 #include "push_callback_interface.h" 41 #include "system_event_observer.h" 42 #include "notification_subscriber_manager.h" 43 44 namespace OHOS { 45 namespace Notification { 46 class AdvancedNotificationService final : public AnsManagerStub { 47 public: 48 struct NotificationRequestDb { 49 sptr<NotificationRequest> request {nullptr}; 50 sptr<NotificationBundleOption> bundleOption {nullptr}; 51 }; 52 53 struct RecentNotification { 54 sptr<Notification> notification = nullptr; 55 bool isActive = false; 56 int32_t deleteReason = 0; 57 int64_t deleteTime = 0; 58 }; 59 60 ~AdvancedNotificationService() override; 61 62 DISALLOW_COPY_AND_MOVE(AdvancedNotificationService); 63 64 /** 65 * @brief Get the instance of service. 66 * 67 * @return Returns the instance. 68 */ 69 static sptr<AdvancedNotificationService> GetInstance(); 70 71 static std::map<std::string, uint32_t>& GetDefaultSlotConfig(); 72 73 void SelfClean(); 74 75 /** 76 * @brief Get notification_svr_queue of service. 77 * 78 * @return Returns the queue. 79 */ 80 std::shared_ptr<ffrt::queue> GetNotificationSvrQueue(); 81 82 // AnsManagerStub 83 84 /** 85 * @brief Publishes a notification with a specified label. 86 * @note If a notification with the same ID has been published by the current application and has not been deleted, 87 * this method will update the notification. 88 * 89 * @param label Indicates the label of the notification to publish. 90 * @param notification Indicates the NotificationRequest object for setting the notification content. 91 * This parameter must be specified. 92 * @return Returns ERR_OK on success, others on failure. 93 */ 94 ErrCode Publish(const std::string &label, const sptr<NotificationRequest> &request) override; 95 96 /** 97 * @brief Cancels a published notification matching the specified label and notificationId. 98 * 99 * @param notificationId Indicates the ID of the notification to cancel. 100 * @param label Indicates the label of the notification to cancel. 101 * @return Returns cancel notification result. 102 */ 103 ErrCode Cancel(int32_t notificationId, const std::string &label) override; 104 105 /** 106 * @brief Cancels all the published notifications. 107 * 108 * @return Returns ERR_OK on success, others on failure. 109 */ 110 ErrCode CancelAll() override; 111 112 /** 113 * @brief Cancels a published agent notification. 114 * 115 * @param notificationId Indicates the unique notification ID in the application. 116 * The value must be the ID of a published notification. 117 * Otherwise, this method does not take effect. 118 * @param representativeBundle Indicates the name of application bundle your application is representing. 119 * @param userId Indicates the specific user. 120 * @return Returns cancel notification result. 121 */ 122 ErrCode CancelAsBundle( 123 int32_t notificationId, const std::string &representativeBundle, int32_t userId) override; 124 125 /** 126 * @brief Adds a notification slot by type. 127 * 128 * @param slotType Indicates the notification slot type to be added. 129 * @return Returns ERR_OK on success, others on failure. 130 */ 131 ErrCode AddSlotByType(NotificationConstant::SlotType slotType) override; 132 133 /** 134 * @brief Creates multiple notification slots. 135 * 136 * @param slots Indicates the notification slots to create. 137 * @return Returns ERR_OK on success, others on failure. 138 */ 139 ErrCode AddSlots(const std::vector<sptr<NotificationSlot>> &slots) override; 140 141 /** 142 * @brief Deletes a created notification slot based on the slot ID. 143 * 144 * @param slotType Indicates the type of the slot, which is created by AddNotificationSlot 145 * This parameter must be specified. 146 * @return Returns ERR_OK on success, others on failure. 147 */ 148 ErrCode RemoveSlotByType(const NotificationConstant::SlotType &slotType) override; 149 150 /** 151 * @brief Deletes all notification slots. 152 * 153 * @return Returns ERR_OK on success, others on failure. 154 */ 155 ErrCode RemoveAllSlots() override; 156 157 /** 158 * @brief Queries a created notification slot. 159 * 160 * @param slotType Indicates the ID of the slot, which is created by AddNotificationSlot(NotificationSlot). This 161 * parameter must be specified. 162 * @param slot Indicates the created NotificationSlot. 163 * @return Returns ERR_OK on success, others on failure. 164 */ 165 ErrCode GetSlotByType(const NotificationConstant::SlotType &slotType, sptr<NotificationSlot> &slot) override; 166 167 /** 168 * @brief Obtains all notification slots of this application. 169 * 170 * @param slots Indicates the created NotificationSlot. 171 * @return Returns ERR_OK on success, others on failure. 172 */ 173 ErrCode GetSlots(std::vector<sptr<NotificationSlot>> &slots) override; 174 175 /** 176 * @brief Obtains the number of slot. 177 * 178 * @param bundleOption Indicates the bundle name and uid of the application. 179 * @param num Indicates the number of slot. 180 * @return Returns ERR_OK on success, others on failure. 181 */ 182 ErrCode GetSlotNumAsBundle(const sptr<NotificationBundleOption> &bundleOption, uint64_t &num) override; 183 184 /** 185 * @brief Obtains active notifications of the current application in the system. 186 * 187 * @param notifications Indicates active NotificationRequest objects of the current application. 188 * @return Returns ERR_OK on success, others on failure. 189 */ 190 ErrCode GetActiveNotifications(std::vector<sptr<NotificationRequest>> ¬ifications) override; 191 192 /** 193 * @brief Obtains the number of active notifications of the current application in the system. 194 * 195 * @param num Indicates the number of active notifications of the current application. 196 * @return Returns ERR_OK on success, others on failure. 197 */ 198 ErrCode GetActiveNotificationNums(uint64_t &num) override; 199 200 /** 201 * @brief Obtains all active notifications in the current system. The caller must have system permissions to 202 * call this method. 203 * 204 * @param notifications Indicates all active notifications of this application. 205 * @return Returns ERR_OK on success, others on failure. 206 */ 207 ErrCode GetAllActiveNotifications(std::vector<sptr<Notification>> ¬ifications) override; 208 209 /** 210 * @brief Obtains the active notifications corresponding to the specified key in the system. To call this method 211 * to obtain particular active notifications, you must have received the notifications and obtained the key 212 * via {Notification::GetKey()}. 213 * 214 * @param key Indicates the key array for querying corresponding active notifications. 215 * If this parameter is null, this method returns all active notifications in the system. 216 * @param notification Indicates the set of active notifications corresponding to the specified key. 217 * @return Returns ERR_OK on success, others on failure. 218 */ 219 ErrCode GetSpecialActiveNotifications( 220 const std::vector<std::string> &key, std::vector<sptr<Notification>> ¬ifications) override; 221 222 ErrCode GetActiveNotificationByFilter( 223 const sptr<NotificationBundleOption> &bundleOption, const int32_t notificationId, const std::string &label, 224 const std::vector<std::string> extraInfoKeys, sptr<NotificationRequest> &request) override; 225 226 /** 227 * @brief Allows another application to act as an agent to publish notifications in the name of your application 228 * bundle. 229 * 230 * @param agent Indicates the name of the application bundle that can publish notifications for your application. 231 * @return Returns ERR_OK on success, others on failure. 232 */ 233 ErrCode SetNotificationAgent(const std::string &agent) override; 234 235 /** 236 * @brief Obtains the name of the application bundle that can publish notifications in the name of your application. 237 * 238 * @param agent Indicates the name of the application bundle that can publish notifications for your application if 239 * any; returns null otherwise. 240 * @return Returns ERR_OK on success, others on failure. 241 */ 242 ErrCode GetNotificationAgent(std::string &agent) override; 243 244 /** 245 * @brief Checks whether your application has permission to publish notifications by calling 246 * PublishNotificationAsBundle(string, NotificationRequest) in the name of another application indicated by the 247 * given representativeBundle. 248 * 249 * @param representativeBundle Indicates the name of application bundle your application is representing. 250 * @param canPublish Indicates whether your application has permission to publish notifications. 251 * @return Returns ERR_OK on success, others on failure. 252 */ 253 ErrCode CanPublishAsBundle(const std::string &representativeBundle, bool &canPublish) override; 254 255 /** 256 * @brief Publishes a notification in the name of a specified application bundle. 257 * @note If the notification to be published has the same ID as a published notification that has not been canceled, 258 * the existing notification will be replaced by the new one. 259 * 260 * @param notification Indicates the NotificationRequest object for setting the notification content. 261 * This parameter must be specified. 262 * @param representativeBundle Indicates the name of the application bundle that allows your application to publish 263 * notifications for it by calling setNotificationAgent. 264 * @return Returns ERR_OK on success, others on failure. 265 */ 266 ErrCode PublishAsBundle( 267 const sptr<NotificationRequest> notification, const std::string &representativeBundle) override; 268 269 /** 270 * @brief Sets the number of active notifications of the current application as the number to be displayed on the 271 * notification badge. 272 * 273 * @param num Indicates the badge number. 274 * @return Returns ERR_OK on success, others on failure. 275 */ 276 ErrCode SetNotificationBadgeNum(int32_t num) override; 277 278 /** 279 * @brief Obtains the importance level of this application. 280 * 281 * @param importance Indicates the importance level of this application, which can be LEVEL_NONE, 282 LEVEL_MIN, LEVEL_LOW, LEVEL_DEFAULT, LEVEL_HIGH, or LEVEL_UNDEFINED. 283 * @return Returns ERR_OK on success, others on failure. 284 */ 285 ErrCode GetBundleImportance(int32_t &importance) override; 286 287 /** 288 * @brief Checks whether this application has permission to modify the Do Not Disturb (DND) notification policy. 289 * 290 * @param granted True if the application has permission; false for otherwise. 291 * @return Returns ERR_OK on success, others on failure. 292 */ 293 ErrCode HasNotificationPolicyAccessPermission(bool &granted) override; 294 295 /** 296 * @brief Trigger the local live view after the button has been clicked. 297 * @note Your application must have platform signature to use this method. 298 * 299 * @param bundleOption Indicates the bundle name and uid of the application whose notifications has been clicked. 300 * @param notificationId Indicates the id of the notification. 301 * @param buttonOption Indicates which button has been clicked. 302 * @return Returns trigger localLiveView result. 303 */ 304 ErrCode TriggerLocalLiveView(const sptr<NotificationBundleOption> &bundleOption, 305 const int32_t notificationId, const sptr<NotificationButtonOption> &buttonOption) override; 306 307 /** 308 * @brief Delete notification. 309 * 310 * @param bundleOption Indicates the NotificationBundleOption of the notification. 311 * @param notificationId Indicates the id of the notification. 312 * @param label Indicates the label of the notification. 313 * @param removeReason Indicates the reason of remove notification. 314 * @return Returns ERR_OK on success, others on failure. 315 */ 316 ErrCode RemoveNotification(const sptr<NotificationBundleOption> &bundleOption, int32_t notificationId, 317 const std::string &label, int32_t removeReason) override; 318 319 /** 320 * @brief Delete all notifications. 321 * 322 * @param bundleOption Indicates the NotificationBundleOption of notifications. 323 * @return Returns ERR_OK on success, others on failure. 324 */ 325 ErrCode RemoveAllNotifications(const sptr<NotificationBundleOption> &bundleOption) override; 326 327 ErrCode RemoveNotifications(const std::vector<std::string> &keys, int32_t removeReason) override; 328 329 ErrCode RemoveNotificationBySlot(const sptr<NotificationBundleOption> &bundleOption, 330 const sptr<NotificationSlot> &slot); 331 332 /** 333 * @brief Delete notification based on key. 334 * 335 * @param key Indicates the key to delete notification. 336 * @param removeReason Indicates the reason of remove notification. 337 * @return Returns ERR_OK on success, others on failure. 338 */ 339 ErrCode Delete(const std::string &key, int32_t removeReason) override; 340 341 /** 342 * @brief Remove notifications based on bundle. 343 * 344 * @param bundleOption Indicates the NotificationBundleOption of notifications. 345 * @return Returns ERR_OK on success, others on failure. 346 */ 347 ErrCode DeleteByBundle(const sptr<NotificationBundleOption> &bundleOption) override; 348 349 /** 350 * @brief Remove all notifications. 351 * 352 * @return Returns ERR_OK on success, others on failure. 353 */ 354 ErrCode DeleteAll() override; 355 356 /** 357 * @brief Get all the slots corresponding to the bundle. 358 * 359 * @param bundleOption Indicates the NotificationBundleOption object. 360 * @param slots Indicates the notification slots. 361 * @return Returns ERR_OK on success, others on failure. 362 */ 363 ErrCode GetSlotsByBundle( 364 const sptr<NotificationBundleOption> &bundleOption, std::vector<sptr<NotificationSlot>> &slots) override; 365 366 /** 367 * @brief Update slots according to bundle. 368 * 369 * @param bundleOption Indicates the NotificationBundleOption object. 370 * @param slots Indicates the notification slots to be updated. 371 * @return Returns ERR_OK on success, others on failure. 372 */ 373 ErrCode UpdateSlots( 374 const sptr<NotificationBundleOption> &bundleOption, const std::vector<sptr<NotificationSlot>> &slots) override; 375 376 /** 377 * @brief Allow notifications to be sent based on the deviceId. 378 * 379 * @param deviceId Indicates the device Id. 380 * @return Returns ERR_OK on success, others on failure. 381 */ 382 ErrCode RequestEnableNotification(const std::string &deviceId, 383 const sptr<AnsDialogCallback> &callback, 384 const sptr<IRemoteObject> &callerToken) override; 385 386 /** 387 * @brief Set whether to allow the specified deviceId to send notifications for current bundle. 388 * 389 * @param deviceId Indicates the device Id. 390 * @param enabled Indicates the flag that allows notification to be pulished. 391 * @return Returns ERR_OK on success, others on failure. 392 */ 393 ErrCode SetNotificationsEnabledForBundle(const std::string &deviceId, bool enabled) override; 394 395 /** 396 * @brief Set whether to allow the specified deviceId to send notifications for all bundles. 397 * 398 * @param deviceId Indicates the device Id. 399 * @param enabled Indicates the flag that allows notification to be pulished. 400 * @return Returns ERR_OK on success, others on failure. 401 */ 402 ErrCode SetNotificationsEnabledForAllBundles(const std::string &deviceId, bool enabled) override; 403 404 /** 405 * @brief Set whether to allow the specified bundle to send notifications. 406 * 407 * @param bundleOption Indicates the NotificationBundleOption object. 408 * @param enabled Indicates the flag that allows notification to be pulished. 409 * @return Returns ERR_OK on success, others on failure. 410 */ 411 ErrCode SetNotificationsEnabledForSpecialBundle( 412 const std::string &deviceId, const sptr<NotificationBundleOption> &bundleOption, bool enabled) override; 413 414 /** 415 * @brief Sets whether the bundle allows the banner to display notification. 416 * 417 * @param bundleOption Indicates the NotificationBundleOption object. 418 * @param enabled Indicates the flag that allows badge to be shown. 419 * @return Returns ERR_OK on success, others on failure. 420 */ 421 ErrCode SetShowBadgeEnabledForBundle(const sptr<NotificationBundleOption> &bundleOption, bool enabled) override; 422 423 /** 424 * @brief Gets whether the bundle allows the badge to display the status of notifications. 425 * 426 * @param bundleOption Indicates the NotificationBundleOption object. 427 * @param enabled Indicates the flag that allows badge to be shown. 428 * @return Returns ERR_OK on success, others on failure. 429 */ 430 ErrCode GetShowBadgeEnabledForBundle(const sptr<NotificationBundleOption> &bundleOption, bool &enabled) override; 431 432 /** 433 * @brief Gets whether allows the badge to display the status of notifications. 434 * 435 * @param enabled Indicates the flag that allows badge to be shown. 436 * @return Returns ERR_OK on success, others on failure. 437 */ 438 ErrCode GetShowBadgeEnabled(bool &enabled) override; 439 440 /** 441 * @brief Subscribes notifications. 442 * 443 * @param subscriber Indicates the subscriber. 444 * @param info Indicates the NotificationSubscribeInfo object. 445 * @return Returns ERR_OK on success, others on failure. 446 */ 447 ErrCode Subscribe(const sptr<AnsSubscriberInterface> &subscriber, 448 const sptr<NotificationSubscribeInfo> &info) override; 449 450 /** 451 * @brief Subscribes notifications self. 452 * 453 * @param subscriber Indicates the subscriber. 454 * @return Returns ERR_OK on success, others on failure. 455 */ 456 ErrCode SubscribeSelf(const sptr<AnsSubscriberInterface> &subscriber) override; 457 458 /** 459 * @brief Subscribes notifications. 460 * 461 * @param subscriber Indicates the subscriber. 462 * @param info Indicates the NotificationSubscribeInfo object. 463 * @return Returns ERR_OK on success, others on failure. 464 */ 465 ErrCode SubscribeLocalLiveView(const sptr<AnsSubscriberLocalLiveViewInterface> &subscriber, 466 const sptr<NotificationSubscribeInfo> &info) override; 467 468 /** 469 * @brief Unsubscribes notifications. 470 * 471 * @param subscriber Indicates the subscriber. 472 * @param info Indicates the NotificationSubscribeInfo object. 473 * @return Returns ERR_OK on success, others on failure. 474 */ 475 ErrCode Unsubscribe(const sptr<AnsSubscriberInterface> &subscriber, 476 const sptr<NotificationSubscribeInfo> &info) override; 477 478 /** 479 * @brief Checks whether this device is allowed to publish notifications. 480 * 481 * @param allowed Indicates the flag that allows notification. 482 * @return Returns ERR_OK on success, others on failure. 483 */ 484 ErrCode IsAllowedNotify(bool &allowed) override; 485 486 /** 487 * @brief Checks whether this application is allowed to publish notifications. 488 * 489 * @param allowed Indicates the flag that allows notification. 490 * @return Returns ERR_OK on success, others on failure. 491 */ 492 ErrCode IsAllowedNotifySelf(bool &allowed) override; 493 494 /** 495 * @brief Checks whether notifications are allowed for a specific bundle. 496 * 497 * @param bundleOption Indicates the NotificationBundleOption object. 498 * @param allowed Indicates the flag that allows notification. 499 * @return Returns ERR_OK on success, others on failure. 500 */ 501 ErrCode IsSpecialBundleAllowedNotify(const sptr<NotificationBundleOption> &bundleOption, bool &allowed) override; 502 503 /** 504 * @brief Set do not disturb date. 505 * 506 * @param date Indicates the NotificationDoNotDisturbDate object. 507 * @return Returns ERR_OK on success, others on failure. 508 */ 509 ErrCode SetDoNotDisturbDate(const sptr<NotificationDoNotDisturbDate> &date) override; 510 511 /** 512 * @brief Get do not disturb date. 513 * 514 * @param date Indicates the NotificationDoNotDisturbDate object. 515 * @return Returns ERR_OK on success, others on failure. 516 */ 517 ErrCode GetDoNotDisturbDate(sptr<NotificationDoNotDisturbDate> &date) override; 518 519 /** 520 * @brief Get whether Do Not Disturb mode is supported. 521 * 522 * @param doesSupport Indicates the flag that supports DND mode. 523 * @return Returns ERR_OK on success, others on failure. 524 */ 525 ErrCode DoesSupportDoNotDisturbMode(bool &doesSupport) override; 526 527 /** 528 * @brief Cancel notifications according to group. 529 * 530 * @param groupName Indicates the group name. 531 * @return Returns ERR_OK on success, others on failure. 532 */ 533 ErrCode CancelGroup(const std::string &groupName) override; 534 535 /** 536 * @brief Delete notifications according to bundle and group. 537 * 538 * @param bundleOption Indicates the NotificationBundleOption object. 539 * @param groupName Indicates the group name. 540 * @return Returns ERR_OK on success, others on failure. 541 */ 542 ErrCode RemoveGroupByBundle( 543 const sptr<NotificationBundleOption> &bundleOption, const std::string &groupName) override; 544 545 /** 546 * @brief Gets whether distributed notification is enabled. 547 * 548 * @param enabled Indicates the enabled flag. 549 * @return Returns ERR_OK on success, others on failure. 550 */ 551 ErrCode IsDistributedEnabled(bool &enabled) override; 552 553 /** 554 * @brief Sets distributed notification enabled or disabled. 555 * 556 * @param enabled Indicates the enabled flag. 557 * @return Returns ERR_OK on success, others on failure. 558 */ 559 ErrCode EnableDistributed(bool enabled) override; 560 561 /** 562 * @brief Sets distributed notification enabled or disabled for specific bundle. 563 * 564 * @param bundleOption Indicates the NotificationBundleOption object. 565 * @param enabled Indicates the enabled flag. 566 * @return Returns ERR_OK on success, others on failure. 567 */ 568 ErrCode EnableDistributedByBundle(const sptr<NotificationBundleOption> &bundleOption, bool enabled) override; 569 570 /** 571 * @brief Sets distributed notification enabled or disabled for current bundle. 572 * 573 * @param enabled Indicates the enabled flag. 574 * @return Returns ERR_OK on success, others on failure. 575 */ 576 ErrCode EnableDistributedSelf(bool enabled) override; 577 578 /** 579 * @brief Gets whether distributed notification is enabled for specific bundle. 580 * 581 * @param bundleOption Indicates the NotificationBundleOption object. 582 * @param enabled Indicates the enabled flag. 583 * @return Returns ERR_OK on success, others on failure. 584 */ 585 ErrCode IsDistributedEnableByBundle(const sptr<NotificationBundleOption> &bundleOption, bool &enabled) override; 586 587 /** 588 * @brief Get the reminder type of the current device. 589 * 590 * @param remindType Reminder type for the device. 591 * @return Returns ERR_OK on success, others on failure. 592 */ 593 ErrCode GetDeviceRemindType(NotificationConstant::RemindType &remindType) override; 594 595 /** 596 * @brief Publishes a continuous notification. 597 * 598 * @param request Notification requests that need to be posted. 599 * @return Returns ERR_OK on success, others on failure. 600 */ 601 ErrCode PublishContinuousTaskNotification(const sptr<NotificationRequest> &request) override; 602 603 /** 604 * @brief Cancels a continuous notification. 605 * 606 * @param label Identifies the label of the specified notification. 607 * @param notificationId Identifies the id of the specified notification. 608 * @return Returns ERR_OK on success, others on failure. 609 */ 610 ErrCode CancelContinuousTaskNotification(const std::string &label, int32_t notificationId) override; 611 612 /** 613 * @brief Check reminder permission 614 */ 615 bool CheckReminderPermission(); 616 617 /** 618 * @brief Publishes a reminder notification. 619 * 620 * @param reminder Identifies the reminder notification request that needs to be published. 621 * @return Returns ERR_OK on success, others on failure. 622 */ 623 ErrCode PublishReminder(sptr<ReminderRequest> &reminder) override; 624 625 /** 626 * @brief Cancel a reminder notifications. 627 * 628 * @param reminderId Identifies the reminders id that needs to be canceled. 629 * @return Returns ERR_OK on success, others on failure. 630 */ 631 ErrCode CancelReminder(const int32_t reminderId) override; 632 633 /** 634 * @brief Get all valid reminder notifications. 635 * 636 * @param reminders Identifies the list of all valid notifications. 637 * @return Returns ERR_OK on success, others on failure. 638 */ 639 ErrCode GetValidReminders(std::vector<sptr<ReminderRequest>> &reminders) override; 640 641 /** 642 * @brief Cancel all reminder notifications. 643 * 644 * @return Returns ERR_OK on success, others on failure. 645 */ 646 ErrCode CancelAllReminders() override; 647 648 /** 649 * @brief Checks whether this device is support template. 650 * 651 * @param templateName Identifies the template name for searching as a condition. 652 * @param support Identifies the support flag. 653 * @return Returns ERR_OK on success, others on failure. 654 */ 655 ErrCode IsSupportTemplate(const std::string &templateName, bool &support) override; 656 657 /** 658 * @brief Checks Whether the specified users is allowed to publish notifications. 659 * 660 * @param userId Identifies the user's id. 661 * @param allowed Identifies the allowed flag. 662 * @return Returns ERR_OK on success, others on failure. 663 */ 664 ErrCode IsSpecialUserAllowedNotify(const int32_t &userId, bool &allowed) override; 665 666 /** 667 * @brief Sets whether to allow all applications to publish notifications on a specified device. The caller must 668 * have system permissions to call this method. 669 * 670 * @param deviceId Indicates the ID of the device running the application. At present, this parameter can only 671 * be null or an empty string, indicating the current device. 672 * @param enabled Specifies whether to allow all applications to publish notifications. The value true 673 * indicates that notifications are allowed, and the value false indicates that notifications 674 * are not allowed. 675 * @return Returns ERR_OK on success, others on failure. 676 */ 677 ErrCode SetNotificationsEnabledByUser(const int32_t &deviceId, bool enabled) override; 678 679 /** 680 * @brief Delete all notifications by user. 681 * 682 * @param userId Indicates the user id. 683 * @return Returns ERR_OK on success, others on failure. 684 */ 685 ErrCode DeleteAllByUser(const int32_t &userId) override; 686 687 /** 688 * @brief Set do not disturb date by user. 689 * 690 * @param userId Indicates the user id. 691 * @param date Indicates NotificationDoNotDisturbDate object. 692 * @return Returns ERR_OK on success, others on failure. 693 */ 694 ErrCode SetDoNotDisturbDate(const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date) override; 695 696 /** 697 * @brief Get the do not disturb date by user. 698 * 699 * @param userId Indicates the user id. 700 * @param date Indicates the NotificationDoNotDisturbDate object. 701 * @return Returns ERR_OK on success, others on failure. 702 */ 703 ErrCode GetDoNotDisturbDate(const int32_t &userId, sptr<NotificationDoNotDisturbDate> &date) override; 704 ErrCode SetEnabledForBundleSlot(const sptr<NotificationBundleOption> &bundleOption, 705 const NotificationConstant::SlotType &slotType, bool enabled, bool isForceControl) override; 706 ErrCode GetEnabledForBundleSlot(const sptr<NotificationBundleOption> &bundleOption, 707 const NotificationConstant::SlotType &slotType, bool &enabled) override; 708 ErrCode GetEnabledForBundleSlotSelf(const NotificationConstant::SlotType &slotType, bool &enabled) override; 709 710 // SystemEvent 711 712 /** 713 * @brief Obtains the event of bundle removed. 714 * 715 * @param bundleOption Indicates the bundle info. 716 */ 717 void OnBundleRemoved(const sptr<NotificationBundleOption> &bundleOption); 718 719 /** 720 * @brief Set whether to sync notifications to devices that do not have the app installed. 721 * 722 * @param userId Indicates the specific user. 723 * @param enabled Allow or disallow sync notifications. 724 * @return Returns set enabled result. 725 */ 726 ErrCode SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled) override; 727 728 /** 729 * @brief Obtains whether to sync notifications to devices that do not have the app installed. 730 * 731 * @param userId Indicates the specific user. 732 * @param enabled Allow or disallow sync notifications. 733 * @return Returns get enabled result. 734 */ 735 ErrCode GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled) override; 736 737 /** 738 * @brief Obtains the number of slotFlags. 739 * 740 * @param bundleOption Indicates the bundle name and uid of the application. 741 * @param slot Indicates the specified slot object 742 * @param slotFlags Indicates the slogFlags of slot. 743 * @return Returns ERR_OK on success, others on failure. 744 */ 745 virtual ErrCode GetSlotFlagsAsBundle(const sptr<NotificationBundleOption>& bundleOption, 746 uint32_t &slotFlags) override; 747 748 /** 749 * @brief Set the slotFlags of slot. 750 * 751 * @param bundleOption Indicates the bundle name and uid of the application. 752 * @param slot Indicates the specified slot object 753 * @param slotFlags Indicates the slogFlags of slot to set. 754 * @return Returns ERR_OK on success, others on failure. 755 */ 756 virtual ErrCode SetSlotFlagsAsBundle(const sptr<NotificationBundleOption>& bundleOption, 757 uint32_t slotFlags) override; 758 759 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED 760 /** 761 * @brief Obtains the event of turn on screen. 762 */ 763 void OnScreenOn(); 764 765 /** 766 * @brief Obtains the event of turn off screen. 767 */ 768 void OnScreenOff(); 769 #endif 770 void OnResourceRemove(int32_t userId); 771 void OnBundleDataCleared(const sptr<NotificationBundleOption> &bundleOption); 772 773 /** 774 * @brief Obtains the event of bundle install. 775 * 776 * @param bundleOption Indicates the bundle info. 777 */ 778 void OnBundleDataAdd(const sptr<NotificationBundleOption> &bundleOption); 779 780 /** 781 * @brief Obtains the event of bundle update. 782 * 783 * @param bundleOption Indicates the bundle info. 784 */ 785 void OnBundleDataUpdate(const sptr<NotificationBundleOption> &bundleOption); 786 787 /** 788 * @brief Boot system completed event callback. 789 */ 790 void OnBootSystemCompleted(); 791 792 // Distributed KvStore 793 794 /** 795 * @brief Obtains the death event of the Distributed KvStore service. 796 */ 797 void OnDistributedKvStoreDeathRecipient(); 798 799 ErrCode CancelPreparedNotification( 800 int32_t notificationId, const std::string &label, const sptr<NotificationBundleOption> &bundleOption); 801 ErrCode PrepareNotificationInfo( 802 const sptr<NotificationRequest> &request, sptr<NotificationBundleOption> &bundleOption); 803 ErrCode PublishPreparedNotification( 804 const sptr<NotificationRequest> &request, const sptr<NotificationBundleOption> &bundleOption); 805 806 /** 807 * @brief Dump current running status for debuging. 808 * 809 * @param cmd Indicates the specified dump command. 810 * @param bundle Indicates the specified bundle name. 811 * @param userId Indicates the specified userId. 812 * @param dumpInfo Indicates the container containing datas. 813 * @return Returns ERR_OK on success, others on failure. 814 */ 815 ErrCode ShellDump(const std::string &cmd, const std::string &bundle, int32_t userId, 816 std::vector<std::string> &dumpInfo) override; 817 818 /** 819 * @brief Set badge number. 820 * 821 * @param badgeNumber The badge number. 822 * @return Returns set badge number result. 823 */ 824 ErrCode SetBadgeNumber(int32_t badgeNumber) override; 825 826 /** 827 * @brief Register Push Callback. 828 * 829 * @param pushCallback PushCallBack. 830 * @param notificationCheckRequest Filter conditions for push check 831 * @return Returns register push Callback result. 832 */ 833 ErrCode RegisterPushCallback(const sptr<IRemoteObject>& pushCallback, 834 const sptr<NotificationCheckRequest> ¬ificationCheckRequest) override; 835 836 /** 837 * @brief Unregister Push Callback. 838 * 839 * @return Returns unregister push Callback result. 840 */ 841 ErrCode UnregisterPushCallback() override; 842 843 /** 844 * @brief Reset pushcallback proxy 845 */ 846 void ResetPushCallbackProxy(); 847 848 /** 849 * @brief Init The Default Installation Package Notification Enabled. 850 */ 851 void InitNotificationEnableList(); 852 /** 853 * @brief Remove Local Live Notifications 854 */ 855 ErrCode RemoveSystemLiveViewNotifications(const std::string& bundleName); 856 857 /** 858 * @brief Set the notification SoundEnabled and VibrationEnabled by soltType 859 */ 860 void SetRequestBySlotType(const sptr<NotificationRequest> &request); 861 862 // Might fail if ces subscribe failed, if failed, dialogManager_ will be set nullptr 863 bool CreateDialogManager(); 864 865 private: 866 struct RecentInfo { 867 std::list<std::shared_ptr<RecentNotification>> list; 868 size_t recentCount = 16; 869 }; 870 871 AdvancedNotificationService(); 872 873 void StartFilters(); 874 void StopFilters(); 875 ErrCode Filter(const std::shared_ptr<NotificationRecord> &record, bool isRecover = false); 876 877 void AddToNotificationList(const std::shared_ptr<NotificationRecord> &record); 878 void UpdateInNotificationList(const std::shared_ptr<NotificationRecord> &record); 879 ErrCode AssignToNotificationList(const std::shared_ptr<NotificationRecord> &record); 880 ErrCode RemoveFromNotificationList(const sptr<NotificationBundleOption> &bundleOption, const std::string &label, 881 int32_t notificationId, sptr<Notification> ¬ification, bool isCancel = false); 882 ErrCode RemoveFromNotificationList(const std::string &key, sptr<Notification> ¬ification, 883 bool isCancel, int32_t removeReason); 884 ErrCode RemoveFromNotificationListForDeleteAll(const std::string &key, 885 const int32_t &userId, sptr<Notification> ¬ification); 886 std::shared_ptr<NotificationRecord> GetFromNotificationList(const std::string &key); 887 std::vector<std::string> GetNotificationKeys(const sptr<NotificationBundleOption> &bundleOption); 888 bool IsNotificationExists(const std::string &key); 889 void SortNotificationList(); 890 static bool NotificationCompare( 891 const std::shared_ptr<NotificationRecord> &first, const std::shared_ptr<NotificationRecord> &second); 892 ErrCode FlowControl(const std::shared_ptr<NotificationRecord> &record); 893 894 sptr<NotificationSortingMap> GenerateSortingMap(); 895 static sptr<NotificationBundleOption> GenerateBundleOption(); 896 static sptr<NotificationBundleOption> GenerateValidBundleOption(const sptr<NotificationBundleOption> &bundleOption); 897 898 std::string TimeToString(int64_t time); 899 int64_t GetNowSysTime(); 900 ErrCode ActiveNotificationDump(const std::string& bundle, int32_t userId, std::vector<std::string> &dumpInfo); 901 ErrCode RecentNotificationDump(const std::string& bundle, int32_t userId, std::vector<std::string> &dumpInfo); 902 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED 903 ErrCode DistributedNotificationDump(const std::string& bundle, int32_t userId, 904 std::vector<std::string> &dumpInfo); 905 #endif 906 ErrCode SetRecentNotificationCount(const std::string arg); 907 void UpdateRecentNotification(sptr<Notification> ¬ification, bool isDelete, int32_t reason); 908 909 void AdjustDateForDndTypeOnce(int64_t &beginDate, int64_t &endDate); 910 static bool CheckPermission(const std::string &permission); 911 ErrCode PrepareNotificationRequest(const sptr<NotificationRequest> &request); 912 ErrCode PrepareContinuousTaskNotificationRequest(const sptr<NotificationRequest> &request, const int32_t &uid); 913 static bool GetActiveUserId(int& userId); 914 void TriggerRemoveWantAgent(const sptr<NotificationRequest> &request); 915 bool CheckApiCompatibility(const sptr<NotificationBundleOption> &bundleOption); 916 ErrCode IsAllowedNotifySelf(const sptr<NotificationBundleOption> &bundleOption, bool &allowed); 917 918 ErrCode SetNotificationRemindType(sptr<Notification> notification, bool isLocal); 919 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED 920 std::vector<std::string> GetLocalNotificationKeys(const sptr<NotificationBundleOption> &bundleOption); 921 NotificationConstant::RemindType GetRemindType(); 922 ErrCode DoDistributedPublish( 923 const sptr<NotificationBundleOption> bundleOption, const std::shared_ptr<NotificationRecord> record); 924 ErrCode DoDistributedDelete( 925 const std::string deviceId, const std::string bundleName, const sptr<Notification> notification); 926 void GetDistributedInfo(const std::string &key, std::string &deviceId, std::string &bundleName); 927 bool CheckDistributedNotificationType(const sptr<NotificationRequest> &request); 928 void OnDistributedPublish( 929 const std::string &deviceId, const std::string &bundleName, sptr<NotificationRequest> &request); 930 void OnDistributedUpdate( 931 const std::string &deviceId, const std::string &bundleName, sptr<NotificationRequest> &request); 932 void OnDistributedDelete( 933 const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id); 934 static ErrCode GetDistributedEnableInApplicationInfo( 935 const sptr<NotificationBundleOption> bundleOption, bool &enable); 936 bool CheckPublishWithoutApp(const int32_t userId, const sptr<NotificationRequest> &request); 937 void InitDistributeCallBack(); 938 #endif 939 940 ErrCode SetDoNotDisturbDateByUser(const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date); 941 ErrCode GetDoNotDisturbDateByUser(const int32_t &userId, sptr<NotificationDoNotDisturbDate> &date); 942 ErrCode GetHasPoppedDialog(const sptr<NotificationBundleOption> bundleOption, bool &hasPopped); 943 static ErrCode GetAppTargetBundle(const sptr<NotificationBundleOption> &bundleOption, 944 sptr<NotificationBundleOption> &targetBundle); 945 bool PublishSlotChangeCommonEvent(const sptr<NotificationBundleOption> &bundleOption); 946 void ReportInfoToResourceSchedule(const int32_t userId, const std::string &bundleName); 947 int Dump(int fd, const std::vector<std::u16string> &args) override; 948 void GetDumpInfo(const std::vector<std::u16string> &args, std::string &result); 949 950 static void SendSubscribeHiSysEvent(int32_t pid, int32_t uid, const sptr<NotificationSubscribeInfo> &info, 951 ErrCode errCode); 952 static void SendUnSubscribeHiSysEvent(int32_t pid, int32_t uid, const sptr<NotificationSubscribeInfo> &info); 953 void SendPublishHiSysEvent(const sptr<NotificationRequest> &request, ErrCode errCode); 954 void SendCancelHiSysEvent(int32_t notificationId, const std::string &label, 955 const sptr<NotificationBundleOption> &bundleOption, ErrCode errCode); 956 void SendRemoveHiSysEvent(int32_t notificationId, const std::string &label, 957 const sptr<NotificationBundleOption> &bundleOption, ErrCode errCode); 958 void SendEnableNotificationHiSysEvent(const sptr<NotificationBundleOption> &bundleOption, bool enabled, 959 ErrCode errCode); 960 void SendEnableNotificationSlotHiSysEvent(const sptr<NotificationBundleOption> &bundleOption, 961 const NotificationConstant::SlotType &slotType, bool enabled, ErrCode errCode); 962 void SendFlowControlOccurHiSysEvent(const std::shared_ptr<NotificationRecord> &record); 963 ErrCode PrePublishNotificationBySa(const sptr<NotificationRequest> &request, int32_t uid, std::string &bundle); 964 ErrCode PublishNotificationBySa(const sptr<NotificationRequest> &request); 965 bool IsNeedPushCheck(const sptr<NotificationRequest> &request); 966 void FillExtraInfoToJson(const sptr<NotificationRequest> &request, 967 sptr<NotificationCheckRequest> &checkRequest, nlohmann::json &jsonObject); 968 ErrCode PushCheck(const sptr<NotificationRequest> &request); 969 uint64_t StartAutoDelete(const std::string &key, int64_t deleteTimePoint, int32_t reason); 970 void TriggerAutoDelete(const std::string &hashCode, int32_t reason); 971 void SendNotificationsOnCanceled(std::vector<sptr<Notification>> ¬ifications, 972 const sptr<NotificationSortingMap> ¬ificationMap, int32_t deleteReason); 973 void SetAgentNotification(sptr<NotificationRequest>& notificationRequest, std::string& bundleName); 974 ErrCode SetDefaultNotificationEnabled( 975 const sptr<NotificationBundleOption> &bundleOption, bool enabled); 976 static bool GetBundleInfoByNotificationBundleOption( 977 const sptr<NotificationBundleOption> &bundleOption, AppExecFwk::BundleInfo &bundleInfo); 978 979 ErrCode GetTargetRecordList(const std::string& bundleName, NotificationConstant::SlotType slotType, 980 NotificationContent::Type contentType, std::vector<std::shared_ptr<NotificationRecord>>& recordList); 981 ErrCode RemoveNotificationFromRecordList(const std::vector<std::shared_ptr<NotificationRecord>>& recordList); 982 void OnSubscriberAdd(const std::shared_ptr<NotificationSubscriberManager::SubscriberRecord> &record); 983 void RecoverLiveViewFromDb(); 984 bool IsLiveViewCanRecover(const sptr<NotificationRequest> request); 985 ErrCode FillNotificationRecord(const NotificationRequestDb &requestdbObj, 986 std::shared_ptr<NotificationRecord> record); 987 static int32_t SetNotificationRequestToDb(const NotificationRequestDb &requestDb); 988 static int32_t GetNotificationRequestFromDb(const std::string &key, NotificationRequestDb &requestDb); 989 static int32_t GetBatchNotificationRequestsFromDb(std::vector<NotificationRequestDb> &requests); 990 static int32_t DeleteNotificationRequestFromDb(const std::string &key); 991 void CancelAutoDeleteTimer(uint64_t timerId); 992 ErrCode UpdateNotificationTimerInfo(const std::shared_ptr<NotificationRecord> &record); 993 ErrCode SetFinishTimer(const std::shared_ptr<NotificationRecord> &record); 994 ErrCode StartFinishTimer(const std::shared_ptr<NotificationRecord> &record, int64_t expireTimePoint); 995 void CancelFinishTimer(const std::shared_ptr<NotificationRecord> &record); 996 ErrCode SetUpdateTimer(const std::shared_ptr<NotificationRecord> &record); 997 ErrCode StartUpdateTimer(const std::shared_ptr<NotificationRecord> &record, int64_t expireTimePoint); 998 void CancelUpdateTimer(const std::shared_ptr<NotificationRecord> &record); 999 void StartArchiveTimer(const std::shared_ptr<NotificationRecord> &record); 1000 void CancelArchiveTimer(const std::shared_ptr<NotificationRecord> &record); 1001 void ProcForDeleteLiveView(const std::shared_ptr<NotificationRecord> &record); 1002 ErrCode CheckCommonParams(); 1003 std::shared_ptr<NotificationRecord> GetRecordFromNotificationList( 1004 int32_t notificationId, int32_t uid, const std::string &label, const std::string &bundleName); 1005 void AddLiveViewSubscriber(); 1006 void EraseLiveViewSubsciber(const std::string &bundleName); 1007 bool GetLiveViewSubscribeState(const std::string &bundleName); 1008 bool CheckLocalLiveViewSubscribed(const sptr<NotificationRequest> &request); 1009 bool CheckLocalLiveViewAllowed(const sptr<NotificationRequest> &request); 1010 std::shared_ptr<NotificationRecord> MakeNotificationRecord( 1011 const sptr<NotificationRequest> &request, const sptr<NotificationBundleOption> &bundleOption); 1012 ErrCode IsAllowedNotifyForBundle(const sptr<NotificationBundleOption> &bundleOption, bool &allowed); 1013 void FillActionButtons(const sptr<NotificationRequest> &request); 1014 ErrCode IsAllowedGetNotificationByFilter(const std::shared_ptr<NotificationRecord> &record); 1015 ErrCode FillRequestByKeys(const sptr<NotificationRequest> &oldRequest, 1016 const std::vector<std::string> extraInfoKeys, sptr<NotificationRequest> &newRequest); 1017 ErrCode IsAllowedRemoveSlot(const sptr<NotificationBundleOption> &bundleOption, 1018 const NotificationConstant::SlotType &slotType); 1019 bool IsNeedNotifyConsumed(const sptr<NotificationRequest> &request); 1020 private: 1021 static sptr<AdvancedNotificationService> instance_; 1022 static std::mutex instanceMutex_; 1023 static std::mutex pushMutex_; 1024 static std::map<NotificationConstant::SlotType, sptr<IPushCallBack>> pushCallBacks_; 1025 static std::map<NotificationConstant::SlotType, sptr<NotificationCheckRequest>> checkRequests_; 1026 1027 std::shared_ptr<OHOS::AppExecFwk::EventRunner> runner_ = nullptr; 1028 std::shared_ptr<OHOS::AppExecFwk::EventHandler> handler_ = nullptr; 1029 std::list<std::shared_ptr<NotificationRecord>> notificationList_; 1030 std::list<std::chrono::system_clock::time_point> flowControlTimestampList_; 1031 std::shared_ptr<RecentInfo> recentInfo_ = nullptr; 1032 std::shared_ptr<DistributedKvStoreDeathRecipient> distributedKvStoreDeathRecipient_ = nullptr; 1033 std::shared_ptr<SystemEventObserver> systemEventObserver_ = nullptr; 1034 DistributedKv::DistributedKvDataManager dataManager_; 1035 sptr<IRemoteObject::DeathRecipient> pushRecipient_ = nullptr; 1036 std::shared_ptr<ffrt::queue> notificationSvrQueue_ = nullptr; 1037 static std::string supportCheckSaPermission_; 1038 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED 1039 NotificationConstant::DistributedReminderPolicy distributedReminderPolicy_ = DEFAULT_DISTRIBUTED_REMINDER_POLICY; 1040 bool localScreenOn_ = true; 1041 #endif 1042 std::shared_ptr<PermissionFilter> permissonFilter_ = nullptr; 1043 std::shared_ptr<NotificationSlotFilter> notificationSlotFilter_ = nullptr; 1044 std::shared_ptr<NotificationDialogManager> dialogManager_ = nullptr; 1045 std::set<std::string> localLiveViewSubscribedList_; 1046 std::mutex liveViewMutext_; 1047 }; 1048 1049 /** 1050 * @class PushCallbackRecipient 1051 * PushCallbackRecipient notices IRemoteBroker died. 1052 */ 1053 class PushCallbackRecipient : public IRemoteObject::DeathRecipient { 1054 public: 1055 PushCallbackRecipient(); 1056 virtual ~PushCallbackRecipient(); 1057 void OnRemoteDied(const wptr<IRemoteObject> &remote); 1058 }; 1059 } // namespace Notification 1060 } // namespace OHOS 1061 1062 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_ADVANCED_NOTIFICATION_SERVICE_H 1063