修复恢复所有任务时,不能恢复任务组的bug

pull/330/head
AriaLyy 7 years ago
parent 55c0a47395
commit f5babb38f1
  1. 93
      Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java
  2. 4
      Aria/src/main/java/com/arialyy/aria/core/command/normal/AddCmd.java
  3. 2
      Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelAllCmd.java
  4. 14
      Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelCmd.java
  5. 4
      Aria/src/main/java/com/arialyy/aria/core/command/normal/HighestPriorityCmd.java
  6. 72
      Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java
  7. 10
      Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java
  8. 2
      Aria/src/main/java/com/arialyy/aria/core/command/normal/StopAllCmd.java
  9. 7
      Aria/src/main/java/com/arialyy/aria/core/command/normal/StopCmd.java
  10. 119
      Aria/src/main/java/com/arialyy/aria/core/queue/QueueControl.java
  11. 4
      app/src/main/java/com/arialyy/simple/download/FtpDownloadActivity.java
  12. 6
      app/src/main/java/com/arialyy/simple/upload/FtpUploadActivity.java

@ -16,15 +16,17 @@
package com.arialyy.aria.core.command.normal; package com.arialyy.aria.core.command.normal;
import android.os.Handler;
import com.arialyy.aria.core.command.AbsCmd; import com.arialyy.aria.core.command.AbsCmd;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity; import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity;
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.AbsTaskEntity;
import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; import com.arialyy.aria.core.queue.DownloadGroupTaskQueue;
import com.arialyy.aria.core.queue.DownloadTaskQueue; import com.arialyy.aria.core.queue.DownloadTaskQueue;
import com.arialyy.aria.core.queue.UploadTaskQueue; import com.arialyy.aria.core.queue.UploadTaskQueue;
import com.arialyy.aria.core.upload.UploadTaskEntity; import com.arialyy.aria.core.upload.UploadTaskEntity;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
/** /**
@ -32,12 +34,13 @@ import com.arialyy.aria.util.CommonUtil;
* 下载命令 * 下载命令
*/ */
public abstract class AbsNormalCmd<T extends AbsTaskEntity> extends AbsCmd<T> { public abstract class AbsNormalCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
/** /**
* 能否执行命令 * 能否执行命令
*/ */
boolean canExeCmd = true; boolean canExeCmd = true;
private AbsTask tempTask = null;
/** /**
* @param targetName 产生任务的对象名 * @param targetName 产生任务的对象名
*/ */
@ -58,4 +61,90 @@ public abstract class AbsNormalCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
isDownloadCmd = true; isDownloadCmd = true;
} }
} }
/**
* 删除所有任务
*/
void removeAll() {
mQueue.removeAllTask();
}
/**
* 停止所有任务
*/
void stopAll() {
mQueue.stopAllTask();
}
/**
* 停止任务
*/
void stopTask() {
if (tempTask == null) createTask();
mQueue.stopTask(tempTask);
}
/**
* 删除任务
*/
void removeTask() {
if (tempTask == null) createTask();
mQueue.removeTask(tempTask);
}
/**
* 启动任务
*/
void startTask() {
mQueue.startTask(tempTask);
}
/**
* 启动指定任务
*
* @param task 指定任务
*/
void startTask(AbsTask task) {
mQueue.startTask(task);
}
/**
* 从队列中获取任务
*
* @return 执行任务
*/
AbsTask getTask() {
tempTask = mQueue.getTask(mTaskEntity.getEntity());
return tempTask;
}
/**
* 从队列中获取任务
*
* @return 执行任务
*/
AbsTask getTask(AbsEntity entity) {
tempTask = mQueue.getTask(entity);
return tempTask;
}
/**
* 创建任务
*
* @return 创建的任务
*/
AbsTask createTask() {
tempTask = mQueue.createTask(mTargetName, mTaskEntity);
return tempTask;
}
/**
* 创建指定实体的任务
*
* @param taskEntity 特定的任务实体
* @return 创建的任务
*/
AbsTask createTask(AbsTaskEntity taskEntity) {
return mQueue.createTask(mTargetName, taskEntity);
}
} }

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

