Merge branch 'v_2.0' of https://github.com/AriaLyy/Aria into v_2.0

pull/2/head
AriaLyy 8 years ago
commit 2e164100d1
  1. 1
      .gitignore
  2. 4
      Aria/build.gradle
  3. 3
      Aria/src/main/java/com/arialyy/aria/core/AMReceiver.java
  4. 18
      Aria/src/main/java/com/arialyy/aria/core/AMTarget.java
  5. 41
      Aria/src/main/java/com/arialyy/aria/core/Aria.java
  6. 152
      Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
  7. 2
      Aria/src/main/java/com/arialyy/aria/core/DownloadEntity.java
  8. 20
      Aria/src/main/java/com/arialyy/aria/core/DownloadManager.java
  9. 2
      Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java
  10. 7
      Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java
  11. 2
      Aria/src/main/java/com/arialyy/aria/core/command/CmdFactory.java
  12. 2
      Aria/src/main/java/com/arialyy/aria/core/command/IDownloadCmd.java
  13. 17
      Aria/src/main/java/com/arialyy/aria/core/command/SingleCmd.java
  14. 4
      Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java
  15. 7
      Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java
  16. 33
      Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java
  17. 2
      Aria/src/main/java/com/arialyy/aria/core/queue/IDownloader.java
  18. 2
      Aria/src/main/java/com/arialyy/aria/core/queue/IPool.java
  19. 2
      Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java
  20. 19
      Aria/src/main/java/com/arialyy/aria/core/queue/pool/CachePool.java
  21. 22
      Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java
  22. 22
      Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java
  23. 2
      Aria/src/main/java/com/arialyy/aria/core/scheduler/IDownloadSchedulers.java
  24. 15
      Aria/src/main/java/com/arialyy/aria/core/scheduler/OnSchedulerListener.java
  25. 2
      Aria/src/main/java/com/arialyy/aria/core/task/DownloadListener.java
  26. 6
      Aria/src/main/java/com/arialyy/aria/core/task/DownloadUtil.java
  27. 2
      Aria/src/main/java/com/arialyy/aria/core/task/IDownloadListener.java
  28. 2
      Aria/src/main/java/com/arialyy/aria/core/task/IDownloadUtil.java
  29. 11
      Aria/src/main/java/com/arialyy/aria/core/task/Task.java
  30. 2
      Aria/src/main/java/com/arialyy/aria/core/task/TaskFactory.java
  31. 11
      Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java
  32. 2
      Aria/src/main/java/com/arialyy/aria/orm/DbUtil.java
  33. 2
      Aria/src/main/java/com/arialyy/aria/orm/Id.java
  34. 2
      Aria/src/main/java/com/arialyy/aria/orm/Ignore.java
  35. 2
      Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java
  36. 2
      Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java
  37. 31
      Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
  38. 208
      Aria/src/main/java/com/arialyy/aria/util/Configuration.java
  39. 2
      Aria/src/main/java/com/arialyy/aria/util/PathUtil.java
  40. 58
      BroadCast.md
  41. 209
      README.md
  42. 2
      app/src/main/java/com/arialyy/simple/activity/MainActivity.java
  43. 13
      app/src/main/java/com/arialyy/simple/activity/MultiTaskActivity.java
  44. 22
      app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java
  45. 4
      app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java
  46. 2
      app/src/main/java/com/arialyy/simple/base/BaseActivity.java
  47. 4
      app/src/main/java/com/arialyy/simple/base/BaseApplication.java
  48. 2
      app/src/main/java/com/arialyy/simple/base/BaseDialog.java
  49. 2
      app/src/main/java/com/arialyy/simple/base/BaseModule.java
  50. 2
      app/src/main/java/com/arialyy/simple/dialog/DownloadNumDialog.java
  51. 8
      app/src/main/java/com/arialyy/simple/module/DownloadModule.java
  52. 2
      app/src/main/java/com/arialyy/simple/widget/HorizontalProgressBarWithNumber.java
  53. 15
      app/src/main/res/layout/activity_multi.xml
  54. BIN
      app/src/main/res/mipmap-hdpi/ic_launcher.png
  55. BIN
      app/src/main/res/mipmap-mdpi/ic_launcher.png
  56. BIN
      app/src/main/res/mipmap-xhdpi/ic_launcher.png
  57. BIN
      app/src/main/res/mipmap-xxhdpi/ic_launcher.png
  58. BIN
      app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
  59. 2
      app/src/main/res/values/strings.xml
  60. 21
      gradle.properties
  61. BIN
      img/download_img.gif
  62. BIN
      img/performance.png

1
.gitignore vendored

@ -10,3 +10,4 @@
.externalNativeBuild
.idea/misc.xml
.gradle
/.idea

