11package com .mapbox .services .android .navigation .v5 .milestone ;
22
3+ import com .mapbox .api .directions .v5 .models .BannerComponents ;
34import 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 ;
69import 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}
1621public class BannerInstructionMilestone extends Milestone {
1722
1823 private BannerInstructions instructions ;
19- private RouteUtils routeUtils ;
2024
21- BannerInstructionMilestone (Builder builder ) {
25+ private 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 != null ) {
62+ List <BannerComponents > primaryComponents = 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+ primaryComponents .add (bannerComponents );
74+ }
75+ Integer bannerSectionDegrees = bannerSection .getDegrees ();
76+ Double degrees = null ;
77+ if (bannerSectionDegrees != null ) {
78+ degrees = Double .valueOf (bannerSectionDegrees );
79+ }
80+ banner = BannerText .builder ()
81+ .text (bannerSection .getText ())
82+ .type (bannerSection .getType ())
83+ .modifier (bannerSection .getModifier ())
84+ .degrees (degrees )
85+ .drivingSide (bannerSection .getDrivingSide ())
86+ .components (primaryComponents )
87+ .build ();
88+ }
89+ return banner ;
90+ }
91+
3992 /**
4093 * Returns the given {@link BannerInstructions} for the time that the milestone is triggered.
4194 *
@@ -46,22 +99,6 @@ public BannerInstructions getBannerInstructions() {
4699 return instructions ;
47100 }
48101
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-
65102 public static final class Builder extends Milestone .Builder {
66103
67104 private Trigger .Statement trigger ;
0 commit comments