Skip to content

Commit 887f352

Browse files
authored
fix(network): modernize simplenetworkdetector with updated android apis (#736)
* fix: Add API 29+ support while keeping backward compatibility with minimal changes * refactor: Simplify and align code in SimpleNetworkDetector * refactor: improve network detection for modern and legacy APIs --------- Co-authored-by: KyungEun No <[email protected]>
1 parent 7036754 commit 887f352

File tree

1 file changed

+58
-18
lines changed

1 file changed

+58
-18
lines changed

services/src/main/java/io/opentelemetry/android/internal/services/network/detector/SimpleNetworkDetector.java

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
import static io.opentelemetry.android.internal.services.network.CurrentNetworkProvider.UNKNOWN_NETWORK;
1010

1111
import android.net.ConnectivityManager;
12+
import android.net.Network;
13+
import android.net.NetworkCapabilities;
1214
import android.net.NetworkInfo;
15+
import androidx.annotation.RequiresApi;
1316
import io.opentelemetry.android.common.internal.features.networkattributes.data.CurrentNetwork;
1417
import io.opentelemetry.android.common.internal.features.networkattributes.data.NetworkState;
1518

@@ -25,30 +28,67 @@ class SimpleNetworkDetector implements NetworkDetector {
2528

2629
@Override
2730
public CurrentNetwork detectCurrentNetwork() {
28-
NetworkInfo activeNetwork =
29-
connectivityManager.getActiveNetworkInfo(); // Deprecated in API 29
31+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
32+
return detectUsingModernApi();
33+
} else {
34+
return detectUsingLegacyApi();
35+
}
36+
}
37+
38+
@RequiresApi(api = android.os.Build.VERSION_CODES.Q)
39+
private CurrentNetwork detectUsingModernApi() {
40+
Network network = connectivityManager.getActiveNetwork();
41+
if (network == null) {
42+
return NO_NETWORK;
43+
}
44+
45+
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
46+
if (capabilities == null) {
47+
return UNKNOWN_NETWORK;
48+
}
49+
50+
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
51+
return buildCurrentNetwork(NetworkState.TRANSPORT_CELLULAR, "");
52+
}
53+
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
54+
return buildCurrentNetwork(NetworkState.TRANSPORT_WIFI, "");
55+
}
56+
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
57+
return buildCurrentNetwork(NetworkState.TRANSPORT_VPN, "");
58+
}
59+
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
60+
return buildCurrentNetwork(NetworkState.TRANSPORT_WIRED, "");
61+
}
62+
63+
return UNKNOWN_NETWORK;
64+
}
65+
66+
@SuppressWarnings("deprecation")
67+
private CurrentNetwork detectUsingLegacyApi() {
68+
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
3069
if (activeNetwork == null) {
3170
return NO_NETWORK;
3271
}
72+
3373
switch (activeNetwork.getType()) {
34-
case ConnectivityManager.TYPE_MOBILE: // Deprecated in API 28
35-
return CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR)
36-
.subType(activeNetwork.getSubtypeName())
37-
.build();
38-
case ConnectivityManager.TYPE_WIFI: // Deprecated in API 28
39-
return CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI)
40-
.subType(activeNetwork.getSubtypeName())
41-
.build();
74+
case ConnectivityManager.TYPE_MOBILE:
75+
return buildCurrentNetwork(
76+
NetworkState.TRANSPORT_CELLULAR, activeNetwork.getSubtypeName());
77+
case ConnectivityManager.TYPE_WIFI:
78+
return buildCurrentNetwork(
79+
NetworkState.TRANSPORT_WIFI, activeNetwork.getSubtypeName());
4280
case ConnectivityManager.TYPE_VPN:
43-
return CurrentNetwork.builder(NetworkState.TRANSPORT_VPN)
44-
.subType(activeNetwork.getSubtypeName())
45-
.build();
81+
return buildCurrentNetwork(
82+
NetworkState.TRANSPORT_VPN, activeNetwork.getSubtypeName());
4683
case ConnectivityManager.TYPE_ETHERNET:
47-
return CurrentNetwork.builder(NetworkState.TRANSPORT_WIRED)
48-
.subType(activeNetwork.getSubtypeName())
49-
.build();
84+
return buildCurrentNetwork(
85+
NetworkState.TRANSPORT_WIRED, activeNetwork.getSubtypeName());
86+
default:
87+
return UNKNOWN_NETWORK;
5088
}
51-
// there is an active network, but it doesn't fall into the neat buckets above
52-
return UNKNOWN_NETWORK;
89+
}
90+
91+
private CurrentNetwork buildCurrentNetwork(NetworkState state, String subType) {
92+
return CurrentNetwork.builder(state).subType(subType).build();
5393
}
5494
}

0 commit comments

Comments
 (0)