修复重构loader后ftp模块的bug

pull/617/head
laoyuyu 5 years ago
parent c39bfbdd88
commit e14928e260
  1. 2
      Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java
  2. 3
      Aria/src/main/java/com/arialyy/aria/core/scheduler/TaskSchedulers.java
  3. 4
      HttpComponent/src/main/java/com/arialyy/aria/http/HttpFileInfoTask.java
  4. 2
      HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGInfoTask.java
  5. 4
      M3U8Component/src/main/java/com/arialyy/aria/m3u8/live/LiveStateManager.java
  6. 4
      M3U8Component/src/main/java/com/arialyy/aria/m3u8/vod/VodStateManager.java
  7. 4
      PublicComponent/src/main/java/com/arialyy/aria/core/common/RecordHelper.java
  8. 6
      PublicComponent/src/main/java/com/arialyy/aria/core/config/XMLReader.java
  9. 16
      PublicComponent/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java
  10. 27
      PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoader.java
  11. 7
      PublicComponent/src/main/java/com/arialyy/aria/core/inf/IThreadStateManager.java
  12. 1
      PublicComponent/src/main/java/com/arialyy/aria/core/listener/BaseListener.java
  13. 16
      PublicComponent/src/main/java/com/arialyy/aria/core/listener/DownloadGroupListener.java
  14. 2
      PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsNormalTTBuilder.java
  15. 2
      PublicComponent/src/main/java/com/arialyy/aria/core/loader/IRecordHandler.java
  16. 22
      PublicComponent/src/main/java/com/arialyy/aria/core/loader/NormalLoader.java
  17. 20
      PublicComponent/src/main/java/com/arialyy/aria/core/loader/NormalThreadStateManager.java
  18. 3
      PublicComponent/src/main/java/com/arialyy/aria/core/loader/SubLoader.java
  19. 17
      PublicComponent/src/main/java/com/arialyy/aria/core/task/ThreadTask.java
  20. 32
      PublicComponent/src/main/java/com/arialyy/aria/util/FileUtil.java
  21. 2
      PublicComponent/src/main/java/com/arialyy/aria/util/RecordUtil.java
  22. 2
      app/src/main/java/com/arialyy/simple/DbTestActivity.java
  23. 2
      app/src/main/java/com/arialyy/simple/core/download/DownloadModule.java
  24. 11
      app/src/main/java/com/arialyy/simple/core/download/FtpDownloadActivity.java
  25. 7
      app/src/main/java/com/arialyy/simple/core/download/FtpDownloadModule.java
  26. 2
      app/src/main/java/com/arialyy/simple/core/download/m3u8/M3U8LiveModule.java
  27. 2
      app/src/main/java/com/arialyy/simple/modlue/AnyRunnModule.java
  28. 2
      app/src/main/java/com/arialyy/simple/widget/SubStateLinearLayout.java

@ -311,7 +311,7 @@ public abstract class AbsTaskQueue<TASK extends AbsTask, TASK_WRAPPER extends Ab
task.start(); task.start();
break; break;
case IEntity.STATE_CANCEL: case IEntity.STATE_CANCEL:
ALog.e(TAG, String.format("任务【%s】重试失败,原因:任务已删除", task.getTaskName())); ALog.e(TAG, String.format("任务【%s】重试失败,原因:任务已删除", task.getTaskName()));
break; break;
case IEntity.STATE_COMPLETE: case IEntity.STATE_COMPLETE:
ALog.e(TAG, String.format("任务【%s】重试失败,原因:已完成", task.getTaskName())); ALog.e(TAG, String.format("任务【%s】重试失败,原因:已完成", task.getTaskName()));

