Skip to content

Commit

Permalink
Add version-specific functionality for connections.
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamBJohnsonx committed Nov 18, 2020
1 parent e166a0a commit 55b026f
Showing 1 changed file with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import android.annotation.TargetApi;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.os.Build;

Expand Down Expand Up @@ -62,9 +64,15 @@ public DefaultConnectionService(Context ctx) {
public boolean isConnectionAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) mConnectionContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
@SuppressWarnings("deprecation")
final boolean isConnectionAvailable = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
final boolean isConnectionAvailable;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
final Network activeNetwork = connectivityManager.getActiveNetwork();
final NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(activeNetwork);
isConnectionAvailable = capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
} else {
final NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
isConnectionAvailable = activeNetwork != null && activeNetwork.isConnected();
}
Telemetry.emit((BaseEvent) new BaseEvent().put(TelemetryEventStrings.Key.NETWORK_CONNECTION, String.valueOf(isConnectionAvailable)));
return isConnectionAvailable;
}
Expand Down

0 comments on commit 55b026f

Please sign in to comment.