From 5a6156db19a025083cc389ad4b6da7cc9d87a9b8 Mon Sep 17 00:00:00 2001 From: KyungEun No <74607521+shruddms@users.noreply.github.com> Date: Wed, 8 Jan 2025 23:34:18 +0900 Subject: [PATCH 1/3] fix: Add API 29+ support while keeping backward compatibility with minimal changes --- .../detector/SimpleNetworkDetector.java | 51 ++++++++++++++++--- 1 file changed, 45 insertions(+), 6 deletions(-) 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..d7b998f5 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,6 +9,8 @@ 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 io.opentelemetry.android.common.internal.features.networkattributes.data.CurrentNetwork; import io.opentelemetry.android.common.internal.features.networkattributes.data.NetworkState; @@ -25,17 +27,54 @@ class SimpleNetworkDetector implements NetworkDetector { @Override public CurrentNetwork detectCurrentNetwork() { - NetworkInfo activeNetwork = - connectivityManager.getActiveNetworkInfo(); // Deprecated in API 29 + // For API 29 and above, use modern APIs + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) { + Network network = connectivityManager.getActiveNetwork(); + if (network == null) { + return NO_NETWORK; + } + + NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network); + if (capabilities == null) { + return UNKNOWN_NETWORK; + } + + // Determine network type based on transport capabilities + if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { + return CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR) + .subType("") // Additional details can be added + .build(); + } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { + return CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI) + .subType("") + .build(); + } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) { + return CurrentNetwork.builder(NetworkState.TRANSPORT_VPN) + .subType("") + .build(); + } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) { + return CurrentNetwork.builder(NetworkState.TRANSPORT_WIRED) + .subType("") + .build(); + } + + // Default to UNKNOWN_NETWORK for other types + return UNKNOWN_NETWORK; + } + + // For API 28 and below, use deprecated methods + NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo(); // Deprecated in API 29 if (activeNetwork == null) { return NO_NETWORK; } - switch (activeNetwork.getType()) { - case ConnectivityManager.TYPE_MOBILE: // Deprecated in API 28 + + // Determine network type using TYPE_* constants + switch (activeNetwork.getType()) { // Deprecated in API 28 + case ConnectivityManager.TYPE_MOBILE: return CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR) .subType(activeNetwork.getSubtypeName()) .build(); - case ConnectivityManager.TYPE_WIFI: // Deprecated in API 28 + case ConnectivityManager.TYPE_WIFI: return CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI) .subType(activeNetwork.getSubtypeName()) .build(); @@ -48,7 +87,7 @@ public CurrentNetwork detectCurrentNetwork() { .subType(activeNetwork.getSubtypeName()) .build(); } - // there is an active network, but it doesn't fall into the neat buckets above + // Return UNKNOWN_NETWORK if type does not match predefined cases return UNKNOWN_NETWORK; } } From 6179531da5c50e16f84fc31a759b1f5d8a537d21 Mon Sep 17 00:00:00 2001 From: KyungEun No <74607521+shruddms@users.noreply.github.com> Date: Thu, 9 Jan 2025 21:35:25 +0900 Subject: [PATCH 2/3] refactor: Simplify and align code in SimpleNetworkDetector --- .../network/detector/SimpleNetworkDetector.java | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) 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 d7b998f5..4be64cf0 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 @@ -45,17 +45,11 @@ public CurrentNetwork detectCurrentNetwork() { .subType("") // Additional details can be added .build(); } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { - return CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI) - .subType("") - .build(); + return CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI).subType("").build(); } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) { - return CurrentNetwork.builder(NetworkState.TRANSPORT_VPN) - .subType("") - .build(); + return CurrentNetwork.builder(NetworkState.TRANSPORT_VPN).subType("").build(); } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) { - return CurrentNetwork.builder(NetworkState.TRANSPORT_WIRED) - .subType("") - .build(); + return CurrentNetwork.builder(NetworkState.TRANSPORT_WIRED).subType("").build(); } // Default to UNKNOWN_NETWORK for other types @@ -63,7 +57,8 @@ public CurrentNetwork detectCurrentNetwork() { } // For API 28 and below, use deprecated methods - NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo(); // Deprecated in API 29 + NetworkInfo activeNetwork = + connectivityManager.getActiveNetworkInfo(); // Deprecated in API 29 if (activeNetwork == null) { return NO_NETWORK; } From 2474fff6de4e6ab0445798c8d182b5da69cbbb2d Mon Sep 17 00:00:00 2001 From: KyungEun No <74607521+shruddms@users.noreply.github.com> Date: Sat, 11 Jan 2025 20:38:47 +0900 Subject: [PATCH 3/3] refactor: improve network detection for modern and legacy APIs --- .../detector/SimpleNetworkDetector.java | 90 ++++++++++--------- 1 file changed, 48 insertions(+), 42 deletions(-) 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 4be64cf0..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 @@ -12,6 +12,7 @@ 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; @@ -27,62 +28,67 @@ class SimpleNetworkDetector implements NetworkDetector { @Override public CurrentNetwork detectCurrentNetwork() { - // For API 29 and above, use modern APIs if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) { - Network network = connectivityManager.getActiveNetwork(); - if (network == null) { - return NO_NETWORK; - } - - NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network); - if (capabilities == null) { - return UNKNOWN_NETWORK; - } + return detectUsingModernApi(); + } else { + return detectUsingLegacyApi(); + } + } - // Determine network type based on transport capabilities - if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { - return CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR) - .subType("") // Additional details can be added - .build(); - } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { - return CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI).subType("").build(); - } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) { - return CurrentNetwork.builder(NetworkState.TRANSPORT_VPN).subType("").build(); - } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) { - return CurrentNetwork.builder(NetworkState.TRANSPORT_WIRED).subType("").build(); - } + @RequiresApi(api = android.os.Build.VERSION_CODES.Q) + private CurrentNetwork detectUsingModernApi() { + Network network = connectivityManager.getActiveNetwork(); + if (network == null) { + return NO_NETWORK; + } - // Default to UNKNOWN_NETWORK for other types + NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network); + if (capabilities == null) { return UNKNOWN_NETWORK; } - // For API 28 and below, use deprecated methods - NetworkInfo activeNetwork = - connectivityManager.getActiveNetworkInfo(); // Deprecated in API 29 + 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; } - // Determine network type using TYPE_* constants - switch (activeNetwork.getType()) { // Deprecated in API 28 + switch (activeNetwork.getType()) { case ConnectivityManager.TYPE_MOBILE: - return CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR) - .subType(activeNetwork.getSubtypeName()) - .build(); + return buildCurrentNetwork( + NetworkState.TRANSPORT_CELLULAR, activeNetwork.getSubtypeName()); case ConnectivityManager.TYPE_WIFI: - return CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI) - .subType(activeNetwork.getSubtypeName()) - .build(); + 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; } - // Return UNKNOWN_NETWORK if type does not match predefined cases - return UNKNOWN_NETWORK; + } + + private CurrentNetwork buildCurrentNetwork(NetworkState state, String subType) { + return CurrentNetwork.builder(state).subType(subType).build(); } }