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 explicit ReminderRequestCalendar(const ReminderRequestCalendar &other); 64 ReminderRequestCalendar& operator = (const ReminderRequestCalendar &other); ~ReminderRequestCalendar()65 ~ReminderRequestCalendar() override {} 66 GetYear()67 inline uint16_t GetYear() const 68 { 69 return year_; 70 } 71 GetMonth()72 inline uint8_t GetMonth() const 73 { 74 return month_; 75 } 76 GetDay()77 inline uint8_t GetDay() const 78 { 79 return day_; 80 } 81 GetHour()82 inline uint8_t GetHour() const 83 { 84 return hour_; 85 } 86 GetMinute()87 inline uint8_t GetMinute() const 88 { 89 return minute_; 90 } 91 GetSecond()92 inline uint8_t GetSecond() const 93 { 94 return second_; 95 } 96 GetFirstDesignateYear()97 inline uint16_t GetFirstDesignateYear() const 98 { 99 return firstDesignateYear_; 100 } 101 GetFirstDesignageMonth()102 inline uint16_t GetFirstDesignageMonth() const 103 { 104 return firstDesignateMonth_; 105 } 106 GetFirstDesignateDay()107 inline uint16_t GetFirstDesignateDay() const 108 { 109 return firstDesignateDay_; 110 } 111 112 std::vector<uint8_t> GetRepeatMonths() const; 113 std::vector<uint8_t> GetRepeatDays() const; 114 115 virtual bool UpdateNextReminder() override; 116 117 /** 118 * Marshal a reminder object into a Parcel. 119 * 120 * @param parcel Indicates the Parcel. 121 */ 122 virtual bool Marshalling(Parcel &parcel) const override; 123 124 /** 125 * Unmarshal object from a Parcel. 126 * 127 * @param parcel Indicates the Parcel. 128 * @return reminder object. 129 */ 130 static ReminderRequestCalendar *Unmarshalling(Parcel &parcel); 131 132 /** 133 * Unmarshal unique properties of alarm from a Parcel. 134 * 135 * @param parcel Indicates the Parcel. 136 * @return true if read parcel success. 137 */ 138 bool ReadFromParcel(Parcel &parcel) override; 139 bool SetNextTriggerTime() override; 140 141 static const uint8_t MAX_MONTHS_OF_YEAR; 142 static const uint8_t MAX_DAYS_OF_MONTH; 143 virtual void RecoverFromDb(const std::shared_ptr<NativeRdb::ResultSet> &resultSet) override; 144 static void AppendValuesBucket(const sptr<ReminderRequest> &reminder, 145 const sptr<NotificationBundleOption> &bundleOption, NativeRdb::ValuesBucket &values); 146 static uint8_t GetDaysOfMonth(const uint16_t &year, const uint8_t &month); 147 148 // For database recovery. 149 static void InitDbColumns(); 150 151 protected: 152 virtual uint64_t PreGetNextTriggerTimeIgnoreSnooze(bool ignoreRepeat, bool forceToGetNext) const override; 153 154 private: ReminderRequestCalendar()155 ReminderRequestCalendar() : ReminderRequest() {} 156 157 uint8_t GetNextDay(const uint16_t &settedYear, const uint8_t &settedMonth, const tm &now, const tm &target) const; 158 uint64_t GetNextTriggerTime() const; 159 uint64_t GetNextTriggerTimeAsRepeatReminder(const tm &nowTime, const tm &tarTime) const; GetRepeatDay()160 uint32_t GetRepeatDay() const 161 { 162 return repeatDay_; 163 } GetRepeatMonth()164 uint16_t GetRepeatMonth() const 165 { 166 return repeatMonth_; 167 } 168 uint64_t GetTimeInstantMilli( 169 uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) const; 170 171 /** 172 * @brief Init dateTime_ when read from parcel. 173 */ 174 void InitDateTime(); 175 void InitDateTime(const tm &dateTime); 176 bool IsRepeatReminder() const; 177 bool IsRepeatMonth(uint8_t month) const; 178 bool IsRepeatDay(uint8_t day) const; 179 void SetDay(const uint8_t &day, const bool &isSet); 180 void SetMonth(const uint8_t &month, const bool &isSet); 181 void SetRepeatMonths(const std::vector<uint8_t> &repeatMonths); 182 void SetRepeatDaysOfMonth(const std::vector<uint8_t> &repeatDays); 183 184 static const uint8_t JANUARY; 185 static const uint8_t DECEMBER; 186 static const uint8_t DEFAULT_SNOOZE_TIMES; 187 188 tm dateTime_ = { 189 .tm_sec = 0, 190 .tm_min = 0, 191 .tm_hour = 0, 192 .tm_mday = 1, 193 .tm_mon = 0, 194 .tm_year = 0, 195 .tm_wday = 0, 196 .tm_yday = 0, 197 .tm_isdst = -1 198 }; 199 uint16_t firstDesignateYear_ {1}; 200 uint8_t firstDesignateMonth_ {1}; 201 uint8_t firstDesignateDay_ {1}; 202 uint16_t year_ {1}; 203 uint8_t month_ {1}; 204 uint8_t day_ {1}; 205 uint8_t hour_ {1}; 206 uint8_t minute_ {1}; 207 uint8_t second_ {0}; 208 uint16_t repeatMonth_ {0}; 209 uint32_t repeatDay_ {0}; 210 211 static const uint8_t DAY_ARRAY[12]; 212 static const uint8_t FEBRUARY; 213 static const uint8_t LEAP_MONTH; 214 static const uint8_t NON_LEAP_MONTH; 215 static const uint16_t SOLAR_YEAR; 216 static const uint8_t LEAP_PARAM_MIN; 217 static const uint8_t LEAP_PARAM_MAX; 218 }; 219 } 220 } 221 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_REMINDER_REQUEST_CALENDAR_H