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

Commit

Permalink
[android] Expose getLayer, getSource and Observer interface for snaps…
Browse files Browse the repository at this point in the history
…hotter

So that users can modify properties of an existing layer / source objects
  • Loading branch information
alexshalamov committed Mar 25, 2020
1 parent 77d95d2 commit 2b67ce7
Show file tree
Hide file tree
Showing 18 changed files with 82 additions and 57 deletions.
27 changes: 27 additions & 0 deletions platform/android/src/snapshotter/map_snapshotter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <mbgl/util/string.hpp>

#include "../attach_env.hpp"
#include "../style/layers/layer_manager.hpp"
#include "map_snapshot.hpp"

namespace mbgl {
Expand Down Expand Up @@ -282,6 +283,30 @@ void MapSnapshotter::addImages(JNIEnv& env, const jni::Array<jni::Object<mbgl::a
}
}

jni::Local<jni::Object<Layer>> MapSnapshotter::getLayer(JNIEnv& env, const jni::String& layerId) {
// Find the layer
mbgl::style::Layer* coreLayer = snapshotter->getStyle().getLayer(jni::Make<std::string>(env, layerId));
if (!coreLayer) {
mbgl::Log::Debug(mbgl::Event::JNI, "No layer found");
return jni::Local<jni::Object<Layer>>();
}

// Create and return the layer's native peer
return LayerManagerAndroid::get()->createJavaLayerPeer(env, *coreLayer);
}

jni::Local<jni::Object<Source>> MapSnapshotter::getSource(JNIEnv& env, const jni::String& sourceId) {
// Find the source
mbgl::style::Source* coreSource = snapshotter->getStyle().getSource(jni::Make<std::string>(env, sourceId));
if (!coreSource) {
mbgl::Log::Debug(mbgl::Event::JNI, "No source found");
return jni::Local<jni::Object<Source>>();
}

// Create and return the source's native peer
return jni::NewLocal(env, Source::peerForCoreSource(env, *coreSource));
}

// Static methods //

void MapSnapshotter::registerNative(jni::JNIEnv& env) {
Expand Down Expand Up @@ -314,6 +339,8 @@ void MapSnapshotter::registerNative(jni::JNIEnv& env) {
METHOD(&MapSnapshotter::addLayerAbove, "nativeAddLayerAbove"),
METHOD(&MapSnapshotter::addSource, "nativeAddSource"),
METHOD(&MapSnapshotter::addImages, "nativeAddImages"),
METHOD(&MapSnapshotter::getLayer, "nativeGetLayer"),
METHOD(&MapSnapshotter::getSource, "nativeGetSource"),
METHOD(&MapSnapshotter::setStyleJson, "setStyleJson"),
METHOD(&MapSnapshotter::setSize, "setSize"),
METHOD(&MapSnapshotter::setCameraPosition, "setCameraPosition"),
Expand Down
2 changes: 2 additions & 0 deletions platform/android/src/snapshotter/map_snapshotter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class MapSnapshotter final : public mbgl::MapSnapshotterObserver {
void addLayerAbove(JNIEnv&, jlong, const jni::String&);
void addSource(JNIEnv&, const jni::Object<Source>&, jlong nativePtr);
void addImages(JNIEnv&, const jni::Array<jni::Object<mbgl::android::Image>>&);
jni::Local<jni::Object<Layer>> getLayer(JNIEnv&, const jni::String&);
jni::Local<jni::Object<Source>> getSource(JNIEnv&, const jni::String&);

// MapSnapshotterObserver overrides
void onDidFailLoadingStyle(const std::string&) override;
Expand Down
5 changes: 2 additions & 3 deletions platform/android/src/style/sources/custom_geometry_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ namespace android {

CustomGeometrySource::CustomGeometrySource(jni::JNIEnv& env,
mbgl::style::Source& coreSource,
AndroidRendererFrontend& frontend)
: Source(env, coreSource, createJavaPeer(env), frontend) {
}
AndroidRendererFrontend* frontend)
: Source(env, coreSource, createJavaPeer(env), frontend) {}

CustomGeometrySource::~CustomGeometrySource() {
releaseThreads();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CustomGeometrySource : public Source {
static void registerNative(jni::JNIEnv&);

CustomGeometrySource(jni::JNIEnv&, const jni::String&, const jni::Object<>&);
CustomGeometrySource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend&);
CustomGeometrySource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend*);
~CustomGeometrySource();

bool removeFromMap(JNIEnv&, const jni::Object<Source>&, mbgl::Map&) override;
Expand Down
2 changes: 1 addition & 1 deletion platform/android/src/style/sources/geojson_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ GeoJSONSource::GeoJSONSource(jni::JNIEnv& env, const jni::String& sourceId, cons
converter(std::make_unique<Actor<FeatureConverter>>(Scheduler::GetBackground(),
source.as<style::GeoJSONSource>()->impl().getOptions())) {}

GeoJSONSource::GeoJSONSource(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend& frontend)
GeoJSONSource::GeoJSONSource(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend* frontend)
: Source(env, coreSource, createJavaPeer(env), frontend),
converter(std::make_unique<Actor<FeatureConverter>>(Scheduler::GetBackground(),
source.as<style::GeoJSONSource>()->impl().getOptions())) {}
Expand Down
2 changes: 1 addition & 1 deletion platform/android/src/style/sources/geojson_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class GeoJSONSource : public Source {
static void registerNative(jni::JNIEnv&);

GeoJSONSource(jni::JNIEnv&, const jni::String&, const jni::Object<>&);
GeoJSONSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend&);
GeoJSONSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend*);
~GeoJSONSource();

private:
Expand Down
7 changes: 2 additions & 5 deletions platform/android/src/style/sources/image_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ namespace android {
) {
}

ImageSource::ImageSource(jni::JNIEnv& env,
mbgl::style::Source& coreSource,
AndroidRendererFrontend& frontend)
: Source(env, coreSource, createJavaPeer(env), frontend) {
}
ImageSource::ImageSource(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend* frontend)
: Source(env, coreSource, createJavaPeer(env), frontend) {}

ImageSource::~ImageSource() = default;

Expand Down
2 changes: 1 addition & 1 deletion platform/android/src/style/sources/image_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ImageSource : public Source {

ImageSource(jni::JNIEnv&, const jni::String&, const jni::Object<LatLngQuad>&);

ImageSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend&);
ImageSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend*);

~ImageSource();

Expand Down
7 changes: 3 additions & 4 deletions platform/android/src/style/sources/raster_dem_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ namespace android {
}

RasterDEMSource::RasterDEMSource(jni::JNIEnv& env,
mbgl::style::Source& coreSource,
AndroidRendererFrontend& frontend)
: Source(env, coreSource, createJavaPeer(env), frontend) {
}
mbgl::style::Source& coreSource,
AndroidRendererFrontend* frontend)
: Source(env, coreSource, createJavaPeer(env), frontend) {}

RasterDEMSource::~RasterDEMSource() = default;

Expand Down
2 changes: 1 addition & 1 deletion platform/android/src/style/sources/raster_dem_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class RasterDEMSource : public Source {
static void registerNative(jni::JNIEnv&);

RasterDEMSource(jni::JNIEnv&, const jni::String&, const jni::Object<>&, jni::jint);
RasterDEMSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend&);
RasterDEMSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend*);
~RasterDEMSource();

