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,7 +199,6 @@ 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) {

Loading…
Cancel
Save