diff options
3 files changed, 56 insertions, 1 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java index 06fb737c52..cf6233a805 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java @@ -462,4 +462,25 @@ public class OfflineManager { @Keep private native void mergeOfflineRegions(FileSource fileSource, String path, MergeOfflineRegionsCallback callback); + + /** + * Insert the provided resource into the ambient cache + * This method mimics the caching that would take place if the equivalent + * resource were requested in the process of map rendering. + * Use this method to pre-warm the cache with resources you know + * will be requested. + * + * This call is asynchronous: the data may not be immediately available + * for in-progress requests, although subsequent requests should have + * access to the cached data. + * + * @param url The URL of the resource to insert + * @param data Response data to store for this resource. Data is expected to be uncompressed; + * internally, the cache will compress data as necessary. + * @param modified Optional "modified" response header, in seconds since 1970, or 0 if not set + * @param expires Optional "expires" response header, in seconds since 1970, or 0 if not set + * @param etag Optional "entity tag" response header + */ + @Keep + public native void putResourceWithUrl(String url, byte[] data, long modified, long expires, String etag); } diff --git a/platform/android/src/offline/offline_manager.cpp b/platform/android/src/offline/offline_manager.cpp index c6432f766a..07e10d4c81 100644 --- a/platform/android/src/offline/offline_manager.cpp +++ b/platform/android/src/offline/offline_manager.cpp @@ -121,7 +121,8 @@ void OfflineManager::registerNative(jni::JNIEnv& env) { METHOD(&OfflineManager::setOfflineMapboxTileCountLimit, "setOfflineMapboxTileCountLimit"), METHOD(&OfflineManager::listOfflineRegions, "listOfflineRegions"), METHOD(&OfflineManager::createOfflineRegion, "createOfflineRegion"), - METHOD(&OfflineManager::mergeOfflineRegions, "mergeOfflineRegions")); + METHOD(&OfflineManager::mergeOfflineRegions, "mergeOfflineRegions"), + METHOD(&OfflineManager::putResourceWithUrl, "putResourceWithUrl")); } // OfflineManager::ListOfflineRegionsCallback // @@ -201,5 +202,30 @@ void OfflineManager::MergeOfflineRegionsCallback::onMerge(jni::JNIEnv& env, callback.Call(env, method, jregions); } +void OfflineManager::putResourceWithUrl(jni::JNIEnv& env, + const jni::String& url_, + const jni::Array<jni::jbyte>& arr, + jlong modified, + jlong expires, + const jni::String& eTag_) { + auto url = jni::Make<std::string>(env, url_); + auto data = std::make_shared<std::string>(arr.Length(env), char()); + jni::GetArrayRegion(env, *arr, 0, data->size(), reinterpret_cast<jbyte*>(&(*data)[0])); + mbgl::Resource resource(mbgl::Resource::Kind::Unknown, url); + mbgl::Response response; + response.data = data; + if (eTag_) { + response.etag = jni::Make<std::string>(env, eTag_); + } + if (modified > 0) { + response.modified = Timestamp(mbgl::Seconds(modified)); + } + if (expires > 0) { + response.expires = Timestamp(mbgl::Seconds(expires)); + } + + fileSource.put(resource, response); +} + } // namespace android } // namespace mbgl diff --git a/platform/android/src/offline/offline_manager.hpp b/platform/android/src/offline/offline_manager.hpp index b2ebc63a63..52c9ab70a0 100644 --- a/platform/android/src/offline/offline_manager.hpp +++ b/platform/android/src/offline/offline_manager.hpp @@ -75,6 +75,14 @@ public: const jni::String&, const jni::Object<MergeOfflineRegionsCallback>&); + void putResourceWithUrl(jni::JNIEnv&, + const jni::String& url, + const jni::Array<jni::jbyte>& data, + jlong modified, + jlong expires, + const jni::String& eTag); + + private: mbgl::DefaultFileSource& fileSource; }; |