[android] throw configuration exception when accessing Mapbox services with an invalid token

This commit is contained in:
Łukasz Paczos
2019-07-09 12:35:38 +02:00
committed by Łukasz Paczos
parent 1c931e4006
commit 348c437c0a
5 changed files with 105 additions and 1 deletions

View File

@@ -5,6 +5,7 @@ import android.content.Context;
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;
@@ -87,6 +88,20 @@ public final class Mapbox {
public static void setAccessToken(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);
}
@@ -97,6 +112,13 @@ public final class Mapbox {
* @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();
}

View File

@@ -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.
@@ -22,4 +23,11 @@ public class MapboxConfigurationException extends RuntimeException {
+ "\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);
}
}

View File

@@ -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.
*/

View File

@@ -79,6 +79,11 @@ public class TelemetryImpl implements TelemetryDefinition {
}
}
@Override
public void disableTelemetrySession() {
telemetry.disable();
}
/**
* Set the debug logging state of telemetry.
*