[android] move style callback invocation to MapboxMap
This commit is contained in:
@@ -11,6 +11,7 @@ import android.support.annotation.RequiresPermission;
|
||||
import android.support.annotation.StyleRes;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import com.mapbox.android.core.location.LocationEngine;
|
||||
import com.mapbox.android.core.location.LocationEngineCallback;
|
||||
import com.mapbox.android.core.location.LocationEngineProvider;
|
||||
@@ -27,6 +28,7 @@ import com.mapbox.mapboxsdk.maps.MapboxMap;
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap.OnCameraIdleListener;
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap.OnCameraMoveListener;
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap.OnMapClickListener;
|
||||
import com.mapbox.mapboxsdk.maps.Style;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
@@ -124,11 +126,6 @@ public final class LocationComponent {
|
||||
*/
|
||||
private boolean isComponentInitialized;
|
||||
|
||||
/**
|
||||
* Indicates whether the style has been initialized.
|
||||
*/
|
||||
private boolean isStyleInitialized;
|
||||
|
||||
/**
|
||||
* Indicates that the component is enabled and should be displaying location if Mapbox components are available and
|
||||
* the lifecycle is in a started state.
|
||||
@@ -461,14 +458,16 @@ public final class LocationComponent {
|
||||
*
|
||||
* @param options to update the current style
|
||||
*/
|
||||
public void applyStyle(@NonNull LocationComponentOptions options) {
|
||||
this.options = options;
|
||||
locationLayerController.applyStyle(options);
|
||||
locationCameraController.initializeOptions(options);
|
||||
staleStateManager.setEnabled(options.enableStaleState());
|
||||
staleStateManager.setDelayTime(options.staleStateTimeout());
|
||||
locationAnimatorCoordinator.setTrackingAnimationDurationMultiplier(options.trackingAnimationDurationMultiplier());
|
||||
updateMapWithOptions(options);
|
||||
public void applyStyle(@NonNull final LocationComponentOptions options) {
|
||||
LocationComponent.this.options = options;
|
||||
if (mapboxMap.getStyle() != null) {
|
||||
locationLayerController.applyStyle(options);
|
||||
locationCameraController.initializeOptions(options);
|
||||
staleStateManager.setEnabled(options.enableStaleState());
|
||||
staleStateManager.setDelayTime(options.staleStateTimeout());
|
||||
locationAnimatorCoordinator.setTrackingAnimationDurationMultiplier(options.trackingAnimationDurationMultiplier());
|
||||
updateMapWithOptions(options);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -838,7 +837,6 @@ public final class LocationComponent {
|
||||
* Internal use.
|
||||
*/
|
||||
public void onFinishLoadingStyle() {
|
||||
isStyleInitialized = true;
|
||||
if (isComponentInitialized) {
|
||||
locationLayerController.initializeComponents(options);
|
||||
locationCameraController.initializeOptions(options);
|
||||
@@ -848,7 +846,7 @@ public final class LocationComponent {
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
private void onLocationLayerStart() {
|
||||
if (!isComponentInitialized || !isComponentStarted) {
|
||||
if (!isComponentInitialized || !isComponentStarted || mapboxMap.getStyle() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -894,45 +892,48 @@ public final class LocationComponent {
|
||||
mapboxMap.removeOnCameraIdleListener(onCameraIdleListener);
|
||||
}
|
||||
|
||||
private void initialize(@NonNull Context context, @NonNull LocationComponentOptions options) {
|
||||
if (isComponentInitialized) {
|
||||
return;
|
||||
}
|
||||
isComponentInitialized = true;
|
||||
this.options = options;
|
||||
private void initialize(@NonNull final Context context, @NonNull final LocationComponentOptions options) {
|
||||
mapboxMap.getStyle(new Style.OnStyleLoaded() {
|
||||
@Override
|
||||
public void onStyleLoaded(@NonNull Style style) {
|
||||
if (isComponentInitialized) {
|
||||
return;
|
||||
}
|
||||
isComponentInitialized = true;
|
||||
LocationComponent.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, 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);
|
||||
|
||||
if (isStyleInitialized) {
|
||||
onFinishLoadingStyle();
|
||||
}
|
||||
|
||||
onLocationLayerStart();
|
||||
onLocationLayerStart();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void initializeLocationEngine(@NonNull Context context) {
|
||||
|
||||
@@ -78,6 +78,7 @@ final class LocationLayerController implements MapboxAnimator.OnLayerAnimationsV
|
||||
this.layerSourceProvider = layerSourceProvider;
|
||||
this.bitmapProvider = bitmapProvider;
|
||||
this.locationFeature = featureProvider.generateLocationFeature(locationFeature, options);
|
||||
initializeComponents(options);
|
||||
setRenderMode(RenderMode.NORMAL);
|
||||
}
|
||||
|
||||
|
||||
@@ -1059,14 +1059,13 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
|
||||
}
|
||||
}
|
||||
|
||||
private class MapCallback implements OnWillStartLoadingMapListener, OnDidFinishLoadingStyleListener,
|
||||
private class MapCallback implements OnDidFinishLoadingStyleListener,
|
||||
OnDidFinishRenderingFrameListener, OnDidFinishLoadingMapListener,
|
||||
OnCameraIsChangingListener, OnCameraDidChangeListener {
|
||||
|
||||
private final List<OnMapReadyCallback> onMapReadyCallbackList = new ArrayList<>();
|
||||
|
||||
MapCallback() {
|
||||
addOnWillStartLoadingMapListener(this);
|
||||
addOnDidFinishLoadingStyleListener(this);
|
||||
addOnDidFinishRenderingFrameListener(this);
|
||||
addOnDidFinishLoadingMapListener(this);
|
||||
@@ -1103,7 +1102,6 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
|
||||
|
||||
void onDestroy() {
|
||||
onMapReadyCallbackList.clear();
|
||||
removeOnWillStartLoadingMapListener(this);
|
||||
removeOnDidFinishLoadingStyleListener(this);
|
||||
removeOnDidFinishRenderingFrameListener(this);
|
||||
removeOnDidFinishLoadingMapListener(this);
|
||||
@@ -1111,13 +1109,6 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
|
||||
removeOnCameraDidChangeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWillStartLoadingMap() {
|
||||
if (mapboxMap != null) {
|
||||
mapboxMap.onStartLoadingMap();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDidFinishLoadingStyle() {
|
||||
if (mapboxMap != null) {
|
||||
|
||||
@@ -12,6 +12,7 @@ import android.support.annotation.Size;
|
||||
import android.support.annotation.UiThread;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
|
||||
import com.mapbox.android.gestures.AndroidGesturesManager;
|
||||
import com.mapbox.android.gestures.MoveGestureDetector;
|
||||
import com.mapbox.android.gestures.RotateGestureDetector;
|
||||
@@ -19,6 +20,7 @@ import com.mapbox.android.gestures.ShoveGestureDetector;
|
||||
import com.mapbox.android.gestures.StandardScaleGestureDetector;
|
||||
import com.mapbox.geojson.Feature;
|
||||
import com.mapbox.geojson.Geometry;
|
||||
import com.mapbox.mapboxsdk.MapStrictMode;
|
||||
import com.mapbox.mapboxsdk.annotations.Annotation;
|
||||
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
@@ -69,7 +71,7 @@ public final class MapboxMap {
|
||||
@Nullable
|
||||
private MapboxMap.OnFpsChangedListener onFpsChangedListener;
|
||||
|
||||
private List<Style.OnStyleLoaded> styleLoadedCallbacks = new ArrayList<>();
|
||||
private final List<Style.OnStyleLoaded> styleLoadedCallbacks = new ArrayList<>();
|
||||
|
||||
@Nullable
|
||||
private Style style;
|
||||
@@ -102,10 +104,10 @@ public final class MapboxMap {
|
||||
* Get the Style of the map asynchronously.
|
||||
*/
|
||||
public void getStyle(@NonNull Style.OnStyleLoaded onStyleLoaded) {
|
||||
if (style == null) {
|
||||
styleLoadedCallbacks.add(onStyleLoaded);
|
||||
} else {
|
||||
if (style != null && style.isStyleLoaded()) {
|
||||
onStyleLoaded.onStyleLoaded(style);
|
||||
} else {
|
||||
styleLoadedCallbacks.add(onStyleLoaded);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,7 +121,11 @@ public final class MapboxMap {
|
||||
*/
|
||||
@Nullable
|
||||
public Style getStyle() {
|
||||
return style;
|
||||
if (style == null || !style.isStyleLoaded()) {
|
||||
return null;
|
||||
} else {
|
||||
return style;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -194,18 +200,11 @@ public final class MapboxMap {
|
||||
transform.invalidateCameraPosition();
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the map will start loading style.
|
||||
*/
|
||||
void onStartLoadingMap() {
|
||||
locationComponent.onStartLoadingMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* Called the map finished loading style.
|
||||
*/
|
||||
void onFinishLoadingStyle() {
|
||||
locationComponent.onFinishLoadingStyle();
|
||||
notifyStyleLoaded();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -750,19 +749,39 @@ public final class MapboxMap {
|
||||
}
|
||||
|
||||
public void setStyle(Style.Builder builder, final Style.OnStyleLoaded callback) {
|
||||
builder.build(nativeMapView, new Style.OnStyleLoaded() {
|
||||
@Override
|
||||
public void onStyleLoaded(Style style) {
|
||||
MapboxMap.this.style = style;
|
||||
if (callback != null) {
|
||||
callback.onStyleLoaded(style);
|
||||
}
|
||||
for (Style.OnStyleLoaded styleLoadedCallback : styleLoadedCallbacks) {
|
||||
styleLoadedCallback.onStyleLoaded(style);
|
||||
}
|
||||
styleLoadedCallbacks.clear();
|
||||
locationComponent.onStartLoadingMap();
|
||||
if (style != null) {
|
||||
style.onWillStartLoadingMap();
|
||||
}
|
||||
|
||||
if (callback != null) {
|
||||
styleLoadedCallbacks.add(callback);
|
||||
}
|
||||
|
||||
style = builder.build(nativeMapView);
|
||||
if (!TextUtils.isEmpty(style.getUrl())) {
|
||||
nativeMapView.setStyleUrl(style.getUrl());
|
||||
} else if (!TextUtils.isEmpty(style.getJson())) {
|
||||
nativeMapView.setStyleJson(style.getJson());
|
||||
} else {
|
||||
// user didn't provide a `from` component,
|
||||
// flag the style as loaded,
|
||||
// add components defined added using the `with` prefix.
|
||||
notifyStyleLoaded();
|
||||
}
|
||||
}
|
||||
|
||||
private void notifyStyleLoaded() {
|
||||
if (style != null) {
|
||||
style.onDidFinishLoadingStyle();
|
||||
locationComponent.onFinishLoadingStyle();
|
||||
for (Style.OnStyleLoaded styleLoadedCallback : styleLoadedCallbacks) {
|
||||
styleLoadedCallback.onStyleLoaded(style);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
MapStrictMode.strictModeViolation("No style to provide.");
|
||||
}
|
||||
styleLoadedCallbacks.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,6 +12,7 @@ import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.text.TextUtils;
|
||||
import android.util.DisplayMetrics;
|
||||
|
||||
import com.mapbox.geojson.Feature;
|
||||
import com.mapbox.geojson.Geometry;
|
||||
import com.mapbox.mapboxsdk.LibraryLoader;
|
||||
@@ -71,9 +72,6 @@ final class NativeMapView {
|
||||
// Flag to indicating destroy was called
|
||||
private boolean destroyed = false;
|
||||
|
||||
// Used for style load callbacks
|
||||
private StyleCallback styleCallback;
|
||||
|
||||
// Holds the pointer to JNI NativeMapView
|
||||
@Keep
|
||||
long nativePtr = 0;
|
||||
@@ -940,9 +938,6 @@ final class NativeMapView {
|
||||
@Keep
|
||||
private void onWillStartLoadingMap() {
|
||||
stateCallback.onWillStartLoadingMap();
|
||||
if (styleCallback != null) {
|
||||
styleCallback.onWillStartLoadingMap();
|
||||
}
|
||||
}
|
||||
|
||||
@Keep
|
||||
@@ -977,7 +972,6 @@ final class NativeMapView {
|
||||
|
||||
@Keep
|
||||
private void onDidFinishLoadingStyle() {
|
||||
styleCallback.onDidFinishLoadingStyle();
|
||||
stateCallback.onDidFinishLoadingStyle();
|
||||
}
|
||||
|
||||
@@ -1342,10 +1336,6 @@ final class NativeMapView {
|
||||
});
|
||||
}
|
||||
|
||||
void setStyleCallback(@NonNull StyleCallback callback) {
|
||||
this.styleCallback = callback;
|
||||
}
|
||||
|
||||
//
|
||||
// Image conversion
|
||||
//
|
||||
|
||||
@@ -5,6 +5,7 @@ import android.support.annotation.IntRange;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.StringDef;
|
||||
|
||||
import com.mapbox.mapboxsdk.constants.MapboxConstants;
|
||||
import com.mapbox.mapboxsdk.style.layers.Layer;
|
||||
import com.mapbox.mapboxsdk.style.layers.TransitionOptions;
|
||||
@@ -26,14 +27,15 @@ import java.util.Map;
|
||||
* has been loaded by underlying map.
|
||||
* </p>
|
||||
*/
|
||||
public class Style implements NativeMapView.StyleCallback {
|
||||
public class Style {
|
||||
|
||||
private final NativeMapView nativeMapView;
|
||||
private final HashMap<String, Source> sources = new HashMap<>();
|
||||
private final HashMap<String, Layer> layers = new HashMap<>();
|
||||
private final HashMap<String, Bitmap> images = new HashMap<>();
|
||||
private final OnStyleLoaded onStyleLoaded;
|
||||
private final Builder builder;
|
||||
private String styleUrl;
|
||||
private String styleJson;
|
||||
private boolean styleLoaded;
|
||||
|
||||
/**
|
||||
@@ -41,12 +43,12 @@ public class Style implements NativeMapView.StyleCallback {
|
||||
*
|
||||
* @param builder the builder used for creating this style
|
||||
* @param nativeMapView the map object used to load this style
|
||||
* @param styleLoaded the callback used to notify about finish style loading
|
||||
*/
|
||||
private Style(@NonNull Builder builder, @NonNull NativeMapView nativeMapView, @Nullable OnStyleLoaded styleLoaded) {
|
||||
private Style(@NonNull Builder builder, @NonNull NativeMapView nativeMapView) {
|
||||
this.builder = builder;
|
||||
styleUrl = builder.styleUrl;
|
||||
styleJson = builder.styleJson;
|
||||
this.nativeMapView = nativeMapView;
|
||||
this.onStyleLoaded = styleLoaded;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,9 +56,9 @@ public class Style implements NativeMapView.StyleCallback {
|
||||
*
|
||||
* @return the style url
|
||||
*/
|
||||
@NonNull
|
||||
@Nullable
|
||||
public String getUrl() {
|
||||
return nativeMapView.getStyleUrl();
|
||||
return styleUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,9 +66,9 @@ public class Style implements NativeMapView.StyleCallback {
|
||||
*
|
||||
* @return the style json
|
||||
*/
|
||||
@NonNull
|
||||
@Nullable
|
||||
public String getJson() {
|
||||
return nativeMapView.getStyleJson();
|
||||
return styleJson;
|
||||
}
|
||||
|
||||
//
|
||||
@@ -367,8 +369,7 @@ public class Style implements NativeMapView.StyleCallback {
|
||||
* Called when the underlying map will start loading a new style. This method will clean up this style
|
||||
* by setting the java sources and layers in a detached state and removing them from core.
|
||||
*/
|
||||
@Override
|
||||
public void onWillStartLoadingMap() {
|
||||
void onWillStartLoadingMap() {
|
||||
for (Source source : sources.values()) {
|
||||
if (source != null) {
|
||||
source.setDetached();
|
||||
@@ -397,8 +398,7 @@ public class Style implements NativeMapView.StyleCallback {
|
||||
* Called when the underlying map has finished loading this style.
|
||||
* This method will add all components added to the builder that were defined with the 'with' prefix.
|
||||
*/
|
||||
@Override
|
||||
public void onDidFinishLoadingStyle() {
|
||||
void onDidFinishLoadingStyle() {
|
||||
if (!styleLoaded) {
|
||||
styleLoaded = true;
|
||||
for (Source source : builder.sources) {
|
||||
@@ -425,13 +425,13 @@ public class Style implements NativeMapView.StyleCallback {
|
||||
if (builder.transitionOptions != null) {
|
||||
setTransition(builder.transitionOptions);
|
||||
}
|
||||
|
||||
if (onStyleLoaded != null) {
|
||||
onStyleLoaded.onStyleLoaded(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean isStyleLoaded() {
|
||||
return styleLoaded;
|
||||
}
|
||||
|
||||
//
|
||||
// Builder
|
||||
//
|
||||
@@ -601,24 +601,9 @@ public class Style implements NativeMapView.StyleCallback {
|
||||
|
||||
/**
|
||||
* Build the composed style.
|
||||
*
|
||||
* @param nativeMapView the native map used for style loading
|
||||
* @param styleLoaded the callback to be invoked when the style has loaded
|
||||
*/
|
||||
void build(@NonNull NativeMapView nativeMapView, @Nullable OnStyleLoaded styleLoaded) {
|
||||
Style style = new Style(this, nativeMapView, styleLoaded);
|
||||
nativeMapView.setStyleCallback(style);
|
||||
|
||||
if (styleUrl != null) {
|
||||
nativeMapView.setStyleUrl(styleUrl);
|
||||
} else if (styleJson != null) {
|
||||
nativeMapView.setStyleJson(styleJson);
|
||||
} else {
|
||||
// user didn't provide a `from` component,
|
||||
// flag the style as loaded,
|
||||
// add components defined added using the `with` prefix.
|
||||
style.onDidFinishLoadingStyle();
|
||||
}
|
||||
Style build(@NonNull NativeMapView nativeMapView) {
|
||||
return new Style(this, nativeMapView);
|
||||
}
|
||||
|
||||
private class ImageWrapper {
|
||||
|
||||
Reference in New Issue
Block a user