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