Skip to content

Commit fa4fc60

Browse files
author
Guardiola31337
committed
update route progress and banner instruction milestone for banner data from navigator
1 parent 7cf3955 commit fa4fc60

File tree

5 files changed

+115
-96
lines changed

5 files changed

+115
-96
lines changed

gradle/dependencies.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ext {
1111
mapboxMapSdk : '6.7.2',
1212
mapboxSdkServices : '4.2.0',
1313
mapboxEvents : '3.5.6',
14-
mapboxNavigator : '3.4.9',
14+
mapboxNavigator : 'components-SNAPSHOT-10',
1515
searchSdk : '0.1.0-SNAPSHOT',
1616
autoValue : '1.5.4',
1717
autoValueParcel : '0.2.5',

libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/milestone/BannerInstructionMilestone.java

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package com.mapbox.services.android.navigation.v5.milestone;
22

3+
import com.mapbox.api.directions.v5.models.BannerComponents;
34
import com.mapbox.api.directions.v5.models.BannerInstructions;
4-
import com.mapbox.api.directions.v5.models.LegStep;
5-
import com.mapbox.services.android.navigation.v5.routeprogress.RouteLegProgress;
5+
import com.mapbox.api.directions.v5.models.BannerText;
6+
import com.mapbox.navigator.BannerComponent;
7+
import com.mapbox.navigator.BannerInstruction;
8+
import com.mapbox.navigator.BannerSection;
69
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;
7-
import com.mapbox.services.android.navigation.v5.utils.RouteUtils;
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
813

914
/**
1015
* A default milestone that is added to {@link com.mapbox.services.android.navigation.v5.navigation.MapboxNavigation}
@@ -16,26 +21,69 @@
1621
public class BannerInstructionMilestone extends Milestone {
1722

1823
private BannerInstructions instructions;
19-
private RouteUtils routeUtils;
2024

2125
BannerInstructionMilestone(Builder builder) {
2226
super(builder);
23-
routeUtils = new RouteUtils();
2427
}
2528

2629
@Override
2730
public boolean isOccurring(RouteProgress previousRouteProgress, RouteProgress routeProgress) {
28-
RouteLegProgress legProgress = routeProgress.currentLegProgress();
29-
LegStep currentStep = legProgress.currentStep();
30-
double stepDistanceRemaining = legProgress.currentStepProgress().distanceRemaining();
31-
BannerInstructions instructions = routeUtils.findCurrentBannerInstructions(currentStep, stepDistanceRemaining);
32-
if (shouldBeShown(instructions, stepDistanceRemaining)) {
33-
this.instructions = instructions;
31+
return updateCurrentBanner(routeProgress);
32+
}
33+
34+
private boolean updateCurrentBanner(RouteProgress routeProgress) {
35+
BannerInstruction currentBannerInstruction = routeProgress.bannerInstruction();
36+
if (currentBannerInstruction != null) {
37+
BannerSection currentPrimary = currentBannerInstruction.getPrimary();
38+
BannerText primary = retrieveBannerFrom(currentPrimary);
39+
BannerSection currentSecondary = currentBannerInstruction.getSecondary();
40+
BannerText secondary = retrieveBannerFrom(currentSecondary);
41+
BannerSection currentSub = currentBannerInstruction.getSub();
42+
BannerText sub = retrieveBannerFrom(currentSub);
43+
44+
this.instructions = BannerInstructions.builder()
45+
.primary(primary)
46+
.secondary(secondary)
47+
.sub(sub)
48+
.distanceAlongGeometry(currentBannerInstruction.getRemainingStepDistance())
49+
.build();
3450
return true;
3551
}
3652
return false;
3753
}
3854

55+
private BannerText retrieveBannerFrom(BannerSection bannerSection) {
56+
BannerText banner = null;
57+
if (bannerSection == null) {
58+
return banner;
59+
}
60+
List<BannerComponent> currentComponents = bannerSection.getComponents();
61+
if (!currentComponents.isEmpty()) {
62+
List<BannerComponents> primaryCommponents = new ArrayList<>();
63+
for (BannerComponent bannerComponent : currentComponents) {
64+
BannerComponents bannerComponents = BannerComponents.builder()
65+
.text(bannerComponent.getText())
66+
.type(bannerComponent.getType())
67+
.abbreviation(bannerComponent.getAbbr())
68+
.abbreviationPriority(bannerComponent.getAbbrPriority())
69+
.imageBaseUrl(bannerComponent.getImageBaseurl())
70+
.directions(bannerComponent.getDirections())
71+
.active(bannerComponent.getActive())
72+
.build();
73+
primaryCommponents.add(bannerComponents);
74+
}
75+
banner = BannerText.builder()
76+
.text(bannerSection.getText())
77+
.type(bannerSection.getType())
78+
.modifier(bannerSection.getModifier())
79+
.degrees((double) bannerSection.getDegrees())
80+
.drivingSide(bannerSection.getDrivingSide())
81+
.components(primaryCommponents)
82+
.build();
83+
}
84+
return banner;
85+
}
86+
3987
/**
4088
* Returns the given {@link BannerInstructions} for the time that the milestone is triggered.
4189
*
@@ -46,22 +94,6 @@ public BannerInstructions getBannerInstructions() {
4694
return instructions;
4795
}
4896

49-
/**
50-
* Uses the current step distance remaining to check against banner instructions distance.
51-
*
52-
* @param instructions given banner instructions from the list of step instructions
53-
* @param stepDistanceRemaining distance remaining along the current step
54-
* @return true if time to show the instructions, false if not
55-
*/
56-
private boolean shouldBeShown(BannerInstructions instructions, double stepDistanceRemaining) {
57-
boolean isNewInstruction = this.instructions == null || !this.instructions.equals(instructions);
58-
boolean isValidNewInstruction = instructions != null && isNewInstruction;
59-
boolean withinDistanceAlongGeometry = isValidNewInstruction
60-
&& instructions.distanceAlongGeometry() >= stepDistanceRemaining;
61-
boolean isFirstInstruction = this.instructions == null && instructions != null;
62-
return isFirstInstruction || withinDistanceAlongGeometry;
63-
}
64-
6597
public static final class Builder extends Milestone.Builder {
6698

6799
private Trigger.Statement trigger;

libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/navigation/NavigationRouteProcessor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.mapbox.api.directions.v5.models.RouteLeg;
99
import com.mapbox.api.directions.v5.models.StepIntersection;
1010
import com.mapbox.geojson.Point;
11+
import com.mapbox.navigator.BannerInstruction;
1112
import com.mapbox.navigator.NavigationStatus;
1213
import com.mapbox.navigator.RouteState;
1314
import com.mapbox.navigator.VoiceInstruction;
@@ -104,8 +105,8 @@ private RouteProgress buildRouteProgressFrom(NavigationStatus status) {
104105
.inTunnel(status.getInTunnel())
105106
.currentState(currentRouteState);
106107

107-
// TODO build banner instructions from status here
108108
addVoiceInstructions(status, progressBuilder);
109+
addBannerInstructions(status, progressBuilder);
109110
addUpcomingStepPoints(progressBuilder);
110111
return progressBuilder.build();
111112
}
@@ -142,4 +143,9 @@ private void addVoiceInstructions(NavigationStatus status, RouteProgress.Builder
142143
VoiceInstruction voiceInstruction = status.getVoiceInstruction();
143144
progressBuilder.voiceInstruction(voiceInstruction);
144145
}
146+
147+
private void addBannerInstructions(NavigationStatus status, RouteProgress.Builder progressBuilder) {
148+
BannerInstruction bannerInstruction = status.getBannerInstruction();
149+
progressBuilder.bannerInstruction(bannerInstruction);
150+
}
145151
}

libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/routeprogress/RouteProgress.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.mapbox.api.directions.v5.models.RouteLeg;
1010
import com.mapbox.api.directions.v5.models.StepIntersection;
1111
import com.mapbox.geojson.Point;
12+
import com.mapbox.navigator.BannerInstruction;
1213
import com.mapbox.navigator.VoiceInstruction;
1314

1415
import java.util.List;
@@ -168,6 +169,15 @@ public int remainingWaypoints() {
168169
@Nullable
169170
public abstract VoiceInstruction voiceInstruction();
170171

172+
/**
173+
* Current banner instruction.
174+
*
175+
* @return current banner instruction
176+
* @since 0.25.0
177+
*/
178+
@Nullable
179+
public abstract BannerInstruction bannerInstruction();
180+
171181
/**
172182
* Returns the current state of progress along the route. Provides route and location tracking
173183
* information.
@@ -264,6 +274,8 @@ public abstract Builder intersectionDistancesAlongStep(
264274

265275
public abstract Builder voiceInstruction(@Nullable VoiceInstruction voiceInstruction);
266276

277+
public abstract Builder bannerInstruction(@Nullable BannerInstruction bannerInstruction);
278+
267279
public abstract Builder currentState(@Nullable RouteProgressState currentState);
268280

269281
abstract RouteProgress autoBuild(); // not public

libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/milestone/BannerInstructionMilestoneTest.java

Lines changed: 36 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,76 @@
11
package com.mapbox.services.android.navigation.v5.milestone;
22

3-
import com.mapbox.api.directions.v5.models.BannerInstructions;
4-
import com.mapbox.api.directions.v5.models.LegStep;
3+
import com.mapbox.navigator.BannerComponent;
4+
import com.mapbox.navigator.BannerInstruction;
5+
import com.mapbox.navigator.BannerSection;
56
import com.mapbox.services.android.navigation.v5.BaseTest;
67
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;
78

89
import org.junit.Test;
910

10-
import java.util.List;
11+
import java.util.ArrayList;
1112

12-
import static junit.framework.Assert.assertEquals;
13-
import static junit.framework.Assert.assertFalse;
14-
import static junit.framework.Assert.assertNotNull;
15-
import static junit.framework.Assert.assertTrue;
13+
import static org.junit.Assert.assertEquals;
14+
import static org.junit.Assert.assertFalse;
15+
import static org.junit.Assert.assertTrue;
1616

1717
public class BannerInstructionMilestoneTest extends BaseTest {
1818

1919
@Test
20-
public void sanity() {
21-
BannerInstructionMilestone milestone = buildBannerInstructionMilestone();
22-
23-
assertNotNull(milestone);
24-
}
25-
26-
@Test
27-
public void onBeginningOfStep_bannerInstructionsShouldTrigger() throws Exception {
20+
public void checksNotBannerShownIfBannerInstructionIsNull() throws Exception {
2821
RouteProgress routeProgress = buildDefaultTestRouteProgress();
29-
routeProgress = createBeginningOfStepRouteProgress(routeProgress);
22+
BannerInstruction nullBannerInstruction = null;
23+
routeProgress = add(nullBannerInstruction, routeProgress);
3024
BannerInstructionMilestone milestone = buildBannerInstructionMilestone();
3125

3226
boolean isOccurring = milestone.isOccurring(routeProgress, routeProgress);
3327

34-
assertTrue(isOccurring);
35-
}
36-
37-
@Test
38-
public void onSameInstructionOccurring_milestoneDoesNotTriggerTwice() throws Exception {
39-
RouteProgress routeProgress = buildDefaultTestRouteProgress();
40-
RouteProgress firstProgress = createBeginningOfStepRouteProgress(routeProgress);
41-
double fortyMetersIntoStep = routeProgress.currentLegProgress().currentStep().distance() - 40;
42-
RouteProgress secondProgress = routeProgress.toBuilder()
43-
.stepDistanceRemaining(fortyMetersIntoStep)
44-
.stepIndex(0)
45-
.build();
46-
BannerInstructionMilestone milestone = buildBannerInstructionMilestone();
47-
48-
milestone.isOccurring(firstProgress, firstProgress);
49-
boolean shouldNotBeOccurring = milestone.isOccurring(firstProgress, secondProgress);
50-
51-
assertFalse(shouldNotBeOccurring);
28+
assertFalse(isOccurring);
5229
}
5330

5431
@Test
55-
public void nullInstructions_MilestoneDoesNotGetTriggered() throws Exception {
32+
public void checksBannerShownIfBannerInstructionIsNotNull() throws Exception {
5633
RouteProgress routeProgress = buildDefaultTestRouteProgress();
57-
LegStep currentStep = routeProgress.currentLegProgress().currentStep();
58-
List<BannerInstructions> instructions = currentStep.bannerInstructions();
59-
instructions.clear();
60-
routeProgress = createBeginningOfStepRouteProgress(routeProgress);
34+
BannerInstruction emptyBannerInstruction = buildEmptyBannerInstructionWithPrimary(null);
35+
routeProgress = add(emptyBannerInstruction, routeProgress);
6136
BannerInstructionMilestone milestone = buildBannerInstructionMilestone();
6237

6338
boolean isOccurring = milestone.isOccurring(routeProgress, routeProgress);
6439

65-
assertFalse(isOccurring);
40+
assertTrue(isOccurring);
6641
}
6742

6843
@Test
69-
public void onOccurringMilestone_beginningOfStep_bannerInstructionsAreReturned() throws Exception {
44+
public void checksBannerMappingIfBannerSectionHasComponents() throws Exception {
7045
RouteProgress routeProgress = buildDefaultTestRouteProgress();
71-
routeProgress = routeProgress.toBuilder()
72-
.stepDistanceRemaining(routeProgress.currentLegProgress().currentStep().distance())
73-
.stepIndex(1)
74-
.build();
75-
BannerInstructions instructions = routeProgress.currentLegProgress().currentStep().bannerInstructions().get(0);
46+
int anAbbrPriority = 0;
47+
boolean isActive = true;
48+
ArrayList<String> anyDirections = new ArrayList<>();
49+
anyDirections.add("a direction");
50+
BannerComponent aComponent = new BannerComponent("a type", "a text", "an abbr", anAbbrPriority,
51+
"an image base url", isActive, anyDirections);
52+
ArrayList<BannerComponent> components = new ArrayList<>();
53+
components.add(aComponent);
54+
BannerSection sectionWithComponents = new BannerSection("a text", "a type", "a modifier", 60, "a driving side",
55+
components);
56+
BannerInstruction emptyBannerInstruction = buildEmptyBannerInstructionWithPrimary(sectionWithComponents);
57+
routeProgress = add(emptyBannerInstruction, routeProgress);
7658
BannerInstructionMilestone milestone = buildBannerInstructionMilestone();
7759

78-
milestone.isOccurring(routeProgress, routeProgress);
60+
boolean isOccurring = milestone.isOccurring(routeProgress, routeProgress);
7961

80-
assertEquals(instructions, milestone.getBannerInstructions());
62+
assertTrue(isOccurring);
63+
assertEquals(1, routeProgress.bannerInstruction().getPrimary().getComponents().size());
8164
}
8265

83-
@Test
84-
public void onOccurringMilestone_endOfStep_bannerInstructionsAreReturned() throws Exception {
85-
RouteProgress routeProgress = buildDefaultTestRouteProgress();
86-
int tenMetersRemainingInStep = 10;
87-
routeProgress = routeProgress.toBuilder()
88-
.stepDistanceRemaining(tenMetersRemainingInStep)
89-
.stepIndex(1)
66+
private RouteProgress add(BannerInstruction bannerInstruction, RouteProgress routeProgress) {
67+
return routeProgress.toBuilder()
68+
.bannerInstruction(bannerInstruction)
9069
.build();
91-
List<BannerInstructions> bannerInstructions = routeProgress.currentLegProgress().currentStep().bannerInstructions();
92-
BannerInstructions instructions = bannerInstructions.get(bannerInstructions.size() - 1);
93-
BannerInstructionMilestone milestone = buildBannerInstructionMilestone();
94-
95-
milestone.isOccurring(routeProgress, routeProgress);
96-
97-
assertEquals(instructions, milestone.getBannerInstructions());
9870
}
9971

100-
private RouteProgress createBeginningOfStepRouteProgress(RouteProgress routeProgress) {
101-
return routeProgress.toBuilder()
102-
.stepDistanceRemaining(routeProgress.currentLegProgress().currentStep().distance())
103-
.stepIndex(0)
104-
.build();
72+
private BannerInstruction buildEmptyBannerInstructionWithPrimary(BannerSection section) {
73+
return new BannerInstruction(section, null, null, -1, -1);
10574
}
10675

10776
private BannerInstructionMilestone buildBannerInstructionMilestone() {

0 commit comments

Comments
 (0)