diff --git a/Aria/src/main/java/com/arialyy/aria/core/Aria.java b/Aria/src/main/java/com/arialyy/aria/core/Aria.java index 27e19d2e..5dc70cfe 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/Aria.java +++ b/Aria/src/main/java/com/arialyy/aria/core/Aria.java @@ -59,62 +59,6 @@ import com.arialyy.aria.core.upload.UploadTask; * */ @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() { } diff --git a/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java b/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java index 260d1d29..d0df8509 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java @@ -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)) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupStartCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupStartCmd.java index 7fdb8465..3bd7bea8 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupStartCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupStartCmd.java @@ -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 extends AbsGroupCmd { } @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); + } + } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/AddCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/AddCmd.java index 6f7f3e9f..8ed5de0d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/AddCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/AddCmd.java @@ -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 extends AbsNormalCmd { @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); diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelCmd.java index 4e13b329..ff859066 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelCmd.java @@ -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 extends AbsNormalCmd { @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)) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java index cd3336e7..58b26bdc 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java @@ -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 extends AbsNormalCmd { 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 extends AbsNormalCmd { } } - 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, "添加命令执行失败,【该任务已经存在】"); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java index 11c713af..2b042a27 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java @@ -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 extends AbsNormalCmd { 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); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopCmd.java index cd926b7c..9d0b0811 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopCmd.java @@ -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 extends AbsNormalCmd { @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, "停止命令执行失败,【调度器中没有该任务】"); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java index e556c424..f76ee2f2 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java @@ -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 { +public class DownloadGroupTarget + extends AbsGroupTarget { + private List mUrls; + + DownloadGroupTarget(DownloadGroupEntity entity, String targetName, List urls) { + this.mEntity = entity; + this.mTargetName = targetName; + this.mUrls = urls; + mTaskEntity = new DownloadGroupTaskEntity(entity); + } /** * 设置保存路径组 */ public DownloadGroupTarget setDownloadPaths(List 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; } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java index 045902dd..5b6589c6 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java @@ -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 { - private static final String TAG = "DownloadReceiver"; + private final String TAG = "DownloadReceiver"; public ISchedulerListener listener; /** @@ -73,15 +74,17 @@ public class DownloadReceiver extends AbsReceiver { } /** - * 添加调度器回调 - * - * @see #register() + * 加载下载地址,如果任务组的中的下载地址改变了,则任务从新的一个任务组 */ - @Deprecated public DownloadReceiver addSchedulerListener( - ISchedulerListener listener) { - this.listener = listener; - DownloadSchedulers.getInstance().addSchedulerListener(targetName, listener); - return this; + public DownloadGroupTarget load(List 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 { DownloadSchedulers.getInstance().unRegister(obj); } + /** + * 添加调度器回调 + * + * @see #register() + */ + @Deprecated public DownloadReceiver addSchedulerListener( + ISchedulerListener listener) { + this.listener = listener; + DownloadSchedulers.getInstance().addSchedulerListener(targetName, listener); + return this; + } + /** * 移除回调 * @@ -165,8 +180,8 @@ public class DownloadReceiver extends AbsReceiver { @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 keys = ariaManager.getReceiver().keySet(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java index e70f6eb4..2d51273c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java @@ -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 { 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 { 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 { WeakReference outHandler; WeakReference 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 { 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 { 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 { } } - 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(); } } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupEntity.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupEntity.java index 9432a0de..b324fd57 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupEntity.java @@ -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(); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTarget.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTarget.java index 34834da5..c514577b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTarget.java @@ -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 - implements ITarget { - - 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 headers) { - if (headers != null && headers.size() > 0) { - Set keys = headers.keySet(); - for (String key : keys) { - mTaskEntity.headers.put(key, headers.get(key)); - } - } - return (TARGET) this; - } +public abstract class AbsGroupTarget + extends AbsTarget { - @Override public TARGET setRequestMode(RequestEnum requestEnum) { - mTaskEntity.requestEnum = requestEnum; - return (TARGET) this; - } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalTarget.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalTarget.java index c8fc1cc0..b8351713 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalTarget.java @@ -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 - implements ITarget { +public abstract class AbsNormalTarget + extends AbsTarget { 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 headers) { - if (headers != null && headers.size() > 0) { - Set 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(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java new file mode 100644 index 00000000..1d96daba --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java @@ -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 + implements ITarget { + 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 headers) { + if (headers != null && headers.size() > 0) { + Set 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(); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java b/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java index 41841d5b..34c54227 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java @@ -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 urls) { + if (urls == null || urls.isEmpty()) { + throw new IllegalArgumentException("链接组不能为null"); + } + } + + /** + * 检查下载任务组保存路径 + */ + public static void checkDownloadPaths(List paths) { + if (paths == null || paths.isEmpty()) { + throw new IllegalArgumentException("链接保存路径不能为null"); + } + } + /** * 检测上传地址是否为null */ diff --git a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java index d9407c60..443daf8d 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -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 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; + } + /** * 删除上传任务的配置,包括 * diff --git a/app/src/main/assets/aria_config.xml b/app/src/main/assets/aria_config.xml index 9972caa4..474752d0 100644 --- a/app/src/main/assets/aria_config.xml +++ b/app/src/main/assets/aria_config.xml @@ -7,9 +7,6 @@ - - - @@ -40,8 +37,6 @@ - - diff --git a/aria/src/main/java/com/arialyy/aria/core/Configuration.java b/aria/src/main/java/com/arialyy/aria/core/Configuration.java index 8adcb24a..ea4c9d27 100644 --- a/aria/src/main/java/com/arialyy/aria/core/Configuration.java +++ b/aria/src/main/java/com/arialyy/aria/core/Configuration.java @@ -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; } diff --git a/aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java b/aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java index 88c30140..f48ab56b 100644 --- a/aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java +++ b/aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java @@ -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 { 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 { 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 outHandler; WeakReference 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 { } } - 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(); } }