添加dialog支持、修复一个内存溢出的问题

pull/2/head
AriaLyy 8 years ago
parent 2e164100d1
commit d9107ad23b
  1. 3
      .idea/misc.xml
  2. 18
      Aria/src/main/java/com/arialyy/aria/core/AMTarget.java
  3. 95
      Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
  4. 24
      Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java
  5. 2
      Aria/src/main/java/com/arialyy/aria/orm/DbUtil.java

@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="NullableNotNullManager"> <component name="NullableNotNullManager">
<option name="myDefaultNullable" value="android.support.annotation.Nullable" /> <option name="myDefaultNullable" value="android.support.annotation.Nullable" />
<option name="myDefaultNotNull" value="android.support.annotation.NonNull" /> <option name="myDefaultNotNull" value="android.support.annotation.NonNull" />

@ -30,7 +30,7 @@ import java.util.List;
public class AMTarget { public class AMTarget {
private AMReceiver receiver; private AMReceiver receiver;
public AMTarget(AMReceiver receiver) { AMTarget(AMReceiver receiver) {
this.receiver = receiver; this.receiver = receiver;
} }
@ -50,6 +50,7 @@ public class AMTarget {
cmds.add(CommonUtil.createCmd(receiver.obj, receiver.entity, CmdFactory.TASK_CREATE)); cmds.add(CommonUtil.createCmd(receiver.obj, receiver.entity, CmdFactory.TASK_CREATE));
cmds.add(CommonUtil.createCmd(receiver.obj, receiver.entity, CmdFactory.TASK_START)); cmds.add(CommonUtil.createCmd(receiver.obj, receiver.entity, CmdFactory.TASK_START));
receiver.manager.setCmds(cmds).exe(); receiver.manager.setCmds(cmds).exe();
cmds.clear();
} }
/** /**
@ -76,6 +77,21 @@ public class AMTarget {
CommonUtil.createCmd(receiver.obj, receiver.entity, CmdFactory.TASK_CANCEL)).exe(); CommonUtil.createCmd(receiver.obj, receiver.entity, CmdFactory.TASK_CANCEL)).exe();
} }
/**
* 是否在下载
*/
public boolean isDownloading() {
return DownloadManager.getInstance().getTaskQueue().getTask(receiver.entity).isDownloading();
}
/**
* 重新下载
*/
public void reStart() {
cancel();
start();
}
public static class SimpleSchedulerListener implements OnSchedulerListener { public static class SimpleSchedulerListener implements OnSchedulerListener {
@Override public void onTaskPre(Task task) { @Override public void onTaskPre(Task task) {

@ -45,12 +45,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);
@ -93,7 +93,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));
@ -152,8 +152,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));
} }
@ -167,8 +167,8 @@ 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 = mTargets.get(clsName); AMReceiver target = mTargets.get(clsName);
if (target == null) { if (target == null) {
target = new AMReceiver(); target = new AMReceiver();
target.obj = obj; target.obj = obj;
@ -197,20 +197,20 @@ 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) dismissField.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();来注销事件");
} else { } else {
dialog.setCancelMessage(createCancelMessage());
} }
} else { } else {
dialog.setCancelMessage(createDismissMessage());
} }
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
@ -236,6 +236,55 @@ import java.util.Set;
} }
} }
/**
* 创建Dialog取消消息
*/
private Message createCancelMessage() {
final Message cancelMsg = new Message();
cancelMsg.what = 0x44;
cancelMsg.obj = new Dialog.OnCancelListener() {
@Override public void onCancel(DialogInterface dialog) {
destroySchedulerListener(dialog);
}
};
return cancelMsg;
}
/**
* 创建Dialog dismiss取消消息
*/
private Message createDismissMessage() {
final Message cancelMsg = new Message();
cancelMsg.what = 0x43;
cancelMsg.obj = new Dialog.OnDismissListener() {
@Override public void onDismiss(DialogInterface dialog) {
destroySchedulerListener(dialog);
}
};
return cancelMsg;
}
/**
* onDestroy
*/
private void destroySchedulerListener(Object obj) {
Set<String> keys = mTargets.keySet();
String clsName = obj.getClass().getName();
for (String key : keys) {
if (key.equals(clsName) || key.contains(clsName)) {
AMReceiver target = mTargets.get(key);
if (target.obj != null) {
if (target.obj instanceof Application || target.obj instanceof Service) break;
target.removeSchedulerListener();
mTargets.remove(key);
}
break;
}
}
}
/** /**
* Activity生命周期 * Activity生命周期
*/ */
@ -266,19 +315,7 @@ import java.util.Set;
} }
@Override public void onActivityDestroyed(Activity activity) { @Override public void onActivityDestroyed(Activity activity) {
Set<String> keys = mTargets.keySet(); destroySchedulerListener(activity);
for (String key : keys) {
String clsName = activity.getClass().getName();
if (key.equals(clsName) || key.contains(clsName)) {
AMReceiver target = mTargets.get(key);
if (target.obj != null) {
if (target.obj instanceof Application || target.obj instanceof Service) break;
target.removeSchedulerListener();
mTargets.remove(key);
}
break;
}
}
} }
} }
} }

@ -36,37 +36,37 @@ 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; public static final int RESUME = 7;
private static final String TAG = "DownloadSchedulers"; private static final String TAG = "DownloadSchedulers";
private static final Object LOCK = new Object(); private static final Object LOCK = new Object();
private static volatile DownloadSchedulers INSTANCE = null; private static volatile DownloadSchedulers INSTANCE = null;
/** /**
@ -223,6 +223,8 @@ public class DownloadSchedulers implements IDownloadSchedulers {
if (target == null) { if (target == null) {
throw new IllegalArgumentException("target 不能为null"); throw new IllegalArgumentException("target 不能为null");
} }
mSchedulerListeners.remove(target.getClass().getName()); OnSchedulerListener listener = mSchedulerListeners.get(target.getClass().getName());
mSchedulerListeners.remove(listener);
listener = null;
} }
} }

@ -436,7 +436,7 @@ public class DbUtil {
} }
entity.rowID = cursor.getInt(cursor.getColumnIndex("rowid")); entity.rowID = cursor.getInt(cursor.getColumnIndex("rowid"));
entitys.add(entity); entitys.add(entity);
Log.d(TAG, "rowid ==> " + entity.rowID); //Log.d(TAG, "rowid ==> " + entity.rowID);
} }
} catch (InstantiationException e) { } catch (InstantiationException e) {
e.printStackTrace(); e.printStackTrace();

Loading…
Cancel
Save