• 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_INTERFACES_INNER_API_REMINDER_REQUEST_CALENDAR_H
17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_REMINDER_REQUEST_CALENDAR_H
18 
19 #include "reminder_request.h"
20 
21 namespace OHOS {
22 namespace Notification {
23 class ReminderRequestCalendar : public ReminderRequest {
24 public:
25     /**
26      * @brief A {@link ReminderRequest} child class used for creating reminders of calendar clocks.
27      *
28      * @note The params must meet the following conditions.
29      * otherwise the application may crash due to an illegal parameter exception.
30      * <ul>
31      * <li> The length of repeatMonths vectory cannot exceed 12. </li>
32      * <li> The length of repeateDays vectory cannot exceed 31. </li>
33      * <li> There must be at least one valid reminder time. Ensure that the time specified by dateTime
34      * does not expired, or repeatMonths and repeateDays are valid. </li>
35      * </ul>
36      *
37      * The repeateMonths and repeatDays must both be set to implements repeated reminders.
38      * By default, this reminder is not snoozed. You can call {@link SetTimeInterval} to
39      * set the snooze interval.
40      *
41      * @param dateTime Indicates the date and time when this calendar event reminder will be triggered.
42      *                 The time is accurate to minute. For example, the value
43      *                 {@link LocalDateTime(2021, 3, 3, 16, 15)} indicates that the reminder will be
44      *                 triggered at 16:15 on March 3, 2021.
45      * @param repeatMonths Indicates the months in which this reminder will be repeated. For example,
46      *                     the value {2, 4} indicates that the reminder will be triggered on particular
47      *                     days in February and April.
48      * @param repeatDays Indicates the days in a month when this reminder will be repeated. For example,
49      *                   the value {2, 4} indicates that the reminder will be triggered on the second
50      *                   and fourth day of the specific months.
51      */
52     ReminderRequestCalendar(const tm &dateTime, const std::vector<uint8_t> &repeatMonths,
53         const std::vector<uint8_t> &repeatDays, const std::vector<uint8_t> &daysOfWeek);
54 
55     /**
56      * @brief This constructor should only be used in background proxy service process
57      * when reminder instance recovery from database.
58      *
59      * @param reminderId Indicates reminder id.
60      */
ReminderRequestCalendar(int32_t reminderId)61     explicit ReminderRequestCalendar(int32_t reminderId) : ReminderRequest(reminderId)
62     {
63         SetReminderType(ReminderType::CALENDAR);
64     };
65 
66     explicit ReminderRequestCalendar(const ReminderRequestCalendar &other);
67     ReminderRequestCalendar& operator = (const ReminderRequestCalendar &other);
~ReminderRequestCalendar()68     ~ReminderRequestCalendar() override {}
69 
70     void SetRRuleWantAgentInfo(const std::shared_ptr<WantAgentInfo> &wantAgentInfo);
71 
72     std::shared_ptr<ReminderRequest::WantAgentInfo> GetRRuleWantAgentInfo();
73 
74     void AddExcludeDate(const int64_t date);
75     void DelExcludeDates();
76     std::vector<int64_t> GetExcludeDates() const;
77     bool IsInExcludeDate() const;
78 
GetYear()79     inline uint16_t GetYear() const
80     {
81         return year_;
82     }
83 
GetMonth()84     inline uint8_t GetMonth() const
85     {
86         return month_;
87     }
88 
GetDay()89     inline uint8_t GetDay() const
90     {
91         return day_;
92     }
93 
GetHour()94     inline uint8_t GetHour() const
95     {
96         return hour_;
97     }
98 
GetMinute()99     inline uint8_t GetMinute() const
100     {
101         return minute_;
102     }
103 
GetSecond()104     inline uint8_t GetSecond() const
105     {
106         return second_;
107     }
108 
GetFirstDesignateYear()109     inline uint16_t GetFirstDesignateYear() const
110     {
111         return firstDesignateYear_;
112     }
113 
GetFirstDesignageMonth()114     inline uint16_t GetFirstDesignageMonth() const
115     {
116         return firstDesignateMonth_;
117     }
118 
GetFirstDesignateDay()119     inline uint16_t GetFirstDesignateDay() const
120     {
121         return firstDesignateDay_;
122     }
123 
124     /**
125      * @brief Gets the repeat day.
126      */
GetRepeatDay()127     uint32_t GetRepeatDay() const
128     {
129         return repeatDay_;
130     }
131 
132     /**
133      * @brief Gets the repeat month.
134      */
GetRepeatMonth()135     uint16_t GetRepeatMonth() const
136     {
137         return repeatMonth_;
138     }
139 
140     /**
141      * @brief Gets the start date time.
142      */
143     uint64_t GetDateTime() const;
144 
145     /**
146      * @brief Gets the end date time.
147      */
148     uint64_t GetEndDateTime() const;
149 
150     /**
151      * @brief Sets the year.
152      *
153      * @param year Indicates the year.
154      */
155     void SetYear(const uint16_t year);
156 
157     /**
158      * @brief Sets the month.
159      *
160      * @param month Indicates the month.
161      */
162     void SetMonth(const uint8_t month);
163 
164     /**
165      * @brief Sets the day.
166      *
167      * @param day Indicates the day.
168      */
169     void SetDay(const uint8_t day);
170 
171     /**
172      * @brief Sets the hour.
173      *
174      * @param hour Indicates the hour.
175      */
176     void SetHour(const uint8_t hour);
177 
178     /**
179      * @brief Sets the minute.
180      *
181      * @param minute Indicates the minute.
182      */
183     void SetMinute(const uint8_t minute);
184 
185     /**
186      * @brief Sets the repeat day.
187      *
188      * @param repeatDay Indicates the repeat day.
189      */
190     void SetRepeatDay(const uint32_t repeatDay);
191 
192     /**
193      * @brief Sets the repeat month.
194      *
195      * @param repeatMonth Indicates the repeat month.
196      */
197     void SetRepeatMonth(const uint16_t repeatMonth);
198 
199     /**
200      * @brief Sets the first designate year.
201      *
202      * @param firstDesignateYear Indicates the first designate year.
203      */
204     void SetFirstDesignateYear(const uint16_t firstDesignateYear);
205 
206     /**
207      * @brief Sets the first designate month.
208      *
209      * @param firstDesignateMonth Indicates the first designate month.
210      */
211     void SetFirstDesignageMonth(const uint16_t firstDesignateMonth);
212 
213     /**
214      * @brief Sets the first designate day.
215      *
216      * @param firstDesignateDay Indicates the first designate day.
217      */
218     void SetFirstDesignateDay(const uint16_t firstDesignateDay);
219 
220     /**
221      * @brief Sets the hour.
222      *
223      * @param hour Indicates the hour.
224      */
225     void SetDateTime(const uint64_t time);
226 
227     /**
228      * @brief Serialize the rrule to string.
229      * Persist to the rdb.
230      */
231     std::string SerializationRRule();
232 
233     /**
234      * @brief Deserialize the rrule from string.
235      * Recover from the rdb.
236      */
237     void DeserializationRRule(const std::string& str);
238 
239     /**
240      * @brief Serialize the exclude date to string.
241      * Persist to the rdb.
242      */
243     std::string SerializationExcludeDates();
244 
245     /**
246      * @brief Deserialize the exclude date from string.
247      * Recover from the rdb.
248      */
249     void DeserializationExcludeDates(const std::string& str);
250 
251     bool InitTriggerTime();
252 
253     std::vector<uint8_t> GetRepeatMonths() const;
254     std::vector<uint8_t> GetRepeatDays() const;
255 
256     virtual bool UpdateNextReminder() override;
257     virtual bool OnDateTimeChange() override;
258 
259     /**
260      * @brief Check reminder request is repeat
261      */
262     bool IsRepeat() const override;
263 
264     /**
265      * @brief Check reminder request is in exclude date
266      */
267     bool CheckExcludeDate() override;
268 
269     /**
270      * @brief Check rrule want agent, pull up service extension
271      */
272     bool IsPullUpService() override;
273 
274     /**
275      * @brief Check need notification reminder. due to system timer.
276      * When change system time to later, more than the trigger time, system timer must trigger.
277      */
278     bool IsNeedNotification() override;
279 
280     /**
281      * Marshal a reminder object into a Parcel.
282      *
283      * @param parcel Indicates the Parcel.
284      */
285     virtual bool Marshalling(Parcel &parcel) const override;
286 
287     /**
288      * Unmarshal object from a Parcel.
289      *
290      * @param parcel Indicates the Parcel.
291      * @return reminder object.
292      */
293     static ReminderRequestCalendar *Unmarshalling(Parcel &parcel);
294 
295     /**
296      * Unmarshal unique properties of alarm from a Parcel.
297      *
298      * @param parcel Indicates the Parcel.
299      * @return true if read parcel success.
300      */
301     bool ReadFromParcel(Parcel &parcel) override;
302     bool WriteParcel(Parcel &parcel) const override;
303     bool SetNextTriggerTime() override;
304 
305     static uint8_t GetDaysOfMonth(const uint16_t &year, const uint8_t &month);
306     bool SetEndDateTime(const uint64_t time);
307 
308     /**
309      * @brief Sets the start time when the notification was last displayed in the notification bar.
310      * When OnDateTimeChange or OnClose, the time will change to next start time if the reminder
311      * is repeat, otherwise not changed.
312      */
313     void SetLastStartDateTime(const uint64_t time);
314 
315     /**
316      * @brief Get the start time when the notification was last displayed in the notification bar.
317      */
318     uint64_t GetLastStartDateTime() const;
319 
ReminderRequestCalendar()320     ReminderRequestCalendar() : ReminderRequest(ReminderType::CALENDAR) {};
321 
322     /**
323      * @brief Copy datashare reminder
324      */
325     void Copy(const sptr<ReminderRequest>& other);
326 
327 public:
328     static constexpr uint8_t MAX_MONTHS_OF_YEAR = 12;
329     static constexpr uint8_t MAX_DAYS_OF_MONTH = 31;
330 
331 protected:
332     virtual uint64_t PreGetNextTriggerTimeIgnoreSnooze(bool ignoreRepeat, bool forceToGetNext) override;
333 
334 private:
335 
336     uint8_t GetNextDay(const uint16_t &settedYear, const uint8_t &settedMonth, const tm &now, const tm &target) const;
337     uint64_t GetNextTriggerTime(const bool updateLast = false);
338     uint64_t GetNextTriggerTimeAsRepeatReminder(const tm &nowTime, const tm &tarTime) const;
339     uint64_t GetTimeInstantMilli(
340         uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) const;
341 
342     /**
343      * @brief Init dateTime_ when read from parcel.
344      */
345     void InitDateTime();
346     void InitDateTime(const tm &dateTime);
347     bool IsRepeatReminder() const;
348     bool IsRepeatMonth(uint8_t month) const;
349     bool IsRepeatDay(uint8_t day) const;
350     void SetDay(const uint8_t &day, const bool &isSet);
351     void SetMonth(const uint8_t &month, const bool &isSet);
352     void SetRepeatMonths(const std::vector<uint8_t> &repeatMonths);
353     void SetRepeatDaysOfMonth(const std::vector<uint8_t> &repeatDays);
354     bool CheckCalenderIsExpired(const uint64_t now);
355 
356     /**
357      * @brief When OnShow or OnSnooze, need calculate the start time of this alert
358      */
359     void CalcLastStartDateTime();
360 
361     static const uint8_t DEFAULT_SNOOZE_TIMES;
362 
363     tm dateTime_ = {
364         .tm_sec = 0,
365         .tm_min = 0,
366         .tm_hour = 0,
367         .tm_mday = 1,
368         .tm_mon = 0,
369         .tm_year = 0,
370         .tm_wday = 0,
371         .tm_yday = 0,
372         .tm_isdst = -1
373     };
374     uint16_t firstDesignateYear_ {1};
375     uint8_t firstDesignateMonth_ {1};
376     uint8_t firstDesignateDay_ {1};
377     uint16_t year_ {1};
378     uint8_t month_ {1};
379     uint8_t day_ {1};
380     uint8_t hour_ {1};
381     uint8_t minute_ {1};
382     uint8_t second_ {0};
383     uint16_t repeatMonth_ {0};
384     uint32_t repeatDay_ {0};
385 
386     uint64_t startDateTime_{0};
387     uint64_t endDateTime_{0};
388     uint64_t durationTime_{0};
389     uint64_t lastStartDateTime_{0};
390 
391     std::set<int64_t> excludeDates_;
392 
393     // repeat calendar
394     std::shared_ptr<WantAgentInfo> rruleWantAgentInfo_ = nullptr;
395 
396 private:
397     static const uint8_t DAY_ARRAY[12];
398     static constexpr uint8_t JANUARY = 1;
399     static constexpr uint8_t DECEMBER = 12;
400     // if startDateTime < now and now < endDateTime, triggerTime = now + DELAY
401     static constexpr uint64_t DEFAULT_DELAY_TIME = 3000;
402 
403     // for check leap year
404     static constexpr uint8_t FEBRUARY = 2;
405     static constexpr uint8_t LEAP_MONTH = 29;
406     static constexpr uint8_t NON_LEAP_MONTH = 28;
407     static constexpr uint16_t SOLAR_YEAR = 400;
408     static constexpr uint8_t LEAP_PARAM_MIN = 4;
409     static constexpr uint8_t LEAP_PARAM_MAX = 100;
410 };
411 }
412 }
413 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_REMINDER_REQUEST_CALENDAR_H