From 375eb254995d55eeb64a8a10501d5fb0d00cf0b9 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Tue, 11 Jul 2017 17:26:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E7=BB=84bug=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aria/core/download/DownloadEntity.java | 98 ++++++-- .../core/download/DownloadGroupTarget.java | 15 +- .../aria/core/download/DownloadGroupTask.java | 9 +- .../aria/core/download/DownloadReceiver.java | 21 +- .../aria/core/download/DownloadTarget.java | 8 +- .../aria/core/download/DownloadTask.java | 10 +- .../downloader/DownloadGroupUtil.java | 65 +++-- .../download/downloader/FileInfoThread.java | 21 +- ...nloadUtil.java => SimpleDownloadUtil.java} | 31 ++- .../com/arialyy/aria/core/inf/AbsEntity.java | 15 ++ .../com/arialyy/aria/core/inf/AbsTarget.java | 27 ++ .../arialyy/aria/core/inf/AbsTaskEntity.java | 20 ++ .../java/com/arialyy/aria/orm/DBConfig.java | 2 +- .../java/com/arialyy/aria/orm/SqlHelper.java | 6 +- .../com/arialyy/aria/util/CommonUtil.java | 4 + .../com/arialyy/compiler/AriaProcessor.java | 4 +- ...ElementHandle.java => ElementHandler.java} | 4 +- .../simple/download/SingleTaskActivity.java | 19 +- .../fragment_download/DownloadFragment.java | 26 +- .../download/group/DownloadGroupActivity.java | 31 ++- .../simple/download/group/GroupModule.java | 2 +- .../res/layout/activity_download_group.xml | 28 ++- app/src/main/res/layout/activity_single.xml | 26 +- app/src/main/res/layout/content_single.xml | 238 ++++++++++-------- app/src/main/res/layout/fragment_download.xml | 27 +- app/src/main/res/values/strings.xml | 2 +- .../arialyy/aria/core/upload/UploadTask.java | 4 +- 27 files changed, 523 insertions(+), 240 deletions(-) rename Aria/src/main/java/com/arialyy/aria/core/download/downloader/{DownloadUtil.java => SimpleDownloadUtil.java} (69%) rename AriaCompiler/src/main/java/com/arialyy/compiler/{ElementHandle.java => ElementHandler.java} (99%) diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java index 340c91f1..9b7ad44f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java @@ -19,6 +19,7 @@ package com.arialyy.aria.core.download; import android.os.Parcel; import android.os.Parcelable; import com.arialyy.aria.core.inf.AbsNormalEntity; +import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.orm.Primary; /** @@ -37,9 +38,79 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable { */ private String groupName = ""; + /** + * 通过{@link AbsTaskEntity#md5Key}从服务器的返回信息中获取的文件md5信息,如果服务器没有返回,则不会设置该信息 + * 如果你已经设置了该任务的MD5信息,Aria也不会从服务器返回的信息中获取该信息 + */ + private String md5Code = ""; + + /** + * 通过{@link AbsTaskEntity#dispositionKey}从服务器的返回信息中获取的文件描述信息 + */ + private String disposition = ""; + + /** + * 从disposition获取到的文件名,如果可以获取到,则会赋值到这个字段 + */ + private String serverFileName = ""; + public DownloadEntity() { } + @Override public String toString() { + return "DownloadEntity{" + + "downloadUrl='" + + downloadUrl + + '\'' + + ", downloadPath='" + + downloadPath + + '\'' + + ", isDownloadComplete=" + + isDownloadComplete + + ", isRedirect=" + + isRedirect + + ", redirectUrl='" + + redirectUrl + + '\'' + + ", groupName='" + + groupName + + '\'' + + ", md5Code='" + + md5Code + + '\'' + + ", disposition='" + + disposition + + '\'' + + ", serverFileName='" + + serverFileName + + '\'' + + '}'; + } + + public String getMd5Code() { + return md5Code; + } + + public void setMd5Code(String md5Code) { + this.md5Code = md5Code; + } + + public String getDisposition() { + return disposition; + } + + public void setDisposition(String disposition) { + this.disposition = disposition; + } + + public String getServerFileName() { + return serverFileName; + } + + public void setServerFileName(String serverFileName) { + this.serverFileName = serverFileName; + } + public String getGroupName() { return groupName; } @@ -106,6 +177,9 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable { dest.writeByte(this.isRedirect ? (byte) 1 : (byte) 0); dest.writeString(this.redirectUrl); dest.writeString(this.groupName); + dest.writeString(this.md5Code); + dest.writeString(this.disposition); + dest.writeString(this.serverFileName); } protected DownloadEntity(Parcel in) { @@ -116,27 +190,9 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable { this.isRedirect = in.readByte() != 0; this.redirectUrl = in.readString(); this.groupName = in.readString(); - } - - @Override public String toString() { - return "DownloadEntity{" - + "downloadUrl='" - + downloadUrl - + '\'' - + ", downloadPath='" - + downloadPath - + '\'' - + ", isDownloadComplete=" - + isDownloadComplete - + ", isRedirect=" - + isRedirect - + ", redirectUrl='" - + redirectUrl - + '\'' - + ", groupName='" - + groupName - + '\'' - + '}'; + this.md5Code = in.readString(); + this.disposition = in.readString(); + this.serverFileName = in.readString(); } public static final Creator CREATOR = new Creator() { 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 96eca6d8..52ea22a5 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 @@ -16,6 +16,7 @@ package com.arialyy.aria.core.download; import android.text.TextUtils; +import android.util.Log; import com.arialyy.aria.core.inf.AbsGroupTarget; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.CommonUtil; @@ -29,6 +30,7 @@ import java.util.List; public class DownloadGroupTarget extends AbsGroupTarget { private List mUrls = new ArrayList<>(); + private final String TAG = "DownloadGroupTarget"; /** * 子任务文件名 */ @@ -158,7 +160,7 @@ public class DownloadGroupTarget for (int i = 0, len = mUrls.size(); i < len; i++) { DownloadEntity entity = new DownloadEntity(); entity.setDownloadUrl(mUrls.get(i)); - String fileName = mSubTaskFileName.isEmpty() ? CommonUtil.keyToHashKey(mUrls.get(i)) + String fileName = mSubTaskFileName.isEmpty() ? createFileName(entity.getDownloadUrl()) : mSubTaskFileName.get(i); entity.setDownloadPath(mEntity.getDirPath() + "/" + fileName); entity.setGroupName(mGroupName); @@ -170,14 +172,5 @@ public class DownloadGroupTarget return list; } - ///** - // * 创建文件名,如果url链接有后缀名,则使用url中的后缀名 - // * @return url 的 hashKey - // */ - //private String createFileName(String url){ - // if (url.contains(".")){ - // int s = url.lastIndexOf("."); - // String fileName = url.substring() - // } - //} + } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java index 0307a31a..2c87c210 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java @@ -17,6 +17,7 @@ package com.arialyy.aria.core.download; import android.content.Context; import android.os.Handler; +import android.util.Log; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.download.downloader.DownloadGroupUtil; import com.arialyy.aria.core.download.downloader.DownloadListener; @@ -34,6 +35,7 @@ import java.lang.ref.WeakReference; * 任务组任务 */ public class DownloadGroupTask extends AbsGroupTask { + private final String TAG = "DownloadGroupTask"; private DListener mListener; private IDownloadUtil mUtil; @@ -55,7 +57,7 @@ public class DownloadGroupTask extends AbsGroupTask outHandler; WeakReference wTask; Context context; @@ -92,6 +95,7 @@ public class DownloadGroupTask extends AbsGroupTask urls) { + CheckUtil.checkDownloadUrls(urls); + String hashCode = CommonUtil.getMd5Code(urls); + return DbEntity.findFirst(DownloadGroupTaskEntity.class, "key=?", hashCode); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java index 799e729d..cef162c0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java @@ -39,7 +39,9 @@ public class DownloadTarget mTaskEntity = DbEntity.findFirst(DownloadTaskEntity.class, "key=?", url); if (mTaskEntity == null) { mTaskEntity = new DownloadTaskEntity(); - mTaskEntity.entity = new DownloadEntity(); + mTaskEntity.key = url; + mTaskEntity.entity = getEntity(url); + mTaskEntity.save(); } if (mTaskEntity.entity == null) { mTaskEntity.entity = getEntity(url); @@ -57,12 +59,14 @@ public class DownloadTarget DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl); if (entity == null) { entity = new DownloadEntity(); + entity.setDownloadUrl(downloadUrl); + entity.setGroupChild(false); + entity.save(); } File file = new File(entity.getDownloadPath()); if (!file.exists()) { entity.setState(IEntity.STATE_WAIT); } - entity.setDownloadUrl(downloadUrl); return entity; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java index 5c36b1d9..8567a1fb 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java @@ -21,12 +21,11 @@ import android.os.Handler; import android.util.Log; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.download.downloader.DownloadListener; -import com.arialyy.aria.core.download.downloader.DownloadUtil; +import com.arialyy.aria.core.download.downloader.SimpleDownloadUtil; import com.arialyy.aria.core.download.downloader.IDownloadListener; import com.arialyy.aria.core.inf.AbsNormalTask; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.scheduler.ISchedulers; -import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CommonUtil; import java.io.File; import java.lang.ref.WeakReference; @@ -39,7 +38,7 @@ public class DownloadTask extends AbsNormalTask { public static final String TAG = "DownloadTask"; private IDownloadListener mListener; - private DownloadUtil mUtil; + private SimpleDownloadUtil mUtil; private boolean isWait = false; private DownloadTask(DownloadTaskEntity taskEntity, Handler outHandler) { @@ -47,7 +46,7 @@ public class DownloadTask extends AbsNormalTask { mOutHandler = outHandler; mContext = AriaManager.APP; mListener = new DListener(mContext, this, mOutHandler); - mUtil = new DownloadUtil(taskEntity, mListener); + mUtil = new SimpleDownloadUtil(taskEntity, mListener); } /** @@ -164,7 +163,6 @@ public class DownloadTask extends AbsNormalTask { String targetName; public Builder(String targetName, DownloadTaskEntity taskEntity) { - CheckUtil.checkTaskEntity(taskEntity); this.targetName = targetName; this.taskEntity = taskEntity; } @@ -226,6 +224,7 @@ public class DownloadTask extends AbsNormalTask { @Override public void onPostPre(long fileSize) { entity.setFileSize(fileSize); + entity.setConvertFileSize(CommonUtil.formatFileSize(fileSize)); sendInState2Target(ISchedulers.POST_PRE); saveData(IEntity.STATE_POST_PRE, -1); } @@ -305,7 +304,6 @@ public class DownloadTask extends AbsNormalTask { entity.setDownloadComplete(state == IEntity.STATE_COMPLETE); entity.setCurrentProgress(location); entity.update(); - } } } \ No newline at end of file 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 241a1251..45cb39aa 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 @@ -21,6 +21,7 @@ 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.orm.DbEntity; +import com.arialyy.aria.util.CommonUtil; import java.io.File; import java.util.HashMap; import java.util.Map; @@ -40,7 +41,7 @@ public class DownloadGroupUtil implements IDownloadUtil { * 任务组所有任务总大小 */ private long mTotalSize = 0; - private long mCurrentProgress = 0; + private long mCurrentLocation = 0; private ExecutorService mInfoPool; private ExecutorService mExePool; private IDownloadListener mListener; @@ -54,7 +55,7 @@ public class DownloadGroupUtil implements IDownloadUtil { /** * 初始化失败的任务数 */ - private int mFailNum = 0; + private int mInitFailNum = 0; /** * 保存所有没有下载完成的任务,key为下载地址 */ @@ -75,20 +76,28 @@ public class DownloadGroupUtil implements IDownloadUtil { */ private SparseArray mFileInfoCallbacks = new SparseArray<>(); + //任务总数 + private int mTaskNum = 0; + //已经完成的任务数 + private int mCompleteNum = 0; + //失败的任务数 + private int mFailNum = 0; + public DownloadGroupUtil(IDownloadListener listener, DownloadGroupTaskEntity taskEntity) { mListener = listener; mTaskEntity = taskEntity; mInfoPool = Executors.newCachedThreadPool(); mExePool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); - + mTaskNum = mTaskEntity.entity.getSubTask().size(); for (DownloadEntity entity : mTaskEntity.entity.getSubTask()) { File file = new File(entity.getDownloadPath()); if (entity.isDownloadComplete() && file.exists()) { mTotalSize += entity.getFileSize(); + mCompleteNum++; } else { mExeMap.put(entity.getDownloadUrl(), createDownloadTask(entity)); } - mCurrentProgress += entity.getCurrentProgress(); + mCurrentLocation += entity.getCurrentProgress(); } } @@ -97,7 +106,7 @@ public class DownloadGroupUtil implements IDownloadUtil { } @Override public long getCurrentLocation() { - return mCurrentProgress; + return mCurrentLocation; } @Override public boolean isDownloading() { @@ -106,6 +115,7 @@ public class DownloadGroupUtil implements IDownloadUtil { @Override public void cancelDownload() { isRunning = false; + mListener.onCancel(); closeTimer(); if (!mInfoPool.isShutdown()) { mInfoPool.shutdown(); @@ -125,6 +135,7 @@ public class DownloadGroupUtil implements IDownloadUtil { @Override public void stopDownload() { isRunning = false; + mListener.onStop(mCurrentLocation); closeTimer(); if (!mInfoPool.isShutdown()) { mInfoPool.shutdown(); @@ -156,6 +167,7 @@ public class DownloadGroupUtil implements IDownloadUtil { @Override public void resumeDownload() { startDownload(); + mListener.onResume(mCurrentLocation); } /** @@ -175,7 +187,7 @@ public class DownloadGroupUtil implements IDownloadUtil { startChildDownload(te); } mInitNum++; - if (mInitNum + mFailNum == mTaskEntity.getEntity().getSubTask().size()) { + if (mInitNum + mInitFailNum == mTaskEntity.getEntity().getSubTask().size()) { startRunningFlow(); } } @@ -186,12 +198,15 @@ public class DownloadGroupUtil implements IDownloadUtil { mFailMap.put(url, te); mFileInfoCallbacks.put(te.hashCode(), this); } - mFailNum++; - failNum++; - if (failNum < 10) { - mInfoPool.execute(createFileInfoThread(te)); + mInitFailNum++; + //404链接不重试下载 + if (!errorMsg.contains("错误码:404")) { + if (failNum < 10) { + mInfoPool.execute(createFileInfoThread(te)); + } } - if (mInitNum + mFailNum == mTaskEntity.getEntity().getSubTask().size()) { + failNum++; + if (mInitNum + mInitFailNum == mTaskEntity.getEntity().getSubTask().size()) { startRunningFlow(); } } @@ -213,12 +228,14 @@ public class DownloadGroupUtil implements IDownloadUtil { */ private void startRunningFlow() { mListener.onPostPre(mTotalSize); - mListener.onStart(mCurrentProgress); + mListener.onStart(mCurrentLocation); closeTimer(); mTimer = new Timer(true); mTimer.schedule(new TimerTask() { @Override public void run() { - mListener.onProgress(mCurrentProgress); + if (mCurrentLocation >= 0) { + mListener.onProgress(mCurrentLocation); + } } }, 0, 1000); } @@ -258,6 +275,7 @@ public class DownloadGroupUtil implements IDownloadUtil { DownloadTaskEntity taskEntity; DownloadEntity entity; + long lastLen = 0; ChildDownloadListener(DownloadTaskEntity entity) { this.taskEntity = entity; @@ -270,6 +288,7 @@ public class DownloadGroupUtil implements IDownloadUtil { @Override public void onPostPre(long fileSize) { entity.setFileSize(fileSize); + entity.setConvertFileSize(CommonUtil.formatFileSize(fileSize)); saveData(IEntity.STATE_POST_PRE, -1); } @@ -279,10 +298,12 @@ public class DownloadGroupUtil implements IDownloadUtil { @Override public void onStart(long startLocation) { saveData(IEntity.STATE_POST_PRE, IEntity.STATE_RUNNING); + lastLen = startLocation; } @Override public void onProgress(long currentLocation) { - mCurrentProgress += currentLocation; + mCurrentLocation += (currentLocation - lastLen); + lastLen = currentLocation; } @Override public void onStop(long stopLocation) { @@ -295,11 +316,27 @@ public class DownloadGroupUtil implements IDownloadUtil { @Override public void onComplete() { saveData(IEntity.STATE_COMPLETE, entity.getFileSize()); + mCompleteNum++; + if (mCompleteNum >= mTaskNum) { + mListener.onComplete(); + closeTimer(); + } } @Override public void onFail() { entity.setFailNum(entity.getFailNum() + 1); saveData(IEntity.STATE_FAIL, -1); + reTry(); + } + + /** + * 失败后重试下载,如果失败次数超过5次,不再重试 + */ + private void reTry() { + if (entity.getFailNum() < 5) { + Downloader dt = mDownloaderMap.get(entity.getDownloadUrl()); + mExePool.execute(dt); + } } private void saveData(int state, long location) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FileInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FileInfoThread.java index 0cf0bea8..ab6dd7f1 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FileInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FileInfoThread.java @@ -24,6 +24,9 @@ import com.arialyy.aria.util.CommonUtil; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; +import java.net.URLDecoder; +import java.util.List; +import java.util.Map; /** * 下载文件信息获取 @@ -91,6 +94,19 @@ class FileInfoThread implements Runnable { int len = conn.getContentLength(); int code = conn.getResponseCode(); boolean isComplete = false; + if (TextUtils.isEmpty(mEntity.getMd5Code())) { + String md5Code = conn.getHeaderField(mTaskEntity.md5Key); + mEntity.setMd5Code(md5Code); + } + String disposition = conn.getHeaderField(mTaskEntity.dispositionKey); + if (!TextUtils.isEmpty(disposition)) { + mEntity.setDisposition(disposition); + if (disposition.contains(mTaskEntity.dispositionFileKey)) { + String[] infos = disposition.split("="); + mEntity.setServerFileName(URLDecoder.decode(infos[1], "utf-8")); + } + } + mTaskEntity.code = code; if (code == HttpURLConnection.HTTP_PARTIAL) { if (!checkLen(len)) return; @@ -107,9 +123,9 @@ class FileInfoThread implements Runnable { } else if (code == HttpURLConnection.HTTP_MOVED_TEMP || code == HttpURLConnection.HTTP_MOVED_PERM || code == HttpURLConnection.HTTP_SEE_OTHER) { - mTaskEntity.redirectUrlKey = conn.getHeaderField(mTaskEntity.redirectUrlKey); + mTaskEntity.redirectUrl = conn.getHeaderField(mTaskEntity.redirectUrlKey); mEntity.setRedirect(true); - mEntity.setRedirectUrl(mTaskEntity.redirectUrlKey); + mEntity.setRedirectUrl(mTaskEntity.redirectUrl); handle302Turn(conn); } else { failDownload("任务【" + mEntity.getDownloadUrl() + "】下载失败,错误码:" + code); @@ -143,6 +159,7 @@ class FileInfoThread implements Runnable { conn.setConnectTimeout(mConnectTimeOut); conn.connect(); handleConnect(conn); + conn.disconnect(); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java similarity index 69% rename from Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadUtil.java rename to Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java index 1555dc67..6759a047 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java @@ -16,20 +16,21 @@ package com.arialyy.aria.core.download.downloader; +import android.text.TextUtils; import android.util.Log; import com.arialyy.aria.core.download.DownloadTaskEntity; /** * Created by lyy on 2015/8/25. - * 下载工具类 + * 简单的下载工具 */ -public class DownloadUtil implements IDownloadUtil, Runnable { - private static final String TAG = "DownloadUtil"; +public class SimpleDownloadUtil implements IDownloadUtil, Runnable { + private static final String TAG = "SimpleDownloadUtil"; private IDownloadListener mListener; private Downloader mDT; private DownloadTaskEntity mTaskEntity; - public DownloadUtil(DownloadTaskEntity entity, IDownloadListener downloadListener) { + public SimpleDownloadUtil(DownloadTaskEntity entity, IDownloadListener downloadListener) { mTaskEntity = entity; mListener = downloadListener; mDT = new Downloader(downloadListener, entity); @@ -86,14 +87,20 @@ public class DownloadUtil implements IDownloadUtil, Runnable { } @Override public void run() { - new Thread(new FileInfoThread(mTaskEntity, new FileInfoThread.OnFileInfoCallback() { - @Override public void onComplete(String url, int code) { - mDT.startDownload(); - } + if (TextUtils.isEmpty(mTaskEntity.redirectUrl)) { + new Thread(new FileInfoThread(mTaskEntity, new FileInfoThread.OnFileInfoCallback() { + @Override public void onComplete(String url, int code) { + mListener.onPostPre(mTaskEntity.getEntity().getFileSize()); + mDT.startDownload(); + } - @Override public void onFail(String url, String errorMsg) { - failDownload(errorMsg); - } - })).start(); + @Override public void onFail(String url, String errorMsg) { + failDownload(errorMsg); + } + })).start(); + } else { + mListener.onPostPre(mTaskEntity.getEntity().getFileSize()); + new Downloader(mListener, mTaskEntity).startDownload(); + } } } \ No newline at end of file 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 d69d1b43..7cab6a94 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 @@ -45,6 +45,11 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable * 文件大小 */ private long fileSize = 1; + /** + * 转换后的文件大小 + */ + private String convertFileSize = ""; + private int state = STATE_WAIT; /** * 当前下载进度 @@ -55,6 +60,14 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable */ private long completeTime; + public String getConvertFileSize() { + return convertFileSize; + } + + public void setConvertFileSize(String convertFileSize) { + this.convertFileSize = convertFileSize; + } + public int getFailNum() { return failNum; } @@ -132,6 +145,7 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable dest.writeInt(this.failNum); dest.writeString(this.str); dest.writeLong(this.fileSize); + dest.writeString(this.convertFileSize); dest.writeInt(this.state); dest.writeLong(this.currentProgress); dest.writeLong(this.completeTime); @@ -143,6 +157,7 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable this.failNum = in.readInt(); this.str = in.readString(); this.fileSize = in.readLong(); + this.convertFileSize = in.readString(); this.state = in.readInt(); this.currentProgress = in.readLong(); this.completeTime = in.readLong(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java index 1d96daba..5356dcb7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java @@ -16,6 +16,7 @@ package com.arialyy.aria.core.inf; import android.support.annotation.NonNull; +import android.text.TextUtils; import android.util.Log; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.RequestEnum; @@ -178,4 +179,30 @@ public abstract class AbsTarget 0) { + tempUrl = url.substring(0, end); + int tempEnd = tempUrl.lastIndexOf("/"); + if (tempEnd > 0) { + fileName = tempUrl.substring(tempEnd + 1, tempUrl.length()); + } + } else { + int tempEnd = url.lastIndexOf("/"); + if (tempEnd > 0) { + fileName = url.substring(tempEnd + 1, url.length()); + } + } + if (TextUtils.isEmpty(fileName)) { + fileName = CommonUtil.keyToHashKey(url); + } + return fileName; + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java index ba341fdc..7829e670 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java @@ -42,11 +42,31 @@ public abstract class AbsTaskEntity extends DbEntity { */ public RequestEnum requestEnum = RequestEnum.GET; + /** + * 从链接中含有的文件md5码信息所需要的key + */ + public String md5Key = "Content-MD5"; + + /** + * 从链接中获取文件描述信息所需要的key + */ + public String dispositionKey = "Content-Disposition"; + /** * 重定向后,从链接中获取新url所需要的key */ public String redirectUrlKey = "location"; + /** + * 从Disposition获取的文件名说需要的key + */ + public String dispositionFileKey = "attachment;filename"; + + /** + * 重定向链接 + */ + public String redirectUrl = ""; + /** * 用于判断删除任务时是否需要删除文件{@code true}删除 */ diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java b/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java index 3e09d360..6d943536 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java @@ -32,7 +32,7 @@ import java.util.Map; public class DBConfig { static Map mapping = new HashMap<>(); static String DB_NAME; - static int VERSION = 7; + static int VERSION = 8; static { if (TextUtils.isEmpty(DB_NAME)) { diff --git a/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java b/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java index 988359c8..4d87cb04 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java @@ -533,9 +533,9 @@ final class SqlHelper extends SQLiteOpenHelper { * @param type {@link DbUtil} */ static void print(int type, String sql) { - //if (true) { - // return; - //} + if (true) { + return; + } String str = ""; switch (type) { case CREATE_TABLE: 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 97edabe7..5335a519 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -357,6 +357,10 @@ public class CommonUtil { List fields = new ArrayList<>(); Class personClazz = clazz.getSuperclass(); if (personClazz != null) { + Class rootClazz = personClazz.getSuperclass(); + if (rootClazz != null) { + Collections.addAll(fields, rootClazz.getDeclaredFields()); + } Collections.addAll(fields, personClazz.getDeclaredFields()); } Collections.addAll(fields, clazz.getDeclaredFields()); diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/AriaProcessor.java b/AriaCompiler/src/main/java/com/arialyy/compiler/AriaProcessor.java index 9203afa8..814c6945 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/AriaProcessor.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/AriaProcessor.java @@ -32,12 +32,12 @@ import javax.lang.model.element.TypeElement; * 事件注解扫描器 */ @AutoService(Processor.class) public class AriaProcessor extends AbstractProcessor { - ElementHandle mHandler; + ElementHandler mHandler; @Override public synchronized void init(ProcessingEnvironment processingEnv) { super.init(processingEnv); PrintLog.init(processingEnv.getMessager()); - mHandler = new ElementHandle(processingEnv.getFiler(), processingEnv.getElementUtils()); + mHandler = new ElementHandler(processingEnv.getFiler(), processingEnv.getElementUtils()); } @Override public Set getSupportedAnnotationTypes() { diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java b/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandler.java similarity index 99% rename from AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java rename to AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandler.java index 88ef94a3..1f460e0c 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandler.java @@ -49,14 +49,14 @@ import javax.lang.model.util.Elements; * Created by lyy on 2017/6/6. * 元素处理 */ -class ElementHandle { +class ElementHandler { private Filer mFiler; private Elements mElementUtil; private Map mMethods = new HashMap<>(); private Map> mListenerClass = new HashMap<>(); - ElementHandle(Filer filer, Elements elements) { + ElementHandler(Filer filer, Elements elements) { mFiler = filer; mElementUtil = elements; } 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 06a1c425..d0284ad7 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -16,15 +16,10 @@ package com.arialyy.simple.download; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; -import android.support.v7.widget.Toolbar; -import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -36,18 +31,15 @@ import butterknife.Bind; import com.arialyy.annotations.Download; import com.arialyy.aria.core.download.DownloadTarget; import com.arialyy.aria.core.Aria; -import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.util.CommonUtil; -import com.arialyy.frame.util.FileUtil; import com.arialyy.frame.util.show.L; import com.arialyy.frame.util.show.T; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.databinding.ActivitySingleBinding; import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; -import java.io.File; public class SingleTaskActivity extends BaseActivity { public static final int DOWNLOAD_PRE = 0x01; @@ -65,7 +57,8 @@ public class SingleTaskActivity extends BaseActivity { //"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk"; //"http://down2.xiaoshuofuwuqi.com/d/file/filetxt/20170608/14/%BA%DA%CE%D7%CA%A6%E1%C8%C6%F0.txt"; //"http://tinghuaapp.oss-cn-shanghai.aliyuncs.com/20170612201739607815"; - "http://static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk"; + //"http://static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk"; + "https://d.pcs.baidu.com/file/bc7334aba5443c2596d905a0bcf9e734?fid=2852548966-250528-290956601240893&time=1499758796&rt=sh&sign=FDTAERVY-DCb740ccc5511e5e8fedcff06b081203-HO8uC%2FT83oxUXZdObsg3b54%2Bzv8%3D&expires=8h&chkv=1&chkbd=0&chkpc=et&dp-logid=4444968052010847094&dp-callid=0&r=463246632"; //"http://oqcpqqvuf.bkt.clouddn.com/ceshi.txt"; //"http://down8.androidgame-store.com/201706122321/97967927DD4E53D9905ECAA7874C8128/new/game1/19/45319/com.neuralprisma-2.5.2.174-2000174_1494784835.apk?f=web_1"; //不支持断点的链接 @@ -75,8 +68,6 @@ public class SingleTaskActivity extends BaseActivity { @Bind(R.id.start) Button mStart; @Bind(R.id.stop) Button mStop; @Bind(R.id.cancel) Button mCancel; - @Bind(R.id.size) TextView mSize; - @Bind(R.id.speed) TextView mSpeed; @Bind(R.id.speeds) RadioGroup mRg; private Handler mUpdateHandler = new Handler() { @@ -92,13 +83,13 @@ public class SingleTaskActivity extends BaseActivity { } else { mPb.setProgress((int) ((current * 100) / len)); } - mSpeed.setText(task.getConvertSpeed()); + getBinding().setSpeed(task.getConvertSpeed()); break; case DOWNLOAD_PRE: setBtState(false); break; case DOWNLOAD_START: - mSize.setText(CommonUtil.formatFileSize((Long) msg.obj)); + getBinding().setFileSize(CommonUtil.formatFileSize((Long) msg.obj)); break; case DOWNLOAD_FAILE: Toast.makeText(SingleTaskActivity.this, "下载失败", Toast.LENGTH_SHORT).show(); @@ -241,7 +232,7 @@ public class SingleTaskActivity extends BaseActivity { } else if (target.isDownloading()) { setBtState(false); } - mSize.setText(target.getConvertFileSize()); + getBinding().setFileSize(target.getConvertFileSize()); } public void onClick(View view) { diff --git a/app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java b/app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java index d78f82fe..8e34589b 100644 --- a/app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java +++ b/app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java @@ -38,25 +38,20 @@ import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; * Created by Aria.Lao on 2017/1/4. */ public class DownloadFragment extends AbsFragment { - @Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb; @Bind(R.id.start) Button mStart; @Bind(R.id.stop) Button mStop; @Bind(R.id.cancel) Button mCancel; - @Bind(R.id.size) TextView mSize; - @Bind(R.id.speed) TextView mSpeed; - private static final String DOWNLOAD_URL = - "http://static.ilongyuan.cn/rayark/RayarkFZ_2.0.7.apk"; + private static final String DOWNLOAD_URL = "http://static.ilongyuan.cn/rayark/RayarkFZ_2.0.7.apk"; @Override protected void init(Bundle savedInstanceState) { if (Aria.download(this).taskExists(DOWNLOAD_URL)) { DownloadTarget target = Aria.download(this).load(DOWNLOAD_URL); - int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize()); - mPb.setProgress(p); + getBinding().setProgress(target.getPercent()); } DownloadEntity entity = Aria.download(this).getDownloadEntity(DOWNLOAD_URL); if (entity != null) { - mSize.setText(CommonUtil.formatFileSize(entity.getFileSize())); + getBinding().setFileSize(CommonUtil.formatFileSize(entity.getFileSize())); int state = entity.getState(); setBtState(state != DownloadEntity.STATE_RUNNING); } else { @@ -83,30 +78,29 @@ public class DownloadFragment extends AbsFragment { } @Download.onTaskPre public void onTaskPre(DownloadTask task) { - mSize.setText(CommonUtil.formatFileSize(task.getFileSize())); + getBinding().setFileSize(task.getConvertFileSize()); setBtState(false); } @Download.onTaskStop public void onTaskStop(DownloadTask task) { setBtState(true); - mSpeed.setText("0.0kb/s"); + getBinding().setSpeed("0.0kb/s"); } @Download.onTaskCancel public void onTaskCancel(DownloadTask task) { setBtState(true); - mPb.setProgress(0); - mSpeed.setText("0.0kb/s"); + getBinding().setProgress(0); + getBinding().setSpeed("0.0kb/s"); } @Download.onTaskRunning public void onTaskRunning(DownloadTask task) { - long current = task.getCurrentProgress(); long len = task.getFileSize(); if (len == 0) { - mPb.setProgress(0); + getBinding().setProgress(0); } else { - mPb.setProgress((int) ((current * 100) / len)); + getBinding().setProgress(task.getPercent()); } - mSpeed.setText(task.getConvertSpeed()); + getBinding().setSpeed(task.getConvertSpeed()); } @Override protected void onDelayLoad() { 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 560d2c96..0a486f12 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 @@ -19,16 +19,18 @@ import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.Button; -import android.widget.TextView; import butterknife.Bind; import com.arialyy.annotations.DownloadGroup; import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.download.DownloadGroupTask; +import com.arialyy.aria.core.download.DownloadGroupTaskEntity; +import com.arialyy.aria.util.CommonUtil; import com.arialyy.frame.util.show.L; +import com.arialyy.frame.util.show.T; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.databinding.ActivityDownloadGroupBinding; -import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; import java.util.List; /** @@ -36,12 +38,9 @@ import java.util.List; */ public class DownloadGroupActivity extends BaseActivity { - @Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb; @Bind(R.id.start) Button mStart; @Bind(R.id.stop) Button mStop; @Bind(R.id.cancel) Button mCancel; - @Bind(R.id.size) TextView mSize; - @Bind(R.id.speed) TextView mSpeed; List mUrls; @Override protected void init(Bundle savedInstanceState) { @@ -49,6 +48,13 @@ public class DownloadGroupActivity extends BaseActivity " + task.getPercent()); + getBinding().setProgress(task.getPercent()); + getBinding().setSpeed(task.getConvertSpeed()); } @DownloadGroup.onTaskResume() void taskResume(DownloadGroupTask task) { @@ -95,10 +102,12 @@ public class DownloadGroupActivity extends BaseActivity getUrls() { List urls = new ArrayList<>(); - String[] str = getContext().getResources().getStringArray(R.array.group_urls); + String[] str = getContext().getResources().getStringArray(R.array.download_url); Collections.addAll(urls, str); return urls; } diff --git a/app/src/main/res/layout/activity_download_group.xml b/app/src/main/res/layout/activity_download_group.xml index f7877e92..c5cbdcbf 100644 --- a/app/src/main/res/layout/activity_download_group.xml +++ b/app/src/main/res/layout/activity_download_group.xml @@ -1,5 +1,21 @@ - + + + + + + - + + android:layout_height="match_parent" + /> diff --git a/app/src/main/res/layout/activity_single.xml b/app/src/main/res/layout/activity_single.xml index c2552d66..35c0b299 100644 --- a/app/src/main/res/layout/activity_single.xml +++ b/app/src/main/res/layout/activity_single.xml @@ -1,5 +1,22 @@ - + + + + + + + - + - - - - - - - - + - -