target 编写

pull/330/head
AriaLyy 8 years ago
parent 4e305dcf01
commit aff73f92f9
  1. 56
      Aria/src/main/java/com/arialyy/aria/core/Aria.java
  2. 13
      Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java
  3. 37
      Aria/src/main/java/com/arialyy/aria/core/command/group/GroupStartCmd.java
  4. 3
      Aria/src/main/java/com/arialyy/aria/core/command/normal/AddCmd.java
  5. 5
      Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelCmd.java
  6. 10
      Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java
  7. 6
      Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java
  8. 6
      Aria/src/main/java/com/arialyy/aria/core/command/normal/StopCmd.java
  9. 44
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java
  10. 37
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
  11. 78
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java
  12. 15
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupEntity.java
  13. 58
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTarget.java
  14. 173
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalTarget.java
  15. 181
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java
  16. 19
      Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java
  17. 22
      Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
  18. 5
      app/src/main/assets/aria_config.xml
  19. 14
      aria/src/main/java/com/arialyy/aria/core/Configuration.java
  20. 78
      aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java

@ -59,62 +59,6 @@ import com.arialyy.aria.core.upload.UploadTask;
* </pre>
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) public class Aria {
/**
* 不支持断点
*/
public static final String ACTION_SUPPORT_BREAK_POINT = "ACTION_SUPPORT_BREAK_POINT";
/**
* 预处理完成
*/
public static final String ACTION_PRE = "ACTION_PRE";
/**
* 下载开始前事件
*/
public static final String ACTION_POST_PRE = "ACTION_POST_PRE";
/**
* 开始下载事件
*/
public static final String ACTION_START = "ACTION_START";
/**
* 恢复下载事件
*/
public static final String ACTION_RESUME = "ACTION_RESUME";
/**
* 正在下载事件
*/
public static final String ACTION_RUNNING = "ACTION_RUNNING";
/**
* 停止下载事件
*/
public static final String ACTION_STOP = "ACTION_STOP";
/**
* 取消下载事件
*/
public static final String ACTION_CANCEL = "ACTION_CANCEL";
/**
* 下载完成事件
*/
public static final String ACTION_COMPLETE = "ACTION_COMPLETE";
/**
* 下载失败事件
*/
public static final String ACTION_FAIL = "ACTION_FAIL";
/**
* 下载实体
*/
public static final String DOWNLOAD_ENTITY = "DOWNLOAD_ENTITY";
/**
* 上传实体
*/
public static final String UPLOAD_ENTITY = "UPLOAD_ENTITY";
/**
* 位置
*/
public static final String CURRENT_LOCATION = "CURRENT_LOCATION";
/**
* 速度
*/
public static final String CURRENT_SPEED = "CURRENT_SPEED";
private Aria() {
}

@ -54,9 +54,6 @@ class ConfigHelper extends DefaultHandler {
case "threadNum":
loadThreadNum(value);
break;
case "openBroadcast":
loadBroadcast(value);
break;
case "maxTaskNum":
loadMaxQueue(value);
break;
@ -224,16 +221,6 @@ class ConfigHelper extends DefaultHandler {
}
}
private void loadBroadcast(String value) {
boolean open = Boolean.parseBoolean(value);
if (isDownloadConfig) {
mDownloadConfig.isOpenBreadCast = open;
}
if (isUploadConfig) {
mUploadConfig.isOpenBreadCast = open;
}
}
private void loadThreadNum(String value) {
int num = 3;
if (!TextUtils.isEmpty(value)) {

@ -15,7 +15,12 @@
*/
package com.arialyy.aria.core.command.group;
import android.text.TextUtils;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.QueueMod;
import com.arialyy.aria.core.inf.AbsGroupTask;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.inf.IEntity;
/**
* Created by AriaL on 2017/6/29.
@ -30,6 +35,38 @@ class GroupStartCmd<T extends AbsTaskEntity> extends AbsGroupCmd<T> {
}
@Override public void executeCmd() {
String mod;
int maxTaskNum;
AriaManager manager = AriaManager.getInstance(AriaManager.APP);
if (isDownloadCmd) {
mod = manager.getDownloadConfig().getQueueMod();
maxTaskNum = manager.getDownloadConfig().getMaxTaskNum();
} else {
mod = manager.getUploadConfig().getQueueMod();
maxTaskNum = manager.getUploadConfig().getMaxTaskNum();
}
AbsGroupTask task = (AbsGroupTask) mQueue.getTask(mTaskEntity.getEntity());
if (task == null) {
task = (AbsGroupTask) mQueue.createTask(mTargetName, mTaskEntity);
if (!TextUtils.isEmpty(mTargetName)) {
task.setTargetName(mTargetName);
}
// 任务不存在时,根据配置不同,对任务执行操作
if (mod.equals(QueueMod.NOW.getTag())) {
mQueue.startTask(task);
} else if (mod.equals(QueueMod.WAIT.getTag())) {
if (mQueue.getExePoolSize() < maxTaskNum) {
mQueue.startTask(task);
}
}
} else {
// 任务不存在时,根据配置不同,对任务执行操作
if (!task.isRunning()
&& mod.equals(QueueMod.WAIT.getTag())
&& task.getState() == IEntity.STATE_WAIT) {
mQueue.startTask(task);
}
}
}
}

@ -18,6 +18,7 @@ package com.arialyy.aria.core.command.normal;
import android.util.Log;
import com.arialyy.aria.core.inf.AbsNormalTask;
import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity;
@ -33,7 +34,7 @@ class AddCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
@Override public void executeCmd() {
if (!canExeCmd) return;
AbsNormalTask task = (AbsNormalTask) mQueue.getTask(mTaskEntity.getEntity());
AbsTask task = mQueue.getTask(mTaskEntity.getEntity());
if (task == null) {
mTaskEntity.getEntity().setState(IEntity.STATE_WAIT);
mQueue.createTask(mTargetName, mTaskEntity);

@ -18,6 +18,7 @@ package com.arialyy.aria.core.command.normal;
import android.text.TextUtils;
import com.arialyy.aria.core.inf.AbsNormalTask;
import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.AbsTaskEntity;
/**
@ -31,9 +32,9 @@ class CancelCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
@Override public void executeCmd() {
if (!canExeCmd) return;
AbsNormalTask task = (AbsNormalTask) mQueue.getTask(mTaskEntity.getEntity());
AbsTask task = mQueue.getTask(mTaskEntity.getEntity());
if (task == null) {
task = (AbsNormalTask) mQueue.createTask(mTargetName, mTaskEntity);
task = mQueue.createTask(mTargetName, mTaskEntity);
}
if (task != null) {
if (!TextUtils.isEmpty(mTargetName)) {

@ -3,7 +3,7 @@ package com.arialyy.aria.core.command.normal;
import android.util.Log;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.inf.AbsNormalTask;
import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.orm.DbEntity;
@ -29,7 +29,7 @@ final class ResumeAllCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
for (DownloadEntity entity : allEntity) {
int exeNum = mQueue.getExePoolSize();
if (exeNum == 0 || exeNum < mQueue.getMaxTaskNum()) {
AbsNormalTask task = createTask(entity);
AbsTask task = createTask(entity);
mQueue.startTask(task);
} else {
entity.setState(IEntity.STATE_WAIT);
@ -38,11 +38,11 @@ final class ResumeAllCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
}
}
private AbsNormalTask createTask(DownloadEntity entity) {
AbsNormalTask task = (AbsNormalTask) mQueue.getTask(entity);
private AbsTask createTask(DownloadEntity entity) {
AbsTask task = mQueue.getTask(entity);
if (task == null) {
DownloadTaskEntity taskEntity = new DownloadTaskEntity(entity);
task = (AbsNormalTask) mQueue.createTask(mTargetName, taskEntity);
task = mQueue.createTask(mTargetName, taskEntity);
} else {
Log.w(TAG, "添加命令执行失败,【该任务已经存在】");
}

@ -19,7 +19,7 @@ package com.arialyy.aria.core.command.normal;
import android.text.TextUtils;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.QueueMod;
import com.arialyy.aria.core.inf.AbsNormalTask;
import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity;
@ -47,9 +47,9 @@ class StartCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
maxTaskNum = manager.getUploadConfig().getMaxTaskNum();
}
AbsNormalTask task = (AbsNormalTask) mQueue.getTask(mTaskEntity.getEntity());
AbsTask task = mQueue.getTask(mTaskEntity.getEntity());
if (task == null) {
task = (AbsNormalTask) mQueue.createTask(mTargetName, mTaskEntity);
task = mQueue.createTask(mTargetName, mTaskEntity);
if (!TextUtils.isEmpty(mTargetName)) {
task.setTargetName(mTargetName);
}

@ -18,7 +18,7 @@ package com.arialyy.aria.core.command.normal;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.inf.AbsNormalTask;
import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity;
@ -34,10 +34,10 @@ class StopCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
@Override public void executeCmd() {
if (!canExeCmd) return;
AbsNormalTask task = (AbsNormalTask) mQueue.getTask(mTaskEntity.getEntity());
AbsTask task = mQueue.getTask(mTaskEntity.getEntity());
if (task == null) {
if (mTaskEntity.getEntity().getState() == IEntity.STATE_RUNNING) {
task = (AbsNormalTask) mQueue.createTask(mTargetName, mTaskEntity);
task = mQueue.createTask(mTargetName, mTaskEntity);
mQueue.stopTask(task);
} else {
Log.w(TAG, "停止命令执行失败,【调度器中没有该任务】");

@ -16,23 +16,57 @@
package com.arialyy.aria.core.download;
import com.arialyy.aria.core.inf.AbsGroupTarget;
import com.arialyy.aria.core.inf.ITarget;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.util.CheckUtil;
import java.io.File;
import java.util.List;
/**
* Created by AriaL on 2017/6/29.
*/
public class DownloadGroupTarget extends AbsGroupTarget<DownloadGroupTarget, DownloadTaskEntity> {
public class DownloadGroupTarget
extends AbsGroupTarget<DownloadGroupTarget, DownloadGroupEntity, DownloadGroupTaskEntity> {
private List<String> mUrls;
DownloadGroupTarget(DownloadGroupEntity entity, String targetName, List<String> urls) {
this.mEntity = entity;
this.mTargetName = targetName;
this.mUrls = urls;
mTaskEntity = new DownloadGroupTaskEntity(entity);
}
/**
* 设置保存路径组
*/
public DownloadGroupTarget setDownloadPaths(List<String> paths) {
CheckUtil.checkDownloadPaths(paths);
if (mUrls.size() != paths.size()) {
throw new IllegalArgumentException("下载链接数必须要和保存路径的数量一致");
}
for (int i = 0, len = mUrls.size(); i < len; i++) {
mTaskEntity.getEntity().getChild().add(createDownloadEntity(mUrls.get(i), paths.get(i)));
}
return this;
}
@Override public int getPercent() {
return 0;
/**
* 创建子任务下载实体
*
* @param url 下载地址
* @param path 保存路径
*/
private DownloadEntity createDownloadEntity(String url, String path) {
DownloadEntity entity = DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", url);
if (entity == null) {
entity = new DownloadEntity();
}
File file = new File(path);
if (!file.exists()) {
entity.setState(IEntity.STATE_WAIT);
}
entity.setDownloadPath(path);
entity.setDownloadUrl(url);
entity.setFileName(file.getName());
return entity;
}
}

@ -23,6 +23,7 @@ import com.arialyy.aria.core.inf.IReceiver;
import com.arialyy.aria.core.command.normal.NormalCmdFactory;
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
import com.arialyy.aria.core.scheduler.ISchedulerListener;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
import java.io.File;
@ -34,7 +35,7 @@ import java.util.Set;
* 下载功能接收器
*/
public class DownloadReceiver extends AbsReceiver<DownloadEntity> {
private static final String TAG = "DownloadReceiver";
private final String TAG = "DownloadReceiver";
public ISchedulerListener<DownloadTask> listener;
/**
@ -73,15 +74,17 @@ public class DownloadReceiver extends AbsReceiver<DownloadEntity> {
}
/**
* 添加调度器回调
*
* @see #register()
* 加载下载地址如果任务组的中的下载地址改变了则任务从新的一个任务组
*/
@Deprecated public DownloadReceiver addSchedulerListener(
ISchedulerListener<DownloadTask> listener) {
this.listener = listener;
DownloadSchedulers.getInstance().addSchedulerListener(targetName, listener);
return this;
public DownloadGroupTarget load(List<String> urls) {
CheckUtil.checkDownloadUrls(urls);
DownloadGroupEntity entity =
DbEntity.findData(DownloadGroupEntity.class, "urlHash=?", CommonUtil.getMd5Code(urls));
if (entity == null) {
entity = new DownloadGroupEntity();
}
return new DownloadGroupTarget(entity, targetName, urls);
}
/**
@ -99,6 +102,18 @@ public class DownloadReceiver extends AbsReceiver<DownloadEntity> {
DownloadSchedulers.getInstance().unRegister(obj);
}
/**
* 添加调度器回调
*
* @see #register()
*/
@Deprecated public DownloadReceiver addSchedulerListener(
ISchedulerListener<DownloadTask> listener) {
this.listener = listener;
DownloadSchedulers.getInstance().addSchedulerListener(targetName, listener);
return this;
}
/**
* 移除回调
*
@ -165,8 +180,8 @@ public class DownloadReceiver extends AbsReceiver<DownloadEntity> {
@Override public void removeAllTask(boolean removeFile) {
final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP);
AriaManager.getInstance(AriaManager.APP)
.setCmd(
CommonUtil.createCmd(targetName, new DownloadTaskEntity(), NormalCmdFactory.TASK_CANCEL_ALL))
.setCmd(CommonUtil.createCmd(targetName, new DownloadTaskEntity(),
NormalCmdFactory.TASK_CANCEL_ALL))
.exe();
Set<String> keys = ariaManager.getReceiver().keySet();

@ -20,7 +20,6 @@ import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.downloader.DownloadListener;
import com.arialyy.aria.core.download.downloader.DownloadUtil;
@ -144,11 +143,6 @@ public class DownloadTask extends AbsNormalTask<DownloadEntity> {
if (mOutHandler != null) {
mOutHandler.obtainMessage(DownloadSchedulers.STOP, this).sendToTarget();
}
// 发送停止下载的广播
Intent intent = CommonUtil.createIntent(mContext.getPackageName(), Aria.ACTION_STOP);
intent.putExtra(Aria.CURRENT_LOCATION, mEntity.getCurrentProgress());
intent.putExtra(Aria.DOWNLOAD_ENTITY, mEntity);
mContext.sendBroadcast(intent);
}
}
@ -161,10 +155,6 @@ public class DownloadTask extends AbsNormalTask<DownloadEntity> {
if (mOutHandler != null) {
mOutHandler.obtainMessage(DownloadSchedulers.CANCEL, this).sendToTarget();
}
//发送取消下载的广播
Intent intent = CommonUtil.createIntent(mContext.getPackageName(), Aria.ACTION_CANCEL);
intent.putExtra(Aria.DOWNLOAD_ENTITY, mEntity);
mContext.sendBroadcast(intent);
}
mUtil.cancelDownload();
}
@ -206,14 +196,12 @@ public class DownloadTask extends AbsNormalTask<DownloadEntity> {
WeakReference<Handler> outHandler;
WeakReference<DownloadTask> wTask;
Context context;
Intent sendIntent;
long lastLen = 0; //上一次发送长度
long lastTime = 0;
long INTERVAL_TIME = 1000; //1m更新周期
boolean isFirst = true;
DownloadEntity downloadEntity;
DownloadEntity entity;
DownloadTask task;
boolean isOpenBroadCast = false;
boolean isConvertSpeed = false;
DListener(Context context, DownloadTask task, Handler outHandler) {
@ -221,44 +209,36 @@ public class DownloadTask extends AbsNormalTask<DownloadEntity> {
this.outHandler = new WeakReference<>(outHandler);
this.wTask = new WeakReference<>(task);
this.task = wTask.get();
this.downloadEntity = this.task.getDownloadEntity();
sendIntent = CommonUtil.createIntent(context.getPackageName(), Aria.ACTION_RUNNING);
sendIntent.putExtra(Aria.DOWNLOAD_ENTITY, downloadEntity);
this.entity = this.task.getDownloadEntity();
final AriaManager manager = AriaManager.getInstance(context);
isOpenBroadCast = manager.getDownloadConfig().isOpenBreadCast();
isConvertSpeed = manager.getDownloadConfig().isConvertSpeed();
}
@Override public void supportBreakpoint(boolean support) {
if (!support) {
sendInState2Target(ISchedulers.SUPPORT_BREAK_POINT);
sendIntent(Aria.ACTION_SUPPORT_BREAK_POINT, -1);
}
}
@Override public void onPre() {
downloadEntity.setState(IEntity.STATE_PRE);
sendInState2Target(ISchedulers.PRE);
sendIntent(Aria.ACTION_PRE, -1);
saveData(IEntity.STATE_PRE, -1);
}
@Override public void onPostPre(long fileSize) {
downloadEntity.setFileSize(fileSize);
downloadEntity.setState(IEntity.STATE_POST_PRE);
entity.setFileSize(fileSize);
sendInState2Target(ISchedulers.POST_PRE);
sendIntent(Aria.ACTION_POST_PRE, -1);
saveData(IEntity.STATE_POST_PRE, -1);
}
@Override public void onResume(long resumeLocation) {
downloadEntity.setState(IEntity.STATE_RUNNING);
sendInState2Target(ISchedulers.RESUME);
sendIntent(Aria.ACTION_RESUME, resumeLocation);
saveData(IEntity.STATE_RUNNING, resumeLocation);
}
@Override public void onStart(long startLocation) {
downloadEntity.setState(IEntity.STATE_RUNNING);
sendInState2Target(ISchedulers.START);
sendIntent(Aria.ACTION_START, startLocation);
saveData(IEntity.STATE_RUNNING, startLocation);
}
@Override public void onProgress(long currentLocation) {
@ -270,53 +250,43 @@ public class DownloadTask extends AbsNormalTask<DownloadEntity> {
isFirst = false;
}
handleSpeed(speed);
downloadEntity.setCurrentProgress(currentLocation);
entity.setCurrentProgress(currentLocation);
lastLen = currentLocation;
sendInState2Target(ISchedulers.RUNNING);
if (!isOpenBroadCast) return;
sendIntent.putExtra(Aria.CURRENT_LOCATION, currentLocation);
sendIntent.putExtra(Aria.CURRENT_SPEED, speed);
context.sendBroadcast(sendIntent);
}
}
@Override public void onStop(long stopLocation) {
downloadEntity.setState(task.isWait ? IEntity.STATE_WAIT : IEntity.STATE_STOP);
handleSpeed(0);
sendInState2Target(ISchedulers.STOP);
sendIntent(Aria.ACTION_STOP, stopLocation);
saveData(task.isWait ? IEntity.STATE_WAIT : IEntity.STATE_STOP, stopLocation);
}
@Override public void onCancel() {
downloadEntity.setState(IEntity.STATE_CANCEL);
handleSpeed(0);
sendInState2Target(ISchedulers.CANCEL);
sendIntent(Aria.ACTION_CANCEL, -1);
downloadEntity.deleteData();
saveData(IEntity.STATE_CANCEL, -1);
entity.deleteData();
}
@Override public void onComplete() {
downloadEntity.setState(IEntity.STATE_COMPLETE);
downloadEntity.setDownloadComplete(true);
handleSpeed(0);
sendInState2Target(ISchedulers.COMPLETE);
sendIntent(Aria.ACTION_COMPLETE, downloadEntity.getFileSize());
saveData(IEntity.STATE_COMPLETE, entity.getFileSize());
}
@Override public void onFail() {
downloadEntity.setFailNum(downloadEntity.getFailNum() + 1);
downloadEntity.setState(IEntity.STATE_FAIL);
entity.setFailNum(entity.getFailNum() + 1);
handleSpeed(0);
sendInState2Target(ISchedulers.FAIL);
sendIntent(Aria.ACTION_FAIL, -1);
saveData(IEntity.STATE_FAIL, -1);
}
private void handleSpeed(long speed) {
if (isConvertSpeed) {
downloadEntity.setConvertSpeed(CommonUtil.formatFileSize(speed) + "/s");
entity.setConvertSpeed(CommonUtil.formatFileSize(speed) + "/s");
} else {
downloadEntity.setSpeed(speed);
entity.setSpeed(speed);
}
}
@ -331,17 +301,11 @@ public class DownloadTask extends AbsNormalTask<DownloadEntity> {
}
}
private void sendIntent(String action, long location) {
downloadEntity.setDownloadComplete(action.equals(Aria.ACTION_COMPLETE));
downloadEntity.setCurrentProgress(location);
downloadEntity.update();
if (!isOpenBroadCast) return;
Intent intent = CommonUtil.createIntent(context.getPackageName(), action);
intent.putExtra(Aria.DOWNLOAD_ENTITY, downloadEntity);
if (location != -1) {
intent.putExtra(Aria.CURRENT_LOCATION, location);
}
context.sendBroadcast(intent);
private void saveData(int state, long location) {
entity.setState(state);
entity.setDownloadComplete(state == IEntity.STATE_COMPLETE);
entity.setCurrentProgress(location);
entity.update();
}
}
}

@ -29,6 +29,19 @@ public abstract class AbsGroupEntity extends AbsEntity implements Parcelable {
*/
private String groupName = "";
/**
* 任务地址相加的urlmd5
*/
private String urlmd5 = "";
public String getUrlmd5() {
return urlmd5;
}
public void setUrlmd5(String urlmd5) {
this.urlmd5 = urlmd5;
}
public String getGroupName() {
return groupName;
}
@ -47,10 +60,12 @@ public abstract class AbsGroupEntity extends AbsEntity implements Parcelable {
@Override public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeString(this.groupName);
dest.writeString(this.urlmd5);
}
protected AbsGroupEntity(Parcel in) {
super(in);
this.groupName = in.readString();
this.urlmd5 = in.readString();
}
}

@ -15,65 +15,11 @@
*/
package com.arialyy.aria.core.inf;
import android.support.annotation.NonNull;
import com.arialyy.aria.core.RequestEnum;
import java.util.Map;
import java.util.Set;
/**
* Created by AriaL on 2017/6/29.
* 任务组超类
*/
public abstract class AbsGroupTarget<TARGET extends AbsGroupTarget, TASK_ENTITY extends AbsTaskEntity>
implements ITarget<TARGET> {
protected TASK_ENTITY mTaskEntity;
@Override public void resume() {
}
@Override public void start() {
}
@Override public void stop() {
}
@Override public void cancel() {
}
@Override public long getSize() {
return 0;
}
@Override public String getConvertSize() {
return null;
}
@Override public long getCurrentProgress() {
return 0;
}
@Override public TARGET addHeader(@NonNull String key, @NonNull String header) {
mTaskEntity.headers.put(key, header);
return (TARGET) this;
}
@Override public TARGET addHeaders(Map<String, String> headers) {
if (headers != null && headers.size() > 0) {
Set<String> keys = headers.keySet();
for (String key : keys) {
mTaskEntity.headers.put(key, headers.get(key));
}
}
return (TARGET) this;
}
public abstract class AbsGroupTarget<TARGET extends AbsTarget, ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity>
extends AbsTarget<TARGET, ENTITY, TASK_ENTITY> {
@Override public TARGET setRequestMode(RequestEnum requestEnum) {
mTaskEntity.requestEnum = requestEnum;
return (TARGET) this;
}
}

@ -15,53 +15,19 @@
*/
package com.arialyy.aria.core.inf;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.RequestEnum;
import com.arialyy.aria.core.command.normal.NormalCmdFactory;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.util.CommonUtil;
import java.util.Map;
import java.util.Set;
/**
* Created by lyy on 2017/2/28.
*/
public abstract class AbsNormalTarget<TARGET extends AbsNormalTarget, ENTITY extends AbsNormalEntity, TASK_ENTITY extends AbsTaskEntity>
implements ITarget<TARGET> {
public abstract class AbsNormalTarget<TARGET extends AbsTarget, ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity>
extends AbsTarget<TARGET, ENTITY, TASK_ENTITY> {
protected ENTITY mEntity;
protected TASK_ENTITY mTaskEntity;
protected String mTargetName;
/**
* 设置扩展字段用来保存你的其它数据如果你的数据比较多你可以把你的数据转换为JSON字符串然后再存到Aria中
*
* @param str 扩展数据
*/
public AbsNormalTarget setExtendField(String str) {
mEntity.setStr(str);
return this;
}
/**
* 获取存放的扩展字段
* 设置扩展字段{@link #setExtendField(String)}
*/
public String getExtendField() {
return mEntity.getStr();
}
/**
* 获取任务状态
*
* @return {@link IEntity}
*/
public int getTaskState() {
return mEntity.getState();
}
/**
* 将任务设置为最高优先级任务最高优先级任务有以下特点
@ -74,7 +40,8 @@ public abstract class AbsNormalTarget<TARGET extends AbsNormalTarget, ENTITY ext
*/
protected void setHighestPriority() {
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_HIGHEST_PRIORITY))
.setCmd(
CommonUtil.createCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_HIGHEST_PRIORITY))
.exe();
}
@ -89,22 +56,6 @@ public abstract class AbsNormalTarget<TARGET extends AbsNormalTarget, ENTITY ext
mTaskEntity.redirectUrlKey = redirectUrlKey;
}
/**
* 获取任务进度百分比
*
* @return 返回任务进度
*/
@Override public int getPercent() {
if (mEntity == null) {
Log.e("AbsNormalTarget", "下载管理器中没有该任务");
return 0;
}
if (mEntity.getFileSize() != 0) {
return (int) (mEntity.getCurrentProgress() * 100 / mEntity.getFileSize());
}
return 0;
}
/**
* 删除记录
*/
@ -121,27 +72,6 @@ public abstract class AbsNormalTarget<TARGET extends AbsNormalTarget, ENTITY ext
return getSize();
}
@Override public long getSize() {
if (mEntity instanceof DownloadEntity) {
DownloadEntity entity = (DownloadEntity) this.mEntity;
return entity.getFileSize();
} else if (mEntity instanceof UploadEntity) {
UploadEntity entity = (UploadEntity) this.mEntity;
return entity.getFileSize();
}
return 0;
}
@Override public String getConvertSize() {
if (mEntity instanceof DownloadEntity) {
DownloadEntity entity = (DownloadEntity) this.mEntity;
return CommonUtil.formatFileSize(entity.getFileSize());
} else if (mEntity instanceof UploadEntity) {
UploadEntity entity = (UploadEntity) this.mEntity;
return CommonUtil.formatFileSize(entity.getFileSize());
}
return "0b";
}
/**
* 获取单位转换后的文件大小
@ -159,112 +89,19 @@ public abstract class AbsNormalTarget<TARGET extends AbsNormalTarget, ENTITY ext
return false;
}
/**
* 获取任务进度如果任务存在则返回当前进度
*
* @return 该任务进度
*/
public long getCurrentProgress() {
if (mEntity instanceof DownloadEntity) {
DownloadEntity entity = (DownloadEntity) this.mEntity;
return entity.getCurrentProgress();
} else if (mEntity instanceof UploadEntity) {
UploadEntity entity = (UploadEntity) this.mEntity;
return entity.getCurrentProgress();
}
return -1;
}
/**
* 给url请求添加头部
*
* @param key 头部key
* @param header 头部value
*/
public TARGET addHeader(@NonNull String key, @NonNull String header) {
mTaskEntity.headers.put(key, header);
return (TARGET) this;
}
/**
* 给url请求添加头部
*/
public TARGET addHeaders(Map<String, String> headers) {
if (headers != null && headers.size() > 0) {
Set<String> keys = headers.keySet();
for (String key : keys) {
mTaskEntity.headers.put(key, headers.get(key));
}
}
return (TARGET) this;
}
/**
* 设置请求类型
*
* @param requestEnum {@link RequestEnum}
*/
public TARGET setRequestMode(RequestEnum requestEnum) {
mTaskEntity.requestEnum = requestEnum;
return (TARGET) this;
}
/**
* 添加任务
*/
public void add() {
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_CREATE))
.exe();
}
/**
* 开始下载
*/
@Override public void start() {
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_START))
.exe();
}
/**
* 停止下载
*
* @see #stop()
*/
@Deprecated public void pause() {
stop();
}
@Override public void stop() {
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_STOP))
.exe();
}
/**
* 恢复下载
*/
@Override public void resume() {
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_START))
.exe();
}
/**
* 取消下载
*/
@Override public void cancel() {
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_CANCEL))
.exe();
}
/**
* 重新下载
*/
void reStart() {
public void reStart() {
cancel();
start();
}

@ -0,0 +1,181 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.inf;
import android.support.annotation.NonNull;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.RequestEnum;
import com.arialyy.aria.core.command.normal.NormalCmdFactory;
import com.arialyy.aria.util.CommonUtil;
import java.util.Map;
import java.util.Set;
/**
* Created by AriaL on 2017/7/3.
*/
public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity>
implements ITarget<TARGET> {
protected ENTITY mEntity;
protected TASK_ENTITY mTaskEntity;
protected String mTargetName;
/**
* 获取任务进度如果任务存在则返回当前进度
*
* @return 该任务进度
*/
public long getCurrentProgress() {
return mEntity == null ? -1 : mEntity.getCurrentProgress();
}
/**
* 获取任务文件大小
*
* @return 文件大小
*/
@Override public long getSize() {
return mEntity == null ? 0 : mEntity.getFileSize();
}
/**
* 获取单位转换后的文件大小
*
* @return 文件大小{@code xxx mb}
*/
@Override public String getConvertSize() {
return mEntity == null ? "0b" : CommonUtil.formatFileSize(mEntity.getFileSize());
}
/**
* 设置扩展字段用来保存你的其它数据如果你的数据比较多你可以把你的数据转换为JSON字符串然后再存到Aria中
*
* @param str 扩展数据
*/
public TARGET setExtendField(String str) {
mEntity.setStr(str);
return (TARGET) this;
}
/**
* 获取存放的扩展字段
* 设置扩展字段{@link #setExtendField(String)}
*/
public String getExtendField() {
return mEntity.getStr();
}
/**
* 获取任务状态
*
* @return {@link IEntity}
*/
public int getTaskState() {
return mEntity.getState();
}
/**
* 获取任务进度百分比
*
* @return 返回任务进度
*/
@Override public int getPercent() {
if (mEntity == null) {
Log.e("AbsTarget", "下载管理器中没有该任务");
return 0;
}
if (mEntity.getFileSize() != 0) {
return (int) (mEntity.getCurrentProgress() * 100 / mEntity.getFileSize());
}
return 0;
}
/**
* 给url请求添加头部
*
* @param key 头部key
* @param header 头部value
*/
public TARGET addHeader(@NonNull String key, @NonNull String header) {
mTaskEntity.headers.put(key, header);
return (TARGET) this;
}
/**
* 给url请求添加头部
*/
public TARGET addHeaders(Map<String, String> headers) {
if (headers != null && headers.size() > 0) {
Set<String> keys = headers.keySet();
for (String key : keys) {
mTaskEntity.headers.put(key, headers.get(key));
}
}
return (TARGET) this;
}
/**
* 设置请求类型
*
* @param requestEnum {@link RequestEnum}
*/
public TARGET setRequestMode(RequestEnum requestEnum) {
mTaskEntity.requestEnum = requestEnum;
return (TARGET) this;
}
/**
* 开始下载
*/
@Override public void start() {
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_START))
.exe();
}
/**
* 停止下载
*
* @see #stop()
*/
@Deprecated public void pause() {
stop();
}
@Override public void stop() {
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_STOP))
.exe();
}
/**
* 恢复下载
*/
@Override public void resume() {
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_START))
.exe();
}
/**
* 取消下载
*/
@Override public void cancel() {
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_CANCEL))
.exe();
}
}

