From d0f19242bb332fe7ce4475e402644f4679f735de Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 7 Jun 2020 12:20:12 +0200 Subject: [PATCH] Workaround Android returning wrong is connected state --- .../main/java/eu/faircode/netguard/Util.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/eu/faircode/netguard/Util.java b/app/src/main/java/eu/faircode/netguard/Util.java index b640e4d6..de188398 100644 --- a/app/src/main/java/eu/faircode/netguard/Util.java +++ b/app/src/main/java/eu/faircode/netguard/Util.java @@ -165,8 +165,24 @@ public class Util { public static boolean isConnected(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - NetworkInfo ni = (cm == null ? null : cm.getActiveNetworkInfo()); - return (ni != null && ni.isConnected()); + if (cm == null) + return false; + + NetworkInfo ni = cm.getActiveNetworkInfo(); + if (ni != null && ni.isConnected()) + return true; + + Network[] networks = cm.getAllNetworks(); + if (networks == null) + return false; + + for (Network network : networks) { + ni = cm.getNetworkInfo(network); + if (ni != null && ni.getType() != ConnectivityManager.TYPE_VPN && ni.isConnected()) + return true; + } + + return false; } public static boolean isWifiActive(Context context) {