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 #include "reminder_request_calendar.h"
17
18 #include "ans_log_wrapper.h"
19
20 namespace OHOS {
21 namespace Notification {
22 const uint8_t ReminderRequestCalendar::MAX_MONTHS_OF_YEAR = 12;
23 const uint8_t ReminderRequestCalendar::MAX_DAYS_OF_MONTH = 31;
24 const uint8_t ReminderRequestCalendar::JANUARY = 1;
25 const uint8_t ReminderRequestCalendar::DECEMBER = 12;
26 const uint8_t ReminderRequestCalendar::DEFAULT_SNOOZE_TIMES = 3;
27
28 // For database recovery.
29 const std::string ReminderRequestCalendar::REPEAT_DAYS = "repeat_days";
30 const std::string ReminderRequestCalendar::REPEAT_MONTHS = "repeat_months";
31 const std::string ReminderRequestCalendar::FIRST_DESIGNATE_YEAR = "first_designate_year";
32 const std::string ReminderRequestCalendar::FIRST_DESIGNATE_MONTH = "first_designate_month";
33 const std::string ReminderRequestCalendar::FIRST_DESIGNATE_DAY = "first_designate_day";
34 const std::string ReminderRequestCalendar::CALENDAR_YEAR = "calendar_year";
35 const std::string ReminderRequestCalendar::CALENDAR_MONTH = "calendar_month";
36 const std::string ReminderRequestCalendar::CALENDAR_DAY = "calendar_day";
37 const std::string ReminderRequestCalendar::CALENDAR_HOUR = "calendar_hour";
38 const std::string ReminderRequestCalendar::CALENDAR_MINUTE = "calendar_minute";
39
40 const uint8_t ReminderRequestCalendar::DAY_ARRAY[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
41 const uint8_t ReminderRequestCalendar::FEBRUARY = 2;
42 const uint8_t ReminderRequestCalendar::LEAP_MONTH = 29;
43 const uint8_t ReminderRequestCalendar::NON_LEAP_MONTH = 28;
44 const uint16_t ReminderRequestCalendar::SOLAR_YEAR = 400;
45 const uint8_t ReminderRequestCalendar::LEAP_PARAM_MIN = 4;
46 const uint8_t ReminderRequestCalendar::LEAP_PARAM_MAX = 100;
47
ReminderRequestCalendar(const tm & dateTime,const std::vector<uint8_t> & repeatMonths,const std::vector<uint8_t> & repeatDays)48 ReminderRequestCalendar::ReminderRequestCalendar(const tm &dateTime,
49 const std::vector<uint8_t> &repeatMonths, const std::vector<uint8_t> &repeatDays)
50 : ReminderRequest(ReminderRequest::ReminderType::CALENDAR)
51 {
52 // 1. record the information which designated by user at first time.
53 firstDesignateYear_ = static_cast<uint16_t>(GetActualTime(TimeTransferType::YEAR, dateTime.tm_year));
54 firstDesignateMonth_ = static_cast<uint8_t>(GetActualTime(TimeTransferType::MONTH, dateTime.tm_mon));
55 firstDesignateDay_ = dateTime.tm_mday;
56 SetRepeatMonths(repeatMonths);
57 SetRepeatDaysOfMonth(repeatDays);
58 SetSnoozeTimes(DEFAULT_SNOOZE_TIMES);
59
60 // 2. should SetNextTriggerTime() after constructor
61 InitDateTime(dateTime);
62 }
63
ReminderRequestCalendar(const ReminderRequestCalendar & other)64 ReminderRequestCalendar::ReminderRequestCalendar(const ReminderRequestCalendar &other) : ReminderRequest(other)
65 {
66 dateTime_ = other.dateTime_;
67 firstDesignateYear_ = other.firstDesignateYear_;
68 firstDesignateMonth_ = other.firstDesignateMonth_;
69 firstDesignateDay_ = other.firstDesignateDay_;
70 year_ = other.year_;
71 month_ = other.month_;
72 day_ = other.day_;
73 hour_ = other.hour_;
74 minute_ = other.minute_;
75 second_ = other.second_;
76 repeatMonth_ = other.repeatMonth_;
77 repeatDay_ = other.repeatDay_;
78 }
79
SetNextTriggerTime()80 bool ReminderRequestCalendar::SetNextTriggerTime()
81 {
82 hour_ = static_cast<uint8_t>(dateTime_.tm_hour);
83 minute_ = static_cast<uint8_t>(dateTime_.tm_min);
84 uint64_t nextTriggerTime = INVALID_LONG_LONG_VALUE;
85 if ((nextTriggerTime = GetNextTriggerTime()) != INVALID_LONG_LONG_VALUE) {
86 time_t target = static_cast<time_t>(nextTriggerTime / MILLI_SECONDS);
87 (void)localtime_r(&target, &dateTime_);
88 } else {
89 ANSR_LOGW("Not exist next trigger time, please check the param of ReminderRequestCalendar constructor.");
90 return false;
91 }
92
93 // set the time information (used to transfer to proxy service) which is decided to trigger firstly.
94 year_ = static_cast<uint16_t>(GetActualTime(TimeTransferType::YEAR, dateTime_.tm_year));
95 month_ = static_cast<uint8_t>(GetActualTime(TimeTransferType::MONTH, dateTime_.tm_mon));
96 day_ = static_cast<uint8_t>(dateTime_.tm_mday);
97 second_ = 0;
98 SetTriggerTimeInMilli(nextTriggerTime);
99 return true;
100 }
101
GetDaysOfMonth(const uint16_t & year,const uint8_t & month)102 uint8_t ReminderRequestCalendar::GetDaysOfMonth(const uint16_t &year, const uint8_t &month)
103 {
104 uint8_t days;
105 if (month == FEBRUARY) {
106 days = ((((year % LEAP_PARAM_MIN == 0) && (year % LEAP_PARAM_MAX != 0)) || (year % SOLAR_YEAR == 0))
107 ? LEAP_MONTH : NON_LEAP_MONTH);
108 } else {
109 days = DAY_ARRAY[month - 1];
110 }
111 return days;
112 }
113
GetNextDay(const uint16_t & settedYear,const uint8_t & settedMonth,const tm & now,const tm & target) const114 uint8_t ReminderRequestCalendar::GetNextDay(
115 const uint16_t &settedYear, const uint8_t &settedMonth, const tm &now, const tm &target) const
116 {
117 uint32_t repeatDayTmp = repeatDay_;
118 uint8_t daysOfSpecialMonth = GetDaysOfMonth(settedYear, settedMonth);
119 uint8_t setDayTmp = INVALID_U8_VALUE;
120 for (uint8_t i = 1; i <= daysOfSpecialMonth; i++) {
121 if ((repeatDayTmp & (1 << (i - 1))) > 0) {
122 struct tm setTime;
123 setTime.tm_year = GetCTime(TimeTransferType::YEAR, settedYear);
124 setTime.tm_mon = GetCTime(TimeTransferType::MONTH, settedMonth);
125 setTime.tm_mday = static_cast<int>(i);
126 setTime.tm_hour = target.tm_hour;
127 setTime.tm_min = target.tm_min;
128 setTime.tm_sec = target.tm_sec;
129 setTime.tm_isdst = -1;
130
131 struct tm nowTime;
132 nowTime.tm_year = now.tm_year;
133 nowTime.tm_mon = now.tm_mon;
134 nowTime.tm_mday = now.tm_mday;
135 nowTime.tm_hour = now.tm_hour;
136 nowTime.tm_min = now.tm_min;
137 nowTime.tm_sec = now.tm_sec;
138 nowTime.tm_isdst = -1;
139
140 if (mktime(&nowTime) >= mktime(&setTime)) {
141 continue;
142 } else {
143 setDayTmp = i;
144 return setDayTmp;
145 }
146 }
147 }
148 return setDayTmp;
149 }
150
GetNextTriggerTime() const151 uint64_t ReminderRequestCalendar::GetNextTriggerTime() const
152 {
153 uint64_t triggerTimeInMilli = INVALID_LONG_LONG_VALUE;
154 time_t now;
155 (void)time(&now); // unit is seconds.
156 struct tm nowTime;
157 (void)localtime_r(&now, &nowTime);
158 nowTime.tm_sec = 0;
159 struct tm tarTime;
160 tarTime.tm_year = GetCTime(TimeTransferType::YEAR, firstDesignateYear_);
161 tarTime.tm_mon = GetCTime(TimeTransferType::MONTH, firstDesignateMonth_);
162 tarTime.tm_mday = firstDesignateDay_;
163 tarTime.tm_hour = hour_;
164 tarTime.tm_min = minute_;
165 tarTime.tm_sec = 0;
166 tarTime.tm_isdst = -1;
167
168 ANSR_LOGD("Now time is: %{public}s", GetDateTimeInfo(now).c_str());
169 if (!(repeatMonth_ > 0 && repeatDay_ > 0)) {
170 ANSR_LOGD("tarTime: %{public}d-%{public}d-%{public}d %{public}d:%{public}d:%{public}d",
171 tarTime.tm_year, tarTime.tm_mon, tarTime.tm_mday, tarTime.tm_hour, tarTime.tm_min, tarTime.tm_sec);
172 const time_t target = mktime(&tarTime);
173 if (target == -1) {
174 ANSR_LOGW("mktime return error.");
175 }
176 if (now < target) {
177 triggerTimeInMilli = ReminderRequest::GetDurationSinceEpochInMilli(target);
178 ANSR_LOGD("Next calendar time:%{public}s", GetDateTimeInfo(target).c_str());
179 }
180 return triggerTimeInMilli;
181 }
182 triggerTimeInMilli = GetNextTriggerTimeAsRepeatReminder(nowTime, tarTime);
183 return triggerTimeInMilli;
184 }
185
GetNextTriggerTimeAsRepeatReminder(const tm & nowTime,const tm & tarTime) const186 uint64_t ReminderRequestCalendar::GetNextTriggerTimeAsRepeatReminder(const tm &nowTime, const tm &tarTime) const
187 {
188 uint64_t triggerTimeInMilli = INVALID_LONG_LONG_VALUE;
189 uint16_t setYear = static_cast<uint16_t>(GetActualTime(TimeTransferType::YEAR, nowTime.tm_year));
190 uint8_t setMonth = INVALID_U8_VALUE;
191 uint8_t setDay = INVALID_U8_VALUE;
192 uint8_t beginMonth = static_cast<uint8_t>(GetActualTime(TimeTransferType::MONTH, nowTime.tm_mon));
193 uint8_t count = 1;
194 uint16_t repeatMonthTmp = repeatMonth_;
195 for (uint8_t i = beginMonth; i < (MAX_MONTHS_OF_YEAR + beginMonth + 1); i++) {
196 if ((repeatMonthTmp & (1 << ((i - 1) % MAX_MONTHS_OF_YEAR))) > 0) {
197 setMonth = (i % MAX_MONTHS_OF_YEAR);
198 setMonth = setMonth == 0 ? DECEMBER : setMonth;
199 if (count != 1) {
200 setYear = setMonth <= beginMonth ? setYear + 1 : setYear;
201 }
202 setDay = GetNextDay(setYear, setMonth, nowTime, tarTime);
203 }
204 if (setDay != INVALID_U8_VALUE) {
205 break;
206 }
207 count++;
208 }
209 if ((triggerTimeInMilli = GetTimeInstantMilli(setYear, setMonth, setDay, hour_, minute_, second_))
210 != INVALID_LONG_LONG_VALUE) {
211 ANSR_LOGD("Next calendar time:%{public}hu/%{public}hhu/%{public}hhu %{public}hhu:%{public}hhu:%{public}hhu",
212 setYear, setMonth, setDay, hour_, minute_, second_);
213 }
214 return triggerTimeInMilli;
215 }
216
GetTimeInstantMilli(uint16_t year,uint8_t month,uint8_t day,uint8_t hour,uint8_t minute,uint8_t second) const217 uint64_t ReminderRequestCalendar::GetTimeInstantMilli(
218 uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) const
219 {
220 struct tm tar;
221 tar.tm_year = GetCTime(TimeTransferType::YEAR, year);
222 tar.tm_mon = GetCTime(TimeTransferType::MONTH, month);
223 tar.tm_mday = static_cast<int>(day);
224 tar.tm_hour = static_cast<int>(hour);
225 tar.tm_min = static_cast<int>(minute);
226 tar.tm_sec = static_cast<int>(second);
227 tar.tm_isdst = -1;
228
229 ANSR_LOGD("tar: %{public}d-%{public}d-%{public}d %{public}d:%{public}d:%{public}d",
230 tar.tm_year, tar.tm_mon, tar.tm_mday, tar.tm_hour, tar.tm_min, tar.tm_sec);
231 const time_t target = mktime(&tar);
232 if (target == -1) {
233 ANSR_LOGW("mktime return error.");
234 return INVALID_LONG_LONG_VALUE;
235 }
236 return ReminderRequest::GetDurationSinceEpochInMilli(target);
237 }
238
InitDateTime()239 void ReminderRequestCalendar::InitDateTime()
240 {
241 dateTime_.tm_year = GetCTime(TimeTransferType::YEAR, year_);
242 dateTime_.tm_mon = GetCTime(TimeTransferType::MONTH, month_);
243 dateTime_.tm_mday = static_cast<int>(day_);
244 dateTime_.tm_hour = static_cast<int>(hour_);
245 dateTime_.tm_min = static_cast<int>(minute_);
246 dateTime_.tm_sec = static_cast<int>(second_);
247 dateTime_.tm_isdst = -1;
248 }
249
InitDateTime(const tm & dateTime)250 void ReminderRequestCalendar::InitDateTime(const tm &dateTime)
251 {
252 dateTime_.tm_year = dateTime.tm_year;
253 dateTime_.tm_mon = dateTime.tm_mon;
254 dateTime_.tm_mday = dateTime.tm_mday;
255 dateTime_.tm_hour = dateTime.tm_hour;
256 dateTime_.tm_min = dateTime.tm_min;
257 dateTime_.tm_sec = dateTime.tm_sec;
258 dateTime_.tm_isdst = -1;
259 }
260
IsRepeatReminder() const261 bool ReminderRequestCalendar::IsRepeatReminder() const
262 {
263 return (repeatMonth_ > 0 && repeatDay_ > 0) || (GetTimeInterval() > 0 && GetSnoozeTimes() > 0);
264 }
265
266
IsRepeatMonth(uint8_t month) const267 bool ReminderRequestCalendar::IsRepeatMonth(uint8_t month) const
268 {
269 return (repeatMonth_ & (1 << (month - 1))) > 0;
270 }
271
IsRepeatDay(uint8_t day) const272 bool ReminderRequestCalendar::IsRepeatDay(uint8_t day) const
273 {
274 return (repeatDay_ & (1 << (day - 1))) > 0;
275 }
276
SetDay(const uint8_t & day,const bool & isSet)277 void ReminderRequestCalendar::SetDay(const uint8_t &day, const bool &isSet)
278 {
279 if (day < 1 || day > MAX_DAYS_OF_MONTH) {
280 return;
281 }
282 if (isSet) {
283 repeatDay_ |= 1 << (day - 1);
284 } else {
285 repeatDay_ &= ~(1 << (day - 1));
286 }
287 }
288
SetMonth(const uint8_t & month,const bool & isSet)289 void ReminderRequestCalendar::SetMonth(const uint8_t &month, const bool &isSet)
290 {
291 if (month < JANUARY || month > DECEMBER) {
292 return;
293 }
294 if (isSet) {
295 repeatMonth_ |= 1 << (month - 1);
296 } else {
297 repeatMonth_ &= ~ (1 << (month - 1));
298 }
299 }
300
SetRepeatMonths(const std::vector<uint8_t> & repeatMonths)301 void ReminderRequestCalendar::SetRepeatMonths(const std::vector<uint8_t> &repeatMonths)
302 {
303 if (repeatMonths.size() > MAX_MONTHS_OF_YEAR) {
304 ANSR_LOGW("The length of repeat months array should not larger than %{public}hhu", MAX_MONTHS_OF_YEAR);
305 return;
306 }
307 repeatMonth_ = 0;
308 for (auto it = repeatMonths.begin(); it != repeatMonths.end(); ++it) {
309 SetMonth((*it), true);
310 }
311 }
312
SetRepeatDaysOfMonth(const std::vector<uint8_t> & repeatDays)313 void ReminderRequestCalendar::SetRepeatDaysOfMonth(const std::vector<uint8_t> &repeatDays)
314 {
315 if (repeatDays.size() > MAX_DAYS_OF_MONTH) {
316 ANSR_LOGW("The length of repeat days array should not larger than %{public}hhu", MAX_DAYS_OF_MONTH);
317 return;
318 }
319 repeatDay_ = 0;
320 for (auto it = repeatDays.begin(); it != repeatDays.end(); ++it) {
321 SetDay((*it), true);
322 }
323 }
324
GetRepeatMonths() const325 std::vector<uint8_t> ReminderRequestCalendar::GetRepeatMonths() const
326 {
327 std::vector<uint8_t> repeatMonths;
328 for (int32_t i = 0; i < MAX_MONTHS_OF_YEAR; i++) {
329 if (IsRepeatMonth(i + 1)) {
330 repeatMonths.push_back(i + 1);
331 }
332 }
333 return repeatMonths;
334 }
335
GetRepeatDays() const336 std::vector<uint8_t> ReminderRequestCalendar::GetRepeatDays() const
337 {
338 std::vector<uint8_t> repeatDays;
339 for (int32_t i = 0; i < MAX_DAYS_OF_MONTH; i++) {
340 if (IsRepeatDay(i + 1)) {
341 repeatDays.push_back(i + 1);
342 }
343 }
344 return repeatDays;
345 }
346
OnDateTimeChange()347 bool ReminderRequestCalendar::OnDateTimeChange()
348 {
349 return ReminderRequest::OnDateTimeChange();
350 }
351
OnTimeZoneChange()352 bool ReminderRequestCalendar::OnTimeZoneChange()
353 {
354 return ReminderRequest::OnTimeZoneChange();
355 }
356
UpdateNextReminder()357 bool ReminderRequestCalendar::UpdateNextReminder()
358 {
359 ANSR_LOGD("UpdateNextReminder calendar time");
360 if (!IsRepeatReminder()) {
361 ANSR_LOGI("No need to update next trigger time as it is an one-time reminder.");
362 SetSnoozeTimesDynamic(GetSnoozeTimes());
363 SetExpired(true);
364 return false;
365 }
366 uint8_t leftSnoozeTimes = GetSnoozeTimesDynamic();
367 if (leftSnoozeTimes > 0 && (GetTimeInterval() > 0)) {
368 ANSR_LOGI("Left snooze times: %{public}d, update next triggerTime", leftSnoozeTimes);
369 SetTriggerTimeInMilli(GetTriggerTimeInMilli() + GetTimeInterval() * MILLI_SECONDS);
370 SetSnoozeTimesDynamic(--leftSnoozeTimes);
371 } else {
372 SetSnoozeTimesDynamic(GetSnoozeTimes());
373 if (repeatMonth_ == 0 || repeatDay_ == 0) {
374 ANSR_LOGI("Not a day repeat reminder, no need to update to next trigger time.");
375 SetExpired(true);
376 return false;
377 } else {
378 uint64_t nextTriggerTime = GetNextTriggerTime();
379 if (nextTriggerTime != INVALID_LONG_LONG_VALUE) {
380 ANSR_LOGI("Set next trigger time successful, reset dynamic snoozeTimes");
381 SetTriggerTimeInMilli(nextTriggerTime);
382 } else {
383 ANSR_LOGW("Set next trigger time invalidate");
384 SetExpired(true);
385 return false;
386 }
387 }
388 }
389 return true;
390 }
391
PreGetNextTriggerTimeIgnoreSnooze(bool ignoreRepeat,bool forceToGetNext) const392 uint64_t ReminderRequestCalendar::PreGetNextTriggerTimeIgnoreSnooze(bool ignoreRepeat, bool forceToGetNext) const
393 {
394 if (ignoreRepeat || (repeatMonth_ > 0 && repeatDay_ > 0)) {
395 return GetNextTriggerTime();
396 } else {
397 return INVALID_LONG_LONG_VALUE;
398 }
399 }
400
Marshalling(Parcel & parcel) const401 bool ReminderRequestCalendar::Marshalling(Parcel &parcel) const
402 {
403 ReminderRequest::Marshalling(parcel);
404
405 // write int
406 if (!parcel.WriteUint16(year_)) {
407 ANSR_LOGE("Failed to write year");
408 return false;
409 }
410 if (!parcel.WriteUint8(month_)) {
411 ANSR_LOGE("Failed to write month");
412 return false;
413 }
414 if (!parcel.WriteUint8(day_)) {
415 ANSR_LOGE("Failed to write day");
416 return false;
417 }
418 if (!parcel.WriteUint8(hour_)) {
419 ANSR_LOGE("Failed to write hour");
420 return false;
421 }
422 if (!parcel.WriteUint8(minute_)) {
423 ANSR_LOGE("Failed to write minute");
424 return false;
425 }
426 if (!parcel.WriteUint8(second_)) {
427 ANSR_LOGE("Failed to write second");
428 return false;
429 }
430 if (!parcel.WriteUint16(repeatMonth_)) {
431 ANSR_LOGE("Failed to write repeatMonth");
432 return false;
433 }
434 if (!parcel.WriteUint32(repeatDay_)) {
435 ANSR_LOGE("Failed to write repeateDay");
436 return false;
437 }
438 if (!parcel.WriteUint16(firstDesignateYear_)) {
439 ANSR_LOGE("Failed to write firstDesignateYear");
440 return false;
441 }
442 if (!parcel.WriteUint8(firstDesignateMonth_)) {
443 ANSR_LOGE("Failed to write firstDesignateMonth");
444 return false;
445 }
446 if (!parcel.WriteUint8(firstDesignateDay_)) {
447 ANSR_LOGE("Failed to write firstDesignateDay");
448 return false;
449 }
450 return true;
451 }
452
Unmarshalling(Parcel & parcel)453 ReminderRequestCalendar *ReminderRequestCalendar::Unmarshalling(Parcel &parcel)
454 {
455 ANSR_LOGD("New calendar");
456 auto objptr = new (std::nothrow) ReminderRequestCalendar();
457 if (objptr == nullptr) {
458 ANS_LOGE("Failed to create reminder calendar due to no memory.");
459 return objptr;
460 }
461 if (!objptr->ReadFromParcel(parcel)) {
462 delete objptr;
463 objptr = nullptr;
464 }
465 return objptr;
466 }
467
ReadFromParcel(Parcel & parcel)468 bool ReminderRequestCalendar::ReadFromParcel(Parcel &parcel)
469 {
470 ReminderRequest::ReadFromParcel(parcel);
471
472 // read int
473 if (!parcel.ReadUint16(year_)) {
474 ANSR_LOGE("Failed to read year");
475 return false;
476 }
477 if (!parcel.ReadUint8(month_)) {
478 ANSR_LOGE("Failed to read month");
479 return false;
480 }
481 if (!parcel.ReadUint8(day_)) {
482 ANSR_LOGE("Failed to read day");
483 return false;
484 }
485 if (!parcel.ReadUint8(hour_)) {
486 ANSR_LOGE("Failed to read hour");
487 return false;
488 }
489 if (!parcel.ReadUint8(minute_)) {
490 ANSR_LOGE("Failed to read minute");
491 return false;
492 }
493 if (!parcel.ReadUint8(second_)) {
494 ANSR_LOGE("Failed to read second");
495 return false;
496 }
497 if (!parcel.ReadUint16(repeatMonth_)) {
498 ANSR_LOGE("Failed to read repeatMonth");
499 return false;
500 }
501 if (!parcel.ReadUint32(repeatDay_)) {
502 ANSR_LOGE("Failed to read repeateDay");
503 return false;
504 }
505 InitDateTime();
506 if (!parcel.ReadUint16(firstDesignateYear_)) {
507 ANSR_LOGE("Failed to read firstDesignateYear");
508 return false;
509 }
510 if (!parcel.ReadUint8(firstDesignateMonth_)) {
511 ANSR_LOGE("Failed to read firstDesignateMonth");
512 return false;
513 }
514 if (!parcel.ReadUint8(firstDesignateDay_)) {
515 ANSR_LOGE("Failed to read firstDesignateDay");
516 return false;
517 }
518 return true;
519 }
520
RecoverFromDb(const std::shared_ptr<NativeRdb::ResultSet> & resultSet)521 void ReminderRequestCalendar::RecoverFromDb(const std::shared_ptr<NativeRdb::ResultSet> &resultSet)
522 {
523 ReminderRequest::RecoverFromDb(resultSet);
524
525 // repeatDay
526 repeatDay_ = static_cast<uint32_t>(RecoverInt64FromDb(resultSet, REPEAT_DAYS, DbRecoveryType::INT));
527
528 // repeatMonth
529 repeatMonth_ =
530 static_cast<uint16_t>(RecoverInt64FromDb(resultSet, REPEAT_MONTHS, DbRecoveryType::INT));
531
532 // firstDesignateYear
533 firstDesignateYear_ =
534 static_cast<uint16_t>(RecoverInt64FromDb(resultSet, FIRST_DESIGNATE_YEAR, DbRecoveryType::INT));
535
536 // firstDesignateMonth
537 firstDesignateMonth_ =
538 static_cast<uint8_t>(RecoverInt64FromDb(resultSet, FIRST_DESIGNATE_MONTH, DbRecoveryType::INT));
539
540 // firstDesignateDay
541 firstDesignateDay_ =
542 static_cast<uint8_t>(RecoverInt64FromDb(resultSet, FIRST_DESIGNATE_DAY, DbRecoveryType::INT));
543
544 // year
545 year_ = static_cast<uint16_t>(RecoverInt64FromDb(resultSet, CALENDAR_YEAR, DbRecoveryType::INT));
546
547 // month
548 month_ = static_cast<uint8_t>(RecoverInt64FromDb(resultSet, CALENDAR_MONTH, DbRecoveryType::INT));
549
550 // day
551 day_ = static_cast<uint8_t>(RecoverInt64FromDb(resultSet, CALENDAR_DAY, DbRecoveryType::INT));
552
553 // hour
554 hour_ = static_cast<uint8_t>(RecoverInt64FromDb(resultSet, CALENDAR_HOUR, DbRecoveryType::INT));
555
556 // minute
557 minute_ = static_cast<uint8_t>(RecoverInt64FromDb(resultSet, CALENDAR_MINUTE, DbRecoveryType::INT));
558 }
559
AppendValuesBucket(const sptr<ReminderRequest> & reminder,const sptr<NotificationBundleOption> & bundleOption,NativeRdb::ValuesBucket & values)560 void ReminderRequestCalendar::AppendValuesBucket(const sptr<ReminderRequest> &reminder,
561 const sptr<NotificationBundleOption> &bundleOption, NativeRdb::ValuesBucket &values)
562 {
563 uint32_t repeatDay = 0;
564 uint16_t repeatMonth = 0;
565 uint16_t firstDesignateYear = 0;
566 uint8_t firstDesignateMonth = 0;
567 uint8_t firstDesignateDay = 0;
568 uint16_t year = 0;
569 uint8_t month = 0;
570 uint8_t day = 0;
571 uint8_t hour = 0;
572 uint8_t minute = 0;
573 if (reminder->GetReminderType() == ReminderRequest::ReminderType::CALENDAR) {
574 ReminderRequestCalendar* calendar = static_cast<ReminderRequestCalendar*>(reminder.GetRefPtr());
575 if (calendar != nullptr) {
576 repeatDay = calendar->GetRepeatDay();
577 repeatMonth = calendar->GetRepeatMonth();
578 firstDesignateYear = calendar->GetFirstDesignateYear();
579 firstDesignateMonth = calendar->GetFirstDesignageMonth();
580 firstDesignateDay = calendar->GetFirstDesignateDay();
581 year = calendar->GetYear();
582 month = calendar->GetMonth();
583 day = calendar->GetDay();
584 hour = calendar->GetHour();
585 minute = calendar->GetMinute();
586 }
587 }
588 values.PutInt(REPEAT_DAYS, repeatDay);
589 values.PutInt(REPEAT_MONTHS, repeatMonth);
590 values.PutInt(FIRST_DESIGNATE_YEAR, firstDesignateYear);
591 values.PutInt(FIRST_DESIGNATE_MONTH, firstDesignateMonth);
592 values.PutInt(FIRST_DESIGNATE_DAY, firstDesignateDay);
593 values.PutInt(CALENDAR_YEAR, year);
594 values.PutInt(CALENDAR_MONTH, month);
595 values.PutInt(CALENDAR_DAY, day);
596 values.PutInt(CALENDAR_HOUR, hour);
597 values.PutInt(CALENDAR_MINUTE, minute);
598 }
599
InitDbColumns()600 void ReminderRequestCalendar::InitDbColumns()
601 {
602 ReminderRequest::AddColumn(REPEAT_DAYS, "INT", false);
603 ReminderRequest::AddColumn(REPEAT_MONTHS, "INT", false);
604 ReminderRequest::AddColumn(FIRST_DESIGNATE_YEAR, "INT", false);
605 ReminderRequest::AddColumn(FIRST_DESIGNATE_MONTH, "INT", false);
606 ReminderRequest::AddColumn(FIRST_DESIGNATE_DAY, "INT", false);
607 ReminderRequest::AddColumn(CALENDAR_YEAR, "INT", false);
608 ReminderRequest::AddColumn(CALENDAR_MONTH, "INT", false);
609 ReminderRequest::AddColumn(CALENDAR_DAY, "INT", false);
610 ReminderRequest::AddColumn(CALENDAR_HOUR, "INT", false);
611 ReminderRequest::AddColumn(CALENDAR_MINUTE, "INT", false);
612 }
613 }
614 }