199552fe9Sopenharmony_ci/* 299552fe9Sopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd. 399552fe9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 499552fe9Sopenharmony_ci * you may not use this file except in compliance with the License. 599552fe9Sopenharmony_ci * You may obtain a copy of the License at 699552fe9Sopenharmony_ci * 799552fe9Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 899552fe9Sopenharmony_ci * 999552fe9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1099552fe9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1199552fe9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1299552fe9Sopenharmony_ci * See the License for the specific language governing permissions and 1399552fe9Sopenharmony_ci * limitations under the License. 1499552fe9Sopenharmony_ci */ 1599552fe9Sopenharmony_ci 1699552fe9Sopenharmony_ci#include "timed_task.h" 1799552fe9Sopenharmony_ci 1899552fe9Sopenharmony_ci#include "standby_service_log.h" 1999552fe9Sopenharmony_ci#include "common_constant.h" 2099552fe9Sopenharmony_ci#include "time_provider.h" 2199552fe9Sopenharmony_ci#include "standby_config_manager.h" 2299552fe9Sopenharmony_ci 2399552fe9Sopenharmony_cinamespace OHOS { 2499552fe9Sopenharmony_cinamespace DevStandbyMgr { 2599552fe9Sopenharmony_cinamespace { 2699552fe9Sopenharmony_ci constexpr int32_t LOW_DELAY_TIME_INTERVAL = 0; 2799552fe9Sopenharmony_ci constexpr int32_t HIGH_DELAY_TIME_INTERVAL = 15 * 60 * 1000; 2899552fe9Sopenharmony_ci} 2999552fe9Sopenharmony_ciTimedTask::TimedTask() 3099552fe9Sopenharmony_ci{} 3199552fe9Sopenharmony_ci 3299552fe9Sopenharmony_ciTimedTask::TimedTask(bool repeat, uint64_t interval, bool isExact, bool isIdle) 3399552fe9Sopenharmony_ci{ 3499552fe9Sopenharmony_ci this->repeat = repeat; 3599552fe9Sopenharmony_ci this->interval = interval; 3699552fe9Sopenharmony_ci this->type = TIMER_TYPE_WAKEUP; 3799552fe9Sopenharmony_ci if (isExact) { 3899552fe9Sopenharmony_ci this->type = TIMER_TYPE_WAKEUP + TIMER_TYPE_EXACT; 3999552fe9Sopenharmony_ci } 4099552fe9Sopenharmony_ci if (StandbyConfigManager::GetInstance()->GetStandbySwitch(S3_SWITCH)) { 4199552fe9Sopenharmony_ci this->type = TIMER_TYPE_EXACT; 4299552fe9Sopenharmony_ci } 4399552fe9Sopenharmony_ci if (isIdle) { 4499552fe9Sopenharmony_ci this->type = TIMER_TYPE_IDLE; 4599552fe9Sopenharmony_ci } 4699552fe9Sopenharmony_ci} 4799552fe9Sopenharmony_ci 4899552fe9Sopenharmony_ciTimedTask::TimedTask(bool repeat, uint64_t interval, int type) 4999552fe9Sopenharmony_ci{ 5099552fe9Sopenharmony_ci this->repeat = repeat; 5199552fe9Sopenharmony_ci this->interval = interval; 5299552fe9Sopenharmony_ci this->type = type; 5399552fe9Sopenharmony_ci} 5499552fe9Sopenharmony_ci 5599552fe9Sopenharmony_ciTimedTask::~TimedTask() 5699552fe9Sopenharmony_ci{} 5799552fe9Sopenharmony_ci 5899552fe9Sopenharmony_civoid TimedTask::OnTrigger() 5999552fe9Sopenharmony_ci{ 6099552fe9Sopenharmony_ci STANDBYSERVICE_LOGD("timed task had been triggered"); 6199552fe9Sopenharmony_ci if (callBack_ != nullptr) { 6299552fe9Sopenharmony_ci STANDBYSERVICE_LOGD("start invoke callback function of timed task"); 6399552fe9Sopenharmony_ci callBack_(); 6499552fe9Sopenharmony_ci } 6599552fe9Sopenharmony_ci STANDBYSERVICE_LOGD("end timed task callback"); 6699552fe9Sopenharmony_ci} 6799552fe9Sopenharmony_ci 6899552fe9Sopenharmony_civoid TimedTask::SetType(const int &type) 6999552fe9Sopenharmony_ci{ 7099552fe9Sopenharmony_ci this->type = type; 7199552fe9Sopenharmony_ci} 7299552fe9Sopenharmony_ci 7399552fe9Sopenharmony_civoid TimedTask::SetRepeat(bool repeat) 7499552fe9Sopenharmony_ci{ 7599552fe9Sopenharmony_ci this->repeat = repeat; 7699552fe9Sopenharmony_ci} 7799552fe9Sopenharmony_ci 7899552fe9Sopenharmony_civoid TimedTask::SetInterval(const uint64_t& interval) 7999552fe9Sopenharmony_ci{ 8099552fe9Sopenharmony_ci this->interval = interval; 8199552fe9Sopenharmony_ci} 8299552fe9Sopenharmony_ci 8399552fe9Sopenharmony_civoid TimedTask::SetWantAgent(std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent) 8499552fe9Sopenharmony_ci{ 8599552fe9Sopenharmony_ci this->wantAgent = wantAgent; 8699552fe9Sopenharmony_ci} 8799552fe9Sopenharmony_ci 8899552fe9Sopenharmony_civoid TimedTask::SetCallbackInfo(const std::function<void()>& callBack) 8999552fe9Sopenharmony_ci{ 9099552fe9Sopenharmony_ci this->callBack_ = callBack; 9199552fe9Sopenharmony_ci} 9299552fe9Sopenharmony_ci 9399552fe9Sopenharmony_ciuint64_t WEAK_FUNC TimedTask::CreateTimer(bool repeat, uint64_t interval, bool isExact, bool isIdle, 9499552fe9Sopenharmony_ci const std::function<void()>& callBack) 9599552fe9Sopenharmony_ci{ 9699552fe9Sopenharmony_ci auto timedTask = std::make_shared<TimedTask>(repeat, interval, isExact, isIdle); 9799552fe9Sopenharmony_ci timedTask->SetCallbackInfo(callBack); 9899552fe9Sopenharmony_ci return MiscServices::TimeServiceClient::GetInstance()->CreateTimer(timedTask); 9999552fe9Sopenharmony_ci} 10099552fe9Sopenharmony_ci 10199552fe9Sopenharmony_ciuint64_t WEAK_FUNC TimedTask::CreateTimer(bool repeat, uint64_t interval, int type, 10299552fe9Sopenharmony_ci const std::function<void()>& callBack) 10399552fe9Sopenharmony_ci{ 10499552fe9Sopenharmony_ci auto timedTask = std::make_shared<TimedTask>(repeat, interval, type); 10599552fe9Sopenharmony_ci timedTask->SetCallbackInfo(callBack); 10699552fe9Sopenharmony_ci return MiscServices::TimeServiceClient::GetInstance()->CreateTimer(timedTask); 10799552fe9Sopenharmony_ci} 10899552fe9Sopenharmony_ci 10999552fe9Sopenharmony_cibool WEAK_FUNC TimedTask::StartDayNightSwitchTimer(uint64_t& timeId) 11099552fe9Sopenharmony_ci{ 11199552fe9Sopenharmony_ci int64_t timeDiff {0}; 11299552fe9Sopenharmony_ci if (!TimeProvider::TimeDiffToDayNightSwitch(timeDiff)) { 11399552fe9Sopenharmony_ci return false; 11499552fe9Sopenharmony_ci } 11599552fe9Sopenharmony_ci timeDiff += TimeProvider::GetRandomDelay(LOW_DELAY_TIME_INTERVAL, HIGH_DELAY_TIME_INTERVAL); 11699552fe9Sopenharmony_ci STANDBYSERVICE_LOGI("start next day and night switch after " SPUBI64 " ms", timeDiff); 11799552fe9Sopenharmony_ci 11899552fe9Sopenharmony_ci auto curTimeStamp = MiscServices::TimeServiceClient::GetInstance()->GetWallTimeMs(); 11999552fe9Sopenharmony_ci if (!MiscServices::TimeServiceClient::GetInstance()->StartTimer(timeId, curTimeStamp + timeDiff)) { 12099552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("day and night switch observer start failed"); 12199552fe9Sopenharmony_ci return false; 12299552fe9Sopenharmony_ci } 12399552fe9Sopenharmony_ci return true; 12499552fe9Sopenharmony_ci} 12599552fe9Sopenharmony_ci 12699552fe9Sopenharmony_cibool WEAK_FUNC TimedTask::RegisterDayNightSwitchTimer(uint64_t& timeId, bool repeat, uint64_t interval, 12799552fe9Sopenharmony_ci const std::function<void()>& callBack) 12899552fe9Sopenharmony_ci{ 12999552fe9Sopenharmony_ci timeId = CreateTimer(repeat, interval, false, false, callBack); 13099552fe9Sopenharmony_ci if (timeId == 0) { 13199552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("create timer failed"); 13299552fe9Sopenharmony_ci return false; 13399552fe9Sopenharmony_ci } 13499552fe9Sopenharmony_ci return StartDayNightSwitchTimer(timeId); 13599552fe9Sopenharmony_ci} 13699552fe9Sopenharmony_ci} // namespace DevStandbyMgr 13799552fe9Sopenharmony_ci} // namespace OHOS