[android] Add OfflineManager.putResourceForURL

Useful for pre-warming the ambient cache
This commit is contained in:
Chris Loer
2018-10-16 14:43:48 -07:00
committed by Chris Loer
parent 5a45976b7c
commit 3f98d6a821
3 changed files with 56 additions and 1 deletions

View File

@@ -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);
}

View File

@@ -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

View File

@@ -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;
};