From 2fd4615996b33c39e83ee5d5be29d8d8c68a8d9d Mon Sep 17 00:00:00 2001 From: Tobrun Date: Thu, 18 Jan 2018 14:50:52 +0100 Subject: add native methods --- .../main/java/com/mapbox/mapboxsdk/maps/Image.java | 2 +- .../com/mapbox/mapboxsdk/maps/NativeMapView.java | 44 ------ .../java/com/mapbox/mapboxsdk/style/Style.java | 149 +++++++++++++-------- 3 files changed, 93 insertions(+), 102 deletions(-) diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Image.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Image.java index b2f6cef3b0..dc45e71df7 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Image.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Image.java @@ -1,6 +1,6 @@ package com.mapbox.mapboxsdk.maps; -class Image { +public class Image { private final byte[] buffer; private final float pixelRatio; private final String name; diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java index f1635c898f..03d398776a 100755 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java @@ -1132,49 +1132,5 @@ final class NativeMapView { // Image conversion // - private static class BitmapImageConversionTask extends AsyncTask, Void, List> { - private NativeMapView nativeMapView; - - BitmapImageConversionTask(NativeMapView nativeMapView) { - this.nativeMapView = nativeMapView; - } - - @Override - protected List doInBackground(HashMap... params) { - HashMap bitmapHashMap = params[0]; - - List images = new ArrayList<>(); - ByteBuffer buffer; - String name; - Bitmap bitmap; - - for (Map.Entry stringBitmapEntry : bitmapHashMap.entrySet()) { - name = stringBitmapEntry.getKey(); - bitmap = stringBitmapEntry.getValue(); - - if (bitmap.getConfig() != Bitmap.Config.ARGB_8888) { - bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, false); - } - - buffer = ByteBuffer.allocate(bitmap.getByteCount()); - bitmap.copyPixelsToBuffer(buffer); - - float density = bitmap.getDensity() == Bitmap.DENSITY_NONE ? Bitmap.DENSITY_NONE : bitmap.getDensity(); - float pixelRatio = density / DisplayMetrics.DENSITY_DEFAULT; - - images.add(new Image(buffer.array(), pixelRatio, name, bitmap.getWidth(), bitmap.getHeight())); - } - - return images; - } - - @Override - protected void onPostExecute(List images) { - super.onPostExecute(images); - if (nativeMapView != null && !nativeMapView.isDestroyedOn("nativeAddImages")) { - nativeMapView.nativeAddImages(images.toArray(new Image[images.size()])); - } - } - } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/Style.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/Style.java index 1bf5d25b4a..442850f0e4 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/Style.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/Style.java @@ -1,111 +1,146 @@ package com.mapbox.mapboxsdk.style; import android.graphics.Bitmap; +import android.os.AsyncTask; import android.support.annotation.IntRange; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.util.DisplayMetrics; import com.mapbox.mapboxsdk.camera.CameraPosition; +import com.mapbox.mapboxsdk.maps.Image; +import com.mapbox.mapboxsdk.style.layers.CannotAddLayerException; 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.CannotAddSourceException; import com.mapbox.mapboxsdk.style.sources.Source; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class Style { - void loadJson(String json) { + private Style(){ } - void loadURL(String url) { - + public static Style fromJson(String json){ + return new Style(); } - String getJson() { - return ""; + public static Style fromUrl(String url) { + return new Style(); } - String getURL() { - return ""; + public void addImage(String id, Bitmap image){ + if (image.getConfig() != Bitmap.Config.ARGB_8888) { + image = image.copy(Bitmap.Config.ARGB_8888, false); + } + float pixelRatio = image.getDensity() / DisplayMetrics.DENSITY_DEFAULT; + ByteBuffer buffer = ByteBuffer.allocate(image.getByteCount()); + image.copyPixelsToBuffer(buffer); + nativeAddImage(id, image.getWidth(), image.getHeight(), pixelRatio, buffer.array()); } - String getName() { - return ""; + public void addImages(@NonNull HashMap bitmapHashMap) { + //noinspection unchecked + new BitmapImageConversionTask(this).execute(bitmapHashMap); } - CameraPosition getDefaultCamera() { - return null; - } + public native String getJson(); - TransitionOptions getTransitionOptions() { - return null; - } + public native String getURL(); - void setTransitionOptions(TransitionOptions options) { - } + public native String getName(); - void setLight(Light light) { - } + public native CameraPosition getDefaultCamera(); - Light getLight() { - return null; - } + public native TransitionOptions getTransitionOptions(); - Bitmap getImage(String image) { - return null; - } + public native void setTransitionOptions(TransitionOptions options); - void addImage(String id, Bitmap bitmap) { + public native void setLight(Light light); - } + public native void getLight(); - void removeImage(String id) { + public native Bitmap getImage(String image); - } + private native void nativeAddImage(String name, int width, int height, float pixelRatio, byte[] array); - //@NonNull - List getSources() { - return null; - } + private native void nativeAddImages(Image[] images); - @Nullable - Source getSource(String id) { - return null; - } + public native void removeImage(String id); - void addSource(Source source) { + private native Layer nativeGetLayer(String layerId); - } + private native void nativeAddLayer(long layerPtr, String before) throws CannotAddLayerException; - void removeSource(Source source) { + private native void nativeAddLayerAbove(long layerPtr, String above) throws CannotAddLayerException; - } + private native void nativeAddLayerAt(long layerPtr, int index) throws CannotAddLayerException; - List getLayers() { - return null; - } + private native Layer nativeRemoveLayerById(String layerId); - @Nullable - Layer getLayer(String layerId) { - return null; - } + private native void nativeRemoveLayer(long layerId); - public void addLayer(@NonNull Layer layer) { - } + private native Layer nativeRemoveLayerAt(int index); - public void addLayerBelow(@NonNull Layer layer, @NonNull String below) { - } + private native Source[] nativeGetSources(); - public void addLayerAbove(@NonNull Layer layer, @NonNull String above) { - } + private native Source nativeGetSource(String sourceId); - public void addLayerAt(@NonNull Layer layer, @IntRange(from = 0) int index) { - } + private native void nativeAddSource(Source source, long sourcePtr) throws CannotAddSourceException; + + private native void nativeRemoveSource(Source source, long sourcePtr); + + private static class BitmapImageConversionTask extends AsyncTask, Void, List> { + + private Style style; + + BitmapImageConversionTask(Style style) { + this.style = style; + } + + @Override + protected List doInBackground(HashMap... params) { + HashMap bitmapHashMap = params[0]; + + List images = new ArrayList<>(); + ByteBuffer buffer; + String name; + Bitmap bitmap; + + for (Map.Entry stringBitmapEntry : bitmapHashMap.entrySet()) { + name = stringBitmapEntry.getKey(); + bitmap = stringBitmapEntry.getValue(); + + if (bitmap.getConfig() != Bitmap.Config.ARGB_8888) { + bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, false); + } + + buffer = ByteBuffer.allocate(bitmap.getByteCount()); + bitmap.copyPixelsToBuffer(buffer); + + float density = bitmap.getDensity() == Bitmap.DENSITY_NONE ? Bitmap.DENSITY_NONE : bitmap.getDensity(); + float pixelRatio = density / DisplayMetrics.DENSITY_DEFAULT; + + images.add(new Image(buffer.array(), pixelRatio, name, bitmap.getWidth(), bitmap.getHeight())); + } - void removeLayer(String id) { + return images; + } + @Override + protected void onPostExecute(List images) { + super.onPostExecute(images); + if (style != null) { + style.nativeAddImages(images.toArray(new Image[images.size()])); + } + } } } -- cgit v1.2.1