• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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_LOGE("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_LOGE("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("called");
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("called");
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_LOGE("Failed to updateTime info due to TimeServiceClient is null.");
110         return;
111     }
112     int64_t bootTime = timer->GetBootTimeMs();
113     if (bootTime < 0) {
114         ANSR_LOGE("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     return WriteParcel(parcel);
124 }
125 
WriteParcel(Parcel & parcel) const126 bool ReminderRequestTimer::WriteParcel(Parcel &parcel) const
127 {
128     if (ReminderRequest::WriteParcel(parcel)) {
129         // write int
130         WRITE_UINT64_RETURN_FALSE_LOG(parcel, firstRealTimeInMilliSeconds_, "firstRealTimeInMilliSeconds");
131         WRITE_UINT64_RETURN_FALSE_LOG(parcel, countDownTimeInSeconds_, "countDownTimeInSeconds");
132         return true;
133     }
134     return false;
135 }
136 
Unmarshalling(Parcel & parcel)137 ReminderRequestTimer *ReminderRequestTimer::Unmarshalling(Parcel &parcel)
138 {
139     auto objptr = new (std::nothrow) ReminderRequestTimer();
140     if (objptr == nullptr) {
141         ANSR_LOGE("null objptr");
142         return objptr;
143     }
144     if (!objptr->ReadFromParcel(parcel)) {
145         delete objptr;
146         objptr = nullptr;
147     }
148     return objptr;
149 }
150 
ReadFromParcel(Parcel & parcel)151 bool ReminderRequestTimer::ReadFromParcel(Parcel &parcel)
152 {
153     if (ReminderRequest::ReadFromParcel(parcel)) {
154         // read int
155         READ_UINT64_RETURN_FALSE_LOG(parcel, firstRealTimeInMilliSeconds_, "firstRealTimeInMilliSeconds");
156         READ_UINT64_RETURN_FALSE_LOG(parcel, countDownTimeInSeconds_, "countDownTimeInSeconds");
157         return true;
158     }
159     return false;
160 }
161 }
162 }
163