group target

pull/330/head
AriaLyy 8 years ago
parent 5533742855
commit 449d9c763a
  1. 36
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupEntity.java
  2. 110
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java
  3. 7
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
  4. 4
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java
  5. 8
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java
  6. 2
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java
  7. 8
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java
  8. 4
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java
  9. 10
      Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java

@ -18,6 +18,7 @@ package com.arialyy.aria.core.download;
import android.os.Parcel; import android.os.Parcel;
import com.arialyy.aria.core.inf.AbsGroupEntity; import com.arialyy.aria.core.inf.AbsGroupEntity;
import com.arialyy.aria.orm.OneToMany; import com.arialyy.aria.orm.OneToMany;
import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -27,15 +28,29 @@ import java.util.List;
*/ */
public class DownloadGroupEntity extends AbsGroupEntity { public class DownloadGroupEntity extends AbsGroupEntity {
@OneToMany(table = DownloadEntity.class, key = "groupName") @OneToMany(table = DownloadEntity.class, key = "groupName") private List<DownloadEntity>
private List<DownloadEntity> mChild = new LinkedList<>(); mSubtask = new ArrayList<>();
public List<DownloadEntity> getChild() { //任务组下载文件的文件夹地址
return mChild; private String mDirPath = "";
public List<DownloadEntity> getSubTask() {
return mSubtask;
}
public void setSubTasks(List<DownloadEntity> subTasks) {
this.mSubtask = subTasks;
} }
public void setChild(List<DownloadEntity> child) { public String getDirPath() {
this.mChild = child; return mDirPath;
}
public void setDirPath(String dirPath) {
this.mDirPath = dirPath;
}
public DownloadGroupEntity() {
} }
@Override public int describeContents() { @Override public int describeContents() {
@ -44,15 +59,14 @@ public class DownloadGroupEntity extends AbsGroupEntity {
@Override public void writeToParcel(Parcel dest, int flags) { @Override public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags); super.writeToParcel(dest, flags);
dest.writeTypedList(this.mChild); dest.writeTypedList(this.mSubtask);
} dest.writeString(this.mDirPath);
public DownloadGroupEntity() {
} }
protected DownloadGroupEntity(Parcel in) { protected DownloadGroupEntity(Parcel in) {
super(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>() { public static final Creator<DownloadGroupEntity> CREATOR = new Creator<DownloadGroupEntity>() {

@ -15,27 +15,38 @@
*/ */
package com.arialyy.aria.core.download; package com.arialyy.aria.core.download;
import android.text.TextUtils;
import com.arialyy.aria.core.inf.AbsGroupTarget; import com.arialyy.aria.core.inf.AbsGroupTarget;
import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* Created by AriaL on 2017/6/29. * Created by AriaL on 2017/6/29.
*/ */
public class DownloadGroupTarget public class DownloadGroupTarget
extends AbsGroupTarget<DownloadGroupTarget, DownloadGroupEntity, DownloadGroupTaskEntity> { 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 String mGroupName;
/**
* 是否已经设置了文件路径
*/
private boolean isSetDirPathed = false;
DownloadGroupTarget(List<String> urls, String targetName) { DownloadGroupTarget(List<String> urls, String targetName) {
this.mTargetName = targetName; this.mTargetName = targetName;
this.mUrls = urls; this.mUrls = urls;
mGroupName = CommonUtil.getMd5Code(urls); mGroupName = CommonUtil.getMd5Code(urls);
mTaskEntity = DbEntity.findData(DownloadGroupTaskEntity.class, "key=?", mGroupName); mTaskEntity = DbEntity.findFirst(DownloadGroupTaskEntity.class, "key=?", mGroupName);
if (mTaskEntity == null) { if (mTaskEntity == null) {
mTaskEntity = new DownloadGroupTaskEntity(); mTaskEntity = new DownloadGroupTaskEntity();
mTaskEntity.key = mGroupName; mTaskEntity.key = mGroupName;
@ -49,7 +60,7 @@ public class DownloadGroupTarget
private DownloadGroupEntity getDownloadGroupEntity() { private DownloadGroupEntity getDownloadGroupEntity() {
DownloadGroupEntity entity = DownloadGroupEntity entity =
DbEntity.findData(DownloadGroupEntity.class, "groupName=?", mGroupName); DbEntity.findFirst(DownloadGroupEntity.class, "groupName=?", mGroupName);
if (entity == null) { if (entity == null) {
entity = new DownloadGroupEntity(); entity = new DownloadGroupEntity();
} }
@ -58,7 +69,7 @@ public class DownloadGroupTarget
/** /**
* 设置任务组的文件夹路径在Aria中任务组的所有子任务都会下载到以任务组组名的文件夹中 * 设置任务组的文件夹路径在Aria中任务组的所有子任务都会下载到以任务组组名的文件夹中
* groupDirPath = "/mnt/sdcard/download/", groupName = "group_test" * groupDirPath = "/mnt/sdcard/download/group_test"
* <pre> * <pre>
* {@code * {@code
* + mnt * + mnt
@ -75,43 +86,86 @@ public class DownloadGroupTarget
* *
* @param groupDirPath 任务组保存文件夹路径 * @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; return this;
} }
/** /**
* 设置保存路径组 * 改变任务组文件夹路径
*
* @param newDirPath 新的文件夹路径
*/
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 setDownloadPaths(List<String> paths) { public DownloadGroupTarget setSubtaskFileName(List<String> subTaskFileName) {
CheckUtil.checkDownloadPaths(paths); if (subTaskFileName == null || subTaskFileName.isEmpty()) return this;
if (mUrls.size() != paths.size()) { mSubtaskFileName.addAll(subTaskFileName);
if (mUrls.size() != subTaskFileName.size()) {
throw new IllegalArgumentException("下载链接数必须要和保存路径的数量一致"); throw new IllegalArgumentException("下载链接数必须要和保存路径的数量一致");
} }
for (int i = 0, len = mUrls.size(); i < len; i++) { if (isSetDirPathed) {
mTaskEntity.getEntity().getChild().add(createDownloadEntity(mUrls.get(i), paths.get(i))); List<DownloadEntity> entities = mEntity.getSubTask();
int i = 0;
for (DownloadEntity entity : entities) {
entity.setFileName(mSubtaskFileName.get(i));
entity.update();
}
} }
return this; return this;
} }
/** /**
* 创建子任务下载实体 * 创建子任务
*
* @param url 下载地址
* @param path 保存路径
*/ */
private DownloadEntity createDownloadEntity(String url, String path) { private List<DownloadEntity> createSubTask() {
DownloadEntity entity = DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", url); List<DownloadEntity> list = new ArrayList<>();
if (entity == null) { for (int i = 0, len = mUrls.size(); i < len; i++) {
entity = new DownloadEntity(); DownloadEntity entity = new DownloadEntity();
} entity.setDownloadUrl(mUrls.get(i));
File file = new File(path); String fileName = mSubtaskFileName.get(i);
if (!file.exists()) { if (TextUtils.isEmpty(fileName)) {
entity.setState(IEntity.STATE_WAIT); fileName = CommonUtil.keyToHashKey(mUrls.get(i));
}
entity.setDownloadPath(mEntity.getDirPath() + "/" + fileName);
entity.setGroupName(mGroupName);
entity.save();
list.add(entity);
} }
entity.setDownloadPath(path); return list;
entity.setDownloadUrl(url);
entity.setFileName(file.getName());
return entity;
} }
} }

@ -18,15 +18,12 @@ package com.arialyy.aria.core.download;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.inf.AbsReceiver; 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.inf.IReceiver;
import com.arialyy.aria.core.command.normal.NormalCmdFactory; import com.arialyy.aria.core.command.normal.NormalCmdFactory;
import com.arialyy.aria.core.scheduler.DownloadSchedulers; import com.arialyy.aria.core.scheduler.DownloadSchedulers;
import com.arialyy.aria.core.scheduler.ISchedulerListener; import com.arialyy.aria.core.scheduler.ISchedulerListener;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import java.io.File;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -119,14 +116,14 @@ public class DownloadReceiver extends AbsReceiver<DownloadEntity> {
*/ */
public DownloadEntity getDownloadEntity(String downloadUrl) { public DownloadEntity getDownloadEntity(String downloadUrl) {
CheckUtil.checkDownloadUrl(downloadUrl); CheckUtil.checkDownloadUrl(downloadUrl);
return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl); return DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl);
} }
/** /**
* 下载任务是否存在 * 下载任务是否存在
*/ */
@Override public boolean taskExists(String 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() { @Override public List<DownloadEntity> getTaskList() {

@ -36,7 +36,7 @@ public class DownloadTarget
DownloadTarget(String url, String targetName) { DownloadTarget(String url, String targetName) {
mTargetName = targetName; mTargetName = targetName;
mTaskEntity = DbEntity.findData(DownloadTaskEntity.class, "key=?", url); mTaskEntity = DbEntity.findFirst(DownloadTaskEntity.class, "key=?", url);
if (mTaskEntity == null) { if (mTaskEntity == null) {
mTaskEntity = new DownloadTaskEntity(); mTaskEntity = new DownloadTaskEntity();
mTaskEntity.entity = new DownloadEntity(); mTaskEntity.entity = new DownloadEntity();
@ -54,7 +54,7 @@ public class DownloadTarget
*/ */
private DownloadEntity getEntity(String downloadUrl) { private DownloadEntity getEntity(String downloadUrl) {
DownloadEntity entity = DownloadEntity entity =
DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl); DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl);
if (entity == null) { if (entity == null) {
entity = new DownloadEntity(); entity = new DownloadEntity();
} }

@ -81,7 +81,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
mInfoPool = Executors.newCachedThreadPool(); mInfoPool = Executors.newCachedThreadPool();
mExePool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); mExePool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
for (DownloadEntity entity : mTaskEntity.entity.getChild()) { for (DownloadEntity entity : mTaskEntity.entity.getSubTask()) {
File file = new File(entity.getDownloadPath()); File file = new File(entity.getDownloadPath());
if (entity.isDownloadComplete() && file.exists()) { if (entity.isDownloadComplete() && file.exists()) {
mTotalSize += entity.getFileSize(); mTotalSize += entity.getFileSize();
@ -175,7 +175,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
startChildDownload(te); startChildDownload(te);
} }
mInitNum++; mInitNum++;
if (mInitNum + mFailNum == mTaskEntity.getEntity().getChild().size()) { if (mInitNum + mFailNum == mTaskEntity.getEntity().getSubTask().size()) {
startRunningFlow(); startRunningFlow();
} }
} }
@ -191,7 +191,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
if (failNum < 10) { if (failNum < 10) {
mInfoPool.execute(createFileInfoThread(te)); mInfoPool.execute(createFileInfoThread(te));
} }
if (mInitNum + mFailNum == mTaskEntity.getEntity().getChild().size()) { if (mInitNum + mFailNum == mTaskEntity.getEntity().getSubTask().size()) {
startRunningFlow(); startRunningFlow();
} }
} }
@ -238,7 +238,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
*/ */
private DownloadTaskEntity createDownloadTask(DownloadEntity entity) { private DownloadTaskEntity createDownloadTask(DownloadEntity entity) {
DownloadTaskEntity taskEntity = DownloadTaskEntity taskEntity =
DbEntity.findData(DownloadTaskEntity.class, "key=?", entity.getDownloadUrl()); DbEntity.findFirst(DownloadTaskEntity.class, "key=?", entity.getDownloadUrl());
if (taskEntity != null) { if (taskEntity != null) {
return taskEntity; return taskEntity;
} }

@ -192,7 +192,7 @@ class Downloader implements Runnable, IDownloadUtil {
CommonUtil.createFile(mConfigFile.getPath()); CommonUtil.createFile(mConfigFile.getPath());
} else if (!mTempFile.exists()) { } else if (!mTempFile.exists()) {
isNewTask = true; isNewTask = true;
} else if (DbEntity.findData(DownloadEntity.class, "downloadUrl=?", mEntity.getDownloadUrl()) } else if (DbEntity.findFirst(DownloadEntity.class, "downloadUrl=?", mEntity.getDownloadUrl())
== null) { == null) {
isNewTask = true; isNewTask = true;
} else { } else {

@ -17,21 +17,17 @@ package com.arialyy.aria.core.upload;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import com.arialyy.aria.core.AriaManager; 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.command.normal.NormalCmdFactory;
import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.inf.AbsReceiver; 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.inf.IReceiver;
import com.arialyy.aria.core.scheduler.ISchedulerListener; import com.arialyy.aria.core.scheduler.ISchedulerListener;
import com.arialyy.aria.core.scheduler.UploadSchedulers; import com.arialyy.aria.core.scheduler.UploadSchedulers;
import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern;
/** /**
* Created by lyy on 2017/2/6. * Created by lyy on 2017/2/6.
@ -56,14 +52,14 @@ public class UploadReceiver extends AbsReceiver<UploadEntity> {
*/ */
public UploadEntity getUploadEntity(String filePath) { public UploadEntity getUploadEntity(String filePath) {
CheckUtil.checkUploadPath(filePath); CheckUtil.checkUploadPath(filePath);
return DbEntity.findData(UploadEntity.class, "filePath=?", filePath); return DbEntity.findFirst(UploadEntity.class, "filePath=?", filePath);
} }
/** /**
* 下载任务是否存在 * 下载任务是否存在
*/ */
@Override public boolean taskExists(String 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() { @Override public List<UploadEntity> getTaskList() {

@ -28,7 +28,7 @@ public class UploadTarget extends AbsNormalTarget<UploadTarget, UploadEntity, Up
UploadTarget(String filePath, String targetName) { UploadTarget(String filePath, String targetName) {
this.mTargetName = targetName; this.mTargetName = targetName;
mTaskEntity = DbEntity.findData(UploadTaskEntity.class, "key=?", filePath); mTaskEntity = DbEntity.findFirst(UploadTaskEntity.class, "key=?", filePath);
if (mTaskEntity == null) { if (mTaskEntity == null) {
mTaskEntity = new UploadTaskEntity(); mTaskEntity = new UploadTaskEntity();
mTaskEntity.entity = new UploadEntity(); mTaskEntity.entity = new UploadEntity();
@ -40,7 +40,7 @@ public class UploadTarget extends AbsNormalTarget<UploadTarget, UploadEntity, Up
} }
private UploadEntity getUploadEntity(String filePath) { private UploadEntity getUploadEntity(String filePath) {
UploadEntity entity = UploadEntity.findData(UploadEntity.class, "filePath=?", filePath); UploadEntity entity = UploadEntity.findFirst(UploadEntity.class, "filePath=?", filePath);
if (entity == null) { if (entity == null) {
entity = new UploadEntity(); entity = new UploadEntity();
} }

@ -57,7 +57,7 @@ public class DbEntity {
/** /**
* 查询一组数据 * 查询一组数据
* <code> * <code>
* DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl); * DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl);
* </code> * </code>
* *
* @return 没有数据返回null * @return 没有数据返回null
@ -70,12 +70,12 @@ public class DbEntity {
/** /**
* 查询一行数据 * 查询一行数据
* <code> * <code>
* DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl); * DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl);
* </code> * </code>
* *
* @return 没有数据返回null * @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(); DbUtil util = DbUtil.getInstance();
List<T> datas = util.findData(clazz, expression); List<T> datas = util.findData(clazz, expression);
return datas == null ? null : datas.size() > 0 ? datas.get(0) : null; return datas == null ? null : datas.size() > 0 ? datas.get(0) : null;
@ -149,7 +149,7 @@ public class DbEntity {
updateRowID(); 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) { @NonNull String[] values) {
DbUtil util = DbUtil.getInstance(); DbUtil util = DbUtil.getInstance();
List<T> list = util.findData(clazz, wheres, values); List<T> list = util.findData(clazz, wheres, values);
@ -178,7 +178,7 @@ public class DbEntity {
values.add(field.get(this) + ""); 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()])); values.toArray(new String[values.size()]));
if (entity != null) { if (entity != null) {
rowID = entity.rowID; rowID = entity.rowID;

Loading…
Cancel
Save