11package com .mapbox .services .android .navigation .ui .v5 .instruction .maneuver ;
22
33import android .content .Context ;
4+ import android .content .res .TypedArray ;
45import android .graphics .Canvas ;
56import android .graphics .PointF ;
7+ import android .support .annotation .ColorInt ;
8+ import android .support .annotation .FloatRange ;
69import android .support .annotation .NonNull ;
710import android .support .annotation .Nullable ;
11+ import android .support .v4 .content .ContextCompat ;
812import android .support .v4 .util .Pair ;
913import android .text .TextUtils ;
1014import android .util .AttributeSet ;
1115import android .view .View ;
1216
1317import com .mapbox .services .android .navigation .ui .v5 .R ;
14- import com .mapbox .services .android .navigation .ui .v5 .ThemeSwitcher ;
1518
1619import java .util .HashSet ;
1720import java .util .Map ;
2023import static com .mapbox .services .android .navigation .v5 .navigation .NavigationConstants .ManeuverModifier ;
2124import static com .mapbox .services .android .navigation .v5 .navigation .NavigationConstants .ManeuverType ;
2225import static com .mapbox .services .android .navigation .v5 .navigation .NavigationConstants .STEP_MANEUVER_MODIFIER_LEFT ;
23- import static com .mapbox .services .android .navigation .v5 .navigation .NavigationConstants
24- .STEP_MANEUVER_MODIFIER_SHARP_LEFT ;
25- import static com .mapbox .services .android .navigation .v5 .navigation .NavigationConstants
26- .STEP_MANEUVER_MODIFIER_SLIGHT_LEFT ;
26+ import static com .mapbox .services .android .navigation .v5 .navigation .NavigationConstants .STEP_MANEUVER_MODIFIER_SHARP_LEFT ;
27+ import static com .mapbox .services .android .navigation .v5 .navigation .NavigationConstants .STEP_MANEUVER_MODIFIER_SLIGHT_LEFT ;
2728import static com .mapbox .services .android .navigation .v5 .navigation .NavigationConstants .STEP_MANEUVER_MODIFIER_UTURN ;
2829import static com .mapbox .services .android .navigation .v5 .navigation .NavigationConstants .STEP_MANEUVER_TYPE_ARRIVE ;
2930import static com .mapbox .services .android .navigation .v5 .navigation .NavigationConstants .STEP_MANEUVER_TYPE_EXIT_ROTARY ;
30- import static com .mapbox .services .android .navigation .v5 .navigation .NavigationConstants
31- .STEP_MANEUVER_TYPE_EXIT_ROUNDABOUT ;
31+ import static com .mapbox .services .android .navigation .v5 .navigation .NavigationConstants .STEP_MANEUVER_TYPE_EXIT_ROUNDABOUT ;
3232import static com .mapbox .services .android .navigation .v5 .navigation .NavigationConstants .STEP_MANEUVER_TYPE_FORK ;
3333import static com .mapbox .services .android .navigation .v5 .navigation .NavigationConstants .STEP_MANEUVER_TYPE_OFF_RAMP ;
3434import static com .mapbox .services .android .navigation .v5 .navigation .NavigationConstants .STEP_MANEUVER_TYPE_ROTARY ;
3535import static com .mapbox .services .android .navigation .v5 .navigation .NavigationConstants .STEP_MANEUVER_TYPE_ROUNDABOUT ;
36- import static com .mapbox .services .android .navigation .v5 .navigation .NavigationConstants
37- .STEP_MANEUVER_TYPE_ROUNDABOUT_TURN ;
36+ import static com .mapbox .services .android .navigation .v5 .navigation .NavigationConstants .STEP_MANEUVER_TYPE_ROUNDABOUT_TURN ;
3837
3938
4039/**
@@ -81,32 +80,66 @@ public class ManeuverView extends View {
8180 private String maneuverType = null ;
8281 @ ManeuverModifier
8382 private String maneuverModifier = null ;
84- private Pair < String , String > maneuverTypeAndModifier = new Pair <>( null , null );
83+ @ ColorInt
8584 private int primaryColor ;
85+ @ ColorInt
8686 private int secondaryColor ;
8787 private float roundaboutAngle = DEFAULT_ROUNDABOUT_ANGLE ;
88+ private Pair <String , String > maneuverTypeAndModifier = new Pair <>(null , null );
8889 private PointF size ;
8990
91+ /**
92+ * A custom view that can be used with the Mapbox Directions API.
93+ * <p>
94+ * By providing a {@link String} maneuver type and maneuver modifier, the
95+ * corresponding maneuver icon will be rendered in this view.
96+ *
97+ * @param context to use when creating a view from code
98+ */
9099 public ManeuverView (Context context ) {
91100 super (context );
92101 }
93102
103+ /**
104+ * A custom view that can be used with the Mapbox Directions API.
105+ * <p>
106+ * By providing a {@link String} maneuver type and maneuver modifier, the
107+ * corresponding maneuver icon will be rendered in this view.
108+ *
109+ * @param context for inflating a view from XML
110+ * @param attrs for inflating a view from XML
111+ */
94112 public ManeuverView (Context context , AttributeSet attrs ) {
95113 super (context , attrs );
114+ initializeColorFrom (attrs );
96115 }
97116
117+ /**
118+ * A custom view that can be used with the Mapbox Directions API.
119+ * <p>
120+ * By providing a {@link String} maneuver type and maneuver modifier, the
121+ * corresponding maneuver icon will be rendered in this view.
122+ *
123+ * @param context for inflation from XML and apply a class-specific base style
124+ * @param attrs for inflation from XML and apply a class-specific base style
125+ * @param defStyle for inflation from XML and apply a class-specific base style
126+ */
98127 public ManeuverView (Context context , AttributeSet attrs , int defStyle ) {
99128 super (context , attrs , defStyle );
129+ initializeColorFrom (attrs );
100130 }
101131
102- @ Override
103- protected void onFinishInflate () {
104- super .onFinishInflate ();
105- setLayerType (LAYER_TYPE_SOFTWARE , null );
106- initManeuverColor ();
107- }
108-
109- public void setManeuverTypeAndModifier (@ NonNull String maneuverType , String maneuverModifier ) {
132+ /**
133+ * Updates the maneuver type and modifier which determine how this view will
134+ * render itself.
135+ * <p>
136+ * If determined the provided maneuver type and modifier will render a new image,
137+ * the view will invalidate and redraw itself with the new data.
138+ *
139+ * @param maneuverType to determine the maneuver icon to render
140+ * @param maneuverModifier to determine the maneuver icon to render
141+ */
142+ public void setManeuverTypeAndModifier (@ NonNull String maneuverType , @ Nullable String maneuverModifier ) {
110143 if (isNewTypeOrModifier (maneuverType , maneuverModifier )) {
111144 this .maneuverType = maneuverType ;
112145 this .maneuverModifier = maneuverModifier ;
@@ -119,13 +152,53 @@ public void setManeuverTypeAndModifier(@NonNull String maneuverType, String mane
119152 }
120153 }
121154
122- public void setRoundaboutAngle (float roundaboutAngle ) {
155+ /**
156+ * Updates the angle to render the roundabout maneuver.
157+ * <p>
158+ * This value will only be considered if the current maneuver type is
159+ * a roundabout.
160+ *
161+ * @param roundaboutAngle angle to be rendered
162+ */
163+ public void setRoundaboutAngle (@ FloatRange (from = 60f , to = 300f ) float roundaboutAngle ) {
123164 if (ROUNDABOUT_MANEUVER_TYPES .contains (maneuverType ) && this .roundaboutAngle != roundaboutAngle ) {
124165 updateRoundaboutAngle (roundaboutAngle );
125166 invalidate ();
126167 }
127168 }
128169
170+ /**
171+ * Updates maneuver view primary color.
172+ * <p>
173+ * The {@link ManeuverView} will be invalidated and redrawn
174+ * with the new color provided.
175+ *
176+ * @param primaryColor to be set
177+ */
178+ public void setPrimaryColor (@ ColorInt int primaryColor ) {
179+ this .primaryColor = primaryColor ;
180+ invalidate ();
181+ }
182+
183+ /**
184+ * Updates maneuver view secondary color.
185+ * <p>
186+ * The {@link ManeuverView} will be invalidated and redrawn
187+ * with the new color provided.
188+ *
189+ * @param secondaryColor to be set
190+ */
191+ public void setSecondaryColor (@ ColorInt int secondaryColor ) {
192+ this .secondaryColor = secondaryColor ;
193+ invalidate ();
194+ }
195+
196+ @ Override
197+ protected void onFinishInflate () {
198+ super .onFinishInflate ();
199+ setLayerType (LAYER_TYPE_SOFTWARE , null );
200+ }
201+
129202 @ Override
130203 protected void onMeasure (int widthMeasureSpec , int heightMeasureSpec ) {
131204 super .onMeasure (widthMeasureSpec , heightMeasureSpec );
@@ -156,11 +229,13 @@ protected void onDraw(Canvas canvas) {
156229 setScaleX (flip ? -1 : 1 );
157230 }
158231
159- private void initManeuverColor () {
160- this .primaryColor = ThemeSwitcher .retrieveThemeColor (getContext (),
161- R .attr .navigationViewBannerManeuverPrimary );
162- this .secondaryColor = ThemeSwitcher .retrieveThemeColor (getContext (),
163- R .attr .navigationViewBannerManeuverSecondary );
232+ private void initializeColorFrom (AttributeSet attributeSet ) {
233+ TypedArray typedArray = getContext ().obtainStyledAttributes (attributeSet , R .styleable .ManeuverView );
234+ primaryColor = typedArray .getColor (R .styleable .ManeuverView_maneuverViewPrimaryColor ,
235+ ContextCompat .getColor (getContext (), R .color .mapbox_navigation_view_color_banner_maneuver_primary ));
236+ secondaryColor = typedArray .getColor (R .styleable .ManeuverView_maneuverViewSecondaryColor ,
237+ ContextCompat .getColor (getContext (), R .color .mapbox_navigation_view_color_banner_maneuver_secondary ));
238+ typedArray .recycle ();
164239 }
165240
166241 private boolean isNewTypeOrModifier (String maneuverType , String maneuverModifier ) {
0 commit comments