diff options
author | Asheem Mamoowala <asheem.mamoowala@mapbox.com> | 2017-05-25 14:36:05 -0700 |
---|---|---|
committer | Asheem Mamoowala <asheem.mamoowala@mapbox.com> | 2017-06-19 09:50:08 -0700 |
commit | b7c7d3fdab283d7bf03d8acf68b9cfd478d6973f (patch) | |
tree | d0e2d24300e5b28fe3a5f91d37456ffcb988d287 /platform/android/src | |
parent | ab5b310a9eb7c88935cc457da1af81349fbe8d41 (diff) | |
download | qtlocation-mapboxgl-b7c7d3fdab283d7bf03d8acf68b9cfd478d6973f.tar.gz |
[android] Add ImageSource bindings
Diffstat (limited to 'platform/android/src')
-rw-r--r-- | platform/android/src/geometry/lat_lng_quad.cpp | 39 | ||||
-rw-r--r-- | platform/android/src/geometry/lat_lng_quad.hpp | 30 | ||||
-rwxr-xr-x | platform/android/src/jni.cpp | 2 | ||||
-rw-r--r-- | platform/android/src/style/conversion/latlngquad.hpp | 24 | ||||
-rw-r--r-- | platform/android/src/style/sources/image_source.cpp | 48 | ||||
-rw-r--r-- | platform/android/src/style/sources/image_source.hpp | 7 | ||||
-rw-r--r-- | platform/android/src/style/sources/sources.cpp | 5 |
7 files changed, 126 insertions, 29 deletions
diff --git a/platform/android/src/geometry/lat_lng_quad.cpp b/platform/android/src/geometry/lat_lng_quad.cpp new file mode 100644 index 0000000000..2b36139e18 --- /dev/null +++ b/platform/android/src/geometry/lat_lng_quad.cpp @@ -0,0 +1,39 @@ +#include "lat_lng_quad.hpp" +#include "lat_lng.hpp" + +namespace mbgl { +namespace android { + +jni::Object<LatLngQuad> LatLngQuad::New(jni::JNIEnv& env, std::array<mbgl::LatLng, 4> coordinates) { + static auto quadConstructor = LatLngQuad::javaClass.GetConstructor<jni::Object<LatLng>, jni::Object<LatLng>, jni::Object<LatLng>, jni::Object<LatLng>>(env); + return LatLngQuad::javaClass.New(env, quadConstructor, + LatLng::New(env, coordinates[0]), + LatLng::New(env, coordinates[1]), + LatLng::New(env, coordinates[2]), + LatLng::New(env, coordinates[3])); +} + +std::array<mbgl::LatLng, 4> LatLngQuad::getLatLngArray(jni::JNIEnv& env, jni::Object<LatLngQuad> quad) { + static auto topLeftField = LatLngQuad::javaClass.GetField <jni::Object<LatLng>>(env, "topLeft"); + static auto topRightField = LatLngQuad::javaClass.GetField <jni::Object<LatLng>>(env, "topRight"); + static auto bottomRightField = LatLngQuad::javaClass.GetField <jni::Object<LatLng>>(env, "bottomRight"); + static auto bottomLeftField = LatLngQuad::javaClass.GetField <jni::Object<LatLng>>(env, "bottomLeft"); + + return std::array < mbgl::LatLng, 4 > {{ + LatLng::getLatLng(env, quad.Get(env, topLeftField)), + LatLng::getLatLng(env, quad.Get(env, topRightField)), + LatLng::getLatLng(env, quad.Get(env, bottomRightField)), + LatLng::getLatLng(env, quad.Get(env, bottomLeftField)) + }}; +} + +void LatLngQuad::registerNative(jni::JNIEnv& env) { + // Lookup the class + LatLngQuad::javaClass = *jni::Class<LatLngQuad>::Find(env).NewGlobalRef(env).release(); +} + +jni::Class<LatLngQuad> LatLngQuad::javaClass; + + +} // namespace android +} // namespace mbgl
\ No newline at end of file diff --git a/platform/android/src/geometry/lat_lng_quad.hpp b/platform/android/src/geometry/lat_lng_quad.hpp new file mode 100644 index 0000000000..8f8c9abeef --- /dev/null +++ b/platform/android/src/geometry/lat_lng_quad.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include <mbgl/util/noncopyable.hpp> +#include <mbgl/util/geo.hpp> +#include <mbgl/util/geometry.hpp> + +#include <jni/jni.hpp> +#include <array> + +namespace mbgl { +namespace android { + +class LatLngQuad : private mbgl::util::noncopyable { +public: + + static constexpr auto Name() { return "com/mapbox/mapboxsdk/geometry/LatLngQuad"; }; + + static jni::Object<LatLngQuad> New(jni::JNIEnv&, std::array<mbgl::LatLng, 4>); + + static std::array<mbgl::LatLng, 4> getLatLngArray(jni::JNIEnv&, jni::Object<LatLngQuad>); + + static jni::Class<LatLngQuad> javaClass; + + static void registerNative(jni::JNIEnv&); + +}; + + +} // namespace android +} // namespace mbgl
\ No newline at end of file diff --git a/platform/android/src/jni.cpp b/platform/android/src/jni.cpp index 6c490fad5c..db8dd1dbdf 100755 --- a/platform/android/src/jni.cpp +++ b/platform/android/src/jni.cpp @@ -23,6 +23,7 @@ #include "geojson/position.hpp" #include "geometry/lat_lng.hpp" #include "geometry/lat_lng_bounds.hpp" +#include "geometry/lat_lng_quad.hpp" #include "geometry/projected_meters.hpp" #include "graphics/pointf.hpp" #include "graphics/rectf.hpp" @@ -127,6 +128,7 @@ void registerNatives(JavaVM *vm) { // Geometry LatLng::registerNative(env); LatLngBounds::registerNative(env); + LatLngQuad::registerNative(env); ProjectedMeters::registerNative(env); // GSon diff --git a/platform/android/src/style/conversion/latlngquad.hpp b/platform/android/src/style/conversion/latlngquad.hpp new file mode 100644 index 0000000000..9d1a83e164 --- /dev/null +++ b/platform/android/src/style/conversion/latlngquad.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include <mapbox/geojson.hpp> +#include <mbgl/style/conversion.hpp> +#include <mbgl/style/conversion/geojson.hpp> +#include <jni/jni.hpp> + +namespace mbgl { +namespace style { +namespace conversion { + +template <> +optional<std::array<LatLng, 4>> Converter<std::array<LatLng, 4>>::operator()(const mbgl::android::Value& value, Error& error) const { + if (value.isNull() || !value.isArray()) { + error = { "value cannot be converted to LatLng array" }; + return {}; + } + + return convert<GeoJSON>(value.toString(), error); +} + +} // namespace conversion +} // namespace style +} // namespace mbgl diff --git a/platform/android/src/style/sources/image_source.cpp b/platform/android/src/style/sources/image_source.cpp index 9909675f44..cc7e1e7404 100644 --- a/platform/android/src/style/sources/image_source.cpp +++ b/platform/android/src/style/sources/image_source.cpp @@ -6,15 +6,19 @@ // C++ -> Java conversion #include "../../conversion/conversion.hpp" #include <mbgl/style/conversion.hpp> +#include <mbgl/util/premultiply.hpp> +#include "../../bitmap.hpp" #include <string> +#include <array> namespace mbgl { namespace android { - ImageSource::ImageSource(jni::JNIEnv& env, jni::String sourceId) + ImageSource::ImageSource(jni::JNIEnv& env, jni::String sourceId, jni::Object<LatLngQuad> coordinatesObject) : Source(env, std::make_unique<mbgl::style::ImageSource>( - jni::Make<std::string>(env, sourceId) + jni::Make<std::string>(env, sourceId), + LatLngQuad::getLatLngArray(env, coordinatesObject) ) ) { } @@ -25,31 +29,20 @@ namespace android { ImageSource::~ImageSource() = default; - void GeoJSONSource::setGeoJSONString(jni::JNIEnv& env, jni::String json) { - using namespace mbgl::style::conversion; - - // Convert the jni object - Error error; - optional<GeoJSON> converted = convert<GeoJSON>(Value(env, json), error); - if(!converted) { - mbgl::Log::Error(mbgl::Event::JNI, "Error setting geo json: " + error.message); - return; - } - - // Update the core source - source.as<mbgl::style::GeoJSONSource>()->GeoJSONSource::setGeoJSON(*converted); - } - void ImageSource::setURL(jni::JNIEnv& env, jni::String url) { // Update the core source - source.as<mbgl::style::GeoJSONSource>()->GeoJSONSource::setURL(jni::Make<std::string>(env, url)); + source.as<mbgl::style::ImageSource>()->ImageSource::setURL(jni::Make<std::string>(env, url)); } jni::String ImageSource::getURL(jni::JNIEnv& env) { - std::string url = source.as<mbgl::style::ImageSource>()->ImageSource::getURL(); - return !url.empty() ? jni::Make<jni::String>(env, url) : jni::String(); + optional<std::string> url = source.as<mbgl::style::ImageSource>()->ImageSource::getURL(); + return url ? jni::Make<jni::String>(env, *url) : jni::String(); } + void ImageSource::setImage(jni::JNIEnv& env, jni::Object<Bitmap> bitmap) { + UnassociatedImage image = util::unpremultiply(Bitmap::GetImage(env, bitmap)); + source.as<mbgl::style::ImageSource>()->setImage(std:: move(image)); + } jni::Class<ImageSource> ImageSource::javaClass; @@ -66,14 +59,13 @@ namespace android { // Register the peer jni::RegisterNativePeer<ImageSource>( - env, ImageSource::javaClass, "nativePtr", - std::make_unique<ImageSource, JNIEnv&, jni::String, jni::Object<>>, - "initialize", - "finalize", - METHOD(&ImageSource::setGeoJSONString, "nativeSetGeoJsonString"), - METHOD(&ImageSource::setURL, "nativeSetUrl"), - METHOD(&ImageSource::getURL, "nativeGetUrl"), - METHOD(&ImageSource::getURL, "nativeGetUrl"), + env, ImageSource::javaClass, "nativePtr", + std::make_unique<ImageSource, JNIEnv&, jni::String, jni::Object<LatLngQuad>>, + "initialize", + "finalize", + METHOD(&ImageSource::setURL, "nativeSetUrl"), + METHOD(&ImageSource::getURL, "nativeGetUrl"), + METHOD(&ImageSource::setImage, "nativeSetImage") ); } diff --git a/platform/android/src/style/sources/image_source.hpp b/platform/android/src/style/sources/image_source.hpp index d5802ed5a1..309d17a299 100644 --- a/platform/android/src/style/sources/image_source.hpp +++ b/platform/android/src/style/sources/image_source.hpp @@ -1,12 +1,15 @@ #pragma once #include "source.hpp" +#include "../../geometry/lat_lng_quad.hpp" #include <mbgl/style/sources/image_source.hpp> #include <jni/jni.hpp> namespace mbgl { namespace android { +class Bitmap; + class ImageSource : public Source { public: @@ -16,7 +19,7 @@ public: static void registerNative(jni::JNIEnv&); - ImageSource(jni::JNIEnv&, jni::String, jni::Object<>); + ImageSource(jni::JNIEnv&, jni::String, jni::Object<LatLngQuad>); ImageSource(mbgl::Map&, mbgl::style::ImageSource&); @@ -25,6 +28,8 @@ public: void setURL(jni::JNIEnv&, jni::String); jni::String getURL(jni::JNIEnv&); + void setImage(jni::JNIEnv&, jni::Object<Bitmap>); + jni::jobject* createJavaPeer(jni::JNIEnv&); }; // class ImageSource diff --git a/platform/android/src/style/sources/sources.cpp b/platform/android/src/style/sources/sources.cpp index b4e70202b4..7ca6328e71 100644 --- a/platform/android/src/style/sources/sources.cpp +++ b/platform/android/src/style/sources/sources.cpp @@ -2,11 +2,13 @@ #include <mbgl/style/source.hpp> #include <mbgl/style/sources/geojson_source.hpp> +#include <mbgl/style/sources/image_source.hpp> #include <mbgl/style/sources/raster_source.hpp> #include <mbgl/style/sources/vector_source.hpp> #include "source.hpp" #include "geojson_source.hpp" +#include "image_source.hpp" #include "raster_source.hpp" #include "unknown_source.hpp" #include "vector_source.hpp" @@ -22,6 +24,8 @@ Source* initializeSourcePeer(mbgl::Map& map, mbgl::style::Source& coreSource) { 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 if (coreSource.is<mbgl::style::ImageSource>()) { + source = new ImageSource(map, *coreSource.as<mbgl::style::ImageSource>()); } else { source = new UnknownSource(map, coreSource); } @@ -39,6 +43,7 @@ jni::jobject* createJavaSourcePeer(jni::JNIEnv& env, mbgl::Map& map, mbgl::style void registerNativeSources(jni::JNIEnv& env) { Source::registerNative(env); GeoJSONSource::registerNative(env); + ImageSource::registerNative(env); RasterSource::registerNative(env); UnknownSource::registerNative(env); VectorSource::registerNative(env); |