• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "conditions/timer_listener.h"
16 
17 #include "work_queue_event_handler.h"
18 #include "work_sched_hilog.h"
19 
20 namespace OHOS {
21 namespace WorkScheduler {
TimerListener(std::shared_ptr<WorkQueueManager> workQueueManager)22 TimerListener::TimerListener(std::shared_ptr<WorkQueueManager> workQueueManager)
23 {
24     workQueueManager_ = workQueueManager;
25 }
26 
OnConditionChanged(WorkCondition::Type conditionType,std::shared_ptr<DetectorValue> conditionVal)27 void TimerListener::OnConditionChanged(WorkCondition::Type conditionType,
28     std::shared_ptr<DetectorValue> conditionVal)
29 {
30     workQueueManager_->OnConditionChanged(conditionType, conditionVal);
31 }
32 
Start()33 bool TimerListener::Start()
34 {
35     if (!eventRunner_) {
36         eventRunner_ = AppExecFwk::EventRunner::Create(TIMER_LISTENER);
37         if (eventRunner_ == nullptr) {
38             WS_HILOGE("Init failed due to create EventHandler");
39             return false;
40         }
41     }
42     if (!handler_) {
43         handler_ = std::make_shared<WorkQueueEventHandler>(eventRunner_, workQueueManager_);
44     }
45     if (workQueueManager_ == nullptr) {
46         WS_HILOGE("workQueueManager_ is null");
47         return false;
48     }
49     uint32_t time = workQueueManager_->GetTimeCycle();
50     handler_->SendEvent(AppExecFwk::InnerEvent::Get(WorkQueueEventHandler::TIMER_TICK, 0), time);
51     return true;
52 }
53 
Stop()54 bool TimerListener::Stop()
55 {
56     if (handler_ != nullptr) {
57         handler_->RemoveEvent(WorkQueueEventHandler::TIMER_TICK);
58     }
59     return true;
60 }
61 } // namespace WorkScheduler
62 } // namespace OHOS