[android] Sources: peer model, mutability (#6054)

This commit is contained in:
Ivo van Dongen
2016-09-21 11:04:32 +02:00
committed by GitHub
parent 3b546b9646
commit eb97dbe383
24 changed files with 886 additions and 87 deletions

View File

@@ -22,6 +22,7 @@
#include <mbgl/map/camera.hpp>
#include <mbgl/annotation/annotation.hpp>
#include <mbgl/style/layer.hpp>
#include <mbgl/style/source.hpp>
#include <mbgl/sprite/sprite_image.hpp>
#include <mbgl/platform/event.hpp>
#include <mbgl/platform/log.hpp>
@@ -1185,23 +1186,35 @@ void nativeRemoveLayer(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, j
}
}
void nativeAddSource(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jstring* id, jni::jobject* jsource) {
jni::jobject* nativeGetSource(JNIEnv *env, jni::jobject* obj, jni::jlong nativeMapViewPtr, jni::jstring* sourceId) {
mbgl::Log::Debug(mbgl::Event::JNI, "nativeGetSource");
assert(env);
assert(nativeMapViewPtr != 0);
//Get the native map peer
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
//Find the source
mbgl::style::Source* coreSource = nativeMapView->getMap().getSource(std_string_from_jstring(env, sourceId));
if (!coreSource) {
mbgl::Log::Debug(mbgl::Event::JNI, "No source found");
return jni::Object<Source>();
}
//Create and return the source's native peer
return createJavaSourcePeer(*env, nativeMapView->getMap(), *coreSource);
}
void nativeAddSource(JNIEnv *env, jni::jobject* obj, jni::jlong nativeMapViewPtr, jni::jlong nativeSourcePtr) {
mbgl::Log::Debug(mbgl::Event::JNI, "nativeAddSource");
assert(nativeMapViewPtr != 0);
assert(id != nullptr);
assert(jsource != nullptr);
assert(nativeSourcePtr != 0);
//Convert
mbgl::optional<std::unique_ptr<mbgl::style::Source>> source = convertToNativeSource(
*env,
jni::Object<jni::jobject>(jsource), jni::String(id)
);
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
Source *source = reinterpret_cast<Source *>(nativeSourcePtr);
//Add to map view
if (source) {
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
nativeMapView->getMap().addSource(std::move(*source));
}
nativeMapView->getMap().addSource(source->releaseCoreSource());
}
void nativeRemoveSource(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jstring* id) {
@@ -1687,6 +1700,7 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
java::registerNatives(env);
registerNativeLayers(env);
registerNativeSources(env);
latLngClass = &jni::FindClass(env, "com/mapbox/mapboxsdk/geometry/LatLng");
latLngClass = jni::NewGlobalRef(env, latLngClass).release();
@@ -1838,7 +1852,8 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
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(nativeAddSource, "(JLjava/lang/String;Lcom/mapbox/mapboxsdk/style/sources/Source;)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(nativeSetContentPadding, "(JDDDD)V"),
MAKE_NATIVE_METHOD(nativeScheduleTakeSnapshot, "(J)V"),

View File

@@ -1,6 +1,6 @@
#pragma once
#include "value.hpp"
#include "../value.hpp"
#include <mapbox/geojson.hpp>
#include <mbgl/style/conversion.hpp>
@@ -43,6 +43,15 @@ Result<GeoJSON> convertGeoJSON(const mbgl::android::Value& value) {
return geoJSON;
}
template <>
struct Converter<GeoJSON> {
Result<GeoJSON> operator()(const mbgl::android::Value& value) const {
return convertGeoJSON(value);
}
};
} // namespace conversion
} // namespace style
} // namespace mbgl

View File

