任务组bug修复

This commit is contained in:
AriaLyy
2017-07-12 18:07:48 +08:00
parent 375eb25499
commit 746e8752bd
19 changed files with 157 additions and 160 deletions

View File

@ -27,9 +27,8 @@ import com.arialyy.aria.orm.Primary;
* 下载实体
*/
public class DownloadEntity extends AbsNormalEntity implements Parcelable {
@Primary private String downloadUrl = ""; //下载路径
private String downloadPath = ""; //保存路径
private boolean isDownloadComplete = false; //是否下载完成
private String downloadUrl = ""; //下载路径
@Primary private String downloadPath = ""; //保存路径
private boolean isRedirect = false; //是否重定向
private String redirectUrl = ""; //重定向链接
@ -65,8 +64,6 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable {
+ ", downloadPath='"
+ downloadPath
+ '\''
+ ", isDownloadComplete="
+ isDownloadComplete
+ ", isRedirect="
+ isRedirect
+ ", redirectUrl='"
@ -137,14 +134,6 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable {
return this;
}
public boolean isDownloadComplete() {
return isDownloadComplete;
}
public void setDownloadComplete(boolean downloadComplete) {
isDownloadComplete = downloadComplete;
}
@Override public DownloadEntity clone() throws CloneNotSupportedException {
return (DownloadEntity) super.clone();
}
@ -173,7 +162,6 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable {
super.writeToParcel(dest, flags);
dest.writeString(this.downloadUrl);
dest.writeString(this.downloadPath);
dest.writeByte(this.isDownloadComplete ? (byte) 1 : (byte) 0);
dest.writeByte(this.isRedirect ? (byte) 1 : (byte) 0);
dest.writeString(this.redirectUrl);
dest.writeString(this.groupName);
@ -186,7 +174,6 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable {
super(in);
this.downloadUrl = in.readString();
this.downloadPath = in.readString();
this.isDownloadComplete = in.readByte() != 0;
this.isRedirect = in.readByte() != 0;
this.redirectUrl = in.readString();
this.groupName = in.readString();

View File

@ -16,7 +16,6 @@
package com.arialyy.aria.core.download;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.inf.AbsGroupTarget;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CommonUtil;
@ -171,6 +170,4 @@ public class DownloadGroupTarget
}
return list;
}
}

View File

@ -17,7 +17,6 @@ package com.arialyy.aria.core.download;
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.downloader.DownloadGroupUtil;
import com.arialyy.aria.core.download.downloader.DownloadListener;
@ -57,10 +56,20 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, Dow
}
@Override public void stop() {
if (!mUtil.isDownloading()) {
if (mOutHandler != null) {
mOutHandler.obtainMessage(ISchedulers.STOP, this).sendToTarget();
}
}
mUtil.stopDownload();
}
@Override public void cancel() {
if (!mUtil.isDownloading()) {
if (mOutHandler != null) {
mOutHandler.obtainMessage(ISchedulers.CANCEL, this).sendToTarget();
}
}
mUtil.cancelDownload();
}

View File

@ -36,17 +36,21 @@ public class DownloadTarget
DownloadTarget(String url, String targetName) {
mTargetName = targetName;
mTaskEntity = DbEntity.findFirst(DownloadTaskEntity.class, "key=?", url);
DownloadEntity entity = DbEntity.findFirst(DownloadEntity.class, "downloadUrl=?", url);
if (entity == null) {
entity = getEntity(url);
}
mEntity = entity;
mTaskEntity = DbEntity.findFirst(DownloadTaskEntity.class, "key=?", entity.getDownloadPath());
if (mTaskEntity == null) {
mTaskEntity = new DownloadTaskEntity();
mTaskEntity.key = url;
mTaskEntity.entity = getEntity(url);
mTaskEntity.key = entity.getDownloadPath();
mTaskEntity.entity = entity;
mTaskEntity.save();
}
if (mTaskEntity.entity == null) {
mTaskEntity.entity = getEntity(url);
mTaskEntity.entity = mEntity;
}
mEntity = mTaskEntity.entity;
}
/**
@ -56,7 +60,8 @@ public class DownloadTarget
*/
private DownloadEntity getEntity(String downloadUrl) {
DownloadEntity entity =
DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl);
DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=? and isGroupChild='false'",
downloadUrl);
if (entity == null) {
entity = new DownloadEntity();
entity.setDownloadUrl(downloadUrl);
@ -108,6 +113,8 @@ public class DownloadTarget
File file = new File(downloadPath);
mEntity.setDownloadPath(downloadPath);
mEntity.setFileName(file.getName());
mTaskEntity.key = downloadPath;
mTaskEntity.update();
return this;
}

