修复停止全部任务,会重试的bug

pull/330/head
laoyuyu 7 years ago
parent a7e238e1d1
commit ebe0173074
  1. 6
      Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java
  2. 23
      Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java
  3. 15
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java
  4. 16
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java
  5. 12
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java
  6. 15
      Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java
  7. 4
      app/src/main/AndroidManifest.xml
  8. 2
      app/src/main/assets/aria_config.xml
  9. 8
      app/src/main/java/com/arialyy/simple/base/BaseApplication.java
  10. 36
      app/src/main/java/com/arialyy/simple/common/ConnectionChangeReceiver.java
  11. 2
      app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java
  12. 2
      app/src/main/res/values/strings.xml

@ -112,9 +112,6 @@ final class ResumeAllCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
int maxTaskNum = mQueue.getMaxTaskNum();
if (mWaitList == null || mWaitList.isEmpty()) return;
for (AbsTaskEntity te : mWaitList) {
if (mQueue.getCurrentExePoolNum() < maxTaskNum) {
startTask(createTask(te));
} else {
if (te instanceof DownloadTaskEntity) {
mQueue = DownloadTaskQueue.getInstance();
} else if (te instanceof UploadTaskEntity) {
@ -122,6 +119,9 @@ final class ResumeAllCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
} else if (te instanceof DownloadGroupTaskEntity) {
mQueue = DownloadGroupTaskQueue.getInstance();
}
if (mQueue.getCurrentExePoolNum() < maxTaskNum) {
startTask(createTask(te));
} else {
createTask(te);
}
}

@ -274,13 +274,15 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY
ALog.w(TAG, String.format("任务【%s】thread__%s__重试失败,网络未连接", mConfig.TEMP_FILE.getName(),
mConfig.THREAD_ID));
}
if (mFailTimes < RETRY_NUM
&& needRetry
&& (NetUtils.isConnected(AriaManager.APP) || isNotNetRetry)
&& isBreak()) {
if (mFailTimes < RETRY_NUM && needRetry && (NetUtils.isConnected(AriaManager.APP)
|| isNotNetRetry) && isBreak()) {
mFailTimer = new Timer(true);
mFailTimer.schedule(new TimerTask() {
@Override public void run() {
if (isBreak()) {
handleSailState(false);
return;
}
mFailTimes++;
ALog.w(TAG, String.format("任务【%s】thread__%s__正在重试", mConfig.TEMP_FILE.getName(),
mConfig.THREAD_ID));
@ -290,13 +292,22 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY
}
}, RETRY_INTERVAL);
} else {
handleSailState(!isBreak());
}
}
/**
* 处理失败状态
*
* @param taskNeedReTry 任务是否需要重试{@code true} 需要
*/
private void handleSailState(boolean taskNeedReTry) {
STATE.FAIL_NUM++;
if (STATE.isFail()) {
STATE.isRunning = false;
STATE.isStop = true;
ALog.e(TAG, String.format("任务【%s】执行失败", mConfig.TEMP_FILE.getName()));
mListener.onFail(true);
}
mListener.onFail(taskNeedReTry);
}
}

