• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
16 #include "power_mgr_monitor.h"
17 
18 #include <common_event_manager.h>
19 #include <common_event_support.h>
20 #include <unistd.h>
21 
22 #include "power_common.h"
23 #include "power_mgr_service.h"
24 #include "power_state_machine.h"
25 
26 using namespace OHOS::AAFwk;
27 using namespace OHOS::EventFwk;
28 using namespace std;
29 
30 namespace OHOS {
31 namespace PowerMgr {
32 const map<string, PowerMgrMonitor::HandleEventFunc> PowerMgrMonitor::EVENT_HANDLES {
33     {CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED, &PowerMgrMonitor::HandleStartUpCompleted},
34 };
35 
~PowerMgrMonitor()36 PowerMgrMonitor::~PowerMgrMonitor()
37 {
38     if (subscriber_ != nullptr) {
39         CommonEventManager::UnSubscribeCommonEvent(subscriber_);
40     }
41 }
42 
Start()43 bool PowerMgrMonitor::Start()
44 {
45     InitEventHandles();
46     return RegisterSubscriber(GetSubscribeInfo());
47 }
48 
InitEventHandles()49 void PowerMgrMonitor::InitEventHandles()
50 {
51     if (!eventHandles_.empty()) {
52         return;
53     }
54     for (auto &eh : EVENT_HANDLES) {
55         POWER_HILOGI(MODULE_SERVICE, "Add event: %{public}s", eh.first.c_str());
56         eventHandles_.emplace(eh.first, bind(eh.second, this, placeholders::_1));
57     }
58 }
59 
GetSubscribeInfo() const60 sptr<CesInfo> PowerMgrMonitor::GetSubscribeInfo() const
61 {
62     MatchingSkills skill;
63     for (auto &eh : eventHandles_) {
64         skill.AddEvent(eh.first);
65     }
66     sptr<CesInfo> info = new CesInfo(skill);
67     return info;
68 }
69 
RegisterSubscriber(const sptr<CesInfo> & info)70 bool PowerMgrMonitor::RegisterSubscriber(const sptr<CesInfo>& info)
71 {
72     static const int32_t MAX_RETRY_TIMES = 2;
73 
74     auto succeed = false;
75     shared_ptr<Ces> s = make_shared<EventSubscriber>(info, eventHandles_);
76     // Retry to register subscriber due to timming issues between system abilities
77     for (int32_t tryTimes = 0; tryTimes < MAX_RETRY_TIMES; tryTimes++) {
78         succeed = CommonEventManager::SubscribeCommonEvent(s);
79         if (succeed) {
80             break;
81         }
82         POWER_HILOGE(MODULE_SERVICE, "Sleep for a while and retry to register subscriber");
83         usleep(50000); // sleep 50ms
84     }
85     if (!succeed) {
86         POWER_HILOGE(MODULE_SERVICE, "Failed to register subscriber");
87         return false;
88     }
89     subscriber_ = s;
90     POWER_HILOGI(MODULE_SERVICE, "Succeed to register subscriber");
91     return true;
92 }
93 
HandleScreenStateChanged(const IntentWant & want) const94 void PowerMgrMonitor::HandleScreenStateChanged(const IntentWant& want) const
95 {
96     bool isScreenOn = want.GetAction() == CommonEventSupport::COMMON_EVENT_SCREEN_ON;
97     POWER_HILOGD(MODULE_SERVICE, "Screen is %{public}s", isScreenOn ? "on" : "off");
98     DelayedSpSingleton<PowerMgrService>::GetInstance()->GetPowerStateMachine()->ReceiveScreenEvent(isScreenOn);
99 }
100 
HandleStartUpCompleted(const IntentWant & want) const101 void PowerMgrMonitor::HandleStartUpCompleted(const IntentWant& want __attribute__((__unused__))) const
102 {
103     POWER_HILOGD(MODULE_SERVICE, "Start up completed");
104     DelayedSpSingleton<PowerMgrService>::GetInstance()->GetPowerStateMachine()->ResetInactiveTimer();
105 }
106 
HandleEvent(const IntentWant & want)107 void PowerMgrMonitor::EventSubscriber::HandleEvent(const IntentWant& want)
108 {
109     auto action = want.GetAction();
110     auto it = eventHandles_.find(action);
111     if (it == eventHandles_.end()) {
112         POWER_HILOGI(MODULE_SERVICE, "Ignore event: %{public}s", action.c_str());
113         return;
114     }
115     POWER_HILOGI(MODULE_SERVICE, "Handle event: %{public}s", action.c_str());
116     it->second(want);
117 }
118 } // namespace PowerMgr
119 } // namespace OHOS
120