Browse Source

Add cancel mechanism to Android #39 #37

Ben Hsieh 8 years ago
parent
commit
c00f557a2d

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

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