From 3e9e56d8784f069c0f23473044af9a4c9ce2ffc1 Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 14 Jun 2019 11:00:34 +0200 Subject: [PATCH] Prevent crash --- .../eu/faircode/email/ConnectionHelper.java | 63 +++++++++---------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ConnectionHelper.java b/app/src/main/java/eu/faircode/email/ConnectionHelper.java index 818542262d..9cc45887ad 100644 --- a/app/src/main/java/eu/faircode/email/ConnectionHelper.java +++ b/app/src/main/java/eu/faircode/email/ConnectionHelper.java @@ -116,11 +116,11 @@ public class ConnectionHelper { if (state.connected && !roaming) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) { - NetworkInfo ani = cm.getActiveNetworkInfo(); + NetworkInfo ani = (cm == null ? null : cm.getActiveNetworkInfo()); if (ani != null) state.roaming = ani.isRoaming(); } else { - Network active = cm.getActiveNetwork(); + Network active = (cm == null ? null : cm.getActiveNetwork()); if (active != null) { NetworkCapabilities caps = cm.getNetworkCapabilities(active); if (caps != null) @@ -152,13 +152,13 @@ public class ConnectionHelper { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { - NetworkInfo ani = cm.getActiveNetworkInfo(); + NetworkInfo ani = (cm == null ? null : cm.getActiveNetworkInfo()); if (ani == null || !ani.isConnected()) return null; return cm.isActiveNetworkMetered(); } - Network active = cm.getActiveNetwork(); + Network active = (cm == null ? null : cm.getActiveNetwork()); if (active == null) { Log.i("isMetered: no active network"); return null; @@ -199,42 +199,41 @@ public class ConnectionHelper { boolean underlying = false; Network[] networks = cm.getAllNetworks(); - if (networks != null) - for (Network network : networks) { - caps = cm.getNetworkCapabilities(network); - if (caps == null) { - Log.i("isMetered: no underlying caps"); - continue; // network unknown - } + for (Network network : networks) { + caps = cm.getNetworkCapabilities(network); + if (caps == null) { + Log.i("isMetered: no underlying caps"); + continue; // network unknown + } - Log.i("isMetered: underlying caps=" + caps); + Log.i("isMetered: underlying caps=" + caps); - if (!caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) { - Log.i("isMetered: underlying no internet"); - continue; - } + if (!caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) { + Log.i("isMetered: underlying no internet"); + continue; + } - if (!caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)) { - Log.i("isMetered: underlying restricted"); - continue; - } + if (!caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)) { + Log.i("isMetered: underlying restricted"); + continue; + } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && - !caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOREGROUND)) { - Log.i("isMetered: underlying background"); - continue; - } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && + !caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOREGROUND)) { + Log.i("isMetered: underlying background"); + continue; + } - if (caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)) { - underlying = true; - Log.i("isMetered: underlying is connected"); + if (caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)) { + underlying = true; + Log.i("isMetered: underlying is connected"); - if (caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)) { - Log.i("isMetered: underlying is unmetered"); - return false; - } + if (caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)) { + Log.i("isMetered: underlying is unmetered"); + return false; } } + } if (!underlying) { Log.i("isMetered: no underlying network");