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 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) {
43 listener_.service_->GetHandler()->RemoveEvent(WorkEventHandler::CHECK_DEEPIDLE_MSG);
44 listener_.OnConditionChanged(WorkCondition::Type::DEEP_IDLE,
45 std::make_shared<DetectorValue>(0, 0, false, std::string()));
46 auto task = [weak = weak_from_this()]() {
47 auto strong = weak.lock();
48 if (!strong) {
49 WS_HILOGE("ScreenEventSubscriber::OnReceiveEvent strong is null");
50 return;
51 }
52 int32_t ret = strong->listener_.service_->StopDeepIdleWorks();
53 if (ret != ERR_OK) {
54 WS_HILOGE("stop work after unlocking failed, error code:%{public}d.", ret);
55 } else {
56 WS_HILOGI("stop work after unlocking successed.");
57 }
58 };
59 auto handler = DelayedSingleton<WorkSchedulerService>::GetInstance()->GetHandler();
60 if (handler) {
61 handler->PostTask(task);
62 }
63 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
64 listener_.service_->GetHandler()->RemoveEvent(WorkEventHandler::CHECK_DEEPIDLE_MSG);
65 listener_.service_->GetHandler()->SendEvent(
66 AppExecFwk::InnerEvent::Get(WorkEventHandler::CHECK_DEEPIDLE_MSG, 0), MIN_DEEP_IDLE_SCREEN_OFF_TIME_MIN);
67 }
68 }
69
ScreenListener(std::shared_ptr<WorkQueueManager> workQueueManager,std::shared_ptr<WorkSchedulerService> service)70 ScreenListener::ScreenListener(std::shared_ptr<WorkQueueManager> workQueueManager,
71 std::shared_ptr<WorkSchedulerService> service)
72 {
73 workQueueManager_ = workQueueManager;
74 service_ = service;
75 }
76
~ScreenListener()77 ScreenListener::~ScreenListener()
78 {
79 this->Stop();
80 }
81
CreateScreenEventSubscriber(ScreenListener & listener)82 std::shared_ptr<EventFwk::CommonEventSubscriber> CreateScreenEventSubscriber(ScreenListener &listener)
83 {
84 EventFwk::MatchingSkills skill = EventFwk::MatchingSkills();
85 skill.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED);
86 skill.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
87 EventFwk::CommonEventSubscribeInfo info(skill);
88 return std::make_shared<ScreenEventSubscriber>(info, listener);
89 }
90
Start()91 bool ScreenListener::Start()
92 {
93 WS_HILOGD("screen listener start.");
94 this->commonEventSubscriber = CreateScreenEventSubscriber(*this);
95 return EventFwk::CommonEventManager::SubscribeCommonEvent(this->commonEventSubscriber);
96 }
97
Stop()98 bool ScreenListener::Stop()
99 {
100 WS_HILOGD("screen listener stop.");
101 if (this->commonEventSubscriber != nullptr) {
102 bool result = EventFwk::CommonEventManager::UnSubscribeCommonEvent(this->commonEventSubscriber);
103 if (result) {
104 this->commonEventSubscriber = nullptr;
105 }
106 return result;
107 }
108 return true;
109 }
110
OnConditionChanged(WorkCondition::Type conditionType,std::shared_ptr<DetectorValue> conditionVal)111 void ScreenListener::OnConditionChanged(WorkCondition::Type conditionType,
112 std::shared_ptr<DetectorValue> conditionVal)
113 {
114 if (workQueueManager_ != nullptr) {
115 workQueueManager_->OnConditionChanged(conditionType, conditionVal);
116 } else {
117 WS_HILOGE("workQueueManager_ is nullptr.");
118 }
119 }
120 } // namespace WorkScheduler
121 } // namespace OHOS