Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] map snapshotter - expose attributions
Browse files Browse the repository at this point in the history
  • Loading branch information
ivovandongen committed Oct 31, 2017
1 parent d65676d commit 1c4b6a7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -38,6 +40,13 @@ public Bitmap getBitmap() {
*/
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();

Expand Down
26 changes: 26 additions & 0 deletions platform/android/src/jni/collection.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <jni/jni.hpp>

#include <string>
#include <vector>

namespace jni {

inline Array<String> MakeAnything(ThingToMake<Array<String>>, JNIEnv& env, const std::vector<std::string>& vector)
{
static auto clazz = *Class<StringTag>::Find(env).NewGlobalRef(env).release();
auto result = Array<String>::New(env, vector.size(), clazz);

std::size_t index = 0;
for (auto&& item : vector) {
auto element = Make<jni::String>(env, item);
result.Set(env, index, element);
DeleteLocalRef(env, element);
index++;
}

return result;
}

}
6 changes: 4 additions & 2 deletions platform/android/src/snapshotter/map_snapshot.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "map_snapshot.hpp"

#include "../bitmap.hpp"
#include "../jni/collection.hpp"

#include <memory>

Expand All @@ -25,14 +26,15 @@ jni::Object<PointF> MapSnapshot::pixelForLatLng(jni::JNIEnv& env, jni::Object<La
jni::Object<MapSnapshot> MapSnapshot::New(JNIEnv& env,
PremultipliedImage&& image,
float pixelRatio,
std::vector<std::string> 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<jni::jlong, jni::Object<Bitmap>>(env);
static auto constructor = javaClass.GetConstructor<jni::jlong, jni::Object<Bitmap>, jni::Array<jni::String>>(env);
auto nativePeer = std::make_unique<MapSnapshot>(pixelRatio, pointForFn);
return javaClass.New(env, constructor, reinterpret_cast<jlong>(nativePeer.release()), bitmap);
return javaClass.New(env, constructor, reinterpret_cast<jlong>(nativePeer.release()), bitmap, jni::Make<jni::Array<jni::String>>(env, attributions));
}

jni::Class<MapSnapshot> MapSnapshot::javaClass;
Expand Down
4 changes: 4 additions & 0 deletions platform/android/src/snapshotter/map_snapshot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include "../geometry/lat_lng.hpp"
#include "../graphics/pointf.hpp"

#include <vector>
#include <string>

namespace mbgl {
namespace android {

Expand All @@ -22,6 +25,7 @@ class MapSnapshot {
static jni::Object<MapSnapshot> New(JNIEnv& env,
PremultipliedImage&& image,
float pixelRatio,
std::vector<std::string> attributions,
PointForFn pointForFn);

MapSnapshot(jni::JNIEnv&) {};
Expand Down
6 changes: 4 additions & 2 deletions platform/android/src/snapshotter/map_snapshotter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ MapSnapshotter::~MapSnapshotter() = default;
void MapSnapshotter::start(JNIEnv&) {
MBGL_VERIFY_THREAD(tid);

snapshotCallback = std::make_unique<Actor<mbgl::MapSnapshotter::Callback>>(*Scheduler::GetCurrent(), [this](std::exception_ptr err, PremultipliedImage image, mbgl::MapSnapshotter::PointForFn pointForFn) {
snapshotCallback = std::make_unique<Actor<mbgl::MapSnapshotter::Callback>>(
*Scheduler::GetCurrent(),
[this](std::exception_ptr err, PremultipliedImage image, std::vector<std::string> attributions, mbgl::MapSnapshotter::PointForFn pointForFn) {
MBGL_VERIFY_THREAD(tid);
android::UniqueEnv _env = android::AttachEnv();

Expand All @@ -68,7 +70,7 @@ void MapSnapshotter::start(JNIEnv&) {
javaPeer->Call(*_env, onSnapshotFailed, jni::Make<jni::String>(*_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<void (jni::Object<MapSnapshot>)>(*_env, "onSnapshotReady");
Expand Down

0 comments on commit 1c4b6a7

Please sign in to comment.