Merge branch 'v_3.0'

pull/330/head
AriaLyy 7 years ago
commit 0d95ad7e79
  1. 8
      Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java
  2. 34
      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. 4
      README.md
  15. 11
      app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java
  16. 2
      build.gradle

@ -130,7 +130,7 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
}, 0, 1000);
}
private void closeTimer() {
protected void closeTimer() {
if (mTimer != null) {
mTimer.purge();
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 void failDownload(String errorMsg) {
closeTimer();
Log.e(TAG, errorMsg);
mConstance.isRunning = false;
mListener.onFail();
}
}

@ -28,6 +28,8 @@ import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by lyy on 2017/1/18.
@ -35,6 +37,14 @@ import java.util.Properties;
*/
public abstract class AbsThreadTask<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity<ENTITY>>
implements Runnable {
/**
* 线程重试次数
*/
private final int RETRY_NUM = 5;
/**
* 线程重试间隔
*/
private final int RETRY_INTERVAL = 5000;
private final String TAG = "AbsThreadTask";
protected long mChildCurrentLocation = 0, mSleepTime = 0;
protected int mBufSize;
@ -48,6 +58,8 @@ public abstract class AbsThreadTask<ENTITY extends AbsEntity, TASK_ENTITY extend
* FTP 服务器编码
*/
public static String SERVER_CHARSET = "ISO-8859-1";
private int mFailNum = 0;
private Timer mFailTimer;
protected AbsThreadTask(StateConstance constance, IEventListener listener,
SubThreadConfig<TASK_ENTITY> info) {
@ -168,11 +180,11 @@ public abstract class AbsThreadTask<ENTITY extends AbsEntity, TASK_ENTITY extend
writeConfig(false, currentLocation);
if (STATE.isFail()) {
Log.e(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】执行失败");
mListener.onFail();
mListener.onFail(true);
}
} else {
Log.e(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】执行失败");
mListener.onFail();
mListener.onFail(true);
}
} catch (IOException e) {
e.printStackTrace();
@ -180,6 +192,24 @@ public abstract class AbsThreadTask<ENTITY extends AbsEntity, TASK_ENTITY extend
}
}
/**
* 重试当前线程
*/
private void retryThis(boolean needRetry) {
if (mFailNum < RETRY_NUM && needRetry) {
if (mFailTimer != null){
mFailTimer.purge();
mFailTimer.cancel();
}
mFailTimer = new Timer();
mFailTimer.schedule(new TimerTask() {
@Override public void run() {
AbsThreadTask.this.run();
}
}, RETRY_INTERVAL);
}
}
/**
* 将记录写入到配置文件
*/

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

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

@ -15,6 +15,7 @@
*/
package com.arialyy.aria.core.download.downloader;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.common.AbsFileer;
import com.arialyy.aria.core.common.AbsThreadTask;
@ -34,6 +35,7 @@ import java.io.IOException;
* 文件下载器
*/
class Downloader extends AbsFileer<DownloadEntity, DownloadTaskEntity> {
private String TAG = "Downloader";
Downloader(IDownloadListener listener, DownloadTaskEntity taskEntity) {
super(listener, taskEntity);
@ -100,4 +102,12 @@ class Downloader extends AbsFileer<DownloadEntity, DownloadTaskEntity> {
}
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) {
mListener.onFail();
mListener.onFail(true);
}
}).start();
}

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

@ -23,7 +23,10 @@ import com.arialyy.aria.util.CommonUtil;
* Created by AriaL on 2017/6/29.
*/
public abstract class AbsTask<ENTITY extends AbsEntity> implements ITask<ENTITY> {
/**
* 是否需要重试默认为true
*/
public boolean needRetry = true;
protected ENTITY mEntity;
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 下载任务
*/
private void handleFailTask(final TASK task) {
if (!task.needRetry) {
mQueue.removeTask(task.getEntity());
startNextTask();
return;
}
long interval = 2000;
int num = 10;
if (task instanceof DownloadTask) {

@ -92,10 +92,11 @@ class BaseUListener<ENTITY extends AbsEntity, TASK extends AbsTask<ENTITY>>
sendInState2Target(ISchedulers.COMPLETE);
}
@Override public void onFail() {
@Override public void onFail(boolean needRetry) {
mEntity.setFailNum(mEntity.getFailNum() + 1);
saveData(IEntity.STATE_FAIL, mEntity.getCurrentProgress());
handleSpeed(0);
mTask.needRetry = needRetry;
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() {
try {
mListener.onFail();
mListener.onFail(true);
STATE.isRunning = false;
if (mOutputStream != null) {
mOutputStream.close();

@ -29,8 +29,8 @@ Aria有以下特点:
[![Download](https://api.bintray.com/packages/arialyy/maven/AriaApi/images/download.svg)](https://bintray.com/arialyy/maven/AriaApi/_latestVersion)
[![Download](https://api.bintray.com/packages/arialyy/maven/AriaCompiler/images/download.svg)](https://bintray.com/arialyy/maven/AriaCompiler/_latestVersion)
```java
compile 'com.arialyy.aria:aria-core:3.2.19'
annotationProcessor 'com.arialyy.aria:aria-compiler:3.2.19'
compile 'com.arialyy.aria:aria-core:3.2.20'
annotationProcessor 'com.arialyy.aria:aria-compiler:3.2.20'
```
## 示例

@ -81,15 +81,7 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
Aria.download(this).load(mUrls).stop();
break;
case R.id.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);
Aria.download(this).load(mUrls).cancel();
break;
}
}
@ -114,6 +106,7 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
}
@DownloadGroup.onTaskRunning() protected void running(DownloadGroupTask task) {
L.d(TAG, "P ==> " + task.getPercent());
getBinding().setProgress(task.getPercent());
getBinding().setSpeed(task.getConvertSpeed());
mChildList.updateChildProgress(task.getEntity().getSubTask());

@ -37,7 +37,7 @@ task clean(type: Delete) {
ext {
userOrg = 'arialyy'
groupId = 'com.arialyy.aria'
publishVersion = '3.2.19'
publishVersion = '3.2.20'
repoName='maven'
desc = 'android 下载框架'
website = 'https://github.com/AriaLyy/Aria'

Loading…
Cancel
Save