fix bug https://github.com/AriaLyy/Aria/issues/283
fix bug https://github.com/AriaLyy/Aria/issues/305
现在,停止所有任务,未开始的任务状态将变为停止
优化停止任务的速度
pull/330/head
laoyuyu 6 years ago
parent 542ea8c42f
commit 652b53c837
  1. 4
      Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java
  2. 67
      Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java
  3. 6
      Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java
  4. 39
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java
  5. 66
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java
  6. 5
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java
  7. 4
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTask.java
  8. 11
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalTask.java
  9. 59
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java
  10. 15
      Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java
  11. 22
      Aria/src/main/java/com/arialyy/aria/core/inf/TaskSchedulerType.java
  12. 22
      Aria/src/main/java/com/arialyy/aria/core/manager/ThreadTaskManager.java
  13. 11
      Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java
  14. 4
      Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java
  15. 18
      Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java
  16. 7
      Aria/src/main/java/com/arialyy/aria/core/upload/BaseUListener.java
  17. 7
      Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
  18. 31
      AriaCompiler/src/main/java/com/arialyy/compiler/EventProxyFiler.java
  19. 5
      DEV_LOG.md
  20. 3
      app/src/main/AndroidManifest.xml
  21. 23
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  22. 38
      aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java

@ -1,5 +1,6 @@
package com.arialyy.aria.core.command.normal;
import android.text.TextUtils;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadGroupEntity;
@ -62,6 +63,9 @@ final class ResumeAllCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
"isGroupChild=? AND state!=? ORDER BY stopTime DESC", "false", "1");
if (entities != null && !entities.isEmpty()) {
for (DownloadEntity entity : entities) {
//if (TextUtils.isEmpty(entity.getDownloadPath())){
// continue;
//}
resumeTask(TEManager.getInstance().getTEntity(DownloadTaskEntity.class, entity.getKey()));
}
}

