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

Commit

Permalink
[android] Use inherited javaPeer in CustomGeometrySource C++ peer
Browse files Browse the repository at this point in the history
  • Loading branch information
Asheem Mamoowala committed Nov 22, 2017
1 parent f06ebcc commit e2702c7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public CustomGeometrySource(String id, GeometryTileProvider provider) {
public CustomGeometrySource(String id, GeometryTileProvider provider, GeoJsonOptions options) {
this.provider = provider;
executor = Executors.newFixedThreadPool(4);
initialize(this, id, options);
initialize(id, options);
}

/**
Expand Down Expand Up @@ -103,7 +103,7 @@ public List<Feature> querySourceFeatures(@Nullable Filter.Statement filter) {
return features != null ? Arrays.asList(features) : new ArrayList<Feature>();
}

protected native void initialize(CustomGeometrySource self, String sourceId, Object options);
protected native void initialize(String sourceId, Object options);

private native Feature[] querySourceFeatures(Object[] filter);

Expand All @@ -127,7 +127,7 @@ private void fetchTile(int z, int x, int y) {
TileID tileID = new TileID(z, x, y);
cancelledTileRequests.put(tileID, cancelFlag);
GeometryTileRequest request = new GeometryTileRequest(tileID, provider, this, cancelFlag);
executor.submit(request);
executor.execute(request);
}

@WorkerThread
Expand Down
41 changes: 28 additions & 13 deletions platform/android/src/style/sources/custom_geometry_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Java -> C++ conversion
#include "../android_conversion.hpp"
#include "../conversion/filter.hpp"
//#include "../conversion/geojson.hpp"

// C++ -> Java conversion
#include "../../conversion/conversion.hpp"
Expand All @@ -21,7 +20,10 @@ namespace android {
// This conversion is expected not to fail because it's used only in contexts where
// the value was originally a GeoJsonOptions object on the Java side. If it fails
// to convert, it's a bug in our serialization or Java-side static typing.
static style::CustomGeometrySource::Options convertCustomGeometrySourceOptions(jni::JNIEnv& env, jni::Object<> options, style::TileFunction fetchFn, style::TileFunction cancelFn) {
static style::CustomGeometrySource::Options convertCustomGeometrySourceOptions(jni::JNIEnv& env,
jni::Object<> options,
style::TileFunction fetchFn,
style::TileFunction cancelFn) {
using namespace mbgl::style::conversion;
if (!options) {
return style::CustomGeometrySource::Options();
Expand All @@ -36,14 +38,14 @@ namespace android {
return *result;
}

CustomGeometrySource::CustomGeometrySource(jni::JNIEnv& env, jni::Object<CustomGeometrySource> _obj, jni::String sourceId, jni::Object<> options)
CustomGeometrySource::CustomGeometrySource(jni::JNIEnv& env,
jni::String sourceId,
jni::Object<> options)
: Source(env, std::make_unique<mbgl::style::CustomGeometrySource>(
jni::Make<std::string>(env, sourceId),
convertCustomGeometrySourceOptions(env,
options,
std::bind(&CustomGeometrySource::fetchTile, this, std::placeholders::_1),
std::bind(&CustomGeometrySource::cancelTile, this, std::placeholders::_1))) ),
javaPeer(_obj.NewGlobalRef(env)) {
jni::Make<std::string>(env, sourceId),
convertCustomGeometrySourceOptions(env, options,
std::bind(&CustomGeometrySource::fetchTile, this, std::placeholders::_1),
std::bind(&CustomGeometrySource::cancelTile, this, std::placeholders::_1)))) {
}

CustomGeometrySource::CustomGeometrySource(jni::JNIEnv& env,
Expand All @@ -56,19 +58,31 @@ namespace android {

void CustomGeometrySource::fetchTile (const mbgl::CanonicalTileID& tileID) {
android::UniqueEnv _env = android::AttachEnv();

static auto fetchTile = javaClass.GetMethod<void (jni::jint, jni::jint, jni::jint)>(*_env, "fetchTile");

assert(javaPeer);
javaPeer->Call(*_env, fetchTile, (int)tileID.z, (int)tileID.x, (int)tileID.y);

auto peer = jni::Cast(*_env, *javaPeer, javaClass);
peer.Call(*_env, fetchTile, (int)tileID.z, (int)tileID.x, (int)tileID.y);
};

void CustomGeometrySource::cancelTile(const mbgl::CanonicalTileID& tileID) {
android::UniqueEnv _env = android::AttachEnv();

static auto cancelTile = javaClass.GetMethod<void (jni::jint, jni::jint, jni::jint)>(*_env, "cancelTile");

assert(javaPeer);
javaPeer->Call(*_env, cancelTile, (int)tileID.z, (int)tileID.x, (int)tileID.y);

auto peer = jni::Cast(*_env, *javaPeer, javaClass);
peer.Call(*_env, cancelTile, (int)tileID.z, (int)tileID.x, (int)tileID.y);
};

void CustomGeometrySource::setTileData(jni::JNIEnv& env, jni::jint z, jni::jint x, jni::jint y, jni::Object<geojson::FeatureCollection> jFeatures) {
void CustomGeometrySource::setTileData(jni::JNIEnv& env,
jni::jint z,
jni::jint x,
jni::jint y,
jni::Object<geojson::FeatureCollection> jFeatures) {
using namespace mbgl::android::geojson;

// Convert the jni object
Expand All @@ -81,6 +95,7 @@ namespace android {
void CustomGeometrySource::invalidateTile(jni::JNIEnv&, jni::jint z, jni::jint x, jni::jint y) {
source.as<mbgl::style::CustomGeometrySource>()->CustomGeometrySource::invalidateTile(CanonicalTileID(z, x, y));
}

void CustomGeometrySource::invalidateBounds(jni::JNIEnv& env, jni::Object<LatLngBounds> jBounds) {
auto bounds = LatLngBounds::getLatLngBounds(env, jBounds);
source.as<mbgl::style::CustomGeometrySource>()->CustomGeometrySource::invalidateRegion(bounds);
Expand Down Expand Up @@ -114,7 +129,7 @@ namespace android {
// Register the peer
jni::RegisterNativePeer<CustomGeometrySource>(
env, CustomGeometrySource::javaClass, "nativePtr",
std::make_unique<CustomGeometrySource, JNIEnv&, jni::Object<CustomGeometrySource>, jni::String, jni::Object<>>,
std::make_unique<CustomGeometrySource, JNIEnv&, jni::String, jni::Object<>>,
"initialize",
"finalize",
METHOD(&CustomGeometrySource::querySourceFeatures, "querySourceFeatures"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ class CustomGeometrySource : public Source {

static void registerNative(jni::JNIEnv&);

CustomGeometrySource(jni::JNIEnv&,
jni::Object<CustomGeometrySource>,
jni::String,
jni::Object<>);
CustomGeometrySource(jni::JNIEnv&, jni::String, jni::Object<>);

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

Expand All @@ -44,7 +41,6 @@ class CustomGeometrySource : public Source {
private:
jni::Object<Source> createJavaPeer(jni::JNIEnv&);

jni::UniqueObject<CustomGeometrySource> javaPeer;
}; // class CustomGeometrySource

} // namespace android
Expand Down

0 comments on commit e2702c7

Please sign in to comment.