diff options
author | Łukasz Paczos <lukasz.paczos@mapbox.com> | 2018-09-10 20:59:49 +0200 |
---|---|---|
committer | Łukasz Paczos <lukasz.paczos@mapbox.com> | 2018-09-26 13:47:28 +0200 |
commit | 4148a5a91aefef20f28e520d1c0d4b6485cf0234 (patch) | |
tree | 0022bb210f3789c506d4f2b0f6a841ae4308f5a2 /platform/android/src | |
parent | f1a094700719f4ab6edb789c977f4b522d1ddc95 (diff) | |
download | qtlocation-mapboxgl-4148a5a91aefef20f28e520d1c0d4b6485cf0234.tar.gz |
[android] expose offline database merge API
Diffstat (limited to 'platform/android/src')
-rw-r--r-- | platform/android/src/offline/offline_manager.cpp | 72 | ||||
-rw-r--r-- | platform/android/src/offline/offline_manager.hpp | 22 |
2 files changed, 84 insertions, 10 deletions
diff --git a/platform/android/src/offline/offline_manager.cpp b/platform/android/src/offline/offline_manager.cpp index b27af8bdae..c6432f766a 100644 --- a/platform/android/src/offline/offline_manager.cpp +++ b/platform/android/src/offline/offline_manager.cpp @@ -34,7 +34,7 @@ void OfflineManager::listOfflineRegions(jni::JNIEnv& env_, const jni::Object<Fil if (regions) { OfflineManager::ListOfflineRegionsCallback::onList( - *env, *jFileSource, *callback, std::move(*regions)); + *env, *jFileSource, *callback, *regions); } else { OfflineManager::ListOfflineRegionsCallback::onError( *env, *callback, regions.error()); @@ -70,7 +70,7 @@ void OfflineManager::createOfflineRegion(jni::JNIEnv& env_, if (region) { OfflineManager::CreateOfflineRegionCallback::onCreate( - *env, *jFileSource, *callback, std::move(*region) + *env, *jFileSource, *callback, *region ); } else { OfflineManager::CreateOfflineRegionCallback::onError( @@ -79,9 +79,36 @@ void OfflineManager::createOfflineRegion(jni::JNIEnv& env_, }); } +void OfflineManager::mergeOfflineRegions(jni::JNIEnv& env_, const jni::Object<FileSource>& jFileSource_, + const jni::String& jString_, + const jni::Object<MergeOfflineRegionsCallback>& callback_) { + auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); + auto globalFilesource = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, jFileSource_); + + auto path = jni::Make<std::string>(env_, jString_); + fileSource.mergeOfflineRegions(path, [ + //Keep a shared ptr to a global reference of the callback and file source so they are not GC'd in the meanwhile + callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)), + jFileSource = std::make_shared<decltype(globalFilesource)>(std::move(globalFilesource)) + ](mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> regions) mutable { + + // Reattach, the callback comes from a different thread + android::UniqueEnv env = android::AttachEnv(); + + if (regions) { + OfflineManager::MergeOfflineRegionsCallback::onMerge( + *env, *jFileSource, *callback, *regions); + } else { + OfflineManager::MergeOfflineRegionsCallback::onError( + *env, *callback, regions.error()); + } + }); +} + void OfflineManager::registerNative(jni::JNIEnv& env) { jni::Class<ListOfflineRegionsCallback>::Singleton(env); jni::Class<CreateOfflineRegionCallback>::Singleton(env); + jni::Class<MergeOfflineRegionsCallback>::Singleton(env); static auto& javaClass = jni::Class<OfflineManager>::Singleton(env); @@ -93,7 +120,8 @@ void OfflineManager::registerNative(jni::JNIEnv& env) { "finalize", METHOD(&OfflineManager::setOfflineMapboxTileCountLimit, "setOfflineMapboxTileCountLimit"), METHOD(&OfflineManager::listOfflineRegions, "listOfflineRegions"), - METHOD(&OfflineManager::createOfflineRegion, "createOfflineRegion")); + METHOD(&OfflineManager::createOfflineRegion, "createOfflineRegion"), + METHOD(&OfflineManager::mergeOfflineRegions, "mergeOfflineRegions")); } // OfflineManager::ListOfflineRegionsCallback // @@ -110,13 +138,13 @@ void OfflineManager::ListOfflineRegionsCallback::onError(jni::JNIEnv& env, void OfflineManager::ListOfflineRegionsCallback::onList(jni::JNIEnv& env, const jni::Object<FileSource>& jFileSource, const jni::Object<OfflineManager::ListOfflineRegionsCallback>& callback, - mbgl::optional<std::vector<mbgl::OfflineRegion>> regions) { + mbgl::OfflineRegions& regions) { static auto& javaClass = jni::Class<OfflineManager::ListOfflineRegionsCallback>::Singleton(env); static auto method = javaClass.GetMethod<void (jni::Array<jni::Object<OfflineRegion>>)>(env, "onList"); std::size_t index = 0; - auto jregions = jni::Array<jni::Object<OfflineRegion>>::New(env, regions->size()); - for (auto& region : *regions) { + auto jregions = jni::Array<jni::Object<OfflineRegion>>::New(env, regions.size()); + for (auto& region : regions) { jregions.Set(env, index, OfflineRegion::New(env, jFileSource, std::move(region))); index++; } @@ -138,11 +166,39 @@ void OfflineManager::CreateOfflineRegionCallback::onError(jni::JNIEnv& env, void OfflineManager::CreateOfflineRegionCallback::onCreate(jni::JNIEnv& env, const jni::Object<FileSource>& jFileSource, const jni::Object<OfflineManager::CreateOfflineRegionCallback>& callback, - mbgl::optional<mbgl::OfflineRegion> region) { + mbgl::OfflineRegion& region) { static auto& javaClass = jni::Class<OfflineManager::CreateOfflineRegionCallback>::Singleton(env); static auto method = javaClass.GetMethod<void (jni::Object<OfflineRegion>)>(env, "onCreate"); - callback.Call(env, method, OfflineRegion::New(env, jFileSource, std::move(*region))); + callback.Call(env, method, OfflineRegion::New(env, jFileSource, std::move(region))); +} + +// OfflineManager::MergeOfflineRegionsCallback // + +void OfflineManager::MergeOfflineRegionsCallback::onError(jni::JNIEnv& env, + const jni::Object<OfflineManager::MergeOfflineRegionsCallback>& callback, + std::exception_ptr error) { + static auto& javaClass = jni::Class<OfflineManager::MergeOfflineRegionsCallback>::Singleton(env); + static auto method = javaClass.GetMethod<void (jni::String)>(env, "onError"); + + callback.Call(env, method, jni::Make<jni::String>(env, mbgl::util::toString(error))); +} + +void OfflineManager::MergeOfflineRegionsCallback::onMerge(jni::JNIEnv& env, + const jni::Object<FileSource>& jFileSource, + const jni::Object<MergeOfflineRegionsCallback>& callback, + mbgl::OfflineRegions& regions) { + static auto& javaClass = jni::Class<OfflineManager::MergeOfflineRegionsCallback>::Singleton(env); + static auto method = javaClass.GetMethod<void (jni::Array<jni::Object<OfflineRegion>>)>(env, "onMerge"); + + std::size_t index = 0; + auto jregions = jni::Array<jni::Object<OfflineRegion>>::New(env, regions.size()); + for (auto& region : regions) { + jregions.Set(env, index, OfflineRegion::New(env, jFileSource, std::move(region))); + index++; + } + + callback.Call(env, method, jregions); } } // namespace android diff --git a/platform/android/src/offline/offline_manager.hpp b/platform/android/src/offline/offline_manager.hpp index 21ca5ca9c1..b2ebc63a63 100644 --- a/platform/android/src/offline/offline_manager.hpp +++ b/platform/android/src/offline/offline_manager.hpp @@ -8,6 +8,7 @@ #include "../file_source.hpp" #include "offline_region.hpp" #include "offline_region_definition.hpp" +#include "../java_types.hpp" namespace mbgl { @@ -25,7 +26,7 @@ public: static void onList(jni::JNIEnv&, const jni::Object<FileSource>&, const jni::Object<OfflineManager::ListOfflineRegionsCallback>&, - mbgl::optional<std::vector<mbgl::OfflineRegion>>); + mbgl::OfflineRegions&); }; class CreateOfflineRegionCallback { @@ -37,7 +38,19 @@ public: static void onCreate(jni::JNIEnv&, const jni::Object<FileSource>&, const jni::Object<OfflineManager::CreateOfflineRegionCallback>&, - mbgl::optional<mbgl::OfflineRegion>); + mbgl::OfflineRegion&); + }; + + class MergeOfflineRegionsCallback { + public: + static constexpr auto Name() { return "com/mapbox/mapboxsdk/offline/OfflineManager$MergeOfflineRegionsCallback";} + + static void onError(jni::JNIEnv&, const jni::Object<OfflineManager::MergeOfflineRegionsCallback>&, std::exception_ptr); + + static void onMerge(jni::JNIEnv&, + const jni::Object<FileSource>&, + const jni::Object<MergeOfflineRegionsCallback>&, + mbgl::OfflineRegions&); }; static constexpr auto Name() { return "com/mapbox/mapboxsdk/offline/OfflineManager"; }; @@ -57,6 +70,11 @@ public: const jni::Array<jni::jbyte>& metadata, const jni::Object<OfflineManager::CreateOfflineRegionCallback>& callback); + void mergeOfflineRegions(jni::JNIEnv&, + const jni::Object<FileSource>&, + const jni::String&, + const jni::Object<MergeOfflineRegionsCallback>&); + private: mbgl::DefaultFileSource& fileSource; }; |