199552fe9Sopenharmony_ci/*
299552fe9Sopenharmony_ci * Copyright (c) 2023 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 "dark_state.h"
1799552fe9Sopenharmony_ci
1899552fe9Sopenharmony_ci#include "time_provider.h"
1999552fe9Sopenharmony_ci#include "timed_task.h"
2099552fe9Sopenharmony_ci
2199552fe9Sopenharmony_ci#include "standby_service_log.h"
2299552fe9Sopenharmony_ci#include "standby_config_manager.h"
2399552fe9Sopenharmony_ci#include "iconstraint_manager_adapter.h"
2499552fe9Sopenharmony_ci#include "istate_manager_adapter.h"
2599552fe9Sopenharmony_ci
2699552fe9Sopenharmony_ci
2799552fe9Sopenharmony_cinamespace OHOS {
2899552fe9Sopenharmony_cinamespace DevStandbyMgr {
2999552fe9Sopenharmony_ciDarkState::DarkState(uint32_t curState, uint32_t curPhase, const std::shared_ptr<IStateManagerAdapter>&
3099552fe9Sopenharmony_ci    stateManager, std::shared_ptr<AppExecFwk::EventHandler>& handler): BaseState(curState, curPhase,
3199552fe9Sopenharmony_ci    stateManager, handler)
3299552fe9Sopenharmony_ci{
3399552fe9Sopenharmony_ci    darkTimeOut_ = TimeConstant::MSEC_PER_SEC * StandbyConfigManager::GetInstance()->GetStandbyParam(DARK_TIMEOUT);
3499552fe9Sopenharmony_ci    nextState_ = StandbyState::DARK;
3599552fe9Sopenharmony_ci    if (StandbyConfigManager::GetInstance()->GetStandbySwitch(NAP_SWITCH)) {
3699552fe9Sopenharmony_ci        nextState_ = StandbyState::NAP;
3799552fe9Sopenharmony_ci    } else if (StandbyConfigManager::GetInstance()->GetStandbySwitch(SLEEP_SWITCH)) {
3899552fe9Sopenharmony_ci        nextState_ = StandbyState::SLEEP;
3999552fe9Sopenharmony_ci    }
4099552fe9Sopenharmony_ci}
4199552fe9Sopenharmony_ci
4299552fe9Sopenharmony_ciErrCode DarkState::BeginState()
4399552fe9Sopenharmony_ci{
4499552fe9Sopenharmony_ci    curPhase_ = 0;
4599552fe9Sopenharmony_ci    if (nextState_ != StandbyState::DARK) {
4699552fe9Sopenharmony_ci        return StartStateTransitionTimer(darkTimeOut_);
4799552fe9Sopenharmony_ci    }
4899552fe9Sopenharmony_ci    return ERR_OK;
4999552fe9Sopenharmony_ci}
5099552fe9Sopenharmony_ci
5199552fe9Sopenharmony_ciErrCode DarkState::EndState()
5299552fe9Sopenharmony_ci{
5399552fe9Sopenharmony_ci    StopTimedTask(TRANSIT_NEXT_STATE_TIMED_TASK);
5499552fe9Sopenharmony_ci    handler_->RemoveTask(TRANSIT_NEXT_STATE_TIMED_TASK);
5599552fe9Sopenharmony_ci    return ERR_OK;
5699552fe9Sopenharmony_ci}
5799552fe9Sopenharmony_ci
5899552fe9Sopenharmony_cibool DarkState::CheckTransitionValid(uint32_t nextState)
5999552fe9Sopenharmony_ci{
6099552fe9Sopenharmony_ci    if (nextState == StandbyState::MAINTENANCE) {
6199552fe9Sopenharmony_ci        STANDBYSERVICE_LOGE("can not transit from dark to maintenance");
6299552fe9Sopenharmony_ci        return false;
6399552fe9Sopenharmony_ci    }
6499552fe9Sopenharmony_ci    return true;
6599552fe9Sopenharmony_ci}
6699552fe9Sopenharmony_ci
6799552fe9Sopenharmony_civoid DarkState::EndEvalCurrentState(bool evalResult)
6899552fe9Sopenharmony_ci{
6999552fe9Sopenharmony_ci    auto stateManagerPtr = stateManager_.lock();
7099552fe9Sopenharmony_ci    if (!stateManagerPtr) {
7199552fe9Sopenharmony_ci        STANDBYSERVICE_LOGW("state manager is nullptr, can not end evalution current state");
7299552fe9Sopenharmony_ci        return;
7399552fe9Sopenharmony_ci    }
7499552fe9Sopenharmony_ci    if (!evalResult) {
7599552fe9Sopenharmony_ci        STANDBYSERVICE_LOGD("constraint evalution failed, block current state");
7699552fe9Sopenharmony_ci        stateManagerPtr->BlockCurrentState();
7799552fe9Sopenharmony_ci    } else if (nextState_ != StandbyState::DARK) {
7899552fe9Sopenharmony_ci        stateManagerPtr->TransitToStateInner(nextState_);
7999552fe9Sopenharmony_ci    }
8099552fe9Sopenharmony_ci}
8199552fe9Sopenharmony_ci}  // namespace DevStandbyMgr
8299552fe9Sopenharmony_ci}  // namespace OHOS