修复暂停后删除任务闪退问题,添加删除记录api

pull/330/head
AriaLyy 8 years ago
parent 9882455cef
commit 9a6e60f67f
  1. 4
      Aria/build.gradle
  2. 7
      Aria/src/main/java/com/arialyy/aria/core/command/AbsCmd.java
  3. 17
      Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java
  4. 22
      Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java
  5. 19
      Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java
  6. 26
      Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java
  7. 28
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java
  8. 3
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java
  9. 11
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java
  10. 2
      Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java
  11. 24
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java
  12. 2
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadUtil.java
  13. 29
      Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java
  14. 3
      README.md
  15. 10
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java

@ -7,8 +7,8 @@ android {
defaultConfig { defaultConfig {
minSdkVersion 9 minSdkVersion 9
targetSdkVersion 23 targetSdkVersion 23
versionCode 101 versionCode 102
versionName "3.0.2" versionName "3.0.3"
} }
buildTypes { buildTypes {
release { release {

@ -35,12 +35,17 @@ public abstract class AbsCmd<T extends ITaskEntity> implements ICmd {
T mEntity; T mEntity;
String TAG; String TAG;
String mTargetName; String mTargetName;
/**
* 能否执行命令
*/
boolean cancelExe = true;
/** /**
* @param targetName 产生任务的对象名 * @param targetName 产生任务的对象名
*/ */
AbsCmd(String targetName, T entity) { AbsCmd(String targetName, T entity) {
CheckUtil.checkCmdEntity(entity); cancelExe = CheckUtil.checkCmdEntity(entity,
!(this instanceof CancelCmd) || !(this instanceof StopCmd));
mTargetName = targetName; mTargetName = targetName;
mEntity = entity; mEntity = entity;
TAG = CommonUtil.getClassName(this); TAG = CommonUtil.getClassName(this);

@ -31,6 +31,7 @@ class AddCmd<T extends ITaskEntity> extends AbsCmd<T> {
} }
@Override public void executeCmd() { @Override public void executeCmd() {
if (!cancelExe) return;
ITask task = mQueue.getTask(mEntity.getEntity()); ITask task = mQueue.getTask(mEntity.getEntity());
if (task == null) { if (task == null) {
mQueue.createTask(mTargetName, mEntity); mQueue.createTask(mTargetName, mEntity);
@ -39,20 +40,4 @@ class AddCmd<T extends ITaskEntity> extends AbsCmd<T> {
} }
} }
//AddCmd(DownloadTaskEntity entity) {
// super(entity);
//}
//
//AddCmd(String targetName, DownloadTaskEntity entity) {
// super(targetName, entity);
//}
//
//@Override public void executeCmd() {
// DownloadTask task = mQueue.getTask(mEntity.downloadEntity);
// if (task == null) {
// mQueue.createTask(mTargetName, mEntity);
// } else {
// Log.w(TAG, "添加命令执行失败,【该任务已经存在】");
// }
//}
} }

@ -29,6 +29,7 @@ class CancelCmd<T extends ITaskEntity> extends AbsCmd<T> {
} }
@Override public void executeCmd() { @Override public void executeCmd() {
if (!cancelExe) return;
ITask task = mQueue.getTask(mEntity.getEntity()); ITask task = mQueue.getTask(mEntity.getEntity());
if (task == null) { if (task == null) {
task = mQueue.createTask(mTargetName, mEntity); task = mQueue.createTask(mTargetName, mEntity);
@ -40,25 +41,4 @@ class CancelCmd<T extends ITaskEntity> extends AbsCmd<T> {
mQueue.cancelTask(task); mQueue.cancelTask(task);
} }
} }
//CancelCmd(DownloadTaskEntity entity) {
// super(entity);
//}
//
//CancelCmd(String targetName, DownloadTaskEntity entity) {
// super(targetName, entity);
//}
//
//@Override public void executeCmd() {
// DownloadTask task = mQueue.getTask(mEntity.downloadEntity);
// if (task == null) {
// task = mQueue.createTask(mTargetName, mEntity);
// }
// if (task != null) {
// if (mTargetName != null) {
// task.setTargetName(mTargetName);
// }
// mQueue.cancelTask(task);
// }
//}
} }

@ -31,6 +31,7 @@ class StartCmd<T extends ITaskEntity> extends AbsCmd<T> {
} }
@Override public void executeCmd() { @Override public void executeCmd() {
if (!cancelExe) return;
ITask task = mQueue.getTask(mEntity.getEntity()); ITask task = mQueue.getTask(mEntity.getEntity());
if (task == null) { if (task == null) {
task = mQueue.createTask(mTargetName, mEntity); task = mQueue.createTask(mTargetName, mEntity);
@ -41,22 +42,4 @@ class StartCmd<T extends ITaskEntity> extends AbsCmd<T> {
} }
} }
//StartCmd(DownloadTaskEntity entity) {
// super(entity);
//}
//
//StartCmd(String targetName, DownloadTaskEntity entity) {
// super(targetName, entity);
//}
//
//@Override public void executeCmd() {
// DownloadTask task = mQueue.getTask(mEntity.downloadEntity);
// if (task == null) {
// task = mQueue.createTask(mTargetName, mEntity);
// }
// if (task != null) {
// task.setTargetName(mTargetName);
// mQueue.startTask(task);
// }
//}
} }

@ -33,6 +33,7 @@ class StopCmd<T extends ITaskEntity> extends AbsCmd<T> {
} }
@Override public void executeCmd() { @Override public void executeCmd() {
if (!cancelExe) return;
ITask task = mQueue.getTask(mEntity.getEntity()); ITask task = mQueue.getTask(mEntity.getEntity());
if (task == null) { if (task == null) {
if (mEntity.getEntity().getState() == IEntity.STATE_RUNNING) { if (mEntity.getEntity().getState() == IEntity.STATE_RUNNING) {
@ -48,29 +49,4 @@ class StopCmd<T extends ITaskEntity> extends AbsCmd<T> {
mQueue.stopTask(task); mQueue.stopTask(task);
} }
} }
//StopCmd(DownloadTaskEntity entity) {
// super(entity);
//}
//
//StopCmd(String targetName, DownloadTaskEntity entity) {
// super(targetName, entity);
//}
//
//@Override public void executeCmd() {
// DownloadTask task = mQueue.getTask(mEntity.downloadEntity);
// if (task == null) {
// if (mEntity.downloadEntity.getState() == DownloadEntity.STATE_RUNNING) {
// task = mQueue.createTask(mTargetName, mEntity);
// mQueue.stopTask(task);
// } else {
// Log.w(TAG, "停止命令执行失败,【调度器中没有该任务】");
// }
// } else {
// if (!TextUtils.isEmpty(mTargetName)) {
// task.setTargetName(mTargetName);
// }
// mQueue.stopTask(task);
// }
//}
} }

@ -137,6 +137,10 @@ public class DownloadTask implements ITask {
this.mTargetName = targetName; this.mTargetName = targetName;
} }
@Override public void removeRecord() {
mEntity.deleteData();
}
/** /**
* 停止下载 * 停止下载
*/ */
@ -161,10 +165,7 @@ public class DownloadTask implements ITask {
* 取消下载 * 取消下载
*/ */
@Override public void cancel() { @Override public void cancel() {
if (mUtil.isDownloading()) { if (!mEntity.isDownloadComplete()) {
mUtil.cancelDownload();
} else {
// 如果任务不是下载状态
mUtil.cancelDownload(); mUtil.cancelDownload();
mUtil.delConfigFile(); mUtil.delConfigFile();
mUtil.delTempFile(); mUtil.delTempFile();
@ -177,6 +178,22 @@ public class DownloadTask implements ITask {
intent.putExtra(Aria.ENTITY, mEntity); intent.putExtra(Aria.ENTITY, mEntity);
mContext.sendBroadcast(intent); mContext.sendBroadcast(intent);
} }
//if (mEntity.isDownloadComplete()) {
// //mUtil.cancelDownload();
//} else {
// // 如果任务不是下载状态
// mUtil.cancelDownload();
// mUtil.delConfigFile();
// mUtil.delTempFile();
// mEntity.deleteData();
// if (mOutHandler != null) {
// mOutHandler.obtainMessage(DownloadSchedulers.CANCEL, this).sendToTarget();
// }
// //发送取消下载的广播
// Intent intent = CommonUtil.createIntent(mContext.getPackageName(), Aria.ACTION_CANCEL);
// intent.putExtra(Aria.ENTITY, mEntity);
// mContext.sendBroadcast(intent);
//}
} }
public static class Builder { public static class Builder {
@ -186,7 +203,8 @@ public class DownloadTask implements ITask {
String targetName; String targetName;
public Builder(String targetName, DownloadTaskEntity taskEntity) { public Builder(String targetName, DownloadTaskEntity taskEntity) {
CheckUtil.checkDownloadTaskEntity(taskEntity.downloadEntity); //CheckUtil.checkDownloadTaskEntity(taskEntity.downloadEntity);
CheckUtil.checkTaskEntity(taskEntity);
this.targetName = targetName; this.targetName = targetName;
this.taskEntity = taskEntity; this.taskEntity = taskEntity;
} }

@ -65,7 +65,8 @@ public class DownloadUtil implements IDownloadUtil, Runnable {
DownloadUtil(Context context, DownloadTaskEntity entity, IDownloadListener downloadListener, DownloadUtil(Context context, DownloadTaskEntity entity, IDownloadListener downloadListener,
int threadNum) { int threadNum) {
CheckUtil.checkDownloadTaskEntity(entity.downloadEntity); //CheckUtil.checkDownloadTaskEntity(entity.downloadEntity);
CheckUtil.checkTaskEntity(entity);
mDownloadEntity = entity.downloadEntity; mDownloadEntity = entity.downloadEntity;
mContext = context.getApplicationContext(); mContext = context.getApplicationContext();
mDownloadTaskEntity = entity; mDownloadTaskEntity = entity;

@ -51,6 +51,17 @@ public class AbsTarget<ENTITY extends IEntity, TASK_ENTITY extends ITaskEntity>
taskEntity.redirectUrlKey = redirectUrlKey; taskEntity.redirectUrlKey = redirectUrlKey;
} }
/**
* 删除记录
*/
public void removeRecord() {
if (entity instanceof DownloadEntity) {
((DownloadEntity) entity).deleteData();
} else if (entity instanceof UploadEntity) {
((UploadEntity) entity).deleteData();
}
}
/** /**
* 获取任务文件大小 * 获取任务文件大小
* *

@ -51,4 +51,6 @@ public interface ITask {
public long getCurrentProgress(); public long getCurrentProgress();
public void setTargetName(String targetName); public void setTargetName(String targetName);
public void removeRecord();
} }

@ -55,6 +55,10 @@ public class UploadTask implements ITask {
mTargetName = targetName; mTargetName = targetName;
} }
@Override public void removeRecord() {
mUploadEntity.deleteData();
}
@Override public String getKey() { @Override public String getKey() {
return mUploadEntity.getFilePath(); return mUploadEntity.getFilePath();
} }
@ -87,9 +91,8 @@ public class UploadTask implements ITask {
} }
@Override public void cancel() { @Override public void cancel() {
if (mUtil.isRunning()) {
mUtil.cancel(); if (!mUploadEntity.isComplete()) {
} else {
// 如果任务不是下载状态 // 如果任务不是下载状态
mUtil.cancel(); mUtil.cancel();
mUploadEntity.deleteData(); mUploadEntity.deleteData();
@ -101,6 +104,21 @@ public class UploadTask implements ITask {
intent.putExtra(Aria.ENTITY, mUploadEntity); intent.putExtra(Aria.ENTITY, mUploadEntity);
AriaManager.APP.sendBroadcast(intent); AriaManager.APP.sendBroadcast(intent);
} }
//if (mUtil.isRunning()) {
// mUtil.cancel();
//} else {
// // 如果任务不是下载状态
// mUtil.cancel();
// mUploadEntity.deleteData();
// if (mOutHandler != null) {
// mOutHandler.obtainMessage(DownloadSchedulers.CANCEL, this).sendToTarget();
// }
// //发送取消下载的广播
// Intent intent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), Aria.ACTION_CANCEL);
// intent.putExtra(Aria.ENTITY, mUploadEntity);
// AriaManager.APP.sendBroadcast(intent);
//}
} }
public String getTargetName() { public String getTargetName() {

@ -50,7 +50,7 @@ final class UploadUtil implements Runnable {
UploadUtil(UploadTaskEntity taskEntity, IUploadListener listener) { UploadUtil(UploadTaskEntity taskEntity, IUploadListener listener) {
mTaskEntity = taskEntity; mTaskEntity = taskEntity;
CheckUtil.checkUploadTaskEntity(taskEntity.uploadEntity); CheckUtil.checkTaskEntity(taskEntity);
mUploadEntity = taskEntity.uploadEntity; mUploadEntity = taskEntity.uploadEntity;
if (listener == null) { if (listener == null) {
throw new IllegalArgumentException("上传监听不能为空"); throw new IllegalArgumentException("上传监听不能为空");

@ -17,6 +17,7 @@
package com.arialyy.aria.util; package com.arialyy.aria.util;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.download.DownloadEntity; 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.ITaskEntity; import com.arialyy.aria.core.inf.ITaskEntity;
@ -95,31 +96,39 @@ public class CheckUtil {
/** /**
* 检查命令实体 * 检查命令实体
*
* @param checkPath 删除命令不需要检查下载路径和文件名
*/ */
public static void checkCmdEntity(ITaskEntity entity) { public static boolean checkCmdEntity(ITaskEntity entity, boolean checkPath) {
boolean b = false;
if (entity instanceof DownloadTaskEntity) { if (entity instanceof DownloadTaskEntity) {
DownloadEntity entity1 = ((DownloadTaskEntity) entity).downloadEntity; DownloadEntity entity1 = ((DownloadTaskEntity) entity).downloadEntity;
if (entity1 == null) { if (entity1 == null) {
throw new NullPointerException("下载实体不能为空"); Log.e(TAG, "下载实体不能为空");
} else if (TextUtils.isEmpty(entity1.getDownloadUrl())) { } else if (checkPath && TextUtils.isEmpty(entity1.getDownloadUrl())) {
throw new IllegalArgumentException("下载链接不能为空"); Log.e(TAG, "下载链接不能为空");
} else if (TextUtils.isEmpty(entity1.getDownloadPath())) { } else if (checkPath && TextUtils.isEmpty(entity1.getDownloadPath())) {
throw new IllegalArgumentException("保存路径不能为空"); Log.e(TAG, "保存路径不能为空");
} else {
b = true;
} }
} else if (entity instanceof UploadTaskEntity) { } else if (entity instanceof UploadTaskEntity) {
UploadEntity entity1 = ((UploadTaskEntity) entity).uploadEntity; UploadEntity entity1 = ((UploadTaskEntity) entity).uploadEntity;
if (entity1 == null) { if (entity1 == null) {
throw new NullPointerException("上传实体不能为空"); Log.e(TAG, "上传实体不能为空");
} else if (TextUtils.isEmpty(entity1.getFilePath())) { } else if (TextUtils.isEmpty(entity1.getFilePath())) {
throw new IllegalArgumentException("上传文件路径不能为空"); Log.e(TAG, "上传文件路径不能为空");
} else {
b = true;
} }
} }
return b;
} }
/** /**
* 检查上传实体是否合法 * 检查上传实体是否合法
*/ */
public static void checkUploadTaskEntity(UploadEntity entity) { private static void checkUploadTaskEntity(UploadEntity entity) {
if (entity == null) { if (entity == null) {
throw new NullPointerException("上传实体不能为空"); throw new NullPointerException("上传实体不能为空");
} else if (TextUtils.isEmpty(entity.getFilePath())) { } else if (TextUtils.isEmpty(entity.getFilePath())) {
@ -135,7 +144,7 @@ public class CheckUtil {
* *
* @param entity 下载实体 * @param entity 下载实体
*/ */
public static void checkDownloadTaskEntity(DownloadEntity entity) { private static void checkDownloadTaskEntity(DownloadEntity entity) {
if (entity == null) { if (entity == null) {
throw new NullPointerException("下载实体不能为空"); throw new NullPointerException("下载实体不能为空");
} else if (TextUtils.isEmpty(entity.getDownloadUrl())) { } else if (TextUtils.isEmpty(entity.getDownloadUrl())) {

@ -20,7 +20,7 @@ Aria怎样使用?
## 下载 ## 下载
[![Download](https://api.bintray.com/packages/arialyy/maven/Aria/images/download.svg)](https://bintray.com/arialyy/maven/Aria/_latestVersion)</br> [![Download](https://api.bintray.com/packages/arialyy/maven/Aria/images/download.svg)](https://bintray.com/arialyy/maven/Aria/_latestVersion)</br>
```java ```java
compile 'com.arialyy.aria:Aria:3.0.2' compile 'com.arialyy.aria:Aria:3.0.3'
``` ```
## 示例 ## 示例
@ -143,6 +143,7 @@ compile 'com.arialyy.aria:Aria:3.0.2'
*** ***
## 开发日志 ## 开发日志
+ v_3.0.3 修复暂停后删除任务,闪退问题,添加删除记录的api
+ v_3.0.2 支持30x重定向链接下载 + v_3.0.2 支持30x重定向链接下载
+ v_3.0.0 添加上传任务支持,修复一些已发现的bug + v_3.0.0 添加上传任务支持,修复一些已发现的bug
+ v_2.4.4 修复不支持断点的下载链接拿不到文件大小的问题 + v_2.4.4 修复不支持断点的下载链接拿不到文件大小的问题

@ -55,10 +55,11 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
private static final String DOWNLOAD_URL = private static final String DOWNLOAD_URL =
//"http://kotlinlang.org/docs/kotlin-docs.pdf"; //"http://kotlinlang.org/docs/kotlin-docs.pdf";
//"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe"; //"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";
//不支持断点的链接 //"http://static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk";
//"http://ox.konsung.net:5555/ksdc-web/download/downloadFile/?fileName=ksdc_1.0.2.apk&rRange=0-"; //不支持断点的链接
"http://172.18.104.50:8080/download/_302turn"; //"http://ox.konsung.net:5555/ksdc-web/download/downloadFile/?fileName=ksdc_1.0.2.apk&rRange=0-";
//"http://172.18.104.50:8080/download/_302turn";
@Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb; @Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb;
@Bind(R.id.start) Button mStart; @Bind(R.id.start) Button mStart;
@Bind(R.id.stop) Button mStop; @Bind(R.id.stop) Button mStop;
@ -233,6 +234,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
private void stop() { private void stop() {
Aria.download(this).load(DOWNLOAD_URL).pause(); Aria.download(this).load(DOWNLOAD_URL).pause();
//Aria.download(this).load(DOWNLOAD_URL).removeRecord();
} }
private void cancel() { private void cancel() {

Loading…
Cancel
Save