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