@ -36,7 +36,7 @@ final class CancelAllCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
} }
@Override public void executeCmd() { @Override public void executeCmd() {
mQueue.removeAllTask(); removeAll();
if (mTaskEntity instanceof DownloadTaskEntity) { if (mTaskEntity instanceof DownloadTaskEntity) {
handleDownloadRemove(); handleDownloadRemove();
} else if (mTaskEntity instanceof UploadTaskEntity){ } else if (mTaskEntity instanceof UploadTaskEntity){

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

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

@ -1,11 +1,10 @@
package com.arialyy.aria.core.command.normal; package com.arialyy.aria.core.command.normal;
import android.util.Log; import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity; 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.AbsTaskEntity;
import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.upload.UploadTaskEntity;
import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.DbEntity;
import java.util.List; import java.util.List;
@ -24,29 +23,58 @@ final class ResumeAllCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
} }
@Override public void executeCmd() { @Override public void executeCmd() {
List<DownloadEntity> allEntity = if (isDownloadCmd) {
DbEntity.findDatas(DownloadEntity.class, "state=?", IEntity.STATE_STOP + ""); resumeDownload();
for (DownloadEntity entity : allEntity) { } else {
int exeNum = mQueue.getCurrentExePoolNum(); resumeUpload();
if (exeNum == 0 || exeNum < mQueue.getMaxTaskNum()) { }
AbsTask task = createTask(entity); }
mQueue.startTask(task);
} else { /**
entity.setState(IEntity.STATE_WAIT); * 恢复下载包括普通任务和任务组
createTask(entity); */
} private void resumeDownload() {
List<DownloadTaskEntity> dTaskEntity =
DbEntity.findDatas(DownloadTaskEntity.class, "isGroupTask=?", "false");
for (DownloadTaskEntity te : dTaskEntity) {
int state = te.getState();
if (state == IEntity.STATE_COMPLETE || state == IEntity.STATE_FAIL) continue;
resumeEntity(te);
}
List<DownloadGroupTaskEntity> groupTask = DbEntity.findAllData(DownloadGroupTaskEntity.class);
for (DownloadGroupTaskEntity te : groupTask) {
int state = te.getState();
if (state == IEntity.STATE_COMPLETE || state == IEntity.STATE_FAIL) continue;
resumeEntity(te);
} }
} }
private AbsTask createTask(DownloadEntity entity) { /**
AbsTask task = mQueue.getTask(entity); * 恢复上传包括普通任务和任务组
if (task == null) { */
DownloadTaskEntity taskEntity = new DownloadTaskEntity(); private void resumeUpload() {
taskEntity.entity = entity; List<UploadTaskEntity> dTaskEntity =
task = mQueue.createTask(mTargetName, taskEntity); DbEntity.findDatas(UploadTaskEntity.class, "isGroupTask=?", "false");
for (UploadTaskEntity te : dTaskEntity) {
int state = te.getState();
if (state == IEntity.STATE_COMPLETE || state == IEntity.STATE_FAIL) continue;
resumeEntity(te);
}
}
/**
* 恢复实体任务
*
* @param te 任务实体
*/
private void resumeEntity(AbsTaskEntity te) {
int exeNum = mQueue.getCurrentExePoolNum();
if (exeNum == 0 || exeNum < mQueue.getMaxTaskNum()) {
startTask(createTask(te));
} else { } else {
Log.w(TAG, "添加命令执行失败,【该任务已经存在】"); te.getEntity().setState(IEntity.STATE_WAIT);
createTask(te);
} }
return task;
} }
} }

