Skip to content
Permalink
Browse files

fix bug #422

  • Loading branch information...
AriaLyy committed Jul 16, 2019
1 parent b1bc452 commit a2604a943176139a8772d50e27b9aaa700a3c443
Showing with 176 additions and 119 deletions.
  1. +5 −1 Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
  2. +0 −1 Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java
  3. +3 −2 Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java
  4. +1 −2 Aria/src/main/java/com/arialyy/aria/core/common/NormalFileer.java
  5. +0 −3 Aria/src/main/java/com/arialyy/aria/core/download/DGTaskWrapper.java
  6. +2 −2 Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java
  7. +1 −1 Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java
  8. +4 −4 Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java
  9. +3 −3 Aria/src/main/java/com/arialyy/aria/core/download/group/AbsGroupUtil.java
  10. +2 −2 .../java/com/arialyy/aria/core/download/group/{ChildDownloadListener.java → ChildDLoadListener.java}
  11. +28 −16 Aria/src/main/java/com/arialyy/aria/core/download/group/{DownloadGroupUtil.java → DGroupUtil.java}
  12. +1 −1 Aria/src/main/java/com/arialyy/aria/core/download/group/FtpDirDownloadUtil.java
  13. +2 −2 Aria/src/main/java/com/arialyy/aria/core/download/group/GroupRunState.java
  14. +1 −1 ...in/java/com/arialyy/aria/core/download/group/{IDownloadGroupListener.java → IDGroupListener.java}
  15. +16 −13 Aria/src/main/java/com/arialyy/aria/core/download/group/SimpleSchedulers.java
  16. +7 −3 Aria/src/main/java/com/arialyy/aria/core/download/group/SubDLoadUtil.java
  17. +4 −0 Aria/src/main/java/com/arialyy/aria/core/inf/AbsEntity.java
  18. +3 −0 Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalEntity.java
  19. +10 −7 Aria/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java
  20. +17 −9 Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java
  21. +6 −2 DEV_LOG.md
  22. +26 −32 README.md
  23. +1 −1 app/src/main/assets/aria_config.xml
  24. +2 −1 app/src/main/java/com/arialyy/simple/core/download/HttpDownloadModule.java
  25. +6 −3 app/src/main/java/com/arialyy/simple/core/download/group/DownloadGroupActivity.java
  26. +18 −2 app/src/main/java/com/arialyy/simple/widget/SubStateLinearLayout.java
  27. +2 −1 app/src/main/res/values/strings.xml
  28. +1 −1 build.gradle
  29. BIN img/m3u8VodDownload.gif
  30. +4 −3 py/download.py
@@ -92,6 +92,7 @@
private AppConfig mAConfig;
private DGroupConfig mDGConfig;
private Handler mAriaHandler;
private DelegateWrapper mDbWrapper;

private AriaManager(Context context) {
APP = context.getApplicationContext();
@@ -172,7 +173,7 @@ private void initDb(Context context) {
dbConfig.delete();
}
}
DelegateWrapper.init(context.getApplicationContext());
mDbWrapper = DelegateWrapper.init(context.getApplicationContext());
}

