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

Commit

Permalink
[android] - style image accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Aug 18, 2017
1 parent e204f6c commit 385e4e6
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,10 @@ public void removeImage(String name) {
nativeMapView.removeImage(name);
}

public Bitmap getImage(@NonNull String name) {
return nativeMapView.getImage(name);
}

//
// MinZoom
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,13 @@ public void removeImage(String name) {
nativeRemoveImage(name);
}

public Bitmap getImage(String name) {
if (isDestroyedOn("getImage")) {
return null;
}
return nativeGetImage(name);
}

// Feature querying

@NonNull
Expand Down Expand Up @@ -1078,6 +1085,8 @@ private native void nativeAddImage(String name, int width, int height, float pix

private native void nativeRemoveImage(String name);

private native Bitmap nativeGetImage(String name);

private native void nativeUpdatePolygon(long polygonId, Polygon polygon);

private native void nativeUpdatePolyline(long polylineId, Polyline polyline);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.mapbox.mapboxsdk.testapp.style;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.test.espresso.UiController;
import android.support.test.runner.AndroidJUnit4;

import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.action.MapboxMapAction;
import com.mapbox.mapboxsdk.testapp.activity.BaseActivityTest;
import com.mapbox.mapboxsdk.testapp.activity.style.RuntimeStyleTestActivity;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

/**
* CRUD tests around Image
*/
@RunWith(AndroidJUnit4.class)
public class ImageTest extends BaseActivityTest {

private static final String IMAGE_ID = "test.image";

@Override
protected Class getActivityClass() {
return RuntimeStyleTestActivity.class;
}

@Test
public void testAddGetImage() {
validateTestSetup();
MapboxMapAction.invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
Drawable drawable = rule.getActivity().getResources().getDrawable(R.drawable.ic_launcher_round);
assertTrue(drawable instanceof BitmapDrawable);

Bitmap bitmapSet = ((BitmapDrawable) drawable).getBitmap();
mapboxMap.addImage(IMAGE_ID, bitmapSet);

Bitmap bitmapGet = mapboxMap.getImage(IMAGE_ID);
assertTrue(bitmapGet.sameAs(bitmapSet));

mapboxMap.removeImage(IMAGE_ID);
assertNull(mapboxMap.getImage(IMAGE_ID));
}
});
}
}
10 changes: 10 additions & 0 deletions platform/android/src/native_map_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "geometry/lat_lng_bounds.hpp"
#include "map/camera_position.hpp"
#include "style/light.hpp"
#include "bitmap_factory.hpp"

namespace mbgl {
namespace android {
Expand Down Expand Up @@ -1043,6 +1044,14 @@ void NativeMapView::removeImage(JNIEnv& env, jni::String name) {
map->getStyle().removeImage(jni::Make<std::string>(env, name));
}

jni::Object<Bitmap> NativeMapView::getImage(JNIEnv& env, jni::String name) {
const mbgl::style::Image *image = map->getStyle().getImage(jni::Make<std::string>(env, name));
if (image) {
return Bitmap::CreateBitmap(env, image->getImage());
} else {
return jni::Object<Bitmap>();
}
}

void NativeMapView::setPrefetchesTiles(JNIEnv&, jni::jboolean enable) {
map->setPrefetchZoomDelta(enable ? util::DEFAULT_PREFETCH_ZOOM_DELTA : uint8_t(0));
Expand Down Expand Up @@ -1553,6 +1562,7 @@ void NativeMapView::registerNative(jni::JNIEnv& env) {
METHOD(&NativeMapView::removeSource, "nativeRemoveSource"),
METHOD(&NativeMapView::addImage, "nativeAddImage"),
METHOD(&NativeMapView::removeImage, "nativeRemoveImage"),
METHOD(&NativeMapView::getImage, "nativeGetImage"),
METHOD(&NativeMapView::setLatLngBounds, "nativeSetLatLngBounds"),
METHOD(&NativeMapView::setPrefetchesTiles, "nativeSetPrefetchesTiles"),
METHOD(&NativeMapView::getPrefetchesTiles, "nativeGetPrefetchesTiles")
Expand Down
3 changes: 3 additions & 0 deletions platform/android/src/native_map_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "geometry/lat_lng_bounds.hpp"
#include "map/camera_position.hpp"
#include "style/light.hpp"
#include "bitmap.hpp"

#include <exception>
#include <string>
Expand Down Expand Up @@ -253,6 +254,8 @@ class NativeMapView : public RendererBackend, public MapObserver {

void removeImage(JNIEnv&, jni::String);

jni::Object<Bitmap> getImage(JNIEnv&, jni::String);

void setPrefetchesTiles(JNIEnv&, jni::jboolean);

jni::jboolean getPrefetchesTiles(JNIEnv&);
Expand Down

0 comments on commit 385e4e6

Please sign in to comment.