Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.mapbox.mapboxsdk/mapbox-android-services/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.mapbox.mapboxsdk/mapbox-android-services) [![CircleCI](https://circleci.com/gh/mapbox/mapbox-java.svg?style=svg)](https://circleci.com/gh/mapbox/mapbox-java)

Mapbox Android Services contains directions, geocoding, and many more APIs to use inside your Android or Java application. This repository holds the source code for the project and is divided into several modules to make it easier for developers to only include the dependencies needed for their project.

## Getting Started

If you are looking to include this inside your project, please take a look at [the detailed instructions](https://www.mapbox.com/android-docs/mapbox-services/) found in our docs. If you are interested in building from source, read the contributing guide inside this project.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class MapboxEvent implements Serializable {

// Events and attributes for v2
public static final String MAPBOX_EVENTS_BASE_URL = "https://events.mapbox.com";
public static final String MAPBOX_EVENTS_CN_BASE_URL = "https://events.mapbox.cn";
public static final String SOURCE_MAPBOX = "mapbox";

// Event types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public void initialize(@NonNull Context context, @NonNull String accessToken, @N
validateTelemetryServiceConfigured();
setupHttpClient();
checkStagingServerInformation();
setUserAgent();
rotateSessionId();
readDisplayMetrics();
registerBatteryUpdates();
Expand Down Expand Up @@ -192,42 +193,51 @@ private void checkStagingServerInformation() {
try {
String stagingURL = null;
String stagingAccessToken = null;
boolean cnServer = false;

// Try app metadata first
ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(),
PackageManager.GET_META_DATA);
if (appInfo != null && appInfo.metaData != null) {
stagingURL = appInfo.metaData.getString(TelemetryConstants.KEY_META_DATA_STAGING_SERVER);
stagingAccessToken = appInfo.metaData.getString(TelemetryConstants.KEY_META_DATA_STAGING_ACCESS_TOKEN);
cnServer = appInfo.metaData.getBoolean(TelemetryConstants.KEY_META_DATA_CN_SERVER);
}

// Try shared preferences otherwise
if (TextUtils.isEmpty(stagingURL) || TextUtils.isEmpty(stagingAccessToken)) {
SharedPreferences prefs = TelemetryUtils.getSharedPreferences(context);
stagingURL = prefs.getString(TelemetryConstants.MAPBOX_SHARED_PREFERENCE_KEY_TELEMETRY_STAGING_URL, null);
stagingAccessToken = prefs.getString(
TelemetryConstants.MAPBOX_SHARED_PREFERENCE_KEY_TELEMETRY_STAGING_ACCESS_TOKEN, null);
}
if (cnServer) {
// Enable CN endpoint
client.setEnableCnEndpoint();
} else {
// Try shared preferences otherwise
if (TextUtils.isEmpty(stagingURL) || TextUtils.isEmpty(stagingAccessToken)) {
SharedPreferences prefs = TelemetryUtils.getSharedPreferences(context);
stagingURL = prefs.getString(TelemetryConstants.MAPBOX_SHARED_PREFERENCE_KEY_TELEMETRY_STAGING_URL, null);
stagingAccessToken = prefs.getString(
TelemetryConstants.MAPBOX_SHARED_PREFERENCE_KEY_TELEMETRY_STAGING_ACCESS_TOKEN, null);
}

// Set new client information (if needed)
if (!TextUtils.isEmpty(stagingURL) && !TextUtils.isEmpty(stagingAccessToken)) {
Log.w(LOG_TAG, String.format("Using staging server '%s' with access token '%s'.",
stagingURL, stagingAccessToken));
client.setEventsEndpoint(stagingURL);
client.setAccessToken(stagingAccessToken);
client.setStagingEnvironment(true);
// Set new client information (if needed)
if (!TextUtils.isEmpty(stagingURL) && !TextUtils.isEmpty(stagingAccessToken)) {
Log.w(LOG_TAG, String.format("Using staging server '%s' with access token '%s'.",
stagingURL, stagingAccessToken));
client.setEventsEndpoint(stagingURL);
client.setAccessToken(stagingAccessToken);
client.setStagingEnvironment(true);
}
}

// Append app identifier to user agent if present
String appIdentifier = TelemetryUtils.getApplicationIdentifier(context);
String fullUserAgent = TextUtils.isEmpty(appIdentifier) ? userAgent : Util.toHumanReadableAscii(
String.format(TelemetryConstants.DEFAULT_LOCALE, "%s %s", appIdentifier, userAgent));
client.setUserAgent(fullUserAgent);
} catch (Exception exception) {
Log.e(LOG_TAG, String.format("Failed to check for staging credentials: %s", exception.getMessage()));
}
}

private void setUserAgent() {
// Append app identifier to user agent if present
String appIdentifier = TelemetryUtils.getApplicationIdentifier(context);
String fullUserAgent = TextUtils.isEmpty(appIdentifier) ? userAgent : Util.toHumanReadableAscii(
String.format(TelemetryConstants.DEFAULT_LOCALE, "%s %s", appIdentifier, userAgent));
client.setUserAgent(fullUserAgent);
}

/**
* Changes session ID based on time boundary
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public class TelemetryConstants {

public static final int SESSION_ID_ROTATION_MS = 24 * 60 * 60 * 1_000; // 24 hours

/**
* Key used to store CN endpoint settings in AndroidManifest.xml
*/
public static final String KEY_META_DATA_CN_SERVER = "com.mapbox.CnEventsServer";

/**
* Key used to store staging data server url in AndroidManifest.xml
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public class TelemetryClient {

private String userAgent = null;
private String eventsEndpoint = MapboxEvent.MAPBOX_EVENTS_BASE_URL;
private String eventsCnEndpoint = MapboxEvent.MAPBOX_EVENTS_CN_BASE_URL;
private boolean stagingEnvironment = false;
private boolean enableCnEndpoint = false;
private OkHttpClient client;

public TelemetryClient(String accessToken) {
Expand All @@ -55,7 +57,7 @@ public void setUserAgent(String userAgent) {
}

public String getEventsEndpoint() {
return eventsEndpoint;
return isEnableCnEndpoint() ? eventsCnEndpoint : eventsEndpoint;
}

public void setEventsEndpoint(String eventsEndpoint) {
Expand All @@ -78,6 +80,14 @@ public void setStagingEnvironment(boolean stagingEnvironment) {
this.stagingEnvironment = stagingEnvironment;
}

public boolean isEnableCnEndpoint() {
return enableCnEndpoint;
}

public void setEnableCnEndpoint() {
this.enableCnEndpoint = true;
}

/**
* Based on http://square.github.io/okhttp/3.x/okhttp/okhttp3/CertificatePinner.html
*
Expand All @@ -103,7 +113,11 @@ private CertificatePinner buildCertificatePinner() {
.add("events.mapbox.com", "sha256/Tb0uHZ/KQjWh8N9+CZFLc4zx36LONQ55l6laDi1qtT4=")
.add("events.mapbox.com", "sha256/RRM1dGqnDFsCJXBTHky16vi1obOlCgFFn/yOhI/y+ho=")
.add("events.mapbox.com", "sha256/WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18=")
.add("events.mapbox.com", "sha256/yGp2XoimPmIK24X3bNV1IaK+HqvbGEgqar5nauDdC5E=");
.add("events.mapbox.com", "sha256/yGp2XoimPmIK24X3bNV1IaK+HqvbGEgqar5nauDdC5E=")
// CN - DigiCert
.add("events.mapbox.cn", "sha256/gakY+fylqW6kp6piqnaQNLZFzT8HlhzP5lsGJk5WguE=")
.add("events.mapbox.cn", "sha256/5kJvNEMw0KjrCAu7eXY5HZdvyCS13BbA0VJG1RSP91w=")
.add("events.mapbox.cn", "sha256/r/mIkG3eEpVdm+u/ko/cwxzOMo1bk4TyHIlByibiA5E=");
}

return certificatePinnerBuilder.build();
Expand Down