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 void AddEvent(std::string eventName, EventHandle callback); 40 bool Register(); 41 42 #ifndef THERMAL_OBSERVER_UT_TEST 43 private: 44 #endif 45 class EventSubscriber : public Ces { 46 public: EventSubscriber(const sptr<CesInfo> & info,const std::map<std::string,EventHandle> & handles)47 EventSubscriber(const sptr<CesInfo>& info, const std::map<std::string, EventHandle>& handles) : Ces(*info), 48 eventHandles_(handles) {} OnReceiveEvent(const EventFwk::CommonEventData & data)49 void OnReceiveEvent(const EventFwk::CommonEventData &data) override 50 { 51 HandleEvent(data); 52 } 53 ~EventSubscriber() override = default; 54 private: 55 void HandleEvent(const OHOS::EventFwk::CommonEventData &data); 56 57 const std::map<std::string, EventHandle>& eventHandles_; 58 }; 59 60 void InitEventHandles(std::string eventName, EventHandle callback); 61 sptr<CesInfo> GetSubscribeInfo() const; 62 bool RegisterSubscriber(const sptr<CesInfo>& info); 63 void HandleEventChanged(const OHOS::EventFwk::CommonEventData &data) const; 64 void HandleBatteryChargerCompleted(const OHOS::EventFwk::CommonEventData &data) const; 65 std::shared_ptr<Ces> subscriber_; 66 std::map<std::string, EventHandle> eventHandles_; 67 }; 68 } // namespace PowerMgr 69 } // namespace OHOS 70 71 #endif // THERMAL_COMMON_EVENT_RECEIVER_H 72