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

Reinitialize token dependent components when a new one is provided #15081

Merged
merged 2 commits into from
Jul 11, 2019
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,6 +5,7 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;

import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.exceptions.MapboxConfigurationException;
import com.mapbox.mapboxsdk.log.Logger;
Expand Down Expand Up @@ -84,9 +85,23 @@ public static String getAccessToken() {
/**
* Set the current active accessToken.
*/
public static void setAccessToken(String accessToken) {
public static void setAccessToken(@Nullable String accessToken) {
validateMapbox();
INSTANCE.accessToken = accessToken;

// cleanup telemetry which is dependent on an access token
if (INSTANCE.telemetry != null) {
INSTANCE.telemetry.disableTelemetrySession();
INSTANCE.telemetry = null;
}

// initialize components dependent on a token
if (isAccessTokenValid(accessToken)) {
initializeTelemetry();
INSTANCE.accounts = new AccountsManager();
} else {
INSTANCE.accounts = null;
}
FileSource.getInstance(getApplicationContext()).setAccessToken(accessToken);
}

Expand All @@ -97,6 +112,13 @@ public static void setAccessToken(String accessToken) {
* @return the SKU token
*/
public static String getSkuToken() {
if (INSTANCE.accounts == null) {
throw new MapboxConfigurationException(
"A valid 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."
+ "Currently provided token is: " + INSTANCE.accessToken);
}
return INSTANCE.accounts.getSkuToken();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mapbox.mapboxsdk.exceptions;

import android.content.Context;
import android.support.annotation.NonNull;

/**
* A MapboxConfigurationException is thrown by MapboxMap when the SDK hasn't been properly initialised.
Expand All @@ -22,4 +23,11 @@ public MapboxConfigurationException() {
+ "\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.");
}

/**
* Creates a Mapbox configuration exception thrown by MapboxMap when the SDK hasn't been properly initialised.
*/
public MapboxConfigurationException(@NonNull String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public interface TelemetryDefinition {
*/
void setUserTelemetryRequestState(boolean enabled);

/**
* Disables a started telemetry service for this session only.
*/
void disableTelemetrySession();

/**
* Set the end-user selected state to participate or opt-out in telemetry collection.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public void setUserTelemetryRequestState(boolean enabledTelemetry) {
}
}

@Override
public void disableTelemetrySession() {
telemetry.disable();
}

/**
* Set the debug logging state of telemetry.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ private FileSource(String cachePath, AssetManager assetManager) {
public native void deactivate();

@Keep
public native void setAccessToken(@NonNull String accessToken);
public native void setAccessToken(@Nullable String accessToken);

@NonNull
@Keep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@

import com.mapbox.mapboxsdk.AppCenter;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.exceptions.MapboxConfigurationException;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;

import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertSame;
import static junit.framework.Assert.assertTrue;

Expand All @@ -18,6 +26,16 @@ public class MapboxTest extends AppCenter {
private static final String ACCESS_TOKEN = "pk.0000000001";
private static final String ACCESS_TOKEN_2 = "pk.0000000002";

@Rule
public ExpectedException expectedException = ExpectedException.none();

private String realToken;

@Before
public void setup() {
realToken = Mapbox.getAccessToken();
}

@Test
@UiThreadTest
public void testConnected() {
Expand All @@ -37,11 +55,57 @@ public void testConnected() {
@Test
@UiThreadTest
public void setAccessToken() {
String realToken = Mapbox.getAccessToken();
Mapbox.setAccessToken(ACCESS_TOKEN);
assertSame(ACCESS_TOKEN, Mapbox.getAccessToken());
Mapbox.setAccessToken(ACCESS_TOKEN_2);
assertSame(ACCESS_TOKEN_2, Mapbox.getAccessToken());
}

@Test
@UiThreadTest
public void setInvalidAccessToken() {
final String invalidAccessToken = "xyz";
expectedException.expect(MapboxConfigurationException.class);
expectedException.expectMessage(
"A valid 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."
+ "Currently provided token is: " + invalidAccessToken
);

Mapbox.setAccessToken(invalidAccessToken);
assertNull(Mapbox.getTelemetry());
Mapbox.getSkuToken();
}

@Test
@UiThreadTest
public void setNullAccessToken() {
expectedException.expect(MapboxConfigurationException.class);
expectedException.expectMessage(
"A valid 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."
+ "Currently provided token is: " + null
);

Mapbox.setAccessToken(null);
assertNull(Mapbox.getTelemetry());
Mapbox.getSkuToken();
}

@Test
@UiThreadTest
public void setValidAccessToken() {
final String invalidAccessToken = "xyz";
Mapbox.setAccessToken(invalidAccessToken);
Mapbox.setAccessToken(ACCESS_TOKEN);
assertNotNull(Mapbox.getTelemetry());
assertNotNull(Mapbox.getSkuToken());
}

@After
public void tearDown() {
Mapbox.setAccessToken(realToken);
}
}
2 changes: 1 addition & 1 deletion platform/android/src/file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jni::Local<jni::String> FileSource::getAccessToken(jni::JNIEnv& env) {
}

void FileSource::setAccessToken(jni::JNIEnv& env, const jni::String& token) {
fileSource->setAccessToken(jni::Make<std::string>(env, token));
fileSource->setAccessToken(token ? jni::Make<std::string>(env, token) : "");
}

void FileSource::setAPIBaseUrl(jni::JNIEnv& env, const jni::String& url) {
Expand Down