[android] Add ImageSource bindings

This commit is contained in:
Asheem Mamoowala
2017-05-25 14:36:05 -07:00
parent ab5b310a9e
commit b7c7d3fdab
18 changed files with 528 additions and 29 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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")
);
}

View File

@@ -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

View File

@@ -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);