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