任务组bug fix
This commit is contained in:
@ -19,6 +19,7 @@ package com.arialyy.aria.core.download;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import com.arialyy.aria.core.inf.AbsNormalEntity;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
import com.arialyy.aria.orm.Primary;
|
||||
|
||||
/**
|
||||
@ -37,9 +38,79 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable {
|
||||
*/
|
||||
private String groupName = "";
|
||||
|
||||
/**
|
||||
* 通过{@link AbsTaskEntity#md5Key}从服务器的返回信息中获取的文件md5信息,如果服务器没有返回,则不会设置该信息
|
||||
* 如果你已经设置了该任务的MD5信息,Aria也不会从服务器返回的信息中获取该信息
|
||||
*/
|
||||
private String md5Code = "";
|
||||
|
||||
/**
|
||||
* 通过{@link AbsTaskEntity#dispositionKey}从服务器的返回信息中获取的文件描述信息
|
||||
*/
|
||||
private String disposition = "";
|
||||
|
||||
/**
|
||||
* 从disposition获取到的文件名,如果可以获取到,则会赋值到这个字段
|
||||
*/
|
||||
private String serverFileName = "";
|
||||
|
||||
public DownloadEntity() {
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
return "DownloadEntity{"
|
||||
+ "downloadUrl='"
|
||||
+ downloadUrl
|
||||
+ '\''
|
||||
+ ", downloadPath='"
|
||||
+ downloadPath
|
||||
+ '\''
|
||||
+ ", isDownloadComplete="
|
||||
+ isDownloadComplete
|
||||
+ ", isRedirect="
|
||||
+ isRedirect
|
||||
+ ", redirectUrl='"
|
||||
+ redirectUrl
|
||||
+ '\''
|
||||
+ ", groupName='"
|
||||
+ groupName
|
||||
+ '\''
|
||||
+ ", md5Code='"
|
||||
+ md5Code
|
||||
+ '\''
|
||||
+ ", disposition='"
|
||||
+ disposition
|
||||
+ '\''
|
||||
+ ", serverFileName='"
|
||||
+ serverFileName
|
||||
+ '\''
|
||||
+ '}';
|
||||
}
|
||||
|
||||
public String getMd5Code() {
|
||||
return md5Code;
|
||||
}
|
||||
|
||||
public void setMd5Code(String md5Code) {
|
||||
this.md5Code = md5Code;
|
||||
}
|
||||
|
||||
public String getDisposition() {
|
||||
return disposition;
|
||||
}
|
||||
|
||||
public void setDisposition(String disposition) {
|
||||
this.disposition = disposition;
|
||||
}
|
||||
|
||||
public String getServerFileName() {
|
||||
return serverFileName;
|
||||
}
|
||||
|
||||
public void setServerFileName(String serverFileName) {
|
||||
this.serverFileName = serverFileName;
|
||||
}
|
||||
|
||||
public String getGroupName() {
|
||||
return groupName;
|
||||
}
|
||||
@ -106,6 +177,9 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable {
|
||||
dest.writeByte(this.isRedirect ? (byte) 1 : (byte) 0);
|
||||
dest.writeString(this.redirectUrl);
|
||||
dest.writeString(this.groupName);
|
||||
dest.writeString(this.md5Code);
|
||||
dest.writeString(this.disposition);
|
||||
dest.writeString(this.serverFileName);
|
||||
}
|
||||
|
||||
protected DownloadEntity(Parcel in) {
|
||||
@ -116,27 +190,9 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable {
|
||||
this.isRedirect = in.readByte() != 0;
|
||||
this.redirectUrl = in.readString();
|
||||
this.groupName = in.readString();
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
return "DownloadEntity{"
|
||||
+ "downloadUrl='"
|
||||
+ downloadUrl
|
||||
+ '\''
|
||||
+ ", downloadPath='"
|
||||
+ downloadPath
|
||||
+ '\''
|
||||
+ ", isDownloadComplete="
|
||||
+ isDownloadComplete
|
||||
+ ", isRedirect="
|
||||
+ isRedirect
|
||||
+ ", redirectUrl='"
|
||||
+ redirectUrl
|
||||
+ '\''
|
||||
+ ", groupName='"
|
||||
+ groupName
|
||||
+ '\''
|
||||
+ '}';
|
||||
this.md5Code = in.readString();
|
||||
this.disposition = in.readString();
|
||||
this.serverFileName = in.readString();
|
||||
}
|
||||
|
||||
public static final Creator<DownloadEntity> CREATOR = new Creator<DownloadEntity>() {
|
||||
|
@ -16,6 +16,7 @@
|
||||
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;
|
||||
@ -29,6 +30,7 @@ import java.util.List;
|
||||
public class DownloadGroupTarget
|
||||
extends AbsGroupTarget<DownloadGroupTarget, DownloadGroupEntity, DownloadGroupTaskEntity> {
|
||||
private List<String> mUrls = new ArrayList<>();
|
||||
private final String TAG = "DownloadGroupTarget";
|
||||
/**
|
||||
* 子任务文件名
|
||||
*/
|
||||
@ -158,7 +160,7 @@ public class DownloadGroupTarget
|
||||
for (int i = 0, len = mUrls.size(); i < len; i++) {
|
||||
DownloadEntity entity = new DownloadEntity();
|
||||
entity.setDownloadUrl(mUrls.get(i));
|
||||
String fileName = mSubTaskFileName.isEmpty() ? CommonUtil.keyToHashKey(mUrls.get(i))
|
||||
String fileName = mSubTaskFileName.isEmpty() ? createFileName(entity.getDownloadUrl())
|
||||
: mSubTaskFileName.get(i);
|
||||
entity.setDownloadPath(mEntity.getDirPath() + "/" + fileName);
|
||||
entity.setGroupName(mGroupName);
|
||||
@ -170,14 +172,5 @@ public class DownloadGroupTarget
|
||||
return list;
|
||||
}
|
||||
|
||||
///**
|
||||
// * 创建文件名,如果url链接有后缀名,则使用url中的后缀名
|
||||
// * @return url 的 hashKey
|
||||
// */
|
||||
//private String createFileName(String url){
|
||||
// if (url.contains(".")){
|
||||
// int s = url.lastIndexOf(".");
|
||||
// String fileName = url.substring()
|
||||
// }
|
||||
//}
|
||||
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ 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;
|
||||
@ -34,6 +35,7 @@ import java.lang.ref.WeakReference;
|
||||
* 任务组任务
|
||||
*/
|
||||
public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, DownloadGroupEntity> {
|
||||
private final String TAG = "DownloadGroupTask";
|
||||
private DListener mListener;
|
||||
private IDownloadUtil mUtil;
|
||||
|
||||
@ -55,7 +57,7 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, Dow
|
||||
}
|
||||
|
||||
@Override public void stop() {
|
||||
mUtil.startDownload();
|
||||
mUtil.stopDownload();
|
||||
}
|
||||
|
||||
@Override public void cancel() {
|
||||
@ -66,6 +68,7 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, Dow
|
||||
* 下载监听类
|
||||
*/
|
||||
private static class DListener extends DownloadListener {
|
||||
private final String TAG = "DListener";
|
||||
WeakReference<Handler> outHandler;
|
||||
WeakReference<DownloadGroupTask> wTask;
|
||||
Context context;
|
||||
@ -92,6 +95,7 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, Dow
|
||||
|
||||
@Override public void onPostPre(long fileSize) {
|
||||
entity.setFileSize(fileSize);
|
||||
entity.setConvertFileSize(CommonUtil.formatFileSize(fileSize));
|
||||
saveData(IEntity.STATE_POST_PRE, -1);
|
||||
sendInState2Target(ISchedulers.POST_PRE);
|
||||
}
|
||||
@ -107,14 +111,15 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, Dow
|
||||
}
|
||||
|
||||
@Override public void onProgress(long currentLocation) {
|
||||
entity.setCurrentProgress(currentLocation);
|
||||
long speed = currentLocation - lastLen;
|
||||
if (isFirst) {
|
||||
speed = 0;
|
||||
isFirst = false;
|
||||
}
|
||||
handleSpeed(speed);
|
||||
lastLen = currentLocation;
|
||||
sendInState2Target(ISchedulers.RUNNING);
|
||||
lastLen = currentLocation;
|
||||
}
|
||||
|
||||
@Override public void onStop(long stopLocation) {
|
||||
|
@ -24,6 +24,8 @@ import com.arialyy.aria.core.scheduler.DownloadGroupSchedulers;
|
||||
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
|
||||
import com.arialyy.aria.core.scheduler.ISchedulerListener;
|
||||
import com.arialyy.aria.core.upload.ProxyHelper;
|
||||
import com.arialyy.aria.orm.DbEntity;
|
||||
import com.arialyy.aria.orm.DbUtil;
|
||||
import com.arialyy.aria.util.CheckUtil;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
@ -136,7 +138,24 @@ public class DownloadReceiver extends AbsReceiver {
|
||||
*/
|
||||
public DownloadEntity getDownloadEntity(String downloadUrl) {
|
||||
CheckUtil.checkDownloadUrl(downloadUrl);
|
||||
return DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl);
|
||||
return DbEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过下载链接获取保存在数据库的下载任务实体
|
||||
*/
|
||||
public DownloadTaskEntity getDownloadTask(String downloadUrl) {
|
||||
CheckUtil.checkDownloadUrl(downloadUrl);
|
||||
return DbEntity.findFirst(DownloadTaskEntity.class, "key=?", downloadUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过下载链接获取保存在数据库的下载任务组实体
|
||||
*/
|
||||
public DownloadGroupTaskEntity getDownlaodGroupTask(List<String> urls) {
|
||||
CheckUtil.checkDownloadUrls(urls);
|
||||
String hashCode = CommonUtil.getMd5Code(urls);
|
||||
return DbEntity.findFirst(DownloadGroupTaskEntity.class, "key=?", hashCode);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -39,7 +39,9 @@ public class DownloadTarget
|
||||
mTaskEntity = DbEntity.findFirst(DownloadTaskEntity.class, "key=?", url);
|
||||
if (mTaskEntity == null) {
|
||||
mTaskEntity = new DownloadTaskEntity();
|
||||
mTaskEntity.entity = new DownloadEntity();
|
||||
mTaskEntity.key = url;
|
||||
mTaskEntity.entity = getEntity(url);
|
||||
mTaskEntity.save();
|
||||
}
|
||||
if (mTaskEntity.entity == null) {
|
||||
mTaskEntity.entity = getEntity(url);
|
||||
@ -57,12 +59,14 @@ public class DownloadTarget
|
||||
DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl);
|
||||
if (entity == null) {
|
||||
entity = new DownloadEntity();
|
||||
entity.setDownloadUrl(downloadUrl);
|
||||
entity.setGroupChild(false);
|
||||
entity.save();
|
||||
}
|
||||
File file = new File(entity.getDownloadPath());
|
||||
if (!file.exists()) {
|
||||
entity.setState(IEntity.STATE_WAIT);
|
||||
}
|
||||
entity.setDownloadUrl(downloadUrl);
|
||||
return entity;
|
||||
}
|
||||
|
||||
|
@ -21,12 +21,11 @@ import android.os.Handler;
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.core.AriaManager;
|
||||
import com.arialyy.aria.core.download.downloader.DownloadListener;
|
||||
import com.arialyy.aria.core.download.downloader.DownloadUtil;
|
||||
import com.arialyy.aria.core.download.downloader.SimpleDownloadUtil;
|
||||
import com.arialyy.aria.core.download.downloader.IDownloadListener;
|
||||
import com.arialyy.aria.core.inf.AbsNormalTask;
|
||||
import com.arialyy.aria.core.inf.IEntity;
|
||||
import com.arialyy.aria.core.scheduler.ISchedulers;
|
||||
import com.arialyy.aria.util.CheckUtil;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import java.io.File;
|
||||
import java.lang.ref.WeakReference;
|
||||
@ -39,7 +38,7 @@ public class DownloadTask extends AbsNormalTask<DownloadEntity> {
|
||||
public static final String TAG = "DownloadTask";
|
||||
|
||||
private IDownloadListener mListener;
|
||||
private DownloadUtil mUtil;
|
||||
private SimpleDownloadUtil mUtil;
|
||||
private boolean isWait = false;
|
||||
|
||||
private DownloadTask(DownloadTaskEntity taskEntity, Handler outHandler) {
|
||||
@ -47,7 +46,7 @@ public class DownloadTask extends AbsNormalTask<DownloadEntity> {
|
||||
mOutHandler = outHandler;
|
||||
mContext = AriaManager.APP;
|
||||
mListener = new DListener(mContext, this, mOutHandler);
|
||||
mUtil = new DownloadUtil(taskEntity, mListener);
|
||||
mUtil = new SimpleDownloadUtil(taskEntity, mListener);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -164,7 +163,6 @@ public class DownloadTask extends AbsNormalTask<DownloadEntity> {
|
||||
String targetName;
|
||||
|
||||
public Builder(String targetName, DownloadTaskEntity taskEntity) {
|
||||
CheckUtil.checkTaskEntity(taskEntity);
|
||||
this.targetName = targetName;
|
||||
this.taskEntity = taskEntity;
|
||||
}
|
||||
@ -226,6 +224,7 @@ public class DownloadTask extends AbsNormalTask<DownloadEntity> {
|
||||
|
||||
@Override public void onPostPre(long fileSize) {
|
||||
entity.setFileSize(fileSize);
|
||||
entity.setConvertFileSize(CommonUtil.formatFileSize(fileSize));
|
||||
sendInState2Target(ISchedulers.POST_PRE);
|
||||
saveData(IEntity.STATE_POST_PRE, -1);
|
||||
}
|
||||
@ -305,7 +304,6 @@ public class DownloadTask extends AbsNormalTask<DownloadEntity> {
|
||||
entity.setDownloadComplete(state == IEntity.STATE_COMPLETE);
|
||||
entity.setCurrentProgress(location);
|
||||
entity.update();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -21,6 +21,7 @@ import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
|
||||
import com.arialyy.aria.core.download.DownloadTaskEntity;
|
||||
import com.arialyy.aria.core.inf.IEntity;
|
||||
import com.arialyy.aria.orm.DbEntity;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@ -40,7 +41,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
||||
* 任务组所有任务总大小
|
||||
*/
|
||||
private long mTotalSize = 0;
|
||||
private long mCurrentProgress = 0;
|
||||
private long mCurrentLocation = 0;
|
||||
private ExecutorService mInfoPool;
|
||||
private ExecutorService mExePool;
|
||||
private IDownloadListener mListener;
|
||||
@ -54,7 +55,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
||||
/**
|
||||
* 初始化失败的任务数
|
||||
*/
|
||||
private int mFailNum = 0;
|
||||
private int mInitFailNum = 0;
|
||||
/**
|
||||
* 保存所有没有下载完成的任务,key为下载地址
|
||||
*/
|
||||
@ -75,20 +76,28 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
||||
*/
|
||||
private SparseArray<FileInfoThread.OnFileInfoCallback> mFileInfoCallbacks = new SparseArray<>();
|
||||
|
||||
//任务总数
|
||||
private int mTaskNum = 0;
|
||||
//已经完成的任务数
|
||||
private int mCompleteNum = 0;
|
||||
//失败的任务数
|
||||
private int mFailNum = 0;
|
||||
|
||||
public DownloadGroupUtil(IDownloadListener listener, DownloadGroupTaskEntity taskEntity) {
|
||||
mListener = listener;
|
||||
mTaskEntity = taskEntity;
|
||||
mInfoPool = Executors.newCachedThreadPool();
|
||||
mExePool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
||||
|
||||
mTaskNum = mTaskEntity.entity.getSubTask().size();
|
||||
for (DownloadEntity entity : mTaskEntity.entity.getSubTask()) {
|
||||
File file = new File(entity.getDownloadPath());
|
||||
if (entity.isDownloadComplete() && file.exists()) {
|
||||
mTotalSize += entity.getFileSize();
|
||||
mCompleteNum++;
|
||||
} else {
|
||||
mExeMap.put(entity.getDownloadUrl(), createDownloadTask(entity));
|
||||
}
|
||||
mCurrentProgress += entity.getCurrentProgress();
|
||||
mCurrentLocation += entity.getCurrentProgress();
|
||||
}
|
||||
}
|
||||
|
||||
@ -97,7 +106,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
||||
}
|
||||
|
||||
@Override public long getCurrentLocation() {
|
||||
return mCurrentProgress;
|
||||
return mCurrentLocation;
|
||||
}
|
||||
|
||||
@Override public boolean isDownloading() {
|
||||
@ -106,6 +115,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
||||
|
||||
@Override public void cancelDownload() {
|
||||
isRunning = false;
|
||||
mListener.onCancel();
|
||||
closeTimer();
|
||||
if (!mInfoPool.isShutdown()) {
|
||||
mInfoPool.shutdown();
|
||||
@ -125,6 +135,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
||||
|
||||
@Override public void stopDownload() {
|
||||
isRunning = false;
|
||||
mListener.onStop(mCurrentLocation);
|
||||
closeTimer();
|
||||
if (!mInfoPool.isShutdown()) {
|
||||
mInfoPool.shutdown();
|
||||
@ -156,6 +167,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
||||
|
||||
@Override public void resumeDownload() {
|
||||
startDownload();
|
||||
mListener.onResume(mCurrentLocation);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -175,7 +187,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
||||
startChildDownload(te);
|
||||
}
|
||||
mInitNum++;
|
||||
if (mInitNum + mFailNum == mTaskEntity.getEntity().getSubTask().size()) {
|
||||
if (mInitNum + mInitFailNum == mTaskEntity.getEntity().getSubTask().size()) {
|
||||
startRunningFlow();
|
||||
}
|
||||
}
|
||||
@ -186,12 +198,15 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
||||
mFailMap.put(url, te);
|
||||
mFileInfoCallbacks.put(te.hashCode(), this);
|
||||
}
|
||||
mFailNum++;
|
||||
failNum++;
|
||||
if (failNum < 10) {
|
||||
mInfoPool.execute(createFileInfoThread(te));
|
||||
mInitFailNum++;
|
||||
//404链接不重试下载
|
||||
if (!errorMsg.contains("错误码:404")) {
|
||||
if (failNum < 10) {
|
||||
mInfoPool.execute(createFileInfoThread(te));
|
||||
}
|
||||
}
|
||||
if (mInitNum + mFailNum == mTaskEntity.getEntity().getSubTask().size()) {
|
||||
failNum++;
|
||||
if (mInitNum + mInitFailNum == mTaskEntity.getEntity().getSubTask().size()) {
|
||||
startRunningFlow();
|
||||
}
|
||||
}
|
||||
@ -213,12 +228,14 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
||||
*/
|
||||
private void startRunningFlow() {
|
||||
mListener.onPostPre(mTotalSize);
|
||||
mListener.onStart(mCurrentProgress);
|
||||
mListener.onStart(mCurrentLocation);
|
||||
closeTimer();
|
||||
mTimer = new Timer(true);
|
||||
mTimer.schedule(new TimerTask() {
|
||||
@Override public void run() {
|
||||
mListener.onProgress(mCurrentProgress);
|
||||
if (mCurrentLocation >= 0) {
|
||||
mListener.onProgress(mCurrentLocation);
|
||||
}
|
||||
}
|
||||
}, 0, 1000);
|
||||
}
|
||||
@ -258,6 +275,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
||||
|
||||
DownloadTaskEntity taskEntity;
|
||||
DownloadEntity entity;
|
||||
long lastLen = 0;
|
||||
|
||||
ChildDownloadListener(DownloadTaskEntity entity) {
|
||||
this.taskEntity = entity;
|
||||
@ -270,6 +288,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
||||
|
||||
@Override public void onPostPre(long fileSize) {
|
||||
entity.setFileSize(fileSize);
|
||||
entity.setConvertFileSize(CommonUtil.formatFileSize(fileSize));
|
||||
saveData(IEntity.STATE_POST_PRE, -1);
|
||||
}
|
||||
|
||||
@ -279,10 +298,12 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
||||
|
||||
@Override public void onStart(long startLocation) {
|
||||
saveData(IEntity.STATE_POST_PRE, IEntity.STATE_RUNNING);
|
||||
lastLen = startLocation;
|
||||
}
|
||||
|
||||
@Override public void onProgress(long currentLocation) {
|
||||
mCurrentProgress += currentLocation;
|
||||
mCurrentLocation += (currentLocation - lastLen);
|
||||
lastLen = currentLocation;
|
||||
}
|
||||
|
||||
@Override public void onStop(long stopLocation) {
|
||||
@ -295,11 +316,27 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
||||
|
||||
@Override public void onComplete() {
|
||||
saveData(IEntity.STATE_COMPLETE, entity.getFileSize());
|
||||
mCompleteNum++;
|
||||
if (mCompleteNum >= mTaskNum) {
|
||||
mListener.onComplete();
|
||||
closeTimer();
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void onFail() {
|
||||
entity.setFailNum(entity.getFailNum() + 1);
|
||||
saveData(IEntity.STATE_FAIL, -1);
|
||||
reTry();
|
||||
}
|
||||
|
||||
/**
|
||||
* 失败后重试下载,如果失败次数超过5次,不再重试
|
||||
*/
|
||||
private void reTry() {
|
||||
if (entity.getFailNum() < 5) {
|
||||
Downloader dt = mDownloaderMap.get(entity.getDownloadUrl());
|
||||
mExePool.execute(dt);
|
||||
}
|
||||
}
|
||||
|
||||
private void saveData(int state, long location) {
|
||||
|
@ -24,6 +24,9 @@ import com.arialyy.aria.util.CommonUtil;
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 下载文件信息获取
|
||||
@ -91,6 +94,19 @@ class FileInfoThread implements Runnable {
|
||||
int len = conn.getContentLength();
|
||||
int code = conn.getResponseCode();
|
||||
boolean isComplete = false;
|
||||
if (TextUtils.isEmpty(mEntity.getMd5Code())) {
|
||||
String md5Code = conn.getHeaderField(mTaskEntity.md5Key);
|
||||
mEntity.setMd5Code(md5Code);
|
||||
}
|
||||
String disposition = conn.getHeaderField(mTaskEntity.dispositionKey);
|
||||
if (!TextUtils.isEmpty(disposition)) {
|
||||
mEntity.setDisposition(disposition);
|
||||
if (disposition.contains(mTaskEntity.dispositionFileKey)) {
|
||||
String[] infos = disposition.split("=");
|
||||
mEntity.setServerFileName(URLDecoder.decode(infos[1], "utf-8"));
|
||||
}
|
||||
}
|
||||
|
||||
mTaskEntity.code = code;
|
||||
if (code == HttpURLConnection.HTTP_PARTIAL) {
|
||||
if (!checkLen(len)) return;
|
||||
@ -107,9 +123,9 @@ class FileInfoThread implements Runnable {
|
||||
} else if (code == HttpURLConnection.HTTP_MOVED_TEMP
|
||||
|| code == HttpURLConnection.HTTP_MOVED_PERM
|
||||
|| code == HttpURLConnection.HTTP_SEE_OTHER) {
|
||||
mTaskEntity.redirectUrlKey = conn.getHeaderField(mTaskEntity.redirectUrlKey);
|
||||
mTaskEntity.redirectUrl = conn.getHeaderField(mTaskEntity.redirectUrlKey);
|
||||
mEntity.setRedirect(true);
|
||||
mEntity.setRedirectUrl(mTaskEntity.redirectUrlKey);
|
||||
mEntity.setRedirectUrl(mTaskEntity.redirectUrl);
|
||||
handle302Turn(conn);
|
||||
} else {
|
||||
failDownload("任务【" + mEntity.getDownloadUrl() + "】下载失败,错误码:" + code);
|
||||
@ -143,6 +159,7 @@ class FileInfoThread implements Runnable {
|
||||
conn.setConnectTimeout(mConnectTimeOut);
|
||||
conn.connect();
|
||||
handleConnect(conn);
|
||||
conn.disconnect();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -16,20 +16,21 @@
|
||||
|
||||
package com.arialyy.aria.core.download.downloader;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.core.download.DownloadTaskEntity;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2015/8/25.
|
||||
* 下载工具类
|
||||
* 简单的下载工具
|
||||
*/
|
||||
public class DownloadUtil implements IDownloadUtil, Runnable {
|
||||
private static final String TAG = "DownloadUtil";
|
||||
public class SimpleDownloadUtil implements IDownloadUtil, Runnable {
|
||||
private static final String TAG = "SimpleDownloadUtil";
|
||||
private IDownloadListener mListener;
|
||||
private Downloader mDT;
|
||||
private DownloadTaskEntity mTaskEntity;
|
||||
|
||||
public DownloadUtil(DownloadTaskEntity entity, IDownloadListener downloadListener) {
|
||||
public SimpleDownloadUtil(DownloadTaskEntity entity, IDownloadListener downloadListener) {
|
||||
mTaskEntity = entity;
|
||||
mListener = downloadListener;
|
||||
mDT = new Downloader(downloadListener, entity);
|
||||
@ -86,14 +87,20 @@ public class DownloadUtil implements IDownloadUtil, Runnable {
|
||||
}
|
||||
|
||||
@Override public void run() {
|
||||
new Thread(new FileInfoThread(mTaskEntity, new FileInfoThread.OnFileInfoCallback() {
|
||||
@Override public void onComplete(String url, int code) {
|
||||
mDT.startDownload();
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
@Override public void onFail(String url, String errorMsg) {
|
||||
failDownload(errorMsg);
|
||||
}
|
||||
})).start();
|
||||
@Override public void onFail(String url, String errorMsg) {
|
||||
failDownload(errorMsg);
|
||||
}
|
||||
})).start();
|
||||
} else {
|
||||
mListener.onPostPre(mTaskEntity.getEntity().getFileSize());
|
||||
new Downloader(mListener, mTaskEntity).startDownload();
|
||||
}
|
||||
}
|
||||
}
|
@ -45,6 +45,11 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
|
||||
* 文件大小
|
||||
*/
|
||||
private long fileSize = 1;
|
||||
/**
|
||||
* 转换后的文件大小
|
||||
*/
|
||||
private String convertFileSize = "";
|
||||
|
||||
private int state = STATE_WAIT;
|
||||
/**
|
||||
* 当前下载进度
|
||||
@ -55,6 +60,14 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
|
||||
*/
|
||||
private long completeTime;
|
||||
|
||||
public String getConvertFileSize() {
|
||||
return convertFileSize;
|
||||
}
|
||||
|
||||
public void setConvertFileSize(String convertFileSize) {
|
||||
this.convertFileSize = convertFileSize;
|
||||
}
|
||||
|
||||
public int getFailNum() {
|
||||
return failNum;
|
||||
}
|
||||
@ -132,6 +145,7 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
|
||||
dest.writeInt(this.failNum);
|
||||
dest.writeString(this.str);
|
||||
dest.writeLong(this.fileSize);
|
||||
dest.writeString(this.convertFileSize);
|
||||
dest.writeInt(this.state);
|
||||
dest.writeLong(this.currentProgress);
|
||||
dest.writeLong(this.completeTime);
|
||||
@ -143,6 +157,7 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
|
||||
this.failNum = in.readInt();
|
||||
this.str = in.readString();
|
||||
this.fileSize = in.readLong();
|
||||
this.convertFileSize = in.readString();
|
||||
this.state = in.readInt();
|
||||
this.currentProgress = in.readLong();
|
||||
this.completeTime = in.readLong();
|
||||
|
@ -16,6 +16,7 @@
|
||||
package com.arialyy.aria.core.inf;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.core.AriaManager;
|
||||
import com.arialyy.aria.core.RequestEnum;
|
||||
@ -178,4 +179,30 @@ public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEnti
|
||||
.setCmd(CommonUtil.createCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_CANCEL))
|
||||
.exe();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建文件名,如果url链接有后缀名,则使用url中的后缀名
|
||||
*
|
||||
* @return url 的 hashKey
|
||||
*/
|
||||
protected String createFileName(String url) {
|
||||
int end = url.indexOf("?");
|
||||
String tempUrl, fileName = "";
|
||||
if (end > 0) {
|
||||
tempUrl = url.substring(0, end);
|
||||
int tempEnd = tempUrl.lastIndexOf("/");
|
||||
if (tempEnd > 0) {
|
||||
fileName = tempUrl.substring(tempEnd + 1, tempUrl.length());
|
||||
}
|
||||
} else {
|
||||
int tempEnd = url.lastIndexOf("/");
|
||||
if (tempEnd > 0) {
|
||||
fileName = url.substring(tempEnd + 1, url.length());
|
||||
}
|
||||
}
|
||||
if (TextUtils.isEmpty(fileName)) {
|
||||
fileName = CommonUtil.keyToHashKey(url);
|
||||
}
|
||||
return fileName;
|
||||
}
|
||||
}
|
||||
|
@ -42,11 +42,31 @@ public abstract class AbsTaskEntity<ENTITY extends AbsEntity> extends DbEntity {
|
||||
*/
|
||||
public RequestEnum requestEnum = RequestEnum.GET;
|
||||
|
||||
/**
|
||||
* 从链接中含有的文件md5码信息所需要的key
|
||||
*/
|
||||
public String md5Key = "Content-MD5";
|
||||
|
||||
/**
|
||||
* 从链接中获取文件描述信息所需要的key
|
||||
*/
|
||||
public String dispositionKey = "Content-Disposition";
|
||||
|
||||
/**
|
||||
* 重定向后,从链接中获取新url所需要的key
|
||||
*/
|
||||
public String redirectUrlKey = "location";
|
||||
|
||||
/**
|
||||
* 从Disposition获取的文件名说需要的key
|
||||
*/
|
||||
public String dispositionFileKey = "attachment;filename";
|
||||
|
||||
/**
|
||||
* 重定向链接
|
||||
*/
|
||||
public String redirectUrl = "";
|
||||
|
||||
/**
|
||||
* 用于判断删除任务时是否需要删除文件{@code true}删除
|
||||
*/
|
||||
|
@ -32,7 +32,7 @@ import java.util.Map;
|
||||
public class DBConfig {
|
||||
static Map<String, Class> mapping = new HashMap<>();
|
||||
static String DB_NAME;
|
||||
static int VERSION = 7;
|
||||
static int VERSION = 8;
|
||||
|
||||
static {
|
||||
if (TextUtils.isEmpty(DB_NAME)) {
|
||||
|
@ -533,9 +533,9 @@ final class SqlHelper extends SQLiteOpenHelper {
|
||||
* @param type {@link DbUtil}
|
||||
*/
|
||||
static void print(int type, String sql) {
|
||||
//if (true) {
|
||||
// return;
|
||||
//}
|
||||
if (true) {
|
||||
return;
|
||||
}
|
||||
String str = "";
|
||||
switch (type) {
|
||||
case CREATE_TABLE:
|
||||
|
@ -357,6 +357,10 @@ public class CommonUtil {
|
||||
List<Field> fields = new ArrayList<>();
|
||||
Class personClazz = clazz.getSuperclass();
|
||||
if (personClazz != null) {
|
||||
Class rootClazz = personClazz.getSuperclass();
|
||||
if (rootClazz != null) {
|
||||
Collections.addAll(fields, rootClazz.getDeclaredFields());
|
||||
}
|
||||
Collections.addAll(fields, personClazz.getDeclaredFields());
|
||||
}
|
||||
Collections.addAll(fields, clazz.getDeclaredFields());
|
||||
|
Reference in New Issue
Block a user