优化代码,优化demo

This commit is contained in:
AriaLyy
2017-06-03 19:01:23 +08:00
parent 1764f50258
commit 59b983eaec
9 changed files with 145 additions and 84 deletions

View File

@ -21,7 +21,6 @@ import android.util.Log;
import com.arialyy.aria.core.RequestEnum;
import com.arialyy.aria.core.inf.AbsTarget;
import com.arialyy.aria.core.queue.DownloadTaskQueue;
import com.arialyy.aria.util.CheckUtil;
import java.io.File;
import java.util.Map;
@ -67,8 +66,6 @@ public class DownloadTarget extends AbsTarget<DownloadEntity, DownloadTaskEntity
}
@Override public int getPercent() {
DownloadEntity entity = DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?",
this.entity.getDownloadUrl());
if (entity == null) {
Log.e("DownloadTarget", "下载管理器中没有该任务");
return 0;
@ -103,8 +100,8 @@ public class DownloadTarget extends AbsTarget<DownloadEntity, DownloadTaskEntity
/**
* 下载任务是否存在
*/
@Override public boolean taskExists(String downloadUrl) {
return DownloadTaskQueue.getInstance().getTask(downloadUrl) != null;
@Override public boolean taskExists() {
return DownloadTaskQueue.getInstance().getTask(entity.getDownloadUrl()) != null;
}
/**
@ -141,15 +138,15 @@ public class DownloadTarget extends AbsTarget<DownloadEntity, DownloadTaskEntity
return this;
}
private DownloadEntity getDownloadEntity(String downloadUrl) {
CheckUtil.checkDownloadUrl(downloadUrl);
return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
private DownloadEntity getDownloadEntity() {
return entity;
}
/**
* 是否在下载
*/
public boolean isDownloading() {
return DownloadTaskQueue.getInstance().getTask(entity).isRunning();
DownloadTask task = DownloadTaskQueue.getInstance().getTask(entity);
return task != null && task.isRunning();
}
}

View File

@ -112,7 +112,8 @@ final class SingleThreadTask implements Runnable {
}
//支持断点的处理
if (mConfigEntity.isSupportBreakpoint) {
Log.i(TAG, "线程【" + mConfigEntity.THREAD_ID + "】下载完毕");
Log.i(TAG,
"任务【" + mConfigEntity.TEMP_FILE.getName() + "】线程【" + mConfigEntity.THREAD_ID + "】下载完毕");
writeConfig(mConfigEntity.TEMP_FILE.getName() + "_state_" + mConfigEntity.THREAD_ID, 1);
mListener.onChildComplete(mConfigEntity.END_LOCATION);
CONSTANCE.COMPLETE_THREAD_NUM++;
@ -130,14 +131,11 @@ final class SingleThreadTask implements Runnable {
mListener.onComplete();
}
} catch (MalformedURLException e) {
CONSTANCE.FAIL_NUM++;
failDownload(mConfigEntity, mChildCurrentLocation, "下载链接异常", e);
} catch (IOException e) {
CONSTANCE.FAIL_NUM++;
failDownload(mConfigEntity, mChildCurrentLocation, "下载失败【" + mConfigEntity.DOWNLOAD_URL + "",
e);
} catch (Exception e) {
CONSTANCE.FAIL_NUM++;
failDownload(mConfigEntity, mChildCurrentLocation, "获取流失败", e);
}
}
@ -218,6 +216,7 @@ final class SingleThreadTask implements Runnable {
Exception ex) {
synchronized (LOCK) {
try {
CONSTANCE.FAIL_NUM++;
CONSTANCE.isDownloading = false;
CONSTANCE.isStop = true;
if (ex != null) {

View File

@ -23,9 +23,7 @@ import com.arialyy.aria.core.RequestEnum;
import com.arialyy.aria.core.command.AbsCmd;
import com.arialyy.aria.core.command.CmdFactory;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.queue.DownloadTaskQueue;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CommonUtil;
import java.util.ArrayList;
import java.util.List;
@ -89,31 +87,39 @@ public abstract class AbsTarget<ENTITY extends IEntity, TASK_ENTITY extends ITas
/**
* 获取任务文件大小
*
* @return -1没有找到该任务
* @return 文件大小
*/
public long getFileSize() {
if (entity instanceof DownloadEntity) {
DownloadEntity entity = DbEntity.findData(DownloadEntity.class, "downloadUrl=?",
((DownloadEntity) this.entity).getDownloadUrl());
if (entity == null) {
throw new NullPointerException("没有找到该任务");
}
DownloadEntity entity = (DownloadEntity) this.entity;
return entity.getFileSize();
} else if (entity instanceof UploadEntity) {
UploadEntity entity = DbEntity.findData(UploadEntity.class, "filePath=?",
((UploadEntity) this.entity).getFilePath());
if (entity == null) {
throw new NullPointerException("没有找到该任务");
}
UploadEntity entity = (UploadEntity) this.entity;
return entity.getFileSize();
}
return -1;
return 0;
}
/**
* 获取单位转换后的文件大小
*
* @return 文件大小{@code xxx mb}
*/
public String getConvertFileSize() {
if (entity instanceof DownloadEntity) {
DownloadEntity entity = (DownloadEntity) this.entity;
return CommonUtil.formatFileSize(entity.getFileSize());
} else if (entity instanceof UploadEntity) {
UploadEntity entity = (UploadEntity) this.entity;
return CommonUtil.formatFileSize(entity.getFileSize());
}
return "0b";
}
/**
* 下载任务是否存在
*/
public boolean taskExists(String downloadUrl) {
public boolean taskExists() {
return false;
}
@ -127,26 +133,16 @@ public abstract class AbsTarget<ENTITY extends IEntity, TASK_ENTITY extends ITas
}
/**
* 获取当前任务进度,如果任务存在,则返回当前进度
* 获取任务进度,如果任务存在,则返回当前进度
*
* @return -1没有找到该任务
* @return 该任务进度
*/
public long getCurrentProgress() {
if (entity instanceof DownloadEntity) {
DownloadEntity entity = DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?",
((DownloadEntity) this.entity).getDownloadUrl());
if (entity == null) {
Log.e("DownloadTarget", "下载管理器中没有该任务");
return -1;
}
DownloadEntity entity = (DownloadEntity) this.entity;
return entity.getCurrentProgress();
} else if (entity instanceof UploadEntity) {
UploadEntity entity = DbEntity.findData(UploadEntity.class, "filePath=?",
((UploadEntity) this.entity).getFilePath());
if (entity == null) {
Log.e("DownloadTarget", "下载管理器中没有该任务");
return -1;
}
UploadEntity entity = (UploadEntity) this.entity;
return entity.getCurrentProgress();
}
return -1;

View File

@ -59,7 +59,7 @@ public class DownloadTaskQueue
Set<String> keys = exeTasks.keySet();
for (String key : keys) {
DownloadTask temp = exeTasks.get(key);
if (temp != null && temp.isRunning() && temp.isHighestPriorityTask() && !temp.getKey()
if (temp != null && temp.isRunning() && temp.isHighestPriorityTask() && !temp.getKey()
.equals(task.getKey())) {
Log.e(TAG, "设置最高优先级任务失败,失败原因【任务中已经有最高优先级任务,请等待上一个最高优先级任务完成,或手动暂停该任务】");
task.setHighestPriority(false);
@ -76,18 +76,26 @@ public class DownloadTaskQueue
for (int i = 0; i < maxSize; i++) {
DownloadTask oldTsk = mExecutePool.pollTask();
if (oldTsk != null && oldTsk.isRunning()) {
oldTsk.stop();
if (i == maxSize - 1) {
oldTsk.stop();
break;
}
tempTasks.add(oldTsk);
}
}
startTask(task);
int i = 0, len = tempTasks.size() - 1;
for (DownloadTask oldTask : tempTasks) {
if (i < len) {
startTask(oldTask);
}
i++;
for (DownloadTask temp : tempTasks){
mExecutePool.putTask(temp);
}
//int i = 0, len = tempTasks.size() - 1;
//for (DownloadTask oldTask : tempTasks) {
// if (i < len) {
// startTask(oldTask);
// }
// i++;
//}
}
}

View File

@ -103,8 +103,8 @@ public class UploadTarget extends AbsTarget<UploadEntity, UploadTaskEntity> {
/**
* 下载任务是否存在
*/
@Override public boolean taskExists(String downloadUrl) {
return UploadTaskQueue.getInstance().getTask(downloadUrl) != null;
@Override public boolean taskExists() {
return UploadTaskQueue.getInstance().getTask(entity.getFilePath()) != null;
}
/**
@ -117,14 +117,15 @@ public class UploadTarget extends AbsTarget<UploadEntity, UploadTaskEntity> {
return this;
}
private UploadEntity getDownloadEntity(@NonNull String filePath) {
return DbEntity.findData(UploadEntity.class, "filePath=?", filePath);
private UploadEntity getDownloadEntity() {
return entity;
}
/**
* 是否在下载
*/
public boolean isUploading() {
return UploadTaskQueue.getInstance().getTask(entity).isRunning();
UploadTask task = UploadTaskQueue.getInstance().getTask(entity);
return task != null && task.isRunning();
}
}