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 #ifndef POWERMGR_POWER_MGR_MONITOR_H 17 #define POWERMGR_POWER_MGR_MONITOR_H 18 19 #include <functional> 20 #include <map> 21 #include <memory> 22 #include <string> 23 24 #include <common_event_subscriber.h> 25 #include <ohos/aafwk/content/want.h> 26 27 using IntentWant = OHOS::AAFwk::Want; 28 using Ces = OHOS::EventFwk::CommonEventSubscriber; 29 using CesInfo = OHOS::EventFwk::CommonEventSubscribeInfo; 30 using EventHandle = std::function<void(const IntentWant&)>; 31 32 namespace OHOS { 33 namespace PowerMgr { 34 class PowerMgrMonitor { 35 public: 36 PowerMgrMonitor() = default; 37 ~PowerMgrMonitor(); 38 39 bool Start(); 40 41 private: 42 typedef void (PowerMgrMonitor::*HandleEventFunc)(const IntentWant&) const; 43 44 class EventSubscriber : public Ces { 45 public: EventSubscriber(const sptr<CesInfo> & info,const std::map<std::string,EventHandle> & handles)46 EventSubscriber(const sptr<CesInfo>& info, const std::map<std::string, EventHandle>& handles) 47 : Ces(*info), eventHandles_(handles) {} OnReceiveEvent(const EventFwk::CommonEventData & data)48 void OnReceiveEvent(const EventFwk::CommonEventData &data) override 49 { 50 HandleEvent(data.GetWant()); 51 } 52 ~EventSubscriber() override = default; 53 54 private: 55 void HandleEvent(const IntentWant& want); 56 57 const std::map<std::string, EventHandle>& eventHandles_; 58 }; 59 60 void InitEventHandles(); 61 sptr<CesInfo> GetSubscribeInfo() const; 62 bool RegisterSubscriber(const sptr<CesInfo>& info); 63 void HandleScreenStateChanged(const IntentWant& want) const; 64 void HandleStartUpCompleted(const IntentWant& want) const; 65 66 static const std::map<std::string, HandleEventFunc> EVENT_HANDLES; 67 std::shared_ptr<Ces> subscriber_; 68 std::map<std::string, EventHandle> eventHandles_; 69 }; 70 } // namespace PowerMgr 71 } // namespace OHOS 72 #endif // POWERMGR_POWER_MGR_MONITOR_H 73