任务组bug fix

pull/330/head
AriaLyy 7 years ago
parent bc48fd5b4d
commit 375eb25499
  1. 98
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java
  2. 15
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java
  3. 9
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java
  4. 21
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
  5. 8
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java
  6. 10
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java
  7. 65
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java
  8. 21
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/FileInfoThread.java
  9. 31
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java
  10. 15
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsEntity.java
  11. 27
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java
  12. 20
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java
  13. 2
      Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java
  14. 6
      Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java
  15. 4
      Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
  16. 4
      AriaCompiler/src/main/java/com/arialyy/compiler/AriaProcessor.java
  17. 4
      AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandler.java
  18. 19
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  19. 26
      app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java
  20. 31
      app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java
  21. 2
      app/src/main/java/com/arialyy/simple/download/group/GroupModule.java
  22. 28
      app/src/main/res/layout/activity_download_group.xml
  23. 26
      app/src/main/res/layout/activity_single.xml
  24. 238
      app/src/main/res/layout/content_single.xml
  25. 27
      app/src/main/res/layout/fragment_download.xml
  26. 2
      app/src/main/res/values/strings.xml
  27. 4
      aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java

@ -19,6 +19,7 @@ package com.arialyy.aria.core.download;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import com.arialyy.aria.core.inf.AbsNormalEntity; import com.arialyy.aria.core.inf.AbsNormalEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.orm.Primary; import com.arialyy.aria.orm.Primary;
/** /**
@ -37,9 +38,79 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable {
*/ */
private String groupName = ""; private String groupName = "";
/**
* 通过{@link AbsTaskEntity#md5Key}从服务器的返回信息中获取的文件md5信息如果服务器没有返回则不会设置该信息
* 如果你已经设置了该任务的MD5信息Aria也不会从服务器返回的信息中获取该信息
*/
private String md5Code = "";
/**
* 通过{@link AbsTaskEntity#dispositionKey}从服务器的返回信息中获取的文件描述信息
*/
private String disposition = "";
/**
* 从disposition获取到的文件名如果可以获取到则会赋值到这个字段
*/
private String serverFileName = "";
public DownloadEntity() { 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() { public String getGroupName() {
return groupName; return groupName;
} }
@ -106,6 +177,9 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable {
dest.writeByte(this.isRedirect ? (byte) 1 : (byte) 0); dest.writeByte(this.isRedirect ? (byte) 1 : (byte) 0);
dest.writeString(this.redirectUrl); dest.writeString(this.redirectUrl);
dest.writeString(this.groupName); dest.writeString(this.groupName);
dest.writeString(this.md5Code);
dest.writeString(this.disposition);
dest.writeString(this.serverFileName);
} }
protected DownloadEntity(Parcel in) { protected DownloadEntity(Parcel in) {
@ -116,27 +190,9 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable {
this.isRedirect = in.readByte() != 0; this.isRedirect = in.readByte() != 0;
this.redirectUrl = in.readString(); this.redirectUrl = in.readString();
this.groupName = in.readString(); this.groupName = in.readString();
} this.md5Code = in.readString();
this.disposition = in.readString();
@Override public String toString() { this.serverFileName = in.readString();
return "DownloadEntity{"
+ "downloadUrl='"
+ downloadUrl
+ '\''
+ ", downloadPath='"
+ downloadPath
+ '\''
+ ", isDownloadComplete="
+ isDownloadComplete
+ ", isRedirect="
+ isRedirect
+ ", redirectUrl='"
+ redirectUrl
+ '\''
+ ", groupName='"
+ groupName
+ '\''
+ '}';
} }
public static final Creator<DownloadEntity> CREATOR = new Creator<DownloadEntity>() { public static final Creator<DownloadEntity> CREATOR = new Creator<DownloadEntity>() {

@ -16,6 +16,7 @@
package com.arialyy.aria.core.download; package com.arialyy.aria.core.download;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.inf.AbsGroupTarget; import com.arialyy.aria.core.inf.AbsGroupTarget;
import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
@ -29,6 +30,7 @@ import java.util.List;
public class DownloadGroupTarget public class DownloadGroupTarget
extends AbsGroupTarget<DownloadGroupTarget, DownloadGroupEntity, DownloadGroupTaskEntity> { extends AbsGroupTarget<DownloadGroupTarget, DownloadGroupEntity, DownloadGroupTaskEntity> {
private List<String> mUrls = new ArrayList<>(); private List<String> 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++) { for (int i = 0, len = mUrls.size(); i < len; i++) {
DownloadEntity entity = new DownloadEntity(); DownloadEntity entity = new DownloadEntity();
entity.setDownloadUrl(mUrls.get(i)); entity.setDownloadUrl(mUrls.get(i));
String fileName = mSubTaskFileName.isEmpty() ? CommonUtil.keyToHashKey(mUrls.get(i)) String fileName = mSubTaskFileName.isEmpty() ? createFileName(entity.getDownloadUrl())
: mSubTaskFileName.get(i); : mSubTaskFileName.get(i);
entity.setDownloadPath(mEntity.getDirPath() + "/" + fileName); entity.setDownloadPath(mEntity.getDirPath() + "/" + fileName);
entity.setGroupName(mGroupName); entity.setGroupName(mGroupName);
@ -170,14 +172,5 @@ public class DownloadGroupTarget
return list; return list;
} }
///**
// * 创建文件名,如果url链接有后缀名,则使用url中的后缀名
// * @return url 的 hashKey
// */
//private String createFileName(String url){
// if (url.contains(".")){
// int s = url.lastIndexOf(".");
// String fileName = url.substring()
// }
//}
} }

@ -17,6 +17,7 @@ package com.arialyy.aria.core.download;
import android.content.Context; import android.content.Context;
import android.os.Handler; import android.os.Handler;
import android.util.Log;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.downloader.DownloadGroupUtil; import com.arialyy.aria.core.download.downloader.DownloadGroupUtil;
import com.arialyy.aria.core.download.downloader.DownloadListener; import com.arialyy.aria.core.download.downloader.DownloadListener;
@ -34,6 +35,7 @@ import java.lang.ref.WeakReference;
* 任务组任务 * 任务组任务
*/ */
public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, DownloadGroupEntity> { public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, DownloadGroupEntity> {
private final String TAG = "DownloadGroupTask";
private DListener mListener; private DListener mListener;
private IDownloadUtil mUtil; private IDownloadUtil mUtil;
@ -55,7 +57,7 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, Dow
} }
@Override public void stop() { @Override public void stop() {
mUtil.startDownload(); mUtil.stopDownload();
} }
@Override public void cancel() { @Override public void cancel() {
@ -66,6 +68,7 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, Dow
* 下载监听类 * 下载监听类
*/ */
private static class DListener extends DownloadListener { private static class DListener extends DownloadListener {
private final String TAG = "DListener";
WeakReference<Handler> outHandler; WeakReference<Handler> outHandler;
WeakReference<DownloadGroupTask> wTask; WeakReference<DownloadGroupTask> wTask;
Context context; Context context;
@ -92,6 +95,7 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, Dow
@Override public void onPostPre(long fileSize) { @Override public void onPostPre(long fileSize) {
entity.setFileSize(fileSize); entity.setFileSize(fileSize);
entity.setConvertFileSize(CommonUtil.formatFileSize(fileSize));
saveData(IEntity.STATE_POST_PRE, -1); saveData(IEntity.STATE_POST_PRE, -1);
sendInState2Target(ISchedulers.POST_PRE); sendInState2Target(ISchedulers.POST_PRE);
} }
@ -107,14 +111,15 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, Dow
} }
@Override public void onProgress(long currentLocation) { @Override public void onProgress(long currentLocation) {
entity.setCurrentProgress(currentLocation);
long speed = currentLocation - lastLen; long speed = currentLocation - lastLen;
if (isFirst) { if (isFirst) {
speed = 0; speed = 0;
isFirst = false; isFirst = false;
} }
handleSpeed(speed); handleSpeed(speed);
lastLen = currentLocation;
sendInState2Target(ISchedulers.RUNNING); sendInState2Target(ISchedulers.RUNNING);
lastLen = currentLocation;
} }
@Override public void onStop(long stopLocation) { @Override public void onStop(long stopLocation) {

@ -24,6 +24,8 @@ import com.arialyy.aria.core.scheduler.DownloadGroupSchedulers;
import com.arialyy.aria.core.scheduler.DownloadSchedulers; import com.arialyy.aria.core.scheduler.DownloadSchedulers;
import com.arialyy.aria.core.scheduler.ISchedulerListener; import com.arialyy.aria.core.scheduler.ISchedulerListener;
import com.arialyy.aria.core.upload.ProxyHelper; import com.arialyy.aria.core.upload.ProxyHelper;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.orm.DbUtil;
import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
@ -136,7 +138,24 @@ public class DownloadReceiver extends AbsReceiver {
*/ */
public DownloadEntity getDownloadEntity(String downloadUrl) { public DownloadEntity getDownloadEntity(String downloadUrl) {
CheckUtil.checkDownloadUrl(downloadUrl); CheckUtil.checkDownloadUrl(downloadUrl);
return DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl); return DbEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl);
}
/**
* 通过下载链接获取保存在数据库的下载任务实体
*/
public DownloadTaskEntity getDownloadTask(String downloadUrl) {
CheckUtil.checkDownloadUrl(downloadUrl);
return DbEntity.findFirst(DownloadTaskEntity.class, "key=?", downloadUrl);
}
/**
* 通过下载链接获取保存在数据库的下载任务组实体
*/
public DownloadGroupTaskEntity getDownlaodGroupTask(List<String> urls) {
CheckUtil.checkDownloadUrls(urls);
String hashCode = CommonUtil.getMd5Code(urls);
return DbEntity.findFirst(DownloadGroupTaskEntity.class, "key=?", hashCode);
} }
/** /**

@ -39,7 +39,9 @@ public class DownloadTarget
mTaskEntity = DbEntity.findFirst(DownloadTaskEntity.class, "key=?", url); mTaskEntity = DbEntity.findFirst(DownloadTaskEntity.class, "key=?", url);
if (mTaskEntity == null) { if (mTaskEntity == null) {
mTaskEntity = new DownloadTaskEntity(); mTaskEntity = new DownloadTaskEntity();
mTaskEntity.entity = new DownloadEntity(); mTaskEntity.key = url;
mTaskEntity.entity = getEntity(url);
mTaskEntity.save();
} }
if (mTaskEntity.entity == null) { if (mTaskEntity.entity == null) {
mTaskEntity.entity = getEntity(url); mTaskEntity.entity = getEntity(url);
@ -57,12 +59,14 @@ public class DownloadTarget
DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl); DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl);
if (entity == null) { if (entity == null) {
entity = new DownloadEntity(); entity = new DownloadEntity();
entity.setDownloadUrl(downloadUrl);
entity.setGroupChild(false);
entity.save();
} }
File file = new File(entity.getDownloadPath()); File file = new File(entity.getDownloadPath());
if (!file.exists()) { if (!file.exists()) {
entity.setState(IEntity.STATE_WAIT); entity.setState(IEntity.STATE_WAIT);
} }
entity.setDownloadUrl(downloadUrl);
return entity; return entity;
} }

@ -21,12 +21,11 @@ import android.os.Handler;
import android.util.Log; import android.util.Log;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.downloader.DownloadListener; 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.download.downloader.IDownloadListener;
import com.arialyy.aria.core.inf.AbsNormalTask; import com.arialyy.aria.core.inf.AbsNormalTask;
import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.scheduler.ISchedulers; import com.arialyy.aria.core.scheduler.ISchedulers;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import java.io.File; import java.io.File;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
@ -39,7 +38,7 @@ public class DownloadTask extends AbsNormalTask<DownloadEntity> {
public static final String TAG = "DownloadTask"; public static final String TAG = "DownloadTask";
private IDownloadListener mListener; private IDownloadListener mListener;
private DownloadUtil mUtil; private SimpleDownloadUtil mUtil;
private boolean isWait = false; private boolean isWait = false;
private DownloadTask(DownloadTaskEntity taskEntity, Handler outHandler) { private DownloadTask(DownloadTaskEntity taskEntity, Handler outHandler) {
@ -47,7 +46,7 @@ public class DownloadTask extends AbsNormalTask<DownloadEntity> {
mOutHandler = outHandler; mOutHandler = outHandler;
mContext = AriaManager.APP; mContext = AriaManager.APP;
mListener = new DListener(mContext, this, mOutHandler); 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<DownloadEntity> {
String targetName; String targetName;
public Builder(String targetName, DownloadTaskEntity taskEntity) { public Builder(String targetName, DownloadTaskEntity taskEntity) {
CheckUtil.checkTaskEntity(taskEntity);
this.targetName = targetName; this.targetName = targetName;
this.taskEntity = taskEntity; this.taskEntity = taskEntity;
} }
@ -226,6 +224,7 @@ public class DownloadTask extends AbsNormalTask<DownloadEntity> {
@Override public void onPostPre(long fileSize) { @Override public void onPostPre(long fileSize) {
entity.setFileSize(fileSize); entity.setFileSize(fileSize);
entity.setConvertFileSize(CommonUtil.formatFileSize(fileSize));
sendInState2Target(ISchedulers.POST_PRE); sendInState2Target(ISchedulers.POST_PRE);
saveData(IEntity.STATE_POST_PRE, -1); saveData(IEntity.STATE_POST_PRE, -1);
} }
@ -305,7 +304,6 @@ public class DownloadTask extends AbsNormalTask<DownloadEntity> {
entity.setDownloadComplete(state == IEntity.STATE_COMPLETE); entity.setDownloadComplete(state == IEntity.STATE_COMPLETE);
entity.setCurrentProgress(location); entity.setCurrentProgress(location);
entity.update(); entity.update();
} }
} }
} }

@ -21,6 +21,7 @@ import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CommonUtil;
import java.io.File; import java.io.File;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -40,7 +41,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
* 任务组所有任务总大小 * 任务组所有任务总大小
*/ */
private long mTotalSize = 0; private long mTotalSize = 0;
private long mCurrentProgress = 0; private long mCurrentLocation = 0;
private ExecutorService mInfoPool; private ExecutorService mInfoPool;
private ExecutorService mExePool; private ExecutorService mExePool;
private IDownloadListener mListener; private IDownloadListener mListener;
@ -54,7 +55,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
/** /**
* 初始化失败的任务数 * 初始化失败的任务数
*/ */
private int mFailNum = 0; private int mInitFailNum = 0;
/** /**
* 保存所有没有下载完成的任务key为下载地址 * 保存所有没有下载完成的任务key为下载地址
*/ */
@ -75,20 +76,28 @@ public class DownloadGroupUtil implements IDownloadUtil {
*/ */
private SparseArray<FileInfoThread.OnFileInfoCallback> mFileInfoCallbacks = new SparseArray<>(); private SparseArray<FileInfoThread.OnFileInfoCallback> mFileInfoCallbacks = new SparseArray<>();
//任务总数
private int mTaskNum = 0;
//已经完成的任务数
private int mCompleteNum = 0;
//失败的任务数
private int mFailNum = 0;
public DownloadGroupUtil(IDownloadListener listener, DownloadGroupTaskEntity taskEntity) { public DownloadGroupUtil(IDownloadListener listener, DownloadGroupTaskEntity taskEntity) {
mListener = listener; mListener = listener;
mTaskEntity = taskEntity; mTaskEntity = taskEntity;
mInfoPool = Executors.newCachedThreadPool(); mInfoPool = Executors.newCachedThreadPool();
mExePool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); mExePool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
mTaskNum = mTaskEntity.entity.getSubTask().size();
for (DownloadEntity entity : mTaskEntity.entity.getSubTask()) { for (DownloadEntity entity : mTaskEntity.entity.getSubTask()) {
File file = new File(entity.getDownloadPath()); File file = new File(entity.getDownloadPath());
if (entity.isDownloadComplete() && file.exists()) { if (entity.isDownloadComplete() && file.exists()) {
mTotalSize += entity.getFileSize(); mTotalSize += entity.getFileSize();
mCompleteNum++;
} else { } else {
mExeMap.put(entity.getDownloadUrl(), createDownloadTask(entity)); 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() { @Override public long getCurrentLocation() {
return mCurrentProgress; return mCurrentLocation;
} }
@Override public boolean isDownloading() { @Override public boolean isDownloading() {
@ -106,6 +115,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
@Override public void cancelDownload() { @Override public void cancelDownload() {
isRunning = false; isRunning = false;
mListener.onCancel();
closeTimer(); closeTimer();
if (!mInfoPool.isShutdown()) { if (!mInfoPool.isShutdown()) {
mInfoPool.shutdown(); mInfoPool.shutdown();
@ -125,6 +135,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
@Override public void stopDownload() { @Override public void stopDownload() {
isRunning = false; isRunning = false;
mListener.onStop(mCurrentLocation);
closeTimer(); closeTimer();
if (!mInfoPool.isShutdown()) { if (!mInfoPool.isShutdown()) {
mInfoPool.shutdown(); mInfoPool.shutdown();
@ -156,6 +167,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
@Override public void resumeDownload() { @Override public void resumeDownload() {
startDownload(); startDownload();
mListener.onResume(mCurrentLocation);
} }
/** /**
@ -175,7 +187,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
startChildDownload(te); startChildDownload(te);
} }
mInitNum++; mInitNum++;
if (mInitNum + mFailNum == mTaskEntity.getEntity().getSubTask().size()) { if (mInitNum + mInitFailNum == mTaskEntity.getEntity().getSubTask().size()) {
startRunningFlow(); startRunningFlow();
} }
} }
@ -186,12 +198,15 @@ public class DownloadGroupUtil implements IDownloadUtil {
mFailMap.put(url, te); mFailMap.put(url, te);
mFileInfoCallbacks.put(te.hashCode(), this); mFileInfoCallbacks.put(te.hashCode(), this);
} }
mFailNum++; mInitFailNum++;
failNum++; //404链接不重试下载
if (failNum < 10) { if (!errorMsg.contains("错误码:404")) {
mInfoPool.execute(createFileInfoThread(te)); if (failNum < 10) {
mInfoPool.execute(createFileInfoThread(te));
}
} }
if (mInitNum + mFailNum == mTaskEntity.getEntity().getSubTask().size()) { failNum++;
if (mInitNum + mInitFailNum == mTaskEntity.getEntity().getSubTask().size()) {
startRunningFlow(); startRunningFlow();
} }
} }
@ -213,12 +228,14 @@ public class DownloadGroupUtil implements IDownloadUtil {
*/ */
private void startRunningFlow() { private void startRunningFlow() {
mListener.onPostPre(mTotalSize); mListener.onPostPre(mTotalSize);
mListener.onStart(mCurrentProgress); mListener.onStart(mCurrentLocation);
closeTimer(); closeTimer();
mTimer = new Timer(true); mTimer = new Timer(true);
mTimer.schedule(new TimerTask() { mTimer.schedule(new TimerTask() {
@Override public void run() { @Override public void run() {
mListener.onProgress(mCurrentProgress); if (mCurrentLocation >= 0) {
mListener.onProgress(mCurrentLocation);
}
} }
}, 0, 1000); }, 0, 1000);
} }
@ -258,6 +275,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
DownloadTaskEntity taskEntity; DownloadTaskEntity taskEntity;
DownloadEntity entity; DownloadEntity entity;
long lastLen = 0;
ChildDownloadListener(DownloadTaskEntity entity) { ChildDownloadListener(DownloadTaskEntity entity) {
this.taskEntity = entity; this.taskEntity = entity;
@ -270,6 +288,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
@Override public void onPostPre(long fileSize) { @Override public void onPostPre(long fileSize) {
entity.setFileSize(fileSize); entity.setFileSize(fileSize);
entity.setConvertFileSize(CommonUtil.formatFileSize(fileSize));
saveData(IEntity.STATE_POST_PRE, -1); saveData(IEntity.STATE_POST_PRE, -1);
} }
@ -279,10 +298,12 @@ public class DownloadGroupUtil implements IDownloadUtil {
@Override public void onStart(long startLocation) { @Override public void onStart(long startLocation) {
saveData(IEntity.STATE_POST_PRE, IEntity.STATE_RUNNING); saveData(IEntity.STATE_POST_PRE, IEntity.STATE_RUNNING);
lastLen = startLocation;
} }
@Override public void onProgress(long currentLocation) { @Override public void onProgress(long currentLocation) {
mCurrentProgress += currentLocation; mCurrentLocation += (currentLocation - lastLen);
lastLen = currentLocation;
} }
@Override public void onStop(long stopLocation) { @Override public void onStop(long stopLocation) {
@ -295,11 +316,27 @@ public class DownloadGroupUtil implements IDownloadUtil {
@Override public void onComplete() { @Override public void onComplete() {
saveData(IEntity.STATE_COMPLETE, entity.getFileSize()); saveData(IEntity.STATE_COMPLETE, entity.getFileSize());
mCompleteNum++;
if (mCompleteNum >= mTaskNum) {
mListener.onComplete();
closeTimer();
}
} }
@Override public void onFail() { @Override public void onFail() {
entity.setFailNum(entity.getFailNum() + 1); entity.setFailNum(entity.getFailNum() + 1);
saveData(IEntity.STATE_FAIL, -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) { private void saveData(int state, long location) {

@ -24,6 +24,9 @@ import com.arialyy.aria.util.CommonUtil;
import java.io.IOException; import java.io.IOException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; 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 len = conn.getContentLength();
int code = conn.getResponseCode(); int code = conn.getResponseCode();
boolean isComplete = false; 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; mTaskEntity.code = code;
if (code == HttpURLConnection.HTTP_PARTIAL) { if (code == HttpURLConnection.HTTP_PARTIAL) {
if (!checkLen(len)) return; if (!checkLen(len)) return;
@ -107,9 +123,9 @@ class FileInfoThread implements Runnable {
} else if (code == HttpURLConnection.HTTP_MOVED_TEMP } else if (code == HttpURLConnection.HTTP_MOVED_TEMP
|| code == HttpURLConnection.HTTP_MOVED_PERM || code == HttpURLConnection.HTTP_MOVED_PERM
|| code == HttpURLConnection.HTTP_SEE_OTHER) { || code == HttpURLConnection.HTTP_SEE_OTHER) {
mTaskEntity.redirectUrlKey = conn.getHeaderField(mTaskEntity.redirectUrlKey); mTaskEntity.redirectUrl = conn.getHeaderField(mTaskEntity.redirectUrlKey);
mEntity.setRedirect(true); mEntity.setRedirect(true);
mEntity.setRedirectUrl(mTaskEntity.redirectUrlKey); mEntity.setRedirectUrl(mTaskEntity.redirectUrl);
handle302Turn(conn); handle302Turn(conn);
} else { } else {
failDownload("任务【" + mEntity.getDownloadUrl() + "】下载失败,错误码:" + code); failDownload("任务【" + mEntity.getDownloadUrl() + "】下载失败,错误码:" + code);
@ -143,6 +159,7 @@ class FileInfoThread implements Runnable {
conn.setConnectTimeout(mConnectTimeOut); conn.setConnectTimeout(mConnectTimeOut);
conn.connect(); conn.connect();
handleConnect(conn); handleConnect(conn);
conn.disconnect();
} }
/** /**

@ -16,20 +16,21 @@
package com.arialyy.aria.core.download.downloader; package com.arialyy.aria.core.download.downloader;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity;
/** /**
* Created by lyy on 2015/8/25. * Created by lyy on 2015/8/25.
* 下载工具 * 简单的下载工具
*/ */
public class DownloadUtil implements IDownloadUtil, Runnable { public class SimpleDownloadUtil implements IDownloadUtil, Runnable {
private static final String TAG = "DownloadUtil"; private static final String TAG = "SimpleDownloadUtil";
private IDownloadListener mListener; private IDownloadListener mListener;
private Downloader mDT; private Downloader mDT;
private DownloadTaskEntity mTaskEntity; private DownloadTaskEntity mTaskEntity;
public DownloadUtil(DownloadTaskEntity entity, IDownloadListener downloadListener) { public SimpleDownloadUtil(DownloadTaskEntity entity, IDownloadListener downloadListener) {
mTaskEntity = entity; mTaskEntity = entity;
mListener = downloadListener; mListener = downloadListener;
mDT = new Downloader(downloadListener, entity); mDT = new Downloader(downloadListener, entity);
@ -86,14 +87,20 @@ public class DownloadUtil implements IDownloadUtil, Runnable {
} }
@Override public void run() { @Override public void run() {
new Thread(new FileInfoThread(mTaskEntity, new FileInfoThread.OnFileInfoCallback() { if (TextUtils.isEmpty(mTaskEntity.redirectUrl)) {
@Override public void onComplete(String url, int code) { new Thread(new FileInfoThread(mTaskEntity, new FileInfoThread.OnFileInfoCallback() {
mDT.startDownload(); @Override public void onComplete(String url, int code) {
} mListener.onPostPre(mTaskEntity.getEntity().getFileSize());
mDT.startDownload();
}
@Override public void onFail(String url, String errorMsg) { @Override public void onFail(String url, String errorMsg) {
failDownload(errorMsg); failDownload(errorMsg);
} }
})).start(); })).start();
} else {
mListener.onPostPre(mTaskEntity.getEntity().getFileSize());
new Downloader(mListener, mTaskEntity).startDownload();
}
} }
} }

@ -45,6 +45,11 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
* 文件大小 * 文件大小
*/ */
private long fileSize = 1; private long fileSize = 1;
/**
* 转换后的文件大小
*/
private String convertFileSize = "";
private int state = STATE_WAIT; private int state = STATE_WAIT;
/** /**
* 当前下载进度 * 当前下载进度
@ -55,6 +60,14 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
*/ */
private long completeTime; private long completeTime;
public String getConvertFileSize() {
return convertFileSize;
}
public void setConvertFileSize(String convertFileSize) {
this.convertFileSize = convertFileSize;
}
public int getFailNum() { public int getFailNum() {
return failNum; return failNum;
} }
@ -132,6 +145,7 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
dest.writeInt(this.failNum); dest.writeInt(this.failNum);
dest.writeString(this.str); dest.writeString(this.str);
dest.writeLong(this.fileSize); dest.writeLong(this.fileSize);
dest.writeString(this.convertFileSize);
dest.writeInt(this.state); dest.writeInt(this.state);
dest.writeLong(this.currentProgress); dest.writeLong(this.currentProgress);
dest.writeLong(this.completeTime); dest.writeLong(this.completeTime);
@ -143,6 +157,7 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
this.failNum = in.readInt(); this.failNum = in.readInt();
this.str = in.readString(); this.str = in.readString();
this.fileSize = in.readLong(); this.fileSize = in.readLong();
this.convertFileSize = in.readString();
this.state = in.readInt(); this.state = in.readInt();
this.currentProgress = in.readLong(); this.currentProgress = in.readLong();
this.completeTime = in.readLong(); this.completeTime = in.readLong();

@ -16,6 +16,7 @@
package com.arialyy.aria.core.inf; package com.arialyy.aria.core.inf;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.RequestEnum; import com.arialyy.aria.core.RequestEnum;
@ -178,4 +179,30 @@ public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEnti
.setCmd(CommonUtil.createCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_CANCEL)) .setCmd(CommonUtil.createCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_CANCEL))
.exe(); .exe();
} }
/**
* 创建文件名如果url链接有后缀名则使用url中的后缀名
*
* @return url hashKey
*/
protected String createFileName(String url) {
int end = url.indexOf("?");
String tempUrl, fileName = "";
if (end > 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;
}
} }

@ -42,11 +42,31 @@ public abstract class AbsTaskEntity<ENTITY extends AbsEntity> extends DbEntity {
*/ */
public RequestEnum requestEnum = RequestEnum.GET; public RequestEnum requestEnum = RequestEnum.GET;
/**
* 从链接中含有的文件md5码信息所需要的key
*/
public String md5Key = "Content-MD5";
/**
* 从链接中获取文件描述信息所需要的key
*/
public String dispositionKey = "Content-Disposition";
/** /**
* 重定向后从链接中获取新url所需要的key * 重定向后从链接中获取新url所需要的key
*/ */
public String redirectUrlKey = "location"; public String redirectUrlKey = "location";
/**
* 从Disposition获取的文件名说需要的key
*/
public String dispositionFileKey = "attachment;filename";
/**
* 重定向链接
*/
public String redirectUrl = "";
/** /**
* 用于判断删除任务时是否需要删除文件{@code true}删除 * 用于判断删除任务时是否需要删除文件{@code true}删除
*/ */

@ -32,7 +32,7 @@ import java.util.Map;
public class DBConfig { public class DBConfig {
static Map<String, Class> mapping = new HashMap<>(); static Map<String, Class> mapping = new HashMap<>();
static String DB_NAME; static String DB_NAME;
static int VERSION = 7; static int VERSION = 8;
static { static {
if (TextUtils.isEmpty(DB_NAME)) { if (TextUtils.isEmpty(DB_NAME)) {

@ -533,9 +533,9 @@ final class SqlHelper extends SQLiteOpenHelper {
* @param type {@link DbUtil} * @param type {@link DbUtil}
*/ */
static void print(int type, String sql) { static void print(int type, String sql) {
//if (true) { if (true) {
// return; return;
//} }
String str = ""; String str = "";
switch (type) { switch (type) {
case CREATE_TABLE: case CREATE_TABLE:

@ -357,6 +357,10 @@ public class CommonUtil {
List<Field> fields = new ArrayList<>(); List<Field> fields = new ArrayList<>();
Class personClazz = clazz.getSuperclass(); Class personClazz = clazz.getSuperclass();
if (personClazz != null) { if (personClazz != null) {
Class rootClazz = personClazz.getSuperclass();
if (rootClazz != null) {
Collections.addAll(fields, rootClazz.getDeclaredFields());
}
Collections.addAll(fields, personClazz.getDeclaredFields()); Collections.addAll(fields, personClazz.getDeclaredFields());
} }
Collections.addAll(fields, clazz.getDeclaredFields()); Collections.addAll(fields, clazz.getDeclaredFields());

@ -32,12 +32,12 @@ import javax.lang.model.element.TypeElement;
* 事件注解扫描器 * 事件注解扫描器
*/ */
@AutoService(Processor.class) public class AriaProcessor extends AbstractProcessor { @AutoService(Processor.class) public class AriaProcessor extends AbstractProcessor {
ElementHandle mHandler; ElementHandler mHandler;
@Override public synchronized void init(ProcessingEnvironment processingEnv) { @Override public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv); super.init(processingEnv);
PrintLog.init(processingEnv.getMessager()); PrintLog.init(processingEnv.getMessager());
mHandler = new ElementHandle(processingEnv.getFiler(), processingEnv.getElementUtils()); mHandler = new ElementHandler(processingEnv.getFiler(), processingEnv.getElementUtils());
} }
@Override public Set<String> getSupportedAnnotationTypes() { @Override public Set<String> getSupportedAnnotationTypes() {

@ -49,14 +49,14 @@ import javax.lang.model.util.Elements;
* Created by lyy on 2017/6/6. * Created by lyy on 2017/6/6.
* 元素处理 * 元素处理
*/ */
class ElementHandle { class ElementHandler {
private Filer mFiler; private Filer mFiler;
private Elements mElementUtil; private Elements mElementUtil;
private Map<String, ProxyMethodParam> mMethods = new HashMap<>(); private Map<String, ProxyMethodParam> mMethods = new HashMap<>();
private Map<String, Set<String>> mListenerClass = new HashMap<>(); private Map<String, Set<String>> mListenerClass = new HashMap<>();
ElementHandle(Filer filer, Elements elements) { ElementHandler(Filer filer, Elements elements) {
mFiler = filer; mFiler = filer;
mElementUtil = elements; mElementUtil = elements;
} }

@ -16,15 +16,10 @@
package com.arialyy.simple.download; package com.arialyy.simple.download;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -36,18 +31,15 @@ import butterknife.Bind;
import com.arialyy.annotations.Download; import com.arialyy.annotations.Download;
import com.arialyy.aria.core.download.DownloadTarget; import com.arialyy.aria.core.download.DownloadTarget;
import com.arialyy.aria.core.Aria; 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.download.DownloadTask;
import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.util.CommonUtil; 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.L;
import com.arialyy.frame.util.show.T; import com.arialyy.frame.util.show.T;
import com.arialyy.simple.R; import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.ActivitySingleBinding; import com.arialyy.simple.databinding.ActivitySingleBinding;
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
import java.io.File;
public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> { public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
public static final int DOWNLOAD_PRE = 0x01; public static final int DOWNLOAD_PRE = 0x01;
@ -65,7 +57,8 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
//"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk"; //"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://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://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://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"; //"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<ActivitySingleBinding> {
@Bind(R.id.start) Button mStart; @Bind(R.id.start) Button mStart;
@Bind(R.id.stop) Button mStop; @Bind(R.id.stop) Button mStop;
@Bind(R.id.cancel) Button mCancel; @Bind(R.id.cancel) Button mCancel;
@Bind(R.id.size) TextView mSize;
@Bind(R.id.speed) TextView mSpeed;
@Bind(R.id.speeds) RadioGroup mRg; @Bind(R.id.speeds) RadioGroup mRg;
private Handler mUpdateHandler = new Handler() { private Handler mUpdateHandler = new Handler() {
@ -92,13 +83,13 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
} else { } else {
mPb.setProgress((int) ((current * 100) / len)); mPb.setProgress((int) ((current * 100) / len));
} }
mSpeed.setText(task.getConvertSpeed()); getBinding().setSpeed(task.getConvertSpeed());
break; break;
case DOWNLOAD_PRE: case DOWNLOAD_PRE:
setBtState(false); setBtState(false);
break; break;
case DOWNLOAD_START: case DOWNLOAD_START:
mSize.setText(CommonUtil.formatFileSize((Long) msg.obj)); getBinding().setFileSize(CommonUtil.formatFileSize((Long) msg.obj));
break; break;
case DOWNLOAD_FAILE: case DOWNLOAD_FAILE:
Toast.makeText(SingleTaskActivity.this, "下载失败", Toast.LENGTH_SHORT).show(); Toast.makeText(SingleTaskActivity.this, "下载失败", Toast.LENGTH_SHORT).show();
@ -241,7 +232,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
} else if (target.isDownloading()) { } else if (target.isDownloading()) {
setBtState(false); setBtState(false);
} }
mSize.setText(target.getConvertFileSize()); getBinding().setFileSize(target.getConvertFileSize());
} }
public void onClick(View view) { public void onClick(View view) {

@ -38,25 +38,20 @@ import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
* Created by Aria.Lao on 2017/1/4. * Created by Aria.Lao on 2017/1/4.
*/ */
public class DownloadFragment extends AbsFragment<FragmentDownloadBinding> { public class DownloadFragment extends AbsFragment<FragmentDownloadBinding> {
@Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb;
@Bind(R.id.start) Button mStart; @Bind(R.id.start) Button mStart;
@Bind(R.id.stop) Button mStop; @Bind(R.id.stop) Button mStop;
@Bind(R.id.cancel) Button mCancel; @Bind(R.id.cancel) Button mCancel;
@Bind(R.id.size) TextView mSize;
@Bind(R.id.speed) TextView mSpeed;
private static final String DOWNLOAD_URL = private static final String DOWNLOAD_URL = "http://static.ilongyuan.cn/rayark/RayarkFZ_2.0.7.apk";
"http://static.ilongyuan.cn/rayark/RayarkFZ_2.0.7.apk";
@Override protected void init(Bundle savedInstanceState) { @Override protected void init(Bundle savedInstanceState) {
if (Aria.download(this).taskExists(DOWNLOAD_URL)) { if (Aria.download(this).taskExists(DOWNLOAD_URL)) {
DownloadTarget target = Aria.download(this).load(DOWNLOAD_URL); DownloadTarget target = Aria.download(this).load(DOWNLOAD_URL);
int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize()); getBinding().setProgress(target.getPercent());
mPb.setProgress(p);
} }
DownloadEntity entity = Aria.download(this).getDownloadEntity(DOWNLOAD_URL); DownloadEntity entity = Aria.download(this).getDownloadEntity(DOWNLOAD_URL);
if (entity != null) { if (entity != null) {
mSize.setText(CommonUtil.formatFileSize(entity.getFileSize())); getBinding().setFileSize(CommonUtil.formatFileSize(entity.getFileSize()));
int state = entity.getState(); int state = entity.getState();
setBtState(state != DownloadEntity.STATE_RUNNING); setBtState(state != DownloadEntity.STATE_RUNNING);
} else { } else {
@ -83,30 +78,29 @@ public class DownloadFragment extends AbsFragment<FragmentDownloadBinding> {
} }
@Download.onTaskPre public void onTaskPre(DownloadTask task) { @Download.onTaskPre public void onTaskPre(DownloadTask task) {
mSize.setText(CommonUtil.formatFileSize(task.getFileSize())); getBinding().setFileSize(task.getConvertFileSize());
setBtState(false); setBtState(false);
} }
@Download.onTaskStop public void onTaskStop(DownloadTask task) { @Download.onTaskStop public void onTaskStop(DownloadTask task) {
setBtState(true); setBtState(true);
mSpeed.setText("0.0kb/s"); getBinding().setSpeed("0.0kb/s");
} }
@Download.onTaskCancel public void onTaskCancel(DownloadTask task) { @Download.onTaskCancel public void onTaskCancel(DownloadTask task) {
setBtState(true); setBtState(true);
mPb.setProgress(0); getBinding().setProgress(0);
mSpeed.setText("0.0kb/s"); getBinding().setSpeed("0.0kb/s");
} }
@Download.onTaskRunning public void onTaskRunning(DownloadTask task) { @Download.onTaskRunning public void onTaskRunning(DownloadTask task) {
long current = task.getCurrentProgress();
long len = task.getFileSize(); long len = task.getFileSize();
if (len == 0) { if (len == 0) {
mPb.setProgress(0); getBinding().setProgress(0);
} else { } else {
mPb.setProgress((int) ((current * 100) / len)); getBinding().setProgress(task.getPercent());
} }
mSpeed.setText(task.getConvertSpeed()); getBinding().setSpeed(task.getConvertSpeed());
} }
@Override protected void onDelayLoad() { @Override protected void onDelayLoad() {

@ -19,16 +19,18 @@ import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.TextView;
import butterknife.Bind; import butterknife.Bind;
import com.arialyy.annotations.DownloadGroup; import com.arialyy.annotations.DownloadGroup;
import com.arialyy.aria.core.Aria; 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.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.L;
import com.arialyy.frame.util.show.T;
import com.arialyy.simple.R; import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.ActivityDownloadGroupBinding; import com.arialyy.simple.databinding.ActivityDownloadGroupBinding;
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
import java.util.List; import java.util.List;
/** /**
@ -36,12 +38,9 @@ import java.util.List;
*/ */
public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBinding> { public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBinding> {
@Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb;
@Bind(R.id.start) Button mStart; @Bind(R.id.start) Button mStart;
@Bind(R.id.stop) Button mStop; @Bind(R.id.stop) Button mStop;
@Bind(R.id.cancel) Button mCancel; @Bind(R.id.cancel) Button mCancel;
@Bind(R.id.size) TextView mSize;
@Bind(R.id.speed) TextView mSpeed;
List<String> mUrls; List<String> mUrls;
@Override protected void init(Bundle savedInstanceState) { @Override protected void init(Bundle savedInstanceState) {
@ -49,6 +48,13 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
Aria.download(this).register(); Aria.download(this).register();
setTitle("任务组"); setTitle("任务组");
mUrls = getModule(GroupModule.class).getUrls(); mUrls = getModule(GroupModule.class).getUrls();
DownloadGroupTaskEntity entity = Aria.download(this).getDownlaodGroupTask(mUrls);
if (entity != null) {
DownloadGroupEntity groupEntity = entity.getEntity();
getBinding().setFileSize(groupEntity.getConvertFileSize());
getBinding().setProgress(
(int) (groupEntity.getCurrentProgress() * 100 / groupEntity.getFileSize()));
}
} }
@Override protected int setLayoutId() { @Override protected int setLayoutId() {
@ -58,17 +64,16 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
public void onClick(View view) { public void onClick(View view) {
switch (view.getId()) { switch (view.getId()) {
case R.id.start: case R.id.start:
//String text = ((TextView) view).getText().toString();
Aria.download(this) Aria.download(this)
.load(mUrls) .load(mUrls)
.setDownloadDirPath(Environment.getExternalStorageDirectory().getPath() + "/group_test") .setDownloadDirPath(Environment.getExternalStorageDirectory().getPath() + "/group_test")
.start(); .start();
break; break;
case R.id.stop: case R.id.stop:
//Aria.download(this).load(DOWNLOAD_URL).pause(); Aria.download(this).load(mUrls).stop();
break; break;
case R.id.cancel: case R.id.cancel:
//Aria.download(this).load(DOWNLOAD_URL).cancel(); Aria.download(this).load(mUrls).cancel();
break; break;
} }
} }
@ -79,6 +84,7 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
@DownloadGroup.onTaskPre() protected void onTaskPre(DownloadGroupTask task) { @DownloadGroup.onTaskPre() protected void onTaskPre(DownloadGroupTask task) {
L.d(TAG, "group task pre"); L.d(TAG, "group task pre");
getBinding().setFileSize(task.getConvertFileSize());
} }
@DownloadGroup.onTaskStart() void taskStart(DownloadGroupTask task) { @DownloadGroup.onTaskStart() void taskStart(DownloadGroupTask task) {
@ -86,7 +92,8 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
} }
@DownloadGroup.onTaskRunning() protected void running(DownloadGroupTask task) { @DownloadGroup.onTaskRunning() protected void running(DownloadGroupTask task) {
L.d(TAG, "group task running ==> " + task.getPercent()); getBinding().setProgress(task.getPercent());
getBinding().setSpeed(task.getConvertSpeed());
} }
@DownloadGroup.onTaskResume() void taskResume(DownloadGroupTask task) { @DownloadGroup.onTaskResume() void taskResume(DownloadGroupTask task) {
@ -95,10 +102,12 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
@DownloadGroup.onTaskStop() void taskStop(DownloadGroupTask task) { @DownloadGroup.onTaskStop() void taskStop(DownloadGroupTask task) {
L.d(TAG, "group task stop"); L.d(TAG, "group task stop");
getBinding().setSpeed("");
} }
@DownloadGroup.onTaskCancel() void taskCancel(DownloadGroupTask task) { @DownloadGroup.onTaskCancel() void taskCancel(DownloadGroupTask task) {
L.d(TAG, "group task cancel"); getBinding().setSpeed("");
getBinding().setProgress(0);
} }
@DownloadGroup.onTaskFail() void taskFail(DownloadGroupTask task) { @DownloadGroup.onTaskFail() void taskFail(DownloadGroupTask task) {
@ -106,6 +115,6 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
} }
@DownloadGroup.onTaskComplete() void taskComplete(DownloadGroupTask task) { @DownloadGroup.onTaskComplete() void taskComplete(DownloadGroupTask task) {
L.d(TAG, "group task complete"); T.showShort(this, "任务组下载完成");
} }
} }

@ -32,7 +32,7 @@ public class GroupModule extends BaseModule {
List<String> getUrls() { List<String> getUrls() {
List<String> urls = new ArrayList<>(); List<String> 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); Collections.addAll(urls, str);
return urls; return urls;
} }

@ -1,5 +1,21 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"> <layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto"
>
<data>
<variable
name="fileSize"
type="String"
/>
<variable
name="speed"
type="String"
/>
<variable
name="progress"
type="int"
/>
</data>
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
@ -9,10 +25,16 @@
<include layout="@layout/layout_bar"/> <include layout="@layout/layout_bar"/>
<include layout="@layout/content_single"/> <include
layout="@layout/content_single"
bind:fileSize="@{fileSize}"
bind:progress="@{progress}"
bind:speed="@{speed}"
/>
<View <View
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"/> android:layout_height="match_parent"
/>
</LinearLayout> </LinearLayout>
</layout> </layout>

@ -1,5 +1,22 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"> <layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto"
>
<data>
<variable
name="fileSize"
type="String"
/>
<variable
name="speed"
type="String"
/>
<variable
name="progress"
type="int"
/>
</data>
<android.support.design.widget.CoordinatorLayout <android.support.design.widget.CoordinatorLayout
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
@ -18,7 +35,12 @@
</android.support.design.widget.AppBarLayout> </android.support.design.widget.AppBarLayout>
<include layout="@layout/content_single"/> <include
layout="@layout/content_single"
bind:fileSize="@{fileSize}"
bind:progress="@{progress}"
bind:speed="@{speed}"
/>
<android.support.design.widget.FloatingActionButton <android.support.design.widget.FloatingActionButton
android:id="@+id/fab" android:id="@+id/fab"

@ -1,135 +1,155 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.arialyy.simple.download.SingleTaskActivity"
tools:showIn="@layout/activity_single"
> >
<com.arialyy.simple.widget.HorizontalProgressBarWithNumber <data>
android:id="@+id/progressBar" <variable
android:layout_width="wrap_content" name="fileSize"
android:layout_height="20dp" type="String"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_margin="16dp"
android:layout_toLeftOf="@+id/size"
android:max="100"
style="?android:attr/progressBarStyleHorizontal"
/>
<TextView
android:id="@+id/size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/progressBar"
android:layout_marginRight="16dp"
android:text="0mb"
android:textSize="16sp"
/>
<LinearLayout
android:id="@+id/handle_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBar"
android:orientation="horizontal"
>
<TextView
android:id="@+id/speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="0kb/s"
android:textColor="@color/black"
/> />
<variable
<Button name="speed"
android:id="@+id/start" type="String"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="开始"
style="?buttonBarButtonStyle"
/> />
<variable
<Button name="progress"
android:id="@+id/stop" type="int"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="暂停"
style="?buttonBarButtonStyle"
/> />
</data>
<Button <RelativeLayout
android:id="@+id/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="删除任务"
style="?buttonBarButtonStyle"
/>
</LinearLayout>
<TextView
android:id="@+id/speed_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/handle_bar"
/>
<RadioGroup
android:visibility="gone"
android:id="@+id/speeds"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_below="@+id/speed_hint" app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:orientation="horizontal" tools:context="com.arialyy.simple.download.SingleTaskActivity"
tools:showIn="@layout/activity_single"
> >
<RadioButton <com.arialyy.simple.widget.HorizontalProgressBarWithNumber
android:id="@+id/progressBar"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="20dp"
android:layout_weight="1" android:layout_alignParentLeft="true"
android:text="256kb" android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_margin="16dp"
android:layout_toLeftOf="@+id/size"
android:max="100"
android:progress="@{progress}"
style="?android:attr/progressBarStyleHorizontal"
/> />
<RadioButton <TextView
android:id="@+id/size"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_alignParentRight="true"
android:text="512kb" android:layout_alignTop="@+id/progressBar"
android:layout_marginRight="16dp"
android:text="@{fileSize}"
android:textSize="16sp"
/> />
<RadioButton
android:layout_width="wrap_content"
<LinearLayout
android:id="@+id/handle_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_below="@+id/progressBar"
android:text="1mb" android:orientation="horizontal"
/> >
<RadioButton <TextView
android:id="@+id/speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="@{speed}"
android:textColor="@color/black"
/>
<Button
android:id="@+id/start"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="开始"
style="?buttonBarButtonStyle"
/>
<Button
android:id="@+id/stop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="暂停"
style="?buttonBarButtonStyle"
/>
<Button
android:id="@+id/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="删除任务"
style="?buttonBarButtonStyle"
/>
</LinearLayout>
<TextView
android:id="@+id/speed_hint"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_below="@+id/handle_bar"
android:text="2mb"
/> />
<RadioButton <RadioGroup
android:layout_width="wrap_content" android:id="@+id/speeds"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_below="@+id/speed_hint"
android:text="max" android:orientation="horizontal"
/> android:visibility="gone"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="256kb"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="512kb"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1mb"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2mb"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="max"
/>
</RadioGroup> </RadioGroup>
</RelativeLayout> </RelativeLayout>
</layout>

@ -1,12 +1,35 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"> <layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto"
>
<data>
<variable
name="fileSize"
type="String"
/>
<variable
name="speed"
type="String"
/>
<variable
name="progress"
type="int"
/>
</data>
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:orientation="vertical"
> >
<include layout="@layout/dialog_download"/> <include
layout="@layout/dialog_download"
bind:fileSize="@{fileSize}"
bind:progress="@{progress}"
bind:speed="@{speed}"
/>
</LinearLayout> </LinearLayout>
</layout> </layout>

@ -46,7 +46,7 @@
<string-array name="group_urls"> <string-array name="group_urls">
<item>http://img.sc115.com/uploads/allimg/110420/20110420225600154.jpg</item> <item>http://img.sc115.com/uploads/allimg/110420/20110420225600154.jpg</item>
<item>https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQv8s5M31tFNvbjtGgxqmx14eh2h2651gkbrx-kN0cNYPD0qvRi</item> <item>http://img05.tooopen.com/images/20160121/tooopen_sy_155168162826.jpg</item>
<item>http://img03.tooopen.com/images/20130811/tooopen_15265353.jpg</item> <item>http://img03.tooopen.com/images/20130811/tooopen_15265353.jpg</item>
<!--<item></item>--> <!--<item></item>-->
<!--<item></item>--> <!--<item></item>-->

@ -77,8 +77,7 @@ public class UploadTask extends AbsNormalTask<UploadEntity> {
} }
} }
private static class private static class UListener extends UploadListener {
UListener extends UploadListener {
WeakReference<Handler> outHandler; WeakReference<Handler> outHandler;
WeakReference<UploadTask> task; WeakReference<UploadTask> task;
long lastLen = 0; //上一次发送长度 long lastLen = 0; //上一次发送长度
@ -106,6 +105,7 @@ public class UploadTask extends AbsNormalTask<UploadEntity> {
@Override public void onPostPre(long fileSize) { @Override public void onPostPre(long fileSize) {
super.onPostPre(fileSize); super.onPostPre(fileSize);
entity.setFileSize(fileSize); entity.setFileSize(fileSize);
entity.setConvertFileSize(CommonUtil.formatFileSize(fileSize));
sendInState2Target(ISchedulers.POST_PRE); sendInState2Target(ISchedulers.POST_PRE);
saveData(IEntity.STATE_POST_PRE, 0); saveData(IEntity.STATE_POST_PRE, 0);
} }

Loading…
Cancel
Save