Skip to content

Commit

Permalink
fix: delete deprecated code to check connectivity
Browse files Browse the repository at this point in the history
  • Loading branch information
CappielloAntonio committed Dec 30, 2024
1 parent f0d31e4 commit d3ca43e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

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

import com.cappielloantonio.tempo.App;

Expand Down Expand Up @@ -39,7 +40,19 @@ public CacheUtil(int maxAge, int maxStale) {

private boolean isConnected() {
ConnectivityManager connectivityManager = (ConnectivityManager) App.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
return (netInfo != null && netInfo.isConnected());

if (connectivityManager != null) {
Network network = connectivityManager.getActiveNetwork();

if (network != null) {
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);

if (capabilities != null) {
return capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
}
}
}

return false;
}
}
12 changes: 10 additions & 2 deletions app/src/main/java/com/cappielloantonio/tempo/util/NetworkUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

import com.cappielloantonio.tempo.App;
Expand All @@ -11,9 +13,15 @@ public static boolean isOffline() {
ConnectivityManager connectivityManager = (ConnectivityManager) App.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);

if (connectivityManager != null) {
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
Network network = connectivityManager.getActiveNetwork();

return networkInfo == null || !networkInfo.isConnected();
if (network != null) {
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);

if (capabilities != null) {
return !capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) || !capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
}
}
}

return true;
Expand Down

0 comments on commit d3ca43e

Please sign in to comment.