• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #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 <set>
21 #include <list>
22 #include <memory>
23 #include <mutex>
24 
25 #include "event_handler.h"
26 #include "event_runner.h"
27 #include "ffrt.h"
28 #include "refbase.h"
29 
30 #include "ans_const_define.h"
31 #include "ans_manager_stub.h"
32 #include "common_notification_publish_process.h"
33 #include "distributed_kv_data_manager.h"
34 #include "distributed_kvstore_death_recipient.h"
35 #include "live_publish_process.h"
36 #include "notification.h"
37 #include "notification_bundle_option.h"
38 #include "notification_dialog_manager.h"
39 #include "notification_do_not_disturb_profile.h"
40 #include "notification_record.h"
41 #include "notification_slot_filter.h"
42 #include "notification_sorting_map.h"
43 #include "permission_filter.h"
44 #include "push_callback_interface.h"
45 #include "system_event_observer.h"
46 #include "notification_subscriber_manager.h"
47 #include "distributed_device_status.h"
48 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
49 #include "reminder_swing_decision_center.h"
50 #endif
51 #include "notification_clone_bundle_info.h"
52 
53 namespace OHOS {
54 namespace Notification {
55 
56 static const uint32_t DEFAULT_SLOT_FLAGS = 59; // 0b111011
57 class AdvancedNotificationService final : public AnsManagerStub,
58     public std::enable_shared_from_this<AdvancedNotificationService> {
59 public:
60     struct NotificationRequestDb {
61         sptr<NotificationRequest> request {nullptr};
62         sptr<NotificationBundleOption> bundleOption {nullptr};
63     };
64 
65     struct RecentNotification {
66         sptr<Notification> notification = nullptr;
67         bool isActive = false;
68         int32_t deleteReason = 0;
69         int64_t deleteTime = 0;
70     };
71 
72     ~AdvancedNotificationService() override;
73 
74     DISALLOW_COPY_AND_MOVE(AdvancedNotificationService);
75 
76     /**
77      * @brief Get the instance of service.
78      *
79      * @return Returns the instance.
80      */
81     static sptr<AdvancedNotificationService> GetInstance();
82 
83     static std::map<std::string, uint32_t>& GetDefaultSlotConfig();
84 
85     void SelfClean();
86 
87     /**
88      * @brief Get notification_svr_queue of service.
89      *
90      * @return Returns the queue.
91      */
92     std::shared_ptr<ffrt::queue> GetNotificationSvrQueue();
93 
94     /**
95      * @brief Submit an async task into notification_svr_queue.
96      *
97      * @param func Indicates the function.
98      */
99     void SubmitAsyncTask(const std::function<void()>& func);
100     /**
101      * @brief Submit a sync task into notification_svr_queue.
102      *
103      * @param func Indicates the function.
104      */
105     void SubmitSyncTask(const std::function<void()>& func);
106 
107     // AnsManagerStub
108 
109     /**
110      * @brief Publishes a notification with a specified label.
111      * @note If a notification with the same ID has been published by the current application and has not been deleted,
112      *       this method will update the notification.
113      *
114      * @param label Indicates the label of the notification to publish.
115      * @param notification Indicates the NotificationRequest object for setting the notification content.
116      *                This parameter must be specified.
117      * @return Returns ERR_OK on success, others on failure.
118      */
119     ErrCode Publish(const std::string &label, const sptr<NotificationRequest> &request) override;
120 
121     /**
122      * @brief Publishes a notification.
123      * @note If a notification with the same ID has been published by the current application and has not been deleted,
124      *       this method will update the notification.
125      *
126      * @param notification Indicates the NotificationRequest object for setting the notification content.
127      *                This parameter must be specified.
128      * @return Returns ERR_OK on success, others on failure.
129      */
130     ErrCode PublishNotificationForIndirectProxy(const sptr<NotificationRequest> &request) override;
131 
132     /**
133      * @brief Cancels a published notification matching the specified label and notificationId.
134      *
135      * @param notificationId Indicates the ID of the notification to cancel.
136      * @param label Indicates the label of the notification to cancel.
137      * @param instanceKey Indicates the application instance key.
138      * @return Returns cancel notification result.
139      */
140     ErrCode Cancel(int32_t notificationId, const std::string &label, const std::string &instanceKey) override;
141 
142     /**
143      * @brief Cancels all the published notifications.
144      *
145      * @param instanceKey Indicates the application instance key.
146      * @return Returns ERR_OK on success, others on failure.
147      */
148     ErrCode CancelAll(const std::string &instanceKey) override;
149 
150     /**
151      * @brief Cancels a published agent notification.
152      *
153      * @param notificationId Indicates the unique notification ID in the application.
154      *                       The value must be the ID of a published notification.
155      *                       Otherwise, this method does not take effect.
156      * @param representativeBundle Indicates the name of application bundle your application is representing.
157      * @param userId Indicates the specific user.
158      * @return Returns cancel notification result.
159      */
160     ErrCode CancelAsBundle(
161         int32_t notificationId, const std::string &representativeBundle, int32_t userId) override;
162 
163     /**
164      * @brief Cancels a published agent notification.
165      *
166      * @param bundleOption Indicates the bundle of application bundle your application is representing.
167      * @param notificationId Indicates the unique notification ID in the application.
168      *                       The value must be the ID of a published notification.
169      *                       Otherwise, this method does not take effect.
170      * @return Returns cancel notification result.
171      */
172     ErrCode CancelAsBundle(const sptr<NotificationBundleOption> &bundleOption, int32_t notificationId) override;
173 
174     /**
175      * @brief Cancels a published agent notification.
176      *
177      * @param bundleOption Indicates the bundle of application bundle your application is representing.
178      * @param notificationId Indicates the unique notification ID in the application.
179      *                       The value must be the ID of a published notification.
180      *                       Otherwise, this method does not take effect.
181      * @param userId Indicates the specific user.
182      * @return Returns cancel notification result.
183      */
184     ErrCode CancelAsBundle(
185         const sptr<NotificationBundleOption> &bundleOption, int32_t notificationId, int32_t userId) override;
186 
187     /**
188      * @brief Adds a notification slot by type.
189      *
190      * @param slotType Indicates the notification slot type to be added.
191      * @return Returns ERR_OK on success, others on failure.
192      */
193     ErrCode AddSlotByType(NotificationConstant::SlotType slotType) override;
194 
195     /**
196      * @brief Creates multiple notification slots.
197      *
198      * @param slots Indicates the notification slots to create.
199      * @return Returns ERR_OK on success, others on failure.
200      */
201     ErrCode AddSlots(const std::vector<sptr<NotificationSlot>> &slots) override;
202 
203     /**
204      * @brief Deletes a created notification slot based on the slot ID.
205      *
206      * @param slotType Indicates the type of the slot, which is created by AddNotificationSlot
207      *                 This parameter must be specified.
208      * @return Returns ERR_OK on success, others on failure.
209      */
210     ErrCode RemoveSlotByType(const NotificationConstant::SlotType &slotType) override;
211 
212     /**
213      * @brief Deletes all notification slots.
214      *
215      * @return Returns ERR_OK on success, others on failure.
216      */
217     ErrCode RemoveAllSlots() override;
218 
219     /**
220      * @brief Queries a created notification slot.
221      *
222      * @param slotType Indicates the ID of the slot, which is created by AddNotificationSlot(NotificationSlot). This
223      *        parameter must be specified.
224      * @param slot Indicates the created NotificationSlot.
225      * @return Returns ERR_OK on success, others on failure.
226      */
227     ErrCode GetSlotByType(const NotificationConstant::SlotType &slotType, sptr<NotificationSlot> &slot) override;
228 
229     /**
230      * @brief Obtains all notification slots of this application.
231      *
232      * @param slots Indicates the created NotificationSlot.
233      * @return Returns ERR_OK on success, others on failure.
234      */
235     ErrCode GetSlots(std::vector<sptr<NotificationSlot>> &slots) override;
236 
237     /**
238      * @brief Obtains the number of slot.
239      *
240      * @param bundleOption Indicates the bundle name and uid of the application.
241      * @param num Indicates the number of slot.
242      * @return Returns ERR_OK on success, others on failure.
243      */
244     ErrCode GetSlotNumAsBundle(const sptr<NotificationBundleOption> &bundleOption, uint64_t &num) override;
245 
246     /**
247      * @brief Obtains active notifications of the current application in the system.
248      *
249      * @param notifications Indicates active NotificationRequest objects of the current application.
250      * @param instanceKey Indicates the application instance key.
251      * @return Returns ERR_OK on success, others on failure.
252      */
253     ErrCode GetActiveNotifications(std::vector<sptr<NotificationRequest>> &notifications,
254         const std::string &instanceKey) override;
255 
256     /**
257      * @brief Obtains the number of active notifications of the current application in the system.
258      *
259      * @param num Indicates the number of active notifications of the current application.
260      * @return Returns ERR_OK on success, others on failure.
261      */
262     ErrCode GetActiveNotificationNums(uint64_t &num) override;
263 
264     /**
265      * @brief Obtains all active notifications by slot type in the current system. The caller must have system
266      * permissions to call this method.
267      *
268      * @param notification Indicates all active notifications of this application.
269      * @return Returns get all active notifications
270      */
271     ErrCode GetAllNotificationsBySlotType(std::vector<sptr<Notification>> &notifications,
272         const NotificationConstant::SlotType slotType) override;
273 
274     /**
275      * @brief Obtains all active notifications in the current system. The caller must have system permissions to
276      * call this method.
277      *
278      * @param notifications Indicates all active notifications of this application.
279      * @return Returns ERR_OK on success, others on failure.
280      */
281     ErrCode GetAllActiveNotifications(std::vector<sptr<Notification>> &notifications) override;
282 
283     /**
284      * @brief Obtains the active notifications corresponding to the specified key in the system. To call this method
285      * to obtain particular active notifications, you must have received the notifications and obtained the key
286      * via {Notification::GetKey()}.
287      *
288      * @param key Indicates the key array for querying corresponding active notifications.
289      *            If this parameter is null, this method returns all active notifications in the system.
290      * @param notification Indicates the set of active notifications corresponding to the specified key.
291      * @return Returns ERR_OK on success, others on failure.
292      */
293     ErrCode GetSpecialActiveNotifications(
294         const std::vector<std::string> &key, std::vector<sptr<Notification>> &notifications) override;
295 
296     ErrCode GetActiveNotificationByFilter(
297         const sptr<NotificationBundleOption> &bundleOption, const int32_t notificationId, const std::string &label,
298         const std::vector<std::string> extraInfoKeys, sptr<NotificationRequest> &request) override;
299 
300     /**
301      * @brief Checks whether your application has permission to publish notifications by calling
302      * PublishNotificationAsBundle(string, NotificationRequest) in the name of another application indicated by the
303      * given representativeBundle.
304      *
305      * @param representativeBundle Indicates the name of application bundle your application is representing.
306      * @param canPublish Indicates whether your application has permission to publish notifications.
307      * @return Returns ERR_OK on success, others on failure.
308      */
309     ErrCode CanPublishAsBundle(const std::string &representativeBundle, bool &canPublish) override;
310 
311     /**
312      * @brief Publishes a notification in the name of a specified application bundle.
313      * @note If the notification to be published has the same ID as a published notification that has not been canceled,
314      * the existing notification will be replaced by the new one.
315      *
316      * @param notification Indicates the NotificationRequest object for setting the notification content.
317      *                This parameter must be specified.
318      * @param representativeBundle Indicates the name of the application bundle that allows your application to publish
319      *                             notifications for it by calling setNotificationAgent.
320      * @return Returns ERR_OK on success, others on failure.
321      */
322     ErrCode PublishAsBundle(
323         const sptr<NotificationRequest> notification, const std::string &representativeBundle) override;
324 
325     /**
326      * @brief Sets the number of active notifications of the current application as the number to be displayed on the
327      * notification badge.
328      *
329      * @param num Indicates the badge number.
330      * @return Returns ERR_OK on success, others on failure.
331      */
332     ErrCode SetNotificationBadgeNum(int32_t num) override;
333 
334     /**
335      * @brief Obtains the importance level of this application.
336      *
337      * @param importance Indicates the importance level of this application, which can be LEVEL_NONE,
338                LEVEL_MIN, LEVEL_LOW, LEVEL_DEFAULT, LEVEL_HIGH, or LEVEL_UNDEFINED.
339      * @return Returns ERR_OK on success, others on failure.
340      */
341     ErrCode GetBundleImportance(int32_t &importance) override;
342 
343     /**
344      * @brief Checks whether this application has permission to modify the Do Not Disturb (DND) notification policy.
345      *
346      * @param granted True if the application has permission; false for otherwise.
347      * @return Returns ERR_OK on success, others on failure.
348      */
349     ErrCode HasNotificationPolicyAccessPermission(bool &granted) override;
350 
351     /**
352      * @brief Trigger the local live view after the button has been clicked.
353      * @note Your application must have platform signature to use this method.
354      *
355      * @param bundleOption Indicates the bundle name and uid of the application whose notifications has been clicked.
356      * @param notificationId Indicates the id of the notification.
357      * @param buttonOption Indicates which button has been clicked.
358      * @return Returns trigger localLiveView result.
359      */
360     ErrCode TriggerLocalLiveView(const sptr<NotificationBundleOption> &bundleOption,
361         const int32_t notificationId, const sptr<NotificationButtonOption> &buttonOption) override;
362 
363     /**
364      * @brief Delete notification.
365      *
366      * @param bundleOption Indicates the NotificationBundleOption of the notification.
367      * @param notificationId Indicates the id of the notification.
368      * @param label Indicates the label of the notification.
369      * @param removeReason Indicates the reason of remove notification.
370      * @return Returns ERR_OK on success, others on failure.
371      */
372     ErrCode RemoveNotification(const sptr<NotificationBundleOption> &bundleOption, int32_t notificationId,
373         const std::string &label, int32_t removeReason) override;
374 
375     /**
376      * @brief Delete all notifications.
377      *
378      * @param bundleOption Indicates the NotificationBundleOption of notifications.
379      * @return Returns ERR_OK on success, others on failure.
380      */
381     ErrCode RemoveAllNotifications(const sptr<NotificationBundleOption> &bundleOption) override;
382 
383     ErrCode RemoveAllNotificationsForDisable(const sptr<NotificationBundleOption> &bundleOption);
384 
385     ErrCode RemoveNotifications(const std::vector<std::string> &keys, int32_t removeReason) override;
386 
387     ErrCode GetUnifiedGroupInfoFromDb(std::string &enable);
388 
389     /**
390      * @brief Delete notification based on key.
391      *
392      * @param key Indicates the key to delete notification.
393      * @param removeReason Indicates the reason of remove notification.
394      * @return Returns ERR_OK on success, others on failure.
395      */
396     ErrCode Delete(const std::string &key, int32_t removeReason) override;
397 
398     /**
399      * @brief Remove notifications based on bundle.
400      *
401      * @param bundleOption Indicates the NotificationBundleOption of notifications.
402      * @return Returns ERR_OK on success, others on failure.
403      */
404     ErrCode DeleteByBundle(const sptr<NotificationBundleOption> &bundleOption) override;
405 
406     /**
407      * @brief Remove all notifications.
408      *
409      * @return Returns ERR_OK on success, others on failure.
410      */
411     ErrCode DeleteAll() override;
412 
413     /**
414      * @brief Get all the slots corresponding to the bundle.
415      *
416      * @param bundleOption Indicates the NotificationBundleOption object.
417      * @param slots Indicates the notification slots.
418      * @return Returns ERR_OK on success, others on failure.
419      */
420     ErrCode GetSlotsByBundle(
421         const sptr<NotificationBundleOption> &bundleOption, std::vector<sptr<NotificationSlot>> &slots) override;
422 
423     /**
424      * @brief Get the specified slot corresponding to the bundle.
425      *
426      * @param bundleOption Indicates the NotificationBundleOption object.
427      * @param slotType Indicates the ID of the slot, which is created by AddNotificationSlot(NotificationSlot). This
428      *        parameter must be specified.
429      * @param slot Indicates the notification slot.
430      * @return Returns ERR_OK on success, others on failure.
431      */
432     ErrCode GetSlotByBundle(
433         const sptr<NotificationBundleOption> &bundleOption, const NotificationConstant::SlotType &slotType,
434         sptr<NotificationSlot> &slot) override;
435 
436     /**
437      * @brief Update slots according to bundle.
438      *
439      * @param bundleOption Indicates the NotificationBundleOption object.
440      * @param slots Indicates the notification slots to be updated.
441      * @return Returns ERR_OK on success, others on failure.
442      */
443     ErrCode UpdateSlots(
444         const sptr<NotificationBundleOption> &bundleOption, const std::vector<sptr<NotificationSlot>> &slots) override;
445 
446     /**
447      * @brief Allow notifications to be sent based on the deviceId.
448      *
449      * @param deviceId Indicates the device Id.
450      * @return Returns ERR_OK on success, others on failure.
451      */
452     ErrCode RequestEnableNotification(const std::string &deviceId,
453         const sptr<AnsDialogCallback> &callback,
454         const sptr<IRemoteObject> &callerToken) override;
455 
456     /**
457      * @brief Allow application to publish notifications.
458      *
459      * @param bundleName bundle name.
460      * @param uid uid.
461      * @return Returns set notifications enabled for the bundle result.
462      */
463     ErrCode RequestEnableNotification(const std::string bundleName, const int32_t uid) override;
464 
465     /**
466      * @brief Set whether to allow the specified deviceId to send notifications for current bundle.
467      *
468      * @param deviceId Indicates the device Id.
469      * @param enabled Indicates the flag that allows notification to be pulished.
470      * @return Returns ERR_OK on success, others on failure.
471      */
472     ErrCode SetNotificationsEnabledForBundle(const std::string &deviceId, bool enabled) override;
473 
474     /**
475      * @brief Set whether to allow the specified deviceId to send notifications for all bundles.
476      *
477      * @param deviceId Indicates the device Id.
478      * @param enabled Indicates the flag that allows notification to be pulished.
479      * @return Returns ERR_OK on success, others on failure.
480      */
481     ErrCode SetNotificationsEnabledForAllBundles(const std::string &deviceId, bool enabled) override;
482 
483     /**
484      * @brief Set whether to allow the specified bundle to send notifications.
485      *
486      * @param bundleOption Indicates the NotificationBundleOption object.
487      * @param enabled Indicates the flag that allows notification to be pulished.
488      * @return Returns ERR_OK on success, others on failure.
489      */
490     ErrCode SetNotificationsEnabledForSpecialBundle(
491         const std::string &deviceId, const sptr<NotificationBundleOption> &bundleOption, bool enabled) override;
492 
493     /**
494      * @brief Sets whether the bundle allows the banner to display notification.
495      *
496      * @param bundleOption Indicates the NotificationBundleOption object.
497      * @param enabled Indicates the flag that allows badge to be shown.
498      * @return Returns ERR_OK on success, others on failure.
499      */
500     ErrCode SetShowBadgeEnabledForBundle(const sptr<NotificationBundleOption> &bundleOption, bool enabled) override;
501 
502     /**
503      * @brief Gets whether the bundle allows the badge to display the status of notifications.
504      *
505      * @param bundleOption Indicates the NotificationBundleOption object.
506      * @param enabled Indicates the flag that allows badge to be shown.
507      * @return Returns ERR_OK on success, others on failure.
508      */
509     ErrCode GetShowBadgeEnabledForBundle(const sptr<NotificationBundleOption> &bundleOption, bool &enabled) override;
510 
511     /**
512      * @brief Gets whether allows the badge to display the status of notifications.
513      *
514      * @param enabled Indicates the flag that allows badge to be shown.
515      * @return Returns ERR_OK on success, others on failure.
516      */
517     ErrCode GetShowBadgeEnabled(bool &enabled) override;
518 
519     /**
520      * @brief Subscribes notifications.
521      *
522      * @param subscriber Indicates the subscriber.
523      * @param info Indicates the NotificationSubscribeInfo object.
524      * @return Returns ERR_OK on success, others on failure.
525      */
526     ErrCode Subscribe(const sptr<AnsSubscriberInterface> &subscriber,
527         const sptr<NotificationSubscribeInfo> &info) override;
528 
529     /**
530      * @brief Subscribes notifications self.
531      *
532      * @param subscriber Indicates the subscriber.
533      * @return Returns ERR_OK on success, others on failure.
534      */
535     ErrCode SubscribeSelf(const sptr<AnsSubscriberInterface> &subscriber) override;
536 
537     /**
538      * @brief Subscribes notifications.
539      *
540      * @param subscriber Indicates the subscriber.
541      * @param info Indicates the NotificationSubscribeInfo object.
542      * @return Returns ERR_OK on success, others on failure.
543      */
544     ErrCode SubscribeLocalLiveView(const sptr<AnsSubscriberLocalLiveViewInterface> &subscriber,
545         const sptr<NotificationSubscribeInfo> &info, const bool isNative) override;
546 
547     /**
548      * @brief Unsubscribes notifications.
549      *
550      * @param subscriber Indicates the subscriber.
551      * @param info Indicates the NotificationSubscribeInfo object.
552      * @return Returns ERR_OK on success, others on failure.
553      */
554     ErrCode Unsubscribe(const sptr<AnsSubscriberInterface> &subscriber,
555         const sptr<NotificationSubscribeInfo> &info) override;
556 
557     /**
558      * @brief Checks whether this device is allowed to publish notifications.
559      *
560      * @param allowed Indicates the flag that allows notification.
561      * @return Returns ERR_OK on success, others on failure.
562      */
563     ErrCode IsAllowedNotify(bool &allowed) override;
564 
565     /**
566      * @brief Checks whether this application is allowed to publish notifications.
567      *
568      * @param allowed Indicates the flag that allows notification.
569      * @return Returns ERR_OK on success, others on failure.
570      */
571     ErrCode IsAllowedNotifySelf(bool &allowed) override;
572 
573     /**
574      * @brief Checks whether this application can pop enable notification dialog.
575      *
576      * @param  canPop True if can pop enable notification dialog
577      * @return Returns is canPop result.
578      */
579     ErrCode CanPopEnableNotificationDialog(const sptr<AnsDialogCallback> &callback,
580         bool &canPop, std::string &bundleName) override;
581 
582     /**
583      * @brief remove enable notification dialog.
584      *
585      * @return Returns remove dialog result.
586      */
587     ErrCode RemoveEnableNotificationDialog() override;
588 
589     ErrCode RemoveEnableNotificationDialog(const sptr<NotificationBundleOption> &bundleOption);
590 
591     /**
592      * @brief Checks whether notifications are allowed for a specific bundle.
593      *
594      * @param bundleOption Indicates the NotificationBundleOption object.
595      * @param allowed Indicates the flag that allows notification.
596      * @return Returns ERR_OK on success, others on failure.
597      */
598     ErrCode IsSpecialBundleAllowedNotify(const sptr<NotificationBundleOption> &bundleOption, bool &allowed) override;
599 
600     /**
601      * @brief Is coming call need silent in do not disturb mode.
602      *
603      * @param phoneNumber the calling format number.
604      * @return Returns silent in do not disturb mode.
605      */
606     ErrCode IsNeedSilentInDoNotDisturbMode(const std::string &phoneNumber, int32_t callerType) override;
607 
608     /**
609      * @brief Cancel notifications according to group.
610      *
611      * @param groupName Indicates the group name.
612      * @param instanceKey Indicates the application instance key.
613      * @return Returns ERR_OK on success, others on failure.
614      */
615     ErrCode CancelGroup(const std::string &groupName, const std::string &instanceKey) override;
616 
617     /**
618      * @brief Delete notifications according to bundle and group.
619      *
620      * @param bundleOption Indicates the NotificationBundleOption object.
621      * @param groupName Indicates the group name.
622      * @return Returns ERR_OK on success, others on failure.
623      */
624     ErrCode RemoveGroupByBundle(
625         const sptr<NotificationBundleOption> &bundleOption, const std::string &groupName) override;
626 
627     /**
628      * @brief Gets whether distributed notification is enabled.
629      *
630      * @param enabled Indicates the enabled flag.
631      * @return Returns ERR_OK on success, others on failure.
632      */
633     ErrCode IsDistributedEnabled(bool &enabled) override;
634 
635     /**
636      * @brief Sets distributed notification enabled or disabled.
637      *
638      * @param enabled Indicates the enabled flag.
639      * @return Returns ERR_OK on success, others on failure.
640      */
641     ErrCode EnableDistributed(bool enabled) override;
642 
643     /**
644      * @brief Sets distributed notification enabled or disabled for specific bundle.
645      *
646      * @param bundleOption Indicates the NotificationBundleOption object.
647      * @param enabled Indicates the enabled flag.
648      * @return Returns ERR_OK on success, others on failure.
649      */
650     ErrCode EnableDistributedByBundle(const sptr<NotificationBundleOption> &bundleOption, bool enabled) override;
651 
652     /**
653      * @brief Sets distributed notification enabled or disabled for current bundle.
654      *
655      * @param enabled Indicates the enabled flag.
656      * @return Returns ERR_OK on success, others on failure.
657      */
658     ErrCode EnableDistributedSelf(bool enabled) override;
659 
660     /**
661      * @brief Gets whether distributed notification is enabled for specific bundle.
662      *
663      * @param bundleOption Indicates the NotificationBundleOption object.
664      * @param enabled Indicates the enabled flag.
665      * @return Returns ERR_OK on success, others on failure.
666      */
667     ErrCode IsDistributedEnableByBundle(const sptr<NotificationBundleOption> &bundleOption, bool &enabled) override;
668 
669     /**
670      * @brief Get the reminder type of the current device.
671      *
672      * @param remindType Reminder type for the device.
673      * @return Returns ERR_OK on success, others on failure.
674      */
675     ErrCode GetDeviceRemindType(NotificationConstant::RemindType &remindType) override;
676 
677     /**
678      * @brief Publishes a continuous notification.
679      *
680      * @param request Notification requests that need to be posted.
681      * @return Returns ERR_OK on success, others on failure.
682      */
683     ErrCode PublishContinuousTaskNotification(const sptr<NotificationRequest> &request) override;
684 
685     /**
686      * @brief Cancels a continuous notification.
687      *
688      * @param label Identifies the label of the specified notification.
689      * @param notificationId Identifies the id of the specified notification.
690      * @return Returns ERR_OK on success, others on failure.
691      */
692     ErrCode CancelContinuousTaskNotification(const std::string &label, int32_t notificationId) override;
693 
694     /**
695      * @brief Check reminder permission
696      */
697     bool CheckReminderPermission();
698 
699     /**
700      * @brief Checks whether this device is support template.
701      *
702      * @param templateName Identifies the template name for searching as a condition.
703      * @param support Identifies the support flag.
704      * @return Returns ERR_OK on success, others on failure.
705      */
706     ErrCode IsSupportTemplate(const std::string &templateName, bool &support) override;
707 
708     /**
709      * @brief Checks Whether the specified users is allowed to publish notifications.
710      *
711      * @param userId Identifies the user's id.
712      * @param allowed Identifies the allowed flag.
713      * @return Returns ERR_OK on success, others on failure.
714      */
715     ErrCode IsSpecialUserAllowedNotify(const int32_t &userId, bool &allowed) override;
716 
717     /**
718      * @brief Sets whether to allow all applications to publish notifications on a specified device. The caller must
719      * have system permissions to call this method.
720      *
721      * @param deviceId Indicates the ID of the device running the application. At present, this parameter can only
722      *                 be null or an empty string, indicating the current device.
723      * @param enabled Specifies whether to allow all applications to publish notifications. The value true
724      *                indicates that notifications are allowed, and the value false indicates that notifications
725      *                are not allowed.
726      * @return Returns ERR_OK on success, others on failure.
727      */
728     ErrCode SetNotificationsEnabledByUser(const int32_t &deviceId, bool enabled) override;
729 
730     /**
731      * @brief Delete all notifications by user.
732      *
733      * @param userId Indicates the user id.
734      * @return Returns ERR_OK on success, others on failure.
735      */
736     ErrCode DeleteAllByUser(const int32_t &userId) override;
737 
738     ErrCode SetEnabledForBundleSlot(const sptr<NotificationBundleOption> &bundleOption,
739         const NotificationConstant::SlotType &slotType, bool enabled, bool isForceControl) override;
740     ErrCode GetEnabledForBundleSlot(const sptr<NotificationBundleOption> &bundleOption,
741         const NotificationConstant::SlotType &slotType, bool &enabled) override;
742     ErrCode GetEnabledForBundleSlotSelf(const NotificationConstant::SlotType &slotType, bool &enabled) override;
743 
744     // SystemEvent
745 
746     /**
747      * @brief Obtains the event of bundle removed.
748      *
749      * @param bundleOption Indicates the bundle info.
750      */
751     void OnBundleRemoved(const sptr<NotificationBundleOption> &bundleOption);
752 
753     /**
754      * @brief Obtains the event of bundle batch removed.
755      *
756      * @param notifications Notification vector.
757      */
758     void ExecBatchCancel(std::vector<sptr<Notification>> &notifications, int32_t &reason);
759 
760     /**
761      * @brief Obtains the event of user removed.
762      *
763      * @param userId Indicates the user.
764      */
765     void OnUserRemoved(const int32_t &userId);
766 
767     /**
768      * @brief Set whether to sync notifications to devices that do not have the app installed.
769      *
770      * @param userId Indicates the specific user.
771      * @param enabled Allow or disallow sync notifications.
772      * @return Returns set enabled result.
773      */
774     ErrCode SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled) override;
775 
776     /**
777      * @brief Obtains whether to sync notifications to devices that do not have the app installed.
778      *
779      * @param userId Indicates the specific user.
780      * @param enabled Allow or disallow sync notifications.
781      * @return Returns get enabled result.
782      */
783     ErrCode GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled) override;
784 
785     /**
786      * @brief Obtains the number of slotFlags.
787      *
788      * @param bundleOption Indicates the bundle name and uid of the application.
789      * @param slot      Indicates the specified slot object
790      * @param slotFlags Indicates the slogFlags of slot.
791      * @return Returns ERR_OK on success, others on failure.
792      */
793     virtual ErrCode GetSlotFlagsAsBundle(const sptr<NotificationBundleOption>& bundleOption,
794         uint32_t &slotFlags) override;
795 
796     /**
797      * @brief Set the slotFlags of slot.
798      *
799      * @param bundleOption Indicates the bundle name and uid of the application.
800      * @param slot      Indicates the specified slot object
801      * @param slotFlags Indicates the slogFlags of slot to set.
802      * @return Returns ERR_OK on success, others on failure.
803      */
804     virtual ErrCode SetSlotFlagsAsBundle(const sptr<NotificationBundleOption>& bundleOption,
805         uint32_t slotFlags) override;
806 
807 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
808     /**
809      * @brief Obtains the event of turn on screen.
810      */
811     void OnScreenOn();
812 
813     /**
814      * @brief Obtains the event of turn off screen.
815      */
816     void OnScreenOff();
817 #endif
818     void OnResourceRemove(int32_t userId);
819     void OnBundleDataCleared(const sptr<NotificationBundleOption> &bundleOption);
820 
821     /**
822      * @brief Obtains the event of bundle install.
823      *
824      * @param bundleOption Indicates the bundle info.
825      */
826     void OnBundleDataAdd(const sptr<NotificationBundleOption> &bundleOption);
827 
828     /**
829      * @brief Obtains the event of bundle update.
830      *
831      * @param bundleOption Indicates the bundle info.
832      */
833     void OnBundleDataUpdate(const sptr<NotificationBundleOption> &bundleOption);
834 
835     /**
836      * @brief Boot system completed event callback.
837      */
838     void OnBootSystemCompleted();
839 
840     // Distributed KvStore
841 
842     /**
843      * @brief Obtains the death event of the Distributed KvStore service.
844      */
845     void OnDistributedKvStoreDeathRecipient();
846 
847     ErrCode CancelPreparedNotification(int32_t notificationId, const std::string &label,
848         const sptr<NotificationBundleOption> &bundleOption, const int32_t reason);
849 
850     ErrCode PrepareNotificationInfo(
851         const sptr<NotificationRequest> &request, sptr<NotificationBundleOption> &bundleOption);
852     ErrCode PublishPreparedNotification(const sptr<NotificationRequest> &request,
853         const sptr<NotificationBundleOption> &bundleOption, bool isUpdateByOwner = false);
854 
855     /**
856      * @brief Dump current running status for debuging.
857      *
858      * @param cmd Indicates the specified dump command.
859      * @param bundle Indicates the specified bundle name.
860      * @param userId Indicates the specified userId.
861      * @param dumpInfo Indicates the container containing datas.
862      * @return Returns ERR_OK on success, others on failure.
863      */
864     ErrCode ShellDump(const std::string &cmd, const std::string &bundle, int32_t userId, int32_t recvUserId,
865         std::vector<std::string> &dumpInfo) override;
866 
867     /**
868      * @brief Set badge number.
869      *
870      * @param badgeNumber The badge number.
871      * @param instanceKey The application instance key.
872      * @return Returns set badge number result.
873      */
874     ErrCode SetBadgeNumber(int32_t badgeNumber, const std::string &instanceKey) override;
875 
876     /**
877      * @brief Set badge number by bundle.
878      *
879      * @param bundleOption Indicates the bundle name and uid of the application.
880      * @param badgeNumber The badge number.
881      * @return Returns set badge number by bundle result.
882      */
883     ErrCode SetBadgeNumberByBundle(const sptr<NotificationBundleOption> &bundleOption, int32_t badgeNumber) override;
884 
885     /**
886      * @brief Set badge number for dh by bundle.
887      *
888      * @param bundleOption Indicates the bundle name and uid of the application.
889      * @param badgeNumber The badge number.
890      * @return Returns set badge number by bundle result.
891      */
892     ErrCode SetBadgeNumberForDhByBundle(
893         const sptr<NotificationBundleOption> &bundleOption, int32_t badgeNumber) override;
894 
895     /**
896      * @brief Obtains allow notification application list.
897      *
898      * @param bundleOption Indicates the bundle bundleOption.
899      * @return Returns ERR_OK on success, others on failure.
900      */
901     ErrCode GetAllNotificationEnabledBundles(std::vector<NotificationBundleOption> &bundleOption) override;
902 
903     /**
904      * @brief Obtains allow liveview application list.
905      *
906      * @param bundleOption Indicates the bundle bundleOption.
907      * @return Returns ERR_OK on success, others on failure.
908      */
909     ErrCode GetAllLiveViewEnabledBundles(std::vector<NotificationBundleOption> &bundleOption) override;
910 
911     /**
912      * @brief Obtains allow distribued application list.
913      *
914      * @param deviceType Indicates device type.
915      * @param bundleOption Indicates the bundle bundleOption.
916      * @return Returns ERR_OK on success, others on failure.
917      */
918     ErrCode GetAllDistribuedEnabledBundles(const std::string &deviceType,
919         std::vector<NotificationBundleOption> &bundleOption) override;
920 
921     /**
922      * @brief Register Push Callback.
923      *
924      * @param pushCallback PushCallBack.
925      * @param notificationCheckRequest Filter conditions for push check
926      * @return Returns register push Callback result.
927      */
928     ErrCode RegisterPushCallback(const sptr<IRemoteObject>& pushCallback,
929         const sptr<NotificationCheckRequest> &notificationCheckRequest) override;
930 
931     /**
932      * @brief Unregister Push Callback.
933      *
934      * @return Returns unregister push Callback result.
935      */
936     ErrCode UnregisterPushCallback() override;
937 
938     /**
939      * @brief Sets whether to allow a specified application to publish notifications cross
940      * device collaboration. The caller must have system permissions to call this method.
941      *
942      * @param bundleOption Indicates the bundle name and uid of the application.
943      * @param deviceType Indicates the type of the device running the application.
944      * @param enabled Specifies whether to allow the given application to publish notifications. The value
945      *                true indicates that notifications are allowed, and the value false indicates that
946      *                notifications are not allowed.
947      * @return Returns set notifications enabled for specified bundle result.
948      */
949     ErrCode SetDistributedEnabledByBundle(const sptr<NotificationBundleOption> &bundleOption,
950         const std::string &deviceType, const bool enabled) override;
951 
952     /**
953      * @brief Get whether to allow a specified application to publish notifications cross
954      * device collaboration. The caller must have system permissions to call this method.
955      *
956      * @param bundleOption Indicates the bundle name and uid of the application.
957      * @param deviceType Indicates the type of the device running the application.
958      * @param enabled Specifies whether to allow the given application to publish notifications. The value
959      *                true indicates that notifications are allowed, and the value false indicates that
960      *                notifications are not allowed.
961      * @return Returns set notifications enabled for specified bundle result.
962      */
963     ErrCode IsDistributedEnabledByBundle(const sptr<NotificationBundleOption> &bundleOption,
964         const std::string &deviceType, bool &enabled) override;
965 
966     /**
967      * @brief Get Enable smartphone to collaborate with other devices for intelligent reminders
968      *
969      * @param deviceType Indicates the type of the device running the application.
970      * @param enabled Specifies whether to allow the given device to publish notifications.
971      *                The value true indicates that notifications are allowed, and the value
972      *                false indicates that notifications are not allowed.
973      * @return Returns set notifications enabled for specified bundle result.
974      */
975     ErrCode IsSmartReminderEnabled(const std::string &deviceType, bool &enabled) override;
976 
977     /**
978      * @brief Set Enable smartphone to collaborate with other devices for intelligent reminders
979      *
980      * @param deviceType Indicates the type of the device running the application.
981      * @param enabled Specifies whether to allow the given device to publish notifications.
982      *                The value true indicates that notifications are allowed, and the value
983      *                false indicates that notifications are not allowed.
984      * @return Returns set notifications enabled for specified bundle result.
985      */
986     ErrCode SetSmartReminderEnabled(const std::string &deviceType, const bool enabled) override;
987 
988     /**
989      * @brief Set the channel switch for collaborative reminders.
990        The caller must have system permissions to call this method.
991      *
992      * @param slotType Indicates the slot type of the application.
993      * @param deviceType Indicates the type of the device running the application.
994      * @param enabled Indicates slot switch status.
995      * @return Returns set channel switch result.
996      */
997     ErrCode SetDistributedEnabledBySlot(
998         const NotificationConstant::SlotType &slotType, const std::string &deviceType, const bool enabled) override;
999 
1000     /**
1001      * @brief Query the channel switch for collaborative reminders.
1002        The caller must have system permissions to call this method.
1003      *
1004      * @param slotType Indicates the slot type of the application.
1005      * @param deviceType Indicates the type of the device running the application.
1006      * @param enabled Indicates slot switch status.
1007      * @return Returns channel switch result.
1008      */
1009     ErrCode IsDistributedEnabledBySlot(
1010         const NotificationConstant::SlotType &slotType, const std::string &deviceType, bool &enabled) override;
1011 
1012     /**
1013      * @brief Set the status of the target device.
1014      *
1015      * @param deviceType Type of the device whose status you want to set.
1016      * @param status The status.
1017      * @return Returns set result.
1018      */
1019     ErrCode SetTargetDeviceStatus(const std::string &deviceType, const uint32_t status) override;
1020 
1021     /**
1022      * @brief Set the status of the target device.
1023      *
1024      * @param deviceType Type of the device whose status you want to set.
1025      * @param status The status.
1026      * @return Returns set result.
1027      */
1028     ErrCode SetTargetDeviceStatus(const std::string &deviceType, const uint32_t status,
1029         const uint32_t controlFlag) override;
1030 
1031     /**
1032      * @brief clear notification when aggregate local switch close.
1033      */
1034     void ClearAllNotificationGroupInfo(std::string localSwitch);
1035 
1036     /**
1037      * @brief Reset pushcallback proxy
1038      */
1039     void ResetPushCallbackProxy();
1040 
1041     /**
1042      * @brief Set the notification SlotFlags whitelist.
1043      */
1044     void SetSlotFlagsTrustlistsAsBundle(const sptr<NotificationBundleOption> &bundleOption);
1045 
1046     /**
1047      * @brief Init The Default Installation Package Notification Enabled.
1048      */
1049     void InitNotificationEnableList();
1050     /**
1051      * @brief Remove Local Live Notifications
1052      */
1053     ErrCode RemoveSystemLiveViewNotifications(const std::string& bundleName, const int32_t uid);
1054 
1055     /**
1056      * @brief Remove Local Live Notifications created by sa.
1057      */
1058     ErrCode RemoveSystemLiveViewNotificationsOfSa(int32_t uid);
1059 
1060     /**
1061      * @brief Set the notification flags by soltType.
1062      */
1063     void SetRequestBySlotType(const sptr<NotificationRequest> &request,
1064         const sptr<NotificationBundleOption> &bundleOption);
1065 
1066     // Might fail if ces subscribe failed, if failed, dialogManager_ will be set nullptr
1067     bool CreateDialogManager();
1068 
1069     /**
1070      * @brief Set agent relationship.
1071      *
1072      * @param key Indicates storing agent relationship if the value is "PROXY_PKG".
1073      * @param value Indicates key-value pair of agent relationship.
1074      * @return Returns set result.
1075      */
1076     ErrCode SetAdditionConfig(const std::string &key, const std::string &value) override;
1077 
1078     /**
1079      * @brief Cancels a published agent notification.
1080      *
1081      * @param bundleOption Indicates the bundle name and uid of the application.
1082      * @param id Indicates the unique notification ID in the application.
1083      * @return Returns cancel result.
1084      */
1085     ErrCode CancelAsBundleWithAgent(const sptr<NotificationBundleOption> &bundleOption, const int32_t id) override;
1086 
1087     /**
1088      * @brief Get the status of the target device.
1089      *
1090      * @param deviceType Type of the device whose status you want to set.
1091      * @param status The status.
1092      * @return Returns set result.
1093      */
1094     ErrCode GetTargetDeviceStatus(const std::string &deviceType, int32_t &status) override;
1095 
1096     /**
1097      * @brief Init publish process.
1098      */
1099     bool InitPublishProcess();
1100 
1101     /**
1102     * @brief Recover LiveView from DB.
1103     */
1104     void RecoverLiveViewFromDb(int32_t userId = -1);
1105 
1106 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
1107     /**
1108      * @brief Register Swing Callback.
1109      *
1110      * @param swingCallback SwingCallBack.
1111      * @return Returns register swing Callback result.
1112      */
1113     ErrCode RegisterSwingCallback(const sptr<IRemoteObject>& swingCallback) override;
1114 #endif
1115 
1116     /**
1117      * @brief update unified group info.
1118      */
1119     void UpdateUnifiedGroupInfo(const std::string &key, std::shared_ptr<NotificationUnifiedGroupInfo> &groupInfo);
1120 
1121     /**
1122      * @brief Whether reminders are allowed.
1123      */
1124     bool AllowUseReminder(const std::string& bundleName);
1125 
1126     /**
1127      * @brief Whether reminders are allowed.
1128      *
1129      * @param bundleName app bundleName
1130      * @param isAllowUseReminder is allow use reminder
1131      * @return Returns ERR_OK on success, others on failure.
1132      */
1133     ErrCode AllowUseReminder(const std::string& bundleName, bool& isAllowUseReminder) override;
1134 
1135     /**
1136      * @brief Distribution operation based on hashCode.
1137      *
1138      * @param hashCode Unique ID of the notification.
1139      * @return Returns ERR_OK on success, others on failure.
1140      */
1141     ErrCode DistributeOperation(sptr<NotificationOperationInfo>& operationInfo,
1142         const sptr<OperationCallbackInterface> &callback) override;
1143 
1144     /**
1145      * @brief Reply distribute operation.
1146      *
1147      * @param hashCode Unique ID of the notification.
1148      * @param result The result of the distribute operation.
1149      * @return Returns ERR_OK on success, others on failure.
1150      */
1151     ErrCode ReplyDistributeOperation(const std::string& hashCode, const int32_t result) override;
1152 
1153     /**
1154      * @brief Get notificationRequest by hashCode.
1155      *
1156      * @param hashCode Unique ID of the notification.
1157      * @param notificationRequest The request of of the notification.
1158      * @return Returns ERR_OK on success, others on failure.
1159      */
1160     ErrCode GetNotificationRequestByHashCode(
1161         const std::string& hashCode, sptr<NotificationRequest>& notificationRequest) override;
1162 
1163     int32_t OnBackup(MessageParcel& data, MessageParcel& reply);
1164 
1165     int32_t OnRestore(MessageParcel& data, MessageParcel& reply);
1166 
1167     void ResetDistributedEnabled();
1168 
1169     ErrCode UpdateNotificationTimerByUid(const int32_t uid, const bool isPaused) override;
1170 
1171     void UpdateCloneBundleInfo(const NotificationCloneBundleInfo cloneBundleInfo);
1172 
1173     void TryStartReminderAgentService();
1174 
1175     static sptr<NotificationBundleOption> GenerateBundleOption();
1176     static sptr<NotificationBundleOption> GenerateValidBundleOption(const sptr<NotificationBundleOption> &bundleOption);
1177 
1178     ErrCode DisableNotificationFeature(const sptr<NotificationDisable> &notificationDisable) override;
1179 
1180     bool IsDisableNotification(const std::string &bundleName);
1181 
1182     bool IsNeedToControllerByDisableNotification(const sptr<NotificationRequest> &request);
1183 
1184     void SetAndPublishSubscriberExistFlag(const std::string& deviceType, bool existFlag);
1185     ErrCode RemoveAllNotificationsByBundleName(const std::string &bundleName, int32_t reason);
1186 
1187     /**
1188      * @brief set rule of generate hashCode.
1189      *
1190      * @param type generate hashCode.
1191      * @return Returns ERR_OK on success, others on failure.
1192      */
1193     ErrCode SetHashCodeRule(const uint32_t type) override;
1194 
1195 protected:
1196     /**
1197      * @brief Query whether there is a agent relationship between the two apps.
1198      *
1199      * @param agentBundleName The bundleName of the agent app.
1200      * @param sourceBundleName The bundleName of the source app.
1201      * @return Returns true if There is an agent relationship; returns false otherwise.
1202      */
1203     bool IsAgentRelationship(const std::string &agentBundleName, const std::string &sourceBundleName);
1204 
1205 public:
1206     bool CheckApiCompatibility(const sptr<NotificationBundleOption> &bundleOption);
1207     ErrCode SetDefaultNotificationEnabled(
1208         const sptr<NotificationBundleOption> &bundleOption, bool enabled);
1209     ErrCode RemoveNotificationBySlot(const sptr<NotificationBundleOption> &bundleOption,
1210         const sptr<NotificationSlot> &slot, const int reason);
1211     bool PublishSlotChangeCommonEvent(const sptr<NotificationBundleOption> &bundleOption);
1212 private:
1213     struct RecentInfo {
1214         std::list<std::shared_ptr<RecentNotification>> list;
1215         size_t recentCount = 16;
1216     };
1217 
1218     struct SoundPermissionInfo {
1219         std::set<std::string> bundleName_;
1220         std::atomic<bool> needUpdateCache_ = true;
1221         bool allPackage_ = false;
1222         std::mutex dbMutex_;
1223     };
1224 
1225     enum UploadStatus {
1226         CREATE,
1227         FIRST_UPDATE_TIME_OUT,
1228         CONTINUOUS_UPDATE_TIME_OUT,
1229         END,
1230         FINISH
1231     };
1232 
1233     enum ContactPolicy {
1234         FORBID_EVERYONE = 1,
1235         ALLOW_EVERYONE = 2,
1236         ALLOW_EXISTING_CONTACTS = 3,
1237         ALLOW_FAVORITE_CONTACTS = 4,
1238         ALLOW_SPECIFIED_CONTACTS = 5,
1239         FORBID_SPECIFIED_CONTACTS = 6,
1240     };
1241 
1242     AdvancedNotificationService();
1243 
1244     void StartFilters();
1245     void StopFilters();
1246     ErrCode Filter(const std::shared_ptr<NotificationRecord> &record, bool isRecover = false);
1247     void ChangeNotificationByControlFlags(const std::shared_ptr<NotificationRecord> &record,
1248         const bool isAgentController);
1249     ErrCode CheckPublishPreparedNotification(const std::shared_ptr<NotificationRecord> &record, bool isSystemApp);
1250     void AddToNotificationList(const std::shared_ptr<NotificationRecord> &record);
1251     void AddToDelayNotificationList(const std::shared_ptr<NotificationRecord> &record);
1252     ErrCode UpdateInNotificationList(const std::shared_ptr<NotificationRecord> &record);
1253     void UpdateInDelayNotificationList(const std::shared_ptr<NotificationRecord> &record);
1254     ErrCode AssignToNotificationList(const std::shared_ptr<NotificationRecord> &record);
1255     ErrCode RemoveFromNotificationList(const sptr<NotificationBundleOption> &bundleOption,
1256         NotificationKey notificationKey, sptr<Notification> &notification, int32_t removeReason,
1257         bool isCancel = false);
1258     ErrCode RemoveFromNotificationList(const std::string &key, sptr<Notification> &notification,
1259         bool isCancel, int32_t removeReason);
1260     ErrCode RemoveFromNotificationListForDeleteAll(const std::string &key,
1261         const int32_t &userId, sptr<Notification> &notification);
1262     bool RemoveFromDelayedNotificationList(const std::string &key);
1263     std::shared_ptr<NotificationRecord> GetFromNotificationList(const std::string &key);
1264     std::shared_ptr<NotificationRecord> GetFromNotificationList(const int32_t ownerUid, const int32_t notificationId);
1265     std::shared_ptr<NotificationRecord> GetFromDelayedNotificationList(
1266         const int32_t ownerUid, const int32_t notificationId);
1267     std::vector<std::string> GetNotificationKeys(const sptr<NotificationBundleOption> &bundleOption);
1268     std::vector<std::string> GetNotificationKeysByBundle(const sptr<NotificationBundleOption> &bundleOption);
1269     bool IsNotificationExists(const std::string &key);
1270     void SortNotificationList();
1271     static bool NotificationCompare(
1272         const std::shared_ptr<NotificationRecord> &first, const std::shared_ptr<NotificationRecord> &second);
1273     ErrCode PublishInNotificationList(const std::shared_ptr<NotificationRecord> &record);
1274 
1275     sptr<NotificationSortingMap> GenerateSortingMap();
1276 
1277     std::string TimeToString(int64_t time);
1278     int64_t GetNowSysTime();
1279     void ExtendDumpForFlags(std::shared_ptr<NotificationFlags>, std::stringstream &stream);
1280     ErrCode ActiveNotificationDump(const std::string& bundle, int32_t userId, int32_t recvUserId,
1281         std::vector<std::string> &dumpInfo);
1282     ErrCode RecentNotificationDump(const std::string& bundle, int32_t userId, int32_t recvUserId,
1283         std::vector<std::string> &dumpInfo);
1284 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
1285     ErrCode DistributedNotificationDump(const std::string& bundle, int32_t userId, int32_t recvUserId,
1286         std::vector<std::string> &dumpInfo);
1287 #endif
1288     ErrCode SetRecentNotificationCount(const std::string arg);
1289     void UpdateRecentNotification(sptr<Notification> &notification, bool isDelete, int32_t reason);
1290 
1291     void AdjustDateForDndTypeOnce(int64_t &beginDate, int64_t &endDate);
1292     ErrCode PrepareNotificationRequest(const sptr<NotificationRequest> &request);
1293     ErrCode PrepareContinuousTaskNotificationRequest(const sptr<NotificationRequest> &request, const int32_t &uid);
1294 
1295     void TriggerRemoveWantAgent(const sptr<NotificationRequest> &request, int32_t removeReason, bool isThirdParty);
1296 
1297     ErrCode IsAllowedNotifySelf(const sptr<NotificationBundleOption> &bundleOption, bool &allowed);
1298 
1299     ErrCode SetNotificationRemindType(sptr<Notification> notification, bool isLocal);
1300 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
1301     std::vector<std::string> GetLocalNotificationKeys(const sptr<NotificationBundleOption> &bundleOption);
1302     NotificationConstant::RemindType GetRemindType();
1303     ErrCode DoDistributedPublish(
1304         const sptr<NotificationBundleOption> bundleOption, const std::shared_ptr<NotificationRecord> record);
1305     ErrCode DoDistributedDelete(
1306         const std::string deviceId, const std::string bundleName, const sptr<Notification> notification);
1307     void GetDistributedInfo(const std::string &key, std::string &deviceId, std::string &bundleName);
1308     bool CheckDistributedNotificationType(const sptr<NotificationRequest> &request);
1309     void OnDistributedPublish(
1310         const std::string &deviceId, const std::string &bundleName, sptr<NotificationRequest> &request);
1311     void OnDistributedUpdate(
1312         const std::string &deviceId, const std::string &bundleName, sptr<NotificationRequest> &request);
1313     void OnDistributedDelete(
1314         const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id);
1315     static ErrCode GetDistributedEnableInApplicationInfo(
1316         const sptr<NotificationBundleOption> bundleOption, bool &enable);
1317     bool CheckPublishWithoutApp(const int32_t userId, const sptr<NotificationRequest> &request);
1318     void InitDistributeCallBack();
1319 #endif
1320 
1321     ErrCode GetHasPoppedDialog(const sptr<NotificationBundleOption> bundleOption, bool &hasPopped);
1322     static ErrCode GetAppTargetBundle(const sptr<NotificationBundleOption> &bundleOption,
1323         sptr<NotificationBundleOption> &targetBundle);
1324     void ReportInfoToResourceSchedule(const int32_t userId, const std::string &bundleName);
1325     int Dump(int fd, const std::vector<std::u16string> &args) override;
1326     void GetDumpInfo(const std::vector<std::u16string> &args, std::string &result);
1327 
1328     static void SendSubscribeHiSysEvent(int32_t pid, int32_t uid, const sptr<NotificationSubscribeInfo> &info,
1329         ErrCode errCode);
1330     static void SendUnSubscribeHiSysEvent(int32_t pid, int32_t uid, const sptr<NotificationSubscribeInfo> &info);
1331     void SendPublishHiSysEvent(const sptr<NotificationRequest> &request, ErrCode errCode);
1332     void SendCancelHiSysEvent(int32_t notificationId, const std::string &label,
1333         const sptr<NotificationBundleOption> &bundleOption, ErrCode errCode);
1334     void SendRemoveHiSysEvent(int32_t notificationId, const std::string &label,
1335         const sptr<NotificationBundleOption> &bundleOption, ErrCode errCode);
1336     void SendEnableNotificationHiSysEvent(const sptr<NotificationBundleOption> &bundleOption, bool enabled,
1337         ErrCode errCode);
1338     void SendEnableNotificationSlotHiSysEvent(const sptr<NotificationBundleOption> &bundleOption,
1339         const NotificationConstant::SlotType &slotType, bool enabled, ErrCode errCode);
1340     void SendFlowControlOccurHiSysEvent(const std::shared_ptr<NotificationRecord> &record);
1341     void SendLiveViewUploadHiSysEvent(const std::shared_ptr<NotificationRecord> &record, int32_t uploadStatus);
1342 
1343     ErrCode SetRequestBundleInfo(const sptr<NotificationRequest> &request, int32_t uid, std::string &bundle);
1344     ErrCode PrePublishNotificationBySa(const sptr<NotificationRequest> &request, int32_t uid, std::string &bundle);
1345     ErrCode PrePublishRequest(const sptr<NotificationRequest> &request);
1346     ErrCode PublishNotificationBySa(const sptr<NotificationRequest> &request);
1347     bool IsNeedPushCheck(const sptr<NotificationRequest> &request);
1348     void FillExtraInfoToJson(const sptr<NotificationRequest> &request,
1349         sptr<NotificationCheckRequest> &checkRequest, nlohmann::json &jsonObject);
1350     void CreatePushCheckJson(const sptr<NotificationRequest> &request,
1351         sptr<NotificationCheckRequest> &checkRequest, nlohmann::json &jsonObject);
1352     ErrCode PushCheck(const sptr<NotificationRequest> &request);
1353     uint64_t StartAutoDelete(const std::shared_ptr<NotificationRecord> &record,
1354         int64_t deleteTimePoint, int32_t reason);
1355     void TriggerAutoDelete(const std::string &hashCode, int32_t reason);
1356     void SendNotificationsOnCanceled(std::vector<sptr<Notification>> &notifications,
1357         const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason);
1358     void SetAgentNotification(sptr<NotificationRequest>& notificationRequest, std::string& bundleName);
1359     static bool GetBundleInfoByNotificationBundleOption(
1360         const sptr<NotificationBundleOption> &bundleOption, AppExecFwk::BundleInfo &bundleInfo);
1361 
1362     ErrCode GetTargetRecordList(const int32_t uid, NotificationConstant::SlotType slotType,
1363         NotificationContent::Type contentType, std::vector<std::shared_ptr<NotificationRecord>>& recordList);
1364     ErrCode GetCommonTargetRecordList(const int32_t uid, NotificationConstant::SlotType slotType,
1365         NotificationContent::Type contentType, std::vector<std::shared_ptr<NotificationRecord>>& recordList);
1366     ErrCode RemoveNotificationFromRecordList(const std::vector<std::shared_ptr<NotificationRecord>>& recordList);
1367     void OnSubscriberAdd(const std::shared_ptr<NotificationSubscriberManager::SubscriberRecord> &record);
1368     bool IsLiveViewCanRecover(const sptr<NotificationRequest> request);
1369     ErrCode FillNotificationRecord(const NotificationRequestDb &requestdbObj,
1370         std::shared_ptr<NotificationRecord> record);
1371     static int32_t SetNotificationRequestToDb(const NotificationRequestDb &requestDb);
1372     static int32_t GetNotificationRequestFromDb(const std::string &key, NotificationRequestDb &requestDb);
1373     static int32_t GetBatchNotificationRequestsFromDb(std::vector<NotificationRequestDb> &requests,
1374         int32_t userId = -1);
1375     static int32_t DoubleDeleteNotificationFromDb(const std::string &key,
1376         const std::string &secureKey, const int32_t userId);
1377     static int32_t DeleteNotificationRequestFromDb(const std::string &key, const int32_t userId);
1378     void CancelTimer(uint64_t timerId);
1379     void BatchCancelTimer(std::vector<uint64_t> timerIds);
1380     ErrCode UpdateNotificationTimerInfo(const std::shared_ptr<NotificationRecord> &record);
1381     ErrCode SetFinishTimer(const std::shared_ptr<NotificationRecord> &record);
1382     ErrCode StartFinishTimer(const std::shared_ptr<NotificationRecord> &record,
1383         int64_t expireTimePoint, const int32_t reason);
1384     void CancelFinishTimer(const std::shared_ptr<NotificationRecord> &record);
1385     ErrCode SetUpdateTimer(const std::shared_ptr<NotificationRecord> &record);
1386     ErrCode StartUpdateTimer(const std::shared_ptr<NotificationRecord> &record,
1387         int64_t expireTimePoint, const int32_t reason);
1388     void CancelUpdateTimer(const std::shared_ptr<NotificationRecord> &record);
1389     void StartArchiveTimer(const std::shared_ptr<NotificationRecord> &record);
1390     void CancelArchiveTimer(const std::shared_ptr<NotificationRecord> &record);
1391     ErrCode StartAutoDeletedTimer(const std::shared_ptr<NotificationRecord> &record);
1392     void ProcForDeleteLiveView(const std::shared_ptr<NotificationRecord> &record);
1393     void QueryDoNotDisturbProfile(const int32_t &userId, std::string &enable, std::string &profileId);
1394     void QueryIntelligentExperienceEnable(const int32_t &userId, std::string &enable);
1395     void CheckDoNotDisturbProfile(const std::shared_ptr<NotificationRecord> &record);
1396     void ReportDoNotDisturbModeChanged(const int32_t &userId, std::string &enable);
1397     void DoNotDisturbUpdataReminderFlags(const std::shared_ptr<NotificationRecord> &record);
1398     ErrCode CheckCommonParams();
1399     std::shared_ptr<NotificationRecord> GetRecordFromNotificationList(
1400         int32_t notificationId, int32_t uid, const std::string &label, const std::string &bundleName);
1401     std::shared_ptr<NotificationRecord> MakeNotificationRecord(
1402         const sptr<NotificationRequest> &request, const sptr<NotificationBundleOption> &bundleOption);
1403     ErrCode IsAllowedNotifyForBundle(const sptr<NotificationBundleOption> &bundleOption, bool &allowed);
1404     void FillActionButtons(const sptr<NotificationRequest> &request);
1405     ErrCode IsAllowedGetNotificationByFilter(const std::shared_ptr<NotificationRecord> &record,
1406         const sptr<NotificationBundleOption> &bundleOption);
1407     ErrCode FillRequestByKeys(const sptr<NotificationRequest> &oldRequest,
1408         const std::vector<std::string> extraInfoKeys, sptr<NotificationRequest> &newRequest);
1409     ErrCode IsAllowedRemoveSlot(const sptr<NotificationBundleOption> &bundleOption,
1410         const NotificationConstant::SlotType &slotType);
1411     void HandleBadgeEnabledChanged(const sptr<NotificationBundleOption> &bundleOption, bool enabled);
1412     ErrCode CheckBundleOptionValid(sptr<NotificationBundleOption> &bundleOption);
1413     bool IsNeedNotifyConsumed(const sptr<NotificationRequest> &request);
1414     ErrCode AddRecordToMemory(const std::shared_ptr<NotificationRecord> &record,
1415         bool isSystemApp, bool isUpdateByOwner, const bool isAgentController);
1416     ErrCode DuplicateMsgControl(const sptr<NotificationRequest> &request);
1417     void RemoveExpiredUniqueKey();
1418     bool IsDuplicateMsg(const std::string &uniqueKey);
1419     void DeleteDuplicateMsgs(const sptr<NotificationBundleOption> &bundleOption);
1420     ErrCode PublishRemoveDuplicateEvent(const std::shared_ptr<NotificationRecord> &record);
1421     ErrCode UpdateSlotAuthInfo(const std::shared_ptr<NotificationRecord> &record);
1422     std::vector<AppExecFwk::BundleInfo> GetBundlesOfActiveUser();
1423     void RemoveNotificationList(const std::shared_ptr<NotificationRecord> &record);
1424     void FillLockScreenPicture(const sptr<NotificationRequest> &newRequest,
1425         const sptr<NotificationRequest> &oldRequest);
1426     static ErrCode SetLockScreenPictureToDb(const sptr<NotificationRequest> &request);
1427     static ErrCode GetLockScreenPictureFromDb(NotificationRequest *request);
1428     void RemoveDoNotDisturbProfileTrustList(const sptr<NotificationBundleOption> &bundleOption);
1429     ErrCode DeleteAllByUserInner(const int32_t &userId, int32_t reason, bool isAsync = false);
1430     ErrCode RemoveAllNotificationsInner(const sptr<NotificationBundleOption> &bundleOption, int32_t reason);
1431     ErrCode AssignValidNotificationSlot(const std::shared_ptr<NotificationRecord> &record,
1432         const sptr<NotificationBundleOption> &bundleOption);
1433     ErrCode UpdateSlotReminderModeBySlotFlags(const sptr<NotificationBundleOption> &bundle, uint32_t slotFlags);
1434     ErrCode CheckSoundPermission(const sptr<NotificationRequest> &request, std::string bundleName);
1435     void GenerateSlotReminderMode(const sptr<NotificationSlot> &slot, const sptr<NotificationBundleOption> &bundle,
1436         bool isSpecifiedSlot = false, uint32_t defaultSlotFlags = DEFAULT_SLOT_FLAGS);
1437     static void CloseAlert(const std::shared_ptr<NotificationRecord> &record);
1438     bool IsUpdateSystemLiveviewByOwner(const sptr<NotificationRequest> &request);
1439     bool IsSaCreateSystemLiveViewAsBundle(const std::shared_ptr<NotificationRecord> &record, int32_t ipcUid);
1440     ErrCode SaPublishSystemLiveViewAsBundle(const std::shared_ptr<NotificationRecord> &record);
1441     bool IsNotificationExistsInDelayList(const std::string &key);
1442     uint64_t StartDelayPublishTimer(const int32_t ownerUid, const int32_t notificationId, const uint32_t delayTime);
1443     ErrCode StartPublishDelayedNotification(const std::shared_ptr<NotificationRecord> &record);
1444     void StartPublishDelayedNotificationTimeOut(const int32_t ownerUid, const int32_t notificationId);
1445     void UpdateRecordByOwner(const std::shared_ptr<NotificationRecord> &record, bool isSystemApp);
1446     void StartFinishTimerForUpdate(const std::shared_ptr<NotificationRecord> &record, uint64_t process);
1447     ErrCode CheckLongTermLiveView(const sptr<NotificationRequest> &request, const std::string &key);
1448     void ExcuteCancelGroupCancel(const sptr<NotificationBundleOption>& bundleOption,
1449         const std::string &groupName, const int32_t reason);
1450     ErrCode ExcuteCancelAll(const sptr<NotificationBundleOption>& bundleOption, const int32_t reason);
1451     ErrCode ExcuteDelete(const std::string &key, const int32_t removeReason);
1452     ErrCode CheckNeedSilent(const std::string &phoneNumber, int32_t callerType, int32_t userId);
1453     ErrCode QueryContactByProfileId(const std::string &phoneNumber, const std::string &policy, int32_t userId);
1454     uint32_t GetDefaultSlotFlags(const sptr<NotificationRequest> &request);
1455     bool IsSystemUser(int32_t userId);
1456     ErrCode CollaboratePublish(const sptr<NotificationRequest> &request);
1457     void SetCollaborateReminderFlag(const sptr<NotificationRequest> &request);
1458     ErrCode SetEnabledForBundleSlotInner(const sptr<NotificationBundleOption> &bundleOption,
1459         const sptr<NotificationBundleOption> &bundle,
1460         const NotificationConstant::SlotType &slotType, bool enabled, bool isForceControl);
1461     ErrCode AddSlotThenPublishEvent(
1462         const sptr<NotificationSlot> &slot,
1463         const sptr<NotificationBundleOption> &bundle,
1464         bool enabled, bool isForceControl);
1465     ErrCode OnRecoverLiveView(const std::vector<std::string> &keys);
1466     void HandleUpdateLiveViewNotificationTimer(const int32_t uid, const bool isPaused);
1467     void CancelWantAgent(const sptr<Notification> &notification);
1468     void CancelOnceWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> &wantAgent);
1469     void PublishSubscriberExistFlagEvent(bool headsetExistFlag, bool wearableExistFlag);
1470     void SetClassificationWithVoip(const sptr<NotificationRequest> &request);
1471     void UpdateCollaborateTimerInfo(const std::shared_ptr<NotificationRecord> &record);
1472     ErrCode CommonRequestEnableNotification(const std::string &deviceId,
1473         const sptr<AnsDialogCallback> &callback,
1474         const sptr<IRemoteObject> &callerToken,
1475         const sptr<NotificationBundleOption> bundleOption,
1476         const bool innerLake);
1477     void ClearSlotTypeData(const sptr<NotificationRequest> &request, int32_t callingUid, int32_t sourceType);
1478 
1479 private:
1480     static sptr<AdvancedNotificationService> instance_;
1481     static std::mutex instanceMutex_;
1482     static std::mutex pushMutex_;
1483     static std::map<NotificationConstant::SlotType, sptr<IPushCallBack>> pushCallBacks_;
1484     static std::map<NotificationConstant::SlotType, sptr<NotificationCheckRequest>> checkRequests_;
1485     bool aggregateLocalSwitch_ = false;
1486     std::shared_ptr<OHOS::AppExecFwk::EventRunner> runner_ = nullptr;
1487     std::shared_ptr<OHOS::AppExecFwk::EventHandler> handler_ = nullptr;
1488     std::list<std::shared_ptr<NotificationRecord>> notificationList_;
1489     std::shared_ptr<RecentInfo> recentInfo_ = nullptr;
1490     std::shared_ptr<DistributedKvStoreDeathRecipient> distributedKvStoreDeathRecipient_ = nullptr;
1491     std::shared_ptr<SystemEventObserver> systemEventObserver_ = nullptr;
1492     DistributedKv::DistributedKvDataManager dataManager_;
1493     sptr<IRemoteObject::DeathRecipient> pushRecipient_ = nullptr;
1494     std::shared_ptr<ffrt::queue> notificationSvrQueue_ = nullptr;
1495     std::map<NotificationConstant::SlotType, std::shared_ptr<BasePublishProcess>> publishProcess_;
1496 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
1497     NotificationConstant::DistributedReminderPolicy distributedReminderPolicy_ = DEFAULT_DISTRIBUTED_REMINDER_POLICY;
1498     bool localScreenOn_ = true;
1499 #endif
1500     std::shared_ptr<SoundPermissionInfo> soundPermissionInfo_ = nullptr;
1501     std::shared_ptr<PermissionFilter> permissonFilter_ = nullptr;
1502     std::shared_ptr<NotificationSlotFilter> notificationSlotFilter_ = nullptr;
1503     std::shared_ptr<NotificationDialogManager> dialogManager_ = nullptr;
1504     std::list<std::pair<std::chrono::system_clock::time_point, std::string>> uniqueKeyList_;
1505     std::list<std::pair<std::shared_ptr<NotificationRecord>, uint64_t>> delayNotificationList_;
1506     std::mutex delayNotificationMutext_;
1507     static std::mutex doNotDisturbMutex_;
1508     std::map<int32_t, std::string> doNotDisturbEnableRecord_;
1509     bool isCachedAppAndDeviceRelationMap_ = false;
1510     std::map<std::string, std::string> appAndDeviceRelationMap_;
1511 };
1512 
1513 /**
1514  * @class PushCallbackRecipient
1515  * PushCallbackRecipient notices IRemoteBroker died.
1516  */
1517 class PushCallbackRecipient : public IRemoteObject::DeathRecipient {
1518 public:
1519     PushCallbackRecipient();
1520     virtual ~PushCallbackRecipient();
1521     void OnRemoteDied(const wptr<IRemoteObject> &remote);
1522 };
1523 }  // namespace Notification
1524 }  // namespace OHOS
1525 
1526 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_ADVANCED_NOTIFICATION_SERVICE_H
1527