summaryrefslogtreecommitdiff
path: root/platform/android/src/style/layers/custom_layer.cpp
blob: 5a8b239a8d51388de127c9baaf60b57259e26165 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "custom_layer.hpp"

#include <string>

#include <mbgl/util/logging.hpp>

namespace mbgl {
namespace android {

    CustomLayer::CustomLayer(jni::JNIEnv& env, const jni::String& layerId, jni::jlong host)
        : Layer(std::make_unique<mbgl::style::CustomLayer>(
                jni::Make<std::string>(env, layerId),
                std::unique_ptr<mbgl::style::CustomLayerHost>(reinterpret_cast<mbgl::style::CustomLayerHost*>(host)))
                ) {
    }

    CustomLayer::CustomLayer(mbgl::style::CustomLayer& coreLayer) : Layer(coreLayer) {}

    CustomLayer::CustomLayer(std::unique_ptr<mbgl::style::CustomLayer> coreLayer) : Layer(std::move(coreLayer)) {}

    CustomLayer::~CustomLayer() = default;

    namespace {
        jni::Local<jni::Object<Layer>> createJavaPeer(jni::JNIEnv& env, Layer* layer) {
            static auto& javaClass = jni::Class<CustomLayer>::Singleton(env);
            static auto constructor = javaClass.GetConstructor<jni::jlong>(env);
            return javaClass.New(env, constructor, reinterpret_cast<jni::jlong>(layer));
        }
    }

    CustomJavaLayerPeerFactory::~CustomJavaLayerPeerFactory() = default;

    jni::Local<jni::Object<Layer>> CustomJavaLayerPeerFactory::createJavaLayerPeer(jni::JNIEnv& env,
                                                                                   mbgl::style::Layer& layer) {
        return createJavaPeer(env, new CustomLayer(static_cast<mbgl::style::CustomLayer&>(layer)));
    }

    jni::Local<jni::Object<Layer>> CustomJavaLayerPeerFactory::createJavaLayerPeer(
        jni::JNIEnv& env, std::unique_ptr<mbgl::style::Layer> layer) {
        return createJavaPeer(env,
                              new CustomLayer(std::unique_ptr<mbgl::style::CustomLayer>(
                                  static_cast<mbgl::style::CustomLayer*>(layer.release()))));
    }

    void CustomJavaLayerPeerFactory::registerNative(jni::JNIEnv& env) {
        // Lookup the class
        static auto& javaClass = jni::Class<CustomLayer>::Singleton(env);

        #define METHOD(MethodPtr, name) jni::MakeNativePeerMethod<decltype(MethodPtr), (MethodPtr)>(name)

        // Register the peer
        jni::RegisterNativePeer<CustomLayer>(env,
                                             javaClass,
                                             "nativePtr",
                                             jni::MakePeer<CustomLayer, const jni::String&, jni::jlong>,
                                             "initialize",
                                             "finalize");
    }

} // namespace android
} // namespace mbgl