@@ -0,0 +1,38 @@
#pragma once
#include <mbgl/util/optional.hpp>
#include <mbgl/util/variant.hpp>
#include <mbgl/util/tileset.hpp>
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/tileset.hpp>
#include <jni/jni.hpp>
#include <string>
namespace mbgl {
namespace style {
namespace conversion {
template <>
struct Converter<variant<std::string, Tileset>> {
template <class V>
Result<variant<std::string, Tileset>> operator()(const V& value) const {
if (isObject(value)) {
Result<Tileset> tileset = convert<Tileset>(value);
if (!tileset) {
return tileset.error();
}
return *tileset;
} else {
return *toString(value);
}
}
};
}
}
}

View File

@@ -0,0 +1,77 @@
#include "geojson_source.hpp"
#include "../android_conversion.hpp"
#include "../conversion/geojson.hpp"
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/geojson_options.hpp>
#include <string>
namespace mbgl {
namespace android {
GeoJSONSource::GeoJSONSource(jni::JNIEnv& env, jni::String sourceId, jni::Object<> options)
: Source(env, std::make_unique<mbgl::style::GeoJSONSource>(
jni::Make<std::string>(env, sourceId),
options ? *style::conversion::convert<style::GeoJSONOptions>(Value(env, options)) : style::GeoJSONOptions()
)
) {
}
GeoJSONSource::GeoJSONSource(mbgl::Map& map, mbgl::style::GeoJSONSource& coreSource)
: Source(map, coreSource) {
}
GeoJSONSource::~GeoJSONSource() = default;
void GeoJSONSource::setGeoJSON(jni::JNIEnv& env, jni::Object<> json) {
using namespace mbgl::style::conversion;
//Convert the jni object
Result<GeoJSON> converted = convert<GeoJSON>(Value(env, json));
if(!converted) {
mbgl::Log::Error(mbgl::Event::JNI, "Error setting geo json: " + converted.error().message);
return;
}
//Update the core source
source.as<mbgl::style::GeoJSONSource>()->GeoJSONSource::setGeoJSON(*converted);
//Repaint
updateStyle(false);
}
void GeoJSONSource::setURL(jni::JNIEnv& env, jni::String url) {
//Update the core source
source.as<mbgl::style::GeoJSONSource>()->GeoJSONSource::setURL(jni::Make<std::string>(env, url));
//Repaint
updateStyle(false);
}
jni::Class<GeoJSONSource> GeoJSONSource::javaClass;
jni::jobject* GeoJSONSource::createJavaPeer(jni::JNIEnv& env) {
static auto constructor = GeoJSONSource::javaClass.template GetConstructor<jni::jlong>(env);
return GeoJSONSource::javaClass.New(env, constructor, reinterpret_cast<jni::jlong>(this));
}
void GeoJSONSource::registerNative(jni::JNIEnv& env) {
//Lookup the class
GeoJSONSource::javaClass = *jni::Class<GeoJSONSource>::Find(env).NewGlobalRef(env).release();
#define METHOD(MethodPtr, name) jni::MakeNativePeerMethod<decltype(MethodPtr), (MethodPtr)>(name)
//Register the peer
jni::RegisterNativePeer<GeoJSONSource>(
env, GeoJSONSource::javaClass, "nativePtr",
std::make_unique<GeoJSONSource, JNIEnv&, jni::String, jni::Object<>>,
"initialize",
"finalize",
METHOD(&GeoJSONSource::setGeoJSON, "nativeSetGeoJson"),
METHOD(&GeoJSONSource::setURL, "nativeSetUrl")
);
}
} // namespace android
} // namespace mbgl

View File

@@ -0,0 +1,34 @@
#pragma once
#include "source.hpp"
#include <mbgl/style/sources/geojson_source.hpp>
#include <jni/jni.hpp>
namespace mbgl {
namespace android {
class GeoJSONSource : public Source {
public:
static constexpr auto Name() { return "com/mapbox/mapboxsdk/style/sources/GeoJsonSource"; };
static jni::Class<GeoJSONSource> javaClass;
static void registerNative(jni::JNIEnv&);
GeoJSONSource(jni::JNIEnv&, jni::String, jni::Object<>);
GeoJSONSource(mbgl::Map&, mbgl::style::GeoJSONSource&);
~GeoJSONSource();
void setGeoJSON(jni::JNIEnv&, jni::Object<>);
void setURL(jni::JNIEnv&, jni::String);
jni::jobject* createJavaPeer(jni::JNIEnv&);
}; // class GeoJSONSource
} // namespace android
} // namespace mbgl

