From ebe01730743d125b993ad8aa48740cb613289270 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Sun, 24 Jun 2018 12:01:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=81=9C=E6=AD=A2=E5=85=A8?= =?UTF-8?q?=E9=83=A8=E4=BB=BB=E5=8A=A1=EF=BC=8C=E4=BC=9A=E9=87=8D=E8=AF=95?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/command/normal/ResumeAllCmd.java | 14 ++++---- .../aria/core/common/AbsThreadTask.java | 33 +++++++++++------ .../download/downloader/AbsGroupUtil.java | 15 ++++++-- .../downloader/HttpFileInfoThread.java | 16 ++++----- .../downloader/SimpleDownloadUtil.java | 12 +++++++ .../arialyy/aria/core/queue/AbsTaskQueue.java | 15 ++++---- app/src/main/AndroidManifest.xml | 4 +-- app/src/main/assets/aria_config.xml | 2 +- .../arialyy/simple/base/BaseApplication.java | 8 ++++- .../common/ConnectionChangeReceiver.java | 36 +++++++++++++++++++ .../multi_download/MultiDownloadActivity.java | 2 ++ app/src/main/res/values/strings.xml | 2 +- 12 files changed, 118 insertions(+), 41 deletions(-) create mode 100644 app/src/main/java/com/arialyy/simple/common/ConnectionChangeReceiver.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java index 03c6a823..3f1ff7e7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java @@ -112,16 +112,16 @@ final class ResumeAllCmd extends AbsNormalCmd { int maxTaskNum = mQueue.getMaxTaskNum(); if (mWaitList == null || mWaitList.isEmpty()) return; for (AbsTaskEntity te : mWaitList) { + if (te instanceof DownloadTaskEntity) { + mQueue = DownloadTaskQueue.getInstance(); + } else if (te instanceof UploadTaskEntity) { + mQueue = UploadTaskQueue.getInstance(); + } else if (te instanceof DownloadGroupTaskEntity) { + mQueue = DownloadGroupTaskQueue.getInstance(); + } if (mQueue.getCurrentExePoolNum() < maxTaskNum) { startTask(createTask(te)); } else { - if (te instanceof DownloadTaskEntity) { - mQueue = DownloadTaskQueue.getInstance(); - } else if (te instanceof UploadTaskEntity) { - mQueue = UploadTaskQueue.getInstance(); - } else if (te instanceof DownloadGroupTaskEntity) { - mQueue = DownloadGroupTaskQueue.getInstance(); - } createTask(te); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java index b170baf9..ca621b21 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java @@ -274,13 +274,15 @@ public abstract class AbsThreadTask 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 { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java index eb3f61c8..71b206c8 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java @@ -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); } } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java index 18fc33f4..04894024 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java @@ -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 diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java index 5fa95d79..d3d49f2d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java @@ -67,20 +67,19 @@ abstract class AbsTaskQueue - + diff --git a/app/src/main/assets/aria_config.xml b/app/src/main/assets/aria_config.xml index b9aa70df..55c155be 100644 --- a/app/src/main/assets/aria_config.xml +++ b/app/src/main/assets/aria_config.xml @@ -34,7 +34,7 @@ - + diff --git a/app/src/main/java/com/arialyy/simple/base/BaseApplication.java b/app/src/main/java/com/arialyy/simple/base/BaseApplication.java index c4fe6368..cffe0330 100644 --- a/app/src/main/java/com/arialyy/simple/base/BaseApplication.java +++ b/app/src/main/java/com/arialyy/simple/base/BaseApplication.java @@ -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)); } } \ No newline at end of file diff --git a/app/src/main/java/com/arialyy/simple/common/ConnectionChangeReceiver.java b/app/src/main/java/com/arialyy/simple/common/ConnectionChangeReceiver.java new file mode 100644 index 00000000..33a7831e --- /dev/null +++ b/app/src/main/java/com/arialyy/simple/common/ConnectionChangeReceiver.java @@ -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) { + // + //} + } +} \ No newline at end of file diff --git a/app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java b/app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java index c1cd770a..6ae5bcad 100644 --- a/app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java @@ -126,6 +126,7 @@ public class MultiDownloadActivity extends BaseActivity 三国群英传.apk - 海岛骑兵 + 海岛骑兵.apk