下载器编写
This commit is contained in:
@ -1,69 +1,125 @@
|
|||||||
package com.arialyy.downloadutil.core;
|
package com.arialyy.downloadutil.core;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by lyy on 2016/8/17.
|
* Created by lyy on 2016/8/17.
|
||||||
* 下载任务调度类
|
* 下载任务调度类
|
||||||
*/
|
*/
|
||||||
public class DownloadTarget extends IDownloadTarget {
|
public class DownloadTarget extends IDownloadTarget {
|
||||||
|
private static final String TAG = "DownloadTarget";
|
||||||
private static final Object LOCK = new Object();
|
private static final Object LOCK = new Object();
|
||||||
private static volatile DownloadTarget INSTANCE = null;
|
private static volatile DownloadTarget INSTANCE = null;
|
||||||
|
private Context mContext;
|
||||||
|
|
||||||
public static DownloadTarget getInstance() {
|
public static DownloadTarget getInstance() {
|
||||||
if (INSTANCE == null) {
|
if (INSTANCE == null) {
|
||||||
synchronized (LOCK) {
|
Log.e(TAG, "请在Application中调用DownloadTarget.init()方法注册下载器");
|
||||||
INSTANCE = new DownloadTarget();
|
return null;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return INSTANCE;
|
return INSTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
private DownloadTarget() {
|
/**
|
||||||
|
* 初始化下载器
|
||||||
|
*
|
||||||
|
* @param context 全局Context
|
||||||
|
*/
|
||||||
|
public static void init(Context context) {
|
||||||
|
if (INSTANCE == null) {
|
||||||
|
synchronized (LOCK) {
|
||||||
|
INSTANCE = new DownloadTarget(context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private DownloadTarget() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
private DownloadTarget(Context context) {
|
||||||
|
super();
|
||||||
|
mContext = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void startTask(Task task) {
|
public void startTask(Task task) {
|
||||||
|
if (mExecutePool.putTask(task)) {
|
||||||
|
task.start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stopTask(Task task) {
|
public void stopTask(Task task) {
|
||||||
|
if (mExecutePool.removeTask(task)) {
|
||||||
|
task.stop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void cancelTask(Task task) {
|
public void cancelTask(Task task) {
|
||||||
|
if (mExecutePool.removeTask(task)) {
|
||||||
|
task.cancel();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void reTryStart(Task task) {
|
public void reTryStart(Task task) {
|
||||||
|
if (!task.getDownloadUtil().isDownloading()) {
|
||||||
|
task.start();
|
||||||
|
} else {
|
||||||
|
Log.w(TAG, "任务没有完全停止,重试下载失败");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createTask(String downloadUrl, String downloadPath) {
|
public void createTask(String downloadUrl, String downloadPath) {
|
||||||
|
DownloadEntity entity = new DownloadEntity();
|
||||||
|
entity.setDownloadUrl(downloadUrl);
|
||||||
|
entity.setDownloadPath(downloadPath);
|
||||||
|
Task task = TaskFactory.getInstance().createTask(mContext, entity, mTaskHandler);
|
||||||
|
mCachePool.putTask(task);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Task getTask(String downloadUrl) {
|
public Task getTask(String downloadUrl) {
|
||||||
return null;
|
Task task = mCachePool.getTask(downloadUrl);
|
||||||
|
if (task == null) {
|
||||||
|
task = mExecutePool.getTask(downloadUrl);
|
||||||
|
}
|
||||||
|
return task;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getTaskState(String downloadUrl) {
|
public int getTaskState(String downloadUrl) {
|
||||||
return 0;
|
Task task = getTask(downloadUrl);
|
||||||
|
if (task == null) {
|
||||||
|
Log.e(TAG, "没有找到下载链接为【" + downloadUrl + "】的下载任务");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return task.getDownloadEntity().getState();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removeTask(String downloadUrl) {
|
public void removeTask(String downloadUrl) {
|
||||||
|
Task task = mCachePool.getTask(downloadUrl);
|
||||||
|
if (task != null) {
|
||||||
|
Log.d(TAG, "任务删除" + (mCachePool.removeTask(task) ? "成功" : "失败"));
|
||||||
|
} else {
|
||||||
|
task = mExecutePool.getTask(downloadUrl);
|
||||||
|
}
|
||||||
|
if (task != null) {
|
||||||
|
Log.d(TAG, "任务删除" + (mCachePool.removeTask(task) ? "成功" : "失败"));
|
||||||
|
} else {
|
||||||
|
Log.w(TAG, "没有找到下载链接为【" + downloadUrl + "】的任务");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Task getNextTask() {
|
public Task getNextTask() {
|
||||||
return null;
|
return mCachePool.pollTask();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -166,6 +166,36 @@ public abstract class IDownloadTarget implements IDownloader, ITask {
|
|||||||
handleFailTask(entity);
|
handleFailTask(entity);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
callback(msg.what, entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 回调
|
||||||
|
*
|
||||||
|
* @param state 状态
|
||||||
|
* @param entity 下载实体
|
||||||
|
*/
|
||||||
|
private void callback(int state, DownloadEntity entity) {
|
||||||
|
if (target.mTargetListener != null) {
|
||||||
|
Task task = target.getTask(entity.getDownloadUrl());
|
||||||
|
switch (state) {
|
||||||
|
case START:
|
||||||
|
target.mTargetListener.onTaskStart(task);
|
||||||
|
break;
|
||||||
|
case STOP:
|
||||||
|
target.mTargetListener.onTaskStop(task);
|
||||||
|
break;
|
||||||
|
case CANCEL:
|
||||||
|
target.mTargetListener.onTaskCancel(task);
|
||||||
|
break;
|
||||||
|
case COMPLETE:
|
||||||
|
target.mTargetListener.onTaskCancel(task);
|
||||||
|
break;
|
||||||
|
case FAIL:
|
||||||
|
target.mTargetListener.onTaskFail(task);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -55,6 +55,13 @@ public class Task {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取下载工具
|
||||||
|
*/
|
||||||
|
public DownLoadUtil getDownloadUtil() {
|
||||||
|
return util;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 取消下载
|
* 取消下载
|
||||||
*/
|
*/
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.arialyy.downloadutil.core;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by lyy on 2016/8/18.
|
||||||
|
* 创建任务类型
|
||||||
|
*/
|
||||||
|
public enum TaskEnum {
|
||||||
|
ORDINARY, NO_USR_CALLBACK
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.arialyy.downloadutil.core;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.Handler;
|
||||||
|
|
||||||
|
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by lyy on 2016/8/18.
|
||||||
|
* 任务工厂
|
||||||
|
*/
|
||||||
|
public class TaskFactory {
|
||||||
|
private static final String TAG = "TaskFactory";
|
||||||
|
|
||||||
|
private static final Object LOCK = new Object();
|
||||||
|
private static volatile TaskFactory INSTANCE = null;
|
||||||
|
|
||||||
|
private TaskFactory() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TaskFactory getInstance() {
|
||||||
|
if (INSTANCE == null) {
|
||||||
|
synchronized (LOCK) {
|
||||||
|
INSTANCE = new TaskFactory();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建普通下载任务
|
||||||
|
* @param context
|
||||||
|
* @param entity 下载实体
|
||||||
|
* @param handler {@link com.arialyy.downloadutil.core.IDownloadTarget.AutoTaskHandler}
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public Task createTask(Context context, DownloadEntity entity, Handler handler) {
|
||||||
|
Task.Builder builder = new Task.Builder(context, entity);
|
||||||
|
builder.setOutHandler(handler);
|
||||||
|
return builder.builder();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -9,7 +9,7 @@ import com.arialyy.downloadutil.core.Task;
|
|||||||
public interface ITask {
|
public interface ITask {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建一个新的下载任务
|
* 创建一个新的下载任务,创建时只是将新任务存储到缓存池
|
||||||
*
|
*
|
||||||
* @param downloadUrl 下载链接
|
* @param downloadUrl 下载链接
|
||||||
* @param downloadPath 保存路径
|
* @param downloadPath 保存路径
|
||||||
@ -28,7 +28,7 @@ public interface ITask {
|
|||||||
* 通过下载链接搜索下载任务
|
* 通过下载链接搜索下载任务
|
||||||
*
|
*
|
||||||
* @param downloadUrl 下载链接
|
* @param downloadUrl 下载链接
|
||||||
* @return {@link com.arialyy.downloadutil.entity.DownloadEntity#STATE_FAIL}
|
* @return {@code -1 ==> 错误},{@link com.arialyy.downloadutil.entity.DownloadEntity#STATE_FAIL}
|
||||||
*/
|
*/
|
||||||
public int getTaskState(String downloadUrl);
|
public int getTaskState(String downloadUrl);
|
||||||
|
|
||||||
|
@ -79,12 +79,7 @@ public class CachePool implements IPool {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
String key = Util.keyToHashKey(downloadUrl);
|
String key = Util.keyToHashKey(downloadUrl);
|
||||||
Task task = mCacheArray.get(key);
|
return mCacheArray.get(key);
|
||||||
if (task != null) {
|
|
||||||
mCacheArray.remove(key);
|
|
||||||
mCacheQueue.remove(task);
|
|
||||||
}
|
|
||||||
return task;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,7 +105,6 @@ public class ExecutePool implements IPool {
|
|||||||
synchronized (LOCK) {
|
synchronized (LOCK) {
|
||||||
Task task = mExecuteQueue.poll();
|
Task task = mExecuteQueue.poll();
|
||||||
if (task != null) {
|
if (task != null) {
|
||||||
task.stop();
|
|
||||||
String url = task.getDownloadEntity().getDownloadUrl();
|
String url = task.getDownloadEntity().getDownloadUrl();
|
||||||
mExecuteArray.remove(Util.keyToHashKey(url));
|
mExecuteArray.remove(Util.keyToHashKey(url));
|
||||||
}
|
}
|
||||||
@ -121,13 +120,7 @@ public class ExecutePool implements IPool {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
String key = Util.keyToHashKey(downloadUrl);
|
String key = Util.keyToHashKey(downloadUrl);
|
||||||
Task task = mExecuteArray.get(key);
|
return mExecuteArray.get(key);
|
||||||
if (task != null) {
|
|
||||||
task.stop();
|
|
||||||
mExecuteArray.remove(key);
|
|
||||||
mExecuteQueue.remove(task);
|
|
||||||
}
|
|
||||||
return task;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,7 +131,6 @@ public class ExecutePool implements IPool {
|
|||||||
Log.e(TAG, "任务不能为空");
|
Log.e(TAG, "任务不能为空");
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
task.stop();
|
|
||||||
String key = Util.keyToHashKey(task.getDownloadEntity().getDownloadUrl());
|
String key = Util.keyToHashKey(task.getDownloadEntity().getDownloadUrl());
|
||||||
mExecuteArray.remove(key);
|
mExecuteArray.remove(key);
|
||||||
return mExecuteQueue.remove(task);
|
return mExecuteQueue.remove(task);
|
||||||
|
Reference in New Issue
Block a user