AriaLyy 7 years ago
parent 3abb8d84f7
commit 8ac8e22251
  1. 8
      Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java
  2. 11
      Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java
  3. 5
      Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java
  4. 10
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java
  5. 10
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java
  6. 2
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java
  7. 2
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java
  8. 5
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java
  9. 4
      Aria/src/main/java/com/arialyy/aria/core/inf/IEventListener.java
  10. 5
      Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java
  11. 3
      Aria/src/main/java/com/arialyy/aria/core/upload/BaseUListener.java
  12. 2
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadListener.java
  13. 2
      Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java
  14. 11
      app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java

@ -130,7 +130,7 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
}, 0, 1000); }, 0, 1000);
} }
private void closeTimer() { protected void closeTimer() {
if (mTimer != null) { if (mTimer != null) {
mTimer.purge(); mTimer.purge();
mTimer.cancel(); mTimer.cancel();
@ -389,10 +389,4 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
*/ */
protected abstract AbsThreadTask selectThreadTask(SubThreadConfig<TASK_ENTITY> config); protected abstract AbsThreadTask selectThreadTask(SubThreadConfig<TASK_ENTITY> config);
protected void failDownload(String errorMsg) {
closeTimer();
Log.e(TAG, errorMsg);
mConstance.isRunning = false;
mListener.onFail();
}
} }

@ -168,11 +168,11 @@ public abstract class AbsThreadTask<ENTITY extends AbsEntity, TASK_ENTITY extend
writeConfig(false, currentLocation); writeConfig(false, currentLocation);
if (STATE.isFail()) { if (STATE.isFail()) {
Log.e(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】执行失败"); Log.e(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】执行失败");
mListener.onFail(); mListener.onFail(true);
} }
} else { } else {
Log.e(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】执行失败"); Log.e(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】执行失败");
mListener.onFail(); mListener.onFail(true);
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -180,6 +180,13 @@ public abstract class AbsThreadTask<ENTITY extends AbsEntity, TASK_ENTITY extend
} }
} }
/**
* 重试当前线程
*/
private void retryThis(){
}
/** /**
* 将记录写入到配置文件 * 将记录写入到配置文件
*/ */

@ -16,13 +16,11 @@
package com.arialyy.aria.core.download; package com.arialyy.aria.core.download;
import android.os.Handler; import android.os.Handler;
import android.util.Log;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsEntity;
import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.IDownloadListener; import com.arialyy.aria.core.inf.IDownloadListener;
import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.IEventListener;
import com.arialyy.aria.core.scheduler.ISchedulers; import com.arialyy.aria.core.scheduler.ISchedulers;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
@ -105,10 +103,11 @@ class BaseDListener<ENTITY extends AbsEntity, TASK extends AbsTask<ENTITY>>
sendInState2Target(ISchedulers.COMPLETE); sendInState2Target(ISchedulers.COMPLETE);
} }
@Override public void onFail() { @Override public void onFail(boolean needRetry) {
mEntity.setFailNum(mEntity.getFailNum() + 1); mEntity.setFailNum(mEntity.getFailNum() + 1);
saveData(IEntity.STATE_FAIL, mEntity.getCurrentProgress()); saveData(IEntity.STATE_FAIL, mEntity.getCurrentProgress());
handleSpeed(0); handleSpeed(0);
mTask.needRetry = needRetry;
sendInState2Target(ISchedulers.FAIL); sendInState2Target(ISchedulers.FAIL);
} }

@ -103,7 +103,7 @@ abstract class AbsGroupUtil implements IUtil {
mCurrentLocation += entity.getFileSize(); mCurrentLocation += entity.getFileSize();
} else { } else {
mExeMap.put(entity.getUrl(), createChildDownloadTask(entity)); mExeMap.put(entity.getUrl(), createChildDownloadTask(entity));
mCurrentLocation += entity.getCurrentProgress(); mCurrentLocation += file.exists() ? entity.getCurrentProgress() : 0;
mActualTaskNum++; mActualTaskNum++;
} }
mTotalSize += entity.getFileSize(); mTotalSize += entity.getFileSize();
@ -339,19 +339,19 @@ abstract class AbsGroupUtil implements IUtil {
} }
} }
@Override public void onFail() { @Override public void onFail(boolean needRetry) {
entity.setFailNum(entity.getFailNum() + 1); entity.setFailNum(entity.getFailNum() + 1);
saveData(IEntity.STATE_FAIL, lastLen); saveData(IEntity.STATE_FAIL, lastLen);
handleSpeed(0); handleSpeed(0);
reTry(); reTry(needRetry);
} }
/** /**
* 失败后重试下载如果失败次数超过5次不再重试 * 失败后重试下载如果失败次数超过5次不再重试
*/ */
private void reTry() { private void reTry(boolean needRetry) {
synchronized (AriaManager.LOCK) { synchronized (AriaManager.LOCK) {
if (entity.getFailNum() < 5 && isRunning) { if (entity.getFailNum() < 5 && isRunning && needRetry) {
reStartTask(); reStartTask();
} else { } else {
mFailNum++; mFailNum++;

@ -15,6 +15,7 @@
*/ */
package com.arialyy.aria.core.download.downloader; package com.arialyy.aria.core.download.downloader;
import android.util.Log;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.common.AbsFileer; import com.arialyy.aria.core.common.AbsFileer;
import com.arialyy.aria.core.common.AbsThreadTask; import com.arialyy.aria.core.common.AbsThreadTask;
@ -34,6 +35,7 @@ import java.io.IOException;
* 文件下载器 * 文件下载器
*/ */
class Downloader extends AbsFileer<DownloadEntity, DownloadTaskEntity> { class Downloader extends AbsFileer<DownloadEntity, DownloadTaskEntity> {
private String TAG = "Downloader";
Downloader(IDownloadListener listener, DownloadTaskEntity taskEntity) { Downloader(IDownloadListener listener, DownloadTaskEntity taskEntity) {
super(listener, taskEntity); super(listener, taskEntity);
@ -100,4 +102,12 @@ class Downloader extends AbsFileer<DownloadEntity, DownloadTaskEntity> {
} }
return null; return null;
} }
private void failDownload(String errorMsg) {
closeTimer();
Log.e(TAG, errorMsg);
mConstance.isRunning = false;
mListener.onFail(false);
}
} }

@ -47,7 +47,7 @@ public class FtpDirDownloadUtil extends AbsGroupUtil {
} }
@Override public void onFail(String url, String errorMsg) { @Override public void onFail(String url, String errorMsg) {
mListener.onFail(); mListener.onFail(true);
} }
}).start(); }).start();
} }

