diff --git a/.idea/misc.xml b/.idea/misc.xml index d2e67251..cbd46bd1 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -27,7 +27,7 @@ - + diff --git a/.idea/modules.xml b/.idea/modules.xml index 9f4bd159..5f6de159 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,8 +2,8 @@ - + 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 c1b20207..4b3fb688 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/Aria.java +++ b/Aria/src/main/java/com/arialyy/aria/core/Aria.java @@ -100,7 +100,11 @@ import com.arialyy.aria.core.upload.UploadTask; /** * 下载实体 */ - public static final String ENTITY = "DOWNLOAD_ENTITY"; + public static final String DOWNLOAD_ENTITY = "DOWNLOAD_ENTITY"; + /** + * 上传实体 + */ + public static final String UPLOAD_ENTITY = "UPLOAD_ENTITY"; /** * 位置 */ 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 ccc040c0..8bdc1c30 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/Configuration.java +++ b/Aria/src/main/java/com/arialyy/aria/core/Configuration.java @@ -86,7 +86,7 @@ class Configuration { oldMaxTaskNum = this.maxTaskNum; this.maxTaskNum = maxTaskNum; saveKey("maxTaskNum", maxTaskNum + ""); - DownloadTaskQueue.getInstance().setDownloadNum(maxTaskNum); + DownloadTaskQueue.getInstance().setMaxTaskNum(maxTaskNum); return this; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java index 65dcfc28..f937d321 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java @@ -69,6 +69,7 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity { return failNum; } + @Override public void setFailNum(int failNum) { this.failNum = failNum; } 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 8611e1fe..385220f5 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 @@ -208,7 +208,7 @@ public class DownloadTask implements ITask { // 发送停止下载的广播 Intent intent = CommonUtil.createIntent(mContext.getPackageName(), Aria.ACTION_STOP); intent.putExtra(Aria.CURRENT_LOCATION, mEntity.getCurrentProgress()); - intent.putExtra(Aria.ENTITY, mEntity); + intent.putExtra(Aria.DOWNLOAD_ENTITY, mEntity); mContext.sendBroadcast(intent); } } @@ -227,7 +227,7 @@ public class DownloadTask implements ITask { } //发送取消下载的广播 Intent intent = CommonUtil.createIntent(mContext.getPackageName(), Aria.ACTION_CANCEL); - intent.putExtra(Aria.ENTITY, mEntity); + intent.putExtra(Aria.DOWNLOAD_ENTITY, mEntity); mContext.sendBroadcast(intent); } } @@ -285,7 +285,7 @@ public class DownloadTask implements ITask { this.task = wTask.get(); this.downloadEntity = this.task.getDownloadEntity(); sendIntent = CommonUtil.createIntent(context.getPackageName(), Aria.ACTION_RUNNING); - sendIntent.putExtra(Aria.ENTITY, downloadEntity); + sendIntent.putExtra(Aria.DOWNLOAD_ENTITY, downloadEntity); final AriaManager manager = AriaManager.getInstance(context); isOpenBroadCast = manager.getDownloadConfig().isOpenBreadCast(); isConvertSpeed = manager.getDownloadConfig().isConvertSpeed(); @@ -406,7 +406,7 @@ public class DownloadTask implements ITask { downloadEntity.update(); if (!isOpenBroadCast) return; Intent intent = CommonUtil.createIntent(context.getPackageName(), action); - intent.putExtra(Aria.ENTITY, downloadEntity); + intent.putExtra(Aria.DOWNLOAD_ENTITY, downloadEntity); if (location != -1) { intent.putExtra(Aria.CURRENT_LOCATION, location); } 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 02feaab0..0941d294 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 @@ -40,6 +40,13 @@ public class AbsTarget protected TASK_ENTITY taskEntity; protected String targetName; + /** + * 将该任务优先级提到最高 + */ + protected void setToFirst(){ + + } + /** * 重定向后,新url的key,默认为location */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/IEntity.java b/Aria/src/main/java/com/arialyy/aria/core/inf/IEntity.java index 992d21a5..fcca2e5e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/IEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/IEntity.java @@ -60,4 +60,6 @@ public interface IEntity { @Ignore public static final int STATE_CANCEL = 7; public int getState(); + + public void setFailNum(int failNum); } 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 7fc1d722..bdccfe9c 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 @@ -16,6 +16,7 @@ package com.arialyy.aria.core.queue; +import android.util.Log; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.inf.ITaskEntity; @@ -24,8 +25,89 @@ import com.arialyy.aria.core.queue.pool.ExecutePool; /** * Created by Aria.Lao on 2017/2/23. + * 任务队列 */ abstract class AbsTaskQueue implements ITaskQueue { + private final String TAG = "AbsTaskQueue"; CachePool mCachePool = new CachePool<>(); + ExecutePool mExecutePool = null; + + /** + * 获取任务执行池 + */ + public ExecutePool getExecutePool() { + return mExecutePool; + } + + /** + * 获取缓存池 + */ + public CachePool getCachePool() { + return mCachePool; + } + + /** + * 获取缓存任务数 + * + * @return 获取缓存的任务数 + */ + @Override public int cachePoolSize() { + return mCachePool.size(); + } + + /** + * 获取当前运行的任务数 + * + * @return 当前正在执行的任务数 + */ + @Override public int executePoolSize() { + return mExecutePool.size(); + } + + @Override public TASK getTask(String url) { + TASK task = mExecutePool.getTask(url); + if (task == null) { + task = mCachePool.getTask(url); + } + return task; + } + + @Override public void startTask(TASK task) { + if (mExecutePool.putTask(task)) { + mCachePool.removeTask(task); + task.getEntity().setFailNum(0); + task.start(); + } + } + + @Override public void stopTask(TASK task) { + if (!task.isRunning()) Log.w(TAG, "停止任务失败,【任务已经停止】"); + if (mExecutePool.removeTask(task)) { + task.stop(); + } else { + task.stop(); + Log.w(TAG, "停止任务失败,【任务已经停止】"); + } + } + + @Override public void reTryStart(TASK task) { + if (task == null) { + Log.w(TAG, "重试下载失败,task 为null"); + return; + } + if (!task.isRunning()) { + task.start(); + } else { + Log.w(TAG, "任务没有完全停止,重试下载失败"); + } + } + + @Override public void cancelTask(TASK task) { + task.cancel(); + } + + @Override public TASK getNextTask() { + return mCachePool.pollTask(); + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java index 210b835e..0986e74a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java @@ -23,7 +23,6 @@ import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.inf.IEntity; -import com.arialyy.aria.core.queue.pool.CachePool; import com.arialyy.aria.core.queue.pool.ExecutePool; import com.arialyy.aria.core.scheduler.DownloadSchedulers; @@ -36,7 +35,6 @@ public class DownloadTaskQueue private static final String TAG = "DownloadTaskQueue"; private static volatile DownloadTaskQueue INSTANCE = null; private static final Object LOCK = new Object(); - private ExecutePool mExecutePool = new ExecutePool<>(true); public static DownloadTaskQueue getInstance() { if (INSTANCE == null) { @@ -48,79 +46,10 @@ public class DownloadTaskQueue } private DownloadTaskQueue() { + mExecutePool = new ExecutePool<>(true); } - /** - * 获取任务执行池 - */ - public ExecutePool getExecutePool() { - return mExecutePool; - } - - /** - * 获取缓存池 - */ - public CachePool getCachePool() { - return mCachePool; - } - - /** - * 获取当前运行的任务数 - * - * @return 当前正在执行的任务数 - */ - public int getCurrentTaskNum() { - return mExecutePool.size(); - } - - /** - * 获取缓存任务数 - * - * @return 获取缓存的任务数 - */ - public int getCacheTaskNum() { - return mCachePool.size(); - } - - @Override public void startTask(DownloadTask task) { - if (mExecutePool.putTask(task)) { - mCachePool.removeTask(task); - task.getDownloadEntity().setFailNum(0); - task.start(); - } - } - - @Override public void stopTask(DownloadTask task) { - if (!task.isRunning()) Log.w(TAG, "停止任务失败,【任务已经停止】"); - if (mExecutePool.removeTask(task)) { - task.stop(); - } else { - task.stop(); - Log.w(TAG, "停止任务失败,【任务已经停止】"); - } - } - - @Override public void cancelTask(DownloadTask task) { - task.cancel(); - } - - @Override public void reTryStart(DownloadTask task) { - if (task == null) { - Log.w(TAG, "重试下载失败,task 为null"); - return; - } - if (!task.isRunning()) { - task.start(); - } else { - Log.w(TAG, "任务没有完全停止,重试下载失败"); - } - } - - @Override public int size() { - return mExecutePool.size(); - } - - @Override public void setDownloadNum(int downloadNum) { + @Override public void setMaxTaskNum(int downloadNum) { //原始长度 int size = AriaManager.getInstance(AriaManager.APP).getDownloadConfig().oldMaxTaskNum; int diff = downloadNum - size; @@ -164,14 +93,6 @@ public class DownloadTaskQueue return getTask(entity.getDownloadUrl()); } - @Override public DownloadTask getTask(String url) { - DownloadTask task = mExecutePool.getTask(url); - if (task == null) { - task = mCachePool.getTask(url); - } - return task; - } - @Override public void removeTask(DownloadEntity entity) { DownloadTask task = mExecutePool.getTask(entity.getDownloadUrl()); if (task != null) { @@ -183,7 +104,4 @@ public class DownloadTaskQueue } } - @Override public DownloadTask getNextTask() { - return mCachePool.pollTask(); - } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java index 8469cb0f..95da95f3 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java @@ -37,40 +37,45 @@ public interface ITaskQueue { case STOP: case CANCEL: mQueue.removeTask(entity); - if (mQueue.size() < AriaManager.getInstance(AriaManager.APP) + if (mQueue.executePoolSize() < AriaManager.getInstance(AriaManager.APP) .getUploadConfig() .getMaxTaskNum()) { startNextTask(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java index 0b2dd657..ef6b0b72 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java @@ -171,7 +171,7 @@ public class UploadSchedulers implements ISchedulers { case STOP: case CANCEL: mQueue.removeTask(entity); - if (mQueue.size() < AriaManager.getInstance(AriaManager.APP) + if (mQueue.executePoolSize() < AriaManager.getInstance(AriaManager.APP) .getUploadConfig() .getMaxTaskNum()) { startNextTask(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java index 816cb24c..f3d86e57 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java @@ -81,7 +81,7 @@ public class UploadEntity extends DbEntity implements IEntity, Parcelable { return failNum; } - public void setFailNum(int failNum) { + @Override public void setFailNum(int failNum) { this.failNum = failNum; } 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 8a23431c..f8146a41 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 @@ -99,7 +99,7 @@ public class UploadTask implements ITask { } //发送取消下载的广播 Intent intent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), Aria.ACTION_CANCEL); - intent.putExtra(Aria.ENTITY, mUploadEntity); + intent.putExtra(Aria.UPLOAD_ENTITY, mUploadEntity); AriaManager.APP.sendBroadcast(intent); } } @@ -201,7 +201,7 @@ public class UploadTask implements ITask { this.task = new WeakReference<>(task); uploadEntity = this.task.get().getUploadEntity(); sendIntent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), Aria.ACTION_RUNNING); - sendIntent.putExtra(Aria.ENTITY, uploadEntity); + sendIntent.putExtra(Aria.UPLOAD_ENTITY, uploadEntity); context = AriaManager.APP; final AriaManager manager = AriaManager.getInstance(context); isOpenBroadCast = manager.getUploadConfig().isOpenBreadCast(); @@ -301,7 +301,7 @@ public class UploadTask implements ITask { uploadEntity.update(); if (!isOpenBroadCast) return; Intent intent = CommonUtil.createIntent(context.getPackageName(), action); - intent.putExtra(Aria.ENTITY, uploadEntity); + intent.putExtra(Aria.UPLOAD_ENTITY, uploadEntity); if (location != -1) { intent.putExtra(Aria.CURRENT_LOCATION, location); } diff --git a/app/src/main/java/com/arialyy/simple/download/DownloadModule.java b/app/src/main/java/com/arialyy/simple/download/DownloadModule.java index aa9dab49..8269134b 100644 --- a/app/src/main/java/com/arialyy/simple/download/DownloadModule.java +++ b/app/src/main/java/com/arialyy/simple/download/DownloadModule.java @@ -176,7 +176,7 @@ public class DownloadModule extends BaseModule { String action = intent.getAction(); switch (action) { case Aria.ACTION_POST_PRE: - DownloadEntity entity = intent.getParcelableExtra(Aria.ENTITY); + DownloadEntity entity = intent.getParcelableExtra(Aria.DOWNLOAD_ENTITY); len = entity.getFileSize(); L.d(TAG, "download pre"); handler.obtainMessage(SingleTaskActivity.DOWNLOAD_PRE, len).sendToTarget();