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 BGTASK_LOGD("requestId %{public}d remainTime %{public}d", GetRequestId(), remainTime);
35 return (remainTime < 0) ? 0 : remainTime;
36 }
37
GetAdvanceCallbackTime()38 int32_t DelaySuspendInfoEx::GetAdvanceCallbackTime()
39 {
40 int32_t advanceCallbackTime = GetRemainDelayTime() - advanceTime_;
41 return (advanceCallbackTime > 0) ? advanceCallbackTime : 0;
42 }
43
StartAccounting()44 void DelaySuspendInfoEx::StartAccounting()
45 {
46 if (baseTime_ == 0) {
47 baseTime_ = TimeProvider::GetCurrentTime();
48 }
49 }
50
StopAccounting()51 void DelaySuspendInfoEx::StopAccounting()
52 {
53 if (baseTime_ != 0) {
54 spendTime_ += TimeProvider::GetCurrentTime() - baseTime_;
55 baseTime_ = 0;
56 }
57 }
58
Dump(std::string & result)59 void DelaySuspendInfoEx::Dump(std::string& result)
60 {
61 result.append("{ requestId: " + std::to_string(GetRequestId()));
62 result.append(", reaminTime: " + std::to_string(GetRemainDelayTime()));
63 result.append(", advanceCallbackTime: " + std::to_string(GetAdvanceCallbackTime()) + "}");
64 }
65 } // namespace BackgroundTaskMgr
66 } // namespace OHOS