From a310df22813063ae507a03952fde53a26b6d8b47 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Mon, 13 Nov 2017 21:56:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=BA=BF=E7=A8=8B=E9=87=8D?= =?UTF-8?q?=E5=90=AF=E7=9A=84=E5=AF=BC=E8=87=B4=E4=B8=8B=E8=BD=BD=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E7=9A=84=E4=B8=80=E4=B8=AA=E9=97=AE=E9=A2=98=EF=BC=8C?= =?UTF-8?q?https://github.com/AriaLyy/Aria/issues/160?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../arialyy/aria/core/common/AbsFileer.java | 53 +++++++++++-------- .../aria/core/common/AbsThreadTask.java | 24 +++++---- .../aria/core/common/StateConstance.java | 2 +- .../download/downloader/FtpThreadTask.java | 6 ++- .../download/downloader/HttpThreadTask.java | 6 ++- .../core/upload/uploader/FtpThreadTask.java | 4 ++ .../com/arialyy/aria/util/CommonUtil.java | 26 ++++----- app/src/main/assets/aria_config.xml | 2 +- .../simple/download/FtpDownloadActivity.java | 2 +- .../simple/download/SingleTaskActivity.java | 4 +- 10 files changed, 78 insertions(+), 51 deletions(-) diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java index 616cdf88..c6066e96 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java @@ -40,7 +40,7 @@ import java.util.concurrent.Executors; */ public abstract class AbsFileer> implements Runnable, IUtil { - private final String TAG = "Downloader"; + private final String TAG = "AbsFileer"; protected IEventListener mListener; protected TASK_ENTITY mTaskEntity; protected ENTITY mEntity; @@ -50,7 +50,12 @@ public abstract class AbsFileer mTask = new SparseArray<>(); /** @@ -72,7 +77,7 @@ public abstract class AbsFileer keys = pro.keySet(); int num = 0; for (Object key : keys) { - if (String.valueOf(key).contains("_record_")) { + String str = String.valueOf(key); + if (str.contains("_record_")) { num++; + } else if (str.contains("_state_")) { + mCompleteThreadNum++; } } if (num == 0) { return true; } - mRealThreadNum = num; - for (int i = 0; i < mRealThreadNum; i++) { + mStartThreadNum = num; + for (int i = 0; i < mStartThreadNum; i++) { if (pro.getProperty(mTempFile.getName() + "_record_" + i) == null) { Object state = pro.getProperty(mTempFile.getName() + "_state_" + i); if (state != null && Integer.parseInt(state + "") == 1) { @@ -264,8 +275,8 @@ public abstract class AbsFileer 5000) { + if (System.currentTimeMillis() - mLastSaveTime > 5000 + && mChildCurrentLocation < mConfig.END_LOCATION) { mLastSaveTime = System.currentTimeMillis(); new Thread(new Runnable() { @Override public void run() { @@ -209,15 +211,15 @@ public abstract class AbsThreadTask= mConfig.END_LOCATION || isComplete) { + if (record >= mConfig.END_LOCATION || isComplete) { key = mConfig.TEMP_FILE.getName() + "_state_" + mConfig.THREAD_ID; value = "1"; + } else if (0 < record && record < mConfig.END_LOCATION) { + key = mConfig.TEMP_FILE.getName() + "_record_" + mConfig.THREAD_ID; + value = String.valueOf(record); } if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(value)) { File configFile = new File(mConfigFPath); diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/StateConstance.java b/Aria/src/main/java/com/arialyy/aria/core/common/StateConstance.java index 0feee240..386c55f0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/StateConstance.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/StateConstance.java @@ -56,7 +56,7 @@ public class StateConstance { * 所有子线程是否都已经下载失败 */ public boolean isFail() { - return FAIL_NUM + COMPLETE_THREAD_NUM >= THREAD_NUM; + return COMPLETE_THREAD_NUM != THREAD_NUM && FAIL_NUM + COMPLETE_THREAD_NUM >= THREAD_NUM; } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java index cc34b746..b932cf8c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java @@ -76,7 +76,7 @@ class FtpThreadTask extends AbsFtpThreadTask int len; while ((len = is.read(buffer)) != -1) { - if (STATE.isCancel || STATE.isStop){ + if (STATE.isCancel || STATE.isStop) { break; } if (mSleepTime > 0) Thread.sleep(mSleepTime); @@ -102,6 +102,10 @@ class FtpThreadTask extends AbsFtpThreadTask STATE.isRunning = false; mListener.onComplete(); } + if (STATE.isFail()) { + STATE.isRunning = false; + mListener.onFail(false); + } } catch (IOException e) { fail(mChildCurrentLocation, "下载失败【" + mConfig.URL + "】", e); } catch (Exception e) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java index a05c1cdd..3153d504 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java @@ -100,8 +100,12 @@ final class HttpThreadTask extends AbsThreadTask { STATE.isRunning = false; mListener.onComplete(); } + if (STATE.isFail()){ + STATE.isRunning = false; + mListener.onFail(false); + } } catch (IOException e) { fail(mChildCurrentLocation, "上传失败【" + mConfig.URL + "】", e); } catch (Exception e) { diff --git a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java index e4497dc1..090f8cd0 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -30,7 +30,6 @@ import com.arialyy.aria.core.command.group.GroupCmdFactory; import com.arialyy.aria.core.command.normal.AbsNormalCmd; import com.arialyy.aria.core.command.normal.NormalCmdFactory; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.download.DownloadGroupTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.inf.AbsGroupTaskEntity; @@ -148,7 +147,6 @@ public class CommonUtil { for (String str : strs) { url = url.replaceAll(str, URLEncoder.encode(str, "UTF-8")); } - } catch (UnsupportedEncodingException e) { e.printStackTrace(); } @@ -284,7 +282,6 @@ public class CommonUtil { */ public static void delDownloadGroupTaskConfig(boolean removeFile, DownloadGroupTaskEntity tEntity) { - DownloadGroupEntity entity = tEntity.getEntity(); List tasks = DbEntity.findDatas(DownloadTaskEntity.class, "groupName=?", tEntity.key); if (tasks != null && !tasks.isEmpty()) { @@ -293,17 +290,19 @@ public class CommonUtil { } } - File dir = new File(tEntity.getEntity().getDirPath()); - if (removeFile) { - if (dir.exists()) { - dir.delete(); - } - } else { - if (!tEntity.getEntity().isComplete()) { - dir.delete(); + if (tEntity.getEntity() != null) { + File dir = new File(tEntity.getEntity().getDirPath()); + if (removeFile) { + if (dir.exists()) { + dir.delete(); + } + } else { + if (!tEntity.getEntity().isComplete()) { + dir.delete(); + } } + tEntity.deleteData(); } - tEntity.deleteData(); } /** @@ -314,6 +313,9 @@ public class CommonUtil { */ public static void delUploadTaskConfig(boolean removeFile, UploadTaskEntity tEntity) { UploadEntity uEntity = tEntity.getEntity(); + if (uEntity == null) { + return; + } File file = new File(uEntity.getFilePath()); if (removeFile) { if (file.exists()) { diff --git a/app/src/main/assets/aria_config.xml b/app/src/main/assets/aria_config.xml index 7255c73f..92324c01 100644 --- a/app/src/main/assets/aria_config.xml +++ b/app/src/main/assets/aria_config.xml @@ -5,7 +5,7 @@ - + diff --git a/app/src/main/java/com/arialyy/simple/download/FtpDownloadActivity.java b/app/src/main/java/com/arialyy/simple/download/FtpDownloadActivity.java index a1b70935..1b4827e8 100644 --- a/app/src/main/java/com/arialyy/simple/download/FtpDownloadActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/FtpDownloadActivity.java @@ -38,7 +38,7 @@ public class FtpDownloadActivity extends BaseActivity { private static final String DOWNLOAD_URL = //"http://kotlinlang.org/docs/kotlin-docs.pdf"; //"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe"; - "http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk"; + //"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk"; //"http://sitcac.daxincf.cn/wp-content/uploads/swift_vido/01/element.mp4_1"; //"http://120.25.196.56:8000/filereq?id=15692406294&ipncid=105635&client=android&filename=20170819185541.avi"; //"http://down2.xiaoshuofuwuqi.com/d/file/filetxt/20170608/14/%BA%DA%CE%D7%CA%A6%E1%C8%C6%F0.txt"; @@ -59,7 +59,7 @@ public class SingleTaskActivity extends BaseActivity { //不支持断点的链接 //"http://ox.konsung.net:5555/ksdc-web/download/downloadFile/?fileName=ksdc_1.0.2.apk&rRange=0-"; //"http://172.18.104.50:8080/download/_302turn"; - //"http://gdown.baidu.com/data/wisegame/0904344dee4a2d92/QQ_718.apk"; + "http://gdown.baidu.com/data/wisegame/0904344dee4a2d92/QQ_718.apk"; //"http://172.21.1.99:8080/download/test+ 中文123.zip"; @Bind(R.id.start) Button mStart; @Bind(R.id.stop) Button mStop;