代码重构

pull/2/head
lyy 9 years ago
parent e1adbe50c8
commit 131b9c2770
  1. 2
      downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadManager.java
  2. 196
      downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadSchedulers.java
  3. 69
      downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadTaskQueue.java
  4. 226
      downloadutil/src/main/java/com/arialyy/downloadutil/core/IDownloadTarget.java
  5. 23
      downloadutil/src/main/java/com/arialyy/downloadutil/core/Task.java
  6. 8
      downloadutil/src/main/java/com/arialyy/downloadutil/core/TaskFactory.java
  7. 17
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/CmdFactory.java
  8. 17
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/IDownloadCmd.java
  9. 24
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/StateCmd.java
  10. 25
      downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/IDownloadSchedulers.java
  11. 11
      downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/ITaskQueue.java
  12. 10
      downloadutil/src/main/java/com/arialyy/downloadutil/help/Lockhelp.java

@ -88,7 +88,7 @@ public class DownloadManager {
private DownloadManager(Context context) {
mContext = context;
DownloadTarget.init(context);
DownloadTaskQueue.init(context);
DbUtil.init(context);
}

@ -0,0 +1,196 @@
package com.arialyy.downloadutil.core;
import android.os.Message;
import android.util.Log;
import com.arialyy.downloadutil.core.inf.IDownloadSchedulers;
import com.arialyy.downloadutil.core.pool.ExecutePool;
/**
* Created by lyy on 2016/8/16.
* 任务下载器提供抽象的方法供具体的实现类操作
*/
public class DownloadSchedulers implements IDownloadSchedulers {
private static final String TAG = "DownloadSchedulers";
private static final Object LOCK = new Object();
private static volatile DownloadSchedulers INSTANCE = null;
/**
* 任务开始
*/
public static final int START = 1;
/**
* 任务停止
*/
public static final int STOP = 2;
/**
* 任务失败
*/
public static final int FAIL = 3;
/**
* 任务取消
*/
public static final int CANCEL = 4;
/**
* 任务完成
*/
public static final int COMPLETE = 5;
/**
* 下载失败次数
*/
int mFailNum = 10;
/**
* 超时时间
*/
long mTimeOut = 10000;
/**
* 下载器任务监听
*/
OnTargetListener mTargetListener;
DownloadTaskQueue mQueue;
public static DownloadSchedulers getInstance(DownloadTaskQueue queue) {
if (INSTANCE == null) {
synchronized (LOCK) {
INSTANCE = new DownloadSchedulers(queue);
}
}
return INSTANCE;
}
/**
* Target处理任务监听
*/
public interface OnTargetListener {
/**
* 任务开始
*/
public void onTaskStart(Task task);
/**
* 任务停止
*/
public void onTaskStop(Task task);
/**
* 任务取消
*/
public void onTaskCancel(Task task);
/**
* 任务下载失败
*/
public void onTaskFail(Task task);
/**
* 任务完成
*/
public void onTaskComplete(Task task);
}
public DownloadSchedulers(DownloadTaskQueue downloadTaskQueue) {
mQueue = downloadTaskQueue;
}
@Override public boolean handleMessage(Message msg) {
DownloadEntity entity = (DownloadEntity) msg.obj;
if (entity == null) {
Log.e(TAG, "请传入下载实体DownloadEntity");
return true;
}
switch (msg.what) {
case STOP:
case CANCEL:
if (mQueue.getExecutePool().size() != ExecutePool.SIZE) {
startNextTask(entity);
}
break;
case COMPLETE:
startNextTask(entity);
break;
case FAIL:
handleFailTask(entity);
break;
}
callback(msg.what, entity);
return true;
}
/**
* 回调
*
* @param state 状态
* @param entity 下载实体
*/
private void callback(int state, DownloadEntity entity) {
if (mTargetListener != null) {
Task task = mQueue.getTask(entity);
switch (state) {
case START:
mTargetListener.onTaskStart(task);
break;
case STOP:
mTargetListener.onTaskStop(task);
break;
case CANCEL:
mTargetListener.onTaskCancel(task);
break;
case COMPLETE:
mTargetListener.onTaskComplete(task);
break;
case FAIL:
mTargetListener.onTaskFail(task);
break;
}
}
}
/**
* 处理下载任务下载失败的情形
*
* @param entity 失败实体
*/
@Override public void handleFailTask(DownloadEntity entity) {
if (entity.getFailNum() <= mFailNum) {
Task task = mQueue.getTask(entity);
mQueue.reTryStart(task);
} else {
startNextTask(entity);
}
}
/**
* 启动下一个任务条件任务停止取消下载任务完成
*
* @param entity 通过Handler传递的下载实体
*/
@Override public void startNextTask(DownloadEntity entity) {
mQueue.removeTask(entity);
Task newTask = mQueue.getNextTask();
if (newTask == null) {
Log.w(TAG, "没有下一任务");
return;
}
if (newTask.getDownloadEntity().getState() == DownloadEntity.STATE_WAIT) {
mQueue.startTask(newTask);
}
}
/**
* 设置下载器监听
*
* @param targetListener {@link OnTargetListener}
*/
public void setOnTargetListener(OnTargetListener targetListener) {
this.mTargetListener = targetListener;
}
public void setFailNum(int mFailNum) {
this.mFailNum = mFailNum;
}
public void setTimeOut(long timeOut) {
this.mTimeOut = timeOut;
}
}

@ -2,42 +2,80 @@ package com.arialyy.downloadutil.core;
import android.content.Context;
import android.util.Log;
import com.arialyy.downloadutil.core.inf.IDownloadSchedulers;
import com.arialyy.downloadutil.core.inf.IDownloader;
import com.arialyy.downloadutil.core.inf.ITaskQueue;
import com.arialyy.downloadutil.core.pool.CachePool;
import com.arialyy.downloadutil.core.pool.ExecutePool;
/**
* 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;
public class DownloadTaskQueue implements ITaskQueue, IDownloader {
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 ExecutePool mExecutePool = ExecutePool.getInstance();
private Context mContext;
public static DownloadTarget getInstance() {
public static DownloadTaskQueue getInstance() {
if (INSTANCE == null) {
throw new NullPointerException("请在Application中调用init进行注册");
}
return INSTANCE;
}
static DownloadTarget init(Context context) {
static DownloadTaskQueue init(Context context) {
if (INSTANCE == null) {
synchronized (LOCK) {
INSTANCE = new DownloadTarget(context.getApplicationContext());
INSTANCE = new DownloadTaskQueue(context.getApplicationContext());
}
}
return INSTANCE;
}
private DownloadTarget() {
super();
private DownloadTaskQueue() {
}
private DownloadTarget(Context context) {
private DownloadTaskQueue(Context context) {
super();
mContext = context;
}
/**
* 获取任务执行池
*/
public ExecutePool getExecutePool() {
return mExecutePool;
}
/**
* 获取缓存池
*/
public CachePool getCachePool() {
return mCachePool;
}
/**
* 获取当前运行的任务数
*
* @return 当前正在执行的任务数
*/
public int getCurrentTaskNum() {
return mExecutePool.size();
}
/**
* 获取缓存任务数
*
* @return 获取缓存的任务数
*/
public int getCacheTaskNum() {
return mCachePool.size();
}
@Override public void startTask(Task task) {
if (mExecutePool.putTask(task)) {
mCachePool.removeTask(task);
@ -71,7 +109,14 @@ public class DownloadTarget extends IDownloadTarget {
}
@Override public Task createTask(DownloadEntity entity) {
Task task = TaskFactory.getInstance().createTask(mContext, entity, mTaskHandler);
return createTask(entity, null);
}
@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);
return task;
}

@ -1,226 +0,0 @@
package com.arialyy.downloadutil.core;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.arialyy.downloadutil.core.inf.IDownloader;
import com.arialyy.downloadutil.core.inf.ITask;
import com.arialyy.downloadutil.core.pool.CachePool;
import com.arialyy.downloadutil.core.pool.ExecutePool;
/**
* Created by lyy on 2016/8/16.
* 任务下载器提供抽象的方法供具体的实现类操作
*/
public abstract class IDownloadTarget implements IDownloader, ITask {
/**
* 任务开始
*/
public static final int START = 1;
/**
* 任务停止
*/
public static final int STOP = 2;
/**
* 任务失败
*/
public static final int FAIL = 3;
/**
* 任务取消
*/
public static final int CANCEL = 4;
/**
* 任务完成
*/
public static final int COMPLETE = 5;
protected CachePool mCachePool = CachePool.getInstance();
protected ExecutePool mExecutePool = ExecutePool.getInstance();
protected AutoTaskHandler mTaskHandler;
/**
* 下载失败次数
*/
protected int mFailNum = 10;
/**
* 超时时间
*/
protected long mTimeOut = 10000;
/**
* 下载器任务监听
*/
protected OnTargetListener mTargetListener;
/**
* Target处理任务监听
*/
public interface OnTargetListener {
/**
* 任务开始
*/
public void onTaskStart(Task task);
/**
* 任务停止
*/
public void onTaskStop(Task task);
/**
* 任务取消
*/
public void onTaskCancel(Task task);
/**
* 任务下载失败
*/
public void onTaskFail(Task task);
/**
* 任务完成
*/
public void onTaskComplete(Task task);
}
protected IDownloadTarget() {
mTaskHandler = new AutoTaskHandler(this);
}
/**
* 设置下载器监听
*
* @param targetListener {@link OnTargetListener}
*/
public void setOnTargetListener(OnTargetListener targetListener) {
this.mTargetListener = targetListener;
}
/**
* 获取任务执行池
*/
public ExecutePool getExecutePool() {
return mExecutePool;
}
/**
* 获取当前运行的任务数
*
* @return 当前正在执行的任务数
*/
public int getCurrentTaskNum() {
return mExecutePool.size();
}
/**
* 获取缓存任务数
*
* @return 获取缓存的任务数
*/
public int getCacheTaskNum() {
return mCachePool.size();
}
public void setFailNum(int mFailNum) {
this.mFailNum = mFailNum;
}
public void setTimeOut(long timeOut) {
this.mTimeOut = timeOut;
}
/**
* 自动处理任务停止下载失败取消下载自动下载下一个任务的操作
*/
private static class AutoTaskHandler extends Handler {
private static final String TAG = "AutoTaskHandler";
IDownloadTarget target;
public AutoTaskHandler(IDownloadTarget target) {
this.target = target;
}
@Override public void handleMessage(Message msg) {
super.handleMessage(msg);
DownloadEntity entity = (DownloadEntity) msg.obj;
if (entity == null) {
Log.e(TAG, "请传入下载实体DownloadEntity");
return;
}
switch (msg.what) {
case STOP:
case CANCEL:
if (target.mExecutePool.size() != ExecutePool.SIZE) {
startNextTask(entity);
}
break;
case COMPLETE:
startNextTask(entity);
break;
case FAIL:
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);
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.onTaskComplete(task);
break;
case FAIL:
target.mTargetListener.onTaskFail(task);
break;
}
}
}
/**
* 处理下载任务下载失败的情形
*
* @param entity 失败实体
*/
private void handleFailTask(DownloadEntity entity) {
if (entity.getFailNum() <= target.mFailNum) {
Task task = target.getTask(entity);
target.reTryStart(task);
} else {
startNextTask(entity);
}
}
/**
* 启动下一个任务条件任务停止取消下载任务完成
*
* @param entity 通过Handler传递的下载实体
*/
private void startNextTask(DownloadEntity entity) {
target.removeTask(entity);
Task newTask = target.getNextTask();
if (newTask == null) {
Log.w(TAG, "没有下一任务");
return;
}
if (newTask.getDownloadEntity().getState() == DownloadEntity.STATE_WAIT) {
target.startTask(newTask);
}
}
}
}

