1 /* 2 * Copyright (c) 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 #ifndef OHOS_EVENT_MANAGER_ADAPT_H 17 #define OHOS_EVENT_MANAGER_ADAPT_H 18 19 #include <functional> 20 #include <map> 21 #include <mutex> 22 23 #include "common_event_data.h" 24 #include "common_event_manager.h" 25 #include "common_event_subscribe_info.h" 26 #include "common_event_subscriber.h" 27 #include "dm_log.h" 28 #include "matching_skills.h" 29 #include "single_instance.h" 30 #include "system_ability_status_change_stub.h" 31 32 namespace OHOS { 33 namespace DistributedHardware { 34 using OHOS::EventFwk::CommonEventData; 35 using OHOS::EventFwk::CommonEventSubscriber; 36 using OHOS::EventFwk::CommonEventSubscribeInfo; 37 using CommomEventCallback = std::function<void(int32_t)>; 38 39 class DmEventSubscriber : public CommonEventSubscriber { 40 public: DmEventSubscriber(const CommonEventSubscribeInfo & subscribeInfo,const CommomEventCallback & callback,const std::string & eventName)41 DmEventSubscriber(const CommonEventSubscribeInfo &subscribeInfo, const CommomEventCallback &callback, 42 const std::string &eventName) : CommonEventSubscriber(subscribeInfo), 43 eventName_(eventName), callback_(callback) {} 44 virtual ~DmEventSubscriber() = default; 45 std::string GetSubscriberEventName() const; 46 void OnReceiveEvent(const CommonEventData &data) override; 47 48 private: 49 std::string eventName_; 50 CommomEventCallback callback_; 51 }; 52 53 class DmCommonEventManager { 54 public: 55 DmCommonEventManager() = default; 56 ~DmCommonEventManager(); 57 bool SubscribeServiceEvent(const std::string &eventName, const CommomEventCallback &callback); 58 bool UnsubscribeServiceEvent(); 59 60 private: 61 std::string eventName_; 62 bool eventValidFlag_ = false; 63 std::mutex evenSubscriberMutex_; 64 std::shared_ptr<DmEventSubscriber> subscriber_ = nullptr; 65 sptr<ISystemAbilityStatusChange> statusChangeListener_ = nullptr; 66 67 private: 68 class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub { 69 public: SystemAbilityStatusChangeListener(std::shared_ptr<DmEventSubscriber> subscriber)70 explicit SystemAbilityStatusChangeListener(std::shared_ptr<DmEventSubscriber> subscriber) 71 : changeSubscriber_(subscriber) {} 72 ~SystemAbilityStatusChangeListener() = default; 73 virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 74 virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 75 76 private: 77 std::shared_ptr<DmEventSubscriber> changeSubscriber_; 78 }; 79 }; 80 } // namespace DistributedHardware 81 } // namespace OHOS 82 #endif // OHOS_EVENT_MANAGER_ADAPT_H 83