View File

@@ -0,0 +1,54 @@
#include "raster_source.hpp"
#include "../android_conversion.hpp"
#include "../value.hpp"
#include "../conversion/url_or_tileset.hpp"
#include <mbgl/util/variant.hpp>
#include <string>
namespace mbgl {
namespace android {
RasterSource::RasterSource(jni::JNIEnv& env, jni::String sourceId, jni::Object<> urlOrTileSet, jni::jint tileSize)
: Source(
env,
std::make_unique<mbgl::style::RasterSource>(
jni::Make<std::string>(env, sourceId),
*style::conversion::convert<variant<std::string, Tileset>>(Value(env, urlOrTileSet)),
tileSize
)
) {
}
RasterSource::RasterSource(mbgl::Map& map, mbgl::style::RasterSource& coreSource)
: Source(map, coreSource) {
}
RasterSource::~RasterSource() = default;
jni::Class<RasterSource> RasterSource::javaClass;
jni::jobject* RasterSource::createJavaPeer(jni::JNIEnv& env) {
static auto constructor = RasterSource::javaClass.template GetConstructor<jni::jlong>(env);
return RasterSource::javaClass.New(env, constructor, reinterpret_cast<jni::jlong>(this));
}
void RasterSource::registerNative(jni::JNIEnv& env) {
//Lookup the class
RasterSource::javaClass = *jni::Class<RasterSource>::Find(env).NewGlobalRef(env).release();
#define METHOD(MethodPtr, name) jni::MakeNativePeerMethod<decltype(MethodPtr), (MethodPtr)>(name)
//Register the peer
jni::RegisterNativePeer<RasterSource>(
env, RasterSource::javaClass, "nativePtr",
std::make_unique<RasterSource, JNIEnv&, jni::String, jni::Object<>, jni::jint>,
"initialize",
"finalize"
);
}
} // namespace android
} // namespace mbgl

View File

@@ -0,0 +1,30 @@
#pragma once
#include "source.hpp"
#include <mbgl/style/sources/raster_source.hpp>
#include <jni/jni.hpp>
namespace mbgl {
namespace android {
class RasterSource : public Source {
public:
static constexpr auto Name() { return "com/mapbox/mapboxsdk/style/sources/RasterSource"; };
static jni::Class<RasterSource> javaClass;
static void registerNative(jni::JNIEnv&);
RasterSource(jni::JNIEnv&, jni::String, jni::Object<>, jni::jint);
RasterSource(mbgl::Map&, mbgl::style::RasterSource&);
~RasterSource();
jni::jobject* createJavaPeer(jni::JNIEnv&);
}; // class RasterSource
} // namespace android
} // namespace mbgl

View File

@@ -0,0 +1,72 @@
#include "source.hpp"
#include "../android_conversion.hpp"
#include <jni/jni.hpp>
#include <mbgl/platform/log.hpp>
//Java -> C++ conversion
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/source.hpp>
//C++ -> Java conversion
#include "../conversion/property_value.hpp"
#include <string>
namespace mbgl {
namespace android {
/**
* Invoked when the construction is initiated from the jvm through a subclass
*/
Source::Source(jni::JNIEnv&, std::unique_ptr<mbgl::style::Source> coreSource)
: ownedSource(std::move(coreSource))
, source(*ownedSource) {
}
Source::Source(mbgl::Map& coreMap, mbgl::style::Source& coreSource) : source(coreSource) , map(&coreMap) {
}
Source::~Source() {
}
jni::String Source::getId(jni::JNIEnv& env) {
return jni::Make<jni::String>(env, source.getID());
}
void Source::updateStyle(jni::jboolean updateClasses) {
//Update the style only if attached
if (ownedSource == nullptr) {
Update flags = mbgl::Update::RecalculateStyle;
if(updateClasses) {
flags = flags | mbgl::Update::Classes;
}
map->update(flags);
} else {
mbgl::Log::Debug(mbgl::Event::JNI, "Not updating as source is not attached to map (yet)");
}
}
std::unique_ptr<mbgl::style::Source> Source::releaseCoreSource() {
assert(ownedSource != nullptr);
return std::move(ownedSource);
}
jni::Class<Source> Source::javaClass;
void Source::registerNative(jni::JNIEnv& env) {
//Lookup the class
Source::javaClass = *jni::Class<Source>::Find(env).NewGlobalRef(env).release();
#define METHOD(MethodPtr, name) jni::MakeNativePeerMethod<decltype(MethodPtr), (MethodPtr)>(name)
//Register the peer
jni::RegisterNativePeer<Source>(env, Source::javaClass, "nativePtr",
METHOD(&Source::getId, "nativeGetId")
);
}
} //android
} //mbgl

