1 /* 2 * Copyright (c) 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_PUBLISH_COMMON_EVENT_H 17 #define OHOS_PUBLISH_COMMON_EVENT_H 18 19 #include "common_event_manager.h" 20 #include "system_ability_status_change_stub.h" 21 22 #include "dm_constants.h" 23 24 namespace OHOS { 25 namespace DistributedHardware { 26 using OHOS::EventFwk::CommonEventData; 27 using OHOS::EventFwk::CommonEventSubscriber; 28 using OHOS::EventFwk::CommonEventSubscribeInfo; 29 using PublishEventCallback = std::function<void(int32_t, int32_t, int32_t)>; 30 31 class DmPublishEventSubscriber : public CommonEventSubscriber { 32 public: DmPublishEventSubscriber(const CommonEventSubscribeInfo & subscribeInfo,const PublishEventCallback & callback,const std::vector<std::string> & eventNameVec)33 DmPublishEventSubscriber(const CommonEventSubscribeInfo &subscribeInfo, const PublishEventCallback &callback, 34 const std::vector<std::string> &eventNameVec) : CommonEventSubscriber(subscribeInfo), 35 eventNameVec_(eventNameVec), callback_(callback) {} 36 ~DmPublishEventSubscriber() override = default; 37 std::vector<std::string> GetSubscriberEventNameVec() const; 38 void OnReceiveEvent(const CommonEventData &data) override; 39 void SetWifiState(const int32_t wifiState); 40 int32_t GetWifiState(); 41 void SetBluetoothState(const int32_t bluetoothState); 42 int32_t GetBluetoothState(); 43 void SetScreenState(const int32_t screenState); 44 int32_t GetScreenState(); 45 46 private: 47 void SetScreenEventState(const std::string &receiveEvent); 48 std::vector<std::string> eventNameVec_; 49 int32_t wifiState_ { -1 }; 50 int32_t bluetoothState_ { -1 }; 51 int32_t screenState_ = DM_SCREEN_UNKNOWN; 52 PublishEventCallback callback_; 53 std::mutex wifiStateMutex_; 54 std::mutex bluetoothStateMutex_; 55 std::mutex screenStateMutex_; 56 }; 57 58 class DmPublishCommonEventManager { 59 public: 60 DmPublishCommonEventManager() = default; 61 ~DmPublishCommonEventManager(); 62 bool SubscribePublishCommonEvent(const std::vector<std::string> &eventNameVec, 63 const PublishEventCallback &callback); 64 bool UnsubscribePublishCommonEvent(); 65 void SetSubscriber(std::shared_ptr<DmPublishEventSubscriber> subscriber); 66 std::shared_ptr<DmPublishEventSubscriber> GetSubscriber(); 67 68 private: 69 std::vector<std::string> eventNameVec_; 70 bool eventValidFlag_ = false; 71 std::mutex evenSubscriberMutex_; 72 std::mutex subscriberMutex_; 73 std::shared_ptr<DmPublishEventSubscriber> subscriber_ = nullptr; 74 sptr<ISystemAbilityStatusChange> statusChangeListener_ = nullptr; 75 int32_t counter_ = 0; 76 77 private: 78 class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub { 79 public: SystemAbilityStatusChangeListener(std::shared_ptr<DmPublishEventSubscriber> publishSubscriber)80 explicit SystemAbilityStatusChangeListener(std::shared_ptr<DmPublishEventSubscriber> publishSubscriber) 81 : changeSubscriber_(publishSubscriber) {} 82 ~SystemAbilityStatusChangeListener() = default; 83 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 84 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 85 86 private: 87 std::shared_ptr<DmPublishEventSubscriber> changeSubscriber_; 88 }; 89 }; 90 } // namespace DistributedHardware 91 } // namespace OHOS 92 #endif // OHOS_PUBLISH_COMMON_EVENT_H 93