|
|
@ -12,13 +12,12 @@ import com.arialyy.downloadutil.core.pool.ExecutePool; |
|
|
|
* Created by lyy on 2016/8/17. |
|
|
|
* Created by lyy on 2016/8/17. |
|
|
|
* 下载任务队列 |
|
|
|
* 下载任务队列 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class DownloadTaskQueue implements ITaskQueue, IDownloader { |
|
|
|
final class DownloadTaskQueue implements ITaskQueue { |
|
|
|
private static final String TAG = "DownloadTaskQueue"; |
|
|
|
private static final String TAG = "DownloadTaskQueue"; |
|
|
|
private static final Object LOCK = new Object(); |
|
|
|
|
|
|
|
private static volatile DownloadTaskQueue INSTANCE = null; |
|
|
|
|
|
|
|
private CachePool mCachePool = CachePool.getInstance(); |
|
|
|
private CachePool mCachePool = CachePool.getInstance(); |
|
|
|
private ExecutePool mExecutePool = ExecutePool.getInstance(); |
|
|
|
private ExecutePool mExecutePool = ExecutePool.getInstance(); |
|
|
|
private Context mContext; |
|
|
|
private Context mContext; |
|
|
|
|
|
|
|
private IDownloadSchedulers mSchedulers; |
|
|
|
|
|
|
|
|
|
|
|
private DownloadTaskQueue() { |
|
|
|
private DownloadTaskQueue() { |
|
|
|
} |
|
|
|
} |
|
|
@ -28,22 +27,6 @@ public class DownloadTaskQueue implements ITaskQueue, IDownloader { |
|
|
|
mContext = context; |
|
|
|
mContext = context; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static DownloadTaskQueue getInstance() { |
|
|
|
|
|
|
|
if (INSTANCE == null) { |
|
|
|
|
|
|
|
throw new NullPointerException("请在Application中调用init进行注册"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return INSTANCE; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static DownloadTaskQueue init(Context context) { |
|
|
|
|
|
|
|
if (INSTANCE == null) { |
|
|
|
|
|
|
|
synchronized (LOCK) { |
|
|
|
|
|
|
|
INSTANCE = new DownloadTaskQueue(context.getApplicationContext()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return INSTANCE; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 获取任务执行池 |
|
|
|
* 获取任务执行池 |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -109,14 +92,7 @@ public class DownloadTaskQueue implements ITaskQueue, IDownloader { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public Task createTask(DownloadEntity entity) { |
|
|
|
@Override public Task createTask(DownloadEntity entity) { |
|
|
|
return createTask(entity, null); |
|
|
|
Task task = TaskFactory.getInstance().createTask(mContext, entity, mSchedulers); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override public Task createTask(DownloadEntity entity, IDownloadSchedulers schedulers) { |
|
|
|
|
|
|
|
if (schedulers == null) { |
|
|
|
|
|
|
|
schedulers = DownloadSchedulers.getInstance(this); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Task task = TaskFactory.getInstance().createTask(mContext, entity, schedulers); |
|
|
|
|
|
|
|
mCachePool.putTask(task); |
|
|
|
mCachePool.putTask(task); |
|
|
|
return task; |
|
|
|
return task; |
|
|
|
} |
|
|
|
} |
|
|
@ -129,15 +105,6 @@ public class DownloadTaskQueue implements ITaskQueue, IDownloader { |
|
|
|
return task; |
|
|
|
return task; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public int getTaskState(DownloadEntity entity) { |
|
|
|
|
|
|
|
Task task = getTask(entity); |
|
|
|
|
|
|
|
if (task == null) { |
|
|
|
|
|
|
|
Log.e(TAG, "没有找到下载链接为【" + entity.getDownloadUrl() + "】的下载任务"); |
|
|
|
|
|
|
|
return -1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return task.getDownloadEntity().getState(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override public void removeTask(DownloadEntity entity) { |
|
|
|
@Override public void removeTask(DownloadEntity entity) { |
|
|
|
Task task = mExecutePool.getTask(entity.getDownloadUrl()); |
|
|
|
Task task = mExecutePool.getTask(entity.getDownloadUrl()); |
|
|
|
if (task != null) { |
|
|
|
if (task != null) { |
|
|
@ -155,4 +122,31 @@ public class DownloadTaskQueue implements ITaskQueue, IDownloader { |
|
|
|
@Override public Task getNextTask() { |
|
|
|
@Override public Task getNextTask() { |
|
|
|
return mCachePool.pollTask(); |
|
|
|
return mCachePool.pollTask(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override public void setScheduler(IDownloadSchedulers schedulers) { |
|
|
|
|
|
|
|
mSchedulers = schedulers; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static class Builder { |
|
|
|
|
|
|
|
Context context; |
|
|
|
|
|
|
|
IDownloadSchedulers schedulers; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Builder(Context context) { |
|
|
|
|
|
|
|
context = context.getApplicationContext(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Builder setDownloadSchedulers(IDownloadSchedulers schedulers) { |
|
|
|
|
|
|
|
this.schedulers = schedulers; |
|
|
|
|
|
|
|
return this; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DownloadTaskQueue build() { |
|
|
|
|
|
|
|
DownloadTaskQueue queue = new DownloadTaskQueue(context); |
|
|
|
|
|
|
|
if (schedulers == null) { |
|
|
|
|
|
|
|
schedulers = DownloadSchedulers.getInstance(queue); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
queue.setScheduler(schedulers); |
|
|
|
|
|
|
|
return queue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|