diff --git a/frameworks/ans/native/src/reminder_helper.cpp b/frameworks/ans/native/src/reminder_helper.cpp index 3197916aec4e4f7c1608785cb4759518bdf28f8e..39fe3105db9e4289f0e28c590c0e1fc8497bd39b 100644 --- a/frameworks/ans/native/src/reminder_helper.cpp +++ b/frameworks/ans/native/src/reminder_helper.cpp @@ -29,7 +29,6 @@ namespace Notification { ErrCode ReminderHelper::PublishReminder(ReminderRequest &reminder) { ANSR_LOGI("PublishReminder start"); - NotificationSlot slot(reminder.GetSlotType()); NotificationHelper::AddNotificationSlot(slot); return DelayedSingleton::GetInstance()->PublishReminder(reminder); diff --git a/frameworks/ans/native/src/reminder_request.cpp b/frameworks/ans/native/src/reminder_request.cpp index d523b9a31f337d1f9ca61ffba7696cab9669ffcc..06dc80e8652330cd5f6ce93a5598604ad9a598c3 100644 --- a/frameworks/ans/native/src/reminder_request.cpp +++ b/frameworks/ans/native/src/reminder_request.cpp @@ -261,11 +261,11 @@ bool ReminderRequest::HandleTimeZoneChange( ANSR_LOGE("Get now time error"); return false; } - if (newZoneTriggerTime <= (static_cast(now))) { + if (newZoneTriggerTime <= GetDurationSinceEpochInMilli(now)) { snoozeTimesDynamic_ = 0; showImmediately = true; } else { - SetTriggerTimeInMilli(newZoneTriggerTime * MILLI_SECONDS); + SetTriggerTimeInMilli(newZoneTriggerTime); showImmediately = false; } } @@ -367,7 +367,8 @@ bool ReminderRequest::OnTimeZoneChange() struct tm *oriTime = gmtime(&oldZoneTriggerTime); time_t newZoneTriggerTime = mktime(oriTime); uint64_t nextTriggerTime = PreGetNextTriggerTimeIgnoreSnooze(true, false); - return HandleTimeZoneChange(oldZoneTriggerTime, newZoneTriggerTime, nextTriggerTime); + return HandleTimeZoneChange( + triggerTimeInMilli_, GetDurationSinceEpochInMilli(newZoneTriggerTime), nextTriggerTime); } ReminderRequest& ReminderRequest::SetMaxScreenWantAgentInfo( @@ -849,16 +850,23 @@ bool ReminderRequest::ReadFromParcel(Parcel &parcel) info.title = title; actionButtonMap_.insert(std::pair(type, info)); } - InitNotificationRequest(); + if (!InitNotificationRequest()) { + return false; + } return true; } -void ReminderRequest::InitNotificationRequest() +bool ReminderRequest::InitNotificationRequest() { ANSR_LOGI("Init notification"); - notificationRequest_ = new NotificationRequest(notificationId_); + notificationRequest_ = new (std::nothrow) NotificationRequest(notificationId_); + if (notificationRequest_ == nullptr) { + ANSR_LOGE("Failed to create notification."); + return false; + } displayContent_ = content_; AddActionButtons(true); + return true; } bool ReminderRequest::IsAlerting() const @@ -866,6 +874,18 @@ bool ReminderRequest::IsAlerting() const return (state_ & REMINDER_STATUS_ALERTING) != 0; } +uint64_t ReminderRequest::GetDurationSinceEpochInMilli(const time_t target) +{ + auto tarEndTimePoint = std::chrono::system_clock::from_time_t(target); + auto tarDuration = std::chrono::duration_cast(tarEndTimePoint.time_since_epoch()); + int64_t tarDate = tarDuration.count(); + if (tarDate < 0) { + ANSR_LOGW("tarDuration is less than 0."); + return INVALID_LONG_LONG_VALUE; + } + return static_cast(tarDate); +} + std::string ReminderRequest::GetDateTimeInfo(const time_t &timeInSecond) const { return GetTimeInfoInner(timeInSecond, TimeFormat::YMDHMS); @@ -879,7 +899,7 @@ uint64_t ReminderRequest::GetNowInstantMilli() const ANSR_LOGE("Get now time error"); return 0; } - return static_cast(now) * MILLI_SECONDS; + return GetDurationSinceEpochInMilli(now); } std::string ReminderRequest::GetShowTime(const uint64_t showTime) const diff --git a/frameworks/ans/native/src/reminder_request_alarm.cpp b/frameworks/ans/native/src/reminder_request_alarm.cpp index 2c0de6b23f3ab63f6925b6b1916eac72e34f86a4..67ee01eae407a0e6521d66b4acba8c8bea2ede85 100644 --- a/frameworks/ans/native/src/reminder_request_alarm.cpp +++ b/frameworks/ans/native/src/reminder_request_alarm.cpp @@ -122,6 +122,7 @@ uint64_t ReminderRequestAlarm::GetNextTriggerTime(bool forceToGetNext) const tar.tm_hour = hour_; tar.tm_min = minute_; tar.tm_sec = 0; + tar.tm_isdst = -1; const time_t target = mktime(&tar); int8_t nextDayInterval = GetNextAlarm(now, target); @@ -151,7 +152,7 @@ uint64_t ReminderRequestAlarm::GetNextTriggerTime(bool forceToGetNext) const if (static_cast(nextTriggerTime) <= 0) { return 0; } - return static_cast(nextTriggerTime) * ReminderRequest::MILLI_SECONDS; + return ReminderRequest::GetDurationSinceEpochInMilli(nextTriggerTime); } int8_t ReminderRequestAlarm::GetNextAlarm(const time_t now, const time_t target) const @@ -273,7 +274,7 @@ bool ReminderRequestAlarm::Marshalling(Parcel &parcel) const ReminderRequestAlarm *ReminderRequestAlarm::Unmarshalling(Parcel &parcel) { ANSR_LOGD("New alarm"); - auto objptr = new ReminderRequestAlarm(); + auto objptr = new (std::nothrow) ReminderRequestAlarm(); if ((objptr != nullptr) && !objptr->ReadFromParcel(parcel)) { delete objptr; objptr = nullptr; diff --git a/frameworks/ans/native/src/reminder_request_calendar.cpp b/frameworks/ans/native/src/reminder_request_calendar.cpp index 87751491934383e7148ec654cc3738010f53243d..b74a4a4d60d9dbab2841cacab840982994ff16dc 100644 --- a/frameworks/ans/native/src/reminder_request_calendar.cpp +++ b/frameworks/ans/native/src/reminder_request_calendar.cpp @@ -113,6 +113,7 @@ uint8_t ReminderRequestCalendar::GetNextDay( setTime.tm_hour = target.tm_hour; setTime.tm_min = target.tm_min; setTime.tm_sec = target.tm_sec; + setTime.tm_isdst = -1; struct tm nowTime; nowTime.tm_year = now.tm_year; @@ -121,6 +122,8 @@ uint8_t ReminderRequestCalendar::GetNextDay( nowTime.tm_hour = now.tm_hour; nowTime.tm_min = now.tm_min; nowTime.tm_sec = now.tm_sec; + nowTime.tm_isdst = -1; + if (mktime(&nowTime) >= mktime(&setTime)) { continue; } else { @@ -151,12 +154,18 @@ uint64_t ReminderRequestCalendar::GetNextTriggerTime() const tarTime.tm_hour = hour_; tarTime.tm_min = minute_; tarTime.tm_sec = 0; + tarTime.tm_isdst = -1; ANSR_LOGD("Now time is: %{public}s", GetDateTimeInfo(now).c_str()); if (!(repeatMonth_ > 0 && repeatDay_ > 0)) { + 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 (now <= target) { - triggerTimeInMilli = static_cast(target) * MILLI_SECONDS; + if (target == -1) { + ANSR_LOGW("mktime return error."); + } + if (now < target) { + triggerTimeInMilli = ReminderRequest::GetDurationSinceEpochInMilli(target); ANSR_LOGD("Next calendar time:%{public}s", GetDateTimeInfo(target).c_str()); } return triggerTimeInMilli; @@ -206,19 +215,16 @@ uint64_t ReminderRequestCalendar::GetTimeInstantMilli( tar.tm_hour = hour; tar.tm_min = minute; tar.tm_sec = second; + tar.tm_isdst = -1; + + ANSR_LOGD("tar: %{public}d-%{public}d-%{public}d %{public}d:%{public}d:%{public}d", + tar.tm_year, tar.tm_mon, tar.tm_mday, tar.tm_hour, tar.tm_min, tar.tm_sec); const time_t target = mktime(&tar); if (target == -1) { ANSR_LOGW("mktime return error."); return INVALID_LONG_LONG_VALUE; } - auto tarEndTimePoint = std::chrono::system_clock::from_time_t(target); - auto tarDuration = std::chrono::duration_cast(tarEndTimePoint.time_since_epoch()); - int64_t tarDate = tarDuration.count(); - if (tarDate < 0) { - ANSR_LOGW("tarDuration is less than 0."); - return INVALID_LONG_LONG_VALUE; - } - return static_cast(tarDate); + return ReminderRequest::GetDurationSinceEpochInMilli(target); } void ReminderRequestCalendar::InitDateTime() @@ -229,6 +235,7 @@ void ReminderRequestCalendar::InitDateTime() dateTime_.tm_hour = hour_; dateTime_.tm_min = minute_; dateTime_.tm_sec = second_; + dateTime_.tm_isdst = -1; } void ReminderRequestCalendar::InitDateTime(const tm &dateTime) @@ -239,6 +246,7 @@ void ReminderRequestCalendar::InitDateTime(const tm &dateTime) dateTime_.tm_hour = dateTime.tm_hour; dateTime_.tm_min = dateTime.tm_min; dateTime_.tm_sec = dateTime.tm_sec; + dateTime_.tm_isdst = -1; } bool ReminderRequestCalendar::IsRepeatReminder() const diff --git a/frameworks/ans/native/src/reminder_request_timer.cpp b/frameworks/ans/native/src/reminder_request_timer.cpp index 4fc99ed1697bd1c8997ae1e76f725ae963eaac35..d0fd4337b2432cc0974cfcb9d5103ad071705014 100644 --- a/frameworks/ans/native/src/reminder_request_timer.cpp +++ b/frameworks/ans/native/src/reminder_request_timer.cpp @@ -31,7 +31,7 @@ ReminderRequestTimer::ReminderRequestTimer(uint64_t countDownTimeInSeconds) time_t now; // unit is seconds. (void)time(&now); ReminderRequest::SetTriggerTimeInMilli( - (static_cast(now) + countDownTimeInSeconds_) * ReminderRequest::MILLI_SECONDS); + ReminderRequest::GetDurationSinceEpochInMilli(now) + countDownTimeInSeconds_ * ReminderRequest::MILLI_SECONDS); sptr timer = MiscServices::TimeServiceClient::GetInstance(); firstRealTimeInMilliSeconds_ = timer->GetBootTimeMs(); } @@ -88,7 +88,7 @@ void ReminderRequestTimer::UpdateTimeInfo(const std::string description) ANSR_LOGD("%{public}s, update countdown time trigger time", description.c_str()); time_t now; (void)time(&now); // unit is seconds. - whenToChangeSysTime_ = static_cast(now) * MILLI_SECONDS; + whenToChangeSysTime_ = ReminderRequest::GetDurationSinceEpochInMilli(now); sptr timer = MiscServices::TimeServiceClient::GetInstance(); int64_t bootTime = timer->GetBootTimeMs(); SetTriggerTimeInMilli(whenToChangeSysTime_ + (countDownTimeInSeconds_ * MILLI_SECONDS - diff --git a/interfaces/innerkits/ans/native/include/reminder_request.h b/interfaces/innerkits/ans/native/include/reminder_request.h index 8e320cda6eb4b3e877d199d79e0dd13b8575654b..14fb032d6ff2f0af0445dfee2cc70254c92418b0 100644 --- a/interfaces/innerkits/ans/native/include/reminder_request.h +++ b/interfaces/innerkits/ans/native/include/reminder_request.h @@ -560,6 +560,7 @@ public: static const std::string PARAM_REMINDER_ID; static int GetActualTime(const TimeTransferType &type, int cTime); static int GetCTime(const TimeTransferType &type, int actualTime); + static uint64_t GetDurationSinceEpochInMilli(const time_t target); static int32_t GetUid(const int &userId, const std::string &bundleName); static int GetUserId(const int &uid); @@ -583,7 +584,7 @@ private: std::string GetState(const uint8_t state) const; bool HandleSysTimeChange(uint64_t oriTriggerTime, uint64_t optTriggerTime); bool HandleTimeZoneChange(uint64_t oldZoneTriggerTime, uint64_t newZoneTriggerTime, uint64_t optTriggerTime); - void InitNotificationRequest(); + bool InitNotificationRequest(); void SetMaxScreenWantAgent(AppExecFwk::ElementName &element); void SetState(bool deSet, const uint8_t newState, std::string function); void SetWantAgent(AppExecFwk::ElementName &element); diff --git a/interfaces/kits/napi/ans/src/reminder/reminder_common.cpp b/interfaces/kits/napi/ans/src/reminder/reminder_common.cpp index 8c7d091ba166e7ef7d2abc6e7a4ef8b431b7bdd2..83ba3aba1535b91a2254f5b4fe2d09696e2fbf89 100644 --- a/interfaces/kits/napi/ans/src/reminder/reminder_common.cpp +++ b/interfaces/kits/napi/ans/src/reminder/reminder_common.cpp @@ -446,6 +446,7 @@ napi_value ReminderCommon::CreateReminderCalendar( dateTime.tm_hour = propertyHourVal; dateTime.tm_min = propertyMinteVal; dateTime.tm_sec = 0; + dateTime.tm_isdst = -1; reminder = std::make_shared(dateTime, repeatMonths, repeatDays); return NotificationNapi::Common::NapiGetNull(env); } diff --git a/services/ans/include/reminder_data_manager.h b/services/ans/include/reminder_data_manager.h index 889058add1aa198dd02ee0ff0efa6561461d9495..b1d22285a35a000f2fea65ea06991d547efeb1d0 100644 --- a/services/ans/include/reminder_data_manager.h +++ b/services/ans/include/reminder_data_manager.h @@ -391,6 +391,7 @@ private: static std::mutex MUTEX; static std::mutex SHOW_MUTEX; static std::mutex ALERT_MUTEX; + static std::mutex TIMER_MUTEX; /** * Max number of reminders limit for the whole system. diff --git a/services/ans/src/reminder_data_manager.cpp b/services/ans/src/reminder_data_manager.cpp index 1fb4bd01d505e29e3876fbf02ee733622fc203e6..52adfccde3323e4a36e343f52e2db06b76e26392 100644 --- a/services/ans/src/reminder_data_manager.cpp +++ b/services/ans/src/reminder_data_manager.cpp @@ -33,6 +33,7 @@ std::shared_ptr ReminderDataManager::REMINDER_DATA_MANAGER std::mutex ReminderDataManager::MUTEX; std::mutex ReminderDataManager::SHOW_MUTEX; std::mutex ReminderDataManager::ALERT_MUTEX; +std::mutex ReminderDataManager::TIMER_MUTEX; void ReminderDataManager::PublishReminder(sptr &reminder, sptr &bundleOption) @@ -322,6 +323,7 @@ void ReminderDataManager::CloseReminder(const OHOS::EventFwk::Want &want, bool c return; } CloseReminder(reminder, cancelNotification); + StartRecentReminder(); } void ReminderDataManager::CloseReminder(const sptr &reminder, bool cancelNotification) @@ -341,7 +343,6 @@ void ReminderDataManager::CloseReminder(const sptr &reminder, b if (cancelNotification) { CancelNotification(reminder); } - StartRecentReminder(); } std::shared_ptr ReminderDataManager::GetInstance() @@ -703,7 +704,7 @@ sptr ReminderDataManager::GetRecentReminderLocked() time_t now; (void)time(&now); // unit is seconds. if (now < 0 - || static_cast(now) * ReminderRequest::MILLI_SECONDS > (*it)->GetTriggerTimeInMilli()) { + || ReminderRequest::GetDurationSinceEpochInMilli(now) > (*it)->GetTriggerTimeInMilli()) { ANSR_LOGE("Get recent reminder while the trigger time is overdue."); it++; continue; @@ -950,7 +951,7 @@ void ReminderDataManager::RemoveReminderLocked(const int32_t &reminderId) void ReminderDataManager::StartTimerLocked(const sptr &reminderRequest, TimerType type) { - std::lock_guard lock(ReminderDataManager::MUTEX); + std::lock_guard lock(ReminderDataManager::TIMER_MUTEX); StartTimer(reminderRequest, type); } @@ -982,7 +983,7 @@ void ReminderDataManager::StartTimer(const sptr &reminderReques ANSR_LOGE("Alerting time out timer has already started."); break; } - triggerTime = static_cast(now) * ReminderRequest::MILLI_SECONDS + triggerTime = ReminderRequest::GetDurationSinceEpochInMilli(now) + static_cast(reminderRequest->GetRingDuration() * ReminderRequest::MILLI_SECONDS); timerIdAlerting_ = timer->CreateTimer(REMINDER_DATA_MANAGER->CreateTimerInfo(type)); timer->StartTimer(timerIdAlerting_, triggerTime); @@ -999,14 +1000,14 @@ void ReminderDataManager::StartTimer(const sptr &reminderReques ANSR_LOGW("Start timer fail"); } else { ANSR_LOGD("Timing info: now:(%{public}llu), tar:(%{public}llu)", - (unsigned long long)(static_cast(now) * ReminderRequest::MILLI_SECONDS), + (unsigned long long)(ReminderRequest::GetDurationSinceEpochInMilli(now)), (unsigned long long)(triggerTime)); } } void ReminderDataManager::StopTimerLocked(TimerType type) { - std::lock_guard lock(ReminderDataManager::MUTEX); + std::lock_guard lock(ReminderDataManager::TIMER_MUTEX); StopTimer(type); }