diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/OfflineActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/OfflineActivity.java index ad8c2525818..753bbf7cb99 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/OfflineActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/OfflineActivity.java @@ -204,7 +204,7 @@ public void onList(OfflineRegion[] offlineRegions) { // Get regions info ArrayList offlineRegionsNames = new ArrayList<>(); for (OfflineRegion offlineRegion : offlineRegions) { - offlineRegionsNames.add("TODO"); + offlineRegionsNames.add("Region " + offlineRegion.getID()); } // Create args @@ -238,9 +238,9 @@ public void onDownloadRegionDialogPositiveClick(String regionName) { // Definition String styleURL = mMapboxMap.getStyleUrl(); LatLngBounds bounds = mMapboxMap.getProjection().getVisibleRegion().latLngBounds; - double minZoom = mMapboxMap.getMinZoom(); + double minZoom = mMapView.getZoom(); double maxZoom = mMapboxMap.getMaxZoom(); - float pixelRatio = 1.0f; + float pixelRatio = this.getResources().getDisplayMetrics().density; OfflineRegionDefinition definition = new OfflineRegionDefinition( styleURL, bounds, minZoom, maxZoom, pixelRatio); diff --git a/platform/android/src/jni.cpp b/platform/android/src/jni.cpp index e2bb5fce67f..63781f06b0f 100755 --- a/platform/android/src/jni.cpp +++ b/platform/android/src/jni.cpp @@ -134,7 +134,13 @@ jfieldID offlineRegionDefinitionId = nullptr; jfieldID offlineRegionMetadataId = nullptr; jfieldID offlineRegionPtrId = nullptr; jclass offlineRegionDefinitionClass = nullptr; +jfieldID offlineRegionDefinitionStyleURLId = nullptr; +jfieldID offlineRegionDefinitionBoundsId = nullptr; +jfieldID offlineRegionDefinitionMinZoomId = nullptr; +jfieldID offlineRegionDefinitionMaxZoomId = nullptr; +jfieldID offlineRegionDefinitionPixelRatioId = nullptr; jclass offlineRegionMetadataClass = nullptr; +jfieldID offlineRegionMetadataMetadataId = nullptr; jclass createOfflineRegionCallbackClass = nullptr; jmethodID createOnCreateMethodId = nullptr; jmethodID createOnErrorMethodId = nullptr; @@ -1744,11 +1750,25 @@ void JNICALL createOfflineRegion(JNIEnv *env, jobject obj, jlong defaultFileSour return; } + // Definition fields + jstring jStyleURL = reinterpret_cast(env->GetObjectField(definition_, offlineRegionDefinitionStyleURLId)); + std::string styleURL = std_string_from_jstring(env, jStyleURL); + jobject jBounds = env->GetObjectField(definition_, offlineRegionDefinitionBoundsId); + jdouble jMinZoom = env->GetDoubleField(definition_, offlineRegionDefinitionMinZoomId); + jdouble jMaxZoom = env->GetDoubleField(definition_, offlineRegionDefinitionMaxZoomId); + jfloat jPixelRatio = env->GetFloatField(definition_, offlineRegionDefinitionPixelRatioId); + + // Bounds fields + jdouble mLatNorth = env->GetDoubleField(jBounds, latLngBoundsLatNorthId); + jdouble mLatSouth = env->GetDoubleField(jBounds, latLngBoundsLatSouthId); + jdouble mLonEast = env->GetDoubleField(jBounds, latLngBoundsLonEastId); + jdouble mLonWest = env->GetDoubleField(jBounds, latLngBoundsLonWestId); + mbgl::LatLngBounds bounds = mbgl::LatLngBounds::hull({ mLatSouth, mLonWest }, { mLatNorth, mLonEast }); + // Definition - mbgl::LatLngBounds bayArea = mbgl::LatLngBounds::hull(mbgl::LatLng(37.2, -122.8), mbgl::LatLng(38.1, -121.7)); - mbgl::OfflineTilePyramidRegionDefinition definition("mapbox://styles/mapbox/streets-v8", bayArea, 0, 15, 1.0); + mbgl::OfflineTilePyramidRegionDefinition definition(styleURL, bounds, jMinZoom, jMaxZoom, jPixelRatio); - // Metadata + // Metadata (std::vector) mbgl::OfflineRegionMetadata metadata; // We need to add the corresponding DeleteGlobalRef @@ -2337,12 +2357,48 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { return JNI_ERR; } + offlineRegionDefinitionStyleURLId = env->GetFieldID(offlineRegionDefinitionClass, "styleURL", "Ljava/lang/String;"); + if (offlineRegionDefinitionStyleURLId == nullptr) { + env->ExceptionDescribe(); + return JNI_ERR; + } + + offlineRegionDefinitionBoundsId = env->GetFieldID(offlineRegionDefinitionClass, "bounds", "Lcom/mapbox/mapboxsdk/geometry/LatLngBounds;"); + if (offlineRegionDefinitionBoundsId == nullptr) { + env->ExceptionDescribe(); + return JNI_ERR; + } + + offlineRegionDefinitionMinZoomId = env->GetFieldID(offlineRegionDefinitionClass, "minZoom", "D"); + if (offlineRegionDefinitionMinZoomId == nullptr) { + env->ExceptionDescribe(); + return JNI_ERR; + } + + offlineRegionDefinitionMaxZoomId = env->GetFieldID(offlineRegionDefinitionClass, "maxZoom", "D"); + if (offlineRegionDefinitionMaxZoomId == nullptr) { + env->ExceptionDescribe(); + return JNI_ERR; + } + + offlineRegionDefinitionPixelRatioId = env->GetFieldID(offlineRegionDefinitionClass, "pixelRatio", "F"); + if (offlineRegionDefinitionPixelRatioId == nullptr) { + env->ExceptionDescribe(); + return JNI_ERR; + } + offlineRegionMetadataClass = env->FindClass("com/mapbox/mapboxsdk/offline/OfflineRegionMetadata"); if (offlineRegionMetadataClass == nullptr) { env->ExceptionDescribe(); return JNI_ERR; } + offlineRegionMetadataMetadataId = env->GetFieldID(offlineRegionDefinitionClass, "metadata", "[B"); + if (offlineRegionMetadataMetadataId == nullptr) { + env->ExceptionDescribe(); + return JNI_ERR; + } + createOfflineRegionCallbackClass = env->FindClass("com/mapbox/mapboxsdk/offline/OfflineManager$CreateOfflineRegionCallback"); if (createOfflineRegionCallbackClass == nullptr) { env->ExceptionDescribe(); @@ -2949,7 +3005,13 @@ extern "C" JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) { offlineRegionMetadataId = nullptr; offlineRegionPtrId = nullptr; env->DeleteGlobalRef(offlineRegionDefinitionClass); + offlineRegionDefinitionStyleURLId = nullptr; + offlineRegionDefinitionBoundsId = nullptr; + offlineRegionDefinitionMinZoomId = nullptr; + offlineRegionDefinitionMaxZoomId = nullptr; + offlineRegionDefinitionPixelRatioId = nullptr; env->DeleteGlobalRef(offlineRegionMetadataClass); + offlineRegionMetadataMetadataId = nullptr; env->DeleteGlobalRef(createOfflineRegionCallbackClass); createOnCreateMethodId = nullptr; createOnErrorMethodId = nullptr; diff --git a/platform/android/src/jni.hpp b/platform/android/src/jni.hpp index 6c3e94702ff..42dc03aa7ef 100644 --- a/platform/android/src/jni.hpp +++ b/platform/android/src/jni.hpp @@ -103,6 +103,8 @@ extern jclass httpRequestClass; extern jmethodID httpRequestStartId; extern jmethodID httpRequestCancelId; +// Offline declarations start + extern jclass offlineManagerClass; extern jmethodID offlineManagerClassConstructorId; extern jfieldID offlineManagerClassPtrId; @@ -116,11 +118,19 @@ extern jfieldID offlineRegionDefinitionId; extern jfieldID offlineRegionMetadataId; extern jfieldID offlineRegionPtrId; extern jclass offlineRegionDefinitionClass; +extern jfieldID offlineRegionDefinitionStyleURLId; +extern jfieldID offlineRegionDefinitionBoundsId; +extern jfieldID offlineRegionDefinitionMinZoomId; +extern jfieldID offlineRegionDefinitionMaxZoomId; +extern jfieldID offlineRegionDefinitionPixelRatioId; extern jclass offlineRegionMetadataClass; +extern jfieldID offlineRegionMetadataMetadataId; extern jclass createOfflineRegionCallbackClass; extern jmethodID createOnCreateMethodId; extern jmethodID createOnErrorMethodId; +// Offline declarations end + extern bool throw_jni_error(JNIEnv *env, const char *msg); extern bool attach_jni_thread(JavaVM* vm, JNIEnv** env, std::string threadName); extern void detach_jni_thread(JavaVM* vm, JNIEnv** env, bool detach);