任务组bug修复

pull/330/head
AriaLyy 7 years ago
parent 375eb25499
commit 746e8752bd
  1. 17
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java
  2. 3
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java
  3. 11
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java
  4. 19
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java
  5. 4
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java
  6. 12
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java
  7. 52
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java
  8. 1
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java
  9. 2
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java
  10. 13
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsEntity.java
  11. 10
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTarget.java
  12. 3
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java
  13. 2
      Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java
  14. 10
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java
  15. 25
      Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
  16. 121
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  17. 7
      app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java
  18. 3
      app/src/main/java/com/arialyy/simple/download/group/GroupModule.java
  19. 2
      app/src/main/res/values/strings.xml

@ -27,9 +27,8 @@ import com.arialyy.aria.orm.Primary;
* 下载实体
*/
public class DownloadEntity extends AbsNormalEntity implements Parcelable {
@Primary private String downloadUrl = ""; //下载路径
private String downloadPath = ""; //保存路径
private boolean isDownloadComplete = false; //是否下载完成
private String downloadUrl = ""; //下载路径
@Primary private String downloadPath = ""; //保存路径
private boolean isRedirect = false; //是否重定向
private String redirectUrl = ""; //重定向链接
@ -65,8 +64,6 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable {
+ ", downloadPath='"
+ downloadPath
+ '\''
+ ", isDownloadComplete="
+ isDownloadComplete
+ ", isRedirect="
+ isRedirect
+ ", redirectUrl='"
@ -137,14 +134,6 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable {
return this;
}
public boolean isDownloadComplete() {
return isDownloadComplete;
}
public void setDownloadComplete(boolean downloadComplete) {
isDownloadComplete = downloadComplete;
}
@Override public DownloadEntity clone() throws CloneNotSupportedException {
return (DownloadEntity) super.clone();
}
@ -173,7 +162,6 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable {
super.writeToParcel(dest, flags);
dest.writeString(this.downloadUrl);
dest.writeString(this.downloadPath);
dest.writeByte(this.isDownloadComplete ? (byte) 1 : (byte) 0);
dest.writeByte(this.isRedirect ? (byte) 1 : (byte) 0);
dest.writeString(this.redirectUrl);
dest.writeString(this.groupName);
@ -186,7 +174,6 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable {
super(in);
this.downloadUrl = in.readString();
this.downloadPath = in.readString();
this.isDownloadComplete = in.readByte() != 0;
this.isRedirect = in.readByte() != 0;
this.redirectUrl = in.readString();
this.groupName = in.readString();

@ -16,7 +16,6 @@
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;
@ -171,6 +170,4 @@ public class DownloadGroupTarget
}
return list;
}
}

@ -17,7 +17,6 @@ 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;
@ -57,10 +56,20 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, Dow
}
@Override public void stop() {
if (!mUtil.isDownloading()) {
if (mOutHandler != null) {
mOutHandler.obtainMessage(ISchedulers.STOP, this).sendToTarget();
}
}
mUtil.stopDownload();
}
@Override public void cancel() {
if (!mUtil.isDownloading()) {
if (mOutHandler != null) {
mOutHandler.obtainMessage(ISchedulers.CANCEL, this).sendToTarget();
}
}
mUtil.cancelDownload();
}

@ -36,17 +36,21 @@ public class DownloadTarget
DownloadTarget(String url, String targetName) {
mTargetName = targetName;
mTaskEntity = DbEntity.findFirst(DownloadTaskEntity.class, "key=?", url);
DownloadEntity entity = DbEntity.findFirst(DownloadEntity.class, "downloadUrl=?", url);
if (entity == null) {
entity = getEntity(url);
}
mEntity = entity;
mTaskEntity = DbEntity.findFirst(DownloadTaskEntity.class, "key=?", entity.getDownloadPath());
if (mTaskEntity == null) {
mTaskEntity = new DownloadTaskEntity();
mTaskEntity.key = url;
mTaskEntity.entity = getEntity(url);
mTaskEntity.key = entity.getDownloadPath();
mTaskEntity.entity = entity;
mTaskEntity.save();
}
if (mTaskEntity.entity == null) {
mTaskEntity.entity = getEntity(url);
mTaskEntity.entity = mEntity;
}
mEntity = mTaskEntity.entity;
}
/**
@ -56,7 +60,8 @@ public class DownloadTarget
*/
private DownloadEntity getEntity(String downloadUrl) {
DownloadEntity entity =
DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl);
DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=? and isGroupChild='false'",
downloadUrl);
if (entity == null) {
entity = new DownloadEntity();
entity.setDownloadUrl(downloadUrl);
@ -108,6 +113,8 @@ public class DownloadTarget
File file = new File(downloadPath);
mEntity.setDownloadPath(downloadPath);
mEntity.setFileName(file.getName());
mTaskEntity.key = downloadPath;
mTaskEntity.update();
return this;
}

