diff --git a/Aria/src/main/java/com/arialyy/aria/core/QueueMod.java b/Aria/src/main/java/com/arialyy/aria/core/QueueMod.java index 70d02300..db9e7b6c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/QueueMod.java +++ b/Aria/src/main/java/com/arialyy/aria/core/QueueMod.java @@ -6,8 +6,10 @@ package com.arialyy.aria.core; */ public enum QueueMod { /** - * 等待模式,如果执行队列已经满了,再次使用start命令执行任务时,该任务会被添加到缓存队列中 - * 当执行队列的任务完成时,将自动执行缓存队列中的任务 + * 等待模式, + * 如果执行队列已经满了,再对其它任务(TASK_A)使用start命令执行任务时 + * 1、TASK_A添加到缓存队列中,当执行队列中的任务完成时,系统会将自动执行缓存队列中的TASK_A + * 2、如果再次对TASK_A使用start命令,TASK_A将会立刻执行 */ WAIT("wait"), diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java index 4177dc1d..44cf797f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java @@ -17,6 +17,7 @@ package com.arialyy.aria.core.command; import android.util.Log; +import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.inf.AbsTaskEntity; @@ -33,7 +34,7 @@ class AddCmd extends AbsCmd { @Override public void executeCmd() { if (!canExeCmd) return; - ITask task = 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/CancelCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java index fd51ad14..74079a93 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java @@ -17,6 +17,7 @@ package com.arialyy.aria.core.command; import android.text.TextUtils; +import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.inf.AbsTaskEntity; @@ -31,7 +32,7 @@ class CancelCmd extends AbsCmd { @Override public void executeCmd() { if (!canExeCmd) return; - ITask task = mQueue.getTask(mTaskEntity.getEntity()); + AbsTask task = mQueue.getTask(mTaskEntity.getEntity()); if (task == null) { task = mQueue.createTask(mTargetName, mTaskEntity); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/HighestPriorityCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/HighestPriorityCmd.java index 57dc35bb..a846a41e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/HighestPriorityCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/HighestPriorityCmd.java @@ -16,6 +16,7 @@ package com.arialyy.aria.core.command; import android.text.TextUtils; +import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.inf.AbsTaskEntity; @@ -39,7 +40,7 @@ final class HighestPriorityCmd extends AbsCmd { @Override public void executeCmd() { if (!canExeCmd) return; - ITask task = mQueue.getTask(mTaskEntity.getEntity()); + AbsTask task = mQueue.getTask(mTaskEntity.getEntity()); if (task == null) { task = mQueue.createTask(mTargetName, mTaskEntity); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/ResumeAllCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/ResumeAllCmd.java index cb87dee7..693b9c80 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/ResumeAllCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/ResumeAllCmd.java @@ -3,6 +3,7 @@ package com.arialyy.aria.core.command; import android.util.Log; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; +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.core.inf.ITask; @@ -29,7 +30,7 @@ final class ResumeAllCmd extends AbsCmd { for (DownloadEntity entity : allEntity) { int exeNum = mQueue.getExePoolSize(); if (exeNum == 0 || exeNum < mQueue.getMaxTaskNum()) { - ITask task = createTask(entity); + AbsTask task = createTask(entity); mQueue.startTask(task); } else { entity.setState(IEntity.STATE_WAIT); @@ -38,8 +39,8 @@ final class ResumeAllCmd extends AbsCmd { } } - private ITask createTask(DownloadEntity entity) { - ITask task = mQueue.getTask(entity); + private AbsTask createTask(DownloadEntity entity) { + AbsTask task = mQueue.getTask(entity); if (task == null) { DownloadTaskEntity taskEntity = new DownloadTaskEntity(entity); task = mQueue.createTask(mTargetName, taskEntity); diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java index a9a3d180..30906615 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java @@ -19,12 +19,15 @@ package com.arialyy.aria.core.command; import android.text.TextUtils; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.QueueMod; +import com.arialyy.aria.core.inf.AbsTask; +import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.inf.AbsTaskEntity; /** * Created by lyy on 2016/8/22. * 开始命令 + * 队列模型{@link QueueMod#NOW}、{@link QueueMod#WAIT} */ class StartCmd extends AbsCmd { @@ -34,31 +37,38 @@ class StartCmd extends AbsCmd { @Override public void executeCmd() { if (!canExeCmd) return; - ITask task = mQueue.getTask(mTaskEntity.getEntity()); + 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(); + } + + AbsTask task = mQueue.getTask(mTaskEntity.getEntity()); if (task == null) { task = mQueue.createTask(mTargetName, mTaskEntity); - } - if (task != null) { if (!TextUtils.isEmpty(mTargetName)) { task.setTargetName(mTargetName); } - 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(); - } + // 任务不存在时,根据配置不同,对任务执行操作 if (mod.equals(QueueMod.NOW.getTag())) { mQueue.startTask(task); - }else if (mod.equals(QueueMod.WAIT.getTag())){ - if (mQueue.getExePoolSize() < maxTaskNum){ + } 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); + } } } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java index 8712d6cc..dc04609b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java @@ -18,6 +18,7 @@ package com.arialyy.aria.core.command; import android.text.TextUtils; import android.util.Log; +import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.inf.AbsTaskEntity; @@ -34,7 +35,7 @@ class StopCmd extends AbsCmd { @Override public void executeCmd() { if (!canExeCmd) return; - ITask task = mQueue.getTask(mTaskEntity.getEntity()); + AbsTask task = mQueue.getTask(mTaskEntity.getEntity()); if (task == null) { if (mTaskEntity.getEntity().getState() == IEntity.STATE_RUNNING) { task = mQueue.createTask(mTargetName, mTaskEntity); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java index d91271e4..3dbf6aca 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java @@ -20,6 +20,8 @@ import android.content.Context; import android.util.Log; import android.util.SparseArray; import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.orm.DbEntity; +import com.arialyy.aria.orm.DbUtil; import com.arialyy.aria.util.BufferedRandomAccessFile; import com.arialyy.aria.util.CommonUtil; import java.io.File; @@ -78,12 +80,28 @@ class DownloadUtil implements IDownloadUtil, Runnable { + AriaManager.DOWNLOAD_TEMP_DIR + mDownloadFile.getName() + ".properties"); + checkTask(); + } + + /** + * 检查任务是否是新任务,新任务条件: + * 1、文件不存在 + * 2、下载记录文件不存在 + * 3、下载记录文件缺失或不匹配 + * 4、数据库记录不存在 + */ + private void checkTask() { try { if (!mConfigFile.exists()) { //记录文件被删除,则重新下载 isNewTask = true; CommonUtil.createFile(mConfigFile.getPath()); + } else if (!mDownloadFile.exists()) { + isNewTask = true; + } else if (DbEntity.findData(DownloadEntity.class, "downloadUrl=?", + mDownloadEntity.getDownloadUrl()) == null) { + isNewTask = true; } else { - isNewTask = !mDownloadFile.exists(); + isNewTask = false; } } catch (Exception e) { e.printStackTrace(); @@ -195,9 +213,10 @@ class DownloadUtil implements IDownloadUtil, Runnable { } @Override public void run() { + HttpURLConnection conn = null; try { URL url = new URL(mDownloadEntity.getDownloadUrl()); - HttpURLConnection conn = ConnectionHelp.handleConnection(url); + conn = ConnectionHelp.handleConnection(url); conn = ConnectionHelp.setConnectParam(mDownloadTaskEntity, conn); conn.setRequestProperty("Range", "bytes=" + 0 + "-"); conn.setConnectTimeout(mConnectTimeOut); @@ -210,6 +229,10 @@ class DownloadUtil implements IDownloadUtil, Runnable { + mDownloadFile.getPath() + "】\n" + CommonUtil.getPrintException(e)); + } finally { + if (conn != null) { + conn.disconnect(); + } } } 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 index 4a286ba5..c3dcbb48 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java @@ -206,11 +206,13 @@ public abstract class AbsTarget cmds = new ArrayList<>(); - cmds.add(CommonUtil.createCmd(targetName, taskEntity, CmdFactory.TASK_CREATE)); - cmds.add(CommonUtil.createCmd(targetName, taskEntity, CmdFactory.TASK_START)); - AriaManager.getInstance(AriaManager.APP).setCmds(cmds).exe(); - cmds.clear(); + //List cmds = new ArrayList<>(); + //cmds.add(CommonUtil.createCmd(targetName, taskEntity, CmdFactory.TASK_CREATE)); + //cmds.add(CommonUtil.createCmd(targetName, taskEntity, CmdFactory.TASK_START)); + //cmds.clear(); + AriaManager.getInstance(AriaManager.APP) + .setCmd(CommonUtil.createCmd(targetName, taskEntity, CmdFactory.TASK_START)) + .exe(); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java index 64e6f543..5521a882 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java @@ -37,6 +37,15 @@ public abstract class AbsTask diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java index 67f55038..64a8ca14 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java @@ -20,6 +20,7 @@ import android.util.Log; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.inf.AbsEntity; +import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.queue.pool.CachePool; @@ -30,7 +31,7 @@ import java.util.Set; * Created by lyy on 2017/2/23. * 任务队列 */ -abstract class AbsTaskQueue +abstract class AbsTaskQueue implements ITaskQueue { private final String TAG = "AbsTaskQueue"; CachePool mCachePool = new CachePool<>(); @@ -118,7 +119,7 @@ abstract class AbsTaskQueue { +public interface ITaskQueue { /** * 通过key判断任务是否正在执行 diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java index 4aa14f66..1bafa3e7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java @@ -21,6 +21,7 @@ import android.util.Log; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.inf.AbsEntity; +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.core.inf.ITask; @@ -34,7 +35,7 @@ import java.util.concurrent.ConcurrentHashMap; /** * Created by lyy on 2017/6/4. */ -public abstract class AbsSchedulers, QUEUE extends ITaskQueue> +public abstract class AbsSchedulers, QUEUE extends ITaskQueue> implements ISchedulers { private static final String TAG = "AbsSchedulers"; @@ -131,7 +132,7 @@ public abstract class AbsSchedulers { //"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk"; //"http://down2.xiaoshuofuwuqi.com/d/file/filetxt/20170608/14/%BA%DA%CE%D7%CA%A6%E1%C8%C6%F0.txt"; //"http://tinghuaapp.oss-cn-shanghai.aliyuncs.com/20170612201739607815"; - "http://static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk"; + "http://static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk"; //"http://oqcpqqvuf.bkt.clouddn.com/ceshi.txt"; //"http://down8.androidgame-store.com/201706122321/97967927DD4E53D9905ECAA7874C8128/new/game1/19/45319/com.neuralprisma-2.5.2.174-2000174_1494784835.apk?f=web_1"; //不支持断点的链接 @@ -209,6 +209,10 @@ public class SingleTaskActivity extends BaseActivity { @Download.onTaskCancel(DOWNLOAD_URL) void taskCancel(DownloadTask task) { mUpdateHandler.sendEmptyMessage(DOWNLOAD_CANCEL); L.d(TAG, "task__cancel"); + //Aria.download(this) + // .load(DOWNLOAD_URL) + // .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk") + // .add(); } @Download.onTaskFail(DOWNLOAD_URL) void taskFail(DownloadTask task) { @@ -257,6 +261,11 @@ public class SingleTaskActivity extends BaseActivity { } else if (text.equals("恢复")) { Aria.download(this).load(DOWNLOAD_URL).resume(); } + //DownloadTarget target = Aria.download(this) + // .load(DOWNLOAD_URL) + // .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk"); + //target.add(); + //target.cancel(); break; case R.id.stop: Aria.download(this).load(DOWNLOAD_URL).pause(); diff --git a/app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java b/app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java index 502a5ce3..915472fa 100644 --- a/app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java @@ -106,10 +106,7 @@ public class MultiDownloadActivity extends BaseActivity