Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Remove requirement of having an access token #12001

Merged
merged 1 commit into from
May 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
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.
Expand All @@ -33,27 +34,27 @@ 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
* @param accessToken Mapbox access token
* @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;
}
Expand All @@ -63,40 +64,20 @@ public static synchronized Mapbox getInstance(@NonNull Context context, @NonNull
*
* @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;
}

Expand All @@ -108,6 +89,7 @@ public static Context getApplicationContext() {
* disconnected, and null for ConnectivityManager to determine.
*/
public static synchronized void setConnected(Boolean connected) {
validateMapbox();
// Connectivity state overridden by app
INSTANCE.connected = connected;
}
Expand All @@ -119,6 +101,7 @@ public static synchronized void setConnected(Boolean connected) {
* @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;
Expand All @@ -128,4 +111,35 @@ public static synchronized Boolean isConnected() {
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.")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.mapbox.mapboxsdk.exceptions.MapboxConfigurationException;

import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -39,13 +37,6 @@ public void testGetAccessToken() {
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
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;
Expand All @@ -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()
Expand All @@ -51,20 +64,18 @@ public void onCreate() {
.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);
}
}
}