[android] #6042 - Check for network connectivity before requesting resources

This commit is contained in:
Antonio Zugaldia
2016-08-24 15:54:16 -04:00
committed by GitHub
parent 2523f33ac4
commit 4579fa031e
3 changed files with 37 additions and 7 deletions

View File

@@ -1,7 +1,10 @@
package com.mapbox.mapboxsdk;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.util.Log;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.exceptions.InvalidAccessTokenException;
@@ -40,6 +43,7 @@ public class MapboxAccountManager {
if (mapboxAccountManager == null) {
mapboxAccountManager = new MapboxAccountManager(context, accessToken);
}
MapboxEventManager eventManager = MapboxEventManager.getMapboxEventManager();
eventManager.initialize(mapboxAccountManager.applicationContext, mapboxAccountManager.accessToken);
return mapboxAccountManager;
@@ -79,4 +83,19 @@ public class MapboxAccountManager {
throw new InvalidAccessTokenException();
}
}
/**
* Determines whether we have an Internet connection available. Please do not rely on this
* method in your apps, this method is used internally by the SDK.
*
* @return true if there is an Internet connection, false otherwise
*/
public boolean isConnected() {
ConnectivityManager cm = (ConnectivityManager) applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean result = (activeNetwork != null && activeNetwork.isConnected());
Log.v("IOUtils", "isConnected result = " + result);
return result;
}
}

View File

@@ -3,10 +3,12 @@ package com.mapbox.mapboxsdk.http;
import android.text.TextUtils;
import android.util.Log;
import com.mapbox.mapboxsdk.MapboxAccountManager;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.NoRouteToHostException;
import java.net.ProtocolException;
import java.net.SocketException;
import java.net.UnknownHostException;
@@ -47,6 +49,11 @@ class HTTPRequest implements Callback {
mNativePtr = nativePtr;
try {
// Don't try a request if we aren't connected
if (!MapboxAccountManager.getInstance().isConnected()) {
throw new NoRouteToHostException("No Internet connection available.");
}
HttpUrl httpUrl = HttpUrl.parse(resourceUrl);
final String host = httpUrl.host().toLowerCase(MapboxConstants.MAPBOX_LOCALE);
if (host.equals("mapbox.com") || host.endsWith(".mapbox.com")) {
@@ -73,7 +80,10 @@ class HTTPRequest implements Callback {
}
public void cancel() {
mCall.cancel();
// mCall can be null if the constructor gets aborted (e.g, under a NoRouteToHostException).
if (mCall != null) {
mCall.cancel();
}
// TODO: We need a lock here because we can try
// to cancel at the same time the request is getting
@@ -124,7 +134,7 @@ class HTTPRequest implements Callback {
Log.w(LOG_TAG, String.format("[HTTP] Request could not be executed: %s", e.getMessage()));
int type = PERMANENT_ERROR;
if ((e instanceof UnknownHostException) || (e instanceof SocketException) || (e instanceof ProtocolException) || (e instanceof SSLException)) {
if ((e instanceof NoRouteToHostException) || (e instanceof UnknownHostException) || (e instanceof SocketException) || (e instanceof ProtocolException) || (e instanceof SSLException)) {
type = CONNECTION_ERROR;
} else if ((e instanceof InterruptedIOException)) {
type = TEMPORARY_ERROR;

View File

@@ -11,8 +11,6 @@ import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.content.res.Configuration;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
@@ -25,14 +23,18 @@ import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;
import com.mapbox.mapboxsdk.BuildConfig;
import com.mapbox.mapboxsdk.MapboxAccountManager;
import com.mapbox.mapboxsdk.constants.GeoConstants;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.exceptions.TelemetryServiceNotConfiguredException;
import com.mapbox.mapboxsdk.location.LocationServices;
import com.mapbox.mapboxsdk.utils.MathUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
@@ -43,6 +45,7 @@ import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import java.util.Vector;
import okhttp3.CertificatePinner;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
@@ -629,9 +632,7 @@ public class MapboxEventManager {
}
// Check for NetworkConnectivity
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo == null || !networkInfo.isConnected()) {
if (!MapboxAccountManager.getInstance().isConnected()) {
Log.w(TAG, "Not connected to network, so empty events cache and return without attempting to send events");
// Make sure that events don't pile up when Offline
// and thus impact available memory over time.