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