diff --git a/plugins/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/traffic/TrafficPluginTest.java b/plugins/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/traffic/TrafficPluginTest.java index d1f4617f5..11fd20898 100644 --- a/plugins/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/traffic/TrafficPluginTest.java +++ b/plugins/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/traffic/TrafficPluginTest.java @@ -44,7 +44,7 @@ public class TrafficPluginTest { @Rule public ActivityTestRule rule = new ActivityTestRule<>(TrafficActivity.class); - public static final String ROUND = "round"; + private static final String ROUND = "round"; private OnMapReadyIdlingResource idlingResource; private MapboxMap mapboxMap; @@ -78,7 +78,7 @@ public void enabled() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController uiController) { - assertTrue(trafficPlugin.isEnabled()); + assertTrue(trafficPlugin.isVisible()); } }); } @@ -88,8 +88,8 @@ public void disabled() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController uiController) { - trafficPlugin.toggle(); - assertFalse(trafficPlugin.isEnabled()); + trafficPlugin.setVisibility(false); + assertFalse(trafficPlugin.isVisible()); } }); } @@ -409,7 +409,7 @@ public void toggleHidesLocalBaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { - trafficPlugin.toggle(); + trafficPlugin.setVisibility(false); assertTrue(mapboxMap.getLayer(Local.BASE_LAYER_ID).getVisibility().getValue().equals(Property.NONE)); } }); @@ -420,7 +420,7 @@ public void toggleHidesLocalCaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { - trafficPlugin.toggle(); + trafficPlugin.setVisibility(false); assertTrue(mapboxMap.getLayer(Local.CASE_LAYER_ID).getVisibility().getValue().equals(Property.NONE)); } }); @@ -431,7 +431,7 @@ public void toggleHidesSecondaryBaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { - trafficPlugin.toggle(); + trafficPlugin.setVisibility(false); assertTrue(mapboxMap.getLayer(Secondary.BASE_LAYER_ID).getVisibility().getValue().equals(Property.NONE)); } }); @@ -442,7 +442,7 @@ public void toggleHidesSecondaryCaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { - trafficPlugin.toggle(); + trafficPlugin.setVisibility(false); assertTrue(mapboxMap.getLayer(Secondary.CASE_LAYER_ID).getVisibility().getValue().equals(Property.NONE)); } }); @@ -453,7 +453,7 @@ public void toggleHidesPrimaryBaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { - trafficPlugin.toggle(); + trafficPlugin.setVisibility(false); assertTrue(mapboxMap.getLayer(Primary.BASE_LAYER_ID).getVisibility().getValue().equals(Property.NONE)); } }); @@ -464,7 +464,7 @@ public void toggleHidesPrimaryCaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { - trafficPlugin.toggle(); + trafficPlugin.setVisibility(false); assertTrue(mapboxMap.getLayer(Primary.CASE_LAYER_ID).getVisibility().getValue().equals(Property.NONE)); } }); @@ -476,7 +476,7 @@ public void toggleHidesTrunkBaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { - trafficPlugin.toggle(); + trafficPlugin.setVisibility(false); assertTrue(mapboxMap.getLayer(Trunk.BASE_LAYER_ID).getVisibility().getValue().equals(Property.NONE)); } }); @@ -487,7 +487,7 @@ public void toggleHidesTrunkCaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { - trafficPlugin.toggle(); + trafficPlugin.setVisibility(false); assertTrue(mapboxMap.getLayer(Trunk.CASE_LAYER_ID).getVisibility().getValue().equals(Property.NONE)); } }); @@ -498,7 +498,7 @@ public void toggleHidesMotorWayBaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { - trafficPlugin.toggle(); + trafficPlugin.setVisibility(false); assertTrue(mapboxMap.getLayer(MotorWay.BASE_LAYER_ID).getVisibility().getValue().equals(Property.NONE)); } }); @@ -509,7 +509,7 @@ public void toggleHidesMotorWayCaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { - trafficPlugin.toggle(); + trafficPlugin.setVisibility(false); assertTrue(mapboxMap.getLayer(MotorWay.CASE_LAYER_ID).getVisibility().getValue().equals(Property.NONE)); } }); diff --git a/plugins/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/TrafficActivity.java b/plugins/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/TrafficActivity.java index ba90b2d55..01b757312 100644 --- a/plugins/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/TrafficActivity.java +++ b/plugins/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/TrafficActivity.java @@ -48,14 +48,14 @@ protected void onCreate(Bundle savedInstanceState) { public void onMapReady(final MapboxMap mapboxMap) { this.mapboxMap = mapboxMap; this.trafficPlugin = new TrafficPlugin(mapView, mapboxMap); - this.trafficPlugin.toggle(); // Enable the traffic view by default + this.trafficPlugin.setVisibility(true); // Enable the traffic view by default } @OnClick(R.id.fabTraffic) public void onTrafficFabClick() { if (mapboxMap != null) { - trafficPlugin.toggle(); - Timber.e("Traffic plugin is enabled :%s", trafficPlugin.isEnabled()); + trafficPlugin.setVisibility(!trafficPlugin.isVisible()); + Timber.e("Traffic plugin is enabled :%s", trafficPlugin.isVisible()); } } diff --git a/plugins/traffic/src/main/java/com/mapbox/mapboxsdk/plugins/traffic/TrafficPlugin.java b/plugins/traffic/src/main/java/com/mapbox/mapboxsdk/plugins/traffic/TrafficPlugin.java index fe26859b1..d389a4612 100644 --- a/plugins/traffic/src/main/java/com/mapbox/mapboxsdk/plugins/traffic/TrafficPlugin.java +++ b/plugins/traffic/src/main/java/com/mapbox/mapboxsdk/plugins/traffic/TrafficPlugin.java @@ -3,6 +3,7 @@ import android.graphics.Color; import android.support.annotation.ColorInt; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; @@ -12,6 +13,8 @@ import com.mapbox.mapboxsdk.style.layers.Filter; import com.mapbox.mapboxsdk.style.layers.Layer; import com.mapbox.mapboxsdk.style.layers.LineLayer; +import com.mapbox.mapboxsdk.style.layers.Property; +import com.mapbox.mapboxsdk.style.layers.SymbolLayer; import com.mapbox.mapboxsdk.style.sources.Source; import com.mapbox.mapboxsdk.style.sources.VectorSource; @@ -35,21 +38,22 @@ import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.visibility; /** - * The traffic plugin allows to add Mapbox Traffic v1 to the Mapbox Android SDK v5.0.2. + * The Traffic Plugin allows you to add Mapbox Traffic v1 to the Mapbox Android SDK. *

* Initialise this plugin in the {@link com.mapbox.mapboxsdk.maps.OnMapReadyCallback#onMapReady(MapboxMap)} and provide * a valid instance of {@link MapView} and {@link MapboxMap}. *

*

- * Use {@link #toggle()} to switch state of this plugin to enable or disabled. - * Use {@link #isEnabled()} to validate if the plugin is active or not. + * Use {@link #setVisibility(boolean)} to switch state of this plugin to enable or disabled. + * Use {@link #isVisible()} to validate if the plugin is active or not. *

*/ public final class TrafficPlugin implements MapView.OnMapChangedListener { private MapboxMap mapboxMap; private List layerIds; - private boolean enabled; + private String belowLayer; + private boolean visible; /** * Create a traffic plugin. @@ -58,8 +62,21 @@ public final class TrafficPlugin implements MapView.OnMapChangedListener { * @param mapboxMap the MapboxMap to apply traffic plugin with */ public TrafficPlugin(@NonNull MapView mapView, @NonNull MapboxMap mapboxMap) { + this(mapView, mapboxMap, null); + } + + /** + * Create a traffic plugin. + * + * @param mapView the MapView to apply the traffic plugin to + * @param mapboxMap the MapboxMap to apply traffic plugin with + * @param belowLayer the layer id where you'd like the traffic to display below + */ + public TrafficPlugin(@NonNull MapView mapView, @NonNull MapboxMap mapboxMap, @Nullable String belowLayer) { this.mapboxMap = mapboxMap; + this.belowLayer = belowLayer; mapView.addOnMapChangedListener(this); + updateState(); } /** @@ -67,20 +84,23 @@ public TrafficPlugin(@NonNull MapView mapView, @NonNull MapboxMap mapboxMap) { * * @return true if enabled, false otherwise */ - public boolean isEnabled() { - return enabled; + public boolean isVisible() { + return visible; } /** - * Toggles the traffic plugin state. - *

- * If the traffic plugin wasn't initialised yet, traffic source and layers will be added to the current map style. - * Else visibility will be toggled based on the current state. - *

+ * Toggles the visibility of the traffic layers. + * + * @param visible true for visible, false for none */ - public void toggle() { - enabled = !enabled; - updateState(); + public void setVisibility(boolean visible) { + this.visible = visible; + List layers = mapboxMap.getLayers(); + for (Layer layer : layers) { + if (layerIds.contains(layer.getId())) { + layer.setProperties(visibility(visible ? Property.VISIBLE : Property.NONE)); + } + } } /** @@ -93,7 +113,7 @@ public void toggle() { */ @Override public void onMapChanged(int change) { - if (change == MapView.DID_FINISH_LOADING_STYLE && isEnabled()) { + if (change == MapView.DID_FINISH_LOADING_STYLE && isVisible()) { updateState(); } } @@ -107,7 +127,7 @@ private void updateState() { initialise(); return; } - setVisibility(enabled); + setVisibility(visible); } /** @@ -168,8 +188,25 @@ private void addLocalLayer() { Local.FUNCTION_LINE_OPACITY_CASE ); - // #TODO https://github.com/mapbox/mapbox-plugins-android/issues/14 - addTrafficLayersToMap(localCase, local, "bridge-motorway"); + addTrafficLayersToMap(localCase, local, placeLayerBelow()); + } + + /** + * Attempts to find the layer which the traffic should be placed below. Depending on the style, this might not always + * be accurate. + */ + private String placeLayerBelow() { + if (belowLayer == null || belowLayer.isEmpty()) { + List styleLayers = mapboxMap.getLayers(); + Layer layer; + for (int i = styleLayers.size() - 1; i >= 0; i--) { + layer = styleLayers.get(i); + if (!(layer instanceof SymbolLayer)) { + return layer.getId(); + } + } + } + return belowLayer; } /** @@ -290,26 +327,12 @@ private String getLastAddedLayerId() { * @param idAboveLayer the id of the layer above */ private void addTrafficLayersToMap(Layer layerCase, Layer layer, String idAboveLayer) { - mapboxMap.addLayerAbove(layerCase, idAboveLayer); + mapboxMap.addLayerBelow(layerCase, idAboveLayer); mapboxMap.addLayerAbove(layer, layerCase.getId()); layerIds.add(layerCase.getId()); layerIds.add(layer.getId()); } - /** - * Toggles the visibility of the traffic layers. - * - * @param visible true for visible, false for none - */ - private void setVisibility(boolean visible) { - List layers = mapboxMap.getLayers(); - for (Layer layer : layers) { - if (layerIds.contains(layer.getId())) { - layer.setProperties(visibility(visible ? "visible" : "none")); - } - } - } - private static class TrafficLayer { private static LineLayer getLineLayer(String lineLayerId, float minZoom, Filter.Statement statement, diff --git a/plugins/traffic/src/test/java/com/mapbox/mapboxsdk/plugins/traffic/TrafficPluginTest.java b/plugins/traffic/src/test/java/com/mapbox/mapboxsdk/plugins/traffic/TrafficPluginTest.java index 2cfc11d1c..e90e7f972 100644 --- a/plugins/traffic/src/test/java/com/mapbox/mapboxsdk/plugins/traffic/TrafficPluginTest.java +++ b/plugins/traffic/src/test/java/com/mapbox/mapboxsdk/plugins/traffic/TrafficPluginTest.java @@ -36,8 +36,8 @@ public void testSanity() { @Test public void testToggle() { TrafficPlugin trafficPlugin = new TrafficPlugin(mapView, mapboxMap); - assertFalse(trafficPlugin.isEnabled()); - trafficPlugin.toggle(); - assertTrue(trafficPlugin.isEnabled()); + assertFalse(trafficPlugin.isVisible()); + trafficPlugin.setVisibility(true); + assertTrue(trafficPlugin.isVisible()); } }