• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_FRAMEWORKS_ANS_CORE_INCLUDE_REMINDER_DATA_MANAGER_H
17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_ANS_CORE_INCLUDE_REMINDER_DATA_MANAGER_H
18 
19 #include <map>
20 #include <vector>
21 
22 #include "advanced_notification_service.h"
23 #include "ans_inner_errors.h"
24 #include "player.h"
25 #include "reminder_request.h"
26 #include "reminder_store.h"
27 #include "reminder_timer_info.h"
28 
29 namespace OHOS {
30 namespace Notification {
31 class ReminderDataManager final {
32 public:
ReminderDataManager()33     ReminderDataManager()
34     {
35         Init(false);
36     };
~ReminderDataManager()37     ~ReminderDataManager() {};
38 
39     ReminderDataManager(ReminderDataManager &other) = delete;
40     ReminderDataManager& operator = (const ReminderDataManager &other) = delete;
41 
42     /**
43      * @brief Cancels all the reminders relative to the bundle option.
44      *
45      * @param packageName Indicates the package name.
46      * @param userId Indicates the user id which the bundle belong to.
47      * @return ERR_OK if success, else not.
48      */
49     ErrCode CancelAllReminders(const std::string &packageName, const int32_t &userId);
50 
51     /**
52      * @brief Cancels the target reminder relative to the reminder id and bundle option.
53      *
54      * @param reminderId Indicates the reminder id.
55      * @param bundleOption Indicates the bundle option.
56      * @return ERR_OK if success, else not.
57      */
58     ErrCode CancelReminder(const int32_t &reminderId, const sptr<NotificationBundleOption> &bundleOption);
59 
60     /**
61      * @brief Close the target reminder which is showing on panel.
62      *        This is manul operation by user: 1.Click close button of the reminder, 2.remove reminder notification.
63      *
64      * @param want Want information that transferred when the event trigger by user.
65      * @param cancelNotification Indicates whether need to cancel notification or not.
66      */
67     void CloseReminder(const OHOS::EventFwk::Want &want, bool cancelNotification);
68 
69     /**
70      * Dump all the reminders information.
71      *
72      * @return reminders informations.
73      */
74     std::string Dump() const;
75 
76     /**
77      * Obtains the single instance.
78      *
79      * @return Single instance of ReminderDataManager.
80      */
81     static std::shared_ptr<ReminderDataManager> GetInstance();
82     static std::shared_ptr<ReminderDataManager> InitInstance(
83         const sptr<AdvancedNotificationService> &advancedNotificationService);
84 
85     /**
86      * Obtains all the valid reminders (which are not expired) relative to the bundle option.
87      *
88      * @param bundleOption Indicates the bundle option.
89      * @param[out] reminders return the valid reminders.
90      */
91     void GetValidReminders(
92         const sptr<NotificationBundleOption> &bundleOption, std::vector<sptr<ReminderRequest>> &reminders);
93 
94     /**
95      * @brief Inits and recovery data from database.
96      *
97      * @param isFromBootComplete Indicates the init is called when boot completed.
98      */
99     void Init(bool isFromBootComplete);
100 
101     void InitUserId();
102 
103     void OnUserRemove(const int32_t& userId);
104 
105     void OnServiceStart();
106 
107     void OnUserSwitch(const int32_t& userId);
108 
109     /**
110      * @brief Triggered when third party application died.
111      *
112      * @param bundleOption Indicates the bundleOption of third party application.
113      */
114     void OnProcessDiedLocked(const sptr<NotificationBundleOption> &bundleOption);
115 
116     /**
117      * Publishs a scheduled reminder.
118      *
119      * @param reminder Indicates the reminder.
120      * @param bundleOption Indicates bundle option the reminder belongs to.
121      * @return ERR_OK if success, else not.
122      */
123     ErrCode PublishReminder(const sptr<ReminderRequest> &reminder,
124         const sptr<NotificationBundleOption> &bundleOption);
125 
126     /**
127      * @brief Refresh all reminders when date/time or timeZone of device changed by user.
128      *
129      * @param type Indicates it is triggered by dateTime change or timeZone change.
130      */
131     void RefreshRemindersDueToSysTimeChange(uint8_t type);
132 
133     /**
134      * @brief Indicates the single instance of ans that used to execute operations in service.
135      *
136      * @param Indicates the single instance of ans notification service.
137      */
138     void SetService(AdvancedNotificationService *advancedNotificationService);
139 
140     bool ShouldAlert(const sptr<ReminderRequest> &reminder) const;
141 
142     /**
143      * @brief Show the reminder.
144      *
145      * @param isSysTimeChanged Indicates it is triggered as dateTime changed by user or not.
146      * @param want Which contains the given reminder.
147      */
148     void ShowActiveReminder(const EventFwk::Want &want);
149 
150     /**
151      * @brief Snooze the reminder by manual.
152      * 1) Snooze the trigger time to the next.
153      * 2) Update the notification(Update notification lable/content...; Stop audio player and vibrator)
154      * 3) Show the notification dialog in the SystemUI
155      * 4) Start a new reminder, which is recent one now.
156      *
157      * @param want Which contains the given reminder.
158      */
159     void SnoozeReminder(const OHOS::EventFwk::Want &want);
160 
161     /**
162      * Starts the recent reminder timing.
163      */
164     void StartRecentReminder();
165 
166     /**
167      * @brief Terminate the alerting reminder.
168      *
169      * 1. Stop sound and vibrate.
170      * 2. Stop the alerting timer.
171      * 3. Update the reminder state.
172      * 4. Update the display content of the notification.
173      *
174      * @param want Which contains the given reminder.
175      */
176     void TerminateAlerting(const OHOS::EventFwk::Want &want);
177 
178     static const uint8_t TIME_ZONE_CHANGE;
179     static const uint8_t DATE_TIME_CHANGE;
180 
181 private:
182     enum class TimerType : uint8_t {
183         TRIGGER_TIMER,
184         ALERTING_TIMER
185     };
186 
187     /**
188      * Add default slot to the reminder if no slot set by user.
189      *
190      * @param reminder Indicates the reminder.
191      */
192     void AddDefaultSlotIfNeeded(sptr<ReminderRequest> &reminder);
193 
194     /**
195      * Add reminder to showed reminder vector.
196      *
197      * @param reminder Indicates the showed reminder.
198      */
199     void AddToShowedReminders(const sptr<ReminderRequest> &reminder);
200 
201     void CancelAllReminders(const int32_t &userId);
202 
203     /**
204      * @brief Cancels all the reminders of the target bundle or user.
205      *
206      * @param packageName Indicates the packageName need to cancel.
207      * @param userId Indicates the userId to cancel.
208      */
209     void CancelRemindersImplLocked(const std::string &packageName, const int32_t &userId);
210 
211     /**
212      * Cancels the notification relative to the reminder.
213      *
214      * @param reminder Indicates the reminder.
215      */
216     void CancelNotification(const sptr<ReminderRequest> &reminder) const;
217 
218     /**
219      * Check whether the number limit of reminders if exceeded.
220      *
221      * @param bundleName Indicates the target bundle.
222      * @return true if number limit is exceeded.
223      */
224     bool CheckReminderLimitExceededLocked(const sptr<NotificationBundleOption> &bundleOption) const;
225     void CloseReminder(const sptr<ReminderRequest> &reminder, bool cancelNotification);
226 
227     /**
228      * Create a information for timer, such as timer type, repeat policy, interval and want agent.
229      *
230      * @param type Indicates the timer type.
231      * @param reminderRequest Indicates the reminder request.
232      * @return pointer of ReminderTimerInfo.
233      */
234     std::shared_ptr<ReminderTimerInfo> CreateTimerInfo(TimerType type,
235         const sptr<ReminderRequest> &reminderRequest) const;
236     void InitTimerInfo(std::shared_ptr<ReminderTimerInfo> &timerInfo,
237         const sptr<ReminderRequest> &reminderRequest) const;
238 
239     void GetImmediatelyShowRemindersLocked(std::vector<sptr<ReminderRequest>> &reminders) const;
240 
241     std::string GetSoundUri(const sptr<ReminderRequest> &reminder);
242 
243     /**
244      * Find the reminder from reminderVector_ by reminder id.
245      *
246      * @param reminderId Indicates the reminder id.
247      * @return pointer of reminder request or nullptr.
248      */
249     sptr<ReminderRequest> FindReminderRequestLocked(const int32_t &reminderId);
250 
251     /**
252      * Find the reminder from {@link reminderVector_} and
253      * {@link notificationBundleOptionMap_} by reminder id and pkgName.
254      *
255      * @param reminderId Indicates the reminder id.
256      * @param pkgName Indicates the package name.
257      * @return pointer of reminder request or nullptr.
258      */
259     sptr<ReminderRequest> FindReminderRequestLocked(const int32_t &reminderId, const std::string &pkgName);
260 
261     /**
262      * Find bundle option from {@link notificationBundleOptionMap_} by reminder id.
263      *
264      * @param reminderId Indicates the reminder id.
265      * @return pointer of NotificationBundleOption or nullptr.
266      */
267     sptr<NotificationBundleOption> FindNotificationBundleOption(const int32_t &reminderId) const;
268 
269     /**
270      * Obtains the recent reminder which is not expired from reminder vector.
271      *
272      * The expired reminders will be removed from reminderVector_ and notificationBundleOptionMap_.
273      *
274      * @return pointer of reminder object.
275      */
276     sptr<ReminderRequest> GetRecentReminderLocked();
277 
278     void HandleImmediatelyShow(std::vector<sptr<ReminderRequest>> &showImmediately, bool isSysTimeChanged);
279 
280     /**
281      * @brief Refresh the reminder due to date/time or timeZone change by user.
282      *
283      * @param type Indicates it is date/time change or timeZone change.
284      * @param reminder Indicates the target reminder.
285      * @return sptr<ReminderRequest> Returns the target reminder if it is need to show immediately, otherwise nullptr.
286      */
287     sptr<ReminderRequest> HandleRefreshReminder(const uint8_t &type, sptr<ReminderRequest> &reminder);
288 
289     /**
290      * @brief Handles all the reminders that have the same notification id and belong to same application
291      *        with the current reminder. Unset the state of "showing".
292      *
293      * @param reminder Indicates the current reminder.
294      */
295     void HandleSameNotificationIdShowing(const sptr<ReminderRequest> reminder);
296 
297     bool HandleSysTimeChange(const sptr<ReminderRequest> reminder) const;
298 
299     /**
300      * @brief Judge the two reminders is belong to the same application or not.
301      *
302      * @param bundleOption Indicates the bundleOption of first reminder.
303      * @param other Indicates the bundleOption of second reminder.
304      * @return true if the two reminders belong to the same application.
305      */
306     bool IsBelongToSameApp(const sptr<NotificationBundleOption> &bundleOption,
307         const sptr<NotificationBundleOption> &other) const;
308 
309     /**
310      * @brief Judges whether the reminder is matched with the bundleOption or userId.
311      *
312      * @param reminder Indicates the target reminder.
313      * @param packageName Indicates the package name.
314      * @param userId Indicates the user id.
315      * @return true If the reminder is matched with the bundleOption or userId.
316      */
317     bool IsMatched(const sptr<ReminderRequest> &reminder, const std::string &packageName, const int32_t &userId) const;
318 
319     bool IsAllowedNotify(const sptr<ReminderRequest> &reminder) const;
320 
321     bool IsReminderAgentReady() const;
322 
323     void LoadReminderFromDb();
324 
325     void PlaySoundAndVibrationLocked(const sptr<ReminderRequest> &reminder);
326     void PlaySoundAndVibration(const sptr<ReminderRequest> &reminder);
327     void StopSoundAndVibrationLocked(const sptr<ReminderRequest> &reminder);
328     void StopSoundAndVibration(const sptr<ReminderRequest> &reminder);
329 
330     /**
331      * Remove from showed reminder vector.
332      *
333      * @param reminder Indicates the reminder need to remove.
334      */
335     void RemoveFromShowedReminders(const sptr<ReminderRequest> &reminder);
336 
337     /**
338      * @brief Refresh the all reminders due to date/time or timeZone change by user.
339      *
340      * @param type Indicates it is date/time change or timeZone change.
341      * @return reminders that need to show immediately.
342      */
343     std::vector<sptr<ReminderRequest>> RefreshRemindersLocked(uint8_t type);
344 
345     /**
346      * Removes the reminder.
347      * 1. removes the reminder from reminderVector_ and notificationBundleOptionMap_.
348      * 2. cancels the notification.
349      *
350      * @param reminderId Indicates the reminder id.
351      */
352     void RemoveReminderLocked(const int32_t &reminderId);
353 
354     /**
355      * Resets timer status.
356      * 1. Sets timerId_ or timerIdAlerting_ with 0.
357      * 2. Sets activeReminderId_ or alertingReminderId with -1.
358      *
359      * @param type Indicates the timer type.
360      */
361     void ResetStates(TimerType type);
362 
363     void SetActiveReminder(const sptr<ReminderRequest> &reminder);
364     void SetAlertingReminder(const sptr<ReminderRequest> &reminder);
365     void ShowActiveReminderExtendLocked(sptr<ReminderRequest> &reminder);
366 
367     /**
368      * @brief Show the reminder on SystemUI.
369      *
370      * @param reminder Indicates the reminder to show.
371      * @param isNeedToPlaySound Indicates whether need to play sound.
372      * @param isNeedToStartNext Indicates whether need to start next reminder.
373      * @param isSysTimeChanged Indicates whether it is triggerred as system time changed by user.
374      * @param needScheduleTimeout Indicates whether need to control the ring duration.
375      */
376     void ShowReminder(const sptr<ReminderRequest> &reminder, const bool &isNeedToPlaySound,
377         const bool &isNeedToStartNext, const bool &isSysTimeChanged, const bool &needScheduleTimeout);
378 
379     void SnoozeReminderImpl(sptr<ReminderRequest> &reminder);
380 
381     /**
382      * Starts timing actually.
383      *
384      * @param reminderRequest Indicates the reminder.
385      * @param type Indicates the timer type.
386      */
387     void StartTimerLocked(const sptr<ReminderRequest> &reminderRequest, TimerType type);
388     void StartTimer(const sptr<ReminderRequest> &reminderRequest, TimerType type);
389 
390     /**
391      * @brief Stop the alerting timer and update reminder information.
392      *
393      * 1. Stop sound and vibrate.
394      * 2. Stop the alerting timer.
395      *
396      * @param reminder Indicates the target reminder.
397      */
398     void StopAlertingReminder(const sptr<ReminderRequest> &reminder);
399 
400     /**
401      * Stops timing.
402      *
403      * @param type Indicates the timer type.
404      */
405     void StopTimer(TimerType type);
406     void StopTimerLocked(TimerType type);
407 
408     /**
409      * @brief Terminate the alerting reminder.
410      *
411      * 1. Stop sound and vibrate.
412      * 2. Stop the alerting timer.
413      * 3. Update the reminder state.
414      * 4. Update the display content of the notification.
415      *
416      * @param reminder Indicates the reminder.
417      * @param reason Indicates the description information.
418      */
419     void TerminateAlerting(const sptr<ReminderRequest> &reminder, const std::string &reason);
420     void TerminateAlerting(const uint16_t waitInSecond, const sptr<ReminderRequest> &reminder);
421 
422     /**
423      * @brief Assign unique reminder id and save reminder in memory.
424      *
425      * @param reminder Indicates a reminder.
426      * @param bundleOption Indicates bundle option relative to the reminder.
427      */
428     void UpdateAndSaveReminderLocked(
429         const sptr<ReminderRequest> &reminder, const sptr<NotificationBundleOption> &bundleOption);
430 
431     void UpdateNotification(const sptr<ReminderRequest> &reminder);
432 
433     static bool cmp(sptr<ReminderRequest> &reminderRequest, sptr<ReminderRequest> &other);
434 
435    /**
436     * Single instance.
437     */
438     static std::shared_ptr<ReminderDataManager> REMINDER_DATA_MANAGER;
439 
440     /**
441      * Used for multi-thread synchronise.
442      */
443     static std::mutex MUTEX;
444     static std::mutex SHOW_MUTEX;
445     static std::mutex ALERT_MUTEX;
446     static std::mutex TIMER_MUTEX;
447 
448     /**
449      * Max number of reminders limit for the whole system.
450      */
451     static const int16_t MAX_NUM_REMINDER_LIMIT_SYSTEM;
452 
453     /**
454      * Max number of reminders limit for one application.
455      */
456     static const int16_t MAX_NUM_REMINDER_LIMIT_APP;
457 
458     bool isReminderAgentReady_ = false;
459 
460     /**
461      * Vector used to record all the reminders in system.
462      */
463     std::vector<sptr<ReminderRequest>> reminderVector_;
464 
465     /**
466      * Vector used to record all the reminders which has been shown on panel.
467      */
468     std::vector<sptr<ReminderRequest>> showedReminderVector_;
469 
470     /**
471      * Map used to record all the bundle information of the reminders in system.
472      */
473     std::map<int32_t, sptr<NotificationBundleOption>> notificationBundleOptionMap_;
474 
475     /**
476      * This timer is used to control the triggerTime of next reminder.
477      */
478     uint64_t timerId_ {0};
479 
480     /**
481      * This timer is used to control the ringDuration of the alerting reminder.
482      */
483     uint64_t timerIdAlerting_ {0};
484 
485     /**
486      * Indicates the active reminder that timing is taking effect.
487      */
488     int32_t activeReminderId_ = -1;
489     sptr<ReminderRequest> activeReminder_ = nullptr;
490 
491     /**
492      * Indicates the reminder which is playing sound or vibration.
493      */
494     int32_t alertingReminderId_ = -1;
495     sptr<ReminderRequest> alertingReminder_ = nullptr;
496 
497     std::shared_ptr<Media::Player> soundPlayer_ = nullptr;
498 
499     /**
500      * Indicates the total count of reminders in system.
501      */
502     int16_t totalCount_ {0};
503     int currentUserId_ {0};
504     sptr<AdvancedNotificationService> advancedNotificationService_ = nullptr;
505     std::shared_ptr<ReminderStore> store_ = nullptr;
506 };
507 }  // namespace OHOS
508 }  // namespace Notification
509 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_ANS_CORE_INCLUDE_REMINDER_DATA_MANAGER_H