22
33import android .app .DownloadManager ;
44import android .content .BroadcastReceiver ;
5- import android .content .ContentResolver ;
65import android .content .Context ;
76import android .content .Intent ;
87import android .content .IntentFilter ;
2120import java .io .ByteArrayInputStream ;
2221import java .io .File ;
2322import java .io .FileInputStream ;
24- import java .io .FileNotFoundException ;
25- import java .io .FileOutputStream ;
2623import java .io .IOException ;
2724import java .io .InputStream ;
2825import java .net .MalformedURLException ;
2926import java .net .URL ;
27+ import java .util .HashMap ;
3028
3129import okhttp3 .Call ;
3230import okhttp3 .Interceptor ;
3331import okhttp3 .MediaType ;
34- import okhttp3 .MultipartBody ;
3532import okhttp3 .OkHttpClient ;
3633import okhttp3 .Request ;
3734import 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