This commit is contained in:
AriaLyy
2017-08-15 10:41:20 +08:00
parent f5422eb287
commit b7b55e42fc
15 changed files with 96 additions and 27 deletions

View File

@@ -16,16 +16,14 @@
package com.arialyy.aria.core.download;
import android.support.annotation.NonNull;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.command.normal.NormalCmdFactory;
import com.arialyy.aria.core.common.ProxyHelper;
import com.arialyy.aria.core.inf.AbsEntity;
import com.arialyy.aria.core.inf.AbsReceiver;
import com.arialyy.aria.core.inf.IReceiver;
import com.arialyy.aria.core.command.normal.NormalCmdFactory;
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.common.ProxyHelper;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
@@ -53,17 +51,40 @@ public class DownloadReceiver extends AbsReceiver {
/**
* 使用下载实体执行下载操作
*
* @param entity 下载实体
*/
public DownloadTarget load(DownloadEntity entity) {
return new DownloadTarget(entity, targetName);
return load(entity, false);
}
/**
* 使用下载实体执行下载操作
*
* @param refreshInfo 是否刷新下载信息
*/
public DownloadTarget load(DownloadEntity entity, boolean refreshInfo) {
return new DownloadTarget(entity, targetName, refreshInfo);
}
/**
* 加载Http、https单任务下载地址
*
* @param url 下载地址
*/
public DownloadTarget load(@NonNull String url) {
return load(url, false);
}
/**
* 加载Http、https单任务下载地址
*
* @param url 下载地址
* @param refreshInfo 是否刷新下载信息
*/
public DownloadTarget load(@NonNull String url, boolean refreshInfo) {
CheckUtil.checkDownloadUrl(url);
return new DownloadTarget(url, targetName);
return new DownloadTarget(url, targetName, refreshInfo);
}
/**
@@ -78,8 +99,17 @@ public class DownloadReceiver extends AbsReceiver {
* 加载ftp单任务下载地址
*/
public FtpDownloadTarget loadFtp(@NonNull String url) {
return loadFtp(url, false);
}
/**
* 加载ftp单任务下载地址
*
* @param refreshInfo 是否刷新下载信息
*/
public FtpDownloadTarget loadFtp(@NonNull String url, boolean refreshInfo) {
CheckUtil.checkDownloadUrl(url);
return new FtpDownloadTarget(url, targetName);
return new FtpDownloadTarget(url, targetName, refreshInfo);
}
/**

View File

@@ -33,16 +33,26 @@ public class DownloadTarget
protected String url;
DownloadTarget(DownloadEntity entity, String targetName) {
this(entity, targetName, false);
}
DownloadTarget(DownloadEntity entity, String targetName, boolean refreshInfo) {
this.url = entity.getUrl();
mTargetName = targetName;
initTask(entity);
mTaskEntity.refreshInfo = refreshInfo;
}
DownloadTarget(String url, String targetName) {
this(url, targetName, false);
}
DownloadTarget(String url, String targetName, boolean refreshInfo) {
this.url = url;
mTargetName = targetName;
DownloadEntity entity = getEntity(url);
initTask(entity);
mTaskEntity.refreshInfo = refreshInfo;
}
private void initTask(DownloadEntity entity) {

View File

@@ -32,6 +32,10 @@ public class FtpDownloadTarget extends DownloadTarget {
private int port;
FtpDownloadTarget(String url, String targetName) {
this(url, targetName, false);
}
FtpDownloadTarget(String url, String targetName, boolean refreshInfo) {
super(url, targetName);
String[] pp = url.split("/")[2].split(":");
this.serverIp = pp[0];
@@ -45,6 +49,7 @@ public class FtpDownloadTarget extends DownloadTarget {
mTaskEntity.serverIp = serverIp;
mTaskEntity.port = port;
mEntity.setFileName(url.substring(lastIndex + 1, url.length()));
mTaskEntity.refreshInfo = refreshInfo;
}
/**

View File

@@ -40,6 +40,7 @@ abstract class AbsFtpInfoThread<ENTITY extends AbsEntity, TASK_ENTITY extends Ab
protected TASK_ENTITY mTaskEntity;
private int mConnectTimeOut;
private OnFileInfoCallback mCallback;
protected long mSize = 0;
AbsFtpInfoThread(TASK_ENTITY taskEntity, OnFileInfoCallback callback) {
mTaskEntity = taskEntity;
@@ -82,8 +83,7 @@ abstract class AbsFtpInfoThread<ENTITY extends AbsEntity, TASK_ENTITY extends Ab
client.setFileType(FTP.BINARY_FILE_TYPE);
FTPFile[] files =
client.listFiles(new String(remotePath.getBytes(charSet), AbsThreadTask.SERVER_CHARSET));
long size = getFileSize(files, client, remotePath);
mEntity.setFileSize(size);
mSize = getFileSize(files, client, remotePath);
reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();

View File

@@ -40,22 +40,25 @@ class Downloader extends AbsFileer<DownloadEntity, DownloadTaskEntity> {
}
@Override protected void checkTask() {
if (!mTaskEntity.isSupportBP) {
isNewTask = true;
return;
}
mConfigFile = new File(mContext.getFilesDir().getPath()
+ AriaManager.DOWNLOAD_TEMP_DIR
+ mEntity.getFileName()
+ ".properties");
mTempFile = new File(mEntity.getDownloadPath());
if (!mTaskEntity.isSupportBP) {
isNewTask = true;
return;
}
if (mTaskEntity.isNewTask) {
isNewTask = true;
return;
}
if (!mConfigFile.exists()) { //记录文件被删除,则重新下载
isNewTask = true;
CommonUtil.createFile(mConfigFile.getPath());
} else if (!mTempFile.exists()) {
isNewTask = true;
} else if (DbEntity.findFirst(DownloadEntity.class, "url=?", mEntity.getDownloadUrl())
== null) {
} else if (DbEntity.findFirst(DownloadEntity.class, "url=?", mEntity.getUrl()) == null) {
isNewTask = true;
} else {
isNewTask = checkConfigFile();

View File

@@ -28,7 +28,6 @@ import org.apache.commons.net.ftp.FTPFile;
* 获取ftp文件夹信息
*/
class FtpDirInfoThread extends AbsFtpInfoThread<DownloadGroupEntity, DownloadGroupTaskEntity> {
private long mSize = 0;
FtpDirInfoThread(DownloadGroupTaskEntity taskEntity, OnFileInfoCallback callback) {
super(taskEntity, callback);
@@ -36,7 +35,6 @@ class FtpDirInfoThread extends AbsFtpInfoThread<DownloadGroupEntity, DownloadGro
@Override void handleFile(String remotePath, FTPFile ftpFile) {
super.handleFile(remotePath, ftpFile);
mSize += ftpFile.getSize();
addEntity(remotePath, ftpFile);
}

View File

@@ -27,4 +27,12 @@ class FtpFileInfoThread extends AbsFtpInfoThread<DownloadEntity, DownloadTaskEnt
FtpFileInfoThread(DownloadTaskEntity taskEntity, OnFileInfoCallback callback) {
super(taskEntity, callback);
}
@Override protected void onPreComplete() {
super.onPreComplete();
if (mSize != mTaskEntity.getEntity().getFileSize()) {
mTaskEntity.isNewTask = true;
}
mEntity.setFileSize(mSize);
}
}

View File

@@ -25,7 +25,6 @@ import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
/**
* 下载文件信息获取
@@ -147,12 +146,15 @@ class HttpFileInfoThread implements Runnable {
}
/**
* 检查长度是否合法
* 检查长度是否合法,并且检查新获取的文件长度是否和数据库的文件长度一直,如果不一致,则表示该任务为新任务
*
* @param len 从服务器获取的文件长度
* @return true, 合法
* @return {@code true}合法
*/
private boolean checkLen(long len) {
if (len != mEntity.getFileSize()) {
mTaskEntity.isNewTask = true;
}
if (len < 0) {
failDownload("任务【" + mEntity.getUrl() + "】下载失败文件长度小于0");
return false;

View File

@@ -23,7 +23,7 @@ import com.arialyy.aria.core.inf.IDownloadListener;
/**
* Created by lyy on 2015/8/25.
* HTTP单任务下载工具
* HTTP\FTP单任务下载工具
*/
public class SimpleDownloadUtil implements IUtil, Runnable {
private static final String TAG = "SimpleDownloadUtil";
@@ -87,7 +87,7 @@ public class SimpleDownloadUtil implements IUtil, Runnable {
@Override public void run() {
mListener.onPre();
if (mTaskEntity.getEntity().getFileSize() <= 1) {
if (mTaskEntity.getEntity().getFileSize() <= 1 || mTaskEntity.refreshInfo) {
new Thread(createInfoThread()).start();
} else {
mDownloader.start();

View File

@@ -50,6 +50,16 @@ public abstract class AbsTaskEntity<ENTITY extends AbsEntity> extends DbEntity {
@Ignore public String userName, userPw, account, serverIp;
@Ignore public int port;
/**
* 刷新信息 {@code true} 重新刷新下载信息
*/
@Ignore public boolean refreshInfo = false;
/**
* 是否是新任务,{@code true} 新任务
*/
@Ignore public boolean isNewTask = false;
/**
* 请求类型
* {@link AbsTaskEntity#HTTP}、{@link AbsTaskEntity#FTP}