@ -24,6 +24,7 @@ import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.core.upload.UploadTaskEntity;
import com.arialyy.aria.exception.FileException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -76,6 +77,24 @@ public class CheckUtil {
if (TextUtils.isEmpty(downloadUrl)) throw new IllegalArgumentException("下载链接不能为null");
}
/**
* 检测下载链接组是否为null
*/
public static void checkDownloadUrls(List<String> urls) {
if (urls == null || urls.isEmpty()) {
throw new IllegalArgumentException("链接组不能为null");
}
}
/**
* 检查下载任务组保存路径
*/
public static void checkDownloadPaths(List<String> paths) {
if (paths == null || paths.isEmpty()) {
throw new IllegalArgumentException("链接保存路径不能为null");
}
}
/**
* 检测上传地址是否为null
*/

@ -52,6 +52,28 @@ import java.util.regex.Pattern;
public class CommonUtil {
private static final String TAG = "CommonUtil";
/**
* 根据下载任务组的url创建key
*
* @return urls null 或者 size为0返回""
*/
public static String getMd5Code(List<String> urls) {
if (urls == null || urls.size() < 1) return "";
String md5 = "";
StringBuilder sb = new StringBuilder();
for (String url : urls) {
sb.append(url);
}
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(sb.toString().getBytes());
md5 = new BigInteger(1, md.digest()).toString(16);
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, e.getMessage());
}
return md5;
}
/**
* 删除上传任务的配置包括
*

@ -7,9 +7,6 @@
<!--设置下载线程,线程下载数改变后,新的下载任务才会生效,如果任务大小小于1m,该设置也不会生效-->
<threadNum value="4"/>
<!--是否打开下载广播,默认为false,不建议使用广播,你可以使用Download注解来实现事件回调-->
<openBroadcast value="false"/>
<!--设置下载队列最大任务数, 默认为2-->
<maxTaskNum value="2"/>
@ -40,8 +37,6 @@
</download>
<upload>
<!--是否打开上传广播,默认为false,不建议使用广播,你可以使用Upload注解来实现事件回调-->
<openBroadcast value="false"/>
<!--设置上传队列最大任务数, 默认为2-->
<maxTaskNum value="2"/>

@ -42,10 +42,6 @@ class Configuration {
*/
public int oldMaxTaskNum = 2;
/**
* 是否发送任务广播true发送
*/
boolean isOpenBreadCast = false;
/**
* 任务队列最大任务数 默认为2
*/
@ -85,16 +81,6 @@ class Configuration {
return this;
}
public boolean isOpenBreadCast() {
return isOpenBreadCast;
}
public BaseConfig setOpenBreadCast(boolean openBreadCast) {
isOpenBreadCast = openBreadCast;
saveKey("isOpenBreadCast", openBreadCast + "");
return this;
}
public int getMaxTaskNum() {
return maxTaskNum;
}

