Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] #3891 - implement offline region definition
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Zugaldia committed Feb 20, 2016
1 parent 4d4cf12 commit 8446964
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public void onList(OfflineRegion[] offlineRegions) {
// Get regions info
ArrayList<String> offlineRegionsNames = new ArrayList<>();
for (OfflineRegion offlineRegion : offlineRegions) {
offlineRegionsNames.add("TODO");
offlineRegionsNames.add("Region " + offlineRegion.getID());
}

// Create args
Expand Down Expand Up @@ -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);

Expand Down
68 changes: 65 additions & 3 deletions platform/android/src/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1744,11 +1750,25 @@ void JNICALL createOfflineRegion(JNIEnv *env, jobject obj, jlong defaultFileSour
return;
}

// Definition fields
jstring jStyleURL = reinterpret_cast<jstring>(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<uint8_t>)
mbgl::OfflineRegionMetadata metadata;

// We need to add the corresponding DeleteGlobalRef
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions platform/android/src/jni.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 8446964

Please sign in to comment.