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_timer.h"
17
18 #include <chrono>
19 #include <cstdlib>
20
21 #include "ans_log_wrapper.h"
22 #include "time_service_client.h"
23
24 namespace OHOS {
25 namespace Notification {
ReminderRequestTimer(uint64_t countDownTimeInSeconds)26 ReminderRequestTimer::ReminderRequestTimer(uint64_t countDownTimeInSeconds)
27 : ReminderRequest(ReminderRequest::ReminderType::TIMER)
28 {
29 CheckParamsValid(countDownTimeInSeconds);
30 countDownTimeInSeconds_ = countDownTimeInSeconds;
31 time_t now; // unit is seconds.
32 (void)time(&now);
33 ReminderRequest::SetTriggerTimeInMilli(
34 ReminderRequest::GetDurationSinceEpochInMilli(now) + countDownTimeInSeconds_ * ReminderRequest::MILLI_SECONDS);
35 sptr<MiscServices::TimeServiceClient> timer = MiscServices::TimeServiceClient::GetInstance();
36 int64_t bootTimeMs = timer->GetBootTimeMs();
37 if (bootTimeMs >= 0) {
38 firstRealTimeInMilliSeconds_ = static_cast<uint64_t>(bootTimeMs);
39 } else {
40 ANSR_LOGW("Get boot time error.");
41 }
42 }
43
ReminderRequestTimer(const ReminderRequestTimer & other)44 ReminderRequestTimer::ReminderRequestTimer(const ReminderRequestTimer &other) : ReminderRequest(other)
45 {
46 firstRealTimeInMilliSeconds_ = other.firstRealTimeInMilliSeconds_;
47 countDownTimeInSeconds_ = other.countDownTimeInSeconds_;
48 }
49
GetInitInfo() const50 uint64_t ReminderRequestTimer::GetInitInfo() const
51 {
52 return countDownTimeInSeconds_;
53 }
54
PreGetNextTriggerTimeIgnoreSnooze(bool ignoreRepeat,bool forceToGetNext) const55 uint64_t ReminderRequestTimer::PreGetNextTriggerTimeIgnoreSnooze(bool ignoreRepeat, bool forceToGetNext) const
56 {
57 ANSR_LOGD("countdown time not support PreGetNextTriggerTimeIgnoreSnooze");
58 return ReminderRequest::INVALID_LONG_LONG_VALUE;
59 }
60
OnDateTimeChange()61 bool ReminderRequestTimer::OnDateTimeChange()
62 {
63 UpdateTimeInfo("onDateTimeChange");
64 return false;
65 }
66
OnTimeZoneChange()67 bool ReminderRequestTimer::OnTimeZoneChange()
68 {
69 UpdateTimeInfo("onTimeZoneChange");
70 return false;
71 }
72
UpdateNextReminder()73 bool ReminderRequestTimer::UpdateNextReminder()
74 {
75 ANSR_LOGD("countdown time not support repeat reminder, no need to update next triggerTime");
76 SetExpired(true);
77 return false;
78 }
79
CheckParamsValid(const uint64_t countDownTimeInSeconds) const80 void ReminderRequestTimer::CheckParamsValid(const uint64_t countDownTimeInSeconds) const
81 {
82 if (countDownTimeInSeconds == 0 || countDownTimeInSeconds >= (UINT64_MAX / ReminderRequest::MILLI_SECONDS)) {
83 ANSR_LOGE("Illegal count down time, please check the description of the constructor");
84 throw std::invalid_argument("Illegal count down time, please check the description of the constructor");
85 }
86 }
87
UpdateTimeInfo(const std::string & description)88 void ReminderRequestTimer::UpdateTimeInfo(const std::string &description)
89 {
90 if (IsExpired()) {
91 return;
92 }
93 ANSR_LOGD("%{public}s, update countdown time trigger time", description.c_str());
94 time_t now;
95 (void)time(&now); // unit is seconds.
96 whenToChangeSysTime_ = ReminderRequest::GetDurationSinceEpochInMilli(now);
97 sptr<MiscServices::TimeServiceClient> timer = MiscServices::TimeServiceClient::GetInstance();
98 int64_t bootTime = timer->GetBootTimeMs();
99 if (bootTime < 0) {
100 ANSR_LOGW("BootTime is illegal");
101 return;
102 }
103 SetTriggerTimeInMilli(whenToChangeSysTime_ + (countDownTimeInSeconds_ * MILLI_SECONDS -
104 (static_cast<uint64_t>(bootTime) - firstRealTimeInMilliSeconds_)));
105 }
106
Marshalling(Parcel & parcel) const107 bool ReminderRequestTimer::Marshalling(Parcel &parcel) const
108 {
109 ReminderRequest::Marshalling(parcel);
110
111 // write int
112 if (!parcel.WriteUint64(firstRealTimeInMilliSeconds_)) {
113 ANSR_LOGE("Failed to write firstRealTimeInMilliSeconds");
114 return false;
115 }
116 if (!parcel.WriteUint64(countDownTimeInSeconds_)) {
117 ANSR_LOGE("Failed to write countDownTimeInSeconds");
118 return false;
119 }
120 return true;
121 }
122
Unmarshalling(Parcel & parcel)123 ReminderRequestTimer *ReminderRequestTimer::Unmarshalling(Parcel &parcel)
124 {
125 auto objptr = new (std::nothrow) ReminderRequestTimer();
126 if (objptr == nullptr) {
127 ANSR_LOGE("Failed to create reminder timer due to no memory.");
128 return objptr;
129 }
130 if (!objptr->ReadFromParcel(parcel)) {
131 delete objptr;
132 objptr = nullptr;
133 }
134 return objptr;
135 }
136
ReadFromParcel(Parcel & parcel)137 bool ReminderRequestTimer::ReadFromParcel(Parcel &parcel)
138 {
139 ReminderRequest::ReadFromParcel(parcel);
140
141 // read int
142 if (!parcel.ReadUint64(firstRealTimeInMilliSeconds_)) {
143 ANSR_LOGE("Failed to read firstRealTimeInMilliSeconds");
144 return false;
145 }
146 if (!parcel.ReadUint64(countDownTimeInSeconds_)) {
147 ANSR_LOGE("Failed to read countDownTimeInSeconds");
148 return false;
149 }
150 return true;
151 }
152 }
153 }