@ -5,6 +5,7 @@ import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;
import com.arialyy.downloadutil.core.inf.IDownloadSchedulers;
import com.arialyy.downloadutil.core.inf.IDownloadUtil;
/**
@ -58,7 +59,7 @@ public class Task {
} else {
mEntity.setState(DownloadEntity.STATE_STOP);
mEntity.save();
sendInState2Target(IDownloadTarget.STOP);
sendInState2Target(DownloadSchedulers.STOP);
// 发送停止下载的广播
Intent intent = createIntent(DownloadManager.ACTION_STOP);
@ -94,7 +95,7 @@ public class Task {
mUtil.delConfigFile();
mUtil.delTempFile();
mEntity.deleteData();
sendInState2Target(IDownloadTarget.CANCEL);
sendInState2Target(DownloadSchedulers.CANCEL);
//发送取消下载的广播
Intent intent = createIntent(DownloadManager.ACTION_CANCEL);
@ -118,7 +119,7 @@ public class Task {
/**
* 将任务状态发送给下载器
*
* @param state {@link IDownloadTarget#START}
* @param state {@link DownloadSchedulers#START}
*/
private void sendInState2Target(int state) {
if (mOutHandler != null) {
@ -170,7 +171,7 @@ public class Task {
@Override public void onStart(long startLocation) {
super.onStart(startLocation);
downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING);
sendInState2Target(IDownloadTarget.START);
sendInState2Target(DownloadSchedulers.START);
sendIntent(DownloadManager.ACTION_START, startLocation);
}
@ -197,14 +198,14 @@ public class Task {
super.onStop(stopLocation);
downloadEntity.setState(DownloadEntity.STATE_STOP);
downloadEntity.setSpeed(0);
sendInState2Target(IDownloadTarget.STOP);
sendInState2Target(DownloadSchedulers.STOP);
sendIntent(DownloadManager.ACTION_STOP, stopLocation);
}
@Override public void onCancel() {
super.onCancel();
downloadEntity.setState(DownloadEntity.STATE_CANCEL);
sendInState2Target(IDownloadTarget.CANCEL);
sendInState2Target(DownloadSchedulers.CANCEL);
sendIntent(DownloadManager.ACTION_CANCEL, -1);
downloadEntity.deleteData();
}
@ -214,7 +215,7 @@ public class Task {
downloadEntity.setState(DownloadEntity.STATE_COMPLETE);
downloadEntity.setDownloadComplete(true);
downloadEntity.setSpeed(0);
sendInState2Target(IDownloadTarget.COMPLETE);
sendInState2Target(DownloadSchedulers.COMPLETE);
sendIntent(DownloadManager.ACTION_COMPLETE, downloadEntity.getFileSize());
}
@ -222,7 +223,7 @@ public class Task {
super.onFail();
downloadEntity.setState(DownloadEntity.STATE_FAIL);
downloadEntity.setSpeed(0);
sendInState2Target(IDownloadTarget.FAIL);
sendInState2Target(DownloadSchedulers.FAIL);
sendIntent(DownloadManager.ACTION_FAIL, -1);
}
@ -254,10 +255,10 @@ public class Task {
/**
* 设置自定义Handler处理下载状态时间
*
* @param outHandler {@link IDownloadTarget.AutoTaskHandler}
* @param schedulers {@link IDownloadSchedulers}
*/
public Builder setOutHandler(Handler outHandler) {
this.outHandler = outHandler;
public Builder setOutHandler(IDownloadSchedulers schedulers) {
this.outHandler = new Handler(schedulers);
return this;
}

@ -1,7 +1,7 @@
package com.arialyy.downloadutil.core;
import android.content.Context;
import android.os.Handler;
import com.arialyy.downloadutil.core.inf.IDownloadSchedulers;
/**
* Created by lyy on 2016/8/18.
@ -30,11 +30,11 @@ public class TaskFactory {
* 创建普通下载任务
*
* @param entity 下载实体
* @param handler "com.arialyy.downloadutil.core.IDownloadTarget.AutoTaskHandler"
* @param schedulers {@link IDownloadSchedulers}
*/
public Task createTask(Context context, DownloadEntity entity, Handler handler) {
public Task createTask(Context context, DownloadEntity entity, IDownloadSchedulers schedulers) {
Task.Builder builder = new Task.Builder(context, entity);
builder.setOutHandler(handler);
builder.setOutHandler(schedulers);
return builder.build();
}
}

@ -28,10 +28,6 @@ public class CmdFactory {
* 停止任务
*/
public static final int TASK_STOP = 0x125;
/**
* 获取任务状态
*/
public static final int TASK_STATE = 0x126;
private static final Object LOCK = new Object();
private static volatile CmdFactory INSTANCE = null;
@ -53,7 +49,7 @@ public class CmdFactory {
* @param context context
* @param entity 下载实体
* @param type 命令类型{@link #TASK_CREATE}{@link #TASK_START}{@link #TASK_CANCEL}{@link
* #TASK_STOP}{@link #TASK_STATE}
* #TASK_STOP}
*/
public IDownloadCmd createCmd(Context context, DownloadEntity entity, int type) {
switch (type) {
@ -66,22 +62,11 @@ public class CmdFactory {
return createCancelCmd(context, entity);
case TASK_STOP:
return createStopCmd(context, entity);
case TASK_STATE:
return createStateCmd(context, entity);
default:
return null;
}
}
/**
* 创建获取任务状态的命令
*
* @return {@link StateCmd}
*/
private StateCmd createStateCmd(Context context, DownloadEntity entity) {
return new StateCmd(context, entity);
}
/**
* 创建停止命令
*

@ -1,9 +1,8 @@
package com.arialyy.downloadutil.core.command;
import android.content.Context;
import com.arialyy.downloadutil.core.DownloadTarget;
import com.arialyy.downloadutil.core.IDownloadTarget;
import com.arialyy.downloadutil.core.DownloadEntity;
import com.arialyy.downloadutil.core.DownloadTaskQueue;
import com.arialyy.downloadutil.help.CheckHelp;
import com.arialyy.downloadutil.util.Util;
@ -12,10 +11,10 @@ import com.arialyy.downloadutil.util.Util;
* 下载命令
*/
public abstract class IDownloadCmd {
protected IDownloadTarget target;
protected Context mContext;
protected DownloadEntity mEntity;
protected String TAG;
DownloadTaskQueue target;
Context mContext;
DownloadEntity mEntity;
String TAG;
/**
* @param context context
@ -25,7 +24,7 @@ public abstract class IDownloadCmd {
if (!CheckHelp.checkDownloadEntity(entity)) {
return;
}
target = DownloadTarget.getInstance();
target = DownloadTaskQueue.getInstance();
mContext = context;
mEntity = entity;
TAG = Util.getClassName(this);
@ -43,9 +42,9 @@ public abstract class IDownloadCmd {
/**
* 设置下载器
*
* @param downloadTarget {@link IDownloadTarget}
* @param downloadTarget {@link DownloadTaskQueue}
*/
public void setDownloadTarget(IDownloadTarget downloadTarget) {
public void setDownloadQueue(DownloadTaskQueue downloadTarget) {
target = downloadTarget;
}
}

@ -1,24 +0,0 @@
package com.arialyy.downloadutil.core.command;
import android.content.Context;
import com.arialyy.downloadutil.core.DownloadEntity;
/**
* Created by lyy on 2016/9/20.
* 获取下载状态的命令
*/
class StateCmd extends IDownloadCmd {
/**
* @param context context
* @param entity 下载实体
*/
StateCmd(Context context, DownloadEntity entity) {
super(context, entity);
}
@Override public void executeComment() {
target.getTaskState(mEntity);
}
}

@ -0,0 +1,25 @@
package com.arialyy.downloadutil.core.inf;
import android.os.Handler;
import com.arialyy.downloadutil.core.DownloadEntity;
/**
* Created by AriaLyy@outlook.com on 2016/11/2.
* 下载调度器接口
*/
public interface IDownloadSchedulers extends Handler.Callback {
/**
* 处理下载任务下载失败的情形
*
* @param entity 下载实体
*/
public void handleFailTask(DownloadEntity entity);
/**
* 启动下一个任务条件任务停止取消下载任务完成
*
* @param entity 通过Handler传递的下载实体
*/
public void startNextTask(DownloadEntity entity);
}

@ -7,7 +7,7 @@ import com.arialyy.downloadutil.core.Task;
* Created by lyy on 2016/8/16.
* 任务功能接口
*/
public interface ITask {
public interface ITaskQueue {
/**
* 创建一个新的下载任务创建时只是将新任务存储到缓存池
@ -17,6 +17,15 @@ public interface ITask {
*/
public Task createTask(DownloadEntity entity);
/**
* 创建一个新的下载任务创建时只是将新任务存储到缓存池
*
* @param entity 下载实体{@link DownloadEntity}
* @param schedulers 下载调度器{@link IDownloadSchedulers}
* @return {@link Task}
*/
public Task createTask(DownloadEntity entity, IDownloadSchedulers schedulers);
/**
* 通过下载链接从缓存池或任务池搜索下载任务如果缓存池或任务池都没有任务则创建新任务
*

@ -0,0 +1,10 @@
package com.arialyy.downloadutil.help;
/**
* Created by AriaLyy@outlook.com on 2016/11/2.
*/
public class LockHelp {
public static final Object LOCK = new Object();
}
Loading…
Cancel
Save