1 /*
2 * Copyright (C) 2021 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 "timer_info.h"
17
18 #include <cinttypes>
19
20 #include "time_hilog.h"
21
22 namespace OHOS {
23 namespace MiscServices {
operator ==(const TimerInfo & other) const24 bool TimerInfo::operator==(const TimerInfo &other) const
25 {
26 return this->id == other.id;
27 }
28
Matches(const std::string & packageName) const29 bool TimerInfo::Matches(const std::string &packageName) const
30 {
31 return false;
32 }
33
TimerInfo(uint64_t _id,int _type,std::chrono::milliseconds _when,std::chrono::steady_clock::time_point _whenElapsed,std::chrono::milliseconds _windowLength,std::chrono::steady_clock::time_point _maxWhen,std::chrono::milliseconds _interval,std::function<void (const uint64_t)> _callback,std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> _wantAgent,uint32_t _flags,int _uid,const std::string & _bundleName)34 TimerInfo::TimerInfo(uint64_t _id, int _type,
35 std::chrono::milliseconds _when,
36 std::chrono::steady_clock::time_point _whenElapsed,
37 std::chrono::milliseconds _windowLength,
38 std::chrono::steady_clock::time_point _maxWhen,
39 std::chrono::milliseconds _interval,
40 std::function<void(const uint64_t)> _callback,
41 std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> _wantAgent,
42 uint32_t _flags,
43 int _uid,
44 const std::string &_bundleName)
45 : id {_id},
46 type {_type},
47 origWhen {_when},
48 wakeup {_type == ITimerManager::ELAPSED_REALTIME_WAKEUP || _type == ITimerManager::RTC_WAKEUP},
49 callback {std::move(_callback)},
50 wantAgent {_wantAgent},
51 flags {_flags},
52 uid {_uid},
53 when {_when},
54 windowLength {_windowLength},
55 whenElapsed {_whenElapsed},
56 maxWhenElapsed {_maxWhen},
57 expectedWhenElapsed {_whenElapsed},
58 expectedMaxWhenElapsed {_maxWhen},
59 repeatInterval {_interval},
60 bundleName {_bundleName}
61 {
62 }
63
UpdateWhenElapsed(std::chrono::steady_clock::time_point policyElapsed,std::chrono::nanoseconds offset)64 bool TimerInfo::UpdateWhenElapsed(std::chrono::steady_clock::time_point policyElapsed, std::chrono::nanoseconds offset)
65 {
66 TIME_HILOGD(TIME_MODULE_SERVICE, "Update whenElapsed, id=%{public}" PRId64 "", id);
67 auto oldWhenElapsed = whenElapsed;
68 auto offsetMill = std::chrono::duration_cast<std::chrono::milliseconds>(offset);
69 whenElapsed = policyElapsed + offsetMill;
70 auto oldMaxWhenElapsed = maxWhenElapsed;
71 maxWhenElapsed = whenElapsed + windowLength;
72 expectedWhenElapsed = whenElapsed;
73 expectedMaxWhenElapsed = maxWhenElapsed;
74 std::chrono::milliseconds currentTime;
75 if (type == ITimerManager::RTC || type == ITimerManager::RTC_WAKEUP) {
76 currentTime =
77 std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
78 } else {
79 currentTime = std::chrono::duration_cast<std::chrono::milliseconds>(policyElapsed.time_since_epoch());
80 }
81 when = currentTime + offsetMill;
82 return (oldWhenElapsed != whenElapsed) || (oldMaxWhenElapsed != maxWhenElapsed);
83 }
84 } // MiscServices
85 } // OHOS