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