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, getStyle and Observer interface for snapsh…
Browse files Browse the repository at this point in the history
…otter

So that users can modify properties of an existing layer / source objects
  • Loading branch information
alexshalamov committed Mar 25, 2020
1 parent 77d95d2 commit 60bf1ae
Show file tree
Hide file tree
Showing 18 changed files with 58 additions and 20 deletions.
28 changes: 28 additions & 0 deletions platform/android/src/snapshotter/map_snapshotter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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

namespace mbgl {
namespace android {
Expand Down Expand Up @@ -282,6 +283,31 @@ 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 +340,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
4 changes: 3 additions & 1 deletion platform/android/src/snapshotter/map_snapshotter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ 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
// MapSnapshotterObserver overrides
void onDidFailLoadingStyle(const std::string&) override;
void onDidFinishLoadingStyle() override;
void onStyleImageMissing(const std::string&) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace android {

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

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
2 changes: 1 addition & 1 deletion platform/android/src/style/sources/image_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace android {

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

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
2 changes: 1 addition & 1 deletion platform/android/src/style/sources/raster_dem_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace android {

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

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
2 changes: 1 addition & 1 deletion platform/android/src/style/sources/raster_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace android {

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

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
15 changes: 11 additions & 4 deletions platform/android/src/style/sources/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
namespace mbgl {
namespace android {

static std::unique_ptr<Source> createSourcePeer(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend& 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>()) {
Expand All @@ -50,15 +50,22 @@ namespace android {

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)
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) {
, rendererFrontend(frontend) {
}

Source::Source(jni::JNIEnv&, std::unique_ptr<mbgl::style::Source> coreSource)
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
2 changes: 1 addition & 1 deletion platform/android/src/style/sources/unknown_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace android {

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

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
2 changes: 1 addition & 1 deletion platform/android/src/style/sources/vector_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace android {

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

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 60bf1ae

Please sign in to comment.