pull/4/head
AriaLyy 8 years ago
parent 15e4194f26
commit 765f42d73c
  1. 2
      .idea/misc.xml
  2. 9
      Aria/src/main/java/com/arialyy/aria/core/AMReceiver.java
  3. 14
      Aria/src/main/java/com/arialyy/aria/core/AMTarget.java
  4. 2
      Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
  5. 4
      Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java
  6. 8
      Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java
  7. 10
      Aria/src/main/java/com/arialyy/aria/core/command/CmdFactory.java
  8. 8
      Aria/src/main/java/com/arialyy/aria/core/command/IDownloadCmd.java
  9. 6
      Aria/src/main/java/com/arialyy/aria/core/command/SingleCmd.java
  10. 6
      Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java
  11. 9
      Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java
  12. 12
      Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java
  13. 4
      Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java
  14. 27
      Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java
  15. 8
      Aria/src/main/java/com/arialyy/aria/core/scheduler/IDownloadSchedulers.java
  16. 2
      Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java

@ -37,7 +37,7 @@
<ConfirmationsSetting value="0" id="Add" /> <ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" /> <ConfirmationsSetting value="0" id="Remove" />
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">

@ -27,9 +27,10 @@ import com.arialyy.aria.util.CheckUtil;
* AM 接收器 * AM 接收器
*/ */
public class AMReceiver { public class AMReceiver {
Object obj; String targetName;
OnSchedulerListener listener; OnSchedulerListener listener;
DownloadEntity entity; DownloadEntity entity;
Object obj;
/** /**
* {@link #load(String)}请使用该方法 * {@link #load(String)}请使用该方法
@ -59,7 +60,7 @@ public class AMReceiver {
*/ */
public AMReceiver addSchedulerListener(OnSchedulerListener listener) { public AMReceiver addSchedulerListener(OnSchedulerListener listener) {
this.listener = listener; this.listener = listener;
DownloadSchedulers.getInstance().addSchedulerListener(obj, listener); DownloadSchedulers.getInstance().addSchedulerListener(targetName, listener);
return this; return this;
} }
@ -68,13 +69,13 @@ public class AMReceiver {
*/ */
public AMReceiver removeSchedulerListener() { public AMReceiver removeSchedulerListener() {
if (listener != null) { if (listener != null) {
DownloadSchedulers.getInstance().removeSchedulerListener(obj, listener); DownloadSchedulers.getInstance().removeSchedulerListener(targetName, listener);
} }
return this; return this;
} }
void destroy() { void destroy() {
obj = null; targetName = null;
listener = null; listener = null;
} }
} }

@ -89,7 +89,8 @@ public class AMTarget {
*/ */
public void add() { public void add() {
DownloadManager.getInstance() DownloadManager.getInstance()
.setCmd(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_CREATE)) .setCmd(
CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_CREATE))
.exe(); .exe();
} }
@ -98,8 +99,8 @@ public class AMTarget {
*/ */
public void start() { public void start() {
List<IDownloadCmd> cmds = new ArrayList<>(); List<IDownloadCmd> cmds = new ArrayList<>();
cmds.add(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_CREATE)); cmds.add(CommonUtil.createCmd(mReceiver.targetName, 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_START));
DownloadManager.getInstance().setCmds(cmds).exe(); DownloadManager.getInstance().setCmds(cmds).exe();
cmds.clear(); cmds.clear();
} }
@ -109,7 +110,7 @@ public class AMTarget {
*/ */
public void stop() { public void stop() {
DownloadManager.getInstance() DownloadManager.getInstance()
.setCmd(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_STOP)) .setCmd(CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_STOP))
.exe(); .exe();
} }
@ -118,7 +119,7 @@ public class AMTarget {
*/ */
public void resume() { public void resume() {
DownloadManager.getInstance() DownloadManager.getInstance()
.setCmd(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_START)) .setCmd(CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_START))
.exe(); .exe();
} }
@ -127,7 +128,8 @@ public class AMTarget {
*/ */
public void cancel() { public void cancel() {
DownloadManager.getInstance() DownloadManager.getInstance()
.setCmd(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_CANCEL)) .setCmd(
CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_CANCEL))
.exe(); .exe();
} }

@ -199,7 +199,7 @@ import java.util.Set;
target = mTargets.get(key); target = mTargets.get(key);
if (target == null) { if (target == null) {
target = new AMReceiver(); target = new AMReceiver();
target.obj = obj; target.targetName = obj.getClass().getName();
mTargets.put(key, target); mTargets.put(key, target);
} }
} }

