• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_ANS_STANDARD_SERVICES_ANS_INCLUDE_ADVANCED_NOTIFICATION_SERVICE_H
17 #define BASE_NOTIFICATION_ANS_STANDARD_SERVICES_ANS_INCLUDE_ADVANCED_NOTIFICATION_SERVICE_H
18 
19 #include <ctime>
20 #include <list>
21 #include <memory>
22 #include <mutex>
23 
24 #include "event_handler.h"
25 #include "event_runner.h"
26 #include "refbase.h"
27 
28 #include "ans_const_define.h"
29 #include "ans_manager_stub.h"
30 #include "distributed_kv_data_manager.h"
31 #include "distributed_kvstore_death_recipient.h"
32 #include "notification.h"
33 #include "notification_bundle_option.h"
34 #include "notification_record.h"
35 #include "notification_sorting_map.h"
36 #include "system_event_observer.h"
37 
38 namespace OHOS {
39 namespace Notification {
40 class AdvancedNotificationService final : public AnsManagerStub {
41 public:
42     ~AdvancedNotificationService() override;
43     DISALLOW_COPY_AND_MOVE(AdvancedNotificationService);
44 
45     static sptr<AdvancedNotificationService> GetInstance();
46 
47     // AnsManagerStub
48     ErrCode Publish(const std::string &label, const sptr<NotificationRequest> &request) override;
49     ErrCode Cancel(int notificationId, const std::string &label) override;
50     ErrCode CancelAll() override;
51     ErrCode AddSlotByType(NotificationConstant::SlotType slotType) override;
52     ErrCode AddSlots(const std::vector<sptr<NotificationSlot>> &slots) override;
53     ErrCode RemoveSlotByType(const NotificationConstant::SlotType &slotType) override;
54     ErrCode RemoveAllSlots() override;
55     ErrCode AddSlotGroups(std::vector<sptr<NotificationSlotGroup>> groups) override;
56     ErrCode GetSlotByType(const NotificationConstant::SlotType &slotType, sptr<NotificationSlot> &slot) override;
57     ErrCode GetSlots(std::vector<sptr<NotificationSlot>> &slots) override;
58     ErrCode GetSlotGroup(const std::string &groupId, sptr<NotificationSlotGroup> &group) override;
59     ErrCode GetSlotGroups(std::vector<sptr<NotificationSlotGroup>> &groups) override;
60     ErrCode GetSlotNumAsBundle(const sptr<NotificationBundleOption> &bundleOption, int &num) override;
61     ErrCode RemoveSlotGroups(const std::vector<std::string> &groupIds) override;
62     ErrCode GetActiveNotifications(std::vector<sptr<NotificationRequest>> &notifications) override;
63     ErrCode GetActiveNotificationNums(int &num) override;
64     ErrCode GetAllActiveNotifications(std::vector<sptr<Notification>> &notifications) override;
65     ErrCode GetSpecialActiveNotifications(
66         const std::vector<std::string> &key, std::vector<sptr<Notification>> &notifications) override;
67     ErrCode SetNotificationAgent(const std::string &agent) override;
68     ErrCode GetNotificationAgent(std::string &agent) override;
69     ErrCode CanPublishAsBundle(const std::string &representativeBundle, bool &canPublish) override;
70     ErrCode PublishAsBundle(
71         const sptr<NotificationRequest> notification, const std::string &representativeBundle) override;
72     ErrCode SetNotificationBadgeNum(int num) override;
73     ErrCode GetBundleImportance(int &importance) override;
74     ErrCode HasNotificationPolicyAccessPermission(bool &granted) override;
75     ErrCode SetPrivateNotificationsAllowed(bool allow) override;
76     ErrCode GetPrivateNotificationsAllowed(bool &allow) override;
77     ErrCode RemoveNotification(
78         const sptr<NotificationBundleOption> &bundleOption, int notificationId, const std::string &label) override;
79     ErrCode RemoveAllNotifications(const sptr<NotificationBundleOption> &bundleOption) override;
80     ErrCode Delete(const std::string &key) override;
81     ErrCode DeleteByBundle(const sptr<NotificationBundleOption> &bundleOption) override;
82     ErrCode DeleteAll() override;
83 
84     ErrCode GetSlotsByBundle(
85         const sptr<NotificationBundleOption> &bundleOption, std::vector<sptr<NotificationSlot>> &slots) override;
86     ErrCode UpdateSlots(
87         const sptr<NotificationBundleOption> &bundleOption, const std::vector<sptr<NotificationSlot>> &slots) override;
88     ErrCode UpdateSlotGroups(const sptr<NotificationBundleOption> &bundleOption,
89         const std::vector<sptr<NotificationSlotGroup>> &groups) override;
90     ErrCode RequestEnableNotification(const std::string &deviceId) override;
91     ErrCode SetNotificationsEnabledForBundle(const std::string &bundle, bool enabled) override;
92     ErrCode SetNotificationsEnabledForAllBundles(const std::string &deviceId, bool enabled) override;
93     ErrCode SetNotificationsEnabledForSpecialBundle(
94         const std::string &deviceId, const sptr<NotificationBundleOption> &bundleOption, bool enabled) override;
95     ErrCode SetShowBadgeEnabledForBundle(const sptr<NotificationBundleOption> &bundleOption, bool enabled) override;
96     ErrCode GetShowBadgeEnabledForBundle(const sptr<NotificationBundleOption> &bundleOption, bool &enabled) override;
97     ErrCode GetShowBadgeEnabled(bool &enabled) override;
98     ErrCode Subscribe(const sptr<IAnsSubscriber> &subscriber, const sptr<NotificationSubscribeInfo> &info) override;
99     ErrCode Unsubscribe(const sptr<IAnsSubscriber> &subscriber, const sptr<NotificationSubscribeInfo> &info) override;
100     ErrCode IsAllowedNotify(bool &allowed) override;
101     ErrCode IsAllowedNotifySelf(bool &allowed) override;
102     ErrCode IsSpecialBundleAllowedNotify(const sptr<NotificationBundleOption> &bundleOption, bool &allowed) override;
103 
104     ErrCode SetDoNotDisturbDate(const sptr<NotificationDoNotDisturbDate> &date) override;
105     ErrCode GetDoNotDisturbDate(sptr<NotificationDoNotDisturbDate> &date) override;
106     ErrCode DoesSupportDoNotDisturbMode(bool &doesSupport) override;
107     ErrCode CancelGroup(const std::string &groupName) override;
108     ErrCode RemoveGroupByBundle(
109         const sptr<NotificationBundleOption> &bundleOption, const std::string &groupName) override;
110 
111     ErrCode IsDistributedEnabled(bool &enabled) override;
112     ErrCode EnableDistributed(bool enabled) override;
113     ErrCode EnableDistributedByBundle(const sptr<NotificationBundleOption> &bundleOption, bool enabled) override;
114     ErrCode EnableDistributedSelf(bool enabled) override;
115     ErrCode IsDistributedEnableByBundle(const sptr<NotificationBundleOption> &bundleOption, bool &enabled) override;
116     ErrCode GetDeviceRemindType(NotificationConstant::RemindType &remindType) override;
117 
118     ErrCode ShellDump(const std::string &dumpOption, std::vector<std::string> &dumpInfo) override;
119     ErrCode PublishContinuousTaskNotification(const sptr<NotificationRequest> &request) override;
120     ErrCode CancelContinuousTaskNotification(const std::string &label, int32_t notificationId) override;
121     ErrCode PublishReminder(sptr<ReminderRequest> &reminder) override;
122     ErrCode CancelReminder(const int32_t reminderId) override;
123     ErrCode GetValidReminders(std::vector<sptr<ReminderRequest>> &reminders) override;
124     ErrCode CancelAllReminders() override;
125     ErrCode IsSupportTemplate(const std::string &templateName, bool &support) override;
126     ErrCode IsSpecialUserAllowedNotify(const int32_t &userId, bool &allowed) override;
127     ErrCode SetNotificationsEnabledByUser(const int32_t &deviceId, bool enabled) override;
128     ErrCode DeleteAllByUser(const int32_t &userId) override;
129     ErrCode SetDoNotDisturbDate(const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date) override;
130     ErrCode GetDoNotDisturbDate(const int32_t &userId, sptr<NotificationDoNotDisturbDate> &date) override;
131 
132     // SystemEvent
133     void OnBundleRemoved(const sptr<NotificationBundleOption> &bundleOption);
134 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
135     void OnScreenOn();
136     void OnScreenOff();
137 #endif
138     void OnResourceRemove(int32_t userId);
139     void OnBundleDataCleared(const sptr<NotificationBundleOption> &bundleOption);
140 
141     // Distributed KvStore
142     void OnDistributedKvStoreDeathRecipient();
143     ErrCode CancelPreparedNotification(
144         int notificationId, const std::string &label, const sptr<NotificationBundleOption> &bundleOption);
145     ErrCode PrepareNotificationInfo(
146         const sptr<NotificationRequest> &request, sptr<NotificationBundleOption> &bundleOption);
147     ErrCode PublishPreparedNotification(
148         const sptr<NotificationRequest> &request, const sptr<NotificationBundleOption> &bundleOption);
149 
150 private:
151     struct RecentInfo;
152     AdvancedNotificationService();
153 
154     void StartFilters();
155     void StopFilters();
156     ErrCode Filter(const std::shared_ptr<NotificationRecord> &record);
157 
158     void AddToNotificationList(const std::shared_ptr<NotificationRecord> &record);
159     void UpdateInNotificationList(const std::shared_ptr<NotificationRecord> &record);
160     ErrCode AssignToNotificationList(const std::shared_ptr<NotificationRecord> &record);
161     ErrCode RemoveFromNotificationList(const sptr<NotificationBundleOption> &bundleOption, const std::string &label,
162         int notificationId, sptr<Notification> &notification, bool isCancel = false);
163     ErrCode RemoveFromNotificationList(const std::string &key, sptr<Notification> &notification, bool isCancel = false);
164     ErrCode RemoveFromNotificationListForDeleteAll(const std::string &key,
165         const int &userId, sptr<Notification> &notification);
166     std::vector<std::string> GetNotificationKeys(const sptr<NotificationBundleOption> &bundleOption);
167     bool IsNotificationExists(const std::string &key);
168     void SortNotificationList();
169     static bool NotificationCompare(
170         const std::shared_ptr<NotificationRecord> &first, const std::shared_ptr<NotificationRecord> &second);
171     ErrCode FlowControl(const std::shared_ptr<NotificationRecord> &record);
172 
173     sptr<NotificationSortingMap> GenerateSortingMap();
174     sptr<NotificationBundleOption> GenerateBundleOption();
175     sptr<NotificationBundleOption> GenerateValidBundleOption(const sptr<NotificationBundleOption> &bundleOption);
176 
177     std::string TimeToString(int64_t time);
178     int64_t GetNowSysTime();
179     ErrCode ActiveNotificationDump(std::vector<std::string> &dumpInfo);
180     ErrCode RecentNotificationDump(std::vector<std::string> &dumpInfo);
181 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
182     ErrCode DistributedNotificationDump(std::vector<std::string> &dumpInfo);
183 #endif
184     ErrCode SetRecentNotificationCount(const std::string arg);
185     void UpdateRecentNotification(sptr<Notification> &notification, bool isDelete, int reason);
186 
187     void AdjustDateForDndTypeOnce(int64_t &beginDate, int64_t &endDate);
188     bool CheckPermission();
189     ErrCode PrepereContinuousTaskNotificationRequest(const sptr<NotificationRequest> &request, const int &uid);
190     bool GetActiveUserId(int& userId);
191     void TriggerRemoveWantAgent(const sptr<NotificationRequest> &request);
192     bool CheckApiCompatibility(const sptr<NotificationBundleOption> &bundleOption);
193     ErrCode IsAllowedNotifySelf(const sptr<NotificationBundleOption> &bundleOption, bool &allowed);
194 
195     ErrCode SetNotificationRemindType(sptr<Notification> notification, bool isLocal);
196 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
197     std::vector<std::string> GetLocalNotificationKeys(const sptr<NotificationBundleOption> &bundleOption);
198     NotificationConstant::RemindType GetRemindType();
199     ErrCode DoDistributedPublish(
200         const sptr<NotificationBundleOption> bundleOption, const std::shared_ptr<NotificationRecord> record);
201     ErrCode DoDistributedDelete(const std::string deviceId, const sptr<Notification> notification);
202     std::string GetNotificationDeviceId(const std::string &key);
203     bool CheckDistributedNotificationType(const sptr<NotificationRequest> &request);
204     void OnDistributedPublish(
205         const std::string &deviceId, const std::string &bundleName, sptr<NotificationRequest> &request);
206     void OnDistributedUpdate(
207         const std::string &deviceId, const std::string &bundleName, sptr<NotificationRequest> &request);
208     void OnDistributedDelete(
209         const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id);
210     ErrCode GetDistributedEnableInApplicationInfo(const sptr<NotificationBundleOption> bundleOption, bool &enable);
211 #endif
212 
213     ErrCode SetDoNotDisturbDateByUser(const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date);
214     ErrCode GetDoNotDisturbDateByUser(const int32_t &userId, sptr<NotificationDoNotDisturbDate> &date);
215     ErrCode SetHasPoppedDialog(const sptr<NotificationBundleOption> bundleOption, bool hasPopped);
216     ErrCode GetHasPoppedDialog(const sptr<NotificationBundleOption> bundleOption, bool &hasPopped);
217     ErrCode GetAppTargetBundle(const sptr<NotificationBundleOption> &bundleOption,
218         sptr<NotificationBundleOption> &targetBundle);
219 
220 private:
221     static sptr<AdvancedNotificationService> instance_;
222     static std::mutex instanceMutex_;
223 
224     std::shared_ptr<OHOS::AppExecFwk::EventRunner> runner_ = nullptr;
225     std::shared_ptr<OHOS::AppExecFwk::EventHandler> handler_ = nullptr;
226     std::list<std::shared_ptr<NotificationRecord>> notificationList_;
227     std::list<std::chrono::system_clock::time_point> flowControlTimestampList_;
228     std::shared_ptr<RecentInfo> recentInfo_ = nullptr;
229     std::shared_ptr<DistributedKvStoreDeathRecipient> distributedKvStoreDeathRecipient_ = nullptr;
230     std::shared_ptr<SystemEventObserver> systemEventObserver_ = nullptr;
231     DistributedKv::DistributedKvDataManager dataManager_;
232 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
233     NotificationConstant::DistributedReminderPolicy distributedReminderPolicy_ = DEFAULT_DISTRIBUTED_REMINDER_POLICY;
234     bool localScreenOn_ = true;
235 #endif
236 };
237 }  // namespace Notification
238 }  // namespace OHOS
239 
240 #endif  // ADVANCED_NOTIFICATION_SERVICE_H
241