pull/4/head
AriaLyy 8 years ago
parent 15e4194f26
commit 765f42d73c
  1. 2
      .idea/misc.xml
  2. 11
      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. 12
      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. 20
      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. 49
      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="Remove" />
</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" />
</component>
<component name="ProjectType">

@ -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;
}
}

@ -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<IDownloadCmd> 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();
}

@ -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);
}
}

@ -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, "添加命令执行失败,【该任务已经存在】");
}

@ -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);
}

@ -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);
}

@ -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();

@ -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);
}
}

@ -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);
}
}

@ -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);
}

@ -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) {

@ -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);
/**
* 通过下载链接从缓存池或任务池搜索下载任务如果缓存池或任务池都没有任务则创建新任务

@ -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<String, OnSchedulerListener> 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<Map.Entry<String, OnSchedulerListener>> iter =
mSchedulerListeners.entrySet().iterator(); iter.hasNext(); ) {
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) {
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<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}
*/
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 {
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);
}

Loading…
Cancel
Save