@ -91,6 +91,7 @@ public abstract class AbsGroupUtil implements IUtil {
//任务组大小
int mGroupSize = 0;
private long mUpdateInterval = 1000;
private boolean isStop = false, isCancel = false;
AbsGroupUtil(IDownloadGroupListener listener, DownloadGroupTaskEntity groupEntity) {
mListener = listener;
@ -224,6 +225,7 @@ public abstract class AbsGroupUtil implements IUtil {
}
@Override public void cancel() {
isCancel = true;
closeTimer(false);
onCancel();
if (!mExePool.isShutdown()) {
@ -246,6 +248,7 @@ public abstract class AbsGroupUtil implements IUtil {
}
@Override public void stop() {
isStop = true;
closeTimer(false);
onStop();
if (!mExePool.isShutdown()) {
@ -294,6 +297,11 @@ public abstract class AbsGroupUtil implements IUtil {
}
@Override public void start() {
if (isStop || isCancel) {
isRunning = false;
closeTimer(false);
return;
}
isRunning = true;
clearState();
onStart();
@ -471,7 +479,7 @@ public abstract class AbsGroupUtil implements IUtil {
if (mCompleteNum == mGroupSize) {
closeTimer(false);
mListener.onComplete();
} else if (mFailMap.size() > 0 && mStopNum + mCompleteNum + mFailMap.size() == mGroupSize) {
} else if (mFailMap.size() > 0 && mStopNum + mCompleteNum + mFailMap.size() >= mGroupSize) {
//如果子任务完成数量加上失败的数量和总任务数一致,则任务组停止下载
closeTimer(false);
mListener.onStop(mCurrentLocation);
@ -492,7 +500,10 @@ public abstract class AbsGroupUtil implements IUtil {
private void reTry(boolean needRetry) {
Downloader dt = mDownloaderMap.get(subEntity.getUrl());
synchronized (AbsGroupUtil.LOCK) {
if (dt != null && !dt.isBreak() && needRetry && subEntity.getFailNum() < 5
if (dt != null
&& !dt.isBreak()
&& needRetry
&& subEntity.getFailNum() < 5
&& (NetUtils.isConnected(AriaManager.APP) || isNotNetRetry)) {
reStartTask(dt);
} else {

@ -40,14 +40,14 @@ class HttpFileInfoThread implements Runnable {
private DownloadEntity mEntity;
private DownloadTaskEntity mTaskEntity;
private int mConnectTimeOut;
private OnFileInfoCallback onFileInfoListener;
private OnFileInfoCallback onFileInfoCallback;
HttpFileInfoThread(DownloadTaskEntity taskEntity, OnFileInfoCallback callback) {
this.mTaskEntity = taskEntity;
mEntity = taskEntity.getEntity();
mConnectTimeOut =
AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getConnectTimeOut();
onFileInfoListener = callback;
onFileInfoCallback = callback;
}
@Override public void run() {
@ -168,9 +168,9 @@ class HttpFileInfoThread implements Runnable {
if (end) {
mTaskEntity.setChunked(isChunked);
mTaskEntity.update();
if (onFileInfoListener != null) {
if (onFileInfoCallback != null) {
CompleteInfo info = new CompleteInfo(code);
onFileInfoListener.onComplete(mEntity.getUrl(), info);
onFileInfoCallback.onComplete(mEntity.getUrl(), info);
}
}
}
@ -200,8 +200,8 @@ class HttpFileInfoThread implements Runnable {
ALog.d(TAG, "30x跳转,新url为【" + newUrl + "】");
if (TextUtils.isEmpty(newUrl) || newUrl.equalsIgnoreCase("null") || !newUrl.startsWith(
"http")) {
if (onFileInfoListener != null) {
onFileInfoListener.onFail(mEntity.getUrl(), "获取重定向链接失败", false);
if (onFileInfoCallback != null) {
onFileInfoCallback.onFail(mEntity.getUrl(), "获取重定向链接失败", false);
}
return;
}
@ -242,8 +242,8 @@ class HttpFileInfoThread implements Runnable {
private void failDownload(String errorMsg, boolean needRetry) {
ALog.e(TAG, errorMsg);
if (onFileInfoListener != null) {
onFileInfoListener.onFail(mEntity.getUrl(), errorMsg, needRetry);
if (onFileInfoCallback != null) {
onFileInfoCallback.onFail(mEntity.getUrl(), errorMsg, needRetry);
}
}
}

@ -34,6 +34,7 @@ public class SimpleDownloadUtil implements IUtil, Runnable {
private IDownloadListener mListener;
private Downloader mDownloader;
private DownloadTaskEntity mTaskEntity;
private boolean isStop = false, isCancel = false;
public SimpleDownloadUtil(DownloadTaskEntity entity, IDownloadListener downloadListener) {
mTaskEntity = entity;
@ -60,6 +61,7 @@ public class SimpleDownloadUtil implements IUtil, Runnable {
* 取消下载
*/
@Override public void cancel() {
isCancel = true;
mDownloader.cancel();
}
@ -67,6 +69,7 @@ public class SimpleDownloadUtil implements IUtil, Runnable {
* 停止下载
*/
@Override public void stop() {
isStop = true;
mDownloader.stop();
}
@ -74,6 +77,9 @@ public class SimpleDownloadUtil implements IUtil, Runnable {
* 多线程断点续传下载文件开始下载
*/
@Override public void start() {
if (isStop || isCancel){
return;
}
new Thread(this).start();
}
@ -86,12 +92,18 @@ public class SimpleDownloadUtil implements IUtil, Runnable {
}
private void failDownload(String msg, boolean needRetry) {
if (isStop || isCancel) {
return;
}
mListener.onFail(needRetry);
ErrorHelp.saveError(TAG, msg, "");
}
@Override public void run() {
mListener.onPre();
if (isStop || isCancel) {
return;
}
if (mTaskEntity.getEntity().getFileSize() <= 1
|| mTaskEntity.isRefreshInfo()
|| mTaskEntity.getRequestType() == AbsTaskEntity.D_FTP

@ -67,20 +67,19 @@ abstract class AbsTaskQueue<TASK extends AbsTask, TASK_ENTITY extends AbsTaskEnt
* 停止所有任务
*/
@Override public void stopAllTask() {
//mCachePool.clear();
//for(String key : mCachePool.getAllTask().keySet()){
// TASK task = mCachePool.getAllTask().get(key);
// if (task != null){
// task.stop();
// }
//}
mCachePool.clear();
for (String key : mExecutePool.getAllTask().keySet()) {
TASK task = mExecutePool.getAllTask().get(key);
if (task != null && task.isRunning()) {
task.stop();
}
}
for(String key : mCachePool.getAllTask().keySet()){
TASK task = mCachePool.getAllTask().get(key);
if (task != null){
task.stop();
}
}
mCachePool.clear();
}
/**

@ -16,10 +16,10 @@
<!--android:name=".test.TestActivity"-->
<!--android:name=".test.AnyRunActivity"-->
<!--android:name=".download.group.DownloadGroupActivity"-->
<!--android:name=".MainActivity"-->
<!--android:name=".test.AnyRunActivity"-->
<!--android:name=".test.TestGroupActivity"-->
<activity
android:name=".test.TestGroupActivity"
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

@ -34,7 +34,7 @@
<threadNum value="3"/>
<!--设置下载队列最大任务数, 默认为2-->
<maxTaskNum value="1"/>
<maxTaskNum value="2"/>
<!--设置下载失败,重试次数,默认为10-->
<reTryNum value="2"/>

@ -17,11 +17,14 @@
package com.arialyy.simple.base;
import android.app.Application;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.StrictMode;
import com.arialyy.aria.core.Aria;
import com.arialyy.frame.core.AbsFrame;
import com.arialyy.simple.BuildConfig;
import com.arialyy.simple.common.ConnectionChangeReceiver;
/**
* Created by Lyy on 2016/9/27.
@ -30,11 +33,14 @@ public class BaseApplication extends Application {
@Override public void onCreate() {
super.onCreate();
AbsFrame.init(this);
//Aria.init(this);
Aria.init(this);
if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
StrictMode.setThreadPolicy(
new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build());
}
registerReceiver(new ConnectionChangeReceiver(),
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
}

@ -0,0 +1,36 @@
package com.arialyy.simple.common;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import com.arialyy.aria.core.Aria;
public class ConnectionChangeReceiver extends BroadcastReceiver {
private static final String TAG = ConnectionChangeReceiver.class.getSimpleName();
@Override public void onReceive(Context context, Intent intent) {
Log.d(TAG, "网络状态改变");
/**
* 获得网络连接服务
*/
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo.State state =
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
if (NetworkInfo.State.CONNECTED == state) {
Aria.download(this).resumeAllTask();
return;
}
Aria.download(this).stopAllTask();
//state = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
//if (NetworkInfo.State.CONNECTED == state) {
//
//}
}
}

@ -126,6 +126,7 @@ public class MultiDownloadActivity extends BaseActivity<ActivityMultiDownloadBin
}
@DownloadGroup.onTaskStop void groupTaskStop(DownloadGroupTask task) {
ALog.d(TAG, String.format("group【%s】stop", task.getTaskName()));
mAdapter.updateState(task.getEntity());
}
@ -134,6 +135,7 @@ public class MultiDownloadActivity extends BaseActivity<ActivityMultiDownloadBin
}
@DownloadGroup.onTaskFail void groupTaskFail(DownloadGroupTask task) {
ALog.d(TAG, String.format("group【%s】fail", task.getTaskName()));
mAdapter.updateState(task.getEntity());
}

@ -97,7 +97,7 @@
<string-array name="group_names_2">
<item>三国群英传.apk</item>
<item>海岛骑兵</item>
<item>海岛骑兵.apk</item>
</string-array>
<string-array name="group_names_3">

Loading…
Cancel
Save