From 765f42d73cf0e3260a7fb562bbac1b8466fe17a6 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Tue, 3 Jan 2017 18:17:15 +0800 Subject: [PATCH] bug fix --- .idea/misc.xml | 2 +- .../com/arialyy/aria/core/AMReceiver.java | 11 +++-- .../java/com/arialyy/aria/core/AMTarget.java | 14 +++--- .../com/arialyy/aria/core/AriaManager.java | 2 +- .../com/arialyy/aria/core/command/AddCmd.java | 4 +- .../arialyy/aria/core/command/CancelCmd.java | 8 +-- .../arialyy/aria/core/command/CmdFactory.java | 10 ++-- .../aria/core/command/IDownloadCmd.java | 12 ++--- .../arialyy/aria/core/command/SingleCmd.java | 6 +-- .../arialyy/aria/core/command/StartCmd.java | 6 +-- .../arialyy/aria/core/command/StopCmd.java | 9 ++-- .../aria/core/queue/DownloadTaskQueue.java | 20 ++++---- .../arialyy/aria/core/queue/ITaskQueue.java | 4 +- .../core/scheduler/DownloadSchedulers.java | 49 +++++++++---------- .../core/scheduler/IDownloadSchedulers.java | 8 +-- .../com/arialyy/aria/util/CommonUtil.java | 2 +- 16 files changed, 85 insertions(+), 82 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index fbb68289..5d199810 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -37,7 +37,7 @@ - + diff --git a/Aria/src/main/java/com/arialyy/aria/core/AMReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/AMReceiver.java index 23cccb3d..fc9be612 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AMReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AMReceiver.java @@ -27,9 +27,10 @@ import com.arialyy.aria.util.CheckUtil; * AM 接收器 */ public class AMReceiver { - Object obj; + String targetName; OnSchedulerListener listener; - DownloadEntity entity; + DownloadEntity entity; + Object obj; /** * {@link #load(String)},请使用该方法 @@ -59,7 +60,7 @@ public class AMReceiver { */ public AMReceiver addSchedulerListener(OnSchedulerListener listener) { this.listener = listener; - DownloadSchedulers.getInstance().addSchedulerListener(obj, listener); + DownloadSchedulers.getInstance().addSchedulerListener(targetName, listener); return this; } @@ -68,13 +69,13 @@ public class AMReceiver { */ public AMReceiver removeSchedulerListener() { if (listener != null) { - DownloadSchedulers.getInstance().removeSchedulerListener(obj, listener); + DownloadSchedulers.getInstance().removeSchedulerListener(targetName, listener); } return this; } void destroy() { - obj = null; + targetName = null; listener = null; } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/AMTarget.java b/Aria/src/main/java/com/arialyy/aria/core/AMTarget.java index ce6aecd9..022c2607 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AMTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AMTarget.java @@ -89,7 +89,8 @@ public class AMTarget { */ public void add() { DownloadManager.getInstance() - .setCmd(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_CREATE)) + .setCmd( + CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_CREATE)) .exe(); } @@ -98,8 +99,8 @@ public class AMTarget { */ public void start() { List cmds = new ArrayList<>(); - cmds.add(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_CREATE)); - cmds.add(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_START)); + cmds.add(CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_CREATE)); + cmds.add(CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_START)); DownloadManager.getInstance().setCmds(cmds).exe(); cmds.clear(); } @@ -109,7 +110,7 @@ public class AMTarget { */ public void stop() { DownloadManager.getInstance() - .setCmd(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_STOP)) + .setCmd(CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_STOP)) .exe(); } @@ -118,7 +119,7 @@ public class AMTarget { */ public void resume() { DownloadManager.getInstance() - .setCmd(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_START)) + .setCmd(CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_START)) .exe(); } @@ -127,7 +128,8 @@ public class AMTarget { */ public void cancel() { DownloadManager.getInstance() - .setCmd(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_CANCEL)) + .setCmd( + CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_CANCEL)) .exe(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java index 0377224e..77edd65f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java @@ -199,7 +199,7 @@ import java.util.Set; target = mTargets.get(key); if (target == null) { target = new AMReceiver(); - target.obj = obj; + target.targetName = obj.getClass().getName(); mTargets.put(key, target); } } 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 8781fe29..320d916f 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 @@ -30,14 +30,14 @@ class AddCmd extends IDownloadCmd { super(entity); } - AddCmd(Object target, DownloadEntity entity) { + AddCmd(String target, DownloadEntity entity) { super(target, entity); } @Override public void executeCmd() { Task task = mQueue.getTask(mEntity); if (task == null) { - mQueue.createTask(mTarget, mEntity); + mQueue.createTask(mTargetName, mEntity); } else { Log.w(TAG, "添加命令执行失败,【该任务已经存在】"); } 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 9e80372d..333e03fc 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 @@ -25,7 +25,7 @@ import com.arialyy.aria.core.task.Task; */ class CancelCmd extends IDownloadCmd { - CancelCmd(Object target, DownloadEntity entity) { + CancelCmd(String target, DownloadEntity entity) { super(target, entity); } @@ -36,11 +36,11 @@ class CancelCmd extends IDownloadCmd { @Override public void executeCmd() { Task task = mQueue.getTask(mEntity); if (task == null) { - task = mQueue.createTask(mTarget, mEntity); + task = mQueue.createTask(mTargetName, mEntity); } if (task != null) { - if (mTarget != null) { - task.setTargetName(mTarget.getClass().getName()); + if (mTargetName != null) { + task.setTargetName(mTargetName); } mQueue.cancelTask(task); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/CmdFactory.java b/Aria/src/main/java/com/arialyy/aria/core/command/CmdFactory.java index 8b676ee2..786fb9dc 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/CmdFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/CmdFactory.java @@ -90,7 +90,7 @@ public class CmdFactory { * @param type 命令类型{@link #TASK_CREATE}、{@link #TASK_START}、{@link #TASK_CANCEL}、{@link * #TASK_STOP} */ - public IDownloadCmd createCmd(Object target, DownloadEntity entity, int type) { + public IDownloadCmd createCmd(String target, DownloadEntity entity, int type) { switch (type) { case TASK_CREATE: return createAddCmd(target, entity); @@ -113,7 +113,7 @@ public class CmdFactory { * * @return {@link StopCmd} */ - private StopCmd createStopCmd(Object target, DownloadEntity entity) { + private StopCmd createStopCmd(String target, DownloadEntity entity) { return new StopCmd(target, entity); } @@ -131,7 +131,7 @@ public class CmdFactory { * * @return {@link AddCmd} */ - private AddCmd createAddCmd(Object target, DownloadEntity entity) { + private AddCmd createAddCmd(String target, DownloadEntity entity) { return new AddCmd(target, entity); } @@ -149,7 +149,7 @@ public class CmdFactory { * * @return {@link StartCmd} */ - private StartCmd createStartCmd(Object target, DownloadEntity entity) { + private StartCmd createStartCmd(String target, DownloadEntity entity) { return new StartCmd(target, entity); } @@ -167,7 +167,7 @@ public class CmdFactory { * * @return {@link CancelCmd} */ - private CancelCmd createCancelCmd(Object target, DownloadEntity entity) { + private CancelCmd createCancelCmd(String target, DownloadEntity entity) { return new CancelCmd(target, entity); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/IDownloadCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/IDownloadCmd.java index 80d85237..0bd6a313 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/IDownloadCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/IDownloadCmd.java @@ -27,10 +27,10 @@ import com.arialyy.aria.core.DownloadEntity; * 下载命令 */ public abstract class IDownloadCmd { - ITaskQueue mQueue; + ITaskQueue mQueue; DownloadEntity mEntity; - String TAG; - Object mTarget; + String TAG; + String mTargetName; /** * @param entity 下载实体 @@ -40,13 +40,13 @@ public abstract class IDownloadCmd { } /** - * @param target 产生任务的对象 + * @param targetName 产生任务的对象名 */ - IDownloadCmd(Object target, DownloadEntity entity) { + IDownloadCmd(String targetName, DownloadEntity entity) { if (!CheckUtil.checkDownloadEntity(entity)) { return; } - mTarget = target; + mTargetName = targetName; mEntity = entity; TAG = CommonUtil.getClassName(this); mQueue = DownloadManager.getInstance().getTaskQueue(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/SingleCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/SingleCmd.java index a45f0b2f..f7bbe04e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/SingleCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/SingleCmd.java @@ -27,7 +27,7 @@ class SingleCmd extends IDownloadCmd { /** * @param entity 下载实体 */ - SingleCmd(Object target, DownloadEntity entity) { + SingleCmd(String target, DownloadEntity entity) { super(target, entity); } @@ -38,11 +38,11 @@ class SingleCmd extends IDownloadCmd { @Override public void executeCmd() { Task task = mQueue.getTask(mEntity); if (task == null) { - task = mQueue.createTask(mTarget, mEntity); + task = mQueue.createTask(mTargetName, mEntity); } else { Log.w(TAG, "添加命令执行失败,【该任务已经存在】"); } - task.setTargetName(mTarget.getClass().getName()); + task.setTargetName(mTargetName); mQueue.startTask(task); } } 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 d30c0b77..11c2dfc2 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 @@ -25,7 +25,7 @@ import com.arialyy.aria.core.task.Task; */ class StartCmd extends IDownloadCmd { - StartCmd(Object target, DownloadEntity entity) { + StartCmd(String target, DownloadEntity entity) { super(target, entity); } @@ -36,10 +36,10 @@ class StartCmd extends IDownloadCmd { @Override public void executeCmd() { Task task = mQueue.getTask(mEntity); if (task == null) { - task = mQueue.createTask(mTarget, mEntity); + task = mQueue.createTask(mTargetName, mEntity); } if (task != null) { - task.setTargetName(mTarget.getClass().getName()); + task.setTargetName(mTargetName); mQueue.startTask(task); } } 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 611f6e12..676e8da7 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 @@ -16,6 +16,7 @@ package com.arialyy.aria.core.command; +import android.text.TextUtils; import android.util.Log; import com.arialyy.aria.core.DownloadEntity; import com.arialyy.aria.core.task.Task; @@ -29,7 +30,7 @@ class StopCmd extends IDownloadCmd { /** * @param entity 下载实体 */ - StopCmd(Object target, DownloadEntity entity) { + StopCmd(String target, DownloadEntity entity) { super(target, entity); } @@ -41,14 +42,14 @@ class StopCmd extends IDownloadCmd { Task task = mQueue.getTask(mEntity); if (task == null) { if (mEntity.getState() == DownloadEntity.STATE_DOWNLOAD_ING) { - task = mQueue.createTask(mTarget, mEntity); + task = mQueue.createTask(mTargetName, mEntity); mQueue.stopTask(task); } else { Log.w(TAG, "停止命令执行失败,【调度器中没有该任务】"); } } else { - if (mTarget != null) { - task.setTargetName(mTarget.getClass().getName()); + if (!TextUtils.isEmpty(mTargetName)) { + task.setTargetName(mTargetName); } mQueue.stopTask(task); } 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 e479b9cc..06228179 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 @@ -17,6 +17,7 @@ package com.arialyy.aria.core.queue; import android.content.Context; +import android.text.TextUtils; import android.util.Log; import com.arialyy.aria.core.DownloadEntity; import com.arialyy.aria.core.queue.pool.CachePool; @@ -32,9 +33,9 @@ import com.arialyy.aria.util.Configuration; * 下载任务队列 */ public class DownloadTaskQueue implements ITaskQueue { - private static final String TAG = "DownloadTaskQueue"; - private CachePool mCachePool = CachePool.getInstance(); - private ExecutePool mExecutePool = ExecutePool.getInstance(); + private static final String TAG = "DownloadTaskQueue"; + private CachePool mCachePool = CachePool.getInstance(); + private ExecutePool mExecutePool = ExecutePool.getInstance(); private Context mContext; //private IDownloadSchedulers mSchedulers; @@ -155,17 +156,16 @@ public class DownloadTaskQueue implements ITaskQueue { } } - @Override public Task createTask(Object target, DownloadEntity entity) { + @Override public Task createTask(String target, DownloadEntity entity) { Task task; - if (target == null) { + if (TextUtils.isEmpty(target)) { //task = TaskFactory.getInstance().createTask(mContext, entity, mSchedulers); - task = TaskFactory.getInstance() - .createTask(mContext, entity, DownloadSchedulers.getInstance()); + task = + TaskFactory.getInstance().createTask(mContext, entity, DownloadSchedulers.getInstance()); } else { task = TaskFactory.getInstance() //.createTask(target.getClass().getName(), mContext, entity, mSchedulers); - .createTask(target.getClass().getName(), mContext, entity, - DownloadSchedulers.getInstance()); + .createTask(target, mContext, entity, DownloadSchedulers.getInstance()); } mCachePool.putTask(task); return task; @@ -202,7 +202,7 @@ public class DownloadTaskQueue implements ITaskQueue { //} public static class Builder { - Context context; + Context context; IDownloadSchedulers schedulers; public Builder(Context context) { 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 485eba63..73163766 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 @@ -47,10 +47,10 @@ public interface ITaskQueue extends IDownloader { * 创建一个新的下载任务,创建时只是将新任务存储到缓存池 * * @param entity 下载实体{@link DownloadEntity} - * @param target 生成该任务的对象 + * @param targetName 生成该任务的对象 * @return {@link Task} */ - public Task createTask(Object target, DownloadEntity entity); + public Task createTask(String targetName, DownloadEntity entity); /** * 通过下载链接从缓存池或任务池搜索下载任务,如果缓存池或任务池都没有任务,则创建新任务 diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java index e6d70851..afaed7a7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java @@ -26,6 +26,7 @@ import com.arialyy.aria.core.task.Task; import com.arialyy.aria.util.Configuration; import java.util.Iterator; import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; /** @@ -36,44 +37,44 @@ public class DownloadSchedulers implements IDownloadSchedulers { /** * 任务预加载 */ - public static final int PRE = 0; + public static final int PRE = 0; /** * 任务开始 */ - public static final int START = 1; + public static final int START = 1; /** * 任务停止 */ - public static final int STOP = 2; + public static final int STOP = 2; /** * 任务失败 */ - public static final int FAIL = 3; + public static final int FAIL = 3; /** * 任务取消 */ - public static final int CANCEL = 4; + public static final int CANCEL = 4; /** * 任务完成 */ - public static final int COMPLETE = 5; + public static final int COMPLETE = 5; /** * 下载中 */ - public static final int RUNNING = 6; + public static final int RUNNING = 6; /** * 恢复下载 */ - public static final int RESUME = 7; - private static final String TAG = "DownloadSchedulers"; - private static final Object LOCK = new Object(); + public static final int RESUME = 7; + private static final String TAG = "DownloadSchedulers"; + private static final Object LOCK = new Object(); private static volatile DownloadSchedulers INSTANCE = null; /** * 下载器任务监听 */ Map mSchedulerListeners = new ConcurrentHashMap<>(); - DownloadManager mManager = DownloadManager.getInstance(); + DownloadManager mManager = DownloadManager.getInstance(); ITaskQueue mQueue; private DownloadSchedulers() { @@ -90,30 +91,24 @@ public class DownloadSchedulers implements IDownloadSchedulers { return INSTANCE; } - @Override public void addSchedulerListener(Object target, OnSchedulerListener schedulerListener) { - if (target == null) { - throw new IllegalArgumentException("target 不能为null"); - } - String name = target.getClass().getName(); - if (mSchedulerListeners.get(name) != null) { + @Override + public void addSchedulerListener(String targetName, OnSchedulerListener schedulerListener) { + if (mSchedulerListeners.get(targetName) != null) { Log.w(TAG, "监听器已存在"); return; } - mSchedulerListeners.put(name, schedulerListener); + mSchedulerListeners.put(targetName, schedulerListener); } @Override - public void removeSchedulerListener(Object target, OnSchedulerListener schedulerListener) { - if (target == null) { - throw new IllegalArgumentException("target 不能为null"); - } + public void removeSchedulerListener(String targetName, OnSchedulerListener schedulerListener) { //OnSchedulerListener listener = mSchedulerListeners.get(target.getClass().getName()); //mSchedulerListeners.remove(listener); //该内存溢出解决方案:http://stackoverflow.com/questions/14585829/how-safe-is-to-delete-already-removed-concurrenthashmap-element for (Iterator> iter = mSchedulerListeners.entrySet().iterator(); iter.hasNext(); ) { Map.Entry entry = iter.next(); - if (entry.getKey().equals(target.getClass().getName())) iter.remove(); + if (entry.getKey().equals(targetName)) iter.remove(); } } @@ -151,8 +146,12 @@ public class DownloadSchedulers implements IDownloadSchedulers { */ private void callback(int state, Task task) { if (mSchedulerListeners.size() > 0) { - if (!TextUtils.isEmpty(task.getTargetName())) { - callback(state, task, mSchedulerListeners.get(task.getTargetName())); + //if (!TextUtils.isEmpty(task.getTargetName())) { + // callback(state, task, mSchedulerListeners.get(task.getTargetName())); + //} + Set keys = mSchedulerListeners.keySet(); + for (String key : keys) { + callback(state, task, mSchedulerListeners.get(key)); } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/IDownloadSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/IDownloadSchedulers.java index d8b2fda9..5b38ceb5 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/IDownloadSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/IDownloadSchedulers.java @@ -28,16 +28,16 @@ public interface IDownloadSchedulers extends Handler.Callback { /** * 注册下载器监听,一个观察者只能注册一次监听 * - * @param target 观察者,创建该监听器的对象 + * @param targetName 观察者,创建该监听器的对象类名 * @param schedulerListener {@link OnSchedulerListener} */ - public void addSchedulerListener(Object target, OnSchedulerListener schedulerListener); + public void addSchedulerListener(String targetName, OnSchedulerListener schedulerListener); /** - * @param target 观察者,创建该监听器的对象 + * @param targetName 观察者,创建该监听器的对象类名 * 取消注册监听器 */ - public void removeSchedulerListener(Object target, OnSchedulerListener schedulerListener); + public void removeSchedulerListener(String targetName, OnSchedulerListener schedulerListener); /** * 处理下载任务下载失败的情形 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 612078b3..3031b232 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -40,7 +40,7 @@ import java.util.Properties; public class CommonUtil { private static final String TAG = "util"; - public static IDownloadCmd createCmd(Object target, DownloadEntity entity, int cmd) { + public static IDownloadCmd createCmd(String target, DownloadEntity entity, int cmd) { return CmdFactory.getInstance().createCmd(target, entity, cmd); }