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

Commit fc2d809

Browse files
committed
brodcast receiver check taskId before resolving.
1 parent 8bdf5b0 commit fc2d809

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

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

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -698,36 +698,37 @@ public void onReceive(Context context, Intent intent) {
698698

699699
}
700700
} else if (RNFetchBlobService.RNFetchBlobServiceBroadcast.equals(action)) {
701-
if (intent.hasCategory(RNFetchBlobService.CategoryProgress)) {
702-
HashMap map = (HashMap)intent.getSerializableExtra(RNFetchBlobService.BroadcastProgressMap);
703-
String taskId = (String)map.get(RNFetchBlobService.KeyTaskId);
704-
WritableMap args = Arguments.createMap();
705-
args.putString("taskId", taskId);
706-
args.putString("written", String.valueOf(map.get(RNFetchBlobService.KeyWritten)));
707-
args.putString("total", String.valueOf(map.get(RNFetchBlobService.KeyTotal)));
708-
709-
// emit event to js context
710-
RNFetchBlob.RCTContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
711-
.emit(RNFetchBlobConst.EVENT_UPLOAD_PROGRESS, args);
712-
} else if (intent.hasCategory(RNFetchBlobService.CategorySuccess)) {
713-
// Could be fail.
714-
try {
715-
byte[] bytes = intent.getByteArrayExtra(RNFetchBlobService.BroadcastMsg);
716-
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_UTF8, new String(bytes, "UTF-8"));
717-
} catch (IOException e) {
718-
callback.invoke("RNFetchBlob failed to encode response data to UTF8 string.", null);
719-
} finally {
720-
// lets unregister.
701+
String _taskId = intent.getStringExtra(RNFetchBlobService.BroadcastTaskId);
702+
if (this.taskId.equals(_taskId)) {
703+
if (intent.hasCategory(RNFetchBlobService.CategoryProgress)) {
704+
HashMap map = (HashMap) intent.getSerializableExtra(RNFetchBlobService.BroadcastProgressMap);
705+
706+
WritableMap args = Arguments.createMap();
707+
args.putString("taskId", _taskId);
708+
args.putString("written", String.valueOf(map.get(RNFetchBlobService.KeyWritten)));
709+
args.putString("total", String.valueOf(map.get(RNFetchBlobService.KeyTotal)));
710+
711+
// emit event to js context
712+
RNFetchBlob.RCTContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
713+
.emit(RNFetchBlobConst.EVENT_UPLOAD_PROGRESS, args);
714+
} else if (intent.hasCategory(RNFetchBlobService.CategorySuccess)) {
715+
// Could be fail.
716+
try {
717+
byte[] bytes = intent.getByteArrayExtra(RNFetchBlobService.BroadcastMsg);
718+
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_UTF8, new String(bytes, "UTF-8"));
719+
} catch (IOException e) {
720+
callback.invoke("RNFetchBlob failed to encode response data to UTF8 string.", null);
721+
} finally {
722+
// lets unregister.
723+
Context appCtx = RNFetchBlob.RCTContext.getApplicationContext();
724+
appCtx.unregisterReceiver(this);
725+
}
726+
} else if (intent.hasCategory(RNFetchBlobService.CategoryFail)) {
727+
callback.invoke("Request failed.", null, null);
721728
Context appCtx = RNFetchBlob.RCTContext.getApplicationContext();
722729
appCtx.unregisterReceiver(this);
723730
}
724-
} else if (intent.hasCategory(RNFetchBlobService.CategoryFail)) {
725-
callback.invoke("Request failed.", null, null);
726-
Context appCtx = RNFetchBlob.RCTContext.getApplicationContext();
727-
appCtx.unregisterReceiver(this);
728731
}
729732
}
730733
}
731-
732-
733734
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public class RNFetchBlobService extends IntentService implements ProgressListene
4040

4141
public static String BroadcastMsg = "BroadcastMsg";
4242
public static String BroadcastProgressMap = "BroadcastProgressMap";
43+
public static String BroadcastTaskId = "BroadcastTaskId";
4344

44-
public static String KeyTaskId = "KeyTaskId";
4545
public static String KeyWritten = "KeyWritten";
4646
public static String KeyTotal = "KeyTotal";
4747

@@ -133,6 +133,7 @@ public void onFailure(Call call, IOException e) {
133133
broadcastIntent.setAction(RNFetchBlobServiceBroadcast);
134134
broadcastIntent.addCategory(CategoryFail);
135135
broadcastIntent.putExtra(BroadcastMsg, e.getMessage().getBytes());
136+
broadcastIntent.putExtra(BroadcastTaskId, _taskId);
136137
sendBroadcast(broadcastIntent);
137138
call.cancel();
138139
}
@@ -144,6 +145,7 @@ public void onResponse(Call call, Response response) throws IOException {
144145
broadcastIntent.setAction(RNFetchBlobServiceBroadcast);
145146
broadcastIntent.addCategory(CategorySuccess);
146147
broadcastIntent.putExtra(BroadcastMsg, response.body().bytes());
148+
broadcastIntent.putExtra(BroadcastTaskId, _taskId);
147149
sendBroadcast(broadcastIntent);
148150

149151
response.body().close();
@@ -175,13 +177,11 @@ public void onProgress(long bytesWritten, long contentLength, boolean done) {
175177
Intent broadcastIntent = new Intent();
176178
broadcastIntent.setAction(RNFetchBlobServiceBroadcast);
177179
broadcastIntent.addCategory(CategoryProgress);
180+
broadcastIntent.putExtra(BroadcastTaskId, _taskId);
178181
HashMap map = new HashMap();
179182
map.put(KeyWritten, Long.valueOf(bytesWritten));
180183
map.put(KeyTotal, Long.valueOf(contentLength));
181-
map.put(KeyTaskId, this._taskId);
182184
broadcastIntent.putExtra(BroadcastProgressMap, map);
183185
sendBroadcast(broadcastIntent);
184186
}
185187
}
186-
187-

0 commit comments

Comments
 (0)