Skip to content

Commit

Permalink
fix: tag should hold by DownloadTaskAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Tianhua Ran committed Nov 7, 2019
1 parent 850a68b commit 4d4b12f
Showing 1 changed file with 14 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.SparseArray;

import com.liulishuo.filedownloader.progress.ProgressAssist;
import com.liulishuo.filedownloader.retry.RetryAssist;
Expand Down Expand Up @@ -54,6 +55,8 @@ public class DownloadTaskAdapter implements BaseDownloadTask, BaseDownloadTask.I
private volatile int attachKey;
private volatile boolean isAddedToList;
private final Object insureDownloadTaskAssembledLock = new Object();
private Object tag;
private SparseArray<Object> tagWithKey;

public DownloadTaskAdapter(String url) {
this.builder = new Builder();
Expand Down Expand Up @@ -127,17 +130,16 @@ public BaseDownloadTask setCallbackProgressIgnored() {

@Override
public BaseDownloadTask setTag(Object tag) {
builder.tag = tag;
this.tag = tag;
return this;
}

@Override
public BaseDownloadTask setTag(int key, Object tag) {
if (key == KEY_TASK_ADAPTER) {
throw new IllegalArgumentException(key + " is used internally, please use another key");
if (tagWithKey == null) {
tagWithKey = new SparseArray<>();
}
builder.keyOfTag = key;
builder.tagWithKey = tag;
tagWithKey.put(key, tag);
return this;
}

Expand Down Expand Up @@ -428,14 +430,16 @@ public boolean isReusedOldFile() {

@Override
public Object getTag() {
insureAssembleDownloadTask();
return downloadTask.getTag();
return tag;
}

@Override
public Object getTag(int key) {
insureAssembleDownloadTask();
return downloadTask.getTag(key);
if (tagWithKey == null) {
return null;
} else {
return tagWithKey.get(key);
}
}

@Override
Expand Down Expand Up @@ -569,9 +573,6 @@ static final class Builder {
boolean pathAsDirectory;
private int minIntervalMillisCallbackProgress
= DEFAULT_CALLBACK_PROGRESS_MIN_INTERVAL_MILLIS;
private Object tag;
private Integer keyOfTag;
private Object tagWithKey;
private boolean forceReDownload;
Map<String, String> headerMap = new HashMap<>();
private boolean isWifiRequired;
Expand All @@ -594,10 +595,7 @@ DownloadTask build() {
builder.addHeader(entry.getKey(), entry.getValue());
}
builder.setAutoCallbackToUIThread(autoCallbackToUIThread);
DownloadTask task = builder.build();
if (tag != null) task.setTag(tag);
if (keyOfTag != null) task.addTag(keyOfTag, tagWithKey);
return task;
return builder.build();
}
}

Expand Down

0 comments on commit 4d4b12f

Please sign in to comment.