Skip to content

Commit b0b29e9

Browse files
ecgrebsarahsnow1
authored andcommitted
Set map label language (#329)
* Adds version of MapView#getMapAsync(...) with Locale param * Sets stylesheet global variable for language using given Locale or default if none is provided
1 parent bfea938 commit b0b29e9

File tree

5 files changed

+136
-3
lines changed

5 files changed

+136
-3
lines changed

core/src/main/java/com/mapzen/android/graphics/MapFragment.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import android.view.View;
1313
import android.view.ViewGroup;
1414

15+
import java.util.Locale;
16+
1517
import static com.mapzen.android.graphics.MapView.OVERLAY_MODE_SDK;
1618

1719
/**
@@ -55,12 +57,23 @@ public void getMapAsync(final OnMapReadyCallback callback) {
5557
/**
5658
* Asynchronously creates the map and configures the vector tiles API key using the string
5759
* resource declared in the client application. Configures the stylesheet using the stylesheet
58-
* parameter
60+
* parameter.
5961
*/
6062
public void getMapAsync(MapStyle mapStyle, final OnMapReadyCallback callback) {
6163
mapView.getMapAsync(mapStyle, callback);
6264
}
6365

66+
/**
67+
* Asynchronously creates the map and configures the vector tiles API key using the string
68+
* resource declared in the client application. Configures the stylesheet using the stylesheet
69+
* parameter.
70+
*
71+
* Also sets {@link Locale} used to determine default language when rendering map labels.
72+
*/
73+
public void getMapAsync(MapStyle mapStyle, Locale locale, final OnMapReadyCallback callback) {
74+
mapView.getMapAsync(mapStyle, locale, callback);
75+
}
76+
6477
@Override public void onDestroy() {
6578
super.onDestroy();
6679
if (mapView != null) {

core/src/main/java/com/mapzen/android/graphics/MapInitializer.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
import android.content.Context;
1010

1111
import java.util.ArrayList;
12+
import java.util.Locale;
1213

1314
import javax.inject.Inject;
1415

1516
/**
1617
* Class responsible for initializing the map.
1718
*/
1819
public class MapInitializer {
20+
static final String STYLE_GLOBAL_VAR_API_KEY = "global.sdk_mapzen_api_key";
21+
static final String STYLE_GLOBAL_VAR_LANGUAGE = "global.ux_language";
1922

2023
private Context context;
2124

@@ -25,6 +28,8 @@ public class MapInitializer {
2528

2629
private MapStateManager mapStateManager;
2730

31+
private Locale locale = Locale.getDefault();
32+
2833
/**
2934
* Creates a new instance.
3035
*/
@@ -51,6 +56,18 @@ public void init(final MapView mapView, MapStyle mapStyle, final OnMapReadyCallb
5156
loadMap(mapView, mapStyle, true, callback);
5257
}
5358

59+
/**
60+
* Initialize map for current {@link MapView} and {@link MapStyle} before notifying via
61+
* {@link OnMapReadyCallback}.
62+
*
63+
* Also sets {@link Locale} used to determine default language when rendering map labels.
64+
*/
65+
public void init(final MapView mapView, MapStyle mapStyle, Locale locale,
66+
final OnMapReadyCallback callback) {
67+
this.locale = locale;
68+
loadMap(mapView, mapStyle, true, callback);
69+
}
70+
5471
private TangramMapView getTangramView(final MapView mapView) {
5572
return mapView.getTangramMapView();
5673
}
@@ -68,7 +85,8 @@ private void loadMap(final MapView mapView, MapStyle mapStyle, boolean styleExpl
6885
private void loadMap(final MapView mapView, String sceneFile, final OnMapReadyCallback callback) {
6986
final ArrayList<SceneUpdate> sceneUpdates = new ArrayList<>(1);
7087
final String apiKey = MapzenManager.instance(context).getApiKey();
71-
sceneUpdates.add(new SceneUpdate("global.sdk_mapzen_api_key", apiKey));
88+
sceneUpdates.add(new SceneUpdate(STYLE_GLOBAL_VAR_API_KEY, apiKey));
89+
sceneUpdates.add(new SceneUpdate(STYLE_GLOBAL_VAR_LANGUAGE, locale.getLanguage()));
7290
getTangramView(mapView).getMapAsync(new com.mapzen.tangram.MapView.OnMapReadyCallback() {
7391
@Override public void onMapReady(MapController mapController) {
7492
mapController.setHttpHandler(tileHttpHandler);

core/src/main/java/com/mapzen/android/graphics/MapView.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import android.widget.RelativeLayout;
1717
import android.widget.TextView;
1818

19+
import java.util.Locale;
20+
1921
import javax.inject.Inject;
2022

2123
/**
@@ -121,13 +123,25 @@ public void getMapAsync(@NonNull OnMapReadyCallback callback) {
121123
* Load map asynchronously using APK key declared in XML resources. For example:
122124
* {@code <string name="mapzen_api_key">[YOUR_API_KEY]</string>}
123125
*
126+
* @param mapStyle mapStyle that should be set.
124127
* @param callback listener to be invoked when map is initialized and ready to use.
125-
* @param mapStyle mapStyle that should be set
126128
*/
127129
public void getMapAsync(MapStyle mapStyle, @NonNull OnMapReadyCallback callback) {
128130
mapInitializer.init(this, mapStyle, callback);
129131
}
130132

133+
/**
134+
* Load map asynchronously using APK key declared in XML resources. For example:
135+
* {@code <string name="mapzen_api_key">[YOUR_API_KEY]</string>}
136+
*
137+
* @param mapStyle mapStyle that should be set.
138+
* @param locale used to determine language that should be used for map labels.
139+
* @param callback listener to be invoked when map is initialized and ready to use.
140+
*/
141+
public void getMapAsync(MapStyle mapStyle, Locale locale, @NonNull OnMapReadyCallback callback) {
142+
mapInitializer.init(this, mapStyle, locale, callback);
143+
}
144+
131145
/**
132146
* Get the compass button.
133147
*/

core/src/test/java/com/mapzen/android/graphics/MapInitializerTest.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,33 @@
22

33
import com.mapzen.android.core.CoreDI;
44
import com.mapzen.android.core.MapzenManager;
5+
import com.mapzen.android.graphics.model.BubbleWrapStyle;
6+
import com.mapzen.tangram.SceneUpdate;
57

68
import org.junit.Before;
79
import org.junit.Test;
810
import org.junit.runner.RunWith;
11+
import org.mockito.ArgumentMatcher;
912
import org.powermock.core.classloader.annotations.PowerMockIgnore;
1013
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
1114
import org.powermock.modules.junit4.PowerMockRunner;
1215

1316
import android.content.Context;
1417

18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.Locale;
21+
1522
import static com.mapzen.TestHelper.getMockContext;
23+
import static com.mapzen.android.graphics.MapInitializer.STYLE_GLOBAL_VAR_API_KEY;
24+
import static com.mapzen.android.graphics.MapInitializer.STYLE_GLOBAL_VAR_LANGUAGE;
1625
import static org.assertj.core.api.Assertions.assertThat;
26+
import static org.mockito.Matchers.any;
27+
import static org.mockito.Matchers.anyString;
28+
import static org.mockito.Matchers.argThat;
1729
import static org.mockito.Mockito.mock;
30+
import static org.mockito.Mockito.verify;
31+
import static org.mockito.Mockito.when;
1832

1933
@RunWith(PowerMockRunner.class)
2034
@PowerMockIgnore("javax.net.ssl.*")
@@ -40,4 +54,65 @@ public class MapInitializerTest {
4054
mapInitializer.init(mapView, callback);
4155
assertThat(callback.map).isInstanceOf(MapzenMap.class);
4256
}
57+
58+
@Test public void init_shouldSetDefaultMapLocale() throws Exception {
59+
// Arrange
60+
MapView mapView = mock(MapView.class);
61+
TangramMapView tangramMapView = mock(TangramMapView.class);
62+
when(mapView.getTangramMapView()).thenReturn(tangramMapView);
63+
MapzenManager.instance(getMockContext()).setApiKey("fake-mapzen-api-key");
64+
65+
// Act
66+
mapInitializer.init(mapView, new BubbleWrapStyle(), null);
67+
68+
// Assert
69+
ArrayList<SceneUpdate> expected = new ArrayList<>();
70+
expected.add(new SceneUpdate(STYLE_GLOBAL_VAR_API_KEY, "fake-mapzen-api-key"));
71+
expected.add(new SceneUpdate(STYLE_GLOBAL_VAR_LANGUAGE, Locale.getDefault().getLanguage()));
72+
verify(tangramMapView).getMapAsync(any(com.mapzen.tangram.MapView.OnMapReadyCallback.class),
73+
anyString(), argThat(new SceneUpdateMatcher(expected)));
74+
}
75+
76+
@Test public void init_shouldSetGivenMapLocale() throws Exception {
77+
// Arrange
78+
MapView mapView = mock(MapView.class);
79+
TangramMapView tangramMapView = mock(TangramMapView.class);
80+
when(mapView.getTangramMapView()).thenReturn(tangramMapView);
81+
MapzenManager.instance(getMockContext()).setApiKey("fake-mapzen-api-key");
82+
83+
// Act
84+
mapInitializer.init(mapView, new BubbleWrapStyle(), Locale.FRENCH, null);
85+
86+
// Assert
87+
ArrayList<SceneUpdate> expected = new ArrayList<>();
88+
expected.add(new SceneUpdate(STYLE_GLOBAL_VAR_API_KEY, "fake-mapzen-api-key"));
89+
expected.add(new SceneUpdate(STYLE_GLOBAL_VAR_LANGUAGE, Locale.FRENCH.getLanguage()));
90+
verify(tangramMapView).getMapAsync(any(com.mapzen.tangram.MapView.OnMapReadyCallback.class),
91+
anyString(), argThat(new SceneUpdateMatcher(expected)));
92+
}
93+
94+
/**
95+
* Custom Mockito matcher for a list of SceneUpdates. Verifies paths and values are all equal.
96+
*/
97+
private class SceneUpdateMatcher extends ArgumentMatcher<List<SceneUpdate>> {
98+
List<SceneUpdate> thisObject;
99+
100+
public SceneUpdateMatcher(List<SceneUpdate> thisObject) {
101+
this.thisObject = thisObject;
102+
}
103+
104+
@Override public boolean matches(Object argument) {
105+
List<SceneUpdate> otherObject = (List<SceneUpdate>) argument;
106+
for (int i = 0; i < thisObject.size(); i++) {
107+
if (!thisObject.get(i).getPath().equals(otherObject.get(i).getPath())) {
108+
return false;
109+
}
110+
if (!thisObject.get(i).getValue().equals(otherObject.get(i).getValue())) {
111+
return false;
112+
}
113+
}
114+
115+
return true;
116+
}
117+
}
43118
}

core/src/test/java/com/mapzen/android/graphics/MapViewTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.mapzen.R;
44
import com.mapzen.android.core.MapzenManager;
5+
import com.mapzen.android.graphics.model.BubbleWrapStyle;
6+
import com.mapzen.android.graphics.model.MapStyle;
57

68
import org.junit.After;
79
import org.junit.Before;
@@ -12,6 +14,8 @@
1214
import android.content.res.TypedArray;
1315
import android.util.AttributeSet;
1416

17+
import java.util.Locale;
18+
1519
import static com.mapzen.TestHelper.getMockContext;
1620
import static com.mapzen.android.graphics.MapView.OVERLAY_MODE_CLASSIC;
1721
import static com.mapzen.android.graphics.MapView.OVERLAY_MODE_SDK;
@@ -54,6 +58,15 @@ public class MapViewTest {
5458
verify(mapInitializer, times(1)).init(mapView, callback);
5559
}
5660

61+
@Test public void getMapAsync_shouldUseGivenLocale() throws Exception {
62+
final MapInitializer mapInitializer = mock(MapInitializer.class);
63+
final OnMapReadyCallback callback = new TestCallback();
64+
final MapStyle mapStyle = new BubbleWrapStyle();
65+
mapView.mapInitializer = mapInitializer;
66+
mapView.getMapAsync(mapStyle, Locale.FRENCH, callback);
67+
verify(mapInitializer, times(1)).init(mapView, mapStyle, Locale.FRENCH, callback);
68+
}
69+
5770
@Test public void shouldInflateLayoutWithOverlayModeSdk() throws Exception {
5871
Context context = mock(Context.class);
5972
TypedArray typedArray = mock(TypedArray.class);

0 commit comments

Comments
 (0)