[android] return layer ownership on remove

This commit is contained in:
Ivo van Dongen
2016-11-14 10:44:06 +01:00
committed by Ivo van Dongen
parent f66ea0bef9
commit 8d53f9f091
6 changed files with 77 additions and 5 deletions

View File

@@ -155,6 +155,17 @@ public class MapboxMap {
getMapView().getNativeMapView().removeLayer(layerId);
}
/**
* Removes the layer. The reference is re-usable after this and can be re-added
*
* @param layer the layer to remove
* @throws NoSuchLayerException
*/
@UiThread
public void removeLayer(@NonNull Layer layer) throws NoSuchLayerException {
getMapView().getNativeMapView().removeLayer(layer);
}
@Nullable
@UiThread
public Source getSource(@NonNull String sourceId) {

View File

@@ -491,7 +491,11 @@ final class NativeMapView {
}
public void removeLayer(@NonNull String layerId) throws NoSuchLayerException {
nativeRemoveLayer(nativeMapViewPtr, layerId);
nativeRemoveLayerById(nativeMapViewPtr, layerId);
}
public void removeLayer(@NonNull Layer layer) throws NoSuchLayerException {
nativeRemoveLayer(nativeMapViewPtr, layer.getNativePtr());
}
public Source getSource(@NonNull String sourceId) {
@@ -743,7 +747,9 @@ final class NativeMapView {
private native void nativeAddLayer(long nativeMapViewPtr, long layerPtr, String before);
private native void nativeRemoveLayer(long nativeMapViewPtr, String layerId) throws NoSuchLayerException;
private native void nativeRemoveLayerById(long nativeMapViewPtr, String layerId) throws NoSuchLayerException;
private native void nativeRemoveLayer(long nativeMapViewPtr, long layerId) throws NoSuchLayerException;
private native Source nativeGetSource(long nativeMapViewPtr, String sourceId);

View File

@@ -1,5 +1,6 @@
package com.mapbox.mapboxsdk.testapp.style;
import android.graphics.Color;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
@@ -19,6 +20,8 @@ import com.mapbox.mapboxsdk.testapp.activity.style.RuntimeStyleTestActivity;
import com.mapbox.mapboxsdk.testapp.utils.OnMapReadyIdlingResource;
import com.mapbox.mapboxsdk.testapp.utils.ViewUtils;
import junit.framework.Assert;
import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.Before;
@@ -30,6 +33,7 @@ import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static junit.framework.Assert.fail;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
@@ -94,7 +98,7 @@ public class RuntimeStyleTests {
try {
mapboxMap.removeLayer("building");
} catch (NoSuchLayerException e) {
assertFalse(true);
fail("Definitively exists: " + e.getMessage());
}
assertNull(mapboxMap.getLayer("building"));
@@ -106,6 +110,22 @@ public class RuntimeStyleTests {
//Assure the reference still works
layer.setProperties(PropertyFactory.visibility(Property.VISIBLE));
//Remove, preserving the reference
try {
mapboxMap.removeLayer(layer);
} catch (NoSuchLayerException e) {
fail("Definitively exists: " + e.getMessage());
}
//Property setters should still work
layer.setProperties(PropertyFactory.fillColor(Color.RED));
//Re-add the reference...
mapboxMap.addLayer(layer);
//Ensure it's there
Assert.assertNotNull(mapboxMap.getLayer(layer.getId()));
}
}

View File

@@ -1109,7 +1109,10 @@ void nativeAddLayer(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jlon
}
}
void nativeRemoveLayer(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jstring* id) {
/**
* Remove by layer id. Ownership is not transferred back
*/
void nativeRemoveLayerById(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jstring* id) {
assert(nativeMapViewPtr != 0);
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
try {
@@ -1119,6 +1122,22 @@ void nativeRemoveLayer(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, j
}
}
/**
* Remove with wrapper object id. Ownership is transferred back to the wrapper
*/
void nativeRemoveLayer(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jlong layerPtr) {
assert(nativeMapViewPtr != 0);
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
mbgl::android::Layer *layer = reinterpret_cast<mbgl::android::Layer *>(layerPtr);
try {
std::unique_ptr<mbgl::style::Layer> coreLayer = nativeMapView->getMap().removeLayer(layer->get().getID());
layer->setLayer(std::move(coreLayer));
} catch (const std::runtime_error& error) {
jni::ThrowNew(*env, jni::FindClass(*env, "com/mapbox/mapboxsdk/style/layers/NoSuchLayerException"), error.what());
}
}
jni::jobject* nativeGetSource(JNIEnv *env, jni::jobject* obj, jni::jlong nativeMapViewPtr, jni::jstring* sourceId) {
assert(env);
assert(nativeMapViewPtr != 0);
@@ -1838,7 +1857,8 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
MAKE_NATIVE_METHOD(nativeFlyTo, "(JDDDJDD)V"),
MAKE_NATIVE_METHOD(nativeGetLayer, "(JLjava/lang/String;)Lcom/mapbox/mapboxsdk/style/layers/Layer;"),
MAKE_NATIVE_METHOD(nativeAddLayer, "(JJLjava/lang/String;)V"),
MAKE_NATIVE_METHOD(nativeRemoveLayer, "(JLjava/lang/String;)V"),
MAKE_NATIVE_METHOD(nativeRemoveLayerById, "(JLjava/lang/String;)V"),
MAKE_NATIVE_METHOD(nativeRemoveLayer, "(JJ)V"),
MAKE_NATIVE_METHOD(nativeGetSource, "(JLjava/lang/String;)Lcom/mapbox/mapboxsdk/style/sources/Source;"),
MAKE_NATIVE_METHOD(nativeAddSource, "(JJ)V"),
MAKE_NATIVE_METHOD(nativeRemoveSource, "(JLjava/lang/String;)V"),

View File

@@ -45,6 +45,10 @@ namespace android {
this->map = &_map;
}
void Layer::setLayer(std::unique_ptr<mbgl::style::Layer> sourceLayer) {
this->ownedLayer = std::move(sourceLayer);
}
std::unique_ptr<mbgl::style::Layer> Layer::releaseCoreLayer() {
assert(ownedLayer != nullptr);
return std::move(ownedLayer);
@@ -54,6 +58,10 @@ namespace android {
return jni::Make<jni::String>(env, layer.getID());
}
style::Layer& Layer::get() {
return layer;
}
void Layer::setLayoutProperty(jni::JNIEnv& env, jni::String jname, jni::Object<> jvalue) {
Value value(env, jvalue);

View File

@@ -34,10 +34,17 @@ public:
virtual jni::jobject* createJavaPeer(jni::JNIEnv&) = 0;
/**
* Set core layer (ie return ownership after remove)
*/
void setLayer(std::unique_ptr<mbgl::style::Layer>);
void addToMap(mbgl::Map&, mbgl::optional<std::string>);
jni::String getId(jni::JNIEnv&);
style::Layer& get();
void setLayoutProperty(jni::JNIEnv&, jni::String, jni::Object<> value);
void setPaintProperty(jni::JNIEnv&, jni::String, jni::Object<> value);