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 #include "conditions/network_listener.h"
16
17 #include "common_event_manager.h"
18 #include "common_event_support.h"
19 #include "matching_skills.h"
20 #include "want.h"
21 #ifdef COMMUNICATION_NETMANAGER_BASE_ENABLE
22 #include "net_supplier_info.h"
23 #endif
24 #include "work_sched_hilog.h"
25
26 namespace OHOS {
27 namespace WorkScheduler {
28 const int32_t DEFAULT_VALUE = -1;
29 #ifdef COMMUNICATION_NETMANAGER_BASE_ENABLE
30 const int32_t BEARER_CELLULAR = 0;
31 const int32_t BEARER_WIFI = 1;
32 const int32_t BEARER_BLUETOOTH = 2;
33 const int32_t BEARER_ETHERNET = 3;
34 const int32_t BEARER_WIFI_AWARE = 5;
35 #endif
36
NetworkEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo,NetworkListener & listener)37 NetworkEventSubscriber::NetworkEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo,
38 NetworkListener &listener) : CommonEventSubscriber(subscribeInfo), listener_(listener) {}
39
OnReceiveEvent(const EventFwk::CommonEventData & data)40 void NetworkEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
41 {
42 const std::string action = data.GetWant().GetAction();
43 WS_HILOGD("OnReceiveEvent get action: %{public}s", action.c_str());
44
45 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE) {
46 int32_t code = data.GetCode();
47 int32_t netType = data.GetWant().GetIntParam("NetType", DEFAULT_VALUE);
48 WS_HILOGI("Condition changed code:%{public}d, netType:%{public}d", code, netType);
49 #ifdef COMMUNICATION_NETMANAGER_BASE_ENABLE
50 if (code == NetManagerStandard::NetConnState::NET_CONN_STATE_CONNECTED) {
51 if (netType == DEFAULT_VALUE) {
52 return;
53 }
54 switch (netType) {
55 case BEARER_CELLULAR:
56 listener_.OnConditionChanged(WorkCondition::Type::NETWORK,
57 std::make_shared<DetectorValue>(WorkCondition::NETWORK_TYPE_MOBILE, 0, 0, std::string()));
58 break;
59 case BEARER_WIFI:
60 listener_.OnConditionChanged(WorkCondition::Type::NETWORK,
61 std::make_shared<DetectorValue>(WorkCondition::NETWORK_TYPE_WIFI, 0, 0, std::string()));
62 break;
63 case BEARER_BLUETOOTH:
64 listener_.OnConditionChanged(WorkCondition::Type::NETWORK,
65 std::make_shared<DetectorValue>(WorkCondition::NETWORK_TYPE_BLUETOOTH, 0, 0, std::string()));
66 break;
67 case BEARER_ETHERNET:
68 listener_.OnConditionChanged(WorkCondition::Type::NETWORK,
69 std::make_shared<DetectorValue>(WorkCondition::NETWORK_TYPE_ETHERNET, 0, 0, std::string()));
70 break;
71 case BEARER_WIFI_AWARE:
72 listener_.OnConditionChanged(WorkCondition::Type::NETWORK,
73 std::make_shared<DetectorValue>(WorkCondition::NETWORK_TYPE_WIFI_P2P, 0, 0, std::string()));
74 break;
75 default:
76 listener_.OnConditionChanged(WorkCondition::Type::NETWORK,
77 std::make_shared<DetectorValue>(WorkCondition::NETWORK_TYPE_ANY, 0, 0, std::string()));
78 break;
79 }
80 } else {
81 listener_.OnConditionChanged(WorkCondition::Type::NETWORK,
82 std::make_shared<DetectorValue>(WorkCondition::NETWORK_UNKNOWN, 0, 0, std::string()));
83 }
84 #else
85 listener_.OnConditionChanged(WorkCondition::Type::NETWORK,
86 std::make_shared<DetectorValue>(WorkCondition::NETWORK_UNKNOWN, 0, 0, std::string()));
87 #endif
88 }
89 }
90
CreateNetworkEventSubscriber(NetworkListener & listener)91 std::shared_ptr<EventFwk::CommonEventSubscriber> CreateNetworkEventSubscriber(NetworkListener &listener)
92 {
93 EventFwk::MatchingSkills skill = EventFwk::MatchingSkills();
94 skill.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE);
95 EventFwk::CommonEventSubscribeInfo info(skill);
96 return std::make_shared<NetworkEventSubscriber>(info, listener);
97 }
98
NetworkListener(std::shared_ptr<WorkQueueManager> workQueueManager)99 NetworkListener::NetworkListener(std::shared_ptr<WorkQueueManager> workQueueManager)
100 {
101 workQueueManager_ = workQueueManager;
102 }
103
~NetworkListener()104 NetworkListener::~NetworkListener()
105 {
106 this->Stop();
107 }
108
Start()109 bool NetworkListener::Start()
110 {
111 WS_HILOGI("NetworkListener start");
112 this->commonEventSubscriber = CreateNetworkEventSubscriber(*this);
113 return EventFwk::CommonEventManager::SubscribeCommonEvent(this->commonEventSubscriber);
114 }
115
Stop()116 bool NetworkListener::Stop()
117 {
118 WS_HILOGI("NetworkListener stop");
119 if (this->commonEventSubscriber != nullptr) {
120 bool result = EventFwk::CommonEventManager::UnSubscribeCommonEvent(this->commonEventSubscriber);
121 if (result) {
122 this->commonEventSubscriber = nullptr;
123 }
124 return result;
125 }
126 return true;
127 }
128
OnConditionChanged(WorkCondition::Type conditionType,std::shared_ptr<DetectorValue> conditionVal)129 void NetworkListener::OnConditionChanged(WorkCondition::Type conditionType,
130 std::shared_ptr<DetectorValue> conditionVal)
131 {
132 if (workQueueManager_ != nullptr) {
133 workQueueManager_->OnConditionChanged(conditionType, conditionVal);
134 } else {
135 WS_HILOGE("workQueueManager_ is nullptr.");
136 }
137 }
138 } // namespace WorkScheduler
139 } // namespace OHOS