1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
package com.mapbox.mapboxsdk.http;
import android.support.annotation.Keep;
import android.support.annotation.Nullable;
import com.mapbox.mapboxsdk.Mapbox;
import java.util.concurrent.locks.ReentrantLock;
@Keep
public class NativeHttpRequest implements HttpResponder {
private final HttpRequest httpRequest = Mapbox.getModuleProvider().createHttpRequest();
// Reentrancy is not needed, but "Lock" is an abstract class.
private final ReentrantLock lock = new ReentrantLock();
@Keep
private long nativePtr;
@Keep
private NativeHttpRequest(long nativePtr, String resourceUrl, String etag, String modified, boolean offlineUsage) {
this.nativePtr = nativePtr;
if (resourceUrl.startsWith("local://")) {
// used by render test to serve files from assets
executeLocalRequest(resourceUrl);
return;
}
httpRequest.executeRequest(this, nativePtr, resourceUrl, etag, modified, offlineUsage);
}
public void cancel() {
httpRequest.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(new LocalRequestTask.OnLocalRequestResponse() {
@Override
public void onResponse(@Nullable byte[] bytes) {
if (bytes != null) {
lock.lock();
if (nativePtr != 0) {
NativeHttpRequest.this.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();
}
@Keep
private native void nativeOnFailure(int type, String message);
@Keep
private native void nativeOnResponse(int code, String etag, String modified, String cacheControl, String expires,
String retryAfter, String xRateLimitReset, byte[] body);
}
|