|
|
@ -29,6 +29,7 @@ import android.support.v4.app.Fragment; |
|
|
|
import android.text.TextUtils; |
|
|
|
import android.text.TextUtils; |
|
|
|
import android.util.Log; |
|
|
|
import android.util.Log; |
|
|
|
import com.arialyy.aria.core.command.CmdFactory; |
|
|
|
import com.arialyy.aria.core.command.CmdFactory; |
|
|
|
|
|
|
|
import com.arialyy.aria.core.scheduler.OnSchedulerListener; |
|
|
|
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.IDownloadCmd; |
|
|
@ -37,6 +38,7 @@ import java.io.File; |
|
|
|
import java.lang.reflect.Field; |
|
|
|
import java.lang.reflect.Field; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
import java.util.Iterator; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.Set; |
|
|
@ -47,12 +49,12 @@ import java.util.Set; |
|
|
|
* Aria管理器,任务操作在这里执行 |
|
|
|
* Aria管理器,任务操作在这里执行 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public class AriaManager { |
|
|
|
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public class AriaManager { |
|
|
|
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, AMReceiver> mTargets = new HashMap<>(); |
|
|
|
private DownloadManager mManager; |
|
|
|
private DownloadManager mManager; |
|
|
|
private LifeCallback mLifeCallback; |
|
|
|
private LifeCallback mLifeCallback; |
|
|
|
|
|
|
|
|
|
|
|
private AriaManager(Context context) { |
|
|
|
private AriaManager(Context context) { |
|
|
|
regAppLifeCallback(context); |
|
|
|
regAppLifeCallback(context); |
|
|
@ -99,7 +101,7 @@ 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.createCmd(entity, CmdFactory.TASK_STOP)); |
|
|
@ -158,8 +160,8 @@ import java.util.Set; |
|
|
|
* 删除所有任务 |
|
|
|
* 删除所有任务 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void cancelAllTask() { |
|
|
|
public void cancelAllTask() { |
|
|
|
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.createCmd(entity, CmdFactory.TASK_CANCEL)); |
|
|
|
} |
|
|
|
} |
|
|
@ -173,9 +175,9 @@ import java.util.Set; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private AMReceiver putTarget(Object obj) { |
|
|
|
private AMReceiver putTarget(Object obj) { |
|
|
|
String clsName = obj.getClass().getName(); |
|
|
|
String clsName = obj.getClass().getName(); |
|
|
|
AMReceiver target = null; |
|
|
|
AMReceiver 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) { |
|
|
|
key = clsName + "_" + ((Fragment) obj).getActivity().getClass().getName(); |
|
|
|
key = clsName + "_" + ((Fragment) obj).getActivity().getClass().getName(); |
|
|
@ -209,12 +211,12 @@ import java.util.Set; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void handleDialogDialogLift(Dialog dialog) { |
|
|
|
private void handleDialogDialogLift(Dialog dialog) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
Field dismissField = CommonUtil.getField(dialog.getClass(), "mDismissMessage"); |
|
|
|
Field dismissField = CommonUtil.getField(dialog.getClass(), "mDismissMessage"); |
|
|
|
Message dismissMsg = (Message) dismissField.get(dialog); |
|
|
|
Message dismissMsg = (Message) dismissField.get(dialog); |
|
|
|
//如果Dialog已经设置Dismiss事件,则查找cancel事件
|
|
|
|
//如果Dialog已经设置Dismiss事件,则查找cancel事件
|
|
|
|
if (dismissMsg != null) { |
|
|
|
if (dismissMsg != null) { |
|
|
|
Field cancelField = CommonUtil.getField(dialog.getClass(), "mCancelMessage"); |
|
|
|
Field cancelField = CommonUtil.getField(dialog.getClass(), "mCancelMessage"); |
|
|
|
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.whit(this).removeSchedulerListener();来注销事件"); |
|
|
@ -282,20 +284,35 @@ import java.util.Set; |
|
|
|
* onDestroy |
|
|
|
* onDestroy |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void destroySchedulerListener(Object obj) { |
|
|
|
private void destroySchedulerListener(Object obj) { |
|
|
|
Set<String> keys = mTargets.keySet(); |
|
|
|
Set<String> keys = mTargets.keySet(); |
|
|
|
String clsName = obj.getClass().getName(); |
|
|
|
String clsName = obj.getClass().getName(); |
|
|
|
for (String key : keys) { |
|
|
|
for (Iterator<Map.Entry<String, AMReceiver>> iter = mTargets.entrySet().iterator(); |
|
|
|
|
|
|
|
iter.hasNext(); ) { |
|
|
|
|
|
|
|
Map.Entry<String, AMReceiver> entry = iter.next(); |
|
|
|
|
|
|
|
String key = entry.getKey(); |
|
|
|
if (key.equals(clsName) || key.contains(clsName)) { |
|
|
|
if (key.equals(clsName) || key.contains(clsName)) { |
|
|
|
AMReceiver receiver = mTargets.get(key); |
|
|
|
AMReceiver receiver = mTargets.get(key); |
|
|
|
if (receiver.obj != null) { |
|
|
|
if (receiver.obj != null) { |
|
|
|
if (receiver.obj instanceof Application || receiver.obj instanceof Service) break; |
|
|
|
if (receiver.obj instanceof Application || receiver.obj instanceof Service) break; |
|
|
|
receiver.removeSchedulerListener(); |
|
|
|
receiver.removeSchedulerListener(); |
|
|
|
receiver.destroy(); |
|
|
|
receiver.destroy(); |
|
|
|
mTargets.remove(key); |
|
|
|
iter.remove(); |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//for (String key : keys) {
|
|
|
|
|
|
|
|
// if (key.equals(clsName) || key.contains(clsName)) {
|
|
|
|
|
|
|
|
// AMReceiver receiver = mTargets.get(key);
|
|
|
|
|
|
|
|
// if (receiver.obj != null) {
|
|
|
|
|
|
|
|
// if (receiver.obj instanceof Application || receiver.obj instanceof Service) break;
|
|
|
|
|
|
|
|
// receiver.removeSchedulerListener();
|
|
|
|
|
|
|
|
// receiver.destroy();
|
|
|
|
|
|
|
|
// mTargets.remove(key);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// break;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
//}
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|