[android] jni.hpp 4.0.0
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "source.hpp"
|
||||
#include "../android_conversion.hpp"
|
||||
#include "../../attach_env.hpp"
|
||||
|
||||
#include <jni/jni.hpp>
|
||||
|
||||
@@ -47,16 +48,16 @@ namespace android {
|
||||
}
|
||||
}
|
||||
|
||||
jni::Object<Source> Source::peerForCoreSource(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend& 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);
|
||||
}
|
||||
return *coreSource.peer.get<std::unique_ptr<Source>>()->javaPeer;
|
||||
return coreSource.peer.get<std::unique_ptr<Source>>()->javaPeer;
|
||||
}
|
||||
|
||||
Source::Source(jni::JNIEnv& env, mbgl::style::Source& coreSource, jni::Object<Source> obj, AndroidRendererFrontend& frontend)
|
||||
Source::Source(jni::JNIEnv& env, mbgl::style::Source& coreSource, const jni::Object<Source>& obj, AndroidRendererFrontend& frontend)
|
||||
: source(coreSource)
|
||||
, javaPeer(obj.NewGlobalRef(env))
|
||||
, javaPeer(jni::NewGlobal(env, obj))
|
||||
, rendererFrontend(&frontend) {
|
||||
}
|
||||
|
||||
@@ -76,23 +77,23 @@ namespace android {
|
||||
if (ownedSource.get() == nullptr && javaPeer.get() != nullptr) {
|
||||
// Manually clear the java peer
|
||||
android::UniqueEnv env = android::AttachEnv();
|
||||
static auto javaClass = jni::Class<Source>::Singleton(*env);
|
||||
static auto& javaClass = jni::Class<Source>::Singleton(*env);
|
||||
static auto nativePtrField = javaClass.GetField<jlong>(*env, "nativePtr");
|
||||
javaPeer->Set(*env, nativePtrField, (jlong) 0);
|
||||
javaPeer.Set(*env, nativePtrField, (jlong) 0);
|
||||
javaPeer.reset();
|
||||
}
|
||||
}
|
||||
|
||||
jni::String Source::getId(jni::JNIEnv& env) {
|
||||
jni::Local<jni::String> Source::getId(jni::JNIEnv& env) {
|
||||
return jni::Make<jni::String>(env, source.getID());
|
||||
}
|
||||
|
||||
jni::String Source::getAttribution(jni::JNIEnv& env) {
|
||||
jni::Local<jni::String> Source::getAttribution(jni::JNIEnv& env) {
|
||||
auto attribution = source.getAttribution();
|
||||
return attribution ? jni::Make<jni::String>(env, attribution.value()) : jni::Make<jni::String>(env,"");
|
||||
}
|
||||
|
||||
void Source::addToMap(JNIEnv& env, jni::Object<Source> obj, mbgl::Map& map, AndroidRendererFrontend& frontend) {
|
||||
void Source::addToMap(JNIEnv& env, const jni::Object<Source>& obj, mbgl::Map& map, AndroidRendererFrontend& frontend) {
|
||||
// Check to see if we own the source first
|
||||
if (!ownedSource) {
|
||||
throw std::runtime_error("Cannot add source twice");
|
||||
@@ -105,12 +106,12 @@ namespace android {
|
||||
source.peer = std::unique_ptr<Source>(this);
|
||||
|
||||
// Add strong reference to java source
|
||||
javaPeer = obj.NewGlobalRef(env);
|
||||
javaPeer = jni::NewGlobal(env, obj);
|
||||
|
||||
rendererFrontend = &frontend;
|
||||
}
|
||||
|
||||
bool Source::removeFromMap(JNIEnv&, jni::Object<Source>, mbgl::Map& map) {
|
||||
bool Source::removeFromMap(JNIEnv&, const jni::Object<Source>&, mbgl::Map& map) {
|
||||
// Cannot remove if not attached yet
|
||||
if (ownedSource) {
|
||||
throw std::runtime_error("Cannot remove detached source");
|
||||
@@ -143,7 +144,7 @@ namespace android {
|
||||
|
||||
void Source::registerNative(jni::JNIEnv& env) {
|
||||
// Lookup the class
|
||||
static auto javaClass = jni::Class<Source>::Singleton(env);
|
||||
static auto& javaClass = jni::Class<Source>::Singleton(env);
|
||||
|
||||
#define METHOD(MethodPtr, name) jni::MakeNativePeerMethod<decltype(MethodPtr), (MethodPtr)>(name)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user