数据库优化,代码逻辑优化

pull/330/head
laoyuyu 7 years ago
parent 2ef9550d86
commit 1e5f01cbd8
  1. 6
      Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
  2. 1
      Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java
  3. 2
      Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java
  4. 50
      Aria/src/main/java/com/arialyy/aria/core/download/BaseGroupTarget.java
  5. 3
      Aria/src/main/java/com/arialyy/aria/core/download/BaseNormalTarget.java
  6. 20
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java
  7. 20
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupEntity.java
  8. 2
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java
  9. 65
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java
  10. 1
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java
  11. 26
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTaskEntity.java
  12. 15
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
  13. 21
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java
  14. 5
      Aria/src/main/java/com/arialyy/aria/core/download/FtpDirDownloadTarget.java
  15. 95
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java
  16. 4
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java
  17. 6
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java
  18. 22
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirInfoThread.java
  19. 9
      Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGroupEntityWrapper.java
  20. 20
      Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGroupSubTaskEntityWrapper.java
  21. 60
      Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGroupTaskEntityWrapper.java
  22. 4
      Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DownloadTaskWrapper.java
  23. 6
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsEntity.java
  24. 4
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupEntity.java
  25. 6
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalEntity.java
  26. 2
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java
  27. 162
      Aria/src/main/java/com/arialyy/aria/core/manager/DGTEFactory.java
  28. 128
      Aria/src/main/java/com/arialyy/aria/core/manager/DGTEntityFactory.java
  29. 30
      Aria/src/main/java/com/arialyy/aria/core/manager/DTEFactory.java
  30. 20
      Aria/src/main/java/com/arialyy/aria/core/manager/IGTEFactory.java
  31. 2
      Aria/src/main/java/com/arialyy/aria/core/manager/INormalTEFactory.java
  32. 97
      Aria/src/main/java/com/arialyy/aria/core/manager/TEManager.java
  33. 14
      Aria/src/main/java/com/arialyy/aria/core/manager/UTEFactory.java
  34. 3
      Aria/src/main/java/com/arialyy/aria/core/upload/BaseNormalTarget.java
  35. 2
      Aria/src/main/java/com/arialyy/aria/core/upload/BaseUListener.java
  36. 67
      Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java
  37. 2
      Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java
  38. 85
      Aria/src/main/java/com/arialyy/aria/orm/DelegateUpdate.java
  39. 2
      Aria/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java
  40. 2
      Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java
  41. 7
      Aria/src/main/java/com/arialyy/aria/orm/annotation/Many.java
  42. 50
      Aria/src/main/java/com/arialyy/aria/util/ALog.java
  43. 4
      Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
  44. 4
      app/src/main/AndroidManifest.xml
  45. 1
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  46. 10
      app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java
  47. 8
      app/src/main/java/com/arialyy/simple/download/group/FTPDirDownloadActivity.java
  48. 2
      app/src/main/java/com/arialyy/simple/download/group/GroupModule.java
  49. 4
      app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java
  50. 92
      app/src/main/java/com/arialyy/simple/test/TestActivity.java
  51. 91
      app/src/main/java/com/arialyy/simple/test/TestGroupActivity.java
  52. 40
      app/src/main/res/layout/activity_test.xml
  53. 17
      aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java

@ -323,6 +323,7 @@ import org.xml.sax.SAXException;
* 根据功能类型和控件类型获取对应的key
*/
private String getKey(boolean isDownload, Object obj) {
// TODO: 2018/4/12 不做限制了,让所有类都可以使用
String clsName = obj.getClass().getName();
String key = "";
if (!(obj instanceof Activity)) {
@ -421,8 +422,8 @@ import org.xml.sax.SAXException;
private void regAppLifeCallback(Context context) {
Context app = context.getApplicationContext();
if (app instanceof Application) {
LifeCallback mLifeCallback = new LifeCallback();
((Application) app).registerActivityLifecycleCallbacks(mLifeCallback);
LifeCallback lifeCallback = new LifeCallback();
((Application) app).registerActivityLifecycleCallbacks(lifeCallback);
}
}
@ -493,6 +494,7 @@ import org.xml.sax.SAXException;
@Override public void onActivityDestroyed(Activity activity) {
destroySchedulerListener(activity);
// TODO: 2018/4/11 维护一个activity堆栈,应用被kill,activity会回调onDestroy方法,需要考虑server后台情况
}
}
}

