1 /* 2 * Copyright (c) 2023 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_SYSTEM_ABILITY_MANAGER_DEVICE_SWITCH_COLLECT_H 17 #define OHOS_SYSTEM_ABILITY_MANAGER_DEVICE_SWITCH_COLLECT_H 18 19 #include <memory> 20 21 #include "common_event_subscriber.h" 22 #include "icollect_plugin.h" 23 #include "system_ability_status_change_stub.h" 24 25 namespace OHOS { 26 class DeviceSwitchCollect; 27 class SwitchEventSubscriber : public EventFwk::CommonEventSubscriber, 28 public std::enable_shared_from_this<SwitchEventSubscriber> { 29 public: SwitchEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo,const sptr<DeviceSwitchCollect> & deviceSwitchCollect)30 SwitchEventSubscriber(const EventFwk::CommonEventSubscribeInfo& subscribeInfo, 31 const sptr<DeviceSwitchCollect>& deviceSwitchCollect) 32 : EventFwk::CommonEventSubscriber(subscribeInfo), 33 deviceSwitchCollect_(deviceSwitchCollect) {}; 34 virtual ~SwitchEventSubscriber() = default; 35 int32_t SubscribeSwitchEvent(); 36 int32_t UnSubscribeSwitchEvent(); 37 void OnReceiveEvent(const EventFwk::CommonEventData& data) override; 38 private: 39 void OnReceiveWifiEvent(const EventFwk::CommonEventData& data); 40 void OnReceiveBluetoothEvent(const EventFwk::CommonEventData& data); 41 void ReportEvent(const OnDemandEvent& event); 42 std::mutex isListenEventLock_; 43 bool isListenSwitchEvent_ = false; 44 wptr<DeviceSwitchCollect> deviceSwitchCollect_; 45 }; 46 47 class CesStateListener : public SystemAbilityStatusChangeStub { 48 public: CesStateListener(const sptr<DeviceSwitchCollect> & deviceSwitchCollect)49 CesStateListener(const sptr<DeviceSwitchCollect>& deviceSwitchCollect) 50 : deviceSwitchCollect_(deviceSwitchCollect) {}; 51 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 52 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 53 private: 54 wptr<DeviceSwitchCollect> deviceSwitchCollect_; 55 }; 56 57 class DeviceSwitchCollect : public ICollectPlugin { 58 public: 59 explicit DeviceSwitchCollect(const sptr<IReport>& report); 60 ~DeviceSwitchCollect() = default; 61 void Init(const std::list<SaProfile>& saProfiles) override; 62 int32_t OnStart() override; 63 int32_t OnStop() override; 64 int32_t AddCollectEvent(const OnDemandEvent& event) override; 65 int32_t SubscribeSwitchEvent(); 66 private: 67 void InitCommonEventSubscriber(); 68 int32_t CheckSwitchEvent(const OnDemandEvent& onDemandEvent); 69 std::shared_ptr<SwitchEventSubscriber> switchEventSubscriber_; 70 sptr<CesStateListener> cesStateListener_; 71 std::atomic<bool> needListenSwitchEvent_ {false}; 72 }; 73 } // namespace OHOS 74 #endif // OHOS_SYSTEM_ABILITY_MANAGER_DEVICE_PARAM_COLLECT_H