@ -82,7 +82,7 @@ public class SimpleDownloadUtil implements IUtil, Runnable {
} }
private void failDownload(String msg) { private void failDownload(String msg) {
mListener.onFail(); mListener.onFail(true);
} }
@Override public void run() { @Override public void run() {

@ -23,7 +23,10 @@ import com.arialyy.aria.util.CommonUtil;
* Created by AriaL on 2017/6/29. * Created by AriaL on 2017/6/29.
*/ */
public abstract class AbsTask<ENTITY extends AbsEntity> implements ITask<ENTITY> { public abstract class AbsTask<ENTITY extends AbsEntity> implements ITask<ENTITY> {
/**
* 是否需要重试默认为true
*/
public boolean needRetry = true;
protected ENTITY mEntity; protected ENTITY mEntity;
protected Handler mOutHandler; protected Handler mOutHandler;

@ -58,6 +58,8 @@ public interface IEventListener {
/** /**
* 下载失败 * 下载失败
* @param needRetry 是否需要重试{@code true} 需要
*/ */
void onFail(); void onFail(boolean needRetry);
} }

@ -225,6 +225,11 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
* @param task 下载任务 * @param task 下载任务
*/ */
private void handleFailTask(final TASK task) { private void handleFailTask(final TASK task) {
if (!task.needRetry) {
mQueue.removeTask(task.getEntity());
startNextTask();
return;
}
long interval = 2000; long interval = 2000;
int num = 10; int num = 10;
if (task instanceof DownloadTask) { if (task instanceof DownloadTask) {

@ -92,10 +92,11 @@ class BaseUListener<ENTITY extends AbsEntity, TASK extends AbsTask<ENTITY>>
sendInState2Target(ISchedulers.COMPLETE); sendInState2Target(ISchedulers.COMPLETE);
} }
@Override public void onFail() { @Override public void onFail(boolean needRetry) {
mEntity.setFailNum(mEntity.getFailNum() + 1); mEntity.setFailNum(mEntity.getFailNum() + 1);
saveData(IEntity.STATE_FAIL, mEntity.getCurrentProgress()); saveData(IEntity.STATE_FAIL, mEntity.getCurrentProgress());
handleSpeed(0); handleSpeed(0);
mTask.needRetry = needRetry;
sendInState2Target(ISchedulers.FAIL); sendInState2Target(ISchedulers.FAIL);
} }

@ -49,7 +49,7 @@ class UploadListener implements IUploadListener {
} }
@Override public void onFail() { @Override public void onFail(boolean needRetry) {
} }
} }

@ -98,7 +98,7 @@ class HttpThreadTask extends AbsThreadTask<UploadEntity, UploadTaskEntity> {
private void fail() { private void fail() {
try { try {
mListener.onFail(); mListener.onFail(true);
STATE.isRunning = false; STATE.isRunning = false;
if (mOutputStream != null) { if (mOutputStream != null) {
mOutputStream.close(); mOutputStream.close();

@ -81,15 +81,7 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
Aria.download(this).load(mUrls).stop(); Aria.download(this).load(mUrls).stop();
break; break;
case R.id.cancel: case R.id.cancel:
//Aria.download(this).load(mUrls).cancel(); Aria.download(this).load(mUrls).cancel();
Aria.download(this).removeAllTask(true);
new Handler().postDelayed(new Runnable() {
@Override public void run() {
L.d(TAG,
"size ==> " + Aria.download(DownloadGroupActivity.this).getTotleTaskList().size());
}
}, 1000);
break; break;
} }
} }
@ -114,6 +106,7 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
} }
@DownloadGroup.onTaskRunning() protected void running(DownloadGroupTask task) { @DownloadGroup.onTaskRunning() protected void running(DownloadGroupTask task) {
L.d(TAG, "P ==> " + task.getPercent());
getBinding().setProgress(task.getPercent()); getBinding().setProgress(task.getPercent());
getBinding().setSpeed(task.getConvertSpeed()); getBinding().setSpeed(task.getConvertSpeed());
mChildList.updateChildProgress(task.getEntity().getSubTask()); mChildList.updateChildProgress(task.getEntity().getSubTask());

Loading…
Cancel
Save