Skip to content

Commit

Permalink
Fix route requests that include waypoint names
Browse files Browse the repository at this point in the history
  • Loading branch information
danesfeder committed Sep 7, 2018
1 parent 2cc0c33 commit a6b8342
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ private NavigationRoute.Builder buildRequestFromLocation(Location location, Rout
}
addDestination(remainingWaypoints, builder);
addWaypoints(remainingWaypoints, builder);
addWaypointNames(progress, builder);
return builder;
}

Expand All @@ -114,6 +115,13 @@ private void addWaypoints(List<Point> remainingCoordinates, NavigationRoute.Buil
}
}

private void addWaypointNames(RouteProgress progress, NavigationRoute.Builder builder) {
String[] remainingWaypointNames = routeUtils.calculateRemainingWaypointNames(progress);
if (remainingWaypointNames != null) {
builder.addWaypointNames(remainingWaypointNames);
}
}

private void executeRouteCall(NavigationRoute.Builder builder) {
if (builder != null) {
builder.accessToken(accessToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.api.directions.v5.models.LegStep;
import com.mapbox.api.directions.v5.models.RouteLeg;
import com.mapbox.api.directions.v5.models.RouteOptions;
import com.mapbox.api.directions.v5.models.VoiceInstructions;
import com.mapbox.core.utils.TextUtils;
import com.mapbox.geojson.Point;
import com.mapbox.services.android.navigation.v5.milestone.BannerInstructionMilestone;
import com.mapbox.services.android.navigation.v5.milestone.Milestone;
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -30,8 +32,8 @@ public class RouteUtils {

private static final String FORCED_LOCATION = "Forced Location";
private static final int FIRST_COORDINATE = 0;
private static final int ZERO_INSTRUCTIONS = 0;
private static final int FIRST_INSTRUCTION = 0;
private static final String SEMICOLON = ";";
private static final Set<String> VALID_PROFILES = new HashSet<String>() {
{
add(DirectionsCriteria.PROFILE_DRIVING_TRAFFIC);
Expand Down Expand Up @@ -136,6 +138,25 @@ public List<Point> calculateRemainingWaypoints(RouteProgress routeProgress) {
return coordinates;
}

/**
* Given a {@link RouteProgress}, this method will calculate the remaining waypoint names
* along the given route based on route option waypoint names and the progress remaining coordinates.
*
* @param routeProgress for route waypoint names and remaining coordinates
* @return String array of remaining waypoint names
* @since 0.19.0
*/
@Nullable
public String[] calculateRemainingWaypointNames(RouteProgress routeProgress) {
RouteOptions routeOptions = routeProgress.directionsRoute().routeOptions();
if (routeOptions == null || TextUtils.isEmpty(routeOptions.waypointNames())) {
return null;
}
String waypointNames = routeOptions.waypointNames();
String[] names = waypointNames.split(SEMICOLON);
return Arrays.copyOfRange(names, FIRST_COORDINATE, routeProgress.remainingWaypoints());
}

/**
* If navigation begins, a location update is sometimes needed to force a
* progress change update as soon as navigation is started.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.api.directions.v5.models.LegStep;
import com.mapbox.api.directions.v5.models.RouteLeg;
import com.mapbox.api.directions.v5.models.RouteOptions;
import com.mapbox.api.directions.v5.models.VoiceInstructions;
import com.mapbox.geojson.Point;
import com.mapbox.services.android.navigation.v5.BaseTest;
import com.mapbox.services.android.navigation.v5.milestone.BannerInstructionMilestone;
import com.mapbox.services.android.navigation.v5.routeprogress.RouteLegProgress;
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static junit.framework.Assert.assertEquals;
Expand Down Expand Up @@ -397,6 +400,67 @@ public void findCurrentVoiceInstructions_returnsCorrectInstructionsEndOfStepDist
assertEquals(currentStep.voiceInstructions().get(2), currentVoiceInstructions);
}

@Test
public void calculateRemainingWaypoints() {
DirectionsRoute route = mock(DirectionsRoute.class);
RouteOptions routeOptions = mock(RouteOptions.class);
when(routeOptions.coordinates()).thenReturn(buildCoordinateList());
when(route.routeOptions()).thenReturn(routeOptions);
RouteProgress routeProgress = mock(RouteProgress.class);
when(routeProgress.remainingWaypoints()).thenReturn(2);
when(routeProgress.directionsRoute()).thenReturn(route);
RouteUtils routeUtils = new RouteUtils();

List<Point> remainingWaypoints = routeUtils.calculateRemainingWaypoints(routeProgress);

assertEquals(2, remainingWaypoints.size());
}

@Test
public void calculateRemainingWaypoints_handlesNullOptions() {
DirectionsRoute route = mock(DirectionsRoute.class);
when(route.routeOptions()).thenReturn(null);
RouteProgress routeProgress = mock(RouteProgress.class);
when(routeProgress.remainingWaypoints()).thenReturn(2);
when(routeProgress.directionsRoute()).thenReturn(route);
RouteUtils routeUtils = new RouteUtils();

List<Point> remainingWaypoints = routeUtils.calculateRemainingWaypoints(routeProgress);

assertNull(remainingWaypoints);
}

@Test
public void calculateRemainingWaypointNames() {
DirectionsRoute route = mock(DirectionsRoute.class);
RouteOptions routeOptions = mock(RouteOptions.class);
when(routeOptions.coordinates()).thenReturn(buildCoordinateList());
when(routeOptions.waypointNames()).thenReturn("first;second;third;fourth");
when(route.routeOptions()).thenReturn(routeOptions);
RouteProgress routeProgress = mock(RouteProgress.class);
when(routeProgress.remainingWaypoints()).thenReturn(2);
when(routeProgress.directionsRoute()).thenReturn(route);
RouteUtils routeUtils = new RouteUtils();

String[] remainingWaypointNames = routeUtils.calculateRemainingWaypointNames(routeProgress);

assertEquals(2, remainingWaypointNames.length);
}

@Test
public void calculateRemainingWaypointNames_handlesNullOptions() {
DirectionsRoute route = mock(DirectionsRoute.class);
when(route.routeOptions()).thenReturn(null);
RouteProgress routeProgress = mock(RouteProgress.class);
when(routeProgress.remainingWaypoints()).thenReturn(2);
when(routeProgress.directionsRoute()).thenReturn(route);
RouteUtils routeUtils = new RouteUtils();

String[] remainingWaypointNames = routeUtils.calculateRemainingWaypointNames(routeProgress);

assertNull(remainingWaypointNames);
}

@NonNull
private RouteProgress buildRouteProgress(int first, DirectionsRoute route, LegStep currentStep,
LegStep upcomingStep) {
Expand All @@ -415,4 +479,13 @@ private void buildBannerInstruction(int first, BannerInstructionMilestone banner
BannerInstructions bannerInstructions = currentStepBannerInstructions.get(first);
when(bannerInstructionMilestone.getBannerInstructions()).thenReturn(bannerInstructions);
}

private List<Point> buildCoordinateList() {
List<Point> coordinates = new ArrayList<>();
coordinates.add(Point.fromLngLat(1.234, 5.678));
coordinates.add(Point.fromLngLat(1.234, 5.678));
coordinates.add(Point.fromLngLat(1.234, 5.678));
coordinates.add(Point.fromLngLat(1.234, 5.678));
return coordinates;
}
}

0 comments on commit a6b8342

Please sign in to comment.