@ -53,6 +53,7 @@ class StartCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
return;
}
String mod;
// TODO: 2018/4/12 配置文件不存在,是否会出现wait获取不到 ?
int maxTaskNum = mQueue.getMaxTaskNum();
AriaManager manager = AriaManager.getInstance(AriaManager.APP);
if (isDownloadCmd) {

@ -154,7 +154,7 @@ class BaseDListener<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity<
mEntity.setState(state);
mEntity.setComplete(state == IEntity.STATE_COMPLETE);
if (state == IEntity.STATE_CANCEL) {
mTaskEntity.deleteData();
mEntity.deleteData();
return;
} else if (mEntity.isComplete()) {
mEntity.setCompleteTime(System.currentTimeMillis());

@ -18,7 +18,6 @@ package com.arialyy.aria.core.download;
import android.text.TextUtils;
import com.arialyy.aria.core.manager.SubTaskManager;
import com.arialyy.aria.core.queue.DownloadGroupTaskQueue;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.ALog;
import java.io.File;
import java.util.List;
@ -36,7 +35,11 @@ abstract class BaseGroupTarget<TARGET extends BaseGroupTarget>
/**
* 文件夹临时路径
*/
protected String mDirPathTemp;
String mDirPathTemp;
/**
* 是否需要修改路径
*/
boolean needModifyPath = false;
private SubTaskManager mSubTaskManager;
@ -110,21 +113,21 @@ abstract class BaseGroupTarget<TARGET extends BaseGroupTarget>
*
* @param newDirPath 新的文件夹路径
*/
private void reChangeDirPath(String newDirPath) {
List<DownloadEntity> subTask = mEntity.getSubTask();
if (subTask != null && !subTask.isEmpty()) {
for (DownloadEntity entity : subTask) {
String oldPath = entity.getDownloadPath();
String newPath = newDirPath + "/" + entity.getFileName();
void reChangeDirPath(String newDirPath) {
List<DownloadTaskEntity> subTasks = mTaskEntity.getSubTaskEntities();
if (subTasks != null && !subTasks.isEmpty()) {
for (DownloadTaskEntity dte : subTasks) {
DownloadEntity de = dte.getEntity();
String oldPath = de.getDownloadPath();
String newPath = newDirPath + "/" + de.getFileName();
File file = new File(oldPath);
if (file.exists()) {
file.renameTo(new File(newPath));
DbEntity.exeSql("UPDATE DownloadEntity SET downloadPath='"
+ newPath
+ "' WHERE downloadPath='"
+ oldPath
+ "'");
DbEntity.exeSql(
"UPDATE DownloadTaskEntity SET key='" + newPath + "' WHERE key='" + oldPath + "'");
}
de.setDownloadPath(newPath);
dte.key = newPath;
de.save();
dte.save();
}
}
}
@ -135,26 +138,25 @@ abstract class BaseGroupTarget<TARGET extends BaseGroupTarget>
* @return {@code true} 合法
*/
boolean checkDirPath() {
final String dirPath = mDirPathTemp;
if (TextUtils.isEmpty(dirPath)) {
if (TextUtils.isEmpty(mDirPathTemp)) {
ALog.e(TAG, "文件夹路径不能为null");
return false;
} else if (!dirPath.startsWith("/")) {
ALog.e(TAG, "文件夹路径【" + dirPath + "】错误");
} else if (!mDirPathTemp.startsWith("/")) {
ALog.e(TAG, "文件夹路径【" + mDirPathTemp + "】错误");
return false;
}
File file = new File(dirPath);
File file = new File(mDirPathTemp);
if (file.isFile()) {
ALog.e(TAG, "路径【" + dirPath + "】是文件,请设置文件夹路径");
ALog.e(TAG, "路径【" + mDirPathTemp + "】是文件,请设置文件夹路径");
return false;
}
if (!mEntity.getDirPath().equals(dirPath)) {
if (TextUtils.isEmpty(mEntity.getDirPath()) || !mEntity.getDirPath().equals(mDirPathTemp)) {
if (!file.exists()) {
file.mkdirs();
}
mEntity.setDirPath(dirPath);
reChangeDirPath(dirPath);
needModifyPath = true;
mEntity.setDirPath(mDirPathTemp);
}
return true;

@ -108,7 +108,8 @@ abstract class BaseNormalTarget<TARGET extends BaseNormalTarget>
@Override protected boolean checkEntity() {
boolean b = getTargetType() < GROUP_HTTP && checkUrl() && checkFilePath();
if (b) {
mTaskEntity.save(mEntity);
mEntity.save();
mTaskEntity.save();
}
return b;
}

@ -21,6 +21,7 @@ import android.os.Parcelable;
import android.text.TextUtils;
import com.arialyy.aria.core.inf.AbsNormalEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.orm.ActionPolicy;
import com.arialyy.aria.orm.annotation.Foreign;
import com.arialyy.aria.orm.annotation.Primary;
import com.arialyy.aria.util.CommonUtil;
@ -30,28 +31,30 @@ import com.arialyy.aria.util.CommonUtil;
* 下载实体
*/
public class DownloadEntity extends AbsNormalEntity implements Parcelable {
@Primary private String downloadPath = ""; //保存路径
@Primary private String downloadPath; //保存路径
/**
* 所属任务组
*/
@Foreign(parent = DownloadGroupEntity.class, column = "groupName") private String groupName = "";
@Foreign(parent = DownloadGroupEntity.class, column = "groupName",
onUpdate = ActionPolicy.CASCADE, onDelete = ActionPolicy.CASCADE)
private String groupName;
/**
* 从服务器的返回信息中获取的文件md5信息如果服务器没有返回则不会设置该信息
* 如果你已经设置了该任务的MD5信息Aria也不会从服务器返回的信息中获取该信息
*/
private String md5Code = "";
private String md5Code;
/**
* 从服务器的返回信息中获取的文件描述信息
*/
private String disposition = "";
private String disposition;
/**
* 从disposition获取到的文件名如果可以获取到则会赋值到这个字段
*/
private String serverFileName = "";
private String serverFileName;
@Override public String getKey() {
return getUrl();
@ -96,13 +99,6 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable {
this.groupName = groupName;
}
/**
* {@link #getUrl()}
*/
@Deprecated public String getDownloadUrl() {
return getUrl();
}
public String getDownloadPath() {
return downloadPath;
}

@ -27,25 +27,21 @@ import java.util.List;
*/
public class DownloadGroupEntity extends AbsGroupEntity {
//@OneToMany(table = DownloadEntity.class, key = "groupName") private List<DownloadEntity> subtask =
// new ArrayList<>();
@Ignore
private List<DownloadEntity> subtask;
@Ignore private List<DownloadEntity> subEntities;
/**
* 任务组下载文件的文件夹地址
*
* @see DownloadGroupTarget#setDirPath(String)
*/
private String dirPath = "";
private String dirPath;
public List<DownloadEntity> getSubTask() {
return subtask;
public List<DownloadEntity> getSubEntities() {
return subEntities;
}
public void setSubTasks(List<DownloadEntity> subTasks) {
this.subtask = subTasks;
public void setSubEntities(List<DownloadEntity> subTasks) {
this.subEntities = subTasks;
}
public String getDirPath() {
@ -73,13 +69,13 @@ public class DownloadGroupEntity extends AbsGroupEntity {
@Override public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeTypedList(this.subtask);
dest.writeTypedList(this.subEntities);
dest.writeString(this.dirPath);
}
protected DownloadGroupEntity(Parcel in) {
super(in);
this.subtask = in.createTypedArrayList(DownloadEntity.CREATOR);
this.subEntities = in.createTypedArrayList(DownloadEntity.CREATOR);
this.dirPath = in.readString();
}

@ -86,7 +86,7 @@ class DownloadGroupListener
private void saveCurrentLocation() {
long location = 0;
for (DownloadEntity e : mEntity.getSubTask()) {
for (DownloadEntity e : mEntity.getSubEntities()) {
location += e.getCurrentProgress();
}
mEntity.setCurrentProgress(location);

@ -17,7 +17,6 @@ package com.arialyy.aria.core.download;
import android.text.TextUtils;
import com.arialyy.aria.core.manager.TEManager;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil;
import java.io.File;
@ -46,21 +45,23 @@ public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> {
if (groupEntity.getUrls() != null && !groupEntity.getUrls().isEmpty()) {
this.mUrls.addAll(groupEntity.getUrls());
}
mGroupName = CommonUtil.getMd5Code(groupEntity.getUrls());
mTaskEntity = TEManager.getInstance().getGTEntity(DownloadGroupTaskEntity.class, mUrls);
mEntity = mTaskEntity.getEntity();
if (mEntity != null) {
mDirPathTemp = mEntity.getDirPath();
}
init();
}
DownloadGroupTarget(List<String> urls, String targetName) {
this.mTargetName = targetName;
this.mUrls = urls;
mGroupName = CommonUtil.getMd5Code(urls);
init();
}
private void init() {
mGroupName = CommonUtil.getMd5Code(mUrls);
mTaskEntity = TEManager.getInstance().getGTEntity(DownloadGroupTaskEntity.class, mUrls);
mEntity = mTaskEntity.getEntity();
if (mEntity != null) {
mDirPathTemp = mEntity.getDirPath();
}
}
/**
@ -105,6 +106,11 @@ public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> {
*/
public DownloadGroupTarget setSubFileName(List<String> subTaskFileName) {
if (subTaskFileName == null || subTaskFileName.isEmpty()) {
ALog.e(TAG, "修改子任务的文件名失败:列表为null");
return this;
}
if (subTaskFileName.size() != mTaskEntity.getSubTaskEntities().size()) {
ALog.e(TAG, "修改子任务的文件名失败:子任务文件名列表数量和子任务的数量不匹配");
return this;
}
mSubNameTemp.clear();
@ -112,27 +118,6 @@ public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> {
return this;
}
/**
* 创建子任务
*/
private List<DownloadEntity> createSubTask() {
List<DownloadEntity> list = new ArrayList<>();
for (int i = 0, len = mUrls.size(); i < len; i++) {
DownloadEntity entity = new DownloadEntity();
entity.setUrl(mUrls.get(i));
String fileName =
mSubNameTemp.isEmpty() ? CommonUtil.createFileName(entity.getUrl()) : mSubNameTemp.get(i);
entity.setDownloadPath(mEntity.getDirPath() + "/" + fileName);
entity.setGroupName(mGroupName);
entity.setGroupChild(true);
entity.setFileName(fileName);
// TODO: 2018/4/3 是否需要在这个插入?
//entity.insert();
list.add(entity);
}
return list;
}
@Override protected int getTargetType() {
return GROUP_HTTP;
}
@ -151,10 +136,14 @@ public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> {
return false;
}
//文件夹路径通过后,并且该实体没有子任务,则创建子任务
if (mEntity.getSubTask() == null || mEntity.getSubTask().isEmpty()) {
mEntity.setSubTasks(createSubTask());
} else {
mEntity.save();
mTaskEntity.save();
if (needModifyPath) {
reChangeDirPath(mDirPathTemp);
}
if (!mSubNameTemp.isEmpty()) {
updateSingleSubFileName();
}
return true;
@ -166,7 +155,7 @@ public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> {
* 更新所有改动的子任务文件名
*/
private void updateSingleSubFileName() {
List<DownloadEntity> entities = mEntity.getSubTask();
List<DownloadEntity> entities = mEntity.getSubEntities();
int i = 0;
for (DownloadEntity entity : entities) {
if (i < mSubNameTemp.size()) {
@ -235,12 +224,10 @@ public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> {
oldFile.renameTo(new File(newPath));
}
CommonUtil.renameDownloadConfig(oldFile.getName(), newName);
DbEntity.exeSql(
"UPDATE DownloadTaskEntity SET key='" + newPath + "' WHERE key='" + oldPath + "'");
// TODO: 2018/4/11 需要校验数据库级联操作是否生效
entity.setDownloadPath(newPath);
entity.setFileName(newName);
// TODO: 2018/4/3 是否需要在这个更新?
//entity.update();
entity.update();
}
}

@ -116,7 +116,6 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity> {
public DownloadGroupTask build() {
DownloadGroupTask task = new DownloadGroupTask(taskEntity, outHandler);
task.setTargetName(targetName);
taskEntity.save();
return task;
}
}

@ -16,18 +16,25 @@
package com.arialyy.aria.core.download;
import com.arialyy.aria.core.inf.AbsGroupTaskEntity;
import com.arialyy.aria.orm.ActionPolicy;
import com.arialyy.aria.orm.annotation.Foreign;
import com.arialyy.aria.orm.annotation.Ignore;
import com.arialyy.aria.orm.annotation.Primary;
import java.util.List;
/**
* Created by AriaL on 2017/7/1.
* 任务组的任务实体
*/
public class DownloadGroupTaskEntity extends AbsGroupTaskEntity<DownloadGroupEntity> {
@Ignore private DownloadGroupEntity entity;
@Primary @Foreign(parent = DownloadGroupEntity.class, column = "groupName")
@Ignore private List<DownloadTaskEntity> subTaskEntities;
@Primary
@Foreign(parent = DownloadGroupEntity.class, column = "groupName",
onUpdate = ActionPolicy.CASCADE, onDelete = ActionPolicy.CASCADE)
private String key;
@Override public DownloadGroupEntity getEntity() {
@ -38,6 +45,14 @@ public class DownloadGroupTaskEntity extends AbsGroupTaskEntity<DownloadGroupEnt
this.entity = entity;
}
public List<DownloadTaskEntity> getSubTaskEntities() {
return subTaskEntities;
}
public void setSubTaskEntities(List<DownloadTaskEntity> subTaskEntities) {
this.subTaskEntities = subTaskEntities;
}
@Override public String getKey() {
return key;
}
@ -45,13 +60,4 @@ public class DownloadGroupTaskEntity extends AbsGroupTaskEntity<DownloadGroupEnt
public void setKey(String key) {
this.key = key;
}
public void save(DownloadGroupEntity groupEntity) {
if (groupEntity != null) {
key = groupEntity.getKey();
entity = groupEntity;
groupEntity.save();
}
save();
}
}

@ -22,6 +22,7 @@ import com.arialyy.aria.core.command.ICmd;
import com.arialyy.aria.core.command.normal.CancelAllCmd;
import com.arialyy.aria.core.command.normal.NormalCmdFactory;
import com.arialyy.aria.core.common.ProxyHelper;
import com.arialyy.aria.core.download.wrapper.DGroupEntityWrapper;
import com.arialyy.aria.core.inf.AbsEntity;
import com.arialyy.aria.core.inf.AbsReceiver;
import com.arialyy.aria.core.inf.AbsTarget;
@ -299,15 +300,15 @@ public class DownloadReceiver extends AbsReceiver {
/**
* 获取FTP文件夹下载任务实体
*
* @param dirPath FTP文件夹本地保存路径
* @param dirUrl FTP文件夹本地下载路径
* @return 返回对应的任务组实体如果查找不到对应的数据或路径为null返回null
*/
public DownloadGroupTaskEntity getFtpDirTask(String dirPath) {
if (TextUtils.isEmpty(dirPath)) {
ALog.e(TAG, "获取FTP文件夹实体失败:文件夹路径为null");
public DownloadGroupTaskEntity getFtpDirTask(String dirUrl) {
if (TextUtils.isEmpty(dirUrl)) {
ALog.e(TAG, "获取FTP文件夹实体失败:下载路径为null");
return null;
}
return TEManager.getInstance().getTEntity(DownloadGroupTaskEntity.class, dirPath);
return TEManager.getInstance().getFDTEntity(DownloadGroupTaskEntity.class, dirUrl);
}
/**
@ -349,12 +350,12 @@ public class DownloadReceiver extends AbsReceiver {
* @return 如果没有任务组列表则返回null
*/
public List<DownloadGroupEntity> getGroupTaskList() {
List<DownloadGroupWrapper> wrappers = DbEntity.findRelationData(DownloadGroupWrapper.class);
List<DGroupEntityWrapper> wrappers = DbEntity.findRelationData(DGroupEntityWrapper.class);
if (wrappers == null || wrappers.isEmpty()) {
return null;
}
List<DownloadGroupEntity> entities = new ArrayList<>();
for (DownloadGroupWrapper wrapper : wrappers) {
for (DGroupEntityWrapper wrapper : wrappers) {
entities.add(wrapper.groupEntity);
}
return entities;

@ -16,6 +16,7 @@
package com.arialyy.aria.core.download;
import com.arialyy.aria.core.inf.AbsNormalTaskEntity;
import com.arialyy.aria.orm.ActionPolicy;
import com.arialyy.aria.orm.annotation.Foreign;
import com.arialyy.aria.orm.annotation.Ignore;
import com.arialyy.aria.orm.annotation.NoNull;
@ -27,7 +28,6 @@ import com.arialyy.aria.orm.annotation.Primary;
*/
public class DownloadTaskEntity extends AbsNormalTaskEntity<DownloadEntity> {
//@OneToOne(table = DownloadEntity.class, key = "downloadPath") public DownloadEntity entity;
@Ignore public DownloadEntity entity;
/**
@ -38,7 +38,9 @@ public class DownloadTaskEntity extends AbsNormalTaskEntity<DownloadEntity> {
/**
* 所属的任务组组名如果不属于任务组则为null
*/
public String groupName = "";
@Foreign(parent = DownloadGroupTaskEntity.class, column = "key",
onUpdate = ActionPolicy.CASCADE, onDelete = ActionPolicy.CASCADE)
public String groupName;
/**
* 是否是chunk模式
@ -53,7 +55,10 @@ public class DownloadTaskEntity extends AbsNormalTaskEntity<DownloadEntity> {
/**
* Task实体对应的key
*/
@Primary @Foreign(parent = DownloadEntity.class, column = "downloadPath") public String key = "";
@Primary
@Foreign(parent = DownloadEntity.class, column = "downloadPath",
onUpdate = ActionPolicy.CASCADE, onDelete = ActionPolicy.CASCADE)
public String key;
public DownloadTaskEntity() {
}
@ -65,14 +70,4 @@ public class DownloadTaskEntity extends AbsNormalTaskEntity<DownloadEntity> {
@Override public String getKey() {
return key;
}
public void save(DownloadEntity entity) {
this.entity = entity;
if (entity != null) {
url = entity.getUrl();
key = entity.getDownloadPath();
entity.save();
}
save();
}
}

@ -37,7 +37,7 @@ public class FtpDirDownloadTarget extends BaseGroupTarget<FtpDirDownloadTarget>
private void init(String key) {
mGroupName = key;
mTaskEntity = TEManager.getInstance().getTEntity(DownloadGroupTaskEntity.class, key);
mTaskEntity = TEManager.getInstance().getFDTEntity(DownloadGroupTaskEntity.class, key);
mTaskEntity.requestType = AbsTaskEntity.D_FTP_DIR;
mEntity = mTaskEntity.getEntity();
if (mEntity != null) {
@ -53,7 +53,8 @@ public class FtpDirDownloadTarget extends BaseGroupTarget<FtpDirDownloadTarget>
@Override protected boolean checkEntity() {
boolean b = getTargetType() == GROUP_FTP_DIR && checkDirPath() && checkUrl();
if (b) {
mTaskEntity.save(mEntity);
mEntity.save();
mTaskEntity.save();
}
return b;
}

@ -16,20 +16,17 @@
package com.arialyy.aria.core.download.downloader;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.FtpUrlEntity;
import com.arialyy.aria.core.common.IUtil;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.inf.IDownloadListener;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.util.NetUtils;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
@ -73,14 +70,15 @@ public abstract class AbsGroupUtil implements IUtil {
Map<String, DownloadTaskEntity> mFailMap = new HashMap<>();
/**
* 下载器映射表key为下载地址
* 该任务组对应的所有任务
*/
private Map<String, Downloader> mDownloaderMap = new HashMap<>();
private Map<String, DownloadTaskEntity> mTasksMap = new HashMap<>();
/**
* 该任务组对应的所有任务
* 下载器映射表key为下载地址
*/
private Map<String, DownloadTaskEntity> mTasksMap = new HashMap<>();
private Map<String, Downloader> mDownloaderMap = new HashMap<>();
/**
* 是否需要读取文件长度{@code true}需要
*/
@ -99,37 +97,31 @@ public abstract class AbsGroupUtil implements IUtil {
int mInitFailNum = 0;
//任务组大小
int mGroupSize = 0;
long mUpdateInterval = 1000;
private long mUpdateInterval = 1000;
AbsGroupUtil(IDownloadGroupListener listener, DownloadGroupTaskEntity groupEntity) {
mListener = listener;
mGTEntity = groupEntity;
mExePool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
List<DownloadTaskEntity> tasks =
DbEntity.findDatas(DownloadTaskEntity.class, "groupName=?", mGTEntity.getKey());
if (tasks != null && !tasks.isEmpty()) {
for (DownloadTaskEntity te : tasks) {
te.removeFile = mGTEntity.removeFile;
if (te.getEntity() == null) continue;
mTasksMap.put(te.getEntity().getUrl(), te);
}
}
mGroupSize = mGTEntity.getEntity().getSubTask().size();
mGroupSize = mGTEntity.getSubTaskEntities().size();
mTotalLen = groupEntity.getEntity().getFileSize();
isNeedLoadFileSize = mTotalLen <= 1;
for (DownloadEntity entity : mGTEntity.getEntity().getSubTask()) {
File file = new File(entity.getDownloadPath());
if (entity.getState() == IEntity.STATE_COMPLETE && file.exists()) {
for (DownloadTaskEntity te : mGTEntity.getSubTaskEntities()) {
File file = new File(te.getKey());
if (te.getState() == IEntity.STATE_COMPLETE && file.exists()) {
mCompleteNum++;
mCurrentLocation += entity.getFileSize();
mCurrentLocation += te.getEntity().getFileSize();
} else {
mExeMap.put(entity.getUrl(), createChildDownloadTask(entity));
mCurrentLocation += file.exists() ? entity.getCurrentProgress() : 0;
mExeMap.put(te.url, te);
mCurrentLocation += file.exists() ? te.getEntity().getCurrentProgress() : 0;
mActualTaskNum++;
}
if (isNeedLoadFileSize) {
mTotalLen += entity.getFileSize();
mTotalLen += te.getEntity().getFileSize();
}
mTasksMap.put(te.url, te);
}
updateFileSize();
mUpdateInterval =
@ -190,12 +182,9 @@ public abstract class AbsGroupUtil implements IUtil {
* @param url 子任务下载地址
*/
public void cancelSubTask(String url) {
List<String> urls = mGTEntity.getEntity().getUrls();
if (urls != null && !urls.isEmpty() && urls.contains(url)) {
urls.remove(url);
// TODO: 2018/4/4 有问题
DownloadTaskEntity det =
DbEntity.findFirst(DownloadTaskEntity.class, "url=? and isGroupTask='true'", url);
Set<String> urls = mTasksMap.keySet();
if (!urls.isEmpty() && urls.contains(url)) {
DownloadTaskEntity det = mTasksMap.get(url);
if (det != null) {
mTotalLen -= det.getEntity().getFileSize();
mGroupSize--;
@ -275,7 +264,6 @@ public abstract class AbsGroupUtil implements IUtil {
}
}
CommonUtil.delDownloadGroupTaskConfig(mGTEntity.removeFile, mGTEntity.getEntity());
mGTEntity.deleteData();
}
public void onCancel() {
@ -306,8 +294,12 @@ public abstract class AbsGroupUtil implements IUtil {
isRunning = true;
mFailNum = 0;
mListener.onPre();
if (mCompleteNum == mGroupSize) {
mListener.onComplete();
} else {
onStart();
}
}
protected void onStart() {
@ -377,43 +369,6 @@ public abstract class AbsGroupUtil implements IUtil {
return dt;
}
/**
* 创建子任务下载信息
*/
DownloadTaskEntity createChildDownloadTask(DownloadEntity entity) {
DownloadTaskEntity taskEntity = mTasksMap.get(entity.getUrl());
if (taskEntity != null) {
taskEntity.entity = entity;
if (getTaskType() == FTP_DIR) {
taskEntity.urlEntity = createFtpUrlEntity(entity);
}
mTasksMap.put(entity.getUrl(), taskEntity);
return taskEntity;
}
taskEntity = new DownloadTaskEntity();
taskEntity.entity = entity;
taskEntity.headers = mGTEntity.headers;
taskEntity.requestEnum = mGTEntity.requestEnum;
taskEntity.removeFile = mGTEntity.removeFile;
taskEntity.groupName = mGTEntity.getKey();
taskEntity.isGroupTask = true;
taskEntity.requestType = mGTEntity.requestType;
taskEntity.key = entity.getDownloadPath();
if (getTaskType() == FTP_DIR) {
taskEntity.urlEntity = createFtpUrlEntity(entity);
}
taskEntity.save();
mTasksMap.put(entity.getUrl(), taskEntity);
return taskEntity;
}
private FtpUrlEntity createFtpUrlEntity(DownloadEntity entity) {
FtpUrlEntity urlEntity = mGTEntity.urlEntity.clone();
urlEntity.url = entity.getUrl();
urlEntity.remotePath = CommonUtil.getRemotePath(entity.getUrl());
return urlEntity;
}
/**
* 子任务事件监听
*/
@ -554,7 +509,7 @@ public abstract class AbsGroupUtil implements IUtil {
subEntity.setState(state);
subEntity.setComplete(state == IEntity.STATE_COMPLETE);
if (state == IEntity.STATE_CANCEL) {
subTaskEntity.deleteData();
subEntity.deleteData();
return;
} else if (subEntity.isComplete()) {
subEntity.setCompleteTime(System.currentTimeMillis());

@ -109,7 +109,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil {
createChildDownload(te);
}
mInitNum++;
if (mInitNum + mInitFailNum >= mGTEntity.getEntity().getSubTask().size()
if (mInitNum + mInitFailNum >= mGTEntity.getEntity().getSubEntities().size()
|| !isNeedLoadFileSize) {
startRunningFlow();
updateFileSize();
@ -132,7 +132,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil {
if (mActualTaskNum < 0) mActualTaskNum = 0;
}
failNum++;
if (mInitNum + mInitFailNum >= mGTEntity.getEntity().getSubTask().size()
if (mInitNum + mInitFailNum >= mGTEntity.getEntity().getSubEntities().size()
|| !isNeedLoadFileSize) {
startRunningFlow();
updateFileSize();

@ -20,6 +20,7 @@ import com.arialyy.aria.core.common.OnFileInfoCallback;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.manager.TEManager;
import com.arialyy.aria.util.ErrorHelp;
import java.util.Set;
@ -44,10 +45,7 @@ public class FtpDirDownloadUtil extends AbsGroupUtil {
new FtpDirInfoThread(mGTEntity, new OnFileInfoCallback() {
@Override public void onComplete(String url, CompleteInfo info) {
if (info.code >= 200 && info.code < 300) {
for (DownloadEntity entity : mGTEntity.getEntity().getSubTask()) {
mExeMap.put(entity.getUrl(), createChildDownloadTask(entity));
}
mActualTaskNum = mGTEntity.getEntity().getSubTask().size();
mActualTaskNum = mGTEntity.getEntity().getSubEntities().size();
mGroupSize = mActualTaskNum;
mTotalLen = mGTEntity.getEntity().getFileSize();
startDownload();

@ -22,6 +22,7 @@ import com.arialyy.aria.core.common.OnFileInfoCallback;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.util.CommonUtil;
import java.nio.charset.Charset;
import java.util.ArrayList;
@ -52,10 +53,14 @@ class FtpDirInfoThread extends AbsFtpInfoThread<DownloadGroupEntity, DownloadGro
mCallback.onComplete(mEntity.getKey(), new CompleteInfo(code));
}
/**
* FTP文件夹的子任务实体 在这生成
*/
private void addEntity(String remotePath, FTPFile ftpFile) {
final FtpUrlEntity urlEntity = mTaskEntity.urlEntity;
DownloadEntity entity = new DownloadEntity();
entity.setUrl(urlEntity.protocol + "://" + urlEntity.hostName + ":" + urlEntity.port + remotePath);
entity.setUrl(
urlEntity.protocol + "://" + urlEntity.hostName + ":" + urlEntity.port + remotePath);
entity.setDownloadPath(mEntity.getDirPath() + "/" + remotePath);
int lastIndex = remotePath.lastIndexOf("/");
String fileName = lastIndex < 0 ? CommonUtil.keyToHashKey(remotePath)
@ -65,12 +70,21 @@ class FtpDirInfoThread extends AbsFtpInfoThread<DownloadGroupEntity, DownloadGro
entity.setGroupChild(true);
entity.setFileSize(ftpFile.getSize());
entity.insert();
DownloadTaskEntity taskEntity = new DownloadTaskEntity();
taskEntity.key = entity.getDownloadPath();
taskEntity.url = entity.getUrl();
taskEntity.isGroupTask = false;
taskEntity.entity = entity;
taskEntity.insert();
if (mEntity.getUrls() == null) {
mEntity.setUrls(new ArrayList<String>());
}
if (mEntity.getSubTask() == null) {
mEntity.setSubTasks(new ArrayList<DownloadEntity>());
if (mEntity.getSubEntities() == null) {
mEntity.setSubEntities(new ArrayList<DownloadEntity>());
}
mEntity.getSubTask().add(entity);
mEntity.getSubEntities().add(entity);
mTaskEntity.getSubTaskEntities().add(taskEntity);
}
}

@ -13,8 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.download;
package com.arialyy.aria.core.download.wrapper;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.orm.AbsWrapper;
import com.arialyy.aria.orm.annotation.Many;
import com.arialyy.aria.orm.annotation.One;
@ -23,9 +25,10 @@ import java.util.List;
/**
* Created by laoyuyu on 2018/3/30.
* 任务组实体和子任务实体的关系
*/
@Wrapper
public class DownloadGroupWrapper extends AbsWrapper {
public class DGroupEntityWrapper extends AbsWrapper {
@One
public DownloadGroupEntity groupEntity;
@ -35,7 +38,7 @@ public class DownloadGroupWrapper extends AbsWrapper {
@Override protected void handleConvert() {
if (subEntity != null && !subEntity.isEmpty()) {
groupEntity.setSubTasks(subEntity);
groupEntity.setSubEntities(subEntity);
}
}
}

@ -13,8 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.download;
package com.arialyy.aria.core.download.wrapper;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.orm.AbsWrapper;
import com.arialyy.aria.orm.annotation.Many;
import com.arialyy.aria.orm.annotation.One;
@ -22,23 +24,21 @@ import com.arialyy.aria.orm.annotation.Wrapper;
import java.util.List;
/**
* Created by laoyuyu on 2018/3/30.
* Created by laoyuyu on 2018/4/11.
* 任务组任务实体和任务组任务实体的子任务实体对应关系
*/
@Wrapper
public class DownloadGroupTaskWrapper extends AbsWrapper {
public class DGroupSubTaskEntityWrapper extends AbsWrapper {
@One
public DownloadGroupEntity entity;
public DownloadGroupTaskEntity dgTaskEntity;
@Many(parentColumn = "key", entityColumn = "groupName")
private List<DownloadGroupTaskEntity> taskEntitys;
public DownloadGroupTaskEntity taskEntity;
public List<DownloadTaskEntity> subTaskEntity;
@Override protected void handleConvert() {
taskEntity = (taskEntitys == null || taskEntitys.isEmpty()) ? null : taskEntitys.get(0);
if (taskEntity != null) {
taskEntity.setEntity(entity);
if (subTaskEntity != null && !subTaskEntity.isEmpty()) {
dgTaskEntity.setSubTaskEntities(subTaskEntity);
}
}
}

@ -0,0 +1,60 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.download.wrapper;
import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.orm.AbsWrapper;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.orm.annotation.Many;
import com.arialyy.aria.orm.annotation.One;
import com.arialyy.aria.orm.annotation.Wrapper;
import java.util.ArrayList;
import java.util.List;
/**
* Created by laoyuyu on 2018/3/30.
* 任务组实体和任务组任务实体的关系
*/
@Wrapper
public class DGroupTaskEntityWrapper extends AbsWrapper {
@One
public DownloadGroupEntity entity;
@Many(parentColumn = "groupName", entityColumn = "key")
private List<DownloadGroupTaskEntity> taskEntitys;
public DownloadGroupTaskEntity taskEntity;
@Override protected void handleConvert() {
taskEntity = (taskEntitys == null || taskEntitys.isEmpty()) ? null : taskEntitys.get(0);
if (taskEntity != null) {
taskEntity.setEntity(entity);
List<DownloadTaskWrapper> subWrappers =
DbEntity.findRelationData(DownloadTaskWrapper.class, "DownloadTaskEntity.groupName=?",
taskEntity.getKey());
if (subWrappers != null && !subWrappers.isEmpty()) {
List<DownloadTaskEntity> temp = new ArrayList<>();
for (DownloadTaskWrapper dw : subWrappers) {
temp.add(dw.taskEntity);
}
taskEntity.setSubTaskEntities(temp);
}
}
}
}

@ -13,8 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.download;
package com.arialyy.aria.core.download.wrapper;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.orm.AbsWrapper;
import com.arialyy.aria.orm.annotation.Many;
import com.arialyy.aria.orm.annotation.One;

@ -31,7 +31,7 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
/**
* 单位转换后的速度
*/
@Ignore private String convertSpeed = "";
@Ignore private String convertSpeed;
/**
* 下载失败计数每次开始都重置为0
*/
@ -40,7 +40,7 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
/**
* 扩展字段
*/
private String str = "";
private String str;
/**
* 文件大小
*/
@ -48,7 +48,7 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
/**
* 转换后的文件大小
*/
private String convertFileSize = "";
private String convertFileSize;
private int state = STATE_WAIT;
/**

@ -28,12 +28,12 @@ public abstract class AbsGroupEntity extends AbsEntity implements Parcelable {
/**
* 组名组名为任务地址相加的url的Md5
*/
@Primary protected String groupName = "";
@Primary protected String groupName;
/**
* 任务组别名
*/
private String alias = "";
private String alias;
private List<String> urls = new ArrayList<>();

@ -26,12 +26,12 @@ public abstract class AbsNormalEntity extends AbsEntity implements Parcelable {
/**
* 服务器地址
*/
private String url = "";
private String url;
/**
* 文件名
*/
private String fileName = "";
private String fileName;
/**
* 是否是任务组里面的下载实体
@ -39,7 +39,7 @@ public abstract class AbsNormalEntity extends AbsEntity implements Parcelable {
private boolean isGroupChild = false;
private boolean isRedirect = false; //是否重定向
private String redirectUrl = ""; //重定向链接
private String redirectUrl; //重定向链接
public String getUrl() {
return url;

@ -64,7 +64,7 @@ public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEnti
ALog.d("AbsTarget", "任务正在下载,即将删除任务");
cancel();
} else {
mTaskEntity.deleteData();
mEntity.deleteData();
TEManager.getInstance().removeTEntity(mEntity.getKey());
}
}

@ -0,0 +1,162 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.manager;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.download.wrapper.DGroupEntityWrapper;
import com.arialyy.aria.core.download.wrapper.DGroupTaskEntityWrapper;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CommonUtil;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Aria.Lao on 2017/11/1.
* 任务实体工厂
*/
class DGTEFactory implements IGTEFactory<DownloadGroupEntity, DownloadGroupTaskEntity> {
private static final String TAG = "DTEFactory";
private static volatile DGTEFactory INSTANCE = null;
private DGTEFactory() {
}
public static DGTEFactory getInstance() {
if (INSTANCE == null) {
synchronized (DGTEFactory.class) {
INSTANCE = new DGTEFactory();
}
}
return INSTANCE;
}
@Override public DownloadGroupTaskEntity getGTE(String groupName, List<String> urls) {
DownloadGroupEntity entity = createDGroupEntity(groupName, urls);
List<DGroupTaskEntityWrapper> wrapper =
DbEntity.findRelationData(DGroupTaskEntityWrapper.class, "DownloadGroupTaskEntity.key=?",
entity.getGroupName());
DownloadGroupTaskEntity gte;
if (wrapper != null && !wrapper.isEmpty()) {
gte = wrapper.get(0).taskEntity;
if (gte == null) {
// 创建新的任务组任务实体
gte = new DownloadGroupTaskEntity();
//创建子任务的任务实体
gte.setSubTaskEntities(createDGSubTaskEntity(entity));
} else if (gte.getSubTaskEntities() == null || gte.getSubTaskEntities().isEmpty()) {
gte.setSubTaskEntities(createDGSubTaskEntity(entity));
}
} else {
gte = new DownloadGroupTaskEntity();
gte.setSubTaskEntities(createDGSubTaskEntity(entity));
}
gte.setKey(entity.getGroupName());
gte.setEntity(entity);
return gte;
}
@Override public DownloadGroupTaskEntity getFTE(String ftpUrl) {
List<DGroupTaskEntityWrapper> wrapper =
DbEntity.findRelationData(DGroupTaskEntityWrapper.class, "DownloadGroupTaskEntity.key=?",
ftpUrl);
DownloadGroupTaskEntity fte;
if (wrapper != null && !wrapper.isEmpty()) {
fte = wrapper.get(0).taskEntity;
if (fte == null) {
fte = new DownloadGroupTaskEntity();
DownloadGroupEntity dge = new DownloadGroupEntity();
dge.setGroupName(ftpUrl);
fte.setEntity(dge);
} else if (fte.getEntity() == null) {
DownloadGroupEntity dge = new DownloadGroupEntity();
dge.setGroupName(ftpUrl);
fte.setEntity(dge);
}
} else {
fte = new DownloadGroupTaskEntity();
DownloadGroupEntity dge = new DownloadGroupEntity();
dge.setGroupName(ftpUrl);
fte.setEntity(dge);
}
fte.setKey(ftpUrl);
fte.urlEntity = CommonUtil.getFtpUrlInfo(ftpUrl);
return fte;
}
/**
* 创建任务组子任务的任务实体
*/
private List<DownloadTaskEntity> createDGSubTaskEntity(DownloadGroupEntity dge) {
List<DownloadTaskEntity> list = new ArrayList<>();
for (DownloadEntity entity : dge.getSubEntities()) {
DownloadTaskEntity taskEntity = new DownloadTaskEntity();
taskEntity.entity = entity;
taskEntity.key = entity.getDownloadPath();
taskEntity.groupName = dge.getKey();
taskEntity.isGroupTask = true;
taskEntity.url = entity.getUrl();
list.add(taskEntity);
}
return list;
}
/**
* 查询任务组实体如果数据库不存在该实体则新创建一个新的任务组实体
*/
private DownloadGroupEntity createDGroupEntity(String groupName, List<String> urls) {
List<DGroupEntityWrapper> wrapper =
DbEntity.findRelationData(DGroupEntityWrapper.class, "DownloadGroupEntity.groupName=?",
groupName);
DownloadGroupEntity groupEntity;
if (wrapper != null && !wrapper.isEmpty()) {
groupEntity = wrapper.get(0).groupEntity;
if (groupEntity == null) {
groupEntity = new DownloadGroupEntity();
groupEntity.setSubEntities(createSubTask(groupName, urls));
}
} else {
groupEntity = new DownloadGroupEntity();
groupEntity.setSubEntities(createSubTask(groupName, urls));
}
groupEntity.setGroupName(groupName);
groupEntity.setUrls(urls);
return groupEntity;
}
/**
* 创建子任务
*/
private List<DownloadEntity> createSubTask(String groupName, List<String> urls) {
List<DownloadEntity> list = new ArrayList<>();
for (int i = 0, len = urls.size(); i < len; i++) {
DownloadEntity entity = new DownloadEntity();
entity.setUrl(urls.get(i));
entity.setDownloadPath(groupName + "_" + i);
entity.setFileName(groupName + "_" + i);
entity.setGroupName(groupName);
entity.setGroupChild(true);
list.add(entity);
}
return list;
}
}

@ -1,128 +0,0 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.manager;
import android.text.TextUtils;
import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
import com.arialyy.aria.core.download.DownloadGroupTaskWrapper;
import com.arialyy.aria.core.download.DownloadGroupWrapper;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CommonUtil;
import java.util.List;
/**
* Created by Aria.Lao on 2017/11/1.
* 任务实体工厂
*/
class DGTEntityFactory implements ITEntityFactory<DownloadGroupEntity, DownloadGroupTaskEntity>,
IGTEntityFactory<DownloadGroupEntity, DownloadGroupTaskEntity> {
private static final String TAG = "DTEntityFactory";
private static volatile DGTEntityFactory INSTANCE = null;
private DGTEntityFactory() {
}
public static DGTEntityFactory getInstance() {
if (INSTANCE == null) {
synchronized (DGTEntityFactory.class) {
INSTANCE = new DGTEntityFactory();
}
}
return INSTANCE;
}
/**
* 通过下载实体创建任务实体
*/
private DownloadGroupTaskEntity create(DownloadGroupEntity entity) {
List<DownloadGroupTaskWrapper> wrapper =
DbEntity.findRelationData(DownloadGroupTaskWrapper.class, "DownloadGroupTaskWrapper.key=?",
entity.getGroupName());
DownloadGroupTaskEntity dgTaskEntity;
if (wrapper != null && !wrapper.isEmpty()) {
dgTaskEntity = wrapper.get(0).taskEntity;
if (dgTaskEntity == null) {
dgTaskEntity = new DownloadGroupTaskEntity();
dgTaskEntity.setEntity(entity);
} else if (dgTaskEntity.getEntity() == null || TextUtils.isEmpty(
dgTaskEntity.getEntity().getKey())) {
dgTaskEntity.setEntity(entity);
}
} else {
dgTaskEntity = new DownloadGroupTaskEntity();
dgTaskEntity.setEntity(entity);
}
return dgTaskEntity;
}
/**
* 对于任务组不能使用这个该方法只用于FTP文件夹下载
*
* @deprecated 任务组使用{@link #create(String, List)}
*/
@Override @Deprecated public DownloadGroupTaskEntity create(String key) {
List<DownloadGroupTaskWrapper> wrapper =
DbEntity.findRelationData(DownloadGroupTaskWrapper.class, "DownloadGroupTaskWrapper.key=?",
key);
DownloadGroupTaskEntity dgTaskEntity;
if (wrapper != null && !wrapper.isEmpty()) {
dgTaskEntity = wrapper.get(0).taskEntity;
if (dgTaskEntity == null) {
dgTaskEntity = new DownloadGroupTaskEntity();
dgTaskEntity.setEntity(getDownloadGroupEntity(key, null));
} else if (dgTaskEntity.getEntity() == null || TextUtils.isEmpty(
dgTaskEntity.getEntity().getKey())) {
dgTaskEntity.setEntity(getDownloadGroupEntity(key, null));
}
dgTaskEntity.urlEntity = CommonUtil.getFtpUrlInfo(key);
return dgTaskEntity;
} else {
dgTaskEntity = new DownloadGroupTaskEntity();
dgTaskEntity.setEntity(getDownloadGroupEntity(key, null));
dgTaskEntity.urlEntity = CommonUtil.getFtpUrlInfo(key);
return dgTaskEntity;
}
}
@Override public DownloadGroupTaskEntity create(String groupName, List<String> urls) {
return create(getDownloadGroupEntity(groupName, urls));
}
/**
* 查询任务组实体如果数据库不存在该实体则新创建一个新的任务组实体
*/
private DownloadGroupEntity getDownloadGroupEntity(String groupName, List<String> urls) {
List<DownloadGroupWrapper> wrapper =
DbEntity.findRelationData(DownloadGroupWrapper.class, "DownloadGroupEntity.groupName=?",
groupName);
DownloadGroupEntity groupEntity;
if (wrapper != null && !wrapper.isEmpty()) {
groupEntity = wrapper.get(0).groupEntity;
if (groupEntity == null) {
groupEntity = new DownloadGroupEntity();
}
} else {
groupEntity = new DownloadGroupEntity();
}
groupEntity.setGroupName(groupName);
groupEntity.setUrls(urls);
return groupEntity;
}
}

@ -18,7 +18,7 @@ package com.arialyy.aria.core.manager;
import android.text.TextUtils;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.download.DownloadTaskWrapper;
import com.arialyy.aria.core.download.wrapper.DownloadTaskWrapper;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.orm.DbEntity;
import java.io.File;
@ -28,17 +28,17 @@ import java.util.List;
* Created by Aria.Lao on 2017/11/1.
* 任务实体工厂
*/
class DTEntityFactory implements ITEntityFactory<DownloadEntity, DownloadTaskEntity> {
private static final String TAG = "DTEntityFactory";
private static volatile DTEntityFactory INSTANCE = null;
class DTEFactory implements INormalTEFactory<DownloadEntity, DownloadTaskEntity> {
private static final String TAG = "DTEFactory";
private static volatile DTEFactory INSTANCE = null;
private DTEntityFactory() {
private DTEFactory() {
}
public static DTEntityFactory getInstance() {
public static DTEFactory getInstance() {
if (INSTANCE == null) {
synchronized (DTEntityFactory.class) {
INSTANCE = new DTEntityFactory();
synchronized (DTEFactory.class) {
INSTANCE = new DTEFactory();
}
}
return INSTANCE;
@ -51,23 +51,22 @@ class DTEntityFactory implements ITEntityFactory<DownloadEntity, DownloadTaskEnt
List<DownloadTaskWrapper> wrapper = DbEntity.findRelationData(DownloadTaskWrapper.class,
"DownloadTaskEntity.key=? and DownloadTaskEntity.isGroupTask='false' and DownloadTaskEntity.url=?",
entity.getDownloadPath(), entity.getUrl());
DownloadTaskEntity taskEntity;
if (wrapper != null && !wrapper.isEmpty()) {
DownloadTaskEntity taskEntity = wrapper.get(0).taskEntity;
taskEntity = wrapper.get(0).taskEntity;
if (taskEntity == null) {
taskEntity = new DownloadTaskEntity();
taskEntity.entity = entity;
} else if (taskEntity.entity == null || TextUtils.isEmpty(taskEntity.entity.getUrl())) {
taskEntity.entity = entity;
}
return taskEntity;
} else {
DownloadTaskEntity taskEntity = new DownloadTaskEntity();
taskEntity = new DownloadTaskEntity();
}
taskEntity.key = entity.getDownloadPath();
taskEntity.url = entity.getUrl();
taskEntity.entity = entity;
return taskEntity;
}
}
/**
* 通过下载地址创建任务实体
@ -89,6 +88,7 @@ class DTEntityFactory implements ITEntityFactory<DownloadEntity, DownloadTaskEnt
entity = new DownloadEntity();
entity.setUrl(downloadUrl);
entity.setGroupChild(false);
entity.setGroupName(null);
}
File file = new File(entity.getDownloadPath());
if (!file.exists()) {

@ -23,10 +23,24 @@ import java.util.List;
* 任务组通过组创建任务
* Created by Aria.Lao on 2017/11/1.
*/
interface IGTEntityFactory<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity<ENTITY>> {
interface IGTEFactory<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity<ENTITY>> {
/**
* 通过key创建任务
* 获取任务组的任务实体
* 1创建实体和任务实体之间的关联
* 2如果在数据库中查找不到对应的数据则新创建任务实体
*
* @param groupName 任务组名
* @param urls 子任务的下载地址
*/
TASK_ENTITY getGTE(String groupName, List<String> urls);
/**
* 获取FTP文件夹的任务实体该方法需要以下操作
* 1创建实体和任务实体之间的关联
* 2如果在数据库中查找不到对应的数据则新创建任务实体
*
* @param ftpUrl ftp文件夹下载路径
*/
TASK_ENTITY create(String groupName, List<String> urls);
TASK_ENTITY getFTE(String ftpUrl);
}

@ -21,7 +21,7 @@ import com.arialyy.aria.core.inf.AbsTaskEntity;
/**
* Created by Aria.Lao on 2017/11/1.
*/
interface ITEntityFactory<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity<ENTITY>> {
interface INormalTEFactory<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity<ENTITY>> {
/**
* 通过key创建任务只适应于单任务

@ -50,18 +50,17 @@ public class TEManager {
}
/**
* 通过key创建任务只适应于单任务不能用于HTTP任务组可用于Ftp文件夹
* 如果是任务组请使用{@link #createGTEntity(Class, List)}
* 通过key创建任务只适应于单任务
*
* @return 如果任务实体创建失败返回null
*/
public <TE extends AbsTaskEntity> TE createTEntity(Class<TE> clazz, String key) {
private <TE extends AbsTaskEntity> TE createNormalTE(Class<TE> clazz, String key) {
final Lock lock = this.lock;
lock.lock();
try {
AbsTaskEntity tEntity = cache.get(convertKey(key));
if (tEntity == null || tEntity.getClass() != clazz) {
ITEntityFactory factory = chooseFactory(clazz);
INormalTEFactory factory = chooseNormalFactory(clazz);
if (factory == null) {
ALog.e(TAG, "任务实体创建失败");
return null;
@ -76,23 +75,44 @@ public class TEManager {
}
/**
* 创建任务实体
* 通过key创建不需要缓存的任务实体只适应于单任务
*
* @return 如果任务实体创建失败返回null
*/
public <TE extends AbsTaskEntity> TE createGTEntity(Class<TE> clazz, List<String> urls) {
public <TE extends AbsTaskEntity> TE createNormalNoCacheTE(Class<TE> clazz, String key) {
final Lock lock = this.lock;
lock.lock();
try {
INormalTEFactory factory = chooseNormalFactory(clazz);
if (factory == null) {
ALog.e(TAG, "任务实体创建失败");
return null;
}
AbsTaskEntity tEntity = factory.create(key);
return (TE) tEntity;
} finally {
lock.unlock();
}
}
/**
* 创建任务组
*
* @return 如果任务实体创建失败返回null
*/
private <TE extends AbsTaskEntity> TE createGTEntity(Class<TE> clazz, List<String> urls) {
final Lock lock = this.lock;
lock.lock();
try {
String groupName = CommonUtil.getMd5Code(urls);
AbsTaskEntity tEntity = cache.get(convertKey(groupName));
if (tEntity == null || tEntity.getClass() != clazz) {
IGTEntityFactory factory = chooseGroupFactory(clazz);
IGTEFactory factory = chooseGroupFactory(clazz);
if (factory == null) {
ALog.e(TAG, "任务实体创建失败");
return null;
}
tEntity = factory.create(groupName, urls);
tEntity = factory.getGTE(groupName, urls);
cache.put(convertKey(groupName), tEntity);
}
return (TE) tEntity;
@ -101,26 +121,49 @@ public class TEManager {
}
}
private IGTEntityFactory chooseGroupFactory(Class clazz) {
/**
* 通过ftp文件夹路径创建FTP文件夹实体
*
* @return 如果任务实体创建失败返回null
*/
private <TE extends AbsTaskEntity> TE createFDTE(Class<TE> clazz, String key) {
final Lock lock = this.lock;
lock.lock();
try {
AbsTaskEntity tEntity = cache.get(convertKey(key));
if (tEntity == null || tEntity.getClass() != clazz) {
IGTEFactory factory = chooseGroupFactory(clazz);
if (factory == null) {
ALog.e(TAG, "任务实体创建失败");
return null;
}
tEntity = factory.getFTE(key);
cache.put(convertKey(key), tEntity);
}
return (TE) tEntity;
} finally {
lock.unlock();
}
}
private IGTEFactory chooseGroupFactory(Class clazz) {
if (clazz == DownloadGroupTaskEntity.class) {
return DGTEntityFactory.getInstance();
return DGTEFactory.getInstance();
}
return null;
}
private ITEntityFactory chooseFactory(Class clazz) {
private INormalTEFactory chooseNormalFactory(Class clazz) {
if (clazz == DownloadTaskEntity.class) {
return DTEntityFactory.getInstance();
return DTEFactory.getInstance();
} else if (clazz == UploadTaskEntity.class) {
return UTEntityFactory.getInstance();
} else if (clazz == DownloadGroupTaskEntity.class) {
return DGTEntityFactory.getInstance();
return UTEFactory.getInstance();
}
return null;
}
/**
* 从缓存中获取任务实体如果任务实体不存在则创建任务实体
* 从缓存中获取任务实体如果任务实体不存在则创建任务实体
*
* @return 创建失败返回null
*/
@ -130,7 +173,27 @@ public class TEManager {
try {
AbsTaskEntity tEntity = cache.get(convertKey(key));
if (tEntity == null) {
return createTEntity(clazz, key);
return createNormalTE(clazz, key);
} else {
return (TE) tEntity;
}
} finally {
lock.unlock();
}
}
/**
* 从缓存中获取FTP文件夹任务实体如果任务实体不存在则创建任务实体
*
* @return 创建失败返回null
*/
public <TE extends AbsTaskEntity> TE getFDTEntity(Class<TE> clazz, String key) {
final Lock lock = this.lock;
lock.lock();
try {
AbsTaskEntity tEntity = cache.get(convertKey(key));
if (tEntity == null) {
return createFDTE(clazz, key);
} else {
return (TE) tEntity;
}

@ -28,17 +28,17 @@ import java.util.regex.Pattern;
* Created by Aria.Lao on 2017/11/1.
* 任务实体工厂
*/
class UTEntityFactory implements ITEntityFactory<UploadEntity, UploadTaskEntity> {
private static final String TAG = "DTEntityFactory";
private static volatile UTEntityFactory INSTANCE = null;
class UTEFactory implements INormalTEFactory<UploadEntity, UploadTaskEntity> {
private static final String TAG = "DTEFactory";
private static volatile UTEFactory INSTANCE = null;
private UTEntityFactory() {
private UTEFactory() {
}
public static UTEntityFactory getInstance() {
public static UTEFactory getInstance() {
if (INSTANCE == null) {
synchronized (UTEntityFactory.class) {
INSTANCE = new UTEntityFactory();
synchronized (UTEFactory.class) {
INSTANCE = new UTEFactory();
}
}
return INSTANCE;

@ -69,7 +69,8 @@ abstract class BaseNormalTarget<TARGET extends AbsUploadTarget>
@Override protected boolean checkEntity() {
boolean b = checkUrl() && checkFilePath();
if (b) {
mTaskEntity.save(mEntity);
mEntity.save();
mTaskEntity.save();
}
return b;
}

@ -140,7 +140,7 @@ class BaseUListener<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity<
mEntity.setState(state);
mEntity.setComplete(state == IEntity.STATE_COMPLETE);
if (state == IEntity.STATE_CANCEL) {
mTaskEntity.deleteData();
mEntity.deleteData();
return;
} else if (mEntity.isComplete()) {
mEntity.setCompleteTime(System.currentTimeMillis());

@ -16,13 +16,7 @@
package com.arialyy.aria.orm;
import android.support.annotation.NonNull;
import com.arialyy.aria.util.CommonUtil;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Created by lyy on 2015/11/2.
@ -30,7 +24,7 @@ import java.util.Map;
*/
public abstract class DbEntity {
private static final Object LOCK = new Object();
protected int rowID = -1;
protected long rowID = -1;
protected DbEntity() {
@ -41,7 +35,8 @@ public abstract class DbEntity {
*
* @param expression 查询条件
*/
public static <T extends AbsWrapper> List<T> findRelationData(Class<T> clazz, String... expression) {
public static <T extends AbsWrapper> List<T> findRelationData(Class<T> clazz,
String... expression) {
return DelegateWrapper.getInstance().findRelationData(clazz, expression);
}
@ -114,20 +109,6 @@ public abstract class DbEntity {
return datas == null ? null : datas.size() > 0 ? datas.get(0) : null;
}
/**
* 获取所有行的rowid
*/
public int[] getRowIds() {
return DelegateWrapper.getInstance().getRowId(getClass());
}
/**
* 获取rowid
*/
public int getRowId(@NonNull Object[] wheres, @NonNull Object[] values) {
return DelegateWrapper.getInstance().getRowId(getClass(), wheres, values);
}
/**
* 删除当前数据
*/
@ -155,6 +136,7 @@ public abstract class DbEntity {
/**
* 保存自身如果表中已经有数据则更新数据否则插入数据
* 只有 target中checkEntity成功后才能保存创建实体部分也不允许保存
*/
public void save() {
synchronized (LOCK) {
@ -171,50 +153,13 @@ public abstract class DbEntity {
*/
private boolean thisIsExist() {
DelegateWrapper util = DelegateWrapper.getInstance();
return util.isExist(getClass(), rowID);
return rowID != -1 && util.isExist(getClass(), rowID);
}
/**
* 插入数据
* 插入数据只有 target中checkEntity成功后才能插入创建实体部分也不允许操作
*/
public void insert() {
DelegateWrapper.getInstance().insertData(this);
updateRowID();
}
private void updateRowID() {
try {
List<Field> fields = CommonUtil.getAllFields(getClass());
StringBuilder sb = new StringBuilder();
List<String> params = new ArrayList<>();
List<String> values = new ArrayList<>();
for (Field field : fields) {
field.setAccessible(true);
if (SqlUtil.isIgnore(field)) {
continue;
}
sb.append(field.getName()).append("=? AND ");
Type type = field.getType();
if (type == List.class) {
values.add(SqlUtil.list2Str(this, field));
} else if (type == Map.class) {
values.add(SqlUtil.map2Str((Map<String, String>) field.get(this)));
} else {
values.add(field.get(this) + "");
}
}
String temp = sb.toString();
String p = temp.substring(0, temp.length() - 5);
params.add(p);
params.addAll(values);
DbEntity entity = findFirst(getClass(), params.toArray(new String[params.size()]));
if (entity != null) {
rowID = entity.rowID;
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}

@ -500,7 +500,7 @@ class DelegateFind extends AbsDelegate {
* 通过rowId判断数据是否存在
*/
<T extends DbEntity> boolean itemExist(SQLiteDatabase db, Class<T> clazz,
int rowId) {
long rowId) {
db = checkDb(db);
String sql = "SELECT rowid FROM " + CommonUtil.getClassName(clazz) + " WHERE rowid=" + rowId;
print(ROW_ID, sql);

@ -15,9 +15,11 @@
*/
package com.arialyy.aria.orm;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import com.arialyy.aria.orm.annotation.Primary;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
import java.lang.reflect.Field;
@ -61,10 +63,7 @@ class DelegateUpdate extends AbsDelegate {
List<Field> fields = CommonUtil.getAllFields(clazz);
DbEntity cacheEntity = mDataCache.get(getCacheKey(dbEntity));
if (fields != null && fields.size() > 0) {
StringBuilder sql = new StringBuilder();
StringBuilder prams = new StringBuilder();
sql.append("UPDATE ").append(CommonUtil.getClassName(dbEntity)).append(" SET ");
int i = 0;
ContentValues values = new ContentValues();
try {
for (Field field : fields) {
field.setAccessible(true);
@ -76,10 +75,7 @@ class DelegateUpdate extends AbsDelegate {
&& !field.getName().equals("state")) { //在LruCache中 state字段总是不能重新赋值...
continue;
}
String value;
prams.append(i > 0 ? ", " : "");
prams.append(field.getName()).append("='");
Type type = field.getType();
if (type == Map.class && checkMap(field)) {
value = SqlUtil.map2Str((Map<String, String>) field.get(dbEntity));
@ -89,19 +85,17 @@ class DelegateUpdate extends AbsDelegate {
Object obj = field.get(dbEntity);
value = obj == null ? "" : convertValue(obj.toString());
}
prams.append(TextUtils.isEmpty(value) ? "" : value);
prams.append("'");
i++;
values.put(field.getName(), value);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if (!TextUtils.isEmpty(prams.toString())) {
sql.append(prams.toString());
sql.append(" where rowid=").append(dbEntity.rowID);
print(MODIFY_DATA, sql.toString());
db.execSQL(sql.toString());
//print(MODIFY_DATA, sql.toString());
if (values.size() > 0) {
db.update(CommonUtil.getClassName(dbEntity), values, "rowid=?",
new String[] { String.valueOf(dbEntity.rowID) });
} else {
ALog.d(TAG, "没有数据更新");
}
}
mDataCache.put(getCacheKey(dbEntity), dbEntity);
@ -116,48 +110,32 @@ class DelegateUpdate extends AbsDelegate {
Class<?> clazz = dbEntity.getClass();
List<Field> fields = CommonUtil.getAllFields(clazz);
if (fields != null && fields.size() > 0) {
StringBuilder sb = new StringBuilder();
sb.append("INSERT INTO ").append(CommonUtil.getClassName(dbEntity)).append("(");
int i = 0;
ContentValues values = new ContentValues();
try {
for (Field field : fields) {
field.setAccessible(true);
if (isIgnore(dbEntity, field)) {
continue;
}
sb.append(i > 0 ? ", " : "");
sb.append(field.getName());
i++;
}
sb.append(") VALUES (");
i = 0;
for (Field field : fields) {
field.setAccessible(true);
if (isIgnore(dbEntity, field)) {
continue;
}
sb.append(i > 0 ? ", " : "");
sb.append("'");
String value = null;
Type type = field.getType();
if (type == Map.class && checkMap(field)) {
sb.append(SqlUtil.map2Str((Map<String, String>) field.get(dbEntity)));
value = SqlUtil.map2Str((Map<String, String>) field.get(dbEntity));
} else if (type == List.class && checkList(field)) {
sb.append(SqlUtil.list2Str(dbEntity, field));
value = SqlUtil.list2Str(dbEntity, field);
} else {
Object obj = field.get(dbEntity);
if (obj != null) {
sb.append(convertValue(field.get(dbEntity).toString()));
value = convertValue(field.get(dbEntity).toString());
}
}
sb.append("'");
i++;
values.put(field.getName(), value);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
sb.append(")");
print(INSERT_DATA, sb.toString());
db.execSQL(sb.toString());
//print(INSERT_DATA, );
dbEntity.rowID = db.insert(CommonUtil.getClassName(dbEntity), null, values);
}
close(db);
}
@ -169,18 +147,33 @@ class DelegateUpdate extends AbsDelegate {
if (SqlUtil.isIgnore(field)) {
return true;
}
if (SqlUtil.isPrimary(field)) { //忽略自动增长的主键
Primary p = field.getAnnotation(Primary.class);
if (p.autoincrement()) {
Object value = field.get(obj);
if (value == null) { // 忽略为空的字段
return true;
}
if (value instanceof String) {
if (TextUtils.isEmpty(String.valueOf(value))) {
return true;
}
if (SqlUtil.isForeign(field)) { //忽略外键为null的字段
Object value = field.get(obj);
if (value == null || (value instanceof String && TextUtils.isEmpty(String.valueOf(value)))) {
}
if (value instanceof List) {
if (((List) value).size() == 0) {
return true;
}
}
if (value instanceof Map) {
if (((Map) value).size() == 0) {
return true;
}
}
if (SqlUtil.isPrimary(field)) { //忽略自动增长的主键
Primary p = field.getAnnotation(Primary.class);
if (p.autoincrement()) {
return true;
}
}
return false;
}
}

@ -121,7 +121,7 @@ public class DelegateWrapper {
/**
* 通过rowId判断数据是否存在
*/
<T extends DbEntity> boolean isExist(Class<T> clazz, int rowId) {
<T extends DbEntity> boolean isExist(Class<T> clazz, long rowId) {
return mDManager.getDelegate(DelegateFind.class).itemExist(mDb, clazz, rowId);
}

@ -36,7 +36,7 @@ final class SqlHelper extends SQLiteOpenHelper {
/**
* 是否将数据库保存在Sd卡{@code true}
*/
private static final boolean SAVE_IN_SDCARD = false;
private static final boolean SAVE_IN_SDCARD = true;
static volatile SqlHelper INSTANCE = null;

@ -34,4 +34,11 @@ import java.lang.annotation.Target;
* 父表在子表对应的字段
*/
String entityColumn();
///**
// * 是否是一对一关系
// *
// * @return {@code true} 是,{@code false} 不是
// */
//boolean isOne2One() default false;
}

@ -19,6 +19,12 @@ package com.arialyy.aria.util;
import android.text.TextUtils;
import android.util.Log;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by Aria.Lao on 2017/10/25.
@ -61,6 +67,50 @@ public class ALog {
return println(Log.ERROR, tag, getExceptionString(e));
}
/**
* 打印MApdebug级别日志
*/
public static void m(String tag, Map map) {
if (LOG_LEVEL <= Log.DEBUG) {
Set set = map.entrySet();
if (set.size() < 1) {
d(tag, "[]");
return;
}
int i = 0;
String[] s = new String[set.size()];
for (Object aSet : set) {
Map.Entry entry = (Map.Entry) aSet;
s[i] = entry.getKey() + " = " + entry.getValue() + ",\n";
i++;
}
println(Log.DEBUG, tag, Arrays.toString(s));
}
}
/**
* 打印JSONdebug级别日志
*/
public static void j(String tag, String jsonStr) {
if (LOG_LEVEL <= Log.DEBUG) {
String message;
try {
if (jsonStr.startsWith("{")) {
JSONObject jsonObject = new JSONObject(jsonStr);
message = jsonObject.toString(4); //这个是核心方法
} else if (jsonStr.startsWith("[")) {
JSONArray jsonArray = new JSONArray(jsonStr);
message = jsonArray.toString(4);
} else {
message = jsonStr;
}
} catch (JSONException e) {
message = jsonStr;
}
println(Log.DEBUG, tag, message);
}
}
/**
* 将异常信息转换为字符串
*/

@ -318,7 +318,7 @@ public class CommonUtil {
return;
}
for (DownloadEntity taskEntity : groupEntity.getSubTask()) {
for (DownloadEntity taskEntity : groupEntity.getSubEntities()) {
delDownloadTaskConfig(removeFile, taskEntity);
}
@ -767,7 +767,7 @@ public class CommonUtil {
return;
}
File file = new File(path);
if (!file.getParentFile().exists()) {
if (file.getParentFile() == null || !file.getParentFile().exists()) {
ALog.d(TAG, "目标文件所在路径不存在,准备创建……");
if (!createDir(file.getParent())) {
ALog.d(TAG, "创建目录文件所在的目录失败!文件路径【" + path + "】");

@ -14,8 +14,10 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar">
<!--android:name=".MainActivity"-->
<!--android:name=".test.TestActivity"-->
<activity
android:name=".MainActivity"
android:name=".test.TestGroupActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

@ -46,7 +46,6 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
//"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://58.210.9.131/tpk/sipgt//TDLYZTGH.tpk"; //chunked 下载
//"https://static.donguo.me//video/ip/course/pfys_1.mp4";
@Bind(R.id.start) Button mStart;

@ -48,7 +48,7 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
DownloadGroupTaskEntity entity = Aria.download(this).getGroupTask(mUrls);
if (entity != null && entity.getEntity() != null) {
DownloadGroupEntity groupEntity = entity.getEntity();
mChildList.addData(groupEntity.getSubTask());
mChildList.addData(groupEntity.getSubEntities());
getBinding().setFileSize(groupEntity.getConvertFileSize());
if (groupEntity.getFileSize() == 0) {
getBinding().setProgress(0);
@ -116,12 +116,12 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
@DownloadGroup.onTaskPre() protected void onTaskPre(DownloadGroupTask task) {
if (mChildList.getSubData().size() <= 0) {
mChildList.addData(task.getEntity().getSubTask());
mChildList.addData(task.getEntity().getSubEntities());
}
L.d(TAG, "group task pre");
getBinding().setFileSize(task.getConvertFileSize());
if (mChildList.getSubData().size() <= 0) {
mChildList.addData(task.getEntity().getSubTask());
mChildList.addData(task.getEntity().getSubEntities());
}
}
@ -132,7 +132,7 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
@DownloadGroup.onTaskRunning() protected void running(DownloadGroupTask task) {
getBinding().setProgress(task.getPercent());
getBinding().setSpeed(task.getConvertSpeed());
mChildList.updateChildProgress(task.getEntity().getSubTask());
mChildList.updateChildProgress(task.getEntity().getSubEntities());
}
@DownloadGroup.onTaskResume() void taskResume(DownloadGroupTask task) {
@ -157,7 +157,7 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
@DownloadGroup.onTaskComplete() void taskComplete(DownloadGroupTask task) {
getBinding().setProgress(100);
getBinding().setSpeed("");
mChildList.updateChildProgress(task.getEntity().getSubTask());
mChildList.updateChildProgress(task.getEntity().getSubEntities());
T.showShort(this, "任务组下载完成");
L.d(TAG, "任务组下载完成");
}

@ -46,7 +46,7 @@ public class FTPDirDownloadActivity extends BaseActivity<ActivityDownloadGroupBi
DownloadGroupTaskEntity entity = Aria.download(this).getFtpDirTask(dir);
if (entity != null && entity.getEntity() != null) {
DownloadGroupEntity groupEntity = entity.getEntity();
mChildList.addData(groupEntity.getSubTask());
mChildList.addData(groupEntity.getSubEntities());
getBinding().setFileSize(groupEntity.getConvertFileSize());
if (groupEntity.getFileSize() == 0) {
getBinding().setProgress(0);
@ -88,7 +88,7 @@ public class FTPDirDownloadActivity extends BaseActivity<ActivityDownloadGroupBi
@DownloadGroup.onTaskPre() protected void onTaskPre(DownloadGroupTask task) {
if (mChildList.getSubData().size() <= 0) {
mChildList.addData(task.getEntity().getSubTask());
mChildList.addData(task.getEntity().getSubEntities());
}
L.d(TAG, "group task pre");
getBinding().setFileSize(task.getConvertFileSize());
@ -101,7 +101,7 @@ public class FTPDirDownloadActivity extends BaseActivity<ActivityDownloadGroupBi
@DownloadGroup.onTaskRunning() protected void running(DownloadGroupTask task) {
getBinding().setProgress(task.getPercent());
getBinding().setSpeed(task.getConvertSpeed());
mChildList.updateChildProgress(task.getEntity().getSubTask());
mChildList.updateChildProgress(task.getEntity().getSubEntities());
}
@DownloadGroup.onTaskResume() void taskResume(DownloadGroupTask task) {
@ -124,7 +124,7 @@ public class FTPDirDownloadActivity extends BaseActivity<ActivityDownloadGroupBi
@DownloadGroup.onTaskComplete() void taskComplete(DownloadGroupTask task) {
getBinding().setProgress(100);
mChildList.updateChildProgress(task.getEntity().getSubTask());
mChildList.updateChildProgress(task.getEntity().getSubEntities());
T.showShort(this, "任务组下载完成");
L.d(TAG, "任务组下载完成");
}

@ -30,7 +30,7 @@ public class GroupModule extends BaseModule {
super(context);
}
List<String> getUrls() {
public List<String> getUrls() {
List<String> urls = new ArrayList<>();
String[] str = getContext().getResources().getStringArray(R.array.group_urls_1);
Collections.addAll(urls, str);

@ -217,9 +217,9 @@ public class DownloadAdapter extends AbsRVAdapter<AbsEntity, DownloadAdapter.Sim
private void handleSubChild(GroupHolder holder, final AbsEntity entity) {
if (holder.childList.getSubData().size() > 0) {
holder.childList.updateChildProgress(((DownloadGroupEntity) entity).getSubTask());
holder.childList.updateChildProgress(((DownloadGroupEntity) entity).getSubEntities());
} else {
holder.childList.addData(((DownloadGroupEntity) entity).getSubTask());
holder.childList.addData(((DownloadGroupEntity) entity).getSubEntities());
}
}

@ -0,0 +1,92 @@
package com.arialyy.simple.test;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import com.arialyy.annotations.Download;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.common.RequestEnum;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.frame.util.show.L;
import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.ActivityTestBinding;
import java.io.File;
/**
* Created by Administrator on 2018/4/12.
*/
public class TestActivity extends BaseActivity<ActivityTestBinding> {
String TAG = "TestActivity";
String URL = "http://58.210.9.131/tpk/sipgt//TDLYZTGH.tpk"; //chunked 下载
@Override protected int setLayoutId() {
return R.layout.activity_test;
}
@Override protected void init(Bundle savedInstanceState) {
super.init(savedInstanceState);
mBar.setVisibility(View.GONE);
Aria.download(this).register();
}
@Download.onWait void onWait(DownloadTask task) {
Log.d(TAG, "wait ==> " + task.getDownloadEntity().getFileName());
}
@Download.onPre protected void onPre(DownloadTask task) {
Log.d(TAG, "onPre");
}
@Download.onTaskStart void taskStart(DownloadTask task) {
Log.d(TAG, "onStart");
}
@Download.onTaskRunning protected void running(DownloadTask task) {
Log.d(TAG, "running");
}
@Download.onTaskResume void taskResume(DownloadTask task) {
Log.d(TAG, "resume");
}
@Download.onTaskStop void taskStop(DownloadTask task) {
Log.d(TAG, "stop");
}
@Download.onTaskCancel void taskCancel(DownloadTask task) {
Log.d(TAG, "cancel");
}
@Download.onTaskFail void taskFail(DownloadTask task) {
Log.d(TAG, "fail");
}
@Download.onTaskComplete void taskComplete(DownloadTask task) {
L.d(TAG, "path ==> " + task.getDownloadEntity().getDownloadPath());
L.d(TAG, "md5Code ==> " + CommonUtil.getFileMD5(new File(task.getDownloadPath())));
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.start:
Aria.download(this)
.load(URL)
.addHeader("Accept-Encoding", "gzip, deflate")
.setRequestMode(RequestEnum.GET)
.setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg1.apk")
.resetState()
.start();
break;
case R.id.stop:
Aria.download(this).load(URL).stop();
break;
case R.id.cancel:
Aria.download(this).load(URL).cancel();
break;
}
}
}

@ -0,0 +1,91 @@
package com.arialyy.simple.test;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import com.arialyy.annotations.DownloadGroup;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadGroupTask;
import com.arialyy.frame.util.show.L;
import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.ActivityTestBinding;
import com.arialyy.simple.download.group.GroupModule;
import java.util.List;
/**
* Created by Administrator on 2018/4/12.
*/
public class TestGroupActivity extends BaseActivity<ActivityTestBinding> {
List<String> mUrls;
@Override protected int setLayoutId() {
return R.layout.activity_test;
}
@Override protected void init(Bundle savedInstanceState) {
super.init(savedInstanceState);
mBar.setVisibility(View.GONE);
Aria.download(this).register();
mUrls = getModule(GroupModule.class).getUrls();
}
@DownloadGroup.onWait void taskWait(DownloadGroupTask task) {
L.d(TAG, task.getTaskName() + "wait");
}
@DownloadGroup.onPre() protected void onPre(DownloadGroupTask task) {
L.d(TAG, "group pre");
}
@DownloadGroup.onTaskPre() protected void onTaskPre(DownloadGroupTask task) {
L.d(TAG, "group task pre");
}
@DownloadGroup.onTaskStart() void taskStart(DownloadGroupTask task) {
L.d(TAG, "group task start");
}
@DownloadGroup.onTaskRunning() protected void running(DownloadGroupTask task) {
L.d(TAG, "group task running");
}
@DownloadGroup.onTaskResume() void taskResume(DownloadGroupTask task) {
L.d(TAG, "group task resume");
}
@DownloadGroup.onTaskStop() void taskStop(DownloadGroupTask task) {
L.d(TAG, "group task stop");
}
@DownloadGroup.onTaskCancel() void taskCancel(DownloadGroupTask task) {
L.d(TAG, "group task cancel");
}
@DownloadGroup.onTaskFail() void taskFail(DownloadGroupTask task) {
L.d(TAG, "group task fail");
}
@DownloadGroup.onTaskComplete() void taskComplete(DownloadGroupTask task) {
L.d(TAG, "group task complete");
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.start:
Aria.download(this)
.loadGroup(mUrls)
.setDirPath(Environment.getExternalStorageDirectory().getPath() + "/download/test/")
.resetState()
.start();
break;
case R.id.stop:
Aria.download(this).loadGroup(mUrls).stop();
break;
case R.id.cancel:
Aria.download(this).loadGroup(mUrls).cancel();
break;
}
}
}

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="start"
/>
<Button
android:id="@+id/stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="stop"
/>
<Button
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="del"
/>
</LinearLayout>
</layout>

@ -16,6 +16,7 @@
package com.arialyy.aria.core.upload;
import com.arialyy.aria.core.inf.AbsNormalTaskEntity;
import com.arialyy.aria.orm.ActionPolicy;
import com.arialyy.aria.orm.annotation.Foreign;
import com.arialyy.aria.orm.annotation.Ignore;
import com.arialyy.aria.orm.annotation.Primary;
@ -31,13 +32,13 @@ public class UploadTaskEntity extends AbsNormalTaskEntity<UploadEntity> {
public String contentType = "multipart/form-data"; //上传的文件类型
public String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)";
//@OneToOne(table = UploadEntity.class, key = "filePath") public UploadEntity entity;
@Ignore public UploadEntity entity;
public String filePath = "";
@Primary @Foreign(parent = UploadEntity.class, column = "filePath")
@Primary
@Foreign(parent = UploadEntity.class, column = "filePath",
onUpdate = ActionPolicy.CASCADE, onDelete = ActionPolicy.CASCADE)
public String key;
/**
@ -55,14 +56,4 @@ public class UploadTaskEntity extends AbsNormalTaskEntity<UploadEntity> {
@Override public String getKey() {
return key;
}
public void save(UploadEntity entity) {
if (entity != null) {
filePath = entity.getFilePath();
key = entity.getKey();
this.entity = entity;
entity.save();
}
save();
}
}

Loading…
Cancel
Save