pull/330/head
AriaLyy 8 years ago
parent da5fbcb462
commit 37c138aa34
  1. 4
      Aria/src/main/java/com/arialyy/aria/core/command/AbsCmd.java
  2. 6
      Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java
  3. 6
      Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java
  4. 4
      Aria/src/main/java/com/arialyy/aria/core/command/HighestPriorityCmd.java
  5. 2
      Aria/src/main/java/com/arialyy/aria/core/command/ResumeAllCmd.java
  6. 7
      Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java
  7. 6
      Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java
  8. 2
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java
  9. 26
      Aria/src/main/java/com/arialyy/aria/core/download/SingleThreadTask.java
  10. 2
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java
  11. 2
      Aria/src/main/java/com/arialyy/aria/core/inf/IEntity.java
  12. 4
      Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java
  13. 8
      Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java
  14. 9
      Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java
  15. 30
      Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java
  16. 2
      Aria/src/main/java/com/arialyy/aria/core/queue/pool/CachePool.java
  17. 12
      Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java
  18. 8
      Aria/src/main/java/com/arialyy/aria/core/queue/pool/IPool.java
  19. 2
      Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java
  20. 9
      app/src/main/java/com/arialyy/simple/base/BaseApplication.java
  21. 3
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java

@ -32,7 +32,7 @@ import com.arialyy.aria.util.CommonUtil;
*/
public abstract class AbsCmd<T extends AbsTaskEntity> implements ICmd {
ITaskQueue mQueue;
T mEntity;
T mTaskEntity;
String TAG;
String mTargetName;
/**
@ -52,7 +52,7 @@ public abstract class AbsCmd<T extends AbsTaskEntity> implements ICmd {
canExeCmd = CheckUtil.checkCmdEntity(entity,
!(this instanceof CancelCmd) || !(this instanceof StopCmd));
mTargetName = targetName;
mEntity = entity;
mTaskEntity = entity;
TAG = CommonUtil.getClassName(this);
if (entity instanceof DownloadTaskEntity) {
mQueue = DownloadTaskQueue.getInstance();

@ -33,10 +33,10 @@ class AddCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
@Override public void executeCmd() {
if (!canExeCmd) return;
ITask task = mQueue.getTask(mEntity.getEntity());
ITask task = mQueue.getTask(mTaskEntity.getEntity());
if (task == null) {
mEntity.getEntity().setState(IEntity.STATE_WAIT);
mQueue.createTask(mTargetName, mEntity);
mTaskEntity.getEntity().setState(IEntity.STATE_WAIT);
mQueue.createTask(mTargetName, mTaskEntity);
} else {
Log.w(TAG, "添加命令执行失败,【该任务已经存在】");
}

@ -31,15 +31,15 @@ class CancelCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
@Override public void executeCmd() {
if (!canExeCmd) return;
ITask task = mQueue.getTask(mEntity.getEntity());
ITask task = mQueue.getTask(mTaskEntity.getEntity());
if (task == null) {
task = mQueue.createTask(mTargetName, mEntity);
task = mQueue.createTask(mTargetName, mTaskEntity);
}
if (task != null) {
if (!TextUtils.isEmpty(mTargetName)) {
task.setTargetName(mTargetName);
}
mQueue.cancelTask(task);
mQueue.removeTask(task);
}
}
}

@ -39,9 +39,9 @@ final class HighestPriorityCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
@Override public void executeCmd() {
if (!canExeCmd) return;
ITask task = mQueue.getTask(mEntity.getEntity());
ITask task = mQueue.getTask(mTaskEntity.getEntity());
if (task == null) {
task = mQueue.createTask(mTargetName, mEntity);
task = mQueue.createTask(mTargetName, mTaskEntity);
}
if (task != null) {
if (!TextUtils.isEmpty(mTargetName)) {

@ -27,7 +27,7 @@ final class ResumeAllCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
List<DownloadEntity> allEntity =
DbEntity.findDatas(DownloadEntity.class, "state=?", IEntity.STATE_STOP + "");
for (DownloadEntity entity : allEntity) {
int exeNum = mQueue.getExeTaskNum();
int exeNum = mQueue.getExePoolSize();
if (exeNum == 0 || exeNum < mQueue.getMaxTaskNum()) {
ITask task = createTask(entity);
mQueue.startTask(task);

@ -17,7 +17,6 @@
package com.arialyy.aria.core.command;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.QueueMod;
import com.arialyy.aria.core.inf.ITask;
@ -35,9 +34,9 @@ class StartCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
@Override public void executeCmd() {
if (!canExeCmd) return;
ITask task = mQueue.getTask(mEntity.getEntity());
ITask task = mQueue.getTask(mTaskEntity.getEntity());
if (task == null) {
task = mQueue.createTask(mTargetName, mEntity);
task = mQueue.createTask(mTargetName, mTaskEntity);
}
if (task != null) {
if (!TextUtils.isEmpty(mTargetName)) {
@ -56,7 +55,7 @@ class StartCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
if (mod.equals(QueueMod.NOW.getTag())) {
mQueue.startTask(task);
}else if (mod.equals(QueueMod.WAIT.getTag())){
if (mQueue.getExeTaskNum() < maxTaskNum){
if (mQueue.getExePoolSize() < maxTaskNum){
mQueue.startTask(task);
}
}

@ -34,10 +34,10 @@ class StopCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
@Override public void executeCmd() {
if (!canExeCmd) return;
ITask task = mQueue.getTask(mEntity.getEntity());
ITask task = mQueue.getTask(mTaskEntity.getEntity());
if (task == null) {
if (mEntity.getEntity().getState() == IEntity.STATE_RUNNING) {
task = mQueue.createTask(mTargetName, mEntity);
if (mTaskEntity.getEntity().getState() == IEntity.STATE_RUNNING) {
task = mQueue.createTask(mTargetName, mTaskEntity);
mQueue.stopTask(task);
} else {
Log.w(TAG, "停止命令执行失败,【调度器中没有该任务】");

@ -21,7 +21,6 @@ import android.util.Log;
import android.util.SparseArray;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.util.BufferedRandomAccessFile;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
import java.io.File;
import java.io.IOException;
@ -371,6 +370,7 @@ class DownloadUtil implements IDownloadUtil, Runnable {
new BufferedRandomAccessFile(new File(mDownloadFile.getPath()), "rwd", 8192);
//设置文件长度
file.setLength(fileLength);
file.close();
mListener.onPostPre(fileLength);
//分配每条线程的下载区间
pro = CommonUtil.loadConfig(mConfigFile);

@ -76,8 +76,9 @@ final class SingleThreadTask implements Runnable {
}
@Override public void run() {
HttpURLConnection conn;
InputStream is;
HttpURLConnection conn = null;
InputStream is = null;
BufferedRandomAccessFile file = null;
try {
URL url = new URL(mConfigEntity.DOWNLOAD_URL);
conn = ConnectionHelp.handleConnection(url);
@ -102,8 +103,7 @@ final class SingleThreadTask implements Runnable {
conn.setReadTimeout(CONSTANCE.READ_TIME_OUT); //设置读取流的等待时间,必须设置该参数
is = conn.getInputStream();
//创建可设置位置的文件
BufferedRandomAccessFile file =
new BufferedRandomAccessFile(mConfigEntity.TEMP_FILE, "rwd", mBufSize);
file = new BufferedRandomAccessFile(mConfigEntity.TEMP_FILE, "rwd", mBufSize);
//设置每条线程写入文件的位置
file.seek(mConfigEntity.START_LOCATION);
byte[] buffer = new byte[mBufSize];
@ -121,10 +121,6 @@ final class SingleThreadTask implements Runnable {
file.write(buffer, 0, len);
progress(len);
}
file.close();
//close 为阻塞的,需要使用线程池来处理
is.close();
conn.disconnect();
if (CONSTANCE.isCancel) {
return;
}
@ -161,6 +157,20 @@ final class SingleThreadTask implements Runnable {
failDownload(mChildCurrentLocation, "下载失败【" + mConfigEntity.DOWNLOAD_URL + "】", e);
} catch (Exception e) {
failDownload(mChildCurrentLocation, "获取流失败", e);
} finally {
try {
if (file != null) {
file.close();
}
if (is != null) {
is.close();
}
if (conn != null) {
conn.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

@ -35,7 +35,7 @@ public abstract class AbsTaskEntity {
public RequestEnum requestEnum = RequestEnum.GET;
/**
* 重定向后新url的key
* 重定向后从链接中获取新url所需要的key
*/
public String redirectUrlKey = "location";

@ -39,7 +39,7 @@ public interface IEntity {
*/
@Ignore public static final int STATE_STOP = 2;
/**
* 未开始状态
* 等待状态
*/
@Ignore public static final int STATE_WAIT = 3;
/**

@ -44,14 +44,14 @@ public interface ITask<ENTITY extends AbsEntity> {
public String getKey();
/**
* 是否正执行
* 任务是否正执行
*
* @return true正在执行
*/
public boolean isRunning();
/**
* 获取工具实体
* 获取信息实体
*/
public ENTITY getEntity();

@ -78,16 +78,16 @@ abstract class AbsTaskQueue<TASK extends ITask, TASK_ENTITY extends AbsTaskEntit
*
* @return 获取缓存的任务数
*/
@Override public int cachePoolSize() {
@Override public int getCachePoolSize() {
return mCachePool.size();
}
/**
* 获取当前运行的任务数
* 获取执行池中的任务数量
*
* @return 当前正在执行的任务数
*/
@Override public int getExeTaskNum() {
@Override public int getExePoolSize() {
return mExecutePool.size();
}
@ -134,7 +134,7 @@ abstract class AbsTaskQueue<TASK extends ITask, TASK_ENTITY extends AbsTaskEntit
}
}
@Override public void cancelTask(TASK task) {
@Override public void removeTask(TASK task) {
task.cancel();
}

@ -91,10 +91,13 @@ public class DownloadTaskQueue
}
}
public void setMaxSpeed(double maxSpeed){
/**
* 最大下载速度
*/
public void setMaxSpeed(double maxSpeed) {
Map<String, DownloadTask> tasks = mExecutePool.getAllTask();
Set<String> keys = tasks.keySet();
for (String key : keys){
for (String key : keys) {
DownloadTask task = tasks.get(key);
task.setMaxSpeed(maxSpeed);
}
@ -116,7 +119,7 @@ public class DownloadTaskQueue
}
}
}
mExecutePool.setDownloadNum(downloadNum);
mExecutePool.setMaxNum(downloadNum);
if (diff >= 1) {
for (int i = 0; i < diff; i++) {
DownloadTask nextTask = getNextTask();

@ -67,11 +67,18 @@ public interface ITaskQueue<TASK extends ITask, TASK_ENTITY extends AbsTaskEntit
void stopTask(TASK task);
/**
* 取消任务
* 通过任务任务实体删除任务
*
* @param task {@link DownloadTask}{@link UploadTask}
*/
void cancelTask(TASK task);
void removeTask(TASK task);
/**
* 通过工作实体删除任务
*
* @param entity 工作实体{@link DownloadEntity}{@link UploadEntity}
*/
void removeTask(ENTITY entity);
/**
* 重试下载
@ -81,24 +88,24 @@ public interface ITaskQueue<TASK extends ITask, TASK_ENTITY extends AbsTaskEntit
void reTryStart(TASK task);
/**
* 获取正在执行的任务数量
* 获取执行池中的任务数量
*/
int getExeTaskNum();
int getExePoolSize();
/**
* 任务缓存池大小
* 获取任务缓存池中的任务数量
*/
int cachePoolSize();
int getCachePoolSize();
/**
* 设置最大任务数
* 设置执行池可执行的最大任务数
*
* @param newMaxNum 最大任务数
*/
void setMaxTaskNum(int newMaxNum);
/**
* 获取可执行队列的大小
* 获取执行池可执行的最大任务数
*/
int getMaxTaskNum();
@ -127,13 +134,6 @@ public interface ITaskQueue<TASK extends ITask, TASK_ENTITY extends AbsTaskEntit
*/
TASK getTask(String url);
/**
* 通过工作实体删除任务
*
* @param entity 工作实体{@link DownloadEntity}{@link UploadEntity}
*/
void removeTask(ENTITY entity);
/**
* 获取缓存池的下一个任务
*

@ -45,7 +45,7 @@ public class CachePool<TASK extends ITask> implements IPool<TASK> {
}
/**
* 获取所有正在执行的任务
* 获取被缓存的任务
*/
public Map<String, TASK> getAllTask() {
return mCacheMap;

@ -83,19 +83,19 @@ public class ExecutePool<TASK extends ITask> implements IPool<TASK> {
}
/**
* 设置执行任务数
* 设置执行队列最大任务数
*
* @param downloadNum 下载数
* @param maxNum 下载数
*/
public void setDownloadNum(int downloadNum) {
public void setMaxNum(int maxNum) {
try {
ArrayBlockingQueue<TASK> temp = new ArrayBlockingQueue<>(downloadNum);
ArrayBlockingQueue<TASK> temp = new ArrayBlockingQueue<>(maxNum);
TASK task;
while ((task = mExecuteQueue.poll(TIME_OUT, TimeUnit.MICROSECONDS)) != null) {
temp.offer(task);
}
mExecuteQueue = temp;
mSize = downloadNum;
mSize = maxNum;
} catch (InterruptedException e) {
e.printStackTrace();
}
@ -104,7 +104,7 @@ public class ExecutePool<TASK extends ITask> implements IPool<TASK> {
/**
* 添加新任务
*
* @param newTask 下载任务
* @param newTask 新任务
*/
private boolean putNewTask(TASK newTask) {
String url = newTask.getKey();

@ -52,17 +52,17 @@ public interface IPool<T extends ITask> {
public boolean removeTask(T task);
/**
* 通过下载链接移除下载任务
* 通过key除下载任务
*
* @param downloadUrl 下载链接
* @param key 下载链接
* @return true:移除成功
*/
public boolean removeTask(String downloadUrl);
public boolean removeTask(String key);
/**
* 池子大小
*
* @return 返回缓存池或者当前任务池大小
* @return 返回缓存池或者执行池大小
*/
public int size();
}

@ -140,7 +140,7 @@ public abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY ex
}
case CANCEL:
mQueue.removeTask(entity);
if (mQueue.getExeTaskNum() < AriaManager.getInstance(AriaManager.APP)
if (mQueue.getExePoolSize() < AriaManager.getInstance(AriaManager.APP)
.getUploadConfig()
.getMaxTaskNum()) {
startNextTask();

@ -17,7 +17,10 @@
package com.arialyy.simple.base;
import android.app.Application;
import android.os.Build;
import android.os.StrictMode;
import com.arialyy.frame.core.AbsFrame;
import com.arialyy.simple.BuildConfig;
/**
* Created by Lyy on 2016/9/27.
@ -26,5 +29,11 @@ public class BaseApplication extends Application {
@Override public void onCreate() {
super.onCreate();
AbsFrame.init(this);
if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
StrictMode.setThreadPolicy(
new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build());
}
}
}

@ -62,7 +62,8 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
private static final String DOWNLOAD_URL =
//"http://kotlinlang.org/docs/kotlin-docs.pdf";
//"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe";
"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk";
//"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk";
"http://down2.xiaoshuofuwuqi.com/d/file/filetxt/20170608/14/%BA%DA%CE%D7%CA%A6%E1%C8%C6%F0.txt";
//"http://tinghuaapp.oss-cn-shanghai.aliyuncs.com/20170612201739607815";
//"http://static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk";
//"http://oqcpqqvuf.bkt.clouddn.com/ceshi.txt";

Loading…
Cancel
Save