diff options
author | Tobrun <tobrun@mapbox.com> | 2018-05-25 11:42:57 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-25 11:42:57 +0200 |
commit | 7710934ff0a2223ae502f6ddce8080f8cf61c9b7 (patch) | |
tree | 2859653b6ee847f4e183a66a91c3c712b6b76636 /platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java | |
parent | 7d050c4cdaf65dedad5ffc740c8819b5aa4e1d47 (diff) | |
download | qtlocation-mapboxgl-7710934ff0a2223ae502f6ddce8080f8cf61c9b7.tar.gz |
[android] - remove requirement of having a mapbox access token (#12001)
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java')
-rw-r--r-- | platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java | 74 |
1 files changed, 44 insertions, 30 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."))); + } } |