From 4579fa031e5d871ff18dc1b1d3e9eef3e6b73f13 Mon Sep 17 00:00:00 2001 From: Antonio Zugaldia Date: Wed, 24 Aug 2016 15:54:16 -0400 Subject: [PATCH] [android] #6042 - Check for network connectivity before requesting resources --- .../mapboxsdk/MapboxAccountManager.java | 19 +++++++++++++++++++ .../mapbox/mapboxsdk/http/HTTPRequest.java | 14 ++++++++++++-- .../telemetry/MapboxEventManager.java | 11 ++++++----- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/MapboxAccountManager.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/MapboxAccountManager.java index f8a83550f09..4930384f5cb 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/MapboxAccountManager.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/MapboxAccountManager.java @@ -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 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; @@ -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; + } + } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPRequest.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPRequest.java index 5b87e70ef68..accdbec40f7 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPRequest.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPRequest.java @@ -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; @@ -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")) { @@ -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 @@ -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; diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/telemetry/MapboxEventManager.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/telemetry/MapboxEventManager.java index 84df822ce9d..34ff31cd2f7 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/telemetry/MapboxEventManager.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/telemetry/MapboxEventManager.java @@ -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; @@ -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; @@ -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; @@ -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.