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 "timed_task.h"
17
18 #include "standby_service_log.h"
19 #include "common_constant.h"
20 #include "time_provider.h"
21
22 namespace OHOS {
23 namespace DevStandbyMgr {
24 namespace {
25 constexpr int32_t LOW_DELAY_TIME_INTERVAL = 0;
26 constexpr int32_t HIGH_DELAY_TIME_INTERVAL = 15 * 60 * 1000;
27 }
TimedTask()28 TimedTask::TimedTask()
29 {}
30
TimedTask(bool repeat,uint64_t interval,bool isExact,bool isIdle)31 TimedTask::TimedTask(bool repeat, uint64_t interval, bool isExact, bool isIdle)
32 {
33 this->repeat = repeat;
34 this->interval = interval;
35 this->type = TIMER_TYPE_WAKEUP;
36 if (isExact) {
37 this->type = TIMER_TYPE_WAKEUP + TIMER_TYPE_EXACT;
38 }
39 if (isIdle) {
40 this->type = TIMER_TYPE_IDLE;
41 }
42 }
43
~TimedTask()44 TimedTask::~TimedTask()
45 {}
46
OnTrigger()47 void TimedTask::OnTrigger()
48 {
49 STANDBYSERVICE_LOGD("timed task had been triggered");
50 if (callBack_ != nullptr) {
51 STANDBYSERVICE_LOGD("start invoke callback function of timed task");
52 callBack_();
53 }
54 STANDBYSERVICE_LOGD("end timed task callback");
55 }
56
SetType(const int & type)57 void TimedTask::SetType(const int &type)
58 {
59 this->type = type;
60 }
61
SetRepeat(bool repeat)62 void TimedTask::SetRepeat(bool repeat)
63 {
64 this->repeat = repeat;
65 }
66
SetInterval(const uint64_t & interval)67 void TimedTask::SetInterval(const uint64_t& interval)
68 {
69 this->interval = interval;
70 }
71
SetWantAgent(std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent)72 void TimedTask::SetWantAgent(std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent)
73 {
74 this->wantAgent = wantAgent;
75 }
76
SetCallbackInfo(const std::function<void ()> & callBack)77 void TimedTask::SetCallbackInfo(const std::function<void()>& callBack)
78 {
79 this->callBack_ = callBack;
80 }
81
CreateTimer(bool repeat,uint64_t interval,bool isExact,bool isIdle,const std::function<void ()> & callBack)82 uint64_t WEAK_FUNC TimedTask::CreateTimer(bool repeat, uint64_t interval, bool isExact, bool isIdle,
83 const std::function<void()>& callBack)
84 {
85 auto timedTask = std::make_shared<TimedTask>(repeat, interval, isExact, isIdle);
86 timedTask->SetCallbackInfo(callBack);
87 return MiscServices::TimeServiceClient::GetInstance()->CreateTimer(timedTask);
88 }
89
StartDayNightSwitchTimer(uint64_t & timeId)90 bool WEAK_FUNC TimedTask::StartDayNightSwitchTimer(uint64_t& timeId)
91 {
92 int64_t timeDiff {0};
93 if (!TimeProvider::TimeDiffToDayNightSwitch(timeDiff)) {
94 return false;
95 }
96 timeDiff += TimeProvider::GetRandomDelay(LOW_DELAY_TIME_INTERVAL, HIGH_DELAY_TIME_INTERVAL);
97 STANDBYSERVICE_LOGI("start next day and night switch after " SPUBI64 " ms", timeDiff);
98
99 auto curTimeStamp = MiscServices::TimeServiceClient::GetInstance()->GetWallTimeMs();
100 if (!MiscServices::TimeServiceClient::GetInstance()->StartTimer(timeId, curTimeStamp + timeDiff)) {
101 STANDBYSERVICE_LOGE("day and night switch observer start failed");
102 return false;
103 }
104 return true;
105 }
106
RegisterDayNightSwitchTimer(uint64_t & timeId,bool repeat,uint64_t interval,const std::function<void ()> & callBack)107 bool WEAK_FUNC TimedTask::RegisterDayNightSwitchTimer(uint64_t& timeId, bool repeat, uint64_t interval,
108 const std::function<void()>& callBack)
109 {
110 timeId = CreateTimer(repeat, interval, false, false, callBack);
111 if (timeId == 0) {
112 STANDBYSERVICE_LOGE("create timer failed");
113 return false;
114 }
115 return StartDayNightSwitchTimer(timeId);
116 }
117 } // namespace DevStandbyMgr
118 } // namespace OHOS