@@ -273,7 +273,8 @@ NetworkUpdateResult saveNetwork(WifiConfiguration config) {
273273 mConfiguredNetworks .get (netId ).status = Status .ENABLED ;
274274 }
275275 mWifiNative .saveConfig ();
276- sendConfiguredNetworksChangedBroadcast ();
276+ sendConfiguredNetworksChangedBroadcast (config , result .isNewNetwork () ?
277+ WifiManager .CHANGE_REASON_ADDED : WifiManager .CHANGE_REASON_CONFIG_CHANGE );
277278 return result ;
278279 }
279280
@@ -307,13 +308,16 @@ void updateStatus(int netId, DetailedState state) {
307308 boolean forgetNetwork (int netId ) {
308309 if (mWifiNative .removeNetwork (netId )) {
309310 mWifiNative .saveConfig ();
311+ WifiConfiguration target = null ;
310312 WifiConfiguration config = mConfiguredNetworks .get (netId );
311313 if (config != null ) {
312- mConfiguredNetworks .remove (netId );
314+ target = mConfiguredNetworks .remove (netId );
313315 mNetworkIds .remove (configKey (config ));
314316 }
315- writeIpAndProxyConfigurations ();
316- sendConfiguredNetworksChangedBroadcast ();
317+ if (target != null ) {
318+ writeIpAndProxyConfigurations ();
319+ sendConfiguredNetworksChangedBroadcast (target , WifiManager .CHANGE_REASON_REMOVED );
320+ }
317321 return true ;
318322 } else {
319323 loge ("Failed to remove network " + netId );
@@ -332,7 +336,11 @@ boolean forgetNetwork(int netId) {
332336 */
333337 int addOrUpdateNetwork (WifiConfiguration config ) {
334338 NetworkUpdateResult result = addOrUpdateNetworkNative (config );
335- sendConfiguredNetworksChangedBroadcast ();
339+ if (result .getNetworkId () != WifiConfiguration .INVALID_NETWORK_ID ) {
340+ sendConfiguredNetworksChangedBroadcast (mConfiguredNetworks .get (result .getNetworkId ()),
341+ result .isNewNetwork ? WifiManager .CHANGE_REASON_ADDED :
342+ WifiManager .CHANGE_REASON_CONFIG_CHANGE );
343+ }
336344 return result .getNetworkId ();
337345 }
338346
@@ -347,14 +355,17 @@ int addOrUpdateNetwork(WifiConfiguration config) {
347355 */
348356 boolean removeNetwork (int netId ) {
349357 boolean ret = mWifiNative .removeNetwork (netId );
358+ WifiConfiguration config = null ;
350359 if (ret ) {
351- WifiConfiguration config = mConfiguredNetworks .get (netId );
360+ config = mConfiguredNetworks .get (netId );
352361 if (config != null ) {
353- mConfiguredNetworks .remove (netId );
362+ config = mConfiguredNetworks .remove (netId );
354363 mNetworkIds .remove (configKey (config ));
355364 }
356365 }
357- sendConfiguredNetworksChangedBroadcast ();
366+ if (config != null ) {
367+ sendConfiguredNetworksChangedBroadcast (config , WifiManager .CHANGE_REASON_REMOVED );
368+ }
358369 return ret ;
359370 }
360371
@@ -364,12 +375,24 @@ boolean removeNetwork(int netId) {
364375 * API. The more powerful selectNetwork()/saveNetwork() is used by the
365376 * state machine for connecting to a network
366377 *
367- * @param netId network to be removed
378+ * @param netId network to be enabled
368379 * @return {@code true} if it succeeds, {@code false} otherwise
369380 */
370381 boolean enableNetwork (int netId , boolean disableOthers ) {
371382 boolean ret = enableNetworkWithoutBroadcast (netId , disableOthers );
372- sendConfiguredNetworksChangedBroadcast ();
383+ if (disableOthers ) {
384+ sendConfiguredNetworksChangedBroadcast ();
385+ } else {
386+ WifiConfiguration enabledNetwork = null ;
387+ synchronized (mConfiguredNetworks ) {
388+ enabledNetwork = mConfiguredNetworks .get (netId );
389+ }
390+ // check just in case the network was removed by someone else.
391+ if (enabledNetwork != null ) {
392+ sendConfiguredNetworksChangedBroadcast (enabledNetwork ,
393+ WifiManager .CHANGE_REASON_CONFIG_CHANGE );
394+ }
395+ }
373396 return ret ;
374397 }
375398
@@ -402,13 +425,18 @@ boolean disableNetwork(int netId) {
402425 */
403426 boolean disableNetwork (int netId , int reason ) {
404427 boolean ret = mWifiNative .disableNetwork (netId );
428+ WifiConfiguration network = null ;
405429 WifiConfiguration config = mConfiguredNetworks .get (netId );
406430 /* Only change the reason if the network was not previously disabled */
407431 if (config != null && config .status != Status .DISABLED ) {
408432 config .status = Status .DISABLED ;
409433 config .disableReason = reason ;
434+ network = config ;
435+ }
436+ if (network != null ) {
437+ sendConfiguredNetworksChangedBroadcast (network ,
438+ WifiManager .CHANGE_REASON_CONFIG_CHANGE );
410439 }
411- sendConfiguredNetworksChangedBroadcast ();
412440 return ret ;
413441 }
414442
@@ -574,9 +602,29 @@ boolean isUsingStaticIp(int netId) {
574602 return false ;
575603 }
576604
605+ /**
606+ * Should be called when a single network configuration is made.
607+ * @param network The network configuration that changed.
608+ * @param reason The reason for the change, should be one of WifiManager.CHANGE_REASON_ADDED,
609+ * WifiManager.CHANGE_REASON_REMOVED, or WifiManager.CHANGE_REASON_CHANGE.
610+ */
611+ private void sendConfiguredNetworksChangedBroadcast (WifiConfiguration network ,
612+ int reason ) {
613+ Intent intent = new Intent (WifiManager .CONFIGURED_NETWORKS_CHANGED_ACTION );
614+ intent .addFlags (Intent .FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT );
615+ intent .putExtra (WifiManager .EXTRA_MULTIPLE_NETWORKS_CHANGED , false );
616+ intent .putExtra (WifiManager .EXTRA_WIFI_CONFIGURATION , network );
617+ intent .putExtra (WifiManager .EXTRA_CHANGE_REASON , reason );
618+ mContext .sendBroadcast (intent );
619+ }
620+
621+ /**
622+ * Should be called when multiple network configuration changes are made.
623+ */
577624 private void sendConfiguredNetworksChangedBroadcast () {
578625 Intent intent = new Intent (WifiManager .CONFIGURED_NETWORKS_CHANGED_ACTION );
579626 intent .addFlags (Intent .FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT );
627+ intent .putExtra (WifiManager .EXTRA_MULTIPLE_NETWORKS_CHANGED , true );
580628 mContext .sendBroadcast (intent );
581629 }
582630
@@ -1135,6 +1183,7 @@ private NetworkUpdateResult addOrUpdateNetworkNative(WifiConfiguration config) {
11351183 mNetworkIds .put (configKey (currentConfig ), netId );
11361184
11371185 NetworkUpdateResult result = writeIpAndProxyConfigurationsOnChange (currentConfig , config );
1186+ result .setIsNewNetwork (newNetwork );
11381187 result .setNetworkId (netId );
11391188 return result ;
11401189 }
@@ -1234,7 +1283,8 @@ private NetworkUpdateResult writeIpAndProxyConfigurationsOnChange(
12341283 if (ipChanged || proxyChanged ) {
12351284 currentConfig .linkProperties = linkProperties ;
12361285 writeIpAndProxyConfigurations ();
1237- sendConfiguredNetworksChangedBroadcast ();
1286+ sendConfiguredNetworksChangedBroadcast (currentConfig ,
1287+ WifiManager .CHANGE_REASON_CONFIG_CHANGE );
12381288 }
12391289 return new NetworkUpdateResult (ipChanged , proxyChanged );
12401290 }
0 commit comments