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, 53 const std::vector<uint8_t> &repeatMonths, const std::vector<uint8_t> &repeatDays); 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 OnDateTimeChange() override; 116 virtual bool OnTimeZoneChange() override; 117 virtual bool UpdateNextReminder() override; 118 119 /** 120 * Marshal a reminder object into a Parcel. 121 * 122 * @param parcel Indicates the Parcel. 123 */ 124 virtual bool Marshalling(Parcel &parcel) const override; 125 126 /** 127 * Unmarshal object from a Parcel. 128 * 129 * @param parcel Indicates the Parcel. 130 * @return reminder object. 131 */ 132 static ReminderRequestCalendar *Unmarshalling(Parcel &parcel); 133 134 /** 135 * Unmarshal unique properties of alarm from a Parcel. 136 * 137 * @param parcel Indicates the Parcel. 138 * @return true if read parcel success. 139 */ 140 bool ReadFromParcel(Parcel &parcel) override; 141 bool SetNextTriggerTime() override; 142 143 static const uint8_t MAX_MONTHS_OF_YEAR; 144 static const uint8_t MAX_DAYS_OF_MONTH; 145 virtual void RecoverFromDb(const std::shared_ptr<NativeRdb::ResultSet> &resultSet) override; 146 static void AppendValuesBucket(const sptr<ReminderRequest> &reminder, 147 const sptr<NotificationBundleOption> &bundleOption, NativeRdb::ValuesBucket &values); 148 static uint8_t GetDaysOfMonth(const uint16_t &year, const uint8_t &month); 149 150 // For database recovery. 151 static void InitDbColumns(); 152 153 protected: 154 virtual uint64_t PreGetNextTriggerTimeIgnoreSnooze(bool ignoreRepeat, bool forceToGetNext) const override; 155 156 private: ReminderRequestCalendar()157 ReminderRequestCalendar() : ReminderRequest() {} 158 159 uint8_t GetNextDay(const uint16_t &settedYear, const uint8_t &settedMonth, const tm &now, const tm &target) const; 160 uint64_t GetNextTriggerTime() const; 161 uint64_t GetNextTriggerTimeAsRepeatReminder(const tm &nowTime, const tm &tarTime) const; GetRepeatDay()162 uint32_t GetRepeatDay() const 163 { 164 return repeatDay_; 165 } GetRepeatMonth()166 uint16_t GetRepeatMonth() const 167 { 168 return repeatMonth_; 169 } 170 uint64_t GetTimeInstantMilli( 171 uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) const; 172 173 /** 174 * @brief Init dateTime_ when read from parcel. 175 */ 176 void InitDateTime(); 177 void InitDateTime(const tm &dateTime); 178 bool IsRepeatReminder() const; 179 bool IsRepeatMonth(uint8_t month) const; 180 bool IsRepeatDay(uint8_t day) const; 181 void SetDay(const uint8_t &day, const bool &isSet); 182 void SetMonth(const uint8_t &month, const bool &isSet); 183 void SetRepeatMonths(const std::vector<uint8_t> &repeatMonths); 184 void SetRepeatDaysOfMonth(const std::vector<uint8_t> &repeatDays); 185 186 static const uint8_t JANUARY; 187 static const uint8_t DECEMBER; 188 static const uint8_t DEFAULT_SNOOZE_TIMES; 189 190 tm dateTime_ = { 191 .tm_sec = 0, 192 .tm_min = 0, 193 .tm_hour = 0, 194 .tm_mday = 1, 195 .tm_mon = 0, 196 .tm_year = 0, 197 .tm_wday = 0, 198 .tm_yday = 0, 199 .tm_isdst = -1 200 }; 201 uint16_t firstDesignateYear_ {1}; 202 uint8_t firstDesignateMonth_ {1}; 203 uint8_t firstDesignateDay_ {1}; 204 uint16_t year_ {1}; 205 uint8_t month_ {1}; 206 uint8_t day_ {1}; 207 uint8_t hour_ {1}; 208 uint8_t minute_ {1}; 209 uint8_t second_ {0}; 210 uint16_t repeatMonth_ {0}; 211 uint32_t repeatDay_ {0}; 212 213 // For database recovery. 214 static const std::string REPEAT_DAYS; 215 static const std::string REPEAT_MONTHS; 216 static const std::string FIRST_DESIGNATE_YEAR; 217 static const std::string FIRST_DESIGNATE_MONTH; 218 static const std::string FIRST_DESIGNATE_DAY; 219 static const std::string CALENDAR_YEAR; 220 static const std::string CALENDAR_MONTH; 221 static const std::string CALENDAR_DAY; 222 static const std::string CALENDAR_HOUR; 223 static const std::string CALENDAR_MINUTE; 224 225 static const uint8_t DAY_ARRAY[12]; 226 static const uint8_t FEBRUARY; 227 static const uint8_t LEAP_MONTH; 228 static const uint8_t NON_LEAP_MONTH; 229 static const uint16_t SOLAR_YEAR; 230 static const uint8_t LEAP_PARAM_MIN; 231 static const uint8_t LEAP_PARAM_MAX; 232 }; 233 } 234 } 235 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_REMINDER_REQUEST_CALENDAR_H