jni::Local<jni::String> getURL(jni::JNIEnv&);
Expand Down
7 changes: 2 additions & 5 deletions platform/android/src/style/sources/raster_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ namespace android {
) {
}

RasterSource::RasterSource(jni::JNIEnv& env,
mbgl::style::Source& coreSource,
AndroidRendererFrontend& frontend)
: Source(env, coreSource, createJavaPeer(env), frontend) {
}
RasterSource::RasterSource(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend* frontend)
: Source(env, coreSource, createJavaPeer(env), frontend) {}

RasterSource::~RasterSource() = default;

Expand Down
2 changes: 1 addition & 1 deletion platform/android/src/style/sources/raster_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class RasterSource : public Source {
static void registerNative(jni::JNIEnv&);

RasterSource(jni::JNIEnv&, const jni::String&, const jni::Object<>&, jni::jint);
RasterSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend&);
RasterSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend*);
~RasterSource();

jni::Local<jni::String> getURL(jni::JNIEnv&);
Expand Down
43 changes: 26 additions & 17 deletions platform/android/src/style/sources/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,42 @@
namespace mbgl {
namespace android {

static std::unique_ptr<Source> createSourcePeer(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend& frontend) {
if (coreSource.is<mbgl::style::VectorSource>()) {
return std::make_unique<VectorSource>(env, *coreSource.as<mbgl::style::VectorSource>(), frontend);
} else if (coreSource.is<mbgl::style::RasterSource>()) {
return std::make_unique<RasterSource>(env, *coreSource.as<mbgl::style::RasterSource>(), frontend);
} else if (coreSource.is<mbgl::style::GeoJSONSource>()) {
return std::make_unique<GeoJSONSource>(env, *coreSource.as<mbgl::style::GeoJSONSource>(), frontend);
} else if (coreSource.is<mbgl::style::ImageSource>()) {
return std::make_unique<ImageSource>(env, *coreSource.as<mbgl::style::ImageSource>(), frontend);
} else {
return std::make_unique<UnknownSource>(env, coreSource, frontend);
}
static std::unique_ptr<Source> createSourcePeer(jni::JNIEnv& env,
mbgl::style::Source& coreSource,
AndroidRendererFrontend* frontend) {
if (coreSource.is<mbgl::style::VectorSource>()) {
return std::make_unique<VectorSource>(env, *coreSource.as<mbgl::style::VectorSource>(), frontend);
} else if (coreSource.is<mbgl::style::RasterSource>()) {
return std::make_unique<RasterSource>(env, *coreSource.as<mbgl::style::RasterSource>(), frontend);
} else if (coreSource.is<mbgl::style::GeoJSONSource>()) {
return std::make_unique<GeoJSONSource>(env, *coreSource.as<mbgl::style::GeoJSONSource>(), frontend);
} else if (coreSource.is<mbgl::style::ImageSource>()) {
return std::make_unique<ImageSource>(env, *coreSource.as<mbgl::style::ImageSource>(), frontend);
} else {
return std::make_unique<UnknownSource>(env, coreSource, frontend);
}
}

const jni::Object<Source>& Source::peerForCoreSource(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend& frontend) {
if (!coreSource.peer.has_value()) {
coreSource.peer = createSourcePeer(env, coreSource, frontend);
coreSource.peer = createSourcePeer(env, coreSource, &frontend);
}
return coreSource.peer.get<std::unique_ptr<Source>>()->javaPeer;
}

Source::Source(jni::JNIEnv& env, mbgl::style::Source& coreSource, const jni::Object<Source>& obj, AndroidRendererFrontend& frontend)
: source(coreSource)
, javaPeer(jni::NewGlobal(env, obj))
, rendererFrontend(&frontend) {
const jni::Object<Source>& Source::peerForCoreSource(jni::JNIEnv& env, mbgl::style::Source& coreSource) {
if (!coreSource.peer.has_value()) {
coreSource.peer = createSourcePeer(env, coreSource, nullptr);
}
return coreSource.peer.get<std::unique_ptr<Source>>()->javaPeer;
}

Source::Source(jni::JNIEnv& env,
mbgl::style::Source& coreSource,
const jni::Object<Source>& obj,
AndroidRendererFrontend* frontend)
: source(coreSource), javaPeer(jni::NewGlobal(env, obj)), rendererFrontend(frontend) {}

Source::Source(jni::JNIEnv&, std::unique_ptr<mbgl::style::Source> coreSource)
: ownedSource(std::move(coreSource))
, source(*ownedSource) {
Expand Down
3 changes: 2 additions & 1 deletion platform/android/src/style/sources/source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ class Source : private mbgl::util::noncopyable {

static void registerNative(jni::JNIEnv&);

static const jni::Object<Source>& peerForCoreSource(jni::JNIEnv&, mbgl::style::Source&);
static const jni::Object<Source>& peerForCoreSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend&);

/*
* Called when a Java object is created for a core source that belongs to a map.
*/
Source(jni::JNIEnv&, mbgl::style::Source&, const jni::Object<Source>&, AndroidRendererFrontend&);
Source(jni::JNIEnv&, mbgl::style::Source&, const jni::Object<Source>&, AndroidRendererFrontend*);

/*
* Called when a Java object is created for a new core source that does not belong to a map.
Expand Down
15 changes: 6 additions & 9 deletions platform/android/src/style/sources/unknown_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@ namespace {
namespace mbgl {
namespace android {

UnknownSource::UnknownSource(jni::JNIEnv& env,
mbgl::style::Source& coreSource,
AndroidRendererFrontend& frontend)
: Source(env, coreSource, createJavaPeer(env), frontend) {
}
UnknownSource::UnknownSource(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend* frontend)
: Source(env, coreSource, createJavaPeer(env), frontend) {}

jni::Local<jni::Object<Source>> UnknownSource::createJavaPeer(jni::JNIEnv& env) {
static auto& javaClass = jni::Class<UnknownSource>::Singleton(env);
static auto constructor = javaClass.GetConstructor<jni::jlong>(env);
return javaClass.New(env, constructor, reinterpret_cast<jni::jlong>(this));
jni::Local<jni::Object<Source>> UnknownSource::createJavaPeer(jni::JNIEnv& env) {
static auto& javaClass = jni::Class<UnknownSource>::Singleton(env);
static auto constructor = javaClass.GetConstructor<jni::jlong>(env);
return javaClass.New(env, constructor, reinterpret_cast<jni::jlong>(this));
}

void UnknownSource::registerNative(jni::JNIEnv& env) {
Expand Down
2 changes: 1 addition & 1 deletion platform/android/src/style/sources/unknown_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class UnknownSource : public Source {

static void registerNative(jni::JNIEnv&);

UnknownSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend&);
UnknownSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend*);

~UnknownSource() = default;

Expand Down
7 changes: 2 additions & 5 deletions platform/android/src/style/sources/vector_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ namespace android {
) {
}

VectorSource::VectorSource(jni::JNIEnv& env,
mbgl::style::Source& coreSource,
AndroidRendererFrontend& frontend)
: Source(env, coreSource, createJavaPeer(env), frontend) {
}
VectorSource::VectorSource(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend* frontend)
: Source(env, coreSource, createJavaPeer(env), frontend) {}

VectorSource::~VectorSource() = default;

Expand Down
2 changes: 1 addition & 1 deletion platform/android/src/style/sources/vector_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class VectorSource : public Source {
static void registerNative(jni::JNIEnv&);

VectorSource(jni::JNIEnv&, const jni::String&, const jni::Object<>&);
VectorSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend&);
VectorSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend*);
~VectorSource();

private:
Expand Down

0 comments on commit 2b67ce7

Please sign in to comment.