• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ = sptr<AppAwareObserver>::MakeSptr();
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_ = sptr<broadcast_proxy::VpnEventObserver>::MakeSptr();
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(bool isConnected)177 int32_t broadcast_proxy::VpnEventObserver::OnVpnStateChanged(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     return ERR_OK;
182 }
183 
HandleSystemEvent(const EventFwk::CommonEventData & eventData)184 void broadcast_proxy::HandleSystemEvent(const EventFwk::CommonEventData& eventData)
185 {
186     auto action = eventData.GetWant().GetAction();
187     auto it = systemEventHandleMap_.find(action);
188     if (it == systemEventHandleMap_.end()) {
189         return;
190     }
191     auto handleFunc = it->second;
192     if (handleFunc != nullptr) {
193         (this->*handleFunc)(eventData);
194     }
195 }
196 
HandleScreenOnEvent(const EventFwk::CommonEventData & eventData)197 void broadcast_proxy::HandleScreenOnEvent(const EventFwk::CommonEventData& eventData)
198 {
199     NETMGR_EXT_LOG_E("HandleScreenOnEvent");
200     Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_SCREEN_ON);
201 }
202 
HandleScreenOffEvent(const EventFwk::CommonEventData & eventData)203 void broadcast_proxy::HandleScreenOffEvent(const EventFwk::CommonEventData& eventData)
204 {
205     NETMGR_EXT_LOG_E("HandleScreenOffEvent");
206     Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_SCREEN_OFF);
207 }
208 
HandleWifiConnEvent(const EventFwk::CommonEventData & eventData)209 void broadcast_proxy::HandleWifiConnEvent(const EventFwk::CommonEventData& eventData)
210 {
211     int32_t state = eventData.GetCode();
212     if (state == WifiConnected || state == WifiDisconnected) {
213         Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_WIFI_CONN_CHANGED, state);
214     }
215     NETMGR_EXT_LOG_E("HandleWifiConnEvent, state:%{public}d", state);
216 }
217 
HandleAirPlaneModeEvent(const EventFwk::CommonEventData & eventData)218 void broadcast_proxy::HandleAirPlaneModeEvent(const EventFwk::CommonEventData& eventData)
219 {
220     int32_t state = eventData.GetCode();
221     NETMGR_EXT_LOG_E("HandleAirPlaneModeEvent, status:%{public}d", state);
222     Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_AIR_MODE_CHANGED, state);
223 }
224 
HandleNetWorkStateChanged(const EventFwk::CommonEventData & eventData)225 void broadcast_proxy::HandleNetWorkStateChanged(const EventFwk::CommonEventData &eventData)
226 {
227     NETMGR_EXT_LOG_I("Networkslice HandleNetWorkStateChanged");
228     auto want = eventData.GetWant();
229     if (want.GetAction() != EventFwk::CommonEventSupport::COMMON_EVENT_NETWORK_STATE_CHANGED) {
230         NETMGR_EXT_LOG_E("Common Event NetWorkStateChanged Error");
231         return;
232     }
233     auto networkStandard = std::make_shared<NetworkStandard>();
234     networkStandard->slotId = want.GetIntParam("slotId", -1);
235     networkStandard->networkState = want.GetStringParam("networkState");
236     NETMGR_EXT_LOG_E("Networkslice slot = %{public}d, nwState : %{public}s",
237         networkStandard->slotId, networkStandard->networkState.c_str());
238     Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_NETWORK_STATE_CHANGED, networkStandard);
239 }
240 
HandleConnectivityChanged(const EventFwk::CommonEventData & eventData)241 void broadcast_proxy::HandleConnectivityChanged(const EventFwk::CommonEventData &eventData)
242 {
243     NETMGR_EXT_LOG_E("HandleConnectivityChanged");
244     Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_CONNECTIVITY_CHANGE);
245 }
246 
OnReceiveEvent(const EventFwk::CommonEventData & data)247 void broadcast_proxy::BroadcastEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData& data)
248 {
249     if (!broadcastFfrtQueue_) {
250         NETMGR_EXT_LOG_E("broadcastFfrtQueue is null");
251         return;
252     }
253     auto taskFunc = [data]() { DelayedSingleton<broadcast_proxy>::GetInstance()->HandleSystemEvent(data); };
254     broadcastFfrtQueue_->submit(taskFunc);
255 }
256 
OnForegroundApplicationChanged(const AppExecFwk::AppStateData & appStateData)257 void broadcast_proxy::AppAwareObserver::OnForegroundApplicationChanged(const AppExecFwk::AppStateData& appStateData)
258 {
259     NETMGR_EXT_LOG_I("AppAwareObserver::OnForegroundApplicationChanged ");
260     int32_t uid = appStateData.uid;
261     int32_t pid = appStateData.pid;
262     int32_t state = appStateData.state;
263     std::string bundleName = appStateData.bundleName;
264     bool isFocused = appStateData.isFocused;
265     NETMGR_EXT_LOG_I("uid:%{public}d, pid:%{public}d, state:%{public}d, isFocused:%{public}d, %{public}s",
266         uid, pid, state, isFocused, bundleName.c_str());
267     if ((state == static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOREGROUND) && !isFocused) ||
268         (state == static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_BACKGROUND))) {
269         return;
270     }
271     if (state == static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOREGROUND) &&
272         lastAppStateData.bundleName != bundleName) {
273         if (lastAppStateData.bundleName != "") {
274             lastAppStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_BACKGROUND);
275             NETMGR_EXT_LOG_I("publish lastAppStateData, bundleName: %{public}s, %{public}d",
276                 lastAppStateData.bundleName.c_str(), lastAppStateData.state);
277             auto callback = std::make_shared<AppExecFwk::AppStateData>(lastAppStateData);
278         }
279         lastAppStateData = appStateData;
280     }
281     NETMGR_EXT_LOG_I("NgrExt publish bundleName: %{public}s, %{public}d", bundleName.c_str(), state);
282     std::shared_ptr<AppExecFwk::AppStateData> msg = std::make_shared<AppExecFwk::AppStateData>(appStateData);
283     Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_FOREGROUND_APP_CHANGED, msg);
284 }
285 
HandleSimStateEvent(const EventFwk::CommonEventData & eventData)286 void broadcast_proxy::HandleSimStateEvent(const EventFwk::CommonEventData& eventData)
287 {
288     NETMGR_EXT_LOG_I("HandleSimStateEvent start");
289     auto want = eventData.GetWant();
290     if (want.GetAction() != EventFwk::CommonEventSupport::COMMON_EVENT_SIM_STATE_CHANGED) {
291         NETMGR_EXT_LOG_E("Common Event CellDataStateChanged Dispached Error");
292         return;
293     }
294     auto simState = std::make_shared<SimState>();
295     simState->slotId = want.GetIntParam("slotId", DEFAULT_VALUE);
296     simState->simStatus = want.GetIntParam("state", DEFAULT_VALUE);
297     NETMGR_EXT_LOG_I("Netmgr HandleSimStateEvent: slotId:%{public}d, simStatus:%{public}d",
298         simState->slotId, simState->simStatus);
299     if (simState->simStatus == (int)OHOS::Telephony::SimState::SIM_STATE_LOADED) {
300         Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_HANDLE_SIM_STATE_CHANGED, simState);
301     }
302 }
303 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)304 void broadcast_proxy::SystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
305 {
306     NETMGR_EXT_LOG_E("OnAddSystemAbility: systemAbilityId:%{public}d", systemAbilityId);
307     switch (systemAbilityId) {
308         case COMMON_EVENT_SERVICE_ID:
309             DelayedSingleton<broadcast_proxy>::GetInstance()->SubscribeCommonEvent();
310             break;
311         case APP_MGR_SERVICE_ID:
312             DelayedSingleton<broadcast_proxy>::GetInstance()->SubscribeApplicationState();
313             break;
314         case COMM_VPN_MANAGER_SYS_ABILITY_ID:
315             RegisterVpnEventCallback();
316             break;
317         case COMM_NETSYS_NATIVE_SYS_ABILITY_ID:
318             RegisterDnsResultCallback();
319             break;
320         default:
321             break;
322     }
323 }
324 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)325 void broadcast_proxy::SystemAbilityListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
326 {
327     NETMGR_EXT_LOG_I("OnRemoveSystemAbility: systemAbilityId:%{public}d", systemAbilityId);
328 }
329 
330 }
331 }
332