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