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 THERMAL_COMMON_EVENT_RECEIVER_H 17 #define THERMAL_COMMON_EVENT_RECEIVER_H 18 19 #include <functional> 20 #include <map> 21 #include <memory> 22 #include <string> 23 24 #include <common_event_subscriber.h> 25 #include <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 OHOS::EventFwk::CommonEventData &data)>; 31 32 namespace OHOS { 33 namespace PowerMgr { 34 class ThermalCommonEventReceiver { 35 public: 36 ThermalCommonEventReceiver() = default; 37 ~ThermalCommonEventReceiver(); 38 39 bool Start(std::string eventName, EventHandle callback); 40 41 #ifndef THERMAL_OBSERVER_UT_TEST 42 private: 43 #endif 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) : Ces(*info), 47 eventHandles_(handles) {} OnReceiveEvent(const EventFwk::CommonEventData & data)48 void OnReceiveEvent(const EventFwk::CommonEventData &data) override 49 { 50 HandleEvent(data); 51 } 52 ~EventSubscriber() override = default; 53 private: 54 void HandleEvent(const OHOS::EventFwk::CommonEventData &data); 55 56 const std::map<std::string, EventHandle>& eventHandles_; 57 }; 58 59 void InitEventHandles(std::string eventName, EventHandle callback); 60 sptr<CesInfo> GetSubscribeInfo() const; 61 bool RegisterSubscriber(const sptr<CesInfo>& info); 62 void HandleEventChanged(const OHOS::EventFwk::CommonEventData &data) const; 63 void HandleBatteryChargerCompleted(const OHOS::EventFwk::CommonEventData &data) const; 64 std::shared_ptr<Ces> subscriber_; 65 std::map<std::string, EventHandle> eventHandles_; 66 }; 67 } // namespace PowerMgr 68 } // namespace OHOS 69 70 #endif // THERMAL_COMMON_EVENT_RECEIVER_H 71