添加单任务下载功能

This commit is contained in:
AriaLyy
2016-11-30 18:12:15 +08:00
parent e0c2538f36
commit f1e28fe491
14 changed files with 288 additions and 142 deletions

View File

@ -116,8 +116,9 @@ public class DownloadEntity extends DbEntity implements Parcelable {
return fileName;
}
public void setFileName(String fileName) {
public DownloadEntity setFileName(String fileName) {
this.fileName = fileName;
return this;
}
public int getFailNum() {
@ -132,8 +133,9 @@ public class DownloadEntity extends DbEntity implements Parcelable {
return downloadUrl;
}
public void setDownloadUrl(String downloadUrl) {
public DownloadEntity setDownloadUrl(String downloadUrl) {
this.downloadUrl = downloadUrl;
return this;
}
public long getCompleteTime() {
@ -148,8 +150,9 @@ public class DownloadEntity extends DbEntity implements Parcelable {
return downloadPath;
}
public void setDownloadPath(String downloadPath) {
public DownloadEntity setDownloadPath(String downloadPath) {
this.downloadPath = downloadPath;
return this;
}
public long getFileSize() {

View File

@ -14,7 +14,6 @@
* limitations under the License.
*/
package com.arialyy.downloadutil.core;
import android.app.Application;
@ -23,6 +22,7 @@ import android.util.Log;
import com.arialyy.downloadutil.core.command.IDownloadCmd;
import com.arialyy.downloadutil.core.queue.ITaskQueue;
import com.arialyy.downloadutil.core.queue.DownloadTaskQueue;
import com.arialyy.downloadutil.core.scheduler.OnSchedulerListener;
import com.arialyy.downloadutil.orm.DbEntity;
import com.arialyy.downloadutil.orm.DbUtil;
import java.util.ArrayList;
@ -36,57 +36,58 @@ public class DownloadManager {
/**
* 预处理完成
*/
public static final String ACTION_PRE = "ACTION_PRE";
public static final String ACTION_PRE = "ACTION_PRE";
/**
* 下载开始前事件
*/
public static final String ACTION_POST_PRE = "ACTION_POST_PRE";
public static final String ACTION_POST_PRE = "ACTION_POST_PRE";
/**
* 开始下载事件
*/
public static final String ACTION_START = "ACTION_START";
public static final String ACTION_START = "ACTION_START";
/**
* 恢复下载事件
*/
public static final String ACTION_RESUME = "ACTION_RESUME";
public static final String ACTION_RESUME = "ACTION_RESUME";
/**
* 正在下载事件
*/
public static final String ACTION_RUNNING = "ACTION_RUNNING";
public static final String ACTION_RUNNING = "ACTION_RUNNING";
/**
* 停止下载事件
*/
public static final String ACTION_STOP = "ACTION_STOP";
public static final String ACTION_STOP = "ACTION_STOP";
/**
* 取消下载事件
*/
public static final String ACTION_CANCEL = "ACTION_CANCEL";
public static final String ACTION_CANCEL = "ACTION_CANCEL";
/**
* 下载完成事件
*/
public static final String ACTION_COMPLETE = "ACTION_COMPLETE";
public static final String ACTION_COMPLETE = "ACTION_COMPLETE";
/**
* 下载失败事件
*/
public static final String ACTION_FAIL = "ACTION_FAIL";
public static final String ACTION_FAIL = "ACTION_FAIL";
/**
* 下载实体
*/
public static final String ENTITY = "DOWNLOAD_ENTITY";
public static final String ENTITY = "DOWNLOAD_ENTITY";
/**
* 位置
*/
public static final String CURRENT_LOCATION = "CURRENT_LOCATION";
public static final String CURRENT_LOCATION = "CURRENT_LOCATION";
/**
* 速度
*/
public static final String CURRENT_SPEED = "CURRENT_SPEED";
private static final String TAG = "DownloadManager";
private static final Object LOCK = new Object();
private static volatile DownloadManager INSTANCE = null;
private List<IDownloadCmd> mCommands = new ArrayList<>();
private Context mContext;
public static final String CURRENT_SPEED = "CURRENT_SPEED";
private static final String TAG = "DownloadManager";
private static final Object LOCK = new Object();
private static volatile DownloadManager INSTANCE = null;
private List<IDownloadCmd> mCommands = new ArrayList<>();
private Context mContext;
private ITaskQueue mTaskQueue;
private DownloadEntity mTempDEntity;
private DownloadManager() {
@ -123,6 +124,15 @@ public class DownloadManager {
return DbEntity.findAllData(DownloadEntity.class);
}
/**
* 注册
* @param listener
*/
public DownloadManager regSchedulerListener(OnSchedulerListener listener) {
mTaskQueue.getDownloadSchedulers().regTargetListener(listener);
return this;
}
/**
* 获取任务队列
*/

View File

@ -44,6 +44,7 @@ public class CmdFactory {
* 停止任务
*/
public static final int TASK_STOP = 0x125;
public static final int TASK_SINGLE = 0x126;
private static final Object LOCK = new Object();
private static volatile CmdFactory INSTANCE = null;
@ -77,11 +78,14 @@ public class CmdFactory {
return createCancelCmd(entity);
case TASK_STOP:
return createStopCmd(entity);
case TASK_SINGLE:
return new SingleCmd(entity);
default:
return null;
}
}
/**
* 创建停止命令
*

View File

@ -0,0 +1,28 @@
package com.arialyy.downloadutil.core.command;
import android.util.Log;
import com.arialyy.downloadutil.core.DownloadEntity;
import com.arialyy.downloadutil.core.task.Task;
/**
* Created by lyy on 2016/11/30.
* 获取任务状态命令
*/
class SingleCmd extends IDownloadCmd {
/**
* @param entity 下载实体
*/
SingleCmd(DownloadEntity entity) {
super(entity);
}
@Override public void executeCmd() {
Task task = mQueue.getTask(mEntity);
if (task == null) {
task = mQueue.createTask(mEntity);
} else {
Log.w(TAG, "添加命令执行失败,【该任务已经存在】");
}
mQueue.startTask(task);
}
}

View File

@ -111,6 +111,10 @@ public class DownloadTaskQueue implements ITaskQueue {
}
}
@Override public IDownloadSchedulers getDownloadSchedulers() {
return mSchedulers;
}
@Override public int size() {
return mExecutePool.size();
}

View File

@ -26,6 +26,11 @@ import com.arialyy.downloadutil.core.task.Task;
*/
public interface ITaskQueue extends IDownloader {
/**
* 获取调度器
*/
public IDownloadSchedulers getDownloadSchedulers();
/**
* 任务池队列大小
*/

View File

@ -14,7 +14,6 @@
* limitations under the License.
*/
package com.arialyy.downloadutil.core.scheduler;
import android.os.Message;
@ -33,25 +32,29 @@ public class DownloadSchedulers implements IDownloadSchedulers {
/**
* 任务开始
*/
public static final int START = 1;
public static final int START = 1;
/**
* 任务停止
*/
public static final int STOP = 2;
public static final int STOP = 2;
/**
* 任务失败
*/
public static final int FAIL = 3;
public static final int FAIL = 3;
/**
* 任务取消
*/
public static final int CANCEL = 4;
public static final int CANCEL = 4;
/**
* 任务完成
*/
public static final int COMPLETE = 5;
private static final String TAG = "DownloadSchedulers";
private static final Object LOCK = new Object();
public static final int COMPLETE = 5;
/**
* 下载中
*/
public static final int RUNNING = 6;
private static final String TAG = "DownloadSchedulers";
private static final Object LOCK = new Object();
private static volatile DownloadSchedulers INSTANCE = null;
/**
* 下载失败次数
@ -66,8 +69,8 @@ public class DownloadSchedulers implements IDownloadSchedulers {
/**
* 下载器任务监听
*/
OnTargetListener mTargetListener;
ITaskQueue mQueue;
OnSchedulerListener mTargetListener;
ITaskQueue mQueue;
public DownloadSchedulers(ITaskQueue downloadTaskQueue) {
mQueue = downloadTaskQueue;
@ -116,6 +119,9 @@ public class DownloadSchedulers implements IDownloadSchedulers {
if (mTargetListener != null) {
Task task = mQueue.getTask(entity);
switch (state) {
case RUNNING:
mTargetListener.onTaskRunning(task);
break;
case START:
mTargetListener.onTaskStart(task);
break;
@ -166,15 +172,14 @@ public class DownloadSchedulers implements IDownloadSchedulers {
}
}
/**
* 设置下载器监听
*
* @param targetListener {@link OnTargetListener}
*/
public void setOnTargetListener(OnTargetListener targetListener) {
@Override public void regTargetListener(OnSchedulerListener targetListener) {
this.mTargetListener = targetListener;
}
@Override public void unRegTargetListener(OnSchedulerListener targetListener) {
mTargetListener = null;
}
public void setFailNum(int mFailNum) {
this.mFailNum = mFailNum;
}
@ -182,34 +187,4 @@ public class DownloadSchedulers implements IDownloadSchedulers {
public void setTimeOut(long timeOut) {
this.mTimeOut = timeOut;
}
/**
* 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);
}
}

View File

@ -14,7 +14,6 @@
* limitations under the License.
*/
package com.arialyy.downloadutil.core.scheduler;
import android.os.Handler;
@ -26,6 +25,18 @@ import com.arialyy.downloadutil.core.DownloadEntity;
*/
public interface IDownloadSchedulers extends Handler.Callback {
/**
* 注册下载器监听
*
* @param targetListener {@link OnSchedulerListener}
*/
public void regTargetListener(OnSchedulerListener targetListener);
/**
* 取消注册监听器
*/
public void unRegTargetListener(OnSchedulerListener targetListener);
/**
* 处理下载任务下载失败的情形
*

View File

@ -0,0 +1,38 @@
package com.arialyy.downloadutil.core.scheduler;
import com.arialyy.downloadutil.core.task.Task;
/**
* Target处理任务监听
*/
public interface OnSchedulerListener {
/**
* 任务开始
*/
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 void onTaskRunning(Task task);
}

View File

@ -257,6 +257,7 @@ public class Task {
}
downloadEntity.setCurrentProgress(currentLocation);
lastLen = currentLocation;
sendInState2Target(DownloadSchedulers.RUNNING);
context.sendBroadcast(sendIntent);
}
}

View File

@ -17,10 +17,8 @@
package com.arialyy.downloadutil.util;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.downloadutil.R;
import com.arialyy.downloadutil.core.DownloadEntity;
import java.io.File;
@ -39,16 +37,16 @@ public class CheckUtil {
*/
public static boolean checkDownloadEntity(DownloadEntity entity) {
if (entity == null) {
Log.w(TAG, Resources.getSystem().getString(R.string.error_entity_null));
Log.w(TAG, "下载实体不能为空");
return false;
} else if (TextUtils.isEmpty(entity.getDownloadUrl())) {
Log.w(TAG, Resources.getSystem().getString(R.string.error_download_url_null));
Log.w(TAG, "下载链接不能为空");
return false;
} else if (TextUtils.isEmpty(entity.getFileName())) {
Log.w(TAG, Resources.getSystem().getString(R.string.error_file_name_null));
Log.w(TAG, "文件名不能为空");
return false;
} else if (TextUtils.isEmpty(entity.getDownloadPath())) {
Log.w(TAG, Resources.getSystem().getString(R.string.error_download_path_null));
Log.w(TAG, "存储地址不能为空");
return false;
}
String fileName = entity.getFileName();

View File

@ -251,6 +251,8 @@ public class CommonUtil {
public static Properties loadConfig(File file) {
Properties properties = new Properties();
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
properties.load(fis);