summaryrefslogtreecommitdiff
path: root/platform/android/src/style/sources/image_source.cpp
blob: 9909675f44b6e1c07303cae64b1d5b49304159d9 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "image_source.hpp"

// Java -> C++ conversion
#include "../android_conversion.hpp"

// C++ -> Java conversion
#include "../../conversion/conversion.hpp"
#include <mbgl/style/conversion.hpp>

#include <string>

namespace mbgl {
namespace android {

    ImageSource::ImageSource(jni::JNIEnv& env, jni::String sourceId)
        : Source(env, std::make_unique<mbgl::style::ImageSource>(
                jni::Make<std::string>(env, sourceId)
                )
            ) {
    }

    ImageSource::ImageSource(mbgl::Map& map, mbgl::style::ImageSource& coreSource)
        : Source(map, coreSource) {
    }

    ImageSource::~ImageSource() = default;

    void GeoJSONSource::setGeoJSONString(jni::JNIEnv& env, jni::String json) {
        using namespace mbgl::style::conversion;

        // Convert the jni object
        Error error;
        optional<GeoJSON> converted = convert<GeoJSON>(Value(env, json), error);
        if(!converted) {
            mbgl::Log::Error(mbgl::Event::JNI, "Error setting geo json: " + error.message);
            return;
        }

        // Update the core source
        source.as<mbgl::style::GeoJSONSource>()->GeoJSONSource::setGeoJSON(*converted);
    }

    void ImageSource::setURL(jni::JNIEnv& env, jni::String url) {
        // Update the core source
        source.as<mbgl::style::GeoJSONSource>()->GeoJSONSource::setURL(jni::Make<std::string>(env, url));
    }

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


    jni::Class<ImageSource> ImageSource::javaClass;

    jni::jobject* ImageSource::createJavaPeer(jni::JNIEnv& env) {
        static auto constructor = ImageSource::javaClass.template GetConstructor<jni::jlong>(env);
        return ImageSource::javaClass.New(env, constructor, reinterpret_cast<jni::jlong>(this));
    }

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

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

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

} // namespace android
} // namespace mbgl