Skip to content

Commit 4342c5a

Browse files
committed
Refactor network selection logic into helper methods
1 parent a579d8d commit 4342c5a

File tree

1 file changed

+52
-44
lines changed

1 file changed

+52
-44
lines changed

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilReq.java

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -402,57 +402,19 @@ else if (this.options.fileCache)
402402

403403
for (Network network : networks) {
404404

405-
NetworkInfo netInfo = connectivityManager.getNetworkInfo(network);
406-
NetworkCapabilities caps = connectivityManager.getNetworkCapabilities(network);
407-
408-
if (caps == null || netInfo == null) {
409-
continue;
410-
}
411-
412-
if (!netInfo.isConnected()) {
413-
continue;
414-
}
415-
416-
if (!caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
405+
if (!isValidWifiNetwork(connectivityManager, network)) {
417406
continue;
418407
}
419408

420409
// if targetHostIpAvailable does not match, fallback to any wifi
421410
if (targetHostIpAvailable) {
422411
String targetHostIp = this.options.targetHostIp;
423412

424-
LinkProperties lp = connectivityManager.getLinkProperties(network);
425-
if (lp != null) {
426-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
427-
// For Android R and above, use DHCP server address
428-
Inet4Address dhcpServer = lp.getDhcpServerAddress();
429-
430-
if (dhcpServer != null && dhcpServer.getHostAddress().equals(targetHostIp)) {
431-
clientBuilder.proxy(Proxy.NO_PROXY);
432-
clientBuilder.socketFactory(network.getSocketFactory());
433-
found = true;
434-
break;
435-
}
436-
}
437-
// For older versions, check each link address for a matching subnet
438-
List<LinkAddress> linkAddresses = lp.getLinkAddresses();
439-
if (linkAddresses != null && !linkAddresses.isEmpty()) {
440-
boolean subnetFound = false;
441-
for (LinkAddress la : linkAddresses) {
442-
String hostAddress = la.getAddress().getHostAddress();
443-
if (hostAddress.equals(targetHostIp)) {
444-
clientBuilder.proxy(Proxy.NO_PROXY);
445-
clientBuilder.socketFactory(network.getSocketFactory());
446-
found = true;
447-
subnetFound = true;
448-
break;
449-
}
450-
}
451-
if (subnetFound) {
452-
break;
453-
}
454-
}
455-
413+
if (networkMatchesTargetIp(connectivityManager, network, targetHostIp)) {
414+
clientBuilder.proxy(Proxy.NO_PROXY);
415+
clientBuilder.socketFactory(network.getSocketFactory());
416+
found = true;
417+
break;
456418
}
457419
}
458420

@@ -1063,4 +1025,50 @@ public static OkHttpClient.Builder enableTls12OnPreLollipop(OkHttpClient.Builder
10631025

10641026
return client;
10651027
}
1028+
1029+
/**
1030+
* Check if a network is a valid connected WiFi network
1031+
*/
1032+
private boolean isValidWifiNetwork(ConnectivityManager cm, Network network) {
1033+
NetworkInfo netInfo = cm.getNetworkInfo(network);
1034+
NetworkCapabilities caps = cm.getNetworkCapabilities(network);
1035+
1036+
if (caps == null || netInfo == null) {
1037+
return false;
1038+
}
1039+
1040+
if (!netInfo.isConnected()) {
1041+
return false;
1042+
}
1043+
1044+
return caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI);
1045+
}
1046+
1047+
/**
1048+
* Check if a network matches the target host IP address
1049+
*/
1050+
private boolean networkMatchesTargetIp(ConnectivityManager cm, Network network, String targetHostIp) {
1051+
LinkProperties lp = cm.getLinkProperties(network);
1052+
if (lp == null) return false;
1053+
1054+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
1055+
// For Android R and above, use DHCP server address
1056+
Inet4Address dhcpServer = lp.getDhcpServerAddress();
1057+
if (dhcpServer != null && dhcpServer.getHostAddress().equals(targetHostIp)) {
1058+
return true;
1059+
}
1060+
}
1061+
1062+
// Always fall back to linkAddresses check
1063+
List<LinkAddress> linkAddresses = lp.getLinkAddresses();
1064+
if (linkAddresses != null && !linkAddresses.isEmpty()) {
1065+
for (LinkAddress la : linkAddresses) {
1066+
if (la.getAddress().getHostAddress().equals(targetHostIp)) {
1067+
return true;
1068+
}
1069+
}
1070+
}
1071+
1072+
return false;
1073+
}
10661074
}

0 commit comments

Comments
 (0)