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
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import okhttp3.EventListener;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Converter;
Expand All @@ -51,9 +55,9 @@
* </p>
*
* @see <a href="https://www.mapbox.com/android-docs/java-sdk/overview/directions/">Android
* Directions documentation</a>
* Directions documentation</a>
* @see <a href="https://www.mapbox.com/api-documentation/navigation/#directions">Directions API
* documentation</a>
* documentation</a>
* @since 1.0.0
*/
@AutoValue
Expand Down Expand Up @@ -179,6 +183,29 @@ public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
});
}

@Override
protected synchronized OkHttpClient getOkHttpClient() {
if (okHttpClient == null) {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
if (isEnableDebug()) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
httpClient.addInterceptor(logging);
}
Interceptor interceptor = interceptor();
if (interceptor != null) {
httpClient.addInterceptor(interceptor);
}
EventListener eventListener = eventListener();
if (eventListener != null) {
httpClient.eventListener(eventListener);
}

okHttpClient = httpClient.build();
}
return okHttpClient;
}

private void errorDidOccur(@Nullable Callback<DirectionsResponse> callback,
@NonNull Response<DirectionsResponse> response) {
// Response gave an error, we try to LOGGER any messages into the LOGGER here.
Expand Down Expand Up @@ -261,8 +288,8 @@ private static String formatWaypointTargets(Point[] waypointTargets) {
coordinatesFormatted[index++] = "";
} else {
coordinatesFormatted[index++] = String.format(Locale.US, "%s,%s",
TextUtils.formatCoordinate(target.longitude()),
TextUtils.formatCoordinate(target.latitude()));
TextUtils.formatCoordinate(target.longitude()),
TextUtils.formatCoordinate(target.latitude()));
}
}
return TextUtils.join(";", coordinatesFormatted);
Expand Down Expand Up @@ -338,6 +365,12 @@ private static String formatWaypointTargets(Point[] waypointTargets) {
@Nullable
abstract String waypointTargets();

@Nullable
abstract Interceptor interceptor();

@Nullable
abstract EventListener eventListener();

/**
* Build a new {@link MapboxDirections} object with the initial values set for
* {@link #baseUrl()}, {@link #profile()}, {@link #user()}, and {@link #geometries()}.
Expand Down Expand Up @@ -533,7 +566,7 @@ public Builder addWaypoint(@NonNull Point waypoint) {
* written in when returned
* @return this builder for chaining options together
* @see <a href="https://www.mapbox.com/api-documentation/navigation/#instructions-languages">Supported
* Languages</a>
* Languages</a>
* @since 2.2.0
*/
public Builder language(@Nullable Locale language) {
Expand Down Expand Up @@ -568,7 +601,7 @@ public Builder language(@Nullable Locale language) {
* or null which will result in no annotations being used
* @return this builder for chaining options together
* @see <a href="https://www.mapbox.com/api-documentation/navigation/#route-leg-object">RouteLeg object
* documentation</a>
* documentation</a>
* @since 2.1.0
*/
public Builder annotations(@Nullable @AnnotationCriteria String... annotations) {
Expand Down Expand Up @@ -714,8 +747,23 @@ public Builder radiuses(@FloatRange(from = 0) double... radiuses) {
*/
public abstract Builder baseUrl(String baseUrl);

abstract Builder coordinates(@NonNull List<Point> coordinates);
/**
* Adds an optional interceptor to set in the OkHttp client.
*
* @param interceptor to set for OkHttp
* @return this builder for chaining options together
*/
public abstract Builder interceptor(Interceptor interceptor);

/**
* Adds an optional event listener to set in the OkHttp client.
*
* @param eventListener to set for OkHttp
* @return this builder for chaining options together
*/
public abstract Builder eventListener(EventListener eventListener);

abstract Builder coordinates(@NonNull List<Point> coordinates);

/**
* Indicates from which side of the road to approach a waypoint.
Expand Down Expand Up @@ -744,6 +792,7 @@ public Builder addApproaches(String... approaches) {
* each separated by ; . Values can be any string and total number of all characters cannot
* exceed 500. If provided, the list of waypointNames must be the same length as the list of
* coordinates, but you can skip a coordinate and show its position with the ; separator.
*
* @param waypointNames Custom names for waypoints used for the arrival instruction.
* @return this builder for chaining options together
* @since 3.3.0
Expand All @@ -761,6 +810,7 @@ public Builder addWaypointNames(@Nullable String... waypointNames) {
* The number of waypoint targets must be the same as the number of coordinates,
* but you can skip a coordinate with a null value.
* Must be used with steps=true.
*
* @param waypointTargets list of coordinate points for drop-off locations
* @return this builder for chaining options together
* @since 4.3.0
Expand Down Expand Up @@ -807,7 +857,7 @@ public MapboxDirections build() {
if (waypointTargets != null) {
if (waypointTargets.length != coordinates.size()) {
throw new ServicesException("Number of waypoint targets must match "
+ " the number of waypoints provided.");
+ " the number of waypoints provided.");
}

waypointTargets(formatWaypointTargets(waypointTargets));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.api.directions.v5.models.LegAnnotation;
import com.mapbox.api.directions.v5.models.RouteOptions;
import com.mapbox.api.directions.v5.models.StepManeuver;
import com.mapbox.core.TestUtils;
import com.mapbox.core.exceptions.ServicesException;
import com.mapbox.geojson.Point;
Expand All @@ -22,12 +21,14 @@
import org.junit.rules.ExpectedException;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import okhttp3.Call;
import okhttp3.EventListener;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
Expand Down Expand Up @@ -730,4 +731,52 @@ public void testWithWaypointTargets() throws Exception {

}

@Test
public void testWithInterceptor() throws Exception {
Interceptor interceptor = new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
return null;
}
};
MapboxDirections mapboxDirections = MapboxDirections.builder()
.profile(PROFILE_CYCLING)
.origin(Point.fromLngLat(-122.42,37.78))
.destination(Point.fromLngLat(-77.03,38.91))
.steps(true)
.voiceInstructions(true)
.voiceUnits(DirectionsCriteria.IMPERIAL)
.addWaypointNames("Home", "Work")
.accessToken(ACCESS_TOKEN)
.baseUrl(mockUrl.toString())
.interceptor(interceptor)
.build();

assertEquals(interceptor, mapboxDirections.interceptor());
}

@Test
public void testWithEventListener() throws Exception {
EventListener eventListener = new EventListener() {
@Override
public void callStart(Call call) {
super.callStart(call);
}
};
MapboxDirections mapboxDirections = MapboxDirections.builder()
.profile(PROFILE_CYCLING)
.origin(Point.fromLngLat(-122.42,37.78))
.destination(Point.fromLngLat(-77.03,38.91))
.steps(true)
.voiceInstructions(true)
.voiceUnits(DirectionsCriteria.IMPERIAL)
.addWaypointNames("Home", "Work")
.accessToken(ACCESS_TOKEN)
.baseUrl(mockUrl.toString())
.eventListener(eventListener)
.build();


assertEquals(eventListener, mapboxDirections.eventListener());
}
}