@ -147,7 +147,7 @@ public class DownloadTask extends AbsNormalTask<DownloadEntity> {
* 取消下载
*/
@Override public void cancel() {
if (!mEntity.isDownloadComplete()) {
if (!mEntity.isComplete()) {
if (!mUtil.isDownloading()) {
if (mOutHandler != null) {
mOutHandler.obtainMessage(ISchedulers.CANCEL, this).sendToTarget();
@ -301,7 +301,7 @@ public class DownloadTask extends AbsNormalTask<DownloadEntity> {
private void saveData(int state, long location) {
entity.setState(state);
entity.setDownloadComplete(state == IEntity.STATE_COMPLETE);
entity.setComplete(state == IEntity.STATE_COMPLETE);
entity.setCurrentProgress(location);
entity.update();
}

@ -24,7 +24,17 @@ import com.arialyy.aria.orm.OneToOne;
*/
public class DownloadTaskEntity extends AbsTaskEntity<DownloadEntity> {
@OneToOne(table = DownloadEntity.class, key = "downloadUrl") public DownloadEntity entity;
@OneToOne(table = DownloadEntity.class, key = "downloadPath") public DownloadEntity entity;
/**
* 所属的任务组组名如果不属于任务组则为null
*/
public String groupName = "";
/**
* 该任务是否属于任务组
*/
public boolean isGroupTask = false;
public DownloadTaskEntity() {
}

@ -24,6 +24,7 @@ import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CommonUtil;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
@ -82,6 +83,10 @@ public class DownloadGroupUtil implements IDownloadUtil {
private int mCompleteNum = 0;
//失败的任务数
private int mFailNum = 0;
/**
* 该任务组对应的所有任务
*/
private Map<String, DownloadTaskEntity> mTasksMap = new HashMap<>();
public DownloadGroupUtil(IDownloadListener listener, DownloadGroupTaskEntity taskEntity) {
mListener = listener;
@ -89,13 +94,20 @@ public class DownloadGroupUtil implements IDownloadUtil {
mInfoPool = Executors.newCachedThreadPool();
mExePool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
mTaskNum = mTaskEntity.entity.getSubTask().size();
List<DownloadTaskEntity> tasks =
DbEntity.findDatas(DownloadTaskEntity.class, "groupName=?", mTaskEntity.key);
if (tasks != null && !tasks.isEmpty()) {
for (DownloadTaskEntity te : tasks) {
mTasksMap.put(te.getEntity().getDownloadUrl(), te);
}
}
for (DownloadEntity entity : mTaskEntity.entity.getSubTask()) {
File file = new File(entity.getDownloadPath());
if (entity.isDownloadComplete() && file.exists()) {
if (entity.isComplete() && file.exists()) {
mTotalSize += entity.getFileSize();
mCompleteNum++;
} else {
mExeMap.put(entity.getDownloadUrl(), createDownloadTask(entity));
mExeMap.put(entity.getDownloadUrl(), createChildDownloadTask(entity));
}
mCurrentLocation += entity.getCurrentProgress();
}
@ -115,8 +127,8 @@ public class DownloadGroupUtil implements IDownloadUtil {
@Override public void cancelDownload() {
isRunning = false;
mListener.onCancel();
closeTimer();
mListener.onCancel();
if (!mInfoPool.isShutdown()) {
mInfoPool.shutdown();
}
@ -131,12 +143,26 @@ public class DownloadGroupUtil implements IDownloadUtil {
dt.cancelDownload();
}
}
delDownloadInfo();
mTaskEntity.deleteData();
}
/**
* 删除所有子任务的下载信息
*/
private void delDownloadInfo() {
List<DownloadTaskEntity> tasks =
DbEntity.findDatas(DownloadTaskEntity.class, "groupName=?", mTaskEntity.key);
if (tasks == null || tasks.isEmpty()) return;
for (DownloadTaskEntity taskEntity : tasks) {
CommonUtil.delDownloadTaskConfig(taskEntity.removeFile, taskEntity);
}
}
@Override public void stopDownload() {
isRunning = false;
mListener.onStop(mCurrentLocation);
closeTimer();
mListener.onStop(mCurrentLocation);
if (!mInfoPool.isShutdown()) {
mInfoPool.shutdown();
}
@ -227,9 +253,9 @@ public class DownloadGroupUtil implements IDownloadUtil {
* 开始进度流程
*/
private void startRunningFlow() {
closeTimer();
mListener.onPostPre(mTotalSize);
mListener.onStart(mCurrentLocation);
closeTimer();
mTimer = new Timer(true);
mTimer.schedule(new TimerTask() {
@Override public void run() {
@ -247,15 +273,15 @@ public class DownloadGroupUtil implements IDownloadUtil {
ChildDownloadListener listener = new ChildDownloadListener(taskEntity);
Downloader dt = new Downloader(listener, taskEntity);
mDownloaderMap.put(taskEntity.getEntity().getDownloadUrl(), dt);
if (mExePool.isShutdown()) return;
mExePool.execute(dt);
}
/**
* 创建子任务下载信息
*/
private DownloadTaskEntity createDownloadTask(DownloadEntity entity) {
DownloadTaskEntity taskEntity =
DbEntity.findFirst(DownloadTaskEntity.class, "key=?", entity.getDownloadUrl());
private DownloadTaskEntity createChildDownloadTask(DownloadEntity entity) {
DownloadTaskEntity taskEntity = mTasksMap.get(entity.getDownloadUrl());
if (taskEntity != null) {
return taskEntity;
}
@ -265,6 +291,10 @@ public class DownloadGroupUtil implements IDownloadUtil {
taskEntity.requestEnum = mTaskEntity.requestEnum;
taskEntity.redirectUrlKey = mTaskEntity.redirectUrlKey;
taskEntity.removeFile = mTaskEntity.removeFile;
taskEntity.groupName = mTaskEntity.key;
taskEntity.isGroupTask = true;
taskEntity.key = entity.getDownloadPath();
taskEntity.save();
return taskEntity;
}
@ -275,6 +305,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
DownloadTaskEntity taskEntity;
DownloadEntity entity;
long lastLen = 0;
ChildDownloadListener(DownloadTaskEntity entity) {
@ -294,6 +325,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
@Override public void onResume(long resumeLocation) {
saveData(IEntity.STATE_POST_PRE, IEntity.STATE_RUNNING);
lastLen = resumeLocation;
}
@Override public void onStart(long startLocation) {
@ -335,13 +367,13 @@ public class DownloadGroupUtil implements IDownloadUtil {
private void reTry() {
if (entity.getFailNum() < 5) {
Downloader dt = mDownloaderMap.get(entity.getDownloadUrl());
mExePool.execute(dt);
dt.startDownload();
}
}
private void saveData(int state, long location) {
entity.setState(state);
entity.setDownloadComplete(state == IEntity.STATE_COMPLETE);
entity.setComplete(state == IEntity.STATE_COMPLETE);
entity.setCurrentProgress(location);
entity.update();
}

@ -84,6 +84,7 @@ class Downloader implements Runnable, IDownloadUtil {
*/
private void startFlow() {
checkTask();
mListener.onPostPre(mEntity.getFileSize());
mConstance.cleanState();
mConstance.isDownloading = true;
try {

@ -90,7 +90,6 @@ public class SimpleDownloadUtil implements IDownloadUtil, Runnable {
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();
}
@ -99,7 +98,6 @@ public class SimpleDownloadUtil implements IDownloadUtil, Runnable {
}
})).start();
} else {
mListener.onPostPre(mTaskEntity.getEntity().getFileSize());
new Downloader(mListener, mTaskEntity).startDownload();
}
}

@ -60,6 +60,16 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
*/
private long completeTime;
private boolean isComplete = false;
public boolean isComplete() {
return isComplete;
}
public void setComplete(boolean complete) {
isComplete = complete;
}
public String getConvertFileSize() {
return convertFileSize;
}
@ -149,6 +159,7 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
dest.writeInt(this.state);
dest.writeLong(this.currentProgress);
dest.writeLong(this.completeTime);
dest.writeByte(this.isComplete ? (byte) 1 : (byte) 0);
}
protected AbsEntity(Parcel in) {
@ -161,5 +172,7 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
this.state = in.readInt();
this.currentProgress = in.readLong();
this.completeTime = in.readLong();
this.isComplete = in.readByte() != 0;
}
}

@ -22,13 +22,5 @@ package com.arialyy.aria.core.inf;
public abstract class AbsGroupTarget<TARGET extends AbsGroupTarget, ENTITY extends AbsGroupEntity, TASK_ENTITY extends AbsTaskEntity>
extends AbsTarget<TARGET, ENTITY, TASK_ENTITY> {
/**
* 设置任务组的组名如果不设置任务组Aria会自动将任务组的所有子任务的key相加取md5码作为任务组组名
*
* @param groupName 任务组组名
*/
public TARGET setGroupName(String groupName) {
mEntity.setGroupName(groupName);
return (TARGET) this;
}
}

@ -68,7 +68,8 @@ public abstract class AbsTaskEntity<ENTITY extends AbsEntity> extends DbEntity {
public String redirectUrl = "";
/**
* 用于判断删除任务时是否需要删除文件{@code true}删除
* {@code true} 删除任务数据库记录并且删除已经下载完成的文件
* {@code false} 如果任务已经完成只删除任务数据库记录
*/
@Ignore public boolean removeFile = false;

@ -118,7 +118,7 @@ public class DownloadTaskQueue
if (!TextUtils.isEmpty(target)) {
task = (DownloadTask) TaskFactory.getInstance()
.createTask(target, entity, DownloadSchedulers.getInstance());
entity.key = entity.getEntity().getDownloadUrl();
entity.key = entity.getEntity().getDownloadPath();
mCachePool.putTask(task);
} else {
Log.e(TAG, "target name 为 null!!");

@ -28,15 +28,7 @@ import com.arialyy.aria.orm.Primary;
public class UploadEntity extends AbsNormalEntity implements Parcelable {
@Primary
private String filePath; //文件路径
private boolean isComplete = false;
public boolean isComplete() {
return isComplete;
}
public void setComplete(boolean complete) {
isComplete = complete;
}
public String getFilePath() {
return filePath;
@ -56,13 +48,11 @@ public class UploadEntity extends AbsNormalEntity implements Parcelable {
@Override public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeString(this.filePath);
dest.writeByte(this.isComplete ? (byte) 1 : (byte) 0);
}
protected UploadEntity(Parcel in) {
super(in);
this.filePath = in.readString();
this.isComplete = in.readByte() != 0;
}
@Ignore public static final Creator<UploadEntity> CREATOR = new Creator<UploadEntity>() {

@ -133,16 +133,22 @@ public class CommonUtil {
/**
* 删除上传任务的配置包括
*
* @param removeFile {@code true} 删除已经上传完成的任务不仅删除上传记录还会删除已经上传完成的文件{@code false}
* 如果文件已经上传完成只删除上传记录
* @param removeFile {@code true} 不仅删除任务数据库记录还会删除已经下载完成的文件
* {@code false}如果任务已经完成只删除任务数据库记录
*/
public static void delUploadTaskConfig(boolean removeFile, UploadTaskEntity tEntity) {
UploadEntity uEntity = tEntity.getEntity();
File file = new File(uEntity.getFilePath());
if (removeFile) {
File file = new File(uEntity.getFilePath());
if (file.exists()) {
file.delete();
}
} else {
if (!uEntity.isComplete()) {
if (file.exists()) {
file.delete();
}
}
}
File config = new File(
AriaManager.APP.getFilesDir().getPath() + "/temp/" + uEntity.getFileName() + ".properties");
@ -156,17 +162,24 @@ public class CommonUtil {
/**
* 删除下载任务的配置包括
*
* @param removeFile{@code true} 删除已经下载完成的任务不仅删除下载记录还会删除已经下载完成的文件{@code false}
* 如果文件已经下载完成只删除下载记录
* @param removeFile {@code true} 不仅删除任务数据库记录还会删除已经下载完成的文件
* {@code false}如果任务已经完成只删除任务数据库记录
*/
public static void delDownloadTaskConfig(boolean removeFile, DownloadTaskEntity tEntity) {
DownloadEntity dEntity = tEntity.getEntity();
File file = new File(dEntity.getDownloadPath());
if (removeFile) {
File file = new File(dEntity.getDownloadPath());
if (file.exists()) {
file.delete();
}
} else {
if (!dEntity.isComplete()) {
if (file.exists()) {
file.delete();
}
}
}
File config = new File(
AriaManager.APP.getFilesDir().getPath() + "/temp/" + dEntity.getFileName() + ".properties");
if (config.exists()) {

@ -18,8 +18,6 @@ package com.arialyy.simple.download;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
@ -33,94 +31,30 @@ import com.arialyy.aria.core.download.DownloadTarget;
import com.arialyy.aria.core.Aria;
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.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;
public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
public static final int DOWNLOAD_PRE = 0x01;
public static final int DOWNLOAD_STOP = 0x02;
public static final int DOWNLOAD_FAILE = 0x03;
public static final int DOWNLOAD_CANCEL = 0x04;
public static final int DOWNLOAD_RESUME = 0x05;
public static final int DOWNLOAD_COMPLETE = 0x06;
public static final int DOWNLOAD_RUNNING = 0x07;
public static final int DOWNLOAD_START = 0x08;
private static final String DOWNLOAD_URL =
//"http://kotlinlang.org/docs/kotlin-docs.pdf";
//"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe";
//"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk";
//"http://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";
"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://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://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://ox.konsung.net:5555/ksdc-web/download/downloadFile/?fileName=ksdc_1.0.2.apk&rRange=0-";
//"http://172.18.104.50:8080/download/_302turn";
@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.speeds) RadioGroup mRg;
private Handler mUpdateHandler = new Handler() {
@Override public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case DOWNLOAD_RUNNING:
DownloadTask task = (DownloadTask) msg.obj;
long current = task.getCurrentProgress();
long len = task.getFileSize();
if (len == 0) {
mPb.setProgress(0);
} else {
mPb.setProgress((int) ((current * 100) / len));
}
getBinding().setSpeed(task.getConvertSpeed());
break;
case DOWNLOAD_PRE:
setBtState(false);
break;
case DOWNLOAD_START:
getBinding().setFileSize(CommonUtil.formatFileSize((Long) msg.obj));
break;
case DOWNLOAD_FAILE:
Toast.makeText(SingleTaskActivity.this, "下载失败", Toast.LENGTH_SHORT).show();
setBtState(true);
break;
case DOWNLOAD_STOP:
Toast.makeText(SingleTaskActivity.this, "暂停下载", Toast.LENGTH_SHORT).show();
mStart.setText("恢复");
setBtState(true);
break;
case DOWNLOAD_CANCEL:
mPb.setProgress(0);
Toast.makeText(SingleTaskActivity.this, "取消下载", Toast.LENGTH_SHORT).show();
mStart.setText("开始");
setBtState(true);
break;
case DOWNLOAD_RESUME:
mStart.setText("暂停");
setBtState(false);
break;
case DOWNLOAD_COMPLETE:
mPb.setProgress(100);
Toast.makeText(SingleTaskActivity.this, "下载完成", Toast.LENGTH_SHORT).show();
mStart.setText("重新开始?");
mCancel.setEnabled(false);
setBtState(true);
break;
}
}
};
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Aria.download(this).register();
@ -175,40 +109,54 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
}
@Download.onPre(DOWNLOAD_URL) protected void onPre(DownloadTask task) {
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize())
.sendToTarget();
setBtState(false);
}
@Download.onTaskStart(DOWNLOAD_URL) void taskStart(DownloadTask task) {
mUpdateHandler.obtainMessage(DOWNLOAD_START, task.getDownloadEntity().getFileSize())
.sendToTarget();
getBinding().setFileSize(task.getConvertFileSize());
}
@Download.onTaskRunning(DOWNLOAD_URL) protected void running(DownloadTask task) {
mUpdateHandler.obtainMessage(DOWNLOAD_RUNNING, task).sendToTarget();
long len = task.getFileSize();
if (len == 0) {
getBinding().setProgress(0);
} else {
getBinding().setProgress(task.getPercent());
}
getBinding().setSpeed(task.getConvertSpeed());
}
@Download.onTaskResume(DOWNLOAD_URL) void taskResume(DownloadTask task) {
mUpdateHandler.obtainMessage(DOWNLOAD_START, task.getFileSize()).sendToTarget();
mStart.setText("暂停");
setBtState(false);
}
@Download.onTaskStop(DOWNLOAD_URL) void taskStop(DownloadTask task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_STOP);
L.d(TAG, "task__stop");
mStart.setText("恢复");
setBtState(true);
getBinding().setSpeed("");
}
@Download.onTaskCancel(DOWNLOAD_URL) void taskCancel(DownloadTask task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_CANCEL);
L.d(TAG, "task__cancel");
getBinding().setProgress(0);
Toast.makeText(SingleTaskActivity.this, "取消下载", Toast.LENGTH_SHORT).show();
mStart.setText("开始");
setBtState(true);
getBinding().setSpeed("");
}
@Download.onTaskFail(DOWNLOAD_URL) void taskFail(DownloadTask task) {
L.d(TAG, "task__fail");
mUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILE);
Toast.makeText(SingleTaskActivity.this, "下载失败", Toast.LENGTH_SHORT).show();
setBtState(true);
}
@Download.onTaskComplete(DOWNLOAD_URL) void taskComplete(DownloadTask task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_COMPLETE);
getBinding().setProgress(100);
Toast.makeText(SingleTaskActivity.this, "下载完成", Toast.LENGTH_SHORT).show();
mStart.setText("重新开始?");
mCancel.setEnabled(false);
setBtState(true);
getBinding().setSpeed("");
}
@Download.onNoSupportBreakPoint(DOWNLOAD_URL)
@ -224,7 +172,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
super.init(savedInstanceState);
setTitle("单任务下载");
DownloadTarget target = Aria.download(this).load(DOWNLOAD_URL);
mPb.setProgress(target.getPercent());
getBinding().setProgress(target.getPercent());
if (target.getTaskState() == IEntity.STATE_STOP) {
mStart.setText("恢复");
mStart.setTextColor(getResources().getColor(android.R.color.holo_blue_light));
@ -248,11 +196,6 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
} else if (text.equals("恢复")) {
Aria.download(this).load(DOWNLOAD_URL).resume();
}
//DownloadTarget target = Aria.download(this)
// .load(DOWNLOAD_URL)
// .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk");
//target.add();
//target.cancel();
break;
case R.id.stop:
Aria.download(this).load(DOWNLOAD_URL).pause();

@ -49,11 +49,11 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
setTitle("任务组");
mUrls = getModule(GroupModule.class).getUrls();
DownloadGroupTaskEntity entity = Aria.download(this).getDownlaodGroupTask(mUrls);
if (entity != null) {
if (entity != null && entity.getEntity() != null) {
DownloadGroupEntity groupEntity = entity.getEntity();
getBinding().setFileSize(groupEntity.getConvertFileSize());
getBinding().setProgress(
(int) (groupEntity.getCurrentProgress() * 100 / groupEntity.getFileSize()));
getBinding().setProgress(groupEntity.isComplete() ? 100
: (int) (groupEntity.getCurrentProgress() * 100 / groupEntity.getFileSize()));
}
}
@ -115,6 +115,7 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
}
@DownloadGroup.onTaskComplete() void taskComplete(DownloadGroupTask task) {
getBinding().setProgress(100);
T.showShort(this, "任务组下载完成");
}
}

@ -32,7 +32,8 @@ public class GroupModule extends BaseModule {
List<String> getUrls() {
List<String> urls = new ArrayList<>();
String[] str = getContext().getResources().getStringArray(R.array.download_url);
//String[] str = getContext().getResources().getStringArray(R.array.download_url);
String[] str = getContext().getResources().getStringArray(R.array.group_urls);
Collections.addAll(urls, str);
return urls;
}

@ -22,6 +22,7 @@
<item>幻影纹章</item>
<item>史上最坑爹的游戏10</item>
<item>鲜果消消乐</item>
<item>航海奇迹</item>
</string-array>
<string-array name="download_url">
@ -30,6 +31,7 @@
<item>http://rs.0.gaoshouyou.com/d/51/46/58514d126c46b8a3f27fc8c7db3b09ec.apk</item>
<item>http://rs.0.gaoshouyou.com/d/23/69/07238f952669727878d7a0e180534c8b.apk</item>
<item>http://rs.0.gaoshouyou.com/d/e7/3d/73e716d3353de5b479fcf7da8d36a5ef.apk</item>
<item>http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk</item>
</string-array>
<string-array name="highest_names">

Loading…
Cancel
Save