From a2645f8043804e9985ebe068ff5717c3ea362d10 Mon Sep 17 00:00:00 2001 From: zhengweina Date: Tue, 24 Oct 2023 20:29:01 +0800 Subject: [PATCH 1/5] calendar add weekly reminder Signed-off-by: zhengweina --- frameworks/ans/src/reminder_request.cpp | 117 +++++++++++++- frameworks/ans/src/reminder_request_alarm.cpp | 147 ++---------------- .../ans/src/reminder_request_calendar.cpp | 24 +-- .../unittest/reminder_request_alarm_test.cpp | 20 +-- .../reminder_request_calendar_test.cpp | 44 ++++-- .../ans_notification_annex_test.cpp | 3 +- .../napi/include/reminder/reminder_common.h | 2 +- frameworks/js/napi/src/reminder/publish.cpp | 17 +- .../js/napi/src/reminder/reminder_common.cpp | 11 +- interfaces/inner_api/reminder_request.h | 37 ++++- interfaces/inner_api/reminder_request_alarm.h | 36 ----- .../inner_api/reminder_request_calendar.h | 4 +- .../reminderrequestalarm_fuzzer.cpp | 15 +- .../reminderrequestcalendar_fuzzer.cpp | 8 +- 14 files changed, 263 insertions(+), 222 deletions(-) diff --git a/frameworks/ans/src/reminder_request.cpp b/frameworks/ans/src/reminder_request.cpp index 7563cbaf3..49e20a810 100644 --- a/frameworks/ans/src/reminder_request.cpp +++ b/frameworks/ans/src/reminder_request.cpp @@ -65,7 +65,11 @@ const std::string ReminderRequest::PARAM_REMINDER_ID = "REMINDER_ID"; const std::string ReminderRequest::SEP_BUTTON_SINGLE = ""; const std::string ReminderRequest::SEP_BUTTON_MULTI = ""; const std::string ReminderRequest::SEP_WANT_AGENT = ""; -const int32_t ReminderRequest::SUNDAY = 7; +const uint8_t ReminderRequest::DAYS_PER_WEEK = 7; +const uint8_t ReminderRequest::MONDAY = 1; +const uint8_t ReminderRequest::SUNDAY = 7; +const uint8_t ReminderRequest::HOURS_PER_DAY = 24; +const uint16_t ReminderRequest::SECONDS_PER_HOUR = 3600; // For database recovery. const std::string ReminderRequest::REMINDER_ID = "reminder_id"; @@ -99,6 +103,7 @@ const std::string ReminderRequest::AGENT = "agent"; const std::string ReminderRequest::MAX_SCREEN_AGENT = "maxScreen_agent"; const std::string ReminderRequest::TAP_DISMISSED = "tapDismissed"; const std::string ReminderRequest::AUTO_DELETED_TIME = "autoDeletedTime"; +const std::string ReminderRequest::REPEAT_DAYS_OF_WEEK = "repeat_days_of_week"; std::string ReminderRequest::sqlOfAddColumns = ""; std::vector ReminderRequest::columns; @@ -135,6 +140,7 @@ ReminderRequest::ReminderRequest(const ReminderRequest &other) this->tapDismissed_= other.tapDismissed_; this->autoDeletedTime_ = other.autoDeletedTime_; this->customButtonUri_ = other.customButtonUri_; + this->repeatDaysOfWeek_ = other.repeatDaysOfWeek_; } ReminderRequest::ReminderRequest(int32_t reminderId) @@ -536,6 +542,8 @@ void ReminderRequest::RecoverFromDb(const std::shared_ptr // state state_ = static_cast(RecoverInt64FromDb(resultSet, STATE, DbRecoveryType::INT)); + //repeatDaysOfWeek_ + repeatDaysOfWeek_ = static_cast(RecoverInt64FromDb(resultSet, REPEAT_DAYS_OF_WEEK, DbRecoveryType::INT)); // action buttons RecoverActionButton(resultSet); @@ -1067,6 +1075,10 @@ bool ReminderRequest::Marshalling(Parcel &parcel) const ANSR_LOGE("Failed to write state"); return false; } + if (!parcel.WriteUint8(repeatDaysOfWeek_)) { + ANSR_LOGE("Failed to write repeatDaysOfWeek"); + return false; + } // write enum if (!parcel.WriteUint8(static_cast(reminderType_))) { @@ -1224,6 +1236,10 @@ bool ReminderRequest::ReadFromParcel(Parcel &parcel) ANSR_LOGE("Failed to read state"); return false; } + if (!parcel.ReadUint8(repeatDaysOfWeek_)) { + ANSR_LOGE("Failed to read repeatDaysOfWeek"); + return false; + } // read enum uint8_t reminderType = static_cast(ReminderType::INVALID); @@ -1656,7 +1672,7 @@ void ReminderRequest::UpdateNotificationContent(const bool &setSnooze) } else { // the reminder has already snoozed by period arithmetic, when the ring duration is over. extendContent = expiredContent_; - } + } if (extendContent == "") { displayContent_ = content_; } else { @@ -1772,6 +1788,7 @@ void ReminderRequest::AppendValuesBucket(const sptr &reminder, values.PutString(CONTENT, reminder->GetContent()); values.PutString(SNOOZE_CONTENT, reminder->GetSnoozeContent()); values.PutString(EXPIRED_CONTENT, reminder->GetExpiredContent()); + values.PutInt(REPEAT_DAYS_OF_WEEK, reminder->GetRepeatDaysOfWeek()); auto wantAgentInfo = reminder->GetWantAgentInfo(); if (wantAgentInfo == nullptr) { std::string info = "null" + ReminderRequest::SEP_WANT_AGENT + "null" + ReminderRequest::SEP_WANT_AGENT + "null"; @@ -1826,6 +1843,7 @@ void ReminderRequest::InitDbColumns() AddColumn(MAX_SCREEN_AGENT, "TEXT", false); AddColumn(TAP_DISMISSED, "TEXT", false); AddColumn(AUTO_DELETED_TIME, "BIGINT", false); + AddColumn(REPEAT_DAYS_OF_WEEK, "INT", false); } void ReminderRequest::AddColumn( @@ -1838,5 +1856,100 @@ void ReminderRequest::AddColumn( sqlOfAddColumns += name + " " + type; } } + +int64_t ReminderRequest::GetNextDaysOfWeek(const time_t now, const time_t target) const +{ + struct tm nowTime; + (void)localtime_r(&now, &nowTime); + int32_t today = GetActualTime(TimeTransferType::WEEK, nowTime.tm_wday); + int32_t dayCount = now >= target ? 1 : 0; + for (; dayCount <= DAYS_PER_WEEK; dayCount++) { + int32_t day = (today + dayCount) % DAYS_PER_WEEK; + day = (day == 0) ? SUNDAY : day; + if (IsRepeatDaysOfWeek(day)) { + break; + } + } + ANSR_LOGI("NextDayInterval is %{public}d", dayCount); + time_t nextTriggerTime = target + dayCount * HOURS_PER_DAY * SECONDS_PER_HOUR; + return GetTriggerTime(now, nextTriggerTime); +} + +bool ReminderRequest::IsRepeatDaysOfWeek(int32_t day) const +{ + return (repeatDaysOfWeek_ & (1 << (day - 1))) > 0; +} + +time_t ReminderRequest::GetTriggerTimeWithDST(const time_t now, const time_t nextTriggerTime) const +{ + time_t triggerTime = nextTriggerTime; + struct tm nowLocal; + struct tm nextLocal; + (void)localtime_r(&now, &nowLocal); + (void)localtime_r(&nextTriggerTime, &nextLocal); + if (nowLocal.tm_isdst == 0 && nextLocal.tm_isdst > 0) { + triggerTime -= SECONDS_PER_HOUR; + } else if (nowLocal.tm_isdst > 0 && nextLocal.tm_isdst == 0) { + triggerTime += SECONDS_PER_HOUR; + } + return triggerTime; +} + +uint8_t ReminderRequest::GetRepeatDaysOfWeek() const +{ + return repeatDaysOfWeek_; +} + +void ReminderRequest::SetRepeatDaysOfWeek(bool set, std::vector daysOfWeek) +{ + if (daysOfWeek.size() == 0) { + return; + } + if (daysOfWeek.size() > DAYS_PER_WEEK) { + ANSR_LOGE("The length of daysOfWeek should not larger than 7"); + return; + } + for (std::vector::iterator it = daysOfWeek.begin(); it != daysOfWeek.end(); ++it) { + if (*it < MONDAY || *it > SUNDAY) { + continue; + } + if (set) { + repeatDaysOfWeek_ |= 1 << (*it - 1); + } else { + repeatDaysOfWeek_ &= ~(1 << (*it - 1)); + } + } +} + +std::vector ReminderRequest::GetDaysOfWeek() const +{ + std::vector repeatDays; + int32_t days[] = {1, 2, 3, 4, 5, 6, 7}; + int32_t len = sizeof(days) / sizeof(int32_t); + for (int32_t i = 0; i < len; i++) { + if (IsRepeatDaysOfWeek(days[i])) { + repeatDays.push_back(days[i]); + } + } + return repeatDays; +} + +uint64_t ReminderRequest::GetTriggerTime(const time_t now, const time_t nextTriggerTime) const +{ + time_t triggerTime = GetTriggerTimeWithDST(now, nextTriggerTime); + struct tm test; + (void)localtime_r(&triggerTime, &test); + ANSR_LOGI("NextTriggerTime: year=%{public}d, mon=%{public}d, day=%{public}d, hour=%{public}d, " + "min=%{public}d, sec=%{public}d, week=%{public}d, nextTriggerTime=%{public}lld", + GetActualTime(TimeTransferType::YEAR, test.tm_year), + GetActualTime(TimeTransferType::MONTH, test.tm_mon), + test.tm_mday, test.tm_hour, test.tm_min, test.tm_sec, + GetActualTime(TimeTransferType::WEEK, test.tm_wday), (long long)triggerTime); + + if (static_cast(triggerTime) <= 0) { + return 0; + } + return GetDurationSinceEpochInMilli(triggerTime); +} } } \ No newline at end of file diff --git a/frameworks/ans/src/reminder_request_alarm.cpp b/frameworks/ans/src/reminder_request_alarm.cpp index 1357fc683..baba77d24 100644 --- a/frameworks/ans/src/reminder_request_alarm.cpp +++ b/frameworks/ans/src/reminder_request_alarm.cpp @@ -20,17 +20,10 @@ namespace OHOS { namespace Notification { -const uint8_t ReminderRequestAlarm::DAYS_PER_WEEK = 7; -const uint8_t ReminderRequestAlarm::MONDAY = 1; -const uint8_t ReminderRequestAlarm::SUNDAY = 7; -const uint8_t ReminderRequestAlarm::HOURS_PER_DAY = 24; -const uint16_t ReminderRequestAlarm::SECONDS_PER_HOUR = 3600; const uint8_t ReminderRequestAlarm::MINUTES_PER_HOUR = 60; -const int8_t ReminderRequestAlarm::INVALID_INT_VALUE = -1; const int8_t ReminderRequestAlarm::DEFAULT_SNOOZE_TIMES = 3; // For database recovery. -const std::string ReminderRequestAlarm::REPEAT_DAYS_OF_WEEK = "repeat_days_of_week"; const std::string ReminderRequestAlarm::ALARM_HOUR = "alarm_hour"; const std::string ReminderRequestAlarm::ALARM_MINUTE = "alarm_minute"; @@ -41,7 +34,7 @@ ReminderRequestAlarm::ReminderRequestAlarm(uint8_t hour, uint8_t minute, const s hour_ = hour; minute_ = minute; CheckParamValid(); - SetDaysOfWeek(true, daysOfWeek); + SetRepeatDaysOfWeek(true, daysOfWeek); SetTriggerTimeInMilli(GetNextTriggerTime(true)); } @@ -49,8 +42,7 @@ ReminderRequestAlarm::ReminderRequestAlarm(const ReminderRequestAlarm &other) : { this->hour_ = other.hour_; this->minute_ = other.minute_; - this->repeatDays_ = other.repeatDays_; - ANSR_LOGD("hour_=%{public}d, minute_=%{public}d, repeatDays_=%{public}d", hour_, minute_, repeatDays_); + ANSR_LOGD("hour_=%{public}d, minute_=%{public}d, repeatDaysOfWeek_=%{public}d", hour_, minute_, other.repeatDaysOfWeek_); } void ReminderRequestAlarm::CheckParamValid() const @@ -67,37 +59,17 @@ void ReminderRequestAlarm::CheckParamValid() const bool ReminderRequestAlarm::IsRepeatReminder() const { - if ((repeatDays_ != 0) || ((GetTimeInterval() > 0) && (GetSnoozeTimes() > 0))) { + if ((repeatDaysOfWeek_ != 0) || ((GetTimeInterval() > 0) && (GetSnoozeTimes() > 0))) { return true; } else { return false; } } -void ReminderRequestAlarm::SetDaysOfWeek(bool set, std::vector daysOfWeek) -{ - if (daysOfWeek.size() == 0) { - return; - } - if (daysOfWeek.size() > DAYS_PER_WEEK) { - ANSR_LOGE("The length of daysOfWeek should not larger than 7"); - return; - } - for (std::vector::iterator it = daysOfWeek.begin(); it != daysOfWeek.end(); ++it) { - if (*it < MONDAY || *it > SUNDAY) { - continue; - } - if (set) { - repeatDays_ |= 1 << (*it - 1); - } else { - repeatDays_ &= ~(1 << (*it - 1)); - } - } -} uint64_t ReminderRequestAlarm::PreGetNextTriggerTimeIgnoreSnooze(bool ignoreRepeat, bool forceToGetNext) const { - if (ignoreRepeat || (repeatDays_)) { + if (ignoreRepeat || (repeatDaysOfWeek_)) { return GetNextTriggerTime(forceToGetNext); } else { return INVALID_LONG_LONG_VALUE; @@ -127,86 +99,19 @@ uint64_t ReminderRequestAlarm::GetNextTriggerTime(bool forceToGetNext) const tar.tm_isdst = -1; const time_t target = mktime(&tar); - int8_t nextDayInterval = GetNextAlarm(now, target); - time_t nextTriggerTime = 0; - if (nextDayInterval == INVALID_INT_VALUE) { - if (now >= target) { - if (forceToGetNext) { - nextTriggerTime = target + 1 * HOURS_PER_DAY * SECONDS_PER_HOUR; - } - } else { - nextTriggerTime = target; - } - } else { - nextTriggerTime = target + nextDayInterval * HOURS_PER_DAY * SECONDS_PER_HOUR; + if (repeatDaysOfWeek_ > 0) { + return GetNextDaysOfWeek(now, target); } - time_t triggerTime = GetTriggerTimeWithDST(now, nextTriggerTime); - struct tm test; - (void)localtime_r(&triggerTime, &test); - ANSR_LOGI("NextTriggerTime: year=%{public}d, mon=%{public}d, day=%{public}d, hour=%{public}d, " - "min=%{public}d, sec=%{public}d, week=%{public}d, nextTriggerTime=%{public}lld", - GetActualTime(TimeTransferType::YEAR, test.tm_year), - GetActualTime(TimeTransferType::MONTH, test.tm_mon), - test.tm_mday, test.tm_hour, test.tm_min, test.tm_sec, - GetActualTime(TimeTransferType::WEEK, test.tm_wday), (long long)triggerTime); - - if (static_cast(triggerTime) <= 0) { - return 0; - } - return ReminderRequest::GetDurationSinceEpochInMilli(triggerTime); -} -time_t ReminderRequestAlarm::GetTriggerTimeWithDST(const time_t now, const time_t nextTriggerTime) const -{ - time_t triggerTime = nextTriggerTime; - struct tm nowLocal; - struct tm nextLocal; - (void)localtime_r(&now, &nowLocal); - (void)localtime_r(&nextTriggerTime, &nextLocal); - if (nowLocal.tm_isdst == 0 && nextLocal.tm_isdst > 0) { - triggerTime -= SECONDS_PER_HOUR; - } else if (nowLocal.tm_isdst > 0 && nextLocal.tm_isdst == 0) { - triggerTime += SECONDS_PER_HOUR; - } - return triggerTime; -} - -int8_t ReminderRequestAlarm::GetNextAlarm(const time_t now, const time_t target) const -{ - if (repeatDays_ == 0) { - return INVALID_INT_VALUE; - } - struct tm nowTime; - (void)localtime_r(&now, &nowTime); - int32_t today = GetActualTime(TimeTransferType::WEEK, nowTime.tm_wday); - int32_t dayCount = now >= target ? 1 : 0; - for (; dayCount <= DAYS_PER_WEEK; dayCount++) { - int32_t day = (today + dayCount) % DAYS_PER_WEEK; - day = (day == 0) ? SUNDAY : day; - if (IsRepeatDay(day)) { - break; - } - } - ANSR_LOGI("NextDayInterval is %{public}d", dayCount); - return dayCount; -} - -bool ReminderRequestAlarm::IsRepeatDay(int32_t day) const -{ - return (repeatDays_ & (1 << (day - 1))) > 0; -} - -std::vector ReminderRequestAlarm::GetDaysOfWeek() const -{ - std::vector repeatDays; - int32_t days[] = {1, 2, 3, 4, 5, 6, 7}; - int32_t len = sizeof(days) / sizeof(int32_t); - for (int32_t i = 0; i < len; i++) { - if (IsRepeatDay(days[i])) { - repeatDays.push_back(days[i]); + time_t nextTriggerTime = 0; + if (now >= target) { + if (forceToGetNext) { + nextTriggerTime = target + 1 * HOURS_PER_DAY * SECONDS_PER_HOUR; } + } else { + nextTriggerTime = target; } - return repeatDays; + return GetTriggerTime(now, nextTriggerTime); } uint8_t ReminderRequestAlarm::GetHour() const @@ -219,11 +124,6 @@ uint8_t ReminderRequestAlarm::GetMinute() const return minute_; } -uint8_t ReminderRequestAlarm::GetRepeatDay() const -{ - return repeatDays_; -} - bool ReminderRequestAlarm::OnDateTimeChange() { return ReminderRequest::OnDateTimeChange(); @@ -245,7 +145,7 @@ bool ReminderRequestAlarm::UpdateNextReminder() SetSnoozeTimesDynamic(--letfSnoozeTimes); } else { SetSnoozeTimesDynamic(GetSnoozeTimes()); - if (repeatDays_ == 0) { + if (repeatDaysOfWeek_ == 0) { ANSR_LOGI("No need to update next triggerTime"); SetExpired(true); return false; @@ -282,10 +182,6 @@ bool ReminderRequestAlarm::Marshalling(Parcel &parcel) const ANSR_LOGE("Failed to write minute"); return false; } - if (!parcel.WriteUint8(repeatDays_)) { - ANSR_LOGE("Failed to write daysOfWeek"); - return false; - } return true; } @@ -317,11 +213,8 @@ bool ReminderRequestAlarm::ReadFromParcel(Parcel &parcel) ANSR_LOGE("Failed to read minute"); return false; } - if (!parcel.ReadUint8(repeatDays_)) { - ANSR_LOGE("Failed to read repeatDays"); - return false; - } - ANSR_LOGD("hour_=%{public}d, minute_=%{public}d, repeatDays_=%{public}d", hour_, minute_, repeatDays_); + + ANSR_LOGD("hour_=%{public}d, minute_=%{public}d", hour_, minute_); return true; } @@ -329,10 +222,6 @@ void ReminderRequestAlarm::RecoverFromDb(const std::shared_ptr(RecoverInt64FromDb(resultSet, REPEAT_DAYS_OF_WEEK, DbRecoveryType::INT)); - // hour hour_ = static_cast(RecoverInt64FromDb(resultSet, ALARM_HOUR, DbRecoveryType::INT)); @@ -345,23 +234,19 @@ void ReminderRequestAlarm::RecoverFromDb(const std::shared_ptr &reminder, const sptr &bundleOption, NativeRdb::ValuesBucket &values) { - uint8_t repeatDays = 0; uint8_t hour = 0; uint8_t minute = 0; if (reminder->GetReminderType() == ReminderRequest::ReminderType::ALARM) { ReminderRequestAlarm* alarm = static_cast(reminder.GetRefPtr()); - repeatDays = alarm->GetRepeatDay(); hour = alarm->GetHour(); minute = alarm->GetMinute(); } - values.PutInt(REPEAT_DAYS_OF_WEEK, repeatDays); values.PutInt(ALARM_HOUR, hour); values.PutInt(ALARM_MINUTE, minute); } void ReminderRequestAlarm::InitDbColumns() { - ReminderRequest::AddColumn(REPEAT_DAYS_OF_WEEK, "INT", false); ReminderRequest::AddColumn(ALARM_HOUR, "INT", false); ReminderRequest::AddColumn(ALARM_MINUTE, "INT", true); } diff --git a/frameworks/ans/src/reminder_request_calendar.cpp b/frameworks/ans/src/reminder_request_calendar.cpp index 2bec0a079..ea37d77f8 100644 --- a/frameworks/ans/src/reminder_request_calendar.cpp +++ b/frameworks/ans/src/reminder_request_calendar.cpp @@ -45,8 +45,8 @@ const uint16_t ReminderRequestCalendar::SOLAR_YEAR = 400; const uint8_t ReminderRequestCalendar::LEAP_PARAM_MIN = 4; const uint8_t ReminderRequestCalendar::LEAP_PARAM_MAX = 100; -ReminderRequestCalendar::ReminderRequestCalendar(const tm &dateTime, - const std::vector &repeatMonths, const std::vector &repeatDays) +ReminderRequestCalendar::ReminderRequestCalendar(const tm &dateTime, const std::vector &repeatMonths, + const std::vector &repeatDays, const std::vector &daysOfWeek) : ReminderRequest(ReminderRequest::ReminderType::CALENDAR) { // 1. record the information which designated by user at first time. @@ -55,6 +55,7 @@ ReminderRequestCalendar::ReminderRequestCalendar(const tm &dateTime, firstDesignateDay_ = dateTime.tm_mday; SetRepeatMonths(repeatMonths); SetRepeatDaysOfMonth(repeatDays); + SetRepeatDaysOfWeek(true, daysOfWeek); SetSnoozeTimes(DEFAULT_SNOOZE_TIMES); // 2. should SetNextTriggerTime() after constructor @@ -164,12 +165,16 @@ uint64_t ReminderRequestCalendar::GetNextTriggerTime() const tarTime.tm_min = minute_; tarTime.tm_sec = 0; tarTime.tm_isdst = -1; - + const time_t target = mktime(&tarTime); ANSR_LOGD("Now time is: %{public}s", GetDateTimeInfo(now).c_str()); - if (!(repeatMonth_ > 0 && repeatDay_ > 0)) { + if (repeatMonth_ > 0 && repeatDay_ > 0) { + triggerTimeInMilli = GetNextTriggerTimeAsRepeatReminder(nowTime, tarTime); + } else if (repeatDaysOfWeek_ > 0 && (target < now)) { + const time_t tar = mktime(&tarTime); + triggerTimeInMilli = GetNextDaysOfWeek(now, tar); + } else { ANSR_LOGD("tarTime: %{public}d-%{public}d-%{public}d %{public}d:%{public}d:%{public}d", tarTime.tm_year, tarTime.tm_mon, tarTime.tm_mday, tarTime.tm_hour, tarTime.tm_min, tarTime.tm_sec); - const time_t target = mktime(&tarTime); if (target == -1) { ANSR_LOGW("mktime return error."); } @@ -177,9 +182,7 @@ uint64_t ReminderRequestCalendar::GetNextTriggerTime() const triggerTimeInMilli = ReminderRequest::GetDurationSinceEpochInMilli(target); ANSR_LOGD("Next calendar time:%{public}s", GetDateTimeInfo(target).c_str()); } - return triggerTimeInMilli; } - triggerTimeInMilli = GetNextTriggerTimeAsRepeatReminder(nowTime, tarTime); return triggerTimeInMilli; } @@ -260,7 +263,8 @@ void ReminderRequestCalendar::InitDateTime(const tm &dateTime) bool ReminderRequestCalendar::IsRepeatReminder() const { - return (repeatMonth_ > 0 && repeatDay_ > 0) || (GetTimeInterval() > 0 && GetSnoozeTimes() > 0); + return (repeatMonth_ > 0 && repeatDay_ > 0) || (repeatDaysOfWeek_ > 0) + || (GetTimeInterval() > 0 && GetSnoozeTimes() > 0); } @@ -376,7 +380,7 @@ bool ReminderRequestCalendar::UpdateNextReminder() SetSnoozeTimesDynamic(--leftSnoozeTimes); } else { SetSnoozeTimesDynamic(GetSnoozeTimes()); - if (repeatMonth_ == 0 || repeatDay_ == 0) { + if ((repeatMonth_ == 0 || repeatDay_ == 0) && (repeatDaysOfWeek_ == 0)) { ANSR_LOGI("Not a day repeat reminder, no need to update to next trigger time."); SetExpired(true); return false; @@ -397,7 +401,7 @@ bool ReminderRequestCalendar::UpdateNextReminder() uint64_t ReminderRequestCalendar::PreGetNextTriggerTimeIgnoreSnooze(bool ignoreRepeat, bool forceToGetNext) const { - if (ignoreRepeat || (repeatMonth_ > 0 && repeatDay_ > 0)) { + if (ignoreRepeat || (repeatMonth_ > 0 && repeatDay_ > 0) || (repeatDaysOfWeek_ > 0)) { return GetNextTriggerTime(); } else { return INVALID_LONG_LONG_VALUE; diff --git a/frameworks/ans/test/unittest/reminder_request_alarm_test.cpp b/frameworks/ans/test/unittest/reminder_request_alarm_test.cpp index 41a77a276..ffabd85d7 100644 --- a/frameworks/ans/test/unittest/reminder_request_alarm_test.cpp +++ b/frameworks/ans/test/unittest/reminder_request_alarm_test.cpp @@ -91,7 +91,7 @@ HWTEST_F(ReminderRequestAlarmTest, initDaysOfWeek_00100, Function | SmallTest | std::vector daysOfWeek (arr, arr + sizeof(arr) / sizeof(uint8_t)); auto rrc = std::make_shared(0, 0, daysOfWeek); uint8_t expectedVal = 7; - EXPECT_TRUE(rrc->GetRepeatDay() == expectedVal) << "repeatDays (1, 2, 3) should be 7"; + EXPECT_TRUE(rrc->GetRepeatDaysOfWeek() == expectedVal) << "repeatDays (1, 2, 3) should be 7"; } /** @@ -105,7 +105,7 @@ HWTEST_F(ReminderRequestAlarmTest, initDaysOfWeek_00200, Function | SmallTest | uint8_t arr[] = {1, 7}; std::vector daysOfWeek (arr, arr + sizeof(arr) / sizeof(uint8_t)); auto rrc = std::make_shared(0, 0, daysOfWeek); - EXPECT_TRUE(rrc->GetRepeatDay() == 65) << "repeatDays (1, 12) should be 65"; + EXPECT_TRUE(rrc->GetRepeatDaysOfWeek() == 65) << "repeatDays (1, 12) should be 65"; } /** @@ -119,7 +119,7 @@ HWTEST_F(ReminderRequestAlarmTest, initDaysOfWeek_00300, Function | SmallTest | uint8_t arr[] = {1, 1, 5, 5, 7, 7, 7}; std::vector daysOfWeek (arr, arr + sizeof(arr) / sizeof(uint8_t)); auto rrc = std::make_shared(0, 0, daysOfWeek); - EXPECT_TRUE(rrc->GetRepeatDay() == 81) << "repeatDays (1, 1, 5 12) should be 81"; + EXPECT_TRUE(rrc->GetRepeatDaysOfWeek() == 81) << "repeatDays (1, 1, 5 12) should be 81"; } /** @@ -134,7 +134,7 @@ HWTEST_F(ReminderRequestAlarmTest, initDaysOfWeek_00400, Function | SmallTest | std::vector daysOfWeek (arr, arr + sizeof(arr) / sizeof(uint8_t)); auto rrc = std::make_shared(0, 0, daysOfWeek); uint8_t expectedVal = 0; - EXPECT_TRUE(rrc->GetRepeatDay() == expectedVal) << "repeatDays () should be 0"; + EXPECT_TRUE(rrc->GetRepeatDaysOfWeek() == expectedVal) << "repeatDays () should be 0"; } /** @@ -352,7 +352,7 @@ HWTEST_F(ReminderRequestAlarmTest, RecoverFromDb_00100, Function | SmallTest | L auto rrc = std::make_shared(0, 0, daysOfWeek); std::shared_ptr resultSet = nullptr; rrc->RecoverFromDb(resultSet); - uint8_t ret = rrc->GetRepeatDay(); + uint8_t ret = rrc->GetRepeatDaysOfWeek(); EXPECT_EQ(ret, 0); } @@ -444,7 +444,7 @@ HWTEST_F(ReminderRequestAlarmTest, CheckParamValid_00200, Function | SmallTest | /** * @tc.name: SetDaysOfWeek_00100 - * @tc.desc: Test SetDaysOfWeek parameters. + * @tc.desc: Test SetRepeatDaysOfWeek parameters. * @tc.type: FUNC * @tc.require: issue */ @@ -455,14 +455,14 @@ HWTEST_F(ReminderRequestAlarmTest, SetDaysOfWeek_00100, Function | SmallTest | L auto rrc = std::make_shared(1, 1, daysOfWeek); bool set = true; - rrc->SetDaysOfWeek(set, daysOfWeek); + rrc->SetRepeatDaysOfWeek(set, daysOfWeek); std::vector result = rrc->GetDaysOfWeek(); EXPECT_EQ(result.size(), 0); } /** * @tc.name: SetDaysOfWeek_00200 - * @tc.desc: Test SetDaysOfWeek parameters. + * @tc.desc: Test SetRepeatDaysOfWeek parameters. * @tc.type: FUNC * @tc.require: issue */ @@ -473,7 +473,7 @@ HWTEST_F(ReminderRequestAlarmTest, SetDaysOfWeek_00200, Function | SmallTest | L auto rrc = std::make_shared(1, 1, daysOfWeek); bool set = false; - rrc->SetDaysOfWeek(set, daysOfWeek); + rrc->SetRepeatDaysOfWeek(set, daysOfWeek); std::vector result = rrc->GetDaysOfWeek(); EXPECT_EQ(result.size(), 0); } @@ -522,7 +522,7 @@ HWTEST_F(ReminderRequestAlarmTest, UpdateNextReminder_00200, Function | SmallTes ret->SetSnoozeTimesDynamic(0); EXPECT_EQ(ret->GetSnoozeTimesDynamic(), 0); - uint8_t result = reminderRequestAlarm->GetRepeatDay(); + uint8_t result = reminderRequestAlarm->GetRepeatDaysOfWeek(); EXPECT_EQ(result, 0); EXPECT_EQ(reminderRequestAlarm->IsRepeatReminder(), true); EXPECT_EQ(reminderRequestAlarm->UpdateNextReminder(), true); diff --git a/frameworks/ans/test/unittest/reminder_request_calendar_test.cpp b/frameworks/ans/test/unittest/reminder_request_calendar_test.cpp index fbfa0a05e..8f36447f1 100644 --- a/frameworks/ans/test/unittest/reminder_request_calendar_test.cpp +++ b/frameworks/ans/test/unittest/reminder_request_calendar_test.cpp @@ -56,9 +56,11 @@ public: nowTime.tm_min = 1; std::vector repeatMonths; std::vector repeatDays; + std::vector daysOfWeek; repeatMonths.push_back(1); repeatDays.push_back(1); - auto calendar = std::make_shared(nowTime, repeatMonths, repeatDays); + daysOfWeek.push_back(1); + auto calendar = std::make_shared(nowTime, repeatMonths, repeatDays, daysOfWeek); calendar->SetNextTriggerTime(); return calendar; } @@ -145,9 +147,11 @@ HWTEST_F(ReminderRequestCalendarTest, initDateTime_00400, Function | SmallTest | std::vector repeatMonths; std::vector repeatDays; + std::vector daysOfWeek; + daysOfWeek.push_back(1); repeatMonths.push_back(1); repeatDays.push_back(1); - auto calendar = std::make_shared(nowTime, repeatMonths, repeatDays); + auto calendar = std::make_shared(nowTime, repeatMonths, repeatDays, daysOfWeek); calendar->SetNextTriggerTime(); std::vector actualRepeatMonths = calendar->GetRepeatMonths(); EXPECT_TRUE(ReminderRequestCalendarTest::IsVectorEqual(repeatMonths, actualRepeatMonths)) @@ -155,7 +159,7 @@ HWTEST_F(ReminderRequestCalendarTest, initDateTime_00400, Function | SmallTest | repeatMonths.clear(); repeatMonths.push_back(12); - calendar = std::make_shared(nowTime, repeatMonths, repeatDays); + calendar = std::make_shared(nowTime, repeatMonths, repeatDays, daysOfWeek); calendar->SetNextTriggerTime(); actualRepeatMonths = calendar->GetRepeatMonths(); EXPECT_TRUE(ReminderRequestCalendarTest::IsVectorEqual(repeatMonths, actualRepeatMonths)) @@ -165,7 +169,7 @@ HWTEST_F(ReminderRequestCalendarTest, initDateTime_00400, Function | SmallTest | for (uint8_t i = 1; i <= 12; i++) { repeatMonths.push_back(i); } - calendar = std::make_shared(nowTime, repeatMonths, repeatDays); + calendar = std::make_shared(nowTime, repeatMonths, repeatDays, daysOfWeek); calendar->SetNextTriggerTime(); actualRepeatMonths = calendar->GetRepeatMonths(); EXPECT_TRUE(ReminderRequestCalendarTest::IsVectorEqual(repeatMonths, actualRepeatMonths)) @@ -188,16 +192,18 @@ HWTEST_F(ReminderRequestCalendarTest, initDateTime_00500, Function | SmallTest | nowTime.tm_year += 1; std::vector repeatMonth; std::vector repeatDay; + std::vector daysOfWeek; + daysOfWeek.push_back(1); repeatMonth.push_back(-1); repeatDay.push_back(1); - auto calendar = std::make_shared(nowTime, repeatMonth, repeatDay); + auto calendar = std::make_shared(nowTime, repeatMonth, repeatDay, daysOfWeek); calendar->SetNextTriggerTime(); std::vector actualRepeatMonths = calendar->GetRepeatMonths(); EXPECT_TRUE(actualRepeatMonths.size() == 0) << "Set repeat month with -1 error."; repeatMonth.clear(); repeatMonth.push_back(13); - calendar = std::make_shared(nowTime, repeatMonth, repeatDay); + calendar = std::make_shared(nowTime, repeatMonth, repeatDay, daysOfWeek); calendar->SetNextTriggerTime(); actualRepeatMonths = calendar->GetRepeatMonths(); EXPECT_TRUE(actualRepeatMonths.size() == 0) << "Set repeat month with 13 error."; @@ -218,9 +224,11 @@ HWTEST_F(ReminderRequestCalendarTest, initDateTime_00600, Function | SmallTest | tm nowTime = *tmp; std::vector repeatMonths; std::vector repeatDays; + std::vector daysOfWeek; + daysOfWeek.push_back(1); repeatMonths.push_back(1); repeatDays.push_back(1); - auto calendar = std::make_shared(nowTime, repeatMonths, repeatDays); + auto calendar = std::make_shared(nowTime, repeatMonths, repeatDays, daysOfWeek); calendar->SetNextTriggerTime(); std::vector actualRepeatDays = calendar->GetRepeatDays(); EXPECT_TRUE(ReminderRequestCalendarTest::IsVectorEqual(repeatDays, actualRepeatDays)) @@ -228,7 +236,7 @@ HWTEST_F(ReminderRequestCalendarTest, initDateTime_00600, Function | SmallTest | repeatDays.clear(); repeatDays.push_back(31); - calendar = std::make_shared(nowTime, repeatMonths, repeatDays); + calendar = std::make_shared(nowTime, repeatMonths, repeatDays, daysOfWeek); calendar->SetNextTriggerTime(); actualRepeatDays = calendar->GetRepeatDays(); EXPECT_TRUE(ReminderRequestCalendarTest::IsVectorEqual(repeatDays, actualRepeatDays)) @@ -238,7 +246,7 @@ HWTEST_F(ReminderRequestCalendarTest, initDateTime_00600, Function | SmallTest | for (uint8_t i = 1; i <= 31; i++) { repeatDays.push_back(i); } - calendar = std::make_shared(nowTime, repeatMonths, repeatDays); + calendar = std::make_shared(nowTime, repeatMonths, repeatDays, daysOfWeek); calendar->SetNextTriggerTime(); actualRepeatDays = calendar->GetRepeatDays(); EXPECT_TRUE(ReminderRequestCalendarTest::IsVectorEqual(repeatDays, actualRepeatDays)) @@ -261,16 +269,18 @@ HWTEST_F(ReminderRequestCalendarTest, initDateTime_00700, Function | SmallTest | nowTime.tm_year += 1; std::vector repeatMonths; std::vector repeatDays; + std::vector daysOfWeek; + daysOfWeek.push_back(-1); repeatMonths.push_back(-1); repeatDays.push_back(-1); - auto calendar = std::make_shared(nowTime, repeatMonths, repeatDays); + auto calendar = std::make_shared(nowTime, repeatMonths, repeatDays, daysOfWeek); calendar->SetNextTriggerTime(); std::vector actualRepeatDays = calendar->GetRepeatDays(); EXPECT_TRUE(actualRepeatDays.size() == 0) << "Set repeat day with -1 error."; repeatDays.clear(); repeatDays.push_back(32); - calendar = std::make_shared(nowTime, repeatMonths, repeatDays); + calendar = std::make_shared(nowTime, repeatMonths, repeatDays, daysOfWeek); calendar->SetNextTriggerTime(); actualRepeatDays = calendar->GetRepeatDays(); EXPECT_TRUE(actualRepeatDays.size() == 0) << "Set repeat day with 32 error."; @@ -418,9 +428,11 @@ HWTEST_F(ReminderRequestCalendarTest, PreGetNextTriggerTimeIgnoreSnooze_02000, F nowTime.tm_year += 1; std::vector repeatMonths; std::vector repeatDays; + std::vector daysOfWeek; + daysOfWeek.push_back(-1); repeatMonths.push_back(-1); - repeatDays.push_back(1); - auto calendar = std::make_shared(nowTime, repeatMonths, repeatDays); + repeatDays.push_back(-1); + auto calendar = std::make_shared(nowTime, repeatMonths, repeatDays, daysOfWeek); EXPECT_EQ(calendar->PreGetNextTriggerTimeIgnoreSnooze(ignoreRepeat, forceToGetNext), 0); } @@ -641,6 +653,10 @@ HWTEST_F(ReminderRequestCalendarTest, UpdateNextReminder_00001, Function | Small uint8_t month = 1; calendar->SetMonth(month, isSet); + std::vector repeatDaysOfWeek; + repeatDaysOfWeek.push_back(1); + calendar->SetRepeatDaysOfWeek(isSet, repeatDaysOfWeek); + auto rrc = std::make_shared(); rrc->SetSnoozeTimes(0); EXPECT_EQ(rrc->GetSnoozeTimes(), 0) << "Get snoozeTimes not 1"; @@ -654,8 +670,10 @@ HWTEST_F(ReminderRequestCalendarTest, UpdateNextReminder_00001, Function | Small uint32_t ret = calendar->GetRepeatDay(); uint16_t ret2 = calendar->GetRepeatMonth(); + uint8_t ret3 = calendar->GetRepeatMonth(); EXPECT_EQ(ret, 0); EXPECT_EQ(ret2, 0); + EXPECT_EQ(ret3, 0); bool result3 = calendar->UpdateNextReminder(); EXPECT_EQ(result3, false); diff --git a/frameworks/core/test/unittest/ans_notification_annex_test/ans_notification_annex_test.cpp b/frameworks/core/test/unittest/ans_notification_annex_test/ans_notification_annex_test.cpp index 294effc1b..9aaf4e2e2 100644 --- a/frameworks/core/test/unittest/ans_notification_annex_test/ans_notification_annex_test.cpp +++ b/frameworks/core/test/unittest/ans_notification_annex_test/ans_notification_annex_test.cpp @@ -301,7 +301,8 @@ HWTEST_F(AnsNotificationUnitAnnexTest, PublishReminder_0300, Function | MediumTe tm dateTime {}; std::vector repeatMonths; std::vector repeatDays; - ReminderRequestCalendar reminder = ReminderRequestCalendar(dateTime, repeatMonths, repeatDays); + std::vector daysOfWeek; + ReminderRequestCalendar reminder = ReminderRequestCalendar(dateTime, repeatMonths, repeatDays, daysOfWeek); ErrCode ret = ans_->PublishReminder(reminder); int errorcode = 201; EXPECT_EQ(ret, errorcode); diff --git a/frameworks/js/napi/include/reminder/reminder_common.h b/frameworks/js/napi/include/reminder/reminder_common.h index a25febc7b..240d82f59 100644 --- a/frameworks/js/napi/include/reminder/reminder_common.h +++ b/frameworks/js/napi/include/reminder/reminder_common.h @@ -31,7 +31,7 @@ const char* ACTION_BUTTON = "actionButton"; const char* ACTION_BUTTON_TITLE = "title"; const char* ACTION_BUTTON_TYPE = "type"; const char* ALARM_HOUR = "hour"; -const char* ALARM_DAYS_OF_WEEK = "daysOfWeek"; +const char* REPEAT_DAYS_OF_WEEK = "daysOfWeek"; const char* ALARM_MINUTE = "minute"; const char* CALENDAR_DATE_TIME = "dateTime"; const char* CALENDAR_YEAR = "year"; diff --git a/frameworks/js/napi/src/reminder/publish.cpp b/frameworks/js/napi/src/reminder/publish.cpp index f75cd24f9..d8cda59f0 100644 --- a/frameworks/js/napi/src/reminder/publish.cpp +++ b/frameworks/js/napi/src/reminder/publish.cpp @@ -425,9 +425,9 @@ void ParseReminderAlarm(const napi_env &env, const ReminderRequest &reminder, na // daysOfWeek napi_create_array(env, &value); - napi_set_named_property(env, result, ALARM_DAYS_OF_WEEK, value); + napi_set_named_property(env, result, REPEAT_DAYS_OF_WEEK, value); int32_t count = 0; - for (auto day : alarm.GetDaysOfWeek()) { + for (auto day : reminder.GetDaysOfWeek()) { if (day) { napi_value napiDay = nullptr; napi_create_int32(env, day, &napiDay); @@ -480,6 +480,19 @@ void ParseReminderCalendar(const napi_env &env, const ReminderRequest &reminder, napi_set_element(env, value, count, napiDay); count++; } + + // daysOfWeek + napi_create_array(env, &value); + napi_set_named_property(env, result, REPEAT_DAYS_OF_WEEK, value); + count = 0; + for (auto day : reminder.GetDaysOfWeek()) { + if (day) { + napi_value napiDay = nullptr; + napi_create_int32(env, day, &napiDay); + napi_set_element(env, value, count, napiDay); + count++; + } + } } void ParseReminder( diff --git a/frameworks/js/napi/src/reminder/reminder_common.cpp b/frameworks/js/napi/src/reminder/reminder_common.cpp index 69f6c869c..d0c645577 100644 --- a/frameworks/js/napi/src/reminder/reminder_common.cpp +++ b/frameworks/js/napi/src/reminder/reminder_common.cpp @@ -482,7 +482,7 @@ napi_value ReminderCommon::CreateReminderAlarm( // daysOfWeek std::vector daysOfWeek; uint8_t maxDaysOfWeek = 7; - if (ParseInt32Array(env, value, ReminderAgentNapi::ALARM_DAYS_OF_WEEK, daysOfWeek, maxDaysOfWeek) == nullptr) { + if (ParseInt32Array(env, value, ReminderAgentNapi::REPEAT_DAYS_OF_WEEK, daysOfWeek, maxDaysOfWeek) == nullptr) { return nullptr; } reminder = std::make_shared( @@ -531,6 +531,13 @@ napi_value ReminderCommon::CreateReminderCalendar( return nullptr; } + // daysOfWeek + std::vector daysOfWeek; + uint8_t maxDaysOfWeek = 7; + if (ParseInt32Array(env, value, ReminderAgentNapi::REPEAT_DAYS_OF_WEEK, daysOfWeek, maxDaysOfWeek) == nullptr) { + return nullptr; + } + tm dateTime; dateTime.tm_year = ReminderRequest::GetCTime(ReminderRequest::TimeTransferType::YEAR, propertyYearVal); dateTime.tm_mon = ReminderRequest::GetCTime(ReminderRequest::TimeTransferType::MONTH, propertyMonthVal); @@ -539,7 +546,7 @@ napi_value ReminderCommon::CreateReminderCalendar( dateTime.tm_min = propertyMinteVal; dateTime.tm_sec = 0; dateTime.tm_isdst = -1; - reminder = std::make_shared(dateTime, repeatMonths, repeatDays); + reminder = std::make_shared(dateTime, repeatMonths, repeatDays, daysOfWeek); if (!(reminder->SetNextTriggerTime())) { return nullptr; } diff --git a/interfaces/inner_api/reminder_request.h b/interfaces/inner_api/reminder_request.h index 3cec37b15..cb1ba52ec 100644 --- a/interfaces/inner_api/reminder_request.h +++ b/interfaces/inner_api/reminder_request.h @@ -633,6 +633,12 @@ public: */ void UpdateNotificationRequest(UpdateNotificationType type, std::string extra); + /** + * @brief Get repeated days of the week. + * + * @return Array of the int type. + */ + std::vector GetDaysOfWeek() const; static int32_t GetActualTime(const TimeTransferType &type, int32_t cTime); static int32_t GetCTime(const TimeTransferType &type, int32_t actualTime); static uint64_t GetDurationSinceEpochInMilli(const time_t target); @@ -648,8 +654,12 @@ public: static const uint16_t MILLI_SECONDS; static const uint16_t SAME_TIME_DISTINGUISH_MILLISECONDS; static const std::string NOTIFICATION_LABEL; - static const int32_t SUNDAY; - + static const uint8_t MONDAY; + static const uint8_t SUNDAY; + static const uint8_t DAYS_PER_WEEK; + static const uint8_t HOURS_PER_DAY; + static const uint16_t SECONDS_PER_HOUR; + static const uint8_t MINUTES_PER_HOUR; /** * @brief Show the reminder with a notification. */ @@ -717,6 +727,7 @@ public: static const std::string MAX_SCREEN_AGENT; static const std::string TAP_DISMISSED; static const std::string AUTO_DELETED_TIME; + static const std::string REPEAT_DAYS_OF_WEEK; static std::string sqlOfAddColumns; static std::vector columns; @@ -746,6 +757,21 @@ protected: */ static void AddColumn(const std::string &name, const std::string &type, const bool &isEnd); + uint8_t repeatDaysOfWeek_{0}; + + /** + * Obtains the next triggerTime if it is a week repeat. + * + * @param now Indicates current time. + * @param now Indicatet target time. + * @return nextTriggerTime. + */ + int64_t GetNextDaysOfWeek(const time_t now, const time_t target) const; + void SetRepeatDaysOfWeek(bool set, std::vector daysOfWeek); + uint8_t GetRepeatDaysOfWeek() const; + time_t GetTriggerTimeWithDST(const time_t now, const time_t nextTriggerTime) const; + uint64_t GetTriggerTime(const time_t now, const time_t nextTriggerTime) const; + private: void AddActionButtons(const bool includeSnooze); void AddRemovalWantAgent(); @@ -768,6 +794,13 @@ private: void UpdateNotificationContent(const bool &setSnooze); void UpdateNotificationCommon(); + /** + * @brief Determine whether it is repeated every week. + * + * @return True if repeate. + */ + bool IsRepeatDaysOfWeek(int32_t day) const; + /** * @brief Used for reminder recovery from database. * diff --git a/interfaces/inner_api/reminder_request_alarm.h b/interfaces/inner_api/reminder_request_alarm.h index e95a5ec2a..4bf99cffc 100644 --- a/interfaces/inner_api/reminder_request_alarm.h +++ b/interfaces/inner_api/reminder_request_alarm.h @@ -57,13 +57,6 @@ public: ReminderRequestAlarm& operator = (const ReminderRequestAlarm &other); ~ReminderRequestAlarm() override {}; - /** - * Obtains the repeat days vector. - * - * @return vector of repeat days. - */ - std::vector GetDaysOfWeek() const; - /** * @brief Obtains the setted {@link hour_}. * @@ -77,7 +70,6 @@ public: * @return setted minute. */ uint8_t GetMinute() const; - uint8_t GetRepeatDay() const; virtual bool OnDateTimeChange() override; virtual bool OnTimeZoneChange() override; virtual bool UpdateNextReminder() override; @@ -130,43 +122,15 @@ private: * @return next trigger time in milli. */ uint64_t GetNextTriggerTime(bool forceToGetNext) const; - - /** - * Judge is it the repeat day setted by user or not. - * - * @param day Indicates the day of week. - * @return true if it is a repeat day. - */ - bool IsRepeatDay(int32_t day) const; bool IsRepeatReminder() const; - void SetDaysOfWeek(bool set, std::vector daysOfWeek); - - /** - * Obtains the next day interval if it is a week repeat alarm. - * - * @param now Indicates current time. - * @param now Indicatet target time. - * @return next day interval. Returns {@link INVALID_INT_VALUE} if it is not a week repeat alarm. - */ - int8_t GetNextAlarm(const time_t now, const time_t target) const; - - time_t GetTriggerTimeWithDST(const time_t now, const time_t nextTriggerTime) const; - static const uint8_t DAYS_PER_WEEK; - static const uint8_t MONDAY; - static const uint8_t SUNDAY; - static const uint8_t HOURS_PER_DAY; - static const uint16_t SECONDS_PER_HOUR; static const uint8_t MINUTES_PER_HOUR; - static const int8_t INVALID_INT_VALUE; static const int8_t DEFAULT_SNOOZE_TIMES; uint8_t hour_ = {0}; uint8_t minute_ = {0}; - uint8_t repeatDays_ = {0}; // For database recovery. - static const std::string REPEAT_DAYS_OF_WEEK; static const std::string ALARM_HOUR; static const std::string ALARM_MINUTE; }; diff --git a/interfaces/inner_api/reminder_request_calendar.h b/interfaces/inner_api/reminder_request_calendar.h index 9204fe339..b519841cd 100644 --- a/interfaces/inner_api/reminder_request_calendar.h +++ b/interfaces/inner_api/reminder_request_calendar.h @@ -49,8 +49,8 @@ public: * the value {2, 4} indicates that the reminder will be triggered on the second * and fourth day of the specific months. */ - ReminderRequestCalendar(const tm &dateTime, - const std::vector &repeatMonths, const std::vector &repeatDays); + ReminderRequestCalendar(const tm &dateTime, const std::vector &repeatMonths, + const std::vector &repeatDays, const std::vector &daysOfWeek); /** * @brief This constructor should only be used in background proxy service process diff --git a/test/fuzztest/reminderrequestalarm_fuzzer/reminderrequestalarm_fuzzer.cpp b/test/fuzztest/reminderrequestalarm_fuzzer/reminderrequestalarm_fuzzer.cpp index 4c4728e92..ab204c921 100644 --- a/test/fuzztest/reminderrequestalarm_fuzzer/reminderrequestalarm_fuzzer.cpp +++ b/test/fuzztest/reminderrequestalarm_fuzzer/reminderrequestalarm_fuzzer.cpp @@ -35,9 +35,9 @@ namespace OHOS { std::vector daysOfWeek; daysOfWeek.push_back(week); auto rrc = std::make_shared(hour, minute, daysOfWeek); - // test SetDaysOfWeek function + // test SetRepeatDaysOfWeek function bool enabled = *data % ENABLE; - rrc->SetDaysOfWeek(enabled, daysOfWeek); + rrc->SetRepeatDaysOfWeek(enabled, daysOfWeek); // test GetDaysOfWeek function rrc->GetDaysOfWeek(); // test CheckParamValid function @@ -48,20 +48,17 @@ namespace OHOS { rrc->PreGetNextTriggerTimeIgnoreSnooze(enabled, enabled); // test GetNextTriggerTime function rrc->GetNextTriggerTime(enabled); - // test GetNextAlarm function + // test GetNextDaysOfWeek function time_t now; (void)time(&now); // unit is seconds. time_t target = *data % ENABLE; - rrc->GetNextAlarm(now, target); - // test IsRepeatDay function - int32_t day = static_cast(GetU32Data(data)); - rrc->IsRepeatDay(day); + rrc->GetNextDaysOfWeek(now, target); // test GetHour function rrc->GetHour(); // test GetMinute function rrc->GetMinute(); - // test GetRepeatDay function - rrc->GetRepeatDay(); + // test GetRepeatDaysOfWeek function + rrc->GetRepeatDaysOfWeek(); // test OnDateTimeChange function rrc->OnDateTimeChange(); // test OnTimeZoneChange function diff --git a/test/fuzztest/reminderrequestcalendar_fuzzer/reminderrequestcalendar_fuzzer.cpp b/test/fuzztest/reminderrequestcalendar_fuzzer/reminderrequestcalendar_fuzzer.cpp index 502679e81..75181ddd5 100644 --- a/test/fuzztest/reminderrequestcalendar_fuzzer/reminderrequestcalendar_fuzzer.cpp +++ b/test/fuzztest/reminderrequestcalendar_fuzzer/reminderrequestcalendar_fuzzer.cpp @@ -29,17 +29,21 @@ namespace OHOS { constexpr uint16_t MINUTES = 60; constexpr uint16_t SECONDS = 60; constexpr uint8_t ENABLE = 2; + constexpr uint8_t WEEK = 7; } bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) { struct tm nowTime; uint8_t months = *data % MONTHS; uint8_t days = *data % DAYS; + uint8_t weeks = *data % WEEK; std::vector repeatMonths; std::vector repeatDays; + std::vector daysOfWeek; + daysOfWeek.push_back(weeks); repeatMonths.push_back(months); repeatDays.push_back(days); - Notification::ReminderRequestCalendar reminderRequestCalendar(nowTime, repeatMonths, repeatDays); + Notification::ReminderRequestCalendar reminderRequestCalendar(nowTime, repeatMonths, repeatDays, daysOfWeek); // test SetNextTriggerTime function reminderRequestCalendar.SetNextTriggerTime(); // test InitDateTime function @@ -51,6 +55,8 @@ namespace OHOS { reminderRequestCalendar.SetDay(days, enabled); // test SetMonth function reminderRequestCalendar.SetMonth(months, enabled); + // test SetRepeatDaysOfWeek function + reminderRequestCalendar.SetRepeatDaysOfWeek(enabled, daysOfWeek); // test SetRepeatMonths function reminderRequestCalendar.SetRepeatMonths(repeatMonths); // test SetRepeatDaysOfMonth function -- Gitee From db993a85919e8d7d27b94b4aad251e23604264ee Mon Sep 17 00:00:00 2001 From: zhengweina Date: Wed, 25 Oct 2023 22:27:27 +0800 Subject: [PATCH 2/5] calendar add weekly reminder Signed-off-by: zhengweina Signed-off-by: zhengweina --- frameworks/ans/src/reminder_request.cpp | 7 ++++--- frameworks/ans/src/reminder_request_calendar.cpp | 8 ++++++-- frameworks/ans/src/reminder_store.cpp | 9 ++++++++- interfaces/inner_api/reminder_request.h | 2 +- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/frameworks/ans/src/reminder_request.cpp b/frameworks/ans/src/reminder_request.cpp index 49e20a810..059b6305b 100644 --- a/frameworks/ans/src/reminder_request.cpp +++ b/frameworks/ans/src/reminder_request.cpp @@ -542,8 +542,9 @@ void ReminderRequest::RecoverFromDb(const std::shared_ptr // state state_ = static_cast(RecoverInt64FromDb(resultSet, STATE, DbRecoveryType::INT)); - //repeatDaysOfWeek_ + // repeatDaysOfWeek_ repeatDaysOfWeek_ = static_cast(RecoverInt64FromDb(resultSet, REPEAT_DAYS_OF_WEEK, DbRecoveryType::INT)); + // action buttons RecoverActionButton(resultSet); @@ -1900,7 +1901,7 @@ uint8_t ReminderRequest::GetRepeatDaysOfWeek() const return repeatDaysOfWeek_; } -void ReminderRequest::SetRepeatDaysOfWeek(bool set, std::vector daysOfWeek) +void ReminderRequest::SetRepeatDaysOfWeek(bool set, const std::vector &daysOfWeek) { if (daysOfWeek.size() == 0) { return; @@ -1909,7 +1910,7 @@ void ReminderRequest::SetRepeatDaysOfWeek(bool set, std::vector daysOfW ANSR_LOGE("The length of daysOfWeek should not larger than 7"); return; } - for (std::vector::iterator it = daysOfWeek.begin(); it != daysOfWeek.end(); ++it) { + for (auto it = daysOfWeek.begin(); it != daysOfWeek.end(); ++it) { if (*it < MONDAY || *it > SUNDAY) { continue; } diff --git a/frameworks/ans/src/reminder_request_calendar.cpp b/frameworks/ans/src/reminder_request_calendar.cpp index ea37d77f8..9b725967d 100644 --- a/frameworks/ans/src/reminder_request_calendar.cpp +++ b/frameworks/ans/src/reminder_request_calendar.cpp @@ -169,8 +169,12 @@ uint64_t ReminderRequestCalendar::GetNextTriggerTime() const ANSR_LOGD("Now time is: %{public}s", GetDateTimeInfo(now).c_str()); if (repeatMonth_ > 0 && repeatDay_ > 0) { triggerTimeInMilli = GetNextTriggerTimeAsRepeatReminder(nowTime, tarTime); - } else if (repeatDaysOfWeek_ > 0 && (target < now)) { - const time_t tar = mktime(&tarTime); + } else if (repeatDaysOfWeek_ > 0 && (target <= now)) { + nowTime.tm_hour = tarTime.tm_hour; + nowTime.tm_min = tarTime.tm_min; + nowTime.tm_sec = tarTime.tm_sec; + nowTime.tm_isdst = tarTime.tm_isdst; + const time_t tar = mktime(&nowTime); triggerTimeInMilli = GetNextDaysOfWeek(now, tar); } else { ANSR_LOGD("tarTime: %{public}d-%{public}d-%{public}d %{public}d:%{public}d:%{public}d", diff --git a/frameworks/ans/src/reminder_store.cpp b/frameworks/ans/src/reminder_store.cpp index 74da38d7b..a3ecfe724 100644 --- a/frameworks/ans/src/reminder_store.cpp +++ b/frameworks/ans/src/reminder_store.cpp @@ -35,6 +35,7 @@ const std::string REMINDER_DB_TABLE = "reminder"; const uint32_t REMINDER_RDB_VERSION = 1; const int32_t STATE_FAIL = -1; std::vector columns; +std::string sqlColumns; } const int32_t ReminderStore::STATE_OK = 0; @@ -72,6 +73,11 @@ int32_t ReminderStore::Init() ReminderRequestAlarm::InitDbColumns(); columns.insert(columns.begin(), ReminderRequest::columns.begin(), ReminderRequest::columns.end()); + for (std::vector::const_iterator it = columns.begin(); it != columns.end(); ++it) { + sqlColumns += *it + ","; + } + sqlColumns = sqlColumns.substr(0, sqlColumns.size() - 1); + ANSR_LOGD("ReminderStore sqlColumns =%{public}s", sqlColumns.c_str()); std::string dbConfig = REMINDER_DB_DIR + REMINDER_DB_NAME; NativeRdb::RdbStoreConfig config_(dbConfig); @@ -310,10 +316,11 @@ int32_t ReminderStore::GetMaxId() std::vector> ReminderStore::GetAllValidReminders() { - std::string queryCondition = "select * from " + REMINDER_DB_TABLE + " where " + std::string queryCondition = "select " + sqlColumns + " from " + REMINDER_DB_TABLE + " where " + ReminderRequest::IS_EXPIRED + " is false order by " + ReminderRequest::TRIGGER_TIME + " asc"; ANSR_LOGD("Get all reminders"); + ANSR_LOGD("ReminderStore GetAllValidReminders queryCondition =%{public}s", queryCondition.c_str()); return GetReminders(queryCondition); } diff --git a/interfaces/inner_api/reminder_request.h b/interfaces/inner_api/reminder_request.h index cb1ba52ec..0f7d09fa9 100644 --- a/interfaces/inner_api/reminder_request.h +++ b/interfaces/inner_api/reminder_request.h @@ -767,7 +767,7 @@ protected: * @return nextTriggerTime. */ int64_t GetNextDaysOfWeek(const time_t now, const time_t target) const; - void SetRepeatDaysOfWeek(bool set, std::vector daysOfWeek); + void SetRepeatDaysOfWeek(bool set, const std::vector &daysOfWeek); uint8_t GetRepeatDaysOfWeek() const; time_t GetTriggerTimeWithDST(const time_t now, const time_t nextTriggerTime) const; uint64_t GetTriggerTime(const time_t now, const time_t nextTriggerTime) const; -- Gitee From c14018af8e6c166a6eb09e8524b4c7b952e565c9 Mon Sep 17 00:00:00 2001 From: zhengweina Date: Sat, 28 Oct 2023 07:00:19 +0000 Subject: [PATCH 3/5] update frameworks/ans/src/reminder_request.cpp. Signed-off-by: zhengweina --- frameworks/ans/src/reminder_request.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/frameworks/ans/src/reminder_request.cpp b/frameworks/ans/src/reminder_request.cpp index ad3e49bff..486994979 100644 --- a/frameworks/ans/src/reminder_request.cpp +++ b/frameworks/ans/src/reminder_request.cpp @@ -2043,6 +2043,7 @@ uint64_t ReminderRequest::GetTriggerTime(const time_t now, const time_t nextTrig return 0; } return GetDurationSinceEpochInMilli(triggerTime); +} void ReminderRequest::OnLanguageChange(const std::shared_ptr &resMgr) { -- Gitee From 8ff5531731f270ec9df57f4ba7687a7602f6b1fe Mon Sep 17 00:00:00 2001 From: zhengweina Date: Sat, 28 Oct 2023 07:15:23 +0000 Subject: [PATCH 4/5] update frameworks/ans/src/reminder_store.cpp. Signed-off-by: zhengweina --- frameworks/ans/src/reminder_store.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/frameworks/ans/src/reminder_store.cpp b/frameworks/ans/src/reminder_store.cpp index a3ecfe724..54f91635e 100644 --- a/frameworks/ans/src/reminder_store.cpp +++ b/frameworks/ans/src/reminder_store.cpp @@ -35,7 +35,7 @@ const std::string REMINDER_DB_TABLE = "reminder"; const uint32_t REMINDER_RDB_VERSION = 1; const int32_t STATE_FAIL = -1; std::vector columns; -std::string sqlColumns; +std::string g_sqlColumns; } const int32_t ReminderStore::STATE_OK = 0; @@ -74,10 +74,10 @@ int32_t ReminderStore::Init() columns.insert(columns.begin(), ReminderRequest::columns.begin(), ReminderRequest::columns.end()); for (std::vector::const_iterator it = columns.begin(); it != columns.end(); ++it) { - sqlColumns += *it + ","; + g_sqlColumns += *it + ","; } - sqlColumns = sqlColumns.substr(0, sqlColumns.size() - 1); - ANSR_LOGD("ReminderStore sqlColumns =%{public}s", sqlColumns.c_str()); + g_sqlColumns = g_sqlColumns.substr(0, g_sqlColumns.size() - 1); + ANSR_LOGD("ReminderStore g_sqlColumns =%{public}s", g_sqlColumns.c_str()); std::string dbConfig = REMINDER_DB_DIR + REMINDER_DB_NAME; NativeRdb::RdbStoreConfig config_(dbConfig); @@ -316,7 +316,7 @@ int32_t ReminderStore::GetMaxId() std::vector> ReminderStore::GetAllValidReminders() { - std::string queryCondition = "select " + sqlColumns + " from " + REMINDER_DB_TABLE + " where " + std::string queryCondition = "select " + g_sqlColumns + " from " + REMINDER_DB_TABLE + " where " + ReminderRequest::IS_EXPIRED + " is false order by " + ReminderRequest::TRIGGER_TIME + " asc"; ANSR_LOGD("Get all reminders"); -- Gitee From e59f4457852802de732f6e0a7fd642e2fcd5adb3 Mon Sep 17 00:00:00 2001 From: zhengweina Date: Sat, 28 Oct 2023 07:17:29 +0000 Subject: [PATCH 5/5] update frameworks/ans/src/reminder_request_alarm.cpp. Signed-off-by: zhengweina --- frameworks/ans/src/reminder_request_alarm.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frameworks/ans/src/reminder_request_alarm.cpp b/frameworks/ans/src/reminder_request_alarm.cpp index baba77d24..78d7c11fc 100644 --- a/frameworks/ans/src/reminder_request_alarm.cpp +++ b/frameworks/ans/src/reminder_request_alarm.cpp @@ -42,7 +42,8 @@ ReminderRequestAlarm::ReminderRequestAlarm(const ReminderRequestAlarm &other) : { this->hour_ = other.hour_; this->minute_ = other.minute_; - ANSR_LOGD("hour_=%{public}d, minute_=%{public}d, repeatDaysOfWeek_=%{public}d", hour_, minute_, other.repeatDaysOfWeek_); + ANSR_LOGD("hour_=%{public}d, minute_=%{public}d, repeatDaysOfWeek_=%{public}d", + hour_, minute_, other.repeatDaysOfWeek_); } void ReminderRequestAlarm::CheckParamValid() const -- Gitee