diff --git a/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java b/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java index 31c1ae982..718a9200e 100644 --- a/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java +++ b/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java @@ -932,23 +932,23 @@ public Builder withBitmapImages(boolean sdf, @NonNull Pair... va return this; } - String getUri() { + public String getUri() { return styleUri; } - String getJson() { + public String getJson() { return styleJson; } - List getSources() { + public List getSources() { return sources; } - List getLayers() { + public List getLayers() { return layers; } - List getImages() { + public List getImages() { return images; } @@ -963,7 +963,7 @@ Style build(@NonNull NativeMap nativeMap) { return new Style(this, nativeMap); } - static class ImageWrapper { + public static class ImageWrapper { Bitmap bitmap; String id; boolean sdf; @@ -985,39 +985,55 @@ static ImageWrapper[] convertToImageArray(HashMap bitmapHashMap, } } - class LayerWrapper { + public class LayerWrapper { Layer layer; LayerWrapper(Layer layer) { this.layer = layer; } + + public Layer getLayer() { + return layer; + } } - class LayerAboveWrapper extends LayerWrapper { + public class LayerAboveWrapper extends LayerWrapper { String aboveLayer; LayerAboveWrapper(Layer layer, String aboveLayer) { super(layer); this.aboveLayer = aboveLayer; } + + public String getAboveLayer() { + return aboveLayer; + } } - class LayerBelowWrapper extends LayerWrapper { + public class LayerBelowWrapper extends LayerWrapper { String belowLayer; LayerBelowWrapper(Layer layer, String belowLayer) { super(layer); this.belowLayer = belowLayer; } + + public String getBelowLayer() { + return belowLayer; + } } - class LayerAtWrapper extends LayerWrapper { + public class LayerAtWrapper extends LayerWrapper { int index; LayerAtWrapper(Layer layer, int index) { super(layer); this.index = index; } + + public int getIndex() { + return index; + } } } diff --git a/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnapshotter.java b/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnapshotter.java index 2df185962..2d5893d4b 100644 --- a/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnapshotter.java +++ b/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnapshotter.java @@ -6,7 +6,6 @@ import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.PointF; -import android.graphics.drawable.Drawable; import android.os.Handler; import android.text.Html; import android.text.TextUtils; @@ -27,17 +26,10 @@ import com.mapbox.mapboxsdk.maps.Style; import com.mapbox.mapboxsdk.maps.TelemetryDefinition; import com.mapbox.mapboxsdk.storage.FileSource; -import com.mapbox.mapboxsdk.style.layers.Layer; -import com.mapbox.mapboxsdk.style.layers.TransitionOptions; -import com.mapbox.mapboxsdk.style.light.Light; import com.mapbox.mapboxsdk.style.sources.Source; import com.mapbox.mapboxsdk.utils.FontUtils; import com.mapbox.mapboxsdk.utils.ThreadUtils; -import java.util.HashMap; -import java.util.List; - -import androidx.annotation.IntRange; import androidx.annotation.Keep; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -54,6 +46,23 @@ public class MapSnapshotter { private static final String TAG = "Mbgl-MapSnapshotter"; + private static final int LOGO_MARGIN_DP = 4; + + // Holds the pointer to JNI NativeMapView + @Keep + private long nativePtr = 0; + + private final Context context; + private boolean fullyLoaded = false; + private Options options; + + @Nullable + private SnapshotReadyCallback callback; + @Nullable + private ErrorHandler errorHandler; + @Nullable + private MapSnapshotterObserver observer; + /** * Get notified on snapshot completion. * @@ -77,87 +86,6 @@ public interface MapSnapshotterObserver { void onStyleImageMissing(MapSnapshotter snapshotter, String imageName); } -// public class MapSnapshotStyle { -// private MapSnapshotStyle() { -// } -// -// public void addLayer(@NonNull Layer layer) { -// MapSnapshotter.this.addLayer(layer); -// } -// -// public void addLayerBelow(@NonNull Layer layer, @NonNull String below) { -// } -// -// public void addLayerAbove(@NonNull Layer layer, @NonNull String above) { -// } -// -// public void addLayerAt(@NonNull Layer layer, @IntRange(from = 0) int index) { -// } -// -// @NonNull -// public List getLayers() { -// } -// -// public Layer getLayer(String layerId) { -// } -// -// public boolean removeLayer(@NonNull String layerId) { -// } -// -// public boolean removeLayer(@NonNull Layer layer) { -// } -// -// public boolean removeLayerAt(@IntRange(from = 0) int index) { -// } -// -// public void addSource(@NonNull Source source) { -// } -// -// @NonNull -// public List getSources() { -// } -// -// public Source getSource(@NonNull String sourceId) { -// } -// -// public boolean removeSource(@NonNull String sourceId) { -// } -// -// public boolean removeSource(@NonNull Source source) { -// } -// -// public void setTransitionOptions(@NonNull TransitionOptions transitionOptions) { -// } -// -// public void addImage(@NonNull String name, @NonNull Bitmap image) { -// addImage(name, image, false); -// } -// -// public void addImage(@NonNull String name, @NonNull Drawable drawable) { -// -// } -// -// public void addImage(@NonNull final String name, @NonNull Bitmap bitmap, boolean sdf) { -// } -// -// public void addImages(@NonNull HashMap images) { -// addImages(images, false); -// } -// -// public void addImages(@NonNull HashMap images, boolean sdf) { -// -// } -// -// public Bitmap getImage(String name) { -// } -// -// public void removeImage(String name) { -// } -// -// public Light getLight() { -// } -// } - /** * Can be used to get notified of errors * in snapshot generation @@ -175,20 +103,6 @@ public interface ErrorHandler { void onError(String error); } - private static final int LOGO_MARGIN_DP = 4; - - // Holds the pointer to JNI NativeMapView - @Keep - private long nativePtr = 0; - - private final Context context; - @Nullable - private SnapshotReadyCallback callback; - @Nullable - private ErrorHandler errorHandler; - @Nullable - private MapSnapshotterObserver observer; - /** * MapSnapshotter options */ @@ -196,13 +110,12 @@ public static class Options { private float pixelRatio = 1; private int width; private int height; - private String styleUri = Style.MAPBOX_STREETS; - private String styleJson; private LatLngBounds region; private CameraPosition cameraPosition; private boolean showLogo = true; private String localIdeographFontFamily = MapboxConstants.DEFAULT_FONT; private String apiBaseUrl; + private Style.Builder builder; /** * @param width the width of the image @@ -217,23 +130,20 @@ public Options(int width, int height) { } /** - * @param uri The style URI to use + * Set a style builder to snapshotter, the contents in builder like layers/sources/images will be applied + * to snapshotter. + * + * @param builder The builder will applied to snapshotter * @return the mutated {@link Options} */ @NonNull - public Options withStyle(String uri) { - this.styleUri = uri; + public Options withStyleBuilder(Style.Builder builder) { + this.builder = builder; return this; } - /** - * @param styleJson The style json to use - * @return the mutated {@link Options} - */ - @NonNull - public Options withStyleJson(String styleJson) { - this.styleJson = styleJson; - return this; + public Style.Builder getBuilder() { + return builder; } /** @@ -376,14 +286,22 @@ public LatLngBounds getRegion() { */ @Deprecated public String getStyleUrl() { - return styleUri; + return builder == null ? Style.MAPBOX_STREETS : builder.getUri(); } /** * @return the style uri */ public String getStyleUri() { - return styleUri; + return builder == null ? Style.MAPBOX_STREETS : builder.getUri(); + } + + /** + * @return the style json + */ + @Nullable + public String getStyleJson() { + return builder == null ? null : builder.getJson(); } /** @@ -435,6 +353,7 @@ public MapSnapshotter(@NonNull Context context, @NonNull Options options) { public MapSnapshotter(@NonNull Context context, @NonNull Options options, MapSnapshotterObserver observer_) { checkThread(); this.context = context.getApplicationContext(); + this.options = options; observer = observer_; TelemetryDefinition telemetry = Mapbox.getTelemetry(); if (telemetry != null) { @@ -447,12 +366,11 @@ public MapSnapshotter(@NonNull Context context, @NonNull Options options, MapSna } nativeInitialize(this, fileSource, options.pixelRatio, options.width, - options.height, options.styleUri, options.styleJson, options.region, options.cameraPosition, + options.height, options.getStyleUri(), options.getStyleJson(), options.region, options.cameraPosition, options.showLogo, options.localIdeographFontFamily); } - /** * Starts loading and rendering the snapshot. The callback will be fired * on the calling thread. @@ -531,10 +449,6 @@ public void cancel() { nativeCancel(); } - public void addLayer(@NonNull Layer layer) { - nativeAddLayer(layer.getNativePtr(), null); - } - /** * Draw an overlay on the map snapshot. * @@ -700,7 +614,7 @@ protected void onSnapshotReady(@NonNull final MapSnapshot snapshot) { @Override public void run() { if (callback != null) { - addOverlay(snapshot); +// addOverlay(snapshot); callback.onSnapshotReady(snapshot); reset(); } @@ -722,7 +636,9 @@ protected void onSnapshotFailed(String reason) { } } - // TODO: add documentation + /** + * Called by JNI peer when snapshot style is ready. + */ @Keep protected void onDidFailLoadingStyle(String reason) { if (observer != null) { @@ -737,6 +653,34 @@ protected void onDidFinishLoadingStyle() { if (observer != null) { observer.onDidFinishLoadingStyle(this); } + if (!fullyLoaded) { + fullyLoaded = true; + Style.Builder builder = options.getBuilder(); + if (builder == null) { + return; + } + for (Source source : builder.getSources()) { +// nativeAddSource(source, source.getNativePtr()); + } + + for (Style.Builder.LayerWrapper layerWrapper : builder.getLayers()) { + if (layerWrapper instanceof Style.Builder.LayerAtWrapper) { + nativeAddLayerAt(layerWrapper.getLayer().getNativePtr(), ((Style.Builder.LayerAtWrapper) layerWrapper).getIndex()); + } else if (layerWrapper instanceof Style.Builder.LayerAboveWrapper) { + nativeAddLayerAbove(layerWrapper.getLayer().getNativePtr(), ((Style.Builder.LayerAboveWrapper) layerWrapper).getAboveLayer()); + } else if (layerWrapper instanceof Style.Builder.LayerBelowWrapper) { + nativeAddLayerBelow(layerWrapper.getLayer().getNativePtr(), ((Style.Builder.LayerBelowWrapper) layerWrapper).getBelowLayer()); + } else { + // just add layer to map, but below annotations + nativeAddLayerBelow(layerWrapper.getLayer().getNativePtr(), MapboxConstants.LAYER_ID_ANNOTATIONS); + } + } + + for (Style.Builder.ImageWrapper image : builder.getImages()) { +// addImage(image.getId(), image.getBitmap(), image.isSdf()); + } + + } } // TODO: add documentation @@ -770,8 +714,23 @@ protected native void nativeInitialize(MapSnapshotter mapSnapshotter, protected native void nativeCancel(); @Keep - public native void nativeAddLayer(long layerPtr, String before); + public native void nativeAddLayerBelow(long layerPtr, String before); + + @Keep + private native void nativeAddLayerAbove(long layerPtr, String above); + + @Keep + private native void nativeAddLayerAt(long layerPtr, int index); + +// @NonNull +// @Keep +// private native Source nativeGetSource(String sourceId); +// +// @Keep +// private native void nativeAddSource(Source source, long sourcePtr); + // @Keep +// private native void nativeAddImages(Image[] images); @Override @Keep protected native void finalize() throws Throwable; diff --git a/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/render/RenderTestDefinition.java b/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/render/RenderTestDefinition.java index 3cff4cad5..fbd8251be 100644 --- a/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/render/RenderTestDefinition.java +++ b/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/render/RenderTestDefinition.java @@ -1,5 +1,6 @@ package com.mapbox.mapboxsdk.testapp.activity.render; +import com.mapbox.mapboxsdk.maps.Style; import com.mapbox.mapboxsdk.snapshotter.MapSnapshotter; public class RenderTestDefinition { @@ -75,7 +76,7 @@ public RenderTestStyleDefinition.Test getTest() { public MapSnapshotter.Options toOptions() { return new MapSnapshotter .Options(getWidth(), getHeight()) - .withStyleJson(styleJson) + .withStyleBuilder(new Style.Builder().fromJson(styleJson)) .withPixelRatio(getPixelRatio()) .withLogo(false); } diff --git a/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/snapshot/MapSnapshotterActivity.java b/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/snapshot/MapSnapshotterActivity.java index ebd2c929a..f35bd7d59 100644 --- a/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/snapshot/MapSnapshotterActivity.java +++ b/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/snapshot/MapSnapshotterActivity.java @@ -2,30 +2,27 @@ import android.graphics.Color; import android.os.Bundle; -import androidx.appcompat.app.AppCompatActivity; import android.view.ViewTreeObserver; import android.widget.GridLayout; import android.widget.ImageView; import com.mapbox.mapboxsdk.camera.CameraPosition; import com.mapbox.mapboxsdk.constants.MapboxConstants; -import com.mapbox.mapboxsdk.maps.Style; import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.geometry.LatLngBounds; +import com.mapbox.mapboxsdk.maps.Style; import com.mapbox.mapboxsdk.snapshotter.MapSnapshotter; import com.mapbox.mapboxsdk.style.layers.BackgroundLayer; -import com.mapbox.mapboxsdk.style.layers.FillLayer; import com.mapbox.mapboxsdk.testapp.R; import java.util.ArrayList; import java.util.List; import java.util.Random; +import androidx.appcompat.app.AppCompatActivity; import timber.log.Timber; -import static com.mapbox.mapboxsdk.style.expressions.Expression.color; import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.backgroundColor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillColor; /** * Test activity showing how to use a the {@link com.mapbox.mapboxsdk.snapshotter.MapSnapshotter} @@ -68,6 +65,7 @@ class SnapshotterObserver implements MapSnapshotter.MapSnapshotterObserver { private MapSnapshotterActivity mapSnapshotterActivity; private int row; private int column; + public SnapshotterObserver(MapSnapshotterActivity mapSnapshotterActivity, int row, int column) { this.mapSnapshotterActivity = mapSnapshotterActivity; this.row = row; @@ -80,16 +78,13 @@ public void onDidFailLoadingStyle(MapSnapshotter snapshotter, String error) { @Override public void onDidFinishLoadingStyle(MapSnapshotter snapshotter) { - BackgroundLayer bg = new BackgroundLayer("green_tint"); - bg.setProperties(backgroundColor(Color.valueOf(randomInRange(0.0f, 1.0f), randomInRange(0.0f, 1.0f), randomInRange(0.0f, 1.0f), 0.2f).toArgb())); - snapshotter.addLayer(bg); snapshotter.start(snapshot -> { Timber.i("Got the snapshot"); ImageView imageView = new ImageView(MapSnapshotterActivity.this); imageView.setImageBitmap(snapshot.getBitmap()); grid.addView( - imageView, - new GridLayout.LayoutParams(GridLayout.spec(row), GridLayout.spec(column)) + imageView, + new GridLayout.LayoutParams(GridLayout.spec(row), GridLayout.spec(column)) ); }); } @@ -101,7 +96,9 @@ public void onStyleImageMissing(MapSnapshotter snapshotter, String imageName) { } private void startSnapShot(final int row, final int column) { - + // Optionally the style + Style.Builder builder = new Style.Builder() + .fromUri((column + row) % 2 == 0 ? Style.MAPBOX_STREETS : Style.DARK); // Define the dimensions MapSnapshotter.Options options = new MapSnapshotter.Options( grid.getMeasuredWidth() / grid.getColumnCount(), @@ -109,9 +106,6 @@ private void startSnapShot(final int row, final int column) { ) // Optionally the pixel ratio .withPixelRatio(1) - - // Optionally the style - .withStyle((column + row) % 2 == 0 ? Style.MAPBOX_STREETS : Style.DARK) .withLocalIdeographFontFamily(MapboxConstants.DEFAULT_FONT); // Optionally the visible region @@ -137,6 +131,10 @@ private void startSnapShot(final int row, final int column) { ); } + BackgroundLayer bg = new BackgroundLayer("green_tint"); + bg.setProperties(backgroundColor(Color.valueOf(randomInRange(0.0f, 1.0f), randomInRange(0.0f, 1.0f), randomInRange(0.0f, 1.0f), 0.2f).toArgb())); + builder.withLayerAbove(bg,"country-label"); + options.withStyleBuilder(builder); MapSnapshotter snapshotter = new MapSnapshotter(MapSnapshotterActivity.this, options, new SnapshotterObserver(this, row, column)); snapshotters.add(snapshotter); } diff --git a/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/snapshot/MapSnapshotterLocalStyleActivity.java b/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/snapshot/MapSnapshotterLocalStyleActivity.java index 1dda9f655..3b8c9ae72 100644 --- a/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/snapshot/MapSnapshotterLocalStyleActivity.java +++ b/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/snapshot/MapSnapshotterLocalStyleActivity.java @@ -7,6 +7,7 @@ import android.widget.ImageView; import com.mapbox.mapboxsdk.camera.CameraPosition; import com.mapbox.mapboxsdk.geometry.LatLng; +import com.mapbox.mapboxsdk.maps.Style; import com.mapbox.mapboxsdk.snapshotter.MapSnapshot; import com.mapbox.mapboxsdk.snapshotter.MapSnapshotter; import com.mapbox.mapboxsdk.testapp.R; @@ -48,7 +49,7 @@ public void onGlobalLayout() { getApplicationContext(), new MapSnapshotter .Options(Math.min(container.getMeasuredWidth(), 1024), Math.min(container.getMeasuredHeight(), 1024)) - .withStyleJson(styleJson) + .withStyleBuilder(new Style.Builder().fromJson(styleJson)) .withCameraPosition(new CameraPosition.Builder().target(new LatLng(52.090737, 5.121420)).zoom(18).build()) ); mapSnapshotter.start(MapSnapshotterLocalStyleActivity.this, error -> Timber.e(error)); diff --git a/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/snapshot/MapSnapshotterMarkerActivity.java b/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/snapshot/MapSnapshotterMarkerActivity.java index 5190bce90..171bac313 100644 --- a/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/snapshot/MapSnapshotterMarkerActivity.java +++ b/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/snapshot/MapSnapshotterMarkerActivity.java @@ -49,7 +49,7 @@ public void onGlobalLayout() { getApplicationContext(), new MapSnapshotter .Options(Math.min(container.getMeasuredWidth(), 1024), Math.min(container.getMeasuredHeight(), 1024)) - .withStyle(Style.OUTDOORS) + .withStyleBuilder(new Style.Builder().fromUri(Style.OUTDOORS)) .withCameraPosition(new CameraPosition.Builder().target(new LatLng(52.090737, 5.121420)).zoom(15).build()) ); mapSnapshotter.start(MapSnapshotterMarkerActivity.this); diff --git a/vendor/mapbox-gl-native b/vendor/mapbox-gl-native index c4502372e..c453bcad5 160000 --- a/vendor/mapbox-gl-native +++ b/vendor/mapbox-gl-native @@ -1 +1 @@ -Subproject commit c4502372e690b60ad9d4dea430531a1463fac635 +Subproject commit c453bcad570f378883fbc6de623348034499ce52