diff options
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com')
2 files changed, 46 insertions, 32 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java index 858c1eed67..a809460375 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java @@ -5,13 +5,14 @@ import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.annotation.UiThread; import android.text.TextUtils; - import com.mapbox.mapboxsdk.constants.MapboxConstants; import com.mapbox.mapboxsdk.exceptions.MapboxConfigurationException; import com.mapbox.mapboxsdk.maps.Telemetry; import com.mapbox.mapboxsdk.net.ConnectivityReceiver; +import timber.log.Timber; /** * The entry point to initialize the Mapbox Android SDK. @@ -33,7 +34,7 @@ public final class Mapbox { /** * Get an instance of Mapbox. * <p> - * This class manages the active access token, application context, and connectivity state. + * This class manages the Mapbox access token, application context, and connectivity state. * </p> * * @param context Android context which holds or is an application context @@ -41,19 +42,19 @@ public final class Mapbox { * @return the single instance of Mapbox */ @UiThread - public static synchronized Mapbox getInstance(@NonNull Context context, @NonNull String accessToken) { + public static synchronized Mapbox getInstance(@NonNull Context context, @Nullable String accessToken) { if (INSTANCE == null) { Context appContext = context.getApplicationContext(); INSTANCE = new Mapbox(appContext, accessToken); - - Telemetry.initialize(); + if (isAccessTokenValid(accessToken)) { + initializeTelemetry(); + } ConnectivityReceiver.instance(appContext); } - return INSTANCE; } - Mapbox(@NonNull Context context, @NonNull String accessToken) { + Mapbox(@NonNull Context context, @Nullable String accessToken) { this.context = context; this.accessToken = accessToken; } @@ -63,40 +64,20 @@ public final class Mapbox { * * @return Mapbox access token */ + @Nullable public static String getAccessToken() { validateMapbox(); - validateAccessToken(); return INSTANCE.accessToken; } /** - * Runtime validation of Mapbox creation. - */ - private static void validateMapbox() throws MapboxConfigurationException { - if (INSTANCE == null) { - throw new MapboxConfigurationException(); - } - } - - /** - * Runtime validation of access token. - * - * @throws MapboxConfigurationException exception thrown when not using a valid accessToken - */ - private static void validateAccessToken() throws MapboxConfigurationException { - String accessToken = INSTANCE.accessToken; - if (TextUtils.isEmpty(accessToken) || (!accessToken.toLowerCase(MapboxConstants.MAPBOX_LOCALE).startsWith("pk.") - && !accessToken.toLowerCase(MapboxConstants.MAPBOX_LOCALE).startsWith("sk."))) { - throw new MapboxConfigurationException(); - } - } - - /** * Application context * * @return the application context */ + @NonNull public static Context getApplicationContext() { + validateMapbox(); return INSTANCE.context; } @@ -108,6 +89,7 @@ public final class Mapbox { * disconnected, and null for ConnectivityManager to determine. */ public static synchronized void setConnected(Boolean connected) { + validateMapbox(); // Connectivity state overridden by app INSTANCE.connected = connected; } @@ -119,6 +101,7 @@ public final class Mapbox { * @return true if there is an internet connection, false otherwise */ public static synchronized Boolean isConnected() { + validateMapbox(); if (INSTANCE.connected != null) { // Connectivity state overridden by app return INSTANCE.connected; @@ -128,4 +111,35 @@ public final class Mapbox { NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); return (activeNetwork != null && activeNetwork.isConnected()); } + + /** + * Initializes telemetry + */ + private static void initializeTelemetry() { + try { + Telemetry.initialize(); + } catch (Exception exception) { + Timber.e(exception); + } + } + + /** + * Runtime validation of Mapbox creation. + */ + private static void validateMapbox() { + if (INSTANCE == null) { + throw new MapboxConfigurationException(); + } + } + + /** + * Runtime validation of Mapbox access token + * + * @param accessToken the access token to validate + * @return true is valid, false otherwise + */ + private static boolean isAccessTokenValid(String accessToken) { + return !(TextUtils.isEmpty(accessToken) + || (!accessToken.toLowerCase(MapboxConstants.MAPBOX_LOCALE).startsWith("pk."))); + } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/exceptions/MapboxConfigurationException.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/exceptions/MapboxConfigurationException.java index e9a0261d85..86032aa2b5 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/exceptions/MapboxConfigurationException.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/exceptions/MapboxConfigurationException.java @@ -17,8 +17,8 @@ public class MapboxConfigurationException extends RuntimeException { * Creates a Mapbox configuration exception thrown by MapboxMap when the SDK hasn't been properly initialised. */ public MapboxConfigurationException() { - super("\nUsing MapView requires setting a valid access token. Use Mapbox.getInstance(Context context, " - + "String accessToken) to provide one. " + super("\nUsing MapView requires calling Mapbox.getInstance(Context context, String accessToken) before " + + "inflating or creating the view. The access token parameter is required when using a Mapbox service." + "\nPlease see https://www.mapbox.com/help/create-api-access-token/ to learn how to create one." + "\nMore information in this guide https://www.mapbox.com/help/first-steps-android-sdk/#access-tokens."); } |