@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 9
targetSdkVersion 23
versionCode 58
versionName "2.3.0"
versionCode 80
versionName "2.3.2"
}
buildTypes {
release {

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,6 +18,7 @@ package com.arialyy.aria.core;
import com.arialyy.aria.core.scheduler.OnSchedulerListener;
/**
* Created by lyy on 2016/12/5.
* AM 接收器
*/
public class AMReceiver {

@ -1,3 +1,18 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core;
import com.arialyy.aria.core.command.CmdFactory;
@ -9,7 +24,8 @@ import java.util.ArrayList;
import java.util.List;
/**
* Created by utstarcom on 2016/12/5.
* Created by lyy on 2016/12/5.
* https://github.com/AriaLyy/Aria
*/
public class AMTarget {
private AMReceiver receiver;

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,6 +19,8 @@ package com.arialyy.aria.core;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Application;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.app.Service;
import android.content.Context;
@ -26,15 +28,31 @@ import android.os.Build;
/**
* Created by lyy on 2016/12/1.
* https://github.com/AriaLyy/Aria
* Aria启动管理全局任务
* <pre>
* <code>
* DownloadEntity mEntity = new DownloadEntity();
* mEntity.setFileName(fileName); //设置文件名
* mEntity.setDownloadUrl(downloadUrl); //设置下载链接
* mEntity.setDownloadPath(downloadPath); //设置存放路径
*
* //启动下载
* Aria.whit(this).load(mEntity).start();
* </code>
* </pre>
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) public class Aria {
private Aria() {
}
/**
* 接受ActivityServiceApplication
*/
public static AMReceiver whit(Context context) {
if (context == null) throw new IllegalArgumentException("context 不能为 null");
//if (context == null) throw new IllegalArgumentException("context 不能为 null");
checkNull(context);
if (context instanceof Activity
|| context instanceof Service
|| context instanceof Application) {
@ -44,12 +62,31 @@ import android.os.Build;
}
}
/**
* 处理Fragment或者DialogFragment
*/
public static AMReceiver whit(Fragment fragment) {
checkNull(fragment);
return AriaManager.getInstance(
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? fragment.getContext()
: fragment.getActivity()).get(fragment);
}
/**
* 处理Dialog
*/
public static AMReceiver whit(Dialog dialog) {
checkNull(dialog);
return AriaManager.getInstance(dialog.getContext()).get(dialog);
}
private static void checkNull(Object obj) {
if (obj == null) throw new IllegalArgumentException("不能传入空对象");
}
/**
* 处理通用事件
*/
public static AriaManager get(Context context) {
if (context == null) throw new IllegalArgumentException("context 不能为 null");
if (context instanceof Activity

@ -1,15 +1,38 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Application;
import android.app.Dialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.command.CmdFactory;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.core.command.IDownloadCmd;
import com.arialyy.aria.util.Configuration;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -18,14 +41,16 @@ import java.util.Set;
/**
* Created by lyy on 2016/12/1.
* https://github.com/AriaLyy/Aria
* Aria管理器任务操作在这里执行
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public class AriaManager {
private static final Object LOCK = new Object();
private static volatile AriaManager INSTANCE = null;
private Map<String, AMReceiver> mTargets = new HashMap<>();
private static final String TAG = "AriaManager";
private static final Object LOCK = new Object();
private static volatile AriaManager INSTANCE = null;
private Map<String, AMReceiver> mTargets = new HashMap<>();
private DownloadManager mManager;
private LifeCallback mLifeCallback;
private LifeCallback mLifeCallback;
private AriaManager(Context context) {
regAppLifeCallback(context);
@ -46,13 +71,21 @@ import java.util.Set;
}
/**
* 设置同时下载的任务数
* 获取下载列表
*/
public List<DownloadEntity> getDownloadList() {
return DownloadEntity.findAllData(DownloadEntity.class);
}
/**
* 通过下载链接获取下载实体
*/
public void setDownloadNum(int num){
if (num <= 0){
throw new IllegalArgumentException("下载任务数不能小于1");
public DownloadEntity getDownloadEntity(String downloadUrl) {
if (TextUtils.isEmpty(downloadUrl)) {
throw new IllegalArgumentException("下载链接不能为null");
}
mManager.getTaskQueue().setDownloadNum(num);
return DownloadEntity.findData(DownloadEntity.class, new String[] { "downloadUrl" },
new String[] { downloadUrl });
}
/**
@ -60,7 +93,7 @@ import java.util.Set;
*/
public void stopAllTask() {
List<DownloadEntity> allEntity = mManager.getAllDownloadEntity();
List<IDownloadCmd> stopCmds = new ArrayList<>();
List<IDownloadCmd> stopCmds = new ArrayList<>();
for (DownloadEntity entity : allEntity) {
if (entity.getState() == DownloadEntity.STATE_DOWNLOAD_ING) {
stopCmds.add(CommonUtil.createCmd(entity, CmdFactory.TASK_STOP));
@ -69,12 +102,58 @@ import java.util.Set;
mManager.setCmds(stopCmds).exe();
}
/**
* 设置下载超时时间
*/
@Deprecated private AriaManager setTimeOut(int timeOut) {
Configuration.getInstance().setTimeOut(timeOut);
return this;
}
/**
* 设置下载失败重试次数
*/
public AriaManager setReTryNum(int reTryNum) {
Configuration.getInstance().setReTryNum(reTryNum);
return this;
}
/**
* 设置下载失败重试间隔
*/
public AriaManager setReTryInterval(int interval) {
Configuration.getInstance().setReTryInterval(interval);
return this;
}
/**
* 是否打开下载广播
*/
public AriaManager openBroadcast(boolean open) {
Configuration.getInstance().setOpenBroadcast(open);
return this;
}
/**
* 设置最大下载数最大下载数不能小于1
*
* @param maxDownloadNum 最大下载数
*/
public AriaManager setMaxDownloadNum(int maxDownloadNum) {
if (maxDownloadNum < 1) {
Log.w(TAG, "最大任务数不能小于 1");
return this;
}
mManager.getTaskQueue().setDownloadNum(maxDownloadNum);
return this;
}
/**
* 删除所有任务
*/
public void cancelAllTask() {
List<DownloadEntity> allEntity = mManager.getAllDownloadEntity();
List<IDownloadCmd> cancelCmds = new ArrayList<>();
List<DownloadEntity> allEntity = mManager.getAllDownloadEntity();
List<IDownloadCmd> cancelCmds = new ArrayList<>();
for (DownloadEntity entity : allEntity) {
cancelCmds.add(CommonUtil.createCmd(entity, CmdFactory.TASK_CANCEL));
}
@ -88,16 +167,56 @@ import java.util.Set;
}
private AMReceiver putTarget(Object obj) {
String clsName = obj.getClass().getName();
AMReceiver target = mTargets.get(clsName);
String clsName = obj.getClass().getName();
AMReceiver target = mTargets.get(clsName);
if (target == null) {
target = new AMReceiver();
target.obj = obj;
mTargets.put(clsName, target);
String key = "";
if (obj instanceof android.support.v4.app.Fragment) {
key = "_" + ((Fragment) obj).getActivity().getClass().getName();
} else if (obj instanceof android.app.Fragment) {
key = "_" + ((android.app.Fragment) obj).getActivity().getClass().getName();
} else if (obj instanceof Dialog) {
Activity activity = ((Dialog) obj).getOwnerActivity();
if (activity != null) {
key = "_" + activity.getClass().getName();
}
handleDialogDialogLift((Dialog) obj);
}
if (!TextUtils.isEmpty(key)) {
mTargets.put(clsName, target);
}
}
return target;
}
/**
* 处理对话框取消或dismiss
*/
private void handleDialogDialogLift(Dialog dialog) {
try {
Field dismissField = CommonUtil.getField(dialog.getClass(), "mDismissMessage");
Message dismissMsg = (Message) dismissField.get(dialog);
//如果Dialog已经设置Dismiss事件,则查找cancel事件
if (dismissMsg != null) {
Field cancelField = CommonUtil.getField(dialog.getClass(), "mCancelMessage");
Message cancelMsg = (Message) dismissField.get(dialog);
if (cancelMsg != null) {
Log.e(TAG, "你已经对Dialog设置了Dismiss和cancel事件。为了防止内存泄露,"
+ "请在dismiss方法中调用Aria.whit(this).removeSchedulerListener();来注销事件");
} else {
}
} else {
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
private AMReceiver getTarget(Object obj) {
AMReceiver target = mTargets.get(obj.getClass().getName());
if (target == null) {
@ -149,7 +268,8 @@ import java.util.Set;
@Override public void onActivityDestroyed(Activity activity) {
Set<String> keys = mTargets.keySet();
for (String key : keys) {
if (key.equals(activity.getClass().getName())) {
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;

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -22,6 +22,7 @@ import com.arialyy.aria.orm.DbUtil;
import com.arialyy.aria.core.command.IDownloadCmd;
import com.arialyy.aria.core.queue.DownloadTaskQueue;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.Configuration;
import java.util.ArrayList;
import java.util.List;
@ -82,21 +83,22 @@ public class DownloadManager {
private static final Object LOCK = new Object();
private static volatile DownloadManager INSTANCE = null;
private List<IDownloadCmd> mCommands = new ArrayList<>();
private Context mContext;
private ITaskQueue mTaskQueue;
public static Context APP;
private ITaskQueue mTaskQueue;
private static Configuration mConfig;
private DownloadManager() {
}
private DownloadManager(Context context) {
mContext = context;
APP = context;
DownloadTaskQueue.Builder builder = new DownloadTaskQueue.Builder(context);
mTaskQueue = builder.build();
DbUtil.init(context);
}
public static DownloadManager init(Context context) {
static DownloadManager init(Context context) {
if (INSTANCE == null) {
synchronized (LOCK) {
INSTANCE = new DownloadManager(context.getApplicationContext());
@ -112,7 +114,7 @@ public class DownloadManager {
return INSTANCE;
}
public List<DownloadEntity> getAllDownloadEntity() {
List<DownloadEntity> getAllDownloadEntity() {
return DbEntity.findAllData(DownloadEntity.class);
}
@ -126,7 +128,7 @@ public class DownloadManager {
/**
* 设置命令
*/
public DownloadManager setCmd(IDownloadCmd command) {
DownloadManager setCmd(IDownloadCmd command) {
mCommands.add(command);
return this;
}
@ -134,7 +136,7 @@ public class DownloadManager {
/**
* 设置一组命令
*/
public DownloadManager setCmds(List<IDownloadCmd> commands) {
DownloadManager setCmds(List<IDownloadCmd> commands) {
if (commands != null && commands.size() > 0) {
mCommands.addAll(commands);
}
@ -144,7 +146,7 @@ public class DownloadManager {
/**
* 执行所有设置的命令
*/
public synchronized void exe() {
synchronized void exe() {
for (IDownloadCmd command : mCommands) {
command.executeCmd();
}

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -14,7 +14,6 @@
* limitations under the License.
*/
package com.arialyy.aria.core.command;
import com.arialyy.aria.core.DownloadEntity;
@ -40,7 +39,9 @@ class CancelCmd extends IDownloadCmd {
task = mQueue.createTask(mTarget, mEntity);
}
if (task != null) {
task.setmTargetName(mTarget.getClass().getName());
if (mTarget != null) {
task.setTargetName(mTarget.getClass().getName());
}
mQueue.cancelTask(task);
}
}

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -1,3 +1,18 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.command;
import android.util.Log;
@ -27,7 +42,7 @@ class SingleCmd extends IDownloadCmd {
} else {
Log.w(TAG, "添加命令执行失败,【该任务已经存在】");
}
task.setmTargetName(mTarget.getClass().getName());
task.setTargetName(mTarget.getClass().getName());
mQueue.startTask(task);
}
}

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -39,7 +39,7 @@ class StartCmd extends IDownloadCmd {
task = mQueue.createTask(mTarget, mEntity);
}
if (task != null) {
task.setmTargetName(mTarget.getClass().getName());
task.setTargetName(mTarget.getClass().getName());
mQueue.startTask(task);
}
}

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -14,7 +14,6 @@
* limitations under the License.
*/
package com.arialyy.aria.core.command;
import android.util.Log;
@ -48,7 +47,9 @@ class StopCmd extends IDownloadCmd {
Log.w(TAG, "停止命令执行失败,【调度器中没有该任务】");
}
} else {
task.setmTargetName(mTarget.getClass().getName());
if (mTarget != null) {
task.setTargetName(mTarget.getClass().getName());
}
mQueue.stopTask(task);
}
}

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -25,6 +25,7 @@ import com.arialyy.aria.core.scheduler.DownloadSchedulers;
import com.arialyy.aria.core.scheduler.IDownloadSchedulers;
import com.arialyy.aria.core.task.Task;
import com.arialyy.aria.core.task.TaskFactory;
import com.arialyy.aria.util.Configuration;
/**
* Created by lyy on 2016/8/17.
@ -36,7 +37,6 @@ public class DownloadTaskQueue implements ITaskQueue {
private ExecutePool mExecutePool = ExecutePool.getInstance();
private Context mContext;
private IDownloadSchedulers mSchedulers;
private int mDownloadNum = 2;
private DownloadTaskQueue() {
}
@ -107,6 +107,10 @@ public class DownloadTaskQueue implements ITaskQueue {
}
@Override public void reTryStart(Task task) {
if (task == null) {
Log.w(TAG, "重试下载失败,task 为null");
return;
}
if (!task.isDownloading()) {
task.start();
} else {
@ -123,7 +127,32 @@ public class DownloadTaskQueue implements ITaskQueue {
}
@Override public void setDownloadNum(int downloadNum) {
//原始长度
int size = Configuration.getInstance().getDownloadNum();
int diff = downloadNum - size;
if (size == downloadNum){
Log.d(TAG, "设置的下载任务数和配置文件的下载任务数一直,跳过");
return;
}
//设置的任务数小于配置任务数
if (diff <= -1 && mExecutePool.size() >= size) {
for (int i = 0, len = Math.abs(diff); i < len; i++) {
Task eTask = mExecutePool.pollTask();
if (eTask != null) {
stopTask(eTask);
}
}
}
mExecutePool.setDownloadNum(downloadNum);
if (diff >= 1) {
for (int i = 0; i < diff; i++) {
Task nextTask = getNextTask();
if (nextTask != null
&& nextTask.getDownloadEntity().getState() == DownloadEntity.STATE_WAIT) {
startTask(nextTask);
}
}
}
}
@Override public Task createTask(Object target, DownloadEntity entity) {

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -14,7 +14,6 @@
* limitations under the License.
*/
package com.arialyy.aria.core.queue.pool;
import android.text.TextUtils;
@ -25,6 +24,7 @@ import com.arialyy.aria.core.task.Task;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
/**
* Created by lyy on 2016/8/14.
@ -35,6 +35,7 @@ public class CachePool implements IPool {
private static final Object LOCK = new Object();
private static final int MAX_NUM = Integer.MAX_VALUE; //最大下载任务数
private static volatile CachePool INSTANCE = null;
private static final long TIME_OUT = 1000;
private Map<String, Task> mCacheArray;
private LinkedBlockingQueue<Task> mCacheQueue;
@ -75,13 +76,19 @@ public class CachePool implements IPool {
@Override public Task pollTask() {
synchronized (LOCK) {
Task task = mCacheQueue.poll();
if (task != null) {
String url = task.getDownloadEntity().getDownloadUrl();
mCacheArray.remove(CommonUtil.keyToHashKey(url));
try {
Task task = null;
task = mCacheQueue.poll(TIME_OUT, TimeUnit.MICROSECONDS);
if (task != null) {
String url = task.getDownloadEntity().getDownloadUrl();
mCacheArray.remove(CommonUtil.keyToHashKey(url));
}
return task;
} catch (InterruptedException e) {
e.printStackTrace();
}
return task;
}
return null;
}
@Override public Task getTask(String downloadUrl) {

@ -14,7 +14,6 @@
* limitations under the License.
*/
package com.arialyy.aria.core.queue.pool;
import android.text.TextUtils;
@ -22,6 +21,7 @@ import android.util.Log;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.core.queue.IPool;
import com.arialyy.aria.core.task.Task;
import com.arialyy.aria.util.Configuration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
@ -36,11 +36,12 @@ public class ExecutePool implements IPool {
private static final Object LOCK = new Object();
private static final long TIME_OUT = 1000;
private static volatile ExecutePool INSTANCE = null;
public static int mSize = 2;
private ArrayBlockingQueue<Task> mExecuteQueue;
private Map<String, Task> mExecuteArray;
private int mSize;
private ExecutePool() {
mSize = Configuration.getInstance().getDownloadNum();
mExecuteQueue = new ArrayBlockingQueue<>(mSize);
mExecuteArray = new HashMap<>();
}
@ -91,6 +92,7 @@ public class ExecutePool implements IPool {
}
mExecuteQueue = temp;
mSize = downloadNum;
Configuration.getInstance().setDownloadNum(mSize);
} catch (InterruptedException e) {
e.printStackTrace();
}
@ -135,12 +137,18 @@ public class ExecutePool implements IPool {
@Override public Task pollTask() {
synchronized (LOCK) {
Task task = mExecuteQueue.poll();
if (task != null) {
String url = task.getDownloadEntity().getDownloadUrl();
mExecuteArray.remove(CommonUtil.keyToHashKey(url));
try {
Task task = null;
task = mExecuteQueue.poll(TIME_OUT, TimeUnit.MICROSECONDS);
if (task != null) {
String url = task.getDownloadEntity().getDownloadUrl();
mExecuteArray.remove(CommonUtil.keyToHashKey(url));
}
return task;
} catch (InterruptedException e) {
e.printStackTrace();
}
return task;
return null;
}
}

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -24,6 +24,7 @@ import com.arialyy.aria.core.DownloadEntity;
import com.arialyy.aria.core.task.Task;
import com.arialyy.aria.core.queue.pool.ExecutePool;
import com.arialyy.aria.core.queue.DownloadTaskQueue;
import com.arialyy.aria.util.Configuration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@ -67,10 +68,6 @@ public class DownloadSchedulers implements IDownloadSchedulers {
private static final String TAG = "DownloadSchedulers";
private static final Object LOCK = new Object();
private static volatile DownloadSchedulers INSTANCE = null;
/**
* 下载失败次数
*/
int mFailNum = 10;
/**
* 超时时间
@ -108,7 +105,7 @@ public class DownloadSchedulers implements IDownloadSchedulers {
case STOP:
case CANCEL:
mQueue.removeTask(entity);
if (mQueue.size() != ExecutePool.mSize) {
if (mQueue.size() != Configuration.getInstance().getDownloadNum()) {
startNextTask(entity);
}
break;
@ -178,11 +175,12 @@ public class DownloadSchedulers implements IDownloadSchedulers {
* @param entity 失败实体
*/
@Override public void handleFailTask(DownloadEntity entity) {
if (entity.getFailNum() <= mFailNum) {
final Configuration config = Configuration.getInstance();
if (entity.getFailNum() <= config.getReTryNum()) {
Task task = mQueue.getTask(entity);
mQueue.reTryStart(task);
try {
Thread.currentThread().sleep(4000);
Thread.currentThread().sleep(config.getReTryInterval());
} catch (InterruptedException e) {
e.printStackTrace();
}
@ -227,12 +225,4 @@ public class DownloadSchedulers implements IDownloadSchedulers {
}
mSchedulerListeners.remove(target.getClass().getName());
}
public void setFailNum(int mFailNum) {
this.mFailNum = mFailNum;
}
public void setTimeOut(long timeOut) {
this.mTimeOut = timeOut;
}
}

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -1,3 +1,18 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.scheduler;
import com.arialyy.aria.core.task.Task;

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -75,7 +75,7 @@ final class DownloadUtil implements IDownloadUtil {
mDownloadEntity = entity;
mListener = downloadListener;
THREAD_NUM = threadNum;
mFixedThreadPool = Executors.newFixedThreadPool(THREAD_NUM);
mFixedThreadPool = Executors.newFixedThreadPool(Integer.MAX_VALUE);
}
public IDownloadListener getListener() {
@ -292,7 +292,7 @@ final class DownloadUtil implements IDownloadUtil {
for (int l : recordL) {
if (l == -1) continue;
Runnable task = mTask.get(l);
if (task != null) {
if (task != null && !mFixedThreadPool.isShutdown()) {
mFixedThreadPool.execute(task);
}
}

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -25,6 +25,7 @@ import com.arialyy.aria.core.DownloadManager;
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
import com.arialyy.aria.core.scheduler.IDownloadSchedulers;
import com.arialyy.aria.core.DownloadEntity;
import com.arialyy.aria.util.Configuration;
/**
* Created by lyy on 2016/8/11.
@ -75,7 +76,7 @@ public class Task {
return mTargetName;
}
public void setmTargetName(String targetName) {
public void setTargetName(String targetName) {
this.mTargetName = targetName;
}
@ -205,7 +206,7 @@ public class Task {
public Task build() {
Task task = new Task(context, downloadEntity);
task.mOutHandler = outHandler;
task.setmTargetName(targetName);
task.setTargetName(targetName);
downloadEntity.save();
return task;
}
@ -325,7 +326,9 @@ public class Task {
if (location != -1) {
intent.putExtra(DownloadManager.CURRENT_LOCATION, location);
}
//context.sendBroadcast(intent);
if (Configuration.isOpenBreadCast) {
context.sendBroadcast(intent);
}
}
}
}

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -14,7 +14,6 @@
* limitations under the License.
*/
package com.arialyy.aria.orm;
import android.support.annotation.NonNull;
@ -46,6 +45,14 @@ public class DbEntity {
return util.findAllData(clazz);
}
/**
* 查询第一条数据
*/
public static <T extends DbEntity> T findFirst(Class<T> clazz) {
List<T> list = findAllData(clazz);
return (list == null || list.size() == 0) ? null : list.get(0);
}
/**
* 查询一组数据
*

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,6 +16,8 @@
package com.arialyy.aria.util;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import com.arialyy.aria.core.command.CmdFactory;
import com.arialyy.aria.core.DownloadEntity;
@ -44,6 +46,33 @@ public class CommonUtil {
return CmdFactory.getInstance().createCmd(entity, cmd);
}
/**
* 存储字符串到配置文件
*
* @param preName 配置文件名
* @param key 存储的键值
* @param value 需要存储的字符串
* @return 成功标志
*/
public static Boolean putString(String preName, Context context, String key, String value) {
SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pre.edit();
editor.putString(key, value);
return editor.commit();
}
/**
* 从配置文件读取字符串
*
* @param preName 配置文件名
* @param key 字符串键值
* @return 键值对应的字符串, 默认返回""
*/
public static String getString(String preName, Context context, String key) {
SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
return pre.getString(key, "");
}
/**
* 获取类里面的所在字段
*/

@ -0,0 +1,208 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.util;
import android.util.Log;
import com.arialyy.aria.core.DownloadManager;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.WeakHashMap;
/**
* Created by AriaL on 2016/12/8.
* 信息配置
*/
public class Configuration {
private static final String TAG = "Configuration";
private static final String CONFIG_FILE = "/Aria/ADConfig.properties";
/**
* 当前调度器最大下载数默认最大下载数为 2
*/
private static final String DOWNLOAD_NUM = "DOWNLOAD_NUM";
/**
* 失败重试次数默认最多重试 10
*/
private static final String RE_TRY_NUM = "RE_TRY_NUM";
/**
* 是否打开下载广播默认 false
*/
private static final String OPEN_BROADCAST = "OPEN_BROADCAST";
/**
* 失败重试间隔时间默认 4000 ms
*/
private static final String RE_TRY_INTERVAL = "RE_TRY_INTERVAL";
/**
* 超时时间默认 10000 ms
*/
private static final String DOWNLOAD_TIME_OUT = "DOWNLOAD_TIME_OUT";
public static boolean isOpenBreadCast = false;
private static Configuration INSTANCE = null;
private File mConfigFile = null;
private static final Object LOCK = new Object();
public static Configuration getInstance() {
if (INSTANCE == null) {
synchronized (LOCK) {
INSTANCE = new Configuration();
}
}
return INSTANCE;
}
private Configuration() {
mConfigFile = new File(DownloadManager.APP.getFilesDir().getPath() + CONFIG_FILE);
try {
if (!mConfigFile.exists()) {
mConfigFile.getParentFile().mkdirs();
mConfigFile.createNewFile();
init();
}else {
isOpenBreadCast = isOpenBroadcast();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void init() {
Map<String, String> config = new WeakHashMap<>();
config.put(DOWNLOAD_NUM, 2 + "");
config.put(RE_TRY_NUM, 10 + "");
config.put(OPEN_BROADCAST, false + "");
config.put(RE_TRY_INTERVAL, 4000 + "");
config.put(DOWNLOAD_TIME_OUT, 10000 + "");
saveConfig(config);
}
private void saveConfig(Map<String, String> config) {
if (config == null || config.size() == 0) {
return;
}
Properties properties = CommonUtil.loadConfig(mConfigFile);
Set<String> keys = config.keySet();
for (String key : keys) {
properties.setProperty(key, config.get(key));
}
CommonUtil.saveConfig(mConfigFile, properties);
}
private void save(String key, String value) {
Map<String, String> map = new WeakHashMap<>();
map.put(key, value);
saveConfig(map);
}
/**
* 获取下载超时时间
*
* @return 默认4000ms
*/
public int getTimeOut() {
return Integer.parseInt(CommonUtil.loadConfig(mConfigFile).getProperty(DOWNLOAD_TIME_OUT));
}
/**
* 设置重试间隔
*/
public void setTimeOut(int timeOut) {
if (timeOut < 10000) {
Log.w(TAG, "下载超时时间不能小于 10000 ms");
return;
}
save(DOWNLOAD_TIME_OUT, timeOut + "");
}
/**
* 获取失败重试间隔时间
*
* @return 默认4000ms
*/
public int getReTryInterval() {
return Integer.parseInt(CommonUtil.loadConfig(mConfigFile).getProperty(RE_TRY_INTERVAL));
}
/**
* 设置重试间隔
*/
public void setReTryInterval(int reTryInterval) {
if (reTryInterval < 4000) {
Log.w(TAG, "重试间隔不能小于4000ms");
return;
}
save(RE_TRY_INTERVAL, reTryInterval + "");
}
/**
* 获取最大下载数
*
* @return 默认返回2
*/
public int getDownloadNum() {
return Integer.parseInt(CommonUtil.loadConfig(mConfigFile).getProperty(DOWNLOAD_NUM));
}
/**
* 设置最大下载数
*/
public void setDownloadNum(int downloadNum) {
if (downloadNum < 1) {
Log.w(TAG, "最大下载数不能小于1");
return;
}
save(DOWNLOAD_NUM, downloadNum + "");
}
/**
* 获取最大重试数
*
* @return 默认返回 10
*/
public int getReTryNum() {
return Integer.parseInt(CommonUtil.loadConfig(mConfigFile).getProperty(RE_TRY_NUM));
}
/**
* 设置重试数
*/
public void setReTryNum(int reTryNum) {
if (reTryNum < 1) {
Log.w(TAG, "最大下载数不能小于1");
return;
}
save(RE_TRY_NUM, reTryNum + "");
}
/**
* 是否打开下载广播
*
* @return 默认false
*/
public boolean isOpenBroadcast() {
return Boolean.parseBoolean(CommonUtil.loadConfig(mConfigFile).getProperty(RE_TRY_NUM));
}
/**
* 设置是否打开下载广播
*/
public void setOpenBroadcast(boolean openBroadcast) {
isOpenBreadCast = openBroadcast;
save(OPEN_BROADCAST, openBroadcast + "");
}
}

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -0,0 +1,58 @@
# 广播使用
#### 一、创建广播接收器,用来接收下载的各种状态
```java
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
long len = 0;
@Override public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//可以通过intent获取到下载实体,下载实体中包含了各种下载状态
DownloadEntity entity = intent.getParcelableExtra(DownloadManager.ENTITY);
switch (action) {
case DownloadManager.ACTION_PRE: //预处理
break;
case DownloadManager.ACTION_POST_PRE: //预处理完成
//预处理完成,便可以获取文件的下载长度了
len = entity.getFileSize();
break;
case DownloadManager.ACTION_START: //开始下载
L.d(TAG, "download start");
break;
case DownloadManager.ACTION_RESUME: //恢复下载
L.d(TAG, "download resume");
long location = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 1);
break;
case DownloadManager.ACTION_RUNNING: //下载中
long current = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 0);
break;
case DownloadManager.ACTION_STOP: //停止下载
L.d(TAG, "download stop");
break;
case DownloadManager.ACTION_COMPLETE: //下载完成
break;
case DownloadManager.ACTION_CANCEL: //取消下载
break;
case DownloadManager.ACTION_FAIL: // 下载失败
break;
}
}
};
```
#### 二、在Activity中创建广播过滤器
```java
@Override protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addDataScheme(getPackageName());
filter.addAction(DownloadManager.ACTION_PRE);
filter.addAction(DownloadManager.ACTION_POST_PRE);
filter.addAction(DownloadManager.ACTION_RESUME);
filter.addAction(DownloadManager.ACTION_START);
filter.addAction(DownloadManager.ACTION_RUNNING);
filter.addAction(DownloadManager.ACTION_STOP);
filter.addAction(DownloadManager.ACTION_CANCEL);
filter.addAction(DownloadManager.ACTION_COMPLETE);
filter.addAction(DownloadManager.ACTION_FAIL);
registerReceiver(mReceiver, filter);
}
```

@ -1,152 +1,107 @@
# DownloadUtil
# Aria
![图标](https://github.com/AriaLyy/DownloadUtil/blob/v_2.0/app/src/main/res/mipmap-hdpi/ic_launcher.png)</br>
这是一个 android 智能切换多任务断点续传工具,使用该工具,你可以很容易实现`多线程下载功能和复杂的任务自动切换功能`</br>
+ 该工具具有以下特点:
- 通过命令控制下载
- 可在广播中接收任务的各种下载状态
Aria,致力于让下载傻瓜化</br>
+ Aria有以下特点:
- 简单
- 可自定义是否使用广播
- 支持多线程、多任务下载
- 支持任务自动切换
- 支持下载速度直接获取
如果你觉得我的代码对你有帮助,您的star和issues将是对我最大支持.`^_^`
[Aria怎样使用?](#使用)
#下载
[![Download](https://api.bintray.com/packages/arialyy/maven/MTDownloadUtil/images/download.svg)](https://bintray.com/arialyy/maven/MTDownloadUtil/_latestVersion)<br/>
compile 'com.arialyy.downloadutil:DownloadUtil:2.1.1'
如果你觉得Aria对你有帮助,您的star和issues将是对我最大支持.`^_^`
## 下载
[![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.2'
```
#示例
## 示例
![多任务下载](https://github.com/AriaLyy/DownloadUtil/blob/v_2.0/img/download_img.gif)
![单任务下载](https://github.com/AriaLyy/DownloadUtil/blob/master/img/11.gif "")
# 性能展示
## 性能展示
![性能展示](https://github.com/AriaLyy/DownloadUtil/blob/v_2.0/img/performance.png)
# 使用
* 一、在Application注册下载器
***
## 使用
### 一、Aria 是实体驱动型的工具,所以,第一步,你需要创建一个下载实体
```java
public class BaseApplication extends Application {
@Override public void onCreate() {
super.onCreate();
//注册下载器
DownloadManager.init(this);
}
}
DownloadEntity mEntity = new DownloadEntity();
mEntity.setFileName(fileName); //设置文件名
mEntity.setDownloadUrl(downloadUrl); //设置下载链接
mEntity.setDownloadPath(downloadPath); //设置存放路径
```
* 二、创建广播接收器,用来接收下载的各种状态
```java
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
long len = 0;
@Override public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//可以通过intent获取到下载实体,下载实体中包含了各种下载状态
DownloadEntity entity = intent.getParcelableExtra(DownloadManager.ENTITY);
switch (action) {
case DownloadManager.ACTION_PRE: //预处理
break;
case DownloadManager.ACTION_POST_PRE: //预处理完成
//预处理完成,便可以获取文件的下载长度了
len = entity.getFileSize();
break;
case DownloadManager.ACTION_START: //开始下载
L.d(TAG, "download start");
break;
case DownloadManager.ACTION_RESUME: //恢复下载
L.d(TAG, "download resume");
long location = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 1);
break;
case DownloadManager.ACTION_RUNNING: //下载中
long current = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 0);
break;
case DownloadManager.ACTION_STOP: //停止下载
L.d(TAG, "download stop");
break;
case DownloadManager.ACTION_COMPLETE: //下载完成
break;
case DownloadManager.ACTION_CANCEL: //取消下载
break;
case DownloadManager.ACTION_FAIL: // 下载失败
break;
}
}
};
```
* 三、在Activity中创建广播过滤器
### 二、为了能接收到Aria传递的数据,你需要把你的Activity或fragment注册到Aria管理器中,注册的方式很简单,在onResume
```java
@Override protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addDataScheme(getPackageName());
filter.addAction(DownloadManager.ACTION_PRE);
filter.addAction(DownloadManager.ACTION_POST_PRE);
filter.addAction(DownloadManager.ACTION_RESUME);
filter.addAction(DownloadManager.ACTION_START);
filter.addAction(DownloadManager.ACTION_RUNNING);
filter.addAction(DownloadManager.ACTION_STOP);
filter.addAction(DownloadManager.ACTION_CANCEL);
filter.addAction(DownloadManager.ACTION_COMPLETE);
filter.addAction(DownloadManager.ACTION_FAIL);
registerReceiver(mReceiver, filter);
}
```
* 四、创建下载实体
```java
DownloadEntity entity = new DownloadEntity();
entity.setFileName(fileName); //设置文件名
entity.setDownloadUrl(downloadUrl); //设置下载链接
entity.setDownloadPath(downloadPath); //设置存放路径
```
* 五、通过命令控制下载(下载状态控制,或下载任务切换将自动完成)</br>
**!!注意:命令需要第四步的下载实体支持**
- 获取命令工厂实例和下载管理器实例
```java
CmdFactory factory = CmdFactory.getInstance();
DownloadManager manager = DownloadManager.getInstance();
```
- 开始命令、恢复下载命令都是同一个
```java
private void start() {
List<IDownloadCmd> commands = new ArrayList<>();
IDownloadCmd addCMD = factory.createCmd(this, entity, CmdFactory.TASK_CREATE);
IDownloadCmd startCmd = factory.createCmd(this, entity, CmdFactory.TASK_START);
commands.add(addCMD);
commands.add(startCmd);
manager.setCmds(commands).exe();
}
```
- 停止命令
```java
private void stop() {
IDownloadCmd stopCmd = factory.createCmd(this, entity, CmdFactory.TASK_STOP);
manager.setCmd(stopCmd).exe();
}
```
- 取消命令(取消下载、删除下载任务)
```java
private void cancel() {
IDownloadCmd cancelCmd = factory.createCmd(this, entity, CmdFactory.TASK_CANCEL);
manager.setCmd(cancelCmd).exe();
}
```
# 修改最大任务数
```
mManager.getTaskQueue().setDownloadNum(num);
super.onResume();
Aria.whit(this).addSchedulerListener(new MySchedulerListener());
}
```
# 开发日志
### 三、还记得上面的DownloadEntity吗?现在是时候使用它进行下载了
- 启动下载
```java
Aria.whit(this).load(mEntity).start();
```
- 暂停下载
```java
Aria.whit(this).load(mEntity).stop();
```
- 恢复下载
```java
Aria.whit(this).load(mEntity).resume();
```
- 取消下载
```java
Aria.whit(this).load(mEntity).cancel();
```
### 四、关于Aria,你还需要知道的一些东西
- 设置下载任务数,Aria默认下载任务为**2**
```java
Aria.get(getContext()).setMaxDownloadNum(num);
```
- 停止所有下载
```java
Aria.get(this).stopAllTask();
```
- 设置失败重试次数,从事次数不能少于 1
```java
Aria.get(this).setReTryNum(10);
```
- 设置失败重试间隔,重试间隔不能小于 5000ms
```java
Aria.get(this).setReTryInterval(5000);
```
- 设置是否打开广播,如果你需要在Service后台获取下载完成情况,那么你需要打开Aria广播,[Aria广播配置](https://github.com/AriaLyy/Aria/blob/v_2.0/BroadCast.md)
```java
Aria.get(this).openBroadcast(true);
```
***
## 开发日志
+ v_2.1.0 修复大量bug
+ v_2.1.1 增加,选择最大下载任务数接口
+ v_2.3.1 重命名为Aria,下载流程简化
License
-------
Copyright 2016 AriaLyy(DownloadUtil)
Copyright 2016 AriaLyy(https://github.com/AriaLyy/Aria)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@ -158,4 +113,4 @@ License
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -104,8 +104,15 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
//};
public void onClick(View view){
DownloadNumDialog dialog = new DownloadNumDialog(this);
dialog.show(getSupportFragmentManager(), "download_num");
switch (view.getId()){
case R.id.num:
DownloadNumDialog dialog = new DownloadNumDialog(this);
dialog.show(getSupportFragmentManager(), "download_num");
break;
case R.id.stop_all:
Aria.get(this).stopAllTask();
break;
}
}
@Override protected void onResume() {

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,6 +17,8 @@
package com.arialyy.simple.activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
@ -30,12 +32,15 @@ import butterknife.Bind;
import com.arialyy.aria.core.AMTarget;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.DownloadEntity;
import com.arialyy.aria.core.DownloadManager;
import com.arialyy.aria.core.task.Task;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.frame.util.show.L;
import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.ActivitySingleBinding;
import com.arialyy.simple.module.DownloadModule;
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
@ -56,7 +61,14 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
@Bind(R.id.toolbar) Toolbar toolbar;
@Bind(R.id.speed) TextView mSpeed;
private DownloadEntity mEntity;
private BroadcastReceiver mReceiver;
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(DownloadManager.ACTION_START)) {
L.d("START");
}
}
};
private Handler mUpdateHandler = new Handler() {
@Override public void handleMessage(Message msg) {
@ -126,10 +138,12 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
@Override protected void onResume() {
super.onResume();
Aria.whit(this).addSchedulerListener(new MySchedulerListener());
//registerReceiver(mReceiver, getModule(DownloadModule.class).getDownloadFilter());
}
@Override protected void onDestroy() {
super.onDestroy();
//unregisterReceiver(mReceiver);
}
@Override protected int setLayoutId() {
@ -141,11 +155,11 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
setSupportActionBar(toolbar);
toolbar.setTitle("单任务下载");
init();
Aria.get(this).openBroadcast(true);
}
private void init() {
mEntity = DbEntity.findData(DownloadEntity.class, new String[] { "downloadUrl" },
new String[] { DOWNLOAD_URL });
mEntity = Aria.get(this).getDownloadEntity(DOWNLOAD_URL);
if (mEntity != null) {
mPb.setProgress((int) ((mEntity.getCurrentProgress() * 100) / mEntity.getFileSize()));
mSize.setText(CommonUtil.formatFileSize(mEntity.getFileSize()));

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -147,7 +147,7 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
}
public void setDownloadNum(int num) {
Aria.get(getContext()).setDownloadNum(num);
Aria.get(getContext()).setMaxDownloadNum(num);
}
private String covertCurrentSize(long currentSize) {

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -28,6 +28,6 @@ public class BaseApplication extends Application {
@Override public void onCreate() {
super.onCreate();
AbsFrame.init(this);
DownloadManager.init(this);
//DownloadManager.init(this);
}
}

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -22,6 +22,7 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.os.Environment;
import android.os.Handler;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.DownloadEntity;
import com.arialyy.aria.core.DownloadManager;
import com.arialyy.aria.util.CommonUtil;
@ -50,9 +51,7 @@ public class DownloadModule extends BaseModule {
String[] urls = getContext().getResources().getStringArray(R.array.test_apk_download_url);
List<DownloadEntity> list = new ArrayList<>();
for (String url : urls) {
DownloadEntity entity =
DownloadEntity.findData(DownloadEntity.class, new String[] { "downloadUrl" },
new String[] { url });
DownloadEntity entity = Aria.get(getContext()).getDownloadEntity(url);
if (entity == null) {
entity = createDownloadEntity(url);
}
@ -93,7 +92,6 @@ public class DownloadModule extends BaseModule {
entity.setDownloadPath(getDownloadPath(url));
entity.setFileName(fileName);
//entity.setFileName("taskName_________" + i);
entity.save();
return entity;
}

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

@ -19,15 +19,26 @@
/>
<Button
android:onClick="onClick"
android:id="@+id/num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="16dp"
android:onClick="onClick"
android:text="下载数设置"
/>
<Button
android:id="@+id/stop_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="16dp"
android:onClick="onClick"
android:text="停止所有"
/>
</RelativeLayout>
</RelativeLayout>
</layout>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 438 B

After

Width:  |  Height:  |  Size: 7.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 319 B

After

Width:  |  Height:  |  Size: 5.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 535 B

After

Width:  |  Height:  |  Size: 9.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 755 B

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 951 B

After

Width:  |  Height:  |  Size: 22 KiB

@ -1,5 +1,5 @@
<resources>
<string name="app_name">DownloadDemo</string>
<string name="app_name">Aria</string>
<string name="action_settings">Settings</string>
<string-array name="test_apk_download_url">

@ -1,18 +1,19 @@
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
## Project-wide Gradle settings.
#
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
#
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# Default value: -Xmx1024m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
#
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# org.gradle.parallel=true
#Wed Dec 07 20:19:22 CST 2016
systemProp.http.proxyPassword=7RbgsDfOoBn
systemProp.http.proxyHost=hilton.h.xduotai.com
systemProp.http.proxyUser=duotai
systemProp.http.proxyPort=10969

Binary file not shown.

Before

Width:  |  Height:  |  Size: 788 KiB

After

Width:  |  Height:  |  Size: 1.1 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 55 KiB

After

Width:  |  Height:  |  Size: 44 KiB

Loading…
Cancel
Save