@ -37,6 +37,7 @@ import com.arialyy.aria.core.task.DownloadTask;
import com.arialyy.aria.core.task.ITask; import com.arialyy.aria.core.task.ITask;
import com.arialyy.aria.core.task.UploadTask; import com.arialyy.aria.core.task.UploadTask;
import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.util.NetUtils; import com.arialyy.aria.util.NetUtils;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
@ -47,7 +48,7 @@ import java.util.concurrent.ConcurrentHashMap;
* Created by lyy on 2017/6/4. 事件调度器用于处理任务状态的调度 * Created by lyy on 2017/6/4. 事件调度器用于处理任务状态的调度
*/ */
public class TaskSchedulers<TASK extends ITask> implements ISchedulers { public class TaskSchedulers<TASK extends ITask> implements ISchedulers {
private final String TAG = "AbsSchedulers"; private final String TAG = CommonUtil.getClassName(getClass());
private static volatile TaskSchedulers INSTANCE; private static volatile TaskSchedulers INSTANCE;
private static FailureTaskHandler mFailureTaskHandler; private static FailureTaskHandler mFailureTaskHandler;

@ -85,7 +85,7 @@ public final class HttpFileInfoTask implements IInfoTask, Runnable {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
failDownload(new AriaIOException(TAG, failDownload(new AriaIOException(TAG,
String.format("下载失败,filePath: %s, url: %s", mEntity.getDownloadPath(), mEntity.getUrl())), String.format("下载失败,filePath: %s, url: %s", mEntity.getFilePath(), mEntity.getUrl())),
true); true);
} finally { } finally {
if (conn != null) { if (conn != null) {
@ -131,7 +131,7 @@ public final class HttpFileInfoTask implements IInfoTask, Runnable {
if (!FileUtil.checkMemorySpace(mEntity.getFilePath(), len)) { if (!FileUtil.checkMemorySpace(mEntity.getFilePath(), len)) {
failDownload(new TaskException(TAG, failDownload(new TaskException(TAG,
String.format("下载失败,内存空间不足;filePath: %s, url: %s", mEntity.getDownloadPath(), String.format("下载失败,内存空间不足;filePath: %s, url: %s", mEntity.getFilePath(),
mEntity.getUrl())), false); mEntity.getUrl())), false);
return; return;
} }

@ -132,7 +132,7 @@ public final class HttpDGInfoTask implements IInfoTask {
wrapper.getEntity().setFileSize(size); wrapper.getEntity().setFileSize(size);
wrapper.getEntity().update(); wrapper.getEntity().update();
getLenComplete = true; getLenComplete = true;
ALog.d(TAG, String.format("获取组合任务长度完成,组合任务总长度:%s,失败的任务数:%s", size, failCount)); ALog.d(TAG, String.format("获取组合任务长度完成,组合任务总长度:%s,失败的任务数:%s", size, failCount));
callback.onSucceed(wrapper.getKey(), new CompleteInfo()); callback.onSucceed(wrapper.getKey(), new CompleteInfo());
notifyLock(); notifyLock();
} }

@ -144,6 +144,10 @@ final class LiveStateManager implements IThreadStateManager {
return mProgress; return mProgress;
} }
@Override public void updateCurrentProgress(long currentProgress) {
mProgress = currentProgress;
}
@Override public void setLooper(TaskRecord taskRecord, Looper looper) { @Override public void setLooper(TaskRecord taskRecord, Looper looper) {
mLooper = looper; mLooper = looper;
} }

@ -247,6 +247,10 @@ public final class VodStateManager implements IThreadStateManager {
return progress; return progress;
} }
@Override public void updateCurrentProgress(long currentProgress) {
progress = currentProgress;
}
private void printInfo(String tag) { private void printInfo(String tag) {
if (false) { if (false) {
ALog.d(tag, String.format( ALog.d(tag, String.format(

@ -21,6 +21,7 @@ import com.arialyy.aria.core.loader.IRecordHandler;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.BufferedRandomAccessFile; import com.arialyy.aria.util.BufferedRandomAccessFile;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.util.FileUtil; import com.arialyy.aria.util.FileUtil;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -32,7 +33,7 @@ import java.io.IOException;
* @Date 2019-09-19 * @Date 2019-09-19
*/ */
public class RecordHelper { public class RecordHelper {
private String TAG = "RecordHelper"; private String TAG = CommonUtil.getClassName(getClass());
private AbsTaskWrapper mWrapper; private AbsTaskWrapper mWrapper;
protected TaskRecord mTaskRecord; protected TaskRecord mTaskRecord;
@ -166,6 +167,7 @@ public class RecordHelper {
tr.isComplete = false; tr.isComplete = false;
tr.endLocation = mWrapper.getEntity().getFileSize(); tr.endLocation = mWrapper.getEntity().getFileSize();
} else if (file.length() == mWrapper.getEntity().getFileSize()) { } else if (file.length() == mWrapper.getEntity().getFileSize()) {
ALog.d(TAG, "文件长度一致,线程完成");
tr.isComplete = true; tr.isComplete = true;
} else { } else {
if (file.length() != tr.startLocation) { if (file.length() != tr.startLocation) {

@ -27,7 +27,7 @@ import org.xml.sax.helpers.DefaultHandler;
* Created by lyy on 2017/5/22. 读取配置文件 * Created by lyy on 2017/5/22. 读取配置文件
*/ */
public class XMLReader extends DefaultHandler { public class XMLReader extends DefaultHandler {
private final String TAG = "XMLReader"; private final String TAG = CommonUtil.getClassName(this);
private DownloadConfig mDownloadConfig = Configuration.getInstance().downloadCfg; private DownloadConfig mDownloadConfig = Configuration.getInstance().downloadCfg;
private UploadConfig mUploadConfig = Configuration.getInstance().uploadCfg; private UploadConfig mUploadConfig = Configuration.getInstance().uploadCfg;
@ -62,13 +62,13 @@ public class XMLReader extends DefaultHandler {
String value = attributes.getValue("value"); String value = attributes.getValue("value");
switch (qName) { switch (qName) {
case "getCreatedThreadNum": // 线程数 case "threadNum": // 线程数
int threadNum = checkInt(value) ? Integer.parseInt(value) : 3; int threadNum = checkInt(value) ? Integer.parseInt(value) : 3;
if (threadNum < 1) { if (threadNum < 1) {
ALog.w(TAG, "下载线程数不能小于 1"); ALog.w(TAG, "下载线程数不能小于 1");
threadNum = 1; threadNum = 1;
} }
setField("getCreatedThreadNum", threadNum, ConfigType.DOWNLOAD); setField("threadNum", threadNum, ConfigType.DOWNLOAD);
break; break;
case "maxTaskNum": //最大任务书 case "maxTaskNum": //最大任务书
int maxTaskNum = checkInt(value) ? Integer.parseInt(value) : 2; int maxTaskNum = checkInt(value) ? Integer.parseInt(value) : 2;

@ -130,27 +130,11 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable {
this.groupHash = groupHash; this.groupHash = groupHash;
} }
/**
* 后面会删除该方法请使用{@link #getFilePath()}
*/
@Deprecated
public String getDownloadPath() {
return downloadPath;
}
@Override @Override
public String getFilePath() { public String getFilePath() {
return downloadPath; return downloadPath;
} }
/**
* 后面会删除该方法请使用{@link #setFilePath(String)}
*/
@Deprecated
public DownloadEntity setDownloadPath(String downloadPath) {
return setFilePath(downloadPath);
}
public DownloadEntity setFilePath(String filePath) { public DownloadEntity setFilePath(String filePath) {
this.downloadPath = filePath; this.downloadPath = filePath;
return this; return this;

@ -98,14 +98,14 @@ public abstract class AbsGroupLoader implements ILoaderVisitor, ILoader {
private void initState(Looper looper) { private void initState(Looper looper) {
mState = new GroupRunState(getWrapper().getKey(), mListener, mSubQueue); mState = new GroupRunState(getWrapper().getKey(), mListener, mSubQueue);
for (DTaskWrapper wrapper : mGTWrapper.getSubTaskWrapper()) { for (DTaskWrapper wrapper : mGTWrapper.getSubTaskWrapper()) {
File subFile = new File(wrapper.getEntity().getFilePath()); long fileLen = checkFileExists(wrapper.getEntity().getFilePath());
if (wrapper.getEntity().getState() == IEntity.STATE_COMPLETE if (wrapper.getEntity().getState() == IEntity.STATE_COMPLETE
&& subFile.exists() && fileLen != -1
&& subFile.length() == wrapper.getEntity().getFileSize()) { && fileLen == wrapper.getEntity().getFileSize()) {
mState.updateCompleteNum(); mState.updateCompleteNum();
mCurrentLocation += wrapper.getEntity().getFileSize(); mCurrentLocation += wrapper.getEntity().getFileSize();
} else { } else {
if (!subFile.exists()) { if (fileLen == -1) {
wrapper.getEntity().setCurrentProgress(0); wrapper.getEntity().setCurrentProgress(0);
} }
wrapper.getEntity().setState(IEntity.STATE_POST_PRE); wrapper.getEntity().setState(IEntity.STATE_POST_PRE);
@ -120,6 +120,25 @@ public abstract class AbsGroupLoader implements ILoaderVisitor, ILoader {
mScheduler = new Handler(looper, SimpleSchedulers.newInstance(mState, mGTWrapper.getKey())); mScheduler = new Handler(looper, SimpleSchedulers.newInstance(mState, mGTWrapper.getKey()));
} }
/**
* 检查文件是否存在需要检查普通任务和分块任务的
*
* @param filePath 文件路径
* @return 文件存在返回文件长度不存在返回-1
*/
private long checkFileExists(String filePath) {
File temp = new File(filePath);
if (temp.exists()) {
return temp.length();
}
File block = new File(String.format(IRecordHandler.SUB_PATH, filePath, 0));
if (block.exists()) {
return block.length();
} else {
return -1;
}
}
@Override public String getKey() { @Override public String getKey() {
return mGTWrapper.getKey(); return mGTWrapper.getKey();
} }

@ -36,6 +36,7 @@ public interface IThreadStateManager extends ILoaderComponent {
String DATA_ERROR_INFO = "DATA_ERROR_INFO"; String DATA_ERROR_INFO = "DATA_ERROR_INFO";
String DATA_THREAD_NAME = "DATA_THREAD_NAME"; String DATA_THREAD_NAME = "DATA_THREAD_NAME";
String DATA_THREAD_LOCATION = "DATA_THREAD_LOCATION"; String DATA_THREAD_LOCATION = "DATA_THREAD_LOCATION";
String DATA_ADD_LEN = "DATA_ADD_LEN"; // 增加的长度
/** /**
* 任务是否已经失败 * 任务是否已经失败
@ -58,6 +59,12 @@ public interface IThreadStateManager extends ILoaderComponent {
*/ */
long getCurrentProgress(); long getCurrentProgress();
/**
* 更新当前进度
* @param currentProgress 当前进度
*/
void updateCurrentProgress(long currentProgress);
/** /**
* 设置消息循环体 * 设置消息循环体
*/ */

@ -136,7 +136,6 @@ public abstract class BaseListener<ENTITY extends AbsEntity, TASK_WRAPPER extend
mEntity.setConvertSpeed(CommonUtil.formatFileSize(speed < 0 ? 0 : speed) + "/s"); mEntity.setConvertSpeed(CommonUtil.formatFileSize(speed < 0 ? 0 : speed) + "/s");
} }
mEntity.setSpeed(speed < 0 ? 0 : speed); mEntity.setSpeed(speed < 0 ? 0 : speed);
if (mTaskWrapper.getRequestType() != ITaskWrapper.M3U8_VOD) { if (mTaskWrapper.getRequestType() != ITaskWrapper.M3U8_VOD) {
mEntity.setPercent((int) (mEntity.getFileSize() <= 0 ? 0 mEntity.setPercent((int) (mEntity.getFileSize() <= 0 ? 0
: mEntity.getCurrentProgress() * 100 / mEntity.getFileSize())); : mEntity.getCurrentProgress() * 100 / mEntity.getFileSize()));

@ -47,7 +47,7 @@ public class DownloadGroupListener
@Override @Override
public void onSubPre(DownloadEntity subEntity) { public void onSubPre(DownloadEntity subEntity) {
handleSpeed(subEntity, 0); handleSubSpeed(subEntity, 0);
saveSubState(IEntity.STATE_PRE, subEntity); saveSubState(IEntity.STATE_PRE, subEntity);
sendInState2Target(ISchedulers.SUB_PRE, subEntity); sendInState2Target(ISchedulers.SUB_PRE, subEntity);
} }
@ -59,7 +59,7 @@ public class DownloadGroupListener
@Override @Override
public void onSubStart(DownloadEntity subEntity) { public void onSubStart(DownloadEntity subEntity) {
handleSpeed(subEntity, 0); handleSubSpeed(subEntity, 0);
saveSubState(IEntity.STATE_RUNNING, subEntity); saveSubState(IEntity.STATE_RUNNING, subEntity);
sendInState2Target(ISchedulers.SUB_START, subEntity); sendInState2Target(ISchedulers.SUB_START, subEntity);
} }
@ -67,7 +67,7 @@ public class DownloadGroupListener
@Override @Override
public void onSubStop(DownloadEntity subEntity, long stopLocation) { public void onSubStop(DownloadEntity subEntity, long stopLocation) {
subEntity.setCurrentProgress(stopLocation); subEntity.setCurrentProgress(stopLocation);
handleSpeed(subEntity, 0); handleSubSpeed(subEntity, 0);
saveSubState(IEntity.STATE_STOP, subEntity); saveSubState(IEntity.STATE_STOP, subEntity);
saveCurrentLocation(); saveCurrentLocation();
sendInState2Target(ISchedulers.SUB_STOP, subEntity); sendInState2Target(ISchedulers.SUB_STOP, subEntity);
@ -75,7 +75,7 @@ public class DownloadGroupListener
@Override @Override
public void onSubComplete(DownloadEntity subEntity) { public void onSubComplete(DownloadEntity subEntity) {
handleSpeed(subEntity, 0); handleSubSpeed(subEntity, 0);
saveSubState(IEntity.STATE_COMPLETE, subEntity); saveSubState(IEntity.STATE_COMPLETE, subEntity);
saveCurrentLocation(); saveCurrentLocation();
sendInState2Target(ISchedulers.SUB_COMPLETE, subEntity); sendInState2Target(ISchedulers.SUB_COMPLETE, subEntity);
@ -83,7 +83,7 @@ public class DownloadGroupListener
@Override @Override
public void onSubFail(DownloadEntity subEntity, BaseException e) { public void onSubFail(DownloadEntity subEntity, BaseException e) {
handleSpeed(subEntity, 0); handleSubSpeed(subEntity, 0);
saveSubState(IEntity.STATE_FAIL, subEntity); saveSubState(IEntity.STATE_FAIL, subEntity);
saveCurrentLocation(); saveCurrentLocation();
sendInState2Target(ISchedulers.SUB_FAIL, subEntity); sendInState2Target(ISchedulers.SUB_FAIL, subEntity);
@ -95,7 +95,7 @@ public class DownloadGroupListener
@Override @Override
public void onSubCancel(DownloadEntity subEntity) { public void onSubCancel(DownloadEntity subEntity) {
handleSpeed(subEntity, 0); handleSubSpeed(subEntity, 0);
saveSubState(IEntity.STATE_CANCEL, subEntity); saveSubState(IEntity.STATE_CANCEL, subEntity);
saveCurrentLocation(); saveCurrentLocation();
sendInState2Target(ISchedulers.SUB_CANCEL, subEntity); sendInState2Target(ISchedulers.SUB_CANCEL, subEntity);
@ -104,7 +104,7 @@ public class DownloadGroupListener
@Override @Override
public void onSubRunning(DownloadEntity subEntity, long currentProgress) { public void onSubRunning(DownloadEntity subEntity, long currentProgress) {
handleSpeed(subEntity, currentProgress); handleSubSpeed(subEntity, currentProgress);
if (System.currentTimeMillis() - mLastSaveTime >= RUN_SAVE_INTERVAL) { if (System.currentTimeMillis() - mLastSaveTime >= RUN_SAVE_INTERVAL) {
saveSubState(IEntity.STATE_RUNNING, subEntity); saveSubState(IEntity.STATE_RUNNING, subEntity);
mLastSaveTime = System.currentTimeMillis(); mLastSaveTime = System.currentTimeMillis();
@ -112,7 +112,7 @@ public class DownloadGroupListener
sendInState2Target(ISchedulers.SUB_RUNNING, subEntity); sendInState2Target(ISchedulers.SUB_RUNNING, subEntity);
} }
private void handleSpeed(DownloadEntity subEntity, long currentProgress) { private void handleSubSpeed(DownloadEntity subEntity, long currentProgress) {
if (currentProgress == 0){ if (currentProgress == 0){
subEntity.setSpeed(0); subEntity.setSpeed(0);
subEntity.setConvertSpeed("0kb/s"); subEntity.setConvertSpeed("0kb/s");

@ -152,7 +152,7 @@ public abstract class AbsNormalTTBuilder implements IThreadTaskBuilder {
} }
threadTasks.add(task); threadTasks.add(task);
} }
if (currentProgress != 0 && currentProgress != getEntity().getCurrentProgress()) { if (currentProgress != getEntity().getCurrentProgress()) {
ALog.d(TAG, String.format("进度修正,当前进度:%s", currentProgress)); ALog.d(TAG, String.format("进度修正,当前进度:%s", currentProgress));
getEntity().setCurrentProgress(currentProgress); getEntity().setCurrentProgress(currentProgress);
} }

@ -37,7 +37,7 @@ public interface IRecordHandler extends ILoaderComponent {
long SUB_LEN = 1024 * 1024; long SUB_LEN = 1024 * 1024;
/** /**
* 分块文件路径 * 分块文件路径文件路径.线程id.part
*/ */
String SUB_PATH = "%s.%s.part"; String SUB_PATH = "%s.%s.part";

@ -28,6 +28,7 @@ import com.arialyy.aria.core.manager.ThreadTaskManager;
import com.arialyy.aria.core.task.IThreadTask; import com.arialyy.aria.core.task.IThreadTask;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.exception.BaseException; import com.arialyy.aria.exception.BaseException;
import com.arialyy.aria.util.FileUtil;
import java.io.File; import java.io.File;
/** /**
@ -71,17 +72,6 @@ public class NormalLoader extends AbsNormalLoader {
EventMsgUtil.getDefault().unRegister(this); EventMsgUtil.getDefault().unRegister(this);
} }
@Override protected void onPostPre() {
super.onPostPre();
if (getListener() instanceof IDLoadListener) {
((IDLoadListener) getListener()).onPostPre(getEntity().getFileSize());
}
File file = new File(getEntity().getFilePath());
if (file.getParentFile() != null && !file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
}
///** ///**
// * 如果使用"Content-Disposition"中的文件名,需要更新{@link #mTempFile}的路径 // * 如果使用"Content-Disposition"中的文件名,需要更新{@link #mTempFile}的路径
// */ // */
@ -105,11 +95,21 @@ public class NormalLoader extends AbsNormalLoader {
} }
protected void startThreadTask() { protected void startThreadTask() {
if (getListener() instanceof IDLoadListener) {
((IDLoadListener) getListener()).onPostPre(getEntity().getFileSize());
}
File file = new File(getEntity().getFilePath());
if (file.getParentFile() != null && !file.getParentFile().exists()) {
FileUtil.createDir(file.getPath());
}
mRecord = mRecordHandler.getRecord(getFileSize()); mRecord = mRecordHandler.getRecord(getFileSize());
mStateManager.setLooper(mRecord, looper); mStateManager.setLooper(mRecord, looper);
getTaskList().addAll(mTTBuilder.buildThreadTask(mRecord, getTaskList().addAll(mTTBuilder.buildThreadTask(mRecord,
new Handler(looper, mStateManager.getHandlerCallback()))); new Handler(looper, mStateManager.getHandlerCallback())));
startThreadNum = mTTBuilder.getCreatedThreadNum(); startThreadNum = mTTBuilder.getCreatedThreadNum();
mStateManager.updateCurrentProgress(getEntity().getCurrentProgress());
if (mStateManager.getCurrentProgress() > 0) { if (mStateManager.getCurrentProgress() > 0) {
getListener().onResume(mStateManager.getCurrentProgress()); getListener().onResume(mStateManager.getCurrentProgress());
} else { } else {

@ -61,11 +61,11 @@ public class NormalThreadStateManager implements IThreadStateManager {
mLooper = looper; mLooper = looper;
} }
private void checkLooper(){ private void checkLooper() {
if (mTaskRecord == null){ if (mTaskRecord == null) {
throw new NullPointerException("任务记录为空"); throw new NullPointerException("任务记录为空");
} }
if (mLooper == null){ if (mLooper == null) {
throw new NullPointerException("Looper为空"); throw new NullPointerException("Looper为空");
} }
} }
@ -112,9 +112,12 @@ public class NormalThreadStateManager implements IThreadStateManager {
} }
break; break;
case STATE_RUNNING: case STATE_RUNNING:
if (msg.obj instanceof Long) { Bundle b = msg.getData();
mProgress += (long) msg.obj; if (b != null) {
long len = b.getLong(IThreadStateManager.DATA_ADD_LEN, 0);
mProgress += len;
} }
break; break;
case STATE_UPDATE_PROGRESS: case STATE_UPDATE_PROGRESS:
if (msg.obj == null) { if (msg.obj == null) {
@ -128,11 +131,8 @@ public class NormalThreadStateManager implements IThreadStateManager {
} }
}; };
/** @Override public void updateCurrentProgress(long currentProgress) {
* 不要使用handle更新启动线程的进度因为有延迟 mProgress = currentProgress;
*/
void updateProgress(long curProgress) {
mProgress = curProgress;
} }
/** /**

@ -30,6 +30,7 @@ import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.exception.BaseException; import com.arialyy.aria.exception.BaseException;
import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import java.io.File;
import java.util.List; import java.util.List;
/** /**
@ -93,6 +94,8 @@ public final class SubLoader implements ILoader, ILoaderVisitor {
private void handlerTask() { private void handlerTask() {
TaskRecord record = recordHandler.getRecord(wrapper.getEntity().getFileSize()); TaskRecord record = recordHandler.getRecord(wrapper.getEntity().getFileSize());
if (record.threadRecords != null if (record.threadRecords != null
&& !TextUtils.isEmpty(record.filePath)
&& new File(record.filePath).exists()
&& !record.threadRecords.isEmpty() && !record.threadRecords.isEmpty()
&& record.threadRecords.get(0).isComplete) { && record.threadRecords.get(0).isComplete) {
ALog.d(TAG, "子任务已完成,key:" + wrapper.getKey()); ALog.d(TAG, "子任务已完成,key:" + wrapper.getKey());

@ -64,7 +64,7 @@ public class ThreadTask implements IThreadTask, IThreadTaskObserver {
/** /**
* 当前线程的下去区间的进度 * 当前线程的下去区间的进度
*/ */
private long mRangeProgress; private long mRangeProgress, mLastRangeProgress;
private IThreadTaskAdapter mAdapter; private IThreadTaskAdapter mAdapter;
private ThreadRecord mRecord; private ThreadRecord mRecord;
private String mThreadNmae; private String mThreadNmae;
@ -89,6 +89,7 @@ public class ThreadTask implements IThreadTask, IThreadTaskObserver {
isNotNetRetry = AriaConfig.getInstance().getAConfig().isNotNetRetry(); isNotNetRetry = AriaConfig.getInstance().getAConfig().isNotNetRetry();
mRangeProgress = mRecord.startLocation; mRangeProgress = mRecord.startLocation;
mLastRangeProgress = mRangeProgress;
updateInterval = config.updateInterval; updateInterval = config.updateInterval;
} }
@ -304,7 +305,7 @@ public class ThreadTask implements IThreadTask, IThreadTaskObserver {
} }
@Override public synchronized void updateCompleteState() { @Override public synchronized void updateCompleteState() {
ALog.i(TAG, String.format("任务【%s】线程__%s__下载完毕", getTaskWrapper().getKey(), mRecord.threadId)); ALog.i(TAG, String.format("任务【%s】线程__%s__完成", getTaskWrapper().getKey(), mRecord.threadId));
writeConfig(true, mRecord.endLocation); writeConfig(true, mRecord.endLocation);
updateState(IThreadStateManager.STATE_COMPLETE, null); updateState(IThreadStateManager.STATE_COMPLETE, null);
} }
@ -334,9 +335,11 @@ public class ThreadTask implements IThreadTask, IThreadTaskObserver {
msg.setData(b); msg.setData(b);
} }
b.putString(IThreadStateManager.DATA_THREAD_NAME, getThreadName()); b.putString(IThreadStateManager.DATA_THREAD_NAME, getThreadName());
b.putLong(IThreadStateManager.DATA_ADD_LEN, mRangeProgress - mLastRangeProgress);
msg.what = IThreadStateManager.STATE_RUNNING; msg.what = IThreadStateManager.STATE_RUNNING;
msg.obj = mRangeProgress; msg.obj = mRangeProgress;
msg.sendToTarget(); msg.sendToTarget();
mLastRangeProgress = mRangeProgress;
mLastSendProgressTime = System.currentTimeMillis(); mLastSendProgressTime = System.currentTimeMillis();
} }
@ -402,13 +405,13 @@ public class ThreadTask implements IThreadTask, IThreadTaskObserver {
if (mFailTimes < RETRY_NUM && needRetry && (NetUtils.isConnected( if (mFailTimes < RETRY_NUM && needRetry && (NetUtils.isConnected(
AriaConfig.getInstance().getAPP()) AriaConfig.getInstance().getAPP())
|| isNotNetRetry) && !isBreak()) { || isNotNetRetry) && !isBreak()) {
ALog.w(TAG, String.format("ts切片【%s】正在重试", getFileName())); ALog.w(TAG, String.format("ts切片【%s】第%s重试", getFileName(), String.valueOf(mFailTimes)));
mFailTimes++; mFailTimes++;
FileUtil.deleteFile(mConfig.tempFile); FileUtil.deleteFile(mConfig.tempFile);
FileUtil.createFile(mConfig.tempFile); FileUtil.createFile(mConfig.tempFile);
ThreadTaskManager.getInstance().retryThread(this); ThreadTaskManager.getInstance().retryThread(this);
} else { } else {
sendFailMsg(null, needRetry); sendFailMsg(null, false);
} }
} }
@ -425,12 +428,12 @@ public class ThreadTask implements IThreadTask, IThreadTaskObserver {
} }
if (mFailTimes < RETRY_NUM && needRetry && (NetUtils.isConnected( if (mFailTimes < RETRY_NUM && needRetry && (NetUtils.isConnected(
AriaConfig.getInstance().getAPP()) || isNotNetRetry) && !isBreak()) { AriaConfig.getInstance().getAPP()) || isNotNetRetry) && !isBreak()) {
ALog.w(TAG, String.format("分块【%s】正在重试", getFileName())); ALog.w(TAG, String.format("分块【%s】第%s次重试", getFileName(), String.valueOf(mFailTimes)));
mFailTimes++; mFailTimes++;
handleBlockRecord(); handleBlockRecord();
ThreadTaskManager.getInstance().retryThread(this); ThreadTaskManager.getInstance().retryThread(this);
} else { } else {
sendFailMsg(null, needRetry); sendFailMsg(null, false);
} }
} }
@ -493,7 +496,7 @@ public class ThreadTask implements IThreadTask, IThreadTaskObserver {
* 将记录写入到配置文件 * 将记录写入到配置文件
* *
* @param isComplete 当前线程是否完成 {@code true}完成 * @param isComplete 当前线程是否完成 {@code true}完成
* @param record 当前下载进度 * @param record 当前进度
*/ */
private void writeConfig(boolean isComplete, final long record) { private void writeConfig(boolean isComplete, final long record) {
if (mRecord != null) { if (mRecord != null) {

@ -190,6 +190,27 @@ public class FileUtil {
return false; return false;
} }
/**
* 删除文件夹
*/
public static boolean deleteDir(File dirFile) {
// 如果dir对应的文件不存在,则退出
if (!dirFile.exists()) {
return false;
}
if (dirFile.isFile()) {
return dirFile.delete();
} else {
for (File file : dirFile.listFiles()) {
deleteDir(file);
}
}
return dirFile.delete();
}
/** /**
* 将对象写入文件 * 将对象写入文件
* *
@ -275,9 +296,14 @@ public class FileUtil {
FileOutputStream fos = null; FileOutputStream fos = null;
FileChannel foc = null; FileChannel foc = null;
try { try {
if (file.exists() && file.isDirectory()) {
ALog.w(TAG, String.format("路径【%s】是文件夹,将删除该文件夹", targetPath));
FileUtil.deleteDir(file);
}
if (!file.exists()) { if (!file.exists()) {
file.createNewFile(); FileUtil.createFile(file);
} }
fos = new FileOutputStream(targetPath); fos = new FileOutputStream(targetPath);
foc = fos.getChannel(); foc = fos.getChannel();
List<FileInputStream> streams = new LinkedList<>(); List<FileInputStream> streams = new LinkedList<>();
@ -383,8 +409,8 @@ public class FileUtil {
*/ */
public static boolean checkMemorySpace(String path, long fileSize) { public static boolean checkMemorySpace(String path, long fileSize) {
File temp = new File(path); File temp = new File(path);
if (!temp.exists()){ if (!temp.exists()) {
if (!temp.getParentFile().exists()){ if (!temp.getParentFile().exists()) {
FileUtil.createDir(temp.getParentFile().getPath()); FileUtil.createDir(temp.getParentFile().getPath());
} }
path = temp.getParentFile().getPath(); path = temp.getParentFile().getPath();

@ -142,7 +142,7 @@ public class RecordUtil {
int type; int type;
if (entity instanceof DownloadEntity) { if (entity instanceof DownloadEntity) {
type = IRecordHandler.TYPE_DOWNLOAD; type = IRecordHandler.TYPE_DOWNLOAD;
filePath = ((DownloadEntity) entity).getDownloadPath(); filePath = entity.getFilePath();
} else if (entity instanceof UploadEntity) { } else if (entity instanceof UploadEntity) {
type = IRecordHandler.TYPE_UPLOAD; type = IRecordHandler.TYPE_UPLOAD;
filePath = entity.getFilePath(); filePath = entity.getFilePath();

@ -55,7 +55,7 @@ public class DbTestActivity extends BaseActivity<ActivityDbTestBinding> {
DownloadEntity entity = new DownloadEntity(); DownloadEntity entity = new DownloadEntity();
entity.setUrl(url); entity.setUrl(url);
entity.setFileName("ssssssssssssssssss"); entity.setFileName("ssssssssssssssssss");
entity.setDownloadPath(key); entity.setFilePath(key);
DTaskWrapper dte = new DTaskWrapper(entity); DTaskWrapper dte = new DTaskWrapper(entity);

@ -129,7 +129,7 @@ public class DownloadModule extends BaseModule {
DownloadEntity entity = new DownloadEntity(); DownloadEntity entity = new DownloadEntity();
entity.setFileName(name); entity.setFileName(name);
entity.setUrl(downloadUrl); entity.setUrl(downloadUrl);
entity.setDownloadPath(path); entity.setFilePath(path);
return entity; return entity;
} }
} }

@ -28,6 +28,7 @@ import com.arialyy.aria.core.common.FtpOption;
import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.task.DownloadTask; import com.arialyy.aria.core.task.DownloadTask;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import com.arialyy.frame.util.show.L; import com.arialyy.frame.util.show.L;
import com.arialyy.frame.util.show.T; import com.arialyy.frame.util.show.T;
@ -48,7 +49,7 @@ public class FtpDownloadActivity extends BaseActivity<ActivityFtpDownloadBinding
private String mUrl, mFilePath; private String mUrl, mFilePath;
private FtpDownloadModule mModule; private FtpDownloadModule mModule;
private long mTaskId; private long mTaskId;
private String user = "boamax\\update", passw = "2020&suzhou"; private String user = "lao", passw = "123456";
@Override protected void init(Bundle savedInstanceState) { @Override protected void init(Bundle savedInstanceState) {
super.init(savedInstanceState); super.init(savedInstanceState);
@ -124,7 +125,7 @@ public class FtpDownloadActivity extends BaseActivity<ActivityFtpDownloadBinding
private FtpOption getFtpOption() { private FtpOption getFtpOption() {
FtpOption option = new FtpOption(); FtpOption option = new FtpOption();
option.login(user, passw); option.login(user, passw);
option.setServerIdentifier(FtpOption.FTPServerIdentifier.SYST_NT); //option.setServerIdentifier(FtpOption.FTPServerIdentifier.SYST_NT);
//option.setConnectionMode(FtpConnectionMode.DATA_CONNECTION_MODE_ACTIVITY); //option.setConnectionMode(FtpConnectionMode.DATA_CONNECTION_MODE_ACTIVITY);
return option; return option;
} }
@ -145,7 +146,7 @@ public class FtpDownloadActivity extends BaseActivity<ActivityFtpDownloadBinding
} }
@Download.onTaskPre() protected void onTaskPre(DownloadTask task) { @Download.onTaskPre() protected void onTaskPre(DownloadTask task) {
L.d(TAG, "ftp task pre"); L.d(TAG, "ftp task pre, fileSize = " + task.getConvertFileSize());
getBinding().setFileSize(task.getConvertFileSize()); getBinding().setFileSize(task.getConvertFileSize());
} }
@ -154,6 +155,7 @@ public class FtpDownloadActivity extends BaseActivity<ActivityFtpDownloadBinding
} }
@Download.onTaskRunning() protected void running(DownloadTask task) { @Download.onTaskRunning() protected void running(DownloadTask task) {
ALog.d(TAG, "running, p = " + task.getPercent() + ", speed = " + task.getConvertSpeed());
getBinding().setProgress(task.getPercent()); getBinding().setProgress(task.getPercent());
getBinding().setSpeed(task.getConvertSpeed()); getBinding().setSpeed(task.getConvertSpeed());
} }
@ -165,6 +167,7 @@ public class FtpDownloadActivity extends BaseActivity<ActivityFtpDownloadBinding
@Download.onTaskStop() void taskStop(DownloadTask task) { @Download.onTaskStop() void taskStop(DownloadTask task) {
L.d(TAG, "ftp task stop"); L.d(TAG, "ftp task stop");
getBinding().setSpeed(""); getBinding().setSpeed("");
getBinding().setStateStr(getString(R.string.resume));
} }
@Download.onTaskCancel() void taskCancel(DownloadTask task) { @Download.onTaskCancel() void taskCancel(DownloadTask task) {
@ -174,12 +177,14 @@ public class FtpDownloadActivity extends BaseActivity<ActivityFtpDownloadBinding
@Download.onTaskFail() void taskFail(DownloadTask task) { @Download.onTaskFail() void taskFail(DownloadTask task) {
L.d(TAG, "ftp task fail"); L.d(TAG, "ftp task fail");
getBinding().setSpeed("");
getBinding().setStateStr(getString(R.string.resume)); getBinding().setStateStr(getString(R.string.resume));
} }
@Download.onTaskComplete() void taskComplete(DownloadTask task) { @Download.onTaskComplete() void taskComplete(DownloadTask task) {
getBinding().setSpeed(""); getBinding().setSpeed("");
getBinding().setProgress(100); getBinding().setProgress(100);
getBinding().setStateStr(getString(R.string.re_start));
Log.d(TAG, "md5 ==> " + CommonUtil.getFileMD5(new File(task.getFilePath()))); Log.d(TAG, "md5 ==> " + CommonUtil.getFileMD5(new File(task.getFilePath())));
T.showShort(this, "文件:" + task.getEntity().getFileName() + ",下载完成"); T.showShort(this, "文件:" + task.getEntity().getFileName() + ",下载完成");
} }

@ -48,9 +48,8 @@ public class FtpDownloadModule extends BaseViewModule {
*/ */
LiveData<DownloadEntity> getFtpDownloadInfo(Context context) { LiveData<DownloadEntity> getFtpDownloadInfo(Context context) {
//String url = AppUtil.getConfigValue(context, FTP_URL_KEY, ftpDefUrl); //String url = AppUtil.getConfigValue(context, FTP_URL_KEY, ftpDefUrl);
//String url = "ftp://9.9.9.72:2121/Cyberduck-6.9.4.30164.zip"; //String filePath = AppUtil.getConfigValue(context, FTP_PATH_KEY, ftpDefPath);
String url = "ftp://58.210.178.52:21/battery1.0.0.apk"; String url = "ftp://9.9.9.72:2121/Cyberduck-6.9.4.30164.zip";
String filePath = AppUtil.getConfigValue(context, FTP_PATH_KEY, ftpDefPath);
singDownloadInfo = Aria.download(context).getFirstDownloadEntity(url); singDownloadInfo = Aria.download(context).getFirstDownloadEntity(url);
if (singDownloadInfo == null) { if (singDownloadInfo == null) {
@ -58,7 +57,7 @@ public class FtpDownloadModule extends BaseViewModule {
singDownloadInfo.setUrl(url); singDownloadInfo.setUrl(url);
String name = getFileName(ftpDefUrl); String name = getFileName(ftpDefUrl);
singDownloadInfo.setFileName(name); singDownloadInfo.setFileName(name);
singDownloadInfo.setFilePath(filePath + name); singDownloadInfo.setFilePath(ftpDefPath + name);
} else { } else {
AppUtil.setConfigValue(context, FTP_PATH_KEY, singDownloadInfo.getFilePath()); AppUtil.setConfigValue(context, FTP_PATH_KEY, singDownloadInfo.getFilePath());
AppUtil.setConfigValue(context, FTP_URL_KEY, singDownloadInfo.getUrl()); AppUtil.setConfigValue(context, FTP_URL_KEY, singDownloadInfo.getUrl());

@ -55,7 +55,7 @@ public class M3U8LiveModule extends BaseViewModule {
singDownloadInfo.setFilePath(filePath); singDownloadInfo.setFilePath(filePath);
singDownloadInfo.setFileName(temp.getName()); singDownloadInfo.setFileName(temp.getName());
} else { } else {
AppUtil.setConfigValue(context, M3U8_LIVE_PATH_KEY, singDownloadInfo.getDownloadPath()); AppUtil.setConfigValue(context, M3U8_LIVE_PATH_KEY, singDownloadInfo.getFilePath());
AppUtil.setConfigValue(context, M3U8_LIVE_URL_KEY, singDownloadInfo.getUrl()); AppUtil.setConfigValue(context, M3U8_LIVE_URL_KEY, singDownloadInfo.getUrl());
} }
liveData.postValue(singDownloadInfo); liveData.postValue(singDownloadInfo);

@ -76,7 +76,7 @@ public class AnyRunnModule {
} }
@Download.onTaskComplete void taskComplete(DownloadTask task) { @Download.onTaskComplete void taskComplete(DownloadTask task) {
L.d(TAG, "path ==> " + task.getDownloadEntity().getDownloadPath()); L.d(TAG, "path ==> " + task.getDownloadEntity().getFilePath());
L.d(TAG, "md5Code ==> " + CommonUtil.getFileMD5(new File(task.getFilePath()))); L.d(TAG, "md5Code ==> " + CommonUtil.getFileMD5(new File(task.getFilePath())));
} }

@ -74,7 +74,7 @@ public class SubStateLinearLayout extends LinearLayout implements View.OnClickLi
int i = 1; int i = 1;
for (DownloadEntity entity : datas) { for (DownloadEntity entity : datas) {
TextView view = createView(i - 1, entity); TextView view = createView(i - 1, entity);
mPosition.put(entity.getDownloadPath(), i); mPosition.put(entity.getFilePath(), i);
addView(view, i); addView(view, i);
i++; i++;
} }

Loading…
Cancel
Save