This commit is contained in:
@ -17,10 +17,10 @@ package com.arialyy.aria.core.download;
|
|||||||
|
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import com.arialyy.aria.core.AriaManager;
|
import com.arialyy.aria.core.AriaManager;
|
||||||
import com.arialyy.aria.core.download.downloader.DownloadListener;
|
|
||||||
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.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;
|
||||||
@ -28,22 +28,23 @@ import java.lang.ref.WeakReference;
|
|||||||
/**
|
/**
|
||||||
* 下载监听类
|
* 下载监听类
|
||||||
*/
|
*/
|
||||||
class DListener<ENTITY extends AbsEntity, TASK extends AbsTask<ENTITY>>
|
class BaseListener<ENTITY extends AbsEntity, TASK extends AbsTask<ENTITY>>
|
||||||
extends DownloadListener {
|
implements IEventListener {
|
||||||
private WeakReference<Handler> outHandler;
|
private WeakReference<Handler> outHandler;
|
||||||
private long lastLen = 0; //上一次发送长度
|
private long mLastLen = 0; //上一次发送长度
|
||||||
private boolean isFirst = true;
|
private boolean isFirst = true;
|
||||||
private ENTITY entity;
|
protected ENTITY mEntity;
|
||||||
private TASK task;
|
protected TASK mTask;
|
||||||
private boolean isConvertSpeed = false;
|
private boolean isConvertSpeed = false;
|
||||||
boolean isWait = false;
|
boolean isWait = false;
|
||||||
|
|
||||||
DListener(TASK task, Handler outHandler) {
|
BaseListener(TASK task, Handler outHandler) {
|
||||||
this.outHandler = new WeakReference<>(outHandler);
|
this.outHandler = new WeakReference<>(outHandler);
|
||||||
this.task = new WeakReference<>(task).get();
|
this.mTask = new WeakReference<>(task).get();
|
||||||
this.entity = this.task.getEntity();
|
this.mEntity = this.mTask.getEntity();
|
||||||
final AriaManager manager = AriaManager.getInstance(AriaManager.APP);
|
final AriaManager manager = AriaManager.getInstance(AriaManager.APP);
|
||||||
isConvertSpeed = manager.getDownloadConfig().isConvertSpeed();
|
isConvertSpeed = manager.getDownloadConfig().isConvertSpeed();
|
||||||
|
mLastLen = mEntity.getCurrentProgress();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void onPre() {
|
@Override public void onPre() {
|
||||||
@ -52,8 +53,8 @@ class DListener<ENTITY extends AbsEntity, TASK extends AbsTask<ENTITY>>
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override public void onPostPre(long fileSize) {
|
@Override public void onPostPre(long fileSize) {
|
||||||
entity.setFileSize(fileSize);
|
mEntity.setFileSize(fileSize);
|
||||||
entity.setConvertFileSize(CommonUtil.formatFileSize(fileSize));
|
mEntity.setConvertFileSize(CommonUtil.formatFileSize(fileSize));
|
||||||
saveData(IEntity.STATE_POST_PRE, -1);
|
saveData(IEntity.STATE_POST_PRE, -1);
|
||||||
sendInState2Target(ISchedulers.POST_PRE);
|
sendInState2Target(ISchedulers.POST_PRE);
|
||||||
}
|
}
|
||||||
@ -69,15 +70,15 @@ class DListener<ENTITY extends AbsEntity, TASK extends AbsTask<ENTITY>>
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override public void onProgress(long currentLocation) {
|
@Override public void onProgress(long currentLocation) {
|
||||||
entity.setCurrentProgress(currentLocation);
|
mEntity.setCurrentProgress(currentLocation);
|
||||||
long speed = currentLocation - lastLen;
|
long speed = currentLocation - mLastLen;
|
||||||
if (isFirst) {
|
if (isFirst) {
|
||||||
speed = 0;
|
speed = 0;
|
||||||
isFirst = false;
|
isFirst = false;
|
||||||
}
|
}
|
||||||
handleSpeed(speed);
|
handleSpeed(speed);
|
||||||
sendInState2Target(ISchedulers.RUNNING);
|
sendInState2Target(ISchedulers.RUNNING);
|
||||||
lastLen = currentLocation;
|
mLastLen = currentLocation;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void onStop(long stopLocation) {
|
@Override public void onStop(long stopLocation) {
|
||||||
@ -93,23 +94,23 @@ class DListener<ENTITY extends AbsEntity, TASK extends AbsTask<ENTITY>>
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override public void onComplete() {
|
@Override public void onComplete() {
|
||||||
saveData(IEntity.STATE_COMPLETE, entity.getFileSize());
|
saveData(IEntity.STATE_COMPLETE, mEntity.getFileSize());
|
||||||
handleSpeed(0);
|
handleSpeed(0);
|
||||||
sendInState2Target(ISchedulers.COMPLETE);
|
sendInState2Target(ISchedulers.COMPLETE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void onFail() {
|
@Override public void onFail() {
|
||||||
entity.setFailNum(entity.getFailNum() + 1);
|
mEntity.setFailNum(mEntity.getFailNum() + 1);
|
||||||
saveData(IEntity.STATE_FAIL, -1);
|
saveData(IEntity.STATE_FAIL, mEntity.getCurrentProgress());
|
||||||
handleSpeed(0);
|
handleSpeed(0);
|
||||||
sendInState2Target(ISchedulers.FAIL);
|
sendInState2Target(ISchedulers.FAIL);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleSpeed(long speed) {
|
private void handleSpeed(long speed) {
|
||||||
if (isConvertSpeed) {
|
if (isConvertSpeed) {
|
||||||
entity.setConvertSpeed(CommonUtil.formatFileSize(speed) + "/s");
|
mEntity.setConvertSpeed(CommonUtil.formatFileSize(speed < 0 ? 0 : speed) + "/s");
|
||||||
} else {
|
} else {
|
||||||
entity.setSpeed(speed);
|
mEntity.setSpeed(speed < 0 ? 0 : speed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,25 +121,25 @@ class DListener<ENTITY extends AbsEntity, TASK extends AbsTask<ENTITY>>
|
|||||||
*/
|
*/
|
||||||
private void sendInState2Target(int state) {
|
private void sendInState2Target(int state) {
|
||||||
if (outHandler.get() != null) {
|
if (outHandler.get() != null) {
|
||||||
outHandler.get().obtainMessage(state, task).sendToTarget();
|
outHandler.get().obtainMessage(state, mTask).sendToTarget();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveData(int state, long location) {
|
private void saveData(int state, long location) {
|
||||||
|
mEntity.setComplete(state == IEntity.STATE_COMPLETE);
|
||||||
if (state == IEntity.STATE_CANCEL) {
|
if (state == IEntity.STATE_CANCEL) {
|
||||||
entity.deleteData();
|
mEntity.deleteData();
|
||||||
} else if (state == IEntity.STATE_COMPLETE) {
|
} else if (state == IEntity.STATE_COMPLETE) {
|
||||||
entity.setState(state);
|
mEntity.setState(state);
|
||||||
entity.setComplete(true);
|
mEntity.setCompleteTime(System.currentTimeMillis());
|
||||||
entity.setCompleteTime(System.currentTimeMillis());
|
mEntity.setCurrentProgress(mEntity.getFileSize());
|
||||||
entity.setCurrentProgress(entity.getFileSize());
|
mEntity.update();
|
||||||
entity.update();
|
|
||||||
} else {
|
} else {
|
||||||
entity.setState(state);
|
mEntity.setState(state);
|
||||||
if (location != -1) {
|
if (location != -1) {
|
||||||
entity.setCurrentProgress(location);
|
mEntity.setCurrentProgress(location);
|
||||||
}
|
}
|
||||||
entity.update();
|
mEntity.update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.arialyy.aria.core.download;
|
||||||
|
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.util.Log;
|
||||||
|
import com.arialyy.aria.core.download.downloader.IDownloadGroupListener;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Aria.Lao on 2017/7/20.
|
||||||
|
* 任务组下载事件
|
||||||
|
*/
|
||||||
|
class DownloadGroupListener extends BaseListener<DownloadGroupEntity, DownloadGroupTask>
|
||||||
|
implements IDownloadGroupListener {
|
||||||
|
private final String TAG = "DownloadGroupListener";
|
||||||
|
|
||||||
|
DownloadGroupListener(DownloadGroupTask task, Handler outHandler) {
|
||||||
|
super(task, outHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public void supportBreakpoint(boolean support, DownloadEntity subEntity) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public void onSubStart(DownloadEntity subEntity) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public void onSubStop(DownloadEntity subEntity) {
|
||||||
|
saveCurrentLocation();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public void onSubComplete(DownloadEntity subEntity) {
|
||||||
|
saveCurrentLocation();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public void onSubFail(DownloadEntity subEntity) {
|
||||||
|
saveCurrentLocation();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public void onSubCancel(DownloadEntity entity) {
|
||||||
|
saveCurrentLocation();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void saveCurrentLocation() {
|
||||||
|
long location = 0;
|
||||||
|
for (DownloadEntity e : mEntity.getSubTask()) {
|
||||||
|
location += e.getCurrentProgress();
|
||||||
|
}
|
||||||
|
mEntity.setCurrentProgress(location);
|
||||||
|
mEntity.update();
|
||||||
|
}
|
||||||
|
}
|
@ -29,7 +29,7 @@ import com.arialyy.aria.util.CheckUtil;
|
|||||||
*/
|
*/
|
||||||
public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, DownloadGroupEntity> {
|
public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, DownloadGroupEntity> {
|
||||||
private final String TAG = "DownloadGroupTask";
|
private final String TAG = "DownloadGroupTask";
|
||||||
private DListener<DownloadGroupEntity, DownloadGroupTask> mListener;
|
private DownloadGroupListener mListener;
|
||||||
private IDownloadUtil mUtil;
|
private IDownloadUtil mUtil;
|
||||||
|
|
||||||
private DownloadGroupTask(DownloadGroupTaskEntity taskEntity, Handler outHandler) {
|
private DownloadGroupTask(DownloadGroupTaskEntity taskEntity, Handler outHandler) {
|
||||||
@ -37,7 +37,7 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, Dow
|
|||||||
mEntity = taskEntity.getEntity();
|
mEntity = taskEntity.getEntity();
|
||||||
mOutHandler = outHandler;
|
mOutHandler = outHandler;
|
||||||
mContext = AriaManager.APP;
|
mContext = AriaManager.APP;
|
||||||
mListener = new DListener<>(this, mOutHandler);
|
mListener = new DownloadGroupListener(this, mOutHandler);
|
||||||
mUtil = new DownloadGroupUtil(mListener, mTaskEntity);
|
mUtil = new DownloadGroupUtil(mListener, mTaskEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.arialyy.aria.core.download;
|
||||||
|
|
||||||
|
import android.os.Handler;
|
||||||
|
import com.arialyy.aria.core.download.downloader.IDownloadListener;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Aria.Lao on 2017/7/20.
|
||||||
|
* 普通任务下载的事件监听器
|
||||||
|
*/
|
||||||
|
class DownloadListener extends BaseListener<DownloadEntity, DownloadTask>
|
||||||
|
implements IDownloadListener {
|
||||||
|
DownloadListener(DownloadTask task, Handler outHandler) {
|
||||||
|
super(task, outHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public void supportBreakpoint(boolean support) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -32,14 +32,14 @@ import java.io.File;
|
|||||||
public class DownloadTask extends AbsNormalTask<DownloadEntity> {
|
public class DownloadTask extends AbsNormalTask<DownloadEntity> {
|
||||||
public static final String TAG = "DownloadTask";
|
public static final String TAG = "DownloadTask";
|
||||||
|
|
||||||
private DListener<DownloadEntity, DownloadTask> mListener;
|
private DownloadListener mListener;
|
||||||
private SimpleDownloadUtil mUtil;
|
private SimpleDownloadUtil mUtil;
|
||||||
|
|
||||||
private DownloadTask(DownloadTaskEntity taskEntity, Handler outHandler) {
|
private DownloadTask(DownloadTaskEntity taskEntity, Handler outHandler) {
|
||||||
mEntity = taskEntity.getEntity();
|
mEntity = taskEntity.getEntity();
|
||||||
mOutHandler = outHandler;
|
mOutHandler = outHandler;
|
||||||
mContext = AriaManager.APP;
|
mContext = AriaManager.APP;
|
||||||
mListener = new DListener<>(this, mOutHandler);
|
mListener = new DownloadListener(this, mOutHandler);
|
||||||
mUtil = new SimpleDownloadUtil(taskEntity, mListener);
|
mUtil = new SimpleDownloadUtil(taskEntity, mListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
|||||||
private long mCurrentLocation = 0;
|
private long mCurrentLocation = 0;
|
||||||
private ExecutorService mInfoPool;
|
private ExecutorService mInfoPool;
|
||||||
private ExecutorService mExePool;
|
private ExecutorService mExePool;
|
||||||
private IDownloadListener mListener;
|
private IDownloadGroupListener mListener;
|
||||||
private DownloadGroupTaskEntity mTaskEntity;
|
private DownloadGroupTaskEntity mTaskEntity;
|
||||||
private boolean isRunning = true;
|
private boolean isRunning = true;
|
||||||
private Timer mTimer;
|
private Timer mTimer;
|
||||||
@ -87,7 +87,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
|||||||
//实际的下载任务数
|
//实际的下载任务数
|
||||||
private int mActualTaskNum = 0;
|
private int mActualTaskNum = 0;
|
||||||
|
|
||||||
public DownloadGroupUtil(IDownloadListener listener, DownloadGroupTaskEntity taskEntity) {
|
public DownloadGroupUtil(IDownloadGroupListener listener, DownloadGroupTaskEntity taskEntity) {
|
||||||
mListener = listener;
|
mListener = listener;
|
||||||
mTaskEntity = taskEntity;
|
mTaskEntity = taskEntity;
|
||||||
mInfoPool = Executors.newCachedThreadPool();
|
mInfoPool = Executors.newCachedThreadPool();
|
||||||
@ -102,8 +102,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
|||||||
}
|
}
|
||||||
for (DownloadEntity entity : mTaskEntity.entity.getSubTask()) {
|
for (DownloadEntity entity : mTaskEntity.entity.getSubTask()) {
|
||||||
File file = new File(entity.getDownloadPath());
|
File file = new File(entity.getDownloadPath());
|
||||||
if (entity.isComplete() && file.exists()) {
|
if (entity.getState() == IEntity.STATE_COMPLETE && file.exists()) {
|
||||||
mTotalSize += entity.getFileSize();
|
|
||||||
mCompleteNum++;
|
mCompleteNum++;
|
||||||
mInitNum++;
|
mInitNum++;
|
||||||
mCurrentLocation += entity.getFileSize();
|
mCurrentLocation += entity.getFileSize();
|
||||||
@ -111,6 +110,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
|||||||
mExeMap.put(entity.getDownloadUrl(), createChildDownloadTask(entity));
|
mExeMap.put(entity.getDownloadUrl(), createChildDownloadTask(entity));
|
||||||
mCurrentLocation += entity.getCurrentProgress();
|
mCurrentLocation += entity.getCurrentProgress();
|
||||||
}
|
}
|
||||||
|
mTotalSize += entity.getFileSize();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,8 +127,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override public void cancelDownload() {
|
@Override public void cancelDownload() {
|
||||||
isRunning = false;
|
closeTimer(false);
|
||||||
closeTimer();
|
|
||||||
mListener.onCancel();
|
mListener.onCancel();
|
||||||
if (!mInfoPool.isShutdown()) {
|
if (!mInfoPool.isShutdown()) {
|
||||||
mInfoPool.shutdown();
|
mInfoPool.shutdown();
|
||||||
@ -161,8 +160,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override public void stopDownload() {
|
@Override public void stopDownload() {
|
||||||
isRunning = false;
|
closeTimer(false);
|
||||||
closeTimer();
|
|
||||||
mListener.onStop(mCurrentLocation);
|
mListener.onStop(mCurrentLocation);
|
||||||
if (!mInfoPool.isShutdown()) {
|
if (!mInfoPool.isShutdown()) {
|
||||||
mInfoPool.shutdown();
|
mInfoPool.shutdown();
|
||||||
@ -182,15 +180,24 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
|||||||
|
|
||||||
@Override public void startDownload() {
|
@Override public void startDownload() {
|
||||||
isRunning = true;
|
isRunning = true;
|
||||||
|
mFailNum = 0;
|
||||||
Set<String> keys = mExeMap.keySet();
|
Set<String> keys = mExeMap.keySet();
|
||||||
mListener.onPre();
|
mListener.onPre();
|
||||||
|
int i = 0;
|
||||||
for (String key : keys) {
|
for (String key : keys) {
|
||||||
DownloadTaskEntity taskEntity = mExeMap.get(key);
|
DownloadTaskEntity taskEntity = mExeMap.get(key);
|
||||||
if (taskEntity != null) {
|
if (taskEntity != null) {
|
||||||
|
if (taskEntity.getState() != IEntity.STATE_FAIL
|
||||||
|
&& taskEntity.getState() != IEntity.STATE_WAIT) {
|
||||||
|
startChildDownload(taskEntity);
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
mInfoPool.execute(createFileInfoThread(taskEntity));
|
mInfoPool.execute(createFileInfoThread(taskEntity));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (i == mExeMap.size()) startRunningFlow();
|
||||||
|
}
|
||||||
|
|
||||||
@Override public void resumeDownload() {
|
@Override public void resumeDownload() {
|
||||||
startDownload();
|
startDownload();
|
||||||
@ -214,7 +221,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
|||||||
startChildDownload(te);
|
startChildDownload(te);
|
||||||
}
|
}
|
||||||
mInitNum++;
|
mInitNum++;
|
||||||
if (mInitNum + mInitFailNum == mTaskEntity.getEntity().getSubTask().size()) {
|
if (mInitNum + mInitFailNum >= mTaskEntity.getEntity().getSubTask().size()) {
|
||||||
startRunningFlow();
|
startRunningFlow();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -235,7 +242,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
|||||||
if (mActualTaskNum < 0) mActualTaskNum = 0;
|
if (mActualTaskNum < 0) mActualTaskNum = 0;
|
||||||
}
|
}
|
||||||
failNum++;
|
failNum++;
|
||||||
if (mInitNum + mInitFailNum == mTaskEntity.getEntity().getSubTask().size()) {
|
if (mInitNum + mInitFailNum >= mTaskEntity.getEntity().getSubTask().size()) {
|
||||||
startRunningFlow();
|
startRunningFlow();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -244,7 +251,8 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
|||||||
return new FileInfoThread(taskEntity, callback);
|
return new FileInfoThread(taskEntity, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void closeTimer() {
|
private void closeTimer(boolean isRunning) {
|
||||||
|
this.isRunning = isRunning;
|
||||||
if (mTimer != null) {
|
if (mTimer != null) {
|
||||||
mTimer.purge();
|
mTimer.purge();
|
||||||
mTimer.cancel();
|
mTimer.cancel();
|
||||||
@ -255,13 +263,15 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
|||||||
* 开始进度流程
|
* 开始进度流程
|
||||||
*/
|
*/
|
||||||
private void startRunningFlow() {
|
private void startRunningFlow() {
|
||||||
closeTimer();
|
closeTimer(true);
|
||||||
mListener.onPostPre(mTotalSize);
|
mListener.onPostPre(mTotalSize);
|
||||||
mListener.onStart(mCurrentLocation);
|
mListener.onStart(mCurrentLocation);
|
||||||
mTimer = new Timer(true);
|
mTimer = new Timer(true);
|
||||||
mTimer.schedule(new TimerTask() {
|
mTimer.schedule(new TimerTask() {
|
||||||
@Override public void run() {
|
@Override public void run() {
|
||||||
if (mCurrentLocation >= 0) {
|
if (!isRunning) {
|
||||||
|
closeTimer(false);
|
||||||
|
} else if (mCurrentLocation >= 0) {
|
||||||
mListener.onProgress(mCurrentLocation);
|
mListener.onProgress(mCurrentLocation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -304,7 +314,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
|||||||
/**
|
/**
|
||||||
* 子任务事件监听
|
* 子任务事件监听
|
||||||
*/
|
*/
|
||||||
private class ChildDownloadListener extends DownloadListener {
|
private class ChildDownloadListener implements IDownloadListener {
|
||||||
|
|
||||||
DownloadTaskEntity taskEntity;
|
DownloadTaskEntity taskEntity;
|
||||||
DownloadEntity entity;
|
DownloadEntity entity;
|
||||||
@ -314,6 +324,8 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
|||||||
ChildDownloadListener(DownloadTaskEntity entity) {
|
ChildDownloadListener(DownloadTaskEntity entity) {
|
||||||
this.taskEntity = entity;
|
this.taskEntity = entity;
|
||||||
this.entity = taskEntity.getEntity();
|
this.entity = taskEntity.getEntity();
|
||||||
|
lastLen = this.entity.getCurrentProgress();
|
||||||
|
this.entity.setFailNum(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void onPre() {
|
@Override public void onPre() {
|
||||||
@ -347,26 +359,35 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
|||||||
@Override public void onStop(long stopLocation) {
|
@Override public void onStop(long stopLocation) {
|
||||||
saveData(IEntity.STATE_STOP, stopLocation);
|
saveData(IEntity.STATE_STOP, stopLocation);
|
||||||
handleSpeed(0);
|
handleSpeed(0);
|
||||||
|
mListener.onSubStop(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void onCancel() {
|
@Override public void onCancel() {
|
||||||
saveData(IEntity.STATE_CANCEL, -1);
|
saveData(IEntity.STATE_CANCEL, -1);
|
||||||
handleSpeed(0);
|
handleSpeed(0);
|
||||||
|
mListener.onSubCancel(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void onComplete() {
|
@Override public void onComplete() {
|
||||||
saveData(IEntity.STATE_COMPLETE, entity.getFileSize());
|
saveData(IEntity.STATE_COMPLETE, entity.getFileSize());
|
||||||
mCompleteNum++;
|
mCompleteNum++;
|
||||||
if (mCompleteNum + mFailNum >= mActualTaskNum) {
|
handleSpeed(0);
|
||||||
closeTimer();
|
mListener.onSubComplete(entity);
|
||||||
|
//如果子任务完成的数量和总任务数一致,表示任务组任务已经完成
|
||||||
|
if (mCompleteNum >= mTaskEntity.getEntity().getSubTask().size()){
|
||||||
|
closeTimer(false);
|
||||||
|
mListener.onComplete();
|
||||||
|
}
|
||||||
|
//如果子任务完成数量加上失败的数量和总任务数一致,则任务组停止下载
|
||||||
|
if (mCompleteNum + mFailNum >= mActualTaskNum) {
|
||||||
|
closeTimer(false);
|
||||||
mListener.onComplete();
|
mListener.onComplete();
|
||||||
}
|
}
|
||||||
handleSpeed(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void onFail() {
|
@Override public void onFail() {
|
||||||
entity.setFailNum(entity.getFailNum() + 1);
|
entity.setFailNum(entity.getFailNum() + 1);
|
||||||
saveData(IEntity.STATE_FAIL, -1);
|
saveData(IEntity.STATE_FAIL, lastLen);
|
||||||
handleSpeed(0);
|
handleSpeed(0);
|
||||||
reTry();
|
reTry();
|
||||||
}
|
}
|
||||||
@ -376,12 +397,27 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
|||||||
*/
|
*/
|
||||||
private void reTry() {
|
private void reTry() {
|
||||||
if (entity.getFailNum() < 5) {
|
if (entity.getFailNum() < 5) {
|
||||||
Downloader dt = mDownloaderMap.get(entity.getDownloadUrl());
|
reStartTask();
|
||||||
dt.startDownload();
|
|
||||||
} else {
|
} else {
|
||||||
mFailNum++;
|
mFailNum++;
|
||||||
|
mListener.onSubFail(entity);
|
||||||
|
//如果失败的任务数大于实际的下载任务数,任务组停止下载
|
||||||
|
if (mFailNum >= mActualTaskNum) {
|
||||||
|
closeTimer(false);
|
||||||
|
mListener.onStop(mCurrentLocation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reStartTask() {
|
||||||
|
Timer timer = new Timer();
|
||||||
|
timer.schedule(new TimerTask() {
|
||||||
|
@Override public void run() {
|
||||||
|
Downloader dt = mDownloaderMap.get(entity.getDownloadUrl());
|
||||||
|
dt.startDownload();
|
||||||
|
}
|
||||||
|
}, 3000);
|
||||||
|
}
|
||||||
|
|
||||||
private void handleSpeed(long speed) {
|
private void handleSpeed(long speed) {
|
||||||
entity.setSpeed(speed);
|
entity.setSpeed(speed);
|
||||||
@ -394,10 +430,14 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
|||||||
if (entity.isComplete()) {
|
if (entity.isComplete()) {
|
||||||
entity.setCompleteTime(System.currentTimeMillis());
|
entity.setCompleteTime(System.currentTimeMillis());
|
||||||
entity.setCurrentProgress(entity.getFileSize());
|
entity.setCurrentProgress(entity.getFileSize());
|
||||||
} else {
|
} else if (location > 0) {
|
||||||
entity.setCurrentProgress(location);
|
entity.setCurrentProgress(location);
|
||||||
}
|
}
|
||||||
entity.update();
|
entity.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override public void supportBreakpoint(boolean support) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,72 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.arialyy.aria.core.download.downloader;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author lyy
|
|
||||||
*/
|
|
||||||
public class DownloadListener implements IDownloadListener {
|
|
||||||
|
|
||||||
@Override public void onResume(long resumeLocation) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public void supportBreakpoint(boolean support) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public void onCancel() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public void onFail() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public void onPre() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public void onPostPre(long fileSize) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public void onProgress(long currentLocation) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public void onChildComplete(long finishLocation) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public void onStart(long startLocation) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public void onChildResume(long resumeLocation) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public void onStop(long stopLocation) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public void onComplete() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -111,7 +111,7 @@ class Downloader implements Runnable, IDownloadUtil {
|
|||||||
mTimer = new Timer(true);
|
mTimer = new Timer(true);
|
||||||
mTimer.schedule(new TimerTask() {
|
mTimer.schedule(new TimerTask() {
|
||||||
@Override public void run() {
|
@Override public void run() {
|
||||||
if (mConstance.isComplete()) {
|
if (mConstance.isComplete() || !mConstance.isDownloading) {
|
||||||
closeTimer();
|
closeTimer();
|
||||||
} else if (mConstance.CURRENT_LOCATION >= 0) {
|
} else if (mConstance.CURRENT_LOCATION >= 0) {
|
||||||
mListener.onProgress(mConstance.CURRENT_LOCATION);
|
mListener.onProgress(mConstance.CURRENT_LOCATION);
|
||||||
@ -343,7 +343,6 @@ class Downloader implements Runnable, IDownloadUtil {
|
|||||||
Long r = Long.parseLong(record + "");
|
Long r = Long.parseLong(record + "");
|
||||||
mConstance.CURRENT_LOCATION += r - startL;
|
mConstance.CURRENT_LOCATION += r - startL;
|
||||||
Log.d(TAG, "任务【" + mEntity.getFileName() + "】线程__" + i + "__恢复下载");
|
Log.d(TAG, "任务【" + mEntity.getFileName() + "】线程__" + i + "__恢复下载");
|
||||||
mListener.onChildResume(r);
|
|
||||||
startL = r;
|
startL = r;
|
||||||
recordL[rl] = i;
|
recordL[rl] = i;
|
||||||
rl++;
|
rl++;
|
||||||
|
@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.arialyy.aria.core.download.downloader;
|
||||||
|
|
||||||
|
import com.arialyy.aria.core.download.DownloadEntity;
|
||||||
|
import com.arialyy.aria.core.inf.IEventListener;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Aria.Lao on 2017/7/20.
|
||||||
|
* 下载任务组事件
|
||||||
|
*/
|
||||||
|
public interface IDownloadGroupListener extends IEventListener {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 子任务支持断点回调
|
||||||
|
*
|
||||||
|
* @param support true,支持;false 不支持
|
||||||
|
*/
|
||||||
|
void supportBreakpoint(boolean support, DownloadEntity subEntity);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 子任务开始下载\恢复下载
|
||||||
|
*/
|
||||||
|
void onSubStart(DownloadEntity subEntity);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 子任务停止下载
|
||||||
|
*/
|
||||||
|
void onSubStop(DownloadEntity subEntity);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 子任务下载完成
|
||||||
|
*/
|
||||||
|
void onSubComplete(DownloadEntity subEntity);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 子任务下载失败
|
||||||
|
*/
|
||||||
|
void onSubFail(DownloadEntity subEntity);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 子任务取消下载
|
||||||
|
*/
|
||||||
|
void onSubCancel(DownloadEntity subEntity);
|
||||||
|
}
|
@ -21,7 +21,7 @@ import com.arialyy.aria.core.inf.IEventListener;
|
|||||||
/**
|
/**
|
||||||
* 下载监听
|
* 下载监听
|
||||||
*/
|
*/
|
||||||
interface IDownloadListener extends IEventListener {
|
public interface IDownloadListener extends IEventListener {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 支持断点回调
|
* 支持断点回调
|
||||||
@ -29,14 +29,4 @@ interface IDownloadListener extends IEventListener {
|
|||||||
* @param support true,支持;false 不支持
|
* @param support true,支持;false 不支持
|
||||||
*/
|
*/
|
||||||
void supportBreakpoint(boolean support);
|
void supportBreakpoint(boolean support);
|
||||||
|
|
||||||
/**
|
|
||||||
* 单一线程的结束位置
|
|
||||||
*/
|
|
||||||
void onChildComplete(long finishLocation);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 子程恢复下载的位置
|
|
||||||
*/
|
|
||||||
void onChildResume(long resumeLocation);
|
|
||||||
}
|
}
|
@ -134,7 +134,6 @@ final class SingleThreadTask implements Runnable {
|
|||||||
+ mConfigEntity.THREAD_ID
|
+ mConfigEntity.THREAD_ID
|
||||||
+ "__下载完毕");
|
+ "__下载完毕");
|
||||||
writeConfig(true, 1);
|
writeConfig(true, 1);
|
||||||
mListener.onChildComplete(mConfigEntity.END_LOCATION);
|
|
||||||
CONSTANCE.COMPLETE_THREAD_NUM++;
|
CONSTANCE.COMPLETE_THREAD_NUM++;
|
||||||
if (CONSTANCE.isComplete()) {
|
if (CONSTANCE.isComplete()) {
|
||||||
File configFile = new File(mConfigFPath);
|
File configFile = new File(mConfigFPath);
|
||||||
|
@ -84,4 +84,13 @@ public abstract class AbsTaskEntity<ENTITY extends AbsEntity> extends DbEntity {
|
|||||||
public int code;
|
public int code;
|
||||||
|
|
||||||
public abstract ENTITY getEntity();
|
public abstract ENTITY getEntity();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取任务下载状态
|
||||||
|
*
|
||||||
|
* @return {@link IEntity}
|
||||||
|
*/
|
||||||
|
public int getState() {
|
||||||
|
return getEntity().getState();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ import java.util.concurrent.TimeUnit;
|
|||||||
* Created by AriaL on 2017/6/29.
|
* Created by AriaL on 2017/6/29.
|
||||||
* 单个下载任务的执行池
|
* 单个下载任务的执行池
|
||||||
*/
|
*/
|
||||||
public class DownloadExecutePool<TASK extends AbsTask> extends BaseExecutePool<TASK> {
|
class DownloadExecutePool<TASK extends AbsTask> extends BaseExecutePool<TASK> {
|
||||||
private final String TAG = "DownloadExecutePool";
|
private final String TAG = "DownloadExecutePool";
|
||||||
|
|
||||||
@Override protected int getMaxSize() {
|
@Override protected int getMaxSize() {
|
||||||
@ -41,6 +41,7 @@ public class DownloadExecutePool<TASK extends AbsTask> extends BaseExecutePool<T
|
|||||||
}
|
}
|
||||||
String url = task.getKey();
|
String url = task.getKey();
|
||||||
if (mExecuteQueue.contains(task)) {
|
if (mExecuteQueue.contains(task)) {
|
||||||
|
if (!task.isRunning()) return true;
|
||||||
Log.e(TAG, "队列中已经包含了该任务,任务key【" + url + "】");
|
Log.e(TAG, "队列中已经包含了该任务,任务key【" + url + "】");
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
|
@ -11,16 +11,16 @@
|
|||||||
<maxTaskNum value="2"/>
|
<maxTaskNum value="2"/>
|
||||||
|
|
||||||
<!--设置下载失败,重试次数,默认为10-->
|
<!--设置下载失败,重试次数,默认为10-->
|
||||||
<reTryNum value="10"/>
|
<reTryNum value="2"/>
|
||||||
|
|
||||||
<!--设置重试间隔,单位为毫秒,默认2000毫秒-->
|
<!--设置重试间隔,单位为毫秒,默认2000毫秒-->
|
||||||
<reTryInterval value="5000"/>
|
<reTryInterval value="5000"/>
|
||||||
|
|
||||||
<!--设置url连接超时时间,单位为毫秒,默认5000毫秒-->
|
<!--设置url连接超时时间,单位为毫秒,默认5000毫秒-->
|
||||||
<connectTimeOut value="10000"/>
|
<connectTimeOut value="1000"/>
|
||||||
|
|
||||||
<!--设置IO流读取时间,单位为毫秒,默认20000毫秒,该时间不能少于10000毫秒-->
|
<!--设置IO流读取时间,单位为毫秒,默认20000毫秒,该时间不能少于10000毫秒-->
|
||||||
<iOTimeOut value="20000"/>
|
<iOTimeOut value="2000"/>
|
||||||
|
|
||||||
<!--设置写文件buff大小,该数值大小不能小于2048,数值变小,下载速度会变慢-->
|
<!--设置写文件buff大小,该数值大小不能小于2048,数值变小,下载速度会变慢-->
|
||||||
<buffSize value="8192"/>
|
<buffSize value="8192"/>
|
||||||
|
Reference in New Issue
Block a user