@ -30,10 +30,8 @@ import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.util.DbHelper;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Timer;
@ -191,7 +189,7 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
/**
* 启动进度获取定时器
*/
private void startTimer() {
private synchronized void startTimer() {
mTimer = new Timer(true);
mTimer.schedule(new TimerTask() {
@Override public void run() {
@ -207,13 +205,11 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
}, 0, mUpdateInterval);
}
protected void closeTimer() {
synchronized (AbsFileer.class) {
if (mTimer != null) {
mTimer.purge();
mTimer.cancel();
mTimer = null;
}
protected synchronized void closeTimer() {
if (mTimer != null) {
mTimer.purge();
mTimer.cancel();
mTimer = null;
}
}
@ -241,41 +237,60 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
return mConstance.CURRENT_LOCATION;
}
public boolean isRunning() {
public synchronized boolean isRunning() {
return mConstance.isRunning;
}
public void cancel() {
public synchronized void cancel() {
if (mConstance.isCancel) {
return;
}
closeTimer();
mConstance.isRunning = false;
mConstance.isCancel = true;
for (int i = 0; i < mTask.size(); i++) {
AbsThreadTask task = mTask.get(i);
if (task != null) {
task.cancel();
new Thread(new Runnable() {
@Override public void run() {
for (int i = 0; i < mTask.size(); i++) {
AbsThreadTask task = mTask.get(i);
if (task != null) {
task.cancel();
}
}
ThreadTaskManager.getInstance().stopTaskThread(mTaskEntity.getKey());
}
}
ThreadTaskManager.getInstance().stopTaskThread(mTaskEntity.getKey());
}).start();
}
public void stop() {
public synchronized void stop() {
if (mConstance.isStop) {
return;
}
closeTimer();
mConstance.isRunning = false;
mConstance.isStop = true;
if (mConstance.isComplete()) return;
for (int i = 0; i < mTask.size(); i++) {
AbsThreadTask task = mTask.get(i);
if (task != null && !task.isThreadComplete()) {
task.stop();
new Thread(new Runnable() {
@Override public void run() {
for (int i = 0; i < mTask.size(); i++) {
AbsThreadTask task = mTask.get(i);
if (task != null && !task.isThreadComplete()) {
task.stop();
}
}
ThreadTaskManager.getInstance().stopTaskThread(mTaskEntity.getKey());
}
}
ThreadTaskManager.getInstance().stopTaskThread(mTaskEntity.getKey());
}).start();
}
/**
* 直接调用的时候会自动启动线程执行
*/
public void start() {
public synchronized void start() {
if (mConstance.isRunning) {
return;
}
new Thread(this).start();
}

@ -23,6 +23,7 @@ import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.inf.IDownloadListener;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.TaskSchedulerType;
import com.arialyy.aria.core.scheduler.ISchedulers;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CommonUtil;
@ -31,7 +32,7 @@ import java.lang.ref.WeakReference;
/**
* 下载监听类
*/
class BaseDListener<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity<ENTITY>, TASK extends AbsTask<TASK_ENTITY>>
class BaseDListener<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity<ENTITY>, TASK extends AbsTask<ENTITY, TASK_ENTITY>>
implements IDownloadListener {
private static final String TAG = "BaseDListener";
WeakReference<Handler> outHandler;
@ -42,7 +43,6 @@ class BaseDListener<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity<
protected TASK_ENTITY mTaskEntity;
private TASK mTask;
private boolean isConvertSpeed = false;
boolean isWait = false;
private long mLastSaveTime;
private long mUpdateInterval;
@ -102,7 +102,7 @@ class BaseDListener<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity<
}
@Override public void onStop(long stopLocation) {
saveData(isWait ? IEntity.STATE_WAIT : IEntity.STATE_STOP, stopLocation);
saveData(mTask.getSchedulerType() == TaskSchedulerType.TYPE_STOP_AND_WAIT ? IEntity.STATE_WAIT : IEntity.STATE_STOP, stopLocation);
handleSpeed(0);
sendInState2Target(ISchedulers.STOP);
}

@ -22,6 +22,7 @@ import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.downloader.DownloadGroupUtil;
import com.arialyy.aria.core.download.downloader.FtpDirDownloadUtil;
import com.arialyy.aria.core.download.downloader.IDownloadGroupListener;
import com.arialyy.aria.core.inf.AbsGroupTask;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.scheduler.ISchedulers;
@ -32,9 +33,7 @@ import com.arialyy.aria.util.CheckUtil;
* Created by AriaL on 2017/6/27.
* 任务组任务
*/
public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity> {
private final String TAG = "DownloadGroupTask";
private DownloadGroupListener mListener;
public class DownloadGroupTask extends AbsGroupTask<DownloadGroupEntity, DownloadGroupTaskEntity> {
private DownloadGroupTask(DownloadGroupTaskEntity taskEntity, Handler outHandler) {
mTaskEntity = taskEntity;
@ -43,48 +42,18 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity> {
mListener = new DownloadGroupListener(this, mOutHandler);
switch (taskEntity.getRequestType()) {
case AbsTaskEntity.D_HTTP:
mUtil = new DownloadGroupUtil(mListener, mTaskEntity);
mUtil = new DownloadGroupUtil((IDownloadGroupListener) mListener, mTaskEntity);
break;
case AbsTaskEntity.D_FTP_DIR:
mUtil = new FtpDirDownloadUtil(mListener, mTaskEntity);
mUtil = new FtpDirDownloadUtil((IDownloadGroupListener) mListener, mTaskEntity);
break;
}
}
@Override public boolean isRunning() {
return mUtil.isRunning();
}
public DownloadGroupEntity getEntity() {
return mTaskEntity.getEntity();
}
@Override public void start() {
if (mUtil.isRunning()) {
ALog.d(TAG, "任务正在下载");
} else {
mUtil.start();
}
}
@Override public void stop() {
super.stop();
if (!mUtil.isRunning()) {
mListener.onStop(getCurrentProgress());
} else {
mUtil.stop();
}
}
@Override public void cancel() {
super.cancel();
if (!mUtil.isRunning()) {
mListener.onCancel();
} else {
mUtil.cancel();
}
}
@Override public String getTaskName() {
return "任务组->" + (TextUtils.isEmpty(mTaskEntity.getEntity().getAlias())
? mTaskEntity.getEntity().getGroupName() : mTaskEntity.getEntity().getAlias());

@ -19,9 +19,9 @@ package com.arialyy.aria.core.download;
import android.os.Handler;
import android.os.Looper;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.common.IUtil;
import com.arialyy.aria.core.download.downloader.SimpleDownloadUtil;
import com.arialyy.aria.core.inf.AbsNormalTask;
import com.arialyy.aria.core.inf.IDownloadListener;
import com.arialyy.aria.core.scheduler.ISchedulers;
import com.arialyy.aria.util.ALog;
import java.io.File;
@ -30,18 +30,15 @@ import java.io.File;
* Created by lyy on 2016/8/11.
* 下载任务类
*/
public class DownloadTask extends AbsNormalTask<DownloadTaskEntity> {
public class DownloadTask extends AbsNormalTask<DownloadEntity, DownloadTaskEntity> {
public static final String TAG = "DownloadTask";
private DownloadListener mListener;
private DownloadEntity mEntity;
private DownloadTask(DownloadTaskEntity taskEntity, Handler outHandler) {
mTaskEntity = taskEntity;
mOutHandler = outHandler;
mContext = AriaManager.APP;
mListener = new DownloadListener(this, mOutHandler);
mUtil = new SimpleDownloadUtil(taskEntity, mListener);
mUtil = new SimpleDownloadUtil(taskEntity, (IDownloadListener) mListener);
mEntity = taskEntity.getEntity();
}
@ -75,67 +72,10 @@ public class DownloadTask extends AbsNormalTask<DownloadTaskEntity> {
return mEntity.getUrl();
}
/**
* 是否真正下载
*
* @return {@code true} 真正下载
*/
@Override public boolean isRunning() {
return mUtil.isRunning();
}
public DownloadEntity getDownloadEntity() {
return mEntity;
}
/**
* 暂停任务并让任务处于等待状态
*/
@Override public void stopAndWait() {
stop(true);
}
/**
* 开始下载
*/
@Override public void start() {
mListener.isWait = false;
if (mUtil.isRunning()) {
ALog.d(TAG, "任务正在下载");
} else {
mUtil.start();
}
}
/**
* 停止下载
*/
@Override public void stop() {
super.stop();
stop(false);
}
private void stop(boolean isWait) {
mListener.isWait = isWait;
if (mUtil.isRunning()) {
mUtil.stop();
} else {
mListener.onStop(mEntity.getCurrentProgress());
}
}
/**
* 取消下载
*/
@Override public void cancel() {
super.cancel();
if (mUtil.isRunning()) {
mUtil.cancel();
} else {
mListener.onCancel();
}
}
@Override public String getTaskName() {
return mEntity.getFileName();
}

@ -58,7 +58,10 @@ class Downloader extends AbsFileer<DownloadEntity, DownloadTaskEntity> {
@Override protected boolean handleNewTask() {
if (!mRecord.isBlock) {
CommonUtil.createFile(mTempFile.getPath());
if (mTempFile.exists()){
mTempFile.delete();
}
//CommonUtil.createFile(mTempFile.getPath());
}
BufferedRandomAccessFile file = null;
try {

@ -21,8 +21,8 @@ import com.arialyy.aria.core.download.downloader.AbsGroupUtil;
* Created by AriaL on 2017/6/29.
* 任务组任务抽象类
*/
public abstract class AbsGroupTask<TASK_ENTITY extends AbsGroupTaskEntity>
extends AbsTask<TASK_ENTITY> {
public abstract class AbsGroupTask<ENTITY extends AbsEntity, TASK_ENTITY extends AbsGroupTaskEntity>
extends AbsTask<ENTITY, TASK_ENTITY> {
@Override public String getKey() {
return mTaskEntity.getEntity().getKey();

@ -18,15 +18,8 @@ package com.arialyy.aria.core.inf;
/**
* Created by lyy on 2017/6/3.
*/
public abstract class AbsNormalTask<TASK_ENTITY extends AbsNormalTaskEntity>
extends AbsTask<TASK_ENTITY> {
/**
* 暂停任务并让任务处于等待状态
*/
public void stopAndWait() {
}
public abstract class AbsNormalTask<ENTITY extends AbsEntity, TASK_ENTITY extends AbsNormalTaskEntity>
extends AbsTask<ENTITY, TASK_ENTITY> {
/**
* 最高优先级命令最高优先级命令有以下属性

@ -18,24 +18,36 @@ package com.arialyy.aria.core.inf;
import android.content.Context;
import android.os.Handler;
import com.arialyy.aria.core.common.IUtil;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil;
/**
* Created by AriaL on 2017/6/29.
*/
public abstract class AbsTask<TASK_ENTITY extends AbsTaskEntity> implements ITask<TASK_ENTITY> {
public abstract class AbsTask<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity>
implements ITask<TASK_ENTITY> {
/**
* 是否需要重试默认为true
*/
public boolean needRetry = true;
protected TASK_ENTITY mTaskEntity;
protected Handler mOutHandler;
protected Context mContext;
boolean isHeighestTask = false;
private boolean isCancel = false, isStop = false;
protected IUtil mUtil;
/**
* 该任务的调度类型
*/
@TaskSchedulerType
private int mSchedulerType = TaskSchedulerType.TYPE_DEFAULT;
protected IEventListener mListener;
protected ENTITY mEntity;
protected String TAG;
protected AbsTask() {
TAG = CommonUtil.getClassName(this);
}
public Handler getOutHandler() {
return mOutHandler;
@ -127,16 +139,55 @@ public abstract class AbsTask<TASK_ENTITY extends AbsTaskEntity> implements ITas
*
* @return 如果实体不存在则返回null否则返回扩展字段
*/
@Override public String getExtendField() {
public String getExtendField() {
return mTaskEntity.getEntity() == null ? null : mTaskEntity.getEntity().getStr();
}
@Override public void start() {
if (mUtil.isRunning()) {
ALog.d(TAG, "任务正在下载");
} else {
mUtil.start();
}
}
@Override public void stop() {
stop(TaskSchedulerType.TYPE_DEFAULT);
}
@Override public void stop(@TaskSchedulerType int type) {
isStop = true;
mSchedulerType = type;
if (mUtil.isRunning()) {
mUtil.stop();
} else {
mListener.onStop(mEntity.getCurrentProgress());
}
}
@Override public void cancel() {
isCancel = true;
if (!mUtil.isRunning()) {
mListener.onCancel();
} else {
mUtil.cancel();
}
}
/**
* 是否真正下载
*
* @return {@code true} 真正下载
*/
@Override public boolean isRunning() {
return mUtil.isRunning();
}
/**
* 任务的调度类型
*/
public int getSchedulerType() {
return mSchedulerType;
}
/**

@ -25,11 +25,6 @@ public interface ITask<TASK_ENTITY extends AbsTaskEntity> {
*/
int getState();
/**
* 获取扩展字段
*/
String getExtendField();
/**
* 唯一标识符DownloadTask 为下载地址UploadTask 为文件路径
*/
@ -49,8 +44,17 @@ public interface ITask<TASK_ENTITY extends AbsTaskEntity> {
void start();
/**
* 停止任务
*/
void stop();
/**
* 停止任务
* @param type {@code 0}默认操作{@code 1}停止任务不自动执行下一任务
*/
void stop(int type);
void cancel();
/**
@ -89,5 +93,4 @@ public interface ITask<TASK_ENTITY extends AbsTaskEntity> {
* @return 返回 3mb
*/
String getConvertCurrentProgress();
}

@ -0,0 +1,22 @@
package com.arialyy.aria.core.inf;
import android.support.annotation.IntDef;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@IntDef({
TaskSchedulerType.TYPE_DEFAULT,
TaskSchedulerType.TYPE_STOP_NOT_NEXT,
TaskSchedulerType.TYPE_STOP_AND_WAIT
})
@Retention(RetentionPolicy.SOURCE) public @interface TaskSchedulerType {
int TYPE_DEFAULT = 1;
/**
* 停止当前任务并且不自动启动下一任务
*/
int TYPE_STOP_NOT_NEXT = 2;
/**
* 停止任务并让当前任务处于等待状态
*/
int TYPE_STOP_AND_WAIT = 3;
}

@ -83,19 +83,21 @@ public class ThreadTaskManager {
}
key = getKey(key);
Set<Future> temp = mThreadTasks.get(key);
try {
for (Future future : temp) {
if (future.isDone() || future.isCancelled()) {
continue;
if (temp != null && temp.size() > 0) {
try {
for (Future future : temp) {
if (future.isDone() || future.isCancelled()) {
continue;
}
AbsThreadTask task = (AbsThreadTask) future.get();
task.setInterrupted(true);
future.cancel(true);
}
AbsThreadTask task = (AbsThreadTask) future.get();
task.setInterrupted(true);
future.cancel(true);
} catch (Exception e) {
ALog.e(TAG, e);
}
} catch (Exception e) {
ALog.e(TAG, e);
temp.clear();
}
temp.clear();
mThreadTasks.remove(key);
}

@ -19,6 +19,7 @@ package com.arialyy.aria.core.queue;
import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.TaskSchedulerType;
import com.arialyy.aria.core.queue.pool.BaseCachePool;
import com.arialyy.aria.core.queue.pool.BaseExecutePool;
import com.arialyy.aria.util.ALog;
@ -69,7 +70,6 @@ abstract class AbsTaskQueue<TASK extends AbsTask, TASK_ENTITY extends AbsTaskEnt
* 停止所有任务
*/
@Override public void stopAllTask() {
mCachePool.clear();
for (String key : mExecutePool.getAllTask().keySet()) {
TASK task = mExecutePool.getAllTask().get(key);
if (task != null) {
@ -80,6 +80,15 @@ abstract class AbsTaskQueue<TASK extends AbsTask, TASK_ENTITY extends AbsTaskEnt
}
}
}
for (String key : mCachePool.getAllTask().keySet()) {
TASK task = mCachePool.getAllTask().get(key);
if (task != null) {
task.stop(TaskSchedulerType.TYPE_STOP_NOT_NEXT);
}
}
mCachePool.clear();
}
/**

@ -19,6 +19,8 @@ package com.arialyy.aria.core.queue;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.TaskSchedulerType;
import com.arialyy.aria.core.queue.pool.BaseCachePool;
import com.arialyy.aria.core.queue.pool.BaseExecutePool;
import com.arialyy.aria.core.queue.pool.DownloadSharePool;
@ -88,7 +90,7 @@ public class DownloadTaskQueue extends AbsTaskQueue<DownloadTask, DownloadTaskEn
DownloadTask oldTsk = mExecutePool.pollTask();
if (oldTsk != null && oldTsk.isRunning()) {
if (i == maxSize - 1) {
oldTsk.stopAndWait();
oldTsk.stop(TaskSchedulerType.TYPE_STOP_AND_WAIT);
mCachePool.putTaskToFirst(oldTsk);
break;
}

@ -26,6 +26,7 @@ import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.inf.GroupSendParams;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.TaskSchedulerType;
import com.arialyy.aria.core.manager.TEManager;
import com.arialyy.aria.core.queue.ITaskQueue;
import com.arialyy.aria.core.upload.UploadTask;
@ -40,7 +41,7 @@ import java.util.concurrent.ConcurrentHashMap;
* Created by lyy on 2017/6/4.
* 事件调度器用于处理任务状态的调度
*/
abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, TASK extends AbsTask<TASK_ENTITY>,
abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, TASK extends AbsTask,
QUEUE extends ITaskQueue<TASK, TASK_ENTITY>> implements ISchedulers<TASK> {
private final String TAG = "AbsSchedulers";
@ -169,13 +170,13 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, TASK extends Abs
mQueue.removeTaskFormQueue(task.getKey());
if (mQueue.getCurrentExePoolNum() < mQueue.getMaxTaskNum()) {
ALog.d(TAG, "stop_next");
startNextTask();
startNextTask(task);
}
break;
case COMPLETE:
mQueue.removeTaskFormQueue(task.getKey());
ALog.d(TAG, "complete_next");
startNextTask();
startNextTask(task);
break;
case FAIL:
handleFailTask(task);
@ -262,7 +263,7 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, TASK extends Abs
callback(FAIL, task);
mQueue.removeTaskFormQueue(task.getKey());
ALog.d(TAG, "fail_next");
startNextTask();
startNextTask(task);
return;
}
long interval = 2000;
@ -284,7 +285,7 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, TASK extends Abs
|| task.getTaskEntity().getEntity().getFailNum() > reTryNum) {
callback(FAIL, task);
mQueue.removeTaskFormQueue(task.getKey());
startNextTask();
startNextTask(task);
ALog.d(TAG, "retry_next");
TEManager.getInstance().removeTEntity(task.getKey());
return;
@ -303,7 +304,7 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, TASK extends Abs
mQueue.reTryStart(task);
} else {
mQueue.removeTaskFormQueue(task.getKey());
startNextTask();
startNextTask(task);
ALog.d(TAG, "retry_next_1");
TEManager.getInstance().removeTEntity(task.getKey());
}
@ -315,7 +316,10 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, TASK extends Abs
/**
* 启动下一个任务条件任务停止取消下载任务完成
*/
private void startNextTask() {
private void startNextTask(TASK oldTask) {
if (oldTask.getSchedulerType() == TaskSchedulerType.TYPE_STOP_NOT_NEXT){
return;
}
TASK newTask = mQueue.getNextTask();
if (newTask == null) {
if (mQueue.getCurrentExePoolNum() == 0) {

@ -23,6 +23,7 @@ import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.IUploadListener;
import com.arialyy.aria.core.inf.TaskSchedulerType;
import com.arialyy.aria.core.scheduler.ISchedulers;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CommonUtil;
@ -31,7 +32,7 @@ import java.lang.ref.WeakReference;
/**
* 下载监听类
*/
class BaseUListener<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity<ENTITY>, TASK extends AbsTask<TASK_ENTITY>>
class BaseUListener<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity<ENTITY>, TASK extends AbsTask<ENTITY, TASK_ENTITY>>
implements IUploadListener {
private WeakReference<Handler> outHandler;
private int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度
@ -41,7 +42,6 @@ class BaseUListener<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity<
protected TASK_ENTITY mTaskEntity;
protected TASK mTask;
private boolean isConvertSpeed = false;
boolean isWait = false;
private long mLastSaveTime;
private long mUpdateInterval;
@ -89,7 +89,8 @@ class BaseUListener<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity<
}
@Override public void onStop(long stopLocation) {
saveData(isWait ? IEntity.STATE_WAIT : IEntity.STATE_STOP, stopLocation);
saveData(mTask.getSchedulerType() == TaskSchedulerType.TYPE_STOP_AND_WAIT ? IEntity.STATE_WAIT
: IEntity.STATE_STOP, stopLocation);
handleSpeed(0);
sendInState2Target(ISchedulers.STOP);
}

@ -940,8 +940,11 @@ public class CommonUtil {
// 创建目标文件
if (file.exists()) {
final File to = new File(file.getAbsolutePath() + System.currentTimeMillis());
file.renameTo(to);
to.delete();
if (file.renameTo(to)) {
to.delete();
}else {
file.delete();
}
}
try {
if (file.createNewFile()) {

@ -133,7 +133,8 @@ final class EventProxyFiler {
//任务组接口
if (taskEnum == TaskEnum.DOWNLOAD_GROUP_SUB) {
ClassName subTask = ClassName.get(TaskEnum.DOWNLOAD_ENTITY.pkg, TaskEnum.DOWNLOAD_ENTITY.className);
ClassName subTask =
ClassName.get(TaskEnum.DOWNLOAD_ENTITY.pkg, TaskEnum.DOWNLOAD_ENTITY.className);
ParameterSpec subTaskParam =
ParameterSpec.builder(subTask, "subEntity").addModifiers(Modifier.FINAL).build();
@ -161,9 +162,19 @@ final class EventProxyFiler {
"keyMapping").addModifiers(Modifier.PRIVATE).initializer("new $T()", HashMap.class).build();
builder.addField(mappingField);
//Set<Integer> type = new HashSet<>();
//添加注解方法
for (TaskEnum te : entity.methods.keySet()) {
Map<Class<? extends Annotation>, String> temp = entity.methods.get(te);
//if (entity.proxyClassName.contains(TaskEnum.DOWNLOAD.proxySuffix)) {
// type.add(1);
//} else if (entity.proxyClassName.contains(TaskEnum.DOWNLOAD_GROUP.proxySuffix)) {
// type.add(2);
//} else if (entity.proxyClassName.contains(TaskEnum.UPLOAD.proxySuffix)) {
// type.add(3);
//} else if (entity.proxyClassName.contains(TaskEnum.UPLOAD_GROUP.proxySuffix)) {
// type.add(4);
//}
if (temp != null) {
for (Class<? extends Annotation> annotation : temp.keySet()) {
MethodSpec method = createProxyMethod(te, annotation, temp.get(annotation));
@ -189,6 +200,24 @@ final class EventProxyFiler {
sb.append("keyMapping.put(\"").append(methodName).append("\", ").append("set);\n");
cb.add(sb.toString(), ClassName.get(HashSet.class));
}
//注册当前类
//for (Integer i : type) {
// String str = null;
// switch (i) {
// case 1:
// case 2:
// str = "$T.download(obj).register();\n";
// break;
// case 3:
// case 4:
// str = "$T.upload(obj).register();\n";
// break;
// }
// if (str != null) {
// cb.add(str, ClassName.get("com.arialyy.aria.core", "Aria"));
// }
//}
MethodSpec structure =
MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addCode(cb.build()).build();
builder.addMethod(structure);

@ -1,4 +1,9 @@
## 开发日志
+ v_3.4.17
- fix bug https://github.com/AriaLyy/Aria/issues/302
- fix bug https://github.com/AriaLyy/Aria/issues/283
- 现在,停止所有任务,未开始的任务状态将变为停止
- 优化停止任务的速度
+ v_3.4.12
- fix bug https://github.com/AriaLyy/Aria/issues/286
- 优化线程池任务

@ -13,12 +13,11 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar">
<!--android:name=".MainActivity"-->
<!--android:name=".test.TestFTPDirActivity"-->
<!--android:name=".test.AnyRunActivity"-->
<!--android:name=".download.SingleTaskActivity"-->
<!--android:name=".test.TestFTPActivity"-->
<!--android:name=".download.group.DownloadGroupActivity"-->
<!--android:name=".MainActivity"-->
<activity
android:name=".download.SingleTaskActivity"
android:label="@string/app_name">

@ -17,6 +17,7 @@
package com.arialyy.simple.download;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
@ -47,7 +48,9 @@ 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";
//"https://yizi-kejian.oss-cn-beijing.aliyuncs.com/qimeng/package1/qmtable11.zip";
"http://rs.0.gaoshouyou.com/d/04/1e/400423a7551e1f3f0eb1812afa1f9b44.apk";
//"http://58.210.9.131/tpk/sipgt//TDLYZTGH.tpk"; //chunked 下载
//"https://static.donguo.me//video/ip/course/pfys_1.mp4";
//"https://www.baidu.com/link?url=_LFCuTPtnzFxVJByJ504QymRywIA1Z_T5xUxe9ZLuxcGM0C_RcdpWyB1eGjbJC-e5wv5wAKM4WmLMAS5KeF6EZJHB8Va3YqZUiaErqK_pxm&wd=&eqid=e8583fe70002d126000000065a99f864";
@ -188,6 +191,11 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
L.d(TAG, "path ==> " + task.getDownloadEntity().getDownloadPath());
L.d(TAG, "md5Code ==> " + CommonUtil.getFileMD5(new File(task.getDownloadPath())));
L.d(TAG, "data ==> " + Aria.download(this).getDownloadEntity(DOWNLOAD_URL));
//Intent install = new Intent(Intent.ACTION_VIEW);
//install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//File apkFile = new File(task.getDownloadPath());
//install.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
//startActivity(install);
}
}
@ -228,7 +236,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
//Aria.download(this).load(DOWNLOAD_URL).removeRecord();
break;
case R.id.cancel:
Aria.download(this).load(DOWNLOAD_URL).cancel();
Aria.download(this).load(DOWNLOAD_URL).cancel(true);
//Aria.download(this).load(DOWNLOAD_URL).removeRecord();
break;
}
@ -237,6 +245,12 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
private void startD() {
//Aria.get(this).setLogLevel(ALog.LOG_CLOSE);
//Aria.download(this).load("aaaa.apk");
String path = Environment.getExternalStorageDirectory().getPath() + "/ggsg8.apk";
//File file = new File(path);
//if (file.exists()){
// file.delete();
//}
Aria.download(SingleTaskActivity.this)
.load(DOWNLOAD_URL)
//.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
@ -245,7 +259,10 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
//.addHeader("Cookie", "BAIDUID=648E5FF020CC69E8DD6F492D1068AAA9:FG=1; BIDUPSID=648E5FF020CC69E8DD6F492D1068AAA9; PSTM=1519099573; BD_UPN=12314753; locale=zh; BDSVRTM=0")
//.useServerFileName(true)
//.setRequestMode(RequestEnum.GET)
.setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg4.apk")
.setFilePath(path)
//.setExtendField("{\n"
// + "\"id\":\"你的样子\"\n< > "
// + "}")
//.resetState()
.start();
//.add();

@ -18,6 +18,7 @@ package com.arialyy.aria.core.upload;
import android.os.Handler;
import android.os.Looper;
import com.arialyy.aria.core.inf.AbsNormalTask;
import com.arialyy.aria.core.inf.IUploadListener;
import com.arialyy.aria.core.scheduler.ISchedulers;
import com.arialyy.aria.core.upload.uploader.SimpleUploadUtil;
import com.arialyy.aria.util.ALog;
@ -26,56 +27,23 @@ import com.arialyy.aria.util.ALog;
* Created by lyy on 2017/2/23.
* 上传任务
*/
public class UploadTask extends AbsNormalTask<UploadTaskEntity> {
private static final String TAG = "UploadTask";
private BaseUListener<UploadEntity, UploadTaskEntity, UploadTask> mListener;
public class UploadTask extends AbsNormalTask<UploadEntity, UploadTaskEntity> {
private UploadTask(UploadTaskEntity taskEntity, Handler outHandler) {
mTaskEntity = taskEntity;
mOutHandler = outHandler;
mListener = new BaseUListener<>(this, mOutHandler);
mUtil = new SimpleUploadUtil(taskEntity, mListener);
mUtil = new SimpleUploadUtil(taskEntity, (IUploadListener) mListener);
}
@Override public String getKey() {
return mTaskEntity.getEntity().getFilePath();
}
@Override public boolean isRunning() {
return mUtil.isRunning();
}
public UploadEntity getEntity() {
return mTaskEntity.getEntity();
}
@Override public void start() {
if (mUtil.isRunning()) {
ALog.d(TAG, "任务正在下载");
} else {
mUtil.start();
}
}
@Override public void stop() {
super.stop();
if (mUtil.isRunning()) {
mUtil.stop();
} else {
mListener.onStop(getCurrentProgress());
}
}
@Override public void cancel() {
super.cancel();
if (mUtil.isRunning()) {
mUtil.cancel();
} else {
mListener.onCancel();
}
}
@Override public String getTaskName() {
return mTaskEntity.getEntity().getFileName();
}

Loading…
Cancel
Save