1 /*
2 * Copyright (c) 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 "delay_suspend_info_ex.h"
17
18 #include "transient_task_log.h"
19 #include "time_provider.h"
20
21 namespace OHOS {
22 namespace BackgroundTaskMgr {
DelaySuspendInfoEx(const int32_t & pid,const int32_t & requestId,const int32_t & delaytime)23 DelaySuspendInfoEx::DelaySuspendInfoEx(const int32_t& pid, const int32_t& requestId, const int32_t& delaytime)
24 {
25 SetPid(pid);
26 SetRequestId(requestId);
27 SetActualDelayTime(delaytime);
28 }
29
GetRemainDelayTime()30 int32_t DelaySuspendInfoEx::GetRemainDelayTime()
31 {
32 int64_t spendTime = (baseTime_ > 0) ? (spendTime_ + (TimeProvider::GetCurrentTime() - baseTime_)) : spendTime_;
33 int32_t remainTime = GetActualDelayTime() - (int32_t)spendTime;
34 return (remainTime < 0) ? 0 : remainTime;
35 }
36
GetAdvanceCallbackTime()37 int32_t DelaySuspendInfoEx::GetAdvanceCallbackTime()
38 {
39 int32_t advanceCallbackTime = GetRemainDelayTime() - advanceTime_;
40 return (advanceCallbackTime > 0) ? advanceCallbackTime : 0;
41 }
42
StartAccounting()43 void DelaySuspendInfoEx::StartAccounting()
44 {
45 if (baseTime_ == 0) {
46 baseTime_ = TimeProvider::GetCurrentTime();
47 }
48 }
49
StopAccounting()50 void DelaySuspendInfoEx::StopAccounting()
51 {
52 if (baseTime_ != 0) {
53 spendTime_ += TimeProvider::GetCurrentTime() - baseTime_;
54 baseTime_ = 0;
55 }
56 }
57
Dump(std::string & result)58 void DelaySuspendInfoEx::Dump(std::string& result)
59 {
60 result.append("{ requestId: " + std::to_string(GetRequestId()));
61 result.append(", reaminTime: " + std::to_string(GetRemainDelayTime()));
62 result.append(", advanceCallbackTime: " + std::to_string(GetAdvanceCallbackTime()) + "}");
63 }
64 } // namespace BackgroundTaskMgr
65 } // namespace OHOS