pull/330/head
AriaLyy 8 years ago
commit 882d2aac1e
  1. 4
      Aria/build.gradle
  2. 11
      Aria/src/main/java/com/arialyy/aria/core/AMReceiver.java
  3. 14
      Aria/src/main/java/com/arialyy/aria/core/AMTarget.java
  4. 55
      Aria/src/main/java/com/arialyy/aria/core/Aria.java
  5. 93
      Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
  6. 4
      Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java
  7. 8
      Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java
  8. 10
      Aria/src/main/java/com/arialyy/aria/core/command/CmdFactory.java
  9. 12
      Aria/src/main/java/com/arialyy/aria/core/command/IDownloadCmd.java
  10. 6
      Aria/src/main/java/com/arialyy/aria/core/command/SingleCmd.java
  11. 6
      Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java
  12. 9
      Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java
  13. 20
      Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java
  14. 4
      Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java
  15. 51
      Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java
  16. 8
      Aria/src/main/java/com/arialyy/aria/core/scheduler/IDownloadSchedulers.java
  17. 2
      Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
  18. 6
      README.md
  19. 22
      app/src/main/java/com/arialyy/simple/activity/MainActivity.java
  20. 120
      app/src/main/java/com/arialyy/simple/dialog/DownloadDialog.java
  21. 124
      app/src/main/java/com/arialyy/simple/pop/DownloadPopupWindow.java
  22. 20
      app/src/main/res/layout/activity_main.xml
  23. 79
      app/src/main/res/layout/dialog_download.xml
  24. BIN
      img/dialog_use.gif
  25. BIN
      img/popupwindow_use.gif

@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 9
targetSdkVersion 23
versionCode 81
versionName "2.3.3"
versionCode 84
versionName "2.3.6"
}
buildTypes {
release {

@ -27,9 +27,10 @@ import com.arialyy.aria.util.CheckUtil;
* AM 接收器
*/
public class AMReceiver {
Object obj;
String targetName;
OnSchedulerListener listener;
DownloadEntity entity;
DownloadEntity entity;
Object obj;
/**
* {@link #load(String)}请使用该方法
@ -59,7 +60,7 @@ public class AMReceiver {
*/
public AMReceiver addSchedulerListener(OnSchedulerListener listener) {
this.listener = listener;
DownloadSchedulers.getInstance().addSchedulerListener(obj, listener);
DownloadSchedulers.getInstance().addSchedulerListener(targetName, listener);
return this;
}
@ -68,13 +69,13 @@ public class AMReceiver {
*/
public AMReceiver removeSchedulerListener() {
if (listener != null) {
DownloadSchedulers.getInstance().removeSchedulerListener(obj, listener);
DownloadSchedulers.getInstance().removeSchedulerListener(targetName, listener);
}
return this;
}
void destroy() {
obj = null;
targetName = null;
listener = null;
}
}

@ -89,7 +89,8 @@ public class AMTarget {
*/
public void add() {
DownloadManager.getInstance()
.setCmd(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_CREATE))
.setCmd(
CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_CREATE))
.exe();
}
@ -98,8 +99,8 @@ public class AMTarget {
*/
public void start() {
List<IDownloadCmd> 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();
}

@ -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启动管理全局任务
* <pre>
* <code>
* 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();
* </code>
* </pre>
*/
@ -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) {

@ -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<String> keys = mTargets.keySet();
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)) {
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;
}
}

@ -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, "添加命令执行失败,【该任务已经存在】");
}

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

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

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

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

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

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

@ -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) {

@ -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);
/**
* 通过下载链接从缓存池或任务池搜索下载任务如果缓存池或任务池都没有任务则创建新任务

@ -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<String, OnSchedulerListener> 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<Map.Entry<String, OnSchedulerListener>> iter =
mSchedulerListeners.entrySet().iterator(); iter.hasNext(); ) {
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 +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<String> keys = mSchedulerListeners.keySet();
for (String key : keys) {
callback(state, task, mSchedulerListeners.get(key));
}
}
}

@ -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);
/**
* 处理下载任务下载失败的情形

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

@ -3,6 +3,7 @@
下载不应该是让人感到痛苦的功能,Aria,致力于让下载傻瓜化。</br>
+ 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)</br>
```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
- 添加断点支持
- 修改下载逻辑,让使用更加简单

@ -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<ActivityMainBinding> {
@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<ActivityMainBinding> {
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<ActivityMainBinding> {
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;
}
}
}

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

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

@ -3,9 +3,9 @@
>
<LinearLayout
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
>
@ -29,5 +29,23 @@
style="?buttonBarButtonStyle"
/>
<Button
android:id="@+id/dialog_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="在dialog中使用"
style="?buttonBarButtonStyle"
/>
<Button
android:id="@+id/pop_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="在popupwindow中使用"
style="?buttonBarButtonStyle"
/>
</LinearLayout>
</layout>

@ -0,0 +1,79 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<com.arialyy.simple.widget.HorizontalProgressBarWithNumber
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_margin="16dp"
android:layout_toLeftOf="@+id/size"
android:max="100"
style="?android:attr/progressBarStyleHorizontal"
/>
<TextView
android:id="@+id/size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/progressBar"
android:layout_marginRight="16dp"
android:text="0mb"
android:textSize="16sp"
/>
<LinearLayout
android:id="@+id/handle_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBar"
android:orientation="horizontal"
>
<TextView
android:id="@+id/speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="0kb/s"
android:textColor="@color/black"
/>
<Button
android:id="@+id/start"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="开始"
style="?buttonBarButtonStyle"
/>
<Button
android:id="@+id/stop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="暂停"
style="?buttonBarButtonStyle"
/>
<Button
android:id="@+id/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="删除"
style="?buttonBarButtonStyle"
/>
</LinearLayout>
</RelativeLayout>

Binary file not shown.

After

Width:  |  Height:  |  Size: 301 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 231 KiB

Loading…
Cancel
Save