Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(network): modernize simplenetworkdetector with updated android apis #736

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Loading