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,const std::shared_ptr<AppExecFwk::EventRunner> & runner)22 TimerListener::TimerListener(std::shared_ptr<WorkQueueManager> workQueueManager,
23 const std::shared_ptr<AppExecFwk::EventRunner>& runner)
24 {
25 workQueueManager_ = workQueueManager;
26 if (runner != nullptr) {
27 eventRunner_ = runner;
28 }
29 }
30
OnConditionChanged(WorkCondition::Type conditionType,std::shared_ptr<DetectorValue> conditionVal)31 void TimerListener::OnConditionChanged(WorkCondition::Type conditionType,
32 std::shared_ptr<DetectorValue> conditionVal)
33 {
34 workQueueManager_->OnConditionChanged(conditionType, conditionVal);
35 }
36
Start()37 bool TimerListener::Start()
38 {
39 if (!eventRunner_) {
40 WS_HILOGE("Init failed due to create EventHandler");
41 return false;
42 }
43 if (!handler_) {
44 handler_ = std::make_shared<WorkQueueEventHandler>(eventRunner_, workQueueManager_);
45 }
46 if (workQueueManager_ == nullptr) {
47 WS_HILOGE("workQueueManager_ is null");
48 return false;
49 }
50 uint32_t time = workQueueManager_->GetTimeCycle();
51 WS_HILOGI("TimerListener start with time = %{public}u.", time);
52 handler_->SendEvent(AppExecFwk::InnerEvent::Get(WorkQueueEventHandler::TIMER_TICK, 0), time);
53 return true;
54 }
55
Stop()56 bool TimerListener::Stop()
57 {
58 if (handler_ != nullptr) {
59 handler_->RemoveEvent(WorkQueueEventHandler::TIMER_TICK);
60 }
61 return true;
62 }
63 } // namespace WorkScheduler
64 } // namespace OHOS