cmds = new ArrayList<>();
- cmds.add(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_CREATE));
- cmds.add(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_START));
+ cmds.add(CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_CREATE));
+ cmds.add(CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_START));
DownloadManager.getInstance().setCmds(cmds).exe();
cmds.clear();
}
@@ -109,7 +110,7 @@ public class AMTarget {
*/
public void stop() {
DownloadManager.getInstance()
- .setCmd(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_STOP))
+ .setCmd(CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_STOP))
.exe();
}
@@ -118,7 +119,7 @@ public class AMTarget {
*/
public void resume() {
DownloadManager.getInstance()
- .setCmd(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_START))
+ .setCmd(CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_START))
.exe();
}
@@ -127,7 +128,8 @@ public class AMTarget {
*/
public void cancel() {
DownloadManager.getInstance()
- .setCmd(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_CANCEL))
+ .setCmd(
+ CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_CANCEL))
.exe();
}
diff --git a/Aria/src/main/java/com/arialyy/aria/core/Aria.java b/Aria/src/main/java/com/arialyy/aria/core/Aria.java
index 705352d3..167d7cc4 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/Aria.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/Aria.java
@@ -24,7 +24,10 @@ import android.app.DialogFragment;
import android.app.Fragment;
import android.app.Service;
import android.content.Context;
+import android.content.ContextWrapper;
import android.os.Build;
+import android.view.ContextThemeWrapper;
+import android.widget.PopupWindow;
import com.arialyy.aria.core.scheduler.OnSchedulerListener;
import com.arialyy.aria.core.task.Task;
@@ -34,13 +37,13 @@ import com.arialyy.aria.core.task.Task;
* Aria启动,管理全局任务
*
*
- * DownloadEntity mEntity = new DownloadEntity();
- * mEntity.setFileName(fileName); //设置文件名
- * mEntity.setDownloadUrl(downloadUrl); //设置下载链接
- * mEntity.setDownloadPath(downloadPath); //设置存放路径
- *
- * //启动下载
- * Aria.whit(this).load(mEntity).start();
+ * //启动下载
+ * Aria.whit(this)
+ * .load(DOWNLOAD_URL) //下载地址,必填
+ * //文件保存路径,必填
+ * .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk")
+ * .setDownloadName("test.apk") //文件名,必填
+ * .start();
*
*
*/
@@ -113,15 +116,33 @@ import com.arialyy.aria.core.task.Task;
}
/**
- * 处理Fragment、或者DialogFragment
+ * 处理Fragment
*/
- public static AMReceiver whit(Fragment fragment) {
+ private static AMReceiver whit(Fragment fragment) {
checkNull(fragment);
return AriaManager.getInstance(
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? fragment.getContext()
: fragment.getActivity()).get(fragment);
}
+ /**
+ * 处理Fragment、或者DialogFragment
+ */
+ public static AMReceiver whit(DialogFragment dialog) {
+ checkNull(dialog);
+ return AriaManager.getInstance(
+ Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? dialog.getContext() : dialog.getActivity())
+ .get(dialog);
+ }
+
+ /**
+ * 处理popupwindow
+ */
+ public static AMReceiver whit(PopupWindow popupWindow) {
+ checkNull(popupWindow);
+ return AriaManager.getInstance(popupWindow.getContentView().getContext()).get(popupWindow);
+ }
+
/**
* 处理Dialog
*/
@@ -148,6 +169,22 @@ import com.arialyy.aria.core.task.Task;
}
}
+ /**
+ * 处理Dialog的通用任务
+ */
+ public static AriaManager get(Dialog dialog) {
+ checkNull(dialog);
+ return AriaManager.getInstance(dialog.getContext());
+ }
+
+ /**
+ * 处理Dialog的通用任务
+ */
+ public static AriaManager get(PopupWindow popupWindow) {
+ checkNull(popupWindow);
+ return AriaManager.getInstance(popupWindow.getContentView().getContext());
+ }
+
public static class SimpleSchedulerListener implements OnSchedulerListener {
@Override public void onTaskPre(Task task) {
diff --git a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
index a48e93c8..b02cdceb 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
@@ -28,15 +28,16 @@ import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
+import android.widget.PopupWindow;
import com.arialyy.aria.core.command.CmdFactory;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.core.command.IDownloadCmd;
import com.arialyy.aria.util.Configuration;
-import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -185,29 +186,68 @@ import java.util.Set;
Activity activity = ((Dialog) obj).getOwnerActivity();
if (activity != null) {
key = clsName + "_" + activity.getClass().getName();
+ } else {
+ key = clsName;
+ }
+ handleDialogLift((Dialog) obj);
+ } else if (obj instanceof PopupWindow) {
+ Context context = ((PopupWindow) obj).getContentView().getContext();
+ if (context instanceof Activity) {
+ key = clsName + "_" + context.getClass().getName();
+ } else {
+ key = clsName;
}
- handleDialogDialogLift((Dialog) obj);
+ handlePopupWindowLift((PopupWindow) obj);
}
} else {
key = clsName;
}
if (TextUtils.isEmpty(key)) {
throw new IllegalArgumentException("未知类型");
- } else {
- target = mTargets.get(key);
- if (target == null) {
- target = new AMReceiver();
- target.obj = obj;
- mTargets.put(key, target);
- }
+ }
+ target = mTargets.get(key);
+ if (target == null) {
+ target = new AMReceiver();
+ target.targetName = obj.getClass().getName();
+ mTargets.put(key, target);
}
return target;
}
+ /**
+ * 出来悬浮框取消或dismiss
+ */
+ private void handlePopupWindowLift(PopupWindow popupWindow) {
+ try {
+ Field dismissField = CommonUtil.getField(popupWindow.getClass(), "mOnDismissListener");
+ PopupWindow.OnDismissListener listener =
+ (PopupWindow.OnDismissListener) dismissField.get(popupWindow);
+ if (listener != null) {
+ Log.e(TAG, "你已经对PopupWindow设置了Dismiss事件。为了防止内存泄露,"
+ + "请在dismiss方法中调用Aria.whit(this).removeSchedulerListener();来注销事件");
+ } else {
+ popupWindow.setOnDismissListener(createPopupWindowListener(popupWindow));
+ }
+ } catch (IllegalAccessException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * 创建popupWindow dismiss事件
+ */
+ private PopupWindow.OnDismissListener createPopupWindowListener(final PopupWindow popupWindow) {
+ return new PopupWindow.OnDismissListener() {
+ @Override public void onDismiss() {
+ destroySchedulerListener(popupWindow);
+ }
+ };
+ }
+
/**
* 处理对话框取消或dismiss
*/
- private void handleDialogDialogLift(Dialog dialog) {
+ private void handleDialogLift(Dialog dialog) {
try {
Field dismissField = CommonUtil.getField(dialog.getClass(), "mDismissMessage");
Message dismissMsg = (Message) dismissField.get(dialog);
@@ -219,10 +259,10 @@ import java.util.Set;
Log.e(TAG, "你已经对Dialog设置了Dismiss和cancel事件。为了防止内存泄露,"
+ "请在dismiss方法中调用Aria.whit(this).removeSchedulerListener();来注销事件");
} else {
- dialog.setCancelMessage(createCancelMessage());
+ dialog.setOnCancelListener(createCancelListener());
}
} else {
- dialog.setCancelMessage(createDismissMessage());
+ dialog.setOnDismissListener(createDismissListener());
}
} catch (IllegalAccessException e) {
e.printStackTrace();
@@ -249,33 +289,27 @@ import java.util.Set;
}
/**
- * 创建Dialog取消消息
+ * 创建Dialog取消事件
*/
- private Message createCancelMessage() {
- final Message cancelMsg = new Message();
- cancelMsg.what = 0x44;
- cancelMsg.obj = new Dialog.OnCancelListener() {
+ private Dialog.OnCancelListener createCancelListener() {
+ return new Dialog.OnCancelListener() {
@Override public void onCancel(DialogInterface dialog) {
destroySchedulerListener(dialog);
}
};
- return cancelMsg;
}
/**
- * 创建Dialog dismiss取消消息
+ * 创建Dialog dismiss取消事件
*/
- private Message createDismissMessage() {
- final Message cancelMsg = new Message();
- cancelMsg.what = 0x43;
- cancelMsg.obj = new Dialog.OnDismissListener() {
+ private Dialog.OnDismissListener createDismissListener() {
+ return new Dialog.OnDismissListener() {
@Override public void onDismiss(DialogInterface dialog) {
destroySchedulerListener(dialog);
}
};
- return cancelMsg;
}
/**
@@ -284,15 +318,18 @@ import java.util.Set;
private void destroySchedulerListener(Object obj) {
Set keys = mTargets.keySet();
String clsName = obj.getClass().getName();
- for (String key : keys) {
+ for (Iterator> iter = mTargets.entrySet().iterator();
+ iter.hasNext(); ) {
+ Map.Entry entry = iter.next();
+ String key = entry.getKey();
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);
}
+ receiver.removeSchedulerListener();
+ receiver.destroy();
+ iter.remove();
break;
}
}
diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java
index 8781fe29..320d916f 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java
@@ -30,14 +30,14 @@ class AddCmd extends IDownloadCmd {
super(entity);
}
- AddCmd(Object target, DownloadEntity entity) {
+ AddCmd(String target, DownloadEntity entity) {
super(target, entity);
}
@Override public void executeCmd() {
Task task = mQueue.getTask(mEntity);
if (task == null) {
- mQueue.createTask(mTarget, mEntity);
+ mQueue.createTask(mTargetName, mEntity);
} else {
Log.w(TAG, "添加命令执行失败,【该任务已经存在】");
}
diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java
index 9e80372d..333e03fc 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java
@@ -25,7 +25,7 @@ import com.arialyy.aria.core.task.Task;
*/
class CancelCmd extends IDownloadCmd {
- CancelCmd(Object target, DownloadEntity entity) {
+ CancelCmd(String target, DownloadEntity entity) {
super(target, entity);
}
@@ -36,11 +36,11 @@ class CancelCmd extends IDownloadCmd {
@Override public void executeCmd() {
Task task = mQueue.getTask(mEntity);
if (task == null) {
- task = mQueue.createTask(mTarget, mEntity);
+ task = mQueue.createTask(mTargetName, mEntity);
}
if (task != null) {
- if (mTarget != null) {
- task.setTargetName(mTarget.getClass().getName());
+ if (mTargetName != null) {
+ task.setTargetName(mTargetName);
}
mQueue.cancelTask(task);
}
diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/CmdFactory.java b/Aria/src/main/java/com/arialyy/aria/core/command/CmdFactory.java
index 8b676ee2..786fb9dc 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/command/CmdFactory.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/command/CmdFactory.java
@@ -90,7 +90,7 @@ public class CmdFactory {
* @param type 命令类型{@link #TASK_CREATE}、{@link #TASK_START}、{@link #TASK_CANCEL}、{@link
* #TASK_STOP}
*/
- public IDownloadCmd createCmd(Object target, DownloadEntity entity, int type) {
+ public IDownloadCmd createCmd(String target, DownloadEntity entity, int type) {
switch (type) {
case TASK_CREATE:
return createAddCmd(target, entity);
@@ -113,7 +113,7 @@ public class CmdFactory {
*
* @return {@link StopCmd}
*/
- private StopCmd createStopCmd(Object target, DownloadEntity entity) {
+ private StopCmd createStopCmd(String target, DownloadEntity entity) {
return new StopCmd(target, entity);
}
@@ -131,7 +131,7 @@ public class CmdFactory {
*
* @return {@link AddCmd}
*/
- private AddCmd createAddCmd(Object target, DownloadEntity entity) {
+ private AddCmd createAddCmd(String target, DownloadEntity entity) {
return new AddCmd(target, entity);
}
@@ -149,7 +149,7 @@ public class CmdFactory {
*
* @return {@link StartCmd}
*/
- private StartCmd createStartCmd(Object target, DownloadEntity entity) {
+ private StartCmd createStartCmd(String target, DownloadEntity entity) {
return new StartCmd(target, entity);
}
@@ -167,7 +167,7 @@ public class CmdFactory {
*
* @return {@link CancelCmd}
*/
- private CancelCmd createCancelCmd(Object target, DownloadEntity entity) {
+ private CancelCmd createCancelCmd(String target, DownloadEntity entity) {
return new CancelCmd(target, entity);
}
diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/IDownloadCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/IDownloadCmd.java
index 80d85237..0bd6a313 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/command/IDownloadCmd.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/command/IDownloadCmd.java
@@ -27,10 +27,10 @@ import com.arialyy.aria.core.DownloadEntity;
* 下载命令
*/
public abstract class IDownloadCmd {
- ITaskQueue mQueue;
+ ITaskQueue mQueue;
DownloadEntity mEntity;
- String TAG;
- Object mTarget;
+ String TAG;
+ String mTargetName;
/**
* @param entity 下载实体
@@ -40,13 +40,13 @@ public abstract class IDownloadCmd {
}
/**
- * @param target 产生任务的对象
+ * @param targetName 产生任务的对象名
*/
- IDownloadCmd(Object target, DownloadEntity entity) {
+ IDownloadCmd(String targetName, DownloadEntity entity) {
if (!CheckUtil.checkDownloadEntity(entity)) {
return;
}
- mTarget = target;
+ mTargetName = targetName;
mEntity = entity;
TAG = CommonUtil.getClassName(this);
mQueue = DownloadManager.getInstance().getTaskQueue();
diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/SingleCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/SingleCmd.java
index a45f0b2f..f7bbe04e 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/command/SingleCmd.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/command/SingleCmd.java
@@ -27,7 +27,7 @@ class SingleCmd extends IDownloadCmd {
/**
* @param entity 下载实体
*/
- SingleCmd(Object target, DownloadEntity entity) {
+ SingleCmd(String target, DownloadEntity entity) {
super(target, entity);
}
@@ -38,11 +38,11 @@ class SingleCmd extends IDownloadCmd {
@Override public void executeCmd() {
Task task = mQueue.getTask(mEntity);
if (task == null) {
- task = mQueue.createTask(mTarget, mEntity);
+ task = mQueue.createTask(mTargetName, mEntity);
} else {
Log.w(TAG, "添加命令执行失败,【该任务已经存在】");
}
- task.setTargetName(mTarget.getClass().getName());
+ task.setTargetName(mTargetName);
mQueue.startTask(task);
}
}
diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java
index d30c0b77..11c2dfc2 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java
@@ -25,7 +25,7 @@ import com.arialyy.aria.core.task.Task;
*/
class StartCmd extends IDownloadCmd {
- StartCmd(Object target, DownloadEntity entity) {
+ StartCmd(String target, DownloadEntity entity) {
super(target, entity);
}
@@ -36,10 +36,10 @@ class StartCmd extends IDownloadCmd {
@Override public void executeCmd() {
Task task = mQueue.getTask(mEntity);
if (task == null) {
- task = mQueue.createTask(mTarget, mEntity);
+ task = mQueue.createTask(mTargetName, mEntity);
}
if (task != null) {
- task.setTargetName(mTarget.getClass().getName());
+ task.setTargetName(mTargetName);
mQueue.startTask(task);
}
}
diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java
index 611f6e12..676e8da7 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java
@@ -16,6 +16,7 @@
package com.arialyy.aria.core.command;
+import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.DownloadEntity;
import com.arialyy.aria.core.task.Task;
@@ -29,7 +30,7 @@ class StopCmd extends IDownloadCmd {
/**
* @param entity 下载实体
*/
- StopCmd(Object target, DownloadEntity entity) {
+ StopCmd(String target, DownloadEntity entity) {
super(target, entity);
}
@@ -41,14 +42,14 @@ class StopCmd extends IDownloadCmd {
Task task = mQueue.getTask(mEntity);
if (task == null) {
if (mEntity.getState() == DownloadEntity.STATE_DOWNLOAD_ING) {
- task = mQueue.createTask(mTarget, mEntity);
+ task = mQueue.createTask(mTargetName, mEntity);
mQueue.stopTask(task);
} else {
Log.w(TAG, "停止命令执行失败,【调度器中没有该任务】");
}
} else {
- if (mTarget != null) {
- task.setTargetName(mTarget.getClass().getName());
+ if (!TextUtils.isEmpty(mTargetName)) {
+ task.setTargetName(mTargetName);
}
mQueue.stopTask(task);
}
diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java
index e479b9cc..06228179 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java
@@ -17,6 +17,7 @@
package com.arialyy.aria.core.queue;
import android.content.Context;
+import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.DownloadEntity;
import com.arialyy.aria.core.queue.pool.CachePool;
@@ -32,9 +33,9 @@ import com.arialyy.aria.util.Configuration;
* 下载任务队列
*/
public class DownloadTaskQueue implements ITaskQueue {
- private static final String TAG = "DownloadTaskQueue";
- private CachePool mCachePool = CachePool.getInstance();
- private ExecutePool mExecutePool = ExecutePool.getInstance();
+ private static final String TAG = "DownloadTaskQueue";
+ private CachePool mCachePool = CachePool.getInstance();
+ private ExecutePool mExecutePool = ExecutePool.getInstance();
private Context mContext;
//private IDownloadSchedulers mSchedulers;
@@ -155,17 +156,16 @@ public class DownloadTaskQueue implements ITaskQueue {
}
}
- @Override public Task createTask(Object target, DownloadEntity entity) {
+ @Override public Task createTask(String target, DownloadEntity entity) {
Task task;
- if (target == null) {
+ if (TextUtils.isEmpty(target)) {
//task = TaskFactory.getInstance().createTask(mContext, entity, mSchedulers);
- task = TaskFactory.getInstance()
- .createTask(mContext, entity, DownloadSchedulers.getInstance());
+ task =
+ TaskFactory.getInstance().createTask(mContext, entity, DownloadSchedulers.getInstance());
} else {
task = TaskFactory.getInstance()
//.createTask(target.getClass().getName(), mContext, entity, mSchedulers);
- .createTask(target.getClass().getName(), mContext, entity,
- DownloadSchedulers.getInstance());
+ .createTask(target, mContext, entity, DownloadSchedulers.getInstance());
}
mCachePool.putTask(task);
return task;
@@ -202,7 +202,7 @@ public class DownloadTaskQueue implements ITaskQueue {
//}
public static class Builder {
- Context context;
+ Context context;
IDownloadSchedulers schedulers;
public Builder(Context context) {
diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java
index 485eba63..73163766 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java
@@ -47,10 +47,10 @@ public interface ITaskQueue extends IDownloader {
* 创建一个新的下载任务,创建时只是将新任务存储到缓存池
*
* @param entity 下载实体{@link DownloadEntity}
- * @param target 生成该任务的对象
+ * @param targetName 生成该任务的对象
* @return {@link Task}
*/
- public Task createTask(Object target, DownloadEntity entity);
+ public Task createTask(String targetName, DownloadEntity entity);
/**
* 通过下载链接从缓存池或任务池搜索下载任务,如果缓存池或任务池都没有任务,则创建新任务
diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java
index e6d70851..5f5b8a4e 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java
@@ -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 mSchedulerListeners = new ConcurrentHashMap<>();
- DownloadManager mManager = DownloadManager.getInstance();
+ DownloadManager mManager = DownloadManager.getInstance();
ITaskQueue mQueue;
private DownloadSchedulers() {
@@ -90,30 +91,20 @@ 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) {
- Log.w(TAG, "监听器已存在");
- return;
- }
- mSchedulerListeners.put(name, schedulerListener);
+ @Override
+ public void addSchedulerListener(String targetName, OnSchedulerListener 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> iter =
mSchedulerListeners.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry entry = iter.next();
- if (entry.getKey().equals(target.getClass().getName())) iter.remove();
+ if (entry.getKey().equals(targetName)) iter.remove();
}
}
@@ -151,8 +142,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 keys = mSchedulerListeners.keySet();
+ for (String key : keys) {
+ callback(state, task, mSchedulerListeners.get(key));
}
}
}
diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/IDownloadSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/IDownloadSchedulers.java
index d8b2fda9..5b38ceb5 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/IDownloadSchedulers.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/IDownloadSchedulers.java
@@ -28,16 +28,16 @@ public interface IDownloadSchedulers extends Handler.Callback {
/**
* 注册下载器监听,一个观察者只能注册一次监听
*
- * @param target 观察者,创建该监听器的对象
+ * @param targetName 观察者,创建该监听器的对象类名
* @param schedulerListener {@link OnSchedulerListener}
*/
- public void addSchedulerListener(Object target, OnSchedulerListener schedulerListener);
+ public void addSchedulerListener(String targetName, OnSchedulerListener schedulerListener);
/**
- * @param target 观察者,创建该监听器的对象
+ * @param targetName 观察者,创建该监听器的对象类名
* 取消注册监听器
*/
- public void removeSchedulerListener(Object target, OnSchedulerListener schedulerListener);
+ public void removeSchedulerListener(String targetName, OnSchedulerListener schedulerListener);
/**
* 处理下载任务下载失败的情形
diff --git a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
index 612078b3..3031b232 100644
--- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
+++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
@@ -40,7 +40,7 @@ import java.util.Properties;
public class CommonUtil {
private static final String TAG = "util";
- public static IDownloadCmd createCmd(Object target, DownloadEntity entity, int cmd) {
+ public static IDownloadCmd createCmd(String target, DownloadEntity entity, int cmd) {
return CmdFactory.getInstance().createCmd(target, entity, cmd);
}
diff --git a/README.md b/README.md
index 2974c45c..a151f00b 100644
--- a/README.md
+++ b/README.md
@@ -3,6 +3,7 @@
下载不应该是让人感到痛苦的功能,Aria,致力于让下载傻瓜化。
+ Aria有以下特点:
- 简单
+ - 可在Dialog、popupWindow等组件中使用
- 可自定义是否使用广播
- 支持多线程、多任务下载
- 支持任务自动切换
@@ -15,14 +16,14 @@
## 下载
[![Download](https://api.bintray.com/packages/arialyy/maven/Aria/images/download.svg)](https://bintray.com/arialyy/maven/Aria/_latestVersion)
```java
-compile 'com.arialyy.aria:Aria:2.3.3'
+compile 'com.arialyy.aria:Aria:2.3.6'
```
## 示例
![多任务下载](https://github.com/AriaLyy/DownloadUtil/blob/v_2.0/img/download_img.gif)
-![单任务下载](https://github.com/AriaLyy/DownloadUtil/blob/master/img/11.gif "")
+![Dialog使用](https://github.com/AriaLyy/DownloadUtil/blob/v_2.0/img/dialog_use.gif "")
## 性能展示
![性能展示](https://github.com/AriaLyy/DownloadUtil/blob/v_2.0/img/performance.png)
@@ -113,6 +114,7 @@ compile 'com.arialyy.aria:Aria:2.3.3'
***
## 开发日志
+ + v_2.3.6 添加dialog、popupWindow支持
+ v_2.3.3
- 添加断点支持
- 修改下载逻辑,让使用更加简单
diff --git a/app/src/main/java/com/arialyy/simple/activity/MainActivity.java b/app/src/main/java/com/arialyy/simple/activity/MainActivity.java
index f0b71af9..8959c5a7 100644
--- a/app/src/main/java/com/arialyy/simple/activity/MainActivity.java
+++ b/app/src/main/java/com/arialyy/simple/activity/MainActivity.java
@@ -21,6 +21,7 @@ import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
+import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import butterknife.Bind;
@@ -30,14 +31,18 @@ import com.arialyy.frame.util.show.T;
import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.ActivityMainBinding;
+import com.arialyy.simple.dialog.DownloadDialog;
+import com.arialyy.simple.pop.DownloadPopupWindow;
/**
* Created by Lyy on 2016/10/13.
*/
public class MainActivity extends BaseActivity {
- @Bind(R.id.toolbar) Toolbar mBar;
- @Bind(R.id.single_task) Button mSigleBt;
- @Bind(R.id.multi_task) Button mMultiBt;
+ @Bind(R.id.toolbar) Toolbar mBar;
+ @Bind(R.id.single_task) Button mSigleBt;
+ @Bind(R.id.multi_task) Button mMultiBt;
+ @Bind(R.id.dialog_task) Button mDialogBt;
+ @Bind(R.id.pop_task) Button mPopBt;
@Override protected int setLayoutId() {
return R.layout.activity_main;
@@ -73,6 +78,8 @@ public class MainActivity extends BaseActivity {
private void setEnable(boolean enable) {
mSigleBt.setEnabled(enable);
mMultiBt.setEnabled(enable);
+ mDialogBt.setEnabled(enable);
+ mPopBt.setEnabled(enable);
}
public void onClick(View view) {
@@ -83,6 +90,15 @@ public class MainActivity extends BaseActivity {
case R.id.multi_task:
startActivity(new Intent(this, MultiTaskActivity.class));
break;
+ case R.id.dialog_task:
+ DownloadDialog dialog = new DownloadDialog(this);
+ dialog.show();
+ break;
+ case R.id.pop_task:
+ DownloadPopupWindow pop = new DownloadPopupWindow(this);
+ //pop.showAsDropDown(mRootView);
+ pop.showAtLocation(mRootView, Gravity.CENTER_VERTICAL, 0, 0);
+ break;
}
}
}
\ No newline at end of file
diff --git a/app/src/main/java/com/arialyy/simple/dialog/DownloadDialog.java b/app/src/main/java/com/arialyy/simple/dialog/DownloadDialog.java
new file mode 100644
index 00000000..f8e723fd
--- /dev/null
+++ b/app/src/main/java/com/arialyy/simple/dialog/DownloadDialog.java
@@ -0,0 +1,120 @@
+package com.arialyy.simple.dialog;
+
+import android.content.Context;
+import android.os.Environment;
+import android.view.View;
+import android.widget.Button;
+import android.widget.TextView;
+import butterknife.Bind;
+import butterknife.OnClick;
+import com.arialyy.aria.core.AMTarget;
+import com.arialyy.aria.core.Aria;
+import com.arialyy.aria.core.DownloadEntity;
+import com.arialyy.aria.core.task.Task;
+import com.arialyy.aria.util.CommonUtil;
+import com.arialyy.frame.core.AbsDialog;
+import com.arialyy.simple.R;
+import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
+
+/**
+ * Created by AriaL on 2017/1/2.
+ */
+public class DownloadDialog extends AbsDialog {
+ @Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb;
+ @Bind(R.id.start) Button mStart;
+ @Bind(R.id.stop) Button mStop;
+ @Bind(R.id.cancel) Button mCancel;
+ @Bind(R.id.size) TextView mSize;
+ @Bind(R.id.speed) TextView mSpeed;
+
+ private static final String DOWNLOAD_URL =
+ "http://static.gaoshouyou.com/d/3a/93/573ae1db9493a801c24bf66128b11e39.apk";
+
+ public DownloadDialog(Context context) {
+ super(context);
+ init();
+ }
+
+ @Override protected int setLayoutId() {
+ return R.layout.dialog_download;
+ }
+
+ private void init() {
+ if (Aria.get(this).taskExists(DOWNLOAD_URL)) {
+ AMTarget target = Aria.whit(this).load(DOWNLOAD_URL);
+ int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize());
+ mPb.setProgress(p);
+ }
+ Aria.whit(this).addSchedulerListener(new MyDialogDownloadCallback());
+ DownloadEntity entity = Aria.get(this).getDownloadEntity(DOWNLOAD_URL);
+ if (entity != null) {
+ mSize.setText(CommonUtil.formatFileSize(entity.getFileSize()));
+ int state = entity.getState();
+ setBtState(state != DownloadEntity.STATE_DOWNLOAD_ING);
+ } else {
+ setBtState(true);
+ }
+ }
+
+ @OnClick({ R.id.start, R.id.stop, R.id.cancel }) public void onClick(View view) {
+ switch (view.getId()) {
+ case R.id.start:
+ Aria.whit(this)
+ .load(DOWNLOAD_URL)
+ .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/daialog.apk")
+ .setDownloadName("daialog.apk")
+ .start();
+ break;
+ case R.id.stop:
+ Aria.whit(this).load(DOWNLOAD_URL).stop();
+ break;
+ case R.id.cancel:
+ Aria.whit(this).load(DOWNLOAD_URL).cancel();
+ break;
+ }
+ }
+
+ @Override protected void dataCallback(int result, Object obj) {
+
+ }
+
+ private void setBtState(boolean startEnable) {
+ mStart.setEnabled(startEnable);
+ mCancel.setEnabled(!startEnable);
+ mStop.setEnabled(!startEnable);
+ }
+
+ private class MyDialogDownloadCallback extends Aria.SimpleSchedulerListener {
+
+ @Override public void onTaskPre(Task task) {
+ super.onTaskPre(task);
+ mSize.setText(CommonUtil.formatFileSize(task.getFileSize()));
+ setBtState(false);
+ }
+
+ @Override public void onTaskStop(Task task) {
+ super.onTaskStop(task);
+ setBtState(true);
+ mSpeed.setText("0.0kb/s");
+ }
+
+ @Override public void onTaskCancel(Task task) {
+ super.onTaskCancel(task);
+ setBtState(true);
+ mPb.setProgress(0);
+ mSpeed.setText("0.0kb/s");
+ }
+
+ @Override public void onTaskRunning(Task task) {
+ super.onTaskRunning(task);
+ long current = task.getCurrentProgress();
+ long len = task.getFileSize();
+ if (len == 0) {
+ mPb.setProgress(0);
+ } else {
+ mPb.setProgress((int) ((current * 100) / len));
+ }
+ mSpeed.setText(CommonUtil.formatFileSize(task.getSpeed()) + "/s");
+ }
+ }
+}
diff --git a/app/src/main/java/com/arialyy/simple/pop/DownloadPopupWindow.java b/app/src/main/java/com/arialyy/simple/pop/DownloadPopupWindow.java
new file mode 100644
index 00000000..20692903
--- /dev/null
+++ b/app/src/main/java/com/arialyy/simple/pop/DownloadPopupWindow.java
@@ -0,0 +1,124 @@
+package com.arialyy.simple.pop;
+
+import android.content.Context;
+import android.graphics.Color;
+import android.graphics.drawable.ClipDrawable;
+import android.graphics.drawable.ColorDrawable;
+import android.os.Environment;
+import android.view.View;
+import android.widget.Button;
+import android.widget.TextView;
+import butterknife.Bind;
+import butterknife.OnClick;
+import com.arialyy.aria.core.AMTarget;
+import com.arialyy.aria.core.Aria;
+import com.arialyy.aria.core.DownloadEntity;
+import com.arialyy.aria.core.task.Task;
+import com.arialyy.aria.util.CommonUtil;
+import com.arialyy.frame.core.AbsDialog;
+import com.arialyy.frame.core.AbsPopupWindow;
+import com.arialyy.simple.R;
+import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
+
+/**
+ * Created by AriaL on 2017/1/2.
+ */
+public class DownloadPopupWindow extends AbsPopupWindow {
+ @Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb;
+ @Bind(R.id.start) Button mStart;
+ @Bind(R.id.stop) Button mStop;
+ @Bind(R.id.cancel) Button mCancel;
+ @Bind(R.id.size) TextView mSize;
+ @Bind(R.id.speed) TextView mSpeed;
+
+ private static final String DOWNLOAD_URL =
+ "http://static.gaoshouyou.com/d/3a/93/573ae1db9493a801c24bf66128b11e39.apk";
+
+ public DownloadPopupWindow(Context context) {
+ super(context, new ColorDrawable(Color.WHITE));
+ initWidget();
+ }
+
+ @Override protected int setLayoutId() {
+ return R.layout.dialog_download;
+ }
+
+ private void initWidget() {
+ if (Aria.get(this).taskExists(DOWNLOAD_URL)) {
+ AMTarget target = Aria.whit(this).load(DOWNLOAD_URL);
+ int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize());
+ mPb.setProgress(p);
+ }
+ Aria.whit(this).addSchedulerListener(new MyDialogDownloadCallback());
+ DownloadEntity entity = Aria.get(this).getDownloadEntity(DOWNLOAD_URL);
+ if (entity != null) {
+ mSize.setText(CommonUtil.formatFileSize(entity.getFileSize()));
+ int state = entity.getState();
+ setBtState(state != DownloadEntity.STATE_DOWNLOAD_ING);
+ } else {
+ setBtState(true);
+ }
+ }
+
+ @OnClick({ R.id.start, R.id.stop, R.id.cancel }) public void onClick(View view) {
+ switch (view.getId()) {
+ case R.id.start:
+ Aria.whit(this)
+ .load(DOWNLOAD_URL)
+ .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/daialog.apk")
+ .setDownloadName("daialog.apk")
+ .start();
+ break;
+ case R.id.stop:
+ Aria.whit(this).load(DOWNLOAD_URL).stop();
+ break;
+ case R.id.cancel:
+ Aria.whit(this).load(DOWNLOAD_URL).cancel();
+ break;
+ }
+ }
+
+ @Override protected void dataCallback(int result, Object obj) {
+
+ }
+
+ private void setBtState(boolean startEnable) {
+ mStart.setEnabled(startEnable);
+ mCancel.setEnabled(!startEnable);
+ mStop.setEnabled(!startEnable);
+ }
+
+ private class MyDialogDownloadCallback extends Aria.SimpleSchedulerListener {
+
+ @Override public void onTaskPre(Task task) {
+ super.onTaskPre(task);
+ mSize.setText(CommonUtil.formatFileSize(task.getFileSize()));
+ setBtState(false);
+ }
+
+ @Override public void onTaskStop(Task task) {
+ super.onTaskStop(task);
+ setBtState(true);
+ mSpeed.setText("0.0kb/s");
+ }
+
+ @Override public void onTaskCancel(Task task) {
+ super.onTaskCancel(task);
+ setBtState(true);
+ mPb.setProgress(0);
+ mSpeed.setText("0.0kb/s");
+ }
+
+ @Override public void onTaskRunning(Task task) {
+ super.onTaskRunning(task);
+ long current = task.getCurrentProgress();
+ long len = task.getFileSize();
+ if (len == 0) {
+ mPb.setProgress(0);
+ } else {
+ mPb.setProgress((int) ((current * 100) / len));
+ }
+ mSpeed.setText(CommonUtil.formatFileSize(task.getSpeed()) + "/s");
+ }
+ }
+}
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index d3137e4e..87847e7a 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -3,9 +3,9 @@
>
@@ -29,5 +29,23 @@
style="?buttonBarButtonStyle"
/>
+
+
+
+
diff --git a/app/src/main/res/layout/dialog_download.xml b/app/src/main/res/layout/dialog_download.xml
new file mode 100644
index 00000000..42611568
--- /dev/null
+++ b/app/src/main/res/layout/dialog_download.xml
@@ -0,0 +1,79 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/img/dialog_use.gif b/img/dialog_use.gif
new file mode 100644
index 00000000..0fa006b4
Binary files /dev/null and b/img/dialog_use.gif differ
diff --git a/img/popupwindow_use.gif b/img/popupwindow_use.gif
new file mode 100644
index 00000000..af3df1ce
Binary files /dev/null and b/img/popupwindow_use.gif differ