|
|
@ -19,7 +19,6 @@ import android.annotation.TargetApi; |
|
|
|
import android.app.Activity; |
|
|
|
import android.app.Activity; |
|
|
|
import android.app.Application; |
|
|
|
import android.app.Application; |
|
|
|
import android.app.Dialog; |
|
|
|
import android.app.Dialog; |
|
|
|
import android.app.Service; |
|
|
|
|
|
|
|
import android.content.Context; |
|
|
|
import android.content.Context; |
|
|
|
import android.content.DialogInterface; |
|
|
|
import android.content.DialogInterface; |
|
|
|
import android.os.Build; |
|
|
|
import android.os.Build; |
|
|
@ -29,11 +28,12 @@ import android.support.v4.app.Fragment; |
|
|
|
import android.text.TextUtils; |
|
|
|
import android.text.TextUtils; |
|
|
|
import android.util.Log; |
|
|
|
import android.util.Log; |
|
|
|
import android.widget.PopupWindow; |
|
|
|
import android.widget.PopupWindow; |
|
|
|
import com.arialyy.aria.core.command.CmdFactory; |
|
|
|
import com.arialyy.aria.core.command.download.CmdFactory; |
|
|
|
|
|
|
|
import com.arialyy.aria.core.receiver.DownloadReceiver; |
|
|
|
import com.arialyy.aria.util.CAConfiguration; |
|
|
|
import com.arialyy.aria.util.CAConfiguration; |
|
|
|
import com.arialyy.aria.util.CheckUtil; |
|
|
|
import com.arialyy.aria.util.CheckUtil; |
|
|
|
import com.arialyy.aria.util.CommonUtil; |
|
|
|
import com.arialyy.aria.util.CommonUtil; |
|
|
|
import com.arialyy.aria.core.command.IDownloadCmd; |
|
|
|
import com.arialyy.aria.core.command.download.IDownloadCmd; |
|
|
|
import com.arialyy.aria.util.Configuration; |
|
|
|
import com.arialyy.aria.util.Configuration; |
|
|
|
import java.lang.reflect.Field; |
|
|
|
import java.lang.reflect.Field; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.ArrayList; |
|
|
@ -52,7 +52,7 @@ import java.util.Set; |
|
|
|
private static final String TAG = "AriaManager"; |
|
|
|
private static final String TAG = "AriaManager"; |
|
|
|
private static final Object LOCK = new Object(); |
|
|
|
private static final Object LOCK = new Object(); |
|
|
|
private static volatile AriaManager INSTANCE = null; |
|
|
|
private static volatile AriaManager INSTANCE = null; |
|
|
|
private Map<String, AMReceiver> mTargets = new HashMap<>(); |
|
|
|
private Map<String, DownloadReceiver> mDownloadTargets = new HashMap<>(); |
|
|
|
private DownloadManager mManager; |
|
|
|
private DownloadManager mManager; |
|
|
|
private LifeCallback mLifeCallback; |
|
|
|
private LifeCallback mLifeCallback; |
|
|
|
|
|
|
|
|
|
|
@ -70,8 +70,8 @@ import java.util.Set; |
|
|
|
return INSTANCE; |
|
|
|
return INSTANCE; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
AMReceiver get(Object obj) { |
|
|
|
DownloadReceiver download(Object obj) { |
|
|
|
return getTarget(obj); |
|
|
|
return getDownloadTarget(obj); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -115,14 +115,15 @@ import java.util.Set; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 停止所有正在执行的任务 |
|
|
|
* 停止所有正在下载的任务 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void stopAllTask() { |
|
|
|
public void stopAllTask() { |
|
|
|
List<DownloadEntity> allEntity = mManager.getAllDownloadEntity(); |
|
|
|
List<DownloadEntity> allEntity = mManager.getAllDownloadEntity(); |
|
|
|
List<IDownloadCmd> stopCmds = new ArrayList<>(); |
|
|
|
List<IDownloadCmd> stopCmds = new ArrayList<>(); |
|
|
|
for (DownloadEntity entity : allEntity) { |
|
|
|
for (DownloadEntity entity : allEntity) { |
|
|
|
if (entity.getState() == DownloadEntity.STATE_DOWNLOAD_ING) { |
|
|
|
if (entity.getState() == DownloadEntity.STATE_DOWNLOAD_ING) { |
|
|
|
stopCmds.add(CommonUtil.createCmd(entity, CmdFactory.TASK_STOP)); |
|
|
|
stopCmds.add( |
|
|
|
|
|
|
|
CommonUtil.createDownloadCmd(new DownloadTaskEntity(entity), CmdFactory.TASK_STOP)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
mManager.setCmds(stopCmds).exe(); |
|
|
|
mManager.setCmds(stopCmds).exe(); |
|
|
@ -181,20 +182,21 @@ import java.util.Set; |
|
|
|
List<DownloadEntity> allEntity = mManager.getAllDownloadEntity(); |
|
|
|
List<DownloadEntity> allEntity = mManager.getAllDownloadEntity(); |
|
|
|
List<IDownloadCmd> cancelCmds = new ArrayList<>(); |
|
|
|
List<IDownloadCmd> cancelCmds = new ArrayList<>(); |
|
|
|
for (DownloadEntity entity : allEntity) { |
|
|
|
for (DownloadEntity entity : allEntity) { |
|
|
|
cancelCmds.add(CommonUtil.createCmd(entity, CmdFactory.TASK_CANCEL)); |
|
|
|
cancelCmds.add( |
|
|
|
|
|
|
|
CommonUtil.createDownloadCmd(new DownloadTaskEntity(entity), CmdFactory.TASK_CANCEL)); |
|
|
|
} |
|
|
|
} |
|
|
|
mManager.setCmds(cancelCmds).exe(); |
|
|
|
mManager.setCmds(cancelCmds).exe(); |
|
|
|
Set<String> keys = mTargets.keySet(); |
|
|
|
Set<String> keys = mDownloadTargets.keySet(); |
|
|
|
for (String key : keys) { |
|
|
|
for (String key : keys) { |
|
|
|
AMReceiver target = mTargets.get(key); |
|
|
|
DownloadReceiver target = mDownloadTargets.get(key); |
|
|
|
target.removeSchedulerListener(); |
|
|
|
target.removeSchedulerListener(); |
|
|
|
mTargets.remove(key); |
|
|
|
mDownloadTargets.remove(key); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private AMReceiver putTarget(Object obj) { |
|
|
|
private DownloadReceiver putTarget(Object obj) { |
|
|
|
String clsName = obj.getClass().getName(); |
|
|
|
String clsName = obj.getClass().getName(); |
|
|
|
AMReceiver target = null; |
|
|
|
DownloadReceiver target = null; |
|
|
|
String key = ""; |
|
|
|
String key = ""; |
|
|
|
if (!(obj instanceof Activity)) { |
|
|
|
if (!(obj instanceof Activity)) { |
|
|
|
if (obj instanceof android.support.v4.app.Fragment) { |
|
|
|
if (obj instanceof android.support.v4.app.Fragment) { |
|
|
@ -224,11 +226,11 @@ import java.util.Set; |
|
|
|
if (TextUtils.isEmpty(key)) { |
|
|
|
if (TextUtils.isEmpty(key)) { |
|
|
|
throw new IllegalArgumentException("未知类型"); |
|
|
|
throw new IllegalArgumentException("未知类型"); |
|
|
|
} |
|
|
|
} |
|
|
|
target = mTargets.get(key); |
|
|
|
target = mDownloadTargets.get(key); |
|
|
|
if (target == null) { |
|
|
|
if (target == null) { |
|
|
|
target = new AMReceiver(); |
|
|
|
target = new DownloadReceiver(); |
|
|
|
target.targetName = obj.getClass().getName(); |
|
|
|
target.targetName = obj.getClass().getName(); |
|
|
|
mTargets.put(key, target); |
|
|
|
mDownloadTargets.put(key, target); |
|
|
|
} |
|
|
|
} |
|
|
|
return target; |
|
|
|
return target; |
|
|
|
} |
|
|
|
} |
|
|
@ -243,7 +245,7 @@ import java.util.Set; |
|
|
|
(PopupWindow.OnDismissListener) dismissField.get(popupWindow); |
|
|
|
(PopupWindow.OnDismissListener) dismissField.get(popupWindow); |
|
|
|
if (listener != null) { |
|
|
|
if (listener != null) { |
|
|
|
Log.e(TAG, "你已经对PopupWindow设置了Dismiss事件。为了防止内存泄露," |
|
|
|
Log.e(TAG, "你已经对PopupWindow设置了Dismiss事件。为了防止内存泄露," |
|
|
|
+ "请在dismiss方法中调用Aria.whit(this).removeSchedulerListener();来注销事件"); |
|
|
|
+ "请在dismiss方法中调用Aria.download(this).removeSchedulerListener();来注销事件"); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
popupWindow.setOnDismissListener(createPopupWindowListener(popupWindow)); |
|
|
|
popupWindow.setOnDismissListener(createPopupWindowListener(popupWindow)); |
|
|
|
} |
|
|
|
} |
|
|
@ -276,7 +278,7 @@ import java.util.Set; |
|
|
|
Message cancelMsg = (Message) cancelField.get(dialog); |
|
|
|
Message cancelMsg = (Message) cancelField.get(dialog); |
|
|
|
if (cancelMsg != null) { |
|
|
|
if (cancelMsg != null) { |
|
|
|
Log.e(TAG, "你已经对Dialog设置了Dismiss和cancel事件。为了防止内存泄露," |
|
|
|
Log.e(TAG, "你已经对Dialog设置了Dismiss和cancel事件。为了防止内存泄露," |
|
|
|
+ "请在dismiss方法中调用Aria.whit(this).removeSchedulerListener();来注销事件"); |
|
|
|
+ "请在dismiss方法中调用Aria.download(this).removeSchedulerListener();来注销事件"); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
dialog.setOnCancelListener(createCancelListener()); |
|
|
|
dialog.setOnCancelListener(createCancelListener()); |
|
|
|
} |
|
|
|
} |
|
|
@ -288,8 +290,8 @@ import java.util.Set; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private AMReceiver getTarget(Object obj) { |
|
|
|
private DownloadReceiver getDownloadTarget(Object obj) { |
|
|
|
AMReceiver target = mTargets.get(obj.getClass().getName()); |
|
|
|
DownloadReceiver target = mDownloadTargets.get(obj.getClass().getName()); |
|
|
|
if (target == null) { |
|
|
|
if (target == null) { |
|
|
|
target = putTarget(obj); |
|
|
|
target = putTarget(obj); |
|
|
|
} |
|
|
|
} |
|
|
@ -335,17 +337,15 @@ import java.util.Set; |
|
|
|
* onDestroy |
|
|
|
* onDestroy |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void destroySchedulerListener(Object obj) { |
|
|
|
private void destroySchedulerListener(Object obj) { |
|
|
|
Set<String> keys = mTargets.keySet(); |
|
|
|
Set<String> keys = mDownloadTargets.keySet(); |
|
|
|
String clsName = obj.getClass().getName(); |
|
|
|
String clsName = obj.getClass().getName(); |
|
|
|
for (Iterator<Map.Entry<String, AMReceiver>> iter = mTargets.entrySet().iterator(); |
|
|
|
for ( |
|
|
|
|
|
|
|
Iterator<Map.Entry<String, DownloadReceiver>> iter = mDownloadTargets.entrySet().iterator(); |
|
|
|
iter.hasNext(); ) { |
|
|
|
iter.hasNext(); ) { |
|
|
|
Map.Entry<String, AMReceiver> entry = iter.next(); |
|
|
|
Map.Entry<String, DownloadReceiver> entry = iter.next(); |
|
|
|
String key = entry.getKey(); |
|
|
|
String key = entry.getKey(); |
|
|
|
if (key.equals(clsName) || key.contains(clsName)) { |
|
|
|
if (key.equals(clsName) || key.contains(clsName)) { |
|
|
|
AMReceiver receiver = mTargets.get(key); |
|
|
|
DownloadReceiver receiver = mDownloadTargets.get(key); |
|
|
|
if (receiver.obj != null) { |
|
|
|
|
|
|
|
if (receiver.obj instanceof Application || receiver.obj instanceof Service) break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
receiver.removeSchedulerListener(); |
|
|
|
receiver.removeSchedulerListener(); |
|
|
|
receiver.destroy(); |
|
|
|
receiver.destroy(); |
|
|
|
iter.remove(); |
|
|
|
iter.remove(); |
|
|
|