1 /*
2 * Copyright (c) 2023 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 "dark_state.h"
17
18 #include "time_provider.h"
19 #include "timed_task.h"
20
21 #include "standby_service_log.h"
22 #include "standby_config_manager.h"
23 #include "iconstraint_manager_adapter.h"
24 #include "istate_manager_adapter.h"
25
26
27 namespace OHOS {
28 namespace DevStandbyMgr {
DarkState(uint32_t curState,uint32_t curPhase,const std::shared_ptr<IStateManagerAdapter> & stateManager,std::shared_ptr<AppExecFwk::EventHandler> & handler)29 DarkState::DarkState(uint32_t curState, uint32_t curPhase, const std::shared_ptr<IStateManagerAdapter>&
30 stateManager, std::shared_ptr<AppExecFwk::EventHandler>& handler): BaseState(curState, curPhase,
31 stateManager, handler)
32 {
33 darkTimeOut_ = TimeConstant::MSEC_PER_SEC * StandbyConfigManager::GetInstance()->GetStandbyParam(DARK_TIMEOUT);
34 nextState_ = StandbyState::DARK;
35 if (StandbyConfigManager::GetInstance()->GetStandbySwitch(NAP_SWITCH)) {
36 nextState_ = StandbyState::NAP;
37 } else if (StandbyConfigManager::GetInstance()->GetStandbySwitch(SLEEP_SWITCH)) {
38 nextState_ = StandbyState::SLEEP;
39 }
40 }
41
BeginState()42 ErrCode DarkState::BeginState()
43 {
44 curPhase_ = 0;
45 if (nextState_ != StandbyState::DARK) {
46 return StartStateTransitionTimer(darkTimeOut_);
47 }
48 return ERR_OK;
49 }
50
EndState()51 ErrCode DarkState::EndState()
52 {
53 StopTimedTask(TRANSIT_NEXT_STATE_TIMED_TASK);
54 handler_->RemoveTask(TRANSIT_NEXT_STATE_TIMED_TASK);
55 return ERR_OK;
56 }
57
CheckTransitionValid(uint32_t nextState)58 bool DarkState::CheckTransitionValid(uint32_t nextState)
59 {
60 if (nextState == StandbyState::MAINTENANCE) {
61 STANDBYSERVICE_LOGE("can not transit from dark to maintenance");
62 return false;
63 }
64 return true;
65 }
66
EndEvalCurrentState(bool evalResult)67 void DarkState::EndEvalCurrentState(bool evalResult)
68 {
69 auto stateManagerPtr = stateManager_.lock();
70 if (!stateManagerPtr) {
71 STANDBYSERVICE_LOGW("state manager is nullptr, can not end evalution current state");
72 return;
73 }
74 if (!evalResult) {
75 STANDBYSERVICE_LOGD("constraint evalution failed, block current state");
76 stateManagerPtr->BlockCurrentState();
77 } else if (nextState_ != StandbyState::DARK) {
78 stateManagerPtr->TransitToStateInner(nextState_);
79 }
80 }
81 } // namespace DevStandbyMgr
82 } // namespace OHOS