summaryrefslogtreecommitdiff
path: root/platform/android/src/style/sources/raster_source.cpp
blob: 33223a5b6909391635dbf3dcc5b8af1314d371d1 (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
62
#include "raster_source.hpp"

#include "../android_conversion.hpp"
#include "../value.hpp"
#include "../conversion/url_or_tileset.hpp"

#include <mbgl/util/variant.hpp>

#include <string>

namespace mbgl {
namespace android {

    RasterSource::RasterSource(jni::JNIEnv& env, jni::String sourceId, jni::Object<> urlOrTileSet, jni::jint tileSize)
        : Source(
            env,
            std::make_unique<mbgl::style::RasterSource>(
                jni::Make<std::string>(env, sourceId),
                convertURLOrTileset(Value(env, urlOrTileSet)),
                tileSize
            )
        ) {
    }

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

    RasterSource::~RasterSource() = default;

    jni::String RasterSource::getURL(jni::JNIEnv& env) {
        optional<std::string> url = source.as<mbgl::style::RasterSource>()->RasterSource::getURL();
        return url ? jni::Make<jni::String>(env, *url) : jni::String();
    }

    jni::Class<RasterSource> RasterSource::javaClass;

    jni::Object<Source> RasterSource::createJavaPeer(jni::JNIEnv& env) {
        static auto constructor = RasterSource::javaClass.template GetConstructor<jni::jlong>(env);
        return jni::Object<Source>(RasterSource::javaClass.New(env, constructor, reinterpret_cast<jni::jlong>(this)).Get());
    }

    void RasterSource::registerNative(jni::JNIEnv& env) {
        // Lookup the class
        RasterSource::javaClass = *jni::Class<RasterSource>::Find(env).NewGlobalRef(env).release();

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

        // Register the peer
        jni::RegisterNativePeer<RasterSource>(
            env, RasterSource::javaClass, "nativePtr",
            std::make_unique<RasterSource, JNIEnv&, jni::String, jni::Object<>, jni::jint>,
            "initialize",
            "finalize",
            METHOD(&RasterSource::getURL, "nativeGetUrl")
        );
    }

} // namespace android
} // namespace mbgl