修复快速切换,暂停、恢复功能时,概率性出现的重新下载问题,添加onPre()回调,onPre()用于请求地址之前执行界面UI更新操作

This commit is contained in:
AriaLyy
2017-06-02 13:43:07 +08:00
parent 56d073d14e
commit cde2ed47f1
24 changed files with 144 additions and 65 deletions

View File

@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 9
targetSdkVersion 23
versionCode 313
versionName "3.1.3"
versionCode 314
versionName "3.1.4"
}
buildTypes {
release {

View File

@ -174,6 +174,15 @@ import com.arialyy.aria.core.upload.UploadTask;
*/
public static class UploadSchedulerListener implements ISchedulerListener<UploadTask> {
/**
* 预处理有时有些地址链接比较慢这时可以先在这个地方出来一些界面上的UI如按钮的状态。
*
* @param url 任务上传地址
*/
@Override public void onPre(String url) {
}
@Override public void onTaskPre(UploadTask task) {
}
@ -212,6 +221,14 @@ import com.arialyy.aria.core.upload.UploadTask;
*/
public static class DownloadSchedulerListener
implements IDownloadSchedulerListener<DownloadTask> {
/**
* 预处理有时有些地址链接比较慢这时可以先在这个地方出来一些界面上的UI如按钮的状态。
*
* @param url 任务下载地址
*/
@Override public void onPre(String url) {
}
@Override public void onTaskPre(DownloadTask task) {

View File

@ -302,6 +302,7 @@ public class DownloadTask implements ITask {
@Override public void onPre() {
super.onPre();
downloadEntity.setState(DownloadEntity.STATE_PRE);
sendInState2Target(ISchedulers.PRE);
sendIntent(Aria.ACTION_PRE, -1);
}
@ -309,21 +310,21 @@ public class DownloadTask implements ITask {
super.onPostPre(fileSize);
downloadEntity.setFileSize(fileSize);
downloadEntity.setState(DownloadEntity.STATE_POST_PRE);
sendInState2Target(DownloadSchedulers.PRE);
sendInState2Target(ISchedulers.POST_PRE);
sendIntent(Aria.ACTION_POST_PRE, -1);
}
@Override public void onResume(long resumeLocation) {
super.onResume(resumeLocation);
downloadEntity.setState(DownloadEntity.STATE_RUNNING);
sendInState2Target(DownloadSchedulers.RESUME);
sendInState2Target(ISchedulers.RESUME);
sendIntent(Aria.ACTION_RESUME, resumeLocation);
}
@Override public void onStart(long startLocation) {
super.onStart(startLocation);
downloadEntity.setState(DownloadEntity.STATE_RUNNING);
sendInState2Target(DownloadSchedulers.START);
sendInState2Target(ISchedulers.START);
sendIntent(Aria.ACTION_START, startLocation);
}
@ -341,7 +342,7 @@ public class DownloadTask implements ITask {
handleSpeed(speed);
downloadEntity.setCurrentProgress(currentLocation);
lastLen = currentLocation;
sendInState2Target(DownloadSchedulers.RUNNING);
sendInState2Target(ISchedulers.RUNNING);
context.sendBroadcast(sendIntent);
}
}
@ -350,7 +351,7 @@ public class DownloadTask implements ITask {
super.onStop(stopLocation);
downloadEntity.setState(DownloadEntity.STATE_STOP);
handleSpeed(0);
sendInState2Target(DownloadSchedulers.STOP);
sendInState2Target(ISchedulers.STOP);
sendIntent(Aria.ACTION_STOP, stopLocation);
}
@ -358,7 +359,7 @@ public class DownloadTask implements ITask {
super.onCancel();
downloadEntity.setState(DownloadEntity.STATE_CANCEL);
handleSpeed(0);
sendInState2Target(DownloadSchedulers.CANCEL);
sendInState2Target(ISchedulers.CANCEL);
sendIntent(Aria.ACTION_CANCEL, -1);
downloadEntity.deleteData();
}
@ -368,7 +369,7 @@ public class DownloadTask implements ITask {
downloadEntity.setState(DownloadEntity.STATE_COMPLETE);
downloadEntity.setDownloadComplete(true);
handleSpeed(0);
sendInState2Target(DownloadSchedulers.COMPLETE);
sendInState2Target(ISchedulers.COMPLETE);
sendIntent(Aria.ACTION_COMPLETE, downloadEntity.getFileSize());
}
@ -377,7 +378,7 @@ public class DownloadTask implements ITask {
downloadEntity.setFailNum(downloadEntity.getFailNum() + 1);
downloadEntity.setState(DownloadEntity.STATE_FAIL);
handleSpeed(0);
sendInState2Target(DownloadSchedulers.FAIL);
sendInState2Target(ISchedulers.FAIL);
sendIntent(Aria.ACTION_FAIL, -1);
}

View File

@ -28,6 +28,7 @@ import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@ -367,7 +368,18 @@ class DownloadUtil implements IDownloadUtil, Runnable {
if (pro.isEmpty()) {
handleNewTask();
} else {
THREAD_NUM = pro.keySet().size();
Set<Object> keys = pro.keySet();
int num = 0;
for (Object key : keys) {
if (String.valueOf(key).contains("_record_")) {
num++;
}
}
if (num == 0){
handleNewTask();
return pro;
}
THREAD_NUM = num;
for (int i = 0; i < THREAD_NUM; i++) {
if (pro.getProperty(mDownloadFile.getName() + "_record_" + i) == null) {
Object state = pro.getProperty(mDownloadFile.getName() + "_state_" + i);

View File

@ -39,7 +39,7 @@ interface IDownloadListener {
public void onFail();
/**
* 预处理
* 预处理有时有些地址链接比较慢这时可以先在这个地方出来一些界面上的UI如按钮的状态
*/
public void onPre();

View File

@ -113,8 +113,7 @@ final class SingleThreadTask implements Runnable {
//支持断点的处理
if (mConfigEntity.isSupportBreakpoint) {
Log.i(TAG, "线程【" + mConfigEntity.THREAD_ID + "】下载完毕");
writeConfig(mConfigEntity.TEMP_FILE.getName() + "_state_" + mConfigEntity.THREAD_ID,
1 + "");
writeConfig(mConfigEntity.TEMP_FILE.getName() + "_state_" + mConfigEntity.THREAD_ID, 1);
mListener.onChildComplete(mConfigEntity.END_LOCATION);
CONSTANCE.COMPLETE_THREAD_NUM++;
if (CONSTANCE.isComplete()) {
@ -151,13 +150,12 @@ final class SingleThreadTask implements Runnable {
try {
if (mConfigEntity.isSupportBreakpoint) {
CONSTANCE.STOP_NUM++;
String location = String.valueOf(mChildCurrentLocation);
Log.d(TAG, "thread_"
+ mConfigEntity.THREAD_ID
+ "_stop, stop location ==> "
+ mChildCurrentLocation);
writeConfig(mConfigEntity.TEMP_FILE.getName() + "_record_" + mConfigEntity.THREAD_ID,
location);
mChildCurrentLocation);
if (CONSTANCE.isStop()) {
Log.d(TAG, "++++++++++++++++ onStop +++++++++++++++++");
CONSTANCE.isDownloading = false;
@ -226,10 +224,8 @@ final class SingleThreadTask implements Runnable {
Log.e(TAG, CommonUtil.getPrintException(ex));
}
if (mConfigEntity.isSupportBreakpoint) {
if (currentLocation != -1) {
String location = String.valueOf(currentLocation);
writeConfig(dEntity.TEMP_FILE.getName() + "_record_" + dEntity.THREAD_ID, location);
}
writeConfig(dEntity.TEMP_FILE.getName() + "_record_" + dEntity.THREAD_ID,
currentLocation);
if (CONSTANCE.isFail()) {
Log.d(TAG, "++++++++++++++++ onFail +++++++++++++++++");
mListener.onFail();
@ -247,10 +243,12 @@ final class SingleThreadTask implements Runnable {
/**
* 将记录写入到配置文件
*/
private void writeConfig(String key, String record) throws IOException {
File configFile = new File(mConfigFPath);
Properties pro = CommonUtil.loadConfig(configFile);
pro.setProperty(key, record);
CommonUtil.saveConfig(configFile, pro);
private void writeConfig(String key, long record) throws IOException {
if (record != -1 && record != 0) {
File configFile = new File(mConfigFPath);
Properties pro = CommonUtil.loadConfig(configFile);
pro.setProperty(key, String.valueOf(record));
CommonUtil.saveConfig(configFile, pro);
}
}
}

View File

@ -128,6 +128,12 @@ public class DownloadSchedulers implements ISchedulers<DownloadTask> {
return;
}
switch (state) {
case PRE:
listener.onPre(task.getKey());
break;
case POST_PRE:
listener.onTaskPre(task);
break;
case RUNNING:
listener.onTaskRunning(task);
break;
@ -140,9 +146,6 @@ public class DownloadSchedulers implements ISchedulers<DownloadTask> {
case RESUME:
listener.onTaskResume(task);
break;
case PRE:
listener.onTaskPre(task);
break;
case CANCEL:
listener.onTaskCancel(task);
break;

View File

@ -22,7 +22,13 @@ import com.arialyy.aria.core.inf.ITask;
*/
public interface ISchedulerListener<TASK extends ITask> {
/**
* 任务预加载
* 预处理有时有些地址链接比较慢这时可以先在这个地方出来一些界面上的UI如按钮的状态。
* 在这个回调中,任务是获取不到文件大小,下载速度等参数
*/
public void onPre(String url);
/**
* 任务预加载完成
*/
public void onTaskPre(TASK task);

View File

@ -27,39 +27,44 @@ public interface ISchedulers<Task extends ITask> extends Handler.Callback {
/**
* 断点支持
*/
public static final int SUPPORT_BREAK_POINT = 8;
public static final int SUPPORT_BREAK_POINT = 9;
/**
* 任务预加载
*/
public static final int PRE = 0;
/**
* 任务预加载完成
*/
public static final int POST_PRE = 1;
/**
* 任务开始
*/
public static final int START = 1;
public static final int START = 2;
/**
* 任务停止
*/
public static final int STOP = 2;
public static final int STOP = 3;
/**
* 任务失败
*/
public static final int FAIL = 3;
public static final int FAIL = 4;
/**
* 任务取消
*/
public static final int CANCEL = 4;
public static final int CANCEL = 5;
/**
* 任务完成
*/
public static final int COMPLETE = 5;
public static final int COMPLETE = 6;
/**
* 任务处理中
*/
public static final int RUNNING = 6;
public static final int RUNNING = 7;
/**
* 恢复任务
*/
public static final int RESUME = 7;
public static final int RESUME = 8;
/**
* 注册下载器监听,一个观察者只能注册一次监听

View File

@ -131,6 +131,12 @@ public class UploadSchedulers implements ISchedulers<UploadTask> {
return;
}
switch (state) {
case PRE:
listener.onPre(task.getKey());
break;
case POST_PRE:
listener.onTaskPre(task);
break;
case RUNNING:
listener.onTaskRunning(task);
break;
@ -143,9 +149,6 @@ public class UploadSchedulers implements ISchedulers<UploadTask> {
case RESUME:
listener.onTaskResume(task);
break;
case PRE:
listener.onTaskPre(task);
break;
case CANCEL:
listener.onTaskCancel(task);
break;

View File

@ -26,10 +26,15 @@ public interface IUploadListener {
*/
public void onPre();
/**
* 预处理完成
*/
public void onPostPre(long fileSize);
/**
* 开始上传
*/
public void onStart(long fileSize);
public void onStart();
/**
* 恢复上传

View File

@ -24,7 +24,11 @@ public class UploadListener implements IUploadListener {
}
@Override public void onStart(long fileSize) {
@Override public void onPostPre(long fileSize) {
}
@Override public void onStart() {
}

View File

@ -214,10 +214,17 @@ public class UploadTask implements ITask {
sendInState2Target(ISchedulers.PRE);
}
@Override public void onStart(long fileSize) {
@Override public void onPostPre(long fileSize) {
super.onPostPre(fileSize);
uploadEntity.setFileSize(fileSize);
uploadEntity.setState(IEntity.STATE_POST_PRE);
sendIntent(Aria.ACTION_POST_PRE, 0);
sendInState2Target(ISchedulers.POST_PRE);
}
@Override public void onStart() {
uploadEntity.setState(IEntity.STATE_RUNNING);
sendIntent(Aria.ACTION_PRE, -1);
sendIntent(Aria.ACTION_START, 0);
sendInState2Target(ISchedulers.START);
}

View File

@ -99,6 +99,8 @@ final class UploadUtil implements Runnable {
}
mOutputStream = mHttpConn.getOutputStream();
mListener.onPostPre(uploadFile.length());
PrintWriter writer =
new PrintWriter(new OutputStreamWriter(mOutputStream, mTaskEntity.charset), true);
@ -107,7 +109,7 @@ final class UploadUtil implements Runnable {
for (String key : keys) {
addFormField(writer, key, mTaskEntity.formFields.get(key));
}
mListener.onStart(uploadFile.length());
mListener.onStart();
uploadFile(writer, mTaskEntity.attachment, uploadFile);
Log.d(TAG, finish(writer) + "");
} catch (IOException e) {