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 api for setOfflineMapboxTileCountLimit an…
Browse files Browse the repository at this point in the history
…d mapboxTileCountLimitExceeded
  • Loading branch information
zugaldia committed Feb 25, 2016
1 parent e094d52 commit 9c96e26
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ public void run() {
});
}

/*
* Changing or bypassing this limit without permission from Mapbox is prohibited
* by the Mapbox Terms of Service.
*/
public void setOfflineMapboxTileCountLimit(long limit) {
setOfflineMapboxTileCountLimit(mDefaultFileSourcePtr, limit);
}


/*
* Native methods
*/
Expand All @@ -169,4 +178,7 @@ private native void createOfflineRegion(
long defaultFileSourcePtr, OfflineRegionDefinition definition,
OfflineRegionMetadata metadata, CreateOfflineRegionCallback callback);

private native void setOfflineMapboxTileCountLimit(
long defaultFileSourcePtr, long limit);

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ public interface OfflineRegionObserver {
* This method will be executed on the main thread.
*/
void onError(OfflineRegionError error);

/*
* Implement this method to be notified when the limit on the number of Mapbox
* tiles stored for offline regions has been reached.
*
* Once the limit has been reached, the SDK will not download further offline
* tiles from Mapbox APIs until existing tiles have been removed. Contact your
* Mapbox sales representative to raise the limit.
*
* This limit does not apply to non-Mapbox tile sources.
*
* This method will be executed on the main thread.
*/
void mapboxTileCountLimitExceeded(long limit);
}

