• 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 "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()48 TimedTask::~TimedTask()
49 {}
50 
OnTrigger()51 void TimedTask::OnTrigger()
52 {
53     STANDBYSERVICE_LOGD("timed task had been triggered");
54     if (callBack_ != nullptr) {
55         STANDBYSERVICE_LOGD("start invoke callback function of timed task");
56         callBack_();
57     }
58     STANDBYSERVICE_LOGD("end timed task callback");
59 }
60 
SetType(const int & type)61 void TimedTask::SetType(const int &type)
62 {
63     this->type = type;
64 }
65 
SetRepeat(bool repeat)66 void TimedTask::SetRepeat(bool repeat)
67 {
68     this->repeat = repeat;
69 }
70 
SetInterval(const uint64_t & interval)71 void TimedTask::SetInterval(const uint64_t& interval)
72 {
73     this->interval = interval;
74 }
75 
SetWantAgent(std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent)76 void TimedTask::SetWantAgent(std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent)
77 {
78     this->wantAgent = wantAgent;
79 }
80 
SetCallbackInfo(const std::function<void ()> & callBack)81 void TimedTask::SetCallbackInfo(const std::function<void()>& callBack)
82 {
83     this->callBack_ = callBack;
84 }
85 
CreateTimer(bool repeat,uint64_t interval,bool isExact,bool isIdle,const std::function<void ()> & callBack)86 uint64_t WEAK_FUNC TimedTask::CreateTimer(bool repeat, uint64_t interval, bool isExact, bool isIdle,
87     const std::function<void()>& callBack)
88 {
89     auto timedTask = std::make_shared<TimedTask>(repeat, interval, isExact, isIdle);
90     timedTask->SetCallbackInfo(callBack);
91     return MiscServices::TimeServiceClient::GetInstance()->CreateTimer(timedTask);
92 }
93 
StartDayNightSwitchTimer(uint64_t & timeId)94 bool WEAK_FUNC TimedTask::StartDayNightSwitchTimer(uint64_t& timeId)
95 {
96     int64_t timeDiff {0};
97     if (!TimeProvider::TimeDiffToDayNightSwitch(timeDiff)) {
98         return false;
99     }
100     timeDiff += TimeProvider::GetRandomDelay(LOW_DELAY_TIME_INTERVAL, HIGH_DELAY_TIME_INTERVAL);
101     STANDBYSERVICE_LOGI("start next day and night switch after " SPUBI64 " ms", timeDiff);
102 
103     auto curTimeStamp = MiscServices::TimeServiceClient::GetInstance()->GetWallTimeMs();
104     if (!MiscServices::TimeServiceClient::GetInstance()->StartTimer(timeId, curTimeStamp + timeDiff)) {
105         STANDBYSERVICE_LOGE("day and night switch observer start failed");
106         return false;
107     }
108     return true;
109 }
110 
RegisterDayNightSwitchTimer(uint64_t & timeId,bool repeat,uint64_t interval,const std::function<void ()> & callBack)111 bool WEAK_FUNC TimedTask::RegisterDayNightSwitchTimer(uint64_t& timeId, bool repeat, uint64_t interval,
112     const std::function<void()>& callBack)
113 {
114     timeId = CreateTimer(repeat, interval, false, false, callBack);
115     if (timeId == 0) {
116         STANDBYSERVICE_LOGE("create timer failed");
117         return false;
118     }
119     return StartDayNightSwitchTimer(timeId);
120 }
121 } // namespace DevStandbyMgr
122 } // namespace OHOS