View File

@@ -0,0 +1,56 @@
#pragma once
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/map/map.hpp>
#include <mbgl/style/source.hpp>
#include "../value.hpp"
#include <jni/jni.hpp>
namespace mbgl {
namespace android {
class Source : private mbgl::util::noncopyable {
public:
static constexpr auto Name() { return "com/mapbox/mapboxsdk/style/sources/Source"; };
static jni::Class<Source> javaClass;
static void registerNative(jni::JNIEnv&);
/*
* Called when a Java object is created on the c++ side
*/
Source(mbgl::Map&, mbgl::style::Source&);
/*
* Called when a Java object was created from the jvm side
*/
Source(jni::JNIEnv&, std::unique_ptr<mbgl::style::Source>);
virtual ~Source();
virtual jni::jobject* createJavaPeer(jni::JNIEnv&) = 0;
jni::String getId(jni::JNIEnv&);
//Release the owned view and return it
std::unique_ptr<mbgl::style::Source> releaseCoreSource();
protected:
void updateStyle(jni::jboolean);
std::unique_ptr<mbgl::style::Source> ownedSource;
mbgl::style::Source& source;
mbgl::Map* map;
};
} //android
} //mbgl

View File

@@ -1,28 +1,46 @@
#include "sources.hpp"
#include "../value.hpp"
#include "../android_conversion.hpp"
#include "../android_geojson.hpp"
#include <mbgl/style/source.hpp>
#include <mbgl/style/sources/geojson_source.hpp>
#include <mbgl/style/sources/raster_source.hpp>
#include <mbgl/style/sources/vector_source.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/source.hpp>
#include <string>
#include "source.hpp"
#include "geojson_source.hpp"
#include "raster_source.hpp"
#include "vector_source.hpp"
namespace mbgl {
namespace android {
mbgl::optional<std::unique_ptr<mbgl::style::Source>> convertToNativeSource(jni::JNIEnv& env, jni::Object<jni::jobject> jvalue, jni::String id) {
using namespace mbgl::style;
Value value(env, jvalue);
conversion::Result<std::unique_ptr<Source>> source = conversion::convert<std::unique_ptr<Source>>(value, jni::Make<std::string>(env, id));
if (!source) {
mbgl::Log::Error(mbgl::Event::JNI, "Unable to add source: " + source.error().message);
return {};
}
return std::move(*source);
Source* initializeSourcePeer(mbgl::Map& map, mbgl::style::Source& coreSource) {
Source* source;
if (coreSource.is<mbgl::style::VectorSource>()) {
source = new VectorSource(map, *coreSource.as<mbgl::style::VectorSource>());
} else if (coreSource.is<mbgl::style::RasterSource>()) {
source = new RasterSource(map, *coreSource.as<mbgl::style::RasterSource>());
} else if (coreSource.is<mbgl::style::GeoJSONSource>()) {
source = new GeoJSONSource(map, *coreSource.as<mbgl::style::GeoJSONSource>());
} else {
throw new std::runtime_error("Source type not implemented");
}
return source;
}
jni::jobject* createJavaSourcePeer(jni::JNIEnv& env, mbgl::Map& map, mbgl::style::Source& coreSource) {
std::unique_ptr<Source> peerSource = std::unique_ptr<Source>(initializeSourcePeer(map, coreSource));
jni::jobject* result = peerSource->createJavaPeer(env);
peerSource.release();
return result;
}
void registerNativeSources(jni::JNIEnv& env) {
Source::registerNative(env);
VectorSource::registerNative(env);
RasterSource::registerNative(env);
GeoJSONSource::registerNative(env);
}
}
}

View File

@@ -1,14 +1,20 @@
#pragma once
#include <mbgl/map/map.hpp>
#include <mbgl/style/source.hpp>
#include <mbgl/util/optional.hpp>
#include "source.hpp"
#include <jni/jni.hpp>
namespace mbgl {
namespace android {
mbgl::optional<std::unique_ptr<mbgl::style::Source>> convertToNativeSource(jni::JNIEnv& env, jni::Object<jni::jobject> jsource, jni::String id);
mbgl::android::Source* initializeSourcePeer(mbgl::Map&, mbgl::style::Source&);
jni::jobject* createJavaSourcePeer(jni::JNIEnv&, mbgl::Map&, mbgl::style::Source&);
void registerNativeSources(jni::JNIEnv&);
}
}

View File

@@ -0,0 +1,53 @@
#include "vector_source.hpp"
#include "../android_conversion.hpp"
#include "../value.hpp"
#include "../conversion/url_or_tileset.hpp"
#include <mbgl/util/variant.hpp>
#include <string>
namespace mbgl {
namespace android {
VectorSource::VectorSource(jni::JNIEnv& env, jni::String sourceId, jni::Object<> urlOrTileSet)
: Source(
env,
std::make_unique<mbgl::style::VectorSource>(
jni::Make<std::string>(env, sourceId),
*style::conversion::convert<variant<std::string, Tileset>>(Value(env, urlOrTileSet))
)
) {
}
VectorSource::VectorSource(mbgl::Map& map, mbgl::style::VectorSource& coreSource)
: Source(map, coreSource) {
}
VectorSource::~VectorSource() = default;
jni::Class<VectorSource> VectorSource::javaClass;
jni::jobject* VectorSource::createJavaPeer(jni::JNIEnv& env) {
static auto constructor = VectorSource::javaClass.template GetConstructor<jni::jlong>(env);
return VectorSource::javaClass.New(env, constructor, reinterpret_cast<jni::jlong>(this));
}
void VectorSource::registerNative(jni::JNIEnv& env) {
//Lookup the class
VectorSource::javaClass = *jni::Class<VectorSource>::Find(env).NewGlobalRef(env).release();
#define METHOD(MethodPtr, name) jni::MakeNativePeerMethod<decltype(MethodPtr), (MethodPtr)>(name)
//Register the peer
jni::RegisterNativePeer<VectorSource>(
env, VectorSource::javaClass, "nativePtr",
std::make_unique<VectorSource, JNIEnv&, jni::String, jni::Object<>>,
"initialize",
"finalize"
);
}
} // namespace android
} // namespace mbgl

View File

@@ -0,0 +1,30 @@
#pragma once
#include "source.hpp"
#include <mbgl/style/sources/vector_source.hpp>
#include <jni/jni.hpp>
namespace mbgl {
namespace android {
class VectorSource : public Source {
public:
static constexpr auto Name() { return "com/mapbox/mapboxsdk/style/sources/VectorSource"; };
static jni::Class<VectorSource> javaClass;
static void registerNative(jni::JNIEnv&);
VectorSource(jni::JNIEnv&, jni::String, jni::Object<>);
VectorSource(mbgl::Map&, mbgl::style::VectorSource&);
~VectorSource();
jni::jobject* createJavaPeer(jni::JNIEnv&);
}; // class VectorSource
} // namespace android
} // namespace mbgl