Skip to content

Commit 04e9367

Browse files
authored
Add support for CN endpoint (mapbox#540)
* add support for CN endpoint * add support for CN endpoint * document new key * force ci
1 parent 90c8ae7 commit 04e9367

5 files changed

Lines changed: 53 additions & 23 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![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)
44

55
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.
6-
6+
77
## Getting Started
88

99
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.

mapbox/libandroid-telemetry/src/main/java/com/mapbox/services/android/telemetry/MapboxEvent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class MapboxEvent implements Serializable {
1515

1616
// Events and attributes for v2
1717
public static final String MAPBOX_EVENTS_BASE_URL = "https://events.mapbox.com";
18+
public static final String MAPBOX_EVENTS_CN_BASE_URL = "https://events.mapbox.cn";
1819
public static final String SOURCE_MAPBOX = "mapbox";
1920

2021
// Event types

mapbox/libandroid-telemetry/src/main/java/com/mapbox/services/android/telemetry/MapboxTelemetry.java

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public void initialize(@NonNull Context context, @NonNull String accessToken, @N
130130
validateTelemetryServiceConfigured();
131131
setupHttpClient();
132132
checkStagingServerInformation();
133+
setUserAgent();
133134
rotateSessionId();
134135
readDisplayMetrics();
135136
registerBatteryUpdates();
@@ -192,42 +193,51 @@ private void checkStagingServerInformation() {
192193
try {
193194
String stagingURL = null;
194195
String stagingAccessToken = null;
196+
boolean cnServer = false;
195197

196198
// Try app metadata first
197199
ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(),
198200
PackageManager.GET_META_DATA);
199201
if (appInfo != null && appInfo.metaData != null) {
200202
stagingURL = appInfo.metaData.getString(TelemetryConstants.KEY_META_DATA_STAGING_SERVER);
201203
stagingAccessToken = appInfo.metaData.getString(TelemetryConstants.KEY_META_DATA_STAGING_ACCESS_TOKEN);
204+
cnServer = appInfo.metaData.getBoolean(TelemetryConstants.KEY_META_DATA_CN_SERVER);
202205
}
203206

204-
// Try shared preferences otherwise
205-
if (TextUtils.isEmpty(stagingURL) || TextUtils.isEmpty(stagingAccessToken)) {
206-
SharedPreferences prefs = TelemetryUtils.getSharedPreferences(context);
207-
stagingURL = prefs.getString(TelemetryConstants.MAPBOX_SHARED_PREFERENCE_KEY_TELEMETRY_STAGING_URL, null);
208-
stagingAccessToken = prefs.getString(
209-
TelemetryConstants.MAPBOX_SHARED_PREFERENCE_KEY_TELEMETRY_STAGING_ACCESS_TOKEN, null);
210-
}
207+
if (cnServer) {
208+
// Enable CN endpoint
209+
client.setEnableCnEndpoint();
210+
} else {
211+
// Try shared preferences otherwise
212+
if (TextUtils.isEmpty(stagingURL) || TextUtils.isEmpty(stagingAccessToken)) {
213+
SharedPreferences prefs = TelemetryUtils.getSharedPreferences(context);
214+
stagingURL = prefs.getString(TelemetryConstants.MAPBOX_SHARED_PREFERENCE_KEY_TELEMETRY_STAGING_URL, null);
215+
stagingAccessToken = prefs.getString(
216+
TelemetryConstants.MAPBOX_SHARED_PREFERENCE_KEY_TELEMETRY_STAGING_ACCESS_TOKEN, null);
217+
}
211218

212-
// Set new client information (if needed)
213-
if (!TextUtils.isEmpty(stagingURL) && !TextUtils.isEmpty(stagingAccessToken)) {
214-
Log.w(LOG_TAG, String.format("Using staging server '%s' with access token '%s'.",
215-
stagingURL, stagingAccessToken));
216-
client.setEventsEndpoint(stagingURL);
217-
client.setAccessToken(stagingAccessToken);
218-
client.setStagingEnvironment(true);
219+
// Set new client information (if needed)
220+
if (!TextUtils.isEmpty(stagingURL) && !TextUtils.isEmpty(stagingAccessToken)) {
221+
Log.w(LOG_TAG, String.format("Using staging server '%s' with access token '%s'.",
222+
stagingURL, stagingAccessToken));
223+
client.setEventsEndpoint(stagingURL);
224+
client.setAccessToken(stagingAccessToken);
225+
client.setStagingEnvironment(true);
226+
}
219227
}
220-
221-
// Append app identifier to user agent if present
222-
String appIdentifier = TelemetryUtils.getApplicationIdentifier(context);
223-
String fullUserAgent = TextUtils.isEmpty(appIdentifier) ? userAgent : Util.toHumanReadableAscii(
224-
String.format(TelemetryConstants.DEFAULT_LOCALE, "%s %s", appIdentifier, userAgent));
225-
client.setUserAgent(fullUserAgent);
226228
} catch (Exception exception) {
227229
Log.e(LOG_TAG, String.format("Failed to check for staging credentials: %s", exception.getMessage()));
228230
}
229231
}
230232

233+
private void setUserAgent() {
234+
// Append app identifier to user agent if present
235+
String appIdentifier = TelemetryUtils.getApplicationIdentifier(context);
236+
String fullUserAgent = TextUtils.isEmpty(appIdentifier) ? userAgent : Util.toHumanReadableAscii(
237+
String.format(TelemetryConstants.DEFAULT_LOCALE, "%s %s", appIdentifier, userAgent));
238+
client.setUserAgent(fullUserAgent);
239+
}
240+
231241
/**
232242
* Changes session ID based on time boundary
233243
*/

mapbox/libandroid-telemetry/src/main/java/com/mapbox/services/android/telemetry/constants/TelemetryConstants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public class TelemetryConstants {
2222

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

25+
/**
26+
* Key used to store CN endpoint settings in AndroidManifest.xml
27+
*/
28+
public static final String KEY_META_DATA_CN_SERVER = "com.mapbox.CnEventsServer";
29+
2530
/**
2631
* Key used to store staging data server url in AndroidManifest.xml
2732
*/

mapbox/libandroid-telemetry/src/main/java/com/mapbox/services/android/telemetry/http/TelemetryClient.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ public class TelemetryClient {
3434

3535
private String userAgent = null;
3636
private String eventsEndpoint = MapboxEvent.MAPBOX_EVENTS_BASE_URL;
37+
private String eventsCnEndpoint = MapboxEvent.MAPBOX_EVENTS_CN_BASE_URL;
3738
private boolean stagingEnvironment = false;
39+
private boolean enableCnEndpoint = false;
3840
private OkHttpClient client;
3941

4042
public TelemetryClient(String accessToken) {
@@ -55,7 +57,7 @@ public void setUserAgent(String userAgent) {
5557
}
5658

5759
public String getEventsEndpoint() {
58-
return eventsEndpoint;
60+
return isEnableCnEndpoint() ? eventsCnEndpoint : eventsEndpoint;
5961
}
6062

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

83+
public boolean isEnableCnEndpoint() {
84+
return enableCnEndpoint;
85+
}
86+
87+
public void setEnableCnEndpoint() {
88+
this.enableCnEndpoint = true;
89+
}
90+
8191
/**
8292
* Based on http://square.github.io/okhttp/3.x/okhttp/okhttp3/CertificatePinner.html
8393
*
@@ -103,7 +113,11 @@ private CertificatePinner buildCertificatePinner() {
103113
.add("events.mapbox.com", "sha256/Tb0uHZ/KQjWh8N9+CZFLc4zx36LONQ55l6laDi1qtT4=")
104114
.add("events.mapbox.com", "sha256/RRM1dGqnDFsCJXBTHky16vi1obOlCgFFn/yOhI/y+ho=")
105115
.add("events.mapbox.com", "sha256/WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18=")
106-
.add("events.mapbox.com", "sha256/yGp2XoimPmIK24X3bNV1IaK+HqvbGEgqar5nauDdC5E=");
116+
.add("events.mapbox.com", "sha256/yGp2XoimPmIK24X3bNV1IaK+HqvbGEgqar5nauDdC5E=")
117+
// CN - DigiCert
118+
.add("events.mapbox.cn", "sha256/gakY+fylqW6kp6piqnaQNLZFzT8HlhzP5lsGJk5WguE=")
119+
.add("events.mapbox.cn", "sha256/5kJvNEMw0KjrCAu7eXY5HZdvyCS13BbA0VJG1RSP91w=")
120+
.add("events.mapbox.cn", "sha256/r/mIkG3eEpVdm+u/ko/cwxzOMo1bk4TyHIlByibiA5E=");
107121
}
108122

109123
return certificatePinnerBuilder.build();

0 commit comments

Comments
 (0)