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
|
package com.mapbox.mapboxsdk.http;
import java.util.concurrent.locks.ReentrantLock;
public class NativeHttpRequest {
// 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;
}
HttpRequest.execute(this, nativePtr, resourceUrl, etag, modified);
}
public void cancel() {
HttpRequest.cancel(nativePtr);
// 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);
}
|