summaryrefslogtreecommitdiff
path: root/platform/android/src
diff options
context:
space:
mode:
Diffstat (limited to 'platform/android/src')
-rw-r--r--platform/android/src/style/sources/custom_vector_source.cpp115
-rw-r--r--platform/android/src/style/sources/custom_vector_source.hpp46
-rw-r--r--platform/android/src/style/sources/sources.cpp2
3 files changed, 163 insertions, 0 deletions
diff --git a/platform/android/src/style/sources/custom_vector_source.cpp b/platform/android/src/style/sources/custom_vector_source.cpp
new file mode 100644
index 0000000000..abc653c651
--- /dev/null
+++ b/platform/android/src/style/sources/custom_vector_source.cpp
@@ -0,0 +1,115 @@
+#include "custom_vector_source.hpp"
+
+#include <mbgl/renderer/query.hpp>
+
+// Java -> C++ conversion
+#include "../android_conversion.hpp"
+#include "../conversion/filter.hpp"
+//#include "../conversion/geojson.hpp"
+
+// C++ -> Java conversion
+#include "../../conversion/conversion.hpp"
+#include "../../conversion/collection.hpp"
+#include "../../geojson/conversion/feature.hpp"
+#include <mbgl/style/conversion/custom_vector_source_options.hpp>
+
+#include <string>
+
+namespace mbgl {
+namespace android {
+
+ // This conversion is expected not to fail because it's used only in contexts where
+ // the value was originally a GeoJsonOptions object on the Java side. If it fails
+ // to convert, it's a bug in our serialization or Java-side static typing.
+ static style::CustomVectorSource::Options convertCustomVectorSourceOptions(jni::JNIEnv& env, jni::Object<> options, style::TileFunction fetchFn, style::TileFunction cancelFn) {
+ using namespace mbgl::style::conversion;
+ if (!options) {
+ return style::CustomVectorSource::Options();
+ }
+ Error error;
+ optional<style::CustomVectorSource::Options> result = convert<style::CustomVectorSource::Options>(Value(env, options), error);
+ if (!result) {
+ throw std::logic_error(error.message);
+ }
+ result->fetchTileFunction = fetchFn;
+ result->cancelTileFunction = cancelFn;
+ return *result;
+ }
+
+ CustomVectorSource::CustomVectorSource(jni::JNIEnv& env, jni::Object<CustomVectorSource> _obj, jni::String sourceId, jni::Object<> options)
+ : Source(env, std::make_unique<mbgl::style::CustomVectorSource>(
+ jni::Make<std::string>(env, sourceId),
+ convertCustomVectorSourceOptions(env, options, std::bind(&CustomVectorSource::fetchTile, this, std::placeholders::_1), std::bind(&CustomVectorSource::cancelTile, this, std::placeholders::_1)))
+ ), javaPeer(SeizeGenericWeakRef(env, jni::Object<CustomVectorSource>(jni::NewWeakGlobalRef(env, _obj.Get()).release()))) {
+
+ }
+
+ CustomVectorSource::CustomVectorSource(mbgl::style::CustomVectorSource& coreSource)
+ : Source(coreSource) {
+ }
+
+ CustomVectorSource::~CustomVectorSource() = default;
+
+ void CustomVectorSource::fetchTile (const mbgl::CanonicalTileID& tileID) {
+ android::UniqueEnv _env = android::AttachEnv();
+ static auto fetchTile = javaClass.GetMethod<void (jni::jint, jni::jint, jni::jint)>(*_env, "fetchTile");
+ assert(javaPeer);
+ javaPeer->Call(*_env, fetchTile, (int)tileID.z, (int)tileID.x, (int)tileID.y);
+ };
+
+ void CustomVectorSource::cancelTile(const mbgl::CanonicalTileID& tileID) {
+ android::UniqueEnv _env = android::AttachEnv();
+ static auto cancelTile = javaClass.GetMethod<void (jni::jint, jni::jint, jni::jint)>(*_env, "cancelTile");
+
+ javaPeer->Call(*_env, cancelTile, (int)tileID.z, (int)tileID.x, (int)tileID.y);
+ };
+
+
+ void CustomVectorSource::setTileData(jni::JNIEnv& env, jni::jint z, jni::jint x, jni::jint y, jni::Object<geojson::FeatureCollection> jFeatures) {
+ using namespace mbgl::android::geojson;
+
+ // Convert the jni object
+ auto geometry = geojson::FeatureCollection::convert(env, jFeatures);
+
+ // Update the core source
+ source.as<mbgl::style::CustomVectorSource>()->CustomVectorSource::setTileData(CanonicalTileID(z, x, y), GeoJSON(geometry));
+ }
+
+ jni::Array<jni::Object<geojson::Feature>> CustomVectorSource::querySourceFeatures(jni::JNIEnv& env,
+ jni::Array<jni::Object<>> jfilter) {
+ using namespace mbgl::android::conversion;
+ using namespace mbgl::android::geojson;
+
+ std::vector<mbgl::Feature> features;
+ if (rendererFrontend) {
+ features = rendererFrontend->querySourceFeatures(source.getID(), { {}, toFilter(env, jfilter) });
+ }
+ return *convert<jni::Array<jni::Object<Feature>>, std::vector<mbgl::Feature>>(env, features);
+ }
+
+ jni::Class<CustomVectorSource> CustomVectorSource::javaClass;
+
+ jni::jobject* CustomVectorSource::createJavaPeer(jni::JNIEnv& env) {
+ static auto constructor = CustomVectorSource::javaClass.template GetConstructor<jni::jlong>(env);
+ return CustomVectorSource::javaClass.New(env, constructor, reinterpret_cast<jni::jlong>(this));
+ }
+
+ void CustomVectorSource::registerNative(jni::JNIEnv& env) {
+ // Lookup the class
+ CustomVectorSource::javaClass = *jni::Class<CustomVectorSource>::Find(env).NewGlobalRef(env).release();
+
+ #define METHOD(MethodPtr, name) jni::MakeNativePeerMethod<decltype(MethodPtr), (MethodPtr)>(name)
+
+ // Register the peer
+ jni::RegisterNativePeer<CustomVectorSource>(
+ env, CustomVectorSource::javaClass, "nativePtr",
+ std::make_unique<CustomVectorSource, JNIEnv&, jni::Object<CustomVectorSource>, jni::String, jni::Object<>>,
+ "initialize",
+ "finalize",
+ METHOD(&CustomVectorSource::querySourceFeatures, "querySourceFeatures"),
+ METHOD(&CustomVectorSource::setTileData, "setTileData")
+ );
+ }
+
+} // namespace android
+} // namespace mbgl
diff --git a/platform/android/src/style/sources/custom_vector_source.hpp b/platform/android/src/style/sources/custom_vector_source.hpp
new file mode 100644
index 0000000000..8e21393b30
--- /dev/null
+++ b/platform/android/src/style/sources/custom_vector_source.hpp
@@ -0,0 +1,46 @@
+#pragma once
+
+#include "source.hpp"
+#include <mbgl/style/sources/custom_vector_source.hpp>
+#include <mbgl/util/geojson.hpp>
+#include <mbgl/tile/tile_id.hpp>
+#include "../../geojson/geometry.hpp"
+#include "../../geojson/feature.hpp"
+#include "../../geojson/feature_collection.hpp"
+#include <jni/jni.hpp>
+
+namespace mbgl {
+namespace android {
+
+class CustomVectorSource : public Source {
+public:
+
+ static constexpr auto Name() { return "com/mapbox/mapboxsdk/style/sources/CustomVectorSource"; };
+
+ static jni::Class<CustomVectorSource> javaClass;
+
+ static void registerNative(jni::JNIEnv&);
+
+ CustomVectorSource(jni::JNIEnv&,
+ jni::Object<CustomVectorSource>,
+ jni::String,
+ jni::Object<>);
+
+ CustomVectorSource(mbgl::style::CustomVectorSource&);
+
+ ~CustomVectorSource();
+
+ void fetchTile(const mbgl::CanonicalTileID& tileID);
+ void cancelTile(const mbgl::CanonicalTileID& tileID);
+ void setTileData(jni::JNIEnv& env, jni::jint z, jni::jint x, jni::jint y, jni::Object<geojson::FeatureCollection> jf);
+
+ jni::Array<jni::Object<geojson::Feature>> querySourceFeatures(jni::JNIEnv&,
+ jni::Array<jni::Object<>> );
+
+ jni::jobject* createJavaPeer(jni::JNIEnv&);
+
+ GenericUniqueWeakObject<CustomVectorSource> javaPeer;
+}; // class CustomVectorSource
+
+} // namespace android
+} // namespace mbgl
diff --git a/platform/android/src/style/sources/sources.cpp b/platform/android/src/style/sources/sources.cpp
index 9ab3ca8e84..023b04122f 100644
--- a/platform/android/src/style/sources/sources.cpp
+++ b/platform/android/src/style/sources/sources.cpp
@@ -12,6 +12,7 @@
#include "raster_source.hpp"
#include "unknown_source.hpp"
#include "vector_source.hpp"
+#include "custom_vector_source.hpp"
namespace {
@@ -54,6 +55,7 @@ void registerNativeSources(jni::JNIEnv& env) {
RasterSource::registerNative(env);
UnknownSource::registerNative(env);
VectorSource::registerNative(env);
+ CustomVectorSource::registerNative(env);
}
}