diff options
author | tobrun <tobrun.van.nuland@gmail.com> | 2018-05-24 13:58:32 +0200 |
---|---|---|
committer | tobrun <tobrun.van.nuland@gmail.com> | 2018-05-24 17:55:51 +0200 |
commit | 04a0cb1eb1f936a59964fffae6d94bdfb3291072 (patch) | |
tree | 4839f730bb3dcd45636c42ae2d1bbeb3cf4ffdd9 | |
parent | 4067a61c80b90e656f129f34f4479fa0c7262b91 (diff) | |
download | qtlocation-mapboxgl-upstream/tvn-force-accesstoken.tar.gz |
[android] - remove requirement of having a mapbox access tokenupstream/tvn-force-accesstoken
3 files changed, 69 insertions, 53 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/test/java/com/mapbox/mapboxsdk/MapboxTest.java b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/MapboxTest.java index 6ee5c157b9..e8d41ce2f1 100644 --- a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/MapboxTest.java +++ b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/MapboxTest.java @@ -4,8 +4,6 @@ import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; -import com.mapbox.mapboxsdk.exceptions.MapboxConfigurationException; - import org.junit.Before; import org.junit.Test; @@ -39,13 +37,6 @@ public class MapboxTest { assertSame(accessToken, Mapbox.getAccessToken()); } - @Test(expected = MapboxConfigurationException.class) - public void testGetInvalidAccessToken() { - final String accessToken = "dummy"; - injectMapboxSingleton(accessToken); - assertSame(accessToken, Mapbox.getAccessToken()); - } - @Test public void testApplicationContext() { injectMapboxSingleton("dummy"); diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MapboxApplication.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MapboxApplication.java index fa13959112..a06a489388 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MapboxApplication.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MapboxApplication.java @@ -3,12 +3,10 @@ package com.mapbox.mapboxsdk.testapp; import android.app.Application; import android.os.StrictMode; import android.text.TextUtils; - import com.mapbox.mapboxsdk.Mapbox; import com.mapbox.mapboxsdk.maps.Telemetry; import com.mapbox.mapboxsdk.testapp.utils.TokenUtils; import com.squareup.leakcanary.LeakCanary; - import timber.log.Timber; import static timber.log.Timber.DebugTree; @@ -30,16 +28,31 @@ public class MapboxApplication extends Application { @Override public void onCreate() { super.onCreate(); + if (!initializeLeakCanary()) { + return; + } + initializeLogger(); + initializeStrictMode(); + initializeMapbox(); + } + private boolean initializeLeakCanary() { if (LeakCanary.isInAnalyzerProcess(this)) { // This process is dedicated to LeakCanary for heap analysis. // You should not init your app in this process. - return; + return false; } LeakCanary.install(this); + return true; + } - initializeLogger(); + private void initializeLogger() { + if (BuildConfig.DEBUG) { + Timber.plant(new DebugTree()); + } + } + private void initializeStrictMode() { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads() .detectDiskWrites() @@ -51,20 +64,18 @@ public class MapboxApplication extends Application { .penaltyLog() .penaltyDeath() .build()); + } - String mapboxAccessToken = TokenUtils.getMapboxAccessToken(getApplicationContext()); - if (TextUtils.isEmpty(mapboxAccessToken) || mapboxAccessToken.equals(DEFAULT_MAPBOX_ACCESS_TOKEN)) { - Timber.e(ACCESS_TOKEN_NOT_SET_MESSAGE); - } - - Mapbox.getInstance(getApplicationContext(), mapboxAccessToken); - + private void initializeMapbox() { + String accessToken = TokenUtils.getMapboxAccessToken(getApplicationContext()); + validateAccessToken(accessToken); + Mapbox.getInstance(getApplicationContext(), accessToken); Telemetry.updateDebugLoggingEnabled(true); } - private void initializeLogger() { - if (BuildConfig.DEBUG) { - Timber.plant(new DebugTree()); + private static void validateAccessToken(String accessToken) { + if (TextUtils.isEmpty(accessToken) || accessToken.equals(DEFAULT_MAPBOX_ACCESS_TOKEN)) { + Timber.e(ACCESS_TOKEN_NOT_SET_MESSAGE); } } } |