1 /*
2 * Copyright (c) 2025-2026 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 #include "broadcast_proxy.h"
17 #include "iservice_registry.h"
18 #include "system_ability_definition.h"
19 #include "netsys_controller.h"
20 #include "sim_state_type.h"
21 #include "networkslicemsgcenter.h"
22
23 namespace OHOS {
24 namespace NetManagerStandard {
25 static const int32_t DEFAULT_VALUE = -1;
26
broadcast_proxy()27 broadcast_proxy::broadcast_proxy()
28 {
29 InitBroadCastHandleMap();
30 Subscribe();
31 }
32
~broadcast_proxy()33 broadcast_proxy::~broadcast_proxy()
34 {
35 UnSubscribe();
36 }
37
InitBroadCastHandleMap()38 void broadcast_proxy::InitBroadCastHandleMap()
39 {
40 systemEventHandleMap_ = {
41 {EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON, &broadcast_proxy::HandleScreenOnEvent},
42 {EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF, &broadcast_proxy::HandleScreenOffEvent},
43 {EventFwk::CommonEventSupport::COMMON_EVENT_WIFI_CONN_STATE, &broadcast_proxy::HandleWifiConnEvent},
44 {EventFwk::CommonEventSupport::COMMON_EVENT_SIM_STATE_CHANGED, &broadcast_proxy::HandleSimStateEvent},
45 {EventFwk::CommonEventSupport::COMMON_EVENT_AIRPLANE_MODE_CHANGED, &broadcast_proxy::HandleAirPlaneModeEvent},
46 {EventFwk::CommonEventSupport::COMMON_EVENT_NETWORK_STATE_CHANGED, &broadcast_proxy::HandleNetWorkStateChanged},
47 {EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE, &broadcast_proxy::HandleConnectivityChanged},
48 };
49 }
50
Subscribe()51 void broadcast_proxy::Subscribe()
52 {
53 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
54 if (samgrProxy == nullptr) {
55 NETMGR_EXT_LOG_E("samgrProxy is nullptr");
56 return;
57 }
58 statusChangeListener_ = new (std::nothrow) SystemAbilityListener();
59 if (statusChangeListener_ == nullptr) {
60 NETMGR_EXT_LOG_E("statusChangeListener_ is nullptr");
61 return;
62 }
63 samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_);
64 samgrProxy->SubscribeSystemAbility(APP_MGR_SERVICE_ID, statusChangeListener_);
65 samgrProxy->SubscribeSystemAbility(COMM_VPN_MANAGER_SYS_ABILITY_ID, statusChangeListener_);
66 samgrProxy->SubscribeSystemAbility(WIFI_DEVICE_SYS_ABILITY_ID, statusChangeListener_);
67 samgrProxy->SubscribeSystemAbility(COMM_NETSYS_NATIVE_SYS_ABILITY_ID, statusChangeListener_);
68 }
69
UnSubscribe()70 void broadcast_proxy::UnSubscribe()
71 {
72 NETMGR_EXT_LOG_E("UnSubscribe enter");
73 if (statusChangeListener_ != nullptr) {
74 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
75 if (samgrProxy != nullptr) {
76 samgrProxy->UnSubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_);
77 samgrProxy->UnSubscribeSystemAbility(APP_MGR_SERVICE_ID, statusChangeListener_);
78 samgrProxy->UnSubscribeSystemAbility(COMM_VPN_MANAGER_SYS_ABILITY_ID, statusChangeListener_);
79 samgrProxy->UnSubscribeSystemAbility(WIFI_DEVICE_SYS_ABILITY_ID, statusChangeListener_);
80 samgrProxy->UnSubscribeSystemAbility(COMM_NETSYS_NATIVE_SYS_ABILITY_ID, statusChangeListener_);
81 }
82 statusChangeListener_ = nullptr;
83 }
84 }
85
SubscribeCommonEvent()86 void broadcast_proxy::SubscribeCommonEvent()
87 {
88 EventFwk::MatchingSkills matchSkills;
89 for (const auto &e : systemEventHandleMap_) {
90 matchSkills.AddEvent(e.first);
91 }
92 EventFwk::CommonEventSubscribeInfo subcriberInfo(matchSkills);
93 if (subscriber_ == nullptr) {
94 subscriber_ = std::make_shared<BroadcastEventSubscriber>(subcriberInfo);
95 }
96 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_)) {
97 NETMGR_EXT_LOG_E("system event register fail.");
98 }
99 }
100
UnSubscribeCommonEvent()101 void broadcast_proxy::UnSubscribeCommonEvent()
102 {
103 if (subscriber_ == nullptr) {
104 return;
105 }
106 if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_)) {
107 NETMGR_EXT_LOG_E("system event unregister fail.");
108 }
109 subscriber_ = nullptr;
110 }
111
SubscribeApplicationState()112 void broadcast_proxy::SubscribeApplicationState()
113 {
114 if (appAwareObserver_ == nullptr) {
115 appAwareObserver_ = new (std::nothrow) AppAwareObserver();
116 }
117 if (appAwareObserver_ == nullptr) {
118 NETMGR_EXT_LOG_E("failed to create appAwareObserver_ instance");
119 return;
120 }
121 sptr<ISystemAbilityManager> samgrClient = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
122 if (samgrClient == nullptr) {
123 NETMGR_EXT_LOG_E("GetSystemAbilityManager failed!");
124 return;
125 }
126
127 sptr<AppExecFwk::IAppMgr> iAppMgr =
128 iface_cast<AppExecFwk::IAppMgr>(samgrClient->GetSystemAbility(APP_MGR_SERVICE_ID));
129 if (iAppMgr == nullptr) {
130 NETMGR_EXT_LOG_E("Subscribe:iAppMgr SA not ready, wait for the SA Added callback");
131 return;
132 }
133 iAppMgr->RegisterApplicationStateObserver(appAwareObserver_);
134 }
135
UnSubscribeApplicationState()136 void broadcast_proxy::UnSubscribeApplicationState()
137 {
138 if (appAwareObserver_ == nullptr) {
139 return;
140 }
141 sptr<ISystemAbilityManager> samgrClient = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
142 if (samgrClient == nullptr) {
143 NETMGR_EXT_LOG_E("GetSystemAbilityManager failed!");
144 return;
145 }
146
147 sptr<AppExecFwk::IAppMgr> iAppMgr =
148 iface_cast<AppExecFwk::IAppMgr>(samgrClient->GetSystemAbility(APP_MGR_SERVICE_ID));
149 if (iAppMgr != nullptr) {
150 iAppMgr->UnregisterApplicationStateObserver(appAwareObserver_);
151 }
152 appAwareObserver_ = nullptr;
153 }
154
RegisterVpnEventCallback()155 void broadcast_proxy::SystemAbilityListener::RegisterVpnEventCallback()
156 {
157 NETMGR_EXT_LOG_E("RegisterVpnEventCallback start.");
158 if (vpnEventObserver_ == nullptr) {
159 vpnEventObserver_ = new (std::nothrow) broadcast_proxy::VpnEventObserver();
160 }
161 if (NetManagerStandard::NetworkVpnClient::GetInstance().RegisterVpnEvent(vpnEventObserver_) != 0) {
162 NETMGR_EXT_LOG_E("vpn event observer register failed");
163 }
164 NETMGR_EXT_LOG_E("RegisterVpnEventCallback success.");
165 }
166
RegisterDnsResultCallback()167 void broadcast_proxy::SystemAbilityListener::RegisterDnsResultCallback()
168 {
169 NETMGR_EXT_LOG_E("RegisterDnsResultCallback start.");
170 if (dnsResultCallback_ == nullptr) {
171 dnsResultCallback_ = std::make_unique<DnsResultCallback>().release();
172 NetsysController::GetInstance().RegisterDnsResultCallback(dnsResultCallback_, 0);
173 }
174 NETMGR_EXT_LOG_E("RegisterVpnEventCallback success.");
175 }
176
OnVpnStateChanged(const bool & isConnected)177 void broadcast_proxy::VpnEventObserver::OnVpnStateChanged(const bool &isConnected)
178 {
179 NETMGR_EXT_LOG_E("vpn state changed. cur state: %{public}d", isConnected);
180 Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_VPN_MODE_CHANGED, isConnected);
181 }
182
HandleSystemEvent(const EventFwk::CommonEventData & eventData)183 void broadcast_proxy::HandleSystemEvent(const EventFwk::CommonEventData& eventData)
184 {
185 auto action = eventData.GetWant().GetAction();
186 auto it = systemEventHandleMap_.find(action);
187 if (it == systemEventHandleMap_.end()) {
188 return;
189 }
190 auto handleFunc = it->second;
191 if (handleFunc != nullptr) {
192 (this->*handleFunc)(eventData);
193 }
194 }
195
HandleScreenOnEvent(const EventFwk::CommonEventData & eventData)196 void broadcast_proxy::HandleScreenOnEvent(const EventFwk::CommonEventData& eventData)
197 {
198 NETMGR_EXT_LOG_E("HandleScreenOnEvent");
199 Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_SCREEN_ON);
200 }
201
HandleScreenOffEvent(const EventFwk::CommonEventData & eventData)202 void broadcast_proxy::HandleScreenOffEvent(const EventFwk::CommonEventData& eventData)
203 {
204 NETMGR_EXT_LOG_E("HandleScreenOffEvent");
205 Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_SCREEN_OFF);
206 }
207
HandleWifiConnEvent(const EventFwk::CommonEventData & eventData)208 void broadcast_proxy::HandleWifiConnEvent(const EventFwk::CommonEventData& eventData)
209 {
210 int32_t state = eventData.GetCode();
211 if (state == WifiConnected || state == WifiDisconnected) {
212 Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_WIFI_CONN_CHANGED, state);
213 }
214 NETMGR_EXT_LOG_E("HandleWifiConnEvent, state:%{public}d", state);
215 }
216
HandleAirPlaneModeEvent(const EventFwk::CommonEventData & eventData)217 void broadcast_proxy::HandleAirPlaneModeEvent(const EventFwk::CommonEventData& eventData)
218 {
219 int32_t state = eventData.GetCode();
220 NETMGR_EXT_LOG_E("HandleAirPlaneModeEvent, status:%{public}d", state);
221 Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_AIR_MODE_CHANGED, state);
222 }
223
HandleNetWorkStateChanged(const EventFwk::CommonEventData & eventData)224 void broadcast_proxy::HandleNetWorkStateChanged(const EventFwk::CommonEventData &eventData)
225 {
226 NETMGR_EXT_LOG_I("Networkslice HandleNetWorkStateChanged");
227 auto want = eventData.GetWant();
228 if (want.GetAction() != EventFwk::CommonEventSupport::COMMON_EVENT_NETWORK_STATE_CHANGED) {
229 NETMGR_EXT_LOG_E("Common Event NetWorkStateChanged Error");
230 return;
231 }
232 auto networkStandard = std::make_shared<NetworkStandard>();
233 networkStandard->slotId = want.GetIntParam("slotId", -1);
234 networkStandard->networkState = want.GetStringParam("networkState");
235 NETMGR_EXT_LOG_E("Networkslice slot = %{public}d, nwState : %{public}s",
236 networkStandard->slotId, networkStandard->networkState.c_str());
237 Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_NETWORK_STATE_CHANGED, networkStandard);
238 }
239
HandleConnectivityChanged(const EventFwk::CommonEventData & eventData)240 void broadcast_proxy::HandleConnectivityChanged(const EventFwk::CommonEventData &eventData)
241 {
242 NETMGR_EXT_LOG_E("HandleConnectivityChanged");
243 Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_CONNECTIVITY_CHANGE);
244 }
245
OnReceiveEvent(const EventFwk::CommonEventData & data)246 void broadcast_proxy::BroadcastEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData& data)
247 {
248 if (!broadcastFfrtQueue_) {
249 NETMGR_EXT_LOG_E("broadcastFfrtQueue is null");
250 return;
251 }
252 auto taskFunc = [data]() { DelayedSingleton<broadcast_proxy>::GetInstance()->HandleSystemEvent(data); };
253 broadcastFfrtQueue_->submit(taskFunc);
254 }
255
OnForegroundApplicationChanged(const AppExecFwk::AppStateData & appStateData)256 void broadcast_proxy::AppAwareObserver::OnForegroundApplicationChanged(const AppExecFwk::AppStateData& appStateData)
257 {
258 NETMGR_EXT_LOG_I("AppAwareObserver::OnForegroundApplicationChanged ");
259 int32_t uid = appStateData.uid;
260 int32_t pid = appStateData.pid;
261 int32_t state = appStateData.state;
262 std::string bundleName = appStateData.bundleName;
263 bool isFocused = appStateData.isFocused;
264 NETMGR_EXT_LOG_I("uid:%{public}d, pid:%{public}d, state:%{public}d, isFocused:%{public}d, %{public}s",
265 uid, pid, state, isFocused, bundleName.c_str());
266 if ((state == static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOREGROUND) && !isFocused) ||
267 (state == static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_BACKGROUND))) {
268 return;
269 }
270 if (state == static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOREGROUND) &&
271 lastAppStateData.bundleName != bundleName) {
272 if (lastAppStateData.bundleName != "") {
273 lastAppStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_BACKGROUND);
274 NETMGR_EXT_LOG_I("publish lastAppStateData, bundleName: %{public}s, %{public}d",
275 lastAppStateData.bundleName.c_str(), lastAppStateData.state);
276 auto callback = std::make_shared<AppExecFwk::AppStateData>(lastAppStateData);
277 }
278 lastAppStateData = appStateData;
279 }
280 NETMGR_EXT_LOG_I("NgrExt publish bundleName: %{public}s, %{public}d", bundleName.c_str(), state);
281 std::shared_ptr<AppExecFwk::AppStateData> msg = std::make_shared<AppExecFwk::AppStateData>(appStateData);
282 Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_FOREGROUND_APP_CHANGED, msg);
283 }
284
HandleSimStateEvent(const EventFwk::CommonEventData & eventData)285 void broadcast_proxy::HandleSimStateEvent(const EventFwk::CommonEventData& eventData)
286 {
287 NETMGR_EXT_LOG_I("HandleSimStateEvent start");
288 auto want = eventData.GetWant();
289 if (want.GetAction() != EventFwk::CommonEventSupport::COMMON_EVENT_SIM_STATE_CHANGED) {
290 NETMGR_EXT_LOG_E("Common Event CellDataStateChanged Dispached Error");
291 return;
292 }
293 auto simState = std::make_shared<SimState>();
294 simState->slotId = want.GetIntParam("slotId", DEFAULT_VALUE);
295 simState->simStatus = want.GetIntParam("state", DEFAULT_VALUE);
296 NETMGR_EXT_LOG_I("Netmgr HandleSimStateEvent: slotId:%{public}d, simStatus:%{public}d",
297 simState->slotId, simState->simStatus);
298 if (simState->simStatus == (int)OHOS::Telephony::SimState::SIM_STATE_LOADED) {
299 Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_HANDLE_SIM_STATE_CHANGED, simState);
300 }
301 }
302
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)303 void broadcast_proxy::SystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
304 {
305 NETMGR_EXT_LOG_E("OnAddSystemAbility: systemAbilityId:%{public}d", systemAbilityId);
306 switch (systemAbilityId) {
307 case COMMON_EVENT_SERVICE_ID:
308 DelayedSingleton<broadcast_proxy>::GetInstance()->SubscribeCommonEvent();
309 break;
310 case APP_MGR_SERVICE_ID:
311 DelayedSingleton<broadcast_proxy>::GetInstance()->SubscribeApplicationState();
312 break;
313 case COMM_VPN_MANAGER_SYS_ABILITY_ID:
314 RegisterVpnEventCallback();
315 break;
316 case COMM_NETSYS_NATIVE_SYS_ABILITY_ID:
317 RegisterDnsResultCallback();
318 break;
319 default:
320 break;
321 }
322 }
323
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)324 void broadcast_proxy::SystemAbilityListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
325 {
326 NETMGR_EXT_LOG_I("OnRemoveSystemAbility: systemAbilityId:%{public}d", systemAbilityId);
327 }
328
329 }
330 }
331