Skip to content

Commit

Permalink
fix(network): modernize simplenetworkdetector with updated android ap…
Browse files Browse the repository at this point in the history
…is (#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]>
  • Loading branch information
orioonyx and orioonyx authored Jan 13, 2025
1 parent 7036754 commit 887f352
Showing 1 changed file with 58 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
}
}

0 comments on commit 887f352

Please sign in to comment.