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