@ -47,26 +47,26 @@ class StartCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
maxTaskNum = manager.getUploadConfig().getMaxTaskNum(); maxTaskNum = manager.getUploadConfig().getMaxTaskNum();
} }
AbsTask task = mQueue.getTask(mTaskEntity.getEntity()); AbsTask task = getTask();
if (task == null) { if (task == null) {
task = mQueue.createTask(mTargetName, mTaskEntity); task = createTask();
if (!TextUtils.isEmpty(mTargetName)) { if (!TextUtils.isEmpty(mTargetName)) {
task.setTargetName(mTargetName); task.setTargetName(mTargetName);
} }
// 任务不存在时,根据配置不同,对任务执行操作 // 任务不存在时,根据配置不同,对任务执行操作
if (mod.equals(QueueMod.NOW.getTag())) { if (mod.equals(QueueMod.NOW.getTag())) {
mQueue.startTask(task); startTask();
} else if (mod.equals(QueueMod.WAIT.getTag())) { } else if (mod.equals(QueueMod.WAIT.getTag())) {
if (mQueue.getCurrentExePoolNum() < maxTaskNum if (mQueue.getCurrentExePoolNum() < maxTaskNum
|| task.getState() == IEntity.STATE_STOP || task.getState() == IEntity.STATE_STOP
|| task.getState() == IEntity.STATE_COMPLETE) { || task.getState() == IEntity.STATE_COMPLETE) {
mQueue.startTask(task); startTask();
} }
} }
} else { } else {
// 任务不存在时,根据配置不同,对任务执行操作 // 任务不存在时,根据配置不同,对任务执行操作
if (!task.isRunning()) { if (!task.isRunning()) {
mQueue.startTask(task); startTask();
} }
} }
} }

@ -15,6 +15,6 @@ final class StopAllCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
} }
@Override public void executeCmd() { @Override public void executeCmd() {
mQueue.stopAllTask(); stopAll();
} }
} }

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

@ -0,0 +1,119 @@
/*
* 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.queue;
import android.os.Handler;
import android.os.Message;
import android.util.SparseArray;
import com.arialyy.aria.core.inf.AbsEntity;
import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.AbsTaskEntity;
/**
* Created by Aria.Lao on 2017/8/3.
* 队列控制器用于处理各种命令
*/
public class QueueControl implements Handler.Callback {
/**
* 获取任务命令
*/
public static final int CMD_GET_TASK = 0xa1;
/**
* 创建任务命令
*/
public static final int CMD_CREATE_TASK = 0xa2;
/**
* 启动任务命令
*/
public static final int CMD_START_TASK = 0xa3;
/**
* 停止任务命令
*/
public static final int CMD_STOP_TASK = 0xa4;
/**
* 删除任务命令
*/
public static final int CMD_CANCEL_TASK = 0xa5;
/**
* 停止所有任务命令
*/
public static final int CMD_STOP_ALL_TASK = 0xa6;
/**
* 删除所有任务命令
*/
public static final int CMD_CANCEL_ALL_TASK = 0xa7;
/**
* 队列类型为单文件下载队列
*/
public static final int TYPE_SIMPLE_DOWNLOAD_QUEUE = 0xc1;
/**
* 队列类型为任务组下载队列
*/
public static final int TYPE_SIMPLE_DOWNLOAD_GROUP_QUEUE = 0xc2;
/**
* 队列类型为单文件上传队列
*/
public static final int TYPE_SIMPLE_UPLOAD_QUEUE = 0xc3;
private Handler outHandler;
private AbsTaskQueue queue;
public QueueControl(Handler.Callback callback, int type) {
outHandler = new Handler(callback);
switch (type) {
case TYPE_SIMPLE_DOWNLOAD_QUEUE:
queue = DownloadTaskQueue.getInstance();
break;
case TYPE_SIMPLE_DOWNLOAD_GROUP_QUEUE:
queue = DownloadGroupTaskQueue.getInstance();
break;
case TYPE_SIMPLE_UPLOAD_QUEUE:
queue = UploadTaskQueue.getInstance();
break;
}
}
@Override public boolean handleMessage(Message msg) {
switch (msg.what) {
case CMD_GET_TASK:
outHandler.obtainMessage(CMD_GET_TASK, queue.getTask((AbsEntity) msg.obj)).sendToTarget();
break;
case CMD_CREATE_TASK:
SparseArray params = (SparseArray) msg.obj;
outHandler.obtainMessage(CMD_CREATE_TASK,
queue.createTask(String.valueOf(params.get(1)), (AbsTaskEntity) params.get(2)))
.sendToTarget();
break;
case CMD_START_TASK:
queue.startTask((AbsTask) msg.obj);
break;
case CMD_STOP_TASK:
queue.stopTask((AbsTask) msg.obj);
break;
case CMD_CANCEL_TASK:
queue.removeTask((AbsTask) msg.obj);
break;
case CMD_STOP_ALL_TASK:
queue.stopAllTask();
break;
case CMD_CANCEL_ALL_TASK:
queue.removeAllTask();
break;
}
return true;
}
}

