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