AriaLyy 8 years ago
parent b7b1c88cc9
commit 3c876d8d52
  1. 6
      Aria/src/main/java/com/arialyy/aria/core/QueueMod.java
  2. 3
      Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java
  3. 3
      Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java
  4. 3
      Aria/src/main/java/com/arialyy/aria/core/command/HighestPriorityCmd.java
  5. 7
      Aria/src/main/java/com/arialyy/aria/core/command/ResumeAllCmd.java
  6. 26
      Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java
  7. 3
      Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java
  8. 27
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java
  9. 12
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java
  10. 9
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java
  11. 5
      Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java
  12. 3
      Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java
  13. 6
      Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java
  14. 1
      app/build.gradle
  15. 9
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  16. 5
      app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java

@ -6,8 +6,10 @@ package com.arialyy.aria.core;
*/
public enum QueueMod {
/**
* 等待模式如果执行队列已经满了再次使用start命令执行任务时该任务会被添加到缓存队列中
* 当执行队列的任务完成时将自动执行缓存队列中的任务
* 等待模式
* 如果执行队列已经满了再对其它任务TASK_A使用start命令执行任务时
* 1TASK_A添加到缓存队列中当执行队列中的任务完成时系统会将自动执行缓存队列中的TASK_A
* 2如果再次对TASK_A使用start命令TASK_A将会立刻执行
*/
WAIT("wait"),

@ -17,6 +17,7 @@
package com.arialyy.aria.core.command;
import android.util.Log;
import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.ITask;
import com.arialyy.aria.core.inf.AbsTaskEntity;
@ -33,7 +34,7 @@ class AddCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
@Override public void executeCmd() {
if (!canExeCmd) return;
ITask task = mQueue.getTask(mTaskEntity.getEntity());
AbsTask task = mQueue.getTask(mTaskEntity.getEntity());
if (task == null) {
mTaskEntity.getEntity().setState(IEntity.STATE_WAIT);
mQueue.createTask(mTargetName, mTaskEntity);

@ -17,6 +17,7 @@
package com.arialyy.aria.core.command;
import android.text.TextUtils;
import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.ITask;
import com.arialyy.aria.core.inf.AbsTaskEntity;
@ -31,7 +32,7 @@ class CancelCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
@Override public void executeCmd() {
if (!canExeCmd) return;
ITask task = mQueue.getTask(mTaskEntity.getEntity());
AbsTask task = mQueue.getTask(mTaskEntity.getEntity());
if (task == null) {
task = mQueue.createTask(mTargetName, mTaskEntity);
}

@ -16,6 +16,7 @@
package com.arialyy.aria.core.command;
import android.text.TextUtils;
import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.ITask;
import com.arialyy.aria.core.inf.AbsTaskEntity;
@ -39,7 +40,7 @@ final class HighestPriorityCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
@Override public void executeCmd() {
if (!canExeCmd) return;
ITask task = mQueue.getTask(mTaskEntity.getEntity());
AbsTask task = mQueue.getTask(mTaskEntity.getEntity());
if (task == null) {
task = mQueue.createTask(mTargetName, mTaskEntity);
}

@ -3,6 +3,7 @@ package com.arialyy.aria.core.command;
import android.util.Log;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity;
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.ITask;
@ -29,7 +30,7 @@ final class ResumeAllCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
for (DownloadEntity entity : allEntity) {
int exeNum = mQueue.getExePoolSize();
if (exeNum == 0 || exeNum < mQueue.getMaxTaskNum()) {
ITask task = createTask(entity);
AbsTask task = createTask(entity);
mQueue.startTask(task);
} else {
entity.setState(IEntity.STATE_WAIT);
@ -38,8 +39,8 @@ final class ResumeAllCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
}
}
private ITask createTask(DownloadEntity entity) {
ITask task = mQueue.getTask(entity);
private AbsTask createTask(DownloadEntity entity) {
AbsTask task = mQueue.getTask(entity);
if (task == null) {
DownloadTaskEntity taskEntity = new DownloadTaskEntity(entity);
task = mQueue.createTask(mTargetName, taskEntity);

@ -19,12 +19,15 @@ package com.arialyy.aria.core.command;
import android.text.TextUtils;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.QueueMod;
import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.ITask;
import com.arialyy.aria.core.inf.AbsTaskEntity;
/**
* Created by lyy on 2016/8/22.
* 开始命令
* 队列模型{@link QueueMod#NOW}{@link QueueMod#WAIT}
*/
class StartCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
@ -34,14 +37,6 @@ class StartCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
@Override public void executeCmd() {
if (!canExeCmd) return;
ITask task = mQueue.getTask(mTaskEntity.getEntity());
if (task == null) {
task = mQueue.createTask(mTargetName, mTaskEntity);
}
if (task != null) {
if (!TextUtils.isEmpty(mTargetName)) {
task.setTargetName(mTargetName);
}
String mod;
int maxTaskNum;
AriaManager manager = AriaManager.getInstance(AriaManager.APP);
@ -52,6 +47,14 @@ class StartCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
mod = manager.getUploadConfig().getQueueMod();
maxTaskNum = manager.getUploadConfig().getMaxTaskNum();
}
AbsTask task = mQueue.getTask(mTaskEntity.getEntity());
if (task == null) {
task = mQueue.createTask(mTargetName, mTaskEntity);
if (!TextUtils.isEmpty(mTargetName)) {
task.setTargetName(mTargetName);
}
// 任务不存在时,根据配置不同,对任务执行操作
if (mod.equals(QueueMod.NOW.getTag())) {
mQueue.startTask(task);
} else if (mod.equals(QueueMod.WAIT.getTag())) {
@ -59,6 +62,13 @@ class StartCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
mQueue.startTask(task);
}
}
} else {
// 任务不存在时,根据配置不同,对任务执行操作
if (!task.isRunning()
&& mod.equals(QueueMod.WAIT.getTag())
&& task.getState() == IEntity.STATE_WAIT) {
mQueue.startTask(task);
}
}
}
}

@ -18,6 +18,7 @@ package com.arialyy.aria.core.command;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.ITask;
import com.arialyy.aria.core.inf.AbsTaskEntity;
@ -34,7 +35,7 @@ class StopCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
@Override public void executeCmd() {
if (!canExeCmd) return;
ITask task = mQueue.getTask(mTaskEntity.getEntity());
AbsTask task = mQueue.getTask(mTaskEntity.getEntity());
if (task == null) {
if (mTaskEntity.getEntity().getState() == IEntity.STATE_RUNNING) {
task = mQueue.createTask(mTargetName, mTaskEntity);

@ -20,6 +20,8 @@ import android.content.Context;
import android.util.Log;
import android.util.SparseArray;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.orm.DbUtil;
import com.arialyy.aria.util.BufferedRandomAccessFile;
import com.arialyy.aria.util.CommonUtil;
import java.io.File;
@ -78,12 +80,28 @@ class DownloadUtil implements IDownloadUtil, Runnable {
+ AriaManager.DOWNLOAD_TEMP_DIR
+ mDownloadFile.getName()
+ ".properties");
checkTask();
}
/**
* 检查任务是否是新任务新任务条件
* 1文件不存在
* 2下载记录文件不存在
* 3下载记录文件缺失或不匹配
* 4数据库记录不存在
*/
private void checkTask() {
try {
if (!mConfigFile.exists()) { //记录文件被删除,则重新下载
isNewTask = true;
CommonUtil.createFile(mConfigFile.getPath());
} else if (!mDownloadFile.exists()) {
isNewTask = true;
} else if (DbEntity.findData(DownloadEntity.class, "downloadUrl=?",
mDownloadEntity.getDownloadUrl()) == null) {
isNewTask = true;
} else {
isNewTask = !mDownloadFile.exists();
isNewTask = false;
}
} catch (Exception e) {
e.printStackTrace();
@ -195,9 +213,10 @@ class DownloadUtil implements IDownloadUtil, Runnable {
}
@Override public void run() {
HttpURLConnection conn = null;
try {
URL url = new URL(mDownloadEntity.getDownloadUrl());
HttpURLConnection conn = ConnectionHelp.handleConnection(url);
conn = ConnectionHelp.handleConnection(url);
conn = ConnectionHelp.setConnectParam(mDownloadTaskEntity, conn);
conn.setRequestProperty("Range", "bytes=" + 0 + "-");
conn.setConnectTimeout(mConnectTimeOut);
@ -210,6 +229,10 @@ class DownloadUtil implements IDownloadUtil, Runnable {
+ mDownloadFile.getPath()
+ "】\n"
+ CommonUtil.getPrintException(e));
} finally {
if (conn != null) {
conn.disconnect();
}
}
}

@ -206,11 +206,13 @@ public abstract class AbsTarget<ENTITY extends AbsEntity, TASK_ENTITY extends Ab
* 开始下载
*/
public void start() {
List<AbsCmd> cmds = new ArrayList<>();
cmds.add(CommonUtil.createCmd(targetName, taskEntity, CmdFactory.TASK_CREATE));
cmds.add(CommonUtil.createCmd(targetName, taskEntity, CmdFactory.TASK_START));
AriaManager.getInstance(AriaManager.APP).setCmds(cmds).exe();
cmds.clear();
//List<AbsCmd> cmds = new ArrayList<>();
//cmds.add(CommonUtil.createCmd(targetName, taskEntity, CmdFactory.TASK_CREATE));
//cmds.add(CommonUtil.createCmd(targetName, taskEntity, CmdFactory.TASK_START));
//cmds.clear();
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createCmd(targetName, taskEntity, CmdFactory.TASK_START))
.exe();
}
/**

@ -37,6 +37,15 @@ public abstract class AbsTask<TASK_ENTITY extends AbsTaskEntity, ENTITY extends
}
/**
* 任务当前状态
*
* @return {@link IEntity}
*/
public int getState() {
return mEntity == null ? IEntity.STATE_OTHER : mEntity.getState();
}
/**
* @return 返回原始byte速度需要你在配置文件中配置
* <pre>

@ -20,6 +20,7 @@ import android.util.Log;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.inf.AbsEntity;
import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.ITask;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.queue.pool.CachePool;
@ -30,7 +31,7 @@ import java.util.Set;
* Created by lyy on 2017/2/23.
* 任务队列
*/
abstract class AbsTaskQueue<TASK extends ITask, TASK_ENTITY extends AbsTaskEntity, ENTITY extends AbsEntity>
abstract class AbsTaskQueue<TASK extends AbsTask, TASK_ENTITY extends AbsTaskEntity, ENTITY extends AbsEntity>
implements ITaskQueue<TASK, TASK_ENTITY, ENTITY> {
private final String TAG = "AbsTaskQueue";
CachePool<TASK> mCachePool = new CachePool<>();
@ -118,7 +119,7 @@ abstract class AbsTaskQueue<TASK extends ITask, TASK_ENTITY extends AbsTaskEntit
task.stop();
} else {
task.stop();
Log.w(TAG, "停止任务失败,【任务已经停止】");
Log.w(TAG, "删除任务失败,【执行队列中没有该任务】");
}
}

@ -19,6 +19,7 @@ package com.arialyy.aria.core.queue;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.ITask;
import com.arialyy.aria.core.inf.AbsTaskEntity;
@ -30,7 +31,7 @@ import com.arialyy.aria.core.upload.UploadTaskEntity;
* Created by lyy on 2016/8/16.
* 任务功能接口
*/
public interface ITaskQueue<TASK extends ITask, TASK_ENTITY extends AbsTaskEntity, ENTITY extends IEntity> {
public interface ITaskQueue<TASK extends AbsTask, TASK_ENTITY extends AbsTaskEntity, ENTITY extends IEntity> {
/**
* 通过key判断任务是否正在执行

@ -21,6 +21,7 @@ import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.inf.AbsEntity;
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.ITask;
@ -34,7 +35,7 @@ import java.util.concurrent.ConcurrentHashMap;
/**
* Created by lyy on 2017/6/4.
*/
public abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends AbsEntity, TASK extends ITask<ENTITY>, QUEUE extends ITaskQueue<TASK, TASK_ENTITY, ENTITY>>
public abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends AbsEntity, TASK extends AbsTask<TASK_ENTITY, ENTITY>, QUEUE extends ITaskQueue<TASK, TASK_ENTITY, ENTITY>>
implements ISchedulers<TASK> {
private static final String TAG = "AbsSchedulers";
@ -131,7 +132,7 @@ public abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY ex
Log.e(TAG, "请传入下载任务");
return true;
}
callback(msg.what, task);
//callback(msg.what, task);
ENTITY entity = task.getEntity();
switch (msg.what) {
case STOP:
@ -154,6 +155,7 @@ public abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY ex
handleFailTask(task);
break;
}
callback(msg.what, task);
return true;
}

@ -42,6 +42,7 @@ dependencies {
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.arialyy.frame:MVVM2:2.2.0'
compile project(':Aria')
compile project(':AriaCompiler')
// compile 'com.arialyy.aria:aria-core:3.1.9'
// annotationProcessor 'com.arialyy.aria:aria-compiler:3.1.9'

@ -209,6 +209,10 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
@Download.onTaskCancel(DOWNLOAD_URL) void taskCancel(DownloadTask task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_CANCEL);
L.d(TAG, "task__cancel");
//Aria.download(this)
// .load(DOWNLOAD_URL)
// .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk")
// .add();
}
@Download.onTaskFail(DOWNLOAD_URL) void taskFail(DownloadTask task) {
@ -257,6 +261,11 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
} else if (text.equals("恢复")) {
Aria.download(this).load(DOWNLOAD_URL).resume();
}
//DownloadTarget target = Aria.download(this)
// .load(DOWNLOAD_URL)
// .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk");
//target.add();
//target.cancel();
break;
case R.id.stop:
Aria.download(this).load(DOWNLOAD_URL).pause();

@ -106,10 +106,7 @@ public class MultiDownloadActivity extends BaseActivity<ActivityMultiDownloadBin
mAdapter.updateState(task.getDownloadEntity());
}
@Download.onTaskRunning({
"https://g37.gdl.netease.com/onmyoji_netease_10_1.0.20.apk",
"http://static.gaoshouyou.com/d/eb/f2/dfeba30541f209ab8a50d847fc1661ce.apk"
}) void taskRunning(DownloadTask task) {
@Download.onTaskRunning() void taskRunning(DownloadTask task) {
mAdapter.setProgress(task.getDownloadEntity());
}
}

Loading…
Cancel
Save