From d3ca43ed4995d1dfdc1716da63a08ed63c33db12 Mon Sep 17 00:00:00 2001 From: CappielloAntonio Date: Mon, 30 Dec 2024 16:45:38 +0100 Subject: [PATCH] fix: delete deprecated code to check connectivity --- .../tempo/subsonic/utils/CacheUtil.java | 19 ++++++++++++++++--- .../tempo/util/NetworkUtil.java | 12 ++++++++++-- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/cappielloantonio/tempo/subsonic/utils/CacheUtil.java b/app/src/main/java/com/cappielloantonio/tempo/subsonic/utils/CacheUtil.java index 1a5170e5..63bde7aa 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/subsonic/utils/CacheUtil.java +++ b/app/src/main/java/com/cappielloantonio/tempo/subsonic/utils/CacheUtil.java @@ -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; @@ -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; } } diff --git a/app/src/main/java/com/cappielloantonio/tempo/util/NetworkUtil.java b/app/src/main/java/com/cappielloantonio/tempo/util/NetworkUtil.java index 1c6d08fc..f3eca98f 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/util/NetworkUtil.java +++ b/app/src/main/java/com/cappielloantonio/tempo/util/NetworkUtil.java @@ -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; @@ -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;