• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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/screen_listener.h"
16 
17 #include <string>
18 
19 #include "common_event_manager.h"
20 #include "common_event_support.h"
21 #include "matching_skills.h"
22 #include "want.h"
23 #include "work_sched_hilog.h"
24 #include "work_sched_utils.h"
25 #include "work_status.h"
26 #include "work_event_handler.h"
27 #include "work_scheduler_service.h"
28 
29 namespace OHOS {
30 namespace WorkScheduler {
31 namespace {
32 const int MIN_DEEP_IDLE_SCREEN_OFF_TIME_MIN = 31 * 60 * 1000;
33 }
34 
ScreenEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo,ScreenListener & listener)35 ScreenEventSubscriber::ScreenEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo,
36     ScreenListener &listener) : EventFwk::CommonEventSubscriber(subscribeInfo), listener_(listener) {}
37 
OnReceiveEvent(const EventFwk::CommonEventData & data)38 void ScreenEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
39 {
40     const std::string action = data.GetWant().GetAction();
41     WS_HILOGI("OnReceiveEvent get action: %{public}s", action.c_str());
42     auto eventHandler = listener_.service_->GetHandler();
43     if (!eventHandler) {
44         WS_HILOGE("event handler is null");
45         return;
46     }
47     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) {
48         eventHandler->RemoveEvent(WorkEventHandler::CHECK_DEEPIDLE_MSG);
49         listener_.OnConditionChanged(WorkCondition::Type::DEEP_IDLE,
50             std::make_shared<DetectorValue>(0, 0, false, std::string()));
51         auto task = [weak = weak_from_this()]() {
52             auto strong = weak.lock();
53             if (!strong) {
54                 WS_HILOGE("ScreenEventSubscriber::OnReceiveEvent strong is null");
55                 return;
56             }
57             int32_t ret = strong->listener_.service_->StopDeepIdleWorks();
58             if (ret != ERR_OK) {
59                 WS_HILOGE("stop work after unlocking failed, error code:%{public}d.", ret);
60             } else {
61                 WS_HILOGI("stop work after unlocking successed.");
62             }
63         };
64         eventHandler->PostTask(task);
65     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
66         if (eventHandler->HasInnerEvent(static_cast<std::uint32_t>(WorkEventHandler::CHECK_DEEPIDLE_MSG))) {
67             WS_HILOGW("has send check deep idle msg, ignore");
68             return;
69         }
70         eventHandler->RemoveEvent(WorkEventHandler::CHECK_DEEPIDLE_MSG);
71         eventHandler->SendEvent(
72             AppExecFwk::InnerEvent::Get(WorkEventHandler::CHECK_DEEPIDLE_MSG, 0), MIN_DEEP_IDLE_SCREEN_OFF_TIME_MIN);
73     }
74 }
75 
ScreenListener(std::shared_ptr<WorkQueueManager> workQueueManager,std::shared_ptr<WorkSchedulerService> service)76 ScreenListener::ScreenListener(std::shared_ptr<WorkQueueManager> workQueueManager,
77     std::shared_ptr<WorkSchedulerService> service)
78 {
79     workQueueManager_ = workQueueManager;
80     service_ = service;
81 }
82 
~ScreenListener()83 ScreenListener::~ScreenListener()
84 {
85     this->Stop();
86 }
87 
CreateScreenEventSubscriber(ScreenListener & listener)88 std::shared_ptr<EventFwk::CommonEventSubscriber> CreateScreenEventSubscriber(ScreenListener &listener)
89 {
90     EventFwk::MatchingSkills skill = EventFwk::MatchingSkills();
91     skill.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED);
92     skill.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
93     EventFwk::CommonEventSubscribeInfo info(skill);
94     return std::make_shared<ScreenEventSubscriber>(info, listener);
95 }
96 
Start()97 bool ScreenListener::Start()
98 {
99     WS_HILOGD("screen listener start.");
100     this->commonEventSubscriber = CreateScreenEventSubscriber(*this);
101     return EventFwk::CommonEventManager::SubscribeCommonEvent(this->commonEventSubscriber);
102 }
103 
Stop()104 bool ScreenListener::Stop()
105 {
106     WS_HILOGD("screen listener stop.");
107     if (this->commonEventSubscriber != nullptr) {
108         bool result = EventFwk::CommonEventManager::UnSubscribeCommonEvent(this->commonEventSubscriber);
109         if (result) {
110             this->commonEventSubscriber = nullptr;
111         }
112         return result;
113     }
114     return true;
115 }
116 
OnConditionChanged(WorkCondition::Type conditionType,std::shared_ptr<DetectorValue> conditionVal)117 void ScreenListener::OnConditionChanged(WorkCondition::Type conditionType,
118     std::shared_ptr<DetectorValue> conditionVal)
119 {
120     if (workQueueManager_ != nullptr) {
121         workQueueManager_->OnConditionChanged(conditionType, conditionVal);
122     } else {
123         WS_HILOGE("workQueueManager_ is nullptr.");
124     }
125 }
126 } // namespace WorkScheduler
127 } // namespace OHOS