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