下载器编写

pull/2/head
lyy 8 years ago
parent 9d06c580f1
commit 01ba765278
  1. 80
      downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadTarget.java
  2. 30
      downloadutil/src/main/java/com/arialyy/downloadutil/core/IDownloadTarget.java
  3. 7
      downloadutil/src/main/java/com/arialyy/downloadutil/core/Task.java
  4. 9
      downloadutil/src/main/java/com/arialyy/downloadutil/core/TaskEnum.java
  5. 44
      downloadutil/src/main/java/com/arialyy/downloadutil/core/TaskFactory.java
  6. 4
      downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/ITask.java
  7. 7
      downloadutil/src/main/java/com/arialyy/downloadutil/core/pool/CachePool.java
  8. 10
      downloadutil/src/main/java/com/arialyy/downloadutil/core/pool/ExecutePool.java

@ -1,69 +1,125 @@
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.
* 下载任务调度类
*/
public class DownloadTarget extends IDownloadTarget {
private static final String TAG = "DownloadTarget";
private static final Object LOCK = new Object();
private static volatile DownloadTarget INSTANCE = null;
private Context mContext;
public static DownloadTarget getInstance() {
if (INSTANCE == null) {
Log.e(TAG, "请在Application中调用DownloadTarget.init()方法注册下载器");
return null;
}
return INSTANCE;
}
/**
* 初始化下载器
*
* @param context 全局Context
*/
public static void init(Context context) {
if (INSTANCE == null) {
synchronized (LOCK) {
INSTANCE = new DownloadTarget();
INSTANCE = new DownloadTarget(context);
}
}
return INSTANCE;
}
private DownloadTarget() {
super();
}
private DownloadTarget(Context context) {
super();
mContext = context;
}
@Override
public void startTask(Task task) {
if (mExecutePool.putTask(task)) {
task.start();
}
}
@Override
public void stopTask(Task task) {
if (mExecutePool.removeTask(task)) {
task.stop();
}
}
@Override
public void cancelTask(Task task) {
if (mExecutePool.removeTask(task)) {
task.cancel();
}
}
@Override
public void reTryStart(Task task) {
if (!task.getDownloadUtil().isDownloading()) {
task.start();
} else {
Log.w(TAG, "任务没有完全停止,重试下载失败");
}
}
@Override
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
public Task getTask(String downloadUrl) {
return null;
Task task = mCachePool.getTask(downloadUrl);
if (task == null) {
task = mExecutePool.getTask(downloadUrl);
}
return task;
}
@Override
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
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
public Task getNextTask() {
return null;
return mCachePool.pollTask();
}
}

@ -166,6 +166,36 @@ public abstract class IDownloadTarget implements IDownloader, ITask {
handleFailTask(entity);
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 {
/**
* 创建一个新的下载任务
* 创建一个新的下载任务创建时只是将新任务存储到缓存池
*
* @param downloadUrl 下载链接
* @param downloadPath 保存路径
@ -28,7 +28,7 @@ public interface ITask {
* 通过下载链接搜索下载任务
*
* @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);

@ -79,12 +79,7 @@ public class CachePool implements IPool {
return null;
}
String key = Util.keyToHashKey(downloadUrl);
Task task = mCacheArray.get(key);
if (task != null) {
mCacheArray.remove(key);
mCacheQueue.remove(task);
}
return task;
return mCacheArray.get(key);
}
}

@ -105,7 +105,6 @@ public class ExecutePool implements IPool {
synchronized (LOCK) {
Task task = mExecuteQueue.poll();
if (task != null) {
task.stop();
String url = task.getDownloadEntity().getDownloadUrl();
mExecuteArray.remove(Util.keyToHashKey(url));
}
@ -121,13 +120,7 @@ public class ExecutePool implements IPool {
return null;
}
String key = Util.keyToHashKey(downloadUrl);
Task task = mExecuteArray.get(key);
if (task != null) {
task.stop();
mExecuteArray.remove(key);
mExecuteQueue.remove(task);
}
return task;
return mExecuteArray.get(key);
}
}
@ -138,7 +131,6 @@ public class ExecutePool implements IPool {
Log.e(TAG, "任务不能为空");
return false;
} else {
task.stop();
String key = Util.keyToHashKey(task.getDownloadEntity().getDownloadUrl());
mExecuteArray.remove(key);
return mExecuteQueue.remove(task);

Loading…
Cancel
Save