任务组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. 61
      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. 15
      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. 30
      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.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<DownloadEntity> CREATOR = new Creator<DownloadEntity>() {

@ -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<DownloadGroupTarget, DownloadGroupEntity, DownloadGroupTaskEntity> {
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++) {
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()
// }
//}
}

@ -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<DownloadGroupTaskEntity, DownloadGroupEntity> {
private final String TAG = "DownloadGroupTask";
private DListener mListener;
private IDownloadUtil mUtil;
@ -55,7 +57,7 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, Dow
}
@Override public void stop() {
mUtil.startDownload();
mUtil.stopDownload();
}
@Override public void cancel() {
@ -66,6 +68,7 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, Dow
* 下载监听类
*/
private static class DListener extends DownloadListener {
private final String TAG = "DListener";
WeakReference<Handler> outHandler;
WeakReference<DownloadGroupTask> wTask;
Context context;
@ -92,6 +95,7 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, Dow
@Override public void onPostPre(long fileSize) {
entity.setFileSize(fileSize);
entity.setConvertFileSize(CommonUtil.formatFileSize(fileSize));
saveData(IEntity.STATE_POST_PRE, -1);
sendInState2Target(ISchedulers.POST_PRE);
}
@ -107,14 +111,15 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, Dow
}
@Override public void onProgress(long currentLocation) {
entity.setCurrentProgress(currentLocation);
long speed = currentLocation - lastLen;
if (isFirst) {
speed = 0;
isFirst = false;
}
handleSpeed(speed);
lastLen = currentLocation;
sendInState2Target(ISchedulers.RUNNING);
lastLen = currentLocation;
}
@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.ISchedulerListener;
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.CommonUtil;
import java.lang.reflect.InvocationTargetException;
@ -136,7 +138,24 @@ public class DownloadReceiver extends AbsReceiver {
*/
public DownloadEntity getDownloadEntity(String 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);
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;
}

@ -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<DownloadEntity> {
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<DownloadEntity> {
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<DownloadEntity> {
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<DownloadEntity> {
@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<DownloadEntity> {
entity.setDownloadComplete(state == IEntity.STATE_COMPLETE);
entity.setCurrentProgress(location);
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.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<FileInfoThread.OnFileInfoCallback> 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++;
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) {

@ -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();
}
/**

@ -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,8 +87,10 @@ public class DownloadUtil implements IDownloadUtil, Runnable {
}
@Override public void run() {
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();
}
@ -95,5 +98,9 @@ public class DownloadUtil implements IDownloadUtil, Runnable {
failDownload(errorMsg);
}
})).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 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();

@ -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<TARGET extends AbsTarget, ENTITY extends AbsEnti
.setCmd(CommonUtil.createCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_CANCEL))
.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;
/**
* 从链接中含有的文件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}删除
*/

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

@ -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:

@ -357,6 +357,10 @@ public class CommonUtil {
List<Field> 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());

@ -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<String> getSupportedAnnotationTypes() {

@ -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<String, ProxyMethodParam> mMethods = new HashMap<>();
private Map<String, Set<String>> mListenerClass = new HashMap<>();
ElementHandle(Filer filer, Elements elements) {
ElementHandler(Filer filer, Elements elements) {
mFiler = filer;
mElementUtil = elements;
}

@ -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<ActivitySingleBinding> {
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://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<ActivitySingleBinding> {
@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<ActivitySingleBinding> {
} 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<ActivitySingleBinding> {
} else if (target.isDownloading()) {
setBtState(false);
}
mSize.setText(target.getConvertFileSize());
getBinding().setFileSize(target.getConvertFileSize());
}
public void onClick(View view) {

@ -38,25 +38,20 @@ import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
* Created by Aria.Lao on 2017/1/4.
*/
public class DownloadFragment extends AbsFragment<FragmentDownloadBinding> {
@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<FragmentDownloadBinding> {
}
@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() {

@ -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<ActivityDownloadGroupBinding> {
@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<String> mUrls;
@Override protected void init(Bundle savedInstanceState) {
@ -49,6 +48,13 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
Aria.download(this).register();
setTitle("任务组");
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() {
@ -58,17 +64,16 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
public void onClick(View view) {
switch (view.getId()) {
case R.id.start:
//String text = ((TextView) view).getText().toString();
Aria.download(this)
.load(mUrls)
.setDownloadDirPath(Environment.getExternalStorageDirectory().getPath() + "/group_test")
.start();
break;
case R.id.stop:
//Aria.download(this).load(DOWNLOAD_URL).pause();
Aria.download(this).load(mUrls).stop();
break;
case R.id.cancel:
//Aria.download(this).load(DOWNLOAD_URL).cancel();
Aria.download(this).load(mUrls).cancel();
break;
}
}
@ -79,6 +84,7 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
@DownloadGroup.onTaskPre() protected void onTaskPre(DownloadGroupTask task) {
L.d(TAG, "group task pre");
getBinding().setFileSize(task.getConvertFileSize());
}
@DownloadGroup.onTaskStart() void taskStart(DownloadGroupTask task) {
@ -86,7 +92,8 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
}
@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) {
@ -95,10 +102,12 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
@DownloadGroup.onTaskStop() void taskStop(DownloadGroupTask task) {
L.d(TAG, "group task stop");
getBinding().setSpeed("");
}
@DownloadGroup.onTaskCancel() void taskCancel(DownloadGroupTask task) {
L.d(TAG, "group task cancel");
getBinding().setSpeed("");
getBinding().setProgress(0);
}
@DownloadGroup.onTaskFail() void taskFail(DownloadGroupTask task) {
@ -106,6 +115,6 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
}
@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> 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;
}

@ -1,5 +1,21 @@
<?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
android:layout_width="match_parent"
@ -9,10 +25,16 @@
<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
android:layout_width="match_parent"
android:layout_height="match_parent"/>
android:layout_height="match_parent"
/>
</LinearLayout>
</layout>

@ -1,5 +1,22 @@
<?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
xmlns:tools="http://schemas.android.com/tools"
@ -18,7 +35,12 @@
</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:id="@+id/fab"

@ -1,7 +1,25 @@
<?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:tools="http://schemas.android.com/tools"
>
<data>
<variable
name="fileSize"
type="String"
/>
<variable
name="speed"
type="String"
/>
<variable
name="progress"
type="int"
/>
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
@ -19,6 +37,7 @@
android:layout_margin="16dp"
android:layout_toLeftOf="@+id/size"
android:max="100"
android:progress="@{progress}"
style="?android:attr/progressBarStyleHorizontal"
/>
@ -29,7 +48,7 @@
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/progressBar"
android:layout_marginRight="16dp"
android:text="0mb"
android:text="@{fileSize}"
android:textSize="16sp"
/>
@ -46,7 +65,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="0kb/s"
android:text="@{speed}"
android:textColor="@color/black"
/>
@ -89,12 +108,12 @@
/>
<RadioGroup
android:visibility="gone"
android:id="@+id/speeds"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/speed_hint"
android:orientation="horizontal"
android:visibility="gone"
>
<RadioButton
@ -132,4 +151,5 @@
</RadioGroup>
</RelativeLayout>
</RelativeLayout>
</layout>

@ -1,12 +1,35 @@
<?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
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="@layout/dialog_download"/>
<include
layout="@layout/dialog_download"
bind:fileSize="@{fileSize}"
bind:progress="@{progress}"
bind:speed="@{speed}"
/>
</LinearLayout>
</layout>

@ -46,7 +46,7 @@
<string-array name="group_urls">
<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></item>-->
<!--<item></item>-->

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

Loading…
Cancel
Save