/*
Expand Down Expand Up @@ -141,6 +155,16 @@ public void run() {
}
});
}

@Override
public void mapboxTileCountLimitExceeded(final long limit) {
getHandler().post(new Runnable() {
@Override
public void run() {
observer.mapboxTileCountLimitExceeded(limit);
}
});
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ public void onError(OfflineRegionError error) {
Log.e(LOG_TAG, "onError reason: " + error.getReason());
Log.e(LOG_TAG, "onError message: " + error.getMessage());
}

@Override
public void mapboxTileCountLimitExceeded(long limit) {
Log.e(LOG_TAG, "Mapbox tile count limit exceeded: " + limit);
}
});

// Change the region state
Expand Down
36 changes: 35 additions & 1 deletion platform/android/src/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ jmethodID createOnErrorMethodId = nullptr;
jclass offlineRegionObserverClass = nullptr;
jmethodID offlineRegionObserveronStatusChangedId = nullptr;
jmethodID offlineRegionObserveronErrorId = nullptr;
jmethodID offlineRegionObserveronLimitId = nullptr;

jclass offlineRegionStatusClass = nullptr;
jmethodID offlineRegionStatusConstructorId = nullptr;
Expand Down Expand Up @@ -1866,6 +1867,18 @@ void JNICALL createOfflineRegion(JNIEnv *env, jobject obj, jlong defaultFileSour
});
}

void JNICALL setOfflineMapboxTileCountLimit(JNIEnv *env, jobject obj, jlong defaultFileSourcePtr, jlong limit) {
mbgl::Log::Debug(mbgl::Event::JNI, "setOfflineMapboxTileCountLimit");

// Checks
assert(defaultFileSourcePtr != 0);
assert(limit > 0);

// Set limit
mbgl::DefaultFileSource *defaultFileSource = reinterpret_cast<mbgl::DefaultFileSource *>(defaultFileSourcePtr);
defaultFileSource->setOfflineMapboxTileCountLimit(limit);
}

void JNICALL setOfflineRegionObserver(JNIEnv *env, jobject obj, jobject offlineRegion_, jobject observerCallback) {
mbgl::Log::Debug(mbgl::Event::JNI, "setOfflineRegionObserver");

Expand Down Expand Up @@ -1931,6 +1944,19 @@ void JNICALL setOfflineRegionObserver(JNIEnv *env, jobject obj, jobject offlineR
detach_jni_thread(theJVM, &env2, renderDetach);
}

void mapboxTileCountLimitExceeded(uint64_t limit) override {
// Env
JNIEnv* env2;
jboolean renderDetach = attach_jni_thread(theJVM, &env2, "Offline Thread");

// Send limit
env2->CallVoidMethod(observerCallback, offlineRegionObserveronLimitId, limit);

// Delete global refs and detach when we're done
env2->DeleteGlobalRef(observerCallback);
detach_jni_thread(theJVM, &env2, renderDetach);
}

jobject observerCallback;
};

Expand Down Expand Up @@ -2631,6 +2657,12 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
return JNI_ERR;
}

offlineRegionObserveronLimitId = env->GetMethodID(offlineRegionObserverClass, "mapboxTileCountLimitExceeded", "(J)V");
if (offlineRegionObserveronLimitId == nullptr) {
env->ExceptionDescribe();
return JNI_ERR;
}

offlineRegionStatusClass = env->FindClass("com/mapbox/mapboxsdk/offline/OfflineRegionStatus");
if (offlineRegionStatusClass == nullptr) {
env->ExceptionDescribe();
Expand Down Expand Up @@ -2878,7 +2910,8 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
{"setAccessToken", "(JLjava/lang/String;)V", reinterpret_cast<void *>(&setAccessToken)},
{"getAccessToken", "(J)Ljava/lang/String;", reinterpret_cast<void *>(&getAccessToken)},
{"listOfflineRegions", "(JLcom/mapbox/mapboxsdk/offline/OfflineManager$ListOfflineRegionsCallback;)V", reinterpret_cast<void *>(&listOfflineRegions)},
{"createOfflineRegion", "(JLcom/mapbox/mapboxsdk/offline/OfflineRegionDefinition;Lcom/mapbox/mapboxsdk/offline/OfflineRegionMetadata;Lcom/mapbox/mapboxsdk/offline/OfflineManager$CreateOfflineRegionCallback;)V", reinterpret_cast<void *>(&createOfflineRegion)}
{"createOfflineRegion", "(JLcom/mapbox/mapboxsdk/offline/OfflineRegionDefinition;Lcom/mapbox/mapboxsdk/offline/OfflineRegionMetadata;Lcom/mapbox/mapboxsdk/offline/OfflineManager$CreateOfflineRegionCallback;)V", reinterpret_cast<void *>(&createOfflineRegion)},
{"setOfflineMapboxTileCountLimit", "(JJ)V", reinterpret_cast<void *>(&setOfflineMapboxTileCountLimit)}
};

if (env->RegisterNatives(offlineManagerClass, offlineManagerMethods.data(), offlineManagerMethods.size()) < 0) {
Expand Down Expand Up @@ -3471,6 +3504,7 @@ extern "C" JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
env->DeleteGlobalRef(offlineRegionObserverClass);
offlineRegionObserveronStatusChangedId = nullptr;
offlineRegionObserveronErrorId = nullptr;
offlineRegionObserveronLimitId = nullptr;

env->DeleteGlobalRef(offlineRegionStatusClass);
offlineRegionStatusConstructorId = nullptr;
Expand Down
5 changes: 5 additions & 0 deletions platform/android/src/jni.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ extern jclass createOfflineRegionCallbackClass;
extern jmethodID createOnCreateMethodId;
extern jmethodID createOnErrorMethodId;

extern jclass offlineRegionObserverClass;
extern jmethodID offlineRegionObserveronStatusChangedId;
extern jmethodID offlineRegionObserveronErrorId;
extern jmethodID offlineRegionObserveronLimitId;

extern jclass offlineRegionStatusClass;
extern jmethodID offlineRegionStatusConstructorId;
extern jfieldID offlineRegionStatusDownloadStateId;
Expand Down

0 comments on commit 9c96e26

Please sign in to comment.