group target
This commit is contained in:
@ -18,6 +18,7 @@ package com.arialyy.aria.core.download;
|
||||
import android.os.Parcel;
|
||||
import com.arialyy.aria.core.inf.AbsGroupEntity;
|
||||
import com.arialyy.aria.orm.OneToMany;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
@ -27,15 +28,29 @@ import java.util.List;
|
||||
*/
|
||||
public class DownloadGroupEntity extends AbsGroupEntity {
|
||||
|
||||
@OneToMany(table = DownloadEntity.class, key = "groupName")
|
||||
private List<DownloadEntity> mChild = new LinkedList<>();
|
||||
@OneToMany(table = DownloadEntity.class, key = "groupName") private List<DownloadEntity>
|
||||
mSubtask = new ArrayList<>();
|
||||
|
||||
public List<DownloadEntity> getChild() {
|
||||
return mChild;
|
||||
//任务组下载文件的文件夹地址
|
||||
private String mDirPath = "";
|
||||
|
||||
public List<DownloadEntity> getSubTask() {
|
||||
return mSubtask;
|
||||
}
|
||||
|
||||
public void setChild(List<DownloadEntity> child) {
|
||||
this.mChild = child;
|
||||
public void setSubTasks(List<DownloadEntity> subTasks) {
|
||||
this.mSubtask = subTasks;
|
||||
}
|
||||
|
||||
public String getDirPath() {
|
||||
return mDirPath;
|
||||
}
|
||||
|
||||
public void setDirPath(String dirPath) {
|
||||
this.mDirPath = dirPath;
|
||||
}
|
||||
|
||||
public DownloadGroupEntity() {
|
||||
}
|
||||
|
||||
@Override public int describeContents() {
|
||||
@ -44,15 +59,14 @@ public class DownloadGroupEntity extends AbsGroupEntity {
|
||||
|
||||
@Override public void writeToParcel(Parcel dest, int flags) {
|
||||
super.writeToParcel(dest, flags);
|
||||
dest.writeTypedList(this.mChild);
|
||||
}
|
||||
|
||||
public DownloadGroupEntity() {
|
||||
dest.writeTypedList(this.mSubtask);
|
||||
dest.writeString(this.mDirPath);
|
||||
}
|
||||
|
||||
protected DownloadGroupEntity(Parcel in) {
|
||||
super(in);
|
||||
this.mChild = in.createTypedArrayList(DownloadEntity.CREATOR);
|
||||
this.mSubtask = in.createTypedArrayList(DownloadEntity.CREATOR);
|
||||
this.mDirPath = in.readString();
|
||||
}
|
||||
|
||||
public static final Creator<DownloadGroupEntity> CREATOR = new Creator<DownloadGroupEntity>() {
|
||||
|
@ -15,27 +15,38 @@
|
||||
*/
|
||||
package com.arialyy.aria.core.download;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import com.arialyy.aria.core.inf.AbsGroupTarget;
|
||||
import com.arialyy.aria.core.inf.IEntity;
|
||||
import com.arialyy.aria.orm.DbEntity;
|
||||
import com.arialyy.aria.util.CheckUtil;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by AriaL on 2017/6/29.
|
||||
*/
|
||||
public class DownloadGroupTarget
|
||||
extends AbsGroupTarget<DownloadGroupTarget, DownloadGroupEntity, DownloadGroupTaskEntity> {
|
||||
private List<String> mUrls;
|
||||
private List<String> mUrls = new ArrayList<>();
|
||||
/**
|
||||
* 子任务文件名
|
||||
*/
|
||||
private List<String> mSubtaskFileName = new ArrayList<>();
|
||||
private String mGroupName;
|
||||
/**
|
||||
* 是否已经设置了文件路径
|
||||
*/
|
||||
private boolean isSetDirPathed = false;
|
||||
|
||||
DownloadGroupTarget(List<String> urls, String targetName) {
|
||||
this.mTargetName = targetName;
|
||||
this.mUrls = urls;
|
||||
mGroupName = CommonUtil.getMd5Code(urls);
|
||||
mTaskEntity = DbEntity.findData(DownloadGroupTaskEntity.class, "key=?", mGroupName);
|
||||
mTaskEntity = DbEntity.findFirst(DownloadGroupTaskEntity.class, "key=?", mGroupName);
|
||||
if (mTaskEntity == null) {
|
||||
mTaskEntity = new DownloadGroupTaskEntity();
|
||||
mTaskEntity.key = mGroupName;
|
||||
@ -49,7 +60,7 @@ public class DownloadGroupTarget
|
||||
|
||||
private DownloadGroupEntity getDownloadGroupEntity() {
|
||||
DownloadGroupEntity entity =
|
||||
DbEntity.findData(DownloadGroupEntity.class, "groupName=?", mGroupName);
|
||||
DbEntity.findFirst(DownloadGroupEntity.class, "groupName=?", mGroupName);
|
||||
if (entity == null) {
|
||||
entity = new DownloadGroupEntity();
|
||||
}
|
||||
@ -58,7 +69,7 @@ public class DownloadGroupTarget
|
||||
|
||||
/**
|
||||
* 设置任务组的文件夹路径,在Aria中,任务组的所有子任务都会下载到以任务组组名的文件夹中。
|
||||
* 如:groupDirPath = "/mnt/sdcard/download/", groupName = "group_test"
|
||||
* 如:groupDirPath = "/mnt/sdcard/download/group_test"
|
||||
* <pre>
|
||||
* {@code
|
||||
* + mnt
|
||||
@ -75,43 +86,86 @@ public class DownloadGroupTarget
|
||||
*
|
||||
* @param groupDirPath 任务组保存文件夹路径
|
||||
*/
|
||||
public DownloadGroupTarget setDownloadPath(String groupDirPath) {
|
||||
public DownloadGroupTarget setDownloadDirPath(String groupDirPath) {
|
||||
if (TextUtils.isEmpty(groupDirPath)) {
|
||||
throw new NullPointerException("任务组文件夹保存路径不能为null");
|
||||
}
|
||||
|
||||
if (mEntity.getDirPath().equals(groupDirPath)) return this;
|
||||
|
||||
File file = new File(groupDirPath);
|
||||
if (file.exists() && file.isFile()) {
|
||||
throw new IllegalArgumentException("路径不能为文件");
|
||||
}
|
||||
if (!file.exists()) {
|
||||
file.mkdirs();
|
||||
}
|
||||
|
||||
if (!TextUtils.isEmpty(mEntity.getDirPath())) {
|
||||
reChangeDirPath(groupDirPath);
|
||||
} else {
|
||||
mEntity.setSubTasks(createSubTask());
|
||||
}
|
||||
mEntity.setDirPath(groupDirPath);
|
||||
mEntity.update();
|
||||
isSetDirPathed = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置保存路径组
|
||||
* 改变任务组文件夹路径
|
||||
*
|
||||
* @param newDirPath 新的文件夹路径
|
||||
*/
|
||||
public DownloadGroupTarget setDownloadPaths(List<String> paths) {
|
||||
CheckUtil.checkDownloadPaths(paths);
|
||||
if (mUrls.size() != paths.size()) {
|
||||
private void reChangeDirPath(String newDirPath) {
|
||||
List<DownloadEntity> subTask = mEntity.getSubTask();
|
||||
if (subTask != null && !subTask.isEmpty()) {
|
||||
for (DownloadEntity entity : subTask) {
|
||||
File file = new File(entity.getDownloadPath());
|
||||
file.renameTo(new File(newDirPath, entity.getFileName()));
|
||||
}
|
||||
} else {
|
||||
mEntity.setSubTasks(createSubTask());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置子任务文件名,该方法如果在{@link #setDownloadDirPath(String)}之前调用,则不生效
|
||||
*/
|
||||
public DownloadGroupTarget setSubtaskFileName(List<String> subTaskFileName) {
|
||||
if (subTaskFileName == null || subTaskFileName.isEmpty()) return this;
|
||||
mSubtaskFileName.addAll(subTaskFileName);
|
||||
if (mUrls.size() != subTaskFileName.size()) {
|
||||
throw new IllegalArgumentException("下载链接数必须要和保存路径的数量一致");
|
||||
}
|
||||
for (int i = 0, len = mUrls.size(); i < len; i++) {
|
||||
mTaskEntity.getEntity().getChild().add(createDownloadEntity(mUrls.get(i), paths.get(i)));
|
||||
if (isSetDirPathed) {
|
||||
List<DownloadEntity> entities = mEntity.getSubTask();
|
||||
int i = 0;
|
||||
for (DownloadEntity entity : entities) {
|
||||
entity.setFileName(mSubtaskFileName.get(i));
|
||||
entity.update();
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建子任务下载实体
|
||||
*
|
||||
* @param url 下载地址
|
||||
* @param path 保存路径
|
||||
* 创建子任务
|
||||
*/
|
||||
private DownloadEntity createDownloadEntity(String url, String path) {
|
||||
DownloadEntity entity = DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", url);
|
||||
if (entity == null) {
|
||||
entity = new DownloadEntity();
|
||||
private List<DownloadEntity> createSubTask() {
|
||||
List<DownloadEntity> list = new ArrayList<>();
|
||||
for (int i = 0, len = mUrls.size(); i < len; i++) {
|
||||
DownloadEntity entity = new DownloadEntity();
|
||||
entity.setDownloadUrl(mUrls.get(i));
|
||||
String fileName = mSubtaskFileName.get(i);
|
||||
if (TextUtils.isEmpty(fileName)) {
|
||||
fileName = CommonUtil.keyToHashKey(mUrls.get(i));
|
||||
}
|
||||
entity.setDownloadPath(mEntity.getDirPath() + "/" + fileName);
|
||||
entity.setGroupName(mGroupName);
|
||||
entity.save();
|
||||
list.add(entity);
|
||||
}
|
||||
File file = new File(path);
|
||||
if (!file.exists()) {
|
||||
entity.setState(IEntity.STATE_WAIT);
|
||||
}
|
||||
entity.setDownloadPath(path);
|
||||
entity.setDownloadUrl(url);
|
||||
entity.setFileName(file.getName());
|
||||
return entity;
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
@ -18,15 +18,12 @@ package com.arialyy.aria.core.download;
|
||||
import android.support.annotation.NonNull;
|
||||
import com.arialyy.aria.core.AriaManager;
|
||||
import com.arialyy.aria.core.inf.AbsReceiver;
|
||||
import com.arialyy.aria.core.inf.IEntity;
|
||||
import com.arialyy.aria.core.inf.IReceiver;
|
||||
import com.arialyy.aria.core.command.normal.NormalCmdFactory;
|
||||
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
|
||||
import com.arialyy.aria.core.scheduler.ISchedulerListener;
|
||||
import com.arialyy.aria.orm.DbEntity;
|
||||
import com.arialyy.aria.util.CheckUtil;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@ -119,14 +116,14 @@ public class DownloadReceiver extends AbsReceiver<DownloadEntity> {
|
||||
*/
|
||||
public DownloadEntity getDownloadEntity(String downloadUrl) {
|
||||
CheckUtil.checkDownloadUrl(downloadUrl);
|
||||
return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
|
||||
return DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载任务是否存在
|
||||
*/
|
||||
@Override public boolean taskExists(String downloadUrl) {
|
||||
return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl) != null;
|
||||
return DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl) != null;
|
||||
}
|
||||
|
||||
@Override public List<DownloadEntity> getTaskList() {
|
||||
|
@ -36,7 +36,7 @@ public class DownloadTarget
|
||||
|
||||
DownloadTarget(String url, String targetName) {
|
||||
mTargetName = targetName;
|
||||
mTaskEntity = DbEntity.findData(DownloadTaskEntity.class, "key=?", url);
|
||||
mTaskEntity = DbEntity.findFirst(DownloadTaskEntity.class, "key=?", url);
|
||||
if (mTaskEntity == null) {
|
||||
mTaskEntity = new DownloadTaskEntity();
|
||||
mTaskEntity.entity = new DownloadEntity();
|
||||
@ -54,7 +54,7 @@ public class DownloadTarget
|
||||
*/
|
||||
private DownloadEntity getEntity(String downloadUrl) {
|
||||
DownloadEntity entity =
|
||||
DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
|
||||
DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl);
|
||||
if (entity == null) {
|
||||
entity = new DownloadEntity();
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
||||
mInfoPool = Executors.newCachedThreadPool();
|
||||
mExePool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
||||
|
||||
for (DownloadEntity entity : mTaskEntity.entity.getChild()) {
|
||||
for (DownloadEntity entity : mTaskEntity.entity.getSubTask()) {
|
||||
File file = new File(entity.getDownloadPath());
|
||||
if (entity.isDownloadComplete() && file.exists()) {
|
||||
mTotalSize += entity.getFileSize();
|
||||
@ -175,7 +175,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
||||
startChildDownload(te);
|
||||
}
|
||||
mInitNum++;
|
||||
if (mInitNum + mFailNum == mTaskEntity.getEntity().getChild().size()) {
|
||||
if (mInitNum + mFailNum == mTaskEntity.getEntity().getSubTask().size()) {
|
||||
startRunningFlow();
|
||||
}
|
||||
}
|
||||
@ -191,7 +191,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
||||
if (failNum < 10) {
|
||||
mInfoPool.execute(createFileInfoThread(te));
|
||||
}
|
||||
if (mInitNum + mFailNum == mTaskEntity.getEntity().getChild().size()) {
|
||||
if (mInitNum + mFailNum == mTaskEntity.getEntity().getSubTask().size()) {
|
||||
startRunningFlow();
|
||||
}
|
||||
}
|
||||
@ -238,7 +238,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
||||
*/
|
||||
private DownloadTaskEntity createDownloadTask(DownloadEntity entity) {
|
||||
DownloadTaskEntity taskEntity =
|
||||
DbEntity.findData(DownloadTaskEntity.class, "key=?", entity.getDownloadUrl());
|
||||
DbEntity.findFirst(DownloadTaskEntity.class, "key=?", entity.getDownloadUrl());
|
||||
if (taskEntity != null) {
|
||||
return taskEntity;
|
||||
}
|
||||
|
@ -192,7 +192,7 @@ class Downloader implements Runnable, IDownloadUtil {
|
||||
CommonUtil.createFile(mConfigFile.getPath());
|
||||
} else if (!mTempFile.exists()) {
|
||||
isNewTask = true;
|
||||
} else if (DbEntity.findData(DownloadEntity.class, "downloadUrl=?", mEntity.getDownloadUrl())
|
||||
} else if (DbEntity.findFirst(DownloadEntity.class, "downloadUrl=?", mEntity.getDownloadUrl())
|
||||
== null) {
|
||||
isNewTask = true;
|
||||
} else {
|
||||
|
@ -17,21 +17,17 @@ package com.arialyy.aria.core.upload;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import com.arialyy.aria.core.AriaManager;
|
||||
import com.arialyy.aria.core.command.normal.AbsNormalCmd;
|
||||
import com.arialyy.aria.core.command.normal.NormalCmdFactory;
|
||||
import com.arialyy.aria.core.download.DownloadTaskEntity;
|
||||
import com.arialyy.aria.core.inf.AbsReceiver;
|
||||
import com.arialyy.aria.core.inf.IEntity;
|
||||
import com.arialyy.aria.core.inf.IReceiver;
|
||||
import com.arialyy.aria.core.scheduler.ISchedulerListener;
|
||||
import com.arialyy.aria.core.scheduler.UploadSchedulers;
|
||||
import com.arialyy.aria.orm.DbEntity;
|
||||
import com.arialyy.aria.util.CheckUtil;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2017/2/6.
|
||||
@ -56,14 +52,14 @@ public class UploadReceiver extends AbsReceiver<UploadEntity> {
|
||||
*/
|
||||
public UploadEntity getUploadEntity(String filePath) {
|
||||
CheckUtil.checkUploadPath(filePath);
|
||||
return DbEntity.findData(UploadEntity.class, "filePath=?", filePath);
|
||||
return DbEntity.findFirst(UploadEntity.class, "filePath=?", filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载任务是否存在
|
||||
*/
|
||||
@Override public boolean taskExists(String filePath) {
|
||||
return DbEntity.findData(UploadEntity.class, "filePath=?", filePath) != null;
|
||||
return DbEntity.findFirst(UploadEntity.class, "filePath=?", filePath) != null;
|
||||
}
|
||||
|
||||
@Override public List<UploadEntity> getTaskList() {
|
||||
|
@ -28,7 +28,7 @@ public class UploadTarget extends AbsNormalTarget<UploadTarget, UploadEntity, Up
|
||||
|
||||
UploadTarget(String filePath, String targetName) {
|
||||
this.mTargetName = targetName;
|
||||
mTaskEntity = DbEntity.findData(UploadTaskEntity.class, "key=?", filePath);
|
||||
mTaskEntity = DbEntity.findFirst(UploadTaskEntity.class, "key=?", filePath);
|
||||
if (mTaskEntity == null) {
|
||||
mTaskEntity = new UploadTaskEntity();
|
||||
mTaskEntity.entity = new UploadEntity();
|
||||
@ -40,7 +40,7 @@ public class UploadTarget extends AbsNormalTarget<UploadTarget, UploadEntity, Up
|
||||
}
|
||||
|
||||
private UploadEntity getUploadEntity(String filePath) {
|
||||
UploadEntity entity = UploadEntity.findData(UploadEntity.class, "filePath=?", filePath);
|
||||
UploadEntity entity = UploadEntity.findFirst(UploadEntity.class, "filePath=?", filePath);
|
||||
if (entity == null) {
|
||||
entity = new UploadEntity();
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ public class DbEntity {
|
||||
/**
|
||||
* 查询一组数据
|
||||
* <code>
|
||||
* DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
|
||||
* DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl);
|
||||
* </code>
|
||||
*
|
||||
* @return 没有数据返回null
|
||||
@ -70,12 +70,12 @@ public class DbEntity {
|
||||
/**
|
||||
* 查询一行数据
|
||||
* <code>
|
||||
* DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
|
||||
* DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl);
|
||||
* </code>
|
||||
*
|
||||
* @return 没有数据返回null
|
||||
*/
|
||||
public static <T extends DbEntity> T findData(Class<T> clazz, String... expression) {
|
||||
public static <T extends DbEntity> T findFirst(Class<T> clazz, String... expression) {
|
||||
DbUtil util = DbUtil.getInstance();
|
||||
List<T> datas = util.findData(clazz, expression);
|
||||
return datas == null ? null : datas.size() > 0 ? datas.get(0) : null;
|
||||
@ -149,7 +149,7 @@ public class DbEntity {
|
||||
updateRowID();
|
||||
}
|
||||
|
||||
private <T extends DbEntity> T findData(Class<T> clazz, @NonNull String[] wheres,
|
||||
private <T extends DbEntity> T findFirst(Class<T> clazz, @NonNull String[] wheres,
|
||||
@NonNull String[] values) {
|
||||
DbUtil util = DbUtil.getInstance();
|
||||
List<T> list = util.findData(clazz, wheres, values);
|
||||
@ -178,7 +178,7 @@ public class DbEntity {
|
||||
values.add(field.get(this) + "");
|
||||
}
|
||||
}
|
||||
DbEntity entity = findData(getClass(), where.toArray(new String[where.size()]),
|
||||
DbEntity entity = findFirst(getClass(), where.toArray(new String[where.size()]),
|
||||
values.toArray(new String[values.size()]));
|
||||
if (entity != null) {
|
||||
rowID = entity.rowID;
|
||||
|
Reference in New Issue
Block a user