Skip to content

Commit 3f33bd2

Browse files
Rubin XuAndroid (Google) Code Review
authored andcommitted
Merge "Enables Wifi network once it is added" into ub-testdpc-rvc
2 parents 845a66f + 15dd063 commit 3f33bd2

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

app/src/main/java/com/afwsamples/testdpc/policy/wifimanagement/WifiConfigUtil.java

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import android.os.Build;
2323

2424
public class WifiConfigUtil {
25+
private static final int INVALID_NETWORK_ID = -1;
2526

2627
/**
2728
* Save or replace the WiFi configuration.
@@ -33,23 +34,37 @@ public class WifiConfigUtil {
3334
public static boolean saveWifiConfiguration(
3435
Context context, WifiConfiguration wifiConfiguration) {
3536
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
36-
return wifiConfiguration.networkId == -1
37-
? addWifiNetwork(wifiManager, wifiConfiguration)
38-
: updateWifiNetwork(wifiManager, wifiConfiguration);
37+
final int networkId;
38+
if (wifiConfiguration.networkId == INVALID_NETWORK_ID) {
39+
networkId = addWifiNetwork(wifiManager, wifiConfiguration);
40+
} else {
41+
networkId = updateWifiNetwork(wifiManager, wifiConfiguration);
42+
}
43+
if (networkId == INVALID_NETWORK_ID) {
44+
return false;
45+
}
46+
wifiManager.enableNetwork(networkId, /* disableOthers= */ false);
47+
return true;
3948
}
4049

41-
private static boolean addWifiNetwork(
50+
/**
51+
* Adds a new Wifi configuration, returning the configuration's networkId, or
52+
* {@link #INVALID_NETWORK_ID} if the operation fails.
53+
*/
54+
private static int addWifiNetwork(
4255
WifiManager wifiManager, WifiConfiguration wifiConfiguration) {
4356
// WifiManager APIs are marked as deprecated but still explicitly supported for DPCs.
4457
int networkId = wifiManager.addNetwork(wifiConfiguration);
45-
if (networkId == -1) {
46-
return false;
58+
if (networkId == INVALID_NETWORK_ID) {
59+
return INVALID_NETWORK_ID;
4760
}
4861
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
4962
// Saving the configuration is required pre-O.
50-
return saveAddedWifiConfiguration(wifiManager, networkId);
63+
if (!saveAddedWifiConfiguration(wifiManager, networkId)) {
64+
return INVALID_NETWORK_ID;
65+
}
5166
}
52-
return true;
67+
return networkId;
5368
}
5469

5570
private static boolean saveAddedWifiConfiguration(WifiManager wifiManager, int networkId) {
@@ -61,18 +76,24 @@ private static boolean saveAddedWifiConfiguration(WifiManager wifiManager, int n
6176
return true;
6277
}
6378

64-
private static boolean updateWifiNetwork(
79+
/**
80+
* Saves an existing Wifi configuration, returning the configuration's networkId, or
81+
* {@link #INVALID_NETWORK_ID} if the operation fails.
82+
*/
83+
private static int updateWifiNetwork(
6584
WifiManager wifiManager, WifiConfiguration wifiConfiguration) {
6685
// WifiManager APIs are marked as deprecated but still explicitly supported for DPCs.
6786
int networkId = wifiManager.updateNetwork(wifiConfiguration);
68-
if (networkId == -1) {
69-
return false;
87+
if (networkId == INVALID_NETWORK_ID) {
88+
return INVALID_NETWORK_ID;
7089
}
7190
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
7291
// Saving the configuration is required pre-O.
73-
return saveUpdatedWifiConfiguration(wifiManager);
92+
if (!saveUpdatedWifiConfiguration(wifiManager)) {
93+
return INVALID_NETWORK_ID;
94+
}
7495
}
75-
return true;
96+
return networkId;
7697
}
7798

7899
private static boolean saveUpdatedWifiConfiguration(WifiManager wifiManager) {

0 commit comments

Comments
 (0)