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