private void initAria() {
@@ -192,6 +193,9 @@ private void amendTaskState() {
};
String sql = "UPDATE %s SET state=2 WHERE state IN (3,4,5,6)";
for (Class clazz : clazzs) {
if (!mDbWrapper.tableExists(clazz)) {
continue;
}
String temp = String.format(sql, clazz.getSimpleName());
DbEntity.exeSql(temp);
}
@@ -160,7 +160,6 @@ private synchronized void startTimer() {
}

public synchronized void closeTimer() {
ALog.i(TAG, "关闭定时器");
if (mTimer != null && !mTimer.isShutdown()) {
mTimer.shutdown();
}
@@ -173,9 +173,10 @@ protected TASK_WRAPPER getTaskWrapper() {
*/

public void setMaxSpeed(int speed) {
if (mSpeedBandUtil != null) {
mSpeedBandUtil.setMaxRate(speed);
if (mSpeedBandUtil == null) {
mSpeedBandUtil = new BandwidthLimiter(getMaxSpeed(), getConfig().startThreadNum);
}
mSpeedBandUtil.setMaxRate(speed);
}

/**
@@ -18,9 +18,7 @@
import android.os.Handler;
import android.os.Looper;
import com.arialyy.aria.core.download.BaseDListener;
import com.arialyy.aria.core.event.Event;
import com.arialyy.aria.core.event.EventMsgUtil;
import com.arialyy.aria.core.event.SpeedEvent;
import com.arialyy.aria.core.inf.AbsNormalEntity;
import com.arialyy.aria.core.inf.AbsTaskWrapper;
import com.arialyy.aria.core.inf.IEventListener;
@@ -190,6 +188,7 @@ private void handleNoSupportBP() {
if (mListener instanceof BaseDListener) {
((BaseDListener) mListener).supportBreakpoint(false);
}
mStartThreadNum = 1;

AbsThreadTask task = createSingThreadTask(mRecord.threadRecords.get(0), 1);
if (task == null) return;
@@ -46,9 +46,6 @@ public void setUnknownSize(boolean unknownSize) {
this.unknownSize = unknownSize;
}

/**
* 组名
*/
@Override public String getKey() {
return getEntity().getKey();
}
@@ -18,7 +18,7 @@
import android.os.Handler;
import com.arialyy.aria.core.common.BaseListener;
import com.arialyy.aria.core.common.RecordHandler;
import com.arialyy.aria.core.download.group.IDownloadGroupListener;
import com.arialyy.aria.core.download.group.IDGroupListener;
import com.arialyy.aria.core.inf.GroupSendParams;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.TaskSchedulerType;
@@ -34,7 +34,7 @@
*/
class DownloadGroupListener
extends BaseListener<DownloadGroupEntity, DGTaskWrapper, DownloadGroupTask>
implements IDownloadGroupListener {
implements IDGroupListener {
private GroupSendParams<DownloadGroupTask, DownloadEntity> mSeedEntity;

DownloadGroupListener(DownloadGroupTask task, Handler outHandler) {
@@ -107,7 +107,7 @@ public DownloadGroupTarget setFileSize(long fileSize) {
/**
* 如果无法获取到组合任务到总长度,请调用该方法,
* 请注意:
* 1、如果组合任务到子任务数过多,请不要使用该标志位,否则Aria将需要消耗大量的时间获取组合任务的总长度。
* 1、如果组合任务到子任务数过多,请不要使用该标志位,否则Aria将需要消耗大量的时间获取组合任务的总长度,直到获取完成组合任务总长度后才会执行下载
* 2、如果你的知道组合任务的总长度,请使用{@link #setFileSize(long)}设置组合任务的长度。
* 3、由于网络或其它原因的存在,这种方式获取的组合任务大小有可能是不准确的。
*/
@@ -20,9 +20,9 @@
import android.text.TextUtils;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.common.IUtil;
import com.arialyy.aria.core.download.group.DownloadGroupUtil;
import com.arialyy.aria.core.download.group.DGroupUtil;
import com.arialyy.aria.core.download.group.FtpDirDownloadUtil;
import com.arialyy.aria.core.download.group.IDownloadGroupListener;
import com.arialyy.aria.core.download.group.IDGroupListener;
import com.arialyy.aria.core.inf.AbsGroupTask;
import com.arialyy.aria.core.inf.AbsTaskWrapper;
import com.arialyy.aria.core.scheduler.ISchedulers;
@@ -57,9 +57,9 @@ public DownloadGroupEntity getEntity() {
@Override protected synchronized IUtil createUtil() {
switch (mTaskWrapper.getRequestType()) {
case AbsTaskWrapper.D_HTTP:
return new DownloadGroupUtil((IDownloadGroupListener) mListener, mTaskWrapper);
return new DGroupUtil((IDGroupListener) mListener, mTaskWrapper);
case AbsTaskWrapper.D_FTP_DIR:
return new FtpDirDownloadUtil((IDownloadGroupListener) mListener, mTaskWrapper);
return new FtpDirDownloadUtil((IDGroupListener) mListener, mTaskWrapper);
}
return null;
}
@@ -44,7 +44,7 @@
int HTTP_GROUP = 0xa2;

private long mCurrentLocation = 0;
protected IDownloadGroupListener mListener;
protected IDGroupListener mListener;
private ScheduledThreadPoolExecutor mTimer;
private long mUpdateInterval;
private boolean isStop = false, isCancel = false;
@@ -55,7 +55,7 @@
DGTaskWrapper mGTWrapper;
GroupRunState mState;

AbsGroupUtil(IDownloadGroupListener listener, DGTaskWrapper groupWrapper) {
AbsGroupUtil(IDGroupListener listener, DGTaskWrapper groupWrapper) {
mListener = listener;
mGTWrapper = groupWrapper;
mUpdateInterval = Configuration.getInstance().downloadCfg.getUpdateInterval();
@@ -195,7 +195,7 @@ public void onPreCancel() {
/**
* onStop前的操作
*
* @return 返回{@code true},直接回调{@link IDownloadGroupListener#onStop(long)}
* @return 返回{@code true},直接回调{@link IDGroupListener#onStop(long)}
*/
protected boolean onPreStop() {

@@ -26,15 +26,15 @@
/**
* 子任务事件监听
*/
class ChildDownloadListener implements IDownloadListener {
class ChildDLoadListener implements IDownloadListener {
private DownloadEntity subEntity;
private int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度
private long lastSaveTime;
private long lastLen;
private Handler schedulers;
private SubDLoadUtil loader;

ChildDownloadListener(Handler schedulers, SubDLoadUtil loader) {
ChildDLoadListener(Handler schedulers, SubDLoadUtil loader) {
this.loader = loader;
this.schedulers = schedulers;
subEntity = loader.getEntity();
@@ -25,25 +25,27 @@
import com.arialyy.aria.core.download.downloader.HttpFileInfoThread;
import com.arialyy.aria.core.inf.AbsEntity;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.IHttpFileLenAdapter;
import com.arialyy.aria.exception.AriaIOException;
import com.arialyy.aria.exception.BaseException;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* Created by AriaL on 2017/6/30.
* 任务组下载工具
*/
public class DownloadGroupUtil extends AbsGroupUtil implements IUtil {
public class DGroupUtil extends AbsGroupUtil implements IUtil {
private static final String TAG = "DownloadGroupUtil";
private final Object LOCK = new Object();
private ExecutorService mPool = null;
private boolean getLenComplete = false;
private List<DTaskWrapper> mTempWrapper = new ArrayList<>();

public DownloadGroupUtil(IDownloadGroupListener listener, DGTaskWrapper taskWrapper) {
public DGroupUtil(IDGroupListener listener, DGTaskWrapper taskWrapper) {
super(listener, taskWrapper);
}

@@ -101,18 +103,18 @@ private void getGroupSize() {
int failCount;

@Override public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (DTaskWrapper dTaskWrapper : mGTWrapper.getSubTaskWrapper()) {
cloneHeader(dTaskWrapper);
mPool.submit(new HttpFileInfoThread(dTaskWrapper, new OnFileInfoCallback() {
@Override public void onComplete(String url, CompleteInfo info) {
createAndStartSubLoader((DTaskWrapper) info.wrapper, false);
if (!mGTWrapper.isUnknownSize()) {
createAndStartSubLoader((DTaskWrapper) info.wrapper, false);
} else {
mTempWrapper.add((DTaskWrapper) info.wrapper);
}
count++;
checkGetSizeComplete(count, failCount);
ALog.d(TAG, "获取子任务信息完成");
}

@Override public void onFail(AbsEntity entity, BaseException e, boolean needRetry) {
@@ -122,6 +124,7 @@ private void getGroupSize() {
mListener.onSubFail((DownloadEntity) entity, new AriaIOException(TAG,
String.format("子任务获取文件长度失败,url:%s", ((DownloadEntity) entity).getUrl())));
checkGetSizeComplete(count, failCount);
mState.countFailNum(entity.getKey());
}
}));
}
@@ -133,6 +136,11 @@ private void getGroupSize() {
* 检查组合任务大小是否获取完成,获取完成后取消阻塞,并设置组合任务大小
*/
private void checkGetSizeComplete(int count, int failCount) {
if (failCount == mGTWrapper.getSubTaskWrapper().size()) {
mListener.onFail(false, new AriaIOException(TAG, "获取子任务长度失败"));
notifyLock();
return;
}
if (count == mGTWrapper.getSubTaskWrapper().size()) {
long size = 0;
for (DTaskWrapper wrapper : mGTWrapper.getSubTaskWrapper()) {
@@ -142,14 +150,18 @@ private void checkGetSizeComplete(int count, int failCount) {
mGTWrapper.getEntity().setFileSize(size);
mGTWrapper.getEntity().update();
getLenComplete = true;
ALog.d(TAG, String.format("获取组合任务长度完成,len:%s", size));
} else if (failCount == mGTWrapper.getSubTaskWrapper().size()) {
mListener.onFail(true, new AriaIOException(TAG, "获取子任务长度失败"));
}
IHttpFileLenAdapter lenAdapter = mGTWrapper.asHttp().getFileLenAdapter();
if (lenAdapter != null && lenAdapter.getClass().isAnonymousClass()) {
mGTWrapper.asHttp().setFileLenAdapter(null);
ALog.d(TAG, String.format("获取组合任务长度完成,组合任务总长度:%s,失败的只任务数:%s", size, failCount));
// 未知大小的组合任务,延迟下载
if (mGTWrapper.isUnknownSize()) {
for (DTaskWrapper wrapper : mTempWrapper) {
createAndStartSubLoader(wrapper, false);
}
}
notifyLock();
}
}

private void notifyLock() {
synchronized (LOCK) {
LOCK.notifyAll();
}
@@ -30,7 +30,7 @@
public class FtpDirDownloadUtil extends AbsGroupUtil {
private String TAG = "FtpDirDownloadUtil";

public FtpDirDownloadUtil(IDownloadGroupListener listener, DGTaskWrapper taskEntity) {
public FtpDirDownloadUtil(IDGroupListener listener, DGTaskWrapper taskEntity) {
super(listener, taskEntity);
}

@@ -51,7 +51,7 @@
/**
* 组合任务监听
*/
IDownloadGroupListener listener;
IDGroupListener listener;

/**
* 子任务队列
@@ -71,7 +71,7 @@

private String mGroupHash;

GroupRunState(String groupHash, IDownloadGroupListener listener, int subSize,
GroupRunState(String groupHash, IDGroupListener listener, int subSize,
SimpleSubQueue queue) {
this.listener = listener;
this.queue = queue;
@@ -23,7 +23,7 @@
* Created by Aria.Lao on 2017/7/20.
* 下载任务组事件
*/
public interface IDownloadGroupListener extends IDownloadListener {
public interface IDGroupListener extends IDownloadListener {

/**
* 子任务预处理
@@ -79,7 +79,7 @@ static SimpleSchedulers newInstance(GroupRunState state) {
* 2、stopNum + failNum + completeNum + cacheNum == subSize,则认为组合任务停止
* 3、failNum == subSize,只有全部的子任务都失败了,才能任务组合任务失败
*/
private synchronized void handleFail(final SubDLoadUtil loader) {
private synchronized void handleFail(final SubDLoadUtil loaderUtil) {
Configuration config = Configuration.getInstance();

long interval = config.dGroupCfg.getSubReTryInterval();
@@ -88,32 +88,34 @@ private synchronized void handleFail(final SubDLoadUtil loader) {

final int reTryNum = num;
if ((!NetUtils.isConnected(AriaManager.APP) && !isNotNetRetry)
|| loader.getEntity().getFailNum() > reTryNum) {
mQueue.removeTaskFromExecQ(loader);
mGState.listener.onSubFail(loader.getEntity(), new TaskException(TAG,
String.format("任务组子任务【%s】下载失败,下载地址【%s】", loader.getEntity().getFileName(),
loader.getEntity().getUrl())));
mGState.countFailNum(loader.getKey());
|| loaderUtil.getDownloader() == null // 如果获取不到文件信息,loader为空
|| loaderUtil.getEntity().getFailNum() > reTryNum) {
mQueue.removeTaskFromExecQ(loaderUtil);
mGState.listener.onSubFail(loaderUtil.getEntity(), new TaskException(TAG,
String.format("任务组子任务【%s】下载失败,下载地址【%s】", loaderUtil.getEntity().getFileName(),
loaderUtil.getEntity().getUrl())));
mGState.countFailNum(loaderUtil.getKey());
if (mGState.getFailNum() == mGState.getSubSize()
|| mGState.getStopNum() + mGState.getFailNum() + mGState.getCompleteNum()
== mGState.getSubSize()) {
mQueue.clear();
mGState.isRunning = false;
mGState.listener.onFail(true, new TaskException(TAG,
mGState.listener.onFail(false, new TaskException(TAG,
String.format("任务组【%s】下载失败", mGState.getGroupHash())));
} else {
startNext();
}
return;
}
final ScheduledThreadPoolExecutor timer = new ScheduledThreadPoolExecutor(1);

// 如果获取不到文件信息,loader为空
final ScheduledThreadPoolExecutor timer = new ScheduledThreadPoolExecutor(1);
timer.schedule(new Runnable() {
@Override public void run() {
AbsEntity entity = loader.getEntity();
AbsEntity entity = loaderUtil.getEntity();
if (entity.getFailNum() <= reTryNum) {
ALog.d(TAG, String.format("任务【%s】开始重试", loader.getEntity().getFileName()));
loader.reStart();
ALog.d(TAG, String.format("任务【%s】开始重试", loaderUtil.getEntity().getFileName()));
loaderUtil.reStart();
} else {
startNext();
}
@@ -159,7 +161,8 @@ private synchronized void handleComplete(SubDLoadUtil loader) {
mGState.updateCompleteNum();
ALog.d(TAG, String.format("总任务数:%s,完成的任务数:%s,失败的任务数:%s,停止的任务数:%s", mGState.getSubSize(),
mGState.getCompleteNum(), mGState.getFailNum(), mGState.getStopNum()));
if (mGState.getCompleteNum() == mGState.getSubSize()) {
if (mGState.getCompleteNum() + mGState.getFailNum() + mGState.getStopNum()
== mGState.getSubSize()) {
if (mGState.getStopNum() == 0 && mGState.getFailNum() == 0) {
mGState.listener.onComplete();
} else {

0 comments on commit a2604a9

Please sign in to comment.
You can’t perform that action at this time.