Browse Source

Add cancel mechanism to Android #39 #37

Ben Hsieh 7 years ago
parent
commit
c00f557a2d

+ 5
- 0
src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java View File

@@ -150,6 +150,11 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
150 150
         fs.readStream(path, encoding, bufferSize);
151 151
     }
152 152
 
153
+    @ReactMethod
154
+    public void cancel(String taskId) {
155
+        RNFetchBlobReq.cancelTask(taskId);
156
+    }
157
+
153 158
     @ReactMethod
154 159
     public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
155 160
         new RNFetchBlobReq(options, taskId, method, url, headers, body, null, callback).run();

+ 22
- 9
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

@@ -2,7 +2,6 @@ package com.RNFetchBlob;
2 2
 
3 3
 import android.app.DownloadManager;
4 4
 import android.content.BroadcastReceiver;
5
-import android.content.ContentResolver;
6 5
 import android.content.Context;
7 6
 import android.content.Intent;
8 7
 import android.content.IntentFilter;
@@ -21,17 +20,15 @@ import com.facebook.react.bridge.ReadableMapKeySetIterator;
21 20
 import java.io.ByteArrayInputStream;
22 21
 import java.io.File;
23 22
 import java.io.FileInputStream;
24
-import java.io.FileNotFoundException;
25
-import java.io.FileOutputStream;
26 23
 import java.io.IOException;
27 24
 import java.io.InputStream;
28 25
 import java.net.MalformedURLException;
29 26
 import java.net.URL;
27
+import java.util.HashMap;
30 28
 
31 29
 import okhttp3.Call;
32 30
 import okhttp3.Interceptor;
33 31
 import okhttp3.MediaType;
34
-import okhttp3.MultipartBody;
35 32
 import okhttp3.OkHttpClient;
36 33
 import okhttp3.Request;
37 34
 import okhttp3.RequestBody;
@@ -55,6 +52,8 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
55 52
         FileStorage
56 53
     };
57 54
 
55
+    static HashMap<String, Call> taskTable = new HashMap<>();
56
+
58 57
     MediaType contentType = RNFetchBlobConst.MIME_OCTET;
59 58
     ReactApplicationContext ctx;
60 59
     RNFetchBlobConfig options;
@@ -94,6 +93,14 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
94 93
             requestType = RequestType.WithoutBody;
95 94
     }
96 95
 
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
+
97 104
     @Override
98 105
     public void run() {
99 106
 
@@ -146,14 +153,14 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
146 153
         else if(this.options.fileCache == true)
147 154
             this.destPath = RNFetchBlobFS.getTmpPath(RNFetchBlob.RCTContext, cacheKey);
148 155
 
149
-        OkHttpClient.Builder client;
156
+        OkHttpClient.Builder clientBuilder;
150 157
 
151 158
         try {
152 159
             // use trusty SSL socket
153 160
             if (this.options.trusty) {
154
-                client = RNFetchBlobUtils.getUnsafeOkHttpClient();
161
+                clientBuilder = RNFetchBlobUtils.getUnsafeOkHttpClient();
155 162
             } else {
156
-                client = new OkHttpClient.Builder();
163
+                clientBuilder = new OkHttpClient.Builder();
157 164
             }
158 165
 
159 166
             final Request.Builder builder = new Request.Builder();
@@ -203,7 +210,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
203 210
             final Request req = builder.build();
204 211
 
205 212
 //          create response handler
206
-            client.addInterceptor(new Interceptor() {
213
+            clientBuilder.addInterceptor(new Interceptor() {
207 214
                 @Override
208 215
                 public Response intercept(Chain chain) throws IOException {
209 216
                 Response originalResponse = chain.proceed(req);
@@ -233,7 +240,10 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
233 240
                 }
234 241
             });
235 242
 
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() {
237 247
                 @Override
238 248
                 public void onFailure(Call call, IOException e) {
239 249
                     callback.invoke(e.getLocalizedMessage(), null);
@@ -266,6 +276,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
266 276
 
267 277
         } catch (Exception error) {
268 278
             error.printStackTrace();
279
+            taskTable.remove(taskId);
269 280
             callback.invoke("RNFetchBlob request error: " + error.getMessage() + error.getCause());
270 281
         }
271 282
     }
@@ -300,6 +311,8 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
300 311
                 }
301 312
                 break;
302 313
         }
314
+        if(taskTable.containsKey(taskId))
315
+            taskTable.remove(taskId);
303 316
     }
304 317
 
305 318
     /**