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/group_listener.h"
16 
17 #include "work_queue_event_handler.h"
18 #include "work_sched_hilog.h"
19 
20 namespace OHOS {
21 namespace WorkScheduler {
GroupListener(std::shared_ptr<WorkQueueManager> workQueueManager,const std::shared_ptr<AppExecFwk::EventRunner> & runner)22 GroupListener::GroupListener(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 GroupListener::OnConditionChanged(WorkCondition::Type conditionType,
32     std::shared_ptr<DetectorValue> conditionVal)
33 {
34     workQueueManager_->OnConditionChanged(conditionType, conditionVal);
35 }
36 
Start()37 bool GroupListener::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     int32_t time = workQueueManager_->GetTimeRetrigger();
51     handler_->SendEvent(AppExecFwk::InnerEvent::Get(WorkQueueEventHandler::GROUP_TICK, 0), time);
52     return true;
53 }
54 
Stop()55 bool GroupListener::Stop()
56 {
57     if (handler_ != nullptr) {
58         handler_->RemoveEvent(WorkQueueEventHandler::GROUP_TICK);
59     }
60     return true;
61 }
62 } // namespace WorkScheduler
63 } // namespace OHOS