Skip to content

Commit

Permalink
feat: Added isInternetAvailable function in ConnectivityUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
Aitorbp committed Sep 23, 2024
1 parent e676f0e commit 6f12f2c
Showing 1 changed file with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
* ownCloud Android client application
*
* @author David A. Velasco
* Copyright (C) 2017 ownCloud GmbH.
* @author Aitor Ballesteros Pavón
*
* Copyright (C) 2024 ownCloud GmbH.
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
Expand All @@ -21,6 +23,8 @@

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;

import timber.log.Timber;
Expand Down Expand Up @@ -51,4 +55,24 @@ public static boolean isNetworkActive(Context context) {

return (activeNetwork != null && activeNetwork.isConnectedOrConnecting());
}

public static boolean isInternetAvailable(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

Network network = connectivityManager.getActiveNetwork();
if (network == null) return false;

NetworkCapabilities activeNetwork = connectivityManager.getNetworkCapabilities(network);
if (activeNetwork == null) return false;

if (activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
return true;
} else if (activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
return true;
} else if (activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
return true;
} else {
return false;
}
}
}

0 comments on commit 6f12f2c

Please sign in to comment.