Prevent crash

pull/156/head
M66B 6 years ago
parent 69acf59245
commit 3e9e56d878

@ -116,11 +116,11 @@ public class ConnectionHelper {
if (state.connected && !roaming) { if (state.connected && !roaming) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
NetworkInfo ani = cm.getActiveNetworkInfo(); NetworkInfo ani = (cm == null ? null : cm.getActiveNetworkInfo());
if (ani != null) if (ani != null)
state.roaming = ani.isRoaming(); state.roaming = ani.isRoaming();
} else { } else {
Network active = cm.getActiveNetwork(); Network active = (cm == null ? null : cm.getActiveNetwork());
if (active != null) { if (active != null) {
NetworkCapabilities caps = cm.getNetworkCapabilities(active); NetworkCapabilities caps = cm.getNetworkCapabilities(active);
if (caps != null) if (caps != null)
@ -152,13 +152,13 @@ public class ConnectionHelper {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { 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()) if (ani == null || !ani.isConnected())
return null; return null;
return cm.isActiveNetworkMetered(); return cm.isActiveNetworkMetered();
} }
Network active = cm.getActiveNetwork(); Network active = (cm == null ? null : cm.getActiveNetwork());
if (active == null) { if (active == null) {
Log.i("isMetered: no active network"); Log.i("isMetered: no active network");
return null; return null;
@ -199,42 +199,41 @@ public class ConnectionHelper {
boolean underlying = false; boolean underlying = false;
Network[] networks = cm.getAllNetworks(); Network[] networks = cm.getAllNetworks();
if (networks != null) for (Network network : networks) {
for (Network network : networks) { caps = cm.getNetworkCapabilities(network);
caps = cm.getNetworkCapabilities(network); if (caps == null) {
if (caps == null) { Log.i("isMetered: no underlying caps");
Log.i("isMetered: no underlying caps"); continue; // network unknown
continue; // network unknown }
}
Log.i("isMetered: underlying caps=" + caps); Log.i("isMetered: underlying caps=" + caps);
if (!caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) { if (!caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
Log.i("isMetered: underlying no internet"); Log.i("isMetered: underlying no internet");
continue; continue;
} }
if (!caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)) { if (!caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)) {
Log.i("isMetered: underlying restricted"); Log.i("isMetered: underlying restricted");
continue; continue;
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P &&
!caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOREGROUND)) { !caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOREGROUND)) {
Log.i("isMetered: underlying background"); Log.i("isMetered: underlying background");
continue; continue;
} }
if (caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)) { if (caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)) {
underlying = true; underlying = true;
Log.i("isMetered: underlying is connected"); Log.i("isMetered: underlying is connected");
if (caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)) { if (caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)) {
Log.i("isMetered: underlying is unmetered"); Log.i("isMetered: underlying is unmetered");
return false; return false;
}
} }
} }
}
if (!underlying) { if (!underlying) {
Log.i("isMetered: no underlying network"); Log.i("isMetered: no underlying network");

Loading…
Cancel
Save