Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Check for network connectivity before requesting resources #6123

Merged
merged 5 commits into from
Aug 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -40,6 +43,7 @@ public static MapboxAccountManager start(Context context, String accessToken) {
if (mapboxAccountManager == null) {
mapboxAccountManager = new MapboxAccountManager(context, accessToken);
}

MapboxEventManager eventManager = MapboxEventManager.getMapboxEventManager();
eventManager.initialize(mapboxAccountManager.applicationContext, mapboxAccountManager.accessToken);
return mapboxAccountManager;
Expand Down Expand Up @@ -79,4 +83,19 @@ public static void validateAccessToken(String accessToken) throws InvalidAccessT
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
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;
Expand Down Expand Up @@ -47,6 +49,11 @@ private HTTPRequest(long nativePtr, String resourceUrl, String userAgent, String
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")) {
Expand All @@ -73,7 +80,10 @@ private HTTPRequest(long nativePtr, String resourceUrl, String userAgent, String
}

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
Expand Down Expand Up @@ -124,7 +134,7 @@ private void onFailure(Exception e) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
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;
Expand All @@ -25,14 +23,18 @@
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;
Expand All @@ -43,6 +45,7 @@
import java.util.TimerTask;
import java.util.UUID;
import java.util.Vector;

import okhttp3.CertificatePinner;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -629,9 +632,7 @@ protected Void doInBackground(Void... voids) {
}

// 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.
Expand Down