@ -30,14 +30,14 @@ class AddCmd extends IDownloadCmd {
super(entity); super(entity);
} }
AddCmd(Object target, DownloadEntity entity) { AddCmd(String target, DownloadEntity entity) {
super(target, entity); super(target, entity);
} }
@Override public void executeCmd() { @Override public void executeCmd() {
Task task = mQueue.getTask(mEntity); Task task = mQueue.getTask(mEntity);
if (task == null) { if (task == null) {
mQueue.createTask(mTarget, mEntity); mQueue.createTask(mTargetName, mEntity);
} else { } else {
Log.w(TAG, "添加命令执行失败,【该任务已经存在】"); Log.w(TAG, "添加命令执行失败,【该任务已经存在】");
} }

@ -25,7 +25,7 @@ import com.arialyy.aria.core.task.Task;
*/ */
class CancelCmd extends IDownloadCmd { class CancelCmd extends IDownloadCmd {
CancelCmd(Object target, DownloadEntity entity) { CancelCmd(String target, DownloadEntity entity) {
super(target, entity); super(target, entity);
} }
@ -36,11 +36,11 @@ class CancelCmd extends IDownloadCmd {
@Override public void executeCmd() { @Override public void executeCmd() {
Task task = mQueue.getTask(mEntity); Task task = mQueue.getTask(mEntity);
if (task == null) { if (task == null) {
task = mQueue.createTask(mTarget, mEntity); task = mQueue.createTask(mTargetName, mEntity);
} }
if (task != null) { if (task != null) {
if (mTarget != null) { if (mTargetName != null) {
task.setTargetName(mTarget.getClass().getName()); task.setTargetName(mTargetName);
} }
mQueue.cancelTask(task); mQueue.cancelTask(task);
} }

@ -90,7 +90,7 @@ public class CmdFactory {
* @param type 命令类型{@link #TASK_CREATE}{@link #TASK_START}{@link #TASK_CANCEL}{@link * @param type 命令类型{@link #TASK_CREATE}{@link #TASK_START}{@link #TASK_CANCEL}{@link
* #TASK_STOP} * #TASK_STOP}
*/ */
public IDownloadCmd createCmd(Object target, DownloadEntity entity, int type) { public IDownloadCmd createCmd(String target, DownloadEntity entity, int type) {
switch (type) { switch (type) {
case TASK_CREATE: case TASK_CREATE:
return createAddCmd(target, entity); return createAddCmd(target, entity);
@ -113,7 +113,7 @@ public class CmdFactory {
* *
* @return {@link StopCmd} * @return {@link StopCmd}
*/ */
private StopCmd createStopCmd(Object target, DownloadEntity entity) { private StopCmd createStopCmd(String target, DownloadEntity entity) {
return new StopCmd(target, entity); return new StopCmd(target, entity);
} }
@ -131,7 +131,7 @@ public class CmdFactory {
* *
* @return {@link AddCmd} * @return {@link AddCmd}
*/ */
private AddCmd createAddCmd(Object target, DownloadEntity entity) { private AddCmd createAddCmd(String target, DownloadEntity entity) {
return new AddCmd(target, entity); return new AddCmd(target, entity);
} }
@ -149,7 +149,7 @@ public class CmdFactory {
* *
* @return {@link StartCmd} * @return {@link StartCmd}
*/ */
private StartCmd createStartCmd(Object target, DownloadEntity entity) { private StartCmd createStartCmd(String target, DownloadEntity entity) {
return new StartCmd(target, entity); return new StartCmd(target, entity);
} }
@ -167,7 +167,7 @@ public class CmdFactory {
* *
* @return {@link CancelCmd} * @return {@link CancelCmd}
*/ */
private CancelCmd createCancelCmd(Object target, DownloadEntity entity) { private CancelCmd createCancelCmd(String target, DownloadEntity entity) {
return new CancelCmd(target, entity); return new CancelCmd(target, entity);
} }

@ -30,7 +30,7 @@ public abstract class IDownloadCmd {
ITaskQueue mQueue; ITaskQueue mQueue;
DownloadEntity mEntity; DownloadEntity mEntity;
String TAG; String TAG;
Object mTarget; String mTargetName;
/** /**
* @param entity 下载实体 * @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)) { if (!CheckUtil.checkDownloadEntity(entity)) {
return; return;
} }
mTarget = target; mTargetName = targetName;
mEntity = entity; mEntity = entity;
TAG = CommonUtil.getClassName(this); TAG = CommonUtil.getClassName(this);
mQueue = DownloadManager.getInstance().getTaskQueue(); mQueue = DownloadManager.getInstance().getTaskQueue();

@ -27,7 +27,7 @@ class SingleCmd extends IDownloadCmd {
/** /**
* @param entity 下载实体 * @param entity 下载实体
*/ */
SingleCmd(Object target, DownloadEntity entity) { SingleCmd(String target, DownloadEntity entity) {
super(target, entity); super(target, entity);
} }
@ -38,11 +38,11 @@ class SingleCmd extends IDownloadCmd {
@Override public void executeCmd() { @Override public void executeCmd() {
Task task = mQueue.getTask(mEntity); Task task = mQueue.getTask(mEntity);
if (task == null) { if (task == null) {
task = mQueue.createTask(mTarget, mEntity); task = mQueue.createTask(mTargetName, mEntity);
} else { } else {
Log.w(TAG, "添加命令执行失败,【该任务已经存在】"); Log.w(TAG, "添加命令执行失败,【该任务已经存在】");
} }
task.setTargetName(mTarget.getClass().getName()); task.setTargetName(mTargetName);
mQueue.startTask(task); mQueue.startTask(task);
} }
} }

@ -25,7 +25,7 @@ import com.arialyy.aria.core.task.Task;
*/ */
class StartCmd extends IDownloadCmd { class StartCmd extends IDownloadCmd {
StartCmd(Object target, DownloadEntity entity) { StartCmd(String target, DownloadEntity entity) {
super(target, entity); super(target, entity);
} }
@ -36,10 +36,10 @@ class StartCmd extends IDownloadCmd {
@Override public void executeCmd() { @Override public void executeCmd() {
Task task = mQueue.getTask(mEntity); Task task = mQueue.getTask(mEntity);
if (task == null) { if (task == null) {
task = mQueue.createTask(mTarget, mEntity); task = mQueue.createTask(mTargetName, mEntity);
} }
if (task != null) { if (task != null) {
task.setTargetName(mTarget.getClass().getName()); task.setTargetName(mTargetName);
mQueue.startTask(task); mQueue.startTask(task);
} }
} }

@ -16,6 +16,7 @@
package com.arialyy.aria.core.command; package com.arialyy.aria.core.command;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.arialyy.aria.core.DownloadEntity; import com.arialyy.aria.core.DownloadEntity;
import com.arialyy.aria.core.task.Task; import com.arialyy.aria.core.task.Task;
@ -29,7 +30,7 @@ class StopCmd extends IDownloadCmd {
/** /**
* @param entity 下载实体 * @param entity 下载实体
*/ */
StopCmd(Object target, DownloadEntity entity) { StopCmd(String target, DownloadEntity entity) {
super(target, entity); super(target, entity);
} }
@ -41,14 +42,14 @@ class StopCmd extends IDownloadCmd {
Task task = mQueue.getTask(mEntity); Task task = mQueue.getTask(mEntity);
if (task == null) { if (task == null) {
if (mEntity.getState() == DownloadEntity.STATE_DOWNLOAD_ING) { if (mEntity.getState() == DownloadEntity.STATE_DOWNLOAD_ING) {
task = mQueue.createTask(mTarget, mEntity); task = mQueue.createTask(mTargetName, mEntity);
mQueue.stopTask(task); mQueue.stopTask(task);
} else { } else {
Log.w(TAG, "停止命令执行失败,【调度器中没有该任务】"); Log.w(TAG, "停止命令执行失败,【调度器中没有该任务】");
} }
} else { } else {
if (mTarget != null) { if (!TextUtils.isEmpty(mTargetName)) {
task.setTargetName(mTarget.getClass().getName()); task.setTargetName(mTargetName);
} }
mQueue.stopTask(task); mQueue.stopTask(task);
} }

@ -17,6 +17,7 @@
package com.arialyy.aria.core.queue; package com.arialyy.aria.core.queue;
import android.content.Context; import android.content.Context;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.arialyy.aria.core.DownloadEntity; import com.arialyy.aria.core.DownloadEntity;
import com.arialyy.aria.core.queue.pool.CachePool; import com.arialyy.aria.core.queue.pool.CachePool;
@ -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; Task task;
if (target == null) { if (TextUtils.isEmpty(target)) {
//task = TaskFactory.getInstance().createTask(mContext, entity, mSchedulers); //task = TaskFactory.getInstance().createTask(mContext, entity, mSchedulers);
task = TaskFactory.getInstance() task =
.createTask(mContext, entity, DownloadSchedulers.getInstance()); TaskFactory.getInstance().createTask(mContext, entity, DownloadSchedulers.getInstance());
} else { } else {
task = TaskFactory.getInstance() task = TaskFactory.getInstance()
//.createTask(target.getClass().getName(), mContext, entity, mSchedulers); //.createTask(target.getClass().getName(), mContext, entity, mSchedulers);
.createTask(target.getClass().getName(), mContext, entity, .createTask(target, mContext, entity, DownloadSchedulers.getInstance());
DownloadSchedulers.getInstance());
} }
mCachePool.putTask(task); mCachePool.putTask(task);
return task; return task;

@ -47,10 +47,10 @@ public interface ITaskQueue extends IDownloader {
* 创建一个新的下载任务创建时只是将新任务存储到缓存池 * 创建一个新的下载任务创建时只是将新任务存储到缓存池
* *
* @param entity 下载实体{@link DownloadEntity} * @param entity 下载实体{@link DownloadEntity}
* @param target 生成该任务的对象 * @param targetName 生成该任务的对象
* @return {@link Task} * @return {@link Task}
*/ */
public Task createTask(Object target, DownloadEntity entity); public Task createTask(String targetName, DownloadEntity entity);
/** /**
* 通过下载链接从缓存池或任务池搜索下载任务如果缓存池或任务池都没有任务则创建新任务 * 通过下载链接从缓存池或任务池搜索下载任务如果缓存池或任务池都没有任务则创建新任务

@ -26,6 +26,7 @@ import com.arialyy.aria.core.task.Task;
import com.arialyy.aria.util.Configuration; import com.arialyy.aria.util.Configuration;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
/** /**
@ -90,30 +91,24 @@ public class DownloadSchedulers implements IDownloadSchedulers {
return INSTANCE; return INSTANCE;
} }
@Override public void addSchedulerListener(Object target, OnSchedulerListener schedulerListener) { @Override
if (target == null) { public void addSchedulerListener(String targetName, OnSchedulerListener schedulerListener) {
throw new IllegalArgumentException("target 不能为null"); if (mSchedulerListeners.get(targetName) != null) {
}
String name = target.getClass().getName();
if (mSchedulerListeners.get(name) != null) {
Log.w(TAG, "监听器已存在"); Log.w(TAG, "监听器已存在");
return; return;
} }
mSchedulerListeners.put(name, schedulerListener); mSchedulerListeners.put(targetName, schedulerListener);
} }
@Override @Override
public void removeSchedulerListener(Object target, OnSchedulerListener schedulerListener) { public void removeSchedulerListener(String targetName, OnSchedulerListener schedulerListener) {
if (target == null) {
throw new IllegalArgumentException("target 不能为null");
}
//OnSchedulerListener listener = mSchedulerListeners.get(target.getClass().getName()); //OnSchedulerListener listener = mSchedulerListeners.get(target.getClass().getName());
//mSchedulerListeners.remove(listener); //mSchedulerListeners.remove(listener);
//该内存溢出解决方案:http://stackoverflow.com/questions/14585829/how-safe-is-to-delete-already-removed-concurrenthashmap-element //该内存溢出解决方案:http://stackoverflow.com/questions/14585829/how-safe-is-to-delete-already-removed-concurrenthashmap-element
for (Iterator<Map.Entry<String, OnSchedulerListener>> iter = for (Iterator<Map.Entry<String, OnSchedulerListener>> iter =
mSchedulerListeners.entrySet().iterator(); iter.hasNext(); ) { mSchedulerListeners.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry<String, OnSchedulerListener> entry = iter.next(); Map.Entry<String, OnSchedulerListener> 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) { private void callback(int state, Task task) {
if (mSchedulerListeners.size() > 0) { if (mSchedulerListeners.size() > 0) {
if (!TextUtils.isEmpty(task.getTargetName())) { //if (!TextUtils.isEmpty(task.getTargetName())) {
callback(state, task, mSchedulerListeners.get(task.getTargetName())); // callback(state, task, mSchedulerListeners.get(task.getTargetName()));
//}
Set<String> keys = mSchedulerListeners.keySet();
for (String key : keys) {
callback(state, task, mSchedulerListeners.get(key));
} }
} }
} }

@ -28,16 +28,16 @@ public interface IDownloadSchedulers extends Handler.Callback {
/** /**
* 注册下载器监听一个观察者只能注册一次监听 * 注册下载器监听一个观察者只能注册一次监听
* *
* @param target 观察者创建该监听器的对象 * @param targetName 观察者创建该监听器的对象类名
* @param schedulerListener {@link OnSchedulerListener} * @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);
/** /**
* 处理下载任务下载失败的情形 * 处理下载任务下载失败的情形

@ -40,7 +40,7 @@ import java.util.Properties;
public class CommonUtil { public class CommonUtil {
private static final String TAG = "util"; 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); return CmdFactory.getInstance().createCmd(target, entity, cmd);
} }

Loading…
Cancel
Save