@ -16,12 +16,9 @@
package com.arialyy.aria.core.upload;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.inf.AbsNormalTask;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
@ -41,7 +38,7 @@ public class UploadTask extends AbsNormalTask<UploadEntity> {
private UploadTask(UploadTaskEntity taskEntity, Handler outHandler) {
mOutHandler = outHandler;
mEntity = taskEntity.uploadEntity;
mEntity = taskEntity.getEntity();
mListener = new UListener(mOutHandler, this);
mUtil = new UploadUtil(taskEntity, mListener);
}
@ -78,118 +75,99 @@ public class UploadTask extends AbsNormalTask<UploadEntity> {
if (mOutHandler != null) {
mOutHandler.obtainMessage(DownloadSchedulers.CANCEL, this).sendToTarget();
}
//发送取消下载的广播
Intent intent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), Aria.ACTION_CANCEL);
intent.putExtra(Aria.UPLOAD_ENTITY, mEntity);
AriaManager.APP.sendBroadcast(intent);
}
}
private static class UListener extends UploadListener {
private static class
UListener extends UploadListener {
WeakReference<Handler> outHandler;
WeakReference<UploadTask> task;
long lastLen = 0; //上一次发送长度
long lastTime = 0;
long INTERVAL_TIME = 1000; //1m更新周期
boolean isFirst = true;
UploadEntity uploadEntity;
Intent sendIntent;
boolean isOpenBroadCast = false;
UploadEntity entity;
boolean isConvertSpeed = false;
Context context;
UListener(Handler outHandle, UploadTask task) {
this.outHandler = new WeakReference<>(outHandle);
this.task = new WeakReference<>(task);
uploadEntity = this.task.get().getEntity();
sendIntent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), Aria.ACTION_RUNNING);
sendIntent.putExtra(Aria.UPLOAD_ENTITY, uploadEntity);
entity = this.task.get().getEntity();
context = AriaManager.APP;
final AriaManager manager = AriaManager.getInstance(context);
isOpenBroadCast = manager.getUploadConfig().isOpenBreadCast();
isConvertSpeed = manager.getUploadConfig().isConvertSpeed();
}
@Override public void onPre() {
uploadEntity.setState(IEntity.STATE_PRE);
sendIntent(Aria.ACTION_PRE, -1);
sendInState2Target(ISchedulers.PRE);
saveData(IEntity.STATE_PRE, -1);
}
@Override public void onPostPre(long fileSize) {
super.onPostPre(fileSize);
uploadEntity.setFileSize(fileSize);
uploadEntity.setState(IEntity.STATE_POST_PRE);
sendIntent(Aria.ACTION_POST_PRE, 0);
entity.setFileSize(fileSize);
sendInState2Target(ISchedulers.POST_PRE);
saveData(IEntity.STATE_POST_PRE, 0);
}
@Override public void onStart() {
uploadEntity.setState(IEntity.STATE_RUNNING);
sendIntent(Aria.ACTION_START, 0);
sendInState2Target(ISchedulers.START);
saveData(IEntity.STATE_RUNNING, 0);
}
@Override public void onResume(long resumeLocation) {
uploadEntity.setState(DownloadEntity.STATE_RUNNING);
sendInState2Target(DownloadSchedulers.RESUME);
sendIntent(Aria.ACTION_RESUME, resumeLocation);
saveData(IEntity.STATE_RUNNING, resumeLocation);
}
@Override public void onStop(long stopLocation) {
uploadEntity.setState(DownloadEntity.STATE_STOP);
handleSpeed(0);
sendInState2Target(DownloadSchedulers.STOP);
sendIntent(Aria.ACTION_STOP, stopLocation);
saveData(IEntity.STATE_STOP, stopLocation);
}
@Override public void onProgress(long currentLocation) {
if (System.currentTimeMillis() - lastTime > INTERVAL_TIME) {
long speed = currentLocation - lastLen;
sendIntent.putExtra(Aria.CURRENT_LOCATION, currentLocation);
sendIntent.putExtra(Aria.CURRENT_SPEED, speed);
lastTime = System.currentTimeMillis();
if (isFirst) {
speed = 0;
isFirst = false;
}
handleSpeed(speed);
uploadEntity.setCurrentProgress(currentLocation);
entity.setCurrentProgress(currentLocation);
lastLen = currentLocation;
sendInState2Target(DownloadSchedulers.RUNNING);
AriaManager.APP.sendBroadcast(sendIntent);
}
}
@Override public void onCancel() {
uploadEntity.setState(DownloadEntity.STATE_CANCEL);
handleSpeed(0);
sendInState2Target(DownloadSchedulers.CANCEL);
sendIntent(Aria.ACTION_CANCEL, -1);
uploadEntity.deleteData();
saveData(IEntity.STATE_CANCEL, -1);
entity.deleteData();
}
@Override public void onComplete() {
uploadEntity.setState(DownloadEntity.STATE_COMPLETE);
uploadEntity.setComplete(true);
entity.setComplete(true);
handleSpeed(0);
sendInState2Target(DownloadSchedulers.COMPLETE);
sendIntent(Aria.ACTION_COMPLETE, uploadEntity.getFileSize());
saveData(IEntity.STATE_COMPLETE, entity.getFileSize());
}
@Override public void onFail() {
uploadEntity.setFailNum(uploadEntity.getFailNum() + 1);
uploadEntity.setState(DownloadEntity.STATE_FAIL);
entity.setFailNum(entity.getFailNum() + 1);
handleSpeed(0);
sendInState2Target(DownloadSchedulers.FAIL);
sendIntent(Aria.ACTION_FAIL, -1);
saveData(IEntity.STATE_FAIL, -1);
}
private void handleSpeed(long speed) {
if (isConvertSpeed) {
uploadEntity.setConvertSpeed(CommonUtil.formatFileSize(speed) + "/s");
entity.setConvertSpeed(CommonUtil.formatFileSize(speed) + "/s");
} else {
uploadEntity.setSpeed(speed);
entity.setSpeed(speed);
}
}
@ -204,17 +182,11 @@ public class UploadTask extends AbsNormalTask<UploadEntity> {
}
}
private void sendIntent(String action, long location) {
uploadEntity.setComplete(action.equals(Aria.ACTION_COMPLETE));
uploadEntity.setCurrentProgress(location);
uploadEntity.update();
if (!isOpenBroadCast) return;
Intent intent = CommonUtil.createIntent(context.getPackageName(), action);
intent.putExtra(Aria.UPLOAD_ENTITY, uploadEntity);
if (location != -1) {
intent.putExtra(Aria.CURRENT_LOCATION, location);
}
context.sendBroadcast(intent);
private void saveData(int state, long location) {
entity.setState(state);
entity.setComplete(state == IEntity.STATE_COMPLETE);
entity.setCurrentProgress(location);
entity.update();
}
}

Loading…
Cancel
Save