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

Keep location component's layers hidden when new style with the "layer-below" option is applied #13936

Merged
merged 2 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ void applyStyle(@NonNull LocationComponentOptions options) {
if (layerBelow == null || !layerBelow.equals(newLayerBelowOption)) {
removeLayers();
addLayers(newLayerBelowOption);
if (isHidden) {
for (String layerId : layerMap) {
setLayerVisibility(layerId, false);
}
}
setRenderMode(renderMode);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.mapbox.mapboxsdk.location;

import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;

import java.lang.ref.WeakReference;

/**
* Class controls the location stale state when the {@link android.location.Location} hasn't
* been updated in 'x' amount of time. {@link LocationComponentOptions#staleStateTimeout()} can be used to
Expand All @@ -14,25 +17,19 @@ class StaleStateManager {
private boolean isEnabled;
private final OnLocationStaleListener innerOnLocationStaleListeners;
@NonNull
private final Handler handler;
private final StaleMessageHandler handler;
private boolean isStale = true;
private long delayTime;

private final int staleStateMessage = 1;

StaleStateManager(OnLocationStaleListener innerListener, LocationComponentOptions options) {
innerOnLocationStaleListeners = innerListener;
handler = new Handler();
handler = new StaleMessageHandler(this);
isEnabled = options.enableStaleState();
delayTime = options.staleStateTimeout();
}

@NonNull
private Runnable staleStateRunnable = new Runnable() {
@Override
public void run() {
setState(true);
}
};

void setEnabled(boolean enabled) {
if (enabled) {
setState(isStale);
Expand All @@ -54,7 +51,9 @@ void updateLatestLocationTime() {

void setDelayTime(long delayTime) {
this.delayTime = delayTime;
postTheCallback();
if (handler.hasMessages(staleStateMessage)) {
postTheCallback();
}
}

void onStart() {
Expand All @@ -69,7 +68,7 @@ void onStop() {

private void postTheCallback() {
handler.removeCallbacksAndMessages(null);
handler.postDelayed(staleStateRunnable, delayTime);
handler.sendEmptyMessageDelayed(staleStateMessage, delayTime);
}

private void setState(boolean stale) {
Expand All @@ -80,4 +79,21 @@ private void setState(boolean stale) {
}
}
}

private static class StaleMessageHandler extends Handler {

private final WeakReference<StaleStateManager> managerWeakReference;

private StaleMessageHandler(StaleStateManager staleStateManager) {
this.managerWeakReference = new WeakReference<>(staleStateManager);
}

@Override
public void handleMessage(Message msg) {
StaleStateManager manager = managerWeakReference.get();
if (manager != null) {
manager.setState(true);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ import com.mapbox.mapboxsdk.camera.CameraUpdateFactory
import com.mapbox.mapboxsdk.geometry.LatLng
import com.mapbox.mapboxsdk.location.LocationComponentConstants.*
import com.mapbox.mapboxsdk.location.modes.RenderMode
import com.mapbox.mapboxsdk.location.utils.LocationComponentAction
import com.mapbox.mapboxsdk.location.utils.*
import com.mapbox.mapboxsdk.location.utils.MapboxTestingUtils.Companion.MAPBOX_HEAVY_STYLE
import com.mapbox.mapboxsdk.location.utils.MapboxTestingUtils.Companion.pushSourceUpdates
import com.mapbox.mapboxsdk.location.utils.StyleChangeIdlingResource
import com.mapbox.mapboxsdk.location.utils.isLayerVisible
import com.mapbox.mapboxsdk.location.utils.querySourceFeatures
import com.mapbox.mapboxsdk.maps.MapboxMap
import com.mapbox.mapboxsdk.maps.Style
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource
Expand Down Expand Up @@ -247,6 +244,33 @@ class LocationLayerControllerTest : EspressoTest() {
executeComponentTest(componentAction)
}

@Test
fun whenStyleChanged_isDisabled_hasLayerBelow_staysHidden() {
val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction {
override fun onLocationComponentAction(component: LocationComponent, mapboxMap: MapboxMap,
style: Style, uiController: UiController, context: Context) {
component.activateLocationComponent(context, style, false)
component.isLocationComponentEnabled = true
component.forceLocationUpdate(location)
TestingAsyncUtils.waitForLayer(uiController, idlingResource.mapView)
component.isLocationComponentEnabled = false
TestingAsyncUtils.waitForLayer(uiController, idlingResource.mapView)
assertThat(mapboxMap.queryRenderedFeatures(location, FOREGROUND_LAYER).isEmpty(), `is`(true))

val options =
LocationComponentOptions.createFromAttributes(context, com.mapbox.mapboxsdk.testapp.R.style.CustomLocationComponent)
.toBuilder()
.layerBelow("road-label")
.build()

component.applyStyle(options)
TestingAsyncUtils.waitForLayer(uiController, idlingResource.mapView)
assertThat(mapboxMap.queryRenderedFeatures(location, FOREGROUND_LAYER).isEmpty(), `is`(true))
}
}
executeComponentTest(componentAction)
}

@Test
fun whenStyleChanged_staleStateChanges() {
val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction {
Expand Down