View File

@ -147,7 +147,7 @@ public class DownloadTask extends AbsNormalTask<DownloadEntity> {
* 取消下载
*/
@Override public void cancel() {
if (!mEntity.isDownloadComplete()) {
if (!mEntity.isComplete()) {
if (!mUtil.isDownloading()) {
if (mOutHandler != null) {
mOutHandler.obtainMessage(ISchedulers.CANCEL, this).sendToTarget();
@ -301,7 +301,7 @@ public class DownloadTask extends AbsNormalTask<DownloadEntity> {
private void saveData(int state, long location) {
entity.setState(state);
entity.setDownloadComplete(state == IEntity.STATE_COMPLETE);
entity.setComplete(state == IEntity.STATE_COMPLETE);
entity.setCurrentProgress(location);
entity.update();
}

View File

@ -24,7 +24,17 @@ import com.arialyy.aria.orm.OneToOne;
*/
public class DownloadTaskEntity extends AbsTaskEntity<DownloadEntity> {
@OneToOne(table = DownloadEntity.class, key = "downloadUrl") public DownloadEntity entity;
@OneToOne(table = DownloadEntity.class, key = "downloadPath") public DownloadEntity entity;
/**
* 所属的任务组组名如果不属于任务组则为null
*/
public String groupName = "";
/**
* 该任务是否属于任务组
*/
public boolean isGroupTask = false;
public DownloadTaskEntity() {
}

View File

@ -24,6 +24,7 @@ import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CommonUtil;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
@ -82,6 +83,10 @@ public class DownloadGroupUtil implements IDownloadUtil {
private int mCompleteNum = 0;
//失败的任务数
private int mFailNum = 0;
/**
* 该任务组对应的所有任务
*/
private Map<String, DownloadTaskEntity> mTasksMap = new HashMap<>();
public DownloadGroupUtil(IDownloadListener listener, DownloadGroupTaskEntity taskEntity) {
mListener = listener;
@ -89,13 +94,20 @@ public class DownloadGroupUtil implements IDownloadUtil {
mInfoPool = Executors.newCachedThreadPool();
mExePool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
mTaskNum = mTaskEntity.entity.getSubTask().size();
List<DownloadTaskEntity> tasks =
DbEntity.findDatas(DownloadTaskEntity.class, "groupName=?", mTaskEntity.key);
if (tasks != null && !tasks.isEmpty()) {
for (DownloadTaskEntity te : tasks) {
mTasksMap.put(te.getEntity().getDownloadUrl(), te);
}
}
for (DownloadEntity entity : mTaskEntity.entity.getSubTask()) {
File file = new File(entity.getDownloadPath());
if (entity.isDownloadComplete() && file.exists()) {
if (entity.isComplete() && file.exists()) {
mTotalSize += entity.getFileSize();
mCompleteNum++;
} else {
mExeMap.put(entity.getDownloadUrl(), createDownloadTask(entity));
mExeMap.put(entity.getDownloadUrl(), createChildDownloadTask(entity));
}
mCurrentLocation += entity.getCurrentProgress();
}
@ -115,8 +127,8 @@ public class DownloadGroupUtil implements IDownloadUtil {
@Override public void cancelDownload() {
isRunning = false;
mListener.onCancel();
closeTimer();
mListener.onCancel();
if (!mInfoPool.isShutdown()) {
mInfoPool.shutdown();
}
@ -131,12 +143,26 @@ public class DownloadGroupUtil implements IDownloadUtil {
dt.cancelDownload();
}
}
delDownloadInfo();
mTaskEntity.deleteData();
}
/**
* 删除所有子任务的下载信息
*/
private void delDownloadInfo() {
List<DownloadTaskEntity> tasks =
DbEntity.findDatas(DownloadTaskEntity.class, "groupName=?", mTaskEntity.key);
if (tasks == null || tasks.isEmpty()) return;
for (DownloadTaskEntity taskEntity : tasks) {
CommonUtil.delDownloadTaskConfig(taskEntity.removeFile, taskEntity);
}
}
@Override public void stopDownload() {
isRunning = false;
mListener.onStop(mCurrentLocation);
closeTimer();
mListener.onStop(mCurrentLocation);
if (!mInfoPool.isShutdown()) {
mInfoPool.shutdown();
}
@ -227,9 +253,9 @@ public class DownloadGroupUtil implements IDownloadUtil {
* 开始进度流程
*/
private void startRunningFlow() {
closeTimer();
mListener.onPostPre(mTotalSize);
mListener.onStart(mCurrentLocation);
closeTimer();
mTimer = new Timer(true);
mTimer.schedule(new TimerTask() {
@Override public void run() {
@ -247,15 +273,15 @@ public class DownloadGroupUtil implements IDownloadUtil {
ChildDownloadListener listener = new ChildDownloadListener(taskEntity);
Downloader dt = new Downloader(listener, taskEntity);
mDownloaderMap.put(taskEntity.getEntity().getDownloadUrl(), dt);
if (mExePool.isShutdown()) return;
mExePool.execute(dt);
}
/**
* 创建子任务下载信息
*/
private DownloadTaskEntity createDownloadTask(DownloadEntity entity) {
DownloadTaskEntity taskEntity =
DbEntity.findFirst(DownloadTaskEntity.class, "key=?", entity.getDownloadUrl());
private DownloadTaskEntity createChildDownloadTask(DownloadEntity entity) {
DownloadTaskEntity taskEntity = mTasksMap.get(entity.getDownloadUrl());
if (taskEntity != null) {
return taskEntity;
}
@ -265,6 +291,10 @@ public class DownloadGroupUtil implements IDownloadUtil {
taskEntity.requestEnum = mTaskEntity.requestEnum;
taskEntity.redirectUrlKey = mTaskEntity.redirectUrlKey;
taskEntity.removeFile = mTaskEntity.removeFile;
taskEntity.groupName = mTaskEntity.key;
taskEntity.isGroupTask = true;
taskEntity.key = entity.getDownloadPath();
taskEntity.save();
return taskEntity;
}
@ -275,6 +305,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
DownloadTaskEntity taskEntity;
DownloadEntity entity;
long lastLen = 0;
ChildDownloadListener(DownloadTaskEntity entity) {
@ -294,6 +325,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
@Override public void onResume(long resumeLocation) {
saveData(IEntity.STATE_POST_PRE, IEntity.STATE_RUNNING);
lastLen = resumeLocation;
}
@Override public void onStart(long startLocation) {
@ -335,13 +367,13 @@ public class DownloadGroupUtil implements IDownloadUtil {
private void reTry() {
if (entity.getFailNum() < 5) {
Downloader dt = mDownloaderMap.get(entity.getDownloadUrl());
mExePool.execute(dt);
dt.startDownload();
}
}
private void saveData(int state, long location) {
entity.setState(state);
entity.setDownloadComplete(state == IEntity.STATE_COMPLETE);
entity.setComplete(state == IEntity.STATE_COMPLETE);
entity.setCurrentProgress(location);
entity.update();
}

View File

@ -84,6 +84,7 @@ class Downloader implements Runnable, IDownloadUtil {
*/
private void startFlow() {
checkTask();
mListener.onPostPre(mEntity.getFileSize());
mConstance.cleanState();
mConstance.isDownloading = true;
try {

View File

@ -90,7 +90,6 @@ public class SimpleDownloadUtil implements IDownloadUtil, Runnable {
if (TextUtils.isEmpty(mTaskEntity.redirectUrl)) {
new Thread(new FileInfoThread(mTaskEntity, new FileInfoThread.OnFileInfoCallback() {
@Override public void onComplete(String url, int code) {
mListener.onPostPre(mTaskEntity.getEntity().getFileSize());
mDT.startDownload();
}
@ -99,7 +98,6 @@ public class SimpleDownloadUtil implements IDownloadUtil, Runnable {
}
})).start();
} else {
mListener.onPostPre(mTaskEntity.getEntity().getFileSize());
new Downloader(mListener, mTaskEntity).startDownload();
}
}

View File

@ -60,6 +60,16 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
*/
private long completeTime;
private boolean isComplete = false;
public boolean isComplete() {
return isComplete;
}
public void setComplete(boolean complete) {
isComplete = complete;
}
public String getConvertFileSize() {
return convertFileSize;
}
@ -149,6 +159,7 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
dest.writeInt(this.state);
dest.writeLong(this.currentProgress);
dest.writeLong(this.completeTime);
dest.writeByte(this.isComplete ? (byte) 1 : (byte) 0);
}
protected AbsEntity(Parcel in) {
@ -161,5 +172,7 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
this.state = in.readInt();
this.currentProgress = in.readLong();
this.completeTime = in.readLong();
this.isComplete = in.readByte() != 0;
}
}

View File

@ -22,13 +22,5 @@ package com.arialyy.aria.core.inf;
public abstract class AbsGroupTarget<TARGET extends AbsGroupTarget, ENTITY extends AbsGroupEntity, TASK_ENTITY extends AbsTaskEntity>
extends AbsTarget<TARGET, ENTITY, TASK_ENTITY> {
/**
* 设置任务组的组名如果不设置任务组Aria会自动将任务组的所有子任务的key相加取md5码作为任务组组名
*
* @param groupName 任务组组名
*/
public TARGET setGroupName(String groupName) {
mEntity.setGroupName(groupName);
return (TARGET) this;
}
}

View File

@ -68,7 +68,8 @@ public abstract class AbsTaskEntity<ENTITY extends AbsEntity> extends DbEntity {
public String redirectUrl = "";
/**
* 用于判断删除任务时是否需要删除文件{@code true}删除
* {@code true} 删除任务数据库记录,并且删除已经下载完成的文件
* {@code false} 如果任务已经完成,只删除任务数据库记录
*/
@Ignore public boolean removeFile = false;

View File

@ -118,7 +118,7 @@ public class DownloadTaskQueue
if (!TextUtils.isEmpty(target)) {
task = (DownloadTask) TaskFactory.getInstance()
.createTask(target, entity, DownloadSchedulers.getInstance());
entity.key = entity.getEntity().getDownloadUrl();
entity.key = entity.getEntity().getDownloadPath();
mCachePool.putTask(task);
} else {
Log.e(TAG, "target name 为 null");

View File

@ -28,15 +28,7 @@ import com.arialyy.aria.orm.Primary;
public class UploadEntity extends AbsNormalEntity implements Parcelable {
@Primary
private String filePath; //文件路径
private boolean isComplete = false;
public boolean isComplete() {
return isComplete;
}
public void setComplete(boolean complete) {
isComplete = complete;
}
public String getFilePath() {
return filePath;
@ -56,13 +48,11 @@ public class UploadEntity extends AbsNormalEntity implements Parcelable {
@Override public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeString(this.filePath);
dest.writeByte(this.isComplete ? (byte) 1 : (byte) 0);
}
protected UploadEntity(Parcel in) {
super(in);
this.filePath = in.readString();
this.isComplete = in.readByte() != 0;
}
@Ignore public static final Creator<UploadEntity> CREATOR = new Creator<UploadEntity>() {

View File

@ -133,16 +133,22 @@ public class CommonUtil {
/**
* 删除上传任务的配置,包括
*
* @param removeFile {@code true} 删除已经上传完成的任务,不仅删除上传记录,还会删除已经上传完成的文件{@code false}
* 如果文件已经上传完成,只删除上传记录
* @param removeFile {@code true} 不仅删除任务数据库记录,还会删除已经下载完成的文件
* {@code false}如果任务已经完成,只删除任务数据库记录
*/
public static void delUploadTaskConfig(boolean removeFile, UploadTaskEntity tEntity) {
UploadEntity uEntity = tEntity.getEntity();
File file = new File(uEntity.getFilePath());
if (removeFile) {
File file = new File(uEntity.getFilePath());
if (file.exists()) {
file.delete();
}
} else {
if (!uEntity.isComplete()) {
if (file.exists()) {
file.delete();
}
}
}
File config = new File(
AriaManager.APP.getFilesDir().getPath() + "/temp/" + uEntity.getFileName() + ".properties");
@ -156,17 +162,24 @@ public class CommonUtil {
/**
* 删除下载任务的配置,包括
*
* @param removeFile{@code true} 删除已经下载完成的任务,不仅删除下载记录,还会删除已经下载完成的文件{@code false}
* 如果文件已经下载完成,只删除下载记录
* @param removeFile {@code true} 不仅删除任务数据库记录,还会删除已经下载完成的文件
* {@code false}如果任务已经完成,只删除任务数据库记录
*/
public static void delDownloadTaskConfig(boolean removeFile, DownloadTaskEntity tEntity) {
DownloadEntity dEntity = tEntity.getEntity();
File file = new File(dEntity.getDownloadPath());
if (removeFile) {
File file = new File(dEntity.getDownloadPath());
if (file.exists()) {
file.delete();
}
} else {
if (!dEntity.isComplete()) {
if (file.exists()) {
file.delete();
}
}
}
File config = new File(
AriaManager.APP.getFilesDir().getPath() + "/temp/" + dEntity.getFileName() + ".properties");
if (config.exists()) {