summaryrefslogtreecommitdiff
path: root/platform
diff options
context:
space:
mode:
authorIvo van Dongen <info@ivovandongen.nl>2016-11-10 12:35:49 +0100
committerIvo van Dongen <ivovandongen@users.noreply.github.com>2016-11-11 13:46:10 +0100
commit822b83ae4e0308f6f84c1955afbd27f753cfba12 (patch)
treedd47005b705c35369ebf16468420f5ff24e52828 /platform
parent3a4a27c95f1c10c480532c46c4edb7b3120b7a2b (diff)
downloadqtlocation-mapboxgl-822b83ae4e0308f6f84c1955afbd27f753cfba12.tar.gz
[android] fix source ownership
Diffstat (limited to 'platform')
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/CannotAddSourceException.java12
-rwxr-xr-xplatform/android/src/jni.cpp8
-rw-r--r--platform/android/src/style/sources/source.cpp13
-rw-r--r--platform/android/src/style/sources/source.hpp9
4 files changed, 39 insertions, 3 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/CannotAddSourceException.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/CannotAddSourceException.java
new file mode 100644
index 0000000000..9d5a837b5a
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/CannotAddSourceException.java
@@ -0,0 +1,12 @@
+package com.mapbox.mapboxsdk.style.sources;
+
+/**
+ * Thrown when adding a source to a map twice
+ */
+public class CannotAddSourceException extends RuntimeException {
+
+ public CannotAddSourceException(String message) {
+ super(message);
+ }
+
+}
diff --git a/platform/android/src/jni.cpp b/platform/android/src/jni.cpp
index 832fa436c7..1d4849dcfb 100755
--- a/platform/android/src/jni.cpp
+++ b/platform/android/src/jni.cpp
@@ -1142,9 +1142,13 @@ void nativeAddSource(JNIEnv *env, jni::jobject* obj, jni::jlong nativeMapViewPtr
assert(nativeSourcePtr != 0);
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
- Source *source = reinterpret_cast<Source *>(nativeSourcePtr);
- nativeMapView->getMap().addSource(source->releaseCoreSource());
+ Source *source = reinterpret_cast<Source *>(nativeSourcePtr);
+ try {
+ source->addToMap(nativeMapView->getMap());
+ } catch (const std::runtime_error& error) {
+ jni::ThrowNew(*env, jni::FindClass(*env, "com/mapbox/mapboxsdk/style/sources/CannotAddSourceException"), error.what());
+ }
}
void nativeRemoveSource(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jstring* id) {
diff --git a/platform/android/src/style/sources/source.cpp b/platform/android/src/style/sources/source.cpp
index f3daa777d1..24fce79e94 100644
--- a/platform/android/src/style/sources/source.cpp
+++ b/platform/android/src/style/sources/source.cpp
@@ -35,6 +35,19 @@ namespace android {
return jni::Make<jni::String>(env, source.getID());
}
+ void Source::addToMap(mbgl::Map& _map) {
+ //Check to see if we own the source first
+ if (!ownedSource) {
+ throw std::runtime_error("Cannot add source twice");
+ }
+
+ //Add source to map
+ _map.addSource(releaseCoreSource());
+
+ //Save pointer to the map
+ this->map = &_map;
+ }
+
std::unique_ptr<mbgl::style::Source> Source::releaseCoreSource() {
assert(ownedSource != nullptr);
return std::move(ownedSource);
diff --git a/platform/android/src/style/sources/source.hpp b/platform/android/src/style/sources/source.hpp
index 0785e4d4e0..b24dc891b4 100644
--- a/platform/android/src/style/sources/source.hpp
+++ b/platform/android/src/style/sources/source.hpp
@@ -32,16 +32,23 @@ public:
virtual ~Source();
+ void addToMap(mbgl::Map&);
+
virtual jni::jobject* createJavaPeer(jni::JNIEnv&) = 0;
jni::String getId(jni::JNIEnv&);
+protected:
//Release the owned view and return it
std::unique_ptr<mbgl::style::Source> releaseCoreSource();
-protected:
+ //Set on newly created sources until added to the map
std::unique_ptr<mbgl::style::Source> ownedSource;
+
+ //Raw pointer that is valid until the source is removed from the map
mbgl::style::Source& source;
+
+ //Map pointer is valid for newly created sources only after adding to the map
mbgl::Map* map;
};