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 if (timer == nullptr) {
37 ANSR_LOGW("Failed to get boot time due to TimeServiceClient is null.");
38 } else {
39 int64_t bootTimeMs = timer->GetBootTimeMs();
40 if (bootTimeMs >= 0) {
41 firstRealTimeInMilliSeconds_ = static_cast<uint64_t>(bootTimeMs);
42 } else {
43 ANSR_LOGW("Get boot time error.");
44 }
45 }
46 }
47
ReminderRequestTimer(const ReminderRequestTimer & other)48 ReminderRequestTimer::ReminderRequestTimer(const ReminderRequestTimer &other) : ReminderRequest(other)
49 {
50 firstRealTimeInMilliSeconds_ = other.firstRealTimeInMilliSeconds_;
51 countDownTimeInSeconds_ = other.countDownTimeInSeconds_;
52 }
53
GetInitInfo() const54 uint64_t ReminderRequestTimer::GetInitInfo() const
55 {
56 return countDownTimeInSeconds_;
57 }
58
PreGetNextTriggerTimeIgnoreSnooze(bool ignoreRepeat,bool forceToGetNext) const59 uint64_t ReminderRequestTimer::PreGetNextTriggerTimeIgnoreSnooze(bool ignoreRepeat, bool forceToGetNext) const
60 {
61 ANSR_LOGD("countdown time not support PreGetNextTriggerTimeIgnoreSnooze");
62 return ReminderRequest::INVALID_LONG_LONG_VALUE;
63 }
64
OnDateTimeChange()65 bool ReminderRequestTimer::OnDateTimeChange()
66 {
67 UpdateTimeInfo("onDateTimeChange");
68 return false;
69 }
70
OnTimeZoneChange()71 bool ReminderRequestTimer::OnTimeZoneChange()
72 {
73 UpdateTimeInfo("onTimeZoneChange");
74 return false;
75 }
76
UpdateNextReminder()77 bool ReminderRequestTimer::UpdateNextReminder()
78 {
79 ANSR_LOGD("countdown time not support repeat reminder, no need to update next triggerTime");
80 SetExpired(true);
81 return false;
82 }
83
CheckParamsValid(const uint64_t countDownTimeInSeconds) const84 void ReminderRequestTimer::CheckParamsValid(const uint64_t countDownTimeInSeconds) const
85 {
86 if (countDownTimeInSeconds == 0 || countDownTimeInSeconds >= (UINT64_MAX / ReminderRequest::MILLI_SECONDS)) {
87 ANSR_LOGE("Illegal count down time, please check the description of the constructor");
88 return;
89 }
90 }
91
UpdateTimeInfo(const std::string & description)92 void ReminderRequestTimer::UpdateTimeInfo(const std::string &description)
93 {
94 if (IsExpired()) {
95 return;
96 }
97 ANSR_LOGD("%{public}s, update countdown time trigger time", description.c_str());
98 time_t now;
99 (void)time(&now); // unit is seconds.
100 whenToChangeSysTime_ = ReminderRequest::GetDurationSinceEpochInMilli(now);
101 sptr<MiscServices::TimeServiceClient> timer = MiscServices::TimeServiceClient::GetInstance();
102 if (timer == nullptr) {
103 ANSR_LOGW("Failed to updateTime info due to TimeServiceClient is null.");
104 return;
105 }
106 int64_t bootTime = timer->GetBootTimeMs();
107 if (bootTime < 0) {
108 ANSR_LOGW("BootTime is illegal");
109 return;
110 }
111 SetTriggerTimeInMilli(whenToChangeSysTime_ + (countDownTimeInSeconds_ * MILLI_SECONDS -
112 (static_cast<uint64_t>(bootTime) - firstRealTimeInMilliSeconds_)));
113 }
114
Marshalling(Parcel & parcel) const115 bool ReminderRequestTimer::Marshalling(Parcel &parcel) const
116 {
117 ReminderRequest::Marshalling(parcel);
118
119 // write int
120 if (!parcel.WriteUint64(firstRealTimeInMilliSeconds_)) {
121 ANSR_LOGE("Failed to write firstRealTimeInMilliSeconds");
122 return false;
123 }
124 if (!parcel.WriteUint64(countDownTimeInSeconds_)) {
125 ANSR_LOGE("Failed to write countDownTimeInSeconds");
126 return false;
127 }
128 return true;
129 }
130
Unmarshalling(Parcel & parcel)131 ReminderRequestTimer *ReminderRequestTimer::Unmarshalling(Parcel &parcel)
132 {
133 auto objptr = new (std::nothrow) ReminderRequestTimer();
134 if (objptr == nullptr) {
135 ANSR_LOGE("Failed to create reminder timer due to no memory.");
136 return objptr;
137 }
138 if (!objptr->ReadFromParcel(parcel)) {
139 delete objptr;
140 objptr = nullptr;
141 }
142 return objptr;
143 }
144
ReadFromParcel(Parcel & parcel)145 bool ReminderRequestTimer::ReadFromParcel(Parcel &parcel)
146 {
147 ReminderRequest::ReadFromParcel(parcel);
148
149 // read int
150 if (!parcel.ReadUint64(firstRealTimeInMilliSeconds_)) {
151 ANSR_LOGE("Failed to read firstRealTimeInMilliSeconds");
152 return false;
153 }
154 if (!parcel.ReadUint64(countDownTimeInSeconds_)) {
155 ANSR_LOGE("Failed to read countDownTimeInSeconds");
156 return false;
157 }
158 return true;
159 }
160 }
161 }