Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit c00f557

Browse files
committed
Add cancel mechanism to Android #39 #37
1 parent 86e3e8e commit c00f557

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ public void readStream(String path, String encoding, int bufferSize) {
150150
fs.readStream(path, encoding, bufferSize);
151151
}
152152

153+
@ReactMethod
154+
public void cancel(String taskId) {
155+
RNFetchBlobReq.cancelTask(taskId);
156+
}
157+
153158
@ReactMethod
154159
public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
155160
new RNFetchBlobReq(options, taskId, method, url, headers, body, null, callback).run();

src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import android.app.DownloadManager;
44
import android.content.BroadcastReceiver;
5-
import android.content.ContentResolver;
65
import android.content.Context;
76
import android.content.Intent;
87
import android.content.IntentFilter;
@@ -21,17 +20,15 @@
2120
import java.io.ByteArrayInputStream;
2221
import java.io.File;
2322
import java.io.FileInputStream;
24-
import java.io.FileNotFoundException;
25-
import java.io.FileOutputStream;
2623
import java.io.IOException;
2724
import java.io.InputStream;
2825
import java.net.MalformedURLException;
2926
import java.net.URL;
27+
import java.util.HashMap;
3028

3129
import okhttp3.Call;
3230
import okhttp3.Interceptor;
3331
import okhttp3.MediaType;
34-
import okhttp3.MultipartBody;
3532
import okhttp3.OkHttpClient;
3633
import okhttp3.Request;
3734
import okhttp3.RequestBody;
@@ -55,6 +52,8 @@ enum ResponseType {
5552
FileStorage
5653
};
5754

55+
static HashMap<String, Call> taskTable = new HashMap<>();
56+
5857
MediaType contentType = RNFetchBlobConst.MIME_OCTET;
5958
ReactApplicationContext ctx;
6059
RNFetchBlobConfig options;
@@ -94,6 +93,14 @@ else if (arrayBody != null)
9493
requestType = RequestType.WithoutBody;
9594
}
9695

96+
public static void cancelTask(String taskId) {
97+
if(taskTable.containsKey(taskId)) {
98+
Call call = taskTable.get(taskId);
99+
call.cancel();
100+
taskTable.remove(taskId);
101+
}
102+
}
103+
97104
@Override
98105
public void run() {
99106

@@ -146,14 +153,14 @@ public void run() {
146153
else if(this.options.fileCache == true)
147154
this.destPath = RNFetchBlobFS.getTmpPath(RNFetchBlob.RCTContext, cacheKey);
148155

149-
OkHttpClient.Builder client;
156+
OkHttpClient.Builder clientBuilder;
150157

151158
try {
152159
// use trusty SSL socket
153160
if (this.options.trusty) {
154-
client = RNFetchBlobUtils.getUnsafeOkHttpClient();
161+
clientBuilder = RNFetchBlobUtils.getUnsafeOkHttpClient();
155162
} else {
156-
client = new OkHttpClient.Builder();
163+
clientBuilder = new OkHttpClient.Builder();
157164
}
158165

159166
final Request.Builder builder = new Request.Builder();
@@ -203,7 +210,7 @@ else if(this.options.fileCache == true)
203210
final Request req = builder.build();
204211

205212
// create response handler
206-
client.addInterceptor(new Interceptor() {
213+
clientBuilder.addInterceptor(new Interceptor() {
207214
@Override
208215
public Response intercept(Chain chain) throws IOException {
209216
Response originalResponse = chain.proceed(req);
@@ -233,7 +240,10 @@ public Response intercept(Chain chain) throws IOException {
233240
}
234241
});
235242

236-
client.build().newCall(req).enqueue(new okhttp3.Callback() {
243+
OkHttpClient client = clientBuilder.build();
244+
Call call = client.newCall(req);
245+
taskTable.put(taskId, call);
246+
call.enqueue(new okhttp3.Callback() {
237247
@Override
238248
public void onFailure(Call call, IOException e) {
239249
callback.invoke(e.getLocalizedMessage(), null);
@@ -266,6 +276,7 @@ public void onResponse(Call call, Response response) throws IOException {
266276

267277
} catch (Exception error) {
268278
error.printStackTrace();
279+
taskTable.remove(taskId);
269280
callback.invoke("RNFetchBlob request error: " + error.getMessage() + error.getCause());
270281
}
271282
}
@@ -300,6 +311,8 @@ private void done(Response resp) {
300311
}
301312
break;
302313
}
314+
if(taskTable.containsKey(taskId))
315+
taskTable.remove(taskId);
303316
}
304317

305318
/**

0 commit comments

Comments
 (0)