From f5babb38f1e2357c87e6cb3a44662a56314bc56b Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Thu, 3 Aug 2017 18:12:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=81=A2=E5=A4=8D=E6=89=80?= =?UTF-8?q?=E6=9C=89=E4=BB=BB=E5=8A=A1=E6=97=B6=EF=BC=8C=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E4=BB=BB=E5=8A=A1=E7=BB=84=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/command/normal/AbsNormalCmd.java | 93 +++++++++++++- .../aria/core/command/normal/AddCmd.java | 4 +- .../core/command/normal/CancelAllCmd.java | 2 +- .../aria/core/command/normal/CancelCmd.java | 14 +-- .../command/normal/HighestPriorityCmd.java | 4 +- .../core/command/normal/ResumeAllCmd.java | 72 +++++++---- .../aria/core/command/normal/StartCmd.java | 10 +- .../aria/core/command/normal/StopAllCmd.java | 2 +- .../aria/core/command/normal/StopCmd.java | 7 +- .../arialyy/aria/core/queue/QueueControl.java | 119 ++++++++++++++++++ .../simple/download/FtpDownloadActivity.java | 4 +- .../simple/upload/FtpUploadActivity.java | 6 +- 12 files changed, 283 insertions(+), 54 deletions(-) create mode 100644 Aria/src/main/java/com/arialyy/aria/core/queue/QueueControl.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java index 675a918a..12bbfa0c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java @@ -16,15 +16,17 @@ package com.arialyy.aria.core.command.normal; +import android.os.Handler; import com.arialyy.aria.core.command.AbsCmd; import com.arialyy.aria.core.download.DownloadGroupTaskEntity; 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.queue.DownloadGroupTaskQueue; import com.arialyy.aria.core.queue.DownloadTaskQueue; import com.arialyy.aria.core.queue.UploadTaskQueue; import com.arialyy.aria.core.upload.UploadTaskEntity; -import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CommonUtil; /** @@ -32,12 +34,13 @@ import com.arialyy.aria.util.CommonUtil; * 下载命令 */ public abstract class AbsNormalCmd extends AbsCmd { - /** * 能否执行命令 */ boolean canExeCmd = true; + private AbsTask tempTask = null; + /** * @param targetName 产生任务的对象名 */ @@ -58,4 +61,90 @@ public abstract class AbsNormalCmd extends AbsCmd { 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); + } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/AddCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/AddCmd.java index e848813b..8d2ccc39 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/AddCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/AddCmd.java @@ -33,10 +33,10 @@ class AddCmd extends AbsNormalCmd { @Override public void executeCmd() { if (!canExeCmd) return; - AbsTask task = mQueue.getTask(mTaskEntity.getEntity()); + AbsTask task = getTask(); if (task == null) { mTaskEntity.getEntity().setState(IEntity.STATE_WAIT); - mQueue.createTask(mTargetName, mTaskEntity); + createTask(); } else { Log.w(TAG, "添加命令执行失败,【该任务已经存在】"); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelAllCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelAllCmd.java index 9d37a0d5..9126a3e0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelAllCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelAllCmd.java @@ -36,7 +36,7 @@ final class CancelAllCmd extends AbsNormalCmd { } @Override public void executeCmd() { - mQueue.removeAllTask(); + removeAll(); if (mTaskEntity instanceof DownloadTaskEntity) { handleDownloadRemove(); } else if (mTaskEntity instanceof UploadTaskEntity){ diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelCmd.java index ff859066..f13ae910 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelCmd.java @@ -17,7 +17,6 @@ package com.arialyy.aria.core.command.normal; import android.text.TextUtils; -import com.arialyy.aria.core.inf.AbsNormalTask; import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.AbsTaskEntity; @@ -32,15 +31,10 @@ class CancelCmd extends AbsNormalCmd { @Override public void executeCmd() { if (!canExeCmd) return; - AbsTask task = mQueue.getTask(mTaskEntity.getEntity()); - if (task == null) { - task = mQueue.createTask(mTargetName, mTaskEntity); - } - if (task != null) { - if (!TextUtils.isEmpty(mTargetName)) { - task.setTargetName(mTargetName); - } - mQueue.removeTask(task); + AbsTask task = getTask(); + if (!TextUtils.isEmpty(mTargetName)) { + task.setTargetName(mTargetName); } + removeTask(); } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/HighestPriorityCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/HighestPriorityCmd.java index e0efa2f7..836b37df 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/HighestPriorityCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/HighestPriorityCmd.java @@ -43,9 +43,9 @@ final class HighestPriorityCmd extends AbsNormalCmd @Override public void executeCmd() { if (!canExeCmd) return; - DownloadTask task = (DownloadTask) mQueue.getTask(mTaskEntity.getEntity()); + DownloadTask task = (DownloadTask) getTask(); if (task == null) { - task = (DownloadTask) mQueue.createTask(mTargetName, mTaskEntity); + task = (DownloadTask) createTask(); } if (task != null) { if (!TextUtils.isEmpty(mTargetName)) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java index 1233ebe1..1d06c79d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java @@ -1,11 +1,10 @@ package com.arialyy.aria.core.command.normal; -import android.util.Log; -import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.download.DownloadGroupTaskEntity; 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.upload.UploadTaskEntity; import com.arialyy.aria.orm.DbEntity; import java.util.List; @@ -24,29 +23,58 @@ final class ResumeAllCmd extends AbsNormalCmd { } @Override public void executeCmd() { - List allEntity = - DbEntity.findDatas(DownloadEntity.class, "state=?", IEntity.STATE_STOP + ""); - for (DownloadEntity entity : allEntity) { - int exeNum = mQueue.getCurrentExePoolNum(); - if (exeNum == 0 || exeNum < mQueue.getMaxTaskNum()) { - AbsTask task = createTask(entity); - mQueue.startTask(task); - } else { - entity.setState(IEntity.STATE_WAIT); - createTask(entity); - } + if (isDownloadCmd) { + resumeDownload(); + } else { + resumeUpload(); + } + } + + /** + * 恢复下载,包括普通任务和任务组 + */ + private void resumeDownload() { + List 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 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(); - taskEntity.entity = entity; - task = mQueue.createTask(mTargetName, taskEntity); + /** + * 恢复上传,包括普通任务和任务组 + */ + private void resumeUpload() { + List dTaskEntity = + 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 { - Log.w(TAG, "添加命令执行失败,【该任务已经存在】"); + te.getEntity().setState(IEntity.STATE_WAIT); + createTask(te); } - return task; } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java index c61b31da..bacc7658 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java @@ -47,26 +47,26 @@ class StartCmd extends AbsNormalCmd { maxTaskNum = manager.getUploadConfig().getMaxTaskNum(); } - AbsTask task = mQueue.getTask(mTaskEntity.getEntity()); + AbsTask task = getTask(); if (task == null) { - task = mQueue.createTask(mTargetName, mTaskEntity); + task = createTask(); if (!TextUtils.isEmpty(mTargetName)) { task.setTargetName(mTargetName); } // 任务不存在时,根据配置不同,对任务执行操作 if (mod.equals(QueueMod.NOW.getTag())) { - mQueue.startTask(task); + startTask(); } else if (mod.equals(QueueMod.WAIT.getTag())) { if (mQueue.getCurrentExePoolNum() < maxTaskNum || task.getState() == IEntity.STATE_STOP || task.getState() == IEntity.STATE_COMPLETE) { - mQueue.startTask(task); + startTask(); } } } else { // 任务不存在时,根据配置不同,对任务执行操作 if (!task.isRunning()) { - mQueue.startTask(task); + startTask(); } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopAllCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopAllCmd.java index 431adfdd..a21483c3 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopAllCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopAllCmd.java @@ -15,6 +15,6 @@ final class StopAllCmd extends AbsNormalCmd { } @Override public void executeCmd() { - mQueue.stopAllTask(); + stopAll(); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopCmd.java index 9d0b0811..a631f9ab 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopCmd.java @@ -34,11 +34,10 @@ class StopCmd extends AbsNormalCmd { @Override public void executeCmd() { if (!canExeCmd) return; - AbsTask task = mQueue.getTask(mTaskEntity.getEntity()); + AbsTask task = getTask(); if (task == null) { if (mTaskEntity.getEntity().getState() == IEntity.STATE_RUNNING) { - task = mQueue.createTask(mTargetName, mTaskEntity); - mQueue.stopTask(task); + stopTask(); } else { Log.w(TAG, "停止命令执行失败,【调度器中没有该任务】"); } @@ -46,7 +45,7 @@ class StopCmd extends AbsNormalCmd { if (!TextUtils.isEmpty(mTargetName)) { task.setTargetName(mTargetName); } - mQueue.stopTask(task); + stopTask(); } } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/QueueControl.java b/Aria/src/main/java/com/arialyy/aria/core/queue/QueueControl.java new file mode 100644 index 00000000..dd7bbd8d --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/QueueControl.java @@ -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; + } +} diff --git a/app/src/main/java/com/arialyy/simple/download/FtpDownloadActivity.java b/app/src/main/java/com/arialyy/simple/download/FtpDownloadActivity.java index 20e22607..a1bf4f89 100644 --- a/app/src/main/java/com/arialyy/simple/download/FtpDownloadActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/FtpDownloadActivity.java @@ -37,7 +37,7 @@ import java.io.File; public class FtpDownloadActivity extends BaseActivity { //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) { super.init(savedInstanceState); @@ -113,7 +113,7 @@ public class FtpDownloadActivity extends BaseActivity " + CommonUtil.getFileMD5(new File(task.getDownloadPath()))); - T.showShort(this, "FTP下载完成"); + T.showShort(this, "文件:" + task.getEntity().getFileName() + ",下载完成"); } @Override protected int setLayoutId() { diff --git a/app/src/main/java/com/arialyy/simple/upload/FtpUploadActivity.java b/app/src/main/java/com/arialyy/simple/upload/FtpUploadActivity.java index 014e293a..f0355077 100644 --- a/app/src/main/java/com/arialyy/simple/upload/FtpUploadActivity.java +++ b/app/src/main/java/com/arialyy/simple/upload/FtpUploadActivity.java @@ -34,8 +34,8 @@ import com.arialyy.simple.databinding.ActivityFtpUploadBinding; * Ftp 文件上传demo */ public class FtpUploadActivity extends BaseActivity { - private final String FILE_PATH = "/mnt/sdcard/gggg.apk"; - private final String URL = "ftp://172.18.104.79:21/upload/"; + private final String FILE_PATH = "/mnt/sdcard/Download/group_test_3/战斗吧剑灵.apk"; + private final String URL = "ftp://172.18.104.66:21/upload/"; @Override protected void init(Bundle savedInstanceState) { setTile("FTP 文件上传"); @@ -99,6 +99,6 @@ public class FtpUploadActivity extends BaseActivity { @Upload.onTaskComplete public void taskComplete(UploadTask task) { getBinding().setProgress(100); getBinding().setSpeed(""); - T.showShort(this, "上传完成"); + T.showShort(this, "文件:" + task.getEntity().getFileName() + ",上传完成"); } }