@@ -172,16 +172,20 @@ synchronized void removeOrCancelDownload(String modelName, Long downloadId) {
172
172
173
173
private synchronized DownloadBroadcastReceiver getReceiverInstance (
174
174
long downloadId , String modelName ) {
175
- DownloadBroadcastReceiver receiver = receiverMaps .get (downloadId );
175
+ DownloadBroadcastReceiver receiver = this . receiverMaps .get (downloadId );
176
176
if (receiver == null ) {
177
177
receiver =
178
178
new DownloadBroadcastReceiver (
179
179
downloadId , modelName , getTaskCompletionSourceInstance (downloadId ));
180
- receiverMaps .put (downloadId , receiver );
180
+ this . receiverMaps .put (downloadId , receiver );
181
181
}
182
182
return receiver ;
183
183
}
184
184
185
+ private synchronized void removeReceiverInstance (long downloadId ) {
186
+ this .receiverMaps .remove (downloadId );
187
+ }
188
+
185
189
private Task <Void > registerReceiverForDownloadId (long downloadId , String modelName ) {
186
190
BroadcastReceiver broadcastReceiver = getReceiverInstance (downloadId , modelName );
187
191
// It is okay to always register here. Since the broadcast receiver is the same via the lookup
@@ -194,15 +198,25 @@ private Task<Void> registerReceiverForDownloadId(long downloadId, String modelNa
194
198
195
199
@ VisibleForTesting
196
200
synchronized TaskCompletionSource <Void > getTaskCompletionSourceInstance (long downloadId ) {
197
- TaskCompletionSource <Void > taskCompletionSource = taskCompletionSourceMaps .get (downloadId );
201
+ TaskCompletionSource <Void > taskCompletionSource = this . taskCompletionSourceMaps .get (downloadId );
198
202
if (taskCompletionSource == null ) {
199
203
taskCompletionSource = new TaskCompletionSource <>();
200
- taskCompletionSourceMaps .put (downloadId , taskCompletionSource );
204
+ this . taskCompletionSourceMaps .put (downloadId , taskCompletionSource );
201
205
}
202
206
203
207
return taskCompletionSource ;
204
208
}
205
209
210
+ @ VisibleForTesting
211
+ synchronized boolean existTaskCompletionSourceInstance (long downloadId ) {
212
+ TaskCompletionSource <Void > taskCompletionSource = this .taskCompletionSourceMaps .get (downloadId );
213
+ return (taskCompletionSource != null );
214
+ }
215
+
216
+ private synchronized void removeTaskCompletionSourceInstance (long downloadId ) {
217
+ this .taskCompletionSourceMaps .remove (downloadId );
218
+ }
219
+
206
220
@ VisibleForTesting
207
221
synchronized Long scheduleModelDownload (@ NonNull CustomModel customModel ) {
208
222
if (downloadManager == null ) {
@@ -441,8 +455,8 @@ public void onReceive(Context context, Intent intent) {
441
455
Integer statusCode = getDownloadingModelStatusCode (downloadId );
442
456
// check to prevent DuplicateTaskCompletionException - this was already updated and removed.
443
457
// Just return.
444
- if (taskCompletionSourceMaps . get (downloadId ) == null ) {
445
- receiverMaps . remove (downloadId );
458
+ if (! existTaskCompletionSourceInstance (downloadId )) {
459
+ removeReceiverInstance (downloadId );
446
460
return ;
447
461
}
448
462
@@ -457,8 +471,8 @@ public void onReceive(Context context, Intent intent) {
457
471
// move on.
458
472
}
459
473
460
- receiverMaps . remove (downloadId );
461
- taskCompletionSourceMaps . remove (downloadId );
474
+ removeReceiverInstance (downloadId );
475
+ removeTaskCompletionSourceInstance (downloadId );
462
476
}
463
477
464
478
if (statusCode != null ) {
0 commit comments