From ce36165e1dd2231d0e4e253bcb1ac281c80ec3cf Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Fri, 1 Sep 2017 14:01:38 +0800 Subject: [PATCH] fix bug https://github.com/AriaLyy/Aria/issues/104 https://github.com/AriaLyy/Aria/issues/106 --- Aria/build.gradle | 4 +- .../com/arialyy/aria/core/TaskManager.java | 85 +++++++++++++++++++ .../core/command/normal/CancelAllCmd.java | 14 ++- .../aria/core/command/normal/CancelCmd.java | 9 +- .../aria/core/download/BaseGroupTarget.java | 6 +- .../aria/core/download/DownloadReceiver.java | 16 ++-- .../download/downloader/AbsGroupUtil.java | 21 ++++- .../com/arialyy/aria/core/inf/AbsTarget.java | 11 +-- .../com/arialyy/aria/core/inf/AbsTask.java | 2 +- .../com/arialyy/aria/util/CommonUtil.java | 7 +- README.md | 10 +-- .../simple/download/SingleTaskActivity.java | 1 + .../download/group/DownloadGroupActivity.java | 2 +- build.gradle | 2 +- 14 files changed, 157 insertions(+), 33 deletions(-) create mode 100644 Aria/src/main/java/com/arialyy/aria/core/TaskManager.java diff --git a/Aria/build.gradle b/Aria/build.gradle index 295039bb..75008e89 100644 --- a/Aria/build.gradle +++ b/Aria/build.gradle @@ -23,8 +23,8 @@ dependencies { testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile project(':AriaAnnotations') - compile 'com.arialyy.aria:aria-ftp-plug:1.0.0' -// compile project(':AriaFtpPlug') +// compile 'com.arialyy.aria:aria-ftp-plug:1.0.0' + compile project(':AriaFtpPlug') } apply from: 'bintray-release.gradle' diff --git a/Aria/src/main/java/com/arialyy/aria/core/TaskManager.java b/Aria/src/main/java/com/arialyy/aria/core/TaskManager.java new file mode 100644 index 00000000..470ab6db --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/TaskManager.java @@ -0,0 +1,85 @@ +/* + * 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; + +import android.util.Log; +import com.arialyy.aria.core.inf.AbsTask; +import com.arialyy.aria.util.CommonUtil; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Created by Aria.Lao on 2017/9/1. + * 任务管理器 + */ +public class TaskManager { + private static final String TAG = "TaskManager"; + private static volatile TaskManager INSTANCE = null; + private Map map = new ConcurrentHashMap<>(); + + public TaskManager getInstance() { + if (INSTANCE == null) { + synchronized (AriaManager.LOCK) { + INSTANCE = new TaskManager(); + } + } + return INSTANCE; + } + + private TaskManager() { + + } + + /** + * 管理器添加任务 + * + * @param key 任务的key,下载为保存路径,任务组为任务组名,上传为文件上传路径 + * @param task 任务 + * @return {@code true}添加成功 + */ + public boolean addTask(String key, AbsTask task) { + String hash = CommonUtil.keyToHashKey(key); + if (map.keySet().contains(hash)) { + Log.e(TAG, "任务【" + key + "】已存在"); + return false; + } + map.put(CommonUtil.keyToHashKey(key), task); + return true; + } + + /** + * 移除任务 + * + * @param key 任务的key,下载为保存路径,任务组为任务组名,上传为文件上传路径 + */ + public void removeTask(String key) { + String hash = CommonUtil.keyToHashKey(key); + for (Iterator> iter = map.entrySet().iterator(); iter.hasNext(); ) { + Map.Entry entry = iter.next(); + if (entry.getKey().equals(hash)) iter.remove(); + } + } + + /** + * 通过key获取任务 + * + * @return 入梅找不到任务,返回null,否则返回key对应的任务 + */ + public AbsTask getTask(String key) { + return map.get(CommonUtil.keyToHashKey(key)); + } +} 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 9126a3e0..fc10fb5a 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 @@ -27,7 +27,13 @@ import java.util.List; * Created by AriaL on 2017/6/27. * 删除所有任务,并且删除所有回掉 */ -final class CancelAllCmd extends AbsNormalCmd { +public class CancelAllCmd extends AbsNormalCmd { + /** + * removeFile {@code true} 删除已经下载完成的任务,不仅删除下载记录,还会删除已经下载完成的文件,{@code false} + * 如果文件已经下载完成,只删除下载记录 + */ + public boolean removeFile = false; + /** * @param targetName 产生任务的对象名 */ @@ -39,7 +45,7 @@ final class CancelAllCmd extends AbsNormalCmd { removeAll(); if (mTaskEntity instanceof DownloadTaskEntity) { handleDownloadRemove(); - } else if (mTaskEntity instanceof UploadTaskEntity){ + } else if (mTaskEntity instanceof UploadTaskEntity) { handleUploadRemove(); } } @@ -51,7 +57,7 @@ final class CancelAllCmd extends AbsNormalCmd { List allEntity = DbEntity.findAllData(UploadTaskEntity.class); if (allEntity == null || allEntity.size() == 0) return; for (UploadTaskEntity entity : allEntity) { - CommonUtil.delUploadTaskConfig(mTaskEntity.removeFile, entity); + CommonUtil.delUploadTaskConfig(removeFile, entity); } } @@ -62,7 +68,7 @@ final class CancelAllCmd extends AbsNormalCmd { List allEntity = DbEntity.findAllData(DownloadTaskEntity.class); if (allEntity == null || allEntity.size() == 0) return; for (DownloadTaskEntity entity : allEntity) { - CommonUtil.delDownloadTaskConfig(mTaskEntity.removeFile, entity); + CommonUtil.delDownloadTaskConfig(removeFile, entity); } } } 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 ff959b2d..ee0341e7 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 @@ -24,7 +24,13 @@ import com.arialyy.aria.core.inf.AbsTaskEntity; * Created by lyy on 2016/9/20. * 取消命令 */ -class CancelCmd extends AbsNormalCmd { +public class CancelCmd extends AbsNormalCmd { + /** + * removeFile {@code true} 删除已经下载完成的任务,不仅删除下载记录,还会删除已经下载完成的文件,{@code false} + * 如果文件已经下载完成,只删除下载记录 + */ + public boolean removeFile = false; + CancelCmd(String targetName, T entity) { super(targetName, entity); } @@ -36,6 +42,7 @@ class CancelCmd extends AbsNormalCmd { task = createTask(); } if (task != null) { + mTaskEntity.removeFile = removeFile; if (!TextUtils.isEmpty(mTargetName)) { task.setTargetName(mTargetName); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/BaseGroupTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/BaseGroupTarget.java index e38a422b..dd9e74b3 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/BaseGroupTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/BaseGroupTarget.java @@ -161,8 +161,10 @@ abstract class BaseGroupTarget entities = mEntity.getSubTask(); int i = 0; for (DownloadEntity entity : entities) { - String newName = mSubTaskFileName.get(i); - updateSubFileName(entity, newName); + if (i < mSubTaskFileName.size()) { + String newName = mSubTaskFileName.get(i); + updateSubFileName(entity, newName); + } i++; } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java index 886fcb68..20f9fd79 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java @@ -16,7 +16,9 @@ package com.arialyy.aria.core.download; import android.support.annotation.NonNull; +import android.text.TextUtils; import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.command.normal.CancelAllCmd; import com.arialyy.aria.core.command.normal.NormalCmdFactory; import com.arialyy.aria.core.common.ProxyHelper; import com.arialyy.aria.core.inf.AbsEntity; @@ -205,8 +207,10 @@ public class DownloadReceiver extends AbsReceiver { */ public DownloadTaskEntity getDownloadTask(String downloadUrl) { CheckUtil.checkDownloadUrl(downloadUrl); - return DbEntity.findFirst(DownloadTaskEntity.class, "groupName=? and isGroupTask='false'", - downloadUrl); + DownloadEntity entity = getDownloadEntity(downloadUrl); + if (entity == null || TextUtils.isEmpty(entity.getDownloadPath())) return null; + return DbEntity.findFirst(DownloadTaskEntity.class, "key=? and isGroupTask='false'", + entity.getDownloadPath()); } /** @@ -295,9 +299,11 @@ public class DownloadReceiver extends AbsReceiver { */ @Override public void removeAllTask(boolean removeFile) { final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP); - ariaManager.setCmd(CommonUtil.createCmd(targetName, new DownloadTaskEntity(), - NormalCmdFactory.TASK_CANCEL_ALL)).exe(); - + CancelAllCmd cancelCmd = + (CancelAllCmd) CommonUtil.createCmd(targetName, new DownloadTaskEntity(), + NormalCmdFactory.TASK_CANCEL_ALL); + cancelCmd.removeFile = removeFile; + ariaManager.setCmd(cancelCmd).exe(); Set keys = ariaManager.getReceiver().keySet(); for (String key : keys) { ariaManager.getReceiver().remove(key); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java index 543f452e..47fab43c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java @@ -39,7 +39,7 @@ import java.util.concurrent.Executors; * 任务组核心逻辑 */ abstract class AbsGroupUtil implements IUtil { - private final String TAG = "DownloadGroupUtil"; + private final String TAG = "AbsGroupUtil"; /** * 任务组所有任务总大小 */ @@ -96,6 +96,7 @@ abstract class AbsGroupUtil implements IUtil { DbEntity.findDatas(DownloadTaskEntity.class, "groupName=?", mTaskEntity.key); if (tasks != null && !tasks.isEmpty()) { for (DownloadTaskEntity te : tasks) { + te.removeFile = mTaskEntity.removeFile; mTasksMap.put(te.getEntity().getUrl(), te); } } @@ -167,9 +168,21 @@ abstract class AbsGroupUtil implements IUtil { private void delDownloadInfo() { List tasks = DbEntity.findDatas(DownloadTaskEntity.class, "groupName=?", mTaskEntity.key); - if (tasks == null || tasks.isEmpty()) return; - for (DownloadTaskEntity taskEntity : tasks) { - CommonUtil.delDownloadTaskConfig(taskEntity.removeFile, taskEntity); + if (tasks != null && !tasks.isEmpty()) { + for (DownloadTaskEntity taskEntity : tasks) { + CommonUtil.delDownloadTaskConfig(mTaskEntity.removeFile, taskEntity); + } + } + + File dir = new File(mTaskEntity.getEntity().getDirPath()); + if (mTaskEntity.removeFile) { + if (dir.exists()) { + dir.delete(); + } + } else { + if (!mTaskEntity.getEntity().isComplete()) { + dir.delete(); + } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java index 42c91bbe..57d0879b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java @@ -19,8 +19,9 @@ import android.support.annotation.NonNull; import android.text.TextUtils; import android.util.Log; import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.common.RequestEnum; +import com.arialyy.aria.core.command.normal.CancelCmd; import com.arialyy.aria.core.command.normal.NormalCmdFactory; +import com.arialyy.aria.core.common.RequestEnum; import com.arialyy.aria.util.CommonUtil; import java.util.Map; import java.util.Set; @@ -205,10 +206,10 @@ public abstract class AbsTarget implements ITask */ private String mTargetName; protected Context mContext; - protected boolean isHeighestTask = false; /** diff --git a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java index 021bbbb8..fa0cc8b9 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -27,10 +27,13 @@ import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.command.normal.NormalCmdFactory; import com.arialyy.aria.core.command.normal.AbsNormalCmd; import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.download.DownloadGroupEntity; +import com.arialyy.aria.core.download.DownloadGroupTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadTaskEntity; +import com.arialyy.aria.orm.DbEntity; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; @@ -214,7 +217,7 @@ public class CommonUtil { } /** - * 删除上传任务的配置,包括 + * 删除上传任务的配置 * * @param removeFile {@code true} 不仅删除任务数据库记录,还会删除已经删除完成的文件 * {@code false}如果任务已经完成,只删除任务数据库记录 @@ -236,7 +239,7 @@ public class CommonUtil { } /** - * 删除下载任务的配置,包括 + * 删除下载任务的配置 * * @param removeFile {@code true} 不仅删除任务数据库记录,还会删除已经下载完成的文件 * {@code false}如果任务已经完成,只删除任务数据库记录 diff --git a/README.md b/README.md index 00ea5347..b1476551 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,8 @@ Aria有以下特点: [![Download](https://api.bintray.com/packages/arialyy/maven/AriaApi/images/download.svg)](https://bintray.com/arialyy/maven/AriaApi/_latestVersion) [![Download](https://api.bintray.com/packages/arialyy/maven/AriaCompiler/images/download.svg)](https://bintray.com/arialyy/maven/AriaCompiler/_latestVersion) ```java -compile 'com.arialyy.aria:aria-core:3.2.20' -annotationProcessor 'com.arialyy.aria:aria-compiler:3.2.20' +compile 'com.arialyy.aria:aria-core:3.2.22' +annotationProcessor 'com.arialyy.aria:aria-compiler:3.2.22' ``` *** @@ -84,11 +84,11 @@ protected void onCreate(Bundle savedInstanceState) { ``` [更多注解使用方法](https://github.com/AriaLyy/Aria/wiki/%E6%B3%A8%E8%A7%A3%E4%BD%BF%E7%94%A8) -## [HTTP任务组下载\FTP下载;HTTP\FTP文件上传](https://github.com/AriaLyy/Aria/wiki/Aria%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8) +### [HTTP任务组下载\FTP下载;HTTP\FTP文件上传](https://github.com/AriaLyy/Aria/wiki/Aria%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8) -## [参数配置](https://github.com/AriaLyy/Aria/wiki/Aria%E5%8F%82%E6%95%B0%E9%85%8D%E7%BD%AE) +### [参数配置](https://github.com/AriaLyy/Aria/wiki/Aria%E5%8F%82%E6%95%B0%E9%85%8D%E7%BD%AE) -## [更多说明,见WIKI](https://github.com/AriaLyy/Aria/wiki) +### [更多说明,见WIKI](https://github.com/AriaLyy/Aria/wiki) ## 混淆配置 ``` diff --git a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java index 7c7152e4..c8e2efc9 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -30,6 +30,7 @@ import com.arialyy.annotations.Download; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.download.DownloadTarget; import com.arialyy.aria.core.download.DownloadTask; +import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.util.CommonUtil; import com.arialyy.frame.util.show.L; diff --git a/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java b/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java index bc4afc27..f7279de4 100644 --- a/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java @@ -83,7 +83,7 @@ public class DownloadGroupActivity extends BaseActivity