Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] re-apply location component's "layer-below" when changed
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasPaczos authored and Łukasz Paczos committed Jan 17, 2019
1 parent f0392e0 commit dd3027c
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ final class LocationLayerController implements MapboxAnimator.OnLayerAnimationsV

private boolean isHidden = true;

@Nullable
private String layerBelow;

LocationLayerController(MapboxMap mapboxMap, Style style, LayerSourceProvider layerSourceProvider,
LayerFeatureProvider featureProvider, LayerBitmapProvider bitmapProvider,
@NonNull LocationComponentOptions options) {
Expand All @@ -99,6 +102,14 @@ void initializeComponents(Style style, LocationComponentOptions options) {
}

void applyStyle(@NonNull LocationComponentOptions options) {
String newLayerBelowOption = options.layerBelow();
if ((layerBelow != null || newLayerBelowOption != null)) {
if (layerBelow == null || !layerBelow.equals(newLayerBelowOption)) {
removeLayers();
addLayers(newLayerBelowOption);
}
}

this.options = options;

float elevation = options.elevation();
Expand Down Expand Up @@ -211,6 +222,7 @@ private void setLayerVisibility(@NonNull String layerId, boolean visible) {
}

private void addLayers(@NonNull String idBelowLayer) {
layerBelow = idBelowLayer;
addSymbolLayer(BEARING_LAYER, idBelowLayer);
addSymbolLayer(FOREGROUND_LAYER, BEARING_LAYER);
addSymbolLayer(BACKGROUND_LAYER, FOREGROUND_LAYER);
Expand All @@ -233,6 +245,13 @@ private void addLayerToMap(Layer layer, @NonNull String idBelowLayer) {
layerMap.add(layer.getId());
}

private void removeLayers() {
for (String layerId : layerMap) {
style.removeLayer(layerId);
}
layerMap.clear();
}

private void setBearingProperty(@NonNull String propertyId, float bearing) {
locationFeature.addNumberProperty(propertyId, bearing);
refreshSource();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import static com.mapbox.mapboxsdk.location.LocationComponentConstants.ACCURACY_LAYER;
import static com.mapbox.mapboxsdk.location.LocationComponentConstants.BACKGROUND_ICON;
import static com.mapbox.mapboxsdk.location.LocationComponentConstants.BACKGROUND_LAYER;
import static com.mapbox.mapboxsdk.location.LocationComponentConstants.BACKGROUND_STALE_ICON;
Expand Down Expand Up @@ -272,6 +274,109 @@ public void applyStyle_styleBearingFromOptions() {
verify(style).addImage(BEARING_ICON, bitmap);
}

@Test
public void applyStyle_layerBelowChanged() {
LayerSourceProvider sourceProvider = buildLayerProvider();
when(sourceProvider.generateSource(any(Feature.class))).thenReturn(mock(GeoJsonSource.class));

Layer bearingLayer = mock(Layer.class);
when(bearingLayer.getId()).thenReturn(BEARING_LAYER);
when(sourceProvider.generateLayer(BEARING_LAYER)).thenReturn(bearingLayer);
Layer foregroundLayer = mock(Layer.class);
when(foregroundLayer.getId()).thenReturn(FOREGROUND_LAYER);
when(sourceProvider.generateLayer(FOREGROUND_LAYER)).thenReturn(foregroundLayer);
Layer backgroundLayer = mock(Layer.class);
when(backgroundLayer.getId()).thenReturn(BACKGROUND_LAYER);
when(sourceProvider.generateLayer(BACKGROUND_LAYER)).thenReturn(backgroundLayer);
Layer shadowLayer = mock(Layer.class);
when(shadowLayer.getId()).thenReturn(SHADOW_LAYER);
when(sourceProvider.generateLayer(SHADOW_LAYER)).thenReturn(shadowLayer);
Layer accuracyLayer = mock(Layer.class);
when(accuracyLayer.getId()).thenReturn(ACCURACY_LAYER);
when(sourceProvider.generateAccuracyLayer()).thenReturn(accuracyLayer);

LocationComponentOptions options = mock(LocationComponentOptions.class);
LayerBitmapProvider bitmapProvider = mock(LayerBitmapProvider.class);
Bitmap bitmap = mock(Bitmap.class);
when(bitmapProvider.generateShadowBitmap(any(LocationComponentOptions.class))).thenReturn(bitmap);

LocationLayerController layerController =
new LocationLayerController(mapboxMap, mapboxMap.getStyle(), sourceProvider, buildFeatureProvider(options),
bitmapProvider, options);

Layer bearingLayer2 = mock(Layer.class);
when(sourceProvider.generateLayer(BEARING_LAYER)).thenReturn(bearingLayer2);
Layer foregroundLayer2 = mock(Layer.class);
when(sourceProvider.generateLayer(FOREGROUND_LAYER)).thenReturn(foregroundLayer2);
Layer backgroundLayer2 = mock(Layer.class);
when(sourceProvider.generateLayer(BACKGROUND_LAYER)).thenReturn(backgroundLayer2);
Layer shadowLayer2 = mock(Layer.class);
when(sourceProvider.generateLayer(SHADOW_LAYER)).thenReturn(shadowLayer2);
Layer accuracyLayer2 = mock(Layer.class);
when(sourceProvider.generateAccuracyLayer()).thenReturn(accuracyLayer2);
String layerBelow = "layer-below";
when(options.layerBelow()).thenReturn(layerBelow);

layerController.applyStyle(options);

verify(style).removeLayer(BEARING_LAYER);
verify(style).removeLayer(FOREGROUND_LAYER);
verify(style).removeLayer(BACKGROUND_LAYER);
verify(style).removeLayer(SHADOW_LAYER);
verify(style).removeLayer(ACCURACY_LAYER);

verify(style).addLayerBelow(bearingLayer2, layerBelow);
verify(style).addLayerBelow(foregroundLayer2, BEARING_LAYER);
verify(style).addLayerBelow(backgroundLayer2, FOREGROUND_LAYER);
verify(style).addLayerBelow(shadowLayer2, BACKGROUND_LAYER);
verify(style).addLayerBelow(accuracyLayer2, BACKGROUND_LAYER);
}

@Test
public void applyStyle_layerBelowNotChanged() {
LayerSourceProvider sourceProvider = buildLayerProvider();
when(sourceProvider.generateSource(any(Feature.class))).thenReturn(mock(GeoJsonSource.class));
LocationComponentOptions options = mock(LocationComponentOptions.class);
LayerBitmapProvider bitmapProvider = mock(LayerBitmapProvider.class);
Bitmap bitmap = mock(Bitmap.class);
when(bitmapProvider.generateShadowBitmap(any(LocationComponentOptions.class))).thenReturn(bitmap);

String layerBelow = "layer-below";
when(options.layerBelow()).thenReturn(layerBelow);

LocationLayerController layerController =
new LocationLayerController(mapboxMap, mapboxMap.getStyle(), sourceProvider, buildFeatureProvider(options),
bitmapProvider, options);

options = mock(LocationComponentOptions.class);
layerBelow = "layer-below";
when(options.layerBelow()).thenReturn(layerBelow);
layerController.applyStyle(options);

verify(style, times(0)).removeLayer(any(String.class));
verify(style, times(5)).addLayerBelow(any(Layer.class), any(String.class));
}

@Test
public void applyStyle_layerBelowNotChangedNull() {
LayerSourceProvider sourceProvider = buildLayerProvider();
when(sourceProvider.generateSource(any(Feature.class))).thenReturn(mock(GeoJsonSource.class));
LocationComponentOptions options = mock(LocationComponentOptions.class);
LayerBitmapProvider bitmapProvider = mock(LayerBitmapProvider.class);
Bitmap bitmap = mock(Bitmap.class);
when(bitmapProvider.generateShadowBitmap(any(LocationComponentOptions.class))).thenReturn(bitmap);

LocationLayerController layerController =
new LocationLayerController(mapboxMap, mapboxMap.getStyle(), sourceProvider, buildFeatureProvider(options),
bitmapProvider, options);

options = mock(LocationComponentOptions.class);
layerController.applyStyle(options);

verify(style, times(0)).removeLayer(any(String.class));
verify(style, times(5)).addLayerBelow(any(Layer.class), Mockito.<String>any());
}

@Test
public void updateForegroundOffset_foregroundIconPropertyIsUpdated() {
LayerSourceProvider sourceProvider = buildLayerProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Toast;

import com.mapbox.android.core.location.LocationEngineRequest;
import com.mapbox.android.core.permissions.PermissionsListener;
import com.mapbox.android.core.permissions.PermissionsManager;
Expand Down Expand Up @@ -43,7 +44,7 @@ public class LocationModesActivity extends AppCompatActivity implements OnMapRea

private LocationComponent locationComponent;
private MapboxMap mapboxMap;
private boolean customStyle;
private boolean defaultStyle = false;

private static final String SAVED_STATE_CAMERA = "saved_state_camera";
private static final String SAVED_STATE_RENDER = "saved_state_render";
Expand Down Expand Up @@ -124,26 +125,14 @@ public void onMapReady(@NonNull MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;

mapboxMap.setStyle(Style.MAPBOX_STREETS, style -> {
int[] padding;
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
padding = new int[] {0, 750, 0, 0};
} else {
padding = new int[] {0, 250, 0, 0};
}

LocationComponentOptions options = LocationComponentOptions.builder(this)
.padding(padding)
.layerBelow("waterway-label")
.build();

locationComponent = mapboxMap.getLocationComponent();
locationComponent.activateLocationComponent(this, style, true,
new LocationEngineRequest.Builder(750)
.setFastestInterval(750)
.setPriority(LocationEngineRequest.PRIORITY_HIGH_ACCURACY)
.build()
);
locationComponent.applyStyle(options);
toggleStyle();
locationComponent.setLocationComponentEnabled(true);
locationComponent.addOnLocationClickListener(this);
locationComponent.addOnCameraTrackingChangedListener(this);
Expand Down Expand Up @@ -195,10 +184,25 @@ private void toggleStyle() {
return;
}

customStyle = !customStyle;
locationComponent.applyStyle(
this,
customStyle ? R.style.CustomLocationComponent : R.style.mapbox_LocationComponent);
defaultStyle = !defaultStyle;
LocationComponentOptions options = LocationComponentOptions.createFromAttributes(
this, defaultStyle ? R.style.mapbox_LocationComponent : R.style.CustomLocationComponent);

if (defaultStyle) {
int[] padding;
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
padding = new int[] {0, 750, 0, 0};
} else {
padding = new int[] {0, 250, 0, 0};
}

options = options.toBuilder()
.padding(padding)
.layerBelow("road-label")
.build();
}

locationComponent.applyStyle(options);
}

private void toggleMapStyle() {
Expand Down

0 comments on commit dd3027c

Please sign in to comment.