This commit is contained in:
AriaLyy
2016-12-06 18:16:31 +08:00
parent 6d696ccfa2
commit c1de3b8419
12 changed files with 88 additions and 90 deletions

View File

@ -22,9 +22,7 @@ import android.app.Application;
import android.app.Fragment;
import android.app.Service;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Build;
import com.bumptech.glide.Glide;
/**
* Created by lyy on 2016/12/1.

View File

@ -75,6 +75,7 @@ public class DownloadEntity extends DbEntity implements Parcelable {
}
};
@Ignore private long speed = 0; //下载速度
@Ignore private int failNum = 0;
private String downloadUrl = ""; //下载路径
private String downloadPath = ""; //保存路径
private String fileName = ""; //文件名
@ -83,7 +84,6 @@ public class DownloadEntity extends DbEntity implements Parcelable {
private int state = STATE_WAIT;
private boolean isDownloadComplete = false; //是否下载完成
private long currentProgress = 0; //当前下载进度
private int failNum = 0;
private long completeTime; //完成时间
public DownloadEntity() {

View File

@ -82,6 +82,7 @@ public class DownloadTaskQueue implements ITaskQueue {
@Override public void startTask(Task task) {
if (mExecutePool.putTask(task)) {
mCachePool.removeTask(task);
task.getDownloadEntity().setFailNum(0);
task.start();
}
}

View File

@ -186,6 +186,11 @@ public class DownloadSchedulers implements IDownloadSchedulers {
if (entity.getFailNum() <= mFailNum) {
Task task = mQueue.getTask(entity);
mQueue.reTryStart(task);
try {
Thread.currentThread().sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
startNextTask(entity);
}

View File

@ -16,7 +16,6 @@
package com.arialyy.downloadutil.core.scheduler;
import android.content.Context;
import android.os.Handler;
import com.arialyy.downloadutil.core.DownloadEntity;

View File

@ -218,7 +218,7 @@ public class Task {
@Override public void onPre() {
super.onPre();
downloadEntity.setState(DownloadEntity.STATE_PRE);
//sendIntent(DownloadManager.ACTION_PRE, -1);
sendIntent(DownloadManager.ACTION_PRE, -1);
}
@Override public void onPostPre(long fileSize) {
@ -226,21 +226,22 @@ public class Task {
downloadEntity.setFileSize(fileSize);
downloadEntity.setState(DownloadEntity.STATE_POST_PRE);
sendInState2Target(DownloadSchedulers.PRE);
//sendIntent(DownloadManager.ACTION_POST_PRE, -1);
sendIntent(DownloadManager.ACTION_POST_PRE, -1);
}
@Override public void onResume(long resumeLocation) {
super.onResume(resumeLocation);
downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING);
sendInState2Target(DownloadSchedulers.RESUME);
//sendIntent(DownloadManager.ACTION_RESUME, resumeLocation);
sendIntent(DownloadManager.ACTION_RESUME, resumeLocation);
}
@Override public void onStart(long startLocation) {
super.onStart(startLocation);
downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING);
downloadEntity.setFailNum(0);
sendInState2Target(DownloadSchedulers.START);
//sendIntent(DownloadManager.ACTION_START, startLocation);
sendIntent(DownloadManager.ACTION_START, startLocation);
}
@Override public void onProgress(long currentLocation) {
@ -259,7 +260,7 @@ public class Task {
downloadEntity.setCurrentProgress(currentLocation);
lastLen = currentLocation;
sendInState2Target(DownloadSchedulers.RUNNING);
//context.sendBroadcast(sendIntent);
context.sendBroadcast(sendIntent);
}
}
@ -268,14 +269,14 @@ public class Task {
downloadEntity.setState(DownloadEntity.STATE_STOP);
downloadEntity.setSpeed(0);
sendInState2Target(DownloadSchedulers.STOP);
//sendIntent(DownloadManager.ACTION_STOP, stopLocation);
sendIntent(DownloadManager.ACTION_STOP, stopLocation);
}
@Override public void onCancel() {
super.onCancel();
downloadEntity.setState(DownloadEntity.STATE_CANCEL);
sendInState2Target(DownloadSchedulers.CANCEL);
//sendIntent(DownloadManager.ACTION_CANCEL, -1);
sendIntent(DownloadManager.ACTION_CANCEL, -1);
downloadEntity.deleteData();
}
@ -285,15 +286,16 @@ public class Task {
downloadEntity.setDownloadComplete(true);
downloadEntity.setSpeed(0);
sendInState2Target(DownloadSchedulers.COMPLETE);
//sendIntent(DownloadManager.ACTION_COMPLETE, downloadEntity.getFileSize());
sendIntent(DownloadManager.ACTION_COMPLETE, downloadEntity.getFileSize());
}
@Override public void onFail() {
super.onFail();
downloadEntity.setFailNum(downloadEntity.getFailNum() + 1);
downloadEntity.setState(DownloadEntity.STATE_FAIL);
downloadEntity.setSpeed(0);
sendInState2Target(DownloadSchedulers.FAIL);
//sendIntent(DownloadManager.ACTION_FAIL, -1);
sendIntent(DownloadManager.ACTION_FAIL, -1);
}
private void sendIntent(String action, long location) {
@ -305,7 +307,7 @@ public class Task {
if (location != -1) {
intent.putExtra(DownloadManager.CURRENT_LOCATION, location);
}
context.sendBroadcast(intent);
//context.sendBroadcast(intent);
}
}
}