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 NETWORK_SEARCH_DEVICE_STATE_OBSERVER_H 17 #define NETWORK_SEARCH_DEVICE_STATE_OBSERVER_H 18 19 #include "common_event_subscriber.h" 20 #include "common_event_support.h" 21 #include "common_event_manager.h" 22 #include "device_state_handler.h" 23 #include "sharing_event_callback_stub.h" 24 #include "net_all_capabilities.h" 25 #include "net_supplier_info.h" 26 #include "system_ability_status_change_stub.h" 27 28 namespace OHOS { 29 namespace Telephony { 30 using CommonEventData = OHOS::EventFwk::CommonEventData; 31 using CommonEventManager = OHOS::EventFwk::CommonEventManager; 32 using CommonEventSubscribeInfo = OHOS::EventFwk::CommonEventSubscribeInfo; 33 using CommonEventSubscriber = OHOS::EventFwk::CommonEventSubscriber; 34 using CommonEventSupport = OHOS::EventFwk::CommonEventSupport; 35 using MatchingSkills = OHOS::EventFwk::MatchingSkills; 36 using NetBearType = OHOS::NetManagerStandard::NetBearType; 37 using NetConnState = OHOS::NetManagerStandard::NetConnState; 38 39 enum DeviceStateEventIntValue { 40 COMMON_EVENT_CONNECTIVITY_CHANGE, 41 COMMON_EVENT_SCREEN_ON, 42 COMMON_EVENT_SCREEN_OFF, 43 COMMON_EVENT_POWER_SAVE_MODE_CHANGED, 44 COMMON_EVENT_CHARGING, 45 COMMON_EVENT_DISCHARGING, 46 COMMON_EVENT_UNKNOWN, 47 }; 48 49 class DeviceStateEventSubscriber : public CommonEventSubscriber { 50 public: DeviceStateEventSubscriber(const CommonEventSubscribeInfo & info)51 explicit DeviceStateEventSubscriber(const CommonEventSubscribeInfo &info) : CommonEventSubscriber(info) {} 52 ~DeviceStateEventSubscriber() = default; 53 void OnReceiveEvent(const CommonEventData &data) override; 54 void SetEventHandler(const std::shared_ptr<DeviceStateHandler> &deviceStateHandler); 55 std::shared_ptr<DeviceStateHandler> GetEventHandler(); 56 void InitEventMap(); 57 58 private: 59 DeviceStateEventIntValue GetDeviceStateEventIntValue(std::string &event) const; 60 void ProcessWifiState(const CommonEventData &data); 61 void ProcessPowerSaveMode(const CommonEventData &data); 62 63 private: 64 std::shared_ptr<DeviceStateHandler> deviceStateHandler_; 65 std::map<std::string, DeviceStateEventIntValue> deviceStateEventMapIntValues_; 66 }; 67 68 class SharingEventCallback : public NetManagerStandard::SharingEventCallbackStub { 69 public: 70 explicit SharingEventCallback(const std::shared_ptr<DeviceStateHandler> &deviceStateHandler); 71 ~SharingEventCallback() = default; 72 void OnSharingStateChanged(const bool &isRunning) override; OnInterfaceSharingStateChanged(const NetManagerStandard::SharingIfaceType & type,const std::string & iface,const NetManagerStandard::SharingIfaceState & state)73 void OnInterfaceSharingStateChanged(const NetManagerStandard::SharingIfaceType &type, const std::string &iface, 74 const NetManagerStandard::SharingIfaceState &state) override {} OnSharingUpstreamChanged(const sptr<NetManagerStandard::NetHandle> netHandle)75 void OnSharingUpstreamChanged(const sptr<NetManagerStandard::NetHandle> netHandle) override {} 76 77 private: 78 std::shared_ptr<DeviceStateHandler> handler_ = nullptr; 79 }; 80 81 class DeviceStateObserver { 82 public: 83 DeviceStateObserver() = default; 84 ~DeviceStateObserver() = default; 85 void StartEventSubscriber(const std::shared_ptr<DeviceStateHandler> &deviceStateHandler); 86 void StopEventSubscriber(); 87 88 private: 89 std::shared_ptr<DeviceStateEventSubscriber> subscriber_; 90 sptr<NetManagerStandard::ISharingEventCallback> sharingEventCallback_ = nullptr; 91 sptr<ISystemAbilityStatusChange> statusChangeListener_ = nullptr; 92 93 private: 94 class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub { 95 public: 96 SystemAbilityStatusChangeListener(std::shared_ptr<DeviceStateEventSubscriber> &subscriber, 97 sptr<NetManagerStandard::ISharingEventCallback> &callback); 98 ~SystemAbilityStatusChangeListener() = default; 99 virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 100 virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 101 102 private: 103 std::shared_ptr<DeviceStateEventSubscriber> sub_ = nullptr; 104 sptr<NetManagerStandard::ISharingEventCallback> callback_ = nullptr; 105 }; 106 }; 107 } // namespace Telephony 108 } // namespace OHOS 109 #endif // NETWORK_SEARCH_DEVICE_STATE_OBSERVER_H 110