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