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

pull/330/head
AriaLyy 8 years ago
parent 56d073d14e
commit cde2ed47f1
  1. 4
      Aria/build.gradle
  2. 17
      Aria/src/main/java/com/arialyy/aria/core/Aria.java
  3. 17
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java
  4. 14
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java
  5. 2
      Aria/src/main/java/com/arialyy/aria/core/download/IDownloadListener.java
  6. 24
      Aria/src/main/java/com/arialyy/aria/core/download/SingleThreadTask.java
  7. 9
      Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java
  8. 8
      Aria/src/main/java/com/arialyy/aria/core/scheduler/ISchedulerListener.java
  9. 21
      Aria/src/main/java/com/arialyy/aria/core/scheduler/ISchedulers.java
  10. 9
      Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java
  11. 7
      Aria/src/main/java/com/arialyy/aria/core/upload/IUploadListener.java
  12. 6
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadListener.java
  13. 11
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java
  14. 4
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadUtil.java
  15. 3
      README.md
  16. 14
      app/src/main/java/com/arialyy/simple/download/DownloadDialog.java
  17. 2
      app/src/main/java/com/arialyy/simple/download/DownloadModule.java
  18. 4
      app/src/main/java/com/arialyy/simple/download/DownloadPopupWindow.java
  19. 4
      app/src/main/java/com/arialyy/simple/download/SimpleNotification.java
  20. 6
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  21. 4
      app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java
  22. 5
      app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java
  23. 2
      app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java
  24. 12
      app/src/main/java/com/arialyy/simple/upload/UploadActivity.java

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

@ -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) {

@ -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);
}

@ -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);

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

@ -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);
}
}
}

@ -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;

@ -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);

