From 76bf0a33a948c50a9f87425ff4f81d9585a6bf5d Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Thu, 24 May 2018 22:41:30 +0800 Subject: [PATCH 01/10] =?UTF-8?q?=E5=88=A0=E9=99=A4=E8=AE=B0=E5=BD=95bug?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9B=E5=8A=A8=E6=80=81=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E9=95=BF=E5=BA=A6=E5=AE=9E=E7=8E=B0=E4=BF=AE=E6=94=B9=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/arialyy/aria/core/ConfigHelper.java | 9 -- .../arialyy/aria/core/common/AbsFileer.java | 29 ++-- .../aria/core/common/StateConstance.java | 5 + .../arialyy/aria/core/common/TaskRecord.java | 6 - .../aria/core/download/BaseDListener.java | 12 +- .../core/download/downloader/Downloader.java | 13 +- .../download/downloader/FtpThreadTask.java | 131 +++++++++++++----- .../download/downloader/HttpThreadTask.java | 81 ++++++++--- .../aria/core/scheduler/AbsSchedulers.java | 4 +- .../aria/core/upload/BaseUListener.java | 9 +- .../com/arialyy/aria/util/CommonUtil.java | 9 +- app/src/main/AndroidManifest.xml | 4 +- app/src/main/assets/aria_config.xml | 14 +- .../arialyy/simple/test/AnyRunActivity.java | 7 +- .../arialyy/simple/test/AnyRunnModule.java | 2 +- .../com/arialyy/aria/core/Configuration.java | 25 +--- 16 files changed, 230 insertions(+), 130 deletions(-) diff --git a/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java b/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java index d6cb98ec..f9113622 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java @@ -59,9 +59,6 @@ class ConfigHelper extends DefaultHandler { String value = attributes.getValue("value"); switch (qName) { - case "openDynamicFile": - loadOpenDynamicFile(value); - break; case "threadNum": loadThreadNum(value); break; @@ -117,12 +114,6 @@ class ConfigHelper extends DefaultHandler { } } - private void loadOpenDynamicFile(String value) { - if (isDownloadConfig) { - mDownloadConfig.openDynamicFile = checkBoolean(value) ? Boolean.valueOf(value) : false; - } - } - private void loadNotNetRetry(String value) { if (isDownloadConfig) { mDownloadConfig.notNetRetry = checkBoolean(value) ? Boolean.valueOf(value) : false; 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 bef04a0c..a8af4f7d 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 @@ -44,8 +44,11 @@ import java.util.concurrent.Executors; */ public abstract class AbsFileer> implements Runnable, IUtil { - public static final String STATE = "_state_"; - public static final String RECORD = "_record_"; + private static final String STATE = "_state_"; + private static final String RECORD = "_record_"; + /** + * 小于1m的文件不启用多线程 + */ protected static final long SUB_LEN = 1024 * 1024; private final String TAG = "AbsFileer"; @@ -57,16 +60,14 @@ public abstract class AbsFileer mTask = new SparseArray<>(); - /** - * 小于1m的文件不启用多线程 - */ + private Timer mTimer; @Deprecated private File mConfigFile; @@ -74,7 +75,7 @@ public abstract class AbsFileer(); mRecord.isGroupRecord = mTaskEntity.getEntity().isGroupChild(); - mRecord.isOpenDynamicFile = - AriaManager.getInstance(AriaManager.APP).getDownloadConfig().isOpenDynamicFile(); if (mRecord.isGroupRecord) { if (mTaskEntity.getEntity() instanceof DownloadEntity) { mRecord.dGroupName = ((DownloadEntity) mTaskEntity.getEntity()).getGroupName(); 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 25db8ea6..8aca4910 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 @@ -30,6 +30,11 @@ public class StateConstance { public boolean isCancel = false; public boolean isStop = false; public TaskRecord TASK_RECORD; + /** + * 是否是使用虚拟文件下载的 + * {@code true}是,{@code false}不是 + */ + public boolean isOpenDynamicFile = false; public StateConstance() { } diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/TaskRecord.java b/Aria/src/main/java/com/arialyy/aria/core/common/TaskRecord.java index cc0cc3fd..ea928de3 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/TaskRecord.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/TaskRecord.java @@ -73,10 +73,4 @@ public class TaskRecord extends DbEntity { @Ignore @Deprecated public String uGroupName; - - /** - * 是否是使用虚拟文件下载的 - * {@code true}是,{@code false}不是 - */ - public boolean isOpenDynamicFile = false; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java b/Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java index 3e1be934..56216403 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java @@ -19,8 +19,6 @@ import android.os.Handler; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.common.TaskRecord; import com.arialyy.aria.core.inf.AbsEntity; -import com.arialyy.aria.core.inf.AbsGroupEntity; -import com.arialyy.aria.core.inf.AbsNormalEntity; import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.IDownloadListener; @@ -158,14 +156,16 @@ class BaseDListener { } @Override protected int setNewTaskThreadNum() { + int threadNum = AriaManager.getInstance(mContext).getDownloadConfig().getThreadNum(); return // 小于1m的文件或是任务组的子任务、使用虚拟文件,线程数都是1 mEntity.getFileSize() <= SUB_LEN || mTaskEntity.getRequestType() == AbsTaskEntity.D_FTP_DIR || mTaskEntity.getRequestType() == AbsTaskEntity.DG_HTTP - || mRecord.isOpenDynamicFile + || threadNum == 1 ? 1 - : AriaManager.getInstance(mContext).getDownloadConfig().getThreadNum(); + : threadNum; } @Override protected boolean handleNewTask() { CommonUtil.createFile(mTempFile.getPath()); BufferedRandomAccessFile file = null; try { - file = new BufferedRandomAccessFile(new File(mTempFile.getPath()), "rwd", 8192); - //设置文件长度 - file.setLength(mRecord.isOpenDynamicFile ? 1 : mEntity.getFileSize()); + if (mTotalThreadNum > 1) { + file = new BufferedRandomAccessFile(new File(mTempFile.getPath()), "rwd", 8192); + //设置文件长度 + file.setLength(mEntity.getFileSize()); + } return true; } catch (IOException e) { failDownload("下载失败【downloadUrl:" 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 125c4422..d0f17778 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 @@ -24,8 +24,13 @@ import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.inf.IDownloadListener; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.BufferedRandomAccessFile; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.channels.Channels; +import java.nio.channels.FileChannel; +import java.nio.channels.ReadableByteChannel; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; @@ -36,10 +41,6 @@ import org.apache.commons.net.ftp.FTPReply; class FtpThreadTask extends AbsFtpThreadTask { private final String TAG = "FtpThreadTask"; private boolean isOpenDynamicFile; - /** - * 2M的动态长度 - */ - private final int LEN_INTERVAL = 1024 * 1024 * 2; FtpThreadTask(StateConstance constance, IDownloadListener listener, SubThreadConfig downloadInfo) { @@ -49,7 +50,7 @@ class FtpThreadTask extends AbsFtpThreadTask mReadTimeOut = manager.getDownloadConfig().getIOTimeOut(); mBufSize = manager.getDownloadConfig().getBuffSize(); isNotNetRetry = manager.getDownloadConfig().isNotNetRetry(); - isOpenDynamicFile = STATE.TASK_RECORD.isOpenDynamicFile; + isOpenDynamicFile = STATE.isOpenDynamicFile; setMaxSpeed(manager.getDownloadConfig().getMaxSpeed()); } @@ -58,7 +59,7 @@ class FtpThreadTask extends AbsFtpThreadTask mChildCurrentLocation = mConfig.START_LOCATION; FTPClient client = null; InputStream is = null; - BufferedRandomAccessFile file = null; + try { ALog.d(TAG, "任务【" + mConfig.TEMP_FILE.getName() @@ -94,31 +95,12 @@ class FtpThreadTask extends AbsFtpThreadTask return; } - file = new BufferedRandomAccessFile(mConfig.TEMP_FILE, "rwd", mBufSize); - file.seek(mConfig.START_LOCATION); - byte[] buffer = new byte[mBufSize]; - int len; - - while ((len = is.read(buffer)) != -1) { - if (STATE.isCancel || STATE.isStop) { - break; - } - if (mSleepTime > 0) Thread.sleep(mSleepTime); - if (isOpenDynamicFile) { - file.setLength( - STATE.CURRENT_LOCATION + LEN_INTERVAL < mEntity.getFileSize() ? STATE.CURRENT_LOCATION - + LEN_INTERVAL : mEntity.getFileSize()); - } - if (mChildCurrentLocation + len >= mConfig.END_LOCATION) { - len = (int) (mConfig.END_LOCATION - mChildCurrentLocation); - file.write(buffer, 0, len); - progress(len); - break; - } else { - file.write(buffer, 0, len); - progress(len); - } + if (isOpenDynamicFile) { + readDynamicFile(is); + } else { + readNormal(is); } + if (STATE.isCancel || STATE.isStop) return; ALog.i(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】线程__" + mConfig.THREAD_ID + "__下载完毕"); writeConfig(true, 1); @@ -138,9 +120,6 @@ class FtpThreadTask extends AbsFtpThreadTask fail(mChildCurrentLocation, "获取流失败", e); } finally { try { - if (file != null) { - file.close(); - } if (is != null) { is.close(); } @@ -153,6 +132,92 @@ class FtpThreadTask extends AbsFtpThreadTask } } + /** + * 动态长度文件读取方式 + */ + private void readDynamicFile(InputStream is) { + FileOutputStream fos = null; + FileChannel foc = null; + ReadableByteChannel fic = null; + try { + int len; + fos = new FileOutputStream(mConfig.TEMP_FILE, true); + foc = fos.getChannel(); + fic = Channels.newChannel(is); + ByteBuffer bf = ByteBuffer.allocate(mBufSize); + while ((len = fic.read(bf)) != -1) { + if (STATE.isCancel || STATE.isStop) { + break; + } + if (mSleepTime > 0) { + Thread.sleep(mSleepTime); + } + bf.flip(); + foc.write(bf); + bf.compact(); + progress(len); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (IOException e) { + fail(mChildCurrentLocation, "下载失败【" + mConfig.URL + "】", e); + } finally { + try { + if (fos != null) { + fos.close(); + } + if (foc != null) { + foc.close(); + } + if (fic != null) { + fic.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + /** + * 多线程写文件方式 + */ + private void readNormal(InputStream is) { + BufferedRandomAccessFile file = null; + try { + file = new BufferedRandomAccessFile(mConfig.TEMP_FILE, "rwd", mBufSize); + file.seek(mConfig.START_LOCATION); + byte[] buffer = new byte[mBufSize]; + int len; + while ((len = is.read(buffer)) != -1) { + if (STATE.isCancel || STATE.isStop) { + break; + } + if (mSleepTime > 0) Thread.sleep(mSleepTime); + if (mChildCurrentLocation + len >= mConfig.END_LOCATION) { + len = (int) (mConfig.END_LOCATION - mChildCurrentLocation); + file.write(buffer, 0, len); + progress(len); + break; + } else { + file.write(buffer, 0, len); + progress(len); + } + } + } catch (IOException e) { + fail(mChildCurrentLocation, "下载失败【" + mConfig.URL + "】", e); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + try { + if (file != null) { + file.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + @Override protected String getTaskType() { return "FTP_DOWNLOAD"; } 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 b3a99db6..74d50cfb 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 @@ -26,11 +26,16 @@ import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.BufferedRandomAccessFile; import com.arialyy.aria.util.CommonUtil; import java.io.BufferedInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; +import java.nio.ByteBuffer; +import java.nio.channels.Channels; +import java.nio.channels.FileChannel; +import java.nio.channels.ReadableByteChannel; /** * Created by lyy on 2017/1/18. @@ -38,10 +43,6 @@ import java.net.URL; */ final class HttpThreadTask extends AbsThreadTask { private final String TAG = "HttpThreadTask"; - /** - * 2M的动态长度 - */ - private final int LEN_INTERVAL = 1024 * 1024 * 2; private boolean isOpenDynamicFile; HttpThreadTask(StateConstance constance, IDownloadListener listener, @@ -52,7 +53,7 @@ final class HttpThreadTask extends AbsThreadTask 0) { + Thread.sleep(mSleepTime); + } + bf.flip(); + foc.write(bf); + bf.compact(); + progress(len); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (IOException e) { + fail(mChildCurrentLocation, "下载失败【" + mConfig.URL + "】", e); + } finally { + try { + if (fos != null) { + fos.close(); + } + if (foc != null) { + foc.close(); + } + if (fic != null) { + fic.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + /** * 读取chunk模式的文件流 * @@ -148,11 +198,6 @@ final class HttpThreadTask extends AbsThreadTask 0) { Thread.sleep(mSleepTime); } - if (isOpenDynamicFile) { - file.setLength( - STATE.CURRENT_LOCATION + LEN_INTERVAL < mEntity.getFileSize() ? STATE.CURRENT_LOCATION - + LEN_INTERVAL : mEntity.getFileSize()); - } file.write(buffer, 0, len); progress(len); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java index c4857df5..13dcad08 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java @@ -311,7 +311,9 @@ abstract class AbsSchedulers - + diff --git a/app/src/main/assets/aria_config.xml b/app/src/main/assets/aria_config.xml index 186611b1..de9e2732 100644 --- a/app/src/main/assets/aria_config.xml +++ b/app/src/main/assets/aria_config.xml @@ -11,17 +11,17 @@ - - - - + diff --git a/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java b/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java index 4ef74def..d99c955e 100644 --- a/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java +++ b/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java @@ -15,9 +15,9 @@ public class AnyRunActivity extends BaseActivity { AnyRunnModule module; String[] urls; int index = 0; - //String URL = "http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk"; + String URL = "http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk"; //String URL = "http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk"; - private final String URL = "ftp://192.168.29.140:21/download/AriaPrj.rar"; + //private final String URL = "ftp://192.168.29.140:21/download/AriaPrj.rar"; //String URL = "https://dl.genymotion.com/releases/genymotion-2.12.1/genymotion-2.12.1-vbox.exe"; @Override protected int setLayoutId() { @@ -42,7 +42,8 @@ public class AnyRunActivity extends BaseActivity { //} //for (int i = 0; i < 10; i++) { - module.startFtp(URL); + //module.startFtp(URL); + module.start(URL); //} //List list = Aria.download(this).getTotalTaskList(); //ALog.d(TAG, "size ==> " + list.size()); diff --git a/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java b/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java index 7d276022..19dd6b8a 100644 --- a/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java +++ b/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java @@ -68,7 +68,7 @@ public class AnyRunnModule { .load(url) //.addHeader("Accept-Encoding", "gzip") .setRequestMode(RequestEnum.GET) - .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg123456.apk") + .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg1234567.apk") //.resetState() .start(); //String[] urls = new String[] { diff --git a/aria/src/main/java/com/arialyy/aria/core/Configuration.java b/aria/src/main/java/com/arialyy/aria/core/Configuration.java index 85b71691..51539db5 100644 --- a/aria/src/main/java/com/arialyy/aria/core/Configuration.java +++ b/aria/src/main/java/com/arialyy/aria/core/Configuration.java @@ -366,6 +366,11 @@ class Configuration { String caName; /** * 下载线程数,下载线程数不能小于1 + * 注意: + * 1、线程下载数改变后,新的下载任务才会生效; + * 2、如果任务大小小于1m,该设置不会生效; + * 3、从3.4.1开始,如果线程数为1,文件初始化时将不再预占用对应长度的空间,下载多少byte,则占多大的空间; + * 对于采用多线程的任务或旧任务,依然采用原来的文件空间占用方式; */ int threadNum = 3; @@ -374,26 +379,6 @@ class Configuration { */ int maxSpeed = 0; - /** - * 是否开启动态文件,开启动态文件后初始化时将不占用磁盘空间,下载多少byte,占多少空间,效果见chrome的下载 - * 注意: - * 1、使用该功能,将自动关闭多线程下载; - * 2、对于已经采用了多线程的任务,依然采用原来的下载方式; - * 3、原本参数是true,任务没下载完成,就参数改为false,那么没下载完成的任务还是会按照参数修改前的方式下载,只有新任务才会根据参数调用不同的下载方式 - * {@code true}使用 - */ - boolean openDynamicFile = true; - - public DownloadConfig setOpenDynamicFile(boolean openDynamicFile) { - this.openDynamicFile = openDynamicFile; - saveKey("openDynamicFile", String.valueOf(openDynamicFile)); - return this; - } - - public boolean isOpenDynamicFile() { - return openDynamicFile; - } - public DownloadConfig setMaxTaskNum(int maxTaskNum) { oldMaxTaskNum = this.maxTaskNum; this.maxTaskNum = maxTaskNum; From 703078e750075928f3affc3b2110ea15513bf186 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Fri, 1 Jun 2018 00:20:53 +0800 Subject: [PATCH 02/10] fix bug https://github.com/AriaLyy/Aria/issues/248 https://github.com/AriaLyy/Aria/issues/247 --- .../com/arialyy/aria/core/download/DownloadReceiver.java | 4 ++-- .../core/download/downloader/HttpFileInfoThread.java | 9 +++++++-- DEV_LOG.md | 3 +++ .../java/com/arialyy/simple/test/AnyRunActivity.java | 8 +++++++- .../main/java/com/arialyy/simple/test/AnyRunnModule.java | 2 +- 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java index 9fccd6d5..e28836d8 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java @@ -265,7 +265,7 @@ public class DownloadReceiver extends AbsReceiver { * @return 如果url错误或查找不到数据,则返回null */ public DownloadEntity getDownloadEntity(String downloadUrl) { - if (CheckUtil.checkUrl(downloadUrl)) { + if (!CheckUtil.checkUrl(downloadUrl)) { return null; } return DbEntity.findFirst(DownloadEntity.class, "url=? and isGroupChild='false'", downloadUrl); @@ -278,7 +278,7 @@ public class DownloadReceiver extends AbsReceiver { * @return 如果url错误或查找不到数据,则返回null */ public DownloadTaskEntity getDownloadTask(String downloadUrl) { - if (CheckUtil.checkUrl(downloadUrl)) { + if (!CheckUtil.checkUrl(downloadUrl)) { return null; } return TEManager.getInstance().getTEntity(DownloadTaskEntity.class, downloadUrl); 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 10183650..4a73f1f2 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 @@ -133,7 +133,11 @@ class HttpFileInfoThread implements Runnable { mTaskEntity.setSupportBP(true); end = true; } else if (code == HttpURLConnection.HTTP_OK) { - if (conn.getHeaderField("Content-Type").equals("text/html")) { + String contentType = conn.getHeaderField("Content-Type"); + if (TextUtils.isEmpty(contentType)) { + return; + } + if (contentType.equals("text/html")) { BufferedReader reader = new BufferedReader(new InputStreamReader(ConnectionHelp.convertInputStream(conn))); StringBuilder sb = new StringBuilder(); @@ -155,7 +159,8 @@ class HttpFileInfoThread implements Runnable { failDownload("任务【" + mEntity.getUrl() + "】下载失败,错误码:404", true); } else if (code == HttpURLConnection.HTTP_MOVED_TEMP || code == HttpURLConnection.HTTP_MOVED_PERM - || code == HttpURLConnection.HTTP_SEE_OTHER) { + || code == HttpURLConnection.HTTP_SEE_OTHER + || code == 307) { handleUrlReTurn(conn, conn.getHeaderField("Location")); } else { failDownload("任务【" + mEntity.getUrl() + "】下载失败,错误码:" + code, true); diff --git a/DEV_LOG.md b/DEV_LOG.md index ece42758..b5ab1e9b 100644 --- a/DEV_LOG.md +++ b/DEV_LOG.md @@ -1,4 +1,7 @@ ## 开发日志 + + v_3.4.2 + - fix bug https://github.com/AriaLyy/Aria/issues/248 + - fix bug https://github.com/AriaLyy/Aria/issues/247 + v_3.4.1 - 移除记录配置文件,改用数据库记录任务记录 - 上传配置添加io超时时间、缓存大小配置 diff --git a/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java b/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java index d99c955e..bbbdb32c 100644 --- a/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java +++ b/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java @@ -1,8 +1,11 @@ package com.arialyy.simple.test; import android.os.Bundle; +import android.util.Log; import android.view.View; import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.databinding.ActivityTestBinding; @@ -15,7 +18,8 @@ public class AnyRunActivity extends BaseActivity { AnyRunnModule module; String[] urls; int index = 0; - String URL = "http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk"; + //String URL = "http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk"; + String URL = "http://d1.showself.com/download/showself_android-s236279_release.apk"; //String URL = "http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk"; //private final String URL = "ftp://192.168.29.140:21/download/AriaPrj.rar"; //String URL = "https://dl.genymotion.com/releases/genymotion-2.12.1/genymotion-2.12.1-vbox.exe"; @@ -30,6 +34,8 @@ public class AnyRunActivity extends BaseActivity { mBar.setVisibility(View.GONE); module = new AnyRunnModule(this); urls = getResources().getStringArray(R.array.group_urls); + DownloadTaskEntity entity = Aria.download(this).getDownloadTask(URL); + Log.d(TAG, entity + ""); } public void onClick(View view) { diff --git a/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java b/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java index 19dd6b8a..fd4fb8fe 100644 --- a/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java +++ b/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java @@ -68,7 +68,7 @@ public class AnyRunnModule { .load(url) //.addHeader("Accept-Encoding", "gzip") .setRequestMode(RequestEnum.GET) - .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg1234567.apk") + .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg12345678.apk") //.resetState() .start(); //String[] urls = new String[] { From 2e4ad16f3efc54f3849660524792b172fd59d7be Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Fri, 1 Jun 2018 19:16:31 +0800 Subject: [PATCH 03/10] fix bug https://github.com/AriaLyy/Aria/issues/250 --- .../download/downloader/AbsGroupUtil.java | 1 - .../downloader/DownloadGroupUtil.java | 34 +++++++++++++------ .../com/arialyy/aria/orm/DelegateFind.java | 5 +-- .../com/arialyy/aria/util/CommonUtil.java | 5 --- DEV_LOG.md | 1 + app/src/main/AndroidManifest.xml | 4 +-- .../arialyy/simple/test/AnyRunActivity.java | 3 +- .../arialyy/simple/test/AnyRunnModule.java | 4 +-- app/src/main/res/values/strings.xml | 4 +-- 9 files changed, 36 insertions(+), 25 deletions(-) diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java index 991d50bd..8bc5bd04 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java @@ -284,7 +284,6 @@ public abstract class AbsGroupUtil implements IUtil { } mTasksMap.put(te.getUrl(), te); } - updateFileSize(); } @Override public void start() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java index 32949d9f..ac9b1cea 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java @@ -35,6 +35,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { private final String TAG = "DownloadGroupUtil"; private ExecutorService mInfoPool; private int mInitCompleteNum, mInitFailNum; + private boolean isStop = false; /** * 文件信息回调组 @@ -53,6 +54,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { @Override public void onCancel() { super.onCancel(); + isStop = true; if (!mInfoPool.isShutdown()) { mInfoPool.shutdown(); } @@ -60,6 +62,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { @Override protected void onStop() { super.onStop(); + isStop = true; if (!mInfoPool.isShutdown()) { mInfoPool.shutdown(); } @@ -67,6 +70,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { @Override protected void onStart() { super.onStart(); + isStop = false; if (mCompleteNum == mGroupSize) { mListener.onComplete(); return; @@ -83,7 +87,9 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { if (taskEntity != null) { if (taskEntity.getState() != IEntity.STATE_FAIL && taskEntity.getState() != IEntity.STATE_WAIT) { + mInitCompleteNum++; createChildDownload(taskEntity); + checkStartFlow(); } else { mInfoPool.execute(createFileInfoThread(taskEntity)); } @@ -105,6 +111,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { int failNum = 0; @Override public void onComplete(String url, CompleteInfo info) { + if (isStop) return; DownloadTaskEntity te = mExeMap.get(url); if (te != null) { if (isNeedLoadFileSize) { @@ -112,15 +119,13 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { } createChildDownload(te); } - mInitCompleteNum ++; + mInitCompleteNum++; - if (mInitCompleteNum + mInitFailNum >= mGroupSize || !isNeedLoadFileSize) { - startRunningFlow(); - updateFileSize(); - } + checkStartFlow(); } @Override public void onFail(String url, String errorMsg, boolean needRetry) { + if (isStop) return; ALog.e(TAG, "任务【" + url + "】初始化失败。"); DownloadTaskEntity te = mExeMap.get(url); if (te != null) { @@ -136,14 +141,23 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { // mInitFailNum++; //} //failNum++; - mInitFailNum ++; - if (mInitCompleteNum + mInitFailNum >= mGroupSize || !isNeedLoadFileSize) { - startRunningFlow(); - updateFileSize(); - } + mInitFailNum++; + checkStartFlow(); } }; } return new HttpFileInfoThread(taskEntity, callback); } + + /** + * 检查能否启动下载流程 + */ + private void checkStartFlow() { + synchronized (DownloadGroupUtil.class) { + if (mInitCompleteNum + mInitFailNum >= mGroupSize || !isNeedLoadFileSize) { + startRunningFlow(); + updateFileSize(); + } + } + } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java b/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java index c2946ce1..ee7f2a4d 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java @@ -30,6 +30,7 @@ import java.lang.reflect.Field; import java.net.URLDecoder; import java.util.ArrayList; import java.util.Date; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -217,8 +218,8 @@ class DelegateFind extends AbsDelegate { } List wrappers = new ArrayList<>(); - Map tempParent = new WeakHashMap<>(); // 所有父表元素,key为父表主键的值 - Map> tempChild = new WeakHashMap<>(); // 所有的字表元素,key为父表主键的值 + Map tempParent = new LinkedHashMap<>(); // 所有父表元素,key为父表主键的值 + Map> tempChild = new LinkedHashMap<>(); // 所有的字表元素,key为父表主键的值 Object old = null; while (cursor.moveToNext()) { 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 629b64ce..122d115f 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -888,11 +888,6 @@ public class CommonUtil { ALog.w(TAG, "修改任务记录失败,新文件路径和旧文件路径一致"); return; } - File oldFile = new File(oldPath); - if (!oldFile.exists()) { - ALog.w(TAG, "修改任务记录失败,文件【" + oldPath + "】不存在"); - return; - } TaskRecord record = DbHelper.getTaskRecord(oldPath); if (record == null) { ALog.w(TAG, "修改任务记录失败,文件【" + oldPath + "】对应的任务记录不存在"); diff --git a/DEV_LOG.md b/DEV_LOG.md index b5ab1e9b..43af1526 100644 --- a/DEV_LOG.md +++ b/DEV_LOG.md @@ -2,6 +2,7 @@ + v_3.4.2 - fix bug https://github.com/AriaLyy/Aria/issues/248 - fix bug https://github.com/AriaLyy/Aria/issues/247 + - fix bug https://github.com/AriaLyy/Aria/issues/250 + v_3.4.1 - 移除记录配置文件,改用数据库记录任务记录 - 上传配置添加io超时时间、缓存大小配置 diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c3a993d0..c65e5e79 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -16,9 +16,9 @@ android:theme="@style/AppTheme.NoActionBar"> - + diff --git a/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java b/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java index bbbdb32c..bf4ab92f 100644 --- a/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java +++ b/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java @@ -19,7 +19,8 @@ public class AnyRunActivity extends BaseActivity { String[] urls; int index = 0; //String URL = "http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk"; - String URL = "http://d1.showself.com/download/showself_android-s236279_release.apk"; + String URL = "http://58.213.157.242:8081/sims_file/rest/v1/file/mshd_touchscreen_ms/guideFile/41c33556-dc4a-4d78-bb76-b9f627f94448.mp4/%E5%85%AB%E5%8D%A6%E6%B4%B2%E5%8D%97%E4%BA%AC%E5%86%9C%E4%B8%9A%E5%98%89%E5%B9%B4%E5%8D%8E0511.mp4"; + //String URL = "http://d1.showself.com/download/showself_android-s236279_release.apk"; //String URL = "http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk"; //private final String URL = "ftp://192.168.29.140:21/download/AriaPrj.rar"; //String URL = "https://dl.genymotion.com/releases/genymotion-2.12.1/genymotion-2.12.1-vbox.exe"; diff --git a/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java b/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java index fd4fb8fe..998acc63 100644 --- a/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java +++ b/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java @@ -38,7 +38,7 @@ public class AnyRunnModule { } @Download.onTaskRunning protected void running(DownloadTask task) { - Log.d(TAG, "running"); + Log.d(TAG, "running;Percent = " + task.getPercent()); } @Download.onTaskResume void taskResume(DownloadTask task) { @@ -68,7 +68,7 @@ public class AnyRunnModule { .load(url) //.addHeader("Accept-Encoding", "gzip") .setRequestMode(RequestEnum.GET) - .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg12345678.apk") + .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/abcd.mp4") //.resetState() .start(); //String[] urls = new String[] { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0706bd39..911363ca 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -64,7 +64,7 @@ - + http://hzdown.muzhiwan.com/2017/05/08/nl.noio.kingdom_59104935e56f0.apk http://hzdown.muzhiwan.com/2017/09/05/com.mir.iphone.empire83jie.mzw_59ae6d5a3638d.apk @@ -81,7 +81,7 @@ - + 战斗吧剑灵.apk 天魔幻想.apk From 426c148370677ae2daf8fd879d4a76bdfd2b7f3d Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Fri, 1 Jun 2018 20:11:44 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=AD=E6=96=AD?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aria/core/common/AbsFtpThreadTask.java | 2 +- .../aria/core/common/AbsThreadTask.java | 109 ++++++++++-------- .../download/downloader/FtpThreadTask.java | 8 +- .../download/downloader/HttpThreadTask.java | 10 +- .../core/upload/uploader/FtpThreadTask.java | 6 +- app/src/main/AndroidManifest.xml | 7 +- .../arialyy/simple/test/AnyRunnModule.java | 2 +- 7 files changed, 81 insertions(+), 63 deletions(-) diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFtpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFtpThreadTask.java index 3bce5644..70c0f142 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFtpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFtpThreadTask.java @@ -87,7 +87,7 @@ public abstract class AbsFtpThreadTask mEntity.getFileSize()) { + taskBreak = true; + fail(mChildCurrentLocation, "下载失败,下载长度超出文件大小", null, false); + return; + } mChildCurrentLocation += len; STATE.CURRENT_LOCATION += len; if (System.currentTimeMillis() - mLastSaveTime > 5000 @@ -182,27 +191,36 @@ public abstract class AbsThreadTask readNormal(is); } - if (STATE.isCancel || STATE.isStop) return; + if (isBreak()){ + return; + } ALog.i(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】线程__" + mConfig.THREAD_ID + "__下载完毕"); writeConfig(true, 1); STATE.COMPLETE_THREAD_NUM++; @@ -146,7 +148,7 @@ class FtpThreadTask extends AbsFtpThreadTask fic = Channels.newChannel(is); ByteBuffer bf = ByteBuffer.allocate(mBufSize); while ((len = fic.read(bf)) != -1) { - if (STATE.isCancel || STATE.isStop) { + if (isBreak()) { break; } if (mSleepTime > 0) { @@ -189,7 +191,7 @@ class FtpThreadTask extends AbsFtpThreadTask byte[] buffer = new byte[mBufSize]; int len; while ((len = is.read(buffer)) != -1) { - if (STATE.isCancel || STATE.isStop) { + if (isBreak()) { break; } if (mSleepTime > 0) Thread.sleep(mSleepTime); 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 74d50cfb..6e290f81 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 @@ -101,7 +101,7 @@ final class HttpThreadTask extends AbsThreadTask 0) { @@ -192,7 +192,7 @@ final class HttpThreadTask extends AbsThreadTask 0) { @@ -205,10 +205,8 @@ final class HttpThreadTask extends AbsThreadTask { file.seek(mConfig.START_LOCATION); } upload(client, file); - if (STATE.isCancel || STATE.isStop) return; + if (isBreak()) { + return; + } ALog.i(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】线程__" + mConfig.THREAD_ID + "__上传完毕"); writeConfig(true, 1); STATE.COMPLETE_THREAD_NUM++; @@ -131,7 +133,7 @@ class FtpThreadTask extends AbsFtpThreadTask { @Override public void onFtpInputStream(FTPClient client, long totalBytesTransferred, int bytesTransferred, long streamSize) { - if ((STATE.isCancel || STATE.isStop) && !isStoped) { + if (isBreak() && !isStoped) { try { isStoped = true; client.abor(); diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c65e5e79..a81e9694 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,7 +2,6 @@ - @@ -15,10 +14,10 @@ android:supportsRtl="true" android:theme="@style/AppTheme.NoActionBar"> - - + + diff --git a/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java b/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java index 998acc63..ff23272b 100644 --- a/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java +++ b/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java @@ -68,7 +68,7 @@ public class AnyRunnModule { .load(url) //.addHeader("Accept-Encoding", "gzip") .setRequestMode(RequestEnum.GET) - .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/abcd.mp4") + .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/abcds.exe") //.resetState() .start(); //String[] urls = new String[] { From e68cca5da3d8ed7a1276da3c707713c75b5ea6ca Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Sat, 2 Jun 2018 15:57:21 +0800 Subject: [PATCH 05/10] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E7=BB=84=E4=B8=8D=E8=83=BD=E8=87=AA=E5=8A=A8=E8=B0=83=E5=BA=A6?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E4=BB=BB=E5=8A=A1=E7=BB=84=E8=BF=9B?= =?UTF-8?q?=E5=BA=A6=E8=B6=85=E5=87=BA=E9=97=AE=E9=A2=98=EF=BC=8C=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E7=BB=84=E6=97=A0=E6=B3=95=E5=9B=9E=E8=B0=83onResume?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=9B=E6=99=AE=E9=80=9A=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E6=B7=BB=E5=8A=A0=E4=BF=AE=E6=AD=A3=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../arialyy/aria/core/common/AbsFileer.java | 9 +++-- .../aria/core/download/BaseDListener.java | 4 +-- .../core/download/DownloadGroupListener.java | 8 +++++ .../download/downloader/AbsGroupUtil.java | 34 +++++++++++++------ .../download/downloader/ConnectionHelp.java | 7 ++-- .../downloader/DownloadGroupUtil.java | 8 ++--- .../download/downloader/HttpThreadTask.java | 2 +- .../com/arialyy/aria/orm/AbsDelegate.java | 2 +- .../com/arialyy/aria/util/CommonUtil.java | 7 ++-- app/src/main/AndroidManifest.xml | 4 +-- app/src/main/assets/aria_config.xml | 2 +- .../simple/download/SingleTaskActivity.java | 9 ++--- 12 files changed, 62 insertions(+), 34 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 a8af4f7d..628f2114 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 @@ -16,6 +16,7 @@ package com.arialyy.aria.core.common; import android.content.Context; +import android.util.Log; import android.util.SparseArray; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.download.DownloadEntity; @@ -67,10 +68,8 @@ public abstract class AbsFileer mTask = new SparseArray<>(); - private Timer mTimer; - @Deprecated - private File mConfigFile; + @Deprecated private File mConfigFile; /** * 进度刷新间隔 */ @@ -465,6 +464,10 @@ public abstract class AbsFileer, TASK extends AbsTask> implements IDownloadListener { private static final String TAG = "BaseDListener"; - protected WeakReference outHandler; + WeakReference outHandler; private int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度 private long mLastLen = 0; //上一次发送长度 private boolean isFirst = true; protected ENTITY mEntity; protected TASK_ENTITY mTaskEntity; - protected TASK mTask; + private TASK mTask; private boolean isConvertSpeed = false; boolean isWait = false; private long mLastSaveTime; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java index 806d341f..d029b959 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java @@ -19,6 +19,7 @@ import android.os.Handler; import com.arialyy.aria.core.download.downloader.IDownloadGroupListener; import com.arialyy.aria.core.inf.GroupSendParams; import com.arialyy.aria.core.scheduler.ISchedulers; +import com.arialyy.aria.util.ALog; /** * Created by Aria.Lao on 2017/7/20. @@ -85,10 +86,17 @@ class DownloadGroupListener } private void saveCurrentLocation() { + if (mEntity.getSubEntities() == null || mEntity.getSubEntities().isEmpty()) { + ALog.w(TAG, "保存进度失败,子任务为null"); + return; + } long location = 0; for (DownloadEntity e : mEntity.getSubEntities()) { location += e.getCurrentProgress(); } + if (location > mEntity.getFileSize()) { + location = mEntity.getFileSize(); + } mEntity.setCurrentProgress(location); mEntity.update(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java index 8bc5bd04..aba4e232 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java @@ -43,11 +43,11 @@ public abstract class AbsGroupUtil implements IUtil { /** * FTP文件夹 */ - protected int FTP_DIR = 0xa1; + int FTP_DIR = 0xa1; /** * HTTP 任务组 */ - protected int HTTP_GROUP = 0xa2; + int HTTP_GROUP = 0xa2; /** * 任务组所有任务总长度 @@ -56,7 +56,7 @@ public abstract class AbsGroupUtil implements IUtil { long mCurrentLocation = 0; private ExecutorService mExePool; protected IDownloadGroupListener mListener; - protected DownloadGroupTaskEntity mGTEntity; + DownloadGroupTaskEntity mGTEntity; private boolean isRunning = false; private Timer mTimer; /** @@ -262,7 +262,7 @@ public abstract class AbsGroupUtil implements IUtil { } /** - * 预处理操作,由于属性的不同,http任务组在构造函数中就可以完成了 + * 预处理操作 * 而FTP文件夹的,需要获取完成所有子任务信息才算预处理完成 */ protected void onPre() { @@ -298,14 +298,13 @@ public abstract class AbsGroupUtil implements IUtil { @Override public void resume() { start(); - mListener.onResume(mCurrentLocation); } @Override public void setMaxSpeed(double maxSpeed) { } - private void clearState(){ + private void clearState() { mDownloaderMap.clear(); mFailMap.clear(); } @@ -325,7 +324,11 @@ public abstract class AbsGroupUtil implements IUtil { void startRunningFlow() { closeTimer(true); mListener.onPostPre(mTotalLen); - mListener.onStart(mCurrentLocation); + if (mCurrentLocation > 0) { + mListener.onResume(mCurrentLocation); + } else { + mListener.onStart(mCurrentLocation); + } startTimer(); } @@ -336,7 +339,16 @@ public abstract class AbsGroupUtil implements IUtil { if (!isRunning) { closeTimer(false); } else if (mCurrentLocation >= 0) { - mListener.onProgress(mCurrentLocation); + long t = 0; + for (DownloadTaskEntity te : mGTEntity.getSubTaskEntities()) { + if (te.getState() == IEntity.STATE_COMPLETE) { + t += te.getEntity().getFileSize(); + } else { + t += te.getEntity().getCurrentProgress(); + } + } + mCurrentLocation = t; + mListener.onProgress(t); } } }, 0, mUpdateInterval); @@ -345,8 +357,8 @@ public abstract class AbsGroupUtil implements IUtil { /** * 创建子任务下载器,默认创建完成自动启动 */ - Downloader createChildDownload(DownloadTaskEntity taskEntity) { - return createChildDownload(taskEntity, true); + void createChildDownload(DownloadTaskEntity taskEntity) { + createChildDownload(taskEntity, true); } /** @@ -408,7 +420,7 @@ public abstract class AbsGroupUtil implements IUtil { @Override public void onProgress(long currentLocation) { long speed = currentLocation - lastLen; - mCurrentLocation += speed; + //mCurrentLocation += speed; subEntity.setCurrentProgress(currentLocation); handleSpeed(speed); mListener.onSubRunning(subEntity); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java index 0803249a..58c37fd9 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java @@ -16,6 +16,7 @@ package com.arialyy.aria.core.download.downloader; import android.text.TextUtils; +import com.arialyy.aria.core.common.RequestEnum; import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.util.SSLContextUtil; import java.io.IOException; @@ -44,7 +45,7 @@ class ConnectionHelp { * @throws IOException */ static InputStream convertInputStream(HttpURLConnection connection) throws IOException { - String encoding = connection.getContentEncoding(); + String encoding = connection.getHeaderField("Content-Encoding"); if (TextUtils.isEmpty(encoding)) { return connection.getInputStream(); } @@ -88,7 +89,9 @@ class ConnectionHelp { */ static HttpURLConnection setConnectParam(DownloadTaskEntity entity, HttpURLConnection conn) throws ProtocolException { - conn.setRequestMethod(entity.getRequestEnum().name); + if (entity.getRequestEnum() == RequestEnum.POST){ + conn.setDoInput(true); + } Set keys = null; if (entity.getHeaders() != null && entity.getHeaders().size() > 0) { keys = entity.getHeaders().keySet(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java index ac9b1cea..1431026a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java @@ -35,7 +35,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { private final String TAG = "DownloadGroupUtil"; private ExecutorService mInfoPool; private int mInitCompleteNum, mInitFailNum; - private boolean isStop = false; + private boolean isStop = false, isStart = false; /** * 文件信息回调组 @@ -45,7 +45,6 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { public DownloadGroupUtil(IDownloadGroupListener listener, DownloadGroupTaskEntity taskEntity) { super(listener, taskEntity); mInfoPool = Executors.newCachedThreadPool(); - onPre(); } @Override int getTaskType() { @@ -69,7 +68,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { } @Override protected void onStart() { - super.onStart(); + onPre(); isStop = false; if (mCompleteNum == mGroupSize) { mListener.onComplete(); @@ -154,9 +153,10 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { */ private void checkStartFlow() { synchronized (DownloadGroupUtil.class) { - if (mInitCompleteNum + mInitFailNum >= mGroupSize || !isNeedLoadFileSize) { + if (!isStart && (mInitCompleteNum + mInitFailNum >= mGroupSize || !isNeedLoadFileSize)) { startRunningFlow(); updateFileSize(); + isStart = true; } } } 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 6e290f81..296b7b13 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 @@ -85,7 +85,7 @@ final class HttpThreadTask extends AbsThreadTask - + diff --git a/app/src/main/assets/aria_config.xml b/app/src/main/assets/aria_config.xml index de9e2732..1feaa308 100644 --- a/app/src/main/assets/aria_config.xml +++ b/app/src/main/assets/aria_config.xml @@ -25,7 +25,7 @@ - + diff --git a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java index f1a9dc69..9e93f01a 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -49,8 +49,9 @@ public class SingleTaskActivity extends BaseActivity { //"http://58.210.9.131/tpk/sipgt//TDLYZTGH.tpk"; //chunked 下载 //"https://static.donguo.me//video/ip/course/pfys_1.mp4"; //"https://www.baidu.com/link?url=_LFCuTPtnzFxVJByJ504QymRywIA1Z_T5xUxe9ZLuxcGM0C_RcdpWyB1eGjbJC-e5wv5wAKM4WmLMAS5KeF6EZJHB8Va3YqZUiaErqK_pxm&wd=&eqid=e8583fe70002d126000000065a99f864"; - "https://d.pcs.baidu.com/file/a02c89a2d479d4fd2756f3313d42491d?fid=4232431903-250528-1114369760340736&dstime=1525491372&rt=sh&sign=FDtAERVY-DCb740ccc5511e5e8fedcff06b081203-3C13vkOkuk4TqXvVYW05zj1K0ao%3D&expires=8h&chkv=1&chkbd=0&chkpc=et&dp-logid=8651730921842106225&dp-callid=0&r=165533013"; - + //"https://d.pcs.baidu.com/file/a02c89a2d479d4fd2756f3313d42491d?fid=4232431903-250528-1114369760340736&dstime=1525491372&rt=sh&sign=FDtAERVY-DCb740ccc5511e5e8fedcff06b081203-3C13vkOkuk4TqXvVYW05zj1K0ao%3D&expires=8h&chkv=1&chkbd=0&chkpc=et&dp-logid=8651730921842106225&dp-callid=0&r=165533013"; + //"http://apk500.bce.baidu-mgame.com/game/67000/67734/20170622040827_oem_5502845.apk?r=1"; + "https://dl.genymotion.com/releases/genymotion-2.12.1/genymotion-2.12.1-vbox.exe"; @Bind(R.id.start) Button mStart; @Bind(R.id.stop) Button mStop; @Bind(R.id.cancel) Button mCancel; @@ -238,8 +239,8 @@ public class SingleTaskActivity extends BaseActivity { //.addHeader("Accept-Encoding", "gzip, deflate") //.addHeader("DNT", "1") //.addHeader("Cookie", "BAIDUID=648E5FF020CC69E8DD6F492D1068AAA9:FG=1; BIDUPSID=648E5FF020CC69E8DD6F492D1068AAA9; PSTM=1519099573; BD_UPN=12314753; locale=zh; BDSVRTM=0") - .useServerFileName(true) - .setRequestMode(RequestEnum.GET) + //.useServerFileName(true) + //.setRequestMode(RequestEnum.GET) .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg3.apk") //.resetState() .start(); From e362621c3899fe60df42df4433560ff4649f9a1e Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Thu, 7 Jun 2018 21:59:14 +0800 Subject: [PATCH 06/10] =?UTF-8?q?ftp=E4=B8=8B=E8=BD=BD=E8=8E=B7=E5=8F=96dn?= =?UTF-8?q?s=E7=9A=84ip=E5=88=97=E8=A1=A8=E6=97=B6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E9=99=90=E5=88=B6=EF=BC=8C=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E7=AB=AF=E5=8F=A3=E4=B8=8D=E5=AD=98=E5=9C=A8=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E7=9A=84=E5=B4=A9=E6=BA=83=E9=97=AE=E9=A2=98=EF=BC=8Chttps://g?= =?UTF-8?q?ithub.com/AriaLyy/Aria/issues/251=20=C3=A4=E4=BF=AE=E5=A4=8Dhtt?= =?UTF-8?q?ps://github.com/AriaLyy/Aria/issues/233?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Aria/build.gradle | 4 +- .../core/command/group/GroupCancelCmd.java | 2 +- .../aria/core/command/normal/StartCmd.java | 5 +- .../arialyy/aria/core/common/AbsFileer.java | 44 ++++++--- .../aria/core/common/AbsFtpInfoThread.java | 56 +++++++++-- .../aria/core/common/AbsFtpThreadTask.java | 2 +- .../aria/core/common/AbsThreadTask.java | 81 +++++++++------- .../aria/core/download/BaseNormalTarget.java | 6 +- .../core/download/DownloadGroupListener.java | 1 + .../core/download/DownloadGroupTarget.java | 8 +- .../download/downloader/AbsGroupUtil.java | 53 +++++++---- .../downloader/DownloadGroupUtil.java | 17 +++- .../download/downloader/FtpThreadTask.java | 37 +++----- .../download/downloader/HttpThreadTask.java | 27 ++---- .../com/arialyy/aria/core/inf/AbsTarget.java | 2 +- .../arialyy/aria/core/queue/AbsTaskQueue.java | 16 ++-- .../aria/core/scheduler/AbsSchedulers.java | 3 +- .../core/upload/uploader/FtpThreadTask.java | 28 ++---- .../core/upload/uploader/HttpThreadTask.java | 6 +- .../main/java/com/arialyy/aria/util/ALog.java | 3 + .../com/arialyy/aria/util/CommonUtil.java | 95 ++++++++----------- .../java/com/arialyy/aria/util/Regular.java | 9 -- README.md | 3 +- app/src/main/AndroidManifest.xml | 5 +- app/src/main/assets/aria_config.xml | 2 +- .../download/group/DownloadGroupActivity.java | 5 +- .../arialyy/simple/test/AnyRunActivity.java | 8 +- .../com/arialyy/simple/test/TestActivity.java | 8 +- aria/src/main/AndroidManifest.xml | 1 + 29 files changed, 302 insertions(+), 235 deletions(-) diff --git a/Aria/build.gradle b/Aria/build.gradle index 18b2d152..572c2daa 100644 --- a/Aria/build.gradle +++ b/Aria/build.gradle @@ -26,8 +26,8 @@ dependencies { testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile project(':AriaAnnotations') - compile 'com.arialyy.aria:aria-ftp-plug:1.0.3' +// compile 'com.arialyy.aria:aria-ftp-plug:1.0.3' - // compile project(':AriaFtpPlug') + compile project(':AriaFtpPlug') } apply from: 'bintray-release.gradle' diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupCancelCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupCancelCmd.java index 8c0c7c75..9205443d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupCancelCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupCancelCmd.java @@ -19,7 +19,7 @@ import com.arialyy.aria.core.inf.AbsGroupTaskEntity; /** * Created by AriaL on 2017/6/29. - * 删除任务组 + * 删除任务组子任务 */ class GroupCancelCmd extends AbsGroupCmd { /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java index b3da691f..51bad424 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java @@ -86,8 +86,11 @@ class StartCmd extends AbsNormalCmd { } } } else { - if (!task.isRunning()) { + //任务没执行并且执行队列中没有该任务,才认为任务没有运行中 + if (!task.isRunning() && !mQueue.taskIsRunning(task.getKey())) { resumeTask(); + }else { + ALog.w(TAG, String.format("任务【%s】已经在运行", task.getTaskName())); } } if (mQueue.getCurrentCachePoolNum() == 0) { 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 a8af4f7d..40ef2e0a 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 @@ -16,7 +16,6 @@ package com.arialyy.aria.core.common; import android.content.Context; -import android.util.SparseArray; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.inf.AbsNormalEntity; @@ -29,8 +28,10 @@ import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.DbHelper; import java.io.File; import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.Timer; @@ -65,8 +66,7 @@ public abstract class AbsFileer mTask = new SparseArray<>(); - + private Map mTask = new HashMap<>(); private Timer mTimer; @Deprecated @@ -188,7 +188,7 @@ public abstract class AbsFileer 0 ? temp : 0; } else { tRecord.startLocation = 0; @@ -373,7 +373,7 @@ public abstract class AbsFileer= 0) { Long r = tr.startLocation; //记录的位置需要在线程区间中 @@ -447,7 +447,7 @@ public abstract class AbsFileer 0) { @@ -488,6 +488,26 @@ public abstract class AbsFileer keys = mTask.keySet(); + for (Integer key : keys) { + AbsThreadTask task = mTask.get(key); + if (task != null && !task.isThreadComplete()) { + task.getConfig().START_LOCATION = task.getCurrentLocation(); + ALog.d(TAG, String.format("任务【%s】开始重试,线程__%s__【开始位置:%s,结束位置:%s】", mEntity.getFileName(), + key, task.getConfig().START_LOCATION, task.getConfig().END_LOCATION)); + mFixedThreadPool.execute(task); + } + } + } + /** * 处理新任务 * @@ -496,7 +516,7 @@ public abstract class AbsFileer config = new SubThreadConfig<>(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFtpInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFtpInfoThread.java index a8fabcd8..c2b649ec 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFtpInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFtpInfoThread.java @@ -26,6 +26,7 @@ import com.arialyy.aria.util.Regular; import java.io.File; import java.io.IOException; import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.net.ftp.FTP; @@ -72,7 +73,7 @@ public abstract class AbsFtpInfoThread 0) { - ALog.i(TAG, "路径【" + setRemotePath() + "】下的文件列表 ==================================="); + ALog.i(TAG, + String.format("路径【%s】下的文件列表 ===================================", setRemotePath())); for (FTPFile file : files1) { ALog.d(TAG, file.toString()); } @@ -114,7 +116,8 @@ public abstract class AbsFtpInfoThread 0) { client = new FTPClient(); InetAddress ip = InetAddress.getByName(urlEntity.hostName); - client.setConnectTimeout(10000); // 连接10s超时 + client.setConnectTimeout(mConnectTimeOut); // 连接10s超时 client.connect(ip, Integer.parseInt(urlEntity.port)); mTaskEntity.getUrlEntity().validAddr = ip; } else { - InetAddress[] ips = InetAddress.getAllByName(urlEntity.hostName); + DNSQueryThread dnsThread = new DNSQueryThread(urlEntity.hostName); + dnsThread.start(); + dnsThread.join(mConnectTimeOut); + InetAddress[] ips = dnsThread.getIps(); client = connect(new FTPClient(), ips, 0, Integer.parseInt(urlEntity.port)); } @@ -206,7 +212,8 @@ public abstract class AbsFtpInfoThread mConfig; protected ENTITY mEntity; protected TASK_ENTITY mTaskEntity; - private int mFailNum = 0; - private String mTaskType; + private int mFailTimes = 0; private Timer mFailTimer; private long mLastSaveTime; private ExecutorService mConfigThreadPool; protected int mConnectTimeOut; //连接超时时间 protected int mReadTimeOut; //流读取的超时时间 protected boolean isNotNetRetry = false; //断网情况是否重试 - protected boolean taskBreak = false; //任务中断 + private boolean taskBreak = false; //任务中断 private Thread mConfigThread = new Thread(new Runnable() { @Override public void run() { @@ -76,13 +75,10 @@ public abstract class AbsThreadTask= Build.VERSION_CODES.LOLLIPOP; } @@ -124,21 +142,16 @@ public abstract class AbsThreadTask } else if (getTargetType() == FTP) { filePath += mEntity.getFileName(); } + } else { + // http文件名设置 + if (TextUtils.isEmpty(mEntity.getFileName())) { + mEntity.setFileName(file.getName()); + } } - mEntity.setFileName(file.getName()); //设置文件保存路径,如果新文件路径和就文件路径不同,则修改路径 if (!filePath.equals(mEntity.getDownloadPath())) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java index 806d341f..48cb6167 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java @@ -89,6 +89,7 @@ class DownloadGroupListener for (DownloadEntity e : mEntity.getSubEntities()) { location += e.getCurrentProgress(); } + mEntity.setCurrentProgress(location); mEntity.update(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java index bb872563..dd148419 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java @@ -143,9 +143,13 @@ public class DownloadGroupTarget extends BaseGroupTarget { reChangeDirPath(mDirPathTemp); } - if (!mSubNameTemp.isEmpty()) { - updateSingleSubFileName(); + if (mSubNameTemp.isEmpty()) { + for (String url : mUrls) { + int lastIndex = url.lastIndexOf(File.separator); + mSubNameTemp.add(url.substring(lastIndex + 1, url.length())); + } } + updateSingleSubFileName(); return true; } return false; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java index 8bc5bd04..f2ac361e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java @@ -39,6 +39,7 @@ import java.util.concurrent.Executors; * 任务组核心逻辑 */ public abstract class AbsGroupUtil implements IUtil { + private static final Object LOCK = new Object(); private final String TAG = "AbsGroupUtil"; /** * FTP文件夹 @@ -158,6 +159,9 @@ public abstract class AbsGroupUtil implements IUtil { DownloadTaskEntity det = mTasksMap.get(url); if (det != null) { mTotalLen -= det.getEntity().getFileSize(); + mCurrentLocation -= det.getEntity().getCurrentProgress(); + mExeMap.remove(det.getKey()); + mFailMap.remove(det.getKey()); mGroupSize--; if (mGroupSize == 0) { closeTimer(false); @@ -305,12 +309,12 @@ public abstract class AbsGroupUtil implements IUtil { } - private void clearState(){ + private void clearState() { mDownloaderMap.clear(); mFailMap.clear(); } - private void closeTimer(boolean isRunning) { + void closeTimer(boolean isRunning) { this.isRunning = isRunning; if (mTimer != null) { mTimer.purge(); @@ -374,6 +378,8 @@ public abstract class AbsGroupUtil implements IUtil { private int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度 private long lastSaveTime; private long lastLen = 0; + private Timer timer; + private boolean isNotNetRetry = false; ChildDownloadListener(DownloadTaskEntity entity) { subTaskEntity = entity; @@ -381,6 +387,7 @@ public abstract class AbsGroupUtil implements IUtil { subEntity.setFailNum(0); lastLen = subEntity.getCurrentProgress(); lastSaveTime = System.currentTimeMillis(); + isNotNetRetry = AriaManager.getInstance(AriaManager.APP).getDownloadConfig().isNotNetRetry(); } @Override public void onPre() { @@ -423,7 +430,7 @@ public abstract class AbsGroupUtil implements IUtil { saveData(IEntity.STATE_STOP, stopLocation); handleSpeed(0); mListener.onSubStop(subEntity); - synchronized (AbsGroupUtil.class) { + synchronized (AbsGroupUtil.LOCK) { mStopNum++; if (mStopNum + mCompleteNum + mFailMap.size() == mGroupSize) { closeTimer(false); @@ -439,16 +446,17 @@ public abstract class AbsGroupUtil implements IUtil { } @Override public void onComplete() { + subEntity.setComplete(true); saveData(IEntity.STATE_COMPLETE, subEntity.getFileSize()); handleSpeed(0); mListener.onSubComplete(subEntity); - synchronized (ChildDownloadListener.class) { + synchronized (AbsGroupUtil.LOCK) { mCompleteNum++; //如果子任务完成的数量和总任务数一致,表示任务组任务已经完成 - if (mCompleteNum >= mGroupSize) { + 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); @@ -464,41 +472,48 @@ public abstract class AbsGroupUtil implements IUtil { } /** - * 重试下载 + * 重试下载,只有全部都下载失败才会执行任务组的整体重试,否则只会执行单个子任务的重试 */ private void reTry(boolean needRetry) { - synchronized (ChildDownloadListener.class) { - if (subEntity.getFailNum() < 5 && needRetry && NetUtils.isConnected(AriaManager.APP)) { + synchronized (AbsGroupUtil.LOCK) { + if (subEntity.getFailNum() < 5 + && needRetry && (NetUtils.isConnected(AriaManager.APP) || isNotNetRetry)) { reStartTask(); } else { mFailMap.put(subTaskEntity.getUrl(), subTaskEntity); mListener.onSubFail(subEntity); - //如果失败的任务数大于实际的下载任务数,任务组停止下载 - if (mFailMap.size() >= mExeMap.size()) { + if (mFailMap.size() == mExeMap.size() || mFailMap.size() + mCompleteNum == mGroupSize) { closeTimer(false); - if (mFailMap.size() == mGroupSize) { //所有任务都失败了,则认为该任务组已经失败 - mListener.onFail(true); - } else { - mListener.onStop(mCurrentLocation); - } + } + if (mFailMap.size() == mGroupSize) { + mListener.onFail(true); + } else if (mFailMap.size() + mCompleteNum >= mExeMap.size()) { + mListener.onStop(mCurrentLocation); } } } } private void reStartTask() { - Timer timer = new Timer(); + if (timer != null) { + timer.purge(); + timer.cancel(); + } + timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { Downloader dt = mDownloaderMap.get(subEntity.getUrl()); - dt.start(); + if (dt != null) { + dt.retryThradTask(); + } } }, 3000); } private void handleSpeed(long speed) { subEntity.setSpeed(speed); - subEntity.setConvertSpeed(speed <= 0 ? "" : CommonUtil.formatFileSize(speed) + "/s"); + subEntity.setConvertSpeed( + speed <= 0 ? "" : String.format("%s/s", CommonUtil.formatFileSize(speed))); subEntity.setPercent((int) (subEntity.getFileSize() <= 0 ? 0 : subEntity.getCurrentProgress() * 100 / subEntity.getFileSize())); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java index ac9b1cea..ed7f8361 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java @@ -34,7 +34,14 @@ import java.util.concurrent.Executors; public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { private final String TAG = "DownloadGroupUtil"; private ExecutorService mInfoPool; - private int mInitCompleteNum, mInitFailNum; + /** + * 初始化完成的任务数 + */ + private int mInitCompleteNum; + /** + * 初始化失败的任务数 + */ + private int mInitFailNum; private boolean isStop = false; /** @@ -126,7 +133,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { @Override public void onFail(String url, String errorMsg, boolean needRetry) { if (isStop) return; - ALog.e(TAG, "任务【" + url + "】初始化失败。"); + ALog.e(TAG, String.format("任务【%s】初始化失败", url)); DownloadTaskEntity te = mExeMap.get(url); if (te != null) { mFailMap.put(url, te); @@ -154,7 +161,11 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { */ private void checkStartFlow() { synchronized (DownloadGroupUtil.class) { - if (mInitCompleteNum + mInitFailNum >= mGroupSize || !isNeedLoadFileSize) { + if (mInitFailNum == mGroupSize){ + closeTimer(false); + mListener.onFail(true); + } + if (mInitCompleteNum + mInitFailNum == mGroupSize || !isNeedLoadFileSize) { startRunningFlow(); updateFileSize(); } 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 d9dc4b4c..547e2aa4 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 @@ -61,15 +61,9 @@ class FtpThreadTask extends AbsFtpThreadTask InputStream is = null; try { - ALog.d(TAG, "任务【" - + mConfig.TEMP_FILE.getName() - + "】线程__" - + mConfig.THREAD_ID - + "__开始下载【开始位置 : " - + mConfig.START_LOCATION - + ",结束位置:" - + mConfig.END_LOCATION - + "】"); + ALog.d(TAG, + String.format("任务【%s】线程__%s__开始下载【开始位置 : %s,结束位置:%s】", mConfig.TEMP_FILE.getName(), + mConfig.THREAD_ID, mConfig.START_LOCATION, mConfig.END_LOCATION)); client = createClient(); if (client == null) return; if (mConfig.START_LOCATION > 0) { @@ -78,18 +72,20 @@ class FtpThreadTask extends AbsFtpThreadTask //发送第二次指令时,还需要再做一次判断 int reply = client.getReplyCode(); if (!FTPReply.isPositivePreliminary(reply) && reply != FTPReply.COMMAND_OK) { - fail(mChildCurrentLocation, "获取文件信息错误,错误码为:" + reply + ",msg:" + client.getReplyString(), + fail(mChildCurrentLocation, + String.format("获取文件信息错误,错误码为:%s,msg:%s", reply, client.getReplyString()), null); client.disconnect(); return; } String remotePath = new String(mTaskEntity.getUrlEntity().remotePath.getBytes(charSet), SERVER_CHARSET); - ALog.i(TAG, "remotePath【" + remotePath + "】"); + ALog.i(TAG, String.format("remotePath【%s】", remotePath)); is = client.retrieveFileStream(remotePath); reply = client.getReplyCode(); if (!FTPReply.isPositivePreliminary(reply)) { - fail(mChildCurrentLocation, "获取流失败,错误码为:" + reply + ",msg:" + client.getReplyString(), + fail(mChildCurrentLocation, + String.format("获取流失败,错误码为:%s,msg:%s", reply, client.getReplyString()), null); client.disconnect(); return; @@ -101,11 +97,12 @@ class FtpThreadTask extends AbsFtpThreadTask readNormal(is); } - if (isBreak()){ + if (isBreak()) { return; } - ALog.i(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】线程__" + mConfig.THREAD_ID + "__下载完毕"); - writeConfig(true, 1); + ALog.i(TAG, + String.format("任务【%s】线程__%s__下载完毕", mConfig.TEMP_FILE.getName(), mConfig.THREAD_ID)); + writeConfig(true, mConfig.END_LOCATION); STATE.COMPLETE_THREAD_NUM++; if (STATE.isComplete()) { STATE.TASK_RECORD.deleteData(); @@ -117,7 +114,7 @@ class FtpThreadTask extends AbsFtpThreadTask mListener.onFail(false); } } catch (IOException e) { - fail(mChildCurrentLocation, "下载失败【" + mConfig.URL + "】", e); + fail(mChildCurrentLocation, String.format("下载失败【%s】", mConfig.URL), e); } catch (Exception e) { fail(mChildCurrentLocation, "获取流失败", e); } finally { @@ -162,7 +159,7 @@ class FtpThreadTask extends AbsFtpThreadTask } catch (InterruptedException e) { e.printStackTrace(); } catch (IOException e) { - fail(mChildCurrentLocation, "下载失败【" + mConfig.URL + "】", e); + fail(mChildCurrentLocation, String.format("下载失败【%s】", mConfig.URL), e); } finally { try { if (fos != null) { @@ -206,7 +203,7 @@ class FtpThreadTask extends AbsFtpThreadTask } } } catch (IOException e) { - fail(mChildCurrentLocation, "下载失败【" + mConfig.URL + "】", e); + fail(mChildCurrentLocation, String.format("下载失败【%s】", mConfig.URL), e); } catch (InterruptedException e) { e.printStackTrace(); } finally { @@ -219,8 +216,4 @@ class FtpThreadTask extends AbsFtpThreadTask } } } - - @Override protected String getTaskType() { - return "FTP_DOWNLOAD"; - } } 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 6e290f81..77419656 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 @@ -67,18 +67,12 @@ final class HttpThreadTask extends AbsThreadTask { FTPClient client = null; BufferedRandomAccessFile file = null; try { - ALog.d(TAG, "任务【" - + mConfig.TEMP_FILE.getName() - + "】线程__" - + mConfig.THREAD_ID - + "__开始上传【开始位置 : " - + mConfig.START_LOCATION - + ",结束位置:" - + mConfig.END_LOCATION - + "】"); + ALog.d(TAG, + String.format("任务【%s】线程__%s__开始上传【开始位置 : %s,结束位置:%s】", mConfig.TEMP_FILE.getName(), + mConfig.THREAD_ID, mConfig.START_LOCATION, mConfig.END_LOCATION)); client = createClient(); if (client == null) return; initPath(); @@ -85,8 +79,9 @@ class FtpThreadTask extends AbsFtpThreadTask { if (isBreak()) { return; } - ALog.i(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】线程__" + mConfig.THREAD_ID + "__上传完毕"); - writeConfig(true, 1); + ALog.i(TAG, + String.format("任务【%s】线程__%s__上传完毕", mConfig.TEMP_FILE.getName(), mConfig.THREAD_ID)); + writeConfig(true, mConfig.END_LOCATION); STATE.COMPLETE_THREAD_NUM++; if (STATE.isComplete()) { STATE.TASK_RECORD.deleteData(); @@ -98,7 +93,7 @@ class FtpThreadTask extends AbsFtpThreadTask { mListener.onFail(false); } } catch (IOException e) { - fail(mChildCurrentLocation, "上传失败【" + mConfig.URL + "】", e); + fail(mChildCurrentLocation, String.format("上传失败【%s】", mConfig.URL), e); } catch (Exception e) { fail(mChildCurrentLocation, "获取流失败", e); } finally { @@ -127,7 +122,7 @@ class FtpThreadTask extends AbsFtpThreadTask { throws IOException { try { - ALog.d(TAG, "remotePath【" + remotePath + "】"); + ALog.d(TAG, String.format("remotePath【%s】", remotePath)); client.storeFile(remotePath, new FtpFISAdapter(bis), new OnFtpInputStreamListener() { boolean isStoped = false; @@ -155,7 +150,8 @@ class FtpThreadTask extends AbsFtpThreadTask { int reply = client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { if (reply != FTPReply.TRANSFER_ABORTED) { - fail(mChildCurrentLocation, "上传文件错误,错误码为:" + reply + ",msg:" + client.getReplyString(), + fail(mChildCurrentLocation, + String.format("上传文件错误,错误码为:%s,msg:%s", reply, client.getReplyString()), null); } if (client.isConnected()) { @@ -163,8 +159,4 @@ class FtpThreadTask extends AbsFtpThreadTask { } } } - - @Override protected String getTaskType() { - return "FTP_UPLOAD"; - } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java index e0c4c641..ac14c2a7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java @@ -62,7 +62,7 @@ class HttpThreadTask extends AbsThreadTask { @Override public void run() { File uploadFile = new File(mEntity.getFilePath()); if (!uploadFile.exists()) { - ALog.e(TAG, "【" + mEntity.getFilePath() + "】,文件不存在。"); + ALog.e(TAG, String.format("【%s】,文件不存在。", mEntity.getFilePath())); fail(); return; } @@ -212,8 +212,4 @@ class HttpThreadTask extends AbsThreadTask { mOutputStream.close(); return response.toString(); } - - @Override protected String getTaskType() { - return "HTTP_UPLOAD"; - } } diff --git a/Aria/src/main/java/com/arialyy/aria/util/ALog.java b/Aria/src/main/java/com/arialyy/aria/util/ALog.java index d4b2dc96..5e5d4b7e 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/ALog.java +++ b/Aria/src/main/java/com/arialyy/aria/util/ALog.java @@ -117,6 +117,9 @@ public class ALog { * 将异常信息转换为字符串 */ public static String getExceptionString(Throwable ex) { + if (ex == null) { + return ""; + } StringBuilder err = new StringBuilder(); err.append("ExceptionDetailed:\n"); err.append("====================Exception Info====================\n"); 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 122d115f..176ad2b6 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -246,53 +246,30 @@ public class CommonUtil { * @param url 输入的url{@code String url = "ftp://z:z@dygod18.com:21211/[电影天堂www.dy2018.com]猩球崛起3:终极之战BD国英双语中英双字.mkv";} */ public static FtpUrlEntity getFtpUrlInfo(String url) { + Uri uri = Uri.parse(url); + + String userInfo = uri.getUserInfo(), remotePath = uri.getPath(); + ALog.d(TAG, + String.format("scheme = %s, user = %s, host = %s, port = %s, path = %s", uri.getScheme(), + userInfo, uri.getHost(), uri.getPort(), remotePath)); + FtpUrlEntity entity = new FtpUrlEntity(); entity.url = url; - //String regex = "(\\w+)://(.*):(\\d*)/(.*)"; - String regex = Regular.REG_FTP_URL; - Pattern p = Pattern.compile(regex); - Matcher m = p.matcher(url); - if (m.find() && m.groupCount() > 0) { - entity.protocol = m.group(1); - String str = m.group(2); - if (str.contains("@")) { - entity.needLogin = true; - //String hostReg = "(\\w+):?(\\w+)?@(.*)"; - String hostReg = Regular.REG_FTP_HOST_NAME; - Pattern hp = Pattern.compile(hostReg); - Matcher hm = hp.matcher(str); - if (hm.find() && hm.groupCount() > 0) { - entity.user = hm.group(1); - entity.password = TextUtils.isEmpty(hm.group(2)) ? "" : hm.group(2); - entity.hostName = hm.group(3); - } + entity.hostName = uri.getHost(); + entity.port = uri.getPort() == -1 ? "21" : String.valueOf(uri.getPort()); + if (!TextUtils.isEmpty(userInfo)) { + String[] temp = userInfo.split(":"); + if (temp.length == 2) { + entity.user = temp[0]; + entity.password = temp[1]; } else { - entity.hostName = str; + entity.user = userInfo; } - entity.port = m.group(3); - //entity.remotePath = TextUtils.isEmpty(m.group(4)) ? "/" : "/" + m.group(4); - entity.remotePath = TextUtils.isEmpty(m.group(4)) ? "/" : m.group(4); } + entity.remotePath = TextUtils.isEmpty(remotePath) ? "/" : remotePath; return entity; } - /** - * 通过url获取FTP文件的remotePath - * - * @return remotePath。如果没有找到,返回"" - */ - public static String getRemotePath(String url) { - String remotePath = null; - String regex = Regular.REG_FTP_URL; - Pattern p = Pattern.compile(regex); - Matcher m = p.matcher(url); - if (m.find() && m.groupCount() > 0) { - return TextUtils.isEmpty(m.group(4)) ? "" : "/" + m.group(4); - } - ALog.w(TAG, "链接【" + url + "】没有找到remotePath"); - return ""; - } - /** * 转换Url * @@ -300,25 +277,27 @@ public class CommonUtil { * @return 转换后的地址 */ public static String convertUrl(String url) { - if (hasDoubleCharacter(url)) { - //预先处理空格,URLEncoder只会把空格转换为+ - url = url.replaceAll(" ", "%20"); - //匹配双字节字符(包括汉字在内) - String regex = Regular.REG_DOUBLE_CHAR_AND_SPACE; - Pattern p = Pattern.compile(regex); - Matcher m = p.matcher(url); - Set strs = new HashSet<>(); - while (m.find()) { - strs.add(m.group()); - } - try { - for (String str : strs) { - url = url.replaceAll(str, URLEncoder.encode(str, "UTF-8")); - } - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } - } + Uri uri = Uri.parse(url); + url = uri.toString(); + //if (hasDoubleCharacter(url)) { + // //预先处理空格,URLEncoder只会把空格转换为+ + // url = url.replaceAll(" ", "%20"); + // //匹配双字节字符(包括汉字在内) + // String regex = Regular.REG_DOUBLE_CHAR_AND_SPACE; + // Pattern p = Pattern.compile(regex); + // Matcher m = p.matcher(url); + // Set strs = new HashSet<>(); + // while (m.find()) { + // strs.add(m.group()); + // } + // try { + // for (String str : strs) { + // url = url.replaceAll(str, URLEncoder.encode(str, "UTF-8")); + // } + // } catch (UnsupportedEncodingException e) { + // e.printStackTrace(); + // } + //} return url; } diff --git a/Aria/src/main/java/com/arialyy/aria/util/Regular.java b/Aria/src/main/java/com/arialyy/aria/util/Regular.java index 829b5dda..ac364a1d 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/Regular.java +++ b/Aria/src/main/java/com/arialyy/aria/util/Regular.java @@ -5,15 +5,6 @@ package com.arialyy.aria.util; * 正则表达式 */ public interface Regular { - /** - * ftp地址 - */ - String REG_FTP_URL = "(\\w+)://(.*):(\\d*)/(.*)"; - - /** - * ftp主机、用户、密码分割 - */ - String REG_FTP_HOST_NAME = "(\\w+):?(\\w+)?@(.*)"; /** * 获取文件名 diff --git a/README.md b/README.md index 1558e602..e3380213 100644 --- a/README.md +++ b/README.md @@ -126,8 +126,7 @@ protected void onCreate(Bundle savedInstanceState) { ## 其他 有任何问题,可以在[issues](https://github.com/AriaLyy/Aria/issues)给我留言反馈。
- 在提交问题前,希望你已经查看过[wiki](https://github.com/AriaLyy/Aria/wiki)或搜索过[issues](https://github.com/AriaLyy/Aria/issues)。
- Aria交流群:524329160(加群条件:对aria有过star、commit、issues) + 在提交问题前,希望你已经查看过[wiki](https://aria.laoyuyu.me/aria_doc/)或搜索过[issues](https://github.com/AriaLyy/Aria/issues)。
*** License diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a81e9694..d7856d59 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -14,8 +14,9 @@ android:supportsRtl="true" android:theme="@style/AppTheme.NoActionBar"> - + + @@ -33,7 +34,7 @@ - + diff --git a/app/src/main/assets/aria_config.xml b/app/src/main/assets/aria_config.xml index de9e2732..cb5efb86 100644 --- a/app/src/main/assets/aria_config.xml +++ b/app/src/main/assets/aria_config.xml @@ -22,7 +22,7 @@ 3、从3.4.1开始,如果线程数为1,文件初始化时将不再预占用对应长度的空间,下载多少byte,则占多大的空间; 对于采用多线程的任务或旧任务,依然采用原来的文件空间占用方式; --> - + diff --git a/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java b/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java index dc18256b..21b27176 100644 --- a/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java @@ -84,7 +84,7 @@ public class DownloadGroupActivity extends BaseActivity { String[] urls; int index = 0; //String URL = "http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk"; - String URL = "http://58.213.157.242:8081/sims_file/rest/v1/file/mshd_touchscreen_ms/guideFile/41c33556-dc4a-4d78-bb76-b9f627f94448.mp4/%E5%85%AB%E5%8D%A6%E6%B4%B2%E5%8D%97%E4%BA%AC%E5%86%9C%E4%B8%9A%E5%98%89%E5%B9%B4%E5%8D%8E0511.mp4"; + //String URL = "http://58.213.157.242:8081/sims_file/rest/v1/file/mshd_touchscreen_ms/guideFile/41c33556-dc4a-4d78-bb76-b9f627f94448.mp4/%E5%85%AB%E5%8D%A6%E6%B4%B2%E5%8D%97%E4%BA%AC%E5%86%9C%E4%B8%9A%E5%98%89%E5%B9%B4%E5%8D%8E0511.mp4"; //String URL = "http://d1.showself.com/download/showself_android-s236279_release.apk"; //String URL = "http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk"; //private final String URL = "ftp://192.168.29.140:21/download/AriaPrj.rar"; //String URL = "https://dl.genymotion.com/releases/genymotion-2.12.1/genymotion-2.12.1-vbox.exe"; + //String URL = "ftp://192.168.29.140:21/download/SDK_Demo-release.apk"; + String URL = "ftp://z:z@dygod18.com:21211/[电影天堂www.dy2018.com]猩球崛起3:终极之战BD国英双语中英双字.mkv"; @Override protected int setLayoutId() { return R.layout.activity_test; @@ -49,8 +51,8 @@ public class AnyRunActivity extends BaseActivity { //} //for (int i = 0; i < 10; i++) { - //module.startFtp(URL); - module.start(URL); + module.startFtp(URL); + //module.start(URL); //} //List list = Aria.download(this).getTotalTaskList(); //ALog.d(TAG, "size ==> " + list.size()); diff --git a/app/src/main/java/com/arialyy/simple/test/TestActivity.java b/app/src/main/java/com/arialyy/simple/test/TestActivity.java index 760a1a9d..4c680de5 100644 --- a/app/src/main/java/com/arialyy/simple/test/TestActivity.java +++ b/app/src/main/java/com/arialyy/simple/test/TestActivity.java @@ -1,5 +1,6 @@ package com.arialyy.simple.test; +import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.View; @@ -7,6 +8,7 @@ import com.arialyy.annotations.Upload; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.common.RequestEnum; import com.arialyy.aria.core.upload.UploadTask; +import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; @@ -22,8 +24,8 @@ public class TestActivity extends BaseActivity { //String URL = "http://58.210.9.131/tpk/sipgt//TDLYZTGH.tpk"; //chunked 下载 //private final String URL = "ftp://192.168.1.3:21/download//AriaPrj.rar"; private final String FILE_PATH = "/mnt/sdcard/AriaPrj.rar"; - //private final String URL = "ftp://192.168.29.140:21/aa//你好"; - private final String URL = "ftp://192.168.29.140:21/upload/"; + private final String URL = "ftp://192.168.1.2:21/aa//你好"; + @Override protected int setLayoutId() { return R.layout.activity_test; @@ -81,6 +83,8 @@ public class TestActivity extends BaseActivity { .setUploadUrl(URL) .setExtendField("韩寒哈大双") .start(); + //Uri uri = Uri.parse("ftp://z:z@dygod18.com:21211/[电影天堂www.dy2018.com]猩球崛起3:终极之战BD国英双语中英双字.mkv"); + //ALog.d(TAG, "sh = " + uri.getScheme() + ", user = " + uri.getUserInfo() + ", host = " + uri.getHost() + ", port = " + uri.getPort() + " remotePath = " + uri.getPath()); break; case R.id.stop: Aria.upload(this).loadFtp(FILE_PATH).stop(); diff --git a/aria/src/main/AndroidManifest.xml b/aria/src/main/AndroidManifest.xml index a50dd348..722c94c4 100644 --- a/aria/src/main/AndroidManifest.xml +++ b/aria/src/main/AndroidManifest.xml @@ -1,6 +1,7 @@ + From 9ad009d33b57813b09bc9c0f4b2cf3c3b58de5c6 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Fri, 8 Jun 2018 01:17:16 +0800 Subject: [PATCH 07/10] bug fix --- .../com/arialyy/aria/core/command/normal/ResumeAllCmd.java | 2 +- .../main/java/com/arialyy/aria/core/common/AbsFileer.java | 5 ++++- .../java/com/arialyy/aria/core/common/AbsThreadTask.java | 1 + .../arialyy/aria/core/download/downloader/AbsGroupUtil.java | 2 +- app/src/main/AndroidManifest.xml | 4 ++-- 5 files changed, 9 insertions(+), 5 deletions(-) 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 ecdc37df..2a6353ea 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 @@ -56,7 +56,6 @@ final class ResumeAllCmd extends AbsNormalCmd { * @param type {@code 1}单任务下载任务;{@code 2}任务组下载任务;{@code 3} 单任务上传任务 */ private List findTaskData(int type) { - // TODO: 2018/4/20 需要测试 List tempList = new ArrayList<>(); if (type == 1) { List wrappers = DbEntity.findRelationData(DTEWrapper.class, @@ -144,6 +143,7 @@ final class ResumeAllCmd extends AbsNormalCmd { } else { te.getEntity().setState(IEntity.STATE_WAIT); createTask(te); + sendWaitState(); } } } 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 40ef2e0a..a523335a 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 @@ -491,7 +491,7 @@ public abstract class AbsFileer - + From cb6f08aa0d8b2f79805da1e239db62673dd5e59d Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Sat, 9 Jun 2018 19:45:15 +0800 Subject: [PATCH 08/10] fix bug --- .../core/command/normal/ResumeAllCmd.java | 83 ++++++++++--------- .../arialyy/aria/core/common/AbsFileer.java | 7 +- .../arialyy/aria/core/common/ProxyHelper.java | 21 ++--- .../aria/core/download/DownloadReceiver.java | 25 ++++++ .../download/downloader/AbsGroupUtil.java | 3 + .../downloader/DownloadGroupUtil.java | 6 +- .../com/arialyy/aria/core/inf/AbsEntity.java | 3 + .../aria/core/scheduler/AbsSchedulers.java | 2 +- .../aria/core/upload/UploadReceiver.java | 7 +- .../com/arialyy/aria/util/CommonUtil.java | 27 ++++-- app/src/main/AndroidManifest.xml | 4 +- .../simple/download/group/GroupModule.java | 2 +- .../multi_download/MultiTaskActivity.java | 2 + app/src/main/res/values/strings.xml | 5 ++ build.gradle | 2 +- 15 files changed, 127 insertions(+), 72 deletions(-) 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 ef4cf1d2..c87cad90 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 @@ -1,18 +1,19 @@ package com.arialyy.aria.core.command.normal; import com.arialyy.aria.core.AriaManager; +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.download.wrapper.DGTEWrapper; -import com.arialyy.aria.core.download.wrapper.DTEWrapper; import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.manager.TEManager; import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; import com.arialyy.aria.core.queue.DownloadTaskQueue; import com.arialyy.aria.core.queue.UploadTaskQueue; +import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadTaskEntity; -import com.arialyy.aria.core.upload.wrapper.UTEWrapper; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; @@ -43,10 +44,10 @@ final class ResumeAllCmd extends AbsNormalCmd { return; } if (isDownloadCmd) { - resumeTask(findTaskData(1)); - resumeTask(findTaskData(2)); + findTaskData(1); + findTaskData(2); } else { - resumeTask(findTaskData(3)); + findTaskData(3); } resumeWaitTask(); } @@ -56,53 +57,49 @@ final class ResumeAllCmd extends AbsNormalCmd { * * @param type {@code 1}单任务下载任务;{@code 2}任务组下载任务;{@code 3} 单任务上传任务 */ - private List findTaskData(int type) { - List tempList = new ArrayList<>(); + private void findTaskData(int type) { if (type == 1) { - List wrappers = DbEntity.findRelationData(DTEWrapper.class, - "DownloadTaskEntity.isGroupTask=? and DownloadTaskEntity.state!=?", "false", "1"); - if (wrappers != null && !wrappers.isEmpty()) { - for (DTEWrapper w : wrappers) { - tempList.add(w.taskEntity); + List entities = + DbEntity.findDatas(DownloadEntity.class, + "isGroupChild=? and state!=?", "false", "1"); + if (entities != null && !entities.isEmpty()) { + for (DownloadEntity entity : entities) { + resumeTask(TEManager.getInstance().getTEntity(DownloadTaskEntity.class, entity.getKey())); } } } else if (type == 2) { - List wrappers = - DbEntity.findRelationData(DGTEWrapper.class, "DownloadGroupTaskEntity.state!=?", "1"); - if (wrappers != null && !wrappers.isEmpty()) { - for (DGTEWrapper w : wrappers) { - tempList.add(w.taskEntity); + List entities = + DbEntity.findDatas(DownloadGroupEntity.class, "state!=?", "1"); + if (entities != null && !entities.isEmpty()) { + for (DownloadGroupEntity entity : entities) { + resumeTask( + TEManager.getInstance().getGTEntity(DownloadGroupTaskEntity.class, entity.getUrls())); } } } else if (type == 3) { - List wrappers = - DbEntity.findRelationData(UTEWrapper.class, "UploadTaskEntity.state!=?", "1"); - if (wrappers != null && !wrappers.isEmpty()) { - for (UTEWrapper w : wrappers) { - tempList.add(w.taskEntity); + List entities = + DbEntity.findDatas(UploadEntity.class, "state!=?", "1"); + if (entities != null && !entities.isEmpty()) { + for (UploadEntity entity : entities) { + resumeTask(TEManager.getInstance().getTEntity(UploadTaskEntity.class, entity.getKey())); } } } - return tempList; } /** * 恢复任务 */ - private void resumeTask(List taskList) { - if (taskList != null && !taskList.isEmpty()) { - for (AbsTaskEntity te : taskList) { - if (te == null || te.getEntity() == null) continue; - int state = te.getState(); - if (state == IEntity.STATE_STOP || state == IEntity.STATE_OTHER) { - resumeEntity(te); - } else if (state == IEntity.STATE_WAIT) { - mWaitList.add(te); - } else if (state == IEntity.STATE_RUNNING) { - if (!mQueue.taskIsRunning(te.getEntity().getKey())) { - resumeEntity(te); - } - } + private void resumeTask(AbsTaskEntity te) { + if (te == null || te.getEntity() == null) return; + int state = te.getState(); + if (state == IEntity.STATE_STOP || state == IEntity.STATE_OTHER) { + resumeEntity(te); + } else if (state == IEntity.STATE_WAIT) { + mWaitList.add(te); + } else if (state == IEntity.STATE_RUNNING) { + if (!mQueue.taskIsRunning(te.getEntity().getKey())) { + resumeEntity(te); } } } @@ -117,6 +114,13 @@ final class ResumeAllCmd extends AbsNormalCmd { 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); } } @@ -129,7 +133,8 @@ final class ResumeAllCmd extends AbsNormalCmd { */ private void resumeEntity(AbsTaskEntity te) { if (te instanceof DownloadTaskEntity) { - if (te.getRequestType() == AbsTaskEntity.D_FTP || te.getRequestType() == AbsTaskEntity.U_FTP) { + if (te.getRequestType() == AbsTaskEntity.D_FTP + || te.getRequestType() == AbsTaskEntity.U_FTP) { te.setUrlEntity(CommonUtil.getFtpUrlInfo(te.getEntity().getKey())); } mQueue = DownloadTaskQueue.getInstance(); 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 6bff75d7..99956f21 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 @@ -511,7 +511,12 @@ public abstract class AbsFileer downloadCounter, uploadCounter, downloadGroupCounter, downloadGroupSubCounter; + public Set downloadCounter = new HashSet<>(), uploadCounter = new HashSet<>(), + downloadGroupCounter = new HashSet<>(), downloadGroupSubCounter = new HashSet<>(); public static volatile ProxyHelper INSTANCE = null; @@ -47,9 +47,10 @@ public class ProxyHelper { } private void init() { - List classes = CommonUtil.getClassName(AriaManager.APP, "com.arialyy.aria"); + List classes = CommonUtil.getClassName(AriaManager.APP, + "com.arialyy.aria.ProxyClassCounter"); for (String className : classes) { - if (!className.startsWith("com.arialyy.aria.ProxyClassCounter")){ + if (!className.startsWith("com.arialyy.aria.ProxyClassCounter")) { continue; } count(className); @@ -66,30 +67,18 @@ public class ProxyHelper { Object object = clazz.newInstance(); Object dc = download.invoke(object); if (dc != null) { - if (downloadCounter == null) { - downloadCounter = new HashSet<>(); - } downloadCounter.addAll((Set) dc); } Object dgc = downloadGroup.invoke(object); if (dgc != null) { - if (downloadGroupCounter == null) { - downloadGroupCounter = new HashSet<>(); - } downloadGroupCounter.addAll((Set) dgc); } Object dgsc = downloadGroupSub.invoke(object); if (dgsc != null) { - if (downloadGroupSubCounter == null) { - downloadGroupSubCounter = new HashSet<>(); - } downloadGroupSubCounter.addAll((Set) dgsc); } Object uc = upload.invoke(object); if (uc != null) { - if (uploadCounter == null) { - uploadCounter = new HashSet<>(); - } uploadCounter.addAll((Set) uc); } } catch (ClassNotFoundException e) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java index 5ded3c79..8a8e64d0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java @@ -294,6 +294,9 @@ public class DownloadReceiver extends AbsReceiver { if (!CheckUtil.checkUrl(downloadUrl)) { return null; } + if (!taskExists(downloadUrl)) { + return null; + } return TEManager.getInstance().getTEntity(DownloadTaskEntity.class, downloadUrl); } @@ -308,6 +311,9 @@ public class DownloadReceiver extends AbsReceiver { ALog.e(TAG, "获取任务组实体失败:任务组子任务下载地址列表为null"); return null; } + if (!taskExists(urls)) { + return null; + } return TEManager.getInstance().getGTEntity(DownloadGroupTaskEntity.class, urls); } @@ -322,6 +328,11 @@ public class DownloadReceiver extends AbsReceiver { ALog.e(TAG, "获取FTP文件夹实体失败:下载路径为null"); return null; } + boolean b = + DownloadGroupEntity.findFirst(DownloadGroupEntity.class, "groupName=?", dirUrl) != null; + if (!b) { + return null; + } return TEManager.getInstance().getFDTEntity(DownloadGroupTaskEntity.class, dirUrl); } @@ -332,6 +343,20 @@ public class DownloadReceiver extends AbsReceiver { return DownloadEntity.findFirst(DownloadEntity.class, "url=?", downloadUrl) != null; } + /** + * 判断任务组是否存在 + * + * @return {@code true} 存在;{@code false} 不存在 + */ + public boolean taskExists(List urls) { + if (urls == null || urls.isEmpty()) { + return false; + } + String groupName = CommonUtil.getMd5Code(urls); + return DownloadGroupEntity.findFirst(DownloadGroupEntity.class, "groupName=?", groupName) + != null; + } + /** * 获取所有普通下载任务 * 获取未完成的普通任务列表{@link #getAllNotCompletTask()} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java index 285c2b15..01726e0c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java @@ -259,6 +259,9 @@ public abstract class AbsGroupUtil implements IUtil { dt.stop(); } } + if (mDownloaderMap.size() == 0){ + mListener.onStop(mCurrentLocation); + } } protected void onStop() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java index b82fdc3f..a552f8b2 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java @@ -44,6 +44,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { private int mInitFailNum; private boolean isStop = false; private boolean isStart = false; + private int mExeNum; /** * 文件信息回调组 @@ -89,6 +90,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { return; } Set keys = mExeMap.keySet(); + mExeNum = mExeMap.size(); for (String key : keys) { DownloadTaskEntity taskEntity = mExeMap.get(key); if (taskEntity != null) { @@ -161,11 +163,11 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { */ private void checkStartFlow() { synchronized (DownloadGroupUtil.class) { - if (mInitFailNum == mExeMap.size()) { + if (mInitFailNum == mExeNum) { closeTimer(false); mListener.onFail(true); } - if (!isStart && mInitCompleteNum + mInitFailNum == mExeMap.size() || !isNeedLoadFileSize) { + if (!isStart && mInitCompleteNum + mInitFailNum == mExeNum || !isNeedLoadFileSize) { startRunningFlow(); updateFileSize(); isStart = true; diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsEntity.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsEntity.java index c4a10c5c..34e3367e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsEntity.java @@ -50,6 +50,9 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable */ private String convertFileSize; + /** + * 任务状态{@link IEntity} + */ private int state = STATE_WAIT; /** * 当前下载进度 diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java index 90f5f8f9..730d00bf 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java @@ -158,7 +158,7 @@ abstract class AbsSchedulers { /** * 通过上传路径获取上传实体 + * 如果任务不存在,方便null */ public UploadEntity getUploadEntity(String filePath) { + if (TextUtils.isEmpty(filePath)) { + return null; + } return DbEntity.findFirst(UploadEntity.class, "filePath=?", filePath); } @@ -98,7 +102,6 @@ public class UploadReceiver extends AbsReceiver { "isGroupChild=? and isComplete=?", "false", "true"); } - @Override public void stopAllTask() { AriaManager.getInstance(AriaManager.APP) .setCmd(NormalCmdFactory.getInstance() 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 fda45c05..47446a22 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -40,6 +40,7 @@ import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.orm.DbEntity; import dalvik.system.DexFile; +import dalvik.system.PathClassLoader; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; @@ -74,26 +75,36 @@ public class CommonUtil { /** * 获取某包下所有类 * - * @param packageName 包名 + * @param className 过滤的类名 * @return 类的完整名称 */ - public static List getClassName(Context context, String packageName) { + public static List getClassName(Context context, String className) { List classNameList = new ArrayList<>(); try { String pPath = context.getPackageCodePath(); File dir = new File(pPath).getParentFile(); + //PathClassLoader classLoader = (PathClassLoader) context.getClassLoader(); String dPath = dir.getPath(); for (String path : dir.list()) { String fPath = dPath + "/" + path; + Log.d(TAG, fPath); if (!fPath.endsWith(".apk")) { continue; } DexFile df = new DexFile(fPath);//通过DexFile查找当前的APK中可执行文件 Enumeration enumeration = df.entries();//获取df中的元素 这里包含了所有可执行的类名 该类名包含了包名+类名的方式 - while (enumeration.hasMoreElements()) {//遍历 - String className = enumeration.nextElement(); - if (className.contains(packageName)) {//在当前所有可执行的类里面查找包含有该包名的所有类 - classNameList.add(className); + while (enumeration.hasMoreElements()) { + String _className = enumeration.nextElement(); + if (!_className.contains(className)) { + continue; + } + //Class clazz = classLoader.loadClass(_className); // 处理4.4手机无法获取到类的问题 + //String cn = clazz.getName(); + //if (clazz.getName().contains(className)) {//在当前所有可执行的类里面查找包含有该包名的所有类 + // classNameList.add(_className); + //} + if (_className.contains(className)){ + classNameList.add(_className); } } df.close(); @@ -101,6 +112,9 @@ public class CommonUtil { } catch (IOException e) { e.printStackTrace(); } + //catch (ClassNotFoundException e) { + // e.printStackTrace(); + //} return classNameList; } @@ -395,7 +409,6 @@ public class CommonUtil { if (subs != null) { for (DownloadEntity sub : subs) { File file = new File(sub.getDownloadPath()); - Log.d(TAG, "exist == " + file.exists() + ", rf == " + removeFile + ", complete = " + sub.isComplete()); if (file.exists() && (removeFile || !sub.isComplete())) { file.delete(); } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 17633b96..ae9d6fd3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -16,10 +16,10 @@ - + diff --git a/app/src/main/java/com/arialyy/simple/download/group/GroupModule.java b/app/src/main/java/com/arialyy/simple/download/group/GroupModule.java index 8595df26..b102585e 100644 --- a/app/src/main/java/com/arialyy/simple/download/group/GroupModule.java +++ b/app/src/main/java/com/arialyy/simple/download/group/GroupModule.java @@ -32,7 +32,7 @@ public class GroupModule extends BaseModule { public List getUrls() { List urls = new ArrayList<>(); - String[] str = getContext().getResources().getStringArray(R.array.group_urls_1); + String[] str = getContext().getResources().getStringArray(R.array.group_url4); Collections.addAll(urls, str); return urls; } diff --git a/app/src/main/java/com/arialyy/simple/download/multi_download/MultiTaskActivity.java b/app/src/main/java/com/arialyy/simple/download/multi_download/MultiTaskActivity.java index e2eaa945..8591cb13 100644 --- a/app/src/main/java/com/arialyy/simple/download/multi_download/MultiTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/multi_download/MultiTaskActivity.java @@ -121,6 +121,7 @@ public class MultiTaskActivity extends BaseActivity { @DownloadGroup.onTaskStop void groupTaskStop(DownloadGroupTask task) { mAdapter.updateBtState(task.getKey(), true); + Log.d(TAG, String.format("group【%s】stop", task.getTaskName())); } @DownloadGroup.onTaskCancel void groupTaskCancel(DownloadGroupTask task) { @@ -129,6 +130,7 @@ public class MultiTaskActivity extends BaseActivity { @DownloadGroup.onTaskFail void groupTaskFail(DownloadGroupTask task) { mAdapter.updateBtState(task.getKey(), true); + Log.d(TAG, String.format("group【%s】fail", task.getTaskName())); } @DownloadGroup.onTaskComplete void taskComplete(DownloadGroupTask task) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 911363ca..e8ac188a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -79,6 +79,11 @@ http://static.gaoshouyou.com/d/92/12/5592a647b8126755647abbe8074fde39.apk + + http://120.79.135.3/img/20180601/VR Karts Sprint.ver.1.04.build.15_crack_align.apk + http://120.79.135.3/img/20180601/main.11.com.daydvr.acefishing.vr.freefull.google.global.android.common.obb + + 战斗吧剑灵.apk diff --git a/build.gradle b/build.gradle index fdd15da1..24224353 100644 --- a/build.gradle +++ b/build.gradle @@ -39,7 +39,7 @@ task clean(type: Delete) { ext { userOrg = 'arialyy' groupId = 'com.arialyy.aria' - publishVersion = '3.4.2_dev1' + publishVersion = '3.4.2_dev6' // publishVersion = '1.0.3' //FTP插件 repoName='maven' desc = 'android 下载框架' From c0993ab538a5a1ea817fa256d0446fb2bd553d00 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Sun, 10 Jun 2018 09:08:54 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=205.0=E4=BB=A5?= =?UTF-8?q?=E4=B8=8B=E7=9A=84=E6=89=8B=E6=9C=BA=E6=97=A0=E6=B3=95=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=88=B0=E4=BB=A3=E7=90=86=E6=96=87=E4=BB=B6=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../arialyy/aria/core/common/ProxyHelper.java | 3 - .../com/arialyy/aria/util/CommonUtil.java | 59 +++++++++++-------- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/ProxyHelper.java b/Aria/src/main/java/com/arialyy/aria/core/common/ProxyHelper.java index b4ca1814..c5e59eb4 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/ProxyHelper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/ProxyHelper.java @@ -50,9 +50,6 @@ public class ProxyHelper { List classes = CommonUtil.getClassName(AriaManager.APP, "com.arialyy.aria.ProxyClassCounter"); for (String className : classes) { - if (!className.startsWith("com.arialyy.aria.ProxyClassCounter")) { - continue; - } count(className); } } 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 47446a22..8337a134 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -40,7 +40,6 @@ import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.orm.DbEntity; import dalvik.system.DexFile; -import dalvik.system.PathClassLoader; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; @@ -80,42 +79,50 @@ public class CommonUtil { */ public static List getClassName(Context context, String className) { List classNameList = new ArrayList<>(); - try { - String pPath = context.getPackageCodePath(); - File dir = new File(pPath).getParentFile(); - //PathClassLoader classLoader = (PathClassLoader) context.getClassLoader(); + String pPath = context.getPackageCodePath(); + File dir = new File(pPath).getParentFile(); + String[] paths = dir.list(); + if (paths == null) { + classNameList.addAll(getPkgClassName(pPath, className)); + } else { String dPath = dir.getPath(); for (String path : dir.list()) { String fPath = dPath + "/" + path; - Log.d(TAG, fPath); if (!fPath.endsWith(".apk")) { continue; } - DexFile df = new DexFile(fPath);//通过DexFile查找当前的APK中可执行文件 - Enumeration enumeration = df.entries();//获取df中的元素 这里包含了所有可执行的类名 该类名包含了包名+类名的方式 - while (enumeration.hasMoreElements()) { - String _className = enumeration.nextElement(); - if (!_className.contains(className)) { - continue; - } - //Class clazz = classLoader.loadClass(_className); // 处理4.4手机无法获取到类的问题 - //String cn = clazz.getName(); - //if (clazz.getName().contains(className)) {//在当前所有可执行的类里面查找包含有该包名的所有类 - // classNameList.add(_className); - //} - if (_className.contains(className)){ - classNameList.add(_className); - } + classNameList.addAll(getPkgClassName(fPath, className)); + } + } + return classNameList; + } + + /** + * 获取指定包名下的所有类 + * + * @param path dex路径 + * @param filterClass 需要过滤的类 + */ + public static List getPkgClassName(String path, String filterClass) { + List list = new ArrayList<>(); + try { + + DexFile df = new DexFile(path);//通过DexFile查找当前的APK中可执行文件 + Enumeration enumeration = df.entries();//获取df中的元素 这里包含了所有可执行的类名 该类名包含了包名+类名的方式 + while (enumeration.hasMoreElements()) { + String _className = enumeration.nextElement(); + if (!_className.contains(filterClass)) { + continue; + } + if (_className.contains(filterClass)) { + list.add(_className); } - df.close(); } + df.close(); } catch (IOException e) { e.printStackTrace(); } - //catch (ClassNotFoundException e) { - // e.printStackTrace(); - //} - return classNameList; + return list; } /** From d2a90f569dee35d8e8462ea39b88240d05e96353 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Mon, 11 Jun 2018 09:40:14 +0800 Subject: [PATCH 10/10] 3.4.2 --- DEV_LOG.md | 4 ++++ README.md | 19 ++++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/DEV_LOG.md b/DEV_LOG.md index 43af1526..d73f0508 100644 --- a/DEV_LOG.md +++ b/DEV_LOG.md @@ -3,6 +3,10 @@ - fix bug https://github.com/AriaLyy/Aria/issues/248 - fix bug https://github.com/AriaLyy/Aria/issues/247 - fix bug https://github.com/AriaLyy/Aria/issues/250 + - 添加任务判断是否存在的api + - 添加代理api + - 修复删除所有没有进出等待的问题 + - 进度有时出错的问题 + v_3.4.1 - 移除记录配置文件,改用数据库记录任务记录 - 上传配置添加io超时时间、缓存大小配置 diff --git a/README.md b/README.md index e3380213..7dd194e7 100644 --- a/README.md +++ b/README.md @@ -29,8 +29,8 @@ Aria有以下特点: [![Download](https://api.bintray.com/packages/arialyy/maven/AriaApi/images/download.svg)](https://bintray.com/arialyy/maven/AriaApi/_latestVersion) [![Download](https://api.bintray.com/packages/arialyy/maven/AriaCompiler/images/download.svg)](https://bintray.com/arialyy/maven/AriaCompiler/_latestVersion) ```java -compile 'com.arialyy.aria:aria-core:3.4.1' -annotationProcessor 'com.arialyy.aria:aria-compiler:3.4.1' +compile 'com.arialyy.aria:aria-core:3.4.2' +annotationProcessor 'com.arialyy.aria:aria-compiler:3.4.2' ``` 如果出现android support,请将 `compile 'com.arialyy.aria:aria-core:'`替换为 ``` @@ -99,13 +99,14 @@ protected void onCreate(Bundle savedInstanceState) { ### [更多说明,见WIKI](http://aria.laoyuyu.me/aria_doc/) ### 版本日志 - + v_3.4.1 - - 移除记录配置文件,改用数据库记录任务记录 - - 上传配置添加io超时时间、缓存大小配置 - - 添加没有网络也会重试的开关 - - 修复多次删除记录的bug - - 文件长度现在可动态增加,详情见 https://aria.laoyuyu.me/aria_doc/start/config.html - - 修复多module同时引用Aria导致打正式包出错的问题 https://github.com/AriaLyy/Aria/issues/240 + + v_3.4.2 + - fix bug https://github.com/AriaLyy/Aria/issues/248 + - fix bug https://github.com/AriaLyy/Aria/issues/247 + - fix bug https://github.com/AriaLyy/Aria/issues/250 + - 添加任务判断是否存在的api + - 添加代理api + - 修复删除所有没有进出等待的问题 + - 进度有时出错的问题 [更多版本记录](https://github.com/AriaLyy/Aria/blob/master/DEV_LOG.md)