• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)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     : id {_id},
45       type {_type},
46       origWhen {_when},
47       wakeup {_type == ITimerManager::ELAPSED_REALTIME_WAKEUP || _type == ITimerManager::RTC_WAKEUP},
48       callback {std::move(_callback)},
49       wantAgent {_wantAgent},
50       flags {_flags},
51       uid {_uid},
52       when {_when},
53       windowLength {_windowLength},
54       whenElapsed {_whenElapsed},
55       maxWhenElapsed {_maxWhen},
56       expectedWhenElapsed {_whenElapsed},
57       expectedMaxWhenElapsed {_maxWhen},
58       repeatInterval {_interval}
59 {
60 }
61 
UpdateWhenElapsed(std::chrono::steady_clock::time_point policyElapsed,std::chrono::nanoseconds offset)62 bool TimerInfo::UpdateWhenElapsed(std::chrono::steady_clock::time_point policyElapsed, std::chrono::nanoseconds offset)
63 {
64     TIME_HILOGD(TIME_MODULE_SERVICE, "Update whenElapsed, id=%{public}" PRId64 "", id);
65     auto oldWhenElapsed = whenElapsed;
66     auto offsetMill = std::chrono::duration_cast<std::chrono::milliseconds>(offset);
67     whenElapsed = policyElapsed + offsetMill;
68     auto oldMaxWhenElapsed = maxWhenElapsed;
69     maxWhenElapsed = whenElapsed + windowLength;
70     expectedWhenElapsed = whenElapsed;
71     expectedMaxWhenElapsed = maxWhenElapsed;
72     std::chrono::milliseconds currentTime;
73     if (type == ITimerManager::RTC || type == ITimerManager::RTC_WAKEUP) {
74         currentTime =
75             std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
76     } else {
77         currentTime = std::chrono::duration_cast<std::chrono::milliseconds>(policyElapsed.time_since_epoch());
78     }
79     when = currentTime + offsetMill;
80     return (oldWhenElapsed != whenElapsed) || (oldMaxWhenElapsed != maxWhenElapsed);
81 }
82 } // MiscServices
83 } // OHOS