@ -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;
/**
* 注册下载器监听一个观察者只能注册一次监听

@ -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;

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

@ -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() {
}

@ -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);
}

@ -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) {

@ -25,7 +25,7 @@ Aria怎样使用?
## 下载
[![Download](https://api.bintray.com/packages/arialyy/maven/Aria/images/download.svg)](https://bintray.com/arialyy/maven/Aria/_latestVersion)</br>
```java
compile 'com.arialyy.aria:Aria:3.1.3'
compile 'com.arialyy.aria:Aria:3.1.4'
```
## 示例
@ -266,6 +266,7 @@ Aria.download(this).removeAllTask();
## 开发日志
+ v_3.1.4 修复快速切换,暂停、恢复功能时,概率性出现的重新下载问题,添加onPre()回调,onPre()用于请求地址之前执行界面UI更新操作。
+ v_3.1.2 优化代码结构
+ v_3.1.0 添加Aria配置文件,优化代码
+ v_3.0.3 修复暂停后删除任务,闪退问题,添加删除记录的api

@ -44,7 +44,7 @@ public class DownloadDialog extends AbsDialog {
@Bind(R.id.speed) TextView mSpeed;
private static final String DOWNLOAD_URL =
"http://clashroyalecdn.static.kunlun.com/Clash_Royale-1.2.6-kunlun_landing_page-release.apk.apk";
"http://static.gaoshouyou.com/d/4b/d7/e04b308d9cd7f0ad4cac18d1a514544c.apk";
public DownloadDialog(Context context) {
super(context);
@ -77,7 +77,7 @@ public class DownloadDialog extends AbsDialog {
case R.id.start:
Aria.download(this)
.load(DOWNLOAD_URL)
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/daialog.apk")
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/飞机大战.apk")
.start();
break;
case R.id.stop:
@ -122,14 +122,10 @@ public class DownloadDialog extends AbsDialog {
@Override public void onTaskRunning(DownloadTask task) {
super.onTaskRunning(task);
long current = task.getCurrentProgress();
long len = task.getFileSize();
if (len == 0) {
mPb.setProgress(0);
} else {
mPb.setProgress((int) ((current * 100) / len));
if (task.getKey().equals(DOWNLOAD_URL)) {
mPb.setProgress(task.getPercent());
mSpeed.setText(task.getConvertSpeed());
}
mSpeed.setText(task.getConvertSpeed());
}
}
}

@ -178,7 +178,7 @@ public class DownloadModule extends BaseModule {
case Aria.ACTION_POST_PRE:
DownloadEntity entity = intent.getParcelableExtra(Aria.DOWNLOAD_ENTITY);
len = entity.getFileSize();
L.d(TAG, "download pre");
L.d(TAG, "download onPre");
handler.obtainMessage(SingleTaskActivity.DOWNLOAD_PRE, len).sendToTarget();
break;
case Aria.ACTION_START:

@ -46,7 +46,7 @@ public class DownloadPopupWindow extends AbsPopupWindow {
@Bind(R.id.speed) TextView mSpeed;
private static final String DOWNLOAD_URL =
"http://static.gaoshouyou.com/d/3a/93/573ae1db9493a801c24bf66128b11e39.apk";
"http://static.gaoshouyou.com/d/25/57/2e25bd9d4557ba31e9beebacfaf9e804.apk";
public DownloadPopupWindow(Context context) {
super(context, new ColorDrawable(Color.WHITE));
@ -79,7 +79,7 @@ public class DownloadPopupWindow extends AbsPopupWindow {
case R.id.start:
Aria.download(this)
.load(DOWNLOAD_URL)
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/daialog.apk")
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/消消乐.apk")
.start();
break;
case R.id.stop:

@ -30,7 +30,7 @@ import com.arialyy.simple.R;
public class SimpleNotification {
private static final String DOWNLOAD_URL =
"http://static.gaoshouyou.com/d/6e/e5/ff6ecaaf45e532e6d07747af82357472.apk";
"http://static.gaoshouyou.com/d/cb/38/f0cb1b2c57388fe14342eecd64bbae65.apk";
private NotificationManager mManager;
private Context mContext;
@ -58,7 +58,7 @@ public class SimpleNotification {
.load(DOWNLOAD_URL)
.setDownloadName("notification_test.apk")
.setDownloadPath(
Environment.getExternalStorageDirectory() + "/Download/notification_test.apk")
Environment.getExternalStorageDirectory() + "/Download/消灭星星.apk")
.start();
}

@ -24,6 +24,7 @@ import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
@ -243,6 +244,11 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
private class MySchedulerListener extends Aria.DownloadSchedulerListener {
@Override public void onPre(String url) {
super.onPre(url);
Log.d(TAG, "url ==> " + url);
}
@Override public void onNoSupportBreakPoint(DownloadTask task) {
super.onNoSupportBreakPoint(task);
T.showShort(SingleTaskActivity.this, "该下载链接不支持断点");

@ -45,7 +45,7 @@ public class DownloadFragment extends AbsFragment<FragmentDownloadBinding> {
@Bind(R.id.speed) TextView mSpeed;
private static final String DOWNLOAD_URL =
"http://rs.0.gaoshouyou.com/d/90/d7/7490c6fd6cd733bef336e766778507c5.apk";
"http://static.ilongyuan.cn/rayark/RayarkFZ_2.0.7.apk";
@Override protected void init(Bundle savedInstanceState) {
if (Aria.download(this).taskExists(DOWNLOAD_URL)) {
@ -73,7 +73,7 @@ public class DownloadFragment extends AbsFragment<FragmentDownloadBinding> {
case R.id.start:
Aria.download(this)
.load(DOWNLOAD_URL)
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/daialog.apk")
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/兰空VOEZ.apk")
.start();
break;
case R.id.stop:

@ -70,7 +70,7 @@ final class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapter
notifyDataSetChanged();
} else {
int position = indexItem(entity.getDownloadUrl());
if (position == -1) {
if (position == -1 || position >= mData.size()) {
return;
}
mData.set(position, entity);
@ -81,7 +81,7 @@ final class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapter
public synchronized void setProgress(DownloadEntity entity) {
String url = entity.getDownloadUrl();
int position = indexItem(url);
if (position == -1) {
if (position == -1 || position >= mData.size()) {
return;
}
@ -169,6 +169,7 @@ final class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapter
case DownloadEntity.STATE_FAIL:
case DownloadEntity.STATE_STOP:
case DownloadEntity.STATE_COMPLETE:
case DownloadEntity.STATE_POST_PRE:
start(entity);
break;
case DownloadEntity.STATE_RUNNING:

@ -61,7 +61,7 @@ public class MultiDownloadActivity extends BaseActivity<ActivityMultiDownloadBin
private class MySchedulerListener extends Aria.DownloadSchedulerListener {
@Override public void onTaskPre(DownloadTask task) {
super.onTaskPre(task);
L.d(TAG, "download pre");
L.d(TAG, "download onPre");
mAdapter.updateState(task.getDownloadEntity());
}

@ -24,6 +24,7 @@ import butterknife.OnClick;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.upload.UploadTask;
import com.arialyy.frame.util.FileUtil;
import com.arialyy.frame.util.show.L;
import com.arialyy.frame.util.show.T;
import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseActivity;
@ -35,6 +36,7 @@ import java.lang.ref.WeakReference;
* Created by Aria.Lao on 2017/2/9.
*/
public class UploadActivity extends BaseActivity<ActivityUploadMeanBinding> {
private static final String TAG = "UploadActivity";
@Bind(R.id.pb) HorizontalProgressBarWithNumber mPb;
private static final int START = 0;
private static final int STOP = 1;
@ -106,6 +108,16 @@ public class UploadActivity extends BaseActivity<ActivityUploadMeanBinding> {
this.handler = new WeakReference<>(handler);
}
@Override public void onPre(String url) {
super.onPre(url);
L.e(TAG, "url ==> " + url);
}
@Override public void onTaskPre(UploadTask task) {
super.onTaskPre(task);
L.d(TAG, "fileSize = " + task.getConvertFileSize());
}
@Override public void onTaskStart(UploadTask task) {
super.onTaskStart(task);
handler.get().obtainMessage(START, task).sendToTarget();

Loading…
Cancel
Save