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