@ -37,7 +37,7 @@ import java.io.File;
public class FtpDownloadActivity extends BaseActivity<ActivityFtpDownloadBinding> { public class FtpDownloadActivity extends BaseActivity<ActivityFtpDownloadBinding> {
//private final String URL = "ftp://172.18.104.129:21/haha/large.rar"; //private final String URL = "ftp://172.18.104.129:21/haha/large.rar";
//private final String URL = "ftp://172.18.104.129:21/haha/large.rar"; //private final String URL = "ftp://172.18.104.129:21/haha/large.rar";
private final String URL = "ftp://172.18.104.129:21/haha/很大的文件_v100.rar"; private final String URL = "ftp://172.18.104.66:21/haha/成都.mp3";
@Override protected void init(Bundle savedInstanceState) { @Override protected void init(Bundle savedInstanceState) {
super.init(savedInstanceState); super.init(savedInstanceState);
@ -113,7 +113,7 @@ public class FtpDownloadActivity extends BaseActivity<ActivityFtpDownloadBinding
getBinding().setSpeed(""); getBinding().setSpeed("");
getBinding().setProgress(100); getBinding().setProgress(100);
Log.d(TAG, "md5 ==> " + CommonUtil.getFileMD5(new File(task.getDownloadPath()))); Log.d(TAG, "md5 ==> " + CommonUtil.getFileMD5(new File(task.getDownloadPath())));
T.showShort(this, "FTP下载完成"); T.showShort(this, "文件:" + task.getEntity().getFileName() + ",下载完成");
} }
@Override protected int setLayoutId() { @Override protected int setLayoutId() {

@ -34,8 +34,8 @@ import com.arialyy.simple.databinding.ActivityFtpUploadBinding;
* Ftp 文件上传demo * Ftp 文件上传demo
*/ */
public class FtpUploadActivity extends BaseActivity<ActivityFtpUploadBinding> { public class FtpUploadActivity extends BaseActivity<ActivityFtpUploadBinding> {
private final String FILE_PATH = "/mnt/sdcard/gggg.apk"; private final String FILE_PATH = "/mnt/sdcard/Download/group_test_3/战斗吧剑灵.apk";
private final String URL = "ftp://172.18.104.79:21/upload/"; private final String URL = "ftp://172.18.104.66:21/upload/";
@Override protected void init(Bundle savedInstanceState) { @Override protected void init(Bundle savedInstanceState) {
setTile("FTP 文件上传"); setTile("FTP 文件上传");
@ -99,6 +99,6 @@ public class FtpUploadActivity extends BaseActivity<ActivityFtpUploadBinding> {
@Upload.onTaskComplete public void taskComplete(UploadTask task) { @Upload.onTaskComplete public void taskComplete(UploadTask task) {
getBinding().setProgress(100); getBinding().setProgress(100);
getBinding().setSpeed(""); getBinding().setSpeed("");
T.showShort(this, "上传完成"); T.showShort(this, "文件:" + task.getEntity().getFileName() + ",上传完成");
} }
} }

Loading…
Cancel
Save