From ccc57aed20f1d9cd0dc1f5151f052c52e1aa8ffc Mon Sep 17 00:00:00 2001 From: Asheem Mamoowala Date: Mon, 13 Nov 2017 16:06:26 -0800 Subject: [android] Use inherited javaPeer in CustomGeometrySource C++ peer --- .../style/sources/CustomGeometrySource.java | 6 ++-- .../src/style/sources/custom_geometry_source.cpp | 41 +++++++++++++++------- .../src/style/sources/custom_geometry_source.hpp | 6 +--- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/CustomGeometrySource.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/CustomGeometrySource.java index e10be1d4ba..1ff1eeacc3 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/CustomGeometrySource.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/CustomGeometrySource.java @@ -51,7 +51,7 @@ public class CustomGeometrySource extends Source { public CustomGeometrySource(String id, GeometryTileProvider provider, GeoJsonOptions options) { this.provider = provider; executor = Executors.newFixedThreadPool(4); - initialize(this, id, options); + initialize(id, options); } /** @@ -103,7 +103,7 @@ public class CustomGeometrySource extends Source { return features != null ? Arrays.asList(features) : new ArrayList(); } - protected native void initialize(CustomGeometrySource self, String sourceId, Object options); + protected native void initialize(String sourceId, Object options); private native Feature[] querySourceFeatures(Object[] filter); @@ -127,7 +127,7 @@ public class CustomGeometrySource extends Source { 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 diff --git a/platform/android/src/style/sources/custom_geometry_source.cpp b/platform/android/src/style/sources/custom_geometry_source.cpp index 205fe55ed9..a60b962e45 100644 --- a/platform/android/src/style/sources/custom_geometry_source.cpp +++ b/platform/android/src/style/sources/custom_geometry_source.cpp @@ -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" @@ -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(); @@ -36,14 +38,14 @@ namespace android { return *result; } - CustomGeometrySource::CustomGeometrySource(jni::JNIEnv& env, jni::Object _obj, jni::String sourceId, jni::Object<> options) + CustomGeometrySource::CustomGeometrySource(jni::JNIEnv& env, + jni::String sourceId, + jni::Object<> options) : Source(env, std::make_unique( - jni::Make(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(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, @@ -56,19 +58,31 @@ namespace android { void CustomGeometrySource::fetchTile (const mbgl::CanonicalTileID& tileID) { android::UniqueEnv _env = android::AttachEnv(); + static auto fetchTile = javaClass.GetMethod(*_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(*_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 jFeatures) { + void CustomGeometrySource::setTileData(jni::JNIEnv& env, + jni::jint z, + jni::jint x, + jni::jint y, + jni::Object jFeatures) { using namespace mbgl::android::geojson; // Convert the jni object @@ -81,6 +95,7 @@ namespace android { void CustomGeometrySource::invalidateTile(jni::JNIEnv&, jni::jint z, jni::jint x, jni::jint y) { source.as()->CustomGeometrySource::invalidateTile(CanonicalTileID(z, x, y)); } + void CustomGeometrySource::invalidateBounds(jni::JNIEnv& env, jni::Object jBounds) { auto bounds = LatLngBounds::getLatLngBounds(env, jBounds); source.as()->CustomGeometrySource::invalidateRegion(bounds); @@ -114,7 +129,7 @@ namespace android { // Register the peer jni::RegisterNativePeer( env, CustomGeometrySource::javaClass, "nativePtr", - std::make_unique, jni::String, jni::Object<>>, + std::make_unique>, "initialize", "finalize", METHOD(&CustomGeometrySource::querySourceFeatures, "querySourceFeatures"), diff --git a/platform/android/src/style/sources/custom_geometry_source.hpp b/platform/android/src/style/sources/custom_geometry_source.hpp index 00d8dc918f..1dc1c07b4f 100644 --- a/platform/android/src/style/sources/custom_geometry_source.hpp +++ b/platform/android/src/style/sources/custom_geometry_source.hpp @@ -22,10 +22,7 @@ public: static void registerNative(jni::JNIEnv&); - CustomGeometrySource(jni::JNIEnv&, - jni::Object, - jni::String, - jni::Object<>); + CustomGeometrySource(jni::JNIEnv&, jni::String, jni::Object<>); CustomGeometrySource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend&); @@ -44,7 +41,6 @@ public: private: jni::Object createJavaPeer(jni::JNIEnv&); - jni::UniqueObject javaPeer; }; // class CustomGeometrySource } // namespace android -- cgit v1.2.1