Skip to content

Commit 94ed389

Browse files
authored
Add ManeuverView APIs for adjusting primary and secondary color (#1709)
1 parent 4bfaccf commit 94ed389

File tree

8 files changed

+122
-31
lines changed

8 files changed

+122
-31
lines changed

libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/instruction/maneuver/ManeuverView.java

Lines changed: 99 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package com.mapbox.services.android.navigation.ui.v5.instruction.maneuver;
22

33
import android.content.Context;
4+
import android.content.res.TypedArray;
45
import android.graphics.Canvas;
56
import android.graphics.PointF;
7+
import android.support.annotation.ColorInt;
8+
import android.support.annotation.FloatRange;
69
import android.support.annotation.NonNull;
710
import android.support.annotation.Nullable;
11+
import android.support.v4.content.ContextCompat;
812
import android.support.v4.util.Pair;
913
import android.text.TextUtils;
1014
import android.util.AttributeSet;
1115
import android.view.View;
1216

1317
import com.mapbox.services.android.navigation.ui.v5.R;
14-
import com.mapbox.services.android.navigation.ui.v5.ThemeSwitcher;
1518

1619
import java.util.HashSet;
1720
import java.util.Map;
@@ -20,21 +23,17 @@
2023
import static com.mapbox.services.android.navigation.v5.navigation.NavigationConstants.ManeuverModifier;
2124
import static com.mapbox.services.android.navigation.v5.navigation.NavigationConstants.ManeuverType;
2225
import 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;
2728
import static com.mapbox.services.android.navigation.v5.navigation.NavigationConstants.STEP_MANEUVER_MODIFIER_UTURN;
2829
import static com.mapbox.services.android.navigation.v5.navigation.NavigationConstants.STEP_MANEUVER_TYPE_ARRIVE;
2930
import 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;
3232
import static com.mapbox.services.android.navigation.v5.navigation.NavigationConstants.STEP_MANEUVER_TYPE_FORK;
3333
import static com.mapbox.services.android.navigation.v5.navigation.NavigationConstants.STEP_MANEUVER_TYPE_OFF_RAMP;
3434
import static com.mapbox.services.android.navigation.v5.navigation.NavigationConstants.STEP_MANEUVER_TYPE_ROTARY;
3535
import 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) {

libandroid-navigation-ui/src/main/res/layout-land/instruction_content_layout.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
android:id="@+id/maneuverView"
2828
android:layout_width="40dp"
2929
android:layout_height="40dp"
30-
android:layout_margin="8dp"/>
30+
android:layout_margin="8dp"
31+
app:maneuverViewPrimaryColor="?attr/navigationViewBannerManeuverPrimary"
32+
app:maneuverViewSecondaryColor="?attr/navigationViewBannerManeuverSecondary"/>
3133

3234
<TextView
3335
android:id="@+id/stepDistanceText"

libandroid-navigation-ui/src/main/res/layout-land/instruction_layout.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
android:layout_margin="8dp"
3333
app:layout_constraintEnd_toEndOf="parent"
3434
app:layout_constraintStart_toStartOf="parent"
35-
app:layout_constraintTop_toTopOf="parent"/>
35+
app:layout_constraintTop_toTopOf="parent"
36+
app:maneuverViewPrimaryColor="?attr/navigationViewBannerManeuverPrimary"
37+
app:maneuverViewSecondaryColor="?attr/navigationViewBannerManeuverSecondary"/>
3638

3739
<TextView
3840
android:id="@+id/stepDistanceText"

libandroid-navigation-ui/src/main/res/layout-land/instruction_layout_alt.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
android:id="@+id/maneuverView"
3030
android:layout_width="40dp"
3131
android:layout_height="40dp"
32-
android:layout_margin="8dp"/>
32+
android:layout_margin="8dp"
33+
app:maneuverViewPrimaryColor="?attr/navigationViewBannerManeuverPrimary"
34+
app:maneuverViewSecondaryColor="?attr/navigationViewBannerManeuverSecondary"/>
3335

3436
<TextView
3537
android:id="@+id/stepDistanceText"

libandroid-navigation-ui/src/main/res/layout-land/sub_instruction_layout.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
android:id="@+id/subManeuverView"
1313
android:layout_width="24dp"
1414
android:layout_height="24dp"
15-
android:layout_marginBottom="8dp"
1615
android:layout_marginTop="8dp"
16+
android:layout_marginBottom="8dp"
1717
app:layout_constraintBottom_toBottomOf="parent"
1818
app:layout_constraintEnd_toStartOf="@+id/guideline"
1919
app:layout_constraintStart_toStartOf="parent"
20-
app:layout_constraintTop_toTopOf="parent"/>
20+
app:layout_constraintTop_toTopOf="parent"
21+
app:maneuverViewPrimaryColor="?attr/navigationViewBannerManeuverPrimary"
22+
app:maneuverViewSecondaryColor="?attr/navigationViewBannerManeuverSecondary"/>
2123

2224
<TextView
2325
android:id="@+id/subStepText"

libandroid-navigation-ui/src/main/res/layout/instruction_content_layout.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
app:layout_constraintEnd_toEndOf="parent"
3131
app:layout_constraintStart_toStartOf="parent"
3232
app:layout_constraintTop_toTopOf="parent"
33-
app:layout_constraintVertical_bias="0.75"/>
33+
app:layout_constraintVertical_bias="0.75"
34+
app:maneuverViewPrimaryColor="?attr/navigationViewBannerManeuverPrimary"
35+
app:maneuverViewSecondaryColor="?attr/navigationViewBannerManeuverSecondary"/>
3436

3537
<TextView
3638
android:id="@+id/stepDistanceText"

libandroid-navigation-ui/src/main/res/layout/sub_instruction_layout.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
app:layout_constraintBottom_toBottomOf="parent"
1717
app:layout_constraintEnd_toStartOf="@+id/guideline"
1818
app:layout_constraintStart_toStartOf="parent"
19-
app:layout_constraintTop_toTopOf="parent"/>
19+
app:layout_constraintTop_toTopOf="parent"
20+
app:maneuverViewPrimaryColor="?attr/navigationViewBannerManeuverPrimary"
21+
app:maneuverViewSecondaryColor="?attr/navigationViewBannerManeuverSecondary"/>
2022

2123
<TextView
2224
android:id="@+id/subStepText"

libandroid-navigation-ui/src/main/res/values/attrs.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@
6666
<!-- For setting the styles in XML -->
6767
<attr name="navigationLightTheme" format="reference"/>
6868
<attr name="navigationDarkTheme" format="reference"/>
69+
</declare-styleable>
6970

71+
<declare-styleable name="ManeuverView">
72+
<attr name="maneuverViewPrimaryColor" format="color"/>
73+
<attr name="maneuverViewSecondaryColor" format="color"/>
7074
</declare-styleable>
7175
</resources>

0 commit comments

Comments
 (0)