[android] - make style part of location component activation, add tests for non existing style and multiple style loading, revisit code comments

This commit is contained in:
Tobrun
2018-12-07 17:00:36 +02:00
committed by Tobrun
parent 648f34aadc
commit 8eaff9ce13
17 changed files with 351 additions and 204 deletions

View File

@@ -56,7 +56,7 @@ import static com.mapbox.mapboxsdk.location.LocationComponentConstants.DEFAULT_T
* <p>
* <strong>
* To get the component object use {@link MapboxMap#getLocationComponent()} and activate it with
* {@link #activateLocationComponent(Context)} or one of the overloads.
* {@link #activateLocationComponent(Context, Style)} or one of the overloads.
* Then, manage its visibility with {@link #setLocationComponentEnabled(boolean)}.
* </strong>
* <p>
@@ -77,7 +77,7 @@ import static com.mapbox.mapboxsdk.location.LocationComponentConstants.DEFAULT_T
* When using any engine, requesting/removing the location updates is going to be managed internally.
* <p>
* You can also push location updates to the component without any internal engine management.
* To achieve that, use {@link #activateLocationComponent(Context, boolean)} with false.
* To achieve that, use {@link #activateLocationComponent(Context, Style, boolean)} with false.
* No engine is going to be initialized and you can push location updates with {@link #forceLocationUpdate(Location)}.
* <p>
* For location puck animation purposes, like navigation,
@@ -90,6 +90,7 @@ public final class LocationComponent {
@NonNull
private final MapboxMap mapboxMap;
private Style style;
private LocationComponentOptions options;
@NonNull
private InternalLocationEngineProvider internalLocationEngineProvider = new InternalLocationEngineProvider();
@@ -201,8 +202,8 @@ public final class LocationComponent {
* @param context the context
*/
@RequiresPermission(anyOf = {ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION})
public void activateLocationComponent(@NonNull Context context) {
activateLocationComponent(context,
public void activateLocationComponent(@NonNull Context context, @NonNull Style style) {
activateLocationComponent(context, style,
LocationComponentOptions.createFromAttributes(context, R.style.mapbox_LocationComponent));
}
@@ -215,11 +216,12 @@ public final class LocationComponent {
* there should be no location engine initialized
*/
@RequiresPermission(anyOf = {ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION})
public void activateLocationComponent(@NonNull Context context, boolean useDefaultLocationEngine) {
public void activateLocationComponent(@NonNull Context context, @NonNull Style style,
boolean useDefaultLocationEngine) {
if (useDefaultLocationEngine) {
activateLocationComponent(context, R.style.mapbox_LocationComponent);
activateLocationComponent(context, style, R.style.mapbox_LocationComponent);
} else {
activateLocationComponent(context, null, R.style.mapbox_LocationComponent);
activateLocationComponent(context, style, null, R.style.mapbox_LocationComponent);
}
}
@@ -233,13 +235,14 @@ public final class LocationComponent {
* @param locationEngineRequest the location request
*/
@RequiresPermission(anyOf = {ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION})
public void activateLocationComponent(@NonNull Context context, boolean useDefaultLocationEngine,
public void activateLocationComponent(@NonNull Context context, @NonNull Style style,
boolean useDefaultLocationEngine,
@NonNull LocationEngineRequest locationEngineRequest) {
setLocationEngineRequest(locationEngineRequest);
if (useDefaultLocationEngine) {
activateLocationComponent(context, R.style.mapbox_LocationComponent);
activateLocationComponent(context, style, R.style.mapbox_LocationComponent);
} else {
activateLocationComponent(context, null, R.style.mapbox_LocationComponent);
activateLocationComponent(context, style, null, R.style.mapbox_LocationComponent);
}
}
@@ -253,8 +256,8 @@ public final class LocationComponent {
* @param styleRes the LocationComponent style res
*/
@RequiresPermission(anyOf = {ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION})
public void activateLocationComponent(@NonNull Context context, @StyleRes int styleRes) {
activateLocationComponent(context, LocationComponentOptions.createFromAttributes(context, styleRes));
public void activateLocationComponent(@NonNull Context context, @NonNull Style style, @StyleRes int styleRes) {
activateLocationComponent(context, style, LocationComponentOptions.createFromAttributes(context, styleRes));
}
/**
@@ -268,8 +271,9 @@ public final class LocationComponent {
* @param options the options
*/
@RequiresPermission(anyOf = {ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION})
public void activateLocationComponent(@NonNull Context context, @NonNull LocationComponentOptions options) {
initialize(context, options);
public void activateLocationComponent(@NonNull Context context, @NonNull Style style,
@NonNull LocationComponentOptions options) {
initialize(context, style, options);
initializeLocationEngine(context);
applyStyle(options);
}
@@ -283,9 +287,9 @@ public final class LocationComponent {
* @param styleRes the LocationComponent style res
*/
@RequiresPermission(anyOf = {ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION})
public void activateLocationComponent(@NonNull Context context, @Nullable LocationEngine locationEngine,
@StyleRes int styleRes) {
activateLocationComponent(context, locationEngine,
public void activateLocationComponent(@NonNull Context context, @NonNull Style style,
@Nullable LocationEngine locationEngine, @StyleRes int styleRes) {
activateLocationComponent(context, style, locationEngine,
LocationComponentOptions.createFromAttributes(context, styleRes));
}
@@ -299,9 +303,10 @@ public final class LocationComponent {
* @param styleRes the LocationComponent style res
*/
@RequiresPermission(anyOf = {ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION})
public void activateLocationComponent(@NonNull Context context, @Nullable LocationEngine locationEngine,
public void activateLocationComponent(@NonNull Context context, @NonNull Style style,
@Nullable LocationEngine locationEngine,
@NonNull LocationEngineRequest locationEngineRequest, @StyleRes int styleRes) {
activateLocationComponent(context, locationEngine, locationEngineRequest,
activateLocationComponent(context, style, locationEngine, locationEngineRequest,
LocationComponentOptions.createFromAttributes(context, styleRes));
}
@@ -312,8 +317,9 @@ public final class LocationComponent {
* @param locationEngine the engine
*/
@RequiresPermission(anyOf = {ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION})
public void activateLocationComponent(@NonNull Context context, @NonNull LocationEngine locationEngine) {
activateLocationComponent(context, locationEngine, R.style.mapbox_LocationComponent);
public void activateLocationComponent(@NonNull Context context, @NonNull Style style,
@NonNull LocationEngine locationEngine) {
activateLocationComponent(context, style, locationEngine, R.style.mapbox_LocationComponent);
}
/**
@@ -324,9 +330,10 @@ public final class LocationComponent {
* @param locationEngineRequest the location request
*/
@RequiresPermission(anyOf = {ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION})
public void activateLocationComponent(@NonNull Context context, @NonNull LocationEngine locationEngine,
public void activateLocationComponent(@NonNull Context context, @NonNull Style style,
@NonNull LocationEngine locationEngine,
@NonNull LocationEngineRequest locationEngineRequest) {
activateLocationComponent(context, locationEngine, locationEngineRequest, R.style.mapbox_LocationComponent);
activateLocationComponent(context, style, locationEngine, locationEngineRequest, R.style.mapbox_LocationComponent);
}
/**
@@ -337,9 +344,10 @@ public final class LocationComponent {
* @param options the options
*/
@RequiresPermission(anyOf = {ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION})
public void activateLocationComponent(@NonNull Context context, @Nullable LocationEngine locationEngine,
public void activateLocationComponent(@NonNull Context context, @NonNull Style style,
@Nullable LocationEngine locationEngine,
@NonNull LocationComponentOptions options) {
initialize(context, options);
initialize(context, style, options);
setLocationEngine(locationEngine);
applyStyle(options);
}
@@ -353,10 +361,11 @@ public final class LocationComponent {
* @param locationEngineRequest the location request
* @param options the options
*/
public void activateLocationComponent(@NonNull Context context, @Nullable LocationEngine locationEngine,
public void activateLocationComponent(@NonNull Context context, @NonNull Style style,
@Nullable LocationEngine locationEngine,
@NonNull LocationEngineRequest locationEngineRequest,
@NonNull LocationComponentOptions options) {
initialize(context, options);
initialize(context, style, options);
setLocationEngineRequest(locationEngineRequest);
setLocationEngine(locationEngine);
applyStyle(options);
@@ -899,57 +908,45 @@ public final class LocationComponent {
mapboxMap.removeOnCameraIdleListener(onCameraIdleListener);
}
private void initialize(@NonNull final Context context, @NonNull final LocationComponentOptions options) {
LocationComponent.this.options = options;
mapboxMap.getStyle(new InitializationCallback(context));
}
class InitializationCallback implements Style.OnStyleLoaded {
private final Context context;
private InitializationCallback(Context context) {
this.context = context;
private void initialize(@NonNull final Context context, @NonNull Style style,
@NonNull final LocationComponentOptions options) {
if (isComponentInitialized) {
return;
}
isComponentInitialized = true;
@Override
public void onStyleLoaded(@NonNull Style style) {
if (isComponentInitialized) {
return;
}
isComponentInitialized = true;
this.style = style;
this.options = options;
mapboxMap.addOnMapClickListener(onMapClickListener);
mapboxMap.addOnMapLongClickListener(onMapLongClickListener);
mapboxMap.addOnMapClickListener(onMapClickListener);
mapboxMap.addOnMapLongClickListener(onMapLongClickListener);
LayerSourceProvider sourceProvider = new LayerSourceProvider();
LayerFeatureProvider featureProvider = new LayerFeatureProvider();
LayerBitmapProvider bitmapProvider = new LayerBitmapProvider(context);
locationLayerController = new LocationLayerController(mapboxMap, sourceProvider, featureProvider,
bitmapProvider,
options);
locationCameraController = new LocationCameraController(
context, mapboxMap, cameraTrackingChangedListener, options, onCameraMoveInvalidateListener);
LayerSourceProvider sourceProvider = new LayerSourceProvider();
LayerFeatureProvider featureProvider = new LayerFeatureProvider();
LayerBitmapProvider bitmapProvider = new LayerBitmapProvider(context);
locationLayerController = new LocationLayerController(mapboxMap, style, sourceProvider, featureProvider,
bitmapProvider, options);
locationCameraController = new LocationCameraController(
context, mapboxMap, cameraTrackingChangedListener, options, onCameraMoveInvalidateListener);
locationAnimatorCoordinator = new LocationAnimatorCoordinator();
locationAnimatorCoordinator.addLayerListener(locationLayerController);
locationAnimatorCoordinator.addCameraListener(locationCameraController);
locationAnimatorCoordinator.setTrackingAnimationDurationMultiplier(options
.trackingAnimationDurationMultiplier());
locationAnimatorCoordinator = new LocationAnimatorCoordinator();
locationAnimatorCoordinator.addLayerListener(locationLayerController);
locationAnimatorCoordinator.addCameraListener(locationCameraController);
locationAnimatorCoordinator.setTrackingAnimationDurationMultiplier(options
.trackingAnimationDurationMultiplier());
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
compassEngine = new LocationComponentCompassEngine(windowManager, sensorManager);
compassEngine.addCompassListener(compassListener);
staleStateManager = new StaleStateManager(onLocationStaleListener, options);
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
compassEngine = new LocationComponentCompassEngine(windowManager, sensorManager);
compassEngine.addCompassListener(compassListener);
staleStateManager = new StaleStateManager(onLocationStaleListener, options);
updateMapWithOptions(options);
updateMapWithOptions(options);
setRenderMode(RenderMode.NORMAL);
setCameraMode(CameraMode.NONE);
setRenderMode(RenderMode.NORMAL);
setCameraMode(CameraMode.NONE);
onLocationLayerStart();
}
onLocationLayerStart();
}
private void initializeLocationEngine(@NonNull Context context) {

View File

@@ -13,6 +13,7 @@ import com.mapbox.geojson.Point;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.location.modes.RenderMode;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.mapboxsdk.style.layers.Layer;
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
@@ -61,6 +62,7 @@ final class LocationLayerController implements MapboxAnimator.OnLayerAnimationsV
private int renderMode;
private final MapboxMap mapboxMap;
private final Style style;
private final LayerSourceProvider layerSourceProvider;
private final LayerBitmapProvider bitmapProvider;
private LocationComponentOptions options;
@@ -71,10 +73,11 @@ final class LocationLayerController implements MapboxAnimator.OnLayerAnimationsV
private boolean isHidden = true;
LocationLayerController(MapboxMap mapboxMap, LayerSourceProvider layerSourceProvider,
LocationLayerController(MapboxMap mapboxMap, Style style, LayerSourceProvider layerSourceProvider,
LayerFeatureProvider featureProvider, LayerBitmapProvider bitmapProvider,
@NonNull LocationComponentOptions options) {
this.mapboxMap = mapboxMap;
this.style = style;
this.layerSourceProvider = layerSourceProvider;
this.bitmapProvider = bitmapProvider;
this.locationFeature = featureProvider.generateLocationFeature(locationFeature, options);
@@ -193,7 +196,7 @@ final class LocationLayerController implements MapboxAnimator.OnLayerAnimationsV
}
private void setLayerVisibility(@NonNull String layerId, boolean visible) {
Layer layer = mapboxMap.getStyle().getLayer(layerId);
Layer layer = style.getLayer(layerId);
if (layer != null) {
String targetVisibility = visible ? VISIBLE : NONE;
if (!layer.getVisibility().value.equals(targetVisibility)) {
@@ -221,7 +224,7 @@ final class LocationLayerController implements MapboxAnimator.OnLayerAnimationsV
}
private void addLayerToMap(Layer layer, @NonNull String idBelowLayer) {
mapboxMap.getStyle().addLayerBelow(layer, idBelowLayer);
style.addLayerBelow(layer, idBelowLayer);
layerMap.add(layer.getId());
}
@@ -243,11 +246,11 @@ final class LocationLayerController implements MapboxAnimator.OnLayerAnimationsV
private void addLocationSource() {
locationSource = layerSourceProvider.generateSource(locationFeature);
mapboxMap.getStyle().addSource(locationSource);
style.addSource(locationSource);
}
private void refreshSource() {
GeoJsonSource source = mapboxMap.getStyle().getSourceAs(LOCATION_SOURCE);
GeoJsonSource source = style.getSourceAs(LOCATION_SOURCE);
if (source != null) {
locationSource.setGeoJson(locationFeature);
}
@@ -272,17 +275,17 @@ final class LocationLayerController implements MapboxAnimator.OnLayerAnimationsV
Bitmap backgroundStaleBitmap = bitmapProvider.generateBitmap(
options.backgroundDrawableStale(), options.backgroundStaleTintColor()
);
mapboxMap.getStyle().addImage(BACKGROUND_ICON, backgroundBitmap);
mapboxMap.getStyle().addImage(BACKGROUND_STALE_ICON, backgroundStaleBitmap);
style.addImage(BACKGROUND_ICON, backgroundBitmap);
style.addImage(BACKGROUND_STALE_ICON, backgroundStaleBitmap);
}
private void styleShadow(@NonNull LocationComponentOptions options) {
mapboxMap.getStyle().addImage(SHADOW_ICON, bitmapProvider.generateShadowBitmap(options));
style.addImage(SHADOW_ICON, bitmapProvider.generateShadowBitmap(options));
}
private void styleBearing(LocationComponentOptions options) {
Bitmap bearingBitmap = bitmapProvider.generateBitmap(options.bearingDrawable(), options.bearingTintColor());
mapboxMap.getStyle().addImage(BEARING_ICON, bearingBitmap);
style.addImage(BEARING_ICON, bearingBitmap);
}
private void styleAccuracy(float accuracyAlpha, @ColorInt int accuracyColor) {
@@ -306,13 +309,13 @@ final class LocationLayerController implements MapboxAnimator.OnLayerAnimationsV
options.gpsDrawable(), options.foregroundStaleTintColor()
);
}
mapboxMap.getStyle().addImage(FOREGROUND_ICON, foregroundBitmap);
mapboxMap.getStyle().addImage(FOREGROUND_STALE_ICON, foregroundBitmapStale);
style.addImage(FOREGROUND_ICON, foregroundBitmap);
style.addImage(FOREGROUND_STALE_ICON, foregroundBitmapStale);
}
private void styleScaling(@NonNull LocationComponentOptions options) {
for (String layerId : layerMap) {
Layer layer = mapboxMap.getStyle().getLayer(layerId);
Layer layer = style.getLayer(layerId);
if (layer instanceof SymbolLayer) {
layer.setProperties(
iconSize(

View File

@@ -1061,7 +1061,7 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
private class MapCallback implements OnDidFinishLoadingStyleListener,
OnDidFinishRenderingFrameListener, OnDidFinishLoadingMapListener,
OnCameraIsChangingListener, OnCameraDidChangeListener {
OnCameraIsChangingListener, OnCameraDidChangeListener, OnDidFailLoadingMapListener {
private final List<OnMapReadyCallback> onMapReadyCallbackList = new ArrayList<>();
@@ -1071,6 +1071,7 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
addOnDidFinishLoadingMapListener(this);
addOnCameraIsChangingListener(this);
addOnCameraDidChangeListener(this);
addOnDidFailLoadingMapListener(this);
}
void initialised() {
@@ -1107,6 +1108,7 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
removeOnDidFinishLoadingMapListener(this);
removeOnCameraIsChangingListener(this);
removeOnCameraDidChangeListener(this);
removeOnDidFailLoadingMapListener(this);
}
@Override
@@ -1116,6 +1118,13 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
}
}
@Override
public void onDidFailLoadingMap(String errorMessage) {
if (mapboxMap != null) {
mapboxMap.onFailLoadingStyle();
}
}
@Override
public void onDidFinishRenderingFrame(boolean fully) {
if (mapboxMap != null) {

View File

@@ -5,6 +5,7 @@ import android.graphics.Bitmap;
import android.graphics.PointF;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.FloatRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@@ -197,12 +198,19 @@ public final class MapboxMap {
}
/**
* Called the map finished loading style.
* Called when the map finished loading a style.
*/
void onFinishLoadingStyle() {
notifyStyleLoaded();
}
/**
* Called when the map failed loading a style.
*/
void onFailLoadingStyle() {
styleLoadedCallbacks.clear();
}
/**
* Called when the region is changing or has changed.
*/
@@ -663,6 +671,7 @@ public final class MapboxMap {
* Loads a new style from the specified offline region definition and moves the map camera to that region.
*
* @param definition the offline region definition
* @param callback the callback to be invoked when the style has loaded
* @see OfflineRegionDefinition
*/
public void setOfflineRegionDefinition(@NonNull OfflineRegionDefinition definition,
@@ -812,7 +821,7 @@ public final class MapboxMap {
// user didn't provide a `from` component,
// flag the style as loaded,
// add components defined added using the `with` prefix.
notifyStyleLoaded();
notifyStyleLoadedDelayed();
}
}
@@ -833,6 +842,15 @@ public final class MapboxMap {
styleLoadedCallbacks.clear();
}
private void notifyStyleLoadedDelayed() {
new Handler().post(new Runnable() {
@Override
public void run() {
notifyStyleLoaded();
}
});
}
/**
* Loads a new map style from MapboxMapOptions if available.
*

View File

@@ -123,7 +123,7 @@ public class Style {
}
/**
* Removes the source. Any references to the source become invalid and should not be used anymore
* Removes the source from the style.
*
* @param sourceId the source to remove
* @return the source handle or null if the source was not present
@@ -261,7 +261,6 @@ public class Style {
* @return the removed layer or null if not found
*/
public boolean removeLayerAt(@IntRange(from = 0) int index) {
// TODO what about runtime added sources?
return nativeMapView.removeLayerAt(index);
}
@@ -366,6 +365,7 @@ public class Style {
* by setting the java sources and layers in a detached state and removing them from core.
*/
void onWillStartLoadingMap() {
styleLoaded = false;
for (Source source : sources.values()) {
if (source != null) {
source.setDetached();
@@ -451,7 +451,7 @@ public class Style {
* </p>
* {@code url} can take the following forms:
* <ul>
* <li>{@code Style.*}: load one of the bundled styles in {@link Style}.</li>
* <li>{@code Style#StyleUrl}: load one of the bundled styles in {@link Style}.</li>
* <li>{@code mapbox://styles/<user>/<style>}:
* loads the style from a <a href="https://www.mapbox.com/account/">Mapbox account.</a>
* {@code user} is your username. {@code style} is the ID of your custom
@@ -461,12 +461,16 @@ public class Style {
* <li>{@code asset://...}:
* loads the style from the APK {@code assets/} directory.
* This is used to load a style bundled with your app.</li>
* <li>{@code file://...}:
* loads the style from a file path. This is used to load a style from disk.
* </li>
* </li>
* <li>{@code null}: loads the default {@link Style#MAPBOX_STREETS} style.</li>
* </ul>
* <p>
* This method is asynchronous and will return before the style finishes loading.
* If you wish to wait for the map to finish loading, listen to the {@link MapView.OnDidFinishLoadingStyleListener}
* callback.
* callback or use {@link MapboxMap#setStyle(String, OnStyleLoaded)} instead.
* </p>
* If the style fails to load or an invalid style URL is set, the map view will become blank.
* An error message will be logged in the Android logcat and {@link MapView.OnDidFailLoadingMapListener} callback