[android] return source ownership on remove
This commit is contained in:
committed by
Ivo van Dongen
parent
8d53f9f091
commit
36dea5ea0d
@@ -212,6 +212,17 @@ public class MapboxMap {
|
||||
getMapView().getNativeMapView().removeSource(sourceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the source, preserving the reverence for re-use
|
||||
*
|
||||
* @param source the source to remove
|
||||
* @throws NoSuchSourceException
|
||||
*/
|
||||
@UiThread
|
||||
public void removeSource(@NonNull Source source) throws NoSuchSourceException {
|
||||
getMapView().getNativeMapView().removeSource(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an image to be used int hte map's style
|
||||
*
|
||||
|
||||
@@ -507,7 +507,11 @@ final class NativeMapView {
|
||||
}
|
||||
|
||||
public void removeSource(@NonNull String sourceId) throws NoSuchSourceException {
|
||||
nativeRemoveSource(nativeMapViewPtr, sourceId);
|
||||
nativeRemoveSourceById(nativeMapViewPtr, sourceId);
|
||||
}
|
||||
|
||||
public void removeSource(@NonNull Source source) throws NoSuchSourceException {
|
||||
nativeRemoveSource(nativeMapViewPtr, source.getNativePtr());
|
||||
}
|
||||
|
||||
public void addImage(@NonNull String name, @NonNull Bitmap image) {
|
||||
@@ -755,7 +759,9 @@ final class NativeMapView {
|
||||
|
||||
private native void nativeAddSource(long nativeMapViewPtr, long nativeSourcePtr);
|
||||
|
||||
private native void nativeRemoveSource(long nativeMapViewPtr, String sourceId) throws NoSuchSourceException;
|
||||
private native void nativeRemoveSourceById(long nativeMapViewPtr, String sourceId) throws NoSuchSourceException;
|
||||
|
||||
private native void nativeRemoveSource(long nativeMapViewPtr, long sourcePtr) throws NoSuchSourceException;
|
||||
|
||||
private native void nativeAddImage(long nativeMapViewPtr, String name, int width, int height, float pixelRatio, byte[] array);
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import com.mapbox.mapboxsdk.style.layers.NoSuchLayerException;
|
||||
import com.mapbox.mapboxsdk.style.layers.Property;
|
||||
import com.mapbox.mapboxsdk.style.layers.PropertyFactory;
|
||||
import com.mapbox.mapboxsdk.style.sources.NoSuchSourceException;
|
||||
import com.mapbox.mapboxsdk.style.sources.Source;
|
||||
import com.mapbox.mapboxsdk.style.sources.VectorSource;
|
||||
import com.mapbox.mapboxsdk.testapp.R;
|
||||
import com.mapbox.mapboxsdk.testapp.activity.style.RuntimeStyleTestActivity;
|
||||
@@ -73,6 +74,8 @@ public class RuntimeStyleTests {
|
||||
} catch (NoSuchSourceException e) {
|
||||
// it's ok..
|
||||
}
|
||||
|
||||
onView(withId(R.id.mapView)).perform(new AddRemoveSourceAction());
|
||||
}
|
||||
|
||||
private class AddRemoveLayerAction implements ViewAction {
|
||||
@@ -129,6 +132,52 @@ public class RuntimeStyleTests {
|
||||
}
|
||||
}
|
||||
|
||||
private class AddRemoveSourceAction implements ViewAction {
|
||||
|
||||
@Override
|
||||
public Matcher<View> getConstraints() {
|
||||
return isDisplayed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return getClass().getSimpleName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void perform(UiController uiController, View view) {
|
||||
MapboxMap mapboxMap = rule.getActivity().getMapboxMap();
|
||||
|
||||
//Add initial source
|
||||
mapboxMap.addSource(new VectorSource("my-source", "mapbox://mapbox.mapbox-terrain-v2"));
|
||||
|
||||
//Remove
|
||||
try {
|
||||
mapboxMap.removeSource("my-source");
|
||||
} catch (NoSuchSourceException e) {
|
||||
fail("Definitively exists: " + e.getMessage());
|
||||
}
|
||||
assertNull(mapboxMap.getLayer("my-source"));
|
||||
|
||||
//Add
|
||||
Source source = new VectorSource("my-source", "mapbox://mapbox.mapbox-terrain-v2");
|
||||
mapboxMap.addSource(source);
|
||||
|
||||
//Remove, preserving the reference
|
||||
try {
|
||||
mapboxMap.removeSource(source);
|
||||
} catch (NoSuchSourceException e) {
|
||||
fail("Definitively exists: " + e.getMessage());
|
||||
}
|
||||
|
||||
//Re-add the reference...
|
||||
mapboxMap.addSource(source);
|
||||
|
||||
//Ensure it's there
|
||||
Assert.assertNotNull(mapboxMap.getSource(source.getId()));
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void unregisterIntentServiceIdlingResource() {
|
||||
Espresso.unregisterIdlingResources(idlingResource);
|
||||
|
||||
@@ -1170,11 +1170,23 @@ void nativeAddSource(JNIEnv *env, jni::jobject* obj, jni::jlong nativeMapViewPtr
|
||||
}
|
||||
}
|
||||
|
||||
void nativeRemoveSource(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jstring* id) {
|
||||
void nativeRemoveSourceById(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jstring* id) {
|
||||
assert(nativeMapViewPtr != 0);
|
||||
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
|
||||
try {
|
||||
nativeMapView->getMap().removeSource(std_string_from_jstring(env, id));
|
||||
} catch (const std::runtime_error& error) {
|
||||
jni::ThrowNew(*env, jni::FindClass(*env, "com/mapbox/mapboxsdk/style/sources/NoSuchSourceException"), error.what());
|
||||
}
|
||||
}
|
||||
|
||||
void nativeRemoveSource(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jlong sourcePtr) {
|
||||
assert(nativeMapViewPtr != 0);
|
||||
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
|
||||
mbgl::android::Source *source = reinterpret_cast<mbgl::android::Source *>(sourcePtr);
|
||||
try {
|
||||
nativeMapView->getMap().removeSource(std_string_from_jstring(env, id));
|
||||
std::unique_ptr<mbgl::style::Source> coreSource = nativeMapView->getMap().removeSource(source->get().getID());
|
||||
source->setSource(std::move(coreSource));
|
||||
} catch (const std::runtime_error& error) {
|
||||
jni::ThrowNew(*env, jni::FindClass(*env, "com/mapbox/mapboxsdk/style/sources/NoSuchSourceException"), error.what());
|
||||
}
|
||||
@@ -1861,7 +1873,8 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
|
||||
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"),
|
||||
MAKE_NATIVE_METHOD(nativeRemoveSourceById, "(JLjava/lang/String;)V"),
|
||||
MAKE_NATIVE_METHOD(nativeRemoveSource, "(JJ)V"),
|
||||
MAKE_NATIVE_METHOD(nativeAddImage, "(JLjava/lang/String;IIF[B)V"),
|
||||
MAKE_NATIVE_METHOD(nativeRemoveImage, "(JLjava/lang/String;)V"),
|
||||
MAKE_NATIVE_METHOD(nativeSetContentPadding, "(JDDDD)V"),
|
||||
|
||||
@@ -31,6 +31,14 @@ namespace android {
|
||||
Source::~Source() {
|
||||
}
|
||||
|
||||
style::Source& Source::get() {
|
||||
return source;
|
||||
}
|
||||
|
||||
void Source::setSource(std::unique_ptr<style::Source> coreSource) {
|
||||
this->ownedSource = std::move(coreSource);
|
||||
}
|
||||
|
||||
jni::String Source::getId(jni::JNIEnv& env) {
|
||||
return jni::Make<jni::String>(env, source.getID());
|
||||
}
|
||||
|
||||
@@ -32,6 +32,13 @@ public:
|
||||
|
||||
virtual ~Source();
|
||||
|
||||
/**
|
||||
* Set core source (ie return ownership after remove)
|
||||
*/
|
||||
void setSource(std::unique_ptr<style::Source>);
|
||||
|
||||
style::Source& get();
|
||||
|
||||
void addToMap(mbgl::Map&);
|
||||
|
||||
virtual jni::jobject* createJavaPeer(jni::JNIEnv&) = 0;
|
||||
|
||||
Reference in New Issue
Block a user