summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/NativeHttpRequest.java
diff options
context:
space:
mode:
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/NativeHttpRequest.java')
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/NativeHttpRequest.java77
1 files changed, 77 insertions, 0 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/NativeHttpRequest.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/NativeHttpRequest.java
new file mode 100644
index 0000000000..eb4a726163
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/NativeHttpRequest.java
@@ -0,0 +1,77 @@
+package com.mapbox.mapboxsdk.http;
+
+import java.util.concurrent.locks.ReentrantLock;
+
+public class NativeHttpRequest implements HttpRequestResponder {
+
+ private final HttpRequestImpl httpRequestImpl = new HttpRequestImpl();
+
+ // Reentrancy is not needed, but "Lock" is an abstract class.
+ private ReentrantLock lock = new ReentrantLock();
+ private long nativePtr = 0;
+
+ private NativeHttpRequest(long nativePtr, String resourceUrl, String etag, String modified) {
+ this.nativePtr = nativePtr;
+
+ if (resourceUrl.startsWith("local://")) {
+ // used by render test to serve files from assets
+ executeLocalRequest(resourceUrl);
+ return;
+ }
+ httpRequestImpl.executeRequest(this, nativePtr, resourceUrl, etag, modified);
+ }
+
+ public void cancel() {
+ httpRequestImpl.cancelRequest();
+
+ // TODO: We need a lock here because we can try
+ // to cancel at the same time the request is getting
+ // answered on the OkHTTP thread. We could get rid of
+ // this lock by using Runnable when we move Android
+ // implementation of mbgl::RunLoop to Looper.
+ lock.lock();
+ nativePtr = 0;
+ lock.unlock();
+ }
+
+ public void onResponse(int responseCode, String etag, String lastModified, String cacheControl, String expires,
+ String retryAfter, String xRateLimitReset, byte[] body) {
+ lock.lock();
+ if (nativePtr != 0) {
+ nativeOnResponse(responseCode,
+ etag,
+ lastModified,
+ cacheControl,
+ expires,
+ retryAfter,
+ xRateLimitReset,
+ body);
+ }
+ lock.unlock();
+ }
+
+ private void executeLocalRequest(String resourceUrl) {
+ new LocalRequestTask(bytes -> {
+ if (bytes != null) {
+ lock.lock();
+ if (nativePtr != 0) {
+ nativeOnResponse(200, null, null, null, null, null, null, bytes);
+ }
+ lock.unlock();
+ }
+ }).execute(resourceUrl);
+ }
+
+ public void handleFailure(int type, String errorMessage) {
+ lock.lock();
+ if (nativePtr != 0) {
+ nativeOnFailure(type, errorMessage);
+ }
+ lock.unlock();
+ }
+
+ private native void nativeOnFailure(int type, String message);
+
+ private native void nativeOnResponse(int code, String etag, String modified, String cacheControl, String expires,
+ String retryAfter, String xRateLimitReset, byte[] body);
+}