diff --git a/services/src/main/java/io/opentelemetry/android/internal/services/network/detector/SimpleNetworkDetector.java b/services/src/main/java/io/opentelemetry/android/internal/services/network/detector/SimpleNetworkDetector.java index 6022c3cc..fc0c8767 100644 --- a/services/src/main/java/io/opentelemetry/android/internal/services/network/detector/SimpleNetworkDetector.java +++ b/services/src/main/java/io/opentelemetry/android/internal/services/network/detector/SimpleNetworkDetector.java @@ -9,7 +9,10 @@ import static io.opentelemetry.android.internal.services.network.CurrentNetworkProvider.UNKNOWN_NETWORK; import android.net.ConnectivityManager; +import android.net.Network; +import android.net.NetworkCapabilities; import android.net.NetworkInfo; +import androidx.annotation.RequiresApi; import io.opentelemetry.android.common.internal.features.networkattributes.data.CurrentNetwork; import io.opentelemetry.android.common.internal.features.networkattributes.data.NetworkState; @@ -25,30 +28,67 @@ class SimpleNetworkDetector implements NetworkDetector { @Override public CurrentNetwork detectCurrentNetwork() { - NetworkInfo activeNetwork = - connectivityManager.getActiveNetworkInfo(); // Deprecated in API 29 + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) { + return detectUsingModernApi(); + } else { + return detectUsingLegacyApi(); + } + } + + @RequiresApi(api = android.os.Build.VERSION_CODES.Q) + private CurrentNetwork detectUsingModernApi() { + Network network = connectivityManager.getActiveNetwork(); + if (network == null) { + return NO_NETWORK; + } + + NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network); + if (capabilities == null) { + return UNKNOWN_NETWORK; + } + + if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { + return buildCurrentNetwork(NetworkState.TRANSPORT_CELLULAR, ""); + } + if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { + return buildCurrentNetwork(NetworkState.TRANSPORT_WIFI, ""); + } + if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) { + return buildCurrentNetwork(NetworkState.TRANSPORT_VPN, ""); + } + if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) { + return buildCurrentNetwork(NetworkState.TRANSPORT_WIRED, ""); + } + + return UNKNOWN_NETWORK; + } + + @SuppressWarnings("deprecation") + private CurrentNetwork detectUsingLegacyApi() { + NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo(); if (activeNetwork == null) { return NO_NETWORK; } + switch (activeNetwork.getType()) { - case ConnectivityManager.TYPE_MOBILE: // Deprecated in API 28 - return CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR) - .subType(activeNetwork.getSubtypeName()) - .build(); - case ConnectivityManager.TYPE_WIFI: // Deprecated in API 28 - return CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI) - .subType(activeNetwork.getSubtypeName()) - .build(); + case ConnectivityManager.TYPE_MOBILE: + return buildCurrentNetwork( + NetworkState.TRANSPORT_CELLULAR, activeNetwork.getSubtypeName()); + case ConnectivityManager.TYPE_WIFI: + return buildCurrentNetwork( + NetworkState.TRANSPORT_WIFI, activeNetwork.getSubtypeName()); case ConnectivityManager.TYPE_VPN: - return CurrentNetwork.builder(NetworkState.TRANSPORT_VPN) - .subType(activeNetwork.getSubtypeName()) - .build(); + return buildCurrentNetwork( + NetworkState.TRANSPORT_VPN, activeNetwork.getSubtypeName()); case ConnectivityManager.TYPE_ETHERNET: - return CurrentNetwork.builder(NetworkState.TRANSPORT_WIRED) - .subType(activeNetwork.getSubtypeName()) - .build(); + return buildCurrentNetwork( + NetworkState.TRANSPORT_WIRED, activeNetwork.getSubtypeName()); + default: + return UNKNOWN_NETWORK; } - // there is an active network, but it doesn't fall into the neat buckets above - return UNKNOWN_NETWORK; + } + + private CurrentNetwork buildCurrentNetwork(NetworkState state, String subType) { + return CurrentNetwork.builder(state).subType(subType).build(); } }