summaryrefslogtreecommitdiff
path: root/platform
diff options
context:
space:
mode:
authorTobrun <tobrun@mapbox.com>2018-05-25 11:42:57 +0200
committerGitHub <noreply@github.com>2018-05-25 11:42:57 +0200
commit7710934ff0a2223ae502f6ddce8080f8cf61c9b7 (patch)
tree2859653b6ee847f4e183a66a91c3c712b6b76636 /platform
parent7d050c4cdaf65dedad5ffc740c8819b5aa4e1d47 (diff)
downloadqtlocation-mapboxgl-7710934ff0a2223ae502f6ddce8080f8cf61c9b7.tar.gz
[android] - remove requirement of having a mapbox access token (#12001)
Diffstat (limited to 'platform')
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java74
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/exceptions/MapboxConfigurationException.java4
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/MapboxTest.java9
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MapboxApplication.java39
4 files changed, 71 insertions, 55 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.");
}
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);
}
}
}