DownloadTarget 添加获取任务进度百分比接口

This commit is contained in:
AriaLyy
2017-06-02 20:12:21 +08:00
parent 5ee2cba331
commit 52a4f1e7ea
4 changed files with 28 additions and 8 deletions

View File

@ -66,6 +66,19 @@ public class DownloadTarget extends AbsTarget<DownloadEntity, DownloadTaskEntity
return this;
}
@Override public int getPercent() {
DownloadEntity entity = DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?",
this.entity.getDownloadUrl());
if (entity == null) {
Log.e("DownloadTarget", "下载管理器中没有该任务");
return 0;
}
if (entity.getFileSize() != 0) {
return (int) (entity.getCurrentProgress() * 100 / entity.getFileSize());
}
return super.getPercent();
}
/**
* 给url请求添加头部
*

View File

@ -108,6 +108,15 @@ public class AbsTarget<ENTITY extends IEntity, TASK_ENTITY extends ITaskEntity>
return false;
}
/**
* 获取任务进度百分比
*
* @return 返回任务进度
*/
protected int getPercent() {
return 0;
}
/**
* 获取当前任务进度,如果任务存在,则返回当前进度
*
@ -118,14 +127,16 @@ public class AbsTarget<ENTITY extends IEntity, TASK_ENTITY extends ITaskEntity>
DownloadEntity entity = DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?",
((DownloadEntity) this.entity).getDownloadUrl());
if (entity == null) {
throw new NullPointerException("下载管理器中没有该任务");
Log.e("DownloadTarget", "下载管理器中没有该任务");
return -1;
}
return entity.getCurrentProgress();
} else if (entity instanceof UploadEntity) {
UploadEntity entity = DbEntity.findData(UploadEntity.class, "filePath=?",
((UploadEntity) this.entity).getFilePath());
if (entity == null) {
throw new NullPointerException("没有找到该任务");
Log.e("DownloadTarget", "下载管理器中没有该任务");
return -1;
}
return entity.getCurrentProgress();
}

View File

@ -52,9 +52,7 @@ public class HighestPriorityActivity extends BaseActivity<ActivityHighestPriorit
private void initWidget() {
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);
mPb.setProgress(Aria.download(this).load(DOWNLOAD_URL).getPercent());
}
mAdapter = new DownloadAdapter(this, getModule(DownloadModule.class).getDownloadTaskList());
mList.setLayoutManager(new LinearLayoutManager(this));

View File

@ -165,9 +165,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
private void init() {
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);
mPb.setProgress(Aria.download(this).load(DOWNLOAD_URL).getPercent());
}
}