From 1c4b6a78328f0f39992fd39df68a9b609c32c059 Mon Sep 17 00:00:00 2001 From: Ivo van Dongen Date: Fri, 27 Oct 2017 20:08:47 +0300 Subject: [android] map snapshotter - expose attributions --- .../mapbox/mapboxsdk/snapshotter/MapSnapshot.java | 11 ++++++++- platform/android/src/jni/collection.hpp | 26 ++++++++++++++++++++++ platform/android/src/snapshotter/map_snapshot.cpp | 6 +++-- platform/android/src/snapshotter/map_snapshot.hpp | 4 ++++ .../android/src/snapshotter/map_snapshotter.cpp | 6 +++-- 5 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 platform/android/src/jni/collection.hpp diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnapshot.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnapshot.java index 2d1412aeda..aefa962402 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnapshot.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnapshot.java @@ -14,13 +14,15 @@ public class MapSnapshot { private long nativePtr = 0; private Bitmap bitmap; + private String[] attributions; /** * Created from native side */ - private MapSnapshot(long nativePtr, Bitmap bitmap) { + private MapSnapshot(long nativePtr, Bitmap bitmap, String[] attributions) { this.nativePtr = nativePtr; this.bitmap = bitmap; + this.attributions = attributions; } /** @@ -38,6 +40,13 @@ public class MapSnapshot { */ public native PointF pixelForLatLng(LatLng latLng); + /** + * @return The attributions for the sources of this snapshot. + */ + protected String[] getAttributions() { + return attributions; + } + // Unused, needed for peer binding private native void initialize(); diff --git a/platform/android/src/jni/collection.hpp b/platform/android/src/jni/collection.hpp new file mode 100644 index 0000000000..5f94ec29ce --- /dev/null +++ b/platform/android/src/jni/collection.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include + +#include +#include + +namespace jni { + +inline Array MakeAnything(ThingToMake>, JNIEnv& env, const std::vector& vector) +{ + static auto clazz = *Class::Find(env).NewGlobalRef(env).release(); + auto result = Array::New(env, vector.size(), clazz); + + std::size_t index = 0; + for (auto&& item : vector) { + auto element = Make(env, item); + result.Set(env, index, element); + DeleteLocalRef(env, element); + index++; + } + + return result; +} + +} diff --git a/platform/android/src/snapshotter/map_snapshot.cpp b/platform/android/src/snapshotter/map_snapshot.cpp index 09e83bbb8a..843a8a487a 100644 --- a/platform/android/src/snapshotter/map_snapshot.cpp +++ b/platform/android/src/snapshotter/map_snapshot.cpp @@ -1,6 +1,7 @@ #include "map_snapshot.hpp" #include "../bitmap.hpp" +#include "../jni/collection.hpp" #include @@ -25,14 +26,15 @@ jni::Object MapSnapshot::pixelForLatLng(jni::JNIEnv& env, jni::Object MapSnapshot::New(JNIEnv& env, PremultipliedImage&& image, float pixelRatio, + std::vector attributions, mbgl::MapSnapshotter::PointForFn pointForFn) { // Create the bitmap auto bitmap = Bitmap::CreateBitmap(env, std::move(image)); // Create the Mapsnapshot peers - static auto constructor = javaClass.GetConstructor>(env); + static auto constructor = javaClass.GetConstructor, jni::Array>(env); auto nativePeer = std::make_unique(pixelRatio, pointForFn); - return javaClass.New(env, constructor, reinterpret_cast(nativePeer.release()), bitmap); + return javaClass.New(env, constructor, reinterpret_cast(nativePeer.release()), bitmap, jni::Make>(env, attributions)); } jni::Class MapSnapshot::javaClass; diff --git a/platform/android/src/snapshotter/map_snapshot.hpp b/platform/android/src/snapshotter/map_snapshot.hpp index 6d60d49728..64090bb48b 100644 --- a/platform/android/src/snapshotter/map_snapshot.hpp +++ b/platform/android/src/snapshotter/map_snapshot.hpp @@ -7,6 +7,9 @@ #include "../geometry/lat_lng.hpp" #include "../graphics/pointf.hpp" +#include +#include + namespace mbgl { namespace android { @@ -22,6 +25,7 @@ public: static jni::Object New(JNIEnv& env, PremultipliedImage&& image, float pixelRatio, + std::vector attributions, PointForFn pointForFn); MapSnapshot(jni::JNIEnv&) {}; diff --git a/platform/android/src/snapshotter/map_snapshotter.cpp b/platform/android/src/snapshotter/map_snapshotter.cpp index a13e91ccd3..3c6623af40 100644 --- a/platform/android/src/snapshotter/map_snapshotter.cpp +++ b/platform/android/src/snapshotter/map_snapshotter.cpp @@ -58,7 +58,9 @@ MapSnapshotter::~MapSnapshotter() = default; void MapSnapshotter::start(JNIEnv&) { MBGL_VERIFY_THREAD(tid); - snapshotCallback = std::make_unique>(*Scheduler::GetCurrent(), [this](std::exception_ptr err, PremultipliedImage image, mbgl::MapSnapshotter::PointForFn pointForFn) { + snapshotCallback = std::make_unique>( + *Scheduler::GetCurrent(), + [this](std::exception_ptr err, PremultipliedImage image, std::vector attributions, mbgl::MapSnapshotter::PointForFn pointForFn) { MBGL_VERIFY_THREAD(tid); android::UniqueEnv _env = android::AttachEnv(); @@ -68,7 +70,7 @@ void MapSnapshotter::start(JNIEnv&) { javaPeer->Call(*_env, onSnapshotFailed, jni::Make(*_env, util::toString(err))); } else { // Create the wrapper - auto mapSnapshot = android::MapSnapshot::New(*_env, std::move(image), pixelRatio, pointForFn); + auto mapSnapshot = android::MapSnapshot::New(*_env, std::move(image), pixelRatio, attributions, pointForFn); // invoke callback static auto onSnapshotReady = javaClass.GetMethod)>(*_env, "onSnapshotReady"); -- cgit v1.2.1