From 66b36b355a89900083f31dc7bce81b0f5800c605 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Mon, 5 Nov 2018 21:13:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Serializable=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=94=AF=E6=8C=81=20https://github.com/AriaLyy/Aria/i?= =?UTF-8?q?ssues/320=20=E5=A4=B1=E8=B4=A5=E5=9B=9E=E8=B0=83=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E9=94=99=E8=AF=AF=E5=8E=9F=E5=9B=A0=20https://github.?= =?UTF-8?q?com/AriaLyy/Aria/issues/310?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aria/core/common/AbsThreadTask.java | 32 ++-- .../aria/core/common/BaseListener.java | 7 +- .../aria/core/common/OnFileInfoCallback.java | 6 +- .../core/common/ftp/AbsFtpInfoThread.java | 41 +++-- .../core/common/ftp/AbsFtpThreadTask.java | 15 +- .../core/download/DownloadGroupListener.java | 6 +- .../download/downloader/AbsGroupUtil.java | 11 +- .../downloader/DownloadGroupUtil.java | 11 +- .../core/download/downloader/Downloader.java | 18 +-- .../downloader/FtpDirDownloadUtil.java | 7 +- .../downloader/FtpFileInfoThread.java | 6 +- .../download/downloader/FtpThreadTask.java | 32 ++-- .../downloader/HttpFileInfoThread.java | 35 +++-- .../download/downloader/HttpThreadTask.java | 27 +++- .../downloader/IDownloadGroupListener.java | 3 +- .../downloader/SimpleDownloadUtil.java | 16 +- .../com/arialyy/aria/core/inf/AbsEntity.java | 3 +- .../com/arialyy/aria/core/inf/AbsTask.java | 31 ++++ .../arialyy/aria/core/inf/IEventListener.java | 7 +- .../core/scheduler/AbsSchedulerListener.java | 9 ++ .../aria/core/scheduler/AbsSchedulers.java | 7 +- .../core/scheduler/ISchedulerListener.java | 10 +- .../aria/core/upload/UploadListener.java | 3 +- .../core/upload/uploader/FtpThreadTask.java | 27 ++-- .../core/upload/uploader/HttpThreadTask.java | 14 +- .../upload/uploader/SimpleUploadUtil.java | 6 +- .../aria/exception/AriaIOException.java | 28 ++++ .../arialyy/aria/exception/BaseException.java | 35 +++++ .../arialyy/aria/exception/TaskException.java | 31 ++++ .../java/com/arialyy/aria/util/CheckUtil.java | 4 +- .../java/com/arialyy/aria/util/ErrorHelp.java | 10 +- .../com/arialyy/compiler/EventProxyFiler.java | 53 +++++-- .../java/com/arialyy/compiler/MethodInfo.java | 27 ++++ .../com/arialyy/compiler/ParamObtainUtil.java | 89 +++++++---- .../com/arialyy/compiler/ProxyClassParam.java | 2 +- DEV_LOG.md | 9 ++ app/src/main/AndroidManifest.xml | 11 +- .../simple/download/KotlinDownloadActivity.kt | 148 +++++++++--------- .../simple/download/SingleTaskActivity.java | 5 +- .../arialyy/simple/test/AnyRunActivity.java | 7 +- .../arialyy/simple/test/AnyRunnModule.java | 8 +- .../arialyy/aria/exception/FileException.java | 12 +- gradle.properties | 4 +- 43 files changed, 598 insertions(+), 275 deletions(-) create mode 100644 Aria/src/main/java/com/arialyy/aria/exception/AriaIOException.java create mode 100644 Aria/src/main/java/com/arialyy/aria/exception/BaseException.java create mode 100644 Aria/src/main/java/com/arialyy/aria/exception/TaskException.java create mode 100644 AriaCompiler/src/main/java/com/arialyy/compiler/MethodInfo.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java index bb4fe7be..906ece5d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java @@ -21,6 +21,9 @@ import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.IEventListener; import com.arialyy.aria.core.manager.ThreadTaskManager; import com.arialyy.aria.core.upload.UploadEntity; +import com.arialyy.aria.exception.BaseException; +import com.arialyy.aria.exception.FileException; +import com.arialyy.aria.exception.TaskException; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.ErrorHelp; import com.arialyy.aria.util.FileUtil; @@ -94,6 +97,7 @@ public abstract class AbsThreadTask mEntity.getFileSize()) { - ALog.d(TAG, String.format("currentLocation=%s, fileSize=%s", STATE.CURRENT_LOCATION, - mEntity.getFileSize())); + String errorMsg = + String.format("下载失败,下载长度超出文件大;currentLocation=%s, fileSize=%s", STATE.CURRENT_LOCATION, + mEntity.getFileSize()); taskBreak = true; - fail(mChildCurrentLocation, "下载失败,下载长度超出文件大小", null, false); + fail(mChildCurrentLocation, new FileException(TAG, errorMsg), false); return; } mChildCurrentLocation += len; @@ -312,11 +318,10 @@ public abstract class AbsThreadTask, TASK extends AbsTask> @@ -103,12 +106,14 @@ public abstract class BaseListener= ips.length) { 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 403d9e1b..6b417a03 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 @@ -21,8 +21,10 @@ import com.arialyy.aria.core.download.downloader.IDownloadGroupListener; import com.arialyy.aria.core.inf.GroupSendParams; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.scheduler.ISchedulers; +import com.arialyy.aria.exception.BaseException; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; +import com.arialyy.aria.util.ErrorHelp; /** * Created by Aria.Lao on 2017/7/20. @@ -64,9 +66,11 @@ class DownloadGroupListener sendInState2Target(ISchedulers.SUB_COMPLETE, subEntity); } - @Override public void onSubFail(DownloadEntity subEntity) { + @Override public void onSubFail(DownloadEntity subEntity, BaseException e) { saveCurrentLocation(); sendInState2Target(ISchedulers.SUB_FAIL, subEntity); + e.printStackTrace(); + ErrorHelp.saveError(e.getTag(), "", ALog.getExceptionString(e)); } @Override public void onSubCancel(DownloadEntity subEntity) { 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 ed34d8ce..45e50fb5 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 @@ -22,6 +22,8 @@ import com.arialyy.aria.core.download.DownloadGroupTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.inf.IDownloadListener; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.exception.BaseException; +import com.arialyy.aria.exception.TaskException; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.NetUtils; @@ -482,7 +484,7 @@ public abstract class AbsGroupUtil implements IUtil, Runnable { } } - @Override public void onFail(boolean needRetry) { + @Override public void onFail(boolean needRetry, BaseException e) { subEntity.setFailNum(subEntity.getFailNum() + 1); saveData(IEntity.STATE_FAIL, lastLen); handleSpeed(0); @@ -504,12 +506,15 @@ public abstract class AbsGroupUtil implements IUtil, Runnable { reStartTask(dt); } else { mFailMap.put(subTaskEntity.getUrl(), subTaskEntity); - mListener.onSubFail(subEntity); + mListener.onSubFail(subEntity, new TaskException(TAG, + String.format("任务组子任务【%s】下载失败,下载地址【%s】", subEntity.getFileName(), + subEntity.getUrl()))); if (mFailMap.size() == mExeMap.size() || mFailMap.size() + mCompleteNum == mGroupSize) { closeTimer(); } if (mFailMap.size() == mGroupSize) { - mListener.onFail(true); + mListener.onFail(true, new TaskException(TAG, + String.format("任务组【%s】下载失败", mGTEntity.getEntity().getGroupName()))); } else if (mFailMap.size() + mCompleteNum >= mExeMap.size()) { mListener.onStop(mCurrentLocation); } 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 ae0c0fdc..fd9a8ccc 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 @@ -22,6 +22,8 @@ import com.arialyy.aria.core.common.OnFileInfoCallback; import com.arialyy.aria.core.download.DownloadGroupTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.exception.BaseException; +import com.arialyy.aria.exception.TaskException; import com.arialyy.aria.util.ALog; import java.util.Set; import java.util.concurrent.ExecutorService; @@ -85,8 +87,8 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { } if (mExeMap.size() == 0) { - ALog.e(TAG, "任务组无可执行任务"); - mListener.onFail(false); + mListener.onFail(false, new TaskException(TAG, + String.format("任务组【%s】无可执行任务", mGTEntity.getEntity().getGroupName()))); return; } Set keys = mExeMap.keySet(); @@ -133,7 +135,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { checkStartFlow(); } - @Override public void onFail(String url, String errorMsg, boolean needRetry) { + @Override public void onFail(String url, BaseException e, boolean needRetry) { if (isStop) return; ALog.e(TAG, String.format("任务【%s】初始化失败", url)); DownloadTaskEntity te = mExeMap.get(url); @@ -169,7 +171,8 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { } if (mInitFailNum == mExeNum) { closeTimer(); - mListener.onFail(true); + mListener.onFail(true, new TaskException(TAG, + String.format("任务组【%s】初始化失败", mGTEntity.getEntity().getGroupName()))); } if (!isStart && mInitCompleteNum + mInitFailNum == mExeNum || !isNeedLoadFileSize) { startRunningFlow(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java index 0072d5c0..4590213b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java @@ -23,10 +23,9 @@ import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.IDownloadListener; -import com.arialyy.aria.util.ALog; +import com.arialyy.aria.exception.BaseException; +import com.arialyy.aria.exception.TaskException; import com.arialyy.aria.util.BufferedRandomAccessFile; -import com.arialyy.aria.util.CommonUtil; -import com.arialyy.aria.util.ErrorHelp; import java.io.File; import java.io.IOException; @@ -58,7 +57,7 @@ class Downloader extends AbsFileer { @Override protected boolean handleNewTask() { if (!mRecord.isBlock) { - if (mTempFile.exists()){ + if (mTempFile.exists()) { mTempFile.delete(); } //CommonUtil.createFile(mTempFile.getPath()); @@ -72,8 +71,9 @@ class Downloader extends AbsFileer { } return true; } catch (IOException e) { - failDownload(String.format("下载失败【downloadUrl:%s】;【filePath:%s】\n %S", mEntity.getUrl(), - mEntity.getDownloadPath(), ALog.getExceptionString(e))); + failDownload(new TaskException(TAG, + String.format("下载失败,filePath: %s, url: %s", mEntity.getDownloadPath(), + mEntity.getUrl()), e)); } finally { if (file != null) { try { @@ -106,11 +106,9 @@ class Downloader extends AbsFileer { return null; } - private void failDownload(String errorMsg) { + private void failDownload(BaseException e) { closeTimer(); - ALog.e(TAG, errorMsg); mConstance.isRunning = false; - mListener.onFail(false); - ErrorHelp.saveError(TAG, "", errorMsg); + mListener.onFail(false, e); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java index 00ec7df8..1cd0715c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java @@ -19,6 +19,8 @@ import com.arialyy.aria.core.common.CompleteInfo; import com.arialyy.aria.core.common.OnFileInfoCallback; import com.arialyy.aria.core.download.DownloadGroupTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; +import com.arialyy.aria.exception.BaseException; +import com.arialyy.aria.exception.TaskException; import com.arialyy.aria.util.ErrorHelp; import java.util.Set; @@ -51,14 +53,13 @@ public class FtpDirDownloadUtil extends AbsGroupUtil { } } - @Override public void onFail(String url, String errorMsg, boolean needRetry) { + @Override public void onFail(String url, BaseException e, boolean needRetry) { DownloadTaskEntity te = mExeMap.get(url); if (te != null) { mFailMap.put(url, te); mExeMap.remove(url); } - mListener.onFail(needRetry); - ErrorHelp.saveError(TAG, "", errorMsg); + mListener.onFail(needRetry, e); } }).start(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpFileInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpFileInfoThread.java index 3d8ab2f0..4ab8bd1e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpFileInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpFileInfoThread.java @@ -20,6 +20,7 @@ import com.arialyy.aria.core.common.OnFileInfoCallback; import com.arialyy.aria.core.common.ftp.AbsFtpInfoThread; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; +import com.arialyy.aria.exception.AriaIOException; import com.arialyy.aria.util.CommonUtil; import org.apache.commons.net.ftp.FTPFile; @@ -28,6 +29,7 @@ import org.apache.commons.net.ftp.FTPFile; * 获取ftp文件信息 */ class FtpFileInfoThread extends AbsFtpInfoThread { + private final String TAG = "FtpFileInfoThread"; FtpFileInfoThread(DownloadTaskEntity taskEntity, OnFileInfoCallback callback) { super(taskEntity, callback); @@ -36,7 +38,9 @@ class FtpFileInfoThread extends AbsFtpInfoThread mConfig.THREAD_ID, mConfig.START_LOCATION, mConfig.END_LOCATION)); client = createClient(); if (client == null) { - fail(mChildCurrentLocation, "ftp client 创建失败", null); + fail(mChildCurrentLocation, new TaskException(TAG, "ftp client 创建失败")); return this; } if (mConfig.START_LOCATION > 0) { @@ -79,8 +81,8 @@ class FtpThreadTask extends AbsFtpThreadTask int reply = client.getReplyCode(); if (!FTPReply.isPositivePreliminary(reply) && reply != FTPReply.COMMAND_OK) { fail(mChildCurrentLocation, - String.format("获取文件信息错误,错误码为:%s,msg:%s", reply, client.getReplyString()), - null); + new AriaIOException(TAG, + String.format("获取文件信息错误,错误码为:%s,msg:%s", reply, client.getReplyString()))); client.disconnect(); return this; } @@ -91,8 +93,8 @@ class FtpThreadTask extends AbsFtpThreadTask reply = client.getReplyCode(); if (!FTPReply.isPositivePreliminary(reply)) { fail(mChildCurrentLocation, - String.format("获取流失败,错误码为:%s,msg:%s", reply, client.getReplyString()), - null); + new AriaIOException(TAG, + String.format("获取流失败,错误码为:%s,msg:%s", reply, client.getReplyString()))); client.disconnect(); return this; } @@ -104,9 +106,11 @@ class FtpThreadTask extends AbsFtpThreadTask handleComplete(); } } catch (IOException e) { - fail(mChildCurrentLocation, String.format("下载失败【%s】", mConfig.URL), e); + fail(mChildCurrentLocation, + new AriaIOException(TAG, String.format("下载失败【%s】", mConfig.URL), e)); } catch (Exception e) { - fail(mChildCurrentLocation, "获取流失败", e); + fail(mChildCurrentLocation, + new AriaIOException(TAG, String.format("下载失败【%s】", mConfig.URL), e)); } finally { try { if (is != null) { @@ -137,9 +141,10 @@ class FtpThreadTask extends AbsFtpThreadTask if (isBlock) { boolean success = mergeFile(); if (!success) { - ALog.e(TAG, String.format("任务【%s】分块文件合并失败", mConfig.TEMP_FILE.getName())); + //ALog.e(TAG, String.format("任务【%s】分块文件合并失败", mConfig.TEMP_FILE.getName())); STATE.isRunning = false; - mListener.onFail(false); + mListener.onFail(false, + new TaskException(TAG, String.format("任务【%s】分块文件合并失败", mConfig.TEMP_FILE.getName()))); return; } } @@ -149,7 +154,8 @@ class FtpThreadTask extends AbsFtpThreadTask } if (STATE.isFail()) { STATE.isRunning = false; - mListener.onFail(false); + mListener.onFail(false, + new TaskException(TAG, String.format("任务【%s】下载失败", mConfig.TEMP_FILE.getName()))); } } @@ -189,7 +195,8 @@ class FtpThreadTask extends AbsFtpThreadTask } handleComplete(); } catch (IOException e) { - fail(mChildCurrentLocation, String.format("下载失败【%s】", mConfig.URL), e); + fail(mChildCurrentLocation, + new AriaIOException(TAG, String.format("下载失败【%s】", mConfig.URL), e)); } finally { try { if (fos != null) { @@ -235,7 +242,8 @@ class FtpThreadTask extends AbsFtpThreadTask } } } catch (IOException e) { - fail(mChildCurrentLocation, String.format("下载失败【%s】", mConfig.URL), e); + fail(mChildCurrentLocation, + new AriaIOException(TAG, String.format("下载失败【%s】", mConfig.URL), e)); } finally { try { if (file != null) { 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 fc87f934..848742e7 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 @@ -22,6 +22,9 @@ import com.arialyy.aria.core.common.OnFileInfoCallback; import com.arialyy.aria.core.common.RequestEnum; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; +import com.arialyy.aria.exception.AriaIOException; +import com.arialyy.aria.exception.BaseException; +import com.arialyy.aria.exception.TaskException; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CommonUtil; @@ -67,12 +70,9 @@ class HttpFileInfoThread implements Runnable { conn.connect(); handleConnect(conn); } catch (IOException e) { - failDownload("下载失败【downloadUrl:" - + mEntity.getUrl() - + "】\n【filePath:" - + mEntity.getDownloadPath() - + "】\n" - + ALog.getExceptionString(e), true); + failDownload(new AriaIOException(TAG, + String.format("下载失败,filePath: %s, url: %s", mEntity.getDownloadPath(), mEntity.getUrl())), + true); } finally { if (conn != null) { conn.disconnect(); @@ -116,7 +116,9 @@ class HttpFileInfoThread implements Runnable { } if (!CommonUtil.checkSDMemorySpace(mEntity.getDownloadPath(), len)) { - failDownload(String.format("路径【%s】内存空间不足", mEntity.getDownloadPath()), false); + failDownload(new TaskException(TAG, + String.format("下载失败,内存空间不足;filePath: %s, url: %s", mEntity.getDownloadPath(), + mEntity.getUrl())), false); return; } @@ -184,14 +186,16 @@ class HttpFileInfoThread implements Runnable { mTaskEntity.setSupportBP(false); end = true; } else if (code == HttpURLConnection.HTTP_NOT_FOUND) { - failDownload("任务【" + mEntity.getUrl() + "】下载失败,错误码:404", true); + failDownload(new AriaIOException(TAG, + String.format("任务下载失败,errorCode:404, url: %s", mEntity.getUrl())), true); } else if (code == HttpURLConnection.HTTP_MOVED_TEMP || code == HttpURLConnection.HTTP_MOVED_PERM || code == HttpURLConnection.HTTP_SEE_OTHER || code == 307) { handleUrlReTurn(conn, conn.getHeaderField("Location")); } else { - failDownload("任务【" + mEntity.getUrl() + "】下载失败,错误码:" + code, true); + failDownload(new AriaIOException(TAG, + String.format("任务下载失败,errorCode:%s, url: %s", code, mEntity.getUrl())), true); } if (end) { mTaskEntity.setChunked(isChunked); @@ -229,12 +233,12 @@ class HttpFileInfoThread implements Runnable { if (TextUtils.isEmpty(newUrl) || newUrl.equalsIgnoreCase("null") || !newUrl.startsWith( "http")) { if (onFileInfoCallback != null) { - onFileInfoCallback.onFail(mEntity.getUrl(), "获取重定向链接失败", false); + onFileInfoCallback.onFail(mEntity.getUrl(), new TaskException(TAG, "获取重定向链接失败"), false); } return; } if (!CheckUtil.checkUrl(newUrl)) { - failDownload("下载失败,重定向url错误", false); + failDownload(new TaskException(TAG, "下载失败,重定向url错误"), false); return; } mTaskEntity.setRedirectUrl(newUrl); @@ -263,16 +267,17 @@ class HttpFileInfoThread implements Runnable { mTaskEntity.setNewTask(true); } if (len < 0) { - failDownload("任务【" + mEntity.getUrl() + "】下载失败,文件长度小于0", true); + failDownload( + new AriaIOException(TAG, String.format("任务下载失败,文件长度小于0, url: %s", mEntity.getUrl())), + true); return false; } return true; } - private void failDownload(String errorMsg, boolean needRetry) { - ALog.e(TAG, errorMsg); + private void failDownload(BaseException e, boolean needRetry) { if (onFileInfoCallback != null) { - onFileInfoCallback.onFail(mEntity.getUrl(), errorMsg, needRetry); + onFileInfoCallback.onFail(mEntity.getUrl(), e, needRetry); } } } \ No newline at end of file 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 7e9534a3..5ae61986 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 @@ -22,6 +22,8 @@ import com.arialyy.aria.core.common.SubThreadConfig; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.inf.IDownloadListener; +import com.arialyy.aria.exception.AriaIOException; +import com.arialyy.aria.exception.TaskException; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.BufferedRandomAccessFile; import com.arialyy.aria.util.CommonUtil; @@ -121,11 +123,17 @@ final class HttpThreadTask extends AbsThreadTask implements ITask { + public static final String ERROR_INFO_KEY = "ERROR_INFO_KEY"; + /** * 是否需要重试,默认为true */ @@ -36,6 +41,10 @@ public abstract class AbsTask mExpand = new HashMap<>(); /** * 该任务的调度类型 */ @@ -53,6 +62,28 @@ public abstract class AbsTask { void onTaskCancel(TASK task); /** - * 任务下载失败 + * 任务失败 + * + * @deprecated @see{{@link #onTaskFail(ITask, Exception)}} */ + @Deprecated void onTaskFail(TASK task); + /** + * 任务失败 + */ + void onTaskFail(TASK task, Exception e); + /** * 任务完成 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadListener.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadListener.java index 17506e92..c37416c2 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadListener.java @@ -16,6 +16,7 @@ package com.arialyy.aria.core.upload; import com.arialyy.aria.core.inf.IUploadListener; +import com.arialyy.aria.exception.BaseException; /** * Created by lyy on 2017/2/23. @@ -49,7 +50,7 @@ class UploadListener implements IUploadListener { } - @Override public void onFail(boolean needRetry) { + @Override public void onFail(boolean needRetry, BaseException e) { } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpThreadTask.java index 12c5f8ef..4ba730a8 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpThreadTask.java @@ -21,6 +21,8 @@ import com.arialyy.aria.core.common.ftp.AbsFtpThreadTask; import com.arialyy.aria.core.inf.IEventListener; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadTaskEntity; +import com.arialyy.aria.exception.AriaIOException; +import com.arialyy.aria.exception.TaskException; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.BufferedRandomAccessFile; import java.io.IOException; @@ -70,7 +72,10 @@ class FtpThreadTask extends AbsFtpThreadTask { client.setRestartOffset(mConfig.START_LOCATION); int reply = client.getReplyCode(); if (!FTPReply.isPositivePreliminary(reply) && reply != FTPReply.FILE_ACTION_OK) { - fail(mChildCurrentLocation, "上传失败,错误码为:" + reply + ",msg:" + client.getReplyString(), null); + fail(mChildCurrentLocation, + new AriaIOException(TAG, + String.format("文件上传错误,错误码为:%s, msg:%s, filePath: %s", reply, + client.getReplyString(), mEntity.getFilePath()))); client.disconnect(); return this; } @@ -95,12 +100,14 @@ class FtpThreadTask extends AbsFtpThreadTask { } if (STATE.isFail()) { STATE.isRunning = false; - mListener.onFail(false); + mListener.onFail(false, new TaskException(TAG, + String.format("上传失败,filePath: %s, uploadUrl: %s", mEntity.getFilePath(), mConfig.URL))); } } catch (IOException e) { - fail(mChildCurrentLocation, String.format("上传失败【%s】", mConfig.URL), e); + fail(mChildCurrentLocation, new AriaIOException(TAG, + String.format("上传失败,filePath: %s, uploadUrl: %s", mEntity.getFilePath(), mConfig.URL))); } catch (Exception e) { - fail(mChildCurrentLocation, "获取流失败", e); + fail(mChildCurrentLocation, new AriaIOException(TAG, null, e)); } finally { try { if (file != null) { @@ -128,7 +135,7 @@ class FtpThreadTask extends AbsFtpThreadTask { throws IOException { try { - ALog.d(TAG, String.format("remotePath【%s】", remotePath)); + ALog.d(TAG, String.format("remotePath: %s", remotePath)); client.storeFile(remotePath, new FtpFISAdapter(bis), new OnFtpInputStreamListener() { boolean isStoped = false; @@ -149,10 +156,11 @@ class FtpThreadTask extends AbsFtpThreadTask { } }); } catch (IOException e) { - if (e.getMessage().contains("IOException caught while copying")) { + if (e.getMessage().contains("AriaIOException caught while copying")) { e.printStackTrace(); } else { - fail(mChildCurrentLocation, "上传失败", e); + fail(mChildCurrentLocation, + new AriaIOException(TAG, String.format("上传失败,filePath: %s", mEntity.getFilePath()), e)); } } @@ -160,8 +168,9 @@ class FtpThreadTask extends AbsFtpThreadTask { if (!FTPReply.isPositiveCompletion(reply)) { if (reply != FTPReply.TRANSFER_ABORTED) { fail(mChildCurrentLocation, - String.format("上传文件错误,错误码为:%s,msg:%s", reply, client.getReplyString()), - null); + new AriaIOException(TAG, + String.format("文件上传错误,错误码为:%s, msg:%s, filePath: %s", reply, + client.getReplyString(), mEntity.getFilePath()))); } if (client.isConnected()) { client.disconnect(); 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 17cd60a9..c5ce84fc 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 @@ -21,6 +21,8 @@ import com.arialyy.aria.core.common.SubThreadConfig; import com.arialyy.aria.core.inf.IUploadListener; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadTaskEntity; +import com.arialyy.aria.exception.BaseException; +import com.arialyy.aria.exception.TaskException; import com.arialyy.aria.util.ALog; import java.io.BufferedReader; import java.io.File; @@ -61,8 +63,9 @@ class HttpThreadTask extends AbsThreadTask { super.call(); File uploadFile = new File(mEntity.getFilePath()); if (!uploadFile.exists()) { - ALog.e(TAG, String.format("【%s】,文件不存在。", mEntity.getFilePath())); - fail(); + fail(new TaskException(TAG, + String.format("上传失败,文件不存在;filePath: %s, url: %s", mEntity.getFilePath(), + mEntity.getUrl()))); return this; } mListener.onPre(); @@ -102,14 +105,15 @@ class HttpThreadTask extends AbsThreadTask { mListener.onComplete(); } catch (Exception e) { e.printStackTrace(); - fail(); + fail(new TaskException(TAG, + String.format("上传失败,filePath: %s, url: %s", mEntity.getFilePath(), mEntity.getUrl()), e)); } return this; } - private void fail() { + private void fail(BaseException e1) { try { - mListener.onFail(true); + mListener.onFail(true, e1); STATE.isRunning = false; if (mOutputStream != null) { mOutputStream.close(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java index 591876c3..774fe935 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java @@ -22,6 +22,8 @@ import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.IUploadListener; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadTaskEntity; +import com.arialyy.aria.exception.BaseException; +import com.arialyy.aria.exception.TaskException; import com.arialyy.aria.util.CheckUtil; /** @@ -60,8 +62,8 @@ public class SimpleUploadUtil implements IUtil, Runnable { } } - @Override public void onFail(String url, String errorMsg, boolean needRetry) { - mListener.onFail(needRetry); + @Override public void onFail(String url, BaseException e, boolean needRetry) { + mListener.onFail(needRetry, e); } }).start(); break; diff --git a/Aria/src/main/java/com/arialyy/aria/exception/AriaIOException.java b/Aria/src/main/java/com/arialyy/aria/exception/AriaIOException.java new file mode 100644 index 00000000..db0ffc7a --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/exception/AriaIOException.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.exception; + +public class AriaIOException extends BaseException{ + private static final String ARIA_NET_EXCEPTION = "Aria Net Exception:"; + + public AriaIOException(String tag, String message) { + super(tag, String.format("%s%s", ARIA_NET_EXCEPTION, message)); + } + + public AriaIOException(String tag, String message, Exception e){ + super(tag, message, e); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/exception/BaseException.java b/Aria/src/main/java/com/arialyy/aria/exception/BaseException.java new file mode 100644 index 00000000..98073c31 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/exception/BaseException.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.exception; + +public abstract class BaseException extends Exception { + + private String tag; + + public BaseException(String tag, String message) { + super(message); + this.tag = tag; + } + + public BaseException(String tag, String message, Exception e) { + super(String.format("%s\n%s", message == null ? "" : message, e == null ? "" : e.getMessage())); + this.tag = tag; + } + + public String getTag() { + return tag; + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/exception/TaskException.java b/Aria/src/main/java/com/arialyy/aria/exception/TaskException.java new file mode 100644 index 00000000..e8f7b765 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/exception/TaskException.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.exception; + +/** + * 任务异常 + */ +public class TaskException extends BaseException { + private static final String ARIA_TASK_EXCEPTION = "Aria Task Exception:"; + + public TaskException(String tag, String detailMessage) { + super(tag, String.format("%s, %s", ARIA_TASK_EXCEPTION, detailMessage)); + } + + public TaskException(String tag, String message, Exception e){ + super(tag, message, e); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java b/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java index 2e61f8c1..4ea24293 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java @@ -222,9 +222,9 @@ public class CheckUtil { } else if (TextUtils.isEmpty(entity.getUrl())) { throw new IllegalArgumentException("下载链接不能为空"); } else if (TextUtils.isEmpty(entity.getFileName())) { - throw new FileException("文件名不能为null"); + throw new NullPointerException("文件名不能为null"); } else if (TextUtils.isEmpty(entity.getDownloadPath())) { - throw new FileException("文件保存路径不能为null"); + throw new NullPointerException("文件保存路径不能为null"); } } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/util/ErrorHelp.java b/Aria/src/main/java/com/arialyy/aria/util/ErrorHelp.java index 8f53fc75..d870f342 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/ErrorHelp.java +++ b/Aria/src/main/java/com/arialyy/aria/util/ErrorHelp.java @@ -16,7 +16,6 @@ package com.arialyy.aria.util; import android.annotation.SuppressLint; -import android.util.Log; import com.arialyy.aria.core.AriaManager; import java.io.File; import java.io.FileWriter; @@ -38,8 +37,7 @@ public class ErrorHelp { * @param ex 异常 */ public static void saveError(String tag, String msg, String ex) { - String message = "\nmsg【" + msg + "】\nException:" + ex; - writeLogToFile(tag, message); + writeLogToFile(tag, String.format("\nmsg【%s】\nException:%s", msg, ex)); } /** @@ -48,10 +46,8 @@ public class ErrorHelp { * @return "/mnt/sdcard/Android/data/{package_name}/files/log/*" */ private static String getLogPath() { - String path = CommonUtil.getAppPath(AriaManager.APP) - + "log/AriaCrash_" - + getData("yyyy-MM-dd_HH:mm:ss") - + ".log"; + String path = String.format("%slog/AriaCrash_%s.log", CommonUtil.getAppPath(AriaManager.APP), + getData("yyyy-MM-dd_HH:mm:ss")); File log = new File(path); if (!log.getParentFile().exists()) { diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/EventProxyFiler.java b/AriaCompiler/src/main/java/com/arialyy/compiler/EventProxyFiler.java index f695759f..bf48b248 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/EventProxyFiler.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/EventProxyFiler.java @@ -16,6 +16,7 @@ package com.arialyy.compiler; import com.arialyy.annotations.Download; +import com.arialyy.annotations.DownloadGroup; import com.arialyy.annotations.Upload; import com.squareup.javapoet.ClassName; import com.squareup.javapoet.CodeBlock; @@ -91,33 +92,54 @@ final class EventProxyFiler { * * @param taskEnum 任务类型枚举{@link TaskEnum} * @param annotation {@link Download}、{@link Upload} - * @param methodName 被代理类注解的方法名 + * @param methodInfo 被代理类注解的方法信息 */ private MethodSpec createProxyMethod(TaskEnum taskEnum, Class annotation, - String methodName) { + MethodInfo methodInfo) { ClassName task = ClassName.get(taskEnum.getPkg(), taskEnum.getClassName()); ParameterSpec taskParam = ParameterSpec.builder(task, "task").addModifiers(Modifier.FINAL).build(); String callCode; + if (taskEnum == TaskEnum.DOWNLOAD_GROUP_SUB) { - callCode = "task, subEntity"; + if (methodInfo.params.get(methodInfo.params.size() - 1) + .asType() + .toString() + .equals(Exception.class.getName()) + && annotation == DownloadGroup.onSubTaskFail.class) { + callCode = "task, subEntity, e"; + } else { + callCode = "task, subEntity"; + } } else { - callCode = "task"; + if (methodInfo.params.get(methodInfo.params.size() - 1) + .asType() + .toString() + .equals(Exception.class.getName()) + && (annotation == Download.onTaskFail.class + || annotation == Upload.onTaskFail.class + || annotation == DownloadGroup.onTaskFail.class)) { + callCode = "task, e"; + } else { + callCode = "task"; + } } StringBuilder sb = new StringBuilder(); - sb.append("Set keys = keyMapping.get(\"").append(methodName).append("\");\n"); + sb.append("Set keys = keyMapping.get(\"") + .append(methodInfo.methodName) + .append("\");\n"); sb.append("if (keys != null) {\n\tif (keys.contains(task.getKey())) {\n") .append("\t\tobj.") - .append(methodName) + .append(methodInfo.methodName) .append("((") .append(taskEnum.getClassName()) .append(")") .append(callCode) .append(");\n\t}\n} else {\n") .append("\tobj.") - .append(methodName) + .append(methodInfo.methodName) .append("((") .append(taskEnum.getClassName()) .append(")") @@ -140,6 +162,15 @@ final class EventProxyFiler { builder.addParameter(subTaskParam); } + + if (annotation == Download.onTaskFail.class + || annotation == Upload.onTaskFail.class + || annotation == DownloadGroup.onTaskFail.class + || annotation == DownloadGroup.onSubTaskFail.class) { + ParameterSpec exception = ParameterSpec.builder(Exception.class, "e").build(); + builder.addParameter(exception); + } + return builder.build(); } @@ -165,7 +196,7 @@ final class EventProxyFiler { //Set type = new HashSet<>(); //添加注解方法 for (TaskEnum te : entity.methods.keySet()) { - Map, String> temp = entity.methods.get(te); + Map, MethodInfo> methodInfoMap = entity.methods.get(te); //if (entity.proxyClassName.contains(TaskEnum.DOWNLOAD.proxySuffix)) { // type.add(1); //} else if (entity.proxyClassName.contains(TaskEnum.DOWNLOAD_GROUP.proxySuffix)) { @@ -175,9 +206,9 @@ final class EventProxyFiler { //} else if (entity.proxyClassName.contains(TaskEnum.UPLOAD_GROUP.proxySuffix)) { // type.add(4); //} - if (temp != null) { - for (Class annotation : temp.keySet()) { - MethodSpec method = createProxyMethod(te, annotation, temp.get(annotation)); + if (methodInfoMap != null) { + for (Class annotation : methodInfoMap.keySet()) { + MethodSpec method = createProxyMethod(te, annotation, methodInfoMap.get(annotation)); builder.addMethod(method); } } diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/MethodInfo.java b/AriaCompiler/src/main/java/com/arialyy/compiler/MethodInfo.java new file mode 100644 index 00000000..13867491 --- /dev/null +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/MethodInfo.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.compiler; + +import java.util.List; +import javax.lang.model.element.VariableElement; + +/** + * 方法信息 + */ +final class MethodInfo { + String methodName; + List params; +} diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/ParamObtainUtil.java b/AriaCompiler/src/main/java/com/arialyy/compiler/ParamObtainUtil.java index 63a4440d..422eb8e2 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/ParamObtainUtil.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/ParamObtainUtil.java @@ -15,6 +15,9 @@ */ package com.arialyy.compiler; +import com.arialyy.annotations.Download; +import com.arialyy.annotations.DownloadGroup; +import com.arialyy.annotations.Upload; import java.lang.annotation.Annotation; import java.util.Collections; import java.util.HashMap; @@ -70,11 +73,15 @@ class ParamObtainUtil { ExecutableElement method = (ExecutableElement) element; TypeElement classElement = (TypeElement) method.getEnclosingElement(); PackageElement packageElement = mElementUtil.getPackageOf(classElement); - checkDownloadMethod(taskEnum, method); + String methodName = method.getSimpleName().toString(); String className = method.getEnclosingElement().toString(); //全类名\ String key = className + taskEnum.proxySuffix; ProxyClassParam proxyEntity = mMethodParams.get(key); + MethodInfo methodInfo = new MethodInfo(); + methodInfo.methodName = methodName; + methodInfo.params = (List) method.getParameters(); + checkMethod(taskEnum, method, annotationClazz, methodInfo.params); if (proxyEntity == null) { proxyEntity = new ProxyClassParam(); @@ -90,9 +97,10 @@ class ParamObtainUtil { } proxyEntity.taskEnums.add(taskEnum); if (proxyEntity.methods.get(taskEnum) == null) { - proxyEntity.methods.put(taskEnum, new HashMap, String>()); + proxyEntity.methods.put(taskEnum, new HashMap, MethodInfo>()); } - proxyEntity.methods.get(taskEnum).put(annotationClazz, methodName); + + proxyEntity.methods.get(taskEnum).put(annotationClazz, methodInfo); proxyEntity.keyMappings.put(methodName, getValues(taskEnum, method, annotationType)); } } @@ -144,52 +152,71 @@ class ParamObtainUtil { /** * 检查和下载相关的方法,如果被注解的方法为private或参数不合法,则抛异常 */ - private void checkDownloadMethod(TaskEnum taskEnum, ExecutableElement method) { + private void checkMethod(TaskEnum taskEnum, ExecutableElement method, + Class annotationClazz, List params) { String methodName = method.getSimpleName().toString(); String className = method.getEnclosingElement().toString(); Set modifiers = method.getModifiers(); if (modifiers.contains(Modifier.PRIVATE)) { - throw new IllegalAccessError(className + "." + methodName + "不能为private方法"); + throw new IllegalAccessError(String.format("%s.%s, 不能为private方法", className, methodName)); } - List params = (List) method.getParameters(); if (taskEnum == TaskEnum.DOWNLOAD_GROUP_SUB) { - if (params.size() != 2) { - throw new IllegalArgumentException(className - + "." - + methodName - + "参数错误, 参数只有两个,且第一个参数必须是" - + getCheckParams(taskEnum) - + ",第二个参数必须是" - + getCheckSubParams(taskEnum)); + if (isFailAnnotation(annotationClazz)) { + if (params.size() != 3 && params.size() != 2) { + throw new IllegalArgumentException( + String.format("%s.%s参数错误, 参数只能是两个或三个,第一个参数是:%s,第二个参数是:%s,第三个参数(可选)是:%s", className, + methodName, + getCheckParams(taskEnum), getCheckSubParams(taskEnum), + Exception.class.getSimpleName())); + } + } else { + if (params.size() != 2) { + throw new IllegalArgumentException( + String.format("%s.%s参数错误, 参数只能是两个,第一个参数是:%s,第二个参数是:%s", className, methodName, + getCheckParams(taskEnum), getCheckSubParams(taskEnum))); + } } } else { - if (params.size() != 1) { - throw new IllegalArgumentException( - className + "." + methodName + "参数错误, 参数只能有一个,且参数必须是" + getCheckParams(taskEnum)); + if (isFailAnnotation(annotationClazz)) { + if (params.size() != 1 && params.size() != 2) { + throw new IllegalArgumentException( + String.format("%s.%s参数错误, 参数只能有一个或两个,第一个参数是:%s,第二个参(可选)数是:%s", className, methodName, + getCheckParams(taskEnum), Exception.class.getSimpleName())); + } + } else { + if (params.size() != 1) { + throw new IllegalArgumentException( + String.format("%s.%s参数错误, 参数只能有一个,且参数必须是:%s", className, methodName, + getCheckParams(taskEnum))); + } } } if (!params.get(0).asType().toString().equals(getCheckParams(taskEnum))) { - throw new IllegalArgumentException(className - + "." - + methodName - + "参数【" - + params.get(0).getSimpleName() - + "】类型错误,参数必须是" - + getCheckParams(taskEnum)); + throw new IllegalArgumentException( + String.format("%s.%s参数【%s】类型错误,参数必须是:%s", className, methodName, + params.get(0).getSimpleName(), getCheckParams(taskEnum))); } if (taskEnum == TaskEnum.DOWNLOAD_GROUP_SUB) { if (!params.get(1).asType().toString().equals(getCheckSubParams(taskEnum))) { - throw new IllegalArgumentException(className - + "." - + methodName - + "参数【" - + params.get(0).getSimpleName() - + "】类型错误,参数必须是" - + getCheckSubParams(taskEnum)); + throw new IllegalArgumentException( + String.format("%s.%s参数【%s】类型错误,参数必须是:%s", className, methodName, + params.get(0).getSimpleName(), getCheckSubParams(taskEnum))); } } } + /** + * 判断是否是任务失败的回调注解 + * + * @return {@code true}是任务失败的回调注解 + */ + private boolean isFailAnnotation(Class annotationClazz) { + return annotationClazz == Download.onTaskFail.class + || annotationClazz == DownloadGroup.onTaskFail.class + || annotationClazz == DownloadGroup.onSubTaskFail.class + || annotationClazz == Upload.onTaskFail.class; + } + /** * 字符串数组转set * diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyClassParam.java b/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyClassParam.java index 3b9a9a88..b5f3f244 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyClassParam.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyClassParam.java @@ -48,5 +48,5 @@ class ProxyClassParam { Set taskEnums; Map> keyMappings = new HashMap<>(); - Map, String>> methods = new HashMap<>(); + Map, MethodInfo>> methods = new HashMap<>(); } diff --git a/DEV_LOG.md b/DEV_LOG.md index c91242bb..b5895bbc 100644 --- a/DEV_LOG.md +++ b/DEV_LOG.md @@ -1,4 +1,13 @@ ## 开发日志 + + v_3.5.2 + - 添加Serializable接口支持 https://github.com/AriaLyy/Aria/issues/320 + - 失败回调增加错误原因 https://github.com/AriaLyy/Aria/issues/310 + ``` + @Download.onTaskFail void taskFail(DownloadTask task, Exception e) { + e.getMessage(); + ... + } + ``` + v_3.5.1 - 优化`taskExists`方法 - 添加`post`参数请求支持 diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b35e5470..05f2f200 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,5 +1,6 @@ @@ -12,14 +13,16 @@ android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" - android:theme="@style/AppTheme.NoActionBar"> + android:theme="@style/AppTheme.NoActionBar" + tools:ignore="GoogleAppIndexingWarning"> - + + @@ -29,7 +32,7 @@ - + diff --git a/app/src/main/java/com/arialyy/simple/download/KotlinDownloadActivity.kt b/app/src/main/java/com/arialyy/simple/download/KotlinDownloadActivity.kt index 16c3e6e8..ef873639 100644 --- a/app/src/main/java/com/arialyy/simple/download/KotlinDownloadActivity.kt +++ b/app/src/main/java/com/arialyy/simple/download/KotlinDownloadActivity.kt @@ -1,75 +1,75 @@ -package com.arialyy.simple.download - -import android.os.Bundle -import android.os.Environment -import android.support.v7.app.AppCompatActivity -import android.util.Log -import android.view.View -import com.arialyy.annotations.Download -import com.arialyy.aria.core.Aria -import com.arialyy.aria.core.download.DownloadTask -import com.arialyy.simple.R -import com.arialyy.simple.base.BaseActivity - -/** - * Created by lyy on 2017/10/23. - */ -class KotlinDownloadActivity : AppCompatActivity() { - - private val DOWNLOAD_URL = "http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk" - - private val TAG = "KotlinDownloadActivity"; - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentView(setLayoutId()) - } - - fun setLayoutId(): Int { - return R.layout.activity_single - } - - fun init(savedInstanceState: Bundle?) { - title = "单任务下载" -// val target = Aria.download(this).load(DOWNLOAD_URL) -// binding.progress = target.getPercent() -// if (target.getTaskState() == IEntity.STATE_STOP) { -// mStart.setText("恢复") -// mStart.setTextColor(resources.getColor(android.R.color.holo_blue_light)) -// setBtState(true) -// } else if (target.isDownloading()) { -// setBtState(false) +//package com.arialyy.simple.download +// +//import android.os.Bundle +//import android.os.Environment +//import android.support.v7.app.AppCompatActivity +//import android.util.Log +//import android.view.View +//import com.arialyy.annotations.Download +//import com.arialyy.aria.core.Aria +//import com.arialyy.aria.core.download.DownloadTask +//import com.arialyy.simple.R +//import com.arialyy.simple.base.BaseActivity +// +///** +// * Created by lyy on 2017/10/23. +// */ +//class KotlinDownloadActivity : AppCompatActivity() { +// +// private val DOWNLOAD_URL = "http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk" +// +// private val TAG = "KotlinDownloadActivity"; +// +// override fun onCreate(savedInstanceState: Bundle?) { +// super.onCreate(savedInstanceState) +// setContentView(setLayoutId()) +// } +// +// fun setLayoutId(): Int { +// return R.layout.activity_single +// } +// +// fun init(savedInstanceState: Bundle?) { +// title = "单任务下载" +//// val target = Aria.download(this).load(DOWNLOAD_URL) +//// binding.progress = target.getPercent() +//// if (target.getTaskState() == IEntity.STATE_STOP) { +//// mStart.setText("恢复") +//// mStart.setTextColor(resources.getColor(android.R.color.holo_blue_light)) +//// setBtState(true) +//// } else if (target.isDownloading()) { +//// setBtState(false) +//// } +//// binding.fileSize = target.getConvertFileSize() +// Aria.get(this).downloadConfig.maxTaskNum = 2 +// Aria.download(this).register() +// } +// +// @Download.onTaskRunning +// protected fun running(task: DownloadTask) { +// Log.d(TAG, task.percent.toString() + "") +//// val len = task.fileSize +//// if (len == 0L) { +//// binding.progress = 0 +//// } else { +//// binding.progress = task.percent +//// } +//// binding.speed = task.convertSpeed +// } +// +// fun onClick(view: View) { +// when (view.id) { +// R.id.start -> startD() +// R.id.stop -> Aria.download(this).load(DOWNLOAD_URL).stop() +// R.id.cancel -> Aria.download(this).load(DOWNLOAD_URL).cancel() // } -// binding.fileSize = target.getConvertFileSize() - Aria.get(this).downloadConfig.maxTaskNum = 2 - Aria.download(this).register() - } - - @Download.onTaskRunning - protected fun running(task: DownloadTask) { - Log.d(TAG, task.percent.toString() + "") -// val len = task.fileSize -// if (len == 0L) { -// binding.progress = 0 -// } else { -// binding.progress = task.percent -// } -// binding.speed = task.convertSpeed - } - - fun onClick(view: View) { - when (view.id) { - R.id.start -> startD() - R.id.stop -> Aria.download(this).load(DOWNLOAD_URL).stop() - R.id.cancel -> Aria.download(this).load(DOWNLOAD_URL).cancel() - } - } - - private fun startD() { - Aria.download(this) - .load(DOWNLOAD_URL) - .addHeader("groupName", "value") - .setDownloadPath(Environment.getExternalStorageDirectory().path + "/hhhhhhhh.apk") - .start() - } -} \ No newline at end of file +// } +// +// private fun startD() { +// Aria.download(this) +// .load(DOWNLOAD_URL) +// .addHeader("groupName", "value") +// .setDownloadPath(Environment.getExternalStorageDirectory().path + "/hhhhhhhh.apk") +// .start() +// } +//} \ No newline at end of file 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 1fcfd4d3..dafd159b 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -16,6 +16,7 @@ package com.arialyy.simple.download; +import android.content.ContentResolver; import android.content.Intent; import android.net.Uri; import android.os.Bundle; @@ -49,7 +50,7 @@ public class SingleTaskActivity extends BaseActivity { private static final String DOWNLOAD_URL = //"http://kotlinlang.org/docs/kotlin-docs.pdf"; //"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe"; - "http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk"; + "http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apks"; //"https://yizi-kejian.oss-cn-beijing.aliyuncs.com/qimeng/package1/qmtable11.zip"; //"http://rs.0.gaoshouyou.com/d/04/1e/400423a7551e1f3f0eb1812afa1f9b44.apk"; //"http://58.210.9.131/tpk/sipgt//TDLYZTGH.tpk"; //chunked 下载 @@ -248,7 +249,7 @@ public class SingleTaskActivity extends BaseActivity { private void startD() { //Aria.get(this).setLogLevel(ALog.LOG_CLOSE); //Aria.download(this).load("aaaa.apk"); - String path = Environment.getExternalStorageDirectory().getPath() + "/ggsg9.apk"; + String path = Environment.getExternalStorageDirectory().getPath() + "/ggsg10.apk"; //File file = new File(path); //if (file.exists()){ // file.delete(); 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 8433f960..a0f1d547 100644 --- a/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java +++ b/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java @@ -31,7 +31,8 @@ public class AnyRunActivity extends BaseActivity { //String URL = "ftps://192.168.29.140:990/download/SDK_Demo-release.apk"; //String URL = "http://d.quanscreen.com/k/down/resourceDownLoad?resourceId=1994&clientId=A000011106034058176"; //String URL = "ftp://z:z@dygod18.com:21211/[电影天堂www.dy2018.com]猩球崛起3:终极之战BD国英双语中英双字.mkv"; - String URL = "https://app-dl.byfen.com//app//201810//ef0400d637e768e9c630b4d38633ee06.zip"; + //String URL = "https://app-dl.byfen.com//app//201810//ef0400d637e768e9c630b4d38633ee06.zip"; + String URL = "ftps://9.9.9.59:990/download/SDK_Demo-release.apk"; //private String URL = "https://www.bilibili.com/bangumi/play/ep77693"; //private String URL = "http://cn-hbsjz-cmcc-v-03.acgvideo.com/upgcxcode/63/82/5108263/5108263-1-80.flv?expires=1530178500&platform=pc&ssig=vr7gLl0duyqWqSMnIpzaDA&oi=3746029570&nfa=BpfiWF+i4mNW8KzjZFHzBQ==&dynamic=1&hfa=2030547937&hfb=Yjk5ZmZjM2M1YzY4ZjAwYTMzMTIzYmIyNWY4ODJkNWI=&trid=3476be01a9254115b15f8cc7198600fe&nfc=1"; @@ -52,8 +53,8 @@ public class AnyRunActivity extends BaseActivity { public void onClick(View view) { switch (view.getId()) { case R.id.start: - module.start(URL); - //module.startFtp(URL); + //module.start(URL); + module.startFtp(URL); break; case R.id.stop: module.stop(URL); 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 06d3ff43..86bb4a03 100644 --- a/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java +++ b/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java @@ -78,10 +78,10 @@ public class AnyRunnModule { .loadFtp(url) .login("lao", "123456") .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/Download/") - //.asFtps() - //.setStorePath("/mnt/sdcard/Download/server.crt") - //.setAlias("www.laoyuyu.me") - //.setStorePass("123456") + .asFtps() + .setStorePath("/mnt/sdcard/Download/server.crt") + .setAlias("www.laoyuyu.me") + .setStorePass("123456") .start(); } diff --git a/aria/src/main/java/com/arialyy/aria/exception/FileException.java b/aria/src/main/java/com/arialyy/aria/exception/FileException.java index 7122da80..0c4ffed5 100644 --- a/aria/src/main/java/com/arialyy/aria/exception/FileException.java +++ b/aria/src/main/java/com/arialyy/aria/exception/FileException.java @@ -19,10 +19,14 @@ package com.arialyy.aria.exception; * Created by lyy on 2017/1/18. * Aria 文件异常 */ -public class FileException extends NullPointerException { - private static final String ARIA_FILE_EXCEPTION = "Aria Exception:"; +public class FileException extends BaseException { + private static final String ARIA_FILE_EXCEPTION = "Aria File Exception:"; - public FileException(String detailMessage) { - super(ARIA_FILE_EXCEPTION + detailMessage); + public FileException(String tag, String detailMessage) { + super(tag, String.format("%s%s", ARIA_FILE_EXCEPTION, detailMessage)); + } + + public FileException(String tag, String message, Exception e){ + super(tag, message, e); } } diff --git a/gradle.properties b/gradle.properties index 36ed63a9..99bf62fd 100644 --- a/gradle.properties +++ b/gradle.properties @@ -13,8 +13,8 @@ # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true #Wed Dec 07 20:19:22 CST 2016 -#org.gradle.daemon=true -#org.gradle.jvmargs=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 +org.gradle.daemon=true +org.gradle.jvmargs=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 # gradle proxy https://chaosleong.github.io/2017/02/10/Configuring-Gradle-Proxy/ #systemProp.socksProxyHost=127.0.0.1 #systemProp.socksProxyPort=60777